doc: add some implementation details on parametric integer programming
[isl.git] / isl_map_simplify.c
blob157d6b7dbd606056b7e53b755139e6530265a3ca
1 /*
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
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include "isl_equalities.h"
13 #include <isl/map.h>
14 #include <isl/seq.h>
15 #include "isl_tab.h"
16 #include <isl_dim_private.h>
17 #include <isl_mat_private.h>
19 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
21 isl_int *t = bmap->eq[a];
22 bmap->eq[a] = bmap->eq[b];
23 bmap->eq[b] = t;
26 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
28 if (a != b) {
29 isl_int *t = bmap->ineq[a];
30 bmap->ineq[a] = bmap->ineq[b];
31 bmap->ineq[b] = t;
35 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
37 swap_inequality((struct isl_basic_map *)bset, a, b);
40 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
42 isl_seq_cpy(c, c + n, rem);
43 isl_seq_clr(c + rem, n);
46 /* Drop n dimensions starting at first.
48 * In principle, this frees up some extra variables as the number
49 * of columns remains constant, but we would have to extend
50 * the div array too as the number of rows in this array is assumed
51 * to be equal to extra.
53 struct isl_basic_set *isl_basic_set_drop_dims(
54 struct isl_basic_set *bset, unsigned first, unsigned n)
56 int i;
58 if (!bset)
59 goto error;
61 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
63 if (n == 0 && !isl_dim_get_tuple_name(bset->dim, isl_dim_set))
64 return bset;
66 bset = isl_basic_set_cow(bset);
67 if (!bset)
68 return NULL;
70 for (i = 0; i < bset->n_eq; ++i)
71 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
72 (bset->dim->n_out-first-n)+bset->extra);
74 for (i = 0; i < bset->n_ineq; ++i)
75 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
76 (bset->dim->n_out-first-n)+bset->extra);
78 for (i = 0; i < bset->n_div; ++i)
79 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
80 (bset->dim->n_out-first-n)+bset->extra);
82 bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
83 if (!bset->dim)
84 goto error;
86 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
87 bset = isl_basic_set_simplify(bset);
88 return isl_basic_set_finalize(bset);
89 error:
90 isl_basic_set_free(bset);
91 return NULL;
94 struct isl_set *isl_set_drop_dims(
95 struct isl_set *set, unsigned first, unsigned n)
97 int i;
99 if (!set)
100 goto error;
102 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
104 if (n == 0 && !isl_dim_get_tuple_name(set->dim, isl_dim_set))
105 return set;
106 set = isl_set_cow(set);
107 if (!set)
108 goto error;
109 set->dim = isl_dim_drop_outputs(set->dim, first, n);
110 if (!set->dim)
111 goto error;
113 for (i = 0; i < set->n; ++i) {
114 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
115 if (!set->p[i])
116 goto error;
119 ISL_F_CLR(set, ISL_SET_NORMALIZED);
120 return set;
121 error:
122 isl_set_free(set);
123 return NULL;
126 /* Move "n" divs starting at "first" to the end of the list of divs.
128 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
129 unsigned first, unsigned n)
131 isl_int **div;
132 int i;
134 if (first + n == bmap->n_div)
135 return bmap;
137 div = isl_alloc_array(bmap->ctx, isl_int *, n);
138 if (!div)
139 goto error;
140 for (i = 0; i < n; ++i)
141 div[i] = bmap->div[first + i];
142 for (i = 0; i < bmap->n_div - first - n; ++i)
143 bmap->div[first + i] = bmap->div[first + n + i];
144 for (i = 0; i < n; ++i)
145 bmap->div[bmap->n_div - n + i] = div[i];
146 free(div);
147 return bmap;
148 error:
149 isl_basic_map_free(bmap);
150 return NULL;
153 /* Drop "n" dimensions of type "type" starting at "first".
155 * In principle, this frees up some extra variables as the number
156 * of columns remains constant, but we would have to extend
157 * the div array too as the number of rows in this array is assumed
158 * to be equal to extra.
160 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
161 enum isl_dim_type type, unsigned first, unsigned n)
163 int i;
164 unsigned dim;
165 unsigned offset;
166 unsigned left;
168 if (!bmap)
169 goto error;
171 dim = isl_basic_map_dim(bmap, type);
172 isl_assert(bmap->ctx, first + n <= dim, goto error);
174 if (n == 0 && !isl_dim_get_tuple_name(bmap->dim, type))
175 return bmap;
177 bmap = isl_basic_map_cow(bmap);
178 if (!bmap)
179 return NULL;
181 offset = isl_basic_map_offset(bmap, type) + first;
182 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
183 for (i = 0; i < bmap->n_eq; ++i)
184 constraint_drop_vars(bmap->eq[i]+offset, n, left);
186 for (i = 0; i < bmap->n_ineq; ++i)
187 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
189 for (i = 0; i < bmap->n_div; ++i)
190 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
192 if (type == isl_dim_div) {
193 bmap = move_divs_last(bmap, first, n);
194 if (!bmap)
195 goto error;
196 isl_basic_map_free_div(bmap, n);
197 } else
198 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
199 if (!bmap->dim)
200 goto error;
202 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
203 bmap = isl_basic_map_simplify(bmap);
204 return isl_basic_map_finalize(bmap);
205 error:
206 isl_basic_map_free(bmap);
207 return NULL;
210 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
211 enum isl_dim_type type, unsigned first, unsigned n)
213 return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset,
214 type, first, n);
217 struct isl_basic_map *isl_basic_map_drop_inputs(
218 struct isl_basic_map *bmap, unsigned first, unsigned n)
220 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
223 struct isl_map *isl_map_drop(struct isl_map *map,
224 enum isl_dim_type type, unsigned first, unsigned n)
226 int i;
228 if (!map)
229 goto error;
231 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
233 if (n == 0 && !isl_dim_get_tuple_name(map->dim, type))
234 return map;
235 map = isl_map_cow(map);
236 if (!map)
237 goto error;
238 map->dim = isl_dim_drop(map->dim, type, first, n);
239 if (!map->dim)
240 goto error;
242 for (i = 0; i < map->n; ++i) {
243 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
244 if (!map->p[i])
245 goto error;
247 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
249 return map;
250 error:
251 isl_map_free(map);
252 return NULL;
255 struct isl_set *isl_set_drop(struct isl_set *set,
256 enum isl_dim_type type, unsigned first, unsigned n)
258 return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
261 struct isl_map *isl_map_drop_inputs(
262 struct isl_map *map, unsigned first, unsigned n)
264 return isl_map_drop(map, isl_dim_in, first, n);
268 * We don't cow, as the div is assumed to be redundant.
270 static struct isl_basic_map *isl_basic_map_drop_div(
271 struct isl_basic_map *bmap, unsigned div)
273 int i;
274 unsigned pos;
276 if (!bmap)
277 goto error;
279 pos = 1 + isl_dim_total(bmap->dim) + div;
281 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
283 for (i = 0; i < bmap->n_eq; ++i)
284 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
286 for (i = 0; i < bmap->n_ineq; ++i) {
287 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
288 isl_basic_map_drop_inequality(bmap, i);
289 --i;
290 continue;
292 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
295 for (i = 0; i < bmap->n_div; ++i)
296 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
298 if (div != bmap->n_div - 1) {
299 int j;
300 isl_int *t = bmap->div[div];
302 for (j = div; j < bmap->n_div - 1; ++j)
303 bmap->div[j] = bmap->div[j+1];
305 bmap->div[bmap->n_div - 1] = t;
307 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
308 isl_basic_map_free_div(bmap, 1);
310 return bmap;
311 error:
312 isl_basic_map_free(bmap);
313 return NULL;
316 struct isl_basic_map *isl_basic_map_normalize_constraints(
317 struct isl_basic_map *bmap)
319 int i;
320 isl_int gcd;
321 unsigned total = isl_basic_map_total_dim(bmap);
323 if (!bmap)
324 return NULL;
326 isl_int_init(gcd);
327 for (i = bmap->n_eq - 1; i >= 0; --i) {
328 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
329 if (isl_int_is_zero(gcd)) {
330 if (!isl_int_is_zero(bmap->eq[i][0])) {
331 bmap = isl_basic_map_set_to_empty(bmap);
332 break;
334 isl_basic_map_drop_equality(bmap, i);
335 continue;
337 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
338 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
339 if (isl_int_is_one(gcd))
340 continue;
341 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
342 bmap = isl_basic_map_set_to_empty(bmap);
343 break;
345 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
348 for (i = bmap->n_ineq - 1; i >= 0; --i) {
349 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
350 if (isl_int_is_zero(gcd)) {
351 if (isl_int_is_neg(bmap->ineq[i][0])) {
352 bmap = isl_basic_map_set_to_empty(bmap);
353 break;
355 isl_basic_map_drop_inequality(bmap, i);
356 continue;
358 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
359 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
360 if (isl_int_is_one(gcd))
361 continue;
362 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
363 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
365 isl_int_clear(gcd);
367 return bmap;
370 struct isl_basic_set *isl_basic_set_normalize_constraints(
371 struct isl_basic_set *bset)
373 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
374 (struct isl_basic_map *)bset);
377 /* Assumes divs have been ordered if keep_divs is set.
379 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
380 unsigned pos, isl_int *eq, int keep_divs, int *progress)
382 unsigned total;
383 int k;
384 int last_div;
386 total = isl_basic_map_total_dim(bmap);
387 last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
388 bmap->n_div);
389 for (k = 0; k < bmap->n_eq; ++k) {
390 if (bmap->eq[k] == eq)
391 continue;
392 if (isl_int_is_zero(bmap->eq[k][1+pos]))
393 continue;
394 if (progress)
395 *progress = 1;
396 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
397 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
400 for (k = 0; k < bmap->n_ineq; ++k) {
401 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
402 continue;
403 if (progress)
404 *progress = 1;
405 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
406 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
407 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
410 for (k = 0; k < bmap->n_div; ++k) {
411 if (isl_int_is_zero(bmap->div[k][0]))
412 continue;
413 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
414 continue;
415 if (progress)
416 *progress = 1;
417 /* We need to be careful about circular definitions,
418 * so for now we just remove the definition of div k
419 * if the equality contains any divs.
420 * If keep_divs is set, then the divs have been ordered
421 * and we can keep the definition as long as the result
422 * is still ordered.
424 if (last_div == -1 || (keep_divs && last_div < k))
425 isl_seq_elim(bmap->div[k]+1, eq,
426 1+pos, 1+total, &bmap->div[k][0]);
427 else
428 isl_seq_clr(bmap->div[k], 1 + total);
429 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
433 /* Assumes divs have been ordered if keep_divs is set.
435 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
436 unsigned div, int keep_divs)
438 unsigned pos = isl_dim_total(bmap->dim) + div;
440 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
442 isl_basic_map_drop_div(bmap, div);
445 /* Check if elimination of div "div" using equality "eq" would not
446 * result in a div depending on a later div.
448 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
449 unsigned div)
451 int k;
452 int last_div;
453 unsigned pos = isl_dim_total(bmap->dim) + div;
455 last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
456 bmap->n_div);
457 if (last_div < 0 || last_div <= div)
458 return 1;
460 for (k = 0; k <= last_div; ++k) {
461 if (isl_int_is_zero(bmap->div[k][0]))
462 return 1;
463 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
464 return 0;
467 return 1;
470 /* Elimininate divs based on equalities
472 static struct isl_basic_map *eliminate_divs_eq(
473 struct isl_basic_map *bmap, int *progress)
475 int d;
476 int i;
477 int modified = 0;
478 unsigned off;
480 bmap = isl_basic_map_order_divs(bmap);
482 if (!bmap)
483 return NULL;
485 off = 1 + isl_dim_total(bmap->dim);
487 for (d = bmap->n_div - 1; d >= 0 ; --d) {
488 for (i = 0; i < bmap->n_eq; ++i) {
489 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
490 !isl_int_is_negone(bmap->eq[i][off + d]))
491 continue;
492 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
493 continue;
494 modified = 1;
495 *progress = 1;
496 eliminate_div(bmap, bmap->eq[i], d, 1);
497 isl_basic_map_drop_equality(bmap, i);
498 break;
501 if (modified)
502 return eliminate_divs_eq(bmap, progress);
503 return bmap;
506 /* Elimininate divs based on inequalities
508 static struct isl_basic_map *eliminate_divs_ineq(
509 struct isl_basic_map *bmap, int *progress)
511 int d;
512 int i;
513 unsigned off;
514 struct isl_ctx *ctx;
516 if (!bmap)
517 return NULL;
519 ctx = bmap->ctx;
520 off = 1 + isl_dim_total(bmap->dim);
522 for (d = bmap->n_div - 1; d >= 0 ; --d) {
523 for (i = 0; i < bmap->n_eq; ++i)
524 if (!isl_int_is_zero(bmap->eq[i][off + d]))
525 break;
526 if (i < bmap->n_eq)
527 continue;
528 for (i = 0; i < bmap->n_ineq; ++i)
529 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
530 break;
531 if (i < bmap->n_ineq)
532 continue;
533 *progress = 1;
534 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
535 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
536 break;
537 bmap = isl_basic_map_drop_div(bmap, d);
538 if (!bmap)
539 break;
541 return bmap;
544 struct isl_basic_map *isl_basic_map_gauss(
545 struct isl_basic_map *bmap, int *progress)
547 int k;
548 int done;
549 int last_var;
550 unsigned total_var;
551 unsigned total;
553 bmap = isl_basic_map_order_divs(bmap);
555 if (!bmap)
556 return NULL;
558 total = isl_basic_map_total_dim(bmap);
559 total_var = total - bmap->n_div;
561 last_var = total - 1;
562 for (done = 0; done < bmap->n_eq; ++done) {
563 for (; last_var >= 0; --last_var) {
564 for (k = done; k < bmap->n_eq; ++k)
565 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
566 break;
567 if (k < bmap->n_eq)
568 break;
570 if (last_var < 0)
571 break;
572 if (k != done)
573 swap_equality(bmap, k, done);
574 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
575 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
577 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
578 progress);
580 if (last_var >= total_var &&
581 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
582 unsigned div = last_var - total_var;
583 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
584 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
585 isl_int_set(bmap->div[div][0],
586 bmap->eq[done][1+last_var]);
587 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
590 if (done == bmap->n_eq)
591 return bmap;
592 for (k = done; k < bmap->n_eq; ++k) {
593 if (isl_int_is_zero(bmap->eq[k][0]))
594 continue;
595 return isl_basic_map_set_to_empty(bmap);
597 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
598 return bmap;
601 struct isl_basic_set *isl_basic_set_gauss(
602 struct isl_basic_set *bset, int *progress)
604 return (struct isl_basic_set*)isl_basic_map_gauss(
605 (struct isl_basic_map *)bset, progress);
609 static unsigned int round_up(unsigned int v)
611 int old_v = v;
613 while (v) {
614 old_v = v;
615 v ^= v & -v;
617 return old_v << 1;
620 static int hash_index(isl_int ***index, unsigned int size, int bits,
621 struct isl_basic_map *bmap, int k)
623 int h;
624 unsigned total = isl_basic_map_total_dim(bmap);
625 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
626 for (h = hash; index[h]; h = (h+1) % size)
627 if (&bmap->ineq[k] != index[h] &&
628 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
629 break;
630 return h;
633 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
634 struct isl_basic_set *bset, int k)
636 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
639 /* If we can eliminate more than one div, then we need to make
640 * sure we do it from last div to first div, in order not to
641 * change the position of the other divs that still need to
642 * be removed.
644 static struct isl_basic_map *remove_duplicate_divs(
645 struct isl_basic_map *bmap, int *progress)
647 unsigned int size;
648 int *index;
649 int *elim_for;
650 int k, l, h;
651 int bits;
652 struct isl_blk eq;
653 unsigned total_var;
654 unsigned total;
655 struct isl_ctx *ctx;
657 if (!bmap || bmap->n_div <= 1)
658 return bmap;
660 total_var = isl_dim_total(bmap->dim);
661 total = total_var + bmap->n_div;
663 ctx = bmap->ctx;
664 for (k = bmap->n_div - 1; k >= 0; --k)
665 if (!isl_int_is_zero(bmap->div[k][0]))
666 break;
667 if (k <= 0)
668 return bmap;
670 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
671 size = round_up(4 * bmap->n_div / 3 - 1);
672 bits = ffs(size) - 1;
673 index = isl_calloc_array(ctx, int, size);
674 if (!index)
675 return bmap;
676 eq = isl_blk_alloc(ctx, 1+total);
677 if (isl_blk_is_error(eq))
678 goto out;
680 isl_seq_clr(eq.data, 1+total);
681 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
682 for (--k; k >= 0; --k) {
683 uint32_t hash;
685 if (isl_int_is_zero(bmap->div[k][0]))
686 continue;
688 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
689 for (h = hash; index[h]; h = (h+1) % size)
690 if (isl_seq_eq(bmap->div[k],
691 bmap->div[index[h]-1], 2+total))
692 break;
693 if (index[h]) {
694 *progress = 1;
695 l = index[h] - 1;
696 elim_for[l] = k + 1;
698 index[h] = k+1;
700 for (l = bmap->n_div - 1; l >= 0; --l) {
701 if (!elim_for[l])
702 continue;
703 k = elim_for[l] - 1;
704 isl_int_set_si(eq.data[1+total_var+k], -1);
705 isl_int_set_si(eq.data[1+total_var+l], 1);
706 eliminate_div(bmap, eq.data, l, 0);
707 isl_int_set_si(eq.data[1+total_var+k], 0);
708 isl_int_set_si(eq.data[1+total_var+l], 0);
711 isl_blk_free(ctx, eq);
712 out:
713 free(index);
714 free(elim_for);
715 return bmap;
718 static int n_pure_div_eq(struct isl_basic_map *bmap)
720 int i, j;
721 unsigned total;
723 total = isl_dim_total(bmap->dim);
724 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
725 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
726 --j;
727 if (j < 0)
728 break;
729 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
730 return 0;
732 return i;
735 /* Normalize divs that appear in equalities.
737 * In particular, we assume that bmap contains some equalities
738 * of the form
740 * a x = m * e_i
742 * and we want to replace the set of e_i by a minimal set and
743 * such that the new e_i have a canonical representation in terms
744 * of the vector x.
745 * If any of the equalities involves more than one divs, then
746 * we currently simply bail out.
748 * Let us first additionally assume that all equalities involve
749 * a div. The equalities then express modulo constraints on the
750 * remaining variables and we can use "parameter compression"
751 * to find a minimal set of constraints. The result is a transformation
753 * x = T(x') = x_0 + G x'
755 * with G a lower-triangular matrix with all elements below the diagonal
756 * non-negative and smaller than the diagonal element on the same row.
757 * We first normalize x_0 by making the same property hold in the affine
758 * T matrix.
759 * The rows i of G with a 1 on the diagonal do not impose any modulo
760 * constraint and simply express x_i = x'_i.
761 * For each of the remaining rows i, we introduce a div and a corresponding
762 * equality. In particular
764 * g_ii e_j = x_i - g_i(x')
766 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
767 * corresponding div (if g_kk != 1).
769 * If there are any equalities not involving any div, then we
770 * first apply a variable compression on the variables x:
772 * x = C x'' x'' = C_2 x
774 * and perform the above parameter compression on A C instead of on A.
775 * The resulting compression is then of the form
777 * x'' = T(x') = x_0 + G x'
779 * and in constructing the new divs and the corresponding equalities,
780 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
781 * by the corresponding row from C_2.
783 static struct isl_basic_map *normalize_divs(
784 struct isl_basic_map *bmap, int *progress)
786 int i, j, k;
787 int total;
788 int div_eq;
789 struct isl_mat *B;
790 struct isl_vec *d;
791 struct isl_mat *T = NULL;
792 struct isl_mat *C = NULL;
793 struct isl_mat *C2 = NULL;
794 isl_int v;
795 int *pos;
796 int dropped, needed;
798 if (!bmap)
799 return NULL;
801 if (bmap->n_div == 0)
802 return bmap;
804 if (bmap->n_eq == 0)
805 return bmap;
807 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
808 return bmap;
810 total = isl_dim_total(bmap->dim);
811 div_eq = n_pure_div_eq(bmap);
812 if (div_eq == 0)
813 return bmap;
815 if (div_eq < bmap->n_eq) {
816 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
817 bmap->n_eq - div_eq, 0, 1 + total);
818 C = isl_mat_variable_compression(B, &C2);
819 if (!C || !C2)
820 goto error;
821 if (C->n_col == 0) {
822 bmap = isl_basic_map_set_to_empty(bmap);
823 isl_mat_free(C);
824 isl_mat_free(C2);
825 goto done;
829 d = isl_vec_alloc(bmap->ctx, div_eq);
830 if (!d)
831 goto error;
832 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
833 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
834 --j;
835 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
837 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
839 if (C) {
840 B = isl_mat_product(B, C);
841 C = NULL;
844 T = isl_mat_parameter_compression(B, d);
845 if (!T)
846 goto error;
847 if (T->n_col == 0) {
848 bmap = isl_basic_map_set_to_empty(bmap);
849 isl_mat_free(C2);
850 isl_mat_free(T);
851 goto done;
853 isl_int_init(v);
854 for (i = 0; i < T->n_row - 1; ++i) {
855 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
856 if (isl_int_is_zero(v))
857 continue;
858 isl_mat_col_submul(T, 0, v, 1 + i);
860 isl_int_clear(v);
861 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
862 if (!pos)
863 goto error;
864 /* We have to be careful because dropping equalities may reorder them */
865 dropped = 0;
866 for (j = bmap->n_div - 1; j >= 0; --j) {
867 for (i = 0; i < bmap->n_eq; ++i)
868 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
869 break;
870 if (i < bmap->n_eq) {
871 bmap = isl_basic_map_drop_div(bmap, j);
872 isl_basic_map_drop_equality(bmap, i);
873 ++dropped;
876 pos[0] = 0;
877 needed = 0;
878 for (i = 1; i < T->n_row; ++i) {
879 if (isl_int_is_one(T->row[i][i]))
880 pos[i] = i;
881 else
882 needed++;
884 if (needed > dropped) {
885 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
886 needed, needed, 0);
887 if (!bmap)
888 goto error;
890 for (i = 1; i < T->n_row; ++i) {
891 if (isl_int_is_one(T->row[i][i]))
892 continue;
893 k = isl_basic_map_alloc_div(bmap);
894 pos[i] = 1 + total + k;
895 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
896 isl_int_set(bmap->div[k][0], T->row[i][i]);
897 if (C2)
898 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
899 else
900 isl_int_set_si(bmap->div[k][1 + i], 1);
901 for (j = 0; j < i; ++j) {
902 if (isl_int_is_zero(T->row[i][j]))
903 continue;
904 if (pos[j] < T->n_row && C2)
905 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
906 C2->row[pos[j]], 1 + total);
907 else
908 isl_int_neg(bmap->div[k][1 + pos[j]],
909 T->row[i][j]);
911 j = isl_basic_map_alloc_equality(bmap);
912 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
913 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
915 free(pos);
916 isl_mat_free(C2);
917 isl_mat_free(T);
919 if (progress)
920 *progress = 1;
921 done:
922 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
924 return bmap;
925 error:
926 isl_mat_free(C);
927 isl_mat_free(C2);
928 isl_mat_free(T);
929 return bmap;
932 static struct isl_basic_map *set_div_from_lower_bound(
933 struct isl_basic_map *bmap, int div, int ineq)
935 unsigned total = 1 + isl_dim_total(bmap->dim);
937 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
938 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
939 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
940 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
941 isl_int_set_si(bmap->div[div][1 + total + div], 0);
943 return bmap;
946 /* Check whether it is ok to define a div based on an inequality.
947 * To avoid the introduction of circular definitions of divs, we
948 * do not allow such a definition if the resulting expression would refer to
949 * any other undefined divs or if any known div is defined in
950 * terms of the unknown div.
952 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
953 int div, int ineq)
955 int j;
956 unsigned total = 1 + isl_dim_total(bmap->dim);
958 /* Not defined in terms of unknown divs */
959 for (j = 0; j < bmap->n_div; ++j) {
960 if (div == j)
961 continue;
962 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
963 continue;
964 if (isl_int_is_zero(bmap->div[j][0]))
965 return 0;
968 /* No other div defined in terms of this one => avoid loops */
969 for (j = 0; j < bmap->n_div; ++j) {
970 if (div == j)
971 continue;
972 if (isl_int_is_zero(bmap->div[j][0]))
973 continue;
974 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
975 return 0;
978 return 1;
981 /* Given two constraints "k" and "l" that are opposite to each other,
982 * except for the constant term, check if we can use them
983 * to obtain an expression for one of the hitherto unknown divs.
984 * "sum" is the sum of the constant terms of the constraints.
985 * If this sum is strictly smaller than the coefficient of one
986 * of the divs, then this pair can be used define the div.
987 * To avoid the introduction of circular definitions of divs, we
988 * do not use the pair if the resulting expression would refer to
989 * any other undefined divs or if any known div is defined in
990 * terms of the unknown div.
992 static struct isl_basic_map *check_for_div_constraints(
993 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
995 int i;
996 unsigned total = 1 + isl_dim_total(bmap->dim);
998 for (i = 0; i < bmap->n_div; ++i) {
999 if (!isl_int_is_zero(bmap->div[i][0]))
1000 continue;
1001 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1002 continue;
1003 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1004 continue;
1005 if (!ok_to_set_div_from_bound(bmap, i, k))
1006 break;
1007 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1008 bmap = set_div_from_lower_bound(bmap, i, k);
1009 else
1010 bmap = set_div_from_lower_bound(bmap, i, l);
1011 if (progress)
1012 *progress = 1;
1013 break;
1015 return bmap;
1018 static struct isl_basic_map *remove_duplicate_constraints(
1019 struct isl_basic_map *bmap, int *progress, int detect_divs)
1021 unsigned int size;
1022 isl_int ***index;
1023 int k, l, h;
1024 int bits;
1025 unsigned total = isl_basic_map_total_dim(bmap);
1026 isl_int sum;
1028 if (!bmap || bmap->n_ineq <= 1)
1029 return bmap;
1031 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1032 bits = ffs(size) - 1;
1033 index = isl_calloc_array(ctx, isl_int **, size);
1034 if (!index)
1035 return bmap;
1037 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
1038 for (k = 1; k < bmap->n_ineq; ++k) {
1039 h = hash_index(index, size, bits, bmap, k);
1040 if (!index[h]) {
1041 index[h] = &bmap->ineq[k];
1042 continue;
1044 if (progress)
1045 *progress = 1;
1046 l = index[h] - &bmap->ineq[0];
1047 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1048 swap_inequality(bmap, k, l);
1049 isl_basic_map_drop_inequality(bmap, k);
1050 --k;
1052 isl_int_init(sum);
1053 for (k = 0; k < bmap->n_ineq-1; ++k) {
1054 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1055 h = hash_index(index, size, bits, bmap, k);
1056 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1057 if (!index[h])
1058 continue;
1059 l = index[h] - &bmap->ineq[0];
1060 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1061 if (isl_int_is_pos(sum)) {
1062 if (detect_divs)
1063 bmap = check_for_div_constraints(bmap, k, l,
1064 sum, progress);
1065 continue;
1067 if (isl_int_is_zero(sum)) {
1068 /* We need to break out of the loop after these
1069 * changes since the contents of the hash
1070 * will no longer be valid.
1071 * Plus, we probably we want to regauss first.
1073 if (progress)
1074 *progress = 1;
1075 isl_basic_map_drop_inequality(bmap, l);
1076 isl_basic_map_inequality_to_equality(bmap, k);
1077 } else
1078 bmap = isl_basic_map_set_to_empty(bmap);
1079 break;
1081 isl_int_clear(sum);
1083 free(index);
1084 return bmap;
1088 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1090 int progress = 1;
1091 if (!bmap)
1092 return NULL;
1093 while (progress) {
1094 progress = 0;
1095 bmap = isl_basic_map_normalize_constraints(bmap);
1096 bmap = remove_duplicate_divs(bmap, &progress);
1097 bmap = eliminate_divs_eq(bmap, &progress);
1098 bmap = eliminate_divs_ineq(bmap, &progress);
1099 bmap = isl_basic_map_gauss(bmap, &progress);
1100 /* requires equalities in normal form */
1101 bmap = normalize_divs(bmap, &progress);
1102 bmap = remove_duplicate_constraints(bmap, &progress, 1);
1104 return bmap;
1107 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1109 return (struct isl_basic_set *)
1110 isl_basic_map_simplify((struct isl_basic_map *)bset);
1114 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1115 isl_int *constraint, unsigned div)
1117 unsigned pos;
1119 if (!bmap)
1120 return -1;
1122 pos = 1 + isl_dim_total(bmap->dim) + div;
1124 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1125 int neg;
1126 isl_int_sub(bmap->div[div][1],
1127 bmap->div[div][1], bmap->div[div][0]);
1128 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1129 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1130 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1131 isl_int_add(bmap->div[div][1],
1132 bmap->div[div][1], bmap->div[div][0]);
1133 if (!neg)
1134 return 0;
1135 if (isl_seq_first_non_zero(constraint+pos+1,
1136 bmap->n_div-div-1) != -1)
1137 return 0;
1138 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1139 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1140 return 0;
1141 if (isl_seq_first_non_zero(constraint+pos+1,
1142 bmap->n_div-div-1) != -1)
1143 return 0;
1144 } else
1145 return 0;
1147 return 1;
1151 /* If the only constraints a div d=floor(f/m)
1152 * appears in are its two defining constraints
1154 * f - m d >=0
1155 * -(f - (m - 1)) + m d >= 0
1157 * then it can safely be removed.
1159 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1161 int i;
1162 unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
1164 for (i = 0; i < bmap->n_eq; ++i)
1165 if (!isl_int_is_zero(bmap->eq[i][pos]))
1166 return 0;
1168 for (i = 0; i < bmap->n_ineq; ++i) {
1169 if (isl_int_is_zero(bmap->ineq[i][pos]))
1170 continue;
1171 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1172 return 0;
1175 for (i = 0; i < bmap->n_div; ++i)
1176 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1177 return 0;
1179 return 1;
1183 * Remove divs that don't occur in any of the constraints or other divs.
1184 * These can arise when dropping some of the variables in a quast
1185 * returned by piplib.
1187 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1189 int i;
1191 if (!bmap)
1192 return NULL;
1194 for (i = bmap->n_div-1; i >= 0; --i) {
1195 if (!div_is_redundant(bmap, i))
1196 continue;
1197 bmap = isl_basic_map_drop_div(bmap, i);
1199 return bmap;
1202 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1204 bmap = remove_redundant_divs(bmap);
1205 if (!bmap)
1206 return NULL;
1207 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1208 return bmap;
1211 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1213 return (struct isl_basic_set *)
1214 isl_basic_map_finalize((struct isl_basic_map *)bset);
1217 struct isl_set *isl_set_finalize(struct isl_set *set)
1219 int i;
1221 if (!set)
1222 return NULL;
1223 for (i = 0; i < set->n; ++i) {
1224 set->p[i] = isl_basic_set_finalize(set->p[i]);
1225 if (!set->p[i])
1226 goto error;
1228 return set;
1229 error:
1230 isl_set_free(set);
1231 return NULL;
1234 struct isl_map *isl_map_finalize(struct isl_map *map)
1236 int i;
1238 if (!map)
1239 return NULL;
1240 for (i = 0; i < map->n; ++i) {
1241 map->p[i] = isl_basic_map_finalize(map->p[i]);
1242 if (!map->p[i])
1243 goto error;
1245 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1246 return map;
1247 error:
1248 isl_map_free(map);
1249 return NULL;
1253 /* Remove definition of any div that is defined in terms of the given variable.
1254 * The div itself is not removed. Functions such as
1255 * eliminate_divs_ineq depend on the other divs remaining in place.
1257 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1258 int pos)
1260 int i;
1262 for (i = 0; i < bmap->n_div; ++i) {
1263 if (isl_int_is_zero(bmap->div[i][0]))
1264 continue;
1265 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1266 continue;
1267 isl_int_set_si(bmap->div[i][0], 0);
1269 return bmap;
1272 /* Eliminate the specified variables from the constraints using
1273 * Fourier-Motzkin. The variables themselves are not removed.
1275 struct isl_basic_map *isl_basic_map_eliminate_vars(
1276 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1278 int d;
1279 int i, j, k;
1280 unsigned total;
1282 if (n == 0)
1283 return bmap;
1284 if (!bmap)
1285 return NULL;
1286 total = isl_basic_map_total_dim(bmap);
1288 bmap = isl_basic_map_cow(bmap);
1289 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1290 bmap = remove_dependent_vars(bmap, d);
1292 for (d = pos + n - 1;
1293 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1294 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1295 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1296 int n_lower, n_upper;
1297 if (!bmap)
1298 return NULL;
1299 for (i = 0; i < bmap->n_eq; ++i) {
1300 if (isl_int_is_zero(bmap->eq[i][1+d]))
1301 continue;
1302 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1303 isl_basic_map_drop_equality(bmap, i);
1304 break;
1306 if (i < bmap->n_eq)
1307 continue;
1308 n_lower = 0;
1309 n_upper = 0;
1310 for (i = 0; i < bmap->n_ineq; ++i) {
1311 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1312 n_lower++;
1313 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1314 n_upper++;
1316 bmap = isl_basic_map_extend_constraints(bmap,
1317 0, n_lower * n_upper);
1318 if (!bmap)
1319 goto error;
1320 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1321 int last;
1322 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1323 continue;
1324 last = -1;
1325 for (j = 0; j < i; ++j) {
1326 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1327 continue;
1328 last = j;
1329 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1330 isl_int_sgn(bmap->ineq[j][1+d]))
1331 continue;
1332 k = isl_basic_map_alloc_inequality(bmap);
1333 if (k < 0)
1334 goto error;
1335 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1336 1+total);
1337 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1338 1+d, 1+total, NULL);
1340 isl_basic_map_drop_inequality(bmap, i);
1341 i = last + 1;
1343 if (n_lower > 0 && n_upper > 0) {
1344 bmap = isl_basic_map_normalize_constraints(bmap);
1345 bmap = remove_duplicate_constraints(bmap, NULL, 0);
1346 bmap = isl_basic_map_gauss(bmap, NULL);
1347 bmap = isl_basic_map_remove_redundancies(bmap);
1348 if (!bmap)
1349 goto error;
1350 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1351 break;
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1355 return bmap;
1356 error:
1357 isl_basic_map_free(bmap);
1358 return NULL;
1361 struct isl_basic_set *isl_basic_set_eliminate_vars(
1362 struct isl_basic_set *bset, unsigned pos, unsigned n)
1364 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1365 (struct isl_basic_map *)bset, pos, n);
1368 /* Don't assume equalities are in order, because align_divs
1369 * may have changed the order of the divs.
1371 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1373 int d, i;
1374 unsigned total;
1376 total = isl_dim_total(bmap->dim);
1377 for (d = 0; d < total; ++d)
1378 elim[d] = -1;
1379 for (i = 0; i < bmap->n_eq; ++i) {
1380 for (d = total - 1; d >= 0; --d) {
1381 if (isl_int_is_zero(bmap->eq[i][1+d]))
1382 continue;
1383 elim[d] = i;
1384 break;
1389 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1391 compute_elimination_index((struct isl_basic_map *)bset, elim);
1394 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1395 struct isl_basic_map *bmap, int *elim)
1397 int d;
1398 int copied = 0;
1399 unsigned total;
1401 total = isl_dim_total(bmap->dim);
1402 for (d = total - 1; d >= 0; --d) {
1403 if (isl_int_is_zero(src[1+d]))
1404 continue;
1405 if (elim[d] == -1)
1406 continue;
1407 if (!copied) {
1408 isl_seq_cpy(dst, src, 1 + total);
1409 copied = 1;
1411 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1413 return copied;
1416 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1417 struct isl_basic_set *bset, int *elim)
1419 return reduced_using_equalities(dst, src,
1420 (struct isl_basic_map *)bset, elim);
1423 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1424 struct isl_basic_set *bset, struct isl_basic_set *context)
1426 int i;
1427 int *elim;
1429 if (!bset || !context)
1430 goto error;
1432 if (context->n_eq == 0) {
1433 isl_basic_set_free(context);
1434 return bset;
1437 bset = isl_basic_set_cow(bset);
1438 if (!bset)
1439 goto error;
1441 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1442 if (!elim)
1443 goto error;
1444 set_compute_elimination_index(context, elim);
1445 for (i = 0; i < bset->n_eq; ++i)
1446 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1447 context, elim);
1448 for (i = 0; i < bset->n_ineq; ++i)
1449 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1450 context, elim);
1451 isl_basic_set_free(context);
1452 free(elim);
1453 bset = isl_basic_set_simplify(bset);
1454 bset = isl_basic_set_finalize(bset);
1455 return bset;
1456 error:
1457 isl_basic_set_free(bset);
1458 isl_basic_set_free(context);
1459 return NULL;
1462 static struct isl_basic_set *remove_shifted_constraints(
1463 struct isl_basic_set *bset, struct isl_basic_set *context)
1465 unsigned int size;
1466 isl_int ***index;
1467 int bits;
1468 int k, h, l;
1470 if (!bset)
1471 return NULL;
1473 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1474 bits = ffs(size) - 1;
1475 index = isl_calloc_array(ctx, isl_int **, size);
1476 if (!index)
1477 return bset;
1479 for (k = 0; k < context->n_ineq; ++k) {
1480 h = set_hash_index(index, size, bits, context, k);
1481 index[h] = &context->ineq[k];
1483 for (k = 0; k < bset->n_ineq; ++k) {
1484 h = set_hash_index(index, size, bits, bset, k);
1485 if (!index[h])
1486 continue;
1487 l = index[h] - &context->ineq[0];
1488 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1489 continue;
1490 bset = isl_basic_set_cow(bset);
1491 if (!bset)
1492 goto error;
1493 isl_basic_set_drop_inequality(bset, k);
1494 --k;
1496 free(index);
1497 return bset;
1498 error:
1499 free(index);
1500 return bset;
1503 /* Tighten (decrease) the constant terms of the inequalities based
1504 * on the equalities, without removing any integer points.
1505 * For example, if there is an equality
1507 * i = 3 * j
1509 * and an inequality
1511 * i >= 1
1513 * then we want to replace the inequality by
1515 * i >= 3
1517 * We do this by computing a variable compression and translating
1518 * the constraints to the compressed space.
1519 * If any constraint has coefficients (except the contant term)
1520 * with a common factor "f", then we can replace the constant term "c"
1521 * by
1523 * f * floor(c/f)
1525 * That is, we add
1527 * f * floor(c/f) - c = -fract(c/f)
1529 * and we can add the same value to the original constraint.
1531 * In the example, the compressed space only contains "j",
1532 * and the inequality translates to
1534 * 3 * j - 1 >= 0
1536 * We add -fract(-1/3) = -2 to the original constraint to obtain
1538 * i - 3 >= 0
1540 static struct isl_basic_set *normalize_constraints_in_compressed_space(
1541 struct isl_basic_set *bset)
1543 int i;
1544 unsigned total;
1545 struct isl_mat *B, *C;
1546 isl_int gcd;
1548 if (!bset)
1549 return NULL;
1551 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1552 return bset;
1554 if (!bset->n_ineq)
1555 return bset;
1557 bset = isl_basic_set_cow(bset);
1558 if (!bset)
1559 return NULL;
1561 total = isl_basic_set_total_dim(bset);
1562 B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
1563 C = isl_mat_variable_compression(B, NULL);
1564 if (!C)
1565 return bset;
1566 if (C->n_col == 0) {
1567 isl_mat_free(C);
1568 return isl_basic_set_set_to_empty(bset);
1570 B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
1571 0, bset->n_ineq, 0, 1 + total);
1572 C = isl_mat_product(B, C);
1573 if (!C)
1574 return bset;
1576 isl_int_init(gcd);
1577 for (i = 0; i < bset->n_ineq; ++i) {
1578 isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd);
1579 if (isl_int_is_one(gcd))
1580 continue;
1581 isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd);
1582 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]);
1584 isl_int_clear(gcd);
1586 isl_mat_free(C);
1588 return bset;
1591 /* Remove all information from bset that is redundant in the context
1592 * of context. Both bset and context are assumed to be full-dimensional.
1594 * We first * remove the inequalities from "bset"
1595 * that are obviously redundant with respect to some inequality in "context".
1597 * If there are any inequalities left, we construct a tableau for
1598 * the context and then add the inequalities of "bset".
1599 * Before adding these inequalities, we freeze all constraints such that
1600 * they won't be considered redundant in terms of the constraints of "bset".
1601 * Then we detect all redundant constraints (among the
1602 * constraints that weren't frozen), first by checking for redundancy in the
1603 * the tableau and then by checking if replacing a constraint by its negation
1604 * would lead to an empty set. This last step is fairly expensive
1605 * and could be optimized by more reuse of the tableau.
1606 * Finally, we update bset according to the results.
1608 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
1609 __isl_take isl_basic_set *context)
1611 int i, k;
1612 isl_basic_set *combined = NULL;
1613 struct isl_tab *tab = NULL;
1614 unsigned context_ineq;
1615 unsigned total;
1617 if (!bset || !context)
1618 goto error;
1620 if (isl_basic_set_is_universe(bset)) {
1621 isl_basic_set_free(context);
1622 return bset;
1625 if (isl_basic_set_is_universe(context)) {
1626 isl_basic_set_free(context);
1627 return bset;
1630 bset = remove_shifted_constraints(bset, context);
1631 if (!bset)
1632 goto error;
1633 if (bset->n_ineq == 0)
1634 goto done;
1636 context_ineq = context->n_ineq;
1637 combined = isl_basic_set_cow(isl_basic_set_copy(context));
1638 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
1639 tab = isl_tab_from_basic_set(combined);
1640 for (i = 0; i < context_ineq; ++i)
1641 if (isl_tab_freeze_constraint(tab, i) < 0)
1642 goto error;
1643 tab = isl_tab_extend(tab, bset->n_ineq);
1644 for (i = 0; i < bset->n_ineq; ++i)
1645 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
1646 goto error;
1647 bset = isl_basic_set_add_constraints(combined, bset, 0);
1648 combined = NULL;
1649 if (!bset)
1650 goto error;
1651 if (isl_tab_detect_redundant(tab) < 0)
1652 goto error;
1653 total = isl_basic_set_total_dim(bset);
1654 for (i = context_ineq; i < bset->n_ineq; ++i) {
1655 int is_empty;
1656 if (tab->con[i].is_redundant)
1657 continue;
1658 tab->con[i].is_redundant = 1;
1659 combined = isl_basic_set_dup(bset);
1660 combined = isl_basic_set_update_from_tab(combined, tab);
1661 combined = isl_basic_set_extend_constraints(combined, 0, 1);
1662 k = isl_basic_set_alloc_inequality(combined);
1663 if (k < 0)
1664 goto error;
1665 isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total);
1666 isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1);
1667 is_empty = isl_basic_set_is_empty(combined);
1668 if (is_empty < 0)
1669 goto error;
1670 isl_basic_set_free(combined);
1671 combined = NULL;
1672 if (!is_empty)
1673 tab->con[i].is_redundant = 0;
1675 for (i = 0; i < context_ineq; ++i)
1676 tab->con[i].is_redundant = 1;
1677 bset = isl_basic_set_update_from_tab(bset, tab);
1678 if (bset) {
1679 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1680 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1683 isl_tab_free(tab);
1684 done:
1685 bset = isl_basic_set_simplify(bset);
1686 bset = isl_basic_set_finalize(bset);
1687 isl_basic_set_free(context);
1688 return bset;
1689 error:
1690 isl_tab_free(tab);
1691 isl_basic_set_free(combined);
1692 isl_basic_set_free(context);
1693 isl_basic_set_free(bset);
1694 return NULL;
1697 /* Remove all information from bset that is redundant in the context
1698 * of context. In particular, equalities that are linear combinations
1699 * of those in context are removed. Then the inequalities that are
1700 * redundant in the context of the equalities and inequalities of
1701 * context are removed.
1703 * We first compute the integer affine hull of the intersection,
1704 * compute the gist inside this affine hull and then add back
1705 * those equalities that are not implied by the context.
1707 * If two constraints are mutually redundant, then uset_gist_full
1708 * will remove the second of those constraints. We therefore first
1709 * sort the constraints so that constraints not involving existentially
1710 * quantified variables are given precedence over those that do.
1711 * We have to perform this sorting before the variable compression,
1712 * because that may effect the order of the variables.
1714 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
1715 __isl_take isl_basic_set *context)
1717 isl_mat *eq;
1718 isl_mat *T, *T2;
1719 isl_basic_set *aff;
1720 isl_basic_set *aff_context;
1721 unsigned total;
1723 if (!bset || !context)
1724 goto error;
1726 bset = isl_basic_set_intersect(bset, isl_basic_set_copy(context));
1727 if (isl_basic_set_fast_is_empty(bset)) {
1728 isl_basic_set_free(context);
1729 return bset;
1731 bset = isl_basic_set_sort_constraints(bset);
1732 aff = isl_basic_set_affine_hull(isl_basic_set_copy(bset));
1733 if (!aff)
1734 goto error;
1735 if (isl_basic_set_fast_is_empty(aff)) {
1736 isl_basic_set_free(aff);
1737 isl_basic_set_free(context);
1738 return bset;
1740 if (aff->n_eq == 0) {
1741 isl_basic_set_free(aff);
1742 return uset_gist_full(bset, context);
1744 total = isl_basic_set_total_dim(bset);
1745 eq = isl_mat_sub_alloc(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
1746 eq = isl_mat_cow(eq);
1747 T = isl_mat_variable_compression(eq, &T2);
1748 if (T && T->n_col == 0) {
1749 isl_mat_free(T);
1750 isl_mat_free(T2);
1751 isl_basic_set_free(context);
1752 isl_basic_set_free(aff);
1753 return isl_basic_set_set_to_empty(bset);
1756 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
1758 bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
1759 context = isl_basic_set_preimage(context, T);
1761 bset = uset_gist_full(bset, context);
1762 bset = isl_basic_set_preimage(bset, T2);
1763 bset = isl_basic_set_intersect(bset, aff);
1764 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
1766 if (bset) {
1767 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1768 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1771 return bset;
1772 error:
1773 isl_basic_set_free(bset);
1774 isl_basic_set_free(context);
1775 return NULL;
1778 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1779 * We simply add the equalities in context to bmap and then do a regular
1780 * div normalizations. Better results can be obtained by normalizing
1781 * only the divs in bmap than do not also appear in context.
1782 * We need to be careful to reduce the divs using the equalities
1783 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1784 * spurious constraints.
1786 static struct isl_basic_map *normalize_divs_in_context(
1787 struct isl_basic_map *bmap, struct isl_basic_map *context)
1789 int i;
1790 unsigned total_context;
1791 int div_eq;
1793 div_eq = n_pure_div_eq(bmap);
1794 if (div_eq == 0)
1795 return bmap;
1797 if (context->n_div > 0)
1798 bmap = isl_basic_map_align_divs(bmap, context);
1800 total_context = isl_basic_map_total_dim(context);
1801 bmap = isl_basic_map_extend_constraints(bmap, context->n_eq, 0);
1802 for (i = 0; i < context->n_eq; ++i) {
1803 int k;
1804 k = isl_basic_map_alloc_equality(bmap);
1805 isl_seq_cpy(bmap->eq[k], context->eq[i], 1 + total_context);
1806 isl_seq_clr(bmap->eq[k] + 1 + total_context,
1807 isl_basic_map_total_dim(bmap) - total_context);
1809 bmap = isl_basic_map_gauss(bmap, NULL);
1810 bmap = normalize_divs(bmap, NULL);
1811 bmap = isl_basic_map_gauss(bmap, NULL);
1812 return bmap;
1815 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1816 struct isl_basic_map *context)
1818 struct isl_basic_set *bset;
1820 if (!bmap || !context)
1821 goto error;
1823 if (isl_basic_map_is_universe(bmap)) {
1824 isl_basic_map_free(context);
1825 return bmap;
1827 if (isl_basic_map_fast_is_empty(context)) {
1828 struct isl_dim *dim = isl_dim_copy(bmap->dim);
1829 isl_basic_map_free(context);
1830 isl_basic_map_free(bmap);
1831 return isl_basic_map_universe(dim);
1833 if (isl_basic_map_fast_is_empty(bmap)) {
1834 isl_basic_map_free(context);
1835 return bmap;
1838 bmap = isl_basic_map_remove_redundancies(bmap);
1839 context = isl_basic_map_remove_redundancies(context);
1841 if (context->n_eq)
1842 bmap = normalize_divs_in_context(bmap, context);
1844 context = isl_basic_map_align_divs(context, bmap);
1845 bmap = isl_basic_map_align_divs(bmap, context);
1847 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1848 isl_basic_map_underlying_set(context));
1850 return isl_basic_map_overlying_set(bset, bmap);
1851 error:
1852 isl_basic_map_free(bmap);
1853 isl_basic_map_free(context);
1854 return NULL;
1858 * Assumes context has no implicit divs.
1860 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
1861 __isl_take isl_basic_map *context)
1863 int i;
1865 if (!map || !context)
1866 goto error;;
1868 if (isl_basic_map_fast_is_empty(context)) {
1869 struct isl_dim *dim = isl_dim_copy(map->dim);
1870 isl_basic_map_free(context);
1871 isl_map_free(map);
1872 return isl_map_universe(dim);
1875 context = isl_basic_map_remove_redundancies(context);
1876 map = isl_map_cow(map);
1877 if (!map || !context)
1878 goto error;;
1879 isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1880 map = isl_map_compute_divs(map);
1881 for (i = 0; i < map->n; ++i)
1882 context = isl_basic_map_align_divs(context, map->p[i]);
1883 for (i = map->n - 1; i >= 0; --i) {
1884 map->p[i] = isl_basic_map_gist(map->p[i],
1885 isl_basic_map_copy(context));
1886 if (!map->p[i])
1887 goto error;
1888 if (isl_basic_map_fast_is_empty(map->p[i])) {
1889 isl_basic_map_free(map->p[i]);
1890 if (i != map->n - 1)
1891 map->p[i] = map->p[map->n - 1];
1892 map->n--;
1895 isl_basic_map_free(context);
1896 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1897 return map;
1898 error:
1899 isl_map_free(map);
1900 isl_basic_map_free(context);
1901 return NULL;
1904 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
1905 __isl_take isl_map *context)
1907 context = isl_map_compute_divs(context);
1908 return isl_map_gist_basic_map(map, isl_map_simple_hull(context));
1911 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1912 struct isl_basic_set *context)
1914 return (struct isl_basic_set *)isl_basic_map_gist(
1915 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1918 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
1919 __isl_take isl_basic_set *context)
1921 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
1922 (struct isl_basic_map *)context);
1925 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
1926 __isl_take isl_set *context)
1928 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1929 (struct isl_map *)context);
1932 /* Quick check to see if two basic maps are disjoint.
1933 * In particular, we reduce the equalities and inequalities of
1934 * one basic map in the context of the equalities of the other
1935 * basic map and check if we get a contradiction.
1937 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1938 struct isl_basic_map *bmap2)
1940 struct isl_vec *v = NULL;
1941 int *elim = NULL;
1942 unsigned total;
1943 int i;
1945 if (!bmap1 || !bmap2)
1946 return -1;
1947 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1948 return -1);
1949 if (bmap1->n_div || bmap2->n_div)
1950 return 0;
1951 if (!bmap1->n_eq && !bmap2->n_eq)
1952 return 0;
1954 total = isl_dim_total(bmap1->dim);
1955 if (total == 0)
1956 return 0;
1957 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1958 if (!v)
1959 goto error;
1960 elim = isl_alloc_array(bmap1->ctx, int, total);
1961 if (!elim)
1962 goto error;
1963 compute_elimination_index(bmap1, elim);
1964 for (i = 0; i < bmap2->n_eq; ++i) {
1965 int reduced;
1966 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1967 bmap1, elim);
1968 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1969 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1970 goto disjoint;
1972 for (i = 0; i < bmap2->n_ineq; ++i) {
1973 int reduced;
1974 reduced = reduced_using_equalities(v->block.data,
1975 bmap2->ineq[i], bmap1, elim);
1976 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1977 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1978 goto disjoint;
1980 compute_elimination_index(bmap2, elim);
1981 for (i = 0; i < bmap1->n_ineq; ++i) {
1982 int reduced;
1983 reduced = reduced_using_equalities(v->block.data,
1984 bmap1->ineq[i], bmap2, elim);
1985 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1986 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1987 goto disjoint;
1989 isl_vec_free(v);
1990 free(elim);
1991 return 0;
1992 disjoint:
1993 isl_vec_free(v);
1994 free(elim);
1995 return 1;
1996 error:
1997 isl_vec_free(v);
1998 free(elim);
1999 return -1;
2002 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
2003 struct isl_basic_set *bset2)
2005 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
2006 (struct isl_basic_map *)bset2);
2009 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
2011 int i, j;
2013 if (!map1 || !map2)
2014 return -1;
2016 if (isl_map_fast_is_equal(map1, map2))
2017 return 0;
2019 for (i = 0; i < map1->n; ++i) {
2020 for (j = 0; j < map2->n; ++j) {
2021 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
2022 map2->p[j]);
2023 if (d != 1)
2024 return d;
2027 return 1;
2030 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
2032 return isl_map_fast_is_disjoint((struct isl_map *)set1,
2033 (struct isl_map *)set2);
2036 /* Check if we can combine a given div with lower bound l and upper
2037 * bound u with some other div and if so return that other div.
2038 * Otherwise return -1.
2040 * We first check that
2041 * - the bounds are opposites of each other (except for the constant
2042 * term)
2043 * - the bounds do not reference any other div
2044 * - no div is defined in terms of this div
2046 * Let m be the size of the range allowed on the div by the bounds.
2047 * That is, the bounds are of the form
2049 * e <= a <= e + m - 1
2051 * with e some expression in the other variables.
2052 * We look for another div b such that no third div is defined in terms
2053 * of this second div b and such that in any constraint that contains
2054 * a (except for the given lower and upper bound), also contains b
2055 * with a coefficient that is m times that of b.
2056 * That is, all constraints (execpt for the lower and upper bound)
2057 * are of the form
2059 * e + f (a + m b) >= 0
2061 * If so, we return b so that "a + m b" can be replaced by
2062 * a single div "c = a + m b".
2064 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
2065 unsigned div, unsigned l, unsigned u)
2067 int i, j;
2068 unsigned dim;
2069 int coalesce = -1;
2071 if (bmap->n_div <= 1)
2072 return -1;
2073 dim = isl_dim_total(bmap->dim);
2074 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
2075 return -1;
2076 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
2077 bmap->n_div - div - 1) != -1)
2078 return -1;
2079 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
2080 dim + bmap->n_div))
2081 return -1;
2083 for (i = 0; i < bmap->n_div; ++i) {
2084 if (isl_int_is_zero(bmap->div[i][0]))
2085 continue;
2086 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
2087 return -1;
2090 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2091 if (isl_int_is_neg(bmap->ineq[l][0])) {
2092 isl_int_sub(bmap->ineq[l][0],
2093 bmap->ineq[l][0], bmap->ineq[u][0]);
2094 bmap = isl_basic_map_copy(bmap);
2095 bmap = isl_basic_map_set_to_empty(bmap);
2096 isl_basic_map_free(bmap);
2097 return -1;
2099 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2100 for (i = 0; i < bmap->n_div; ++i) {
2101 if (i == div)
2102 continue;
2103 if (!pairs[i])
2104 continue;
2105 for (j = 0; j < bmap->n_div; ++j) {
2106 if (isl_int_is_zero(bmap->div[j][0]))
2107 continue;
2108 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
2109 break;
2111 if (j < bmap->n_div)
2112 continue;
2113 for (j = 0; j < bmap->n_ineq; ++j) {
2114 int valid;
2115 if (j == l || j == u)
2116 continue;
2117 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
2118 continue;
2119 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
2120 break;
2121 isl_int_mul(bmap->ineq[j][1 + dim + div],
2122 bmap->ineq[j][1 + dim + div],
2123 bmap->ineq[l][0]);
2124 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
2125 bmap->ineq[j][1 + dim + i]);
2126 isl_int_divexact(bmap->ineq[j][1 + dim + div],
2127 bmap->ineq[j][1 + dim + div],
2128 bmap->ineq[l][0]);
2129 if (!valid)
2130 break;
2132 if (j < bmap->n_ineq)
2133 continue;
2134 coalesce = i;
2135 break;
2137 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2138 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2139 return coalesce;
2142 /* Given a lower and an upper bound on div i, construct an inequality
2143 * that when nonnegative ensures that this pair of bounds always allows
2144 * for an integer value of the given div.
2145 * The lower bound is inequality l, while the upper bound is inequality u.
2146 * The constructed inequality is stored in ineq.
2147 * g, fl, fu are temporary scalars.
2149 * Let the upper bound be
2151 * -n_u a + e_u >= 0
2153 * and the lower bound
2155 * n_l a + e_l >= 0
2157 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2158 * We have
2160 * - f_u e_l <= f_u f_l g a <= f_l e_u
2162 * Since all variables are integer valued, this is equivalent to
2164 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2166 * If this interval is at least f_u f_l g, then it contains at least
2167 * one integer value for a.
2168 * That is, the test constraint is
2170 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2172 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
2173 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
2175 unsigned dim;
2176 dim = isl_dim_total(bmap->dim);
2178 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
2179 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
2180 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
2181 isl_int_neg(fu, fu);
2182 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
2183 1 + dim + bmap->n_div);
2184 isl_int_add(ineq[0], ineq[0], fl);
2185 isl_int_add(ineq[0], ineq[0], fu);
2186 isl_int_sub_ui(ineq[0], ineq[0], 1);
2187 isl_int_mul(g, g, fl);
2188 isl_int_mul(g, g, fu);
2189 isl_int_sub(ineq[0], ineq[0], g);
2192 /* Remove more kinds of divs that are not strictly needed.
2193 * In particular, if all pairs of lower and upper bounds on a div
2194 * are such that they allow at least one integer value of the div,
2195 * the we can eliminate the div using Fourier-Motzkin without
2196 * introducing any spurious solutions.
2198 static struct isl_basic_map *drop_more_redundant_divs(
2199 struct isl_basic_map *bmap, int *pairs, int n)
2201 struct isl_tab *tab = NULL;
2202 struct isl_vec *vec = NULL;
2203 unsigned dim;
2204 int remove = -1;
2205 isl_int g, fl, fu;
2207 isl_int_init(g);
2208 isl_int_init(fl);
2209 isl_int_init(fu);
2211 if (!bmap)
2212 goto error;
2214 dim = isl_dim_total(bmap->dim);
2215 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
2216 if (!vec)
2217 goto error;
2219 tab = isl_tab_from_basic_map(bmap);
2221 while (n > 0) {
2222 int i, l, u;
2223 int best = -1;
2224 enum isl_lp_result res;
2226 for (i = 0; i < bmap->n_div; ++i) {
2227 if (!pairs[i])
2228 continue;
2229 if (best >= 0 && pairs[best] <= pairs[i])
2230 continue;
2231 best = i;
2234 i = best;
2235 for (l = 0; l < bmap->n_ineq; ++l) {
2236 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
2237 continue;
2238 for (u = 0; u < bmap->n_ineq; ++u) {
2239 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
2240 continue;
2241 construct_test_ineq(bmap, i, l, u,
2242 vec->el, g, fl, fu);
2243 res = isl_tab_min(tab, vec->el,
2244 bmap->ctx->one, &g, NULL, 0);
2245 if (res == isl_lp_error)
2246 goto error;
2247 if (res == isl_lp_empty) {
2248 bmap = isl_basic_map_set_to_empty(bmap);
2249 break;
2251 if (res != isl_lp_ok || isl_int_is_neg(g))
2252 break;
2254 if (u < bmap->n_ineq)
2255 break;
2257 if (l == bmap->n_ineq) {
2258 remove = i;
2259 break;
2261 pairs[i] = 0;
2262 --n;
2265 isl_tab_free(tab);
2266 isl_vec_free(vec);
2268 isl_int_clear(g);
2269 isl_int_clear(fl);
2270 isl_int_clear(fu);
2272 free(pairs);
2274 if (remove < 0)
2275 return bmap;
2277 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
2278 return isl_basic_map_drop_redundant_divs(bmap);
2279 error:
2280 free(pairs);
2281 isl_basic_map_free(bmap);
2282 isl_tab_free(tab);
2283 isl_vec_free(vec);
2284 isl_int_clear(g);
2285 isl_int_clear(fl);
2286 isl_int_clear(fu);
2287 return NULL;
2290 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2291 * and the upper bound u, div1 always occurs together with div2 in the form
2292 * (div1 + m div2), where m is the constant range on the variable div1
2293 * allowed by l and u, replace the pair div1 and div2 by a single
2294 * div that is equal to div1 + m div2.
2296 * The new div will appear in the location that contains div2.
2297 * We need to modify all constraints that contain
2298 * div2 = (div - div1) / m
2299 * (If a constraint does not contain div2, it will also not contain div1.)
2300 * If the constraint also contains div1, then we know they appear
2301 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2302 * i.e., the coefficient of div is f.
2304 * Otherwise, we first need to introduce div1 into the constraint.
2305 * Let the l be
2307 * div1 + f >=0
2309 * and u
2311 * -div1 + f' >= 0
2313 * A lower bound on div2
2315 * n div2 + t >= 0
2317 * can be replaced by
2319 * (n * (m div 2 + div1) + m t + n f)/g >= 0
2321 * with g = gcd(m,n).
2322 * An upper bound
2324 * -n div2 + t >= 0
2326 * can be replaced by
2328 * (-n * (m div2 + div1) + m t + n f')/g >= 0
2330 * These constraint are those that we would obtain from eliminating
2331 * div1 using Fourier-Motzkin.
2333 * After all constraints have been modified, we drop the lower and upper
2334 * bound and then drop div1.
2336 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
2337 unsigned div1, unsigned div2, unsigned l, unsigned u)
2339 isl_int a;
2340 isl_int b;
2341 isl_int m;
2342 unsigned dim, total;
2343 int i;
2345 dim = isl_dim_total(bmap->dim);
2346 total = 1 + dim + bmap->n_div;
2348 isl_int_init(a);
2349 isl_int_init(b);
2350 isl_int_init(m);
2351 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
2352 isl_int_add_ui(m, m, 1);
2354 for (i = 0; i < bmap->n_ineq; ++i) {
2355 if (i == l || i == u)
2356 continue;
2357 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
2358 continue;
2359 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
2360 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
2361 isl_int_divexact(a, m, b);
2362 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
2363 if (isl_int_is_pos(b)) {
2364 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2365 b, bmap->ineq[l], total);
2366 } else {
2367 isl_int_neg(b, b);
2368 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2369 b, bmap->ineq[u], total);
2372 isl_int_set(bmap->ineq[i][1 + dim + div2],
2373 bmap->ineq[i][1 + dim + div1]);
2374 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
2377 isl_int_clear(a);
2378 isl_int_clear(b);
2379 isl_int_clear(m);
2380 if (l > u) {
2381 isl_basic_map_drop_inequality(bmap, l);
2382 isl_basic_map_drop_inequality(bmap, u);
2383 } else {
2384 isl_basic_map_drop_inequality(bmap, u);
2385 isl_basic_map_drop_inequality(bmap, l);
2387 bmap = isl_basic_map_drop_div(bmap, div1);
2388 return bmap;
2391 /* First check if we can coalesce any pair of divs and
2392 * then continue with dropping more redundant divs.
2394 * We loop over all pairs of lower and upper bounds on a div
2395 * with coefficient 1 and -1, respectively, check if there
2396 * is any other div "c" with which we can coalesce the div
2397 * and if so, perform the coalescing.
2399 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
2400 struct isl_basic_map *bmap, int *pairs, int n)
2402 int i, l, u;
2403 unsigned dim;
2405 dim = isl_dim_total(bmap->dim);
2407 for (i = 0; i < bmap->n_div; ++i) {
2408 if (!pairs[i])
2409 continue;
2410 for (l = 0; l < bmap->n_ineq; ++l) {
2411 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
2412 continue;
2413 for (u = 0; u < bmap->n_ineq; ++u) {
2414 int c;
2416 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
2417 continue;
2418 c = div_find_coalesce(bmap, pairs, i, l, u);
2419 if (c < 0)
2420 continue;
2421 free(pairs);
2422 bmap = coalesce_divs(bmap, i, c, l, u);
2423 return isl_basic_map_drop_redundant_divs(bmap);
2428 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
2429 return bmap;
2431 return drop_more_redundant_divs(bmap, pairs, n);
2434 /* Remove divs that are not strictly needed.
2435 * In particular, if a div only occurs positively (or negatively)
2436 * in constraints, then it can simply be dropped.
2437 * Also, if a div occurs only occurs in two constraints and if moreover
2438 * those two constraints are opposite to each other, except for the constant
2439 * term and if the sum of the constant terms is such that for any value
2440 * of the other values, there is always at least one integer value of the
2441 * div, i.e., if one plus this sum is greater than or equal to
2442 * the (absolute value) of the coefficent of the div in the constraints,
2443 * then we can also simply drop the div.
2445 * If any divs are left after these simple checks then we move on
2446 * to more complicated cases in drop_more_redundant_divs.
2448 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
2449 struct isl_basic_map *bmap)
2451 int i, j;
2452 unsigned off;
2453 int *pairs = NULL;
2454 int n = 0;
2456 if (!bmap)
2457 goto error;
2459 off = isl_dim_total(bmap->dim);
2460 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
2461 if (!pairs)
2462 goto error;
2464 for (i = 0; i < bmap->n_div; ++i) {
2465 int pos, neg;
2466 int last_pos, last_neg;
2467 int redundant;
2468 int defined;
2470 defined = !isl_int_is_zero(bmap->div[i][0]);
2471 for (j = 0; j < bmap->n_eq; ++j)
2472 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
2473 break;
2474 if (j < bmap->n_eq)
2475 continue;
2476 ++n;
2477 pos = neg = 0;
2478 for (j = 0; j < bmap->n_ineq; ++j) {
2479 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
2480 last_pos = j;
2481 ++pos;
2483 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
2484 last_neg = j;
2485 ++neg;
2488 pairs[i] = pos * neg;
2489 if (pairs[i] == 0) {
2490 for (j = bmap->n_ineq - 1; j >= 0; --j)
2491 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
2492 isl_basic_map_drop_inequality(bmap, j);
2493 bmap = isl_basic_map_drop_div(bmap, i);
2494 free(pairs);
2495 return isl_basic_map_drop_redundant_divs(bmap);
2497 if (pairs[i] != 1)
2498 continue;
2499 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
2500 bmap->ineq[last_neg] + 1,
2501 off + bmap->n_div))
2502 continue;
2504 isl_int_add(bmap->ineq[last_pos][0],
2505 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2506 isl_int_add_ui(bmap->ineq[last_pos][0],
2507 bmap->ineq[last_pos][0], 1);
2508 redundant = isl_int_ge(bmap->ineq[last_pos][0],
2509 bmap->ineq[last_pos][1+off+i]);
2510 isl_int_sub_ui(bmap->ineq[last_pos][0],
2511 bmap->ineq[last_pos][0], 1);
2512 isl_int_sub(bmap->ineq[last_pos][0],
2513 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2514 if (!redundant) {
2515 if (defined ||
2516 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
2517 pairs[i] = 0;
2518 --n;
2519 continue;
2521 bmap = set_div_from_lower_bound(bmap, i, last_pos);
2522 bmap = isl_basic_map_simplify(bmap);
2523 free(pairs);
2524 return isl_basic_map_drop_redundant_divs(bmap);
2526 if (last_pos > last_neg) {
2527 isl_basic_map_drop_inequality(bmap, last_pos);
2528 isl_basic_map_drop_inequality(bmap, last_neg);
2529 } else {
2530 isl_basic_map_drop_inequality(bmap, last_neg);
2531 isl_basic_map_drop_inequality(bmap, last_pos);
2533 bmap = isl_basic_map_drop_div(bmap, i);
2534 free(pairs);
2535 return isl_basic_map_drop_redundant_divs(bmap);
2538 if (n > 0)
2539 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
2541 free(pairs);
2542 return bmap;
2543 error:
2544 free(pairs);
2545 isl_basic_map_free(bmap);
2546 return NULL;
2549 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
2550 struct isl_basic_set *bset)
2552 return (struct isl_basic_set *)
2553 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
2556 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
2558 int i;
2560 if (!map)
2561 return NULL;
2562 for (i = 0; i < map->n; ++i) {
2563 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
2564 if (!map->p[i])
2565 goto error;
2567 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2568 return map;
2569 error:
2570 isl_map_free(map);
2571 return NULL;
2574 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
2576 return (struct isl_set *)
2577 isl_map_drop_redundant_divs((struct isl_map *)set);