isl_qpolynomial_eval: handle void input
[isl.git] / isl_map_simplify.c
blob0a9fcd79191a59e42a69ebb20e623ef96d903789
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 /* Given a lower and an upper bound on div i, construct an inequality
4113 * that when nonnegative ensures that this pair of bounds always allows
4114 * for an integer value of the given div.
4115 * The lower bound is inequality l, while the upper bound is inequality u.
4116 * The constructed inequality is stored in ineq.
4117 * g, fl, fu are temporary scalars.
4119 * Let the upper bound be
4121 * -n_u a + e_u >= 0
4123 * and the lower bound
4125 * n_l a + e_l >= 0
4127 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4128 * We have
4130 * - f_u e_l <= f_u f_l g a <= f_l e_u
4132 * Since all variables are integer valued, this is equivalent to
4134 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4136 * If this interval is at least f_u f_l g, then it contains at least
4137 * one integer value for a.
4138 * That is, the test constraint is
4140 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4142 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
4143 int l, int u, isl_int *ineq, isl_int *g, isl_int *fl, isl_int *fu)
4145 unsigned dim;
4146 dim = isl_space_dim(bmap->dim, isl_dim_all);
4148 isl_int_gcd(*g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
4149 isl_int_divexact(*fl, bmap->ineq[l][1 + dim + i], *g);
4150 isl_int_divexact(*fu, bmap->ineq[u][1 + dim + i], *g);
4151 isl_int_neg(*fu, *fu);
4152 isl_seq_combine(ineq, *fl, bmap->ineq[u], *fu, bmap->ineq[l],
4153 1 + dim + bmap->n_div);
4154 isl_int_add(ineq[0], ineq[0], *fl);
4155 isl_int_add(ineq[0], ineq[0], *fu);
4156 isl_int_sub_ui(ineq[0], ineq[0], 1);
4157 isl_int_mul(*g, *g, *fl);
4158 isl_int_mul(*g, *g, *fu);
4159 isl_int_sub(ineq[0], ineq[0], *g);
4162 /* Remove more kinds of divs that are not strictly needed.
4163 * In particular, if all pairs of lower and upper bounds on a div
4164 * are such that they allow at least one integer value of the div,
4165 * the we can eliminate the div using Fourier-Motzkin without
4166 * introducing any spurious solutions.
4168 static struct isl_basic_map *drop_more_redundant_divs(
4169 struct isl_basic_map *bmap, int *pairs, int n)
4171 struct isl_tab *tab = NULL;
4172 struct isl_vec *vec = NULL;
4173 unsigned dim;
4174 int remove = -1;
4175 isl_int g, fl, fu;
4177 isl_int_init(g);
4178 isl_int_init(fl);
4179 isl_int_init(fu);
4181 if (!bmap)
4182 goto error;
4184 dim = isl_space_dim(bmap->dim, isl_dim_all);
4185 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
4186 if (!vec)
4187 goto error;
4189 tab = isl_tab_from_basic_map(bmap, 0);
4191 while (n > 0) {
4192 int i, l, u;
4193 int best = -1;
4194 enum isl_lp_result res = isl_lp_ok;
4196 for (i = 0; i < bmap->n_div; ++i) {
4197 if (!pairs[i])
4198 continue;
4199 if (best >= 0 && pairs[best] <= pairs[i])
4200 continue;
4201 best = i;
4204 i = best;
4205 for (l = 0; l < bmap->n_ineq; ++l) {
4206 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
4207 continue;
4208 for (u = 0; u < bmap->n_ineq; ++u) {
4209 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
4210 continue;
4211 construct_test_ineq(bmap, i, l, u,
4212 vec->el, &g, &fl, &fu);
4213 res = isl_tab_min(tab, vec->el,
4214 bmap->ctx->one, &g, NULL, 0);
4215 if (res == isl_lp_error)
4216 goto error;
4217 if (res == isl_lp_empty)
4218 break;
4219 if (res != isl_lp_ok || isl_int_is_neg(g))
4220 break;
4222 if (u < bmap->n_ineq)
4223 break;
4225 if (res == isl_lp_empty) {
4226 bmap = isl_basic_map_set_to_empty(bmap);
4227 break;
4229 if (l == bmap->n_ineq) {
4230 remove = i;
4231 break;
4233 pairs[i] = 0;
4234 --n;
4237 isl_tab_free(tab);
4238 isl_vec_free(vec);
4240 isl_int_clear(g);
4241 isl_int_clear(fl);
4242 isl_int_clear(fu);
4244 free(pairs);
4246 if (remove < 0)
4247 return bmap;
4249 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4250 return isl_basic_map_drop_redundant_divs(bmap);
4251 error:
4252 free(pairs);
4253 isl_basic_map_free(bmap);
4254 isl_tab_free(tab);
4255 isl_vec_free(vec);
4256 isl_int_clear(g);
4257 isl_int_clear(fl);
4258 isl_int_clear(fu);
4259 return NULL;
4262 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4263 * and the upper bound u, div1 always occurs together with div2 in the form
4264 * (div1 + m div2), where m is the constant range on the variable div1
4265 * allowed by l and u, replace the pair div1 and div2 by a single
4266 * div that is equal to div1 + m div2.
4268 * The new div will appear in the location that contains div2.
4269 * We need to modify all constraints that contain
4270 * div2 = (div - div1) / m
4271 * The coefficient of div2 is known to be equal to 1 or -1.
4272 * (If a constraint does not contain div2, it will also not contain div1.)
4273 * If the constraint also contains div1, then we know they appear
4274 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4275 * i.e., the coefficient of div is f.
4277 * Otherwise, we first need to introduce div1 into the constraint.
4278 * Let the l be
4280 * div1 + f >=0
4282 * and u
4284 * -div1 + f' >= 0
4286 * A lower bound on div2
4288 * div2 + t >= 0
4290 * can be replaced by
4292 * m div2 + div1 + m t + f >= 0
4294 * An upper bound
4296 * -div2 + t >= 0
4298 * can be replaced by
4300 * -(m div2 + div1) + m t + f' >= 0
4302 * These constraint are those that we would obtain from eliminating
4303 * div1 using Fourier-Motzkin.
4305 * After all constraints have been modified, we drop the lower and upper
4306 * bound and then drop div1.
4308 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4309 unsigned div1, unsigned div2, unsigned l, unsigned u)
4311 isl_ctx *ctx;
4312 isl_int m;
4313 unsigned dim, total;
4314 int i;
4316 ctx = isl_basic_map_get_ctx(bmap);
4318 dim = isl_space_dim(bmap->dim, isl_dim_all);
4319 total = 1 + dim + bmap->n_div;
4321 isl_int_init(m);
4322 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4323 isl_int_add_ui(m, m, 1);
4325 for (i = 0; i < bmap->n_ineq; ++i) {
4326 if (i == l || i == u)
4327 continue;
4328 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4329 continue;
4330 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4331 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4332 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4333 ctx->one, bmap->ineq[l], total);
4334 else
4335 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4336 ctx->one, bmap->ineq[u], total);
4338 isl_int_set(bmap->ineq[i][1 + dim + div2],
4339 bmap->ineq[i][1 + dim + div1]);
4340 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4343 isl_int_clear(m);
4344 if (l > u) {
4345 isl_basic_map_drop_inequality(bmap, l);
4346 isl_basic_map_drop_inequality(bmap, u);
4347 } else {
4348 isl_basic_map_drop_inequality(bmap, u);
4349 isl_basic_map_drop_inequality(bmap, l);
4351 bmap = isl_basic_map_drop_div(bmap, div1);
4352 return bmap;
4355 /* First check if we can coalesce any pair of divs and
4356 * then continue with dropping more redundant divs.
4358 * We loop over all pairs of lower and upper bounds on a div
4359 * with coefficient 1 and -1, respectively, check if there
4360 * is any other div "c" with which we can coalesce the div
4361 * and if so, perform the coalescing.
4363 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4364 struct isl_basic_map *bmap, int *pairs, int n)
4366 int i, l, u;
4367 unsigned dim;
4369 dim = isl_space_dim(bmap->dim, isl_dim_all);
4371 for (i = 0; i < bmap->n_div; ++i) {
4372 if (!pairs[i])
4373 continue;
4374 for (l = 0; l < bmap->n_ineq; ++l) {
4375 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4376 continue;
4377 for (u = 0; u < bmap->n_ineq; ++u) {
4378 int c;
4380 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4381 continue;
4382 c = div_find_coalesce(bmap, pairs, i, l, u);
4383 if (c < 0)
4384 continue;
4385 free(pairs);
4386 bmap = coalesce_divs(bmap, i, c, l, u);
4387 return isl_basic_map_drop_redundant_divs(bmap);
4392 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4393 return bmap;
4395 return drop_more_redundant_divs(bmap, pairs, n);
4398 /* Are the "n" coefficients starting at "first" of inequality constraints
4399 * "i" and "j" of "bmap" equal to each other?
4401 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4402 int first, int n)
4404 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4407 /* Are the "n" coefficients starting at "first" of inequality constraints
4408 * "i" and "j" of "bmap" opposite to each other?
4410 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4411 int first, int n)
4413 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4416 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4417 * apart from the constant term?
4419 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4421 unsigned total;
4423 total = isl_basic_map_dim(bmap, isl_dim_all);
4424 return is_opposite_part(bmap, i, j, 1, total);
4427 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4428 * apart from the constant term and the coefficient at position "pos"?
4430 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4431 int pos)
4433 unsigned total;
4435 total = isl_basic_map_dim(bmap, isl_dim_all);
4436 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4437 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4440 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4441 * apart from the constant term and the coefficient at position "pos"?
4443 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4444 int pos)
4446 unsigned total;
4448 total = isl_basic_map_dim(bmap, isl_dim_all);
4449 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4450 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4453 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4454 * been modified, simplying it if "simplify" is set.
4455 * Free the temporary data structure "pairs" that was associated
4456 * to the old version of "bmap".
4458 static __isl_give isl_basic_map *drop_redundant_divs_again(
4459 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4461 if (simplify)
4462 bmap = isl_basic_map_simplify(bmap);
4463 free(pairs);
4464 return isl_basic_map_drop_redundant_divs(bmap);
4467 /* Is "div" the single unknown existentially quantified variable
4468 * in inequality constraint "ineq" of "bmap"?
4469 * "div" is known to have a non-zero coefficient in "ineq".
4471 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4473 int i;
4474 unsigned n_div, o_div;
4476 if (isl_basic_map_div_is_known(bmap, div))
4477 return 0;
4478 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4479 if (n_div == 1)
4480 return 1;
4481 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4482 for (i = 0; i < n_div; ++i) {
4483 if (i == div)
4484 continue;
4485 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4486 continue;
4487 if (!isl_basic_map_div_is_known(bmap, i))
4488 return 0;
4491 return 1;
4494 /* Does integer division "div" have coefficient 1 in inequality constraint
4495 * "ineq" of "map"?
4497 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4499 unsigned o_div;
4501 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4502 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4503 return 1;
4505 return 0;
4508 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4509 * then try and drop redundant divs again,
4510 * freeing the temporary data structure "pairs" that was associated
4511 * to the old version of "bmap".
4513 static __isl_give isl_basic_map *set_eq_and_try_again(
4514 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4516 bmap = isl_basic_map_cow(bmap);
4517 isl_basic_map_inequality_to_equality(bmap, ineq);
4518 return drop_redundant_divs_again(bmap, pairs, 1);
4521 /* Given two inequality constraints
4523 * f(x) + n d + c >= 0, (ineq)
4525 * with d the variable at position "pos", and
4527 * f(x) + c0 >= 0, (lower)
4529 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4530 * determined by the first constraint.
4531 * That is, store
4533 * ceil((c0 - c)/n)
4535 * in *l.
4537 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4538 int ineq, int lower, int pos, isl_int *l)
4540 isl_int_neg(*l, bmap->ineq[ineq][0]);
4541 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4542 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4545 /* Given two inequality constraints
4547 * f(x) + n d + c >= 0, (ineq)
4549 * with d the variable at position "pos", and
4551 * -f(x) - c0 >= 0, (upper)
4553 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4554 * determined by the first constraint.
4555 * That is, store
4557 * ceil((-c1 - c)/n)
4559 * in *u.
4561 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4562 int ineq, int upper, int pos, isl_int *u)
4564 isl_int_neg(*u, bmap->ineq[ineq][0]);
4565 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4566 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4569 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4570 * does the corresponding lower bound have a fixed value in "bmap"?
4572 * In particular, "ineq" is of the form
4574 * f(x) + n d + c >= 0
4576 * with n > 0, c the constant term and
4577 * d the existentially quantified variable "div".
4578 * That is, the lower bound is
4580 * ceil((-f(x) - c)/n)
4582 * Look for a pair of constraints
4584 * f(x) + c0 >= 0
4585 * -f(x) + c1 >= 0
4587 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4588 * That is, check that
4590 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4592 * If so, return the index of inequality f(x) + c0 >= 0.
4593 * Otherwise, return -1.
4595 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4597 int i;
4598 int lower = -1, upper = -1;
4599 unsigned o_div, n_div;
4600 isl_int l, u;
4601 int equal;
4603 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4604 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4605 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4606 if (i == ineq)
4607 continue;
4608 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4609 continue;
4610 if (lower < 0 &&
4611 is_parallel_except(bmap, ineq, i, o_div + div)) {
4612 lower = i;
4613 continue;
4615 if (upper < 0 &&
4616 is_opposite_except(bmap, ineq, i, o_div + div)) {
4617 upper = i;
4621 if (lower < 0 || upper < 0)
4622 return -1;
4624 isl_int_init(l);
4625 isl_int_init(u);
4627 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4628 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4630 equal = isl_int_eq(l, u);
4632 isl_int_clear(l);
4633 isl_int_clear(u);
4635 return equal ? lower : -1;
4638 /* Given a lower bound constraint "ineq" on the existentially quantified
4639 * variable "div", such that the corresponding lower bound has
4640 * a fixed value in "bmap", assign this fixed value to the variable and
4641 * then try and drop redundant divs again,
4642 * freeing the temporary data structure "pairs" that was associated
4643 * to the old version of "bmap".
4644 * "lower" determines the constant value for the lower bound.
4646 * In particular, "ineq" is of the form
4648 * f(x) + n d + c >= 0,
4650 * while "lower" is of the form
4652 * f(x) + c0 >= 0
4654 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4655 * is ceil((c0 - c)/n).
4657 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4658 int div, int ineq, int lower, int *pairs)
4660 isl_int c;
4661 unsigned o_div;
4663 isl_int_init(c);
4665 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4666 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4667 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4668 free(pairs);
4670 isl_int_clear(c);
4672 return isl_basic_map_drop_redundant_divs(bmap);
4675 /* Remove divs that are not strictly needed based on the inequality
4676 * constraints.
4677 * In particular, if a div only occurs positively (or negatively)
4678 * in constraints, then it can simply be dropped.
4679 * Also, if a div occurs in only two constraints and if moreover
4680 * those two constraints are opposite to each other, except for the constant
4681 * term and if the sum of the constant terms is such that for any value
4682 * of the other values, there is always at least one integer value of the
4683 * div, i.e., if one plus this sum is greater than or equal to
4684 * the (absolute value) of the coefficient of the div in the constraints,
4685 * then we can also simply drop the div.
4687 * If an existentially quantified variable does not have an explicit
4688 * representation, appears in only a single lower bound that does not
4689 * involve any other such existentially quantified variables and appears
4690 * in this lower bound with coefficient 1,
4691 * then fix the variable to the value of the lower bound. That is,
4692 * turn the inequality into an equality.
4693 * If for any value of the other variables, there is any value
4694 * for the existentially quantified variable satisfying the constraints,
4695 * then this lower bound also satisfies the constraints.
4696 * It is therefore safe to pick this lower bound.
4698 * The same reasoning holds even if the coefficient is not one.
4699 * However, fixing the variable to the value of the lower bound may
4700 * in general introduce an extra integer division, in which case
4701 * it may be better to pick another value.
4702 * If this integer division has a known constant value, then plugging
4703 * in this constant value removes the existentially quantified variable
4704 * completely. In particular, if the lower bound is of the form
4705 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4706 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4707 * then the existentially quantified variable can be assigned this
4708 * shared value.
4710 * We skip divs that appear in equalities or in the definition of other divs.
4711 * Divs that appear in the definition of other divs usually occur in at least
4712 * 4 constraints, but the constraints may have been simplified.
4714 * If any divs are left after these simple checks then we move on
4715 * to more complicated cases in drop_more_redundant_divs.
4717 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4718 __isl_take isl_basic_map *bmap)
4720 int i, j;
4721 unsigned off;
4722 int *pairs = NULL;
4723 int n = 0;
4725 if (!bmap)
4726 goto error;
4727 if (bmap->n_div == 0)
4728 return bmap;
4730 off = isl_space_dim(bmap->dim, isl_dim_all);
4731 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4732 if (!pairs)
4733 goto error;
4735 for (i = 0; i < bmap->n_div; ++i) {
4736 int pos, neg;
4737 int last_pos, last_neg;
4738 int redundant;
4739 int defined;
4741 defined = !isl_int_is_zero(bmap->div[i][0]);
4742 for (j = i; j < bmap->n_div; ++j)
4743 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4744 break;
4745 if (j < bmap->n_div)
4746 continue;
4747 for (j = 0; j < bmap->n_eq; ++j)
4748 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4749 break;
4750 if (j < bmap->n_eq)
4751 continue;
4752 ++n;
4753 pos = neg = 0;
4754 for (j = 0; j < bmap->n_ineq; ++j) {
4755 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4756 last_pos = j;
4757 ++pos;
4759 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4760 last_neg = j;
4761 ++neg;
4764 pairs[i] = pos * neg;
4765 if (pairs[i] == 0) {
4766 for (j = bmap->n_ineq - 1; j >= 0; --j)
4767 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4768 isl_basic_map_drop_inequality(bmap, j);
4769 bmap = isl_basic_map_drop_div(bmap, i);
4770 return drop_redundant_divs_again(bmap, pairs, 0);
4772 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4773 int single, lower;
4774 if (pos != 1)
4775 continue;
4776 single = single_unknown(bmap, last_pos, i);
4777 if (!single)
4778 continue;
4779 if (has_coef_one(bmap, i, last_pos))
4780 return set_eq_and_try_again(bmap, last_pos,
4781 pairs);
4782 lower = lower_bound_is_cst(bmap, i, last_pos);
4783 if (lower >= 0)
4784 return fix_cst_lower(bmap, i, last_pos, lower,
4785 pairs);
4786 continue;
4789 isl_int_add(bmap->ineq[last_pos][0],
4790 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4791 isl_int_add_ui(bmap->ineq[last_pos][0],
4792 bmap->ineq[last_pos][0], 1);
4793 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4794 bmap->ineq[last_pos][1+off+i]);
4795 isl_int_sub_ui(bmap->ineq[last_pos][0],
4796 bmap->ineq[last_pos][0], 1);
4797 isl_int_sub(bmap->ineq[last_pos][0],
4798 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4799 if (!redundant) {
4800 if (defined ||
4801 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
4802 pairs[i] = 0;
4803 --n;
4804 continue;
4806 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4807 return drop_redundant_divs_again(bmap, pairs, 1);
4809 if (last_pos > last_neg) {
4810 isl_basic_map_drop_inequality(bmap, last_pos);
4811 isl_basic_map_drop_inequality(bmap, last_neg);
4812 } else {
4813 isl_basic_map_drop_inequality(bmap, last_neg);
4814 isl_basic_map_drop_inequality(bmap, last_pos);
4816 bmap = isl_basic_map_drop_div(bmap, i);
4817 return drop_redundant_divs_again(bmap, pairs, 0);
4820 if (n > 0)
4821 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4823 free(pairs);
4824 return bmap;
4825 error:
4826 free(pairs);
4827 isl_basic_map_free(bmap);
4828 return NULL;
4831 /* Consider the coefficients at "c" as a row vector and replace
4832 * them with their product with "T". "T" is assumed to be a square matrix.
4834 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4836 int n;
4837 isl_ctx *ctx;
4838 isl_vec *v;
4840 if (!T)
4841 return isl_stat_error;
4842 n = isl_mat_rows(T);
4843 if (isl_seq_first_non_zero(c, n) == -1)
4844 return isl_stat_ok;
4845 ctx = isl_mat_get_ctx(T);
4846 v = isl_vec_alloc(ctx, n);
4847 if (!v)
4848 return isl_stat_error;
4849 isl_seq_swp_or_cpy(v->el, c, n);
4850 v = isl_vec_mat_product(v, isl_mat_copy(T));
4851 if (!v)
4852 return isl_stat_error;
4853 isl_seq_swp_or_cpy(c, v->el, n);
4854 isl_vec_free(v);
4856 return isl_stat_ok;
4859 /* Plug in T for the variables in "bmap" starting at "pos".
4860 * T is a linear unimodular matrix, i.e., without constant term.
4862 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4863 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4865 int i;
4866 unsigned n, total;
4868 bmap = isl_basic_map_cow(bmap);
4869 if (!bmap || !T)
4870 goto error;
4872 n = isl_mat_cols(T);
4873 if (n != isl_mat_rows(T))
4874 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4875 "expecting square matrix", goto error);
4877 total = isl_basic_map_dim(bmap, isl_dim_all);
4878 if (pos + n > total || pos + n < pos)
4879 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4880 "invalid range", goto error);
4882 for (i = 0; i < bmap->n_eq; ++i)
4883 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4884 goto error;
4885 for (i = 0; i < bmap->n_ineq; ++i)
4886 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4887 goto error;
4888 for (i = 0; i < bmap->n_div; ++i) {
4889 if (!isl_basic_map_div_is_known(bmap, i))
4890 continue;
4891 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4892 goto error;
4895 isl_mat_free(T);
4896 return bmap;
4897 error:
4898 isl_basic_map_free(bmap);
4899 isl_mat_free(T);
4900 return NULL;
4903 /* Remove divs that are not strictly needed.
4905 * First look for an equality constraint involving two or more
4906 * existentially quantified variables without an explicit
4907 * representation. Replace the combination that appears
4908 * in the equality constraint by a single existentially quantified
4909 * variable such that the equality can be used to derive
4910 * an explicit representation for the variable.
4911 * If there are no more such equality constraints, then continue
4912 * with isl_basic_map_drop_redundant_divs_ineq.
4914 * In particular, if the equality constraint is of the form
4916 * f(x) + \sum_i c_i a_i = 0
4918 * with a_i existentially quantified variable without explicit
4919 * representation, then apply a transformation on the existentially
4920 * quantified variables to turn the constraint into
4922 * f(x) + g a_1' = 0
4924 * with g the gcd of the c_i.
4925 * In order to easily identify which existentially quantified variables
4926 * have a complete explicit representation, i.e., without being defined
4927 * in terms of other existentially quantified variables without
4928 * an explicit representation, the existentially quantified variables
4929 * are first sorted.
4931 * The variable transformation is computed by extending the row
4932 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4934 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4935 * [a_2'] [ a_2 ]
4936 * ... = U ....
4937 * [a_n'] [ a_n ]
4939 * with [c_1/g ... c_n/g] representing the first row of U.
4940 * The inverse of U is then plugged into the original constraints.
4941 * The call to isl_basic_map_simplify makes sure the explicit
4942 * representation for a_1' is extracted from the equality constraint.
4944 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
4945 __isl_take isl_basic_map *bmap)
4947 int first;
4948 int i;
4949 unsigned o_div, n_div;
4950 int l;
4951 isl_ctx *ctx;
4952 isl_mat *T;
4954 if (!bmap)
4955 return NULL;
4956 if (isl_basic_map_divs_known(bmap))
4957 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4958 if (bmap->n_eq == 0)
4959 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4960 bmap = isl_basic_map_sort_divs(bmap);
4961 if (!bmap)
4962 return NULL;
4964 first = isl_basic_map_first_unknown_div(bmap);
4965 if (first < 0)
4966 return isl_basic_map_free(bmap);
4968 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4969 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4971 for (i = 0; i < bmap->n_eq; ++i) {
4972 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
4973 n_div - (first));
4974 if (l < 0)
4975 continue;
4976 l += first;
4977 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
4978 n_div - (l + 1)) == -1)
4979 continue;
4980 break;
4982 if (i >= bmap->n_eq)
4983 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4985 ctx = isl_basic_map_get_ctx(bmap);
4986 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
4987 if (!T)
4988 return isl_basic_map_free(bmap);
4989 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
4990 T = isl_mat_normalize_row(T, 0);
4991 T = isl_mat_unimodular_complete(T, 1);
4992 T = isl_mat_right_inverse(T);
4994 for (i = l; i < n_div; ++i)
4995 bmap = isl_basic_map_mark_div_unknown(bmap, i);
4996 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
4997 bmap = isl_basic_map_simplify(bmap);
4999 return isl_basic_map_drop_redundant_divs(bmap);
5002 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5003 struct isl_basic_set *bset)
5005 return (struct isl_basic_set *)
5006 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
5009 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5011 int i;
5013 if (!map)
5014 return NULL;
5015 for (i = 0; i < map->n; ++i) {
5016 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5017 if (!map->p[i])
5018 goto error;
5020 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5021 return map;
5022 error:
5023 isl_map_free(map);
5024 return NULL;
5027 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5029 return (struct isl_set *)
5030 isl_map_drop_redundant_divs((struct isl_map *)set);
5033 /* Does "bmap" satisfy any equality that involves more than 2 variables
5034 * and/or has coefficients different from -1 and 1?
5036 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5038 int i;
5039 unsigned total;
5041 total = isl_basic_map_dim(bmap, isl_dim_all);
5043 for (i = 0; i < bmap->n_eq; ++i) {
5044 int j, k;
5046 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5047 if (j < 0)
5048 continue;
5049 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5050 !isl_int_is_negone(bmap->eq[i][1 + j]))
5051 return 1;
5053 j += 1;
5054 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5055 if (k < 0)
5056 continue;
5057 j += k;
5058 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5059 !isl_int_is_negone(bmap->eq[i][1 + j]))
5060 return 1;
5062 j += 1;
5063 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5064 if (k >= 0)
5065 return 1;
5068 return 0;
5071 /* Remove any common factor g from the constraint coefficients in "v".
5072 * The constant term is stored in the first position and is replaced
5073 * by floor(c/g). If any common factor is removed and if this results
5074 * in a tightening of the constraint, then set *tightened.
5076 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5077 int *tightened)
5079 isl_ctx *ctx;
5081 if (!v)
5082 return NULL;
5083 ctx = isl_vec_get_ctx(v);
5084 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5085 if (isl_int_is_zero(ctx->normalize_gcd))
5086 return v;
5087 if (isl_int_is_one(ctx->normalize_gcd))
5088 return v;
5089 v = isl_vec_cow(v);
5090 if (!v)
5091 return NULL;
5092 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5093 *tightened = 1;
5094 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5095 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5096 v->size - 1);
5097 return v;
5100 /* If "bmap" is an integer set that satisfies any equality involving
5101 * more than 2 variables and/or has coefficients different from -1 and 1,
5102 * then use variable compression to reduce the coefficients by removing
5103 * any (hidden) common factor.
5104 * In particular, apply the variable compression to each constraint,
5105 * factor out any common factor in the non-constant coefficients and
5106 * then apply the inverse of the compression.
5107 * At the end, we mark the basic map as having reduced constants.
5108 * If this flag is still set on the next invocation of this function,
5109 * then we skip the computation.
5111 * Removing a common factor may result in a tightening of some of
5112 * the constraints. If this happens, then we may end up with two
5113 * opposite inequalities that can be replaced by an equality.
5114 * We therefore call isl_basic_map_detect_inequality_pairs,
5115 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5116 * and isl_basic_map_gauss if such a pair was found.
5118 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5119 __isl_take isl_basic_map *bmap)
5121 unsigned total;
5122 isl_ctx *ctx;
5123 isl_vec *v;
5124 isl_mat *eq, *T, *T2;
5125 int i;
5126 int tightened;
5128 if (!bmap)
5129 return NULL;
5130 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5131 return bmap;
5132 if (isl_basic_map_is_rational(bmap))
5133 return bmap;
5134 if (bmap->n_eq == 0)
5135 return bmap;
5136 if (!has_multiple_var_equality(bmap))
5137 return bmap;
5139 total = isl_basic_map_dim(bmap, isl_dim_all);
5140 ctx = isl_basic_map_get_ctx(bmap);
5141 v = isl_vec_alloc(ctx, 1 + total);
5142 if (!v)
5143 return isl_basic_map_free(bmap);
5145 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5146 T = isl_mat_variable_compression(eq, &T2);
5147 if (!T || !T2)
5148 goto error;
5149 if (T->n_col == 0) {
5150 isl_mat_free(T);
5151 isl_mat_free(T2);
5152 isl_vec_free(v);
5153 return isl_basic_map_set_to_empty(bmap);
5156 tightened = 0;
5157 for (i = 0; i < bmap->n_ineq; ++i) {
5158 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5159 v = isl_vec_mat_product(v, isl_mat_copy(T));
5160 v = normalize_constraint(v, &tightened);
5161 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5162 if (!v)
5163 goto error;
5164 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5167 isl_mat_free(T);
5168 isl_mat_free(T2);
5169 isl_vec_free(v);
5171 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5173 if (tightened) {
5174 int progress = 0;
5176 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5177 if (progress) {
5178 bmap = eliminate_divs_eq(bmap, &progress);
5179 bmap = isl_basic_map_gauss(bmap, NULL);
5183 return bmap;
5184 error:
5185 isl_mat_free(T);
5186 isl_mat_free(T2);
5187 isl_vec_free(v);
5188 return isl_basic_map_free(bmap);
5191 /* Shift the integer division at position "div" of "bmap"
5192 * by "shift" times the variable at position "pos".
5193 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5194 * corresponds to the constant term.
5196 * That is, if the integer division has the form
5198 * floor(f(x)/d)
5200 * then replace it by
5202 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5204 __isl_give isl_basic_map *isl_basic_map_shift_div(
5205 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5207 int i;
5208 unsigned total;
5210 if (!bmap)
5211 return NULL;
5213 total = isl_basic_map_dim(bmap, isl_dim_all);
5214 total -= isl_basic_map_dim(bmap, isl_dim_div);
5216 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5218 for (i = 0; i < bmap->n_eq; ++i) {
5219 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5220 continue;
5221 isl_int_submul(bmap->eq[i][pos],
5222 shift, bmap->eq[i][1 + total + div]);
5224 for (i = 0; i < bmap->n_ineq; ++i) {
5225 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5226 continue;
5227 isl_int_submul(bmap->ineq[i][pos],
5228 shift, bmap->ineq[i][1 + total + div]);
5230 for (i = 0; i < bmap->n_div; ++i) {
5231 if (isl_int_is_zero(bmap->div[i][0]))
5232 continue;
5233 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5234 continue;
5235 isl_int_submul(bmap->div[i][1 + pos],
5236 shift, bmap->div[i][1 + 1 + total + div]);
5239 return bmap;