2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include "isl_equalities.h"
12 #include "isl_map_private.h"
16 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
18 isl_int *t = bmap->eq[a];
19 bmap->eq[a] = bmap->eq[b];
23 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
26 isl_int *t = bmap->ineq[a];
27 bmap->ineq[a] = bmap->ineq[b];
32 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
34 swap_inequality((struct isl_basic_map *)bset, a, b);
37 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
39 isl_seq_cpy(c, c + n, rem);
40 isl_seq_clr(c + rem, n);
43 /* Drop n dimensions starting at first.
45 * In principle, this frees up some extra variables as the number
46 * of columns remains constant, but we would have to extend
47 * the div array too as the number of rows in this array is assumed
48 * to be equal to extra.
50 struct isl_basic_set *isl_basic_set_drop_dims(
51 struct isl_basic_set *bset, unsigned first, unsigned n)
58 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
63 bset = isl_basic_set_cow(bset);
67 for (i = 0; i < bset->n_eq; ++i)
68 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
69 (bset->dim->n_out-first-n)+bset->extra);
71 for (i = 0; i < bset->n_ineq; ++i)
72 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
73 (bset->dim->n_out-first-n)+bset->extra);
75 for (i = 0; i < bset->n_div; ++i)
76 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
77 (bset->dim->n_out-first-n)+bset->extra);
79 bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
83 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
84 bset = isl_basic_set_simplify(bset);
85 return isl_basic_set_finalize(bset);
87 isl_basic_set_free(bset);
91 struct isl_set *isl_set_drop_dims(
92 struct isl_set *set, unsigned first, unsigned n)
99 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
103 set = isl_set_cow(set);
106 set->dim = isl_dim_drop_outputs(set->dim, first, n);
110 for (i = 0; i < set->n; ++i) {
111 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
116 ISL_F_CLR(set, ISL_SET_NORMALIZED);
123 /* Move "n" divs starting at "first" to the end of the list of divs.
125 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
126 unsigned first, unsigned n)
131 if (first + n == bmap->n_div)
134 div = isl_alloc_array(bmap->ctx, isl_int *, n);
137 for (i = 0; i < n; ++i)
138 div[i] = bmap->div[first + i];
139 for (i = 0; i < bmap->n_div - first - n; ++i)
140 bmap->div[first + i] = bmap->div[first + n + i];
141 for (i = 0; i < n; ++i)
142 bmap->div[bmap->n_div - n + i] = div[i];
146 isl_basic_map_free(bmap);
150 /* Drop "n" dimensions of type "type" starting at "first".
152 * In principle, this frees up some extra variables as the number
153 * of columns remains constant, but we would have to extend
154 * the div array too as the number of rows in this array is assumed
155 * to be equal to extra.
157 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
158 enum isl_dim_type type, unsigned first, unsigned n)
168 dim = isl_basic_map_dim(bmap, type);
169 isl_assert(bmap->ctx, first + n <= dim, goto error);
174 bmap = isl_basic_map_cow(bmap);
178 offset = isl_basic_map_offset(bmap, type) + first;
179 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
180 for (i = 0; i < bmap->n_eq; ++i)
181 constraint_drop_vars(bmap->eq[i]+offset, n, left);
183 for (i = 0; i < bmap->n_ineq; ++i)
184 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
186 for (i = 0; i < bmap->n_div; ++i)
187 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
189 if (type == isl_dim_div) {
190 bmap = move_divs_last(bmap, first, n);
193 isl_basic_map_free_div(bmap, n);
195 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
199 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
200 bmap = isl_basic_map_simplify(bmap);
201 return isl_basic_map_finalize(bmap);
203 isl_basic_map_free(bmap);
207 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
208 enum isl_dim_type type, unsigned first, unsigned n)
210 return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset,
214 struct isl_basic_map *isl_basic_map_drop_inputs(
215 struct isl_basic_map *bmap, unsigned first, unsigned n)
217 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
220 struct isl_map *isl_map_drop(struct isl_map *map,
221 enum isl_dim_type type, unsigned first, unsigned n)
228 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
232 map = isl_map_cow(map);
235 map->dim = isl_dim_drop(map->dim, type, first, n);
239 for (i = 0; i < map->n; ++i) {
240 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
244 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
252 struct isl_set *isl_set_drop(struct isl_set *set,
253 enum isl_dim_type type, unsigned first, unsigned n)
255 return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
258 struct isl_map *isl_map_drop_inputs(
259 struct isl_map *map, unsigned first, unsigned n)
261 return isl_map_drop(map, isl_dim_in, first, n);
265 * We don't cow, as the div is assumed to be redundant.
267 static struct isl_basic_map *isl_basic_map_drop_div(
268 struct isl_basic_map *bmap, unsigned div)
276 pos = 1 + isl_dim_total(bmap->dim) + div;
278 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
280 for (i = 0; i < bmap->n_eq; ++i)
281 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
283 for (i = 0; i < bmap->n_ineq; ++i) {
284 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
285 isl_basic_map_drop_inequality(bmap, i);
289 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
292 for (i = 0; i < bmap->n_div; ++i)
293 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
295 if (div != bmap->n_div - 1) {
297 isl_int *t = bmap->div[div];
299 for (j = div; j < bmap->n_div - 1; ++j)
300 bmap->div[j] = bmap->div[j+1];
302 bmap->div[bmap->n_div - 1] = t;
304 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
305 isl_basic_map_free_div(bmap, 1);
309 isl_basic_map_free(bmap);
313 struct isl_basic_map *isl_basic_map_normalize_constraints(
314 struct isl_basic_map *bmap)
318 unsigned total = isl_basic_map_total_dim(bmap);
324 for (i = bmap->n_eq - 1; i >= 0; --i) {
325 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
326 if (isl_int_is_zero(gcd)) {
327 if (!isl_int_is_zero(bmap->eq[i][0])) {
328 bmap = isl_basic_map_set_to_empty(bmap);
331 isl_basic_map_drop_equality(bmap, i);
334 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
335 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
336 if (isl_int_is_one(gcd))
338 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
339 bmap = isl_basic_map_set_to_empty(bmap);
342 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
345 for (i = bmap->n_ineq - 1; i >= 0; --i) {
346 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
347 if (isl_int_is_zero(gcd)) {
348 if (isl_int_is_neg(bmap->ineq[i][0])) {
349 bmap = isl_basic_map_set_to_empty(bmap);
352 isl_basic_map_drop_inequality(bmap, i);
355 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
356 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
357 if (isl_int_is_one(gcd))
359 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
360 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
367 struct isl_basic_set *isl_basic_set_normalize_constraints(
368 struct isl_basic_set *bset)
370 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
371 (struct isl_basic_map *)bset);
374 /* Assumes divs have been ordered if keep_divs is set.
376 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
377 unsigned pos, isl_int *eq, int keep_divs, int *progress)
383 total = isl_basic_map_total_dim(bmap);
384 last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
386 for (k = 0; k < bmap->n_eq; ++k) {
387 if (bmap->eq[k] == eq)
389 if (isl_int_is_zero(bmap->eq[k][1+pos]))
393 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
396 for (k = 0; k < bmap->n_ineq; ++k) {
397 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
401 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
402 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
405 for (k = 0; k < bmap->n_div; ++k) {
406 if (isl_int_is_zero(bmap->div[k][0]))
408 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
412 /* We need to be careful about circular definitions,
413 * so for now we just remove the definition of div k
414 * if the equality contains any divs.
415 * If keep_divs is set, then the divs have been ordered
416 * and we can keep the definition as long as the result
419 if (last_div == -1 || (keep_divs && last_div < k))
420 isl_seq_elim(bmap->div[k]+1, eq,
421 1+pos, 1+total, &bmap->div[k][0]);
423 isl_seq_clr(bmap->div[k], 1 + total);
424 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
428 /* Assumes divs have been ordered if keep_divs is set.
430 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
431 unsigned div, int keep_divs)
433 unsigned pos = isl_dim_total(bmap->dim) + div;
435 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
437 isl_basic_map_drop_div(bmap, div);
440 /* Check if elimination of div "div" using equality "eq" would not
441 * result in a div depending on a later div.
443 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
448 unsigned pos = isl_dim_total(bmap->dim) + div;
450 last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
452 if (last_div < 0 || last_div <= div)
455 for (k = 0; k <= last_div; ++k) {
456 if (isl_int_is_zero(bmap->div[k][0]))
458 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
465 /* Elimininate divs based on equalities
467 static struct isl_basic_map *eliminate_divs_eq(
468 struct isl_basic_map *bmap, int *progress)
475 bmap = isl_basic_map_order_divs(bmap);
480 off = 1 + isl_dim_total(bmap->dim);
482 for (d = bmap->n_div - 1; d >= 0 ; --d) {
483 for (i = 0; i < bmap->n_eq; ++i) {
484 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
485 !isl_int_is_negone(bmap->eq[i][off + d]))
487 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
491 eliminate_div(bmap, bmap->eq[i], d, 1);
492 isl_basic_map_drop_equality(bmap, i);
497 return eliminate_divs_eq(bmap, progress);
501 /* Elimininate divs based on inequalities
503 static struct isl_basic_map *eliminate_divs_ineq(
504 struct isl_basic_map *bmap, int *progress)
515 off = 1 + isl_dim_total(bmap->dim);
517 for (d = bmap->n_div - 1; d >= 0 ; --d) {
518 for (i = 0; i < bmap->n_eq; ++i)
519 if (!isl_int_is_zero(bmap->eq[i][off + d]))
523 for (i = 0; i < bmap->n_ineq; ++i)
524 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
526 if (i < bmap->n_ineq)
529 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
530 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
532 bmap = isl_basic_map_drop_div(bmap, d);
539 struct isl_basic_map *isl_basic_map_gauss(
540 struct isl_basic_map *bmap, int *progress)
548 bmap = isl_basic_map_order_divs(bmap);
553 total = isl_basic_map_total_dim(bmap);
554 total_var = total - bmap->n_div;
556 last_var = total - 1;
557 for (done = 0; done < bmap->n_eq; ++done) {
558 for (; last_var >= 0; --last_var) {
559 for (k = done; k < bmap->n_eq; ++k)
560 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
568 swap_equality(bmap, k, done);
569 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
570 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
572 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
575 if (last_var >= total_var &&
576 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
577 unsigned div = last_var - total_var;
578 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
579 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
580 isl_int_set(bmap->div[div][0],
581 bmap->eq[done][1+last_var]);
582 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
585 if (done == bmap->n_eq)
587 for (k = done; k < bmap->n_eq; ++k) {
588 if (isl_int_is_zero(bmap->eq[k][0]))
590 return isl_basic_map_set_to_empty(bmap);
592 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
596 struct isl_basic_set *isl_basic_set_gauss(
597 struct isl_basic_set *bset, int *progress)
599 return (struct isl_basic_set*)isl_basic_map_gauss(
600 (struct isl_basic_map *)bset, progress);
604 static unsigned int round_up(unsigned int v)
615 static int hash_index(isl_int ***index, unsigned int size, int bits,
616 struct isl_basic_map *bmap, int k)
619 unsigned total = isl_basic_map_total_dim(bmap);
620 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
621 for (h = hash; index[h]; h = (h+1) % size)
622 if (&bmap->ineq[k] != index[h] &&
623 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
628 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
629 struct isl_basic_set *bset, int k)
631 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
634 /* If we can eliminate more than one div, then we need to make
635 * sure we do it from last div to first div, in order not to
636 * change the position of the other divs that still need to
639 static struct isl_basic_map *remove_duplicate_divs(
640 struct isl_basic_map *bmap, int *progress)
648 unsigned total_var = isl_dim_total(bmap->dim);
649 unsigned total = total_var + bmap->n_div;
652 if (bmap->n_div <= 1)
656 for (k = bmap->n_div - 1; k >= 0; --k)
657 if (!isl_int_is_zero(bmap->div[k][0]))
662 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
663 size = round_up(4 * bmap->n_div / 3 - 1);
664 bits = ffs(size) - 1;
665 index = isl_calloc_array(ctx, int, size);
668 eq = isl_blk_alloc(ctx, 1+total);
669 if (isl_blk_is_error(eq))
672 isl_seq_clr(eq.data, 1+total);
673 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
674 for (--k; k >= 0; --k) {
677 if (isl_int_is_zero(bmap->div[k][0]))
680 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
681 for (h = hash; index[h]; h = (h+1) % size)
682 if (isl_seq_eq(bmap->div[k],
683 bmap->div[index[h]-1], 2+total))
692 for (l = bmap->n_div - 1; l >= 0; --l) {
696 isl_int_set_si(eq.data[1+total_var+k], -1);
697 isl_int_set_si(eq.data[1+total_var+l], 1);
698 eliminate_div(bmap, eq.data, l, 0);
699 isl_int_set_si(eq.data[1+total_var+k], 0);
700 isl_int_set_si(eq.data[1+total_var+l], 0);
703 isl_blk_free(ctx, eq);
710 static int n_pure_div_eq(struct isl_basic_map *bmap)
715 total = isl_dim_total(bmap->dim);
716 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
717 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
721 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
727 /* Normalize divs that appear in equalities.
729 * In particular, we assume that bmap contains some equalities
734 * and we want to replace the set of e_i by a minimal set and
735 * such that the new e_i have a canonical representation in terms
737 * If any of the equalities involves more than one divs, then
738 * we currently simply bail out.
740 * Let us first additionally assume that all equalities involve
741 * a div. The equalities then express modulo constraints on the
742 * remaining variables and we can use "parameter compression"
743 * to find a minimal set of constraints. The result is a transformation
745 * x = T(x') = x_0 + G x'
747 * with G a lower-triangular matrix with all elements below the diagonal
748 * non-negative and smaller than the diagonal element on the same row.
749 * We first normalize x_0 by making the same property hold in the affine
751 * The rows i of G with a 1 on the diagonal do not impose any modulo
752 * constraint and simply express x_i = x'_i.
753 * For each of the remaining rows i, we introduce a div and a corresponding
754 * equality. In particular
756 * g_ii e_j = x_i - g_i(x')
758 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
759 * corresponding div (if g_kk != 1).
761 * If there are any equalities not involving any div, then we
762 * first apply a variable compression on the variables x:
764 * x = C x'' x'' = C_2 x
766 * and perform the above parameter compression on A C instead of on A.
767 * The resulting compression is then of the form
769 * x'' = T(x') = x_0 + G x'
771 * and in constructing the new divs and the corresponding equalities,
772 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
773 * by the corresponding row from C_2.
775 static struct isl_basic_map *normalize_divs(
776 struct isl_basic_map *bmap, int *progress)
783 struct isl_mat *T = NULL;
784 struct isl_mat *C = NULL;
785 struct isl_mat *C2 = NULL;
793 if (bmap->n_div == 0)
799 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
802 total = isl_dim_total(bmap->dim);
803 div_eq = n_pure_div_eq(bmap);
807 if (div_eq < bmap->n_eq) {
808 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
809 bmap->n_eq - div_eq, 0, 1 + total);
810 C = isl_mat_variable_compression(B, &C2);
814 bmap = isl_basic_map_set_to_empty(bmap);
821 d = isl_vec_alloc(bmap->ctx, div_eq);
824 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
825 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
827 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
829 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
832 B = isl_mat_product(B, C);
836 T = isl_mat_parameter_compression(B, d);
840 bmap = isl_basic_map_set_to_empty(bmap);
846 for (i = 0; i < T->n_row - 1; ++i) {
847 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
848 if (isl_int_is_zero(v))
850 isl_mat_col_submul(T, 0, v, 1 + i);
853 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
854 /* We have to be careful because dropping equalities may reorder them */
856 for (j = bmap->n_div - 1; j >= 0; --j) {
857 for (i = 0; i < bmap->n_eq; ++i)
858 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
860 if (i < bmap->n_eq) {
861 bmap = isl_basic_map_drop_div(bmap, j);
862 isl_basic_map_drop_equality(bmap, i);
868 for (i = 1; i < T->n_row; ++i) {
869 if (isl_int_is_one(T->row[i][i]))
874 if (needed > dropped) {
875 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
880 for (i = 1; i < T->n_row; ++i) {
881 if (isl_int_is_one(T->row[i][i]))
883 k = isl_basic_map_alloc_div(bmap);
884 pos[i] = 1 + total + k;
885 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
886 isl_int_set(bmap->div[k][0], T->row[i][i]);
888 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
890 isl_int_set_si(bmap->div[k][1 + i], 1);
891 for (j = 0; j < i; ++j) {
892 if (isl_int_is_zero(T->row[i][j]))
894 if (pos[j] < T->n_row && C2)
895 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
896 C2->row[pos[j]], 1 + total);
898 isl_int_neg(bmap->div[k][1 + pos[j]],
901 j = isl_basic_map_alloc_equality(bmap);
902 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
903 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
912 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
922 static struct isl_basic_map *set_div_from_lower_bound(
923 struct isl_basic_map *bmap, int div, int ineq)
925 unsigned total = 1 + isl_dim_total(bmap->dim);
927 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
928 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
929 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
930 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
931 isl_int_set_si(bmap->div[div][1 + total + div], 0);
936 /* Check whether it is ok to define a div based on an inequality.
937 * To avoid the introduction of circular definitions of divs, we
938 * do not allow such a definition if the resulting expression would refer to
939 * any other undefined divs or if any known div is defined in
940 * terms of the unknown div.
942 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
946 unsigned total = 1 + isl_dim_total(bmap->dim);
948 /* Not defined in terms of unknown divs */
949 for (j = 0; j < bmap->n_div; ++j) {
952 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
954 if (isl_int_is_zero(bmap->div[j][0]))
958 /* No other div defined in terms of this one => avoid loops */
959 for (j = 0; j < bmap->n_div; ++j) {
962 if (isl_int_is_zero(bmap->div[j][0]))
964 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
971 /* Given two constraints "k" and "l" that are opposite to each other,
972 * except for the constant term, check if we can use them
973 * to obtain an expression for one of the hitherto unknown divs.
974 * "sum" is the sum of the constant terms of the constraints.
975 * If this sum is strictly smaller than the coefficient of one
976 * of the divs, then this pair can be used define the div.
977 * To avoid the introduction of circular definitions of divs, we
978 * do not use the pair if the resulting expression would refer to
979 * any other undefined divs or if any known div is defined in
980 * terms of the unknown div.
982 static struct isl_basic_map *check_for_div_constraints(
983 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
986 unsigned total = 1 + isl_dim_total(bmap->dim);
988 for (i = 0; i < bmap->n_div; ++i) {
989 if (!isl_int_is_zero(bmap->div[i][0]))
991 if (isl_int_is_zero(bmap->ineq[k][total + i]))
993 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
995 if (!ok_to_set_div_from_bound(bmap, i, k))
997 if (isl_int_is_pos(bmap->ineq[k][total + i]))
998 bmap = set_div_from_lower_bound(bmap, i, k);
1000 bmap = set_div_from_lower_bound(bmap, i, l);
1008 static struct isl_basic_map *remove_duplicate_constraints(
1009 struct isl_basic_map *bmap, int *progress)
1015 unsigned total = isl_basic_map_total_dim(bmap);
1018 if (bmap->n_ineq <= 1)
1021 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1022 bits = ffs(size) - 1;
1023 index = isl_calloc_array(ctx, isl_int **, size);
1027 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
1028 for (k = 1; k < bmap->n_ineq; ++k) {
1029 h = hash_index(index, size, bits, bmap, k);
1031 index[h] = &bmap->ineq[k];
1036 l = index[h] - &bmap->ineq[0];
1037 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1038 swap_inequality(bmap, k, l);
1039 isl_basic_map_drop_inequality(bmap, k);
1043 for (k = 0; k < bmap->n_ineq-1; ++k) {
1044 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1045 h = hash_index(index, size, bits, bmap, k);
1046 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1049 l = index[h] - &bmap->ineq[0];
1050 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1051 if (isl_int_is_pos(sum)) {
1052 bmap = check_for_div_constraints(bmap, k, l, sum,
1056 if (isl_int_is_zero(sum)) {
1057 /* We need to break out of the loop after these
1058 * changes since the contents of the hash
1059 * will no longer be valid.
1060 * Plus, we probably we want to regauss first.
1064 isl_basic_map_drop_inequality(bmap, l);
1065 isl_basic_map_inequality_to_equality(bmap, k);
1067 bmap = isl_basic_map_set_to_empty(bmap);
1077 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1084 bmap = isl_basic_map_normalize_constraints(bmap);
1085 bmap = remove_duplicate_divs(bmap, &progress);
1086 bmap = eliminate_divs_eq(bmap, &progress);
1087 bmap = eliminate_divs_ineq(bmap, &progress);
1088 bmap = isl_basic_map_gauss(bmap, &progress);
1089 /* requires equalities in normal form */
1090 bmap = normalize_divs(bmap, &progress);
1091 bmap = remove_duplicate_constraints(bmap, &progress);
1096 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1098 return (struct isl_basic_set *)
1099 isl_basic_map_simplify((struct isl_basic_map *)bset);
1103 /* If the only constraints a div d=floor(f/m)
1104 * appears in are its two defining constraints
1107 * -(f - (m - 1)) + m d >= 0
1109 * then it can safely be removed.
1111 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1114 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
1116 for (i = 0; i < bmap->n_eq; ++i)
1117 if (!isl_int_is_zero(bmap->eq[i][pos]))
1120 for (i = 0; i < bmap->n_ineq; ++i) {
1121 if (isl_int_is_zero(bmap->ineq[i][pos]))
1123 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1125 isl_int_sub(bmap->div[div][1],
1126 bmap->div[div][1], bmap->div[div][0]);
1127 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1128 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
1129 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1130 isl_int_add(bmap->div[div][1],
1131 bmap->div[div][1], bmap->div[div][0]);
1134 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1135 bmap->n_div-div-1) != -1)
1137 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1138 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
1140 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1141 bmap->n_div-div-1) != -1)
1147 for (i = 0; i < bmap->n_div; ++i)
1148 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1155 * Remove divs that don't occur in any of the constraints or other divs.
1156 * These can arise when dropping some of the variables in a quast
1157 * returned by piplib.
1159 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1166 for (i = bmap->n_div-1; i >= 0; --i) {
1167 if (!div_is_redundant(bmap, i))
1169 bmap = isl_basic_map_drop_div(bmap, i);
1174 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1176 bmap = remove_redundant_divs(bmap);
1179 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1183 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1185 return (struct isl_basic_set *)
1186 isl_basic_map_finalize((struct isl_basic_map *)bset);
1189 struct isl_set *isl_set_finalize(struct isl_set *set)
1195 for (i = 0; i < set->n; ++i) {
1196 set->p[i] = isl_basic_set_finalize(set->p[i]);
1206 struct isl_map *isl_map_finalize(struct isl_map *map)
1212 for (i = 0; i < map->n; ++i) {
1213 map->p[i] = isl_basic_map_finalize(map->p[i]);
1217 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1225 /* Remove definition of any div that is defined in terms of the given variable.
1226 * The div itself is not removed. Functions such as
1227 * eliminate_divs_ineq depend on the other divs remaining in place.
1229 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1234 for (i = 0; i < bmap->n_div; ++i) {
1235 if (isl_int_is_zero(bmap->div[i][0]))
1237 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1239 isl_int_set_si(bmap->div[i][0], 0);
1244 /* Eliminate the specified variables from the constraints using
1245 * Fourier-Motzkin. The variables themselves are not removed.
1247 struct isl_basic_map *isl_basic_map_eliminate_vars(
1248 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1258 total = isl_basic_map_total_dim(bmap);
1260 bmap = isl_basic_map_cow(bmap);
1261 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1262 bmap = remove_dependent_vars(bmap, d);
1264 for (d = pos + n - 1;
1265 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1266 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1267 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1268 int n_lower, n_upper;
1271 for (i = 0; i < bmap->n_eq; ++i) {
1272 if (isl_int_is_zero(bmap->eq[i][1+d]))
1274 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1275 isl_basic_map_drop_equality(bmap, i);
1282 for (i = 0; i < bmap->n_ineq; ++i) {
1283 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1285 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1288 bmap = isl_basic_map_extend_constraints(bmap,
1289 0, n_lower * n_upper);
1292 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1294 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1297 for (j = 0; j < i; ++j) {
1298 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1301 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1302 isl_int_sgn(bmap->ineq[j][1+d]))
1304 k = isl_basic_map_alloc_inequality(bmap);
1307 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1309 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1310 1+d, 1+total, NULL);
1312 isl_basic_map_drop_inequality(bmap, i);
1315 if (n_lower > 0 && n_upper > 0) {
1316 bmap = isl_basic_map_normalize_constraints(bmap);
1317 bmap = remove_duplicate_constraints(bmap, NULL);
1318 bmap = isl_basic_map_gauss(bmap, NULL);
1319 bmap = isl_basic_map_convex_hull(bmap);
1322 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1326 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1329 isl_basic_map_free(bmap);
1333 struct isl_basic_set *isl_basic_set_eliminate_vars(
1334 struct isl_basic_set *bset, unsigned pos, unsigned n)
1336 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1337 (struct isl_basic_map *)bset, pos, n);
1340 /* Don't assume equalities are in order, because align_divs
1341 * may have changed the order of the divs.
1343 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1348 total = isl_dim_total(bmap->dim);
1349 for (d = 0; d < total; ++d)
1351 for (i = 0; i < bmap->n_eq; ++i) {
1352 for (d = total - 1; d >= 0; --d) {
1353 if (isl_int_is_zero(bmap->eq[i][1+d]))
1361 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1363 compute_elimination_index((struct isl_basic_map *)bset, elim);
1366 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1367 struct isl_basic_map *bmap, int *elim)
1373 total = isl_dim_total(bmap->dim);
1374 for (d = total - 1; d >= 0; --d) {
1375 if (isl_int_is_zero(src[1+d]))
1380 isl_seq_cpy(dst, src, 1 + total);
1383 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1388 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1389 struct isl_basic_set *bset, int *elim)
1391 return reduced_using_equalities(dst, src,
1392 (struct isl_basic_map *)bset, elim);
1395 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1396 struct isl_basic_set *bset, struct isl_basic_set *context)
1401 if (!bset || !context)
1404 if (context->n_eq == 0) {
1405 isl_basic_set_free(context);
1409 bset = isl_basic_set_cow(bset);
1413 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1416 set_compute_elimination_index(context, elim);
1417 for (i = 0; i < bset->n_eq; ++i)
1418 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1420 for (i = 0; i < bset->n_ineq; ++i)
1421 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1423 isl_basic_set_free(context);
1425 bset = isl_basic_set_simplify(bset);
1426 bset = isl_basic_set_finalize(bset);
1429 isl_basic_set_free(bset);
1430 isl_basic_set_free(context);
1434 static struct isl_basic_set *remove_shifted_constraints(
1435 struct isl_basic_set *bset, struct isl_basic_set *context)
1445 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1446 bits = ffs(size) - 1;
1447 index = isl_calloc_array(ctx, isl_int **, size);
1451 for (k = 0; k < context->n_ineq; ++k) {
1452 h = set_hash_index(index, size, bits, context, k);
1453 index[h] = &context->ineq[k];
1455 for (k = 0; k < bset->n_ineq; ++k) {
1456 h = set_hash_index(index, size, bits, bset, k);
1459 l = index[h] - &context->ineq[0];
1460 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1462 bset = isl_basic_set_cow(bset);
1465 isl_basic_set_drop_inequality(bset, k);
1475 /* Tighten (decrease) the constant terms of the inequalities based
1476 * on the equalities, without removing any integer points.
1477 * For example, if there is an equality
1485 * then we want to replace the inequality by
1489 * We do this by computing a variable compression and translating
1490 * the constraints to the compressed space.
1491 * If any constraint has coefficients (except the contant term)
1492 * with a common factor "f", then we can replace the constant term "c"
1499 * f * floor(c/f) - c = -fract(c/f)
1501 * and we can add the same value to the original constraint.
1503 * In the example, the compressed space only contains "j",
1504 * and the inequality translates to
1508 * We add -fract(-1/3) = -2 to the original constraint to obtain
1512 static struct isl_basic_set *normalize_constraints_in_compressed_space(
1513 struct isl_basic_set *bset)
1517 struct isl_mat *B, *C;
1523 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1529 bset = isl_basic_set_cow(bset);
1533 total = isl_basic_set_total_dim(bset);
1534 B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
1535 C = isl_mat_variable_compression(B, NULL);
1538 if (C->n_col == 0) {
1540 return isl_basic_set_set_to_empty(bset);
1542 B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
1543 0, bset->n_ineq, 0, 1 + total);
1544 C = isl_mat_product(B, C);
1549 for (i = 0; i < bset->n_ineq; ++i) {
1550 isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd);
1551 if (isl_int_is_one(gcd))
1553 isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd);
1554 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]);
1563 /* Remove all information from bset that is redundant in the context
1564 * of context. Both bset and context are assumed to be full-dimensional.
1566 * We first * remove the inequalities from "bset"
1567 * that are obviously redundant with respect to some inequality in "context".
1569 * If there are any inequalities left, we construct a tableau for
1570 * the context and then add the inequalities of "bset".
1571 * Before adding these inequalities, we freeze all constraints such that
1572 * they won't be considered redundant in terms of the constraints of "bset".
1573 * Then we detect all redundant constraints (among the
1574 * constraints that weren't frozen), first by checking for redundancy in the
1575 * the tableau and then by checking if replacing a constraint by its negation
1576 * would lead to an empty set. This last step is fairly expensive
1577 * and could be optimized by more reuse of the tableau.
1578 * Finally, we update bset according to the results.
1580 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
1581 __isl_take isl_basic_set *context)
1584 isl_basic_set *combined = NULL;
1585 struct isl_tab *tab = NULL;
1586 unsigned context_ineq;
1589 if (!bset || !context)
1592 if (isl_basic_set_is_universe(bset)) {
1593 isl_basic_set_free(context);
1597 if (isl_basic_set_is_universe(context)) {
1598 isl_basic_set_free(context);
1602 bset = remove_shifted_constraints(bset, context);
1605 if (bset->n_ineq == 0)
1608 context_ineq = context->n_ineq;
1609 combined = isl_basic_set_cow(isl_basic_set_copy(context));
1610 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
1611 tab = isl_tab_from_basic_set(combined);
1612 for (i = 0; i < context_ineq; ++i)
1613 if (isl_tab_freeze_constraint(tab, i) < 0)
1615 tab = isl_tab_extend(tab, bset->n_ineq);
1616 for (i = 0; i < bset->n_ineq; ++i)
1617 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
1619 bset = isl_basic_set_add_constraints(combined, bset, 0);
1623 if (isl_tab_detect_redundant(tab) < 0)
1625 total = isl_basic_set_total_dim(bset);
1626 for (i = context_ineq; i < bset->n_ineq; ++i) {
1628 if (tab->con[i].is_redundant)
1630 tab->con[i].is_redundant = 1;
1631 combined = isl_basic_set_dup(bset);
1632 combined = isl_basic_set_update_from_tab(combined, tab);
1633 combined = isl_basic_set_extend_constraints(combined, 0, 1);
1634 k = isl_basic_set_alloc_inequality(combined);
1637 isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total);
1638 isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1);
1639 is_empty = isl_basic_set_is_empty(combined);
1642 isl_basic_set_free(combined);
1645 tab->con[i].is_redundant = 0;
1647 for (i = 0; i < context_ineq; ++i)
1648 tab->con[i].is_redundant = 1;
1649 bset = isl_basic_set_update_from_tab(bset, tab);
1651 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1652 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1657 bset = isl_basic_set_simplify(bset);
1658 bset = isl_basic_set_finalize(bset);
1659 isl_basic_set_free(context);
1663 isl_basic_set_free(combined);
1664 isl_basic_set_free(context);
1665 isl_basic_set_free(bset);
1669 /* Remove all information from bset that is redundant in the context
1670 * of context. In particular, equalities that are linear combinations
1671 * of those in context are removed. Then the inequalities that are
1672 * redundant in the context of the equalities and inequalities of
1673 * context are removed.
1675 * We first compute the integer affine hull of the intersection,
1676 * compute the gist inside this affine hull and then add back
1677 * those equalities that are not implied by the context.
1679 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
1680 __isl_take isl_basic_set *context)
1685 isl_basic_set *aff_context;
1688 if (!bset || !context)
1691 bset = isl_basic_set_intersect(bset, isl_basic_set_copy(context));
1692 if (isl_basic_set_fast_is_empty(bset)) {
1693 isl_basic_set_free(context);
1696 aff = isl_basic_set_affine_hull(isl_basic_set_copy(bset));
1699 if (isl_basic_set_fast_is_empty(aff)) {
1700 isl_basic_set_free(aff);
1701 isl_basic_set_free(context);
1704 if (aff->n_eq == 0) {
1705 isl_basic_set_free(aff);
1706 return uset_gist_full(bset, context);
1708 total = isl_basic_set_total_dim(bset);
1709 eq = isl_mat_sub_alloc(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
1710 eq = isl_mat_cow(eq);
1711 T = isl_mat_variable_compression(eq, &T2);
1712 if (T && T->n_col == 0) {
1715 isl_basic_set_free(context);
1716 isl_basic_set_free(aff);
1717 return isl_basic_set_set_to_empty(bset);
1720 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
1722 bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
1723 context = isl_basic_set_preimage(context, T);
1725 bset = uset_gist_full(bset, context);
1726 bset = isl_basic_set_preimage(bset, T2);
1727 bset = isl_basic_set_intersect(bset, aff);
1728 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
1731 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1732 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1737 isl_basic_set_free(bset);
1738 isl_basic_set_free(context);
1742 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1743 * We simply add the equalities in context to bmap and then do a regular
1744 * div normalizations. Better results can be obtained by normalizing
1745 * only the divs in bmap than do not also appear in context.
1746 * We need to be careful to reduce the divs using the equalities
1747 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1748 * spurious constraints.
1750 static struct isl_basic_map *normalize_divs_in_context(
1751 struct isl_basic_map *bmap, struct isl_basic_map *context)
1754 unsigned total_context;
1757 div_eq = n_pure_div_eq(bmap);
1761 if (context->n_div > 0)
1762 bmap = isl_basic_map_align_divs(bmap, context);
1764 total_context = isl_basic_map_total_dim(context);
1765 bmap = isl_basic_map_extend_constraints(bmap, context->n_eq, 0);
1766 for (i = 0; i < context->n_eq; ++i) {
1768 k = isl_basic_map_alloc_equality(bmap);
1769 isl_seq_cpy(bmap->eq[k], context->eq[i], 1 + total_context);
1770 isl_seq_clr(bmap->eq[k] + 1 + total_context,
1771 isl_basic_map_total_dim(bmap) - total_context);
1773 bmap = isl_basic_map_gauss(bmap, NULL);
1774 bmap = normalize_divs(bmap, NULL);
1775 bmap = isl_basic_map_gauss(bmap, NULL);
1779 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1780 struct isl_basic_map *context)
1782 struct isl_basic_set *bset;
1784 if (!bmap || !context)
1787 if (isl_basic_map_is_universe(context)) {
1788 isl_basic_map_free(context);
1791 if (isl_basic_map_is_universe(bmap)) {
1792 isl_basic_map_free(context);
1795 if (isl_basic_map_fast_is_empty(context)) {
1796 struct isl_dim *dim = isl_dim_copy(bmap->dim);
1797 isl_basic_map_free(context);
1798 isl_basic_map_free(bmap);
1799 return isl_basic_map_universe(dim);
1801 if (isl_basic_map_fast_is_empty(bmap)) {
1802 isl_basic_map_free(context);
1806 bmap = isl_basic_map_convex_hull(bmap);
1807 context = isl_basic_map_convex_hull(context);
1810 bmap = normalize_divs_in_context(bmap, context);
1812 context = isl_basic_map_align_divs(context, bmap);
1813 bmap = isl_basic_map_align_divs(bmap, context);
1815 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1816 isl_basic_map_underlying_set(context));
1818 return isl_basic_map_overlying_set(bset, bmap);
1820 isl_basic_map_free(bmap);
1821 isl_basic_map_free(context);
1826 * Assumes context has no implicit divs.
1828 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
1829 __isl_take isl_basic_map *context)
1833 if (!map || !context)
1836 if (isl_basic_map_is_universe(context)) {
1837 isl_basic_map_free(context);
1840 if (isl_basic_map_fast_is_empty(context)) {
1841 struct isl_dim *dim = isl_dim_copy(map->dim);
1842 isl_basic_map_free(context);
1844 return isl_map_universe(dim);
1847 context = isl_basic_map_convex_hull(context);
1848 map = isl_map_cow(map);
1849 if (!map || !context)
1851 isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1852 map = isl_map_compute_divs(map);
1853 for (i = 0; i < map->n; ++i)
1854 context = isl_basic_map_align_divs(context, map->p[i]);
1855 for (i = 0; i < map->n; ++i) {
1856 map->p[i] = isl_basic_map_gist(map->p[i],
1857 isl_basic_map_copy(context));
1861 isl_basic_map_free(context);
1862 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1866 isl_basic_map_free(context);
1870 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
1871 __isl_take isl_map *context)
1873 return isl_map_gist_basic_map(map, isl_map_convex_hull(context));
1876 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1877 struct isl_basic_set *context)
1879 return (struct isl_basic_set *)isl_basic_map_gist(
1880 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1883 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
1884 __isl_take isl_basic_set *context)
1886 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
1887 (struct isl_basic_map *)context);
1890 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
1891 __isl_take isl_set *context)
1893 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1894 (struct isl_map *)context);
1897 /* Quick check to see if two basic maps are disjoint.
1898 * In particular, we reduce the equalities and inequalities of
1899 * one basic map in the context of the equalities of the other
1900 * basic map and check if we get a contradiction.
1902 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1903 struct isl_basic_map *bmap2)
1905 struct isl_vec *v = NULL;
1910 if (!bmap1 || !bmap2)
1912 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1914 if (bmap1->n_div || bmap2->n_div)
1916 if (!bmap1->n_eq && !bmap2->n_eq)
1919 total = isl_dim_total(bmap1->dim);
1922 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1925 elim = isl_alloc_array(bmap1->ctx, int, total);
1928 compute_elimination_index(bmap1, elim);
1929 for (i = 0; i < bmap2->n_eq; ++i) {
1931 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1933 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1934 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1937 for (i = 0; i < bmap2->n_ineq; ++i) {
1939 reduced = reduced_using_equalities(v->block.data,
1940 bmap2->ineq[i], bmap1, elim);
1941 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1942 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1945 compute_elimination_index(bmap2, elim);
1946 for (i = 0; i < bmap1->n_ineq; ++i) {
1948 reduced = reduced_using_equalities(v->block.data,
1949 bmap1->ineq[i], bmap2, elim);
1950 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1951 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1967 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1968 struct isl_basic_set *bset2)
1970 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1971 (struct isl_basic_map *)bset2);
1974 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1981 if (isl_map_fast_is_equal(map1, map2))
1984 for (i = 0; i < map1->n; ++i) {
1985 for (j = 0; j < map2->n; ++j) {
1986 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1995 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1997 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1998 (struct isl_map *)set2);
2001 /* Check if we can combine a given div with lower bound l and upper
2002 * bound u with some other div and if so return that other div.
2003 * Otherwise return -1.
2005 * We first check that
2006 * - the bounds are opposites of each other (except for the constant
2008 * - the bounds do not reference any other div
2009 * - no div is defined in terms of this div
2011 * Let m be the size of the range allowed on the div by the bounds.
2012 * That is, the bounds are of the form
2014 * e <= a <= e + m - 1
2016 * with e some expression in the other variables.
2017 * We look for another div b such that no third div is defined in terms
2018 * of this second div b and such that in any constraint that contains
2019 * a (except for the given lower and upper bound), also contains b
2020 * with a coefficient that is m times that of b.
2021 * That is, all constraints (execpt for the lower and upper bound)
2024 * e + f (a + m b) >= 0
2026 * If so, we return b so that "a + m b" can be replaced by
2027 * a single div "c = a + m b".
2029 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
2030 unsigned div, unsigned l, unsigned u)
2036 if (bmap->n_div <= 1)
2038 dim = isl_dim_total(bmap->dim);
2039 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
2041 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
2042 bmap->n_div - div - 1) != -1)
2044 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
2048 for (i = 0; i < bmap->n_div; ++i) {
2049 if (isl_int_is_zero(bmap->div[i][0]))
2051 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
2055 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2056 if (isl_int_is_neg(bmap->ineq[l][0])) {
2057 isl_int_sub(bmap->ineq[l][0],
2058 bmap->ineq[l][0], bmap->ineq[u][0]);
2059 bmap = isl_basic_map_copy(bmap);
2060 bmap = isl_basic_map_set_to_empty(bmap);
2061 isl_basic_map_free(bmap);
2064 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2065 for (i = 0; i < bmap->n_div; ++i) {
2070 for (j = 0; j < bmap->n_div; ++j) {
2071 if (isl_int_is_zero(bmap->div[j][0]))
2073 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
2076 if (j < bmap->n_div)
2078 for (j = 0; j < bmap->n_ineq; ++j) {
2080 if (j == l || j == u)
2082 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
2084 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
2086 isl_int_mul(bmap->ineq[j][1 + dim + div],
2087 bmap->ineq[j][1 + dim + div],
2089 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
2090 bmap->ineq[j][1 + dim + i]);
2091 isl_int_divexact(bmap->ineq[j][1 + dim + div],
2092 bmap->ineq[j][1 + dim + div],
2097 if (j < bmap->n_ineq)
2102 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2103 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2107 /* Given a lower and an upper bound on div i, construct an inequality
2108 * that when nonnegative ensures that this pair of bounds always allows
2109 * for an integer value of the given div.
2110 * The lower bound is inequality l, while the upper bound is inequality u.
2111 * The constructed inequality is stored in ineq.
2112 * g, fl, fu are temporary scalars.
2114 * Let the upper bound be
2118 * and the lower bound
2122 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2125 * - f_u e_l <= f_u f_l g a <= f_l e_u
2127 * Since all variables are integer valued, this is equivalent to
2129 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2131 * If this interval is at least f_u f_l g, then it contains at least
2132 * one integer value for a.
2133 * That is, the test constraint is
2135 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2137 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
2138 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
2141 dim = isl_dim_total(bmap->dim);
2143 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
2144 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
2145 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
2146 isl_int_neg(fu, fu);
2147 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
2148 1 + dim + bmap->n_div);
2149 isl_int_add(ineq[0], ineq[0], fl);
2150 isl_int_add(ineq[0], ineq[0], fu);
2151 isl_int_sub_ui(ineq[0], ineq[0], 1);
2152 isl_int_mul(g, g, fl);
2153 isl_int_mul(g, g, fu);
2154 isl_int_sub(ineq[0], ineq[0], g);
2157 /* Remove more kinds of divs that are not strictly needed.
2158 * In particular, if all pairs of lower and upper bounds on a div
2159 * are such that they allow at least one integer value of the div,
2160 * the we can eliminate the div using Fourier-Motzkin without
2161 * introducing any spurious solutions.
2163 static struct isl_basic_map *drop_more_redundant_divs(
2164 struct isl_basic_map *bmap, int *pairs, int n)
2166 struct isl_tab *tab = NULL;
2167 struct isl_vec *vec = NULL;
2179 dim = isl_dim_total(bmap->dim);
2180 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
2184 tab = isl_tab_from_basic_map(bmap);
2189 enum isl_lp_result res;
2191 for (i = 0; i < bmap->n_div; ++i) {
2194 if (best >= 0 && pairs[best] <= pairs[i])
2200 for (l = 0; l < bmap->n_ineq; ++l) {
2201 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
2203 for (u = 0; u < bmap->n_ineq; ++u) {
2204 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
2206 construct_test_ineq(bmap, i, l, u,
2207 vec->el, g, fl, fu);
2208 res = isl_tab_min(tab, vec->el,
2209 bmap->ctx->one, &g, NULL, 0);
2210 if (res == isl_lp_error)
2212 if (res == isl_lp_empty) {
2213 bmap = isl_basic_map_set_to_empty(bmap);
2216 if (res != isl_lp_ok || isl_int_is_neg(g))
2219 if (u < bmap->n_ineq)
2222 if (l == bmap->n_ineq) {
2242 bmap = isl_basic_map_remove(bmap, isl_dim_div, remove, 1);
2243 return isl_basic_map_drop_redundant_divs(bmap);
2246 isl_basic_map_free(bmap);
2255 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2256 * and the upper bound u, div1 always occurs together with div2 in the form
2257 * (div1 + m div2), where m is the constant range on the variable div1
2258 * allowed by l and u, replace the pair div1 and div2 by a single
2259 * div that is equal to div1 + m div2.
2261 * The new div will appear in the location that contains div2.
2262 * We need to modify all constraints that contain
2263 * div2 = (div - div1) / m
2264 * (If a constraint does not contain div2, it will also not contain div1.)
2265 * If the constraint also contains div1, then we know they appear
2266 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2267 * i.e., the coefficient of div is f.
2269 * Otherwise, we first need to introduce div1 into the constraint.
2278 * A lower bound on div2
2282 * can be replaced by
2284 * (n * (m div 2 + div1) + m t + n f)/g >= 0
2286 * with g = gcd(m,n).
2291 * can be replaced by
2293 * (-n * (m div2 + div1) + m t + n f')/g >= 0
2295 * These constraint are those that we would obtain from eliminating
2296 * div1 using Fourier-Motzkin.
2298 * After all constraints have been modified, we drop the lower and upper
2299 * bound and then drop div1.
2301 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
2302 unsigned div1, unsigned div2, unsigned l, unsigned u)
2307 unsigned dim, total;
2310 dim = isl_dim_total(bmap->dim);
2311 total = 1 + dim + bmap->n_div;
2316 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
2317 isl_int_add_ui(m, m, 1);
2319 for (i = 0; i < bmap->n_ineq; ++i) {
2320 if (i == l || i == u)
2322 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
2324 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
2325 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
2326 isl_int_divexact(a, m, b);
2327 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
2328 if (isl_int_is_pos(b)) {
2329 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2330 b, bmap->ineq[l], total);
2333 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2334 b, bmap->ineq[u], total);
2337 isl_int_set(bmap->ineq[i][1 + dim + div2],
2338 bmap->ineq[i][1 + dim + div1]);
2339 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
2346 isl_basic_map_drop_inequality(bmap, l);
2347 isl_basic_map_drop_inequality(bmap, u);
2349 isl_basic_map_drop_inequality(bmap, u);
2350 isl_basic_map_drop_inequality(bmap, l);
2352 bmap = isl_basic_map_drop_div(bmap, div1);
2356 /* First check if we can coalesce any pair of divs and
2357 * then continue with dropping more redundant divs.
2359 * We loop over all pairs of lower and upper bounds on a div
2360 * with coefficient 1 and -1, respectively, check if there
2361 * is any other div "c" with which we can coalesce the div
2362 * and if so, perform the coalescing.
2364 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
2365 struct isl_basic_map *bmap, int *pairs, int n)
2370 dim = isl_dim_total(bmap->dim);
2372 for (i = 0; i < bmap->n_div; ++i) {
2375 for (l = 0; l < bmap->n_ineq; ++l) {
2376 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
2378 for (u = 0; u < bmap->n_ineq; ++u) {
2381 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
2383 c = div_find_coalesce(bmap, pairs, i, l, u);
2387 bmap = coalesce_divs(bmap, i, c, l, u);
2388 return isl_basic_map_drop_redundant_divs(bmap);
2393 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
2396 return drop_more_redundant_divs(bmap, pairs, n);
2399 /* Remove divs that are not strictly needed.
2400 * In particular, if a div only occurs positively (or negatively)
2401 * in constraints, then it can simply be dropped.
2402 * Also, if a div occurs only occurs in two constraints and if moreover
2403 * those two constraints are opposite to each other, except for the constant
2404 * term and if the sum of the constant terms is such that for any value
2405 * of the other values, there is always at least one integer value of the
2406 * div, i.e., if one plus this sum is greater than or equal to
2407 * the (absolute value) of the coefficent of the div in the constraints,
2408 * then we can also simply drop the div.
2410 * If any divs are left after these simple checks then we move on
2411 * to more complicated cases in drop_more_redundant_divs.
2413 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
2414 struct isl_basic_map *bmap)
2424 off = isl_dim_total(bmap->dim);
2425 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
2429 for (i = 0; i < bmap->n_div; ++i) {
2431 int last_pos, last_neg;
2435 defined = !isl_int_is_zero(bmap->div[i][0]);
2436 for (j = 0; j < bmap->n_eq; ++j)
2437 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
2443 for (j = 0; j < bmap->n_ineq; ++j) {
2444 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
2448 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
2453 pairs[i] = pos * neg;
2454 if (pairs[i] == 0) {
2455 for (j = bmap->n_ineq - 1; j >= 0; --j)
2456 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
2457 isl_basic_map_drop_inequality(bmap, j);
2458 bmap = isl_basic_map_drop_div(bmap, i);
2460 return isl_basic_map_drop_redundant_divs(bmap);
2464 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
2465 bmap->ineq[last_neg] + 1,
2469 isl_int_add(bmap->ineq[last_pos][0],
2470 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2471 isl_int_add_ui(bmap->ineq[last_pos][0],
2472 bmap->ineq[last_pos][0], 1);
2473 redundant = isl_int_ge(bmap->ineq[last_pos][0],
2474 bmap->ineq[last_pos][1+off+i]);
2475 isl_int_sub_ui(bmap->ineq[last_pos][0],
2476 bmap->ineq[last_pos][0], 1);
2477 isl_int_sub(bmap->ineq[last_pos][0],
2478 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2481 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
2486 bmap = set_div_from_lower_bound(bmap, i, last_pos);
2487 bmap = isl_basic_map_simplify(bmap);
2489 return isl_basic_map_drop_redundant_divs(bmap);
2491 if (last_pos > last_neg) {
2492 isl_basic_map_drop_inequality(bmap, last_pos);
2493 isl_basic_map_drop_inequality(bmap, last_neg);
2495 isl_basic_map_drop_inequality(bmap, last_neg);
2496 isl_basic_map_drop_inequality(bmap, last_pos);
2498 bmap = isl_basic_map_drop_div(bmap, i);
2500 return isl_basic_map_drop_redundant_divs(bmap);
2504 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
2510 isl_basic_map_free(bmap);
2514 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
2515 struct isl_basic_set *bset)
2517 return (struct isl_basic_set *)
2518 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
2521 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
2527 for (i = 0; i < map->n; ++i) {
2528 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
2532 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2539 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
2541 return (struct isl_set *)
2542 isl_map_drop_redundant_divs((struct isl_map *)set);