isl_ast_build_expr.c: fix typo in comment
[isl.git] / isl_map_simplify.c
blob1861ad0803002794a1b95e47cc1bf822dc40346a
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
28 isl_int *t = bmap->eq[a];
29 bmap->eq[a] = bmap->eq[b];
30 bmap->eq[b] = t;
33 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
35 if (a != b) {
36 isl_int *t = bmap->ineq[a];
37 bmap->ineq[a] = bmap->ineq[b];
38 bmap->ineq[b] = t;
42 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
44 isl_seq_cpy(c, c + n, rem);
45 isl_seq_clr(c + rem, n);
48 /* Drop n dimensions starting at first.
50 * In principle, this frees up some extra variables as the number
51 * of columns remains constant, but we would have to extend
52 * the div array too as the number of rows in this array is assumed
53 * to be equal to extra.
55 struct isl_basic_set *isl_basic_set_drop_dims(
56 struct isl_basic_set *bset, unsigned first, unsigned n)
58 int i;
60 if (!bset)
61 goto error;
63 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
65 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
66 return bset;
68 bset = isl_basic_set_cow(bset);
69 if (!bset)
70 return NULL;
72 for (i = 0; i < bset->n_eq; ++i)
73 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
74 (bset->dim->n_out-first-n)+bset->extra);
76 for (i = 0; i < bset->n_ineq; ++i)
77 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
78 (bset->dim->n_out-first-n)+bset->extra);
80 for (i = 0; i < bset->n_div; ++i)
81 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
82 (bset->dim->n_out-first-n)+bset->extra);
84 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
85 if (!bset->dim)
86 goto error;
88 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
89 bset = isl_basic_set_simplify(bset);
90 return isl_basic_set_finalize(bset);
91 error:
92 isl_basic_set_free(bset);
93 return NULL;
96 struct isl_set *isl_set_drop_dims(
97 struct isl_set *set, unsigned first, unsigned n)
99 int i;
101 if (!set)
102 goto error;
104 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
106 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
107 return set;
108 set = isl_set_cow(set);
109 if (!set)
110 goto error;
111 set->dim = isl_space_drop_outputs(set->dim, first, n);
112 if (!set->dim)
113 goto error;
115 for (i = 0; i < set->n; ++i) {
116 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
117 if (!set->p[i])
118 goto error;
121 ISL_F_CLR(set, ISL_SET_NORMALIZED);
122 return set;
123 error:
124 isl_set_free(set);
125 return NULL;
128 /* Move "n" divs starting at "first" to the end of the list of divs.
130 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
131 unsigned first, unsigned n)
133 isl_int **div;
134 int i;
136 if (first + n == bmap->n_div)
137 return bmap;
139 div = isl_alloc_array(bmap->ctx, isl_int *, n);
140 if (!div)
141 goto error;
142 for (i = 0; i < n; ++i)
143 div[i] = bmap->div[first + i];
144 for (i = 0; i < bmap->n_div - first - n; ++i)
145 bmap->div[first + i] = bmap->div[first + n + i];
146 for (i = 0; i < n; ++i)
147 bmap->div[bmap->n_div - n + i] = div[i];
148 free(div);
149 return bmap;
150 error:
151 isl_basic_map_free(bmap);
152 return NULL;
155 /* Drop "n" dimensions of type "type" starting at "first".
157 * In principle, this frees up some extra variables as the number
158 * of columns remains constant, but we would have to extend
159 * the div array too as the number of rows in this array is assumed
160 * to be equal to extra.
162 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
163 enum isl_dim_type type, unsigned first, unsigned n)
165 int i;
166 unsigned dim;
167 unsigned offset;
168 unsigned left;
170 if (!bmap)
171 goto error;
173 dim = isl_basic_map_dim(bmap, type);
174 isl_assert(bmap->ctx, first + n <= dim, goto error);
176 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
177 return bmap;
179 bmap = isl_basic_map_cow(bmap);
180 if (!bmap)
181 return NULL;
183 offset = isl_basic_map_offset(bmap, type) + first;
184 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
185 for (i = 0; i < bmap->n_eq; ++i)
186 constraint_drop_vars(bmap->eq[i]+offset, n, left);
188 for (i = 0; i < bmap->n_ineq; ++i)
189 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
191 for (i = 0; i < bmap->n_div; ++i)
192 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
194 if (type == isl_dim_div) {
195 bmap = move_divs_last(bmap, first, n);
196 if (!bmap)
197 goto error;
198 isl_basic_map_free_div(bmap, n);
199 } else
200 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
201 if (!bmap->dim)
202 goto error;
204 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
205 bmap = isl_basic_map_simplify(bmap);
206 return isl_basic_map_finalize(bmap);
207 error:
208 isl_basic_map_free(bmap);
209 return NULL;
212 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
213 enum isl_dim_type type, unsigned first, unsigned n)
215 return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset,
216 type, first, n);
219 struct isl_basic_map *isl_basic_map_drop_inputs(
220 struct isl_basic_map *bmap, unsigned first, unsigned n)
222 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
225 struct isl_map *isl_map_drop(struct isl_map *map,
226 enum isl_dim_type type, unsigned first, unsigned n)
228 int i;
230 if (!map)
231 goto error;
233 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
235 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
236 return map;
237 map = isl_map_cow(map);
238 if (!map)
239 goto error;
240 map->dim = isl_space_drop_dims(map->dim, type, first, n);
241 if (!map->dim)
242 goto error;
244 for (i = 0; i < map->n; ++i) {
245 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
246 if (!map->p[i])
247 goto error;
249 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
251 return map;
252 error:
253 isl_map_free(map);
254 return NULL;
257 struct isl_set *isl_set_drop(struct isl_set *set,
258 enum isl_dim_type type, unsigned first, unsigned n)
260 return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
263 struct isl_map *isl_map_drop_inputs(
264 struct isl_map *map, unsigned first, unsigned n)
266 return isl_map_drop(map, isl_dim_in, first, n);
270 * We don't cow, as the div is assumed to be redundant.
272 static struct isl_basic_map *isl_basic_map_drop_div(
273 struct isl_basic_map *bmap, unsigned div)
275 int i;
276 unsigned pos;
278 if (!bmap)
279 goto error;
281 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
283 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
285 for (i = 0; i < bmap->n_eq; ++i)
286 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
288 for (i = 0; i < bmap->n_ineq; ++i) {
289 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
290 isl_basic_map_drop_inequality(bmap, i);
291 --i;
292 continue;
294 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
297 for (i = 0; i < bmap->n_div; ++i)
298 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
300 if (div != bmap->n_div - 1) {
301 int j;
302 isl_int *t = bmap->div[div];
304 for (j = div; j < bmap->n_div - 1; ++j)
305 bmap->div[j] = bmap->div[j+1];
307 bmap->div[bmap->n_div - 1] = t;
309 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
310 isl_basic_map_free_div(bmap, 1);
312 return bmap;
313 error:
314 isl_basic_map_free(bmap);
315 return NULL;
318 struct isl_basic_map *isl_basic_map_normalize_constraints(
319 struct isl_basic_map *bmap)
321 int i;
322 isl_int gcd;
323 unsigned total = isl_basic_map_total_dim(bmap);
325 if (!bmap)
326 return NULL;
328 isl_int_init(gcd);
329 for (i = bmap->n_eq - 1; i >= 0; --i) {
330 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
331 if (isl_int_is_zero(gcd)) {
332 if (!isl_int_is_zero(bmap->eq[i][0])) {
333 bmap = isl_basic_map_set_to_empty(bmap);
334 break;
336 isl_basic_map_drop_equality(bmap, i);
337 continue;
339 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
340 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
341 if (isl_int_is_one(gcd))
342 continue;
343 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
344 bmap = isl_basic_map_set_to_empty(bmap);
345 break;
347 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
350 for (i = bmap->n_ineq - 1; i >= 0; --i) {
351 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
352 if (isl_int_is_zero(gcd)) {
353 if (isl_int_is_neg(bmap->ineq[i][0])) {
354 bmap = isl_basic_map_set_to_empty(bmap);
355 break;
357 isl_basic_map_drop_inequality(bmap, i);
358 continue;
360 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
361 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
362 if (isl_int_is_one(gcd))
363 continue;
364 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
365 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
367 isl_int_clear(gcd);
369 return bmap;
372 struct isl_basic_set *isl_basic_set_normalize_constraints(
373 struct isl_basic_set *bset)
375 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
376 (struct isl_basic_map *)bset);
379 /* Assuming the variable at position "pos" has an integer coefficient
380 * in integer division "div", extract it from this integer division.
381 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
382 * corresponds to the constant term.
384 * That is, the integer division is of the form
386 * floor((... + c * d * x_pos + ...)/d)
388 * Replace it by
390 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
392 static __isl_give isl_basic_map *remove_var_from_div(
393 __isl_take isl_basic_map *bmap, int div, int pos)
395 isl_int shift;
397 isl_int_init(shift);
398 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
399 isl_int_neg(shift, shift);
400 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
401 isl_int_clear(shift);
403 return bmap;
406 /* Check if integer division "div" has any integral coefficient
407 * (or constant term). If so, extract them from the integer division.
409 static __isl_give isl_basic_map *remove_independent_vars_from_div(
410 __isl_take isl_basic_map *bmap, int div)
412 int i;
413 unsigned total = 1 + isl_basic_map_total_dim(bmap);
415 for (i = 0; i < total; ++i) {
416 if (isl_int_is_zero(bmap->div[div][1 + i]))
417 continue;
418 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
419 bmap->div[div][0]))
420 continue;
421 bmap = remove_var_from_div(bmap, div, i);
422 if (!bmap)
423 break;
426 return bmap;
429 /* Check if any known integer division has any integral coefficient
430 * (or constant term). If so, extract them from the integer division.
432 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
433 __isl_take isl_basic_map *bmap)
435 int i;
437 if (!bmap)
438 return NULL;
439 if (bmap->n_div == 0)
440 return bmap;
442 for (i = 0; i < bmap->n_div; ++i) {
443 if (isl_int_is_zero(bmap->div[i][0]))
444 continue;
445 bmap = remove_independent_vars_from_div(bmap, i);
446 if (!bmap)
447 break;
450 return bmap;
453 /* Remove any common factor in numerator and denominator of the div expression,
454 * not taking into account the constant term.
455 * That is, if the div is of the form
457 * floor((a + m f(x))/(m d))
459 * then replace it by
461 * floor((floor(a/m) + f(x))/d)
463 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
464 * and can therefore not influence the result of the floor.
466 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
468 unsigned total = isl_basic_map_total_dim(bmap);
469 isl_ctx *ctx = bmap->ctx;
471 if (isl_int_is_zero(bmap->div[div][0]))
472 return;
473 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
474 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
475 if (isl_int_is_one(ctx->normalize_gcd))
476 return;
477 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
478 ctx->normalize_gcd);
479 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
480 ctx->normalize_gcd);
481 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
482 ctx->normalize_gcd, total);
485 /* Remove any common factor in numerator and denominator of a div expression,
486 * not taking into account the constant term.
487 * That is, look for any div of the form
489 * floor((a + m f(x))/(m d))
491 * and replace it by
493 * floor((floor(a/m) + f(x))/d)
495 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
496 * and can therefore not influence the result of the floor.
498 static __isl_give isl_basic_map *normalize_div_expressions(
499 __isl_take isl_basic_map *bmap)
501 int i;
503 if (!bmap)
504 return NULL;
505 if (bmap->n_div == 0)
506 return bmap;
508 for (i = 0; i < bmap->n_div; ++i)
509 normalize_div_expression(bmap, i);
511 return bmap;
514 /* Assumes divs have been ordered if keep_divs is set.
516 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
517 unsigned pos, isl_int *eq, int keep_divs, int *progress)
519 unsigned total;
520 unsigned space_total;
521 int k;
522 int last_div;
524 total = isl_basic_map_total_dim(bmap);
525 space_total = isl_space_dim(bmap->dim, isl_dim_all);
526 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
527 for (k = 0; k < bmap->n_eq; ++k) {
528 if (bmap->eq[k] == eq)
529 continue;
530 if (isl_int_is_zero(bmap->eq[k][1+pos]))
531 continue;
532 if (progress)
533 *progress = 1;
534 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
535 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
538 for (k = 0; k < bmap->n_ineq; ++k) {
539 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
540 continue;
541 if (progress)
542 *progress = 1;
543 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
544 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
545 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
548 for (k = 0; k < bmap->n_div; ++k) {
549 if (isl_int_is_zero(bmap->div[k][0]))
550 continue;
551 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
552 continue;
553 if (progress)
554 *progress = 1;
555 /* We need to be careful about circular definitions,
556 * so for now we just remove the definition of div k
557 * if the equality contains any divs.
558 * If keep_divs is set, then the divs have been ordered
559 * and we can keep the definition as long as the result
560 * is still ordered.
562 if (last_div == -1 || (keep_divs && last_div < k)) {
563 isl_seq_elim(bmap->div[k]+1, eq,
564 1+pos, 1+total, &bmap->div[k][0]);
565 normalize_div_expression(bmap, k);
566 } else
567 isl_seq_clr(bmap->div[k], 1 + total);
568 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
572 /* Assumes divs have been ordered if keep_divs is set.
574 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
575 isl_int *eq, unsigned div, int keep_divs)
577 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
579 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
581 bmap = isl_basic_map_drop_div(bmap, div);
583 return bmap;
586 /* Check if elimination of div "div" using equality "eq" would not
587 * result in a div depending on a later div.
589 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
590 unsigned div)
592 int k;
593 int last_div;
594 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
595 unsigned pos = space_total + div;
597 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
598 if (last_div < 0 || last_div <= div)
599 return 1;
601 for (k = 0; k <= last_div; ++k) {
602 if (isl_int_is_zero(bmap->div[k][0]))
603 return 1;
604 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
605 return 0;
608 return 1;
611 /* Elimininate divs based on equalities
613 static struct isl_basic_map *eliminate_divs_eq(
614 struct isl_basic_map *bmap, int *progress)
616 int d;
617 int i;
618 int modified = 0;
619 unsigned off;
621 bmap = isl_basic_map_order_divs(bmap);
623 if (!bmap)
624 return NULL;
626 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
628 for (d = bmap->n_div - 1; d >= 0 ; --d) {
629 for (i = 0; i < bmap->n_eq; ++i) {
630 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
631 !isl_int_is_negone(bmap->eq[i][off + d]))
632 continue;
633 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
634 continue;
635 modified = 1;
636 *progress = 1;
637 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
638 if (isl_basic_map_drop_equality(bmap, i) < 0)
639 return isl_basic_map_free(bmap);
640 break;
643 if (modified)
644 return eliminate_divs_eq(bmap, progress);
645 return bmap;
648 /* Elimininate divs based on inequalities
650 static struct isl_basic_map *eliminate_divs_ineq(
651 struct isl_basic_map *bmap, int *progress)
653 int d;
654 int i;
655 unsigned off;
656 struct isl_ctx *ctx;
658 if (!bmap)
659 return NULL;
661 ctx = bmap->ctx;
662 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
664 for (d = bmap->n_div - 1; d >= 0 ; --d) {
665 for (i = 0; i < bmap->n_eq; ++i)
666 if (!isl_int_is_zero(bmap->eq[i][off + d]))
667 break;
668 if (i < bmap->n_eq)
669 continue;
670 for (i = 0; i < bmap->n_ineq; ++i)
671 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
672 break;
673 if (i < bmap->n_ineq)
674 continue;
675 *progress = 1;
676 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
677 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
678 break;
679 bmap = isl_basic_map_drop_div(bmap, d);
680 if (!bmap)
681 break;
683 return bmap;
686 struct isl_basic_map *isl_basic_map_gauss(
687 struct isl_basic_map *bmap, int *progress)
689 int k;
690 int done;
691 int last_var;
692 unsigned total_var;
693 unsigned total;
695 bmap = isl_basic_map_order_divs(bmap);
697 if (!bmap)
698 return NULL;
700 total = isl_basic_map_total_dim(bmap);
701 total_var = total - bmap->n_div;
703 last_var = total - 1;
704 for (done = 0; done < bmap->n_eq; ++done) {
705 for (; last_var >= 0; --last_var) {
706 for (k = done; k < bmap->n_eq; ++k)
707 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
708 break;
709 if (k < bmap->n_eq)
710 break;
712 if (last_var < 0)
713 break;
714 if (k != done)
715 swap_equality(bmap, k, done);
716 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
717 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
719 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
720 progress);
722 if (last_var >= total_var &&
723 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
724 unsigned div = last_var - total_var;
725 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
726 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
727 isl_int_set(bmap->div[div][0],
728 bmap->eq[done][1+last_var]);
729 if (progress)
730 *progress = 1;
731 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
734 if (done == bmap->n_eq)
735 return bmap;
736 for (k = done; k < bmap->n_eq; ++k) {
737 if (isl_int_is_zero(bmap->eq[k][0]))
738 continue;
739 return isl_basic_map_set_to_empty(bmap);
741 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
742 return bmap;
745 struct isl_basic_set *isl_basic_set_gauss(
746 struct isl_basic_set *bset, int *progress)
748 return (struct isl_basic_set*)isl_basic_map_gauss(
749 (struct isl_basic_map *)bset, progress);
753 static unsigned int round_up(unsigned int v)
755 int old_v = v;
757 while (v) {
758 old_v = v;
759 v ^= v & -v;
761 return old_v << 1;
764 /* Hash table of inequalities in a basic map.
765 * "index" is an array of addresses of inequalities in the basic map, some
766 * of which are NULL. The inequalities are hashed on the coefficients
767 * except the constant term.
768 * "size" is the number of elements in the array and is always a power of two
769 * "bits" is the number of bits need to represent an index into the array.
770 * "total" is the total dimension of the basic map.
772 struct isl_constraint_index {
773 unsigned int size;
774 int bits;
775 isl_int ***index;
776 unsigned total;
779 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
781 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
782 __isl_keep isl_basic_map *bmap)
784 isl_ctx *ctx;
786 ci->index = NULL;
787 if (!bmap)
788 return isl_stat_error;
789 ci->total = isl_basic_set_total_dim(bmap);
790 if (bmap->n_ineq == 0)
791 return isl_stat_ok;
792 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
793 ci->bits = ffs(ci->size) - 1;
794 ctx = isl_basic_map_get_ctx(bmap);
795 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
796 if (!ci->index)
797 return isl_stat_error;
799 return isl_stat_ok;
802 /* Free the memory allocated by create_constraint_index.
804 static void constraint_index_free(struct isl_constraint_index *ci)
806 free(ci->index);
809 /* Return the position in ci->index that contains the address of
810 * an inequality that is equal to *ineq up to the constant term,
811 * provided this address is not identical to "ineq".
812 * If there is no such inequality, then return the position where
813 * such an inequality should be inserted.
815 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
817 int h;
818 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
819 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
820 if (ineq != ci->index[h] &&
821 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
822 break;
823 return h;
826 /* Return the position in ci->index that contains the address of
827 * an inequality that is equal to the k'th inequality of "bmap"
828 * up to the constant term, provided it does not point to the very
829 * same inequality.
830 * If there is no such inequality, then return the position where
831 * such an inequality should be inserted.
833 static int hash_index(struct isl_constraint_index *ci,
834 __isl_keep isl_basic_map *bmap, int k)
836 return hash_index_ineq(ci, &bmap->ineq[k]);
839 static int set_hash_index(struct isl_constraint_index *ci,
840 struct isl_basic_set *bset, int k)
842 return hash_index(ci, bset, k);
845 /* Fill in the "ci" data structure with the inequalities of "bset".
847 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
848 __isl_keep isl_basic_set *bset)
850 int k, h;
852 if (create_constraint_index(ci, bset) < 0)
853 return isl_stat_error;
855 for (k = 0; k < bset->n_ineq; ++k) {
856 h = set_hash_index(ci, bset, k);
857 ci->index[h] = &bset->ineq[k];
860 return isl_stat_ok;
863 /* Is the inequality ineq (obviously) redundant with respect
864 * to the constraints in "ci"?
866 * Look for an inequality in "ci" with the same coefficients and then
867 * check if the contant term of "ineq" is greater than or equal
868 * to the constant term of that inequality. If so, "ineq" is clearly
869 * redundant.
871 * Note that hash_index_ineq ignores a stored constraint if it has
872 * the same address as the passed inequality. It is ok to pass
873 * the address of a local variable here since it will never be
874 * the same as the address of a constraint in "ci".
876 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
877 isl_int *ineq)
879 int h;
881 h = hash_index_ineq(ci, &ineq);
882 if (!ci->index[h])
883 return isl_bool_false;
884 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
887 /* If we can eliminate more than one div, then we need to make
888 * sure we do it from last div to first div, in order not to
889 * change the position of the other divs that still need to
890 * be removed.
892 static struct isl_basic_map *remove_duplicate_divs(
893 struct isl_basic_map *bmap, int *progress)
895 unsigned int size;
896 int *index;
897 int *elim_for;
898 int k, l, h;
899 int bits;
900 struct isl_blk eq;
901 unsigned total_var;
902 unsigned total;
903 struct isl_ctx *ctx;
905 bmap = isl_basic_map_order_divs(bmap);
906 if (!bmap || bmap->n_div <= 1)
907 return bmap;
909 total_var = isl_space_dim(bmap->dim, isl_dim_all);
910 total = total_var + bmap->n_div;
912 ctx = bmap->ctx;
913 for (k = bmap->n_div - 1; k >= 0; --k)
914 if (!isl_int_is_zero(bmap->div[k][0]))
915 break;
916 if (k <= 0)
917 return bmap;
919 size = round_up(4 * bmap->n_div / 3 - 1);
920 if (size == 0)
921 return bmap;
922 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
923 bits = ffs(size) - 1;
924 index = isl_calloc_array(ctx, int, size);
925 if (!elim_for || !index)
926 goto out;
927 eq = isl_blk_alloc(ctx, 1+total);
928 if (isl_blk_is_error(eq))
929 goto out;
931 isl_seq_clr(eq.data, 1+total);
932 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
933 for (--k; k >= 0; --k) {
934 uint32_t hash;
936 if (isl_int_is_zero(bmap->div[k][0]))
937 continue;
939 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
940 for (h = hash; index[h]; h = (h+1) % size)
941 if (isl_seq_eq(bmap->div[k],
942 bmap->div[index[h]-1], 2+total))
943 break;
944 if (index[h]) {
945 *progress = 1;
946 l = index[h] - 1;
947 elim_for[l] = k + 1;
949 index[h] = k+1;
951 for (l = bmap->n_div - 1; l >= 0; --l) {
952 if (!elim_for[l])
953 continue;
954 k = elim_for[l] - 1;
955 isl_int_set_si(eq.data[1+total_var+k], -1);
956 isl_int_set_si(eq.data[1+total_var+l], 1);
957 bmap = eliminate_div(bmap, eq.data, l, 1);
958 if (!bmap)
959 break;
960 isl_int_set_si(eq.data[1+total_var+k], 0);
961 isl_int_set_si(eq.data[1+total_var+l], 0);
964 isl_blk_free(ctx, eq);
965 out:
966 free(index);
967 free(elim_for);
968 return bmap;
971 static int n_pure_div_eq(struct isl_basic_map *bmap)
973 int i, j;
974 unsigned total;
976 total = isl_space_dim(bmap->dim, isl_dim_all);
977 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
978 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
979 --j;
980 if (j < 0)
981 break;
982 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
983 return 0;
985 return i;
988 /* Normalize divs that appear in equalities.
990 * In particular, we assume that bmap contains some equalities
991 * of the form
993 * a x = m * e_i
995 * and we want to replace the set of e_i by a minimal set and
996 * such that the new e_i have a canonical representation in terms
997 * of the vector x.
998 * If any of the equalities involves more than one divs, then
999 * we currently simply bail out.
1001 * Let us first additionally assume that all equalities involve
1002 * a div. The equalities then express modulo constraints on the
1003 * remaining variables and we can use "parameter compression"
1004 * to find a minimal set of constraints. The result is a transformation
1006 * x = T(x') = x_0 + G x'
1008 * with G a lower-triangular matrix with all elements below the diagonal
1009 * non-negative and smaller than the diagonal element on the same row.
1010 * We first normalize x_0 by making the same property hold in the affine
1011 * T matrix.
1012 * The rows i of G with a 1 on the diagonal do not impose any modulo
1013 * constraint and simply express x_i = x'_i.
1014 * For each of the remaining rows i, we introduce a div and a corresponding
1015 * equality. In particular
1017 * g_ii e_j = x_i - g_i(x')
1019 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1020 * corresponding div (if g_kk != 1).
1022 * If there are any equalities not involving any div, then we
1023 * first apply a variable compression on the variables x:
1025 * x = C x'' x'' = C_2 x
1027 * and perform the above parameter compression on A C instead of on A.
1028 * The resulting compression is then of the form
1030 * x'' = T(x') = x_0 + G x'
1032 * and in constructing the new divs and the corresponding equalities,
1033 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1034 * by the corresponding row from C_2.
1036 static struct isl_basic_map *normalize_divs(
1037 struct isl_basic_map *bmap, int *progress)
1039 int i, j, k;
1040 int total;
1041 int div_eq;
1042 struct isl_mat *B;
1043 struct isl_vec *d;
1044 struct isl_mat *T = NULL;
1045 struct isl_mat *C = NULL;
1046 struct isl_mat *C2 = NULL;
1047 isl_int v;
1048 int *pos;
1049 int dropped, needed;
1051 if (!bmap)
1052 return NULL;
1054 if (bmap->n_div == 0)
1055 return bmap;
1057 if (bmap->n_eq == 0)
1058 return bmap;
1060 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1061 return bmap;
1063 total = isl_space_dim(bmap->dim, isl_dim_all);
1064 div_eq = n_pure_div_eq(bmap);
1065 if (div_eq == 0)
1066 return bmap;
1068 if (div_eq < bmap->n_eq) {
1069 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1070 bmap->n_eq - div_eq, 0, 1 + total);
1071 C = isl_mat_variable_compression(B, &C2);
1072 if (!C || !C2)
1073 goto error;
1074 if (C->n_col == 0) {
1075 bmap = isl_basic_map_set_to_empty(bmap);
1076 isl_mat_free(C);
1077 isl_mat_free(C2);
1078 goto done;
1082 d = isl_vec_alloc(bmap->ctx, div_eq);
1083 if (!d)
1084 goto error;
1085 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1086 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1087 --j;
1088 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1090 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1092 if (C) {
1093 B = isl_mat_product(B, C);
1094 C = NULL;
1097 T = isl_mat_parameter_compression(B, d);
1098 if (!T)
1099 goto error;
1100 if (T->n_col == 0) {
1101 bmap = isl_basic_map_set_to_empty(bmap);
1102 isl_mat_free(C2);
1103 isl_mat_free(T);
1104 goto done;
1106 isl_int_init(v);
1107 for (i = 0; i < T->n_row - 1; ++i) {
1108 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1109 if (isl_int_is_zero(v))
1110 continue;
1111 isl_mat_col_submul(T, 0, v, 1 + i);
1113 isl_int_clear(v);
1114 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1115 if (!pos)
1116 goto error;
1117 /* We have to be careful because dropping equalities may reorder them */
1118 dropped = 0;
1119 for (j = bmap->n_div - 1; j >= 0; --j) {
1120 for (i = 0; i < bmap->n_eq; ++i)
1121 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1122 break;
1123 if (i < bmap->n_eq) {
1124 bmap = isl_basic_map_drop_div(bmap, j);
1125 isl_basic_map_drop_equality(bmap, i);
1126 ++dropped;
1129 pos[0] = 0;
1130 needed = 0;
1131 for (i = 1; i < T->n_row; ++i) {
1132 if (isl_int_is_one(T->row[i][i]))
1133 pos[i] = i;
1134 else
1135 needed++;
1137 if (needed > dropped) {
1138 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1139 needed, needed, 0);
1140 if (!bmap)
1141 goto error;
1143 for (i = 1; i < T->n_row; ++i) {
1144 if (isl_int_is_one(T->row[i][i]))
1145 continue;
1146 k = isl_basic_map_alloc_div(bmap);
1147 pos[i] = 1 + total + k;
1148 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1149 isl_int_set(bmap->div[k][0], T->row[i][i]);
1150 if (C2)
1151 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1152 else
1153 isl_int_set_si(bmap->div[k][1 + i], 1);
1154 for (j = 0; j < i; ++j) {
1155 if (isl_int_is_zero(T->row[i][j]))
1156 continue;
1157 if (pos[j] < T->n_row && C2)
1158 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1159 C2->row[pos[j]], 1 + total);
1160 else
1161 isl_int_neg(bmap->div[k][1 + pos[j]],
1162 T->row[i][j]);
1164 j = isl_basic_map_alloc_equality(bmap);
1165 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1166 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1168 free(pos);
1169 isl_mat_free(C2);
1170 isl_mat_free(T);
1172 if (progress)
1173 *progress = 1;
1174 done:
1175 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1177 return bmap;
1178 error:
1179 isl_mat_free(C);
1180 isl_mat_free(C2);
1181 isl_mat_free(T);
1182 return bmap;
1185 static struct isl_basic_map *set_div_from_lower_bound(
1186 struct isl_basic_map *bmap, int div, int ineq)
1188 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1190 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1191 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1192 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1193 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1194 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1196 return bmap;
1199 /* Check whether it is ok to define a div based on an inequality.
1200 * To avoid the introduction of circular definitions of divs, we
1201 * do not allow such a definition if the resulting expression would refer to
1202 * any other undefined divs or if any known div is defined in
1203 * terms of the unknown div.
1205 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1206 int div, int ineq)
1208 int j;
1209 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1211 /* Not defined in terms of unknown divs */
1212 for (j = 0; j < bmap->n_div; ++j) {
1213 if (div == j)
1214 continue;
1215 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1216 continue;
1217 if (isl_int_is_zero(bmap->div[j][0]))
1218 return 0;
1221 /* No other div defined in terms of this one => avoid loops */
1222 for (j = 0; j < bmap->n_div; ++j) {
1223 if (div == j)
1224 continue;
1225 if (isl_int_is_zero(bmap->div[j][0]))
1226 continue;
1227 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1228 return 0;
1231 return 1;
1234 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1235 * be a better expression than the current one?
1237 * If we do not have any expression yet, then any expression would be better.
1238 * Otherwise we check if the last variable involved in the inequality
1239 * (disregarding the div that it would define) is in an earlier position
1240 * than the last variable involved in the current div expression.
1242 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1243 int div, int ineq)
1245 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1246 int last_div;
1247 int last_ineq;
1249 if (isl_int_is_zero(bmap->div[div][0]))
1250 return 1;
1252 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1253 bmap->n_div - (div + 1)) >= 0)
1254 return 0;
1256 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1257 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1258 total + bmap->n_div);
1260 return last_ineq < last_div;
1263 /* Given two constraints "k" and "l" that are opposite to each other,
1264 * except for the constant term, check if we can use them
1265 * to obtain an expression for one of the hitherto unknown divs or
1266 * a "better" expression for a div for which we already have an expression.
1267 * "sum" is the sum of the constant terms of the constraints.
1268 * If this sum is strictly smaller than the coefficient of one
1269 * of the divs, then this pair can be used define the div.
1270 * To avoid the introduction of circular definitions of divs, we
1271 * do not use the pair if the resulting expression would refer to
1272 * any other undefined divs or if any known div is defined in
1273 * terms of the unknown div.
1275 static struct isl_basic_map *check_for_div_constraints(
1276 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1278 int i;
1279 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1281 for (i = 0; i < bmap->n_div; ++i) {
1282 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1283 continue;
1284 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1285 continue;
1286 if (!better_div_constraint(bmap, i, k))
1287 continue;
1288 if (!ok_to_set_div_from_bound(bmap, i, k))
1289 break;
1290 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1291 bmap = set_div_from_lower_bound(bmap, i, k);
1292 else
1293 bmap = set_div_from_lower_bound(bmap, i, l);
1294 if (progress)
1295 *progress = 1;
1296 break;
1298 return bmap;
1301 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1302 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1304 struct isl_constraint_index ci;
1305 int k, l, h;
1306 unsigned total = isl_basic_map_total_dim(bmap);
1307 isl_int sum;
1309 if (!bmap || bmap->n_ineq <= 1)
1310 return bmap;
1312 if (create_constraint_index(&ci, bmap) < 0)
1313 return bmap;
1315 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1316 ci.index[h] = &bmap->ineq[0];
1317 for (k = 1; k < bmap->n_ineq; ++k) {
1318 h = hash_index(&ci, bmap, k);
1319 if (!ci.index[h]) {
1320 ci.index[h] = &bmap->ineq[k];
1321 continue;
1323 if (progress)
1324 *progress = 1;
1325 l = ci.index[h] - &bmap->ineq[0];
1326 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1327 swap_inequality(bmap, k, l);
1328 isl_basic_map_drop_inequality(bmap, k);
1329 --k;
1331 isl_int_init(sum);
1332 for (k = 0; k < bmap->n_ineq-1; ++k) {
1333 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1334 h = hash_index(&ci, bmap, k);
1335 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1336 if (!ci.index[h])
1337 continue;
1338 l = ci.index[h] - &bmap->ineq[0];
1339 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1340 if (isl_int_is_pos(sum)) {
1341 if (detect_divs)
1342 bmap = check_for_div_constraints(bmap, k, l,
1343 sum, progress);
1344 continue;
1346 if (isl_int_is_zero(sum)) {
1347 /* We need to break out of the loop after these
1348 * changes since the contents of the hash
1349 * will no longer be valid.
1350 * Plus, we probably we want to regauss first.
1352 if (progress)
1353 *progress = 1;
1354 isl_basic_map_drop_inequality(bmap, l);
1355 isl_basic_map_inequality_to_equality(bmap, k);
1356 } else
1357 bmap = isl_basic_map_set_to_empty(bmap);
1358 break;
1360 isl_int_clear(sum);
1362 constraint_index_free(&ci);
1363 return bmap;
1366 /* Detect all pairs of inequalities that form an equality.
1368 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1369 * Call it repeatedly while it is making progress.
1371 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1372 __isl_take isl_basic_map *bmap, int *progress)
1374 int duplicate;
1376 do {
1377 duplicate = 0;
1378 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1379 &duplicate, 0);
1380 if (progress && duplicate)
1381 *progress = 1;
1382 } while (duplicate);
1384 return bmap;
1387 /* Eliminate knowns divs from constraints where they appear with
1388 * a (positive or negative) unit coefficient.
1390 * That is, replace
1392 * floor(e/m) + f >= 0
1394 * by
1396 * e + m f >= 0
1398 * and
1400 * -floor(e/m) + f >= 0
1402 * by
1404 * -e + m f + m - 1 >= 0
1406 * The first conversion is valid because floor(e/m) >= -f is equivalent
1407 * to e/m >= -f because -f is an integral expression.
1408 * The second conversion follows from the fact that
1410 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1413 * Note that one of the div constraints may have been eliminated
1414 * due to being redundant with respect to the constraint that is
1415 * being modified by this function. The modified constraint may
1416 * no longer imply this div constraint, so we add it back to make
1417 * sure we do not lose any information.
1419 * We skip integral divs, i.e., those with denominator 1, as we would
1420 * risk eliminating the div from the div constraints. We do not need
1421 * to handle those divs here anyway since the div constraints will turn
1422 * out to form an equality and this equality can then be use to eliminate
1423 * the div from all constraints.
1425 static __isl_give isl_basic_map *eliminate_unit_divs(
1426 __isl_take isl_basic_map *bmap, int *progress)
1428 int i, j;
1429 isl_ctx *ctx;
1430 unsigned total;
1432 if (!bmap)
1433 return NULL;
1435 ctx = isl_basic_map_get_ctx(bmap);
1436 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1438 for (i = 0; i < bmap->n_div; ++i) {
1439 if (isl_int_is_zero(bmap->div[i][0]))
1440 continue;
1441 if (isl_int_is_one(bmap->div[i][0]))
1442 continue;
1443 for (j = 0; j < bmap->n_ineq; ++j) {
1444 int s;
1446 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1447 !isl_int_is_negone(bmap->ineq[j][total + i]))
1448 continue;
1450 *progress = 1;
1452 s = isl_int_sgn(bmap->ineq[j][total + i]);
1453 isl_int_set_si(bmap->ineq[j][total + i], 0);
1454 if (s < 0)
1455 isl_seq_combine(bmap->ineq[j],
1456 ctx->negone, bmap->div[i] + 1,
1457 bmap->div[i][0], bmap->ineq[j],
1458 total + bmap->n_div);
1459 else
1460 isl_seq_combine(bmap->ineq[j],
1461 ctx->one, bmap->div[i] + 1,
1462 bmap->div[i][0], bmap->ineq[j],
1463 total + bmap->n_div);
1464 if (s < 0) {
1465 isl_int_add(bmap->ineq[j][0],
1466 bmap->ineq[j][0], bmap->div[i][0]);
1467 isl_int_sub_ui(bmap->ineq[j][0],
1468 bmap->ineq[j][0], 1);
1471 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1472 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1473 return isl_basic_map_free(bmap);
1477 return bmap;
1480 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1482 int progress = 1;
1483 if (!bmap)
1484 return NULL;
1485 while (progress) {
1486 progress = 0;
1487 if (!bmap)
1488 break;
1489 if (isl_basic_map_plain_is_empty(bmap))
1490 break;
1491 bmap = isl_basic_map_normalize_constraints(bmap);
1492 bmap = remove_independent_vars_from_divs(bmap);
1493 bmap = normalize_div_expressions(bmap);
1494 bmap = remove_duplicate_divs(bmap, &progress);
1495 bmap = eliminate_unit_divs(bmap, &progress);
1496 bmap = eliminate_divs_eq(bmap, &progress);
1497 bmap = eliminate_divs_ineq(bmap, &progress);
1498 bmap = isl_basic_map_gauss(bmap, &progress);
1499 /* requires equalities in normal form */
1500 bmap = normalize_divs(bmap, &progress);
1501 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1502 &progress, 1);
1503 if (bmap && progress)
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1506 return bmap;
1509 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1511 return (struct isl_basic_set *)
1512 isl_basic_map_simplify((struct isl_basic_map *)bset);
1516 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1517 isl_int *constraint, unsigned div)
1519 unsigned pos;
1521 if (!bmap)
1522 return -1;
1524 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1526 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1527 int neg;
1528 isl_int_sub(bmap->div[div][1],
1529 bmap->div[div][1], bmap->div[div][0]);
1530 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1531 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1532 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1533 isl_int_add(bmap->div[div][1],
1534 bmap->div[div][1], bmap->div[div][0]);
1535 if (!neg)
1536 return 0;
1537 if (isl_seq_first_non_zero(constraint+pos+1,
1538 bmap->n_div-div-1) != -1)
1539 return 0;
1540 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1541 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1542 return 0;
1543 if (isl_seq_first_non_zero(constraint+pos+1,
1544 bmap->n_div-div-1) != -1)
1545 return 0;
1546 } else
1547 return 0;
1549 return 1;
1552 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1553 isl_int *constraint, unsigned div)
1555 return isl_basic_map_is_div_constraint(bset, constraint, div);
1559 /* If the only constraints a div d=floor(f/m)
1560 * appears in are its two defining constraints
1562 * f - m d >=0
1563 * -(f - (m - 1)) + m d >= 0
1565 * then it can safely be removed.
1567 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1569 int i;
1570 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1572 for (i = 0; i < bmap->n_eq; ++i)
1573 if (!isl_int_is_zero(bmap->eq[i][pos]))
1574 return 0;
1576 for (i = 0; i < bmap->n_ineq; ++i) {
1577 if (isl_int_is_zero(bmap->ineq[i][pos]))
1578 continue;
1579 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1580 return 0;
1583 for (i = 0; i < bmap->n_div; ++i) {
1584 if (isl_int_is_zero(bmap->div[i][0]))
1585 continue;
1586 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1587 return 0;
1590 return 1;
1594 * Remove divs that don't occur in any of the constraints or other divs.
1595 * These can arise when dropping constraints from a basic map or
1596 * when the divs of a basic map have been temporarily aligned
1597 * with the divs of another basic map.
1599 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1601 int i;
1603 if (!bmap)
1604 return NULL;
1606 for (i = bmap->n_div-1; i >= 0; --i) {
1607 if (!div_is_redundant(bmap, i))
1608 continue;
1609 bmap = isl_basic_map_drop_div(bmap, i);
1611 return bmap;
1614 /* Mark "bmap" as final, without checking for obviously redundant
1615 * integer divisions. This function should be used when "bmap"
1616 * is known not to involve any such integer divisions.
1618 __isl_give isl_basic_map *isl_basic_map_mark_final(
1619 __isl_take isl_basic_map *bmap)
1621 if (!bmap)
1622 return NULL;
1623 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1624 return bmap;
1627 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1629 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1631 bmap = remove_redundant_divs(bmap);
1632 bmap = isl_basic_map_mark_final(bmap);
1633 return bmap;
1636 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1638 return (struct isl_basic_set *)
1639 isl_basic_map_finalize((struct isl_basic_map *)bset);
1642 struct isl_set *isl_set_finalize(struct isl_set *set)
1644 int i;
1646 if (!set)
1647 return NULL;
1648 for (i = 0; i < set->n; ++i) {
1649 set->p[i] = isl_basic_set_finalize(set->p[i]);
1650 if (!set->p[i])
1651 goto error;
1653 return set;
1654 error:
1655 isl_set_free(set);
1656 return NULL;
1659 struct isl_map *isl_map_finalize(struct isl_map *map)
1661 int i;
1663 if (!map)
1664 return NULL;
1665 for (i = 0; i < map->n; ++i) {
1666 map->p[i] = isl_basic_map_finalize(map->p[i]);
1667 if (!map->p[i])
1668 goto error;
1670 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1671 return map;
1672 error:
1673 isl_map_free(map);
1674 return NULL;
1678 /* Remove definition of any div that is defined in terms of the given variable.
1679 * The div itself is not removed. Functions such as
1680 * eliminate_divs_ineq depend on the other divs remaining in place.
1682 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1683 int pos)
1685 int i;
1687 if (!bmap)
1688 return NULL;
1690 for (i = 0; i < bmap->n_div; ++i) {
1691 if (isl_int_is_zero(bmap->div[i][0]))
1692 continue;
1693 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1694 continue;
1695 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1696 if (!bmap)
1697 return NULL;
1699 return bmap;
1702 /* Eliminate the specified variables from the constraints using
1703 * Fourier-Motzkin. The variables themselves are not removed.
1705 struct isl_basic_map *isl_basic_map_eliminate_vars(
1706 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1708 int d;
1709 int i, j, k;
1710 unsigned total;
1711 int need_gauss = 0;
1713 if (n == 0)
1714 return bmap;
1715 if (!bmap)
1716 return NULL;
1717 total = isl_basic_map_total_dim(bmap);
1719 bmap = isl_basic_map_cow(bmap);
1720 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1721 bmap = remove_dependent_vars(bmap, d);
1722 if (!bmap)
1723 return NULL;
1725 for (d = pos + n - 1;
1726 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1727 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1728 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1729 int n_lower, n_upper;
1730 if (!bmap)
1731 return NULL;
1732 for (i = 0; i < bmap->n_eq; ++i) {
1733 if (isl_int_is_zero(bmap->eq[i][1+d]))
1734 continue;
1735 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1736 isl_basic_map_drop_equality(bmap, i);
1737 need_gauss = 1;
1738 break;
1740 if (i < bmap->n_eq)
1741 continue;
1742 n_lower = 0;
1743 n_upper = 0;
1744 for (i = 0; i < bmap->n_ineq; ++i) {
1745 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1746 n_lower++;
1747 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1748 n_upper++;
1750 bmap = isl_basic_map_extend_constraints(bmap,
1751 0, n_lower * n_upper);
1752 if (!bmap)
1753 goto error;
1754 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1755 int last;
1756 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1757 continue;
1758 last = -1;
1759 for (j = 0; j < i; ++j) {
1760 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1761 continue;
1762 last = j;
1763 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1764 isl_int_sgn(bmap->ineq[j][1+d]))
1765 continue;
1766 k = isl_basic_map_alloc_inequality(bmap);
1767 if (k < 0)
1768 goto error;
1769 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1770 1+total);
1771 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1772 1+d, 1+total, NULL);
1774 isl_basic_map_drop_inequality(bmap, i);
1775 i = last + 1;
1777 if (n_lower > 0 && n_upper > 0) {
1778 bmap = isl_basic_map_normalize_constraints(bmap);
1779 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1780 NULL, 0);
1781 bmap = isl_basic_map_gauss(bmap, NULL);
1782 bmap = isl_basic_map_remove_redundancies(bmap);
1783 need_gauss = 0;
1784 if (!bmap)
1785 goto error;
1786 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1787 break;
1790 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1791 if (need_gauss)
1792 bmap = isl_basic_map_gauss(bmap, NULL);
1793 return bmap;
1794 error:
1795 isl_basic_map_free(bmap);
1796 return NULL;
1799 struct isl_basic_set *isl_basic_set_eliminate_vars(
1800 struct isl_basic_set *bset, unsigned pos, unsigned n)
1802 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1803 (struct isl_basic_map *)bset, pos, n);
1806 /* Eliminate the specified n dimensions starting at first from the
1807 * constraints, without removing the dimensions from the space.
1808 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1809 * Otherwise, they are projected out and the original space is restored.
1811 __isl_give isl_basic_map *isl_basic_map_eliminate(
1812 __isl_take isl_basic_map *bmap,
1813 enum isl_dim_type type, unsigned first, unsigned n)
1815 isl_space *space;
1817 if (!bmap)
1818 return NULL;
1819 if (n == 0)
1820 return bmap;
1822 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1823 isl_die(bmap->ctx, isl_error_invalid,
1824 "index out of bounds", goto error);
1826 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1827 first += isl_basic_map_offset(bmap, type) - 1;
1828 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1829 return isl_basic_map_finalize(bmap);
1832 space = isl_basic_map_get_space(bmap);
1833 bmap = isl_basic_map_project_out(bmap, type, first, n);
1834 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1835 bmap = isl_basic_map_reset_space(bmap, space);
1836 return bmap;
1837 error:
1838 isl_basic_map_free(bmap);
1839 return NULL;
1842 __isl_give isl_basic_set *isl_basic_set_eliminate(
1843 __isl_take isl_basic_set *bset,
1844 enum isl_dim_type type, unsigned first, unsigned n)
1846 return isl_basic_map_eliminate(bset, type, first, n);
1849 /* Remove all constraints from "bmap" that reference any unknown local
1850 * variables (directly or indirectly).
1852 * Dropping all constraints on a local variable will make it redundant,
1853 * so it will get removed implicitly by
1854 * isl_basic_map_drop_constraints_involving_dims. Some other local
1855 * variables may also end up becoming redundant if they only appear
1856 * in constraints together with the unknown local variable.
1857 * Therefore, start over after calling
1858 * isl_basic_map_drop_constraints_involving_dims.
1860 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1861 __isl_take isl_basic_map *bmap)
1863 isl_bool known;
1864 int i, n_div, o_div;
1866 known = isl_basic_map_divs_known(bmap);
1867 if (known < 0)
1868 return isl_basic_map_free(bmap);
1869 if (known)
1870 return bmap;
1872 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1873 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1875 for (i = 0; i < n_div; ++i) {
1876 known = isl_basic_map_div_is_known(bmap, i);
1877 if (known < 0)
1878 return isl_basic_map_free(bmap);
1879 if (known)
1880 continue;
1881 bmap = remove_dependent_vars(bmap, o_div + i);
1882 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1883 isl_dim_div, i, 1);
1884 if (!bmap)
1885 return NULL;
1886 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1887 i = -1;
1890 return bmap;
1893 /* Remove all constraints from "map" that reference any unknown local
1894 * variables (directly or indirectly).
1896 * Since constraints may get dropped from the basic maps,
1897 * they may no longer be disjoint from each other.
1899 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1900 __isl_take isl_map *map)
1902 int i;
1903 isl_bool known;
1905 known = isl_map_divs_known(map);
1906 if (known < 0)
1907 return isl_map_free(map);
1908 if (known)
1909 return map;
1911 map = isl_map_cow(map);
1912 if (!map)
1913 return NULL;
1915 for (i = 0; i < map->n; ++i) {
1916 map->p[i] =
1917 isl_basic_map_drop_constraint_involving_unknown_divs(
1918 map->p[i]);
1919 if (!map->p[i])
1920 return isl_map_free(map);
1923 if (map->n > 1)
1924 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1926 return map;
1929 /* Don't assume equalities are in order, because align_divs
1930 * may have changed the order of the divs.
1932 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1934 int d, i;
1935 unsigned total;
1937 total = isl_space_dim(bmap->dim, isl_dim_all);
1938 for (d = 0; d < total; ++d)
1939 elim[d] = -1;
1940 for (i = 0; i < bmap->n_eq; ++i) {
1941 for (d = total - 1; d >= 0; --d) {
1942 if (isl_int_is_zero(bmap->eq[i][1+d]))
1943 continue;
1944 elim[d] = i;
1945 break;
1950 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1952 compute_elimination_index((struct isl_basic_map *)bset, elim);
1955 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1956 struct isl_basic_map *bmap, int *elim)
1958 int d;
1959 int copied = 0;
1960 unsigned total;
1962 total = isl_space_dim(bmap->dim, isl_dim_all);
1963 for (d = total - 1; d >= 0; --d) {
1964 if (isl_int_is_zero(src[1+d]))
1965 continue;
1966 if (elim[d] == -1)
1967 continue;
1968 if (!copied) {
1969 isl_seq_cpy(dst, src, 1 + total);
1970 copied = 1;
1972 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1974 return copied;
1977 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1978 struct isl_basic_set *bset, int *elim)
1980 return reduced_using_equalities(dst, src,
1981 (struct isl_basic_map *)bset, elim);
1984 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1985 struct isl_basic_set *bset, struct isl_basic_set *context)
1987 int i;
1988 int *elim;
1990 if (!bset || !context)
1991 goto error;
1993 if (context->n_eq == 0) {
1994 isl_basic_set_free(context);
1995 return bset;
1998 bset = isl_basic_set_cow(bset);
1999 if (!bset)
2000 goto error;
2002 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2003 if (!elim)
2004 goto error;
2005 set_compute_elimination_index(context, elim);
2006 for (i = 0; i < bset->n_eq; ++i)
2007 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2008 context, elim);
2009 for (i = 0; i < bset->n_ineq; ++i)
2010 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2011 context, elim);
2012 isl_basic_set_free(context);
2013 free(elim);
2014 bset = isl_basic_set_simplify(bset);
2015 bset = isl_basic_set_finalize(bset);
2016 return bset;
2017 error:
2018 isl_basic_set_free(bset);
2019 isl_basic_set_free(context);
2020 return NULL;
2023 /* For each inequality in "ineq" that is a shifted (more relaxed)
2024 * copy of an inequality in "context", mark the corresponding entry
2025 * in "row" with -1.
2026 * If an inequality only has a non-negative constant term, then
2027 * mark it as well.
2029 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2030 __isl_keep isl_basic_set *context, int *row)
2032 struct isl_constraint_index ci;
2033 int n_ineq;
2034 unsigned total;
2035 int k;
2037 if (!ineq || !context)
2038 return isl_stat_error;
2039 if (context->n_ineq == 0)
2040 return isl_stat_ok;
2041 if (setup_constraint_index(&ci, context) < 0)
2042 return isl_stat_error;
2044 n_ineq = isl_mat_rows(ineq);
2045 total = isl_mat_cols(ineq) - 1;
2046 for (k = 0; k < n_ineq; ++k) {
2047 int l;
2048 isl_bool redundant;
2050 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2051 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2052 row[k] = -1;
2053 continue;
2055 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2056 if (redundant < 0)
2057 goto error;
2058 if (!redundant)
2059 continue;
2060 row[k] = -1;
2062 constraint_index_free(&ci);
2063 return isl_stat_ok;
2064 error:
2065 constraint_index_free(&ci);
2066 return isl_stat_error;
2069 static struct isl_basic_set *remove_shifted_constraints(
2070 struct isl_basic_set *bset, struct isl_basic_set *context)
2072 struct isl_constraint_index ci;
2073 int k;
2075 if (!bset || !context)
2076 return bset;
2078 if (context->n_ineq == 0)
2079 return bset;
2080 if (setup_constraint_index(&ci, context) < 0)
2081 return bset;
2083 for (k = 0; k < bset->n_ineq; ++k) {
2084 isl_bool redundant;
2086 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2087 if (redundant < 0)
2088 goto error;
2089 if (!redundant)
2090 continue;
2091 bset = isl_basic_set_cow(bset);
2092 if (!bset)
2093 goto error;
2094 isl_basic_set_drop_inequality(bset, k);
2095 --k;
2097 constraint_index_free(&ci);
2098 return bset;
2099 error:
2100 constraint_index_free(&ci);
2101 return bset;
2104 /* Remove constraints from "bmap" that are identical to constraints
2105 * in "context" or that are more relaxed (greater constant term).
2107 * We perform the test for shifted copies on the pure constraints
2108 * in remove_shifted_constraints.
2110 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2111 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2113 isl_basic_set *bset, *bset_context;
2115 if (!bmap || !context)
2116 goto error;
2118 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2119 isl_basic_map_free(context);
2120 return bmap;
2123 context = isl_basic_map_align_divs(context, bmap);
2124 bmap = isl_basic_map_align_divs(bmap, context);
2126 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2127 bset_context = isl_basic_map_underlying_set(context);
2128 bset = remove_shifted_constraints(bset, bset_context);
2129 isl_basic_set_free(bset_context);
2131 bmap = isl_basic_map_overlying_set(bset, bmap);
2133 return bmap;
2134 error:
2135 isl_basic_map_free(bmap);
2136 isl_basic_map_free(context);
2137 return NULL;
2140 /* Does the (linear part of a) constraint "c" involve any of the "len"
2141 * "relevant" dimensions?
2143 static int is_related(isl_int *c, int len, int *relevant)
2145 int i;
2147 for (i = 0; i < len; ++i) {
2148 if (!relevant[i])
2149 continue;
2150 if (!isl_int_is_zero(c[i]))
2151 return 1;
2154 return 0;
2157 /* Drop constraints from "bmap" that do not involve any of
2158 * the dimensions marked "relevant".
2160 static __isl_give isl_basic_map *drop_unrelated_constraints(
2161 __isl_take isl_basic_map *bmap, int *relevant)
2163 int i, dim;
2165 dim = isl_basic_map_dim(bmap, isl_dim_all);
2166 for (i = 0; i < dim; ++i)
2167 if (!relevant[i])
2168 break;
2169 if (i >= dim)
2170 return bmap;
2172 for (i = bmap->n_eq - 1; i >= 0; --i)
2173 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2174 bmap = isl_basic_map_cow(bmap);
2175 if (isl_basic_map_drop_equality(bmap, i) < 0)
2176 return isl_basic_map_free(bmap);
2179 for (i = bmap->n_ineq - 1; i >= 0; --i)
2180 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2181 bmap = isl_basic_map_cow(bmap);
2182 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2183 return isl_basic_map_free(bmap);
2186 return bmap;
2189 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2191 * In particular, for any variable involved in the constraint,
2192 * find the actual group id from before and replace the group
2193 * of the corresponding variable by the minimal group of all
2194 * the variables involved in the constraint considered so far
2195 * (if this minimum is smaller) or replace the minimum by this group
2196 * (if the minimum is larger).
2198 * At the end, all the variables in "c" will (indirectly) point
2199 * to the minimal of the groups that they referred to originally.
2201 static void update_groups(int dim, int *group, isl_int *c)
2203 int j;
2204 int min = dim;
2206 for (j = 0; j < dim; ++j) {
2207 if (isl_int_is_zero(c[j]))
2208 continue;
2209 while (group[j] >= 0 && group[group[j]] != group[j])
2210 group[j] = group[group[j]];
2211 if (group[j] == min)
2212 continue;
2213 if (group[j] < min) {
2214 if (min >= 0 && min < dim)
2215 group[min] = group[j];
2216 min = group[j];
2217 } else
2218 group[group[j]] = min;
2222 /* Allocate an array of groups of variables, one for each variable
2223 * in "context", initialized to zero.
2225 static int *alloc_groups(__isl_keep isl_basic_set *context)
2227 isl_ctx *ctx;
2228 int dim;
2230 dim = isl_basic_set_dim(context, isl_dim_set);
2231 ctx = isl_basic_set_get_ctx(context);
2232 return isl_calloc_array(ctx, int, dim);
2235 /* Drop constraints from "bmap" that only involve variables that are
2236 * not related to any of the variables marked with a "-1" in "group".
2238 * We construct groups of variables that collect variables that
2239 * (indirectly) appear in some common constraint of "bmap".
2240 * Each group is identified by the first variable in the group,
2241 * except for the special group of variables that was already identified
2242 * in the input as -1 (or are related to those variables).
2243 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2244 * otherwise the group of i is the group of group[i].
2246 * We first initialize groups for the remaining variables.
2247 * Then we iterate over the constraints of "bmap" and update the
2248 * group of the variables in the constraint by the smallest group.
2249 * Finally, we resolve indirect references to groups by running over
2250 * the variables.
2252 * After computing the groups, we drop constraints that do not involve
2253 * any variables in the -1 group.
2255 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2256 __isl_take isl_basic_map *bmap, __isl_take int *group)
2258 int dim;
2259 int i;
2260 int last;
2262 if (!bmap)
2263 return NULL;
2265 dim = isl_basic_map_dim(bmap, isl_dim_all);
2267 last = -1;
2268 for (i = 0; i < dim; ++i)
2269 if (group[i] >= 0)
2270 last = group[i] = i;
2271 if (last < 0) {
2272 free(group);
2273 return bmap;
2276 for (i = 0; i < bmap->n_eq; ++i)
2277 update_groups(dim, group, bmap->eq[i] + 1);
2278 for (i = 0; i < bmap->n_ineq; ++i)
2279 update_groups(dim, group, bmap->ineq[i] + 1);
2281 for (i = 0; i < dim; ++i)
2282 if (group[i] >= 0)
2283 group[i] = group[group[i]];
2285 for (i = 0; i < dim; ++i)
2286 group[i] = group[i] == -1;
2288 bmap = drop_unrelated_constraints(bmap, group);
2290 free(group);
2291 return bmap;
2294 /* Drop constraints from "context" that are irrelevant for computing
2295 * the gist of "bset".
2297 * In particular, drop constraints in variables that are not related
2298 * to any of the variables involved in the constraints of "bset"
2299 * in the sense that there is no sequence of constraints that connects them.
2301 * We first mark all variables that appear in "bset" as belonging
2302 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2304 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2305 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2307 int *group;
2308 int dim;
2309 int i, j;
2311 if (!context || !bset)
2312 return isl_basic_set_free(context);
2314 group = alloc_groups(context);
2316 if (!group)
2317 return isl_basic_set_free(context);
2319 dim = isl_basic_set_dim(bset, isl_dim_set);
2320 for (i = 0; i < dim; ++i) {
2321 for (j = 0; j < bset->n_eq; ++j)
2322 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2323 break;
2324 if (j < bset->n_eq) {
2325 group[i] = -1;
2326 continue;
2328 for (j = 0; j < bset->n_ineq; ++j)
2329 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2330 break;
2331 if (j < bset->n_ineq)
2332 group[i] = -1;
2335 return isl_basic_map_drop_unrelated_constraints(context, group);
2338 /* Drop constraints from "context" that are irrelevant for computing
2339 * the gist of the inequalities "ineq".
2340 * Inequalities in "ineq" for which the corresponding element of row
2341 * is set to -1 have already been marked for removal and should be ignored.
2343 * In particular, drop constraints in variables that are not related
2344 * to any of the variables involved in "ineq"
2345 * in the sense that there is no sequence of constraints that connects them.
2347 * We first mark all variables that appear in "bset" as belonging
2348 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2350 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2351 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2353 int *group;
2354 int dim;
2355 int i, j, n;
2357 if (!context || !ineq)
2358 return isl_basic_set_free(context);
2360 group = alloc_groups(context);
2362 if (!group)
2363 return isl_basic_set_free(context);
2365 dim = isl_basic_set_dim(context, isl_dim_set);
2366 n = isl_mat_rows(ineq);
2367 for (i = 0; i < dim; ++i) {
2368 for (j = 0; j < n; ++j) {
2369 if (row[j] < 0)
2370 continue;
2371 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2372 break;
2374 if (j < n)
2375 group[i] = -1;
2378 return isl_basic_map_drop_unrelated_constraints(context, group);
2381 /* Do all "n" entries of "row" contain a negative value?
2383 static int all_neg(int *row, int n)
2385 int i;
2387 for (i = 0; i < n; ++i)
2388 if (row[i] >= 0)
2389 return 0;
2391 return 1;
2394 /* Update the inequalities in "bset" based on the information in "row"
2395 * and "tab".
2397 * In particular, the array "row" contains either -1, meaning that
2398 * the corresponding inequality of "bset" is redundant, or the index
2399 * of an inequality in "tab".
2401 * If the row entry is -1, then drop the inequality.
2402 * Otherwise, if the constraint is marked redundant in the tableau,
2403 * then drop the inequality. Similarly, if it is marked as an equality
2404 * in the tableau, then turn the inequality into an equality and
2405 * perform Gaussian elimination.
2407 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2408 __isl_keep int *row, struct isl_tab *tab)
2410 int i;
2411 unsigned n_ineq;
2412 unsigned n_eq;
2413 int found_equality = 0;
2415 if (!bset)
2416 return NULL;
2417 if (tab && tab->empty)
2418 return isl_basic_set_set_to_empty(bset);
2420 n_ineq = bset->n_ineq;
2421 for (i = n_ineq - 1; i >= 0; --i) {
2422 if (row[i] < 0) {
2423 if (isl_basic_set_drop_inequality(bset, i) < 0)
2424 return isl_basic_set_free(bset);
2425 continue;
2427 if (!tab)
2428 continue;
2429 n_eq = tab->n_eq;
2430 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2431 isl_basic_map_inequality_to_equality(bset, i);
2432 found_equality = 1;
2433 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2434 if (isl_basic_set_drop_inequality(bset, i) < 0)
2435 return isl_basic_set_free(bset);
2439 if (found_equality)
2440 bset = isl_basic_set_gauss(bset, NULL);
2441 bset = isl_basic_set_finalize(bset);
2442 return bset;
2445 /* Update the inequalities in "bset" based on the information in "row"
2446 * and "tab" and free all arguments (other than "bset").
2448 static __isl_give isl_basic_set *update_ineq_free(
2449 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2450 __isl_take isl_basic_set *context, __isl_take int *row,
2451 struct isl_tab *tab)
2453 isl_mat_free(ineq);
2454 isl_basic_set_free(context);
2456 bset = update_ineq(bset, row, tab);
2458 free(row);
2459 isl_tab_free(tab);
2460 return bset;
2463 /* Remove all information from bset that is redundant in the context
2464 * of context.
2465 * "ineq" contains the (possibly transformed) inequalities of "bset",
2466 * in the same order.
2467 * The (explicit) equalities of "bset" are assumed to have been taken
2468 * into account by the transformation such that only the inequalities
2469 * are relevant.
2470 * "context" is assumed not to be empty.
2472 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2473 * A value of -1 means that the inequality is obviously redundant and may
2474 * not even appear in "tab".
2476 * We first mark the inequalities of "bset"
2477 * that are obviously redundant with respect to some inequality in "context".
2478 * Then we remove those constraints from "context" that have become
2479 * irrelevant for computing the gist of "bset".
2480 * Note that this removal of constraints cannot be replaced by
2481 * a factorization because factors in "bset" may still be connected
2482 * to each other through constraints in "context".
2484 * If there are any inequalities left, we construct a tableau for
2485 * the context and then add the inequalities of "bset".
2486 * Before adding these inequalities, we freeze all constraints such that
2487 * they won't be considered redundant in terms of the constraints of "bset".
2488 * Then we detect all redundant constraints (among the
2489 * constraints that weren't frozen), first by checking for redundancy in the
2490 * the tableau and then by checking if replacing a constraint by its negation
2491 * would lead to an empty set. This last step is fairly expensive
2492 * and could be optimized by more reuse of the tableau.
2493 * Finally, we update bset according to the results.
2495 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2496 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2498 int i, r;
2499 int *row = NULL;
2500 isl_ctx *ctx;
2501 isl_basic_set *combined = NULL;
2502 struct isl_tab *tab = NULL;
2503 unsigned n_eq, context_ineq;
2504 unsigned total;
2506 if (!bset || !ineq || !context)
2507 goto error;
2509 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2510 isl_basic_set_free(context);
2511 isl_mat_free(ineq);
2512 return bset;
2515 ctx = isl_basic_set_get_ctx(context);
2516 row = isl_calloc_array(ctx, int, bset->n_ineq);
2517 if (!row)
2518 goto error;
2520 if (mark_shifted_constraints(ineq, context, row) < 0)
2521 goto error;
2522 if (all_neg(row, bset->n_ineq))
2523 return update_ineq_free(bset, ineq, context, row, NULL);
2525 context = drop_irrelevant_constraints_marked(context, ineq, row);
2526 if (!context)
2527 goto error;
2528 if (isl_basic_set_plain_is_universe(context))
2529 return update_ineq_free(bset, ineq, context, row, NULL);
2531 n_eq = context->n_eq;
2532 context_ineq = context->n_ineq;
2533 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2534 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2535 tab = isl_tab_from_basic_set(combined, 0);
2536 for (i = 0; i < context_ineq; ++i)
2537 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2538 goto error;
2539 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2540 goto error;
2541 r = context_ineq;
2542 for (i = 0; i < bset->n_ineq; ++i) {
2543 if (row[i] < 0)
2544 continue;
2545 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2546 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2547 goto error;
2548 row[i] = r++;
2550 if (isl_tab_detect_implicit_equalities(tab) < 0)
2551 goto error;
2552 if (isl_tab_detect_redundant(tab) < 0)
2553 goto error;
2554 total = isl_basic_set_total_dim(bset);
2555 for (i = bset->n_ineq - 1; i >= 0; --i) {
2556 isl_basic_set *test;
2557 int is_empty;
2559 if (row[i] < 0)
2560 continue;
2561 r = row[i];
2562 if (tab->con[n_eq + r].is_redundant)
2563 continue;
2564 test = isl_basic_set_dup(combined);
2565 if (isl_inequality_negate(test, r) < 0)
2566 test = isl_basic_set_free(test);
2567 test = isl_basic_set_update_from_tab(test, tab);
2568 is_empty = isl_basic_set_is_empty(test);
2569 isl_basic_set_free(test);
2570 if (is_empty < 0)
2571 goto error;
2572 if (is_empty)
2573 tab->con[n_eq + r].is_redundant = 1;
2575 bset = update_ineq_free(bset, ineq, context, row, tab);
2576 if (bset) {
2577 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2578 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2581 isl_basic_set_free(combined);
2582 return bset;
2583 error:
2584 free(row);
2585 isl_mat_free(ineq);
2586 isl_tab_free(tab);
2587 isl_basic_set_free(combined);
2588 isl_basic_set_free(context);
2589 isl_basic_set_free(bset);
2590 return NULL;
2593 /* Extract the inequalities of "bset" as an isl_mat.
2595 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2597 unsigned total;
2598 isl_ctx *ctx;
2599 isl_mat *ineq;
2601 if (!bset)
2602 return NULL;
2604 ctx = isl_basic_set_get_ctx(bset);
2605 total = isl_basic_set_total_dim(bset);
2606 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2607 0, 1 + total);
2609 return ineq;
2612 /* Remove all information from "bset" that is redundant in the context
2613 * of "context", for the case where both "bset" and "context" are
2614 * full-dimensional.
2616 static __isl_give isl_basic_set *uset_gist_uncompressed(
2617 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2619 isl_mat *ineq;
2621 ineq = extract_ineq(bset);
2622 return uset_gist_full(bset, ineq, context);
2625 /* Remove all information from "bset" that is redundant in the context
2626 * of "context", for the case where the combined equalities of
2627 * "bset" and "context" allow for a compression that can be obtained
2628 * by preapplication of "T".
2630 * "bset" itself is not transformed by "T". Instead, the inequalities
2631 * are extracted from "bset" and those are transformed by "T".
2632 * uset_gist_full then determines which of the transformed inequalities
2633 * are redundant with respect to the transformed "context" and removes
2634 * the corresponding inequalities from "bset".
2636 * After preapplying "T" to the inequalities, any common factor is
2637 * removed from the coefficients. If this results in a tightening
2638 * of the constant term, then the same tightening is applied to
2639 * the corresponding untransformed inequality in "bset".
2640 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2642 * g f'(x) + r >= 0
2644 * with 0 <= r < g, then it is equivalent to
2646 * f'(x) >= 0
2648 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2649 * subspace compressed by T since the latter would be transformed to
2651 * g f'(x) >= 0
2653 static __isl_give isl_basic_set *uset_gist_compressed(
2654 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2655 __isl_take isl_mat *T)
2657 isl_ctx *ctx;
2658 isl_mat *ineq;
2659 int i, n_row, n_col;
2660 isl_int rem;
2662 ineq = extract_ineq(bset);
2663 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2664 context = isl_basic_set_preimage(context, T);
2666 if (!ineq || !context)
2667 goto error;
2668 if (isl_basic_set_plain_is_empty(context)) {
2669 isl_mat_free(ineq);
2670 isl_basic_set_free(context);
2671 return isl_basic_set_set_to_empty(bset);
2674 ctx = isl_mat_get_ctx(ineq);
2675 n_row = isl_mat_rows(ineq);
2676 n_col = isl_mat_cols(ineq);
2677 isl_int_init(rem);
2678 for (i = 0; i < n_row; ++i) {
2679 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2680 if (isl_int_is_zero(ctx->normalize_gcd))
2681 continue;
2682 if (isl_int_is_one(ctx->normalize_gcd))
2683 continue;
2684 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2685 ctx->normalize_gcd, n_col - 1);
2686 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2687 isl_int_fdiv_q(ineq->row[i][0],
2688 ineq->row[i][0], ctx->normalize_gcd);
2689 if (isl_int_is_zero(rem))
2690 continue;
2691 bset = isl_basic_set_cow(bset);
2692 if (!bset)
2693 break;
2694 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2696 isl_int_clear(rem);
2698 return uset_gist_full(bset, ineq, context);
2699 error:
2700 isl_mat_free(ineq);
2701 isl_basic_set_free(context);
2702 isl_basic_set_free(bset);
2703 return NULL;
2706 /* Project "bset" onto the variables that are involved in "template".
2708 static __isl_give isl_basic_set *project_onto_involved(
2709 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2711 int i, n;
2713 if (!bset || !template)
2714 return isl_basic_set_free(bset);
2716 n = isl_basic_set_dim(template, isl_dim_set);
2718 for (i = 0; i < n; ++i) {
2719 isl_bool involved;
2721 involved = isl_basic_set_involves_dims(template,
2722 isl_dim_set, i, 1);
2723 if (involved < 0)
2724 return isl_basic_set_free(bset);
2725 if (involved)
2726 continue;
2727 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2730 return bset;
2733 /* Remove all information from bset that is redundant in the context
2734 * of context. In particular, equalities that are linear combinations
2735 * of those in context are removed. Then the inequalities that are
2736 * redundant in the context of the equalities and inequalities of
2737 * context are removed.
2739 * First of all, we drop those constraints from "context"
2740 * that are irrelevant for computing the gist of "bset".
2741 * Alternatively, we could factorize the intersection of "context" and "bset".
2743 * We first compute the intersection of the integer affine hulls
2744 * of "bset" and "context",
2745 * compute the gist inside this intersection and then reduce
2746 * the constraints with respect to the equalities of the context
2747 * that only involve variables already involved in the input.
2749 * If two constraints are mutually redundant, then uset_gist_full
2750 * will remove the second of those constraints. We therefore first
2751 * sort the constraints so that constraints not involving existentially
2752 * quantified variables are given precedence over those that do.
2753 * We have to perform this sorting before the variable compression,
2754 * because that may effect the order of the variables.
2756 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2757 __isl_take isl_basic_set *context)
2759 isl_mat *eq;
2760 isl_mat *T;
2761 isl_basic_set *aff;
2762 isl_basic_set *aff_context;
2763 unsigned total;
2765 if (!bset || !context)
2766 goto error;
2768 context = drop_irrelevant_constraints(context, bset);
2770 bset = isl_basic_set_detect_equalities(bset);
2771 aff = isl_basic_set_copy(bset);
2772 aff = isl_basic_set_plain_affine_hull(aff);
2773 context = isl_basic_set_detect_equalities(context);
2774 aff_context = isl_basic_set_copy(context);
2775 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2776 aff = isl_basic_set_intersect(aff, aff_context);
2777 if (!aff)
2778 goto error;
2779 if (isl_basic_set_plain_is_empty(aff)) {
2780 isl_basic_set_free(bset);
2781 isl_basic_set_free(context);
2782 return aff;
2784 bset = isl_basic_set_sort_constraints(bset);
2785 if (aff->n_eq == 0) {
2786 isl_basic_set_free(aff);
2787 return uset_gist_uncompressed(bset, context);
2789 total = isl_basic_set_total_dim(bset);
2790 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2791 eq = isl_mat_cow(eq);
2792 T = isl_mat_variable_compression(eq, NULL);
2793 isl_basic_set_free(aff);
2794 if (T && T->n_col == 0) {
2795 isl_mat_free(T);
2796 isl_basic_set_free(context);
2797 return isl_basic_set_set_to_empty(bset);
2800 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2801 aff_context = project_onto_involved(aff_context, bset);
2803 bset = uset_gist_compressed(bset, context, T);
2804 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2806 if (bset) {
2807 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2808 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2811 return bset;
2812 error:
2813 isl_basic_set_free(bset);
2814 isl_basic_set_free(context);
2815 return NULL;
2818 /* Return a basic map that has the same intersection with "context" as "bmap"
2819 * and that is as "simple" as possible.
2821 * The core computation is performed on the pure constraints.
2822 * When we add back the meaning of the integer divisions, we need
2823 * to (re)introduce the div constraints. If we happen to have
2824 * discovered that some of these integer divisions are equal to
2825 * some affine combination of other variables, then these div
2826 * constraints may end up getting simplified in terms of the equalities,
2827 * resulting in extra inequalities on the other variables that
2828 * may have been removed already or that may not even have been
2829 * part of the input. We try and remove those constraints of
2830 * this form that are most obviously redundant with respect to
2831 * the context. We also remove those div constraints that are
2832 * redundant with respect to the other constraints in the result.
2834 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
2835 struct isl_basic_map *context)
2837 isl_basic_set *bset, *eq;
2838 isl_basic_map *eq_bmap;
2839 unsigned total, n_div, extra, n_eq, n_ineq;
2841 if (!bmap || !context)
2842 goto error;
2844 if (isl_basic_map_plain_is_universe(bmap)) {
2845 isl_basic_map_free(context);
2846 return bmap;
2848 if (isl_basic_map_plain_is_empty(context)) {
2849 isl_space *space = isl_basic_map_get_space(bmap);
2850 isl_basic_map_free(bmap);
2851 isl_basic_map_free(context);
2852 return isl_basic_map_universe(space);
2854 if (isl_basic_map_plain_is_empty(bmap)) {
2855 isl_basic_map_free(context);
2856 return bmap;
2859 bmap = isl_basic_map_remove_redundancies(bmap);
2860 context = isl_basic_map_remove_redundancies(context);
2861 if (!context)
2862 goto error;
2864 context = isl_basic_map_align_divs(context, bmap);
2865 n_div = isl_basic_map_dim(context, isl_dim_div);
2866 total = isl_basic_map_dim(bmap, isl_dim_all);
2867 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
2869 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2870 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
2871 bset = uset_gist(bset,
2872 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
2873 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
2875 if (!bset || bset->n_eq == 0 || n_div == 0 ||
2876 isl_basic_set_plain_is_empty(bset)) {
2877 isl_basic_map_free(context);
2878 return isl_basic_map_overlying_set(bset, bmap);
2881 n_eq = bset->n_eq;
2882 n_ineq = bset->n_ineq;
2883 eq = isl_basic_set_copy(bset);
2884 eq = isl_basic_set_cow(eq);
2885 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
2886 eq = isl_basic_set_free(eq);
2887 if (isl_basic_set_free_equality(bset, n_eq) < 0)
2888 bset = isl_basic_set_free(bset);
2890 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
2891 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
2892 bmap = isl_basic_map_overlying_set(bset, bmap);
2893 bmap = isl_basic_map_intersect(bmap, eq_bmap);
2894 bmap = isl_basic_map_remove_redundancies(bmap);
2896 return bmap;
2897 error:
2898 isl_basic_map_free(bmap);
2899 isl_basic_map_free(context);
2900 return NULL;
2904 * Assumes context has no implicit divs.
2906 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
2907 __isl_take isl_basic_map *context)
2909 int i;
2911 if (!map || !context)
2912 goto error;
2914 if (isl_basic_map_plain_is_empty(context)) {
2915 isl_space *space = isl_map_get_space(map);
2916 isl_map_free(map);
2917 isl_basic_map_free(context);
2918 return isl_map_universe(space);
2921 context = isl_basic_map_remove_redundancies(context);
2922 map = isl_map_cow(map);
2923 if (!map || !context)
2924 goto error;
2925 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
2926 map = isl_map_compute_divs(map);
2927 if (!map)
2928 goto error;
2929 for (i = map->n - 1; i >= 0; --i) {
2930 map->p[i] = isl_basic_map_gist(map->p[i],
2931 isl_basic_map_copy(context));
2932 if (!map->p[i])
2933 goto error;
2934 if (isl_basic_map_plain_is_empty(map->p[i])) {
2935 isl_basic_map_free(map->p[i]);
2936 if (i != map->n - 1)
2937 map->p[i] = map->p[map->n - 1];
2938 map->n--;
2941 isl_basic_map_free(context);
2942 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2943 return map;
2944 error:
2945 isl_map_free(map);
2946 isl_basic_map_free(context);
2947 return NULL;
2950 /* Drop all inequalities from "bmap" that also appear in "context".
2951 * "context" is assumed to have only known local variables and
2952 * the initial local variables of "bmap" are assumed to be the same
2953 * as those of "context".
2954 * The constraints of both "bmap" and "context" are assumed
2955 * to have been sorted using isl_basic_map_sort_constraints.
2957 * Run through the inequality constraints of "bmap" and "context"
2958 * in sorted order.
2959 * If a constraint of "bmap" involves variables not in "context",
2960 * then it cannot appear in "context".
2961 * If a matching constraint is found, it is removed from "bmap".
2963 static __isl_give isl_basic_map *drop_inequalities(
2964 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
2966 int i1, i2;
2967 unsigned total, extra;
2969 if (!bmap || !context)
2970 return isl_basic_map_free(bmap);
2972 total = isl_basic_map_total_dim(context);
2973 extra = isl_basic_map_total_dim(bmap) - total;
2975 i1 = bmap->n_ineq - 1;
2976 i2 = context->n_ineq - 1;
2977 while (bmap && i1 >= 0 && i2 >= 0) {
2978 int cmp;
2980 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
2981 extra) != -1) {
2982 --i1;
2983 continue;
2985 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
2986 context->ineq[i2]);
2987 if (cmp < 0) {
2988 --i2;
2989 continue;
2991 if (cmp > 0) {
2992 --i1;
2993 continue;
2995 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
2996 bmap = isl_basic_map_cow(bmap);
2997 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
2998 bmap = isl_basic_map_free(bmap);
3000 --i1;
3001 --i2;
3004 return bmap;
3007 /* Drop all equalities from "bmap" that also appear in "context".
3008 * "context" is assumed to have only known local variables and
3009 * the initial local variables of "bmap" are assumed to be the same
3010 * as those of "context".
3012 * Run through the equality constraints of "bmap" and "context"
3013 * in sorted order.
3014 * If a constraint of "bmap" involves variables not in "context",
3015 * then it cannot appear in "context".
3016 * If a matching constraint is found, it is removed from "bmap".
3018 static __isl_give isl_basic_map *drop_equalities(
3019 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3021 int i1, i2;
3022 unsigned total, extra;
3024 if (!bmap || !context)
3025 return isl_basic_map_free(bmap);
3027 total = isl_basic_map_total_dim(context);
3028 extra = isl_basic_map_total_dim(bmap) - total;
3030 i1 = bmap->n_eq - 1;
3031 i2 = context->n_eq - 1;
3033 while (bmap && i1 >= 0 && i2 >= 0) {
3034 int last1, last2;
3036 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3037 extra) != -1)
3038 break;
3039 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3040 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3041 if (last1 > last2) {
3042 --i2;
3043 continue;
3045 if (last1 < last2) {
3046 --i1;
3047 continue;
3049 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3050 bmap = isl_basic_map_cow(bmap);
3051 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3052 bmap = isl_basic_map_free(bmap);
3054 --i1;
3055 --i2;
3058 return bmap;
3061 /* Remove the constraints in "context" from "bmap".
3062 * "context" is assumed to have explicit representations
3063 * for all local variables.
3065 * First align the divs of "bmap" to those of "context" and
3066 * sort the constraints. Then drop all constraints from "bmap"
3067 * that appear in "context".
3069 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3070 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3072 isl_bool done, known;
3074 done = isl_basic_map_plain_is_universe(context);
3075 if (done == isl_bool_false)
3076 done = isl_basic_map_plain_is_universe(bmap);
3077 if (done == isl_bool_false)
3078 done = isl_basic_map_plain_is_empty(context);
3079 if (done == isl_bool_false)
3080 done = isl_basic_map_plain_is_empty(bmap);
3081 if (done < 0)
3082 goto error;
3083 if (done) {
3084 isl_basic_map_free(context);
3085 return bmap;
3087 known = isl_basic_map_divs_known(context);
3088 if (known < 0)
3089 goto error;
3090 if (!known)
3091 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3092 "context has unknown divs", goto error);
3094 bmap = isl_basic_map_align_divs(bmap, context);
3095 bmap = isl_basic_map_gauss(bmap, NULL);
3096 bmap = isl_basic_map_sort_constraints(bmap);
3097 context = isl_basic_map_sort_constraints(context);
3099 bmap = drop_inequalities(bmap, context);
3100 bmap = drop_equalities(bmap, context);
3102 isl_basic_map_free(context);
3103 bmap = isl_basic_map_finalize(bmap);
3104 return bmap;
3105 error:
3106 isl_basic_map_free(bmap);
3107 isl_basic_map_free(context);
3108 return NULL;
3111 /* Replace "map" by the disjunct at position "pos" and free "context".
3113 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3114 int pos, __isl_take isl_basic_map *context)
3116 isl_basic_map *bmap;
3118 bmap = isl_basic_map_copy(map->p[pos]);
3119 isl_map_free(map);
3120 isl_basic_map_free(context);
3121 return isl_map_from_basic_map(bmap);
3124 /* Remove the constraints in "context" from "map".
3125 * If any of the disjuncts in the result turns out to be the universe,
3126 * the return this universe.
3127 * "context" is assumed to have explicit representations
3128 * for all local variables.
3130 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3131 __isl_take isl_basic_map *context)
3133 int i;
3134 isl_bool univ, known;
3136 univ = isl_basic_map_plain_is_universe(context);
3137 if (univ < 0)
3138 goto error;
3139 if (univ) {
3140 isl_basic_map_free(context);
3141 return map;
3143 known = isl_basic_map_divs_known(context);
3144 if (known < 0)
3145 goto error;
3146 if (!known)
3147 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3148 "context has unknown divs", goto error);
3150 map = isl_map_cow(map);
3151 if (!map)
3152 goto error;
3153 for (i = 0; i < map->n; ++i) {
3154 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3155 isl_basic_map_copy(context));
3156 univ = isl_basic_map_plain_is_universe(map->p[i]);
3157 if (univ < 0)
3158 goto error;
3159 if (univ && map->n > 1)
3160 return replace_by_disjunct(map, i, context);
3163 isl_basic_map_free(context);
3164 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3165 if (map->n > 1)
3166 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3167 return map;
3168 error:
3169 isl_map_free(map);
3170 isl_basic_map_free(context);
3171 return NULL;
3174 /* Return a map that has the same intersection with "context" as "map"
3175 * and that is as "simple" as possible.
3177 * If "map" is already the universe, then we cannot make it any simpler.
3178 * Similarly, if "context" is the universe, then we cannot exploit it
3179 * to simplify "map"
3180 * If "map" and "context" are identical to each other, then we can
3181 * return the corresponding universe.
3183 * If none of these cases apply, we have to work a bit harder.
3184 * During this computation, we make use of a single disjunct context,
3185 * so if the original context consists of more than one disjunct
3186 * then we need to approximate the context by a single disjunct set.
3187 * Simply taking the simple hull may drop constraints that are
3188 * only implicitly available in each disjunct. We therefore also
3189 * look for constraints among those defining "map" that are valid
3190 * for the context. These can then be used to simplify away
3191 * the corresponding constraints in "map".
3193 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3194 __isl_take isl_map *context)
3196 int equal;
3197 int is_universe;
3198 isl_basic_map *hull;
3200 is_universe = isl_map_plain_is_universe(map);
3201 if (is_universe >= 0 && !is_universe)
3202 is_universe = isl_map_plain_is_universe(context);
3203 if (is_universe < 0)
3204 goto error;
3205 if (is_universe) {
3206 isl_map_free(context);
3207 return map;
3210 equal = isl_map_plain_is_equal(map, context);
3211 if (equal < 0)
3212 goto error;
3213 if (equal) {
3214 isl_map *res = isl_map_universe(isl_map_get_space(map));
3215 isl_map_free(map);
3216 isl_map_free(context);
3217 return res;
3220 context = isl_map_compute_divs(context);
3221 if (!context)
3222 goto error;
3223 if (isl_map_n_basic_map(context) == 1) {
3224 hull = isl_map_simple_hull(context);
3225 } else {
3226 isl_ctx *ctx;
3227 isl_map_list *list;
3229 ctx = isl_map_get_ctx(map);
3230 list = isl_map_list_alloc(ctx, 2);
3231 list = isl_map_list_add(list, isl_map_copy(context));
3232 list = isl_map_list_add(list, isl_map_copy(map));
3233 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3234 list);
3236 return isl_map_gist_basic_map(map, hull);
3237 error:
3238 isl_map_free(map);
3239 isl_map_free(context);
3240 return NULL;
3243 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3244 __isl_take isl_map *context)
3246 return isl_map_align_params_map_map_and(map, context, &map_gist);
3249 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3250 struct isl_basic_set *context)
3252 return (struct isl_basic_set *)isl_basic_map_gist(
3253 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
3256 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3257 __isl_take isl_basic_set *context)
3259 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
3260 (struct isl_basic_map *)context);
3263 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3264 __isl_take isl_basic_set *context)
3266 isl_space *space = isl_set_get_space(set);
3267 isl_basic_set *dom_context = isl_basic_set_universe(space);
3268 dom_context = isl_basic_set_intersect_params(dom_context, context);
3269 return isl_set_gist_basic_set(set, dom_context);
3272 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3273 __isl_take isl_set *context)
3275 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
3276 (struct isl_map *)context);
3279 /* Compute the gist of "bmap" with respect to the constraints "context"
3280 * on the domain.
3282 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3283 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3285 isl_space *space = isl_basic_map_get_space(bmap);
3286 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3288 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3289 return isl_basic_map_gist(bmap, bmap_context);
3292 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3293 __isl_take isl_set *context)
3295 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3296 map_context = isl_map_intersect_domain(map_context, context);
3297 return isl_map_gist(map, map_context);
3300 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3301 __isl_take isl_set *context)
3303 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3304 map_context = isl_map_intersect_range(map_context, context);
3305 return isl_map_gist(map, map_context);
3308 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3309 __isl_take isl_set *context)
3311 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3312 map_context = isl_map_intersect_params(map_context, context);
3313 return isl_map_gist(map, map_context);
3316 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3317 __isl_take isl_set *context)
3319 return isl_map_gist_params(set, context);
3322 /* Quick check to see if two basic maps are disjoint.
3323 * In particular, we reduce the equalities and inequalities of
3324 * one basic map in the context of the equalities of the other
3325 * basic map and check if we get a contradiction.
3327 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3328 __isl_keep isl_basic_map *bmap2)
3330 struct isl_vec *v = NULL;
3331 int *elim = NULL;
3332 unsigned total;
3333 int i;
3335 if (!bmap1 || !bmap2)
3336 return isl_bool_error;
3337 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3338 return isl_bool_error);
3339 if (bmap1->n_div || bmap2->n_div)
3340 return isl_bool_false;
3341 if (!bmap1->n_eq && !bmap2->n_eq)
3342 return isl_bool_false;
3344 total = isl_space_dim(bmap1->dim, isl_dim_all);
3345 if (total == 0)
3346 return isl_bool_false;
3347 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3348 if (!v)
3349 goto error;
3350 elim = isl_alloc_array(bmap1->ctx, int, total);
3351 if (!elim)
3352 goto error;
3353 compute_elimination_index(bmap1, elim);
3354 for (i = 0; i < bmap2->n_eq; ++i) {
3355 int reduced;
3356 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3357 bmap1, elim);
3358 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3359 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3360 goto disjoint;
3362 for (i = 0; i < bmap2->n_ineq; ++i) {
3363 int reduced;
3364 reduced = reduced_using_equalities(v->block.data,
3365 bmap2->ineq[i], bmap1, elim);
3366 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3367 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3368 goto disjoint;
3370 compute_elimination_index(bmap2, elim);
3371 for (i = 0; i < bmap1->n_ineq; ++i) {
3372 int reduced;
3373 reduced = reduced_using_equalities(v->block.data,
3374 bmap1->ineq[i], bmap2, elim);
3375 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3376 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3377 goto disjoint;
3379 isl_vec_free(v);
3380 free(elim);
3381 return isl_bool_false;
3382 disjoint:
3383 isl_vec_free(v);
3384 free(elim);
3385 return isl_bool_true;
3386 error:
3387 isl_vec_free(v);
3388 free(elim);
3389 return isl_bool_error;
3392 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3393 __isl_keep isl_basic_set *bset2)
3395 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
3396 (struct isl_basic_map *)bset2);
3399 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3401 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3402 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3403 __isl_keep isl_basic_map *bmap2))
3405 int i, j;
3407 if (!map1 || !map2)
3408 return isl_bool_error;
3410 for (i = 0; i < map1->n; ++i) {
3411 for (j = 0; j < map2->n; ++j) {
3412 isl_bool d = test(map1->p[i], map2->p[j]);
3413 if (d != isl_bool_true)
3414 return d;
3418 return isl_bool_true;
3421 /* Are "map1" and "map2" obviously disjoint, based on information
3422 * that can be derived without looking at the individual basic maps?
3424 * In particular, if one of them is empty or if they live in different spaces
3425 * (ignoring parameters), then they are clearly disjoint.
3427 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3428 __isl_keep isl_map *map2)
3430 isl_bool disjoint;
3431 isl_bool match;
3433 if (!map1 || !map2)
3434 return isl_bool_error;
3436 disjoint = isl_map_plain_is_empty(map1);
3437 if (disjoint < 0 || disjoint)
3438 return disjoint;
3440 disjoint = isl_map_plain_is_empty(map2);
3441 if (disjoint < 0 || disjoint)
3442 return disjoint;
3444 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3445 map2->dim, isl_dim_in);
3446 if (match < 0 || !match)
3447 return match < 0 ? isl_bool_error : isl_bool_true;
3449 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3450 map2->dim, isl_dim_out);
3451 if (match < 0 || !match)
3452 return match < 0 ? isl_bool_error : isl_bool_true;
3454 return isl_bool_false;
3457 /* Are "map1" and "map2" obviously disjoint?
3459 * If one of them is empty or if they live in different spaces (ignoring
3460 * parameters), then they are clearly disjoint.
3461 * This is checked by isl_map_plain_is_disjoint_global.
3463 * If they have different parameters, then we skip any further tests.
3465 * If they are obviously equal, but not obviously empty, then we will
3466 * not be able to detect if they are disjoint.
3468 * Otherwise we check if each basic map in "map1" is obviously disjoint
3469 * from each basic map in "map2".
3471 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3472 __isl_keep isl_map *map2)
3474 isl_bool disjoint;
3475 isl_bool intersect;
3476 isl_bool match;
3478 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3479 if (disjoint < 0 || disjoint)
3480 return disjoint;
3482 match = isl_space_match(map1->dim, isl_dim_param,
3483 map2->dim, isl_dim_param);
3484 if (match < 0 || !match)
3485 return match < 0 ? isl_bool_error : isl_bool_false;
3487 intersect = isl_map_plain_is_equal(map1, map2);
3488 if (intersect < 0 || intersect)
3489 return intersect < 0 ? isl_bool_error : isl_bool_false;
3491 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3494 /* Are "map1" and "map2" disjoint?
3496 * They are disjoint if they are "obviously disjoint" or if one of them
3497 * is empty. Otherwise, they are not disjoint if one of them is universal.
3498 * If the two inputs are (obviously) equal and not empty, then they are
3499 * not disjoint.
3500 * If none of these cases apply, then check if all pairs of basic maps
3501 * are disjoint.
3503 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3505 isl_bool disjoint;
3506 isl_bool intersect;
3508 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3509 if (disjoint < 0 || disjoint)
3510 return disjoint;
3512 disjoint = isl_map_is_empty(map1);
3513 if (disjoint < 0 || disjoint)
3514 return disjoint;
3516 disjoint = isl_map_is_empty(map2);
3517 if (disjoint < 0 || disjoint)
3518 return disjoint;
3520 intersect = isl_map_plain_is_universe(map1);
3521 if (intersect < 0 || intersect)
3522 return intersect < 0 ? isl_bool_error : isl_bool_false;
3524 intersect = isl_map_plain_is_universe(map2);
3525 if (intersect < 0 || intersect)
3526 return intersect < 0 ? isl_bool_error : isl_bool_false;
3528 intersect = isl_map_plain_is_equal(map1, map2);
3529 if (intersect < 0 || intersect)
3530 return isl_bool_not(intersect);
3532 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3535 /* Are "bmap1" and "bmap2" disjoint?
3537 * They are disjoint if they are "obviously disjoint" or if one of them
3538 * is empty. Otherwise, they are not disjoint if one of them is universal.
3539 * If none of these cases apply, we compute the intersection and see if
3540 * the result is empty.
3542 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3543 __isl_keep isl_basic_map *bmap2)
3545 isl_bool disjoint;
3546 isl_bool intersect;
3547 isl_basic_map *test;
3549 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3550 if (disjoint < 0 || disjoint)
3551 return disjoint;
3553 disjoint = isl_basic_map_is_empty(bmap1);
3554 if (disjoint < 0 || disjoint)
3555 return disjoint;
3557 disjoint = isl_basic_map_is_empty(bmap2);
3558 if (disjoint < 0 || disjoint)
3559 return disjoint;
3561 intersect = isl_basic_map_plain_is_universe(bmap1);
3562 if (intersect < 0 || intersect)
3563 return intersect < 0 ? isl_bool_error : isl_bool_false;
3565 intersect = isl_basic_map_plain_is_universe(bmap2);
3566 if (intersect < 0 || intersect)
3567 return intersect < 0 ? isl_bool_error : isl_bool_false;
3569 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3570 isl_basic_map_copy(bmap2));
3571 disjoint = isl_basic_map_is_empty(test);
3572 isl_basic_map_free(test);
3574 return disjoint;
3577 /* Are "bset1" and "bset2" disjoint?
3579 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3580 __isl_keep isl_basic_set *bset2)
3582 return isl_basic_map_is_disjoint(bset1, bset2);
3585 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3586 __isl_keep isl_set *set2)
3588 return isl_map_plain_is_disjoint((struct isl_map *)set1,
3589 (struct isl_map *)set2);
3592 /* Are "set1" and "set2" disjoint?
3594 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3596 return isl_map_is_disjoint(set1, set2);
3599 /* Is "v" equal to 0, 1 or -1?
3601 static int is_zero_or_one(isl_int v)
3603 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3606 /* Check if we can combine a given div with lower bound l and upper
3607 * bound u with some other div and if so return that other div.
3608 * Otherwise return -1.
3610 * We first check that
3611 * - the bounds are opposites of each other (except for the constant
3612 * term)
3613 * - the bounds do not reference any other div
3614 * - no div is defined in terms of this div
3616 * Let m be the size of the range allowed on the div by the bounds.
3617 * That is, the bounds are of the form
3619 * e <= a <= e + m - 1
3621 * with e some expression in the other variables.
3622 * We look for another div b such that no third div is defined in terms
3623 * of this second div b and such that in any constraint that contains
3624 * a (except for the given lower and upper bound), also contains b
3625 * with a coefficient that is m times that of b.
3626 * That is, all constraints (execpt for the lower and upper bound)
3627 * are of the form
3629 * e + f (a + m b) >= 0
3631 * Furthermore, in the constraints that only contain b, the coefficient
3632 * of b should be equal to 1 or -1.
3633 * If so, we return b so that "a + m b" can be replaced by
3634 * a single div "c = a + m b".
3636 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
3637 unsigned div, unsigned l, unsigned u)
3639 int i, j;
3640 unsigned dim;
3641 int coalesce = -1;
3643 if (bmap->n_div <= 1)
3644 return -1;
3645 dim = isl_space_dim(bmap->dim, isl_dim_all);
3646 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3647 return -1;
3648 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3649 bmap->n_div - div - 1) != -1)
3650 return -1;
3651 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3652 dim + bmap->n_div))
3653 return -1;
3655 for (i = 0; i < bmap->n_div; ++i) {
3656 if (isl_int_is_zero(bmap->div[i][0]))
3657 continue;
3658 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3659 return -1;
3662 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3663 if (isl_int_is_neg(bmap->ineq[l][0])) {
3664 isl_int_sub(bmap->ineq[l][0],
3665 bmap->ineq[l][0], bmap->ineq[u][0]);
3666 bmap = isl_basic_map_copy(bmap);
3667 bmap = isl_basic_map_set_to_empty(bmap);
3668 isl_basic_map_free(bmap);
3669 return -1;
3671 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3672 for (i = 0; i < bmap->n_div; ++i) {
3673 if (i == div)
3674 continue;
3675 if (!pairs[i])
3676 continue;
3677 for (j = 0; j < bmap->n_div; ++j) {
3678 if (isl_int_is_zero(bmap->div[j][0]))
3679 continue;
3680 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3681 break;
3683 if (j < bmap->n_div)
3684 continue;
3685 for (j = 0; j < bmap->n_ineq; ++j) {
3686 int valid;
3687 if (j == l || j == u)
3688 continue;
3689 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
3690 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3691 continue;
3692 break;
3694 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3695 break;
3696 isl_int_mul(bmap->ineq[j][1 + dim + div],
3697 bmap->ineq[j][1 + dim + div],
3698 bmap->ineq[l][0]);
3699 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3700 bmap->ineq[j][1 + dim + i]);
3701 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3702 bmap->ineq[j][1 + dim + div],
3703 bmap->ineq[l][0]);
3704 if (!valid)
3705 break;
3707 if (j < bmap->n_ineq)
3708 continue;
3709 coalesce = i;
3710 break;
3712 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3713 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3714 return coalesce;
3717 /* Given a lower and an upper bound on div i, construct an inequality
3718 * that when nonnegative ensures that this pair of bounds always allows
3719 * for an integer value of the given div.
3720 * The lower bound is inequality l, while the upper bound is inequality u.
3721 * The constructed inequality is stored in ineq.
3722 * g, fl, fu are temporary scalars.
3724 * Let the upper bound be
3726 * -n_u a + e_u >= 0
3728 * and the lower bound
3730 * n_l a + e_l >= 0
3732 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
3733 * We have
3735 * - f_u e_l <= f_u f_l g a <= f_l e_u
3737 * Since all variables are integer valued, this is equivalent to
3739 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
3741 * If this interval is at least f_u f_l g, then it contains at least
3742 * one integer value for a.
3743 * That is, the test constraint is
3745 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
3747 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
3748 int l, int u, isl_int *ineq, isl_int *g, isl_int *fl, isl_int *fu)
3750 unsigned dim;
3751 dim = isl_space_dim(bmap->dim, isl_dim_all);
3753 isl_int_gcd(*g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
3754 isl_int_divexact(*fl, bmap->ineq[l][1 + dim + i], *g);
3755 isl_int_divexact(*fu, bmap->ineq[u][1 + dim + i], *g);
3756 isl_int_neg(*fu, *fu);
3757 isl_seq_combine(ineq, *fl, bmap->ineq[u], *fu, bmap->ineq[l],
3758 1 + dim + bmap->n_div);
3759 isl_int_add(ineq[0], ineq[0], *fl);
3760 isl_int_add(ineq[0], ineq[0], *fu);
3761 isl_int_sub_ui(ineq[0], ineq[0], 1);
3762 isl_int_mul(*g, *g, *fl);
3763 isl_int_mul(*g, *g, *fu);
3764 isl_int_sub(ineq[0], ineq[0], *g);
3767 /* Remove more kinds of divs that are not strictly needed.
3768 * In particular, if all pairs of lower and upper bounds on a div
3769 * are such that they allow at least one integer value of the div,
3770 * the we can eliminate the div using Fourier-Motzkin without
3771 * introducing any spurious solutions.
3773 static struct isl_basic_map *drop_more_redundant_divs(
3774 struct isl_basic_map *bmap, int *pairs, int n)
3776 struct isl_tab *tab = NULL;
3777 struct isl_vec *vec = NULL;
3778 unsigned dim;
3779 int remove = -1;
3780 isl_int g, fl, fu;
3782 isl_int_init(g);
3783 isl_int_init(fl);
3784 isl_int_init(fu);
3786 if (!bmap)
3787 goto error;
3789 dim = isl_space_dim(bmap->dim, isl_dim_all);
3790 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
3791 if (!vec)
3792 goto error;
3794 tab = isl_tab_from_basic_map(bmap, 0);
3796 while (n > 0) {
3797 int i, l, u;
3798 int best = -1;
3799 enum isl_lp_result res;
3801 for (i = 0; i < bmap->n_div; ++i) {
3802 if (!pairs[i])
3803 continue;
3804 if (best >= 0 && pairs[best] <= pairs[i])
3805 continue;
3806 best = i;
3809 i = best;
3810 for (l = 0; l < bmap->n_ineq; ++l) {
3811 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
3812 continue;
3813 for (u = 0; u < bmap->n_ineq; ++u) {
3814 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
3815 continue;
3816 construct_test_ineq(bmap, i, l, u,
3817 vec->el, &g, &fl, &fu);
3818 res = isl_tab_min(tab, vec->el,
3819 bmap->ctx->one, &g, NULL, 0);
3820 if (res == isl_lp_error)
3821 goto error;
3822 if (res == isl_lp_empty) {
3823 bmap = isl_basic_map_set_to_empty(bmap);
3824 break;
3826 if (res != isl_lp_ok || isl_int_is_neg(g))
3827 break;
3829 if (u < bmap->n_ineq)
3830 break;
3832 if (l == bmap->n_ineq) {
3833 remove = i;
3834 break;
3836 pairs[i] = 0;
3837 --n;
3840 isl_tab_free(tab);
3841 isl_vec_free(vec);
3843 isl_int_clear(g);
3844 isl_int_clear(fl);
3845 isl_int_clear(fu);
3847 free(pairs);
3849 if (remove < 0)
3850 return bmap;
3852 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
3853 return isl_basic_map_drop_redundant_divs(bmap);
3854 error:
3855 free(pairs);
3856 isl_basic_map_free(bmap);
3857 isl_tab_free(tab);
3858 isl_vec_free(vec);
3859 isl_int_clear(g);
3860 isl_int_clear(fl);
3861 isl_int_clear(fu);
3862 return NULL;
3865 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
3866 * and the upper bound u, div1 always occurs together with div2 in the form
3867 * (div1 + m div2), where m is the constant range on the variable div1
3868 * allowed by l and u, replace the pair div1 and div2 by a single
3869 * div that is equal to div1 + m div2.
3871 * The new div will appear in the location that contains div2.
3872 * We need to modify all constraints that contain
3873 * div2 = (div - div1) / m
3874 * The coefficient of div2 is known to be equal to 1 or -1.
3875 * (If a constraint does not contain div2, it will also not contain div1.)
3876 * If the constraint also contains div1, then we know they appear
3877 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3878 * i.e., the coefficient of div is f.
3880 * Otherwise, we first need to introduce div1 into the constraint.
3881 * Let the l be
3883 * div1 + f >=0
3885 * and u
3887 * -div1 + f' >= 0
3889 * A lower bound on div2
3891 * div2 + t >= 0
3893 * can be replaced by
3895 * m div2 + div1 + m t + f >= 0
3897 * An upper bound
3899 * -div2 + t >= 0
3901 * can be replaced by
3903 * -(m div2 + div1) + m t + f' >= 0
3905 * These constraint are those that we would obtain from eliminating
3906 * div1 using Fourier-Motzkin.
3908 * After all constraints have been modified, we drop the lower and upper
3909 * bound and then drop div1.
3911 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
3912 unsigned div1, unsigned div2, unsigned l, unsigned u)
3914 isl_ctx *ctx;
3915 isl_int m;
3916 unsigned dim, total;
3917 int i;
3919 ctx = isl_basic_map_get_ctx(bmap);
3921 dim = isl_space_dim(bmap->dim, isl_dim_all);
3922 total = 1 + dim + bmap->n_div;
3924 isl_int_init(m);
3925 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
3926 isl_int_add_ui(m, m, 1);
3928 for (i = 0; i < bmap->n_ineq; ++i) {
3929 if (i == l || i == u)
3930 continue;
3931 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
3932 continue;
3933 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
3934 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
3935 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
3936 ctx->one, bmap->ineq[l], total);
3937 else
3938 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
3939 ctx->one, bmap->ineq[u], total);
3941 isl_int_set(bmap->ineq[i][1 + dim + div2],
3942 bmap->ineq[i][1 + dim + div1]);
3943 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
3946 isl_int_clear(m);
3947 if (l > u) {
3948 isl_basic_map_drop_inequality(bmap, l);
3949 isl_basic_map_drop_inequality(bmap, u);
3950 } else {
3951 isl_basic_map_drop_inequality(bmap, u);
3952 isl_basic_map_drop_inequality(bmap, l);
3954 bmap = isl_basic_map_drop_div(bmap, div1);
3955 return bmap;
3958 /* First check if we can coalesce any pair of divs and
3959 * then continue with dropping more redundant divs.
3961 * We loop over all pairs of lower and upper bounds on a div
3962 * with coefficient 1 and -1, respectively, check if there
3963 * is any other div "c" with which we can coalesce the div
3964 * and if so, perform the coalescing.
3966 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
3967 struct isl_basic_map *bmap, int *pairs, int n)
3969 int i, l, u;
3970 unsigned dim;
3972 dim = isl_space_dim(bmap->dim, isl_dim_all);
3974 for (i = 0; i < bmap->n_div; ++i) {
3975 if (!pairs[i])
3976 continue;
3977 for (l = 0; l < bmap->n_ineq; ++l) {
3978 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
3979 continue;
3980 for (u = 0; u < bmap->n_ineq; ++u) {
3981 int c;
3983 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
3984 continue;
3985 c = div_find_coalesce(bmap, pairs, i, l, u);
3986 if (c < 0)
3987 continue;
3988 free(pairs);
3989 bmap = coalesce_divs(bmap, i, c, l, u);
3990 return isl_basic_map_drop_redundant_divs(bmap);
3995 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3996 return bmap;
3998 return drop_more_redundant_divs(bmap, pairs, n);
4001 /* Are the "n" coefficients starting at "first" of inequality constraints
4002 * "i" and "j" of "bmap" equal to each other?
4004 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4005 int first, int n)
4007 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4010 /* Are the "n" coefficients starting at "first" of inequality constraints
4011 * "i" and "j" of "bmap" opposite to each other?
4013 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4014 int first, int n)
4016 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4019 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4020 * apart from the constant term?
4022 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4024 unsigned total;
4026 total = isl_basic_map_dim(bmap, isl_dim_all);
4027 return is_opposite_part(bmap, i, j, 1, total);
4030 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4031 * apart from the constant term and the coefficient at position "pos"?
4033 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4034 int pos)
4036 unsigned total;
4038 total = isl_basic_map_dim(bmap, isl_dim_all);
4039 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4040 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4043 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4044 * apart from the constant term and the coefficient at position "pos"?
4046 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4047 int pos)
4049 unsigned total;
4051 total = isl_basic_map_dim(bmap, isl_dim_all);
4052 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4053 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4056 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4057 * been modified, simplying it if "simplify" is set.
4058 * Free the temporary data structure "pairs" that was associated
4059 * to the old version of "bmap".
4061 static __isl_give isl_basic_map *drop_redundant_divs_again(
4062 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4064 if (simplify)
4065 bmap = isl_basic_map_simplify(bmap);
4066 free(pairs);
4067 return isl_basic_map_drop_redundant_divs(bmap);
4070 /* Is "div" the single unknown existentially quantified variable
4071 * in inequality constraint "ineq" of "bmap"?
4072 * "div" is known to have a non-zero coefficient in "ineq".
4074 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4076 int i;
4077 unsigned n_div, o_div;
4079 if (isl_basic_map_div_is_known(bmap, div))
4080 return 0;
4081 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4082 if (n_div == 1)
4083 return 1;
4084 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4085 for (i = 0; i < n_div; ++i) {
4086 if (i == div)
4087 continue;
4088 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4089 continue;
4090 if (!isl_basic_map_div_is_known(bmap, i))
4091 return 0;
4094 return 1;
4097 /* Does integer division "div" have coefficient 1 in inequality constraint
4098 * "ineq" of "map"?
4100 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4102 unsigned o_div;
4104 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4105 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4106 return 1;
4108 return 0;
4111 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4112 * then try and drop redundant divs again,
4113 * freeing the temporary data structure "pairs" that was associated
4114 * to the old version of "bmap".
4116 static __isl_give isl_basic_map *set_eq_and_try_again(
4117 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4119 bmap = isl_basic_map_cow(bmap);
4120 isl_basic_map_inequality_to_equality(bmap, ineq);
4121 return drop_redundant_divs_again(bmap, pairs, 1);
4124 /* Given two inequality constraints
4126 * f(x) + n d + c >= 0, (ineq)
4128 * with d the variable at position "pos", and
4130 * f(x) + c0 >= 0, (lower)
4132 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4133 * determined by the first constraint.
4134 * That is, store
4136 * ceil((c0 - c)/n)
4138 * in *l.
4140 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4141 int ineq, int lower, int pos, isl_int *l)
4143 isl_int_neg(*l, bmap->ineq[ineq][0]);
4144 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4145 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4148 /* Given two inequality constraints
4150 * f(x) + n d + c >= 0, (ineq)
4152 * with d the variable at position "pos", and
4154 * -f(x) - c0 >= 0, (upper)
4156 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4157 * determined by the first constraint.
4158 * That is, store
4160 * ceil((-c1 - c)/n)
4162 * in *u.
4164 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4165 int ineq, int upper, int pos, isl_int *u)
4167 isl_int_neg(*u, bmap->ineq[ineq][0]);
4168 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4169 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4172 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4173 * does the corresponding lower bound have a fixed value in "bmap"?
4175 * In particular, "ineq" is of the form
4177 * f(x) + n d + c >= 0
4179 * with n > 0, c the constant term and
4180 * d the existentially quantified variable "div".
4181 * That is, the lower bound is
4183 * ceil((-f(x) - c)/n)
4185 * Look for a pair of constraints
4187 * f(x) + c0 >= 0
4188 * -f(x) + c1 >= 0
4190 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4191 * That is, check that
4193 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4195 * If so, return the index of inequality f(x) + c0 >= 0.
4196 * Otherwise, return -1.
4198 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4200 int i;
4201 int lower = -1, upper = -1;
4202 unsigned o_div, n_div;
4203 isl_int l, u;
4204 int equal;
4206 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4207 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4208 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4209 if (i == ineq)
4210 continue;
4211 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4212 continue;
4213 if (lower < 0 &&
4214 is_parallel_except(bmap, ineq, i, o_div + div)) {
4215 lower = i;
4216 continue;
4218 if (upper < 0 &&
4219 is_opposite_except(bmap, ineq, i, o_div + div)) {
4220 upper = i;
4224 if (lower < 0 || upper < 0)
4225 return -1;
4227 isl_int_init(l);
4228 isl_int_init(u);
4230 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4231 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4233 equal = isl_int_eq(l, u);
4235 isl_int_clear(l);
4236 isl_int_clear(u);
4238 return equal ? lower : -1;
4241 /* Given a lower bound constraint "ineq" on the existentially quantified
4242 * variable "div", such that the corresponding lower bound has
4243 * a fixed value in "bmap", assign this fixed value to the variable and
4244 * then try and drop redundant divs again,
4245 * freeing the temporary data structure "pairs" that was associated
4246 * to the old version of "bmap".
4247 * "lower" determines the constant value for the lower bound.
4249 * In particular, "ineq" is of the form
4251 * f(x) + n d + c >= 0,
4253 * while "lower" is of the form
4255 * f(x) + c0 >= 0
4257 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4258 * is ceil((c0 - c)/n).
4260 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4261 int div, int ineq, int lower, int *pairs)
4263 isl_int c;
4264 unsigned o_div;
4266 isl_int_init(c);
4268 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4269 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4270 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4271 free(pairs);
4273 isl_int_clear(c);
4275 return isl_basic_map_drop_redundant_divs(bmap);
4278 /* Remove divs that are not strictly needed based on the inequality
4279 * constraints.
4280 * In particular, if a div only occurs positively (or negatively)
4281 * in constraints, then it can simply be dropped.
4282 * Also, if a div occurs in only two constraints and if moreover
4283 * those two constraints are opposite to each other, except for the constant
4284 * term and if the sum of the constant terms is such that for any value
4285 * of the other values, there is always at least one integer value of the
4286 * div, i.e., if one plus this sum is greater than or equal to
4287 * the (absolute value) of the coefficient of the div in the constraints,
4288 * then we can also simply drop the div.
4290 * If an existentially quantified variable does not have an explicit
4291 * representation, appears in only a single lower bound that does not
4292 * involve any other such existentially quantified variables and appears
4293 * in this lower bound with coefficient 1,
4294 * then fix the variable to the value of the lower bound. That is,
4295 * turn the inequality into an equality.
4296 * If for any value of the other variables, there is any value
4297 * for the existentially quantified variable satisfying the constraints,
4298 * then this lower bound also satisfies the constraints.
4299 * It is therefore safe to pick this lower bound.
4301 * The same reasoning holds even if the coefficient is not one.
4302 * However, fixing the variable to the value of the lower bound may
4303 * in general introduce an extra integer division, in which case
4304 * it may be better to pick another value.
4305 * If this integer division has a known constant value, then plugging
4306 * in this constant value removes the existentially quantified variable
4307 * completely. In particular, if the lower bound is of the form
4308 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4309 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4310 * then the existentially quantified variable can be assigned this
4311 * shared value.
4313 * We skip divs that appear in equalities or in the definition of other divs.
4314 * Divs that appear in the definition of other divs usually occur in at least
4315 * 4 constraints, but the constraints may have been simplified.
4317 * If any divs are left after these simple checks then we move on
4318 * to more complicated cases in drop_more_redundant_divs.
4320 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4321 __isl_take isl_basic_map *bmap)
4323 int i, j;
4324 unsigned off;
4325 int *pairs = NULL;
4326 int n = 0;
4328 if (!bmap)
4329 goto error;
4330 if (bmap->n_div == 0)
4331 return bmap;
4333 off = isl_space_dim(bmap->dim, isl_dim_all);
4334 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4335 if (!pairs)
4336 goto error;
4338 for (i = 0; i < bmap->n_div; ++i) {
4339 int pos, neg;
4340 int last_pos, last_neg;
4341 int redundant;
4342 int defined;
4344 defined = !isl_int_is_zero(bmap->div[i][0]);
4345 for (j = i; j < bmap->n_div; ++j)
4346 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4347 break;
4348 if (j < bmap->n_div)
4349 continue;
4350 for (j = 0; j < bmap->n_eq; ++j)
4351 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4352 break;
4353 if (j < bmap->n_eq)
4354 continue;
4355 ++n;
4356 pos = neg = 0;
4357 for (j = 0; j < bmap->n_ineq; ++j) {
4358 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4359 last_pos = j;
4360 ++pos;
4362 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4363 last_neg = j;
4364 ++neg;
4367 pairs[i] = pos * neg;
4368 if (pairs[i] == 0) {
4369 for (j = bmap->n_ineq - 1; j >= 0; --j)
4370 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4371 isl_basic_map_drop_inequality(bmap, j);
4372 bmap = isl_basic_map_drop_div(bmap, i);
4373 return drop_redundant_divs_again(bmap, pairs, 0);
4375 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4376 int single, lower;
4377 if (pos != 1)
4378 continue;
4379 single = single_unknown(bmap, last_pos, i);
4380 if (!single)
4381 continue;
4382 if (has_coef_one(bmap, i, last_pos))
4383 return set_eq_and_try_again(bmap, last_pos,
4384 pairs);
4385 lower = lower_bound_is_cst(bmap, i, last_pos);
4386 if (lower >= 0)
4387 return fix_cst_lower(bmap, i, last_pos, lower,
4388 pairs);
4389 continue;
4392 isl_int_add(bmap->ineq[last_pos][0],
4393 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4394 isl_int_add_ui(bmap->ineq[last_pos][0],
4395 bmap->ineq[last_pos][0], 1);
4396 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4397 bmap->ineq[last_pos][1+off+i]);
4398 isl_int_sub_ui(bmap->ineq[last_pos][0],
4399 bmap->ineq[last_pos][0], 1);
4400 isl_int_sub(bmap->ineq[last_pos][0],
4401 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4402 if (!redundant) {
4403 if (defined ||
4404 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
4405 pairs[i] = 0;
4406 --n;
4407 continue;
4409 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4410 return drop_redundant_divs_again(bmap, pairs, 1);
4412 if (last_pos > last_neg) {
4413 isl_basic_map_drop_inequality(bmap, last_pos);
4414 isl_basic_map_drop_inequality(bmap, last_neg);
4415 } else {
4416 isl_basic_map_drop_inequality(bmap, last_neg);
4417 isl_basic_map_drop_inequality(bmap, last_pos);
4419 bmap = isl_basic_map_drop_div(bmap, i);
4420 return drop_redundant_divs_again(bmap, pairs, 0);
4423 if (n > 0)
4424 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4426 free(pairs);
4427 return bmap;
4428 error:
4429 free(pairs);
4430 isl_basic_map_free(bmap);
4431 return NULL;
4434 /* Consider the coefficients at "c" as a row vector and replace
4435 * them with their product with "T". "T" is assumed to be a square matrix.
4437 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4439 int n;
4440 isl_ctx *ctx;
4441 isl_vec *v;
4443 if (!T)
4444 return isl_stat_error;
4445 n = isl_mat_rows(T);
4446 if (isl_seq_first_non_zero(c, n) == -1)
4447 return isl_stat_ok;
4448 ctx = isl_mat_get_ctx(T);
4449 v = isl_vec_alloc(ctx, n);
4450 if (!v)
4451 return isl_stat_error;
4452 isl_seq_swp_or_cpy(v->el, c, n);
4453 v = isl_vec_mat_product(v, isl_mat_copy(T));
4454 if (!v)
4455 return isl_stat_error;
4456 isl_seq_swp_or_cpy(c, v->el, n);
4457 isl_vec_free(v);
4459 return isl_stat_ok;
4462 /* Plug in T for the variables in "bmap" starting at "pos".
4463 * T is a linear unimodular matrix, i.e., without constant term.
4465 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4466 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4468 int i;
4469 unsigned n, total;
4471 bmap = isl_basic_map_cow(bmap);
4472 if (!bmap || !T)
4473 goto error;
4475 n = isl_mat_cols(T);
4476 if (n != isl_mat_rows(T))
4477 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4478 "expecting square matrix", goto error);
4480 total = isl_basic_map_dim(bmap, isl_dim_all);
4481 if (pos + n > total || pos + n < pos)
4482 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4483 "invalid range", goto error);
4485 for (i = 0; i < bmap->n_eq; ++i)
4486 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4487 goto error;
4488 for (i = 0; i < bmap->n_ineq; ++i)
4489 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4490 goto error;
4491 for (i = 0; i < bmap->n_div; ++i) {
4492 if (!isl_basic_map_div_is_known(bmap, i))
4493 continue;
4494 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4495 goto error;
4498 isl_mat_free(T);
4499 return bmap;
4500 error:
4501 isl_basic_map_free(bmap);
4502 isl_mat_free(T);
4503 return NULL;
4506 /* Remove divs that are not strictly needed.
4508 * First look for an equality constraint involving two or more
4509 * existentially quantified variables without an explicit
4510 * representation. Replace the combination that appears
4511 * in the equality constraint by a single existentially quantified
4512 * variable such that the equality can be used to derive
4513 * an explicit representation for the variable.
4514 * If there are no more such equality constraints, then continue
4515 * with isl_basic_map_drop_redundant_divs_ineq.
4517 * In particular, if the equality constraint is of the form
4519 * f(x) + \sum_i c_i a_i = 0
4521 * with a_i existentially quantified variable without explicit
4522 * representation, then apply a transformation on the existentially
4523 * quantified variables to turn the constraint into
4525 * f(x) + g a_1' = 0
4527 * with g the gcd of the c_i.
4528 * In order to easily identify which existentially quantified variables
4529 * have a complete explicit representation, i.e., without being defined
4530 * in terms of other existentially quantified variables without
4531 * an explicit representation, the existentially quantified variables
4532 * are first sorted.
4534 * The variable transformation is computed by extending the row
4535 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4537 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4538 * [a_2'] [ a_2 ]
4539 * ... = U ....
4540 * [a_n'] [ a_n ]
4542 * with [c_1/g ... c_n/g] representing the first row of U.
4543 * The inverse of U is then plugged into the original constraints.
4544 * The call to isl_basic_map_simplify makes sure the explicit
4545 * representation for a_1' is extracted from the equality constraint.
4547 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
4548 __isl_take isl_basic_map *bmap)
4550 int first;
4551 int i;
4552 unsigned o_div, n_div;
4553 int l;
4554 isl_ctx *ctx;
4555 isl_mat *T;
4557 if (!bmap)
4558 return NULL;
4559 if (isl_basic_map_divs_known(bmap))
4560 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4561 if (bmap->n_eq == 0)
4562 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4563 bmap = isl_basic_map_sort_divs(bmap);
4564 if (!bmap)
4565 return NULL;
4567 first = isl_basic_map_first_unknown_div(bmap);
4568 if (first < 0)
4569 return isl_basic_map_free(bmap);
4571 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4572 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4574 for (i = 0; i < bmap->n_eq; ++i) {
4575 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
4576 n_div - (first));
4577 if (l < 0)
4578 continue;
4579 l += first;
4580 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
4581 n_div - (l + 1)) == -1)
4582 continue;
4583 break;
4585 if (i >= bmap->n_eq)
4586 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4588 ctx = isl_basic_map_get_ctx(bmap);
4589 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
4590 if (!T)
4591 return isl_basic_map_free(bmap);
4592 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
4593 T = isl_mat_normalize_row(T, 0);
4594 T = isl_mat_unimodular_complete(T, 1);
4595 T = isl_mat_right_inverse(T);
4597 for (i = l; i < n_div; ++i)
4598 bmap = isl_basic_map_mark_div_unknown(bmap, i);
4599 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
4600 bmap = isl_basic_map_simplify(bmap);
4602 return isl_basic_map_drop_redundant_divs(bmap);
4605 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
4606 struct isl_basic_set *bset)
4608 return (struct isl_basic_set *)
4609 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
4612 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
4614 int i;
4616 if (!map)
4617 return NULL;
4618 for (i = 0; i < map->n; ++i) {
4619 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
4620 if (!map->p[i])
4621 goto error;
4623 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4624 return map;
4625 error:
4626 isl_map_free(map);
4627 return NULL;
4630 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
4632 return (struct isl_set *)
4633 isl_map_drop_redundant_divs((struct isl_map *)set);
4636 /* Does "bmap" satisfy any equality that involves more than 2 variables
4637 * and/or has coefficients different from -1 and 1?
4639 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
4641 int i;
4642 unsigned total;
4644 total = isl_basic_map_dim(bmap, isl_dim_all);
4646 for (i = 0; i < bmap->n_eq; ++i) {
4647 int j, k;
4649 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
4650 if (j < 0)
4651 continue;
4652 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
4653 !isl_int_is_negone(bmap->eq[i][1 + j]))
4654 return 1;
4656 j += 1;
4657 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
4658 if (k < 0)
4659 continue;
4660 j += k;
4661 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
4662 !isl_int_is_negone(bmap->eq[i][1 + j]))
4663 return 1;
4665 j += 1;
4666 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
4667 if (k >= 0)
4668 return 1;
4671 return 0;
4674 /* Remove any common factor g from the constraint coefficients in "v".
4675 * The constant term is stored in the first position and is replaced
4676 * by floor(c/g). If any common factor is removed and if this results
4677 * in a tightening of the constraint, then set *tightened.
4679 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
4680 int *tightened)
4682 isl_ctx *ctx;
4684 if (!v)
4685 return NULL;
4686 ctx = isl_vec_get_ctx(v);
4687 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
4688 if (isl_int_is_zero(ctx->normalize_gcd))
4689 return v;
4690 if (isl_int_is_one(ctx->normalize_gcd))
4691 return v;
4692 v = isl_vec_cow(v);
4693 if (!v)
4694 return NULL;
4695 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
4696 *tightened = 1;
4697 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
4698 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
4699 v->size - 1);
4700 return v;
4703 /* If "bmap" is an integer set that satisfies any equality involving
4704 * more than 2 variables and/or has coefficients different from -1 and 1,
4705 * then use variable compression to reduce the coefficients by removing
4706 * any (hidden) common factor.
4707 * In particular, apply the variable compression to each constraint,
4708 * factor out any common factor in the non-constant coefficients and
4709 * then apply the inverse of the compression.
4710 * At the end, we mark the basic map as having reduced constants.
4711 * If this flag is still set on the next invocation of this function,
4712 * then we skip the computation.
4714 * Removing a common factor may result in a tightening of some of
4715 * the constraints. If this happens, then we may end up with two
4716 * opposite inequalities that can be replaced by an equality.
4717 * We therefore call isl_basic_map_detect_inequality_pairs,
4718 * which checks for such pairs of inequalities as well as eliminate_divs_eq
4719 * and isl_basic_map_gauss if such a pair was found.
4721 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
4722 __isl_take isl_basic_map *bmap)
4724 unsigned total;
4725 isl_ctx *ctx;
4726 isl_vec *v;
4727 isl_mat *eq, *T, *T2;
4728 int i;
4729 int tightened;
4731 if (!bmap)
4732 return NULL;
4733 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
4734 return bmap;
4735 if (isl_basic_map_is_rational(bmap))
4736 return bmap;
4737 if (bmap->n_eq == 0)
4738 return bmap;
4739 if (!has_multiple_var_equality(bmap))
4740 return bmap;
4742 total = isl_basic_map_dim(bmap, isl_dim_all);
4743 ctx = isl_basic_map_get_ctx(bmap);
4744 v = isl_vec_alloc(ctx, 1 + total);
4745 if (!v)
4746 return isl_basic_map_free(bmap);
4748 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
4749 T = isl_mat_variable_compression(eq, &T2);
4750 if (!T || !T2)
4751 goto error;
4752 if (T->n_col == 0) {
4753 isl_mat_free(T);
4754 isl_mat_free(T2);
4755 isl_vec_free(v);
4756 return isl_basic_map_set_to_empty(bmap);
4759 tightened = 0;
4760 for (i = 0; i < bmap->n_ineq; ++i) {
4761 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
4762 v = isl_vec_mat_product(v, isl_mat_copy(T));
4763 v = normalize_constraint(v, &tightened);
4764 v = isl_vec_mat_product(v, isl_mat_copy(T2));
4765 if (!v)
4766 goto error;
4767 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
4770 isl_mat_free(T);
4771 isl_mat_free(T2);
4772 isl_vec_free(v);
4774 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
4776 if (tightened) {
4777 int progress = 0;
4779 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
4780 if (progress) {
4781 bmap = eliminate_divs_eq(bmap, &progress);
4782 bmap = isl_basic_map_gauss(bmap, NULL);
4786 return bmap;
4787 error:
4788 isl_mat_free(T);
4789 isl_mat_free(T2);
4790 isl_vec_free(v);
4791 return isl_basic_map_free(bmap);
4794 /* Shift the integer division at position "div" of "bmap"
4795 * by "shift" times the variable at position "pos".
4796 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
4797 * corresponds to the constant term.
4799 * That is, if the integer division has the form
4801 * floor(f(x)/d)
4803 * then replace it by
4805 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
4807 __isl_give isl_basic_map *isl_basic_map_shift_div(
4808 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
4810 int i;
4811 unsigned total;
4813 if (!bmap)
4814 return NULL;
4816 total = isl_basic_map_dim(bmap, isl_dim_all);
4817 total -= isl_basic_map_dim(bmap, isl_dim_div);
4819 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
4821 for (i = 0; i < bmap->n_eq; ++i) {
4822 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
4823 continue;
4824 isl_int_submul(bmap->eq[i][pos],
4825 shift, bmap->eq[i][1 + total + div]);
4827 for (i = 0; i < bmap->n_ineq; ++i) {
4828 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
4829 continue;
4830 isl_int_submul(bmap->ineq[i][pos],
4831 shift, bmap->ineq[i][1 + total + div]);
4833 for (i = 0; i < bmap->n_div; ++i) {
4834 if (isl_int_is_zero(bmap->div[i][0]))
4835 continue;
4836 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
4837 continue;
4838 isl_int_submul(bmap->div[i][1 + pos],
4839 shift, bmap->div[i][1 + 1 + total + div]);
4842 return bmap;