isl_basic_map_gist: replace by new version based on tableaus
[isl.git] / isl_map_simplify.c
blobca6b58fcb5378342b066ba4edb92b8f18e66e5a3
1 #include "isl_equalities.h"
2 #include "isl_map.h"
3 #include "isl_map_private.h"
4 #include "isl_tab.h"
6 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
8 isl_int *t = bmap->eq[a];
9 bmap->eq[a] = bmap->eq[b];
10 bmap->eq[b] = t;
13 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
15 if (a != b) {
16 isl_int *t = bmap->ineq[a];
17 bmap->ineq[a] = bmap->ineq[b];
18 bmap->ineq[b] = t;
22 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
24 swap_inequality((struct isl_basic_map *)bset, a, b);
27 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
29 isl_seq_cpy(c, c + n, rem);
30 isl_seq_clr(c + rem, n);
33 /* Drop n dimensions starting at first.
35 * In principle, this frees up some extra variables as the number
36 * of columns remains constant, but we would have to extend
37 * the div array too as the number of rows in this array is assumed
38 * to be equal to extra.
40 struct isl_basic_set *isl_basic_set_drop_dims(
41 struct isl_basic_set *bset, unsigned first, unsigned n)
43 int i;
45 if (!bset)
46 goto error;
48 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
50 if (n == 0)
51 return bset;
53 bset = isl_basic_set_cow(bset);
54 if (!bset)
55 return NULL;
57 for (i = 0; i < bset->n_eq; ++i)
58 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
59 (bset->dim->n_out-first-n)+bset->extra);
61 for (i = 0; i < bset->n_ineq; ++i)
62 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
63 (bset->dim->n_out-first-n)+bset->extra);
65 for (i = 0; i < bset->n_div; ++i)
66 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
67 (bset->dim->n_out-first-n)+bset->extra);
69 bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
70 if (!bset->dim)
71 goto error;
73 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
74 bset = isl_basic_set_simplify(bset);
75 return isl_basic_set_finalize(bset);
76 error:
77 isl_basic_set_free(bset);
78 return NULL;
81 struct isl_set *isl_set_drop_dims(
82 struct isl_set *set, unsigned first, unsigned n)
84 int i;
86 if (!set)
87 goto error;
89 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
91 if (n == 0)
92 return set;
93 set = isl_set_cow(set);
94 if (!set)
95 goto error;
96 set->dim = isl_dim_drop_outputs(set->dim, first, n);
97 if (!set->dim)
98 goto error;
100 for (i = 0; i < set->n; ++i) {
101 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
102 if (!set->p[i])
103 goto error;
106 ISL_F_CLR(set, ISL_SET_NORMALIZED);
107 return set;
108 error:
109 isl_set_free(set);
110 return NULL;
113 /* Drop n input dimensions starting at first.
115 * In principle, this frees up some extra variables as the number
116 * of columns remains constant, but we would have to extend
117 * the div array too as the number of rows in this array is assumed
118 * to be equal to extra.
120 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
121 enum isl_dim_type type, unsigned first, unsigned n)
123 int i;
124 unsigned dim;
125 unsigned offset;
126 unsigned left;
128 if (!bmap)
129 goto error;
131 dim = isl_basic_map_dim(bmap, type);
132 isl_assert(bmap->ctx, first + n <= dim, goto error);
134 if (n == 0)
135 return bmap;
137 bmap = isl_basic_map_cow(bmap);
138 if (!bmap)
139 return NULL;
141 offset = isl_basic_map_offset(bmap, type) + first;
142 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
143 for (i = 0; i < bmap->n_eq; ++i)
144 constraint_drop_vars(bmap->eq[i]+offset, n, left);
146 for (i = 0; i < bmap->n_ineq; ++i)
147 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
149 for (i = 0; i < bmap->n_div; ++i)
150 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
152 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
153 if (!bmap->dim)
154 goto error;
156 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
157 bmap = isl_basic_map_simplify(bmap);
158 return isl_basic_map_finalize(bmap);
159 error:
160 isl_basic_map_free(bmap);
161 return NULL;
164 struct isl_basic_map *isl_basic_map_drop_inputs(
165 struct isl_basic_map *bmap, unsigned first, unsigned n)
167 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
170 struct isl_map *isl_map_drop(struct isl_map *map,
171 enum isl_dim_type type, unsigned first, unsigned n)
173 int i;
175 if (!map)
176 goto error;
178 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
180 if (n == 0)
181 return map;
182 map = isl_map_cow(map);
183 if (!map)
184 goto error;
185 map->dim = isl_dim_drop(map->dim, type, first, n);
186 if (!map->dim)
187 goto error;
189 for (i = 0; i < map->n; ++i) {
190 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
191 if (!map->p[i])
192 goto error;
194 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
196 return map;
197 error:
198 isl_map_free(map);
199 return NULL;
202 struct isl_map *isl_map_drop_inputs(
203 struct isl_map *map, unsigned first, unsigned n)
205 return isl_map_drop(map, isl_dim_in, first, n);
209 * We don't cow, as the div is assumed to be redundant.
211 static struct isl_basic_map *isl_basic_map_drop_div(
212 struct isl_basic_map *bmap, unsigned div)
214 int i;
215 unsigned pos;
217 if (!bmap)
218 goto error;
220 pos = 1 + isl_dim_total(bmap->dim) + div;
222 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
224 for (i = 0; i < bmap->n_eq; ++i)
225 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
227 for (i = 0; i < bmap->n_ineq; ++i) {
228 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
229 isl_basic_map_drop_inequality(bmap, i);
230 --i;
231 continue;
233 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
236 for (i = 0; i < bmap->n_div; ++i)
237 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
239 if (div != bmap->n_div - 1) {
240 int j;
241 isl_int *t = bmap->div[div];
243 for (j = div; j < bmap->n_div - 1; ++j)
244 bmap->div[j] = bmap->div[j+1];
246 bmap->div[bmap->n_div - 1] = t;
248 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
249 isl_basic_map_free_div(bmap, 1);
251 return bmap;
252 error:
253 isl_basic_map_free(bmap);
254 return NULL;
257 struct isl_basic_map *isl_basic_map_normalize_constraints(
258 struct isl_basic_map *bmap)
260 int i;
261 isl_int gcd;
262 unsigned total = isl_basic_map_total_dim(bmap);
264 isl_int_init(gcd);
265 for (i = bmap->n_eq - 1; i >= 0; --i) {
266 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
267 if (isl_int_is_zero(gcd)) {
268 if (!isl_int_is_zero(bmap->eq[i][0])) {
269 bmap = isl_basic_map_set_to_empty(bmap);
270 break;
272 isl_basic_map_drop_equality(bmap, i);
273 continue;
275 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
276 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
277 if (isl_int_is_one(gcd))
278 continue;
279 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
280 bmap = isl_basic_map_set_to_empty(bmap);
281 break;
283 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
286 for (i = bmap->n_ineq - 1; i >= 0; --i) {
287 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
288 if (isl_int_is_zero(gcd)) {
289 if (isl_int_is_neg(bmap->ineq[i][0])) {
290 bmap = isl_basic_map_set_to_empty(bmap);
291 break;
293 isl_basic_map_drop_inequality(bmap, i);
294 continue;
296 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
297 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
298 if (isl_int_is_one(gcd))
299 continue;
300 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
301 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
303 isl_int_clear(gcd);
305 return bmap;
308 struct isl_basic_set *isl_basic_set_normalize_constraints(
309 struct isl_basic_set *bset)
311 (struct isl_basic_set *)isl_basic_map_normalize_constraints(
312 (struct isl_basic_map *)bset);
315 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
317 int i;
318 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
319 unsigned len;
320 len = 1 + isl_basic_map_total_dim(bmap);
322 for (i = 0; i < bmap->n_eq; ++i)
323 if (bmap->eq[i] != eq)
324 isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
326 for (i = 0; i < bmap->n_ineq; ++i)
327 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
329 /* We need to be careful about circular definitions,
330 * so for now we just remove the definitions of other divs that
331 * depend on this div and (possibly) recompute them later.
333 for (i = 0; i < bmap->n_div; ++i)
334 if (!isl_int_is_zero(bmap->div[i][0]) &&
335 !isl_int_is_zero(bmap->div[i][1 + pos]))
336 isl_seq_clr(bmap->div[i], 1 + len);
338 isl_basic_map_drop_div(bmap, div);
341 /* Elimininate divs based on equalities
343 static struct isl_basic_map *eliminate_divs_eq(
344 struct isl_basic_map *bmap, int *progress)
346 int d;
347 int i;
348 int modified = 0;
349 unsigned off;
351 if (!bmap)
352 return NULL;
354 off = 1 + isl_dim_total(bmap->dim);
356 for (d = bmap->n_div - 1; d >= 0 ; --d) {
357 for (i = 0; i < bmap->n_eq; ++i) {
358 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
359 !isl_int_is_negone(bmap->eq[i][off + d]))
360 continue;
361 modified = 1;
362 *progress = 1;
363 eliminate_div(bmap, bmap->eq[i], d);
364 isl_basic_map_drop_equality(bmap, i);
365 break;
368 if (modified)
369 return eliminate_divs_eq(bmap, progress);
370 return bmap;
373 /* Elimininate divs based on inequalities
375 static struct isl_basic_map *eliminate_divs_ineq(
376 struct isl_basic_map *bmap, int *progress)
378 int d;
379 int i;
380 unsigned off;
381 struct isl_ctx *ctx;
383 if (!bmap)
384 return NULL;
386 ctx = bmap->ctx;
387 off = 1 + isl_dim_total(bmap->dim);
389 for (d = bmap->n_div - 1; d >= 0 ; --d) {
390 for (i = 0; i < bmap->n_eq; ++i)
391 if (!isl_int_is_zero(bmap->eq[i][off + d]))
392 break;
393 if (i < bmap->n_eq)
394 continue;
395 for (i = 0; i < bmap->n_ineq; ++i)
396 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
397 break;
398 if (i < bmap->n_ineq)
399 continue;
400 *progress = 1;
401 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
402 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
403 break;
404 bmap = isl_basic_map_drop_div(bmap, d);
405 if (!bmap)
406 break;
408 return bmap;
411 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
412 unsigned pos, isl_int *eq, int *progress)
414 unsigned total;
415 int k;
416 int contains_divs;
418 total = isl_basic_map_total_dim(bmap);
419 contains_divs =
420 isl_seq_first_non_zero(eq + 1 + isl_dim_total(bmap->dim),
421 bmap->n_div) != -1;
422 for (k = 0; k < bmap->n_eq; ++k) {
423 if (bmap->eq[k] == eq)
424 continue;
425 if (isl_int_is_zero(bmap->eq[k][1+pos]))
426 continue;
427 if (progress)
428 *progress = 1;
429 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
432 for (k = 0; k < bmap->n_ineq; ++k) {
433 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
434 continue;
435 if (progress)
436 *progress = 1;
437 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
438 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
441 for (k = 0; k < bmap->n_div; ++k) {
442 if (isl_int_is_zero(bmap->div[k][0]))
443 continue;
444 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
445 continue;
446 if (progress)
447 *progress = 1;
448 /* We need to be careful about circular definitions,
449 * so for now we just remove the definition of div k
450 * if the equality contains any divs.
452 if (contains_divs)
453 isl_seq_clr(bmap->div[k], 1 + total);
454 else
455 isl_seq_elim(bmap->div[k]+1, eq,
456 1+pos, 1+total, &bmap->div[k][0]);
457 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
461 struct isl_basic_map *isl_basic_map_gauss(
462 struct isl_basic_map *bmap, int *progress)
464 int k;
465 int done;
466 int last_var;
467 unsigned total_var;
468 unsigned total;
470 if (!bmap)
471 return NULL;
473 total = isl_basic_map_total_dim(bmap);
474 total_var = total - bmap->n_div;
476 last_var = total - 1;
477 for (done = 0; done < bmap->n_eq; ++done) {
478 for (; last_var >= 0; --last_var) {
479 for (k = done; k < bmap->n_eq; ++k)
480 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
481 break;
482 if (k < bmap->n_eq)
483 break;
485 if (last_var < 0)
486 break;
487 if (k != done)
488 swap_equality(bmap, k, done);
489 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
490 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
492 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
493 progress);
495 if (last_var >= total_var &&
496 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
497 unsigned div = last_var - total_var;
498 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
499 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
500 isl_int_set(bmap->div[div][0],
501 bmap->eq[done][1+last_var]);
502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
505 if (done == bmap->n_eq)
506 return bmap;
507 for (k = done; k < bmap->n_eq; ++k) {
508 if (isl_int_is_zero(bmap->eq[k][0]))
509 continue;
510 return isl_basic_map_set_to_empty(bmap);
512 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
513 return bmap;
516 struct isl_basic_set *isl_basic_set_gauss(
517 struct isl_basic_set *bset, int *progress)
519 return (struct isl_basic_set*)isl_basic_map_gauss(
520 (struct isl_basic_map *)bset, progress);
524 static unsigned int round_up(unsigned int v)
526 int old_v = v;
528 while (v) {
529 old_v = v;
530 v ^= v & -v;
532 return old_v << 1;
535 static int hash_index(isl_int ***index, unsigned int size, int bits,
536 struct isl_basic_map *bmap, int k)
538 int h;
539 unsigned total = isl_basic_map_total_dim(bmap);
540 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
541 for (h = hash; index[h]; h = (h+1) % size)
542 if (&bmap->ineq[k] != index[h] &&
543 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
544 break;
545 return h;
548 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
549 struct isl_basic_set *bset, int k)
551 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
554 /* If we can eliminate more than one div, then we need to make
555 * sure we do it from last div to first div, in order not to
556 * change the position of the other divs that still need to
557 * be removed.
559 static struct isl_basic_map *remove_duplicate_divs(
560 struct isl_basic_map *bmap, int *progress)
562 unsigned int size;
563 int *index;
564 int *elim_for;
565 int k, l, h;
566 int bits;
567 struct isl_blk eq;
568 unsigned total_var = isl_dim_total(bmap->dim);
569 unsigned total = total_var + bmap->n_div;
570 struct isl_ctx *ctx;
572 if (bmap->n_div <= 1)
573 return bmap;
575 ctx = bmap->ctx;
576 for (k = bmap->n_div - 1; k >= 0; --k)
577 if (!isl_int_is_zero(bmap->div[k][0]))
578 break;
579 if (k <= 0)
580 return bmap;
582 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
583 size = round_up(4 * bmap->n_div / 3 - 1);
584 bits = ffs(size) - 1;
585 index = isl_calloc_array(ctx, int, size);
586 if (!index)
587 return bmap;
588 eq = isl_blk_alloc(ctx, 1+total);
589 if (isl_blk_is_error(eq))
590 goto out;
592 isl_seq_clr(eq.data, 1+total);
593 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
594 for (--k; k >= 0; --k) {
595 uint32_t hash;
597 if (isl_int_is_zero(bmap->div[k][0]))
598 continue;
600 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
601 for (h = hash; index[h]; h = (h+1) % size)
602 if (isl_seq_eq(bmap->div[k],
603 bmap->div[index[h]-1], 2+total))
604 break;
605 if (index[h]) {
606 *progress = 1;
607 l = index[h] - 1;
608 elim_for[l] = k + 1;
610 index[h] = k+1;
612 for (l = bmap->n_div - 1; l >= 0; --l) {
613 if (!elim_for[l])
614 continue;
615 k = elim_for[l] - 1;
616 isl_int_set_si(eq.data[1+total_var+k], -1);
617 isl_int_set_si(eq.data[1+total_var+l], 1);
618 eliminate_div(bmap, eq.data, l);
619 isl_int_set_si(eq.data[1+total_var+k], 0);
620 isl_int_set_si(eq.data[1+total_var+l], 0);
623 isl_blk_free(ctx, eq);
624 out:
625 free(index);
626 free(elim_for);
627 return bmap;
630 /* Normalize divs that appear in equalities.
632 * In particular, we assume that bmap contains some equalities
633 * of the form
635 * a x = m * e_i
637 * and we want to replace the set of e_i by a minimal set and
638 * such that the new e_i have a canonical representation in terms
639 * of the vector x.
640 * If any of the equalities involves more than one divs, then
641 * we currently simply bail out.
643 * Let us first additionally assume that all equalities involve
644 * a div. The equalities then express modulo constraints on the
645 * remaining variables and we can use "parameter compression"
646 * to find a minimal set of constraints. The result is a transformation
648 * x = T(x') = x_0 + G x'
650 * with G a lower-triangular matrix with all elements below the diagonal
651 * non-negative and smaller than the diagonal element on the same row.
652 * We first normalize x_0 by making the same property hold in the affine
653 * T matrix.
654 * The rows i of G with a 1 on the diagonal do not impose any modulo
655 * constraint and simply express x_i = x'_i.
656 * For each of the remaining rows i, we introduce a div and a corresponding
657 * equality. In particular
659 * g_ii e_j = x_i - g_i(x')
661 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
662 * corresponding div (if g_kk != 1).
664 * If there are any equalities not involving any div, then we
665 * first apply a variable compression on the variables x:
667 * x = C x'' x'' = C_2 x
669 * and perform the above parameter compression on A C instead of on A.
670 * The resulting compression is then of the form
672 * x'' = T(x') = x_0 + G x'
674 * and in constructing the new divs and the corresponding equalities,
675 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
676 * by the corresponding row from C_2.
678 static struct isl_basic_map *normalize_divs(
679 struct isl_basic_map *bmap, int *progress)
681 int i, j, k;
682 int total;
683 int div_eq;
684 struct isl_mat *B;
685 struct isl_vec *d;
686 struct isl_mat *T = NULL;
687 struct isl_mat *C = NULL;
688 struct isl_mat *C2 = NULL;
689 isl_int v;
690 int *pos;
691 int dropped, needed;
693 if (!bmap)
694 return NULL;
696 if (bmap->n_div == 0)
697 return bmap;
699 if (bmap->n_eq == 0)
700 return bmap;
702 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
703 return bmap;
705 total = isl_dim_total(bmap->dim);
706 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
707 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
708 --j;
709 if (j < 0)
710 break;
711 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
712 goto done;
714 div_eq = i;
715 if (div_eq == 0)
716 return bmap;
718 if (div_eq < bmap->n_eq) {
719 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
720 bmap->n_eq - div_eq, 0, 1 + total);
721 C = isl_mat_variable_compression(bmap->ctx, B, &C2);
722 if (!C || !C2)
723 goto error;
724 if (C->n_col == 0) {
725 bmap = isl_basic_map_set_to_empty(bmap);
726 isl_mat_free(bmap->ctx, C);
727 isl_mat_free(bmap->ctx, C2);
728 goto done;
732 d = isl_vec_alloc(bmap->ctx, div_eq);
733 if (!d)
734 goto error;
735 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
736 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
737 --j;
738 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
740 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
742 if (C) {
743 B = isl_mat_product(bmap->ctx, B, C);
744 C = NULL;
747 T = isl_mat_parameter_compression(bmap->ctx, B, d);
748 if (!T)
749 goto error;
750 if (T->n_col == 0) {
751 bmap = isl_basic_map_set_to_empty(bmap);
752 isl_mat_free(bmap->ctx, C2);
753 isl_mat_free(bmap->ctx, T);
754 goto done;
756 isl_int_init(v);
757 for (i = 0; i < T->n_row - 1; ++i) {
758 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
759 if (isl_int_is_zero(v))
760 continue;
761 isl_mat_col_submul(T, 0, v, 1 + i);
763 isl_int_clear(v);
764 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
765 /* We have to be careful because dropping equalities may reorder them */
766 dropped = 0;
767 for (j = bmap->n_div - 1; j >= 0; --j) {
768 for (i = 0; i < bmap->n_eq; ++i)
769 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
770 break;
771 if (i < bmap->n_eq) {
772 bmap = isl_basic_map_drop_div(bmap, j);
773 isl_basic_map_drop_equality(bmap, i);
774 ++dropped;
777 pos[0] = 0;
778 needed = 0;
779 for (i = 1; i < T->n_row; ++i) {
780 if (isl_int_is_one(T->row[i][i]))
781 pos[i] = i;
782 else
783 needed++;
785 if (needed > dropped) {
786 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
787 needed, needed, 0);
788 if (!bmap)
789 goto error;
791 for (i = 1; i < T->n_row; ++i) {
792 if (isl_int_is_one(T->row[i][i]))
793 continue;
794 k = isl_basic_map_alloc_div(bmap);
795 pos[i] = 1 + total + k;
796 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
797 isl_int_set(bmap->div[k][0], T->row[i][i]);
798 if (C2)
799 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
800 else
801 isl_int_set_si(bmap->div[k][1 + i], 1);
802 for (j = 0; j < i; ++j) {
803 if (isl_int_is_zero(T->row[i][j]))
804 continue;
805 if (pos[j] < T->n_row && C2)
806 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
807 C2->row[pos[j]], 1 + total);
808 else
809 isl_int_neg(bmap->div[k][1 + pos[j]],
810 T->row[i][j]);
812 j = isl_basic_map_alloc_equality(bmap);
813 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
814 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
816 free(pos);
817 isl_mat_free(bmap->ctx, C2);
818 isl_mat_free(bmap->ctx, T);
820 *progress = 1;
821 done:
822 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
824 return bmap;
825 error:
826 isl_mat_free(bmap->ctx, C);
827 isl_mat_free(bmap->ctx, C2);
828 isl_mat_free(bmap->ctx, T);
829 return bmap;
832 static struct isl_basic_map *remove_duplicate_constraints(
833 struct isl_basic_map *bmap, int *progress)
835 unsigned int size;
836 isl_int ***index;
837 int k, l, h;
838 int bits;
839 unsigned total = isl_basic_map_total_dim(bmap);
840 isl_int sum;
842 if (bmap->n_ineq <= 1)
843 return bmap;
845 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
846 bits = ffs(size) - 1;
847 index = isl_calloc_array(ctx, isl_int **, size);
848 if (!index)
849 return bmap;
851 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
852 for (k = 1; k < bmap->n_ineq; ++k) {
853 h = hash_index(index, size, bits, bmap, k);
854 if (!index[h]) {
855 index[h] = &bmap->ineq[k];
856 continue;
858 if (progress)
859 *progress = 1;
860 l = index[h] - &bmap->ineq[0];
861 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
862 swap_inequality(bmap, k, l);
863 isl_basic_map_drop_inequality(bmap, k);
864 --k;
866 isl_int_init(sum);
867 for (k = 0; k < bmap->n_ineq-1; ++k) {
868 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
869 h = hash_index(index, size, bits, bmap, k);
870 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
871 if (!index[h])
872 continue;
873 l = index[h] - &bmap->ineq[0];
874 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
875 if (isl_int_is_pos(sum))
876 continue;
877 if (isl_int_is_zero(sum)) {
878 /* We need to break out of the loop after these
879 * changes since the contents of the hash
880 * will no longer be valid.
881 * Plus, we probably we want to regauss first.
883 isl_basic_map_drop_inequality(bmap, l);
884 isl_basic_map_inequality_to_equality(bmap, k);
885 } else
886 bmap = isl_basic_map_set_to_empty(bmap);
887 break;
889 isl_int_clear(sum);
891 free(index);
892 return bmap;
896 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
898 int progress = 1;
899 if (!bmap)
900 return NULL;
901 while (progress) {
902 progress = 0;
903 bmap = isl_basic_map_normalize_constraints(bmap);
904 bmap = eliminate_divs_eq(bmap, &progress);
905 bmap = eliminate_divs_ineq(bmap, &progress);
906 bmap = isl_basic_map_gauss(bmap, &progress);
907 /* requires equalities in normal form */
908 bmap = normalize_divs(bmap, &progress);
909 bmap = remove_duplicate_divs(bmap, &progress);
910 bmap = remove_duplicate_constraints(bmap, &progress);
912 return bmap;
915 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
917 return (struct isl_basic_set *)
918 isl_basic_map_simplify((struct isl_basic_map *)bset);
922 /* If the only constraints a div d=floor(f/m)
923 * appears in are its two defining constraints
925 * f - m d >=0
926 * -(f - (m - 1)) + m d >= 0
928 * then it can safely be removed.
930 static int div_is_redundant(struct isl_basic_map *bmap, int div)
932 int i;
933 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
935 for (i = 0; i < bmap->n_eq; ++i)
936 if (!isl_int_is_zero(bmap->eq[i][pos]))
937 return 0;
939 for (i = 0; i < bmap->n_ineq; ++i) {
940 if (isl_int_is_zero(bmap->ineq[i][pos]))
941 continue;
942 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
943 int neg;
944 isl_int_sub(bmap->div[div][1],
945 bmap->div[div][1], bmap->div[div][0]);
946 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
947 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
948 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
949 isl_int_add(bmap->div[div][1],
950 bmap->div[div][1], bmap->div[div][0]);
951 if (!neg)
952 return 0;
953 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
954 bmap->n_div-div-1) != -1)
955 return 0;
956 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
957 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
958 return 0;
959 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
960 bmap->n_div-div-1) != -1)
961 return 0;
962 } else
963 return 0;
966 for (i = 0; i < bmap->n_div; ++i)
967 if (!isl_int_is_zero(bmap->div[i][1+pos]))
968 return 0;
970 return 1;
974 * Remove divs that don't occur in any of the constraints or other divs.
975 * These can arise when dropping some of the variables in a quast
976 * returned by piplib.
978 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
980 int i;
982 if (!bmap)
983 return NULL;
985 for (i = bmap->n_div-1; i >= 0; --i) {
986 if (!div_is_redundant(bmap, i))
987 continue;
988 bmap = isl_basic_map_drop_div(bmap, i);
990 return bmap;
993 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
995 bmap = remove_redundant_divs(bmap);
996 if (!bmap)
997 return NULL;
998 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
999 return bmap;
1002 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1004 return (struct isl_basic_set *)
1005 isl_basic_map_finalize((struct isl_basic_map *)bset);
1008 struct isl_set *isl_set_finalize(struct isl_set *set)
1010 int i;
1012 if (!set)
1013 return NULL;
1014 for (i = 0; i < set->n; ++i) {
1015 set->p[i] = isl_basic_set_finalize(set->p[i]);
1016 if (!set->p[i])
1017 goto error;
1019 return set;
1020 error:
1021 isl_set_free(set);
1022 return NULL;
1025 struct isl_map *isl_map_finalize(struct isl_map *map)
1027 int i;
1029 if (!map)
1030 return NULL;
1031 for (i = 0; i < map->n; ++i) {
1032 map->p[i] = isl_basic_map_finalize(map->p[i]);
1033 if (!map->p[i])
1034 goto error;
1036 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1037 return map;
1038 error:
1039 isl_map_free(map);
1040 return NULL;
1044 /* Remove any div that is defined in terms of the given variable.
1046 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1047 int pos)
1049 int i;
1050 unsigned dim = isl_dim_total(bmap->dim);
1052 for (i = 0; i < bmap->n_div; ++i) {
1053 if (isl_int_is_zero(bmap->div[i][0]))
1054 continue;
1055 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1056 continue;
1057 bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
1058 if (!bmap)
1059 return NULL;
1061 return bmap;
1064 /* Eliminate the specified variables from the constraints using
1065 * Fourier-Motzkin. The variables themselves are not removed.
1067 struct isl_basic_map *isl_basic_map_eliminate_vars(
1068 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1070 int d;
1071 int i, j, k;
1072 unsigned total;
1074 if (n == 0)
1075 return bmap;
1076 if (!bmap)
1077 return NULL;
1078 total = isl_basic_map_total_dim(bmap);
1080 bmap = isl_basic_map_cow(bmap);
1081 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1082 bmap = remove_dependent_vars(bmap, d);
1084 for (d = pos + n - 1;
1085 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1086 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1087 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1088 int n_lower, n_upper;
1089 if (!bmap)
1090 return NULL;
1091 for (i = 0; i < bmap->n_eq; ++i) {
1092 if (isl_int_is_zero(bmap->eq[i][1+d]))
1093 continue;
1094 eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1095 isl_basic_map_drop_equality(bmap, i);
1096 break;
1098 if (i < bmap->n_eq)
1099 continue;
1100 n_lower = 0;
1101 n_upper = 0;
1102 for (i = 0; i < bmap->n_ineq; ++i) {
1103 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1104 n_lower++;
1105 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1106 n_upper++;
1108 bmap = isl_basic_map_extend_constraints(bmap,
1109 0, n_lower * n_upper);
1110 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1111 int last;
1112 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1113 continue;
1114 last = -1;
1115 for (j = 0; j < i; ++j) {
1116 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1117 continue;
1118 last = j;
1119 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1120 isl_int_sgn(bmap->ineq[j][1+d]))
1121 continue;
1122 k = isl_basic_map_alloc_inequality(bmap);
1123 if (k < 0)
1124 goto error;
1125 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1126 1+total);
1127 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1128 1+d, 1+total, NULL);
1130 isl_basic_map_drop_inequality(bmap, i);
1131 i = last + 1;
1133 if (n_lower > 0 && n_upper > 0) {
1134 bmap = isl_basic_map_normalize_constraints(bmap);
1135 bmap = remove_duplicate_constraints(bmap, NULL);
1136 bmap = isl_basic_map_gauss(bmap, NULL);
1137 bmap = isl_basic_map_convex_hull(bmap);
1138 if (!bmap)
1139 goto error;
1140 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1141 break;
1144 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1145 return bmap;
1146 error:
1147 isl_basic_map_free(bmap);
1148 return NULL;
1151 struct isl_basic_set *isl_basic_set_eliminate_vars(
1152 struct isl_basic_set *bset, unsigned pos, unsigned n)
1154 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1155 (struct isl_basic_map *)bset, pos, n);
1158 /* Don't assume equalities are in order, because align_divs
1159 * may have changed the order of the divs.
1161 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1163 int d, i;
1164 unsigned total;
1166 total = isl_dim_total(bmap->dim);
1167 for (d = 0; d < total; ++d)
1168 elim[d] = -1;
1169 for (i = 0; i < bmap->n_eq; ++i) {
1170 for (d = total - 1; d >= 0; --d) {
1171 if (isl_int_is_zero(bmap->eq[i][1+d]))
1172 continue;
1173 elim[d] = i;
1174 break;
1179 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1181 return compute_elimination_index((struct isl_basic_map *)bset, elim);
1184 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1185 struct isl_basic_map *bmap, int *elim)
1187 int d, i;
1188 int copied = 0;
1189 unsigned total;
1191 total = isl_dim_total(bmap->dim);
1192 for (d = total - 1; d >= 0; --d) {
1193 if (isl_int_is_zero(src[1+d]))
1194 continue;
1195 if (elim[d] == -1)
1196 continue;
1197 if (!copied) {
1198 isl_seq_cpy(dst, src, 1 + total);
1199 copied = 1;
1201 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1203 return copied;
1206 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1207 struct isl_basic_set *bset, int *elim)
1209 return reduced_using_equalities(dst, src,
1210 (struct isl_basic_map *)bset, elim);
1213 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1214 struct isl_basic_set *bset, struct isl_basic_set *context)
1216 int i;
1217 int *elim;
1219 if (!bset || !context)
1220 goto error;
1222 bset = isl_basic_set_cow(bset);
1223 if (!bset)
1224 goto error;
1226 elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1227 if (!elim)
1228 goto error;
1229 set_compute_elimination_index(context, elim);
1230 for (i = 0; i < bset->n_eq; ++i)
1231 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1232 context, elim);
1233 for (i = 0; i < bset->n_ineq; ++i)
1234 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1235 context, elim);
1236 isl_basic_set_free(context);
1237 free(elim);
1238 bset = isl_basic_set_simplify(bset);
1239 bset = isl_basic_set_finalize(bset);
1240 return bset;
1241 error:
1242 isl_basic_set_free(bset);
1243 isl_basic_set_free(context);
1244 return NULL;
1247 static struct isl_basic_set *remove_shifted_constraints(
1248 struct isl_basic_set *bset, struct isl_basic_set *context)
1250 unsigned int size;
1251 isl_int ***index;
1252 int bits;
1253 int k, h, l;
1255 if (!bset)
1256 return NULL;
1258 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1259 bits = ffs(size) - 1;
1260 index = isl_calloc_array(ctx, isl_int **, size);
1261 if (!index)
1262 return bset;
1264 for (k = 0; k < context->n_ineq; ++k) {
1265 h = set_hash_index(index, size, bits, context, k);
1266 index[h] = &context->ineq[k];
1268 for (k = 0; k < bset->n_ineq; ++k) {
1269 h = set_hash_index(index, size, bits, bset, k);
1270 if (!index[h])
1271 continue;
1272 l = index[h] - &context->ineq[0];
1273 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1274 continue;
1275 bset = isl_basic_set_cow(bset);
1276 if (!bset)
1277 goto error;
1278 isl_basic_set_drop_inequality(bset, k);
1279 --k;
1281 free(index);
1282 return bset;
1283 error:
1284 free(index);
1285 return bset;
1288 /* Tighten (decrease) the constant terms of the inequalities based
1289 * on the equalities, without removing any integer points.
1290 * For example, if there is an equality
1292 * i = 3 * j
1294 * and an inequality
1296 * i >= 1
1298 * then we want to replace the inequality by
1300 * i >= 3
1302 * We do this by computing a variable compression and translating
1303 * the constraints to the compressed space.
1304 * If any constraint has coefficients (except the contant term)
1305 * with a common factor "f", then we can replace the constant term "c"
1306 * by
1308 * f * floor(c/f)
1310 * That is, we add
1312 * f * floor(c/f) - c = -fract(c/f)
1314 * and we can add the same value to the original constraint.
1316 * In the example, the compressed space only contains "j",
1317 * and the inequality translates to
1319 * 3 * j - 1 >= 0
1321 * We add -fract(-1/3) = -2 to the original constraint to obtain
1323 * i - 3 >= 0
1325 static struct isl_basic_set *normalize_constraints_in_compressed_space(
1326 struct isl_basic_set *bset)
1328 int i;
1329 unsigned total;
1330 struct isl_mat *B, *C;
1331 isl_int gcd;
1333 if (!bset)
1334 return NULL;
1336 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1337 return bset;
1339 bset = isl_basic_set_cow(bset);
1340 if (!bset)
1341 return NULL;
1343 total = isl_basic_set_total_dim(bset);
1344 B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
1345 C = isl_mat_variable_compression(bset->ctx, B, NULL);
1346 if (!C)
1347 return bset;
1348 if (C->n_col == 0) {
1349 isl_mat_free(bset->ctx, C);
1350 return isl_basic_set_set_to_empty(bset);
1352 B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
1353 0, bset->n_ineq, 0, 1 + total);
1354 C = isl_mat_product(bset->ctx, B, C);
1355 if (!C)
1356 return bset;
1358 isl_int_init(gcd);
1359 for (i = 0; i < bset->n_ineq; ++i) {
1360 isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd);
1361 if (isl_int_is_one(gcd))
1362 continue;
1363 isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd);
1364 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]);
1366 isl_int_clear(gcd);
1368 isl_mat_free(bset->ctx, C);
1370 return bset;
1373 /* Remove all information from bset that is redundant in the context
1374 * of context. In particular, equalities that are linear combinations
1375 * of those in context are removed. Then the inequalities that are
1376 * redundant in the context of the equalities and inequalities of
1377 * context are removed.
1379 * We first simplify the constraints of "bset" in the context of the
1380 * equalities of "context".
1381 * Then we simplify the inequalities of the context in the context
1382 * of the equalities of bset and remove the inequalities from "bset"
1383 * that are obviously redundant with respect to some inequality in "context".
1385 * If there are any inequalities left, we construct a tableau for
1386 * the context and then add the inequalities of "bset".
1387 * Before adding these equalities, we freeze all constraints such that
1388 * they won't be considered redundant in terms of the constraints of "bset".
1389 * Then we detect all equalities and redundant constraints (among the
1390 * constraints that weren't frozen) and update bset according to the results.
1391 * We have to be careful here because we don't want any of the context
1392 * constraints to remain and because we haven't added the equalities of "bset"
1393 * to the tableau so we temporarily have to pretend that there were no
1394 * equalities.
1396 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1397 struct isl_basic_set *context)
1399 int i;
1400 struct isl_tab *tab;
1401 unsigned context_ineq, bset_eq;
1402 struct isl_basic_set *combined = NULL;
1404 if (!context || !bset)
1405 goto error;
1407 if (context->n_eq > 0)
1408 bset = isl_basic_set_reduce_using_equalities(bset,
1409 isl_basic_set_copy(context));
1410 if (!bset)
1411 goto error;
1413 if (bset->n_eq > 0) {
1414 struct isl_basic_set *affine_hull;
1415 affine_hull = isl_basic_set_copy(bset);
1416 affine_hull = isl_basic_set_cow(affine_hull);
1417 if (!affine_hull)
1418 goto error;
1419 isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1420 context = isl_basic_set_intersect(context, affine_hull);
1421 context = isl_basic_set_gauss(context, NULL);
1422 context = normalize_constraints_in_compressed_space(context);
1424 if (!context)
1425 goto error;
1426 if (ISL_F_ISSET(context, ISL_BASIC_SET_EMPTY)) {
1427 isl_basic_set_free(bset);
1428 return context;
1430 if (!context->n_ineq)
1431 goto done;
1432 bset = remove_shifted_constraints(bset, context);
1433 if (!bset->n_ineq)
1434 goto done;
1435 isl_basic_set_free_equality(context, context->n_eq);
1436 context_ineq = context->n_ineq;
1437 combined = isl_basic_set_cow(isl_basic_set_copy(context));
1438 combined = isl_basic_set_extend_constraints(combined,
1439 bset->n_eq, bset->n_ineq);
1440 tab = isl_tab_from_basic_set(combined);
1441 if (!tab)
1442 goto error;
1443 for (i = 0; i < context_ineq; ++i)
1444 tab->con[i].frozen = 1;
1445 tab = isl_tab_extend(bset->ctx, tab, bset->n_ineq);
1446 if (!tab)
1447 goto error;
1448 for (i = 0; i < bset->n_ineq; ++i)
1449 tab = isl_tab_add_ineq(bset->ctx, tab, bset->ineq[i]);
1450 bset = isl_basic_set_add_constraints(combined, bset, 0);
1451 tab = isl_tab_detect_equalities(bset->ctx, tab);
1452 tab = isl_tab_detect_redundant(bset->ctx, tab);
1453 if (!tab)
1454 goto error2;
1455 for (i = 0; i < context_ineq; ++i) {
1456 tab->con[i].is_zero = 0;
1457 tab->con[i].is_redundant = 1;
1459 bset_eq = bset->n_eq;
1460 bset->n_eq = 0;
1461 bset = isl_basic_set_update_from_tab(bset, tab);
1462 bset->n_eq = bset_eq;
1463 isl_tab_free(bset->ctx, tab);
1464 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1465 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1466 done:
1467 bset = isl_basic_set_simplify(bset);
1468 bset = isl_basic_set_finalize(bset);
1469 isl_basic_set_free(context);
1470 return bset;
1471 error:
1472 isl_basic_set_free(combined);
1473 error2:
1474 isl_basic_set_free(bset);
1475 isl_basic_set_free(context);
1476 return NULL;
1479 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1480 struct isl_basic_map *context)
1482 struct isl_basic_set *bset;
1484 if (!bmap || !context)
1485 goto error;
1487 bmap = isl_basic_map_convex_hull(bmap);
1488 context = isl_basic_map_convex_hull(context);
1490 context = isl_basic_map_align_divs(context, bmap);
1491 bmap = isl_basic_map_align_divs(bmap, context);
1493 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1494 isl_basic_map_underlying_set(context));
1496 return isl_basic_map_overlying_set(bset, bmap);
1497 error:
1498 isl_basic_map_free(bmap);
1499 isl_basic_map_free(context);
1500 return NULL;
1504 * Assumes context has no implicit divs.
1506 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1508 int i;
1510 context = isl_basic_map_convex_hull(context);
1511 map = isl_map_cow(map);
1512 if (!map || !context)
1513 return NULL;
1514 isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1515 map = isl_map_compute_divs(map);
1516 for (i = 0; i < map->n; ++i)
1517 context = isl_basic_map_align_divs(context, map->p[i]);
1518 for (i = 0; i < map->n; ++i) {
1519 map->p[i] = isl_basic_map_gist(map->p[i],
1520 isl_basic_map_copy(context));
1521 if (!map->p[i])
1522 goto error;
1524 isl_basic_map_free(context);
1525 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1526 return map;
1527 error:
1528 isl_map_free(map);
1529 isl_basic_map_free(context);
1530 return NULL;
1533 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1534 struct isl_basic_set *context)
1536 return (struct isl_basic_set *)isl_basic_map_gist(
1537 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1540 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1542 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1543 (struct isl_basic_map *)context);
1546 /* Quick check to see if two basic maps are disjoint.
1547 * In particular, we reduce the equalities and inequalities of
1548 * one basic map in the context of the equalities of the other
1549 * basic map and check if we get a contradiction.
1551 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1552 struct isl_basic_map *bmap2)
1554 struct isl_vec *v = NULL;
1555 int *elim = NULL;
1556 unsigned total;
1557 int d, i;
1559 if (!bmap1 || !bmap2)
1560 return -1;
1561 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1562 return -1);
1563 if (bmap1->n_div || bmap2->n_div)
1564 return 0;
1565 if (!bmap1->n_eq && !bmap2->n_eq)
1566 return 0;
1568 total = isl_dim_total(bmap1->dim);
1569 if (total == 0)
1570 return 0;
1571 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1572 if (!v)
1573 goto error;
1574 elim = isl_alloc_array(bmap1->ctx, int, total);
1575 if (!elim)
1576 goto error;
1577 compute_elimination_index(bmap1, elim);
1578 for (i = 0; i < bmap2->n_eq; ++i) {
1579 int reduced;
1580 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1581 bmap1, elim);
1582 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1583 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1584 goto disjoint;
1586 for (i = 0; i < bmap2->n_ineq; ++i) {
1587 int reduced;
1588 reduced = reduced_using_equalities(v->block.data,
1589 bmap2->ineq[i], bmap1, elim);
1590 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1591 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1592 goto disjoint;
1594 compute_elimination_index(bmap2, elim);
1595 for (i = 0; i < bmap1->n_ineq; ++i) {
1596 int reduced;
1597 reduced = reduced_using_equalities(v->block.data,
1598 bmap1->ineq[i], bmap2, elim);
1599 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1600 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1601 goto disjoint;
1603 isl_vec_free(bmap1->ctx, v);
1604 free(elim);
1605 return 0;
1606 disjoint:
1607 isl_vec_free(bmap1->ctx, v);
1608 free(elim);
1609 return 1;
1610 error:
1611 isl_vec_free(bmap1->ctx, v);
1612 free(elim);
1613 return -1;
1616 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1617 struct isl_basic_set *bset2)
1619 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1620 (struct isl_basic_map *)bset2);
1623 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1625 int i, j;
1627 if (!map1 || !map2)
1628 return -1;
1630 if (isl_map_fast_is_equal(map1, map2))
1631 return 0;
1633 for (i = 0; i < map1->n; ++i) {
1634 for (j = 0; j < map2->n; ++j) {
1635 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1636 map2->p[j]);
1637 if (d != 1)
1638 return d;
1641 return 1;
1644 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1646 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1647 (struct isl_map *)set2);