isl_pw_templ.c: any_divs: return isl_bool
[isl.git] / isl_map_simplify.c
blobf6b4e3a3149a5d1b0b35b64c989f696b1a7b0d84
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 int i;
65 if (!bset)
66 goto error;
68 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
70 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
71 return bset;
73 bset = isl_basic_set_cow(bset);
74 if (!bset)
75 return NULL;
77 for (i = 0; i < bset->n_eq; ++i)
78 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
79 (bset->dim->n_out-first-n)+bset->extra);
81 for (i = 0; i < bset->n_ineq; ++i)
82 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
83 (bset->dim->n_out-first-n)+bset->extra);
85 for (i = 0; i < bset->n_div; ++i)
86 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
87 (bset->dim->n_out-first-n)+bset->extra);
89 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
90 if (!bset->dim)
91 goto error;
93 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
94 bset = isl_basic_set_simplify(bset);
95 return isl_basic_set_finalize(bset);
96 error:
97 isl_basic_set_free(bset);
98 return NULL;
101 struct isl_set *isl_set_drop_dims(
102 struct isl_set *set, unsigned first, unsigned n)
104 int i;
106 if (!set)
107 goto error;
109 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
111 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
112 return set;
113 set = isl_set_cow(set);
114 if (!set)
115 goto error;
116 set->dim = isl_space_drop_outputs(set->dim, first, n);
117 if (!set->dim)
118 goto error;
120 for (i = 0; i < set->n; ++i) {
121 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
122 if (!set->p[i])
123 goto error;
126 ISL_F_CLR(set, ISL_SET_NORMALIZED);
127 return set;
128 error:
129 isl_set_free(set);
130 return NULL;
133 /* Move "n" divs starting at "first" to the end of the list of divs.
135 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
136 unsigned first, unsigned n)
138 isl_int **div;
139 int i;
141 if (first + n == bmap->n_div)
142 return bmap;
144 div = isl_alloc_array(bmap->ctx, isl_int *, n);
145 if (!div)
146 goto error;
147 for (i = 0; i < n; ++i)
148 div[i] = bmap->div[first + i];
149 for (i = 0; i < bmap->n_div - first - n; ++i)
150 bmap->div[first + i] = bmap->div[first + n + i];
151 for (i = 0; i < n; ++i)
152 bmap->div[bmap->n_div - n + i] = div[i];
153 free(div);
154 return bmap;
155 error:
156 isl_basic_map_free(bmap);
157 return NULL;
160 /* Drop "n" dimensions of type "type" starting at "first".
162 * In principle, this frees up some extra variables as the number
163 * of columns remains constant, but we would have to extend
164 * the div array too as the number of rows in this array is assumed
165 * to be equal to extra.
167 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
168 enum isl_dim_type type, unsigned first, unsigned n)
170 int i;
171 unsigned dim;
172 unsigned offset;
173 unsigned left;
175 if (!bmap)
176 goto error;
178 dim = isl_basic_map_dim(bmap, type);
179 isl_assert(bmap->ctx, first + n <= dim, goto error);
181 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
182 return bmap;
184 bmap = isl_basic_map_cow(bmap);
185 if (!bmap)
186 return NULL;
188 offset = isl_basic_map_offset(bmap, type) + first;
189 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
190 for (i = 0; i < bmap->n_eq; ++i)
191 constraint_drop_vars(bmap->eq[i]+offset, n, left);
193 for (i = 0; i < bmap->n_ineq; ++i)
194 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
196 for (i = 0; i < bmap->n_div; ++i)
197 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
199 if (type == isl_dim_div) {
200 bmap = move_divs_last(bmap, first, n);
201 if (!bmap)
202 goto error;
203 if (isl_basic_map_free_div(bmap, n) < 0)
204 return isl_basic_map_free(bmap);
205 } else
206 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
207 if (!bmap->dim)
208 goto error;
210 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
211 bmap = isl_basic_map_simplify(bmap);
212 return isl_basic_map_finalize(bmap);
213 error:
214 isl_basic_map_free(bmap);
215 return NULL;
218 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
219 enum isl_dim_type type, unsigned first, unsigned n)
221 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
222 type, first, n));
225 struct isl_basic_map *isl_basic_map_drop_inputs(
226 struct isl_basic_map *bmap, unsigned first, unsigned n)
228 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
231 struct isl_map *isl_map_drop(struct isl_map *map,
232 enum isl_dim_type type, unsigned first, unsigned n)
234 int i;
236 if (!map)
237 goto error;
239 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
241 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
242 return map;
243 map = isl_map_cow(map);
244 if (!map)
245 goto error;
246 map->dim = isl_space_drop_dims(map->dim, type, first, n);
247 if (!map->dim)
248 goto error;
250 for (i = 0; i < map->n; ++i) {
251 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
252 if (!map->p[i])
253 goto error;
255 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
257 return map;
258 error:
259 isl_map_free(map);
260 return NULL;
263 struct isl_set *isl_set_drop(struct isl_set *set,
264 enum isl_dim_type type, unsigned first, unsigned n)
266 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
269 struct isl_map *isl_map_drop_inputs(
270 struct isl_map *map, unsigned first, unsigned n)
272 return isl_map_drop(map, isl_dim_in, first, n);
276 * We don't cow, as the div is assumed to be redundant.
278 __isl_give isl_basic_map *isl_basic_map_drop_div(
279 __isl_take isl_basic_map *bmap, unsigned div)
281 int i;
282 unsigned pos;
284 if (!bmap)
285 goto error;
287 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
289 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
291 for (i = 0; i < bmap->n_eq; ++i)
292 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
294 for (i = 0; i < bmap->n_ineq; ++i) {
295 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
296 isl_basic_map_drop_inequality(bmap, i);
297 --i;
298 continue;
300 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
303 for (i = 0; i < bmap->n_div; ++i)
304 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
306 if (div != bmap->n_div - 1) {
307 int j;
308 isl_int *t = bmap->div[div];
310 for (j = div; j < bmap->n_div - 1; ++j)
311 bmap->div[j] = bmap->div[j+1];
313 bmap->div[bmap->n_div - 1] = t;
315 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
316 isl_basic_map_free_div(bmap, 1);
318 return bmap;
319 error:
320 isl_basic_map_free(bmap);
321 return NULL;
324 struct isl_basic_map *isl_basic_map_normalize_constraints(
325 struct isl_basic_map *bmap)
327 int i;
328 isl_int gcd;
329 unsigned total = isl_basic_map_total_dim(bmap);
331 if (!bmap)
332 return NULL;
334 isl_int_init(gcd);
335 for (i = bmap->n_eq - 1; i >= 0; --i) {
336 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
337 if (isl_int_is_zero(gcd)) {
338 if (!isl_int_is_zero(bmap->eq[i][0])) {
339 bmap = isl_basic_map_set_to_empty(bmap);
340 break;
342 isl_basic_map_drop_equality(bmap, i);
343 continue;
345 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
346 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
347 if (isl_int_is_one(gcd))
348 continue;
349 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
350 bmap = isl_basic_map_set_to_empty(bmap);
351 break;
353 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
356 for (i = bmap->n_ineq - 1; i >= 0; --i) {
357 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
358 if (isl_int_is_zero(gcd)) {
359 if (isl_int_is_neg(bmap->ineq[i][0])) {
360 bmap = isl_basic_map_set_to_empty(bmap);
361 break;
363 isl_basic_map_drop_inequality(bmap, i);
364 continue;
366 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
367 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
368 if (isl_int_is_one(gcd))
369 continue;
370 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
371 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
373 isl_int_clear(gcd);
375 return bmap;
378 struct isl_basic_set *isl_basic_set_normalize_constraints(
379 struct isl_basic_set *bset)
381 isl_basic_map *bmap = bset_to_bmap(bset);
382 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
385 /* Reduce the coefficient of the variable at position "pos"
386 * in integer division "div", such that it lies in the half-open
387 * interval (1/2,1/2], extracting any excess value from this integer division.
388 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
389 * corresponds to the constant term.
391 * That is, the integer division is of the form
393 * floor((... + (c * d + r) * x_pos + ...)/d)
395 * with -d < 2 * r <= d.
396 * Replace it by
398 * floor((... + r * x_pos + ...)/d) + c * x_pos
400 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
401 * Otherwise, c = floor((c * d + r)/d) + 1.
403 * This is the same normalization that is performed by isl_aff_floor.
405 static __isl_give isl_basic_map *reduce_coefficient_in_div(
406 __isl_take isl_basic_map *bmap, int div, int pos)
408 isl_int shift;
409 int add_one;
411 isl_int_init(shift);
412 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
413 isl_int_mul_ui(shift, shift, 2);
414 add_one = isl_int_gt(shift, bmap->div[div][0]);
415 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
416 if (add_one)
417 isl_int_add_ui(shift, shift, 1);
418 isl_int_neg(shift, shift);
419 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
420 isl_int_clear(shift);
422 return bmap;
425 /* Does the coefficient of the variable at position "pos"
426 * in integer division "div" need to be reduced?
427 * That is, does it lie outside the half-open interval (1/2,1/2]?
428 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
429 * 2 * c != d.
431 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
432 int pos)
434 isl_bool r;
436 if (isl_int_is_zero(bmap->div[div][1 + pos]))
437 return isl_bool_false;
439 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
440 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
441 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
442 isl_int_divexact_ui(bmap->div[div][1 + pos],
443 bmap->div[div][1 + pos], 2);
445 return r;
448 /* Reduce the coefficients (including the constant term) of
449 * integer division "div", if needed.
450 * In particular, make sure all coefficients lie in
451 * the half-open interval (1/2,1/2].
453 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
454 __isl_take isl_basic_map *bmap, int div)
456 int i;
457 unsigned total = 1 + isl_basic_map_total_dim(bmap);
459 for (i = 0; i < total; ++i) {
460 isl_bool reduce;
462 reduce = needs_reduction(bmap, div, i);
463 if (reduce < 0)
464 return isl_basic_map_free(bmap);
465 if (!reduce)
466 continue;
467 bmap = reduce_coefficient_in_div(bmap, div, i);
468 if (!bmap)
469 break;
472 return bmap;
475 /* Reduce the coefficients (including the constant term) of
476 * the known integer divisions, if needed
477 * In particular, make sure all coefficients lie in
478 * the half-open interval (1/2,1/2].
480 static __isl_give isl_basic_map *reduce_div_coefficients(
481 __isl_take isl_basic_map *bmap)
483 int i;
485 if (!bmap)
486 return NULL;
487 if (bmap->n_div == 0)
488 return bmap;
490 for (i = 0; i < bmap->n_div; ++i) {
491 if (isl_int_is_zero(bmap->div[i][0]))
492 continue;
493 bmap = reduce_div_coefficients_of_div(bmap, i);
494 if (!bmap)
495 break;
498 return bmap;
501 /* Remove any common factor in numerator and denominator of the div expression,
502 * not taking into account the constant term.
503 * That is, if the div is of the form
505 * floor((a + m f(x))/(m d))
507 * then replace it by
509 * floor((floor(a/m) + f(x))/d)
511 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
512 * and can therefore not influence the result of the floor.
514 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
516 unsigned total = isl_basic_map_total_dim(bmap);
517 isl_ctx *ctx = bmap->ctx;
519 if (isl_int_is_zero(bmap->div[div][0]))
520 return;
521 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
522 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
523 if (isl_int_is_one(ctx->normalize_gcd))
524 return;
525 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
526 ctx->normalize_gcd);
527 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
528 ctx->normalize_gcd);
529 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
530 ctx->normalize_gcd, total);
533 /* Remove any common factor in numerator and denominator of a div expression,
534 * not taking into account the constant term.
535 * That is, look for any div of the form
537 * floor((a + m f(x))/(m d))
539 * and replace it by
541 * floor((floor(a/m) + f(x))/d)
543 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
544 * and can therefore not influence the result of the floor.
546 static __isl_give isl_basic_map *normalize_div_expressions(
547 __isl_take isl_basic_map *bmap)
549 int i;
551 if (!bmap)
552 return NULL;
553 if (bmap->n_div == 0)
554 return bmap;
556 for (i = 0; i < bmap->n_div; ++i)
557 normalize_div_expression(bmap, i);
559 return bmap;
562 /* Assumes divs have been ordered if keep_divs is set.
564 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
565 unsigned pos, isl_int *eq, int keep_divs, int *progress)
567 unsigned total;
568 unsigned space_total;
569 int k;
570 int last_div;
572 total = isl_basic_map_total_dim(bmap);
573 space_total = isl_space_dim(bmap->dim, isl_dim_all);
574 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
575 for (k = 0; k < bmap->n_eq; ++k) {
576 if (bmap->eq[k] == eq)
577 continue;
578 if (isl_int_is_zero(bmap->eq[k][1+pos]))
579 continue;
580 if (progress)
581 *progress = 1;
582 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
583 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
586 for (k = 0; k < bmap->n_ineq; ++k) {
587 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
588 continue;
589 if (progress)
590 *progress = 1;
591 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
592 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
596 for (k = 0; k < bmap->n_div; ++k) {
597 if (isl_int_is_zero(bmap->div[k][0]))
598 continue;
599 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
600 continue;
601 if (progress)
602 *progress = 1;
603 /* We need to be careful about circular definitions,
604 * so for now we just remove the definition of div k
605 * if the equality contains any divs.
606 * If keep_divs is set, then the divs have been ordered
607 * and we can keep the definition as long as the result
608 * is still ordered.
610 if (last_div == -1 || (keep_divs && last_div < k)) {
611 isl_seq_elim(bmap->div[k]+1, eq,
612 1+pos, 1+total, &bmap->div[k][0]);
613 normalize_div_expression(bmap, k);
614 } else
615 isl_seq_clr(bmap->div[k], 1 + total);
616 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
620 /* Assumes divs have been ordered if keep_divs is set.
622 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
623 isl_int *eq, unsigned div, int keep_divs)
625 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
627 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
629 bmap = isl_basic_map_drop_div(bmap, div);
631 return bmap;
634 /* Check if elimination of div "div" using equality "eq" would not
635 * result in a div depending on a later div.
637 static isl_bool ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
638 unsigned div)
640 int k;
641 int last_div;
642 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
643 unsigned pos = space_total + div;
645 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
646 if (last_div < 0 || last_div <= div)
647 return isl_bool_true;
649 for (k = 0; k <= last_div; ++k) {
650 if (isl_int_is_zero(bmap->div[k][0]))
651 continue;
652 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
653 return isl_bool_false;
656 return isl_bool_true;
659 /* Eliminate divs based on equalities
661 static struct isl_basic_map *eliminate_divs_eq(
662 struct isl_basic_map *bmap, int *progress)
664 int d;
665 int i;
666 int modified = 0;
667 unsigned off;
669 bmap = isl_basic_map_order_divs(bmap);
671 if (!bmap)
672 return NULL;
674 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
676 for (d = bmap->n_div - 1; d >= 0 ; --d) {
677 for (i = 0; i < bmap->n_eq; ++i) {
678 isl_bool ok;
680 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
681 !isl_int_is_negone(bmap->eq[i][off + d]))
682 continue;
683 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
684 if (ok < 0)
685 return isl_basic_map_free(bmap);
686 if (!ok)
687 continue;
688 modified = 1;
689 *progress = 1;
690 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
691 if (isl_basic_map_drop_equality(bmap, i) < 0)
692 return isl_basic_map_free(bmap);
693 break;
696 if (modified)
697 return eliminate_divs_eq(bmap, progress);
698 return bmap;
701 /* Elimininate divs based on inequalities
703 static struct isl_basic_map *eliminate_divs_ineq(
704 struct isl_basic_map *bmap, int *progress)
706 int d;
707 int i;
708 unsigned off;
709 struct isl_ctx *ctx;
711 if (!bmap)
712 return NULL;
714 ctx = bmap->ctx;
715 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
717 for (d = bmap->n_div - 1; d >= 0 ; --d) {
718 for (i = 0; i < bmap->n_eq; ++i)
719 if (!isl_int_is_zero(bmap->eq[i][off + d]))
720 break;
721 if (i < bmap->n_eq)
722 continue;
723 for (i = 0; i < bmap->n_ineq; ++i)
724 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
725 break;
726 if (i < bmap->n_ineq)
727 continue;
728 *progress = 1;
729 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
730 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
731 break;
732 bmap = isl_basic_map_drop_div(bmap, d);
733 if (!bmap)
734 break;
736 return bmap;
739 /* Does the equality constraint at position "eq" in "bmap" involve
740 * any local variables in the range [first, first + n)
741 * that are not marked as having an explicit representation?
743 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
744 int eq, unsigned first, unsigned n)
746 unsigned o_div;
747 int i;
749 if (!bmap)
750 return isl_bool_error;
752 o_div = isl_basic_map_offset(bmap, isl_dim_div);
753 for (i = 0; i < n; ++i) {
754 isl_bool unknown;
756 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
757 continue;
758 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
759 if (unknown < 0)
760 return isl_bool_error;
761 if (unknown)
762 return isl_bool_true;
765 return isl_bool_false;
768 /* The last local variable involved in the equality constraint
769 * at position "eq" in "bmap" is the local variable at position "div".
770 * It can therefore be used to extract an explicit representation
771 * for that variable.
772 * Do so unless the local variable already has an explicit representation or
773 * the explicit representation would involve any other local variables
774 * that in turn do not have an explicit representation.
775 * An equality constraint involving local variables without an explicit
776 * representation can be used in isl_basic_map_drop_redundant_divs
777 * to separate out an independent local variable. Introducing
778 * an explicit representation here would block this transformation,
779 * while the partial explicit representation in itself is not very useful.
780 * Set *progress if anything is changed.
782 * The equality constraint is of the form
784 * f(x) + n e >= 0
786 * with n a positive number. The explicit representation derived from
787 * this constraint is
789 * floor((-f(x))/n)
791 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
792 int div, int eq, int *progress)
794 unsigned total, o_div;
795 isl_bool involves;
797 if (!bmap)
798 return NULL;
800 if (!isl_int_is_zero(bmap->div[div][0]))
801 return bmap;
803 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
804 if (involves < 0)
805 return isl_basic_map_free(bmap);
806 if (involves)
807 return bmap;
809 total = isl_basic_map_dim(bmap, isl_dim_all);
810 o_div = isl_basic_map_offset(bmap, isl_dim_div);
811 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
812 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
813 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
814 if (progress)
815 *progress = 1;
816 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
818 return bmap;
821 struct isl_basic_map *isl_basic_map_gauss(
822 struct isl_basic_map *bmap, int *progress)
824 int k;
825 int done;
826 int last_var;
827 unsigned total_var;
828 unsigned total;
830 bmap = isl_basic_map_order_divs(bmap);
832 if (!bmap)
833 return NULL;
835 total = isl_basic_map_total_dim(bmap);
836 total_var = total - bmap->n_div;
838 last_var = total - 1;
839 for (done = 0; done < bmap->n_eq; ++done) {
840 for (; last_var >= 0; --last_var) {
841 for (k = done; k < bmap->n_eq; ++k)
842 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
843 break;
844 if (k < bmap->n_eq)
845 break;
847 if (last_var < 0)
848 break;
849 if (k != done)
850 swap_equality(bmap, k, done);
851 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
852 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
854 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
855 progress);
857 if (last_var >= total_var)
858 bmap = set_div_from_eq(bmap, last_var - total_var,
859 done, progress);
860 if (!bmap)
861 return NULL;
863 if (done == bmap->n_eq)
864 return bmap;
865 for (k = done; k < bmap->n_eq; ++k) {
866 if (isl_int_is_zero(bmap->eq[k][0]))
867 continue;
868 return isl_basic_map_set_to_empty(bmap);
870 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
871 return bmap;
874 struct isl_basic_set *isl_basic_set_gauss(
875 struct isl_basic_set *bset, int *progress)
877 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
878 progress));
882 static unsigned int round_up(unsigned int v)
884 int old_v = v;
886 while (v) {
887 old_v = v;
888 v ^= v & -v;
890 return old_v << 1;
893 /* Hash table of inequalities in a basic map.
894 * "index" is an array of addresses of inequalities in the basic map, some
895 * of which are NULL. The inequalities are hashed on the coefficients
896 * except the constant term.
897 * "size" is the number of elements in the array and is always a power of two
898 * "bits" is the number of bits need to represent an index into the array.
899 * "total" is the total dimension of the basic map.
901 struct isl_constraint_index {
902 unsigned int size;
903 int bits;
904 isl_int ***index;
905 unsigned total;
908 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
910 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
911 __isl_keep isl_basic_map *bmap)
913 isl_ctx *ctx;
915 ci->index = NULL;
916 if (!bmap)
917 return isl_stat_error;
918 ci->total = isl_basic_set_total_dim(bmap);
919 if (bmap->n_ineq == 0)
920 return isl_stat_ok;
921 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
922 ci->bits = ffs(ci->size) - 1;
923 ctx = isl_basic_map_get_ctx(bmap);
924 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
925 if (!ci->index)
926 return isl_stat_error;
928 return isl_stat_ok;
931 /* Free the memory allocated by create_constraint_index.
933 static void constraint_index_free(struct isl_constraint_index *ci)
935 free(ci->index);
938 /* Return the position in ci->index that contains the address of
939 * an inequality that is equal to *ineq up to the constant term,
940 * provided this address is not identical to "ineq".
941 * If there is no such inequality, then return the position where
942 * such an inequality should be inserted.
944 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
946 int h;
947 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
948 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
949 if (ineq != ci->index[h] &&
950 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
951 break;
952 return h;
955 /* Return the position in ci->index that contains the address of
956 * an inequality that is equal to the k'th inequality of "bmap"
957 * up to the constant term, provided it does not point to the very
958 * same inequality.
959 * If there is no such inequality, then return the position where
960 * such an inequality should be inserted.
962 static int hash_index(struct isl_constraint_index *ci,
963 __isl_keep isl_basic_map *bmap, int k)
965 return hash_index_ineq(ci, &bmap->ineq[k]);
968 static int set_hash_index(struct isl_constraint_index *ci,
969 struct isl_basic_set *bset, int k)
971 return hash_index(ci, bset, k);
974 /* Fill in the "ci" data structure with the inequalities of "bset".
976 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
977 __isl_keep isl_basic_set *bset)
979 int k, h;
981 if (create_constraint_index(ci, bset) < 0)
982 return isl_stat_error;
984 for (k = 0; k < bset->n_ineq; ++k) {
985 h = set_hash_index(ci, bset, k);
986 ci->index[h] = &bset->ineq[k];
989 return isl_stat_ok;
992 /* Is the inequality ineq (obviously) redundant with respect
993 * to the constraints in "ci"?
995 * Look for an inequality in "ci" with the same coefficients and then
996 * check if the contant term of "ineq" is greater than or equal
997 * to the constant term of that inequality. If so, "ineq" is clearly
998 * redundant.
1000 * Note that hash_index_ineq ignores a stored constraint if it has
1001 * the same address as the passed inequality. It is ok to pass
1002 * the address of a local variable here since it will never be
1003 * the same as the address of a constraint in "ci".
1005 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
1006 isl_int *ineq)
1008 int h;
1010 h = hash_index_ineq(ci, &ineq);
1011 if (!ci->index[h])
1012 return isl_bool_false;
1013 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
1016 /* If we can eliminate more than one div, then we need to make
1017 * sure we do it from last div to first div, in order not to
1018 * change the position of the other divs that still need to
1019 * be removed.
1021 static struct isl_basic_map *remove_duplicate_divs(
1022 struct isl_basic_map *bmap, int *progress)
1024 unsigned int size;
1025 int *index;
1026 int *elim_for;
1027 int k, l, h;
1028 int bits;
1029 struct isl_blk eq;
1030 unsigned total_var;
1031 unsigned total;
1032 struct isl_ctx *ctx;
1034 bmap = isl_basic_map_order_divs(bmap);
1035 if (!bmap || bmap->n_div <= 1)
1036 return bmap;
1038 total_var = isl_space_dim(bmap->dim, isl_dim_all);
1039 total = total_var + bmap->n_div;
1041 ctx = bmap->ctx;
1042 for (k = bmap->n_div - 1; k >= 0; --k)
1043 if (!isl_int_is_zero(bmap->div[k][0]))
1044 break;
1045 if (k <= 0)
1046 return bmap;
1048 size = round_up(4 * bmap->n_div / 3 - 1);
1049 if (size == 0)
1050 return bmap;
1051 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
1052 bits = ffs(size) - 1;
1053 index = isl_calloc_array(ctx, int, size);
1054 if (!elim_for || !index)
1055 goto out;
1056 eq = isl_blk_alloc(ctx, 1+total);
1057 if (isl_blk_is_error(eq))
1058 goto out;
1060 isl_seq_clr(eq.data, 1+total);
1061 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
1062 for (--k; k >= 0; --k) {
1063 uint32_t hash;
1065 if (isl_int_is_zero(bmap->div[k][0]))
1066 continue;
1068 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
1069 for (h = hash; index[h]; h = (h+1) % size)
1070 if (isl_seq_eq(bmap->div[k],
1071 bmap->div[index[h]-1], 2+total))
1072 break;
1073 if (index[h]) {
1074 *progress = 1;
1075 l = index[h] - 1;
1076 elim_for[l] = k + 1;
1078 index[h] = k+1;
1080 for (l = bmap->n_div - 1; l >= 0; --l) {
1081 if (!elim_for[l])
1082 continue;
1083 k = elim_for[l] - 1;
1084 isl_int_set_si(eq.data[1+total_var+k], -1);
1085 isl_int_set_si(eq.data[1+total_var+l], 1);
1086 bmap = eliminate_div(bmap, eq.data, l, 1);
1087 if (!bmap)
1088 break;
1089 isl_int_set_si(eq.data[1+total_var+k], 0);
1090 isl_int_set_si(eq.data[1+total_var+l], 0);
1093 isl_blk_free(ctx, eq);
1094 out:
1095 free(index);
1096 free(elim_for);
1097 return bmap;
1100 static int n_pure_div_eq(struct isl_basic_map *bmap)
1102 int i, j;
1103 unsigned total;
1105 total = isl_space_dim(bmap->dim, isl_dim_all);
1106 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1107 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1108 --j;
1109 if (j < 0)
1110 break;
1111 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
1112 return 0;
1114 return i;
1117 /* Normalize divs that appear in equalities.
1119 * In particular, we assume that bmap contains some equalities
1120 * of the form
1122 * a x = m * e_i
1124 * and we want to replace the set of e_i by a minimal set and
1125 * such that the new e_i have a canonical representation in terms
1126 * of the vector x.
1127 * If any of the equalities involves more than one divs, then
1128 * we currently simply bail out.
1130 * Let us first additionally assume that all equalities involve
1131 * a div. The equalities then express modulo constraints on the
1132 * remaining variables and we can use "parameter compression"
1133 * to find a minimal set of constraints. The result is a transformation
1135 * x = T(x') = x_0 + G x'
1137 * with G a lower-triangular matrix with all elements below the diagonal
1138 * non-negative and smaller than the diagonal element on the same row.
1139 * We first normalize x_0 by making the same property hold in the affine
1140 * T matrix.
1141 * The rows i of G with a 1 on the diagonal do not impose any modulo
1142 * constraint and simply express x_i = x'_i.
1143 * For each of the remaining rows i, we introduce a div and a corresponding
1144 * equality. In particular
1146 * g_ii e_j = x_i - g_i(x')
1148 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1149 * corresponding div (if g_kk != 1).
1151 * If there are any equalities not involving any div, then we
1152 * first apply a variable compression on the variables x:
1154 * x = C x'' x'' = C_2 x
1156 * and perform the above parameter compression on A C instead of on A.
1157 * The resulting compression is then of the form
1159 * x'' = T(x') = x_0 + G x'
1161 * and in constructing the new divs and the corresponding equalities,
1162 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1163 * by the corresponding row from C_2.
1165 static struct isl_basic_map *normalize_divs(
1166 struct isl_basic_map *bmap, int *progress)
1168 int i, j, k;
1169 int total;
1170 int div_eq;
1171 struct isl_mat *B;
1172 struct isl_vec *d;
1173 struct isl_mat *T = NULL;
1174 struct isl_mat *C = NULL;
1175 struct isl_mat *C2 = NULL;
1176 isl_int v;
1177 int *pos = NULL;
1178 int dropped, needed;
1180 if (!bmap)
1181 return NULL;
1183 if (bmap->n_div == 0)
1184 return bmap;
1186 if (bmap->n_eq == 0)
1187 return bmap;
1189 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1190 return bmap;
1192 total = isl_space_dim(bmap->dim, isl_dim_all);
1193 div_eq = n_pure_div_eq(bmap);
1194 if (div_eq == 0)
1195 return bmap;
1197 if (div_eq < bmap->n_eq) {
1198 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1199 bmap->n_eq - div_eq, 0, 1 + total);
1200 C = isl_mat_variable_compression(B, &C2);
1201 if (!C || !C2)
1202 goto error;
1203 if (C->n_col == 0) {
1204 bmap = isl_basic_map_set_to_empty(bmap);
1205 isl_mat_free(C);
1206 isl_mat_free(C2);
1207 goto done;
1211 d = isl_vec_alloc(bmap->ctx, div_eq);
1212 if (!d)
1213 goto error;
1214 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1215 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1216 --j;
1217 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1219 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1221 if (C) {
1222 B = isl_mat_product(B, C);
1223 C = NULL;
1226 T = isl_mat_parameter_compression(B, d);
1227 if (!T)
1228 goto error;
1229 if (T->n_col == 0) {
1230 bmap = isl_basic_map_set_to_empty(bmap);
1231 isl_mat_free(C2);
1232 isl_mat_free(T);
1233 goto done;
1235 isl_int_init(v);
1236 for (i = 0; i < T->n_row - 1; ++i) {
1237 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1238 if (isl_int_is_zero(v))
1239 continue;
1240 isl_mat_col_submul(T, 0, v, 1 + i);
1242 isl_int_clear(v);
1243 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1244 if (!pos)
1245 goto error;
1246 /* We have to be careful because dropping equalities may reorder them */
1247 dropped = 0;
1248 for (j = bmap->n_div - 1; j >= 0; --j) {
1249 for (i = 0; i < bmap->n_eq; ++i)
1250 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1251 break;
1252 if (i < bmap->n_eq) {
1253 bmap = isl_basic_map_drop_div(bmap, j);
1254 isl_basic_map_drop_equality(bmap, i);
1255 ++dropped;
1258 pos[0] = 0;
1259 needed = 0;
1260 for (i = 1; i < T->n_row; ++i) {
1261 if (isl_int_is_one(T->row[i][i]))
1262 pos[i] = i;
1263 else
1264 needed++;
1266 if (needed > dropped) {
1267 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1268 needed, needed, 0);
1269 if (!bmap)
1270 goto error;
1272 for (i = 1; i < T->n_row; ++i) {
1273 if (isl_int_is_one(T->row[i][i]))
1274 continue;
1275 k = isl_basic_map_alloc_div(bmap);
1276 pos[i] = 1 + total + k;
1277 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1278 isl_int_set(bmap->div[k][0], T->row[i][i]);
1279 if (C2)
1280 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1281 else
1282 isl_int_set_si(bmap->div[k][1 + i], 1);
1283 for (j = 0; j < i; ++j) {
1284 if (isl_int_is_zero(T->row[i][j]))
1285 continue;
1286 if (pos[j] < T->n_row && C2)
1287 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1288 C2->row[pos[j]], 1 + total);
1289 else
1290 isl_int_neg(bmap->div[k][1 + pos[j]],
1291 T->row[i][j]);
1293 j = isl_basic_map_alloc_equality(bmap);
1294 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1295 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1297 free(pos);
1298 isl_mat_free(C2);
1299 isl_mat_free(T);
1301 if (progress)
1302 *progress = 1;
1303 done:
1304 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1306 return bmap;
1307 error:
1308 free(pos);
1309 isl_mat_free(C);
1310 isl_mat_free(C2);
1311 isl_mat_free(T);
1312 return bmap;
1315 static struct isl_basic_map *set_div_from_lower_bound(
1316 struct isl_basic_map *bmap, int div, int ineq)
1318 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1320 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1321 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1322 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1323 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1324 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1326 return bmap;
1329 /* Check whether it is ok to define a div based on an inequality.
1330 * To avoid the introduction of circular definitions of divs, we
1331 * do not allow such a definition if the resulting expression would refer to
1332 * any other undefined divs or if any known div is defined in
1333 * terms of the unknown div.
1335 static isl_bool ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1336 int div, int ineq)
1338 int j;
1339 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1341 /* Not defined in terms of unknown divs */
1342 for (j = 0; j < bmap->n_div; ++j) {
1343 if (div == j)
1344 continue;
1345 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1346 continue;
1347 if (isl_int_is_zero(bmap->div[j][0]))
1348 return isl_bool_false;
1351 /* No other div defined in terms of this one => avoid loops */
1352 for (j = 0; j < bmap->n_div; ++j) {
1353 if (div == j)
1354 continue;
1355 if (isl_int_is_zero(bmap->div[j][0]))
1356 continue;
1357 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1358 return isl_bool_false;
1361 return isl_bool_true;
1364 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1365 * be a better expression than the current one?
1367 * If we do not have any expression yet, then any expression would be better.
1368 * Otherwise we check if the last variable involved in the inequality
1369 * (disregarding the div that it would define) is in an earlier position
1370 * than the last variable involved in the current div expression.
1372 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1373 int div, int ineq)
1375 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1376 int last_div;
1377 int last_ineq;
1379 if (isl_int_is_zero(bmap->div[div][0]))
1380 return isl_bool_true;
1382 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1383 bmap->n_div - (div + 1)) >= 0)
1384 return isl_bool_false;
1386 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1387 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1388 total + bmap->n_div);
1390 return last_ineq < last_div;
1393 /* Given two constraints "k" and "l" that are opposite to each other,
1394 * except for the constant term, check if we can use them
1395 * to obtain an expression for one of the hitherto unknown divs or
1396 * a "better" expression for a div for which we already have an expression.
1397 * "sum" is the sum of the constant terms of the constraints.
1398 * If this sum is strictly smaller than the coefficient of one
1399 * of the divs, then this pair can be used define the div.
1400 * To avoid the introduction of circular definitions of divs, we
1401 * do not use the pair if the resulting expression would refer to
1402 * any other undefined divs or if any known div is defined in
1403 * terms of the unknown div.
1405 static struct isl_basic_map *check_for_div_constraints(
1406 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1408 int i;
1409 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1411 for (i = 0; i < bmap->n_div; ++i) {
1412 isl_bool set_div;
1414 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1415 continue;
1416 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1417 continue;
1418 set_div = better_div_constraint(bmap, i, k);
1419 if (set_div >= 0 && set_div)
1420 set_div = ok_to_set_div_from_bound(bmap, i, k);
1421 if (set_div < 0)
1422 return isl_basic_map_free(bmap);
1423 if (!set_div)
1424 break;
1425 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1426 bmap = set_div_from_lower_bound(bmap, i, k);
1427 else
1428 bmap = set_div_from_lower_bound(bmap, i, l);
1429 if (progress)
1430 *progress = 1;
1431 break;
1433 return bmap;
1436 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1437 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1439 struct isl_constraint_index ci;
1440 int k, l, h;
1441 unsigned total = isl_basic_map_total_dim(bmap);
1442 isl_int sum;
1444 if (!bmap || bmap->n_ineq <= 1)
1445 return bmap;
1447 if (create_constraint_index(&ci, bmap) < 0)
1448 return bmap;
1450 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1451 ci.index[h] = &bmap->ineq[0];
1452 for (k = 1; k < bmap->n_ineq; ++k) {
1453 h = hash_index(&ci, bmap, k);
1454 if (!ci.index[h]) {
1455 ci.index[h] = &bmap->ineq[k];
1456 continue;
1458 if (progress)
1459 *progress = 1;
1460 l = ci.index[h] - &bmap->ineq[0];
1461 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1462 swap_inequality(bmap, k, l);
1463 isl_basic_map_drop_inequality(bmap, k);
1464 --k;
1466 isl_int_init(sum);
1467 for (k = 0; k < bmap->n_ineq-1; ++k) {
1468 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1469 h = hash_index(&ci, bmap, k);
1470 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1471 if (!ci.index[h])
1472 continue;
1473 l = ci.index[h] - &bmap->ineq[0];
1474 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1475 if (isl_int_is_pos(sum)) {
1476 if (detect_divs)
1477 bmap = check_for_div_constraints(bmap, k, l,
1478 sum, progress);
1479 continue;
1481 if (isl_int_is_zero(sum)) {
1482 /* We need to break out of the loop after these
1483 * changes since the contents of the hash
1484 * will no longer be valid.
1485 * Plus, we probably we want to regauss first.
1487 if (progress)
1488 *progress = 1;
1489 isl_basic_map_drop_inequality(bmap, l);
1490 isl_basic_map_inequality_to_equality(bmap, k);
1491 } else
1492 bmap = isl_basic_map_set_to_empty(bmap);
1493 break;
1495 isl_int_clear(sum);
1497 constraint_index_free(&ci);
1498 return bmap;
1501 /* Detect all pairs of inequalities that form an equality.
1503 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1504 * Call it repeatedly while it is making progress.
1506 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1507 __isl_take isl_basic_map *bmap, int *progress)
1509 int duplicate;
1511 do {
1512 duplicate = 0;
1513 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1514 &duplicate, 0);
1515 if (progress && duplicate)
1516 *progress = 1;
1517 } while (duplicate);
1519 return bmap;
1522 /* Eliminate knowns divs from constraints where they appear with
1523 * a (positive or negative) unit coefficient.
1525 * That is, replace
1527 * floor(e/m) + f >= 0
1529 * by
1531 * e + m f >= 0
1533 * and
1535 * -floor(e/m) + f >= 0
1537 * by
1539 * -e + m f + m - 1 >= 0
1541 * The first conversion is valid because floor(e/m) >= -f is equivalent
1542 * to e/m >= -f because -f is an integral expression.
1543 * The second conversion follows from the fact that
1545 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1548 * Note that one of the div constraints may have been eliminated
1549 * due to being redundant with respect to the constraint that is
1550 * being modified by this function. The modified constraint may
1551 * no longer imply this div constraint, so we add it back to make
1552 * sure we do not lose any information.
1554 * We skip integral divs, i.e., those with denominator 1, as we would
1555 * risk eliminating the div from the div constraints. We do not need
1556 * to handle those divs here anyway since the div constraints will turn
1557 * out to form an equality and this equality can then be used to eliminate
1558 * the div from all constraints.
1560 static __isl_give isl_basic_map *eliminate_unit_divs(
1561 __isl_take isl_basic_map *bmap, int *progress)
1563 int i, j;
1564 isl_ctx *ctx;
1565 unsigned total;
1567 if (!bmap)
1568 return NULL;
1570 ctx = isl_basic_map_get_ctx(bmap);
1571 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1573 for (i = 0; i < bmap->n_div; ++i) {
1574 if (isl_int_is_zero(bmap->div[i][0]))
1575 continue;
1576 if (isl_int_is_one(bmap->div[i][0]))
1577 continue;
1578 for (j = 0; j < bmap->n_ineq; ++j) {
1579 int s;
1581 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1582 !isl_int_is_negone(bmap->ineq[j][total + i]))
1583 continue;
1585 *progress = 1;
1587 s = isl_int_sgn(bmap->ineq[j][total + i]);
1588 isl_int_set_si(bmap->ineq[j][total + i], 0);
1589 if (s < 0)
1590 isl_seq_combine(bmap->ineq[j],
1591 ctx->negone, bmap->div[i] + 1,
1592 bmap->div[i][0], bmap->ineq[j],
1593 total + bmap->n_div);
1594 else
1595 isl_seq_combine(bmap->ineq[j],
1596 ctx->one, bmap->div[i] + 1,
1597 bmap->div[i][0], bmap->ineq[j],
1598 total + bmap->n_div);
1599 if (s < 0) {
1600 isl_int_add(bmap->ineq[j][0],
1601 bmap->ineq[j][0], bmap->div[i][0]);
1602 isl_int_sub_ui(bmap->ineq[j][0],
1603 bmap->ineq[j][0], 1);
1606 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1607 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1608 return isl_basic_map_free(bmap);
1612 return bmap;
1615 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1617 int progress = 1;
1618 if (!bmap)
1619 return NULL;
1620 while (progress) {
1621 isl_bool empty;
1623 progress = 0;
1624 empty = isl_basic_map_plain_is_empty(bmap);
1625 if (empty < 0)
1626 return isl_basic_map_free(bmap);
1627 if (empty)
1628 break;
1629 bmap = isl_basic_map_normalize_constraints(bmap);
1630 bmap = reduce_div_coefficients(bmap);
1631 bmap = normalize_div_expressions(bmap);
1632 bmap = remove_duplicate_divs(bmap, &progress);
1633 bmap = eliminate_unit_divs(bmap, &progress);
1634 bmap = eliminate_divs_eq(bmap, &progress);
1635 bmap = eliminate_divs_ineq(bmap, &progress);
1636 bmap = isl_basic_map_gauss(bmap, &progress);
1637 /* requires equalities in normal form */
1638 bmap = normalize_divs(bmap, &progress);
1639 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1640 &progress, 1);
1641 if (bmap && progress)
1642 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1644 return bmap;
1647 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1649 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1653 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1654 isl_int *constraint, unsigned div)
1656 unsigned pos;
1658 if (!bmap)
1659 return isl_bool_error;
1661 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1663 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1664 int neg;
1665 isl_int_sub(bmap->div[div][1],
1666 bmap->div[div][1], bmap->div[div][0]);
1667 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1668 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1669 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1670 isl_int_add(bmap->div[div][1],
1671 bmap->div[div][1], bmap->div[div][0]);
1672 if (!neg)
1673 return isl_bool_false;
1674 if (isl_seq_first_non_zero(constraint+pos+1,
1675 bmap->n_div-div-1) != -1)
1676 return isl_bool_false;
1677 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1678 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1679 return isl_bool_false;
1680 if (isl_seq_first_non_zero(constraint+pos+1,
1681 bmap->n_div-div-1) != -1)
1682 return isl_bool_false;
1683 } else
1684 return isl_bool_false;
1686 return isl_bool_true;
1689 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1690 isl_int *constraint, unsigned div)
1692 return isl_basic_map_is_div_constraint(bset, constraint, div);
1696 /* If the only constraints a div d=floor(f/m)
1697 * appears in are its two defining constraints
1699 * f - m d >=0
1700 * -(f - (m - 1)) + m d >= 0
1702 * then it can safely be removed.
1704 static isl_bool div_is_redundant(struct isl_basic_map *bmap, int div)
1706 int i;
1707 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1709 for (i = 0; i < bmap->n_eq; ++i)
1710 if (!isl_int_is_zero(bmap->eq[i][pos]))
1711 return isl_bool_false;
1713 for (i = 0; i < bmap->n_ineq; ++i) {
1714 isl_bool red;
1716 if (isl_int_is_zero(bmap->ineq[i][pos]))
1717 continue;
1718 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1719 if (red < 0 || !red)
1720 return red;
1723 for (i = 0; i < bmap->n_div; ++i) {
1724 if (isl_int_is_zero(bmap->div[i][0]))
1725 continue;
1726 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1727 return isl_bool_false;
1730 return isl_bool_true;
1734 * Remove divs that don't occur in any of the constraints or other divs.
1735 * These can arise when dropping constraints from a basic map or
1736 * when the divs of a basic map have been temporarily aligned
1737 * with the divs of another basic map.
1739 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1741 int i;
1743 if (!bmap)
1744 return NULL;
1746 for (i = bmap->n_div-1; i >= 0; --i) {
1747 isl_bool redundant;
1749 redundant = div_is_redundant(bmap, i);
1750 if (redundant < 0)
1751 return isl_basic_map_free(bmap);
1752 if (!redundant)
1753 continue;
1754 bmap = isl_basic_map_drop_div(bmap, i);
1756 return bmap;
1759 /* Mark "bmap" as final, without checking for obviously redundant
1760 * integer divisions. This function should be used when "bmap"
1761 * is known not to involve any such integer divisions.
1763 __isl_give isl_basic_map *isl_basic_map_mark_final(
1764 __isl_take isl_basic_map *bmap)
1766 if (!bmap)
1767 return NULL;
1768 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1769 return bmap;
1772 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1774 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1776 bmap = remove_redundant_divs(bmap);
1777 bmap = isl_basic_map_mark_final(bmap);
1778 return bmap;
1781 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1783 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1786 struct isl_set *isl_set_finalize(struct isl_set *set)
1788 int i;
1790 if (!set)
1791 return NULL;
1792 for (i = 0; i < set->n; ++i) {
1793 set->p[i] = isl_basic_set_finalize(set->p[i]);
1794 if (!set->p[i])
1795 goto error;
1797 return set;
1798 error:
1799 isl_set_free(set);
1800 return NULL;
1803 struct isl_map *isl_map_finalize(struct isl_map *map)
1805 int i;
1807 if (!map)
1808 return NULL;
1809 for (i = 0; i < map->n; ++i) {
1810 map->p[i] = isl_basic_map_finalize(map->p[i]);
1811 if (!map->p[i])
1812 goto error;
1814 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1815 return map;
1816 error:
1817 isl_map_free(map);
1818 return NULL;
1822 /* Remove definition of any div that is defined in terms of the given variable.
1823 * The div itself is not removed. Functions such as
1824 * eliminate_divs_ineq depend on the other divs remaining in place.
1826 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1827 int pos)
1829 int i;
1831 if (!bmap)
1832 return NULL;
1834 for (i = 0; i < bmap->n_div; ++i) {
1835 if (isl_int_is_zero(bmap->div[i][0]))
1836 continue;
1837 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1838 continue;
1839 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1840 if (!bmap)
1841 return NULL;
1843 return bmap;
1846 /* Eliminate the specified variables from the constraints using
1847 * Fourier-Motzkin. The variables themselves are not removed.
1849 struct isl_basic_map *isl_basic_map_eliminate_vars(
1850 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1852 int d;
1853 int i, j, k;
1854 unsigned total;
1855 int need_gauss = 0;
1857 if (n == 0)
1858 return bmap;
1859 if (!bmap)
1860 return NULL;
1861 total = isl_basic_map_total_dim(bmap);
1863 bmap = isl_basic_map_cow(bmap);
1864 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1865 bmap = remove_dependent_vars(bmap, d);
1866 if (!bmap)
1867 return NULL;
1869 for (d = pos + n - 1;
1870 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1871 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1872 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1873 int n_lower, n_upper;
1874 if (!bmap)
1875 return NULL;
1876 for (i = 0; i < bmap->n_eq; ++i) {
1877 if (isl_int_is_zero(bmap->eq[i][1+d]))
1878 continue;
1879 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1880 isl_basic_map_drop_equality(bmap, i);
1881 need_gauss = 1;
1882 break;
1884 if (i < bmap->n_eq)
1885 continue;
1886 n_lower = 0;
1887 n_upper = 0;
1888 for (i = 0; i < bmap->n_ineq; ++i) {
1889 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1890 n_lower++;
1891 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1892 n_upper++;
1894 bmap = isl_basic_map_extend_constraints(bmap,
1895 0, n_lower * n_upper);
1896 if (!bmap)
1897 goto error;
1898 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1899 int last;
1900 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1901 continue;
1902 last = -1;
1903 for (j = 0; j < i; ++j) {
1904 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1905 continue;
1906 last = j;
1907 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1908 isl_int_sgn(bmap->ineq[j][1+d]))
1909 continue;
1910 k = isl_basic_map_alloc_inequality(bmap);
1911 if (k < 0)
1912 goto error;
1913 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1914 1+total);
1915 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1916 1+d, 1+total, NULL);
1918 isl_basic_map_drop_inequality(bmap, i);
1919 i = last + 1;
1921 if (n_lower > 0 && n_upper > 0) {
1922 bmap = isl_basic_map_normalize_constraints(bmap);
1923 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1924 NULL, 0);
1925 bmap = isl_basic_map_gauss(bmap, NULL);
1926 bmap = isl_basic_map_remove_redundancies(bmap);
1927 need_gauss = 0;
1928 if (!bmap)
1929 goto error;
1930 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1931 break;
1934 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1935 if (need_gauss)
1936 bmap = isl_basic_map_gauss(bmap, NULL);
1937 return bmap;
1938 error:
1939 isl_basic_map_free(bmap);
1940 return NULL;
1943 struct isl_basic_set *isl_basic_set_eliminate_vars(
1944 struct isl_basic_set *bset, unsigned pos, unsigned n)
1946 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1947 pos, n));
1950 /* Eliminate the specified n dimensions starting at first from the
1951 * constraints, without removing the dimensions from the space.
1952 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1953 * Otherwise, they are projected out and the original space is restored.
1955 __isl_give isl_basic_map *isl_basic_map_eliminate(
1956 __isl_take isl_basic_map *bmap,
1957 enum isl_dim_type type, unsigned first, unsigned n)
1959 isl_space *space;
1961 if (!bmap)
1962 return NULL;
1963 if (n == 0)
1964 return bmap;
1966 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1967 isl_die(bmap->ctx, isl_error_invalid,
1968 "index out of bounds", goto error);
1970 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1971 first += isl_basic_map_offset(bmap, type) - 1;
1972 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1973 return isl_basic_map_finalize(bmap);
1976 space = isl_basic_map_get_space(bmap);
1977 bmap = isl_basic_map_project_out(bmap, type, first, n);
1978 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1979 bmap = isl_basic_map_reset_space(bmap, space);
1980 return bmap;
1981 error:
1982 isl_basic_map_free(bmap);
1983 return NULL;
1986 __isl_give isl_basic_set *isl_basic_set_eliminate(
1987 __isl_take isl_basic_set *bset,
1988 enum isl_dim_type type, unsigned first, unsigned n)
1990 return isl_basic_map_eliminate(bset, type, first, n);
1993 /* Remove all constraints from "bmap" that reference any unknown local
1994 * variables (directly or indirectly).
1996 * Dropping all constraints on a local variable will make it redundant,
1997 * so it will get removed implicitly by
1998 * isl_basic_map_drop_constraints_involving_dims. Some other local
1999 * variables may also end up becoming redundant if they only appear
2000 * in constraints together with the unknown local variable.
2001 * Therefore, start over after calling
2002 * isl_basic_map_drop_constraints_involving_dims.
2004 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
2005 __isl_take isl_basic_map *bmap)
2007 isl_bool known;
2008 int i, n_div, o_div;
2010 known = isl_basic_map_divs_known(bmap);
2011 if (known < 0)
2012 return isl_basic_map_free(bmap);
2013 if (known)
2014 return bmap;
2016 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2017 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
2019 for (i = 0; i < n_div; ++i) {
2020 known = isl_basic_map_div_is_known(bmap, i);
2021 if (known < 0)
2022 return isl_basic_map_free(bmap);
2023 if (known)
2024 continue;
2025 bmap = remove_dependent_vars(bmap, o_div + i);
2026 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
2027 isl_dim_div, i, 1);
2028 if (!bmap)
2029 return NULL;
2030 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2031 i = -1;
2034 return bmap;
2037 /* Remove all constraints from "map" that reference any unknown local
2038 * variables (directly or indirectly).
2040 * Since constraints may get dropped from the basic maps,
2041 * they may no longer be disjoint from each other.
2043 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
2044 __isl_take isl_map *map)
2046 int i;
2047 isl_bool known;
2049 known = isl_map_divs_known(map);
2050 if (known < 0)
2051 return isl_map_free(map);
2052 if (known)
2053 return map;
2055 map = isl_map_cow(map);
2056 if (!map)
2057 return NULL;
2059 for (i = 0; i < map->n; ++i) {
2060 map->p[i] =
2061 isl_basic_map_drop_constraint_involving_unknown_divs(
2062 map->p[i]);
2063 if (!map->p[i])
2064 return isl_map_free(map);
2067 if (map->n > 1)
2068 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2070 return map;
2073 /* Don't assume equalities are in order, because align_divs
2074 * may have changed the order of the divs.
2076 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
2078 int d, i;
2079 unsigned total;
2081 total = isl_space_dim(bmap->dim, isl_dim_all);
2082 for (d = 0; d < total; ++d)
2083 elim[d] = -1;
2084 for (i = 0; i < bmap->n_eq; ++i) {
2085 for (d = total - 1; d >= 0; --d) {
2086 if (isl_int_is_zero(bmap->eq[i][1+d]))
2087 continue;
2088 elim[d] = i;
2089 break;
2094 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
2096 compute_elimination_index(bset_to_bmap(bset), elim);
2099 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2100 struct isl_basic_map *bmap, int *elim)
2102 int d;
2103 int copied = 0;
2104 unsigned total;
2106 total = isl_space_dim(bmap->dim, isl_dim_all);
2107 for (d = total - 1; d >= 0; --d) {
2108 if (isl_int_is_zero(src[1+d]))
2109 continue;
2110 if (elim[d] == -1)
2111 continue;
2112 if (!copied) {
2113 isl_seq_cpy(dst, src, 1 + total);
2114 copied = 1;
2116 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2118 return copied;
2121 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2122 struct isl_basic_set *bset, int *elim)
2124 return reduced_using_equalities(dst, src,
2125 bset_to_bmap(bset), elim);
2128 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
2129 struct isl_basic_set *bset, struct isl_basic_set *context)
2131 int i;
2132 int *elim;
2134 if (!bset || !context)
2135 goto error;
2137 if (context->n_eq == 0) {
2138 isl_basic_set_free(context);
2139 return bset;
2142 bset = isl_basic_set_cow(bset);
2143 if (!bset)
2144 goto error;
2146 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2147 if (!elim)
2148 goto error;
2149 set_compute_elimination_index(context, elim);
2150 for (i = 0; i < bset->n_eq; ++i)
2151 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2152 context, elim);
2153 for (i = 0; i < bset->n_ineq; ++i)
2154 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2155 context, elim);
2156 isl_basic_set_free(context);
2157 free(elim);
2158 bset = isl_basic_set_simplify(bset);
2159 bset = isl_basic_set_finalize(bset);
2160 return bset;
2161 error:
2162 isl_basic_set_free(bset);
2163 isl_basic_set_free(context);
2164 return NULL;
2167 /* For each inequality in "ineq" that is a shifted (more relaxed)
2168 * copy of an inequality in "context", mark the corresponding entry
2169 * in "row" with -1.
2170 * If an inequality only has a non-negative constant term, then
2171 * mark it as well.
2173 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2174 __isl_keep isl_basic_set *context, int *row)
2176 struct isl_constraint_index ci;
2177 int n_ineq;
2178 unsigned total;
2179 int k;
2181 if (!ineq || !context)
2182 return isl_stat_error;
2183 if (context->n_ineq == 0)
2184 return isl_stat_ok;
2185 if (setup_constraint_index(&ci, context) < 0)
2186 return isl_stat_error;
2188 n_ineq = isl_mat_rows(ineq);
2189 total = isl_mat_cols(ineq) - 1;
2190 for (k = 0; k < n_ineq; ++k) {
2191 int l;
2192 isl_bool redundant;
2194 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2195 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2196 row[k] = -1;
2197 continue;
2199 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2200 if (redundant < 0)
2201 goto error;
2202 if (!redundant)
2203 continue;
2204 row[k] = -1;
2206 constraint_index_free(&ci);
2207 return isl_stat_ok;
2208 error:
2209 constraint_index_free(&ci);
2210 return isl_stat_error;
2213 static struct isl_basic_set *remove_shifted_constraints(
2214 struct isl_basic_set *bset, struct isl_basic_set *context)
2216 struct isl_constraint_index ci;
2217 int k;
2219 if (!bset || !context)
2220 return bset;
2222 if (context->n_ineq == 0)
2223 return bset;
2224 if (setup_constraint_index(&ci, context) < 0)
2225 return bset;
2227 for (k = 0; k < bset->n_ineq; ++k) {
2228 isl_bool redundant;
2230 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2231 if (redundant < 0)
2232 goto error;
2233 if (!redundant)
2234 continue;
2235 bset = isl_basic_set_cow(bset);
2236 if (!bset)
2237 goto error;
2238 isl_basic_set_drop_inequality(bset, k);
2239 --k;
2241 constraint_index_free(&ci);
2242 return bset;
2243 error:
2244 constraint_index_free(&ci);
2245 return bset;
2248 /* Remove constraints from "bmap" that are identical to constraints
2249 * in "context" or that are more relaxed (greater constant term).
2251 * We perform the test for shifted copies on the pure constraints
2252 * in remove_shifted_constraints.
2254 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2255 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2257 isl_basic_set *bset, *bset_context;
2259 if (!bmap || !context)
2260 goto error;
2262 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2263 isl_basic_map_free(context);
2264 return bmap;
2267 context = isl_basic_map_align_divs(context, bmap);
2268 bmap = isl_basic_map_align_divs(bmap, context);
2270 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2271 bset_context = isl_basic_map_underlying_set(context);
2272 bset = remove_shifted_constraints(bset, bset_context);
2273 isl_basic_set_free(bset_context);
2275 bmap = isl_basic_map_overlying_set(bset, bmap);
2277 return bmap;
2278 error:
2279 isl_basic_map_free(bmap);
2280 isl_basic_map_free(context);
2281 return NULL;
2284 /* Does the (linear part of a) constraint "c" involve any of the "len"
2285 * "relevant" dimensions?
2287 static int is_related(isl_int *c, int len, int *relevant)
2289 int i;
2291 for (i = 0; i < len; ++i) {
2292 if (!relevant[i])
2293 continue;
2294 if (!isl_int_is_zero(c[i]))
2295 return 1;
2298 return 0;
2301 /* Drop constraints from "bmap" that do not involve any of
2302 * the dimensions marked "relevant".
2304 static __isl_give isl_basic_map *drop_unrelated_constraints(
2305 __isl_take isl_basic_map *bmap, int *relevant)
2307 int i, dim;
2309 dim = isl_basic_map_dim(bmap, isl_dim_all);
2310 for (i = 0; i < dim; ++i)
2311 if (!relevant[i])
2312 break;
2313 if (i >= dim)
2314 return bmap;
2316 for (i = bmap->n_eq - 1; i >= 0; --i)
2317 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2318 bmap = isl_basic_map_cow(bmap);
2319 if (isl_basic_map_drop_equality(bmap, i) < 0)
2320 return isl_basic_map_free(bmap);
2323 for (i = bmap->n_ineq - 1; i >= 0; --i)
2324 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2325 bmap = isl_basic_map_cow(bmap);
2326 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2327 return isl_basic_map_free(bmap);
2330 return bmap;
2333 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2335 * In particular, for any variable involved in the constraint,
2336 * find the actual group id from before and replace the group
2337 * of the corresponding variable by the minimal group of all
2338 * the variables involved in the constraint considered so far
2339 * (if this minimum is smaller) or replace the minimum by this group
2340 * (if the minimum is larger).
2342 * At the end, all the variables in "c" will (indirectly) point
2343 * to the minimal of the groups that they referred to originally.
2345 static void update_groups(int dim, int *group, isl_int *c)
2347 int j;
2348 int min = dim;
2350 for (j = 0; j < dim; ++j) {
2351 if (isl_int_is_zero(c[j]))
2352 continue;
2353 while (group[j] >= 0 && group[group[j]] != group[j])
2354 group[j] = group[group[j]];
2355 if (group[j] == min)
2356 continue;
2357 if (group[j] < min) {
2358 if (min >= 0 && min < dim)
2359 group[min] = group[j];
2360 min = group[j];
2361 } else
2362 group[group[j]] = min;
2366 /* Allocate an array of groups of variables, one for each variable
2367 * in "context", initialized to zero.
2369 static int *alloc_groups(__isl_keep isl_basic_set *context)
2371 isl_ctx *ctx;
2372 int dim;
2374 dim = isl_basic_set_dim(context, isl_dim_set);
2375 ctx = isl_basic_set_get_ctx(context);
2376 return isl_calloc_array(ctx, int, dim);
2379 /* Drop constraints from "bmap" that only involve variables that are
2380 * not related to any of the variables marked with a "-1" in "group".
2382 * We construct groups of variables that collect variables that
2383 * (indirectly) appear in some common constraint of "bmap".
2384 * Each group is identified by the first variable in the group,
2385 * except for the special group of variables that was already identified
2386 * in the input as -1 (or are related to those variables).
2387 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2388 * otherwise the group of i is the group of group[i].
2390 * We first initialize groups for the remaining variables.
2391 * Then we iterate over the constraints of "bmap" and update the
2392 * group of the variables in the constraint by the smallest group.
2393 * Finally, we resolve indirect references to groups by running over
2394 * the variables.
2396 * After computing the groups, we drop constraints that do not involve
2397 * any variables in the -1 group.
2399 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2400 __isl_take isl_basic_map *bmap, __isl_take int *group)
2402 int dim;
2403 int i;
2404 int last;
2406 if (!bmap)
2407 return NULL;
2409 dim = isl_basic_map_dim(bmap, isl_dim_all);
2411 last = -1;
2412 for (i = 0; i < dim; ++i)
2413 if (group[i] >= 0)
2414 last = group[i] = i;
2415 if (last < 0) {
2416 free(group);
2417 return bmap;
2420 for (i = 0; i < bmap->n_eq; ++i)
2421 update_groups(dim, group, bmap->eq[i] + 1);
2422 for (i = 0; i < bmap->n_ineq; ++i)
2423 update_groups(dim, group, bmap->ineq[i] + 1);
2425 for (i = 0; i < dim; ++i)
2426 if (group[i] >= 0)
2427 group[i] = group[group[i]];
2429 for (i = 0; i < dim; ++i)
2430 group[i] = group[i] == -1;
2432 bmap = drop_unrelated_constraints(bmap, group);
2434 free(group);
2435 return bmap;
2438 /* Drop constraints from "context" that are irrelevant for computing
2439 * the gist of "bset".
2441 * In particular, drop constraints in variables that are not related
2442 * to any of the variables involved in the constraints of "bset"
2443 * in the sense that there is no sequence of constraints that connects them.
2445 * We first mark all variables that appear in "bset" as belonging
2446 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2448 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2449 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2451 int *group;
2452 int dim;
2453 int i, j;
2455 if (!context || !bset)
2456 return isl_basic_set_free(context);
2458 group = alloc_groups(context);
2460 if (!group)
2461 return isl_basic_set_free(context);
2463 dim = isl_basic_set_dim(bset, isl_dim_set);
2464 for (i = 0; i < dim; ++i) {
2465 for (j = 0; j < bset->n_eq; ++j)
2466 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2467 break;
2468 if (j < bset->n_eq) {
2469 group[i] = -1;
2470 continue;
2472 for (j = 0; j < bset->n_ineq; ++j)
2473 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2474 break;
2475 if (j < bset->n_ineq)
2476 group[i] = -1;
2479 return isl_basic_map_drop_unrelated_constraints(context, group);
2482 /* Drop constraints from "context" that are irrelevant for computing
2483 * the gist of the inequalities "ineq".
2484 * Inequalities in "ineq" for which the corresponding element of row
2485 * is set to -1 have already been marked for removal and should be ignored.
2487 * In particular, drop constraints in variables that are not related
2488 * to any of the variables involved in "ineq"
2489 * in the sense that there is no sequence of constraints that connects them.
2491 * We first mark all variables that appear in "bset" as belonging
2492 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2494 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2495 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2497 int *group;
2498 int dim;
2499 int i, j, n;
2501 if (!context || !ineq)
2502 return isl_basic_set_free(context);
2504 group = alloc_groups(context);
2506 if (!group)
2507 return isl_basic_set_free(context);
2509 dim = isl_basic_set_dim(context, isl_dim_set);
2510 n = isl_mat_rows(ineq);
2511 for (i = 0; i < dim; ++i) {
2512 for (j = 0; j < n; ++j) {
2513 if (row[j] < 0)
2514 continue;
2515 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2516 break;
2518 if (j < n)
2519 group[i] = -1;
2522 return isl_basic_map_drop_unrelated_constraints(context, group);
2525 /* Do all "n" entries of "row" contain a negative value?
2527 static int all_neg(int *row, int n)
2529 int i;
2531 for (i = 0; i < n; ++i)
2532 if (row[i] >= 0)
2533 return 0;
2535 return 1;
2538 /* Update the inequalities in "bset" based on the information in "row"
2539 * and "tab".
2541 * In particular, the array "row" contains either -1, meaning that
2542 * the corresponding inequality of "bset" is redundant, or the index
2543 * of an inequality in "tab".
2545 * If the row entry is -1, then drop the inequality.
2546 * Otherwise, if the constraint is marked redundant in the tableau,
2547 * then drop the inequality. Similarly, if it is marked as an equality
2548 * in the tableau, then turn the inequality into an equality and
2549 * perform Gaussian elimination.
2551 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2552 __isl_keep int *row, struct isl_tab *tab)
2554 int i;
2555 unsigned n_ineq;
2556 unsigned n_eq;
2557 int found_equality = 0;
2559 if (!bset)
2560 return NULL;
2561 if (tab && tab->empty)
2562 return isl_basic_set_set_to_empty(bset);
2564 n_ineq = bset->n_ineq;
2565 for (i = n_ineq - 1; i >= 0; --i) {
2566 if (row[i] < 0) {
2567 if (isl_basic_set_drop_inequality(bset, i) < 0)
2568 return isl_basic_set_free(bset);
2569 continue;
2571 if (!tab)
2572 continue;
2573 n_eq = tab->n_eq;
2574 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2575 isl_basic_map_inequality_to_equality(bset, i);
2576 found_equality = 1;
2577 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2578 if (isl_basic_set_drop_inequality(bset, i) < 0)
2579 return isl_basic_set_free(bset);
2583 if (found_equality)
2584 bset = isl_basic_set_gauss(bset, NULL);
2585 bset = isl_basic_set_finalize(bset);
2586 return bset;
2589 /* Update the inequalities in "bset" based on the information in "row"
2590 * and "tab" and free all arguments (other than "bset").
2592 static __isl_give isl_basic_set *update_ineq_free(
2593 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2594 __isl_take isl_basic_set *context, __isl_take int *row,
2595 struct isl_tab *tab)
2597 isl_mat_free(ineq);
2598 isl_basic_set_free(context);
2600 bset = update_ineq(bset, row, tab);
2602 free(row);
2603 isl_tab_free(tab);
2604 return bset;
2607 /* Remove all information from bset that is redundant in the context
2608 * of context.
2609 * "ineq" contains the (possibly transformed) inequalities of "bset",
2610 * in the same order.
2611 * The (explicit) equalities of "bset" are assumed to have been taken
2612 * into account by the transformation such that only the inequalities
2613 * are relevant.
2614 * "context" is assumed not to be empty.
2616 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2617 * A value of -1 means that the inequality is obviously redundant and may
2618 * not even appear in "tab".
2620 * We first mark the inequalities of "bset"
2621 * that are obviously redundant with respect to some inequality in "context".
2622 * Then we remove those constraints from "context" that have become
2623 * irrelevant for computing the gist of "bset".
2624 * Note that this removal of constraints cannot be replaced by
2625 * a factorization because factors in "bset" may still be connected
2626 * to each other through constraints in "context".
2628 * If there are any inequalities left, we construct a tableau for
2629 * the context and then add the inequalities of "bset".
2630 * Before adding these inequalities, we freeze all constraints such that
2631 * they won't be considered redundant in terms of the constraints of "bset".
2632 * Then we detect all redundant constraints (among the
2633 * constraints that weren't frozen), first by checking for redundancy in the
2634 * the tableau and then by checking if replacing a constraint by its negation
2635 * would lead to an empty set. This last step is fairly expensive
2636 * and could be optimized by more reuse of the tableau.
2637 * Finally, we update bset according to the results.
2639 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2640 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2642 int i, r;
2643 int *row = NULL;
2644 isl_ctx *ctx;
2645 isl_basic_set *combined = NULL;
2646 struct isl_tab *tab = NULL;
2647 unsigned n_eq, context_ineq;
2648 unsigned total;
2650 if (!bset || !ineq || !context)
2651 goto error;
2653 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2654 isl_basic_set_free(context);
2655 isl_mat_free(ineq);
2656 return bset;
2659 ctx = isl_basic_set_get_ctx(context);
2660 row = isl_calloc_array(ctx, int, bset->n_ineq);
2661 if (!row)
2662 goto error;
2664 if (mark_shifted_constraints(ineq, context, row) < 0)
2665 goto error;
2666 if (all_neg(row, bset->n_ineq))
2667 return update_ineq_free(bset, ineq, context, row, NULL);
2669 context = drop_irrelevant_constraints_marked(context, ineq, row);
2670 if (!context)
2671 goto error;
2672 if (isl_basic_set_plain_is_universe(context))
2673 return update_ineq_free(bset, ineq, context, row, NULL);
2675 n_eq = context->n_eq;
2676 context_ineq = context->n_ineq;
2677 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2678 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2679 tab = isl_tab_from_basic_set(combined, 0);
2680 for (i = 0; i < context_ineq; ++i)
2681 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2682 goto error;
2683 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2684 goto error;
2685 r = context_ineq;
2686 for (i = 0; i < bset->n_ineq; ++i) {
2687 if (row[i] < 0)
2688 continue;
2689 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2690 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2691 goto error;
2692 row[i] = r++;
2694 if (isl_tab_detect_implicit_equalities(tab) < 0)
2695 goto error;
2696 if (isl_tab_detect_redundant(tab) < 0)
2697 goto error;
2698 total = isl_basic_set_total_dim(bset);
2699 for (i = bset->n_ineq - 1; i >= 0; --i) {
2700 isl_basic_set *test;
2701 int is_empty;
2703 if (row[i] < 0)
2704 continue;
2705 r = row[i];
2706 if (tab->con[n_eq + r].is_redundant)
2707 continue;
2708 test = isl_basic_set_dup(combined);
2709 if (isl_inequality_negate(test, r) < 0)
2710 test = isl_basic_set_free(test);
2711 test = isl_basic_set_update_from_tab(test, tab);
2712 is_empty = isl_basic_set_is_empty(test);
2713 isl_basic_set_free(test);
2714 if (is_empty < 0)
2715 goto error;
2716 if (is_empty)
2717 tab->con[n_eq + r].is_redundant = 1;
2719 bset = update_ineq_free(bset, ineq, context, row, tab);
2720 if (bset) {
2721 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2722 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2725 isl_basic_set_free(combined);
2726 return bset;
2727 error:
2728 free(row);
2729 isl_mat_free(ineq);
2730 isl_tab_free(tab);
2731 isl_basic_set_free(combined);
2732 isl_basic_set_free(context);
2733 isl_basic_set_free(bset);
2734 return NULL;
2737 /* Extract the inequalities of "bset" as an isl_mat.
2739 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2741 unsigned total;
2742 isl_ctx *ctx;
2743 isl_mat *ineq;
2745 if (!bset)
2746 return NULL;
2748 ctx = isl_basic_set_get_ctx(bset);
2749 total = isl_basic_set_total_dim(bset);
2750 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2751 0, 1 + total);
2753 return ineq;
2756 /* Remove all information from "bset" that is redundant in the context
2757 * of "context", for the case where both "bset" and "context" are
2758 * full-dimensional.
2760 static __isl_give isl_basic_set *uset_gist_uncompressed(
2761 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2763 isl_mat *ineq;
2765 ineq = extract_ineq(bset);
2766 return uset_gist_full(bset, ineq, context);
2769 /* Remove all information from "bset" that is redundant in the context
2770 * of "context", for the case where the combined equalities of
2771 * "bset" and "context" allow for a compression that can be obtained
2772 * by preapplication of "T".
2774 * "bset" itself is not transformed by "T". Instead, the inequalities
2775 * are extracted from "bset" and those are transformed by "T".
2776 * uset_gist_full then determines which of the transformed inequalities
2777 * are redundant with respect to the transformed "context" and removes
2778 * the corresponding inequalities from "bset".
2780 * After preapplying "T" to the inequalities, any common factor is
2781 * removed from the coefficients. If this results in a tightening
2782 * of the constant term, then the same tightening is applied to
2783 * the corresponding untransformed inequality in "bset".
2784 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2786 * g f'(x) + r >= 0
2788 * with 0 <= r < g, then it is equivalent to
2790 * f'(x) >= 0
2792 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2793 * subspace compressed by T since the latter would be transformed to
2795 * g f'(x) >= 0
2797 static __isl_give isl_basic_set *uset_gist_compressed(
2798 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2799 __isl_take isl_mat *T)
2801 isl_ctx *ctx;
2802 isl_mat *ineq;
2803 int i, n_row, n_col;
2804 isl_int rem;
2806 ineq = extract_ineq(bset);
2807 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2808 context = isl_basic_set_preimage(context, T);
2810 if (!ineq || !context)
2811 goto error;
2812 if (isl_basic_set_plain_is_empty(context)) {
2813 isl_mat_free(ineq);
2814 isl_basic_set_free(context);
2815 return isl_basic_set_set_to_empty(bset);
2818 ctx = isl_mat_get_ctx(ineq);
2819 n_row = isl_mat_rows(ineq);
2820 n_col = isl_mat_cols(ineq);
2821 isl_int_init(rem);
2822 for (i = 0; i < n_row; ++i) {
2823 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2824 if (isl_int_is_zero(ctx->normalize_gcd))
2825 continue;
2826 if (isl_int_is_one(ctx->normalize_gcd))
2827 continue;
2828 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2829 ctx->normalize_gcd, n_col - 1);
2830 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2831 isl_int_fdiv_q(ineq->row[i][0],
2832 ineq->row[i][0], ctx->normalize_gcd);
2833 if (isl_int_is_zero(rem))
2834 continue;
2835 bset = isl_basic_set_cow(bset);
2836 if (!bset)
2837 break;
2838 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2840 isl_int_clear(rem);
2842 return uset_gist_full(bset, ineq, context);
2843 error:
2844 isl_mat_free(ineq);
2845 isl_basic_set_free(context);
2846 isl_basic_set_free(bset);
2847 return NULL;
2850 /* Project "bset" onto the variables that are involved in "template".
2852 static __isl_give isl_basic_set *project_onto_involved(
2853 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2855 int i, n;
2857 if (!bset || !template)
2858 return isl_basic_set_free(bset);
2860 n = isl_basic_set_dim(template, isl_dim_set);
2862 for (i = 0; i < n; ++i) {
2863 isl_bool involved;
2865 involved = isl_basic_set_involves_dims(template,
2866 isl_dim_set, i, 1);
2867 if (involved < 0)
2868 return isl_basic_set_free(bset);
2869 if (involved)
2870 continue;
2871 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2874 return bset;
2877 /* Remove all information from bset that is redundant in the context
2878 * of context. In particular, equalities that are linear combinations
2879 * of those in context are removed. Then the inequalities that are
2880 * redundant in the context of the equalities and inequalities of
2881 * context are removed.
2883 * First of all, we drop those constraints from "context"
2884 * that are irrelevant for computing the gist of "bset".
2885 * Alternatively, we could factorize the intersection of "context" and "bset".
2887 * We first compute the intersection of the integer affine hulls
2888 * of "bset" and "context",
2889 * compute the gist inside this intersection and then reduce
2890 * the constraints with respect to the equalities of the context
2891 * that only involve variables already involved in the input.
2893 * If two constraints are mutually redundant, then uset_gist_full
2894 * will remove the second of those constraints. We therefore first
2895 * sort the constraints so that constraints not involving existentially
2896 * quantified variables are given precedence over those that do.
2897 * We have to perform this sorting before the variable compression,
2898 * because that may effect the order of the variables.
2900 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2901 __isl_take isl_basic_set *context)
2903 isl_mat *eq;
2904 isl_mat *T;
2905 isl_basic_set *aff;
2906 isl_basic_set *aff_context;
2907 unsigned total;
2909 if (!bset || !context)
2910 goto error;
2912 context = drop_irrelevant_constraints(context, bset);
2914 bset = isl_basic_set_detect_equalities(bset);
2915 aff = isl_basic_set_copy(bset);
2916 aff = isl_basic_set_plain_affine_hull(aff);
2917 context = isl_basic_set_detect_equalities(context);
2918 aff_context = isl_basic_set_copy(context);
2919 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2920 aff = isl_basic_set_intersect(aff, aff_context);
2921 if (!aff)
2922 goto error;
2923 if (isl_basic_set_plain_is_empty(aff)) {
2924 isl_basic_set_free(bset);
2925 isl_basic_set_free(context);
2926 return aff;
2928 bset = isl_basic_set_sort_constraints(bset);
2929 if (aff->n_eq == 0) {
2930 isl_basic_set_free(aff);
2931 return uset_gist_uncompressed(bset, context);
2933 total = isl_basic_set_total_dim(bset);
2934 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2935 eq = isl_mat_cow(eq);
2936 T = isl_mat_variable_compression(eq, NULL);
2937 isl_basic_set_free(aff);
2938 if (T && T->n_col == 0) {
2939 isl_mat_free(T);
2940 isl_basic_set_free(context);
2941 return isl_basic_set_set_to_empty(bset);
2944 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2945 aff_context = project_onto_involved(aff_context, bset);
2947 bset = uset_gist_compressed(bset, context, T);
2948 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2950 if (bset) {
2951 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2952 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2955 return bset;
2956 error:
2957 isl_basic_set_free(bset);
2958 isl_basic_set_free(context);
2959 return NULL;
2962 /* Return the number of equality constraints in "bmap" that involve
2963 * local variables. This function assumes that Gaussian elimination
2964 * has been applied to the equality constraints.
2966 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2968 int i;
2969 int total, n_div;
2971 if (!bmap)
2972 return -1;
2974 if (bmap->n_eq == 0)
2975 return 0;
2977 total = isl_basic_map_dim(bmap, isl_dim_all);
2978 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2979 total -= n_div;
2981 for (i = 0; i < bmap->n_eq; ++i)
2982 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2983 n_div) == -1)
2984 return i;
2986 return bmap->n_eq;
2989 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2990 * The constraints are assumed not to involve any local variables.
2992 static __isl_give isl_basic_map *basic_map_from_equalities(
2993 __isl_take isl_space *space, __isl_take isl_mat *eq)
2995 int i, k;
2996 isl_basic_map *bmap = NULL;
2998 if (!space || !eq)
2999 goto error;
3001 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
3002 isl_die(isl_space_get_ctx(space), isl_error_internal,
3003 "unexpected number of columns", goto error);
3005 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
3006 0, eq->n_row, 0);
3007 for (i = 0; i < eq->n_row; ++i) {
3008 k = isl_basic_map_alloc_equality(bmap);
3009 if (k < 0)
3010 goto error;
3011 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
3014 isl_space_free(space);
3015 isl_mat_free(eq);
3016 return bmap;
3017 error:
3018 isl_space_free(space);
3019 isl_mat_free(eq);
3020 isl_basic_map_free(bmap);
3021 return NULL;
3024 /* Construct and return a variable compression based on the equality
3025 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3026 * "n1" is the number of (initial) equality constraints in "bmap1"
3027 * that do involve local variables.
3028 * "n2" is the number of (initial) equality constraints in "bmap2"
3029 * that do involve local variables.
3030 * "total" is the total number of other variables.
3031 * This function assumes that Gaussian elimination
3032 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3033 * such that the equality constraints not involving local variables
3034 * are those that start at "n1" or "n2".
3036 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3037 * then simply compute the compression based on the equality constraints
3038 * in the other basic map.
3039 * Otherwise, combine the equality constraints from both into a new
3040 * basic map such that Gaussian elimination can be applied to this combination
3041 * and then construct a variable compression from the resulting
3042 * equality constraints.
3044 static __isl_give isl_mat *combined_variable_compression(
3045 __isl_keep isl_basic_map *bmap1, int n1,
3046 __isl_keep isl_basic_map *bmap2, int n2, int total)
3048 isl_ctx *ctx;
3049 isl_mat *E1, *E2, *V;
3050 isl_basic_map *bmap;
3052 ctx = isl_basic_map_get_ctx(bmap1);
3053 if (bmap1->n_eq == n1) {
3054 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3055 n2, bmap2->n_eq - n2, 0, 1 + total);
3056 return isl_mat_variable_compression(E2, NULL);
3058 if (bmap2->n_eq == n2) {
3059 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3060 n1, bmap1->n_eq - n1, 0, 1 + total);
3061 return isl_mat_variable_compression(E1, NULL);
3063 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3064 n1, bmap1->n_eq - n1, 0, 1 + total);
3065 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3066 n2, bmap2->n_eq - n2, 0, 1 + total);
3067 E1 = isl_mat_concat(E1, E2);
3068 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3069 bmap = isl_basic_map_gauss(bmap, NULL);
3070 if (!bmap)
3071 return NULL;
3072 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3073 V = isl_mat_variable_compression(E1, NULL);
3074 isl_basic_map_free(bmap);
3076 return V;
3079 /* Extract the stride constraints from "bmap", compressed
3080 * with respect to both the stride constraints in "context" and
3081 * the remaining equality constraints in both "bmap" and "context".
3082 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3083 * "context_n_eq" is the number of (initial) stride constraints in "context".
3085 * Let x be all variables in "bmap" (and "context") other than the local
3086 * variables. First compute a variable compression
3088 * x = V x'
3090 * based on the non-stride equality constraints in "bmap" and "context".
3091 * Consider the stride constraints of "context",
3093 * A(x) + B(y) = 0
3095 * with y the local variables and plug in the variable compression,
3096 * resulting in
3098 * A(V x') + B(y) = 0
3100 * Use these constraints to compute a parameter compression on x'
3102 * x' = T x''
3104 * Now consider the stride constraints of "bmap"
3106 * C(x) + D(y) = 0
3108 * and plug in x = V*T x''.
3109 * That is, return A = [C*V*T D].
3111 static __isl_give isl_mat *extract_compressed_stride_constraints(
3112 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3113 __isl_keep isl_basic_map *context, int context_n_eq)
3115 int total, n_div;
3116 isl_ctx *ctx;
3117 isl_mat *A, *B, *T, *V;
3119 total = isl_basic_map_dim(context, isl_dim_all);
3120 n_div = isl_basic_map_dim(context, isl_dim_div);
3121 total -= n_div;
3123 ctx = isl_basic_map_get_ctx(bmap);
3125 V = combined_variable_compression(bmap, bmap_n_eq,
3126 context, context_n_eq, total);
3128 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3129 B = isl_mat_sub_alloc6(ctx, context->eq,
3130 0, context_n_eq, 1 + total, n_div);
3131 A = isl_mat_product(A, isl_mat_copy(V));
3132 T = isl_mat_parameter_compression_ext(A, B);
3133 T = isl_mat_product(V, T);
3135 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3136 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3138 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3139 0, bmap_n_eq, 0, 1 + total + n_div);
3140 A = isl_mat_product(A, T);
3142 return A;
3145 /* Remove the prime factors from *g that have an exponent that
3146 * is strictly smaller than the exponent in "c".
3147 * All exponents in *g are known to be smaller than or equal
3148 * to those in "c".
3150 * That is, if *g is equal to
3152 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3154 * and "c" is equal to
3156 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3158 * then update *g to
3160 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3161 * p_n^{e_n * (e_n = f_n)}
3163 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3164 * neither does the gcd of *g and c / *g.
3165 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3166 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3167 * Dividing *g by this gcd therefore strictly reduces the exponent
3168 * of the prime factors that need to be removed, while leaving the
3169 * other prime factors untouched.
3170 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3171 * removes all undesired factors, without removing any others.
3173 static void remove_incomplete_powers(isl_int *g, isl_int c)
3175 isl_int t;
3177 isl_int_init(t);
3178 for (;;) {
3179 isl_int_divexact(t, c, *g);
3180 isl_int_gcd(t, t, *g);
3181 if (isl_int_is_one(t))
3182 break;
3183 isl_int_divexact(*g, *g, t);
3185 isl_int_clear(t);
3188 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3189 * of the same stride constraints in a compressed space that exploits
3190 * all equalities in the context and the other equalities in "bmap".
3192 * If the stride constraints of "bmap" are of the form
3194 * C(x) + D(y) = 0
3196 * then A is of the form
3198 * B(x') + D(y) = 0
3200 * If any of these constraints involves only a single local variable y,
3201 * then the constraint appears as
3203 * f(x) + m y_i = 0
3205 * in "bmap" and as
3207 * h(x') + m y_i = 0
3209 * in "A".
3211 * Let g be the gcd of m and the coefficients of h.
3212 * Then, in particular, g is a divisor of the coefficients of h and
3214 * f(x) = h(x')
3216 * is known to be a multiple of g.
3217 * If some prime factor in m appears with the same exponent in g,
3218 * then it can be removed from m because f(x) is already known
3219 * to be a multiple of g and therefore in particular of this power
3220 * of the prime factors.
3221 * Prime factors that appear with a smaller exponent in g cannot
3222 * be removed from m.
3223 * Let g' be the divisor of g containing all prime factors that
3224 * appear with the same exponent in m and g, then
3226 * f(x) + m y_i = 0
3228 * can be replaced by
3230 * f(x) + m/g' y_i' = 0
3232 * Note that (if g' != 1) this changes the explicit representation
3233 * of y_i to that of y_i', so the integer division at position i
3234 * is marked unknown and later recomputed by a call to
3235 * isl_basic_map_gauss.
3237 static __isl_give isl_basic_map *reduce_stride_constraints(
3238 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3240 int i;
3241 int total, n_div;
3242 int any = 0;
3243 isl_int gcd;
3245 if (!bmap || !A)
3246 return isl_basic_map_free(bmap);
3248 total = isl_basic_map_dim(bmap, isl_dim_all);
3249 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3250 total -= n_div;
3252 isl_int_init(gcd);
3253 for (i = 0; i < n; ++i) {
3254 int div;
3256 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3257 if (div < 0)
3258 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3259 "equality constraints modified unexpectedly",
3260 goto error);
3261 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3262 n_div - div - 1) != -1)
3263 continue;
3264 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3265 goto error;
3266 if (isl_int_is_one(gcd))
3267 continue;
3268 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3269 if (isl_int_is_one(gcd))
3270 continue;
3271 isl_int_divexact(bmap->eq[i][1 + total + div],
3272 bmap->eq[i][1 + total + div], gcd);
3273 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3274 if (!bmap)
3275 goto error;
3276 any = 1;
3278 isl_int_clear(gcd);
3280 if (any)
3281 bmap = isl_basic_map_gauss(bmap, NULL);
3283 return bmap;
3284 error:
3285 isl_int_clear(gcd);
3286 isl_basic_map_free(bmap);
3287 return NULL;
3290 /* Simplify the stride constraints in "bmap" based on
3291 * the remaining equality constraints in "bmap" and all equality
3292 * constraints in "context".
3293 * Only do this if both "bmap" and "context" have stride constraints.
3295 * First extract a copy of the stride constraints in "bmap" in a compressed
3296 * space exploiting all the other equality constraints and then
3297 * use this compressed copy to simplify the original stride constraints.
3299 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3300 __isl_keep isl_basic_map *context)
3302 int bmap_n_eq, context_n_eq;
3303 isl_mat *A;
3305 if (!bmap || !context)
3306 return isl_basic_map_free(bmap);
3308 bmap_n_eq = n_div_eq(bmap);
3309 context_n_eq = n_div_eq(context);
3311 if (bmap_n_eq < 0 || context_n_eq < 0)
3312 return isl_basic_map_free(bmap);
3313 if (bmap_n_eq == 0 || context_n_eq == 0)
3314 return bmap;
3316 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3317 context, context_n_eq);
3318 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3320 isl_mat_free(A);
3322 return bmap;
3325 /* Return a basic map that has the same intersection with "context" as "bmap"
3326 * and that is as "simple" as possible.
3328 * The core computation is performed on the pure constraints.
3329 * When we add back the meaning of the integer divisions, we need
3330 * to (re)introduce the div constraints. If we happen to have
3331 * discovered that some of these integer divisions are equal to
3332 * some affine combination of other variables, then these div
3333 * constraints may end up getting simplified in terms of the equalities,
3334 * resulting in extra inequalities on the other variables that
3335 * may have been removed already or that may not even have been
3336 * part of the input. We try and remove those constraints of
3337 * this form that are most obviously redundant with respect to
3338 * the context. We also remove those div constraints that are
3339 * redundant with respect to the other constraints in the result.
3341 * The stride constraints among the equality constraints in "bmap" are
3342 * also simplified with respecting to the other equality constraints
3343 * in "bmap" and with respect to all equality constraints in "context".
3345 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3346 struct isl_basic_map *context)
3348 isl_basic_set *bset, *eq;
3349 isl_basic_map *eq_bmap;
3350 unsigned total, n_div, extra, n_eq, n_ineq;
3352 if (!bmap || !context)
3353 goto error;
3355 if (isl_basic_map_plain_is_universe(bmap)) {
3356 isl_basic_map_free(context);
3357 return bmap;
3359 if (isl_basic_map_plain_is_empty(context)) {
3360 isl_space *space = isl_basic_map_get_space(bmap);
3361 isl_basic_map_free(bmap);
3362 isl_basic_map_free(context);
3363 return isl_basic_map_universe(space);
3365 if (isl_basic_map_plain_is_empty(bmap)) {
3366 isl_basic_map_free(context);
3367 return bmap;
3370 bmap = isl_basic_map_remove_redundancies(bmap);
3371 context = isl_basic_map_remove_redundancies(context);
3372 if (!context)
3373 goto error;
3375 context = isl_basic_map_align_divs(context, bmap);
3376 n_div = isl_basic_map_dim(context, isl_dim_div);
3377 total = isl_basic_map_dim(bmap, isl_dim_all);
3378 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3380 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3381 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3382 bset = uset_gist(bset,
3383 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3384 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3386 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3387 isl_basic_set_plain_is_empty(bset)) {
3388 isl_basic_map_free(context);
3389 return isl_basic_map_overlying_set(bset, bmap);
3392 n_eq = bset->n_eq;
3393 n_ineq = bset->n_ineq;
3394 eq = isl_basic_set_copy(bset);
3395 eq = isl_basic_set_cow(eq);
3396 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3397 eq = isl_basic_set_free(eq);
3398 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3399 bset = isl_basic_set_free(bset);
3401 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3402 eq_bmap = gist_strides(eq_bmap, context);
3403 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3404 bmap = isl_basic_map_overlying_set(bset, bmap);
3405 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3406 bmap = isl_basic_map_remove_redundancies(bmap);
3408 return bmap;
3409 error:
3410 isl_basic_map_free(bmap);
3411 isl_basic_map_free(context);
3412 return NULL;
3416 * Assumes context has no implicit divs.
3418 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3419 __isl_take isl_basic_map *context)
3421 int i;
3423 if (!map || !context)
3424 goto error;
3426 if (isl_basic_map_plain_is_empty(context)) {
3427 isl_space *space = isl_map_get_space(map);
3428 isl_map_free(map);
3429 isl_basic_map_free(context);
3430 return isl_map_universe(space);
3433 context = isl_basic_map_remove_redundancies(context);
3434 map = isl_map_cow(map);
3435 if (!map || !context)
3436 goto error;
3437 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3438 map = isl_map_compute_divs(map);
3439 if (!map)
3440 goto error;
3441 for (i = map->n - 1; i >= 0; --i) {
3442 map->p[i] = isl_basic_map_gist(map->p[i],
3443 isl_basic_map_copy(context));
3444 if (!map->p[i])
3445 goto error;
3446 if (isl_basic_map_plain_is_empty(map->p[i])) {
3447 isl_basic_map_free(map->p[i]);
3448 if (i != map->n - 1)
3449 map->p[i] = map->p[map->n - 1];
3450 map->n--;
3453 isl_basic_map_free(context);
3454 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3455 return map;
3456 error:
3457 isl_map_free(map);
3458 isl_basic_map_free(context);
3459 return NULL;
3462 /* Drop all inequalities from "bmap" that also appear in "context".
3463 * "context" is assumed to have only known local variables and
3464 * the initial local variables of "bmap" are assumed to be the same
3465 * as those of "context".
3466 * The constraints of both "bmap" and "context" are assumed
3467 * to have been sorted using isl_basic_map_sort_constraints.
3469 * Run through the inequality constraints of "bmap" and "context"
3470 * in sorted order.
3471 * If a constraint of "bmap" involves variables not in "context",
3472 * then it cannot appear in "context".
3473 * If a matching constraint is found, it is removed from "bmap".
3475 static __isl_give isl_basic_map *drop_inequalities(
3476 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3478 int i1, i2;
3479 unsigned total, extra;
3481 if (!bmap || !context)
3482 return isl_basic_map_free(bmap);
3484 total = isl_basic_map_total_dim(context);
3485 extra = isl_basic_map_total_dim(bmap) - total;
3487 i1 = bmap->n_ineq - 1;
3488 i2 = context->n_ineq - 1;
3489 while (bmap && i1 >= 0 && i2 >= 0) {
3490 int cmp;
3492 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3493 extra) != -1) {
3494 --i1;
3495 continue;
3497 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3498 context->ineq[i2]);
3499 if (cmp < 0) {
3500 --i2;
3501 continue;
3503 if (cmp > 0) {
3504 --i1;
3505 continue;
3507 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3508 bmap = isl_basic_map_cow(bmap);
3509 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3510 bmap = isl_basic_map_free(bmap);
3512 --i1;
3513 --i2;
3516 return bmap;
3519 /* Drop all equalities from "bmap" that also appear in "context".
3520 * "context" is assumed to have only known local variables and
3521 * the initial local variables of "bmap" are assumed to be the same
3522 * as those of "context".
3524 * Run through the equality constraints of "bmap" and "context"
3525 * in sorted order.
3526 * If a constraint of "bmap" involves variables not in "context",
3527 * then it cannot appear in "context".
3528 * If a matching constraint is found, it is removed from "bmap".
3530 static __isl_give isl_basic_map *drop_equalities(
3531 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3533 int i1, i2;
3534 unsigned total, extra;
3536 if (!bmap || !context)
3537 return isl_basic_map_free(bmap);
3539 total = isl_basic_map_total_dim(context);
3540 extra = isl_basic_map_total_dim(bmap) - total;
3542 i1 = bmap->n_eq - 1;
3543 i2 = context->n_eq - 1;
3545 while (bmap && i1 >= 0 && i2 >= 0) {
3546 int last1, last2;
3548 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3549 extra) != -1)
3550 break;
3551 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3552 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3553 if (last1 > last2) {
3554 --i2;
3555 continue;
3557 if (last1 < last2) {
3558 --i1;
3559 continue;
3561 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3562 bmap = isl_basic_map_cow(bmap);
3563 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3564 bmap = isl_basic_map_free(bmap);
3566 --i1;
3567 --i2;
3570 return bmap;
3573 /* Remove the constraints in "context" from "bmap".
3574 * "context" is assumed to have explicit representations
3575 * for all local variables.
3577 * First align the divs of "bmap" to those of "context" and
3578 * sort the constraints. Then drop all constraints from "bmap"
3579 * that appear in "context".
3581 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3582 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3584 isl_bool done, known;
3586 done = isl_basic_map_plain_is_universe(context);
3587 if (done == isl_bool_false)
3588 done = isl_basic_map_plain_is_universe(bmap);
3589 if (done == isl_bool_false)
3590 done = isl_basic_map_plain_is_empty(context);
3591 if (done == isl_bool_false)
3592 done = isl_basic_map_plain_is_empty(bmap);
3593 if (done < 0)
3594 goto error;
3595 if (done) {
3596 isl_basic_map_free(context);
3597 return bmap;
3599 known = isl_basic_map_divs_known(context);
3600 if (known < 0)
3601 goto error;
3602 if (!known)
3603 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3604 "context has unknown divs", goto error);
3606 bmap = isl_basic_map_align_divs(bmap, context);
3607 bmap = isl_basic_map_gauss(bmap, NULL);
3608 bmap = isl_basic_map_sort_constraints(bmap);
3609 context = isl_basic_map_sort_constraints(context);
3611 bmap = drop_inequalities(bmap, context);
3612 bmap = drop_equalities(bmap, context);
3614 isl_basic_map_free(context);
3615 bmap = isl_basic_map_finalize(bmap);
3616 return bmap;
3617 error:
3618 isl_basic_map_free(bmap);
3619 isl_basic_map_free(context);
3620 return NULL;
3623 /* Replace "map" by the disjunct at position "pos" and free "context".
3625 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3626 int pos, __isl_take isl_basic_map *context)
3628 isl_basic_map *bmap;
3630 bmap = isl_basic_map_copy(map->p[pos]);
3631 isl_map_free(map);
3632 isl_basic_map_free(context);
3633 return isl_map_from_basic_map(bmap);
3636 /* Remove the constraints in "context" from "map".
3637 * If any of the disjuncts in the result turns out to be the universe,
3638 * then return this universe.
3639 * "context" is assumed to have explicit representations
3640 * for all local variables.
3642 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3643 __isl_take isl_basic_map *context)
3645 int i;
3646 isl_bool univ, known;
3648 univ = isl_basic_map_plain_is_universe(context);
3649 if (univ < 0)
3650 goto error;
3651 if (univ) {
3652 isl_basic_map_free(context);
3653 return map;
3655 known = isl_basic_map_divs_known(context);
3656 if (known < 0)
3657 goto error;
3658 if (!known)
3659 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3660 "context has unknown divs", goto error);
3662 map = isl_map_cow(map);
3663 if (!map)
3664 goto error;
3665 for (i = 0; i < map->n; ++i) {
3666 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3667 isl_basic_map_copy(context));
3668 univ = isl_basic_map_plain_is_universe(map->p[i]);
3669 if (univ < 0)
3670 goto error;
3671 if (univ && map->n > 1)
3672 return replace_by_disjunct(map, i, context);
3675 isl_basic_map_free(context);
3676 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3677 if (map->n > 1)
3678 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3679 return map;
3680 error:
3681 isl_map_free(map);
3682 isl_basic_map_free(context);
3683 return NULL;
3686 /* Replace "map" by a universe map in the same space and free "drop".
3688 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3689 __isl_take isl_map *drop)
3691 isl_map *res;
3693 res = isl_map_universe(isl_map_get_space(map));
3694 isl_map_free(map);
3695 isl_map_free(drop);
3696 return res;
3699 /* Return a map that has the same intersection with "context" as "map"
3700 * and that is as "simple" as possible.
3702 * If "map" is already the universe, then we cannot make it any simpler.
3703 * Similarly, if "context" is the universe, then we cannot exploit it
3704 * to simplify "map"
3705 * If "map" and "context" are identical to each other, then we can
3706 * return the corresponding universe.
3708 * If either "map" or "context" consists of multiple disjuncts,
3709 * then check if "context" happens to be a subset of "map",
3710 * in which case all constraints can be removed.
3711 * In case of multiple disjuncts, the standard procedure
3712 * may not be able to detect that all constraints can be removed.
3714 * If none of these cases apply, we have to work a bit harder.
3715 * During this computation, we make use of a single disjunct context,
3716 * so if the original context consists of more than one disjunct
3717 * then we need to approximate the context by a single disjunct set.
3718 * Simply taking the simple hull may drop constraints that are
3719 * only implicitly available in each disjunct. We therefore also
3720 * look for constraints among those defining "map" that are valid
3721 * for the context. These can then be used to simplify away
3722 * the corresponding constraints in "map".
3724 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3725 __isl_take isl_map *context)
3727 int equal;
3728 int is_universe;
3729 int single_disjunct_map, single_disjunct_context;
3730 isl_bool subset;
3731 isl_basic_map *hull;
3733 is_universe = isl_map_plain_is_universe(map);
3734 if (is_universe >= 0 && !is_universe)
3735 is_universe = isl_map_plain_is_universe(context);
3736 if (is_universe < 0)
3737 goto error;
3738 if (is_universe) {
3739 isl_map_free(context);
3740 return map;
3743 equal = isl_map_plain_is_equal(map, context);
3744 if (equal < 0)
3745 goto error;
3746 if (equal)
3747 return replace_by_universe(map, context);
3749 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3750 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3751 if (!single_disjunct_map || !single_disjunct_context) {
3752 subset = isl_map_is_subset(context, map);
3753 if (subset < 0)
3754 goto error;
3755 if (subset)
3756 return replace_by_universe(map, context);
3759 context = isl_map_compute_divs(context);
3760 if (!context)
3761 goto error;
3762 if (single_disjunct_context) {
3763 hull = isl_map_simple_hull(context);
3764 } else {
3765 isl_ctx *ctx;
3766 isl_map_list *list;
3768 ctx = isl_map_get_ctx(map);
3769 list = isl_map_list_alloc(ctx, 2);
3770 list = isl_map_list_add(list, isl_map_copy(context));
3771 list = isl_map_list_add(list, isl_map_copy(map));
3772 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3773 list);
3775 return isl_map_gist_basic_map(map, hull);
3776 error:
3777 isl_map_free(map);
3778 isl_map_free(context);
3779 return NULL;
3782 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3783 __isl_take isl_map *context)
3785 return isl_map_align_params_map_map_and(map, context, &map_gist);
3788 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3789 struct isl_basic_set *context)
3791 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3792 bset_to_bmap(context)));
3795 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3796 __isl_take isl_basic_set *context)
3798 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3799 bset_to_bmap(context)));
3802 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3803 __isl_take isl_basic_set *context)
3805 isl_space *space = isl_set_get_space(set);
3806 isl_basic_set *dom_context = isl_basic_set_universe(space);
3807 dom_context = isl_basic_set_intersect_params(dom_context, context);
3808 return isl_set_gist_basic_set(set, dom_context);
3811 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3812 __isl_take isl_set *context)
3814 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3817 /* Compute the gist of "bmap" with respect to the constraints "context"
3818 * on the domain.
3820 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3821 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3823 isl_space *space = isl_basic_map_get_space(bmap);
3824 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3826 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3827 return isl_basic_map_gist(bmap, bmap_context);
3830 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3831 __isl_take isl_set *context)
3833 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3834 map_context = isl_map_intersect_domain(map_context, context);
3835 return isl_map_gist(map, map_context);
3838 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3839 __isl_take isl_set *context)
3841 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3842 map_context = isl_map_intersect_range(map_context, context);
3843 return isl_map_gist(map, map_context);
3846 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3847 __isl_take isl_set *context)
3849 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3850 map_context = isl_map_intersect_params(map_context, context);
3851 return isl_map_gist(map, map_context);
3854 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3855 __isl_take isl_set *context)
3857 return isl_map_gist_params(set, context);
3860 /* Quick check to see if two basic maps are disjoint.
3861 * In particular, we reduce the equalities and inequalities of
3862 * one basic map in the context of the equalities of the other
3863 * basic map and check if we get a contradiction.
3865 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3866 __isl_keep isl_basic_map *bmap2)
3868 struct isl_vec *v = NULL;
3869 int *elim = NULL;
3870 unsigned total;
3871 int i;
3873 if (!bmap1 || !bmap2)
3874 return isl_bool_error;
3875 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3876 return isl_bool_error);
3877 if (bmap1->n_div || bmap2->n_div)
3878 return isl_bool_false;
3879 if (!bmap1->n_eq && !bmap2->n_eq)
3880 return isl_bool_false;
3882 total = isl_space_dim(bmap1->dim, isl_dim_all);
3883 if (total == 0)
3884 return isl_bool_false;
3885 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3886 if (!v)
3887 goto error;
3888 elim = isl_alloc_array(bmap1->ctx, int, total);
3889 if (!elim)
3890 goto error;
3891 compute_elimination_index(bmap1, elim);
3892 for (i = 0; i < bmap2->n_eq; ++i) {
3893 int reduced;
3894 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3895 bmap1, elim);
3896 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3897 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3898 goto disjoint;
3900 for (i = 0; i < bmap2->n_ineq; ++i) {
3901 int reduced;
3902 reduced = reduced_using_equalities(v->block.data,
3903 bmap2->ineq[i], bmap1, elim);
3904 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3905 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3906 goto disjoint;
3908 compute_elimination_index(bmap2, elim);
3909 for (i = 0; i < bmap1->n_ineq; ++i) {
3910 int reduced;
3911 reduced = reduced_using_equalities(v->block.data,
3912 bmap1->ineq[i], bmap2, elim);
3913 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3914 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3915 goto disjoint;
3917 isl_vec_free(v);
3918 free(elim);
3919 return isl_bool_false;
3920 disjoint:
3921 isl_vec_free(v);
3922 free(elim);
3923 return isl_bool_true;
3924 error:
3925 isl_vec_free(v);
3926 free(elim);
3927 return isl_bool_error;
3930 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3931 __isl_keep isl_basic_set *bset2)
3933 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3934 bset_to_bmap(bset2));
3937 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3939 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3940 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3941 __isl_keep isl_basic_map *bmap2))
3943 int i, j;
3945 if (!map1 || !map2)
3946 return isl_bool_error;
3948 for (i = 0; i < map1->n; ++i) {
3949 for (j = 0; j < map2->n; ++j) {
3950 isl_bool d = test(map1->p[i], map2->p[j]);
3951 if (d != isl_bool_true)
3952 return d;
3956 return isl_bool_true;
3959 /* Are "map1" and "map2" obviously disjoint, based on information
3960 * that can be derived without looking at the individual basic maps?
3962 * In particular, if one of them is empty or if they live in different spaces
3963 * (ignoring parameters), then they are clearly disjoint.
3965 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3966 __isl_keep isl_map *map2)
3968 isl_bool disjoint;
3969 isl_bool match;
3971 if (!map1 || !map2)
3972 return isl_bool_error;
3974 disjoint = isl_map_plain_is_empty(map1);
3975 if (disjoint < 0 || disjoint)
3976 return disjoint;
3978 disjoint = isl_map_plain_is_empty(map2);
3979 if (disjoint < 0 || disjoint)
3980 return disjoint;
3982 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3983 map2->dim, isl_dim_in);
3984 if (match < 0 || !match)
3985 return match < 0 ? isl_bool_error : isl_bool_true;
3987 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3988 map2->dim, isl_dim_out);
3989 if (match < 0 || !match)
3990 return match < 0 ? isl_bool_error : isl_bool_true;
3992 return isl_bool_false;
3995 /* Are "map1" and "map2" obviously disjoint?
3997 * If one of them is empty or if they live in different spaces (ignoring
3998 * parameters), then they are clearly disjoint.
3999 * This is checked by isl_map_plain_is_disjoint_global.
4001 * If they have different parameters, then we skip any further tests.
4003 * If they are obviously equal, but not obviously empty, then we will
4004 * not be able to detect if they are disjoint.
4006 * Otherwise we check if each basic map in "map1" is obviously disjoint
4007 * from each basic map in "map2".
4009 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
4010 __isl_keep isl_map *map2)
4012 isl_bool disjoint;
4013 isl_bool intersect;
4014 isl_bool match;
4016 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4017 if (disjoint < 0 || disjoint)
4018 return disjoint;
4020 match = isl_space_match(map1->dim, isl_dim_param,
4021 map2->dim, isl_dim_param);
4022 if (match < 0 || !match)
4023 return match < 0 ? isl_bool_error : isl_bool_false;
4025 intersect = isl_map_plain_is_equal(map1, map2);
4026 if (intersect < 0 || intersect)
4027 return intersect < 0 ? isl_bool_error : isl_bool_false;
4029 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4032 /* Are "map1" and "map2" disjoint?
4034 * They are disjoint if they are "obviously disjoint" or if one of them
4035 * is empty. Otherwise, they are not disjoint if one of them is universal.
4036 * If the two inputs are (obviously) equal and not empty, then they are
4037 * not disjoint.
4038 * If none of these cases apply, then check if all pairs of basic maps
4039 * are disjoint.
4041 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4043 isl_bool disjoint;
4044 isl_bool intersect;
4046 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4047 if (disjoint < 0 || disjoint)
4048 return disjoint;
4050 disjoint = isl_map_is_empty(map1);
4051 if (disjoint < 0 || disjoint)
4052 return disjoint;
4054 disjoint = isl_map_is_empty(map2);
4055 if (disjoint < 0 || disjoint)
4056 return disjoint;
4058 intersect = isl_map_plain_is_universe(map1);
4059 if (intersect < 0 || intersect)
4060 return intersect < 0 ? isl_bool_error : isl_bool_false;
4062 intersect = isl_map_plain_is_universe(map2);
4063 if (intersect < 0 || intersect)
4064 return intersect < 0 ? isl_bool_error : isl_bool_false;
4066 intersect = isl_map_plain_is_equal(map1, map2);
4067 if (intersect < 0 || intersect)
4068 return isl_bool_not(intersect);
4070 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4073 /* Are "bmap1" and "bmap2" disjoint?
4075 * They are disjoint if they are "obviously disjoint" or if one of them
4076 * is empty. Otherwise, they are not disjoint if one of them is universal.
4077 * If none of these cases apply, we compute the intersection and see if
4078 * the result is empty.
4080 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4081 __isl_keep isl_basic_map *bmap2)
4083 isl_bool disjoint;
4084 isl_bool intersect;
4085 isl_basic_map *test;
4087 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4088 if (disjoint < 0 || disjoint)
4089 return disjoint;
4091 disjoint = isl_basic_map_is_empty(bmap1);
4092 if (disjoint < 0 || disjoint)
4093 return disjoint;
4095 disjoint = isl_basic_map_is_empty(bmap2);
4096 if (disjoint < 0 || disjoint)
4097 return disjoint;
4099 intersect = isl_basic_map_plain_is_universe(bmap1);
4100 if (intersect < 0 || intersect)
4101 return intersect < 0 ? isl_bool_error : isl_bool_false;
4103 intersect = isl_basic_map_plain_is_universe(bmap2);
4104 if (intersect < 0 || intersect)
4105 return intersect < 0 ? isl_bool_error : isl_bool_false;
4107 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4108 isl_basic_map_copy(bmap2));
4109 disjoint = isl_basic_map_is_empty(test);
4110 isl_basic_map_free(test);
4112 return disjoint;
4115 /* Are "bset1" and "bset2" disjoint?
4117 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4118 __isl_keep isl_basic_set *bset2)
4120 return isl_basic_map_is_disjoint(bset1, bset2);
4123 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4124 __isl_keep isl_set *set2)
4126 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4129 /* Are "set1" and "set2" disjoint?
4131 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4133 return isl_map_is_disjoint(set1, set2);
4136 /* Is "v" equal to 0, 1 or -1?
4138 static int is_zero_or_one(isl_int v)
4140 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4143 /* Check if we can combine a given div with lower bound l and upper
4144 * bound u with some other div and if so return that other div.
4145 * Otherwise return -1.
4147 * We first check that
4148 * - the bounds are opposites of each other (except for the constant
4149 * term)
4150 * - the bounds do not reference any other div
4151 * - no div is defined in terms of this div
4153 * Let m be the size of the range allowed on the div by the bounds.
4154 * That is, the bounds are of the form
4156 * e <= a <= e + m - 1
4158 * with e some expression in the other variables.
4159 * We look for another div b such that no third div is defined in terms
4160 * of this second div b and such that in any constraint that contains
4161 * a (except for the given lower and upper bound), also contains b
4162 * with a coefficient that is m times that of b.
4163 * That is, all constraints (execpt for the lower and upper bound)
4164 * are of the form
4166 * e + f (a + m b) >= 0
4168 * Furthermore, in the constraints that only contain b, the coefficient
4169 * of b should be equal to 1 or -1.
4170 * If so, we return b so that "a + m b" can be replaced by
4171 * a single div "c = a + m b".
4173 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4174 unsigned div, unsigned l, unsigned u)
4176 int i, j;
4177 unsigned dim;
4178 int coalesce = -1;
4180 if (bmap->n_div <= 1)
4181 return -1;
4182 dim = isl_space_dim(bmap->dim, isl_dim_all);
4183 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4184 return -1;
4185 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4186 bmap->n_div - div - 1) != -1)
4187 return -1;
4188 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4189 dim + bmap->n_div))
4190 return -1;
4192 for (i = 0; i < bmap->n_div; ++i) {
4193 if (isl_int_is_zero(bmap->div[i][0]))
4194 continue;
4195 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4196 return -1;
4199 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4200 if (isl_int_is_neg(bmap->ineq[l][0])) {
4201 isl_int_sub(bmap->ineq[l][0],
4202 bmap->ineq[l][0], bmap->ineq[u][0]);
4203 bmap = isl_basic_map_copy(bmap);
4204 bmap = isl_basic_map_set_to_empty(bmap);
4205 isl_basic_map_free(bmap);
4206 return -1;
4208 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4209 for (i = 0; i < bmap->n_div; ++i) {
4210 if (i == div)
4211 continue;
4212 if (!pairs[i])
4213 continue;
4214 for (j = 0; j < bmap->n_div; ++j) {
4215 if (isl_int_is_zero(bmap->div[j][0]))
4216 continue;
4217 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4218 break;
4220 if (j < bmap->n_div)
4221 continue;
4222 for (j = 0; j < bmap->n_ineq; ++j) {
4223 int valid;
4224 if (j == l || j == u)
4225 continue;
4226 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4227 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4228 continue;
4229 break;
4231 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4232 break;
4233 isl_int_mul(bmap->ineq[j][1 + dim + div],
4234 bmap->ineq[j][1 + dim + div],
4235 bmap->ineq[l][0]);
4236 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4237 bmap->ineq[j][1 + dim + i]);
4238 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4239 bmap->ineq[j][1 + dim + div],
4240 bmap->ineq[l][0]);
4241 if (!valid)
4242 break;
4244 if (j < bmap->n_ineq)
4245 continue;
4246 coalesce = i;
4247 break;
4249 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4250 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4251 return coalesce;
4254 /* Internal data structure used during the construction and/or evaluation of
4255 * an inequality that ensures that a pair of bounds always allows
4256 * for an integer value.
4258 * "tab" is the tableau in which the inequality is evaluated. It may
4259 * be NULL until it is actually needed.
4260 * "v" contains the inequality coefficients.
4261 * "g", "fl" and "fu" are temporary scalars used during the construction and
4262 * evaluation.
4264 struct test_ineq_data {
4265 struct isl_tab *tab;
4266 isl_vec *v;
4267 isl_int g;
4268 isl_int fl;
4269 isl_int fu;
4272 /* Free all the memory allocated by the fields of "data".
4274 static void test_ineq_data_clear(struct test_ineq_data *data)
4276 isl_tab_free(data->tab);
4277 isl_vec_free(data->v);
4278 isl_int_clear(data->g);
4279 isl_int_clear(data->fl);
4280 isl_int_clear(data->fu);
4283 /* Is the inequality stored in data->v satisfied by "bmap"?
4284 * That is, does it only attain non-negative values?
4285 * data->tab is a tableau corresponding to "bmap".
4287 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4288 struct test_ineq_data *data)
4290 isl_ctx *ctx;
4291 enum isl_lp_result res;
4293 ctx = isl_basic_map_get_ctx(bmap);
4294 if (!data->tab)
4295 data->tab = isl_tab_from_basic_map(bmap, 0);
4296 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4297 if (res == isl_lp_error)
4298 return isl_bool_error;
4299 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4302 /* Given a lower and an upper bound on div i, do they always allow
4303 * for an integer value of the given div?
4304 * Determine this property by constructing an inequality
4305 * such that the property is guaranteed when the inequality is nonnegative.
4306 * The lower bound is inequality l, while the upper bound is inequality u.
4307 * The constructed inequality is stored in data->v.
4309 * Let the upper bound be
4311 * -n_u a + e_u >= 0
4313 * and the lower bound
4315 * n_l a + e_l >= 0
4317 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4318 * We have
4320 * - f_u e_l <= f_u f_l g a <= f_l e_u
4322 * Since all variables are integer valued, this is equivalent to
4324 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4326 * If this interval is at least f_u f_l g, then it contains at least
4327 * one integer value for a.
4328 * That is, the test constraint is
4330 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4332 * or
4334 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4336 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4337 * then the constraint can be scaled down by a factor g',
4338 * with the constant term replaced by
4339 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4340 * Note that the result of applying Fourier-Motzkin to this pair
4341 * of constraints is
4343 * f_l e_u + f_u e_l >= 0
4345 * If the constant term of the scaled down version of this constraint,
4346 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4347 * term of the scaled down test constraint, then the test constraint
4348 * is known to hold and no explicit evaluation is required.
4349 * This is essentially the Omega test.
4351 * If the test constraint consists of only a constant term, then
4352 * it is sufficient to look at the sign of this constant term.
4354 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4355 int l, int u, struct test_ineq_data *data)
4357 unsigned offset, n_div;
4358 offset = isl_basic_map_offset(bmap, isl_dim_div);
4359 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4361 isl_int_gcd(data->g,
4362 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4363 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4364 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4365 isl_int_neg(data->fu, data->fu);
4366 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4367 data->fu, bmap->ineq[l], offset + n_div);
4368 isl_int_mul(data->g, data->g, data->fl);
4369 isl_int_mul(data->g, data->g, data->fu);
4370 isl_int_sub(data->g, data->g, data->fl);
4371 isl_int_sub(data->g, data->g, data->fu);
4372 isl_int_add_ui(data->g, data->g, 1);
4373 isl_int_sub(data->fl, data->v->el[0], data->g);
4375 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4376 if (isl_int_is_zero(data->g))
4377 return isl_int_is_nonneg(data->fl);
4378 if (isl_int_is_one(data->g)) {
4379 isl_int_set(data->v->el[0], data->fl);
4380 return test_ineq_is_satisfied(bmap, data);
4382 isl_int_fdiv_q(data->fl, data->fl, data->g);
4383 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4384 if (isl_int_eq(data->fl, data->v->el[0]))
4385 return isl_bool_true;
4386 isl_int_set(data->v->el[0], data->fl);
4387 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4388 offset - 1 + n_div);
4390 return test_ineq_is_satisfied(bmap, data);
4393 /* Remove more kinds of divs that are not strictly needed.
4394 * In particular, if all pairs of lower and upper bounds on a div
4395 * are such that they allow at least one integer value of the div,
4396 * then we can eliminate the div using Fourier-Motzkin without
4397 * introducing any spurious solutions.
4399 * If at least one of the two constraints has a unit coefficient for the div,
4400 * then the presence of such a value is guaranteed so there is no need to check.
4401 * In particular, the value attained by the bound with unit coefficient
4402 * can serve as this intermediate value.
4404 static struct isl_basic_map *drop_more_redundant_divs(
4405 struct isl_basic_map *bmap, int *pairs, int n)
4407 isl_ctx *ctx;
4408 struct test_ineq_data data = { NULL, NULL };
4409 unsigned off, n_div;
4410 int remove = -1;
4412 isl_int_init(data.g);
4413 isl_int_init(data.fl);
4414 isl_int_init(data.fu);
4416 if (!bmap)
4417 goto error;
4419 ctx = isl_basic_map_get_ctx(bmap);
4420 off = isl_basic_map_offset(bmap, isl_dim_div);
4421 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4422 data.v = isl_vec_alloc(ctx, off + n_div);
4423 if (!data.v)
4424 goto error;
4426 while (n > 0) {
4427 int i, l, u;
4428 int best = -1;
4429 isl_bool has_int;
4431 for (i = 0; i < n_div; ++i) {
4432 if (!pairs[i])
4433 continue;
4434 if (best >= 0 && pairs[best] <= pairs[i])
4435 continue;
4436 best = i;
4439 i = best;
4440 for (l = 0; l < bmap->n_ineq; ++l) {
4441 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4442 continue;
4443 if (isl_int_is_one(bmap->ineq[l][off + i]))
4444 continue;
4445 for (u = 0; u < bmap->n_ineq; ++u) {
4446 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4447 continue;
4448 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4449 continue;
4450 has_int = int_between_bounds(bmap, i, l, u,
4451 &data);
4452 if (has_int < 0)
4453 goto error;
4454 if (data.tab && data.tab->empty)
4455 break;
4456 if (!has_int)
4457 break;
4459 if (u < bmap->n_ineq)
4460 break;
4462 if (data.tab && data.tab->empty) {
4463 bmap = isl_basic_map_set_to_empty(bmap);
4464 break;
4466 if (l == bmap->n_ineq) {
4467 remove = i;
4468 break;
4470 pairs[i] = 0;
4471 --n;
4474 test_ineq_data_clear(&data);
4476 free(pairs);
4478 if (remove < 0)
4479 return bmap;
4481 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4482 return isl_basic_map_drop_redundant_divs(bmap);
4483 error:
4484 free(pairs);
4485 isl_basic_map_free(bmap);
4486 test_ineq_data_clear(&data);
4487 return NULL;
4490 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4491 * and the upper bound u, div1 always occurs together with div2 in the form
4492 * (div1 + m div2), where m is the constant range on the variable div1
4493 * allowed by l and u, replace the pair div1 and div2 by a single
4494 * div that is equal to div1 + m div2.
4496 * The new div will appear in the location that contains div2.
4497 * We need to modify all constraints that contain
4498 * div2 = (div - div1) / m
4499 * The coefficient of div2 is known to be equal to 1 or -1.
4500 * (If a constraint does not contain div2, it will also not contain div1.)
4501 * If the constraint also contains div1, then we know they appear
4502 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4503 * i.e., the coefficient of div is f.
4505 * Otherwise, we first need to introduce div1 into the constraint.
4506 * Let the l be
4508 * div1 + f >=0
4510 * and u
4512 * -div1 + f' >= 0
4514 * A lower bound on div2
4516 * div2 + t >= 0
4518 * can be replaced by
4520 * m div2 + div1 + m t + f >= 0
4522 * An upper bound
4524 * -div2 + t >= 0
4526 * can be replaced by
4528 * -(m div2 + div1) + m t + f' >= 0
4530 * These constraint are those that we would obtain from eliminating
4531 * div1 using Fourier-Motzkin.
4533 * After all constraints have been modified, we drop the lower and upper
4534 * bound and then drop div1.
4536 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4537 unsigned div1, unsigned div2, unsigned l, unsigned u)
4539 isl_ctx *ctx;
4540 isl_int m;
4541 unsigned dim, total;
4542 int i;
4544 ctx = isl_basic_map_get_ctx(bmap);
4546 dim = isl_space_dim(bmap->dim, isl_dim_all);
4547 total = 1 + dim + bmap->n_div;
4549 isl_int_init(m);
4550 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4551 isl_int_add_ui(m, m, 1);
4553 for (i = 0; i < bmap->n_ineq; ++i) {
4554 if (i == l || i == u)
4555 continue;
4556 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4557 continue;
4558 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4559 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4560 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4561 ctx->one, bmap->ineq[l], total);
4562 else
4563 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4564 ctx->one, bmap->ineq[u], total);
4566 isl_int_set(bmap->ineq[i][1 + dim + div2],
4567 bmap->ineq[i][1 + dim + div1]);
4568 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4571 isl_int_clear(m);
4572 if (l > u) {
4573 isl_basic_map_drop_inequality(bmap, l);
4574 isl_basic_map_drop_inequality(bmap, u);
4575 } else {
4576 isl_basic_map_drop_inequality(bmap, u);
4577 isl_basic_map_drop_inequality(bmap, l);
4579 bmap = isl_basic_map_drop_div(bmap, div1);
4580 return bmap;
4583 /* First check if we can coalesce any pair of divs and
4584 * then continue with dropping more redundant divs.
4586 * We loop over all pairs of lower and upper bounds on a div
4587 * with coefficient 1 and -1, respectively, check if there
4588 * is any other div "c" with which we can coalesce the div
4589 * and if so, perform the coalescing.
4591 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4592 struct isl_basic_map *bmap, int *pairs, int n)
4594 int i, l, u;
4595 unsigned dim;
4597 dim = isl_space_dim(bmap->dim, isl_dim_all);
4599 for (i = 0; i < bmap->n_div; ++i) {
4600 if (!pairs[i])
4601 continue;
4602 for (l = 0; l < bmap->n_ineq; ++l) {
4603 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4604 continue;
4605 for (u = 0; u < bmap->n_ineq; ++u) {
4606 int c;
4608 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4609 continue;
4610 c = div_find_coalesce(bmap, pairs, i, l, u);
4611 if (c < 0)
4612 continue;
4613 free(pairs);
4614 bmap = coalesce_divs(bmap, i, c, l, u);
4615 return isl_basic_map_drop_redundant_divs(bmap);
4620 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4621 free(pairs);
4622 return bmap;
4625 return drop_more_redundant_divs(bmap, pairs, n);
4628 /* Are the "n" coefficients starting at "first" of inequality constraints
4629 * "i" and "j" of "bmap" equal to each other?
4631 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4632 int first, int n)
4634 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4637 /* Are the "n" coefficients starting at "first" of inequality constraints
4638 * "i" and "j" of "bmap" opposite to each other?
4640 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4641 int first, int n)
4643 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4646 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4647 * apart from the constant term?
4649 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4651 unsigned total;
4653 total = isl_basic_map_dim(bmap, isl_dim_all);
4654 return is_opposite_part(bmap, i, j, 1, total);
4657 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4658 * apart from the constant term and the coefficient at position "pos"?
4660 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4661 int pos)
4663 unsigned total;
4665 total = isl_basic_map_dim(bmap, isl_dim_all);
4666 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4667 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4670 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4671 * apart from the constant term and the coefficient at position "pos"?
4673 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4674 int pos)
4676 unsigned total;
4678 total = isl_basic_map_dim(bmap, isl_dim_all);
4679 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4680 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4683 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4684 * been modified, simplying it if "simplify" is set.
4685 * Free the temporary data structure "pairs" that was associated
4686 * to the old version of "bmap".
4688 static __isl_give isl_basic_map *drop_redundant_divs_again(
4689 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4691 if (simplify)
4692 bmap = isl_basic_map_simplify(bmap);
4693 free(pairs);
4694 return isl_basic_map_drop_redundant_divs(bmap);
4697 /* Is "div" the single unknown existentially quantified variable
4698 * in inequality constraint "ineq" of "bmap"?
4699 * "div" is known to have a non-zero coefficient in "ineq".
4701 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4702 int div)
4704 int i;
4705 unsigned n_div, o_div;
4706 isl_bool known;
4708 known = isl_basic_map_div_is_known(bmap, div);
4709 if (known < 0 || known)
4710 return isl_bool_not(known);
4711 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4712 if (n_div == 1)
4713 return isl_bool_true;
4714 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4715 for (i = 0; i < n_div; ++i) {
4716 isl_bool known;
4718 if (i == div)
4719 continue;
4720 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4721 continue;
4722 known = isl_basic_map_div_is_known(bmap, i);
4723 if (known < 0 || !known)
4724 return known;
4727 return isl_bool_true;
4730 /* Does integer division "div" have coefficient 1 in inequality constraint
4731 * "ineq" of "map"?
4733 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4735 unsigned o_div;
4737 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4738 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4739 return isl_bool_true;
4741 return isl_bool_false;
4744 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4745 * then try and drop redundant divs again,
4746 * freeing the temporary data structure "pairs" that was associated
4747 * to the old version of "bmap".
4749 static __isl_give isl_basic_map *set_eq_and_try_again(
4750 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4752 bmap = isl_basic_map_cow(bmap);
4753 isl_basic_map_inequality_to_equality(bmap, ineq);
4754 return drop_redundant_divs_again(bmap, pairs, 1);
4757 /* Drop the integer division at position "div", along with the two
4758 * inequality constraints "ineq1" and "ineq2" in which it appears
4759 * from "bmap" and then try and drop redundant divs again,
4760 * freeing the temporary data structure "pairs" that was associated
4761 * to the old version of "bmap".
4763 static __isl_give isl_basic_map *drop_div_and_try_again(
4764 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4765 __isl_take int *pairs)
4767 if (ineq1 > ineq2) {
4768 isl_basic_map_drop_inequality(bmap, ineq1);
4769 isl_basic_map_drop_inequality(bmap, ineq2);
4770 } else {
4771 isl_basic_map_drop_inequality(bmap, ineq2);
4772 isl_basic_map_drop_inequality(bmap, ineq1);
4774 bmap = isl_basic_map_drop_div(bmap, div);
4775 return drop_redundant_divs_again(bmap, pairs, 0);
4778 /* Given two inequality constraints
4780 * f(x) + n d + c >= 0, (ineq)
4782 * with d the variable at position "pos", and
4784 * f(x) + c0 >= 0, (lower)
4786 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4787 * determined by the first constraint.
4788 * That is, store
4790 * ceil((c0 - c)/n)
4792 * in *l.
4794 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4795 int ineq, int lower, int pos, isl_int *l)
4797 isl_int_neg(*l, bmap->ineq[ineq][0]);
4798 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4799 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4802 /* Given two inequality constraints
4804 * f(x) + n d + c >= 0, (ineq)
4806 * with d the variable at position "pos", and
4808 * -f(x) - c0 >= 0, (upper)
4810 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4811 * determined by the first constraint.
4812 * That is, store
4814 * ceil((-c1 - c)/n)
4816 * in *u.
4818 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4819 int ineq, int upper, int pos, isl_int *u)
4821 isl_int_neg(*u, bmap->ineq[ineq][0]);
4822 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4823 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4826 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4827 * does the corresponding lower bound have a fixed value in "bmap"?
4829 * In particular, "ineq" is of the form
4831 * f(x) + n d + c >= 0
4833 * with n > 0, c the constant term and
4834 * d the existentially quantified variable "div".
4835 * That is, the lower bound is
4837 * ceil((-f(x) - c)/n)
4839 * Look for a pair of constraints
4841 * f(x) + c0 >= 0
4842 * -f(x) + c1 >= 0
4844 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4845 * That is, check that
4847 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4849 * If so, return the index of inequality f(x) + c0 >= 0.
4850 * Otherwise, return -1.
4852 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4854 int i;
4855 int lower = -1, upper = -1;
4856 unsigned o_div, n_div;
4857 isl_int l, u;
4858 int equal;
4860 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4861 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4862 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4863 if (i == ineq)
4864 continue;
4865 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4866 continue;
4867 if (lower < 0 &&
4868 is_parallel_except(bmap, ineq, i, o_div + div)) {
4869 lower = i;
4870 continue;
4872 if (upper < 0 &&
4873 is_opposite_except(bmap, ineq, i, o_div + div)) {
4874 upper = i;
4878 if (lower < 0 || upper < 0)
4879 return -1;
4881 isl_int_init(l);
4882 isl_int_init(u);
4884 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4885 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4887 equal = isl_int_eq(l, u);
4889 isl_int_clear(l);
4890 isl_int_clear(u);
4892 return equal ? lower : -1;
4895 /* Given a lower bound constraint "ineq" on the existentially quantified
4896 * variable "div", such that the corresponding lower bound has
4897 * a fixed value in "bmap", assign this fixed value to the variable and
4898 * then try and drop redundant divs again,
4899 * freeing the temporary data structure "pairs" that was associated
4900 * to the old version of "bmap".
4901 * "lower" determines the constant value for the lower bound.
4903 * In particular, "ineq" is of the form
4905 * f(x) + n d + c >= 0,
4907 * while "lower" is of the form
4909 * f(x) + c0 >= 0
4911 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4912 * is ceil((c0 - c)/n).
4914 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4915 int div, int ineq, int lower, int *pairs)
4917 isl_int c;
4918 unsigned o_div;
4920 isl_int_init(c);
4922 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4923 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4924 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4925 free(pairs);
4927 isl_int_clear(c);
4929 return isl_basic_map_drop_redundant_divs(bmap);
4932 /* Remove divs that are not strictly needed based on the inequality
4933 * constraints.
4934 * In particular, if a div only occurs positively (or negatively)
4935 * in constraints, then it can simply be dropped.
4936 * Also, if a div occurs in only two constraints and if moreover
4937 * those two constraints are opposite to each other, except for the constant
4938 * term and if the sum of the constant terms is such that for any value
4939 * of the other values, there is always at least one integer value of the
4940 * div, i.e., if one plus this sum is greater than or equal to
4941 * the (absolute value) of the coefficient of the div in the constraints,
4942 * then we can also simply drop the div.
4944 * If an existentially quantified variable does not have an explicit
4945 * representation, appears in only a single lower bound that does not
4946 * involve any other such existentially quantified variables and appears
4947 * in this lower bound with coefficient 1,
4948 * then fix the variable to the value of the lower bound. That is,
4949 * turn the inequality into an equality.
4950 * If for any value of the other variables, there is any value
4951 * for the existentially quantified variable satisfying the constraints,
4952 * then this lower bound also satisfies the constraints.
4953 * It is therefore safe to pick this lower bound.
4955 * The same reasoning holds even if the coefficient is not one.
4956 * However, fixing the variable to the value of the lower bound may
4957 * in general introduce an extra integer division, in which case
4958 * it may be better to pick another value.
4959 * If this integer division has a known constant value, then plugging
4960 * in this constant value removes the existentially quantified variable
4961 * completely. In particular, if the lower bound is of the form
4962 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4963 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4964 * then the existentially quantified variable can be assigned this
4965 * shared value.
4967 * We skip divs that appear in equalities or in the definition of other divs.
4968 * Divs that appear in the definition of other divs usually occur in at least
4969 * 4 constraints, but the constraints may have been simplified.
4971 * If any divs are left after these simple checks then we move on
4972 * to more complicated cases in drop_more_redundant_divs.
4974 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4975 __isl_take isl_basic_map *bmap)
4977 int i, j;
4978 unsigned off;
4979 int *pairs = NULL;
4980 int n = 0;
4982 if (!bmap)
4983 goto error;
4984 if (bmap->n_div == 0)
4985 return bmap;
4987 off = isl_space_dim(bmap->dim, isl_dim_all);
4988 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4989 if (!pairs)
4990 goto error;
4992 for (i = 0; i < bmap->n_div; ++i) {
4993 int pos, neg;
4994 int last_pos, last_neg;
4995 int redundant;
4996 int defined;
4997 isl_bool opp, set_div;
4999 defined = !isl_int_is_zero(bmap->div[i][0]);
5000 for (j = i; j < bmap->n_div; ++j)
5001 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
5002 break;
5003 if (j < bmap->n_div)
5004 continue;
5005 for (j = 0; j < bmap->n_eq; ++j)
5006 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5007 break;
5008 if (j < bmap->n_eq)
5009 continue;
5010 ++n;
5011 pos = neg = 0;
5012 for (j = 0; j < bmap->n_ineq; ++j) {
5013 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5014 last_pos = j;
5015 ++pos;
5017 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5018 last_neg = j;
5019 ++neg;
5022 pairs[i] = pos * neg;
5023 if (pairs[i] == 0) {
5024 for (j = bmap->n_ineq - 1; j >= 0; --j)
5025 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5026 isl_basic_map_drop_inequality(bmap, j);
5027 bmap = isl_basic_map_drop_div(bmap, i);
5028 return drop_redundant_divs_again(bmap, pairs, 0);
5030 if (pairs[i] != 1)
5031 opp = isl_bool_false;
5032 else
5033 opp = is_opposite(bmap, last_pos, last_neg);
5034 if (opp < 0)
5035 goto error;
5036 if (!opp) {
5037 int lower;
5038 isl_bool single, one;
5040 if (pos != 1)
5041 continue;
5042 single = single_unknown(bmap, last_pos, i);
5043 if (single < 0)
5044 goto error;
5045 if (!single)
5046 continue;
5047 one = has_coef_one(bmap, i, last_pos);
5048 if (one < 0)
5049 goto error;
5050 if (one)
5051 return set_eq_and_try_again(bmap, last_pos,
5052 pairs);
5053 lower = lower_bound_is_cst(bmap, i, last_pos);
5054 if (lower >= 0)
5055 return fix_cst_lower(bmap, i, last_pos, lower,
5056 pairs);
5057 continue;
5060 isl_int_add(bmap->ineq[last_pos][0],
5061 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5062 isl_int_add_ui(bmap->ineq[last_pos][0],
5063 bmap->ineq[last_pos][0], 1);
5064 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5065 bmap->ineq[last_pos][1+off+i]);
5066 isl_int_sub_ui(bmap->ineq[last_pos][0],
5067 bmap->ineq[last_pos][0], 1);
5068 isl_int_sub(bmap->ineq[last_pos][0],
5069 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5070 if (redundant)
5071 return drop_div_and_try_again(bmap, i,
5072 last_pos, last_neg, pairs);
5073 if (defined)
5074 set_div = isl_bool_false;
5075 else
5076 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5077 if (set_div < 0)
5078 return isl_basic_map_free(bmap);
5079 if (set_div) {
5080 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5081 return drop_redundant_divs_again(bmap, pairs, 1);
5083 pairs[i] = 0;
5084 --n;
5087 if (n > 0)
5088 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5090 free(pairs);
5091 return bmap;
5092 error:
5093 free(pairs);
5094 isl_basic_map_free(bmap);
5095 return NULL;
5098 /* Consider the coefficients at "c" as a row vector and replace
5099 * them with their product with "T". "T" is assumed to be a square matrix.
5101 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5103 int n;
5104 isl_ctx *ctx;
5105 isl_vec *v;
5107 if (!T)
5108 return isl_stat_error;
5109 n = isl_mat_rows(T);
5110 if (isl_seq_first_non_zero(c, n) == -1)
5111 return isl_stat_ok;
5112 ctx = isl_mat_get_ctx(T);
5113 v = isl_vec_alloc(ctx, n);
5114 if (!v)
5115 return isl_stat_error;
5116 isl_seq_swp_or_cpy(v->el, c, n);
5117 v = isl_vec_mat_product(v, isl_mat_copy(T));
5118 if (!v)
5119 return isl_stat_error;
5120 isl_seq_swp_or_cpy(c, v->el, n);
5121 isl_vec_free(v);
5123 return isl_stat_ok;
5126 /* Plug in T for the variables in "bmap" starting at "pos".
5127 * T is a linear unimodular matrix, i.e., without constant term.
5129 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5130 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5132 int i;
5133 unsigned n, total;
5135 bmap = isl_basic_map_cow(bmap);
5136 if (!bmap || !T)
5137 goto error;
5139 n = isl_mat_cols(T);
5140 if (n != isl_mat_rows(T))
5141 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5142 "expecting square matrix", goto error);
5144 total = isl_basic_map_dim(bmap, isl_dim_all);
5145 if (pos + n > total || pos + n < pos)
5146 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5147 "invalid range", goto error);
5149 for (i = 0; i < bmap->n_eq; ++i)
5150 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5151 goto error;
5152 for (i = 0; i < bmap->n_ineq; ++i)
5153 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5154 goto error;
5155 for (i = 0; i < bmap->n_div; ++i) {
5156 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5157 continue;
5158 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5159 goto error;
5162 isl_mat_free(T);
5163 return bmap;
5164 error:
5165 isl_basic_map_free(bmap);
5166 isl_mat_free(T);
5167 return NULL;
5170 /* Remove divs that are not strictly needed.
5172 * First look for an equality constraint involving two or more
5173 * existentially quantified variables without an explicit
5174 * representation. Replace the combination that appears
5175 * in the equality constraint by a single existentially quantified
5176 * variable such that the equality can be used to derive
5177 * an explicit representation for the variable.
5178 * If there are no more such equality constraints, then continue
5179 * with isl_basic_map_drop_redundant_divs_ineq.
5181 * In particular, if the equality constraint is of the form
5183 * f(x) + \sum_i c_i a_i = 0
5185 * with a_i existentially quantified variable without explicit
5186 * representation, then apply a transformation on the existentially
5187 * quantified variables to turn the constraint into
5189 * f(x) + g a_1' = 0
5191 * with g the gcd of the c_i.
5192 * In order to easily identify which existentially quantified variables
5193 * have a complete explicit representation, i.e., without being defined
5194 * in terms of other existentially quantified variables without
5195 * an explicit representation, the existentially quantified variables
5196 * are first sorted.
5198 * The variable transformation is computed by extending the row
5199 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5201 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5202 * [a_2'] [ a_2 ]
5203 * ... = U ....
5204 * [a_n'] [ a_n ]
5206 * with [c_1/g ... c_n/g] representing the first row of U.
5207 * The inverse of U is then plugged into the original constraints.
5208 * The call to isl_basic_map_simplify makes sure the explicit
5209 * representation for a_1' is extracted from the equality constraint.
5211 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5212 __isl_take isl_basic_map *bmap)
5214 int first;
5215 int i;
5216 unsigned o_div, n_div;
5217 int l;
5218 isl_ctx *ctx;
5219 isl_mat *T;
5221 if (!bmap)
5222 return NULL;
5223 if (isl_basic_map_divs_known(bmap))
5224 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5225 if (bmap->n_eq == 0)
5226 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5227 bmap = isl_basic_map_sort_divs(bmap);
5228 if (!bmap)
5229 return NULL;
5231 first = isl_basic_map_first_unknown_div(bmap);
5232 if (first < 0)
5233 return isl_basic_map_free(bmap);
5235 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5236 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5238 for (i = 0; i < bmap->n_eq; ++i) {
5239 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5240 n_div - (first));
5241 if (l < 0)
5242 continue;
5243 l += first;
5244 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5245 n_div - (l + 1)) == -1)
5246 continue;
5247 break;
5249 if (i >= bmap->n_eq)
5250 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5252 ctx = isl_basic_map_get_ctx(bmap);
5253 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5254 if (!T)
5255 return isl_basic_map_free(bmap);
5256 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5257 T = isl_mat_normalize_row(T, 0);
5258 T = isl_mat_unimodular_complete(T, 1);
5259 T = isl_mat_right_inverse(T);
5261 for (i = l; i < n_div; ++i)
5262 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5263 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5264 bmap = isl_basic_map_simplify(bmap);
5266 return isl_basic_map_drop_redundant_divs(bmap);
5269 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5270 struct isl_basic_set *bset)
5272 isl_basic_map *bmap = bset_to_bmap(bset);
5273 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5276 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5278 int i;
5280 if (!map)
5281 return NULL;
5282 for (i = 0; i < map->n; ++i) {
5283 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5284 if (!map->p[i])
5285 goto error;
5287 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5288 return map;
5289 error:
5290 isl_map_free(map);
5291 return NULL;
5294 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5296 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5299 /* Does "bmap" satisfy any equality that involves more than 2 variables
5300 * and/or has coefficients different from -1 and 1?
5302 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5304 int i;
5305 unsigned total;
5307 total = isl_basic_map_dim(bmap, isl_dim_all);
5309 for (i = 0; i < bmap->n_eq; ++i) {
5310 int j, k;
5312 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5313 if (j < 0)
5314 continue;
5315 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5316 !isl_int_is_negone(bmap->eq[i][1 + j]))
5317 return 1;
5319 j += 1;
5320 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5321 if (k < 0)
5322 continue;
5323 j += k;
5324 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5325 !isl_int_is_negone(bmap->eq[i][1 + j]))
5326 return 1;
5328 j += 1;
5329 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5330 if (k >= 0)
5331 return 1;
5334 return 0;
5337 /* Remove any common factor g from the constraint coefficients in "v".
5338 * The constant term is stored in the first position and is replaced
5339 * by floor(c/g). If any common factor is removed and if this results
5340 * in a tightening of the constraint, then set *tightened.
5342 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5343 int *tightened)
5345 isl_ctx *ctx;
5347 if (!v)
5348 return NULL;
5349 ctx = isl_vec_get_ctx(v);
5350 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5351 if (isl_int_is_zero(ctx->normalize_gcd))
5352 return v;
5353 if (isl_int_is_one(ctx->normalize_gcd))
5354 return v;
5355 v = isl_vec_cow(v);
5356 if (!v)
5357 return NULL;
5358 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5359 *tightened = 1;
5360 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5361 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5362 v->size - 1);
5363 return v;
5366 /* If "bmap" is an integer set that satisfies any equality involving
5367 * more than 2 variables and/or has coefficients different from -1 and 1,
5368 * then use variable compression to reduce the coefficients by removing
5369 * any (hidden) common factor.
5370 * In particular, apply the variable compression to each constraint,
5371 * factor out any common factor in the non-constant coefficients and
5372 * then apply the inverse of the compression.
5373 * At the end, we mark the basic map as having reduced constants.
5374 * If this flag is still set on the next invocation of this function,
5375 * then we skip the computation.
5377 * Removing a common factor may result in a tightening of some of
5378 * the constraints. If this happens, then we may end up with two
5379 * opposite inequalities that can be replaced by an equality.
5380 * We therefore call isl_basic_map_detect_inequality_pairs,
5381 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5382 * and isl_basic_map_gauss if such a pair was found.
5384 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5385 __isl_take isl_basic_map *bmap)
5387 unsigned total;
5388 isl_ctx *ctx;
5389 isl_vec *v;
5390 isl_mat *eq, *T, *T2;
5391 int i;
5392 int tightened;
5394 if (!bmap)
5395 return NULL;
5396 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5397 return bmap;
5398 if (isl_basic_map_is_rational(bmap))
5399 return bmap;
5400 if (bmap->n_eq == 0)
5401 return bmap;
5402 if (!has_multiple_var_equality(bmap))
5403 return bmap;
5405 total = isl_basic_map_dim(bmap, isl_dim_all);
5406 ctx = isl_basic_map_get_ctx(bmap);
5407 v = isl_vec_alloc(ctx, 1 + total);
5408 if (!v)
5409 return isl_basic_map_free(bmap);
5411 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5412 T = isl_mat_variable_compression(eq, &T2);
5413 if (!T || !T2)
5414 goto error;
5415 if (T->n_col == 0) {
5416 isl_mat_free(T);
5417 isl_mat_free(T2);
5418 isl_vec_free(v);
5419 return isl_basic_map_set_to_empty(bmap);
5422 tightened = 0;
5423 for (i = 0; i < bmap->n_ineq; ++i) {
5424 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5425 v = isl_vec_mat_product(v, isl_mat_copy(T));
5426 v = normalize_constraint(v, &tightened);
5427 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5428 if (!v)
5429 goto error;
5430 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5433 isl_mat_free(T);
5434 isl_mat_free(T2);
5435 isl_vec_free(v);
5437 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5439 if (tightened) {
5440 int progress = 0;
5442 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5443 if (progress) {
5444 bmap = eliminate_divs_eq(bmap, &progress);
5445 bmap = isl_basic_map_gauss(bmap, NULL);
5449 return bmap;
5450 error:
5451 isl_mat_free(T);
5452 isl_mat_free(T2);
5453 isl_vec_free(v);
5454 return isl_basic_map_free(bmap);
5457 /* Shift the integer division at position "div" of "bmap"
5458 * by "shift" times the variable at position "pos".
5459 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5460 * corresponds to the constant term.
5462 * That is, if the integer division has the form
5464 * floor(f(x)/d)
5466 * then replace it by
5468 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5470 __isl_give isl_basic_map *isl_basic_map_shift_div(
5471 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5473 int i;
5474 unsigned total;
5476 if (isl_int_is_zero(shift))
5477 return bmap;
5478 if (!bmap)
5479 return NULL;
5481 total = isl_basic_map_dim(bmap, isl_dim_all);
5482 total -= isl_basic_map_dim(bmap, isl_dim_div);
5484 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5486 for (i = 0; i < bmap->n_eq; ++i) {
5487 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5488 continue;
5489 isl_int_submul(bmap->eq[i][pos],
5490 shift, bmap->eq[i][1 + total + div]);
5492 for (i = 0; i < bmap->n_ineq; ++i) {
5493 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5494 continue;
5495 isl_int_submul(bmap->ineq[i][pos],
5496 shift, bmap->ineq[i][1 + total + div]);
5498 for (i = 0; i < bmap->n_div; ++i) {
5499 if (isl_int_is_zero(bmap->div[i][0]))
5500 continue;
5501 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5502 continue;
5503 isl_int_submul(bmap->div[i][1 + pos],
5504 shift, bmap->div[i][1 + 1 + total + div]);
5507 return bmap;