deprecate isl_map_n_*
[isl.git] / isl_map_simplify.c
blob2bb06d49ca344324b9ff56928cb48812209d2290
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 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
49 isl_seq_cpy(c, c + n, rem);
50 isl_seq_clr(c + rem, n);
53 /* Drop n dimensions starting at first.
55 * In principle, this frees up some extra variables as the number
56 * of columns remains constant, but we would have to extend
57 * the div array too as the number of rows in this array is assumed
58 * to be equal to extra.
60 struct isl_basic_set *isl_basic_set_drop_dims(
61 struct isl_basic_set *bset, unsigned first, unsigned n)
63 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
66 /* Move "n" divs starting at "first" to the end of the list of divs.
68 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
69 unsigned first, unsigned n)
71 isl_int **div;
72 int i;
74 if (first + n == bmap->n_div)
75 return bmap;
77 div = isl_alloc_array(bmap->ctx, isl_int *, n);
78 if (!div)
79 goto error;
80 for (i = 0; i < n; ++i)
81 div[i] = bmap->div[first + i];
82 for (i = 0; i < bmap->n_div - first - n; ++i)
83 bmap->div[first + i] = bmap->div[first + n + i];
84 for (i = 0; i < n; ++i)
85 bmap->div[bmap->n_div - n + i] = div[i];
86 free(div);
87 return bmap;
88 error:
89 isl_basic_map_free(bmap);
90 return NULL;
93 /* Drop "n" dimensions of type "type" starting at "first".
95 * In principle, this frees up some extra variables as the number
96 * of columns remains constant, but we would have to extend
97 * the div array too as the number of rows in this array is assumed
98 * to be equal to extra.
100 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
101 enum isl_dim_type type, unsigned first, unsigned n)
103 int i;
104 unsigned dim;
105 unsigned offset;
106 unsigned left;
108 if (!bmap)
109 goto error;
111 dim = isl_basic_map_dim(bmap, type);
112 isl_assert(bmap->ctx, first + n <= dim, goto error);
114 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
115 return bmap;
117 bmap = isl_basic_map_cow(bmap);
118 if (!bmap)
119 return NULL;
121 offset = isl_basic_map_offset(bmap, type) + first;
122 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
123 for (i = 0; i < bmap->n_eq; ++i)
124 constraint_drop_vars(bmap->eq[i]+offset, n, left);
126 for (i = 0; i < bmap->n_ineq; ++i)
127 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
129 for (i = 0; i < bmap->n_div; ++i)
130 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
132 if (type == isl_dim_div) {
133 bmap = move_divs_last(bmap, first, n);
134 if (!bmap)
135 goto error;
136 if (isl_basic_map_free_div(bmap, n) < 0)
137 return isl_basic_map_free(bmap);
138 } else
139 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
140 if (!bmap->dim)
141 goto error;
143 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
144 bmap = isl_basic_map_simplify(bmap);
145 return isl_basic_map_finalize(bmap);
146 error:
147 isl_basic_map_free(bmap);
148 return NULL;
151 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
152 enum isl_dim_type type, unsigned first, unsigned n)
154 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
155 type, first, n));
158 struct isl_map *isl_map_drop(struct isl_map *map,
159 enum isl_dim_type type, unsigned first, unsigned n)
161 int i;
163 if (!map)
164 goto error;
166 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
168 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
169 return map;
170 map = isl_map_cow(map);
171 if (!map)
172 goto error;
173 map->dim = isl_space_drop_dims(map->dim, type, first, n);
174 if (!map->dim)
175 goto error;
177 for (i = 0; i < map->n; ++i) {
178 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
179 if (!map->p[i])
180 goto error;
182 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
184 return map;
185 error:
186 isl_map_free(map);
187 return NULL;
190 struct isl_set *isl_set_drop(struct isl_set *set,
191 enum isl_dim_type type, unsigned first, unsigned n)
193 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
197 * We don't cow, as the div is assumed to be redundant.
199 __isl_give isl_basic_map *isl_basic_map_drop_div(
200 __isl_take isl_basic_map *bmap, unsigned div)
202 int i;
203 unsigned pos;
205 if (!bmap)
206 goto error;
208 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
210 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
212 for (i = 0; i < bmap->n_eq; ++i)
213 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
215 for (i = 0; i < bmap->n_ineq; ++i) {
216 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
217 isl_basic_map_drop_inequality(bmap, i);
218 --i;
219 continue;
221 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
224 for (i = 0; i < bmap->n_div; ++i)
225 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
227 if (div != bmap->n_div - 1) {
228 int j;
229 isl_int *t = bmap->div[div];
231 for (j = div; j < bmap->n_div - 1; ++j)
232 bmap->div[j] = bmap->div[j+1];
234 bmap->div[bmap->n_div - 1] = t;
236 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
237 if (isl_basic_map_free_div(bmap, 1) < 0)
238 return isl_basic_map_free(bmap);
240 return bmap;
241 error:
242 isl_basic_map_free(bmap);
243 return NULL;
246 struct isl_basic_map *isl_basic_map_normalize_constraints(
247 struct isl_basic_map *bmap)
249 int i;
250 isl_int gcd;
251 unsigned total = isl_basic_map_total_dim(bmap);
253 if (!bmap)
254 return NULL;
256 isl_int_init(gcd);
257 for (i = bmap->n_eq - 1; i >= 0; --i) {
258 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
259 if (isl_int_is_zero(gcd)) {
260 if (!isl_int_is_zero(bmap->eq[i][0])) {
261 bmap = isl_basic_map_set_to_empty(bmap);
262 break;
264 isl_basic_map_drop_equality(bmap, i);
265 continue;
267 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
268 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
269 if (isl_int_is_one(gcd))
270 continue;
271 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
272 bmap = isl_basic_map_set_to_empty(bmap);
273 break;
275 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
278 for (i = bmap->n_ineq - 1; i >= 0; --i) {
279 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
280 if (isl_int_is_zero(gcd)) {
281 if (isl_int_is_neg(bmap->ineq[i][0])) {
282 bmap = isl_basic_map_set_to_empty(bmap);
283 break;
285 isl_basic_map_drop_inequality(bmap, i);
286 continue;
288 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
289 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
290 if (isl_int_is_one(gcd))
291 continue;
292 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
293 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
295 isl_int_clear(gcd);
297 return bmap;
300 struct isl_basic_set *isl_basic_set_normalize_constraints(
301 struct isl_basic_set *bset)
303 isl_basic_map *bmap = bset_to_bmap(bset);
304 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
307 /* Reduce the coefficient of the variable at position "pos"
308 * in integer division "div", such that it lies in the half-open
309 * interval (1/2,1/2], extracting any excess value from this integer division.
310 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
311 * corresponds to the constant term.
313 * That is, the integer division is of the form
315 * floor((... + (c * d + r) * x_pos + ...)/d)
317 * with -d < 2 * r <= d.
318 * Replace it by
320 * floor((... + r * x_pos + ...)/d) + c * x_pos
322 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
323 * Otherwise, c = floor((c * d + r)/d) + 1.
325 * This is the same normalization that is performed by isl_aff_floor.
327 static __isl_give isl_basic_map *reduce_coefficient_in_div(
328 __isl_take isl_basic_map *bmap, int div, int pos)
330 isl_int shift;
331 int add_one;
333 isl_int_init(shift);
334 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
335 isl_int_mul_ui(shift, shift, 2);
336 add_one = isl_int_gt(shift, bmap->div[div][0]);
337 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
338 if (add_one)
339 isl_int_add_ui(shift, shift, 1);
340 isl_int_neg(shift, shift);
341 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
342 isl_int_clear(shift);
344 return bmap;
347 /* Does the coefficient of the variable at position "pos"
348 * in integer division "div" need to be reduced?
349 * That is, does it lie outside the half-open interval (1/2,1/2]?
350 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
351 * 2 * c != d.
353 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
354 int pos)
356 isl_bool r;
358 if (isl_int_is_zero(bmap->div[div][1 + pos]))
359 return isl_bool_false;
361 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
362 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
363 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
364 isl_int_divexact_ui(bmap->div[div][1 + pos],
365 bmap->div[div][1 + pos], 2);
367 return r;
370 /* Reduce the coefficients (including the constant term) of
371 * integer division "div", if needed.
372 * In particular, make sure all coefficients lie in
373 * the half-open interval (1/2,1/2].
375 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
376 __isl_take isl_basic_map *bmap, int div)
378 int i;
379 unsigned total = 1 + isl_basic_map_total_dim(bmap);
381 for (i = 0; i < total; ++i) {
382 isl_bool reduce;
384 reduce = needs_reduction(bmap, div, i);
385 if (reduce < 0)
386 return isl_basic_map_free(bmap);
387 if (!reduce)
388 continue;
389 bmap = reduce_coefficient_in_div(bmap, div, i);
390 if (!bmap)
391 break;
394 return bmap;
397 /* Reduce the coefficients (including the constant term) of
398 * the known integer divisions, if needed
399 * In particular, make sure all coefficients lie in
400 * the half-open interval (1/2,1/2].
402 static __isl_give isl_basic_map *reduce_div_coefficients(
403 __isl_take isl_basic_map *bmap)
405 int i;
407 if (!bmap)
408 return NULL;
409 if (bmap->n_div == 0)
410 return bmap;
412 for (i = 0; i < bmap->n_div; ++i) {
413 if (isl_int_is_zero(bmap->div[i][0]))
414 continue;
415 bmap = reduce_div_coefficients_of_div(bmap, i);
416 if (!bmap)
417 break;
420 return bmap;
423 /* Remove any common factor in numerator and denominator of the div expression,
424 * not taking into account the constant term.
425 * That is, if the div is of the form
427 * floor((a + m f(x))/(m d))
429 * then replace it by
431 * floor((floor(a/m) + f(x))/d)
433 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
434 * and can therefore not influence the result of the floor.
436 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
438 unsigned total = isl_basic_map_total_dim(bmap);
439 isl_ctx *ctx = bmap->ctx;
441 if (isl_int_is_zero(bmap->div[div][0]))
442 return;
443 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
444 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
445 if (isl_int_is_one(ctx->normalize_gcd))
446 return;
447 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
448 ctx->normalize_gcd);
449 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
450 ctx->normalize_gcd);
451 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
452 ctx->normalize_gcd, total);
455 /* Remove any common factor in numerator and denominator of a div expression,
456 * not taking into account the constant term.
457 * That is, look for any div of the form
459 * floor((a + m f(x))/(m d))
461 * and replace it by
463 * floor((floor(a/m) + f(x))/d)
465 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
466 * and can therefore not influence the result of the floor.
468 static __isl_give isl_basic_map *normalize_div_expressions(
469 __isl_take isl_basic_map *bmap)
471 int i;
473 if (!bmap)
474 return NULL;
475 if (bmap->n_div == 0)
476 return bmap;
478 for (i = 0; i < bmap->n_div; ++i)
479 normalize_div_expression(bmap, i);
481 return bmap;
484 /* Assumes divs have been ordered if keep_divs is set.
486 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
487 unsigned pos, isl_int *eq, int keep_divs, int *progress)
489 unsigned total;
490 unsigned space_total;
491 int k;
492 int last_div;
494 total = isl_basic_map_total_dim(bmap);
495 space_total = isl_space_dim(bmap->dim, isl_dim_all);
496 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
497 for (k = 0; k < bmap->n_eq; ++k) {
498 if (bmap->eq[k] == eq)
499 continue;
500 if (isl_int_is_zero(bmap->eq[k][1+pos]))
501 continue;
502 if (progress)
503 *progress = 1;
504 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
505 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
508 for (k = 0; k < bmap->n_ineq; ++k) {
509 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
510 continue;
511 if (progress)
512 *progress = 1;
513 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
514 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
515 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
518 for (k = 0; k < bmap->n_div; ++k) {
519 if (isl_int_is_zero(bmap->div[k][0]))
520 continue;
521 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
522 continue;
523 if (progress)
524 *progress = 1;
525 /* We need to be careful about circular definitions,
526 * so for now we just remove the definition of div k
527 * if the equality contains any divs.
528 * If keep_divs is set, then the divs have been ordered
529 * and we can keep the definition as long as the result
530 * is still ordered.
532 if (last_div == -1 || (keep_divs && last_div < k)) {
533 isl_seq_elim(bmap->div[k]+1, eq,
534 1+pos, 1+total, &bmap->div[k][0]);
535 normalize_div_expression(bmap, k);
536 } else
537 isl_seq_clr(bmap->div[k], 1 + total);
538 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
542 /* Assumes divs have been ordered if keep_divs is set.
544 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
545 isl_int *eq, unsigned div, int keep_divs)
547 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
549 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
551 bmap = isl_basic_map_drop_div(bmap, div);
553 return bmap;
556 /* Check if elimination of div "div" using equality "eq" would not
557 * result in a div depending on a later div.
559 static isl_bool ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
560 unsigned div)
562 int k;
563 int last_div;
564 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
565 unsigned pos = space_total + div;
567 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
568 if (last_div < 0 || last_div <= div)
569 return isl_bool_true;
571 for (k = 0; k <= last_div; ++k) {
572 if (isl_int_is_zero(bmap->div[k][0]))
573 continue;
574 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
575 return isl_bool_false;
578 return isl_bool_true;
581 /* Eliminate divs based on equalities
583 static struct isl_basic_map *eliminate_divs_eq(
584 struct isl_basic_map *bmap, int *progress)
586 int d;
587 int i;
588 int modified = 0;
589 unsigned off;
591 bmap = isl_basic_map_order_divs(bmap);
593 if (!bmap)
594 return NULL;
596 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
598 for (d = bmap->n_div - 1; d >= 0 ; --d) {
599 for (i = 0; i < bmap->n_eq; ++i) {
600 isl_bool ok;
602 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
603 !isl_int_is_negone(bmap->eq[i][off + d]))
604 continue;
605 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
606 if (ok < 0)
607 return isl_basic_map_free(bmap);
608 if (!ok)
609 continue;
610 modified = 1;
611 *progress = 1;
612 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
613 if (isl_basic_map_drop_equality(bmap, i) < 0)
614 return isl_basic_map_free(bmap);
615 break;
618 if (modified)
619 return eliminate_divs_eq(bmap, progress);
620 return bmap;
623 /* Elimininate divs based on inequalities
625 static struct isl_basic_map *eliminate_divs_ineq(
626 struct isl_basic_map *bmap, int *progress)
628 int d;
629 int i;
630 unsigned off;
631 struct isl_ctx *ctx;
633 if (!bmap)
634 return NULL;
636 ctx = bmap->ctx;
637 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
639 for (d = bmap->n_div - 1; d >= 0 ; --d) {
640 for (i = 0; i < bmap->n_eq; ++i)
641 if (!isl_int_is_zero(bmap->eq[i][off + d]))
642 break;
643 if (i < bmap->n_eq)
644 continue;
645 for (i = 0; i < bmap->n_ineq; ++i)
646 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
647 break;
648 if (i < bmap->n_ineq)
649 continue;
650 *progress = 1;
651 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
652 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
653 break;
654 bmap = isl_basic_map_drop_div(bmap, d);
655 if (!bmap)
656 break;
658 return bmap;
661 /* Does the equality constraint at position "eq" in "bmap" involve
662 * any local variables in the range [first, first + n)
663 * that are not marked as having an explicit representation?
665 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
666 int eq, unsigned first, unsigned n)
668 unsigned o_div;
669 int i;
671 if (!bmap)
672 return isl_bool_error;
674 o_div = isl_basic_map_offset(bmap, isl_dim_div);
675 for (i = 0; i < n; ++i) {
676 isl_bool unknown;
678 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
679 continue;
680 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
681 if (unknown < 0)
682 return isl_bool_error;
683 if (unknown)
684 return isl_bool_true;
687 return isl_bool_false;
690 /* The last local variable involved in the equality constraint
691 * at position "eq" in "bmap" is the local variable at position "div".
692 * It can therefore be used to extract an explicit representation
693 * for that variable.
694 * Do so unless the local variable already has an explicit representation or
695 * the explicit representation would involve any other local variables
696 * that in turn do not have an explicit representation.
697 * An equality constraint involving local variables without an explicit
698 * representation can be used in isl_basic_map_drop_redundant_divs
699 * to separate out an independent local variable. Introducing
700 * an explicit representation here would block this transformation,
701 * while the partial explicit representation in itself is not very useful.
702 * Set *progress if anything is changed.
704 * The equality constraint is of the form
706 * f(x) + n e >= 0
708 * with n a positive number. The explicit representation derived from
709 * this constraint is
711 * floor((-f(x))/n)
713 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
714 int div, int eq, int *progress)
716 unsigned total, o_div;
717 isl_bool involves;
719 if (!bmap)
720 return NULL;
722 if (!isl_int_is_zero(bmap->div[div][0]))
723 return bmap;
725 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
726 if (involves < 0)
727 return isl_basic_map_free(bmap);
728 if (involves)
729 return bmap;
731 total = isl_basic_map_dim(bmap, isl_dim_all);
732 o_div = isl_basic_map_offset(bmap, isl_dim_div);
733 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
734 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
735 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
736 if (progress)
737 *progress = 1;
738 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
740 return bmap;
743 struct isl_basic_map *isl_basic_map_gauss(
744 struct isl_basic_map *bmap, int *progress)
746 int k;
747 int done;
748 int last_var;
749 unsigned total_var;
750 unsigned total;
752 bmap = isl_basic_map_order_divs(bmap);
754 if (!bmap)
755 return NULL;
757 total = isl_basic_map_total_dim(bmap);
758 total_var = total - bmap->n_div;
760 last_var = total - 1;
761 for (done = 0; done < bmap->n_eq; ++done) {
762 for (; last_var >= 0; --last_var) {
763 for (k = done; k < bmap->n_eq; ++k)
764 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
765 break;
766 if (k < bmap->n_eq)
767 break;
769 if (last_var < 0)
770 break;
771 if (k != done)
772 swap_equality(bmap, k, done);
773 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
774 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
776 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
777 progress);
779 if (last_var >= total_var)
780 bmap = set_div_from_eq(bmap, last_var - total_var,
781 done, progress);
782 if (!bmap)
783 return NULL;
785 if (done == bmap->n_eq)
786 return bmap;
787 for (k = done; k < bmap->n_eq; ++k) {
788 if (isl_int_is_zero(bmap->eq[k][0]))
789 continue;
790 return isl_basic_map_set_to_empty(bmap);
792 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
793 return bmap;
796 struct isl_basic_set *isl_basic_set_gauss(
797 struct isl_basic_set *bset, int *progress)
799 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
800 progress));
804 static unsigned int round_up(unsigned int v)
806 int old_v = v;
808 while (v) {
809 old_v = v;
810 v ^= v & -v;
812 return old_v << 1;
815 /* Hash table of inequalities in a basic map.
816 * "index" is an array of addresses of inequalities in the basic map, some
817 * of which are NULL. The inequalities are hashed on the coefficients
818 * except the constant term.
819 * "size" is the number of elements in the array and is always a power of two
820 * "bits" is the number of bits need to represent an index into the array.
821 * "total" is the total dimension of the basic map.
823 struct isl_constraint_index {
824 unsigned int size;
825 int bits;
826 isl_int ***index;
827 unsigned total;
830 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
832 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
833 __isl_keep isl_basic_map *bmap)
835 isl_ctx *ctx;
837 ci->index = NULL;
838 if (!bmap)
839 return isl_stat_error;
840 ci->total = isl_basic_set_total_dim(bmap);
841 if (bmap->n_ineq == 0)
842 return isl_stat_ok;
843 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
844 ci->bits = ffs(ci->size) - 1;
845 ctx = isl_basic_map_get_ctx(bmap);
846 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
847 if (!ci->index)
848 return isl_stat_error;
850 return isl_stat_ok;
853 /* Free the memory allocated by create_constraint_index.
855 static void constraint_index_free(struct isl_constraint_index *ci)
857 free(ci->index);
860 /* Return the position in ci->index that contains the address of
861 * an inequality that is equal to *ineq up to the constant term,
862 * provided this address is not identical to "ineq".
863 * If there is no such inequality, then return the position where
864 * such an inequality should be inserted.
866 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
868 int h;
869 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
870 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
871 if (ineq != ci->index[h] &&
872 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
873 break;
874 return h;
877 /* Return the position in ci->index that contains the address of
878 * an inequality that is equal to the k'th inequality of "bmap"
879 * up to the constant term, provided it does not point to the very
880 * same inequality.
881 * If there is no such inequality, then return the position where
882 * such an inequality should be inserted.
884 static int hash_index(struct isl_constraint_index *ci,
885 __isl_keep isl_basic_map *bmap, int k)
887 return hash_index_ineq(ci, &bmap->ineq[k]);
890 static int set_hash_index(struct isl_constraint_index *ci,
891 struct isl_basic_set *bset, int k)
893 return hash_index(ci, bset, k);
896 /* Fill in the "ci" data structure with the inequalities of "bset".
898 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
899 __isl_keep isl_basic_set *bset)
901 int k, h;
903 if (create_constraint_index(ci, bset) < 0)
904 return isl_stat_error;
906 for (k = 0; k < bset->n_ineq; ++k) {
907 h = set_hash_index(ci, bset, k);
908 ci->index[h] = &bset->ineq[k];
911 return isl_stat_ok;
914 /* Is the inequality ineq (obviously) redundant with respect
915 * to the constraints in "ci"?
917 * Look for an inequality in "ci" with the same coefficients and then
918 * check if the contant term of "ineq" is greater than or equal
919 * to the constant term of that inequality. If so, "ineq" is clearly
920 * redundant.
922 * Note that hash_index_ineq ignores a stored constraint if it has
923 * the same address as the passed inequality. It is ok to pass
924 * the address of a local variable here since it will never be
925 * the same as the address of a constraint in "ci".
927 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
928 isl_int *ineq)
930 int h;
932 h = hash_index_ineq(ci, &ineq);
933 if (!ci->index[h])
934 return isl_bool_false;
935 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
938 /* If we can eliminate more than one div, then we need to make
939 * sure we do it from last div to first div, in order not to
940 * change the position of the other divs that still need to
941 * be removed.
943 static struct isl_basic_map *remove_duplicate_divs(
944 struct isl_basic_map *bmap, int *progress)
946 unsigned int size;
947 int *index;
948 int *elim_for;
949 int k, l, h;
950 int bits;
951 struct isl_blk eq;
952 unsigned total_var;
953 unsigned total;
954 struct isl_ctx *ctx;
956 bmap = isl_basic_map_order_divs(bmap);
957 if (!bmap || bmap->n_div <= 1)
958 return bmap;
960 total_var = isl_space_dim(bmap->dim, isl_dim_all);
961 total = total_var + bmap->n_div;
963 ctx = bmap->ctx;
964 for (k = bmap->n_div - 1; k >= 0; --k)
965 if (!isl_int_is_zero(bmap->div[k][0]))
966 break;
967 if (k <= 0)
968 return bmap;
970 size = round_up(4 * bmap->n_div / 3 - 1);
971 if (size == 0)
972 return bmap;
973 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
974 bits = ffs(size) - 1;
975 index = isl_calloc_array(ctx, int, size);
976 if (!elim_for || !index)
977 goto out;
978 eq = isl_blk_alloc(ctx, 1+total);
979 if (isl_blk_is_error(eq))
980 goto out;
982 isl_seq_clr(eq.data, 1+total);
983 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
984 for (--k; k >= 0; --k) {
985 uint32_t hash;
987 if (isl_int_is_zero(bmap->div[k][0]))
988 continue;
990 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
991 for (h = hash; index[h]; h = (h+1) % size)
992 if (isl_seq_eq(bmap->div[k],
993 bmap->div[index[h]-1], 2+total))
994 break;
995 if (index[h]) {
996 *progress = 1;
997 l = index[h] - 1;
998 elim_for[l] = k + 1;
1000 index[h] = k+1;
1002 for (l = bmap->n_div - 1; l >= 0; --l) {
1003 if (!elim_for[l])
1004 continue;
1005 k = elim_for[l] - 1;
1006 isl_int_set_si(eq.data[1+total_var+k], -1);
1007 isl_int_set_si(eq.data[1+total_var+l], 1);
1008 bmap = eliminate_div(bmap, eq.data, l, 1);
1009 if (!bmap)
1010 break;
1011 isl_int_set_si(eq.data[1+total_var+k], 0);
1012 isl_int_set_si(eq.data[1+total_var+l], 0);
1015 isl_blk_free(ctx, eq);
1016 out:
1017 free(index);
1018 free(elim_for);
1019 return bmap;
1022 static int n_pure_div_eq(struct isl_basic_map *bmap)
1024 int i, j;
1025 unsigned total;
1027 total = isl_space_dim(bmap->dim, isl_dim_all);
1028 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1029 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1030 --j;
1031 if (j < 0)
1032 break;
1033 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
1034 return 0;
1036 return i;
1039 /* Normalize divs that appear in equalities.
1041 * In particular, we assume that bmap contains some equalities
1042 * of the form
1044 * a x = m * e_i
1046 * and we want to replace the set of e_i by a minimal set and
1047 * such that the new e_i have a canonical representation in terms
1048 * of the vector x.
1049 * If any of the equalities involves more than one divs, then
1050 * we currently simply bail out.
1052 * Let us first additionally assume that all equalities involve
1053 * a div. The equalities then express modulo constraints on the
1054 * remaining variables and we can use "parameter compression"
1055 * to find a minimal set of constraints. The result is a transformation
1057 * x = T(x') = x_0 + G x'
1059 * with G a lower-triangular matrix with all elements below the diagonal
1060 * non-negative and smaller than the diagonal element on the same row.
1061 * We first normalize x_0 by making the same property hold in the affine
1062 * T matrix.
1063 * The rows i of G with a 1 on the diagonal do not impose any modulo
1064 * constraint and simply express x_i = x'_i.
1065 * For each of the remaining rows i, we introduce a div and a corresponding
1066 * equality. In particular
1068 * g_ii e_j = x_i - g_i(x')
1070 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1071 * corresponding div (if g_kk != 1).
1073 * If there are any equalities not involving any div, then we
1074 * first apply a variable compression on the variables x:
1076 * x = C x'' x'' = C_2 x
1078 * and perform the above parameter compression on A C instead of on A.
1079 * The resulting compression is then of the form
1081 * x'' = T(x') = x_0 + G x'
1083 * and in constructing the new divs and the corresponding equalities,
1084 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1085 * by the corresponding row from C_2.
1087 static struct isl_basic_map *normalize_divs(
1088 struct isl_basic_map *bmap, int *progress)
1090 int i, j, k;
1091 int total;
1092 int div_eq;
1093 struct isl_mat *B;
1094 struct isl_vec *d;
1095 struct isl_mat *T = NULL;
1096 struct isl_mat *C = NULL;
1097 struct isl_mat *C2 = NULL;
1098 isl_int v;
1099 int *pos = NULL;
1100 int dropped, needed;
1102 if (!bmap)
1103 return NULL;
1105 if (bmap->n_div == 0)
1106 return bmap;
1108 if (bmap->n_eq == 0)
1109 return bmap;
1111 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1112 return bmap;
1114 total = isl_space_dim(bmap->dim, isl_dim_all);
1115 div_eq = n_pure_div_eq(bmap);
1116 if (div_eq == 0)
1117 return bmap;
1119 if (div_eq < bmap->n_eq) {
1120 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1121 bmap->n_eq - div_eq, 0, 1 + total);
1122 C = isl_mat_variable_compression(B, &C2);
1123 if (!C || !C2)
1124 goto error;
1125 if (C->n_col == 0) {
1126 bmap = isl_basic_map_set_to_empty(bmap);
1127 isl_mat_free(C);
1128 isl_mat_free(C2);
1129 goto done;
1133 d = isl_vec_alloc(bmap->ctx, div_eq);
1134 if (!d)
1135 goto error;
1136 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1137 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1138 --j;
1139 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1141 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1143 if (C) {
1144 B = isl_mat_product(B, C);
1145 C = NULL;
1148 T = isl_mat_parameter_compression(B, d);
1149 if (!T)
1150 goto error;
1151 if (T->n_col == 0) {
1152 bmap = isl_basic_map_set_to_empty(bmap);
1153 isl_mat_free(C2);
1154 isl_mat_free(T);
1155 goto done;
1157 isl_int_init(v);
1158 for (i = 0; i < T->n_row - 1; ++i) {
1159 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1160 if (isl_int_is_zero(v))
1161 continue;
1162 isl_mat_col_submul(T, 0, v, 1 + i);
1164 isl_int_clear(v);
1165 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1166 if (!pos)
1167 goto error;
1168 /* We have to be careful because dropping equalities may reorder them */
1169 dropped = 0;
1170 for (j = bmap->n_div - 1; j >= 0; --j) {
1171 for (i = 0; i < bmap->n_eq; ++i)
1172 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1173 break;
1174 if (i < bmap->n_eq) {
1175 bmap = isl_basic_map_drop_div(bmap, j);
1176 isl_basic_map_drop_equality(bmap, i);
1177 ++dropped;
1180 pos[0] = 0;
1181 needed = 0;
1182 for (i = 1; i < T->n_row; ++i) {
1183 if (isl_int_is_one(T->row[i][i]))
1184 pos[i] = i;
1185 else
1186 needed++;
1188 if (needed > dropped) {
1189 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1190 needed, needed, 0);
1191 if (!bmap)
1192 goto error;
1194 for (i = 1; i < T->n_row; ++i) {
1195 if (isl_int_is_one(T->row[i][i]))
1196 continue;
1197 k = isl_basic_map_alloc_div(bmap);
1198 pos[i] = 1 + total + k;
1199 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1200 isl_int_set(bmap->div[k][0], T->row[i][i]);
1201 if (C2)
1202 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1203 else
1204 isl_int_set_si(bmap->div[k][1 + i], 1);
1205 for (j = 0; j < i; ++j) {
1206 if (isl_int_is_zero(T->row[i][j]))
1207 continue;
1208 if (pos[j] < T->n_row && C2)
1209 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1210 C2->row[pos[j]], 1 + total);
1211 else
1212 isl_int_neg(bmap->div[k][1 + pos[j]],
1213 T->row[i][j]);
1215 j = isl_basic_map_alloc_equality(bmap);
1216 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1217 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1219 free(pos);
1220 isl_mat_free(C2);
1221 isl_mat_free(T);
1223 if (progress)
1224 *progress = 1;
1225 done:
1226 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1228 return bmap;
1229 error:
1230 free(pos);
1231 isl_mat_free(C);
1232 isl_mat_free(C2);
1233 isl_mat_free(T);
1234 return bmap;
1237 static struct isl_basic_map *set_div_from_lower_bound(
1238 struct isl_basic_map *bmap, int div, int ineq)
1240 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1242 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1243 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1244 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1245 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1246 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1248 return bmap;
1251 /* Check whether it is ok to define a div based on an inequality.
1252 * To avoid the introduction of circular definitions of divs, we
1253 * do not allow such a definition if the resulting expression would refer to
1254 * any other undefined divs or if any known div is defined in
1255 * terms of the unknown div.
1257 static isl_bool ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1258 int div, int ineq)
1260 int j;
1261 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1263 /* Not defined in terms of unknown divs */
1264 for (j = 0; j < bmap->n_div; ++j) {
1265 if (div == j)
1266 continue;
1267 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1268 continue;
1269 if (isl_int_is_zero(bmap->div[j][0]))
1270 return isl_bool_false;
1273 /* No other div defined in terms of this one => avoid loops */
1274 for (j = 0; j < bmap->n_div; ++j) {
1275 if (div == j)
1276 continue;
1277 if (isl_int_is_zero(bmap->div[j][0]))
1278 continue;
1279 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1280 return isl_bool_false;
1283 return isl_bool_true;
1286 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1287 * be a better expression than the current one?
1289 * If we do not have any expression yet, then any expression would be better.
1290 * Otherwise we check if the last variable involved in the inequality
1291 * (disregarding the div that it would define) is in an earlier position
1292 * than the last variable involved in the current div expression.
1294 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1295 int div, int ineq)
1297 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1298 int last_div;
1299 int last_ineq;
1301 if (isl_int_is_zero(bmap->div[div][0]))
1302 return isl_bool_true;
1304 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1305 bmap->n_div - (div + 1)) >= 0)
1306 return isl_bool_false;
1308 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1309 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1310 total + bmap->n_div);
1312 return last_ineq < last_div;
1315 /* Given two constraints "k" and "l" that are opposite to each other,
1316 * except for the constant term, check if we can use them
1317 * to obtain an expression for one of the hitherto unknown divs or
1318 * a "better" expression for a div for which we already have an expression.
1319 * "sum" is the sum of the constant terms of the constraints.
1320 * If this sum is strictly smaller than the coefficient of one
1321 * of the divs, then this pair can be used define the div.
1322 * To avoid the introduction of circular definitions of divs, we
1323 * do not use the pair if the resulting expression would refer to
1324 * any other undefined divs or if any known div is defined in
1325 * terms of the unknown div.
1327 static struct isl_basic_map *check_for_div_constraints(
1328 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1330 int i;
1331 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1333 for (i = 0; i < bmap->n_div; ++i) {
1334 isl_bool set_div;
1336 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1337 continue;
1338 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1339 continue;
1340 set_div = better_div_constraint(bmap, i, k);
1341 if (set_div >= 0 && set_div)
1342 set_div = ok_to_set_div_from_bound(bmap, i, k);
1343 if (set_div < 0)
1344 return isl_basic_map_free(bmap);
1345 if (!set_div)
1346 break;
1347 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1348 bmap = set_div_from_lower_bound(bmap, i, k);
1349 else
1350 bmap = set_div_from_lower_bound(bmap, i, l);
1351 if (progress)
1352 *progress = 1;
1353 break;
1355 return bmap;
1358 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1359 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1361 struct isl_constraint_index ci;
1362 int k, l, h;
1363 unsigned total = isl_basic_map_total_dim(bmap);
1364 isl_int sum;
1366 if (!bmap || bmap->n_ineq <= 1)
1367 return bmap;
1369 if (create_constraint_index(&ci, bmap) < 0)
1370 return bmap;
1372 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1373 ci.index[h] = &bmap->ineq[0];
1374 for (k = 1; k < bmap->n_ineq; ++k) {
1375 h = hash_index(&ci, bmap, k);
1376 if (!ci.index[h]) {
1377 ci.index[h] = &bmap->ineq[k];
1378 continue;
1380 if (progress)
1381 *progress = 1;
1382 l = ci.index[h] - &bmap->ineq[0];
1383 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1384 swap_inequality(bmap, k, l);
1385 isl_basic_map_drop_inequality(bmap, k);
1386 --k;
1388 isl_int_init(sum);
1389 for (k = 0; k < bmap->n_ineq-1; ++k) {
1390 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1391 h = hash_index(&ci, bmap, k);
1392 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1393 if (!ci.index[h])
1394 continue;
1395 l = ci.index[h] - &bmap->ineq[0];
1396 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1397 if (isl_int_is_pos(sum)) {
1398 if (detect_divs)
1399 bmap = check_for_div_constraints(bmap, k, l,
1400 sum, progress);
1401 continue;
1403 if (isl_int_is_zero(sum)) {
1404 /* We need to break out of the loop after these
1405 * changes since the contents of the hash
1406 * will no longer be valid.
1407 * Plus, we probably we want to regauss first.
1409 if (progress)
1410 *progress = 1;
1411 isl_basic_map_drop_inequality(bmap, l);
1412 isl_basic_map_inequality_to_equality(bmap, k);
1413 } else
1414 bmap = isl_basic_map_set_to_empty(bmap);
1415 break;
1417 isl_int_clear(sum);
1419 constraint_index_free(&ci);
1420 return bmap;
1423 /* Detect all pairs of inequalities that form an equality.
1425 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1426 * Call it repeatedly while it is making progress.
1428 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1429 __isl_take isl_basic_map *bmap, int *progress)
1431 int duplicate;
1433 do {
1434 duplicate = 0;
1435 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1436 &duplicate, 0);
1437 if (progress && duplicate)
1438 *progress = 1;
1439 } while (duplicate);
1441 return bmap;
1444 /* Eliminate knowns divs from constraints where they appear with
1445 * a (positive or negative) unit coefficient.
1447 * That is, replace
1449 * floor(e/m) + f >= 0
1451 * by
1453 * e + m f >= 0
1455 * and
1457 * -floor(e/m) + f >= 0
1459 * by
1461 * -e + m f + m - 1 >= 0
1463 * The first conversion is valid because floor(e/m) >= -f is equivalent
1464 * to e/m >= -f because -f is an integral expression.
1465 * The second conversion follows from the fact that
1467 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1470 * Note that one of the div constraints may have been eliminated
1471 * due to being redundant with respect to the constraint that is
1472 * being modified by this function. The modified constraint may
1473 * no longer imply this div constraint, so we add it back to make
1474 * sure we do not lose any information.
1476 * We skip integral divs, i.e., those with denominator 1, as we would
1477 * risk eliminating the div from the div constraints. We do not need
1478 * to handle those divs here anyway since the div constraints will turn
1479 * out to form an equality and this equality can then be used to eliminate
1480 * the div from all constraints.
1482 static __isl_give isl_basic_map *eliminate_unit_divs(
1483 __isl_take isl_basic_map *bmap, int *progress)
1485 int i, j;
1486 isl_ctx *ctx;
1487 unsigned total;
1489 if (!bmap)
1490 return NULL;
1492 ctx = isl_basic_map_get_ctx(bmap);
1493 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1495 for (i = 0; i < bmap->n_div; ++i) {
1496 if (isl_int_is_zero(bmap->div[i][0]))
1497 continue;
1498 if (isl_int_is_one(bmap->div[i][0]))
1499 continue;
1500 for (j = 0; j < bmap->n_ineq; ++j) {
1501 int s;
1503 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1504 !isl_int_is_negone(bmap->ineq[j][total + i]))
1505 continue;
1507 *progress = 1;
1509 s = isl_int_sgn(bmap->ineq[j][total + i]);
1510 isl_int_set_si(bmap->ineq[j][total + i], 0);
1511 if (s < 0)
1512 isl_seq_combine(bmap->ineq[j],
1513 ctx->negone, bmap->div[i] + 1,
1514 bmap->div[i][0], bmap->ineq[j],
1515 total + bmap->n_div);
1516 else
1517 isl_seq_combine(bmap->ineq[j],
1518 ctx->one, bmap->div[i] + 1,
1519 bmap->div[i][0], bmap->ineq[j],
1520 total + bmap->n_div);
1521 if (s < 0) {
1522 isl_int_add(bmap->ineq[j][0],
1523 bmap->ineq[j][0], bmap->div[i][0]);
1524 isl_int_sub_ui(bmap->ineq[j][0],
1525 bmap->ineq[j][0], 1);
1528 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1529 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1530 return isl_basic_map_free(bmap);
1534 return bmap;
1537 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1539 int progress = 1;
1540 if (!bmap)
1541 return NULL;
1542 while (progress) {
1543 isl_bool empty;
1545 progress = 0;
1546 empty = isl_basic_map_plain_is_empty(bmap);
1547 if (empty < 0)
1548 return isl_basic_map_free(bmap);
1549 if (empty)
1550 break;
1551 bmap = isl_basic_map_normalize_constraints(bmap);
1552 bmap = reduce_div_coefficients(bmap);
1553 bmap = normalize_div_expressions(bmap);
1554 bmap = remove_duplicate_divs(bmap, &progress);
1555 bmap = eliminate_unit_divs(bmap, &progress);
1556 bmap = eliminate_divs_eq(bmap, &progress);
1557 bmap = eliminate_divs_ineq(bmap, &progress);
1558 bmap = isl_basic_map_gauss(bmap, &progress);
1559 /* requires equalities in normal form */
1560 bmap = normalize_divs(bmap, &progress);
1561 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1562 &progress, 1);
1563 if (bmap && progress)
1564 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1566 return bmap;
1569 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1571 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1575 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1576 isl_int *constraint, unsigned div)
1578 unsigned pos;
1580 if (!bmap)
1581 return isl_bool_error;
1583 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1585 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1586 int neg;
1587 isl_int_sub(bmap->div[div][1],
1588 bmap->div[div][1], bmap->div[div][0]);
1589 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1590 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1591 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1592 isl_int_add(bmap->div[div][1],
1593 bmap->div[div][1], bmap->div[div][0]);
1594 if (!neg)
1595 return isl_bool_false;
1596 if (isl_seq_first_non_zero(constraint+pos+1,
1597 bmap->n_div-div-1) != -1)
1598 return isl_bool_false;
1599 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1600 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1601 return isl_bool_false;
1602 if (isl_seq_first_non_zero(constraint+pos+1,
1603 bmap->n_div-div-1) != -1)
1604 return isl_bool_false;
1605 } else
1606 return isl_bool_false;
1608 return isl_bool_true;
1611 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1612 isl_int *constraint, unsigned div)
1614 return isl_basic_map_is_div_constraint(bset, constraint, div);
1618 /* If the only constraints a div d=floor(f/m)
1619 * appears in are its two defining constraints
1621 * f - m d >=0
1622 * -(f - (m - 1)) + m d >= 0
1624 * then it can safely be removed.
1626 static isl_bool div_is_redundant(struct isl_basic_map *bmap, int div)
1628 int i;
1629 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1631 for (i = 0; i < bmap->n_eq; ++i)
1632 if (!isl_int_is_zero(bmap->eq[i][pos]))
1633 return isl_bool_false;
1635 for (i = 0; i < bmap->n_ineq; ++i) {
1636 isl_bool red;
1638 if (isl_int_is_zero(bmap->ineq[i][pos]))
1639 continue;
1640 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1641 if (red < 0 || !red)
1642 return red;
1645 for (i = 0; i < bmap->n_div; ++i) {
1646 if (isl_int_is_zero(bmap->div[i][0]))
1647 continue;
1648 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1649 return isl_bool_false;
1652 return isl_bool_true;
1656 * Remove divs that don't occur in any of the constraints or other divs.
1657 * These can arise when dropping constraints from a basic map or
1658 * when the divs of a basic map have been temporarily aligned
1659 * with the divs of another basic map.
1661 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1663 int i;
1665 if (!bmap)
1666 return NULL;
1668 for (i = bmap->n_div-1; i >= 0; --i) {
1669 isl_bool redundant;
1671 redundant = div_is_redundant(bmap, i);
1672 if (redundant < 0)
1673 return isl_basic_map_free(bmap);
1674 if (!redundant)
1675 continue;
1676 bmap = isl_basic_map_drop_div(bmap, i);
1678 return bmap;
1681 /* Mark "bmap" as final, without checking for obviously redundant
1682 * integer divisions. This function should be used when "bmap"
1683 * is known not to involve any such integer divisions.
1685 __isl_give isl_basic_map *isl_basic_map_mark_final(
1686 __isl_take isl_basic_map *bmap)
1688 if (!bmap)
1689 return NULL;
1690 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1691 return bmap;
1694 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1696 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1698 bmap = remove_redundant_divs(bmap);
1699 bmap = isl_basic_map_mark_final(bmap);
1700 return bmap;
1703 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1705 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1708 /* Remove definition of any div that is defined in terms of the given variable.
1709 * The div itself is not removed. Functions such as
1710 * eliminate_divs_ineq depend on the other divs remaining in place.
1712 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1713 int pos)
1715 int i;
1717 if (!bmap)
1718 return NULL;
1720 for (i = 0; i < bmap->n_div; ++i) {
1721 if (isl_int_is_zero(bmap->div[i][0]))
1722 continue;
1723 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1724 continue;
1725 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1726 if (!bmap)
1727 return NULL;
1729 return bmap;
1732 /* Eliminate the specified variables from the constraints using
1733 * Fourier-Motzkin. The variables themselves are not removed.
1735 struct isl_basic_map *isl_basic_map_eliminate_vars(
1736 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1738 int d;
1739 int i, j, k;
1740 unsigned total;
1741 int need_gauss = 0;
1743 if (n == 0)
1744 return bmap;
1745 if (!bmap)
1746 return NULL;
1747 total = isl_basic_map_total_dim(bmap);
1749 bmap = isl_basic_map_cow(bmap);
1750 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1751 bmap = remove_dependent_vars(bmap, d);
1752 if (!bmap)
1753 return NULL;
1755 for (d = pos + n - 1;
1756 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1757 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1758 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1759 int n_lower, n_upper;
1760 if (!bmap)
1761 return NULL;
1762 for (i = 0; i < bmap->n_eq; ++i) {
1763 if (isl_int_is_zero(bmap->eq[i][1+d]))
1764 continue;
1765 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1766 isl_basic_map_drop_equality(bmap, i);
1767 need_gauss = 1;
1768 break;
1770 if (i < bmap->n_eq)
1771 continue;
1772 n_lower = 0;
1773 n_upper = 0;
1774 for (i = 0; i < bmap->n_ineq; ++i) {
1775 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1776 n_lower++;
1777 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1778 n_upper++;
1780 bmap = isl_basic_map_extend_constraints(bmap,
1781 0, n_lower * n_upper);
1782 if (!bmap)
1783 goto error;
1784 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1785 int last;
1786 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1787 continue;
1788 last = -1;
1789 for (j = 0; j < i; ++j) {
1790 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1791 continue;
1792 last = j;
1793 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1794 isl_int_sgn(bmap->ineq[j][1+d]))
1795 continue;
1796 k = isl_basic_map_alloc_inequality(bmap);
1797 if (k < 0)
1798 goto error;
1799 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1800 1+total);
1801 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1802 1+d, 1+total, NULL);
1804 isl_basic_map_drop_inequality(bmap, i);
1805 i = last + 1;
1807 if (n_lower > 0 && n_upper > 0) {
1808 bmap = isl_basic_map_normalize_constraints(bmap);
1809 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1810 NULL, 0);
1811 bmap = isl_basic_map_gauss(bmap, NULL);
1812 bmap = isl_basic_map_remove_redundancies(bmap);
1813 need_gauss = 0;
1814 if (!bmap)
1815 goto error;
1816 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1817 break;
1820 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1821 if (need_gauss)
1822 bmap = isl_basic_map_gauss(bmap, NULL);
1823 return bmap;
1824 error:
1825 isl_basic_map_free(bmap);
1826 return NULL;
1829 struct isl_basic_set *isl_basic_set_eliminate_vars(
1830 struct isl_basic_set *bset, unsigned pos, unsigned n)
1832 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1833 pos, n));
1836 /* Eliminate the specified n dimensions starting at first from the
1837 * constraints, without removing the dimensions from the space.
1838 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1839 * Otherwise, they are projected out and the original space is restored.
1841 __isl_give isl_basic_map *isl_basic_map_eliminate(
1842 __isl_take isl_basic_map *bmap,
1843 enum isl_dim_type type, unsigned first, unsigned n)
1845 isl_space *space;
1847 if (!bmap)
1848 return NULL;
1849 if (n == 0)
1850 return bmap;
1852 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1853 isl_die(bmap->ctx, isl_error_invalid,
1854 "index out of bounds", goto error);
1856 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1857 first += isl_basic_map_offset(bmap, type) - 1;
1858 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1859 return isl_basic_map_finalize(bmap);
1862 space = isl_basic_map_get_space(bmap);
1863 bmap = isl_basic_map_project_out(bmap, type, first, n);
1864 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1865 bmap = isl_basic_map_reset_space(bmap, space);
1866 return bmap;
1867 error:
1868 isl_basic_map_free(bmap);
1869 return NULL;
1872 __isl_give isl_basic_set *isl_basic_set_eliminate(
1873 __isl_take isl_basic_set *bset,
1874 enum isl_dim_type type, unsigned first, unsigned n)
1876 return isl_basic_map_eliminate(bset, type, first, n);
1879 /* Remove all constraints from "bmap" that reference any unknown local
1880 * variables (directly or indirectly).
1882 * Dropping all constraints on a local variable will make it redundant,
1883 * so it will get removed implicitly by
1884 * isl_basic_map_drop_constraints_involving_dims. Some other local
1885 * variables may also end up becoming redundant if they only appear
1886 * in constraints together with the unknown local variable.
1887 * Therefore, start over after calling
1888 * isl_basic_map_drop_constraints_involving_dims.
1890 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1891 __isl_take isl_basic_map *bmap)
1893 isl_bool known;
1894 int i, n_div, o_div;
1896 known = isl_basic_map_divs_known(bmap);
1897 if (known < 0)
1898 return isl_basic_map_free(bmap);
1899 if (known)
1900 return bmap;
1902 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1903 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1905 for (i = 0; i < n_div; ++i) {
1906 known = isl_basic_map_div_is_known(bmap, i);
1907 if (known < 0)
1908 return isl_basic_map_free(bmap);
1909 if (known)
1910 continue;
1911 bmap = remove_dependent_vars(bmap, o_div + i);
1912 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1913 isl_dim_div, i, 1);
1914 if (!bmap)
1915 return NULL;
1916 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1917 i = -1;
1920 return bmap;
1923 /* Remove all constraints from "map" that reference any unknown local
1924 * variables (directly or indirectly).
1926 * Since constraints may get dropped from the basic maps,
1927 * they may no longer be disjoint from each other.
1929 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1930 __isl_take isl_map *map)
1932 int i;
1933 isl_bool known;
1935 known = isl_map_divs_known(map);
1936 if (known < 0)
1937 return isl_map_free(map);
1938 if (known)
1939 return map;
1941 map = isl_map_cow(map);
1942 if (!map)
1943 return NULL;
1945 for (i = 0; i < map->n; ++i) {
1946 map->p[i] =
1947 isl_basic_map_drop_constraint_involving_unknown_divs(
1948 map->p[i]);
1949 if (!map->p[i])
1950 return isl_map_free(map);
1953 if (map->n > 1)
1954 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1956 return map;
1959 /* Don't assume equalities are in order, because align_divs
1960 * may have changed the order of the divs.
1962 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1964 int d, i;
1965 unsigned total;
1967 total = isl_space_dim(bmap->dim, isl_dim_all);
1968 for (d = 0; d < total; ++d)
1969 elim[d] = -1;
1970 for (i = 0; i < bmap->n_eq; ++i) {
1971 for (d = total - 1; d >= 0; --d) {
1972 if (isl_int_is_zero(bmap->eq[i][1+d]))
1973 continue;
1974 elim[d] = i;
1975 break;
1980 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1982 compute_elimination_index(bset_to_bmap(bset), elim);
1985 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1986 struct isl_basic_map *bmap, int *elim)
1988 int d;
1989 int copied = 0;
1990 unsigned total;
1992 total = isl_space_dim(bmap->dim, isl_dim_all);
1993 for (d = total - 1; d >= 0; --d) {
1994 if (isl_int_is_zero(src[1+d]))
1995 continue;
1996 if (elim[d] == -1)
1997 continue;
1998 if (!copied) {
1999 isl_seq_cpy(dst, src, 1 + total);
2000 copied = 1;
2002 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2004 return copied;
2007 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2008 struct isl_basic_set *bset, int *elim)
2010 return reduced_using_equalities(dst, src,
2011 bset_to_bmap(bset), elim);
2014 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
2015 struct isl_basic_set *bset, struct isl_basic_set *context)
2017 int i;
2018 int *elim;
2020 if (!bset || !context)
2021 goto error;
2023 if (context->n_eq == 0) {
2024 isl_basic_set_free(context);
2025 return bset;
2028 bset = isl_basic_set_cow(bset);
2029 if (!bset)
2030 goto error;
2032 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2033 if (!elim)
2034 goto error;
2035 set_compute_elimination_index(context, elim);
2036 for (i = 0; i < bset->n_eq; ++i)
2037 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2038 context, elim);
2039 for (i = 0; i < bset->n_ineq; ++i)
2040 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2041 context, elim);
2042 isl_basic_set_free(context);
2043 free(elim);
2044 bset = isl_basic_set_simplify(bset);
2045 bset = isl_basic_set_finalize(bset);
2046 return bset;
2047 error:
2048 isl_basic_set_free(bset);
2049 isl_basic_set_free(context);
2050 return NULL;
2053 /* For each inequality in "ineq" that is a shifted (more relaxed)
2054 * copy of an inequality in "context", mark the corresponding entry
2055 * in "row" with -1.
2056 * If an inequality only has a non-negative constant term, then
2057 * mark it as well.
2059 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2060 __isl_keep isl_basic_set *context, int *row)
2062 struct isl_constraint_index ci;
2063 int n_ineq;
2064 unsigned total;
2065 int k;
2067 if (!ineq || !context)
2068 return isl_stat_error;
2069 if (context->n_ineq == 0)
2070 return isl_stat_ok;
2071 if (setup_constraint_index(&ci, context) < 0)
2072 return isl_stat_error;
2074 n_ineq = isl_mat_rows(ineq);
2075 total = isl_mat_cols(ineq) - 1;
2076 for (k = 0; k < n_ineq; ++k) {
2077 int l;
2078 isl_bool redundant;
2080 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2081 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2082 row[k] = -1;
2083 continue;
2085 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2086 if (redundant < 0)
2087 goto error;
2088 if (!redundant)
2089 continue;
2090 row[k] = -1;
2092 constraint_index_free(&ci);
2093 return isl_stat_ok;
2094 error:
2095 constraint_index_free(&ci);
2096 return isl_stat_error;
2099 static struct isl_basic_set *remove_shifted_constraints(
2100 struct isl_basic_set *bset, struct isl_basic_set *context)
2102 struct isl_constraint_index ci;
2103 int k;
2105 if (!bset || !context)
2106 return bset;
2108 if (context->n_ineq == 0)
2109 return bset;
2110 if (setup_constraint_index(&ci, context) < 0)
2111 return bset;
2113 for (k = 0; k < bset->n_ineq; ++k) {
2114 isl_bool redundant;
2116 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2117 if (redundant < 0)
2118 goto error;
2119 if (!redundant)
2120 continue;
2121 bset = isl_basic_set_cow(bset);
2122 if (!bset)
2123 goto error;
2124 isl_basic_set_drop_inequality(bset, k);
2125 --k;
2127 constraint_index_free(&ci);
2128 return bset;
2129 error:
2130 constraint_index_free(&ci);
2131 return bset;
2134 /* Remove constraints from "bmap" that are identical to constraints
2135 * in "context" or that are more relaxed (greater constant term).
2137 * We perform the test for shifted copies on the pure constraints
2138 * in remove_shifted_constraints.
2140 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2141 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2143 isl_basic_set *bset, *bset_context;
2145 if (!bmap || !context)
2146 goto error;
2148 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2149 isl_basic_map_free(context);
2150 return bmap;
2153 context = isl_basic_map_align_divs(context, bmap);
2154 bmap = isl_basic_map_align_divs(bmap, context);
2156 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2157 bset_context = isl_basic_map_underlying_set(context);
2158 bset = remove_shifted_constraints(bset, bset_context);
2159 isl_basic_set_free(bset_context);
2161 bmap = isl_basic_map_overlying_set(bset, bmap);
2163 return bmap;
2164 error:
2165 isl_basic_map_free(bmap);
2166 isl_basic_map_free(context);
2167 return NULL;
2170 /* Does the (linear part of a) constraint "c" involve any of the "len"
2171 * "relevant" dimensions?
2173 static int is_related(isl_int *c, int len, int *relevant)
2175 int i;
2177 for (i = 0; i < len; ++i) {
2178 if (!relevant[i])
2179 continue;
2180 if (!isl_int_is_zero(c[i]))
2181 return 1;
2184 return 0;
2187 /* Drop constraints from "bmap" that do not involve any of
2188 * the dimensions marked "relevant".
2190 static __isl_give isl_basic_map *drop_unrelated_constraints(
2191 __isl_take isl_basic_map *bmap, int *relevant)
2193 int i, dim;
2195 dim = isl_basic_map_dim(bmap, isl_dim_all);
2196 for (i = 0; i < dim; ++i)
2197 if (!relevant[i])
2198 break;
2199 if (i >= dim)
2200 return bmap;
2202 for (i = bmap->n_eq - 1; i >= 0; --i)
2203 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2204 bmap = isl_basic_map_cow(bmap);
2205 if (isl_basic_map_drop_equality(bmap, i) < 0)
2206 return isl_basic_map_free(bmap);
2209 for (i = bmap->n_ineq - 1; i >= 0; --i)
2210 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2211 bmap = isl_basic_map_cow(bmap);
2212 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2213 return isl_basic_map_free(bmap);
2216 return bmap;
2219 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2221 * In particular, for any variable involved in the constraint,
2222 * find the actual group id from before and replace the group
2223 * of the corresponding variable by the minimal group of all
2224 * the variables involved in the constraint considered so far
2225 * (if this minimum is smaller) or replace the minimum by this group
2226 * (if the minimum is larger).
2228 * At the end, all the variables in "c" will (indirectly) point
2229 * to the minimal of the groups that they referred to originally.
2231 static void update_groups(int dim, int *group, isl_int *c)
2233 int j;
2234 int min = dim;
2236 for (j = 0; j < dim; ++j) {
2237 if (isl_int_is_zero(c[j]))
2238 continue;
2239 while (group[j] >= 0 && group[group[j]] != group[j])
2240 group[j] = group[group[j]];
2241 if (group[j] == min)
2242 continue;
2243 if (group[j] < min) {
2244 if (min >= 0 && min < dim)
2245 group[min] = group[j];
2246 min = group[j];
2247 } else
2248 group[group[j]] = min;
2252 /* Allocate an array of groups of variables, one for each variable
2253 * in "context", initialized to zero.
2255 static int *alloc_groups(__isl_keep isl_basic_set *context)
2257 isl_ctx *ctx;
2258 int dim;
2260 dim = isl_basic_set_dim(context, isl_dim_set);
2261 ctx = isl_basic_set_get_ctx(context);
2262 return isl_calloc_array(ctx, int, dim);
2265 /* Drop constraints from "bmap" that only involve variables that are
2266 * not related to any of the variables marked with a "-1" in "group".
2268 * We construct groups of variables that collect variables that
2269 * (indirectly) appear in some common constraint of "bmap".
2270 * Each group is identified by the first variable in the group,
2271 * except for the special group of variables that was already identified
2272 * in the input as -1 (or are related to those variables).
2273 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2274 * otherwise the group of i is the group of group[i].
2276 * We first initialize groups for the remaining variables.
2277 * Then we iterate over the constraints of "bmap" and update the
2278 * group of the variables in the constraint by the smallest group.
2279 * Finally, we resolve indirect references to groups by running over
2280 * the variables.
2282 * After computing the groups, we drop constraints that do not involve
2283 * any variables in the -1 group.
2285 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2286 __isl_take isl_basic_map *bmap, __isl_take int *group)
2288 int dim;
2289 int i;
2290 int last;
2292 if (!bmap)
2293 return NULL;
2295 dim = isl_basic_map_dim(bmap, isl_dim_all);
2297 last = -1;
2298 for (i = 0; i < dim; ++i)
2299 if (group[i] >= 0)
2300 last = group[i] = i;
2301 if (last < 0) {
2302 free(group);
2303 return bmap;
2306 for (i = 0; i < bmap->n_eq; ++i)
2307 update_groups(dim, group, bmap->eq[i] + 1);
2308 for (i = 0; i < bmap->n_ineq; ++i)
2309 update_groups(dim, group, bmap->ineq[i] + 1);
2311 for (i = 0; i < dim; ++i)
2312 if (group[i] >= 0)
2313 group[i] = group[group[i]];
2315 for (i = 0; i < dim; ++i)
2316 group[i] = group[i] == -1;
2318 bmap = drop_unrelated_constraints(bmap, group);
2320 free(group);
2321 return bmap;
2324 /* Drop constraints from "context" that are irrelevant for computing
2325 * the gist of "bset".
2327 * In particular, drop constraints in variables that are not related
2328 * to any of the variables involved in the constraints of "bset"
2329 * in the sense that there is no sequence of constraints that connects them.
2331 * We first mark all variables that appear in "bset" as belonging
2332 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2334 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2335 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2337 int *group;
2338 int dim;
2339 int i, j;
2341 if (!context || !bset)
2342 return isl_basic_set_free(context);
2344 group = alloc_groups(context);
2346 if (!group)
2347 return isl_basic_set_free(context);
2349 dim = isl_basic_set_dim(bset, isl_dim_set);
2350 for (i = 0; i < dim; ++i) {
2351 for (j = 0; j < bset->n_eq; ++j)
2352 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2353 break;
2354 if (j < bset->n_eq) {
2355 group[i] = -1;
2356 continue;
2358 for (j = 0; j < bset->n_ineq; ++j)
2359 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2360 break;
2361 if (j < bset->n_ineq)
2362 group[i] = -1;
2365 return isl_basic_map_drop_unrelated_constraints(context, group);
2368 /* Drop constraints from "context" that are irrelevant for computing
2369 * the gist of the inequalities "ineq".
2370 * Inequalities in "ineq" for which the corresponding element of row
2371 * is set to -1 have already been marked for removal and should be ignored.
2373 * In particular, drop constraints in variables that are not related
2374 * to any of the variables involved in "ineq"
2375 * in the sense that there is no sequence of constraints that connects them.
2377 * We first mark all variables that appear in "bset" as belonging
2378 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2380 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2381 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2383 int *group;
2384 int dim;
2385 int i, j, n;
2387 if (!context || !ineq)
2388 return isl_basic_set_free(context);
2390 group = alloc_groups(context);
2392 if (!group)
2393 return isl_basic_set_free(context);
2395 dim = isl_basic_set_dim(context, isl_dim_set);
2396 n = isl_mat_rows(ineq);
2397 for (i = 0; i < dim; ++i) {
2398 for (j = 0; j < n; ++j) {
2399 if (row[j] < 0)
2400 continue;
2401 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2402 break;
2404 if (j < n)
2405 group[i] = -1;
2408 return isl_basic_map_drop_unrelated_constraints(context, group);
2411 /* Do all "n" entries of "row" contain a negative value?
2413 static int all_neg(int *row, int n)
2415 int i;
2417 for (i = 0; i < n; ++i)
2418 if (row[i] >= 0)
2419 return 0;
2421 return 1;
2424 /* Update the inequalities in "bset" based on the information in "row"
2425 * and "tab".
2427 * In particular, the array "row" contains either -1, meaning that
2428 * the corresponding inequality of "bset" is redundant, or the index
2429 * of an inequality in "tab".
2431 * If the row entry is -1, then drop the inequality.
2432 * Otherwise, if the constraint is marked redundant in the tableau,
2433 * then drop the inequality. Similarly, if it is marked as an equality
2434 * in the tableau, then turn the inequality into an equality and
2435 * perform Gaussian elimination.
2437 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2438 __isl_keep int *row, struct isl_tab *tab)
2440 int i;
2441 unsigned n_ineq;
2442 unsigned n_eq;
2443 int found_equality = 0;
2445 if (!bset)
2446 return NULL;
2447 if (tab && tab->empty)
2448 return isl_basic_set_set_to_empty(bset);
2450 n_ineq = bset->n_ineq;
2451 for (i = n_ineq - 1; i >= 0; --i) {
2452 if (row[i] < 0) {
2453 if (isl_basic_set_drop_inequality(bset, i) < 0)
2454 return isl_basic_set_free(bset);
2455 continue;
2457 if (!tab)
2458 continue;
2459 n_eq = tab->n_eq;
2460 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2461 isl_basic_map_inequality_to_equality(bset, i);
2462 found_equality = 1;
2463 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2464 if (isl_basic_set_drop_inequality(bset, i) < 0)
2465 return isl_basic_set_free(bset);
2469 if (found_equality)
2470 bset = isl_basic_set_gauss(bset, NULL);
2471 bset = isl_basic_set_finalize(bset);
2472 return bset;
2475 /* Update the inequalities in "bset" based on the information in "row"
2476 * and "tab" and free all arguments (other than "bset").
2478 static __isl_give isl_basic_set *update_ineq_free(
2479 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2480 __isl_take isl_basic_set *context, __isl_take int *row,
2481 struct isl_tab *tab)
2483 isl_mat_free(ineq);
2484 isl_basic_set_free(context);
2486 bset = update_ineq(bset, row, tab);
2488 free(row);
2489 isl_tab_free(tab);
2490 return bset;
2493 /* Remove all information from bset that is redundant in the context
2494 * of context.
2495 * "ineq" contains the (possibly transformed) inequalities of "bset",
2496 * in the same order.
2497 * The (explicit) equalities of "bset" are assumed to have been taken
2498 * into account by the transformation such that only the inequalities
2499 * are relevant.
2500 * "context" is assumed not to be empty.
2502 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2503 * A value of -1 means that the inequality is obviously redundant and may
2504 * not even appear in "tab".
2506 * We first mark the inequalities of "bset"
2507 * that are obviously redundant with respect to some inequality in "context".
2508 * Then we remove those constraints from "context" that have become
2509 * irrelevant for computing the gist of "bset".
2510 * Note that this removal of constraints cannot be replaced by
2511 * a factorization because factors in "bset" may still be connected
2512 * to each other through constraints in "context".
2514 * If there are any inequalities left, we construct a tableau for
2515 * the context and then add the inequalities of "bset".
2516 * Before adding these inequalities, we freeze all constraints such that
2517 * they won't be considered redundant in terms of the constraints of "bset".
2518 * Then we detect all redundant constraints (among the
2519 * constraints that weren't frozen), first by checking for redundancy in the
2520 * the tableau and then by checking if replacing a constraint by its negation
2521 * would lead to an empty set. This last step is fairly expensive
2522 * and could be optimized by more reuse of the tableau.
2523 * Finally, we update bset according to the results.
2525 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2526 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2528 int i, r;
2529 int *row = NULL;
2530 isl_ctx *ctx;
2531 isl_basic_set *combined = NULL;
2532 struct isl_tab *tab = NULL;
2533 unsigned n_eq, context_ineq;
2535 if (!bset || !ineq || !context)
2536 goto error;
2538 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2539 isl_basic_set_free(context);
2540 isl_mat_free(ineq);
2541 return bset;
2544 ctx = isl_basic_set_get_ctx(context);
2545 row = isl_calloc_array(ctx, int, bset->n_ineq);
2546 if (!row)
2547 goto error;
2549 if (mark_shifted_constraints(ineq, context, row) < 0)
2550 goto error;
2551 if (all_neg(row, bset->n_ineq))
2552 return update_ineq_free(bset, ineq, context, row, NULL);
2554 context = drop_irrelevant_constraints_marked(context, ineq, row);
2555 if (!context)
2556 goto error;
2557 if (isl_basic_set_plain_is_universe(context))
2558 return update_ineq_free(bset, ineq, context, row, NULL);
2560 n_eq = context->n_eq;
2561 context_ineq = context->n_ineq;
2562 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2563 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2564 tab = isl_tab_from_basic_set(combined, 0);
2565 for (i = 0; i < context_ineq; ++i)
2566 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2567 goto error;
2568 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2569 goto error;
2570 r = context_ineq;
2571 for (i = 0; i < bset->n_ineq; ++i) {
2572 if (row[i] < 0)
2573 continue;
2574 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2575 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2576 goto error;
2577 row[i] = r++;
2579 if (isl_tab_detect_implicit_equalities(tab) < 0)
2580 goto error;
2581 if (isl_tab_detect_redundant(tab) < 0)
2582 goto error;
2583 for (i = bset->n_ineq - 1; i >= 0; --i) {
2584 isl_basic_set *test;
2585 int is_empty;
2587 if (row[i] < 0)
2588 continue;
2589 r = row[i];
2590 if (tab->con[n_eq + r].is_redundant)
2591 continue;
2592 test = isl_basic_set_dup(combined);
2593 if (isl_inequality_negate(test, r) < 0)
2594 test = isl_basic_set_free(test);
2595 test = isl_basic_set_update_from_tab(test, tab);
2596 is_empty = isl_basic_set_is_empty(test);
2597 isl_basic_set_free(test);
2598 if (is_empty < 0)
2599 goto error;
2600 if (is_empty)
2601 tab->con[n_eq + r].is_redundant = 1;
2603 bset = update_ineq_free(bset, ineq, context, row, tab);
2604 if (bset) {
2605 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2606 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2609 isl_basic_set_free(combined);
2610 return bset;
2611 error:
2612 free(row);
2613 isl_mat_free(ineq);
2614 isl_tab_free(tab);
2615 isl_basic_set_free(combined);
2616 isl_basic_set_free(context);
2617 isl_basic_set_free(bset);
2618 return NULL;
2621 /* Extract the inequalities of "bset" as an isl_mat.
2623 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2625 unsigned total;
2626 isl_ctx *ctx;
2627 isl_mat *ineq;
2629 if (!bset)
2630 return NULL;
2632 ctx = isl_basic_set_get_ctx(bset);
2633 total = isl_basic_set_total_dim(bset);
2634 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2635 0, 1 + total);
2637 return ineq;
2640 /* Remove all information from "bset" that is redundant in the context
2641 * of "context", for the case where both "bset" and "context" are
2642 * full-dimensional.
2644 static __isl_give isl_basic_set *uset_gist_uncompressed(
2645 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2647 isl_mat *ineq;
2649 ineq = extract_ineq(bset);
2650 return uset_gist_full(bset, ineq, context);
2653 /* Remove all information from "bset" that is redundant in the context
2654 * of "context", for the case where the combined equalities of
2655 * "bset" and "context" allow for a compression that can be obtained
2656 * by preapplication of "T".
2658 * "bset" itself is not transformed by "T". Instead, the inequalities
2659 * are extracted from "bset" and those are transformed by "T".
2660 * uset_gist_full then determines which of the transformed inequalities
2661 * are redundant with respect to the transformed "context" and removes
2662 * the corresponding inequalities from "bset".
2664 * After preapplying "T" to the inequalities, any common factor is
2665 * removed from the coefficients. If this results in a tightening
2666 * of the constant term, then the same tightening is applied to
2667 * the corresponding untransformed inequality in "bset".
2668 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2670 * g f'(x) + r >= 0
2672 * with 0 <= r < g, then it is equivalent to
2674 * f'(x) >= 0
2676 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2677 * subspace compressed by T since the latter would be transformed to
2679 * g f'(x) >= 0
2681 static __isl_give isl_basic_set *uset_gist_compressed(
2682 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2683 __isl_take isl_mat *T)
2685 isl_ctx *ctx;
2686 isl_mat *ineq;
2687 int i, n_row, n_col;
2688 isl_int rem;
2690 ineq = extract_ineq(bset);
2691 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2692 context = isl_basic_set_preimage(context, T);
2694 if (!ineq || !context)
2695 goto error;
2696 if (isl_basic_set_plain_is_empty(context)) {
2697 isl_mat_free(ineq);
2698 isl_basic_set_free(context);
2699 return isl_basic_set_set_to_empty(bset);
2702 ctx = isl_mat_get_ctx(ineq);
2703 n_row = isl_mat_rows(ineq);
2704 n_col = isl_mat_cols(ineq);
2705 isl_int_init(rem);
2706 for (i = 0; i < n_row; ++i) {
2707 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2708 if (isl_int_is_zero(ctx->normalize_gcd))
2709 continue;
2710 if (isl_int_is_one(ctx->normalize_gcd))
2711 continue;
2712 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2713 ctx->normalize_gcd, n_col - 1);
2714 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2715 isl_int_fdiv_q(ineq->row[i][0],
2716 ineq->row[i][0], ctx->normalize_gcd);
2717 if (isl_int_is_zero(rem))
2718 continue;
2719 bset = isl_basic_set_cow(bset);
2720 if (!bset)
2721 break;
2722 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2724 isl_int_clear(rem);
2726 return uset_gist_full(bset, ineq, context);
2727 error:
2728 isl_mat_free(ineq);
2729 isl_basic_set_free(context);
2730 isl_basic_set_free(bset);
2731 return NULL;
2734 /* Project "bset" onto the variables that are involved in "template".
2736 static __isl_give isl_basic_set *project_onto_involved(
2737 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2739 int i, n;
2741 if (!bset || !template)
2742 return isl_basic_set_free(bset);
2744 n = isl_basic_set_dim(template, isl_dim_set);
2746 for (i = 0; i < n; ++i) {
2747 isl_bool involved;
2749 involved = isl_basic_set_involves_dims(template,
2750 isl_dim_set, i, 1);
2751 if (involved < 0)
2752 return isl_basic_set_free(bset);
2753 if (involved)
2754 continue;
2755 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2758 return bset;
2761 /* Remove all information from bset that is redundant in the context
2762 * of context. In particular, equalities that are linear combinations
2763 * of those in context are removed. Then the inequalities that are
2764 * redundant in the context of the equalities and inequalities of
2765 * context are removed.
2767 * First of all, we drop those constraints from "context"
2768 * that are irrelevant for computing the gist of "bset".
2769 * Alternatively, we could factorize the intersection of "context" and "bset".
2771 * We first compute the intersection of the integer affine hulls
2772 * of "bset" and "context",
2773 * compute the gist inside this intersection and then reduce
2774 * the constraints with respect to the equalities of the context
2775 * that only involve variables already involved in the input.
2777 * If two constraints are mutually redundant, then uset_gist_full
2778 * will remove the second of those constraints. We therefore first
2779 * sort the constraints so that constraints not involving existentially
2780 * quantified variables are given precedence over those that do.
2781 * We have to perform this sorting before the variable compression,
2782 * because that may effect the order of the variables.
2784 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2785 __isl_take isl_basic_set *context)
2787 isl_mat *eq;
2788 isl_mat *T;
2789 isl_basic_set *aff;
2790 isl_basic_set *aff_context;
2791 unsigned total;
2793 if (!bset || !context)
2794 goto error;
2796 context = drop_irrelevant_constraints(context, bset);
2798 bset = isl_basic_set_detect_equalities(bset);
2799 aff = isl_basic_set_copy(bset);
2800 aff = isl_basic_set_plain_affine_hull(aff);
2801 context = isl_basic_set_detect_equalities(context);
2802 aff_context = isl_basic_set_copy(context);
2803 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2804 aff = isl_basic_set_intersect(aff, aff_context);
2805 if (!aff)
2806 goto error;
2807 if (isl_basic_set_plain_is_empty(aff)) {
2808 isl_basic_set_free(bset);
2809 isl_basic_set_free(context);
2810 return aff;
2812 bset = isl_basic_set_sort_constraints(bset);
2813 if (aff->n_eq == 0) {
2814 isl_basic_set_free(aff);
2815 return uset_gist_uncompressed(bset, context);
2817 total = isl_basic_set_total_dim(bset);
2818 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2819 eq = isl_mat_cow(eq);
2820 T = isl_mat_variable_compression(eq, NULL);
2821 isl_basic_set_free(aff);
2822 if (T && T->n_col == 0) {
2823 isl_mat_free(T);
2824 isl_basic_set_free(context);
2825 return isl_basic_set_set_to_empty(bset);
2828 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2829 aff_context = project_onto_involved(aff_context, bset);
2831 bset = uset_gist_compressed(bset, context, T);
2832 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2834 if (bset) {
2835 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2836 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2839 return bset;
2840 error:
2841 isl_basic_set_free(bset);
2842 isl_basic_set_free(context);
2843 return NULL;
2846 /* Return the number of equality constraints in "bmap" that involve
2847 * local variables. This function assumes that Gaussian elimination
2848 * has been applied to the equality constraints.
2850 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2852 int i;
2853 int total, n_div;
2855 if (!bmap)
2856 return -1;
2858 if (bmap->n_eq == 0)
2859 return 0;
2861 total = isl_basic_map_dim(bmap, isl_dim_all);
2862 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2863 total -= n_div;
2865 for (i = 0; i < bmap->n_eq; ++i)
2866 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2867 n_div) == -1)
2868 return i;
2870 return bmap->n_eq;
2873 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2874 * The constraints are assumed not to involve any local variables.
2876 static __isl_give isl_basic_map *basic_map_from_equalities(
2877 __isl_take isl_space *space, __isl_take isl_mat *eq)
2879 int i, k;
2880 isl_basic_map *bmap = NULL;
2882 if (!space || !eq)
2883 goto error;
2885 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2886 isl_die(isl_space_get_ctx(space), isl_error_internal,
2887 "unexpected number of columns", goto error);
2889 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2890 0, eq->n_row, 0);
2891 for (i = 0; i < eq->n_row; ++i) {
2892 k = isl_basic_map_alloc_equality(bmap);
2893 if (k < 0)
2894 goto error;
2895 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2898 isl_space_free(space);
2899 isl_mat_free(eq);
2900 return bmap;
2901 error:
2902 isl_space_free(space);
2903 isl_mat_free(eq);
2904 isl_basic_map_free(bmap);
2905 return NULL;
2908 /* Construct and return a variable compression based on the equality
2909 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2910 * "n1" is the number of (initial) equality constraints in "bmap1"
2911 * that do involve local variables.
2912 * "n2" is the number of (initial) equality constraints in "bmap2"
2913 * that do involve local variables.
2914 * "total" is the total number of other variables.
2915 * This function assumes that Gaussian elimination
2916 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2917 * such that the equality constraints not involving local variables
2918 * are those that start at "n1" or "n2".
2920 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2921 * then simply compute the compression based on the equality constraints
2922 * in the other basic map.
2923 * Otherwise, combine the equality constraints from both into a new
2924 * basic map such that Gaussian elimination can be applied to this combination
2925 * and then construct a variable compression from the resulting
2926 * equality constraints.
2928 static __isl_give isl_mat *combined_variable_compression(
2929 __isl_keep isl_basic_map *bmap1, int n1,
2930 __isl_keep isl_basic_map *bmap2, int n2, int total)
2932 isl_ctx *ctx;
2933 isl_mat *E1, *E2, *V;
2934 isl_basic_map *bmap;
2936 ctx = isl_basic_map_get_ctx(bmap1);
2937 if (bmap1->n_eq == n1) {
2938 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2939 n2, bmap2->n_eq - n2, 0, 1 + total);
2940 return isl_mat_variable_compression(E2, NULL);
2942 if (bmap2->n_eq == n2) {
2943 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2944 n1, bmap1->n_eq - n1, 0, 1 + total);
2945 return isl_mat_variable_compression(E1, NULL);
2947 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2948 n1, bmap1->n_eq - n1, 0, 1 + total);
2949 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2950 n2, bmap2->n_eq - n2, 0, 1 + total);
2951 E1 = isl_mat_concat(E1, E2);
2952 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2953 bmap = isl_basic_map_gauss(bmap, NULL);
2954 if (!bmap)
2955 return NULL;
2956 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2957 V = isl_mat_variable_compression(E1, NULL);
2958 isl_basic_map_free(bmap);
2960 return V;
2963 /* Extract the stride constraints from "bmap", compressed
2964 * with respect to both the stride constraints in "context" and
2965 * the remaining equality constraints in both "bmap" and "context".
2966 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2967 * "context_n_eq" is the number of (initial) stride constraints in "context".
2969 * Let x be all variables in "bmap" (and "context") other than the local
2970 * variables. First compute a variable compression
2972 * x = V x'
2974 * based on the non-stride equality constraints in "bmap" and "context".
2975 * Consider the stride constraints of "context",
2977 * A(x) + B(y) = 0
2979 * with y the local variables and plug in the variable compression,
2980 * resulting in
2982 * A(V x') + B(y) = 0
2984 * Use these constraints to compute a parameter compression on x'
2986 * x' = T x''
2988 * Now consider the stride constraints of "bmap"
2990 * C(x) + D(y) = 0
2992 * and plug in x = V*T x''.
2993 * That is, return A = [C*V*T D].
2995 static __isl_give isl_mat *extract_compressed_stride_constraints(
2996 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2997 __isl_keep isl_basic_map *context, int context_n_eq)
2999 int total, n_div;
3000 isl_ctx *ctx;
3001 isl_mat *A, *B, *T, *V;
3003 total = isl_basic_map_dim(context, isl_dim_all);
3004 n_div = isl_basic_map_dim(context, isl_dim_div);
3005 total -= n_div;
3007 ctx = isl_basic_map_get_ctx(bmap);
3009 V = combined_variable_compression(bmap, bmap_n_eq,
3010 context, context_n_eq, total);
3012 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3013 B = isl_mat_sub_alloc6(ctx, context->eq,
3014 0, context_n_eq, 1 + total, n_div);
3015 A = isl_mat_product(A, isl_mat_copy(V));
3016 T = isl_mat_parameter_compression_ext(A, B);
3017 T = isl_mat_product(V, T);
3019 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3020 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3022 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3023 0, bmap_n_eq, 0, 1 + total + n_div);
3024 A = isl_mat_product(A, T);
3026 return A;
3029 /* Remove the prime factors from *g that have an exponent that
3030 * is strictly smaller than the exponent in "c".
3031 * All exponents in *g are known to be smaller than or equal
3032 * to those in "c".
3034 * That is, if *g is equal to
3036 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3038 * and "c" is equal to
3040 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3042 * then update *g to
3044 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3045 * p_n^{e_n * (e_n = f_n)}
3047 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3048 * neither does the gcd of *g and c / *g.
3049 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3050 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3051 * Dividing *g by this gcd therefore strictly reduces the exponent
3052 * of the prime factors that need to be removed, while leaving the
3053 * other prime factors untouched.
3054 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3055 * removes all undesired factors, without removing any others.
3057 static void remove_incomplete_powers(isl_int *g, isl_int c)
3059 isl_int t;
3061 isl_int_init(t);
3062 for (;;) {
3063 isl_int_divexact(t, c, *g);
3064 isl_int_gcd(t, t, *g);
3065 if (isl_int_is_one(t))
3066 break;
3067 isl_int_divexact(*g, *g, t);
3069 isl_int_clear(t);
3072 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3073 * of the same stride constraints in a compressed space that exploits
3074 * all equalities in the context and the other equalities in "bmap".
3076 * If the stride constraints of "bmap" are of the form
3078 * C(x) + D(y) = 0
3080 * then A is of the form
3082 * B(x') + D(y) = 0
3084 * If any of these constraints involves only a single local variable y,
3085 * then the constraint appears as
3087 * f(x) + m y_i = 0
3089 * in "bmap" and as
3091 * h(x') + m y_i = 0
3093 * in "A".
3095 * Let g be the gcd of m and the coefficients of h.
3096 * Then, in particular, g is a divisor of the coefficients of h and
3098 * f(x) = h(x')
3100 * is known to be a multiple of g.
3101 * If some prime factor in m appears with the same exponent in g,
3102 * then it can be removed from m because f(x) is already known
3103 * to be a multiple of g and therefore in particular of this power
3104 * of the prime factors.
3105 * Prime factors that appear with a smaller exponent in g cannot
3106 * be removed from m.
3107 * Let g' be the divisor of g containing all prime factors that
3108 * appear with the same exponent in m and g, then
3110 * f(x) + m y_i = 0
3112 * can be replaced by
3114 * f(x) + m/g' y_i' = 0
3116 * Note that (if g' != 1) this changes the explicit representation
3117 * of y_i to that of y_i', so the integer division at position i
3118 * is marked unknown and later recomputed by a call to
3119 * isl_basic_map_gauss.
3121 static __isl_give isl_basic_map *reduce_stride_constraints(
3122 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3124 int i;
3125 int total, n_div;
3126 int any = 0;
3127 isl_int gcd;
3129 if (!bmap || !A)
3130 return isl_basic_map_free(bmap);
3132 total = isl_basic_map_dim(bmap, isl_dim_all);
3133 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3134 total -= n_div;
3136 isl_int_init(gcd);
3137 for (i = 0; i < n; ++i) {
3138 int div;
3140 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3141 if (div < 0)
3142 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3143 "equality constraints modified unexpectedly",
3144 goto error);
3145 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3146 n_div - div - 1) != -1)
3147 continue;
3148 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3149 goto error;
3150 if (isl_int_is_one(gcd))
3151 continue;
3152 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3153 if (isl_int_is_one(gcd))
3154 continue;
3155 isl_int_divexact(bmap->eq[i][1 + total + div],
3156 bmap->eq[i][1 + total + div], gcd);
3157 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3158 if (!bmap)
3159 goto error;
3160 any = 1;
3162 isl_int_clear(gcd);
3164 if (any)
3165 bmap = isl_basic_map_gauss(bmap, NULL);
3167 return bmap;
3168 error:
3169 isl_int_clear(gcd);
3170 isl_basic_map_free(bmap);
3171 return NULL;
3174 /* Simplify the stride constraints in "bmap" based on
3175 * the remaining equality constraints in "bmap" and all equality
3176 * constraints in "context".
3177 * Only do this if both "bmap" and "context" have stride constraints.
3179 * First extract a copy of the stride constraints in "bmap" in a compressed
3180 * space exploiting all the other equality constraints and then
3181 * use this compressed copy to simplify the original stride constraints.
3183 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3184 __isl_keep isl_basic_map *context)
3186 int bmap_n_eq, context_n_eq;
3187 isl_mat *A;
3189 if (!bmap || !context)
3190 return isl_basic_map_free(bmap);
3192 bmap_n_eq = n_div_eq(bmap);
3193 context_n_eq = n_div_eq(context);
3195 if (bmap_n_eq < 0 || context_n_eq < 0)
3196 return isl_basic_map_free(bmap);
3197 if (bmap_n_eq == 0 || context_n_eq == 0)
3198 return bmap;
3200 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3201 context, context_n_eq);
3202 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3204 isl_mat_free(A);
3206 return bmap;
3209 /* Return a basic map that has the same intersection with "context" as "bmap"
3210 * and that is as "simple" as possible.
3212 * The core computation is performed on the pure constraints.
3213 * When we add back the meaning of the integer divisions, we need
3214 * to (re)introduce the div constraints. If we happen to have
3215 * discovered that some of these integer divisions are equal to
3216 * some affine combination of other variables, then these div
3217 * constraints may end up getting simplified in terms of the equalities,
3218 * resulting in extra inequalities on the other variables that
3219 * may have been removed already or that may not even have been
3220 * part of the input. We try and remove those constraints of
3221 * this form that are most obviously redundant with respect to
3222 * the context. We also remove those div constraints that are
3223 * redundant with respect to the other constraints in the result.
3225 * The stride constraints among the equality constraints in "bmap" are
3226 * also simplified with respecting to the other equality constraints
3227 * in "bmap" and with respect to all equality constraints in "context".
3229 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3230 struct isl_basic_map *context)
3232 isl_basic_set *bset, *eq;
3233 isl_basic_map *eq_bmap;
3234 unsigned total, n_div, extra, n_eq, n_ineq;
3236 if (!bmap || !context)
3237 goto error;
3239 if (isl_basic_map_plain_is_universe(bmap)) {
3240 isl_basic_map_free(context);
3241 return bmap;
3243 if (isl_basic_map_plain_is_empty(context)) {
3244 isl_space *space = isl_basic_map_get_space(bmap);
3245 isl_basic_map_free(bmap);
3246 isl_basic_map_free(context);
3247 return isl_basic_map_universe(space);
3249 if (isl_basic_map_plain_is_empty(bmap)) {
3250 isl_basic_map_free(context);
3251 return bmap;
3254 bmap = isl_basic_map_remove_redundancies(bmap);
3255 context = isl_basic_map_remove_redundancies(context);
3256 if (!context)
3257 goto error;
3259 context = isl_basic_map_align_divs(context, bmap);
3260 n_div = isl_basic_map_dim(context, isl_dim_div);
3261 total = isl_basic_map_dim(bmap, isl_dim_all);
3262 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3264 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3265 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3266 bset = uset_gist(bset,
3267 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3268 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3270 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3271 isl_basic_set_plain_is_empty(bset)) {
3272 isl_basic_map_free(context);
3273 return isl_basic_map_overlying_set(bset, bmap);
3276 n_eq = bset->n_eq;
3277 n_ineq = bset->n_ineq;
3278 eq = isl_basic_set_copy(bset);
3279 eq = isl_basic_set_cow(eq);
3280 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3281 eq = isl_basic_set_free(eq);
3282 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3283 bset = isl_basic_set_free(bset);
3285 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3286 eq_bmap = gist_strides(eq_bmap, context);
3287 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3288 bmap = isl_basic_map_overlying_set(bset, bmap);
3289 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3290 bmap = isl_basic_map_remove_redundancies(bmap);
3292 return bmap;
3293 error:
3294 isl_basic_map_free(bmap);
3295 isl_basic_map_free(context);
3296 return NULL;
3300 * Assumes context has no implicit divs.
3302 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3303 __isl_take isl_basic_map *context)
3305 int i;
3307 if (!map || !context)
3308 goto error;
3310 if (isl_basic_map_plain_is_empty(context)) {
3311 isl_space *space = isl_map_get_space(map);
3312 isl_map_free(map);
3313 isl_basic_map_free(context);
3314 return isl_map_universe(space);
3317 context = isl_basic_map_remove_redundancies(context);
3318 map = isl_map_cow(map);
3319 if (!map || !context)
3320 goto error;
3321 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3322 map = isl_map_compute_divs(map);
3323 if (!map)
3324 goto error;
3325 for (i = map->n - 1; i >= 0; --i) {
3326 map->p[i] = isl_basic_map_gist(map->p[i],
3327 isl_basic_map_copy(context));
3328 if (!map->p[i])
3329 goto error;
3330 if (isl_basic_map_plain_is_empty(map->p[i])) {
3331 isl_basic_map_free(map->p[i]);
3332 if (i != map->n - 1)
3333 map->p[i] = map->p[map->n - 1];
3334 map->n--;
3337 isl_basic_map_free(context);
3338 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3339 return map;
3340 error:
3341 isl_map_free(map);
3342 isl_basic_map_free(context);
3343 return NULL;
3346 /* Drop all inequalities from "bmap" that also appear in "context".
3347 * "context" is assumed to have only known local variables and
3348 * the initial local variables of "bmap" are assumed to be the same
3349 * as those of "context".
3350 * The constraints of both "bmap" and "context" are assumed
3351 * to have been sorted using isl_basic_map_sort_constraints.
3353 * Run through the inequality constraints of "bmap" and "context"
3354 * in sorted order.
3355 * If a constraint of "bmap" involves variables not in "context",
3356 * then it cannot appear in "context".
3357 * If a matching constraint is found, it is removed from "bmap".
3359 static __isl_give isl_basic_map *drop_inequalities(
3360 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3362 int i1, i2;
3363 unsigned total, extra;
3365 if (!bmap || !context)
3366 return isl_basic_map_free(bmap);
3368 total = isl_basic_map_total_dim(context);
3369 extra = isl_basic_map_total_dim(bmap) - total;
3371 i1 = bmap->n_ineq - 1;
3372 i2 = context->n_ineq - 1;
3373 while (bmap && i1 >= 0 && i2 >= 0) {
3374 int cmp;
3376 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3377 extra) != -1) {
3378 --i1;
3379 continue;
3381 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3382 context->ineq[i2]);
3383 if (cmp < 0) {
3384 --i2;
3385 continue;
3387 if (cmp > 0) {
3388 --i1;
3389 continue;
3391 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3392 bmap = isl_basic_map_cow(bmap);
3393 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3394 bmap = isl_basic_map_free(bmap);
3396 --i1;
3397 --i2;
3400 return bmap;
3403 /* Drop all equalities from "bmap" that also appear in "context".
3404 * "context" is assumed to have only known local variables and
3405 * the initial local variables of "bmap" are assumed to be the same
3406 * as those of "context".
3408 * Run through the equality constraints of "bmap" and "context"
3409 * in sorted order.
3410 * If a constraint of "bmap" involves variables not in "context",
3411 * then it cannot appear in "context".
3412 * If a matching constraint is found, it is removed from "bmap".
3414 static __isl_give isl_basic_map *drop_equalities(
3415 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3417 int i1, i2;
3418 unsigned total, extra;
3420 if (!bmap || !context)
3421 return isl_basic_map_free(bmap);
3423 total = isl_basic_map_total_dim(context);
3424 extra = isl_basic_map_total_dim(bmap) - total;
3426 i1 = bmap->n_eq - 1;
3427 i2 = context->n_eq - 1;
3429 while (bmap && i1 >= 0 && i2 >= 0) {
3430 int last1, last2;
3432 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3433 extra) != -1)
3434 break;
3435 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3436 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3437 if (last1 > last2) {
3438 --i2;
3439 continue;
3441 if (last1 < last2) {
3442 --i1;
3443 continue;
3445 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3446 bmap = isl_basic_map_cow(bmap);
3447 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3448 bmap = isl_basic_map_free(bmap);
3450 --i1;
3451 --i2;
3454 return bmap;
3457 /* Remove the constraints in "context" from "bmap".
3458 * "context" is assumed to have explicit representations
3459 * for all local variables.
3461 * First align the divs of "bmap" to those of "context" and
3462 * sort the constraints. Then drop all constraints from "bmap"
3463 * that appear in "context".
3465 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3466 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3468 isl_bool done, known;
3470 done = isl_basic_map_plain_is_universe(context);
3471 if (done == isl_bool_false)
3472 done = isl_basic_map_plain_is_universe(bmap);
3473 if (done == isl_bool_false)
3474 done = isl_basic_map_plain_is_empty(context);
3475 if (done == isl_bool_false)
3476 done = isl_basic_map_plain_is_empty(bmap);
3477 if (done < 0)
3478 goto error;
3479 if (done) {
3480 isl_basic_map_free(context);
3481 return bmap;
3483 known = isl_basic_map_divs_known(context);
3484 if (known < 0)
3485 goto error;
3486 if (!known)
3487 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3488 "context has unknown divs", goto error);
3490 bmap = isl_basic_map_align_divs(bmap, context);
3491 bmap = isl_basic_map_gauss(bmap, NULL);
3492 bmap = isl_basic_map_sort_constraints(bmap);
3493 context = isl_basic_map_sort_constraints(context);
3495 bmap = drop_inequalities(bmap, context);
3496 bmap = drop_equalities(bmap, context);
3498 isl_basic_map_free(context);
3499 bmap = isl_basic_map_finalize(bmap);
3500 return bmap;
3501 error:
3502 isl_basic_map_free(bmap);
3503 isl_basic_map_free(context);
3504 return NULL;
3507 /* Replace "map" by the disjunct at position "pos" and free "context".
3509 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3510 int pos, __isl_take isl_basic_map *context)
3512 isl_basic_map *bmap;
3514 bmap = isl_basic_map_copy(map->p[pos]);
3515 isl_map_free(map);
3516 isl_basic_map_free(context);
3517 return isl_map_from_basic_map(bmap);
3520 /* Remove the constraints in "context" from "map".
3521 * If any of the disjuncts in the result turns out to be the universe,
3522 * then return this universe.
3523 * "context" is assumed to have explicit representations
3524 * for all local variables.
3526 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3527 __isl_take isl_basic_map *context)
3529 int i;
3530 isl_bool univ, known;
3532 univ = isl_basic_map_plain_is_universe(context);
3533 if (univ < 0)
3534 goto error;
3535 if (univ) {
3536 isl_basic_map_free(context);
3537 return map;
3539 known = isl_basic_map_divs_known(context);
3540 if (known < 0)
3541 goto error;
3542 if (!known)
3543 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3544 "context has unknown divs", goto error);
3546 map = isl_map_cow(map);
3547 if (!map)
3548 goto error;
3549 for (i = 0; i < map->n; ++i) {
3550 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3551 isl_basic_map_copy(context));
3552 univ = isl_basic_map_plain_is_universe(map->p[i]);
3553 if (univ < 0)
3554 goto error;
3555 if (univ && map->n > 1)
3556 return replace_by_disjunct(map, i, context);
3559 isl_basic_map_free(context);
3560 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3561 if (map->n > 1)
3562 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3563 return map;
3564 error:
3565 isl_map_free(map);
3566 isl_basic_map_free(context);
3567 return NULL;
3570 /* Replace "map" by a universe map in the same space and free "drop".
3572 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3573 __isl_take isl_map *drop)
3575 isl_map *res;
3577 res = isl_map_universe(isl_map_get_space(map));
3578 isl_map_free(map);
3579 isl_map_free(drop);
3580 return res;
3583 /* Return a map that has the same intersection with "context" as "map"
3584 * and that is as "simple" as possible.
3586 * If "map" is already the universe, then we cannot make it any simpler.
3587 * Similarly, if "context" is the universe, then we cannot exploit it
3588 * to simplify "map"
3589 * If "map" and "context" are identical to each other, then we can
3590 * return the corresponding universe.
3592 * If either "map" or "context" consists of multiple disjuncts,
3593 * then check if "context" happens to be a subset of "map",
3594 * in which case all constraints can be removed.
3595 * In case of multiple disjuncts, the standard procedure
3596 * may not be able to detect that all constraints can be removed.
3598 * If none of these cases apply, we have to work a bit harder.
3599 * During this computation, we make use of a single disjunct context,
3600 * so if the original context consists of more than one disjunct
3601 * then we need to approximate the context by a single disjunct set.
3602 * Simply taking the simple hull may drop constraints that are
3603 * only implicitly available in each disjunct. We therefore also
3604 * look for constraints among those defining "map" that are valid
3605 * for the context. These can then be used to simplify away
3606 * the corresponding constraints in "map".
3608 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3609 __isl_take isl_map *context)
3611 int equal;
3612 int is_universe;
3613 int single_disjunct_map, single_disjunct_context;
3614 isl_bool subset;
3615 isl_basic_map *hull;
3617 is_universe = isl_map_plain_is_universe(map);
3618 if (is_universe >= 0 && !is_universe)
3619 is_universe = isl_map_plain_is_universe(context);
3620 if (is_universe < 0)
3621 goto error;
3622 if (is_universe) {
3623 isl_map_free(context);
3624 return map;
3627 equal = isl_map_plain_is_equal(map, context);
3628 if (equal < 0)
3629 goto error;
3630 if (equal)
3631 return replace_by_universe(map, context);
3633 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3634 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3635 if (!single_disjunct_map || !single_disjunct_context) {
3636 subset = isl_map_is_subset(context, map);
3637 if (subset < 0)
3638 goto error;
3639 if (subset)
3640 return replace_by_universe(map, context);
3643 context = isl_map_compute_divs(context);
3644 if (!context)
3645 goto error;
3646 if (single_disjunct_context) {
3647 hull = isl_map_simple_hull(context);
3648 } else {
3649 isl_ctx *ctx;
3650 isl_map_list *list;
3652 ctx = isl_map_get_ctx(map);
3653 list = isl_map_list_alloc(ctx, 2);
3654 list = isl_map_list_add(list, isl_map_copy(context));
3655 list = isl_map_list_add(list, isl_map_copy(map));
3656 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3657 list);
3659 return isl_map_gist_basic_map(map, hull);
3660 error:
3661 isl_map_free(map);
3662 isl_map_free(context);
3663 return NULL;
3666 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3667 __isl_take isl_map *context)
3669 return isl_map_align_params_map_map_and(map, context, &map_gist);
3672 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3673 struct isl_basic_set *context)
3675 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3676 bset_to_bmap(context)));
3679 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3680 __isl_take isl_basic_set *context)
3682 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3683 bset_to_bmap(context)));
3686 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3687 __isl_take isl_basic_set *context)
3689 isl_space *space = isl_set_get_space(set);
3690 isl_basic_set *dom_context = isl_basic_set_universe(space);
3691 dom_context = isl_basic_set_intersect_params(dom_context, context);
3692 return isl_set_gist_basic_set(set, dom_context);
3695 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3696 __isl_take isl_set *context)
3698 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3701 /* Compute the gist of "bmap" with respect to the constraints "context"
3702 * on the domain.
3704 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3705 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3707 isl_space *space = isl_basic_map_get_space(bmap);
3708 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3710 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3711 return isl_basic_map_gist(bmap, bmap_context);
3714 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3715 __isl_take isl_set *context)
3717 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3718 map_context = isl_map_intersect_domain(map_context, context);
3719 return isl_map_gist(map, map_context);
3722 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3723 __isl_take isl_set *context)
3725 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3726 map_context = isl_map_intersect_range(map_context, context);
3727 return isl_map_gist(map, map_context);
3730 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3731 __isl_take isl_set *context)
3733 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3734 map_context = isl_map_intersect_params(map_context, context);
3735 return isl_map_gist(map, map_context);
3738 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3739 __isl_take isl_set *context)
3741 return isl_map_gist_params(set, context);
3744 /* Quick check to see if two basic maps are disjoint.
3745 * In particular, we reduce the equalities and inequalities of
3746 * one basic map in the context of the equalities of the other
3747 * basic map and check if we get a contradiction.
3749 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3750 __isl_keep isl_basic_map *bmap2)
3752 struct isl_vec *v = NULL;
3753 int *elim = NULL;
3754 unsigned total;
3755 int i;
3757 if (!bmap1 || !bmap2)
3758 return isl_bool_error;
3759 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3760 return isl_bool_error);
3761 if (bmap1->n_div || bmap2->n_div)
3762 return isl_bool_false;
3763 if (!bmap1->n_eq && !bmap2->n_eq)
3764 return isl_bool_false;
3766 total = isl_space_dim(bmap1->dim, isl_dim_all);
3767 if (total == 0)
3768 return isl_bool_false;
3769 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3770 if (!v)
3771 goto error;
3772 elim = isl_alloc_array(bmap1->ctx, int, total);
3773 if (!elim)
3774 goto error;
3775 compute_elimination_index(bmap1, elim);
3776 for (i = 0; i < bmap2->n_eq; ++i) {
3777 int reduced;
3778 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3779 bmap1, elim);
3780 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3781 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3782 goto disjoint;
3784 for (i = 0; i < bmap2->n_ineq; ++i) {
3785 int reduced;
3786 reduced = reduced_using_equalities(v->block.data,
3787 bmap2->ineq[i], bmap1, elim);
3788 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3789 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3790 goto disjoint;
3792 compute_elimination_index(bmap2, elim);
3793 for (i = 0; i < bmap1->n_ineq; ++i) {
3794 int reduced;
3795 reduced = reduced_using_equalities(v->block.data,
3796 bmap1->ineq[i], bmap2, elim);
3797 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3798 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3799 goto disjoint;
3801 isl_vec_free(v);
3802 free(elim);
3803 return isl_bool_false;
3804 disjoint:
3805 isl_vec_free(v);
3806 free(elim);
3807 return isl_bool_true;
3808 error:
3809 isl_vec_free(v);
3810 free(elim);
3811 return isl_bool_error;
3814 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3815 __isl_keep isl_basic_set *bset2)
3817 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3818 bset_to_bmap(bset2));
3821 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3823 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3824 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3825 __isl_keep isl_basic_map *bmap2))
3827 int i, j;
3829 if (!map1 || !map2)
3830 return isl_bool_error;
3832 for (i = 0; i < map1->n; ++i) {
3833 for (j = 0; j < map2->n; ++j) {
3834 isl_bool d = test(map1->p[i], map2->p[j]);
3835 if (d != isl_bool_true)
3836 return d;
3840 return isl_bool_true;
3843 /* Are "map1" and "map2" obviously disjoint, based on information
3844 * that can be derived without looking at the individual basic maps?
3846 * In particular, if one of them is empty or if they live in different spaces
3847 * (ignoring parameters), then they are clearly disjoint.
3849 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3850 __isl_keep isl_map *map2)
3852 isl_bool disjoint;
3853 isl_bool match;
3855 if (!map1 || !map2)
3856 return isl_bool_error;
3858 disjoint = isl_map_plain_is_empty(map1);
3859 if (disjoint < 0 || disjoint)
3860 return disjoint;
3862 disjoint = isl_map_plain_is_empty(map2);
3863 if (disjoint < 0 || disjoint)
3864 return disjoint;
3866 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3867 map2->dim, isl_dim_in);
3868 if (match < 0 || !match)
3869 return match < 0 ? isl_bool_error : isl_bool_true;
3871 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3872 map2->dim, isl_dim_out);
3873 if (match < 0 || !match)
3874 return match < 0 ? isl_bool_error : isl_bool_true;
3876 return isl_bool_false;
3879 /* Are "map1" and "map2" obviously disjoint?
3881 * If one of them is empty or if they live in different spaces (ignoring
3882 * parameters), then they are clearly disjoint.
3883 * This is checked by isl_map_plain_is_disjoint_global.
3885 * If they have different parameters, then we skip any further tests.
3887 * If they are obviously equal, but not obviously empty, then we will
3888 * not be able to detect if they are disjoint.
3890 * Otherwise we check if each basic map in "map1" is obviously disjoint
3891 * from each basic map in "map2".
3893 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3894 __isl_keep isl_map *map2)
3896 isl_bool disjoint;
3897 isl_bool intersect;
3898 isl_bool match;
3900 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3901 if (disjoint < 0 || disjoint)
3902 return disjoint;
3904 match = isl_map_has_equal_params(map1, map2);
3905 if (match < 0 || !match)
3906 return match < 0 ? isl_bool_error : isl_bool_false;
3908 intersect = isl_map_plain_is_equal(map1, map2);
3909 if (intersect < 0 || intersect)
3910 return intersect < 0 ? isl_bool_error : isl_bool_false;
3912 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3915 /* Are "map1" and "map2" disjoint?
3917 * They are disjoint if they are "obviously disjoint" or if one of them
3918 * is empty. Otherwise, they are not disjoint if one of them is universal.
3919 * If the two inputs are (obviously) equal and not empty, then they are
3920 * not disjoint.
3921 * If none of these cases apply, then check if all pairs of basic maps
3922 * are disjoint.
3924 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3926 isl_bool disjoint;
3927 isl_bool intersect;
3929 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3930 if (disjoint < 0 || disjoint)
3931 return disjoint;
3933 disjoint = isl_map_is_empty(map1);
3934 if (disjoint < 0 || disjoint)
3935 return disjoint;
3937 disjoint = isl_map_is_empty(map2);
3938 if (disjoint < 0 || disjoint)
3939 return disjoint;
3941 intersect = isl_map_plain_is_universe(map1);
3942 if (intersect < 0 || intersect)
3943 return intersect < 0 ? isl_bool_error : isl_bool_false;
3945 intersect = isl_map_plain_is_universe(map2);
3946 if (intersect < 0 || intersect)
3947 return intersect < 0 ? isl_bool_error : isl_bool_false;
3949 intersect = isl_map_plain_is_equal(map1, map2);
3950 if (intersect < 0 || intersect)
3951 return isl_bool_not(intersect);
3953 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3956 /* Are "bmap1" and "bmap2" disjoint?
3958 * They are disjoint if they are "obviously disjoint" or if one of them
3959 * is empty. Otherwise, they are not disjoint if one of them is universal.
3960 * If none of these cases apply, we compute the intersection and see if
3961 * the result is empty.
3963 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3964 __isl_keep isl_basic_map *bmap2)
3966 isl_bool disjoint;
3967 isl_bool intersect;
3968 isl_basic_map *test;
3970 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3971 if (disjoint < 0 || disjoint)
3972 return disjoint;
3974 disjoint = isl_basic_map_is_empty(bmap1);
3975 if (disjoint < 0 || disjoint)
3976 return disjoint;
3978 disjoint = isl_basic_map_is_empty(bmap2);
3979 if (disjoint < 0 || disjoint)
3980 return disjoint;
3982 intersect = isl_basic_map_plain_is_universe(bmap1);
3983 if (intersect < 0 || intersect)
3984 return intersect < 0 ? isl_bool_error : isl_bool_false;
3986 intersect = isl_basic_map_plain_is_universe(bmap2);
3987 if (intersect < 0 || intersect)
3988 return intersect < 0 ? isl_bool_error : isl_bool_false;
3990 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3991 isl_basic_map_copy(bmap2));
3992 disjoint = isl_basic_map_is_empty(test);
3993 isl_basic_map_free(test);
3995 return disjoint;
3998 /* Are "bset1" and "bset2" disjoint?
4000 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4001 __isl_keep isl_basic_set *bset2)
4003 return isl_basic_map_is_disjoint(bset1, bset2);
4006 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4007 __isl_keep isl_set *set2)
4009 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4012 /* Are "set1" and "set2" disjoint?
4014 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4016 return isl_map_is_disjoint(set1, set2);
4019 /* Is "v" equal to 0, 1 or -1?
4021 static int is_zero_or_one(isl_int v)
4023 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4026 /* Check if we can combine a given div with lower bound l and upper
4027 * bound u with some other div and if so return that other div.
4028 * Otherwise return -1.
4030 * We first check that
4031 * - the bounds are opposites of each other (except for the constant
4032 * term)
4033 * - the bounds do not reference any other div
4034 * - no div is defined in terms of this div
4036 * Let m be the size of the range allowed on the div by the bounds.
4037 * That is, the bounds are of the form
4039 * e <= a <= e + m - 1
4041 * with e some expression in the other variables.
4042 * We look for another div b such that no third div is defined in terms
4043 * of this second div b and such that in any constraint that contains
4044 * a (except for the given lower and upper bound), also contains b
4045 * with a coefficient that is m times that of b.
4046 * That is, all constraints (execpt for the lower and upper bound)
4047 * are of the form
4049 * e + f (a + m b) >= 0
4051 * Furthermore, in the constraints that only contain b, the coefficient
4052 * of b should be equal to 1 or -1.
4053 * If so, we return b so that "a + m b" can be replaced by
4054 * a single div "c = a + m b".
4056 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4057 unsigned div, unsigned l, unsigned u)
4059 int i, j;
4060 unsigned dim;
4061 int coalesce = -1;
4063 if (bmap->n_div <= 1)
4064 return -1;
4065 dim = isl_space_dim(bmap->dim, isl_dim_all);
4066 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4067 return -1;
4068 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4069 bmap->n_div - div - 1) != -1)
4070 return -1;
4071 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4072 dim + bmap->n_div))
4073 return -1;
4075 for (i = 0; i < bmap->n_div; ++i) {
4076 if (isl_int_is_zero(bmap->div[i][0]))
4077 continue;
4078 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4079 return -1;
4082 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4083 if (isl_int_is_neg(bmap->ineq[l][0])) {
4084 isl_int_sub(bmap->ineq[l][0],
4085 bmap->ineq[l][0], bmap->ineq[u][0]);
4086 bmap = isl_basic_map_copy(bmap);
4087 bmap = isl_basic_map_set_to_empty(bmap);
4088 isl_basic_map_free(bmap);
4089 return -1;
4091 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4092 for (i = 0; i < bmap->n_div; ++i) {
4093 if (i == div)
4094 continue;
4095 if (!pairs[i])
4096 continue;
4097 for (j = 0; j < bmap->n_div; ++j) {
4098 if (isl_int_is_zero(bmap->div[j][0]))
4099 continue;
4100 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4101 break;
4103 if (j < bmap->n_div)
4104 continue;
4105 for (j = 0; j < bmap->n_ineq; ++j) {
4106 int valid;
4107 if (j == l || j == u)
4108 continue;
4109 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4110 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4111 continue;
4112 break;
4114 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4115 break;
4116 isl_int_mul(bmap->ineq[j][1 + dim + div],
4117 bmap->ineq[j][1 + dim + div],
4118 bmap->ineq[l][0]);
4119 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4120 bmap->ineq[j][1 + dim + i]);
4121 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4122 bmap->ineq[j][1 + dim + div],
4123 bmap->ineq[l][0]);
4124 if (!valid)
4125 break;
4127 if (j < bmap->n_ineq)
4128 continue;
4129 coalesce = i;
4130 break;
4132 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4133 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4134 return coalesce;
4137 /* Internal data structure used during the construction and/or evaluation of
4138 * an inequality that ensures that a pair of bounds always allows
4139 * for an integer value.
4141 * "tab" is the tableau in which the inequality is evaluated. It may
4142 * be NULL until it is actually needed.
4143 * "v" contains the inequality coefficients.
4144 * "g", "fl" and "fu" are temporary scalars used during the construction and
4145 * evaluation.
4147 struct test_ineq_data {
4148 struct isl_tab *tab;
4149 isl_vec *v;
4150 isl_int g;
4151 isl_int fl;
4152 isl_int fu;
4155 /* Free all the memory allocated by the fields of "data".
4157 static void test_ineq_data_clear(struct test_ineq_data *data)
4159 isl_tab_free(data->tab);
4160 isl_vec_free(data->v);
4161 isl_int_clear(data->g);
4162 isl_int_clear(data->fl);
4163 isl_int_clear(data->fu);
4166 /* Is the inequality stored in data->v satisfied by "bmap"?
4167 * That is, does it only attain non-negative values?
4168 * data->tab is a tableau corresponding to "bmap".
4170 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4171 struct test_ineq_data *data)
4173 isl_ctx *ctx;
4174 enum isl_lp_result res;
4176 ctx = isl_basic_map_get_ctx(bmap);
4177 if (!data->tab)
4178 data->tab = isl_tab_from_basic_map(bmap, 0);
4179 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4180 if (res == isl_lp_error)
4181 return isl_bool_error;
4182 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4185 /* Given a lower and an upper bound on div i, do they always allow
4186 * for an integer value of the given div?
4187 * Determine this property by constructing an inequality
4188 * such that the property is guaranteed when the inequality is nonnegative.
4189 * The lower bound is inequality l, while the upper bound is inequality u.
4190 * The constructed inequality is stored in data->v.
4192 * Let the upper bound be
4194 * -n_u a + e_u >= 0
4196 * and the lower bound
4198 * n_l a + e_l >= 0
4200 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4201 * We have
4203 * - f_u e_l <= f_u f_l g a <= f_l e_u
4205 * Since all variables are integer valued, this is equivalent to
4207 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4209 * If this interval is at least f_u f_l g, then it contains at least
4210 * one integer value for a.
4211 * That is, the test constraint is
4213 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4215 * or
4217 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4219 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4220 * then the constraint can be scaled down by a factor g',
4221 * with the constant term replaced by
4222 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4223 * Note that the result of applying Fourier-Motzkin to this pair
4224 * of constraints is
4226 * f_l e_u + f_u e_l >= 0
4228 * If the constant term of the scaled down version of this constraint,
4229 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4230 * term of the scaled down test constraint, then the test constraint
4231 * is known to hold and no explicit evaluation is required.
4232 * This is essentially the Omega test.
4234 * If the test constraint consists of only a constant term, then
4235 * it is sufficient to look at the sign of this constant term.
4237 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4238 int l, int u, struct test_ineq_data *data)
4240 unsigned offset, n_div;
4241 offset = isl_basic_map_offset(bmap, isl_dim_div);
4242 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4244 isl_int_gcd(data->g,
4245 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4246 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4247 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4248 isl_int_neg(data->fu, data->fu);
4249 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4250 data->fu, bmap->ineq[l], offset + n_div);
4251 isl_int_mul(data->g, data->g, data->fl);
4252 isl_int_mul(data->g, data->g, data->fu);
4253 isl_int_sub(data->g, data->g, data->fl);
4254 isl_int_sub(data->g, data->g, data->fu);
4255 isl_int_add_ui(data->g, data->g, 1);
4256 isl_int_sub(data->fl, data->v->el[0], data->g);
4258 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4259 if (isl_int_is_zero(data->g))
4260 return isl_int_is_nonneg(data->fl);
4261 if (isl_int_is_one(data->g)) {
4262 isl_int_set(data->v->el[0], data->fl);
4263 return test_ineq_is_satisfied(bmap, data);
4265 isl_int_fdiv_q(data->fl, data->fl, data->g);
4266 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4267 if (isl_int_eq(data->fl, data->v->el[0]))
4268 return isl_bool_true;
4269 isl_int_set(data->v->el[0], data->fl);
4270 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4271 offset - 1 + n_div);
4273 return test_ineq_is_satisfied(bmap, data);
4276 /* Remove more kinds of divs that are not strictly needed.
4277 * In particular, if all pairs of lower and upper bounds on a div
4278 * are such that they allow at least one integer value of the div,
4279 * then we can eliminate the div using Fourier-Motzkin without
4280 * introducing any spurious solutions.
4282 * If at least one of the two constraints has a unit coefficient for the div,
4283 * then the presence of such a value is guaranteed so there is no need to check.
4284 * In particular, the value attained by the bound with unit coefficient
4285 * can serve as this intermediate value.
4287 static struct isl_basic_map *drop_more_redundant_divs(
4288 struct isl_basic_map *bmap, int *pairs, int n)
4290 isl_ctx *ctx;
4291 struct test_ineq_data data = { NULL, NULL };
4292 unsigned off, n_div;
4293 int remove = -1;
4295 isl_int_init(data.g);
4296 isl_int_init(data.fl);
4297 isl_int_init(data.fu);
4299 if (!bmap)
4300 goto error;
4302 ctx = isl_basic_map_get_ctx(bmap);
4303 off = isl_basic_map_offset(bmap, isl_dim_div);
4304 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4305 data.v = isl_vec_alloc(ctx, off + n_div);
4306 if (!data.v)
4307 goto error;
4309 while (n > 0) {
4310 int i, l, u;
4311 int best = -1;
4312 isl_bool has_int;
4314 for (i = 0; i < n_div; ++i) {
4315 if (!pairs[i])
4316 continue;
4317 if (best >= 0 && pairs[best] <= pairs[i])
4318 continue;
4319 best = i;
4322 i = best;
4323 for (l = 0; l < bmap->n_ineq; ++l) {
4324 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4325 continue;
4326 if (isl_int_is_one(bmap->ineq[l][off + i]))
4327 continue;
4328 for (u = 0; u < bmap->n_ineq; ++u) {
4329 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4330 continue;
4331 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4332 continue;
4333 has_int = int_between_bounds(bmap, i, l, u,
4334 &data);
4335 if (has_int < 0)
4336 goto error;
4337 if (data.tab && data.tab->empty)
4338 break;
4339 if (!has_int)
4340 break;
4342 if (u < bmap->n_ineq)
4343 break;
4345 if (data.tab && data.tab->empty) {
4346 bmap = isl_basic_map_set_to_empty(bmap);
4347 break;
4349 if (l == bmap->n_ineq) {
4350 remove = i;
4351 break;
4353 pairs[i] = 0;
4354 --n;
4357 test_ineq_data_clear(&data);
4359 free(pairs);
4361 if (remove < 0)
4362 return bmap;
4364 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4365 return isl_basic_map_drop_redundant_divs(bmap);
4366 error:
4367 free(pairs);
4368 isl_basic_map_free(bmap);
4369 test_ineq_data_clear(&data);
4370 return NULL;
4373 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4374 * and the upper bound u, div1 always occurs together with div2 in the form
4375 * (div1 + m div2), where m is the constant range on the variable div1
4376 * allowed by l and u, replace the pair div1 and div2 by a single
4377 * div that is equal to div1 + m div2.
4379 * The new div will appear in the location that contains div2.
4380 * We need to modify all constraints that contain
4381 * div2 = (div - div1) / m
4382 * The coefficient of div2 is known to be equal to 1 or -1.
4383 * (If a constraint does not contain div2, it will also not contain div1.)
4384 * If the constraint also contains div1, then we know they appear
4385 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4386 * i.e., the coefficient of div is f.
4388 * Otherwise, we first need to introduce div1 into the constraint.
4389 * Let the l be
4391 * div1 + f >=0
4393 * and u
4395 * -div1 + f' >= 0
4397 * A lower bound on div2
4399 * div2 + t >= 0
4401 * can be replaced by
4403 * m div2 + div1 + m t + f >= 0
4405 * An upper bound
4407 * -div2 + t >= 0
4409 * can be replaced by
4411 * -(m div2 + div1) + m t + f' >= 0
4413 * These constraint are those that we would obtain from eliminating
4414 * div1 using Fourier-Motzkin.
4416 * After all constraints have been modified, we drop the lower and upper
4417 * bound and then drop div1.
4419 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4420 unsigned div1, unsigned div2, unsigned l, unsigned u)
4422 isl_ctx *ctx;
4423 isl_int m;
4424 unsigned dim, total;
4425 int i;
4427 ctx = isl_basic_map_get_ctx(bmap);
4429 dim = isl_space_dim(bmap->dim, isl_dim_all);
4430 total = 1 + dim + bmap->n_div;
4432 isl_int_init(m);
4433 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4434 isl_int_add_ui(m, m, 1);
4436 for (i = 0; i < bmap->n_ineq; ++i) {
4437 if (i == l || i == u)
4438 continue;
4439 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4440 continue;
4441 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4442 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4443 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4444 ctx->one, bmap->ineq[l], total);
4445 else
4446 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4447 ctx->one, bmap->ineq[u], total);
4449 isl_int_set(bmap->ineq[i][1 + dim + div2],
4450 bmap->ineq[i][1 + dim + div1]);
4451 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4454 isl_int_clear(m);
4455 if (l > u) {
4456 isl_basic_map_drop_inequality(bmap, l);
4457 isl_basic_map_drop_inequality(bmap, u);
4458 } else {
4459 isl_basic_map_drop_inequality(bmap, u);
4460 isl_basic_map_drop_inequality(bmap, l);
4462 bmap = isl_basic_map_drop_div(bmap, div1);
4463 return bmap;
4466 /* First check if we can coalesce any pair of divs and
4467 * then continue with dropping more redundant divs.
4469 * We loop over all pairs of lower and upper bounds on a div
4470 * with coefficient 1 and -1, respectively, check if there
4471 * is any other div "c" with which we can coalesce the div
4472 * and if so, perform the coalescing.
4474 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4475 struct isl_basic_map *bmap, int *pairs, int n)
4477 int i, l, u;
4478 unsigned dim;
4480 dim = isl_space_dim(bmap->dim, isl_dim_all);
4482 for (i = 0; i < bmap->n_div; ++i) {
4483 if (!pairs[i])
4484 continue;
4485 for (l = 0; l < bmap->n_ineq; ++l) {
4486 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4487 continue;
4488 for (u = 0; u < bmap->n_ineq; ++u) {
4489 int c;
4491 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4492 continue;
4493 c = div_find_coalesce(bmap, pairs, i, l, u);
4494 if (c < 0)
4495 continue;
4496 free(pairs);
4497 bmap = coalesce_divs(bmap, i, c, l, u);
4498 return isl_basic_map_drop_redundant_divs(bmap);
4503 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4504 free(pairs);
4505 return bmap;
4508 return drop_more_redundant_divs(bmap, pairs, n);
4511 /* Are the "n" coefficients starting at "first" of inequality constraints
4512 * "i" and "j" of "bmap" equal to each other?
4514 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4515 int first, int n)
4517 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4520 /* Are the "n" coefficients starting at "first" of inequality constraints
4521 * "i" and "j" of "bmap" opposite to each other?
4523 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4524 int first, int n)
4526 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4529 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4530 * apart from the constant term?
4532 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4534 unsigned total;
4536 total = isl_basic_map_dim(bmap, isl_dim_all);
4537 return is_opposite_part(bmap, i, j, 1, total);
4540 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4541 * apart from the constant term and the coefficient at position "pos"?
4543 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4544 int pos)
4546 unsigned total;
4548 total = isl_basic_map_dim(bmap, isl_dim_all);
4549 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4550 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4553 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4554 * apart from the constant term and the coefficient at position "pos"?
4556 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4557 int pos)
4559 unsigned total;
4561 total = isl_basic_map_dim(bmap, isl_dim_all);
4562 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4563 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4566 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4567 * been modified, simplying it if "simplify" is set.
4568 * Free the temporary data structure "pairs" that was associated
4569 * to the old version of "bmap".
4571 static __isl_give isl_basic_map *drop_redundant_divs_again(
4572 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4574 if (simplify)
4575 bmap = isl_basic_map_simplify(bmap);
4576 free(pairs);
4577 return isl_basic_map_drop_redundant_divs(bmap);
4580 /* Is "div" the single unknown existentially quantified variable
4581 * in inequality constraint "ineq" of "bmap"?
4582 * "div" is known to have a non-zero coefficient in "ineq".
4584 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4585 int div)
4587 int i;
4588 unsigned n_div, o_div;
4589 isl_bool known;
4591 known = isl_basic_map_div_is_known(bmap, div);
4592 if (known < 0 || known)
4593 return isl_bool_not(known);
4594 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4595 if (n_div == 1)
4596 return isl_bool_true;
4597 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4598 for (i = 0; i < n_div; ++i) {
4599 isl_bool known;
4601 if (i == div)
4602 continue;
4603 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4604 continue;
4605 known = isl_basic_map_div_is_known(bmap, i);
4606 if (known < 0 || !known)
4607 return known;
4610 return isl_bool_true;
4613 /* Does integer division "div" have coefficient 1 in inequality constraint
4614 * "ineq" of "map"?
4616 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4618 unsigned o_div;
4620 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4621 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4622 return isl_bool_true;
4624 return isl_bool_false;
4627 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4628 * then try and drop redundant divs again,
4629 * freeing the temporary data structure "pairs" that was associated
4630 * to the old version of "bmap".
4632 static __isl_give isl_basic_map *set_eq_and_try_again(
4633 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4635 bmap = isl_basic_map_cow(bmap);
4636 isl_basic_map_inequality_to_equality(bmap, ineq);
4637 return drop_redundant_divs_again(bmap, pairs, 1);
4640 /* Drop the integer division at position "div", along with the two
4641 * inequality constraints "ineq1" and "ineq2" in which it appears
4642 * from "bmap" and then try and drop redundant divs again,
4643 * freeing the temporary data structure "pairs" that was associated
4644 * to the old version of "bmap".
4646 static __isl_give isl_basic_map *drop_div_and_try_again(
4647 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4648 __isl_take int *pairs)
4650 if (ineq1 > ineq2) {
4651 isl_basic_map_drop_inequality(bmap, ineq1);
4652 isl_basic_map_drop_inequality(bmap, ineq2);
4653 } else {
4654 isl_basic_map_drop_inequality(bmap, ineq2);
4655 isl_basic_map_drop_inequality(bmap, ineq1);
4657 bmap = isl_basic_map_drop_div(bmap, div);
4658 return drop_redundant_divs_again(bmap, pairs, 0);
4661 /* Given two inequality constraints
4663 * f(x) + n d + c >= 0, (ineq)
4665 * with d the variable at position "pos", and
4667 * f(x) + c0 >= 0, (lower)
4669 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4670 * determined by the first constraint.
4671 * That is, store
4673 * ceil((c0 - c)/n)
4675 * in *l.
4677 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4678 int ineq, int lower, int pos, isl_int *l)
4680 isl_int_neg(*l, bmap->ineq[ineq][0]);
4681 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4682 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4685 /* Given two inequality constraints
4687 * f(x) + n d + c >= 0, (ineq)
4689 * with d the variable at position "pos", and
4691 * -f(x) - c0 >= 0, (upper)
4693 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4694 * determined by the first constraint.
4695 * That is, store
4697 * ceil((-c1 - c)/n)
4699 * in *u.
4701 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4702 int ineq, int upper, int pos, isl_int *u)
4704 isl_int_neg(*u, bmap->ineq[ineq][0]);
4705 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4706 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4709 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4710 * does the corresponding lower bound have a fixed value in "bmap"?
4712 * In particular, "ineq" is of the form
4714 * f(x) + n d + c >= 0
4716 * with n > 0, c the constant term and
4717 * d the existentially quantified variable "div".
4718 * That is, the lower bound is
4720 * ceil((-f(x) - c)/n)
4722 * Look for a pair of constraints
4724 * f(x) + c0 >= 0
4725 * -f(x) + c1 >= 0
4727 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4728 * That is, check that
4730 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4732 * If so, return the index of inequality f(x) + c0 >= 0.
4733 * Otherwise, return -1.
4735 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4737 int i;
4738 int lower = -1, upper = -1;
4739 unsigned o_div;
4740 isl_int l, u;
4741 int equal;
4743 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4744 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4745 if (i == ineq)
4746 continue;
4747 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4748 continue;
4749 if (lower < 0 &&
4750 is_parallel_except(bmap, ineq, i, o_div + div)) {
4751 lower = i;
4752 continue;
4754 if (upper < 0 &&
4755 is_opposite_except(bmap, ineq, i, o_div + div)) {
4756 upper = i;
4760 if (lower < 0 || upper < 0)
4761 return -1;
4763 isl_int_init(l);
4764 isl_int_init(u);
4766 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4767 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4769 equal = isl_int_eq(l, u);
4771 isl_int_clear(l);
4772 isl_int_clear(u);
4774 return equal ? lower : -1;
4777 /* Given a lower bound constraint "ineq" on the existentially quantified
4778 * variable "div", such that the corresponding lower bound has
4779 * a fixed value in "bmap", assign this fixed value to the variable and
4780 * then try and drop redundant divs again,
4781 * freeing the temporary data structure "pairs" that was associated
4782 * to the old version of "bmap".
4783 * "lower" determines the constant value for the lower bound.
4785 * In particular, "ineq" is of the form
4787 * f(x) + n d + c >= 0,
4789 * while "lower" is of the form
4791 * f(x) + c0 >= 0
4793 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4794 * is ceil((c0 - c)/n).
4796 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4797 int div, int ineq, int lower, int *pairs)
4799 isl_int c;
4800 unsigned o_div;
4802 isl_int_init(c);
4804 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4805 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4806 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4807 free(pairs);
4809 isl_int_clear(c);
4811 return isl_basic_map_drop_redundant_divs(bmap);
4814 /* Remove divs that are not strictly needed based on the inequality
4815 * constraints.
4816 * In particular, if a div only occurs positively (or negatively)
4817 * in constraints, then it can simply be dropped.
4818 * Also, if a div occurs in only two constraints and if moreover
4819 * those two constraints are opposite to each other, except for the constant
4820 * term and if the sum of the constant terms is such that for any value
4821 * of the other values, there is always at least one integer value of the
4822 * div, i.e., if one plus this sum is greater than or equal to
4823 * the (absolute value) of the coefficient of the div in the constraints,
4824 * then we can also simply drop the div.
4826 * If an existentially quantified variable does not have an explicit
4827 * representation, appears in only a single lower bound that does not
4828 * involve any other such existentially quantified variables and appears
4829 * in this lower bound with coefficient 1,
4830 * then fix the variable to the value of the lower bound. That is,
4831 * turn the inequality into an equality.
4832 * If for any value of the other variables, there is any value
4833 * for the existentially quantified variable satisfying the constraints,
4834 * then this lower bound also satisfies the constraints.
4835 * It is therefore safe to pick this lower bound.
4837 * The same reasoning holds even if the coefficient is not one.
4838 * However, fixing the variable to the value of the lower bound may
4839 * in general introduce an extra integer division, in which case
4840 * it may be better to pick another value.
4841 * If this integer division has a known constant value, then plugging
4842 * in this constant value removes the existentially quantified variable
4843 * completely. In particular, if the lower bound is of the form
4844 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4845 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4846 * then the existentially quantified variable can be assigned this
4847 * shared value.
4849 * We skip divs that appear in equalities or in the definition of other divs.
4850 * Divs that appear in the definition of other divs usually occur in at least
4851 * 4 constraints, but the constraints may have been simplified.
4853 * If any divs are left after these simple checks then we move on
4854 * to more complicated cases in drop_more_redundant_divs.
4856 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4857 __isl_take isl_basic_map *bmap)
4859 int i, j;
4860 unsigned off;
4861 int *pairs = NULL;
4862 int n = 0;
4864 if (!bmap)
4865 goto error;
4866 if (bmap->n_div == 0)
4867 return bmap;
4869 off = isl_space_dim(bmap->dim, isl_dim_all);
4870 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4871 if (!pairs)
4872 goto error;
4874 for (i = 0; i < bmap->n_div; ++i) {
4875 int pos, neg;
4876 int last_pos, last_neg;
4877 int redundant;
4878 int defined;
4879 isl_bool opp, set_div;
4881 defined = !isl_int_is_zero(bmap->div[i][0]);
4882 for (j = i; j < bmap->n_div; ++j)
4883 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4884 break;
4885 if (j < bmap->n_div)
4886 continue;
4887 for (j = 0; j < bmap->n_eq; ++j)
4888 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4889 break;
4890 if (j < bmap->n_eq)
4891 continue;
4892 ++n;
4893 pos = neg = 0;
4894 for (j = 0; j < bmap->n_ineq; ++j) {
4895 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4896 last_pos = j;
4897 ++pos;
4899 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4900 last_neg = j;
4901 ++neg;
4904 pairs[i] = pos * neg;
4905 if (pairs[i] == 0) {
4906 for (j = bmap->n_ineq - 1; j >= 0; --j)
4907 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4908 isl_basic_map_drop_inequality(bmap, j);
4909 bmap = isl_basic_map_drop_div(bmap, i);
4910 return drop_redundant_divs_again(bmap, pairs, 0);
4912 if (pairs[i] != 1)
4913 opp = isl_bool_false;
4914 else
4915 opp = is_opposite(bmap, last_pos, last_neg);
4916 if (opp < 0)
4917 goto error;
4918 if (!opp) {
4919 int lower;
4920 isl_bool single, one;
4922 if (pos != 1)
4923 continue;
4924 single = single_unknown(bmap, last_pos, i);
4925 if (single < 0)
4926 goto error;
4927 if (!single)
4928 continue;
4929 one = has_coef_one(bmap, i, last_pos);
4930 if (one < 0)
4931 goto error;
4932 if (one)
4933 return set_eq_and_try_again(bmap, last_pos,
4934 pairs);
4935 lower = lower_bound_is_cst(bmap, i, last_pos);
4936 if (lower >= 0)
4937 return fix_cst_lower(bmap, i, last_pos, lower,
4938 pairs);
4939 continue;
4942 isl_int_add(bmap->ineq[last_pos][0],
4943 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4944 isl_int_add_ui(bmap->ineq[last_pos][0],
4945 bmap->ineq[last_pos][0], 1);
4946 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4947 bmap->ineq[last_pos][1+off+i]);
4948 isl_int_sub_ui(bmap->ineq[last_pos][0],
4949 bmap->ineq[last_pos][0], 1);
4950 isl_int_sub(bmap->ineq[last_pos][0],
4951 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4952 if (redundant)
4953 return drop_div_and_try_again(bmap, i,
4954 last_pos, last_neg, pairs);
4955 if (defined)
4956 set_div = isl_bool_false;
4957 else
4958 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4959 if (set_div < 0)
4960 return isl_basic_map_free(bmap);
4961 if (set_div) {
4962 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4963 return drop_redundant_divs_again(bmap, pairs, 1);
4965 pairs[i] = 0;
4966 --n;
4969 if (n > 0)
4970 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4972 free(pairs);
4973 return bmap;
4974 error:
4975 free(pairs);
4976 isl_basic_map_free(bmap);
4977 return NULL;
4980 /* Consider the coefficients at "c" as a row vector and replace
4981 * them with their product with "T". "T" is assumed to be a square matrix.
4983 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4985 int n;
4986 isl_ctx *ctx;
4987 isl_vec *v;
4989 if (!T)
4990 return isl_stat_error;
4991 n = isl_mat_rows(T);
4992 if (isl_seq_first_non_zero(c, n) == -1)
4993 return isl_stat_ok;
4994 ctx = isl_mat_get_ctx(T);
4995 v = isl_vec_alloc(ctx, n);
4996 if (!v)
4997 return isl_stat_error;
4998 isl_seq_swp_or_cpy(v->el, c, n);
4999 v = isl_vec_mat_product(v, isl_mat_copy(T));
5000 if (!v)
5001 return isl_stat_error;
5002 isl_seq_swp_or_cpy(c, v->el, n);
5003 isl_vec_free(v);
5005 return isl_stat_ok;
5008 /* Plug in T for the variables in "bmap" starting at "pos".
5009 * T is a linear unimodular matrix, i.e., without constant term.
5011 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5012 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5014 int i;
5015 unsigned n, total;
5017 bmap = isl_basic_map_cow(bmap);
5018 if (!bmap || !T)
5019 goto error;
5021 n = isl_mat_cols(T);
5022 if (n != isl_mat_rows(T))
5023 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5024 "expecting square matrix", goto error);
5026 total = isl_basic_map_dim(bmap, isl_dim_all);
5027 if (pos + n > total || pos + n < pos)
5028 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5029 "invalid range", goto error);
5031 for (i = 0; i < bmap->n_eq; ++i)
5032 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5033 goto error;
5034 for (i = 0; i < bmap->n_ineq; ++i)
5035 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5036 goto error;
5037 for (i = 0; i < bmap->n_div; ++i) {
5038 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5039 continue;
5040 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5041 goto error;
5044 isl_mat_free(T);
5045 return bmap;
5046 error:
5047 isl_basic_map_free(bmap);
5048 isl_mat_free(T);
5049 return NULL;
5052 /* Remove divs that are not strictly needed.
5054 * First look for an equality constraint involving two or more
5055 * existentially quantified variables without an explicit
5056 * representation. Replace the combination that appears
5057 * in the equality constraint by a single existentially quantified
5058 * variable such that the equality can be used to derive
5059 * an explicit representation for the variable.
5060 * If there are no more such equality constraints, then continue
5061 * with isl_basic_map_drop_redundant_divs_ineq.
5063 * In particular, if the equality constraint is of the form
5065 * f(x) + \sum_i c_i a_i = 0
5067 * with a_i existentially quantified variable without explicit
5068 * representation, then apply a transformation on the existentially
5069 * quantified variables to turn the constraint into
5071 * f(x) + g a_1' = 0
5073 * with g the gcd of the c_i.
5074 * In order to easily identify which existentially quantified variables
5075 * have a complete explicit representation, i.e., without being defined
5076 * in terms of other existentially quantified variables without
5077 * an explicit representation, the existentially quantified variables
5078 * are first sorted.
5080 * The variable transformation is computed by extending the row
5081 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5083 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5084 * [a_2'] [ a_2 ]
5085 * ... = U ....
5086 * [a_n'] [ a_n ]
5088 * with [c_1/g ... c_n/g] representing the first row of U.
5089 * The inverse of U is then plugged into the original constraints.
5090 * The call to isl_basic_map_simplify makes sure the explicit
5091 * representation for a_1' is extracted from the equality constraint.
5093 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5094 __isl_take isl_basic_map *bmap)
5096 int first;
5097 int i;
5098 unsigned o_div, n_div;
5099 int l;
5100 isl_ctx *ctx;
5101 isl_mat *T;
5103 if (!bmap)
5104 return NULL;
5105 if (isl_basic_map_divs_known(bmap))
5106 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5107 if (bmap->n_eq == 0)
5108 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5109 bmap = isl_basic_map_sort_divs(bmap);
5110 if (!bmap)
5111 return NULL;
5113 first = isl_basic_map_first_unknown_div(bmap);
5114 if (first < 0)
5115 return isl_basic_map_free(bmap);
5117 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5118 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5120 for (i = 0; i < bmap->n_eq; ++i) {
5121 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5122 n_div - (first));
5123 if (l < 0)
5124 continue;
5125 l += first;
5126 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5127 n_div - (l + 1)) == -1)
5128 continue;
5129 break;
5131 if (i >= bmap->n_eq)
5132 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5134 ctx = isl_basic_map_get_ctx(bmap);
5135 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5136 if (!T)
5137 return isl_basic_map_free(bmap);
5138 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5139 T = isl_mat_normalize_row(T, 0);
5140 T = isl_mat_unimodular_complete(T, 1);
5141 T = isl_mat_right_inverse(T);
5143 for (i = l; i < n_div; ++i)
5144 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5145 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5146 bmap = isl_basic_map_simplify(bmap);
5148 return isl_basic_map_drop_redundant_divs(bmap);
5151 /* Does "bmap" satisfy any equality that involves more than 2 variables
5152 * and/or has coefficients different from -1 and 1?
5154 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5156 int i;
5157 unsigned total;
5159 total = isl_basic_map_dim(bmap, isl_dim_all);
5161 for (i = 0; i < bmap->n_eq; ++i) {
5162 int j, k;
5164 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5165 if (j < 0)
5166 continue;
5167 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5168 !isl_int_is_negone(bmap->eq[i][1 + j]))
5169 return 1;
5171 j += 1;
5172 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5173 if (k < 0)
5174 continue;
5175 j += k;
5176 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5177 !isl_int_is_negone(bmap->eq[i][1 + j]))
5178 return 1;
5180 j += 1;
5181 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5182 if (k >= 0)
5183 return 1;
5186 return 0;
5189 /* Remove any common factor g from the constraint coefficients in "v".
5190 * The constant term is stored in the first position and is replaced
5191 * by floor(c/g). If any common factor is removed and if this results
5192 * in a tightening of the constraint, then set *tightened.
5194 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5195 int *tightened)
5197 isl_ctx *ctx;
5199 if (!v)
5200 return NULL;
5201 ctx = isl_vec_get_ctx(v);
5202 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5203 if (isl_int_is_zero(ctx->normalize_gcd))
5204 return v;
5205 if (isl_int_is_one(ctx->normalize_gcd))
5206 return v;
5207 v = isl_vec_cow(v);
5208 if (!v)
5209 return NULL;
5210 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5211 *tightened = 1;
5212 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5213 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5214 v->size - 1);
5215 return v;
5218 /* If "bmap" is an integer set that satisfies any equality involving
5219 * more than 2 variables and/or has coefficients different from -1 and 1,
5220 * then use variable compression to reduce the coefficients by removing
5221 * any (hidden) common factor.
5222 * In particular, apply the variable compression to each constraint,
5223 * factor out any common factor in the non-constant coefficients and
5224 * then apply the inverse of the compression.
5225 * At the end, we mark the basic map as having reduced constants.
5226 * If this flag is still set on the next invocation of this function,
5227 * then we skip the computation.
5229 * Removing a common factor may result in a tightening of some of
5230 * the constraints. If this happens, then we may end up with two
5231 * opposite inequalities that can be replaced by an equality.
5232 * We therefore call isl_basic_map_detect_inequality_pairs,
5233 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5234 * and isl_basic_map_gauss if such a pair was found.
5236 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5237 __isl_take isl_basic_map *bmap)
5239 unsigned total;
5240 isl_ctx *ctx;
5241 isl_vec *v;
5242 isl_mat *eq, *T, *T2;
5243 int i;
5244 int tightened;
5246 if (!bmap)
5247 return NULL;
5248 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5249 return bmap;
5250 if (isl_basic_map_is_rational(bmap))
5251 return bmap;
5252 if (bmap->n_eq == 0)
5253 return bmap;
5254 if (!has_multiple_var_equality(bmap))
5255 return bmap;
5257 total = isl_basic_map_dim(bmap, isl_dim_all);
5258 ctx = isl_basic_map_get_ctx(bmap);
5259 v = isl_vec_alloc(ctx, 1 + total);
5260 if (!v)
5261 return isl_basic_map_free(bmap);
5263 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5264 T = isl_mat_variable_compression(eq, &T2);
5265 if (!T || !T2)
5266 goto error;
5267 if (T->n_col == 0) {
5268 isl_mat_free(T);
5269 isl_mat_free(T2);
5270 isl_vec_free(v);
5271 return isl_basic_map_set_to_empty(bmap);
5274 tightened = 0;
5275 for (i = 0; i < bmap->n_ineq; ++i) {
5276 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5277 v = isl_vec_mat_product(v, isl_mat_copy(T));
5278 v = normalize_constraint(v, &tightened);
5279 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5280 if (!v)
5281 goto error;
5282 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5285 isl_mat_free(T);
5286 isl_mat_free(T2);
5287 isl_vec_free(v);
5289 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5291 if (tightened) {
5292 int progress = 0;
5294 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5295 if (progress) {
5296 bmap = eliminate_divs_eq(bmap, &progress);
5297 bmap = isl_basic_map_gauss(bmap, NULL);
5301 return bmap;
5302 error:
5303 isl_mat_free(T);
5304 isl_mat_free(T2);
5305 isl_vec_free(v);
5306 return isl_basic_map_free(bmap);
5309 /* Shift the integer division at position "div" of "bmap"
5310 * by "shift" times the variable at position "pos".
5311 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5312 * corresponds to the constant term.
5314 * That is, if the integer division has the form
5316 * floor(f(x)/d)
5318 * then replace it by
5320 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5322 __isl_give isl_basic_map *isl_basic_map_shift_div(
5323 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5325 int i;
5326 unsigned total;
5328 if (isl_int_is_zero(shift))
5329 return bmap;
5330 if (!bmap)
5331 return NULL;
5333 total = isl_basic_map_dim(bmap, isl_dim_all);
5334 total -= isl_basic_map_dim(bmap, isl_dim_div);
5336 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5338 for (i = 0; i < bmap->n_eq; ++i) {
5339 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5340 continue;
5341 isl_int_submul(bmap->eq[i][pos],
5342 shift, bmap->eq[i][1 + total + div]);
5344 for (i = 0; i < bmap->n_ineq; ++i) {
5345 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5346 continue;
5347 isl_int_submul(bmap->ineq[i][pos],
5348 shift, bmap->ineq[i][1 + total + div]);
5350 for (i = 0; i < bmap->n_div; ++i) {
5351 if (isl_int_is_zero(bmap->div[i][0]))
5352 continue;
5353 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5354 continue;
5355 isl_int_submul(bmap->div[i][1 + pos],
5356 shift, bmap->div[i][1 + 1 + total + div]);
5359 return bmap;