isl_basic_map_remove_redundancies: sort constraints
[isl.git] / isl_map_simplify.c
blobe4102ddf92b5aa311d2eedf40ba6a753e5a5fc1a
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 the number of equality constraints in "bmap" that involve
2819 * local variables. This function assumes that Gaussian elimination
2820 * has been applied to the equality constraints.
2822 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2824 int i;
2825 int total, n_div;
2827 if (!bmap)
2828 return -1;
2830 if (bmap->n_eq == 0)
2831 return 0;
2833 total = isl_basic_map_dim(bmap, isl_dim_all);
2834 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2835 total -= n_div;
2837 for (i = 0; i < bmap->n_eq; ++i)
2838 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2839 n_div) == -1)
2840 return i;
2842 return bmap->n_eq;
2845 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2846 * The constraints are assumed not to involve any local variables.
2848 static __isl_give isl_basic_map *basic_map_from_equalities(
2849 __isl_take isl_space *space, __isl_take isl_mat *eq)
2851 int i, k;
2852 isl_basic_map *bmap = NULL;
2854 if (!space || !eq)
2855 goto error;
2857 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2858 isl_die(isl_space_get_ctx(space), isl_error_internal,
2859 "unexpected number of columns", goto error);
2861 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2862 0, eq->n_row, 0);
2863 for (i = 0; i < eq->n_row; ++i) {
2864 k = isl_basic_map_alloc_equality(bmap);
2865 if (k < 0)
2866 goto error;
2867 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2870 isl_space_free(space);
2871 isl_mat_free(eq);
2872 return bmap;
2873 error:
2874 isl_space_free(space);
2875 isl_mat_free(eq);
2876 isl_basic_map_free(bmap);
2877 return NULL;
2880 /* Construct and return a variable compression based on the equality
2881 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2882 * "n1" is the number of (initial) equality constraints in "bmap1"
2883 * that do involve local variables.
2884 * "n2" is the number of (initial) equality constraints in "bmap2"
2885 * that do involve local variables.
2886 * "total" is the total number of other variables.
2887 * This function assumes that Gaussian elimination
2888 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2889 * such that the equality constraints not involving local variables
2890 * are those that start at "n1" or "n2".
2892 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2893 * then simply compute the compression based on the equality constraints
2894 * in the other basic map.
2895 * Otherwise, combine the equality constraints from both into a new
2896 * basic map such that Gaussian elimination can be applied to this combination
2897 * and then construct a variable compression from the resulting
2898 * equality constraints.
2900 static __isl_give isl_mat *combined_variable_compression(
2901 __isl_keep isl_basic_map *bmap1, int n1,
2902 __isl_keep isl_basic_map *bmap2, int n2, int total)
2904 isl_ctx *ctx;
2905 isl_mat *E1, *E2, *V;
2906 isl_basic_map *bmap;
2908 ctx = isl_basic_map_get_ctx(bmap1);
2909 if (bmap1->n_eq == n1) {
2910 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2911 n2, bmap2->n_eq - n2, 0, 1 + total);
2912 return isl_mat_variable_compression(E2, NULL);
2914 if (bmap2->n_eq == n2) {
2915 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2916 n1, bmap1->n_eq - n1, 0, 1 + total);
2917 return isl_mat_variable_compression(E1, NULL);
2919 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2920 n1, bmap1->n_eq - n1, 0, 1 + total);
2921 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2922 n2, bmap2->n_eq - n2, 0, 1 + total);
2923 E1 = isl_mat_concat(E1, E2);
2924 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2925 bmap = isl_basic_map_gauss(bmap, NULL);
2926 if (!bmap)
2927 return NULL;
2928 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2929 V = isl_mat_variable_compression(E1, NULL);
2930 isl_basic_map_free(bmap);
2932 return V;
2935 /* Extract the stride constraints from "bmap", compressed
2936 * with respect to both the stride constraints in "context" and
2937 * the remaining equality constraints in both "bmap" and "context".
2938 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2939 * "context_n_eq" is the number of (initial) stride constraints in "context".
2941 * Let x be all variables in "bmap" (and "context") other than the local
2942 * variables. First compute a variable compression
2944 * x = V x'
2946 * based on the non-stride equality constraints in "bmap" and "context".
2947 * Consider the stride constraints of "context",
2949 * A(x) + B(y) = 0
2951 * with y the local variables and plug in the variable compression,
2952 * resulting in
2954 * A(V x') + B(y) = 0
2956 * Use these constraints to compute a parameter compression on x'
2958 * x' = T x''
2960 * Now consider the stride constraints of "bmap"
2962 * C(x) + D(y) = 0
2964 * and plug in x = V*T x''.
2965 * That is, return A = [C*V*T D].
2967 static __isl_give isl_mat *extract_compressed_stride_constraints(
2968 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2969 __isl_keep isl_basic_map *context, int context_n_eq)
2971 int total, n_div;
2972 isl_ctx *ctx;
2973 isl_mat *A, *B, *T, *V;
2975 total = isl_basic_map_dim(context, isl_dim_all);
2976 n_div = isl_basic_map_dim(context, isl_dim_div);
2977 total -= n_div;
2979 ctx = isl_basic_map_get_ctx(bmap);
2981 V = combined_variable_compression(bmap, bmap_n_eq,
2982 context, context_n_eq, total);
2984 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2985 B = isl_mat_sub_alloc6(ctx, context->eq,
2986 0, context_n_eq, 1 + total, n_div);
2987 A = isl_mat_product(A, isl_mat_copy(V));
2988 T = isl_mat_parameter_compression_ext(A, B);
2989 T = isl_mat_product(V, T);
2991 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2992 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2994 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2995 0, bmap_n_eq, 0, 1 + total + n_div);
2996 A = isl_mat_product(A, T);
2998 return A;
3001 /* Remove the prime factors from *g that have an exponent that
3002 * is strictly smaller than the exponent in "c".
3003 * All exponents in *g are known to be smaller than or equal
3004 * to those in "c".
3006 * That is, if *g is equal to
3008 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3010 * and "c" is equal to
3012 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3014 * then update *g to
3016 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3017 * p_n^{e_n * (e_n = f_n)}
3019 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3020 * neither does the gcd of *g and c / *g.
3021 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3022 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3023 * Dividing *g by this gcd therefore strictly reduces the exponent
3024 * of the prime factors that need to be removed, while leaving the
3025 * other prime factors untouched.
3026 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3027 * removes all undesired factors, without removing any others.
3029 static void remove_incomplete_powers(isl_int *g, isl_int c)
3031 isl_int t;
3033 isl_int_init(t);
3034 for (;;) {
3035 isl_int_divexact(t, c, *g);
3036 isl_int_gcd(t, t, *g);
3037 if (isl_int_is_one(t))
3038 break;
3039 isl_int_divexact(*g, *g, t);
3041 isl_int_clear(t);
3044 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3045 * of the same stride constraints in a compressed space that exploits
3046 * all equalities in the context and the other equalities in "bmap".
3048 * If the stride constraints of "bmap" are of the form
3050 * C(x) + D(y) = 0
3052 * then A is of the form
3054 * B(x') + D(y) = 0
3056 * If any of these constraints involves only a single local variable y,
3057 * then the constraint appears as
3059 * f(x) + m y_i = 0
3061 * in "bmap" and as
3063 * h(x') + m y_i = 0
3065 * in "A".
3067 * Let g be the gcd of m and the coefficients of h.
3068 * Then, in particular, g is a divisor of the coefficients of h and
3070 * f(x) = h(x')
3072 * is known to be a multiple of g.
3073 * If some prime factor in m appears with the same exponent in g,
3074 * then it can be removed from m because f(x) is already known
3075 * to be a multiple of g and therefore in particular of this power
3076 * of the prime factors.
3077 * Prime factors that appear with a smaller exponent in g cannot
3078 * be removed from m.
3079 * Let g' be the divisor of g containing all prime factors that
3080 * appear with the same exponent in m and g, then
3082 * f(x) + m y_i = 0
3084 * can be replaced by
3086 * f(x) + m/g' y_i' = 0
3088 * Note that (if g' != 1) this changes the explicit representation
3089 * of y_i to that of y_i', so the integer division at position i
3090 * is marked unknown and later recomputed by a call to
3091 * isl_basic_map_gauss.
3093 static __isl_give isl_basic_map *reduce_stride_constraints(
3094 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3096 int i;
3097 int total, n_div;
3098 int any = 0;
3099 isl_int gcd;
3101 if (!bmap || !A)
3102 return isl_basic_map_free(bmap);
3104 total = isl_basic_map_dim(bmap, isl_dim_all);
3105 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3106 total -= n_div;
3108 isl_int_init(gcd);
3109 for (i = 0; i < n; ++i) {
3110 int div;
3112 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3113 if (div < 0)
3114 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3115 "equality constraints modified unexpectedly",
3116 goto error);
3117 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3118 n_div - div - 1) != -1)
3119 continue;
3120 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3121 goto error;
3122 if (isl_int_is_one(gcd))
3123 continue;
3124 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3125 if (isl_int_is_one(gcd))
3126 continue;
3127 isl_int_divexact(bmap->eq[i][1 + total + div],
3128 bmap->eq[i][1 + total + div], gcd);
3129 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3130 if (!bmap)
3131 goto error;
3132 any = 1;
3134 isl_int_clear(gcd);
3136 if (any)
3137 bmap = isl_basic_map_gauss(bmap, NULL);
3139 return bmap;
3140 error:
3141 isl_int_clear(gcd);
3142 isl_basic_map_free(bmap);
3143 return NULL;
3146 /* Simplify the stride constraints in "bmap" based on
3147 * the remaining equality constraints in "bmap" and all equality
3148 * constraints in "context".
3149 * Only do this if both "bmap" and "context" have stride constraints.
3151 * First extract a copy of the stride constraints in "bmap" in a compressed
3152 * space exploiting all the other equality constraints and then
3153 * use this compressed copy to simplify the original stride constraints.
3155 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3156 __isl_keep isl_basic_map *context)
3158 int bmap_n_eq, context_n_eq;
3159 isl_mat *A;
3161 if (!bmap || !context)
3162 return isl_basic_map_free(bmap);
3164 bmap_n_eq = n_div_eq(bmap);
3165 context_n_eq = n_div_eq(context);
3167 if (bmap_n_eq < 0 || context_n_eq < 0)
3168 return isl_basic_map_free(bmap);
3169 if (bmap_n_eq == 0 || context_n_eq == 0)
3170 return bmap;
3172 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3173 context, context_n_eq);
3174 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3176 isl_mat_free(A);
3178 return bmap;
3181 /* Return a basic map that has the same intersection with "context" as "bmap"
3182 * and that is as "simple" as possible.
3184 * The core computation is performed on the pure constraints.
3185 * When we add back the meaning of the integer divisions, we need
3186 * to (re)introduce the div constraints. If we happen to have
3187 * discovered that some of these integer divisions are equal to
3188 * some affine combination of other variables, then these div
3189 * constraints may end up getting simplified in terms of the equalities,
3190 * resulting in extra inequalities on the other variables that
3191 * may have been removed already or that may not even have been
3192 * part of the input. We try and remove those constraints of
3193 * this form that are most obviously redundant with respect to
3194 * the context. We also remove those div constraints that are
3195 * redundant with respect to the other constraints in the result.
3197 * The stride constraints among the equality constraints in "bmap" are
3198 * also simplified with respecting to the other equality constraints
3199 * in "bmap" and with respect to all equality constraints in "context".
3201 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3202 struct isl_basic_map *context)
3204 isl_basic_set *bset, *eq;
3205 isl_basic_map *eq_bmap;
3206 unsigned total, n_div, extra, n_eq, n_ineq;
3208 if (!bmap || !context)
3209 goto error;
3211 if (isl_basic_map_plain_is_universe(bmap)) {
3212 isl_basic_map_free(context);
3213 return bmap;
3215 if (isl_basic_map_plain_is_empty(context)) {
3216 isl_space *space = isl_basic_map_get_space(bmap);
3217 isl_basic_map_free(bmap);
3218 isl_basic_map_free(context);
3219 return isl_basic_map_universe(space);
3221 if (isl_basic_map_plain_is_empty(bmap)) {
3222 isl_basic_map_free(context);
3223 return bmap;
3226 bmap = isl_basic_map_remove_redundancies(bmap);
3227 context = isl_basic_map_remove_redundancies(context);
3228 if (!context)
3229 goto error;
3231 context = isl_basic_map_align_divs(context, bmap);
3232 n_div = isl_basic_map_dim(context, isl_dim_div);
3233 total = isl_basic_map_dim(bmap, isl_dim_all);
3234 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3236 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3237 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3238 bset = uset_gist(bset,
3239 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3240 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3242 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3243 isl_basic_set_plain_is_empty(bset)) {
3244 isl_basic_map_free(context);
3245 return isl_basic_map_overlying_set(bset, bmap);
3248 n_eq = bset->n_eq;
3249 n_ineq = bset->n_ineq;
3250 eq = isl_basic_set_copy(bset);
3251 eq = isl_basic_set_cow(eq);
3252 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3253 eq = isl_basic_set_free(eq);
3254 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3255 bset = isl_basic_set_free(bset);
3257 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3258 eq_bmap = gist_strides(eq_bmap, context);
3259 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3260 bmap = isl_basic_map_overlying_set(bset, bmap);
3261 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3262 bmap = isl_basic_map_remove_redundancies(bmap);
3264 return bmap;
3265 error:
3266 isl_basic_map_free(bmap);
3267 isl_basic_map_free(context);
3268 return NULL;
3272 * Assumes context has no implicit divs.
3274 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3275 __isl_take isl_basic_map *context)
3277 int i;
3279 if (!map || !context)
3280 goto error;
3282 if (isl_basic_map_plain_is_empty(context)) {
3283 isl_space *space = isl_map_get_space(map);
3284 isl_map_free(map);
3285 isl_basic_map_free(context);
3286 return isl_map_universe(space);
3289 context = isl_basic_map_remove_redundancies(context);
3290 map = isl_map_cow(map);
3291 if (!map || !context)
3292 goto error;
3293 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3294 map = isl_map_compute_divs(map);
3295 if (!map)
3296 goto error;
3297 for (i = map->n - 1; i >= 0; --i) {
3298 map->p[i] = isl_basic_map_gist(map->p[i],
3299 isl_basic_map_copy(context));
3300 if (!map->p[i])
3301 goto error;
3302 if (isl_basic_map_plain_is_empty(map->p[i])) {
3303 isl_basic_map_free(map->p[i]);
3304 if (i != map->n - 1)
3305 map->p[i] = map->p[map->n - 1];
3306 map->n--;
3309 isl_basic_map_free(context);
3310 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3311 return map;
3312 error:
3313 isl_map_free(map);
3314 isl_basic_map_free(context);
3315 return NULL;
3318 /* Drop all inequalities from "bmap" that also appear in "context".
3319 * "context" is assumed to have only known local variables and
3320 * the initial local variables of "bmap" are assumed to be the same
3321 * as those of "context".
3322 * The constraints of both "bmap" and "context" are assumed
3323 * to have been sorted using isl_basic_map_sort_constraints.
3325 * Run through the inequality constraints of "bmap" and "context"
3326 * in sorted order.
3327 * If a constraint of "bmap" involves variables not in "context",
3328 * then it cannot appear in "context".
3329 * If a matching constraint is found, it is removed from "bmap".
3331 static __isl_give isl_basic_map *drop_inequalities(
3332 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3334 int i1, i2;
3335 unsigned total, extra;
3337 if (!bmap || !context)
3338 return isl_basic_map_free(bmap);
3340 total = isl_basic_map_total_dim(context);
3341 extra = isl_basic_map_total_dim(bmap) - total;
3343 i1 = bmap->n_ineq - 1;
3344 i2 = context->n_ineq - 1;
3345 while (bmap && i1 >= 0 && i2 >= 0) {
3346 int cmp;
3348 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3349 extra) != -1) {
3350 --i1;
3351 continue;
3353 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3354 context->ineq[i2]);
3355 if (cmp < 0) {
3356 --i2;
3357 continue;
3359 if (cmp > 0) {
3360 --i1;
3361 continue;
3363 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3364 bmap = isl_basic_map_cow(bmap);
3365 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3366 bmap = isl_basic_map_free(bmap);
3368 --i1;
3369 --i2;
3372 return bmap;
3375 /* Drop all equalities from "bmap" that also appear in "context".
3376 * "context" is assumed to have only known local variables and
3377 * the initial local variables of "bmap" are assumed to be the same
3378 * as those of "context".
3380 * Run through the equality constraints of "bmap" and "context"
3381 * in sorted order.
3382 * If a constraint of "bmap" involves variables not in "context",
3383 * then it cannot appear in "context".
3384 * If a matching constraint is found, it is removed from "bmap".
3386 static __isl_give isl_basic_map *drop_equalities(
3387 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3389 int i1, i2;
3390 unsigned total, extra;
3392 if (!bmap || !context)
3393 return isl_basic_map_free(bmap);
3395 total = isl_basic_map_total_dim(context);
3396 extra = isl_basic_map_total_dim(bmap) - total;
3398 i1 = bmap->n_eq - 1;
3399 i2 = context->n_eq - 1;
3401 while (bmap && i1 >= 0 && i2 >= 0) {
3402 int last1, last2;
3404 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3405 extra) != -1)
3406 break;
3407 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3408 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3409 if (last1 > last2) {
3410 --i2;
3411 continue;
3413 if (last1 < last2) {
3414 --i1;
3415 continue;
3417 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3418 bmap = isl_basic_map_cow(bmap);
3419 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3420 bmap = isl_basic_map_free(bmap);
3422 --i1;
3423 --i2;
3426 return bmap;
3429 /* Remove the constraints in "context" from "bmap".
3430 * "context" is assumed to have explicit representations
3431 * for all local variables.
3433 * First align the divs of "bmap" to those of "context" and
3434 * sort the constraints. Then drop all constraints from "bmap"
3435 * that appear in "context".
3437 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3438 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3440 isl_bool done, known;
3442 done = isl_basic_map_plain_is_universe(context);
3443 if (done == isl_bool_false)
3444 done = isl_basic_map_plain_is_universe(bmap);
3445 if (done == isl_bool_false)
3446 done = isl_basic_map_plain_is_empty(context);
3447 if (done == isl_bool_false)
3448 done = isl_basic_map_plain_is_empty(bmap);
3449 if (done < 0)
3450 goto error;
3451 if (done) {
3452 isl_basic_map_free(context);
3453 return bmap;
3455 known = isl_basic_map_divs_known(context);
3456 if (known < 0)
3457 goto error;
3458 if (!known)
3459 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3460 "context has unknown divs", goto error);
3462 bmap = isl_basic_map_align_divs(bmap, context);
3463 bmap = isl_basic_map_gauss(bmap, NULL);
3464 bmap = isl_basic_map_sort_constraints(bmap);
3465 context = isl_basic_map_sort_constraints(context);
3467 bmap = drop_inequalities(bmap, context);
3468 bmap = drop_equalities(bmap, context);
3470 isl_basic_map_free(context);
3471 bmap = isl_basic_map_finalize(bmap);
3472 return bmap;
3473 error:
3474 isl_basic_map_free(bmap);
3475 isl_basic_map_free(context);
3476 return NULL;
3479 /* Replace "map" by the disjunct at position "pos" and free "context".
3481 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3482 int pos, __isl_take isl_basic_map *context)
3484 isl_basic_map *bmap;
3486 bmap = isl_basic_map_copy(map->p[pos]);
3487 isl_map_free(map);
3488 isl_basic_map_free(context);
3489 return isl_map_from_basic_map(bmap);
3492 /* Remove the constraints in "context" from "map".
3493 * If any of the disjuncts in the result turns out to be the universe,
3494 * then return this universe.
3495 * "context" is assumed to have explicit representations
3496 * for all local variables.
3498 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3499 __isl_take isl_basic_map *context)
3501 int i;
3502 isl_bool univ, known;
3504 univ = isl_basic_map_plain_is_universe(context);
3505 if (univ < 0)
3506 goto error;
3507 if (univ) {
3508 isl_basic_map_free(context);
3509 return map;
3511 known = isl_basic_map_divs_known(context);
3512 if (known < 0)
3513 goto error;
3514 if (!known)
3515 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3516 "context has unknown divs", goto error);
3518 map = isl_map_cow(map);
3519 if (!map)
3520 goto error;
3521 for (i = 0; i < map->n; ++i) {
3522 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3523 isl_basic_map_copy(context));
3524 univ = isl_basic_map_plain_is_universe(map->p[i]);
3525 if (univ < 0)
3526 goto error;
3527 if (univ && map->n > 1)
3528 return replace_by_disjunct(map, i, context);
3531 isl_basic_map_free(context);
3532 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3533 if (map->n > 1)
3534 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3535 return map;
3536 error:
3537 isl_map_free(map);
3538 isl_basic_map_free(context);
3539 return NULL;
3542 /* Replace "map" by a universe map in the same space and free "drop".
3544 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3545 __isl_take isl_map *drop)
3547 isl_map *res;
3549 res = isl_map_universe(isl_map_get_space(map));
3550 isl_map_free(map);
3551 isl_map_free(drop);
3552 return res;
3555 /* Return a map that has the same intersection with "context" as "map"
3556 * and that is as "simple" as possible.
3558 * If "map" is already the universe, then we cannot make it any simpler.
3559 * Similarly, if "context" is the universe, then we cannot exploit it
3560 * to simplify "map"
3561 * If "map" and "context" are identical to each other, then we can
3562 * return the corresponding universe.
3564 * If either "map" or "context" consists of multiple disjuncts,
3565 * then check if "context" happens to be a subset of "map",
3566 * in which case all constraints can be removed.
3567 * In case of multiple disjuncts, the standard procedure
3568 * may not be able to detect that all constraints can be removed.
3570 * If none of these cases apply, we have to work a bit harder.
3571 * During this computation, we make use of a single disjunct context,
3572 * so if the original context consists of more than one disjunct
3573 * then we need to approximate the context by a single disjunct set.
3574 * Simply taking the simple hull may drop constraints that are
3575 * only implicitly available in each disjunct. We therefore also
3576 * look for constraints among those defining "map" that are valid
3577 * for the context. These can then be used to simplify away
3578 * the corresponding constraints in "map".
3580 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3581 __isl_take isl_map *context)
3583 int equal;
3584 int is_universe;
3585 int single_disjunct_map, single_disjunct_context;
3586 isl_bool subset;
3587 isl_basic_map *hull;
3589 is_universe = isl_map_plain_is_universe(map);
3590 if (is_universe >= 0 && !is_universe)
3591 is_universe = isl_map_plain_is_universe(context);
3592 if (is_universe < 0)
3593 goto error;
3594 if (is_universe) {
3595 isl_map_free(context);
3596 return map;
3599 equal = isl_map_plain_is_equal(map, context);
3600 if (equal < 0)
3601 goto error;
3602 if (equal)
3603 return replace_by_universe(map, context);
3605 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3606 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3607 if (!single_disjunct_map || !single_disjunct_context) {
3608 subset = isl_map_is_subset(context, map);
3609 if (subset < 0)
3610 goto error;
3611 if (subset)
3612 return replace_by_universe(map, context);
3615 context = isl_map_compute_divs(context);
3616 if (!context)
3617 goto error;
3618 if (single_disjunct_context) {
3619 hull = isl_map_simple_hull(context);
3620 } else {
3621 isl_ctx *ctx;
3622 isl_map_list *list;
3624 ctx = isl_map_get_ctx(map);
3625 list = isl_map_list_alloc(ctx, 2);
3626 list = isl_map_list_add(list, isl_map_copy(context));
3627 list = isl_map_list_add(list, isl_map_copy(map));
3628 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3629 list);
3631 return isl_map_gist_basic_map(map, hull);
3632 error:
3633 isl_map_free(map);
3634 isl_map_free(context);
3635 return NULL;
3638 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3639 __isl_take isl_map *context)
3641 return isl_map_align_params_map_map_and(map, context, &map_gist);
3644 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3645 struct isl_basic_set *context)
3647 return (struct isl_basic_set *)isl_basic_map_gist(
3648 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
3651 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3652 __isl_take isl_basic_set *context)
3654 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
3655 (struct isl_basic_map *)context);
3658 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3659 __isl_take isl_basic_set *context)
3661 isl_space *space = isl_set_get_space(set);
3662 isl_basic_set *dom_context = isl_basic_set_universe(space);
3663 dom_context = isl_basic_set_intersect_params(dom_context, context);
3664 return isl_set_gist_basic_set(set, dom_context);
3667 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3668 __isl_take isl_set *context)
3670 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
3671 (struct isl_map *)context);
3674 /* Compute the gist of "bmap" with respect to the constraints "context"
3675 * on the domain.
3677 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3678 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3680 isl_space *space = isl_basic_map_get_space(bmap);
3681 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3683 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3684 return isl_basic_map_gist(bmap, bmap_context);
3687 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3688 __isl_take isl_set *context)
3690 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3691 map_context = isl_map_intersect_domain(map_context, context);
3692 return isl_map_gist(map, map_context);
3695 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3696 __isl_take isl_set *context)
3698 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3699 map_context = isl_map_intersect_range(map_context, context);
3700 return isl_map_gist(map, map_context);
3703 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3704 __isl_take isl_set *context)
3706 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3707 map_context = isl_map_intersect_params(map_context, context);
3708 return isl_map_gist(map, map_context);
3711 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3712 __isl_take isl_set *context)
3714 return isl_map_gist_params(set, context);
3717 /* Quick check to see if two basic maps are disjoint.
3718 * In particular, we reduce the equalities and inequalities of
3719 * one basic map in the context of the equalities of the other
3720 * basic map and check if we get a contradiction.
3722 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3723 __isl_keep isl_basic_map *bmap2)
3725 struct isl_vec *v = NULL;
3726 int *elim = NULL;
3727 unsigned total;
3728 int i;
3730 if (!bmap1 || !bmap2)
3731 return isl_bool_error;
3732 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3733 return isl_bool_error);
3734 if (bmap1->n_div || bmap2->n_div)
3735 return isl_bool_false;
3736 if (!bmap1->n_eq && !bmap2->n_eq)
3737 return isl_bool_false;
3739 total = isl_space_dim(bmap1->dim, isl_dim_all);
3740 if (total == 0)
3741 return isl_bool_false;
3742 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3743 if (!v)
3744 goto error;
3745 elim = isl_alloc_array(bmap1->ctx, int, total);
3746 if (!elim)
3747 goto error;
3748 compute_elimination_index(bmap1, elim);
3749 for (i = 0; i < bmap2->n_eq; ++i) {
3750 int reduced;
3751 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3752 bmap1, elim);
3753 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3754 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3755 goto disjoint;
3757 for (i = 0; i < bmap2->n_ineq; ++i) {
3758 int reduced;
3759 reduced = reduced_using_equalities(v->block.data,
3760 bmap2->ineq[i], bmap1, elim);
3761 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3762 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3763 goto disjoint;
3765 compute_elimination_index(bmap2, elim);
3766 for (i = 0; i < bmap1->n_ineq; ++i) {
3767 int reduced;
3768 reduced = reduced_using_equalities(v->block.data,
3769 bmap1->ineq[i], bmap2, elim);
3770 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3771 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3772 goto disjoint;
3774 isl_vec_free(v);
3775 free(elim);
3776 return isl_bool_false;
3777 disjoint:
3778 isl_vec_free(v);
3779 free(elim);
3780 return isl_bool_true;
3781 error:
3782 isl_vec_free(v);
3783 free(elim);
3784 return isl_bool_error;
3787 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3788 __isl_keep isl_basic_set *bset2)
3790 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
3791 (struct isl_basic_map *)bset2);
3794 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3796 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3797 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3798 __isl_keep isl_basic_map *bmap2))
3800 int i, j;
3802 if (!map1 || !map2)
3803 return isl_bool_error;
3805 for (i = 0; i < map1->n; ++i) {
3806 for (j = 0; j < map2->n; ++j) {
3807 isl_bool d = test(map1->p[i], map2->p[j]);
3808 if (d != isl_bool_true)
3809 return d;
3813 return isl_bool_true;
3816 /* Are "map1" and "map2" obviously disjoint, based on information
3817 * that can be derived without looking at the individual basic maps?
3819 * In particular, if one of them is empty or if they live in different spaces
3820 * (ignoring parameters), then they are clearly disjoint.
3822 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3823 __isl_keep isl_map *map2)
3825 isl_bool disjoint;
3826 isl_bool match;
3828 if (!map1 || !map2)
3829 return isl_bool_error;
3831 disjoint = isl_map_plain_is_empty(map1);
3832 if (disjoint < 0 || disjoint)
3833 return disjoint;
3835 disjoint = isl_map_plain_is_empty(map2);
3836 if (disjoint < 0 || disjoint)
3837 return disjoint;
3839 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3840 map2->dim, isl_dim_in);
3841 if (match < 0 || !match)
3842 return match < 0 ? isl_bool_error : isl_bool_true;
3844 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3845 map2->dim, isl_dim_out);
3846 if (match < 0 || !match)
3847 return match < 0 ? isl_bool_error : isl_bool_true;
3849 return isl_bool_false;
3852 /* Are "map1" and "map2" obviously disjoint?
3854 * If one of them is empty or if they live in different spaces (ignoring
3855 * parameters), then they are clearly disjoint.
3856 * This is checked by isl_map_plain_is_disjoint_global.
3858 * If they have different parameters, then we skip any further tests.
3860 * If they are obviously equal, but not obviously empty, then we will
3861 * not be able to detect if they are disjoint.
3863 * Otherwise we check if each basic map in "map1" is obviously disjoint
3864 * from each basic map in "map2".
3866 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3867 __isl_keep isl_map *map2)
3869 isl_bool disjoint;
3870 isl_bool intersect;
3871 isl_bool match;
3873 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3874 if (disjoint < 0 || disjoint)
3875 return disjoint;
3877 match = isl_space_match(map1->dim, isl_dim_param,
3878 map2->dim, isl_dim_param);
3879 if (match < 0 || !match)
3880 return match < 0 ? isl_bool_error : isl_bool_false;
3882 intersect = isl_map_plain_is_equal(map1, map2);
3883 if (intersect < 0 || intersect)
3884 return intersect < 0 ? isl_bool_error : isl_bool_false;
3886 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3889 /* Are "map1" and "map2" disjoint?
3891 * They are disjoint if they are "obviously disjoint" or if one of them
3892 * is empty. Otherwise, they are not disjoint if one of them is universal.
3893 * If the two inputs are (obviously) equal and not empty, then they are
3894 * not disjoint.
3895 * If none of these cases apply, then check if all pairs of basic maps
3896 * are disjoint.
3898 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3900 isl_bool disjoint;
3901 isl_bool intersect;
3903 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3904 if (disjoint < 0 || disjoint)
3905 return disjoint;
3907 disjoint = isl_map_is_empty(map1);
3908 if (disjoint < 0 || disjoint)
3909 return disjoint;
3911 disjoint = isl_map_is_empty(map2);
3912 if (disjoint < 0 || disjoint)
3913 return disjoint;
3915 intersect = isl_map_plain_is_universe(map1);
3916 if (intersect < 0 || intersect)
3917 return intersect < 0 ? isl_bool_error : isl_bool_false;
3919 intersect = isl_map_plain_is_universe(map2);
3920 if (intersect < 0 || intersect)
3921 return intersect < 0 ? isl_bool_error : isl_bool_false;
3923 intersect = isl_map_plain_is_equal(map1, map2);
3924 if (intersect < 0 || intersect)
3925 return isl_bool_not(intersect);
3927 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3930 /* Are "bmap1" and "bmap2" disjoint?
3932 * They are disjoint if they are "obviously disjoint" or if one of them
3933 * is empty. Otherwise, they are not disjoint if one of them is universal.
3934 * If none of these cases apply, we compute the intersection and see if
3935 * the result is empty.
3937 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3938 __isl_keep isl_basic_map *bmap2)
3940 isl_bool disjoint;
3941 isl_bool intersect;
3942 isl_basic_map *test;
3944 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3945 if (disjoint < 0 || disjoint)
3946 return disjoint;
3948 disjoint = isl_basic_map_is_empty(bmap1);
3949 if (disjoint < 0 || disjoint)
3950 return disjoint;
3952 disjoint = isl_basic_map_is_empty(bmap2);
3953 if (disjoint < 0 || disjoint)
3954 return disjoint;
3956 intersect = isl_basic_map_plain_is_universe(bmap1);
3957 if (intersect < 0 || intersect)
3958 return intersect < 0 ? isl_bool_error : isl_bool_false;
3960 intersect = isl_basic_map_plain_is_universe(bmap2);
3961 if (intersect < 0 || intersect)
3962 return intersect < 0 ? isl_bool_error : isl_bool_false;
3964 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3965 isl_basic_map_copy(bmap2));
3966 disjoint = isl_basic_map_is_empty(test);
3967 isl_basic_map_free(test);
3969 return disjoint;
3972 /* Are "bset1" and "bset2" disjoint?
3974 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3975 __isl_keep isl_basic_set *bset2)
3977 return isl_basic_map_is_disjoint(bset1, bset2);
3980 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3981 __isl_keep isl_set *set2)
3983 return isl_map_plain_is_disjoint((struct isl_map *)set1,
3984 (struct isl_map *)set2);
3987 /* Are "set1" and "set2" disjoint?
3989 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3991 return isl_map_is_disjoint(set1, set2);
3994 /* Is "v" equal to 0, 1 or -1?
3996 static int is_zero_or_one(isl_int v)
3998 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4001 /* Check if we can combine a given div with lower bound l and upper
4002 * bound u with some other div and if so return that other div.
4003 * Otherwise return -1.
4005 * We first check that
4006 * - the bounds are opposites of each other (except for the constant
4007 * term)
4008 * - the bounds do not reference any other div
4009 * - no div is defined in terms of this div
4011 * Let m be the size of the range allowed on the div by the bounds.
4012 * That is, the bounds are of the form
4014 * e <= a <= e + m - 1
4016 * with e some expression in the other variables.
4017 * We look for another div b such that no third div is defined in terms
4018 * of this second div b and such that in any constraint that contains
4019 * a (except for the given lower and upper bound), also contains b
4020 * with a coefficient that is m times that of b.
4021 * That is, all constraints (execpt for the lower and upper bound)
4022 * are of the form
4024 * e + f (a + m b) >= 0
4026 * Furthermore, in the constraints that only contain b, the coefficient
4027 * of b should be equal to 1 or -1.
4028 * If so, we return b so that "a + m b" can be replaced by
4029 * a single div "c = a + m b".
4031 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4032 unsigned div, unsigned l, unsigned u)
4034 int i, j;
4035 unsigned dim;
4036 int coalesce = -1;
4038 if (bmap->n_div <= 1)
4039 return -1;
4040 dim = isl_space_dim(bmap->dim, isl_dim_all);
4041 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4042 return -1;
4043 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4044 bmap->n_div - div - 1) != -1)
4045 return -1;
4046 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4047 dim + bmap->n_div))
4048 return -1;
4050 for (i = 0; i < bmap->n_div; ++i) {
4051 if (isl_int_is_zero(bmap->div[i][0]))
4052 continue;
4053 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4054 return -1;
4057 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4058 if (isl_int_is_neg(bmap->ineq[l][0])) {
4059 isl_int_sub(bmap->ineq[l][0],
4060 bmap->ineq[l][0], bmap->ineq[u][0]);
4061 bmap = isl_basic_map_copy(bmap);
4062 bmap = isl_basic_map_set_to_empty(bmap);
4063 isl_basic_map_free(bmap);
4064 return -1;
4066 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4067 for (i = 0; i < bmap->n_div; ++i) {
4068 if (i == div)
4069 continue;
4070 if (!pairs[i])
4071 continue;
4072 for (j = 0; j < bmap->n_div; ++j) {
4073 if (isl_int_is_zero(bmap->div[j][0]))
4074 continue;
4075 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4076 break;
4078 if (j < bmap->n_div)
4079 continue;
4080 for (j = 0; j < bmap->n_ineq; ++j) {
4081 int valid;
4082 if (j == l || j == u)
4083 continue;
4084 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4085 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4086 continue;
4087 break;
4089 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4090 break;
4091 isl_int_mul(bmap->ineq[j][1 + dim + div],
4092 bmap->ineq[j][1 + dim + div],
4093 bmap->ineq[l][0]);
4094 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4095 bmap->ineq[j][1 + dim + i]);
4096 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4097 bmap->ineq[j][1 + dim + div],
4098 bmap->ineq[l][0]);
4099 if (!valid)
4100 break;
4102 if (j < bmap->n_ineq)
4103 continue;
4104 coalesce = i;
4105 break;
4107 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4108 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4109 return coalesce;
4112 /* Internal data structure used during the construction and/or evaluation of
4113 * an inequality that ensures that a pair of bounds always allows
4114 * for an integer value.
4116 * "tab" is the tableau in which the inequality is evaluated. It may
4117 * be NULL until it is actually needed.
4118 * "v" contains the inequality coefficients.
4119 * "g", "fl" and "fu" are temporary scalars used during the construction and
4120 * evaluation.
4122 struct test_ineq_data {
4123 struct isl_tab *tab;
4124 isl_vec *v;
4125 isl_int g;
4126 isl_int fl;
4127 isl_int fu;
4130 /* Free all the memory allocated by the fields of "data".
4132 static void test_ineq_data_clear(struct test_ineq_data *data)
4134 isl_tab_free(data->tab);
4135 isl_vec_free(data->v);
4136 isl_int_clear(data->g);
4137 isl_int_clear(data->fl);
4138 isl_int_clear(data->fu);
4141 /* Is the inequality stored in data->v satisfied by "bmap"?
4142 * That is, does it only attain non-negative values?
4143 * data->tab is a tableau corresponding to "bmap".
4145 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4146 struct test_ineq_data *data)
4148 isl_ctx *ctx;
4149 enum isl_lp_result res;
4151 ctx = isl_basic_map_get_ctx(bmap);
4152 if (!data->tab)
4153 data->tab = isl_tab_from_basic_map(bmap, 0);
4154 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4155 if (res == isl_lp_error)
4156 return isl_bool_error;
4157 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4160 /* Given a lower and an upper bound on div i, do they always allow
4161 * for an integer value of the given div?
4162 * Determine this property by constructing an inequality
4163 * such that the property is guaranteed when the inequality is nonnegative.
4164 * The lower bound is inequality l, while the upper bound is inequality u.
4165 * The constructed inequality is stored in data->v.
4167 * Let the upper bound be
4169 * -n_u a + e_u >= 0
4171 * and the lower bound
4173 * n_l a + e_l >= 0
4175 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4176 * We have
4178 * - f_u e_l <= f_u f_l g a <= f_l e_u
4180 * Since all variables are integer valued, this is equivalent to
4182 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4184 * If this interval is at least f_u f_l g, then it contains at least
4185 * one integer value for a.
4186 * That is, the test constraint is
4188 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4190 * or
4192 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4194 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4195 * then the constraint can be scaled down by a factor g',
4196 * with the constant term replaced by
4197 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4198 * Note that the result of applying Fourier-Motzkin to this pair
4199 * of constraints is
4201 * f_l e_u + f_u e_l >= 0
4203 * If the constant term of the scaled down version of this constraint,
4204 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4205 * term of the scaled down test constraint, then the test constraint
4206 * is known to hold and no explicit evaluation is required.
4207 * This is essentially the Omega test.
4209 * If the test constraint consists of only a constant term, then
4210 * it is sufficient to look at the sign of this constant term.
4212 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4213 int l, int u, struct test_ineq_data *data)
4215 unsigned offset, n_div;
4216 offset = isl_basic_map_offset(bmap, isl_dim_div);
4217 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4219 isl_int_gcd(data->g,
4220 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4221 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4222 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4223 isl_int_neg(data->fu, data->fu);
4224 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4225 data->fu, bmap->ineq[l], offset + n_div);
4226 isl_int_mul(data->g, data->g, data->fl);
4227 isl_int_mul(data->g, data->g, data->fu);
4228 isl_int_sub(data->g, data->g, data->fl);
4229 isl_int_sub(data->g, data->g, data->fu);
4230 isl_int_add_ui(data->g, data->g, 1);
4231 isl_int_sub(data->fl, data->v->el[0], data->g);
4233 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4234 if (isl_int_is_zero(data->g))
4235 return isl_int_is_nonneg(data->fl);
4236 if (isl_int_is_one(data->g)) {
4237 isl_int_set(data->v->el[0], data->fl);
4238 return test_ineq_is_satisfied(bmap, data);
4240 isl_int_fdiv_q(data->fl, data->fl, data->g);
4241 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4242 if (isl_int_eq(data->fl, data->v->el[0]))
4243 return isl_bool_true;
4244 isl_int_set(data->v->el[0], data->fl);
4245 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4246 offset - 1 + n_div);
4248 return test_ineq_is_satisfied(bmap, data);
4251 /* Remove more kinds of divs that are not strictly needed.
4252 * In particular, if all pairs of lower and upper bounds on a div
4253 * are such that they allow at least one integer value of the div,
4254 * then we can eliminate the div using Fourier-Motzkin without
4255 * introducing any spurious solutions.
4257 * If at least one of the two constraints has a unit coefficient for the div,
4258 * then the presence of such a value is guaranteed so there is no need to check.
4259 * In particular, the value attained by the bound with unit coefficient
4260 * can serve as this intermediate value.
4262 static struct isl_basic_map *drop_more_redundant_divs(
4263 struct isl_basic_map *bmap, int *pairs, int n)
4265 isl_ctx *ctx;
4266 struct test_ineq_data data = { NULL, NULL };
4267 unsigned off, n_div;
4268 int remove = -1;
4270 isl_int_init(data.g);
4271 isl_int_init(data.fl);
4272 isl_int_init(data.fu);
4274 if (!bmap)
4275 goto error;
4277 ctx = isl_basic_map_get_ctx(bmap);
4278 off = isl_basic_map_offset(bmap, isl_dim_div);
4279 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4280 data.v = isl_vec_alloc(ctx, off + n_div);
4281 if (!data.v)
4282 goto error;
4284 while (n > 0) {
4285 int i, l, u;
4286 int best = -1;
4287 isl_bool has_int;
4289 for (i = 0; i < n_div; ++i) {
4290 if (!pairs[i])
4291 continue;
4292 if (best >= 0 && pairs[best] <= pairs[i])
4293 continue;
4294 best = i;
4297 i = best;
4298 for (l = 0; l < bmap->n_ineq; ++l) {
4299 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4300 continue;
4301 if (isl_int_is_one(bmap->ineq[l][off + i]))
4302 continue;
4303 for (u = 0; u < bmap->n_ineq; ++u) {
4304 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4305 continue;
4306 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4307 continue;
4308 has_int = int_between_bounds(bmap, i, l, u,
4309 &data);
4310 if (has_int < 0)
4311 goto error;
4312 if (data.tab && data.tab->empty)
4313 break;
4314 if (!has_int)
4315 break;
4317 if (u < bmap->n_ineq)
4318 break;
4320 if (data.tab && data.tab->empty) {
4321 bmap = isl_basic_map_set_to_empty(bmap);
4322 break;
4324 if (l == bmap->n_ineq) {
4325 remove = i;
4326 break;
4328 pairs[i] = 0;
4329 --n;
4332 test_ineq_data_clear(&data);
4334 free(pairs);
4336 if (remove < 0)
4337 return bmap;
4339 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4340 return isl_basic_map_drop_redundant_divs(bmap);
4341 error:
4342 free(pairs);
4343 isl_basic_map_free(bmap);
4344 test_ineq_data_clear(&data);
4345 return NULL;
4348 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4349 * and the upper bound u, div1 always occurs together with div2 in the form
4350 * (div1 + m div2), where m is the constant range on the variable div1
4351 * allowed by l and u, replace the pair div1 and div2 by a single
4352 * div that is equal to div1 + m div2.
4354 * The new div will appear in the location that contains div2.
4355 * We need to modify all constraints that contain
4356 * div2 = (div - div1) / m
4357 * The coefficient of div2 is known to be equal to 1 or -1.
4358 * (If a constraint does not contain div2, it will also not contain div1.)
4359 * If the constraint also contains div1, then we know they appear
4360 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4361 * i.e., the coefficient of div is f.
4363 * Otherwise, we first need to introduce div1 into the constraint.
4364 * Let the l be
4366 * div1 + f >=0
4368 * and u
4370 * -div1 + f' >= 0
4372 * A lower bound on div2
4374 * div2 + t >= 0
4376 * can be replaced by
4378 * m div2 + div1 + m t + f >= 0
4380 * An upper bound
4382 * -div2 + t >= 0
4384 * can be replaced by
4386 * -(m div2 + div1) + m t + f' >= 0
4388 * These constraint are those that we would obtain from eliminating
4389 * div1 using Fourier-Motzkin.
4391 * After all constraints have been modified, we drop the lower and upper
4392 * bound and then drop div1.
4394 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4395 unsigned div1, unsigned div2, unsigned l, unsigned u)
4397 isl_ctx *ctx;
4398 isl_int m;
4399 unsigned dim, total;
4400 int i;
4402 ctx = isl_basic_map_get_ctx(bmap);
4404 dim = isl_space_dim(bmap->dim, isl_dim_all);
4405 total = 1 + dim + bmap->n_div;
4407 isl_int_init(m);
4408 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4409 isl_int_add_ui(m, m, 1);
4411 for (i = 0; i < bmap->n_ineq; ++i) {
4412 if (i == l || i == u)
4413 continue;
4414 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4415 continue;
4416 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4417 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4418 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4419 ctx->one, bmap->ineq[l], total);
4420 else
4421 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4422 ctx->one, bmap->ineq[u], total);
4424 isl_int_set(bmap->ineq[i][1 + dim + div2],
4425 bmap->ineq[i][1 + dim + div1]);
4426 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4429 isl_int_clear(m);
4430 if (l > u) {
4431 isl_basic_map_drop_inequality(bmap, l);
4432 isl_basic_map_drop_inequality(bmap, u);
4433 } else {
4434 isl_basic_map_drop_inequality(bmap, u);
4435 isl_basic_map_drop_inequality(bmap, l);
4437 bmap = isl_basic_map_drop_div(bmap, div1);
4438 return bmap;
4441 /* First check if we can coalesce any pair of divs and
4442 * then continue with dropping more redundant divs.
4444 * We loop over all pairs of lower and upper bounds on a div
4445 * with coefficient 1 and -1, respectively, check if there
4446 * is any other div "c" with which we can coalesce the div
4447 * and if so, perform the coalescing.
4449 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4450 struct isl_basic_map *bmap, int *pairs, int n)
4452 int i, l, u;
4453 unsigned dim;
4455 dim = isl_space_dim(bmap->dim, isl_dim_all);
4457 for (i = 0; i < bmap->n_div; ++i) {
4458 if (!pairs[i])
4459 continue;
4460 for (l = 0; l < bmap->n_ineq; ++l) {
4461 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4462 continue;
4463 for (u = 0; u < bmap->n_ineq; ++u) {
4464 int c;
4466 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4467 continue;
4468 c = div_find_coalesce(bmap, pairs, i, l, u);
4469 if (c < 0)
4470 continue;
4471 free(pairs);
4472 bmap = coalesce_divs(bmap, i, c, l, u);
4473 return isl_basic_map_drop_redundant_divs(bmap);
4478 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4479 return bmap;
4481 return drop_more_redundant_divs(bmap, pairs, n);
4484 /* Are the "n" coefficients starting at "first" of inequality constraints
4485 * "i" and "j" of "bmap" equal to each other?
4487 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4488 int first, int n)
4490 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4493 /* Are the "n" coefficients starting at "first" of inequality constraints
4494 * "i" and "j" of "bmap" opposite to each other?
4496 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4497 int first, int n)
4499 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4502 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4503 * apart from the constant term?
4505 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4507 unsigned total;
4509 total = isl_basic_map_dim(bmap, isl_dim_all);
4510 return is_opposite_part(bmap, i, j, 1, total);
4513 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4514 * apart from the constant term and the coefficient at position "pos"?
4516 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4517 int pos)
4519 unsigned total;
4521 total = isl_basic_map_dim(bmap, isl_dim_all);
4522 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4523 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4526 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4527 * apart from the constant term and the coefficient at position "pos"?
4529 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4530 int pos)
4532 unsigned total;
4534 total = isl_basic_map_dim(bmap, isl_dim_all);
4535 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4536 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4539 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4540 * been modified, simplying it if "simplify" is set.
4541 * Free the temporary data structure "pairs" that was associated
4542 * to the old version of "bmap".
4544 static __isl_give isl_basic_map *drop_redundant_divs_again(
4545 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4547 if (simplify)
4548 bmap = isl_basic_map_simplify(bmap);
4549 free(pairs);
4550 return isl_basic_map_drop_redundant_divs(bmap);
4553 /* Is "div" the single unknown existentially quantified variable
4554 * in inequality constraint "ineq" of "bmap"?
4555 * "div" is known to have a non-zero coefficient in "ineq".
4557 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4559 int i;
4560 unsigned n_div, o_div;
4562 if (isl_basic_map_div_is_known(bmap, div))
4563 return 0;
4564 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4565 if (n_div == 1)
4566 return 1;
4567 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4568 for (i = 0; i < n_div; ++i) {
4569 if (i == div)
4570 continue;
4571 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4572 continue;
4573 if (!isl_basic_map_div_is_known(bmap, i))
4574 return 0;
4577 return 1;
4580 /* Does integer division "div" have coefficient 1 in inequality constraint
4581 * "ineq" of "map"?
4583 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4585 unsigned o_div;
4587 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4588 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4589 return 1;
4591 return 0;
4594 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4595 * then try and drop redundant divs again,
4596 * freeing the temporary data structure "pairs" that was associated
4597 * to the old version of "bmap".
4599 static __isl_give isl_basic_map *set_eq_and_try_again(
4600 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4602 bmap = isl_basic_map_cow(bmap);
4603 isl_basic_map_inequality_to_equality(bmap, ineq);
4604 return drop_redundant_divs_again(bmap, pairs, 1);
4607 /* Drop the integer division at position "div", along with the two
4608 * inequality constraints "ineq1" and "ineq2" in which it appears
4609 * from "bmap" and then try and drop redundant divs again,
4610 * freeing the temporary data structure "pairs" that was associated
4611 * to the old version of "bmap".
4613 static __isl_give isl_basic_map *drop_div_and_try_again(
4614 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4615 __isl_take int *pairs)
4617 if (ineq1 > ineq2) {
4618 isl_basic_map_drop_inequality(bmap, ineq1);
4619 isl_basic_map_drop_inequality(bmap, ineq2);
4620 } else {
4621 isl_basic_map_drop_inequality(bmap, ineq2);
4622 isl_basic_map_drop_inequality(bmap, ineq1);
4624 bmap = isl_basic_map_drop_div(bmap, div);
4625 return drop_redundant_divs_again(bmap, pairs, 0);
4628 /* Given two inequality constraints
4630 * f(x) + n d + c >= 0, (ineq)
4632 * with d the variable at position "pos", and
4634 * f(x) + c0 >= 0, (lower)
4636 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4637 * determined by the first constraint.
4638 * That is, store
4640 * ceil((c0 - c)/n)
4642 * in *l.
4644 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4645 int ineq, int lower, int pos, isl_int *l)
4647 isl_int_neg(*l, bmap->ineq[ineq][0]);
4648 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4649 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4652 /* Given two inequality constraints
4654 * f(x) + n d + c >= 0, (ineq)
4656 * with d the variable at position "pos", and
4658 * -f(x) - c0 >= 0, (upper)
4660 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4661 * determined by the first constraint.
4662 * That is, store
4664 * ceil((-c1 - c)/n)
4666 * in *u.
4668 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4669 int ineq, int upper, int pos, isl_int *u)
4671 isl_int_neg(*u, bmap->ineq[ineq][0]);
4672 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4673 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4676 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4677 * does the corresponding lower bound have a fixed value in "bmap"?
4679 * In particular, "ineq" is of the form
4681 * f(x) + n d + c >= 0
4683 * with n > 0, c the constant term and
4684 * d the existentially quantified variable "div".
4685 * That is, the lower bound is
4687 * ceil((-f(x) - c)/n)
4689 * Look for a pair of constraints
4691 * f(x) + c0 >= 0
4692 * -f(x) + c1 >= 0
4694 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4695 * That is, check that
4697 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4699 * If so, return the index of inequality f(x) + c0 >= 0.
4700 * Otherwise, return -1.
4702 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4704 int i;
4705 int lower = -1, upper = -1;
4706 unsigned o_div, n_div;
4707 isl_int l, u;
4708 int equal;
4710 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4711 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4712 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4713 if (i == ineq)
4714 continue;
4715 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4716 continue;
4717 if (lower < 0 &&
4718 is_parallel_except(bmap, ineq, i, o_div + div)) {
4719 lower = i;
4720 continue;
4722 if (upper < 0 &&
4723 is_opposite_except(bmap, ineq, i, o_div + div)) {
4724 upper = i;
4728 if (lower < 0 || upper < 0)
4729 return -1;
4731 isl_int_init(l);
4732 isl_int_init(u);
4734 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4735 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4737 equal = isl_int_eq(l, u);
4739 isl_int_clear(l);
4740 isl_int_clear(u);
4742 return equal ? lower : -1;
4745 /* Given a lower bound constraint "ineq" on the existentially quantified
4746 * variable "div", such that the corresponding lower bound has
4747 * a fixed value in "bmap", assign this fixed value to the variable and
4748 * then try and drop redundant divs again,
4749 * freeing the temporary data structure "pairs" that was associated
4750 * to the old version of "bmap".
4751 * "lower" determines the constant value for the lower bound.
4753 * In particular, "ineq" is of the form
4755 * f(x) + n d + c >= 0,
4757 * while "lower" is of the form
4759 * f(x) + c0 >= 0
4761 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4762 * is ceil((c0 - c)/n).
4764 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4765 int div, int ineq, int lower, int *pairs)
4767 isl_int c;
4768 unsigned o_div;
4770 isl_int_init(c);
4772 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4773 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4774 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4775 free(pairs);
4777 isl_int_clear(c);
4779 return isl_basic_map_drop_redundant_divs(bmap);
4782 /* Remove divs that are not strictly needed based on the inequality
4783 * constraints.
4784 * In particular, if a div only occurs positively (or negatively)
4785 * in constraints, then it can simply be dropped.
4786 * Also, if a div occurs in only two constraints and if moreover
4787 * those two constraints are opposite to each other, except for the constant
4788 * term and if the sum of the constant terms is such that for any value
4789 * of the other values, there is always at least one integer value of the
4790 * div, i.e., if one plus this sum is greater than or equal to
4791 * the (absolute value) of the coefficient of the div in the constraints,
4792 * then we can also simply drop the div.
4794 * If an existentially quantified variable does not have an explicit
4795 * representation, appears in only a single lower bound that does not
4796 * involve any other such existentially quantified variables and appears
4797 * in this lower bound with coefficient 1,
4798 * then fix the variable to the value of the lower bound. That is,
4799 * turn the inequality into an equality.
4800 * If for any value of the other variables, there is any value
4801 * for the existentially quantified variable satisfying the constraints,
4802 * then this lower bound also satisfies the constraints.
4803 * It is therefore safe to pick this lower bound.
4805 * The same reasoning holds even if the coefficient is not one.
4806 * However, fixing the variable to the value of the lower bound may
4807 * in general introduce an extra integer division, in which case
4808 * it may be better to pick another value.
4809 * If this integer division has a known constant value, then plugging
4810 * in this constant value removes the existentially quantified variable
4811 * completely. In particular, if the lower bound is of the form
4812 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4813 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4814 * then the existentially quantified variable can be assigned this
4815 * shared value.
4817 * We skip divs that appear in equalities or in the definition of other divs.
4818 * Divs that appear in the definition of other divs usually occur in at least
4819 * 4 constraints, but the constraints may have been simplified.
4821 * If any divs are left after these simple checks then we move on
4822 * to more complicated cases in drop_more_redundant_divs.
4824 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4825 __isl_take isl_basic_map *bmap)
4827 int i, j;
4828 unsigned off;
4829 int *pairs = NULL;
4830 int n = 0;
4832 if (!bmap)
4833 goto error;
4834 if (bmap->n_div == 0)
4835 return bmap;
4837 off = isl_space_dim(bmap->dim, isl_dim_all);
4838 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4839 if (!pairs)
4840 goto error;
4842 for (i = 0; i < bmap->n_div; ++i) {
4843 int pos, neg;
4844 int last_pos, last_neg;
4845 int redundant;
4846 int defined;
4848 defined = !isl_int_is_zero(bmap->div[i][0]);
4849 for (j = i; j < bmap->n_div; ++j)
4850 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4851 break;
4852 if (j < bmap->n_div)
4853 continue;
4854 for (j = 0; j < bmap->n_eq; ++j)
4855 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4856 break;
4857 if (j < bmap->n_eq)
4858 continue;
4859 ++n;
4860 pos = neg = 0;
4861 for (j = 0; j < bmap->n_ineq; ++j) {
4862 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4863 last_pos = j;
4864 ++pos;
4866 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4867 last_neg = j;
4868 ++neg;
4871 pairs[i] = pos * neg;
4872 if (pairs[i] == 0) {
4873 for (j = bmap->n_ineq - 1; j >= 0; --j)
4874 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4875 isl_basic_map_drop_inequality(bmap, j);
4876 bmap = isl_basic_map_drop_div(bmap, i);
4877 return drop_redundant_divs_again(bmap, pairs, 0);
4879 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4880 int single, lower;
4881 if (pos != 1)
4882 continue;
4883 single = single_unknown(bmap, last_pos, i);
4884 if (!single)
4885 continue;
4886 if (has_coef_one(bmap, i, last_pos))
4887 return set_eq_and_try_again(bmap, last_pos,
4888 pairs);
4889 lower = lower_bound_is_cst(bmap, i, last_pos);
4890 if (lower >= 0)
4891 return fix_cst_lower(bmap, i, last_pos, lower,
4892 pairs);
4893 continue;
4896 isl_int_add(bmap->ineq[last_pos][0],
4897 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4898 isl_int_add_ui(bmap->ineq[last_pos][0],
4899 bmap->ineq[last_pos][0], 1);
4900 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4901 bmap->ineq[last_pos][1+off+i]);
4902 isl_int_sub_ui(bmap->ineq[last_pos][0],
4903 bmap->ineq[last_pos][0], 1);
4904 isl_int_sub(bmap->ineq[last_pos][0],
4905 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4906 if (redundant)
4907 return drop_div_and_try_again(bmap, i,
4908 last_pos, last_neg, pairs);
4909 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
4910 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4911 return drop_redundant_divs_again(bmap, pairs, 1);
4913 pairs[i] = 0;
4914 --n;
4917 if (n > 0)
4918 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4920 free(pairs);
4921 return bmap;
4922 error:
4923 free(pairs);
4924 isl_basic_map_free(bmap);
4925 return NULL;
4928 /* Consider the coefficients at "c" as a row vector and replace
4929 * them with their product with "T". "T" is assumed to be a square matrix.
4931 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4933 int n;
4934 isl_ctx *ctx;
4935 isl_vec *v;
4937 if (!T)
4938 return isl_stat_error;
4939 n = isl_mat_rows(T);
4940 if (isl_seq_first_non_zero(c, n) == -1)
4941 return isl_stat_ok;
4942 ctx = isl_mat_get_ctx(T);
4943 v = isl_vec_alloc(ctx, n);
4944 if (!v)
4945 return isl_stat_error;
4946 isl_seq_swp_or_cpy(v->el, c, n);
4947 v = isl_vec_mat_product(v, isl_mat_copy(T));
4948 if (!v)
4949 return isl_stat_error;
4950 isl_seq_swp_or_cpy(c, v->el, n);
4951 isl_vec_free(v);
4953 return isl_stat_ok;
4956 /* Plug in T for the variables in "bmap" starting at "pos".
4957 * T is a linear unimodular matrix, i.e., without constant term.
4959 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4960 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4962 int i;
4963 unsigned n, total;
4965 bmap = isl_basic_map_cow(bmap);
4966 if (!bmap || !T)
4967 goto error;
4969 n = isl_mat_cols(T);
4970 if (n != isl_mat_rows(T))
4971 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4972 "expecting square matrix", goto error);
4974 total = isl_basic_map_dim(bmap, isl_dim_all);
4975 if (pos + n > total || pos + n < pos)
4976 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4977 "invalid range", goto error);
4979 for (i = 0; i < bmap->n_eq; ++i)
4980 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4981 goto error;
4982 for (i = 0; i < bmap->n_ineq; ++i)
4983 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4984 goto error;
4985 for (i = 0; i < bmap->n_div; ++i) {
4986 if (!isl_basic_map_div_is_known(bmap, i))
4987 continue;
4988 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4989 goto error;
4992 isl_mat_free(T);
4993 return bmap;
4994 error:
4995 isl_basic_map_free(bmap);
4996 isl_mat_free(T);
4997 return NULL;
5000 /* Remove divs that are not strictly needed.
5002 * First look for an equality constraint involving two or more
5003 * existentially quantified variables without an explicit
5004 * representation. Replace the combination that appears
5005 * in the equality constraint by a single existentially quantified
5006 * variable such that the equality can be used to derive
5007 * an explicit representation for the variable.
5008 * If there are no more such equality constraints, then continue
5009 * with isl_basic_map_drop_redundant_divs_ineq.
5011 * In particular, if the equality constraint is of the form
5013 * f(x) + \sum_i c_i a_i = 0
5015 * with a_i existentially quantified variable without explicit
5016 * representation, then apply a transformation on the existentially
5017 * quantified variables to turn the constraint into
5019 * f(x) + g a_1' = 0
5021 * with g the gcd of the c_i.
5022 * In order to easily identify which existentially quantified variables
5023 * have a complete explicit representation, i.e., without being defined
5024 * in terms of other existentially quantified variables without
5025 * an explicit representation, the existentially quantified variables
5026 * are first sorted.
5028 * The variable transformation is computed by extending the row
5029 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5031 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5032 * [a_2'] [ a_2 ]
5033 * ... = U ....
5034 * [a_n'] [ a_n ]
5036 * with [c_1/g ... c_n/g] representing the first row of U.
5037 * The inverse of U is then plugged into the original constraints.
5038 * The call to isl_basic_map_simplify makes sure the explicit
5039 * representation for a_1' is extracted from the equality constraint.
5041 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5042 __isl_take isl_basic_map *bmap)
5044 int first;
5045 int i;
5046 unsigned o_div, n_div;
5047 int l;
5048 isl_ctx *ctx;
5049 isl_mat *T;
5051 if (!bmap)
5052 return NULL;
5053 if (isl_basic_map_divs_known(bmap))
5054 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5055 if (bmap->n_eq == 0)
5056 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5057 bmap = isl_basic_map_sort_divs(bmap);
5058 if (!bmap)
5059 return NULL;
5061 first = isl_basic_map_first_unknown_div(bmap);
5062 if (first < 0)
5063 return isl_basic_map_free(bmap);
5065 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5066 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5068 for (i = 0; i < bmap->n_eq; ++i) {
5069 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5070 n_div - (first));
5071 if (l < 0)
5072 continue;
5073 l += first;
5074 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5075 n_div - (l + 1)) == -1)
5076 continue;
5077 break;
5079 if (i >= bmap->n_eq)
5080 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5082 ctx = isl_basic_map_get_ctx(bmap);
5083 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5084 if (!T)
5085 return isl_basic_map_free(bmap);
5086 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5087 T = isl_mat_normalize_row(T, 0);
5088 T = isl_mat_unimodular_complete(T, 1);
5089 T = isl_mat_right_inverse(T);
5091 for (i = l; i < n_div; ++i)
5092 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5093 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5094 bmap = isl_basic_map_simplify(bmap);
5096 return isl_basic_map_drop_redundant_divs(bmap);
5099 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5100 struct isl_basic_set *bset)
5102 return (struct isl_basic_set *)
5103 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
5106 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5108 int i;
5110 if (!map)
5111 return NULL;
5112 for (i = 0; i < map->n; ++i) {
5113 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5114 if (!map->p[i])
5115 goto error;
5117 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5118 return map;
5119 error:
5120 isl_map_free(map);
5121 return NULL;
5124 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5126 return (struct isl_set *)
5127 isl_map_drop_redundant_divs((struct isl_map *)set);
5130 /* Does "bmap" satisfy any equality that involves more than 2 variables
5131 * and/or has coefficients different from -1 and 1?
5133 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5135 int i;
5136 unsigned total;
5138 total = isl_basic_map_dim(bmap, isl_dim_all);
5140 for (i = 0; i < bmap->n_eq; ++i) {
5141 int j, k;
5143 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5144 if (j < 0)
5145 continue;
5146 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5147 !isl_int_is_negone(bmap->eq[i][1 + j]))
5148 return 1;
5150 j += 1;
5151 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5152 if (k < 0)
5153 continue;
5154 j += k;
5155 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5156 !isl_int_is_negone(bmap->eq[i][1 + j]))
5157 return 1;
5159 j += 1;
5160 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5161 if (k >= 0)
5162 return 1;
5165 return 0;
5168 /* Remove any common factor g from the constraint coefficients in "v".
5169 * The constant term is stored in the first position and is replaced
5170 * by floor(c/g). If any common factor is removed and if this results
5171 * in a tightening of the constraint, then set *tightened.
5173 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5174 int *tightened)
5176 isl_ctx *ctx;
5178 if (!v)
5179 return NULL;
5180 ctx = isl_vec_get_ctx(v);
5181 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5182 if (isl_int_is_zero(ctx->normalize_gcd))
5183 return v;
5184 if (isl_int_is_one(ctx->normalize_gcd))
5185 return v;
5186 v = isl_vec_cow(v);
5187 if (!v)
5188 return NULL;
5189 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5190 *tightened = 1;
5191 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5192 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5193 v->size - 1);
5194 return v;
5197 /* If "bmap" is an integer set that satisfies any equality involving
5198 * more than 2 variables and/or has coefficients different from -1 and 1,
5199 * then use variable compression to reduce the coefficients by removing
5200 * any (hidden) common factor.
5201 * In particular, apply the variable compression to each constraint,
5202 * factor out any common factor in the non-constant coefficients and
5203 * then apply the inverse of the compression.
5204 * At the end, we mark the basic map as having reduced constants.
5205 * If this flag is still set on the next invocation of this function,
5206 * then we skip the computation.
5208 * Removing a common factor may result in a tightening of some of
5209 * the constraints. If this happens, then we may end up with two
5210 * opposite inequalities that can be replaced by an equality.
5211 * We therefore call isl_basic_map_detect_inequality_pairs,
5212 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5213 * and isl_basic_map_gauss if such a pair was found.
5215 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5216 __isl_take isl_basic_map *bmap)
5218 unsigned total;
5219 isl_ctx *ctx;
5220 isl_vec *v;
5221 isl_mat *eq, *T, *T2;
5222 int i;
5223 int tightened;
5225 if (!bmap)
5226 return NULL;
5227 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5228 return bmap;
5229 if (isl_basic_map_is_rational(bmap))
5230 return bmap;
5231 if (bmap->n_eq == 0)
5232 return bmap;
5233 if (!has_multiple_var_equality(bmap))
5234 return bmap;
5236 total = isl_basic_map_dim(bmap, isl_dim_all);
5237 ctx = isl_basic_map_get_ctx(bmap);
5238 v = isl_vec_alloc(ctx, 1 + total);
5239 if (!v)
5240 return isl_basic_map_free(bmap);
5242 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5243 T = isl_mat_variable_compression(eq, &T2);
5244 if (!T || !T2)
5245 goto error;
5246 if (T->n_col == 0) {
5247 isl_mat_free(T);
5248 isl_mat_free(T2);
5249 isl_vec_free(v);
5250 return isl_basic_map_set_to_empty(bmap);
5253 tightened = 0;
5254 for (i = 0; i < bmap->n_ineq; ++i) {
5255 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5256 v = isl_vec_mat_product(v, isl_mat_copy(T));
5257 v = normalize_constraint(v, &tightened);
5258 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5259 if (!v)
5260 goto error;
5261 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5264 isl_mat_free(T);
5265 isl_mat_free(T2);
5266 isl_vec_free(v);
5268 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5270 if (tightened) {
5271 int progress = 0;
5273 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5274 if (progress) {
5275 bmap = eliminate_divs_eq(bmap, &progress);
5276 bmap = isl_basic_map_gauss(bmap, NULL);
5280 return bmap;
5281 error:
5282 isl_mat_free(T);
5283 isl_mat_free(T2);
5284 isl_vec_free(v);
5285 return isl_basic_map_free(bmap);
5288 /* Shift the integer division at position "div" of "bmap"
5289 * by "shift" times the variable at position "pos".
5290 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5291 * corresponds to the constant term.
5293 * That is, if the integer division has the form
5295 * floor(f(x)/d)
5297 * then replace it by
5299 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5301 __isl_give isl_basic_map *isl_basic_map_shift_div(
5302 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5304 int i;
5305 unsigned total;
5307 if (!bmap)
5308 return NULL;
5310 total = isl_basic_map_dim(bmap, isl_dim_all);
5311 total -= isl_basic_map_dim(bmap, isl_dim_div);
5313 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5315 for (i = 0; i < bmap->n_eq; ++i) {
5316 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5317 continue;
5318 isl_int_submul(bmap->eq[i][pos],
5319 shift, bmap->eq[i][1 + total + div]);
5321 for (i = 0; i < bmap->n_ineq; ++i) {
5322 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5323 continue;
5324 isl_int_submul(bmap->ineq[i][pos],
5325 shift, bmap->ineq[i][1 + total + div]);
5327 for (i = 0; i < bmap->n_div; ++i) {
5328 if (isl_int_is_zero(bmap->div[i][0]))
5329 continue;
5330 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5331 continue;
5332 isl_int_submul(bmap->div[i][1 + pos],
5333 shift, bmap->div[i][1 + 1 + total + div]);
5336 return bmap;