isl_basic_map_plain_has_fixed_var: return isl_bool
[isl.git] / isl_map_simplify.c
blobf3fcfb6f4873329500c747cb80bb88837d6cbb66
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 int 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 1;
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 0;
656 return 1;
659 /* Elimininate 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 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
679 !isl_int_is_negone(bmap->eq[i][off + d]))
680 continue;
681 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
682 continue;
683 modified = 1;
684 *progress = 1;
685 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
686 if (isl_basic_map_drop_equality(bmap, i) < 0)
687 return isl_basic_map_free(bmap);
688 break;
691 if (modified)
692 return eliminate_divs_eq(bmap, progress);
693 return bmap;
696 /* Elimininate divs based on inequalities
698 static struct isl_basic_map *eliminate_divs_ineq(
699 struct isl_basic_map *bmap, int *progress)
701 int d;
702 int i;
703 unsigned off;
704 struct isl_ctx *ctx;
706 if (!bmap)
707 return NULL;
709 ctx = bmap->ctx;
710 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
712 for (d = bmap->n_div - 1; d >= 0 ; --d) {
713 for (i = 0; i < bmap->n_eq; ++i)
714 if (!isl_int_is_zero(bmap->eq[i][off + d]))
715 break;
716 if (i < bmap->n_eq)
717 continue;
718 for (i = 0; i < bmap->n_ineq; ++i)
719 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
720 break;
721 if (i < bmap->n_ineq)
722 continue;
723 *progress = 1;
724 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
725 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
726 break;
727 bmap = isl_basic_map_drop_div(bmap, d);
728 if (!bmap)
729 break;
731 return bmap;
734 /* Does the equality constraint at position "eq" in "bmap" involve
735 * any local variables in the range [first, first + n)
736 * that are not marked as having an explicit representation?
738 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
739 int eq, unsigned first, unsigned n)
741 unsigned o_div;
742 int i;
744 if (!bmap)
745 return isl_bool_error;
747 o_div = isl_basic_map_offset(bmap, isl_dim_div);
748 for (i = 0; i < n; ++i) {
749 isl_bool unknown;
751 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
752 continue;
753 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
754 if (unknown < 0)
755 return isl_bool_error;
756 if (unknown)
757 return isl_bool_true;
760 return isl_bool_false;
763 /* The last local variable involved in the equality constraint
764 * at position "eq" in "bmap" is the local variable at position "div".
765 * It can therefore be used to extract an explicit representation
766 * for that variable.
767 * Do so unless the local variable already has an explicit representation or
768 * the explicit representation would involve any other local variables
769 * that in turn do not have an explicit representation.
770 * An equality constraint involving local variables without an explicit
771 * representation can be used in isl_basic_map_drop_redundant_divs
772 * to separate out an independent local variable. Introducing
773 * an explicit representation here would block this transformation,
774 * while the partial explicit representation in itself is not very useful.
775 * Set *progress if anything is changed.
777 * The equality constraint is of the form
779 * f(x) + n e >= 0
781 * with n a positive number. The explicit representation derived from
782 * this constraint is
784 * floor((-f(x))/n)
786 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
787 int div, int eq, int *progress)
789 unsigned total, o_div;
790 isl_bool involves;
792 if (!bmap)
793 return NULL;
795 if (!isl_int_is_zero(bmap->div[div][0]))
796 return bmap;
798 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
799 if (involves < 0)
800 return isl_basic_map_free(bmap);
801 if (involves)
802 return bmap;
804 total = isl_basic_map_dim(bmap, isl_dim_all);
805 o_div = isl_basic_map_offset(bmap, isl_dim_div);
806 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
807 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
808 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
809 if (progress)
810 *progress = 1;
811 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
813 return bmap;
816 struct isl_basic_map *isl_basic_map_gauss(
817 struct isl_basic_map *bmap, int *progress)
819 int k;
820 int done;
821 int last_var;
822 unsigned total_var;
823 unsigned total;
825 bmap = isl_basic_map_order_divs(bmap);
827 if (!bmap)
828 return NULL;
830 total = isl_basic_map_total_dim(bmap);
831 total_var = total - bmap->n_div;
833 last_var = total - 1;
834 for (done = 0; done < bmap->n_eq; ++done) {
835 for (; last_var >= 0; --last_var) {
836 for (k = done; k < bmap->n_eq; ++k)
837 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
838 break;
839 if (k < bmap->n_eq)
840 break;
842 if (last_var < 0)
843 break;
844 if (k != done)
845 swap_equality(bmap, k, done);
846 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
847 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
849 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
850 progress);
852 if (last_var >= total_var)
853 bmap = set_div_from_eq(bmap, last_var - total_var,
854 done, progress);
855 if (!bmap)
856 return NULL;
858 if (done == bmap->n_eq)
859 return bmap;
860 for (k = done; k < bmap->n_eq; ++k) {
861 if (isl_int_is_zero(bmap->eq[k][0]))
862 continue;
863 return isl_basic_map_set_to_empty(bmap);
865 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
866 return bmap;
869 struct isl_basic_set *isl_basic_set_gauss(
870 struct isl_basic_set *bset, int *progress)
872 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
873 progress));
877 static unsigned int round_up(unsigned int v)
879 int old_v = v;
881 while (v) {
882 old_v = v;
883 v ^= v & -v;
885 return old_v << 1;
888 /* Hash table of inequalities in a basic map.
889 * "index" is an array of addresses of inequalities in the basic map, some
890 * of which are NULL. The inequalities are hashed on the coefficients
891 * except the constant term.
892 * "size" is the number of elements in the array and is always a power of two
893 * "bits" is the number of bits need to represent an index into the array.
894 * "total" is the total dimension of the basic map.
896 struct isl_constraint_index {
897 unsigned int size;
898 int bits;
899 isl_int ***index;
900 unsigned total;
903 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
905 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
906 __isl_keep isl_basic_map *bmap)
908 isl_ctx *ctx;
910 ci->index = NULL;
911 if (!bmap)
912 return isl_stat_error;
913 ci->total = isl_basic_set_total_dim(bmap);
914 if (bmap->n_ineq == 0)
915 return isl_stat_ok;
916 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
917 ci->bits = ffs(ci->size) - 1;
918 ctx = isl_basic_map_get_ctx(bmap);
919 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
920 if (!ci->index)
921 return isl_stat_error;
923 return isl_stat_ok;
926 /* Free the memory allocated by create_constraint_index.
928 static void constraint_index_free(struct isl_constraint_index *ci)
930 free(ci->index);
933 /* Return the position in ci->index that contains the address of
934 * an inequality that is equal to *ineq up to the constant term,
935 * provided this address is not identical to "ineq".
936 * If there is no such inequality, then return the position where
937 * such an inequality should be inserted.
939 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
941 int h;
942 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
943 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
944 if (ineq != ci->index[h] &&
945 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
946 break;
947 return h;
950 /* Return the position in ci->index that contains the address of
951 * an inequality that is equal to the k'th inequality of "bmap"
952 * up to the constant term, provided it does not point to the very
953 * same inequality.
954 * If there is no such inequality, then return the position where
955 * such an inequality should be inserted.
957 static int hash_index(struct isl_constraint_index *ci,
958 __isl_keep isl_basic_map *bmap, int k)
960 return hash_index_ineq(ci, &bmap->ineq[k]);
963 static int set_hash_index(struct isl_constraint_index *ci,
964 struct isl_basic_set *bset, int k)
966 return hash_index(ci, bset, k);
969 /* Fill in the "ci" data structure with the inequalities of "bset".
971 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
972 __isl_keep isl_basic_set *bset)
974 int k, h;
976 if (create_constraint_index(ci, bset) < 0)
977 return isl_stat_error;
979 for (k = 0; k < bset->n_ineq; ++k) {
980 h = set_hash_index(ci, bset, k);
981 ci->index[h] = &bset->ineq[k];
984 return isl_stat_ok;
987 /* Is the inequality ineq (obviously) redundant with respect
988 * to the constraints in "ci"?
990 * Look for an inequality in "ci" with the same coefficients and then
991 * check if the contant term of "ineq" is greater than or equal
992 * to the constant term of that inequality. If so, "ineq" is clearly
993 * redundant.
995 * Note that hash_index_ineq ignores a stored constraint if it has
996 * the same address as the passed inequality. It is ok to pass
997 * the address of a local variable here since it will never be
998 * the same as the address of a constraint in "ci".
1000 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
1001 isl_int *ineq)
1003 int h;
1005 h = hash_index_ineq(ci, &ineq);
1006 if (!ci->index[h])
1007 return isl_bool_false;
1008 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
1011 /* If we can eliminate more than one div, then we need to make
1012 * sure we do it from last div to first div, in order not to
1013 * change the position of the other divs that still need to
1014 * be removed.
1016 static struct isl_basic_map *remove_duplicate_divs(
1017 struct isl_basic_map *bmap, int *progress)
1019 unsigned int size;
1020 int *index;
1021 int *elim_for;
1022 int k, l, h;
1023 int bits;
1024 struct isl_blk eq;
1025 unsigned total_var;
1026 unsigned total;
1027 struct isl_ctx *ctx;
1029 bmap = isl_basic_map_order_divs(bmap);
1030 if (!bmap || bmap->n_div <= 1)
1031 return bmap;
1033 total_var = isl_space_dim(bmap->dim, isl_dim_all);
1034 total = total_var + bmap->n_div;
1036 ctx = bmap->ctx;
1037 for (k = bmap->n_div - 1; k >= 0; --k)
1038 if (!isl_int_is_zero(bmap->div[k][0]))
1039 break;
1040 if (k <= 0)
1041 return bmap;
1043 size = round_up(4 * bmap->n_div / 3 - 1);
1044 if (size == 0)
1045 return bmap;
1046 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
1047 bits = ffs(size) - 1;
1048 index = isl_calloc_array(ctx, int, size);
1049 if (!elim_for || !index)
1050 goto out;
1051 eq = isl_blk_alloc(ctx, 1+total);
1052 if (isl_blk_is_error(eq))
1053 goto out;
1055 isl_seq_clr(eq.data, 1+total);
1056 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
1057 for (--k; k >= 0; --k) {
1058 uint32_t hash;
1060 if (isl_int_is_zero(bmap->div[k][0]))
1061 continue;
1063 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
1064 for (h = hash; index[h]; h = (h+1) % size)
1065 if (isl_seq_eq(bmap->div[k],
1066 bmap->div[index[h]-1], 2+total))
1067 break;
1068 if (index[h]) {
1069 *progress = 1;
1070 l = index[h] - 1;
1071 elim_for[l] = k + 1;
1073 index[h] = k+1;
1075 for (l = bmap->n_div - 1; l >= 0; --l) {
1076 if (!elim_for[l])
1077 continue;
1078 k = elim_for[l] - 1;
1079 isl_int_set_si(eq.data[1+total_var+k], -1);
1080 isl_int_set_si(eq.data[1+total_var+l], 1);
1081 bmap = eliminate_div(bmap, eq.data, l, 1);
1082 if (!bmap)
1083 break;
1084 isl_int_set_si(eq.data[1+total_var+k], 0);
1085 isl_int_set_si(eq.data[1+total_var+l], 0);
1088 isl_blk_free(ctx, eq);
1089 out:
1090 free(index);
1091 free(elim_for);
1092 return bmap;
1095 static int n_pure_div_eq(struct isl_basic_map *bmap)
1097 int i, j;
1098 unsigned total;
1100 total = isl_space_dim(bmap->dim, isl_dim_all);
1101 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1102 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1103 --j;
1104 if (j < 0)
1105 break;
1106 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
1107 return 0;
1109 return i;
1112 /* Normalize divs that appear in equalities.
1114 * In particular, we assume that bmap contains some equalities
1115 * of the form
1117 * a x = m * e_i
1119 * and we want to replace the set of e_i by a minimal set and
1120 * such that the new e_i have a canonical representation in terms
1121 * of the vector x.
1122 * If any of the equalities involves more than one divs, then
1123 * we currently simply bail out.
1125 * Let us first additionally assume that all equalities involve
1126 * a div. The equalities then express modulo constraints on the
1127 * remaining variables and we can use "parameter compression"
1128 * to find a minimal set of constraints. The result is a transformation
1130 * x = T(x') = x_0 + G x'
1132 * with G a lower-triangular matrix with all elements below the diagonal
1133 * non-negative and smaller than the diagonal element on the same row.
1134 * We first normalize x_0 by making the same property hold in the affine
1135 * T matrix.
1136 * The rows i of G with a 1 on the diagonal do not impose any modulo
1137 * constraint and simply express x_i = x'_i.
1138 * For each of the remaining rows i, we introduce a div and a corresponding
1139 * equality. In particular
1141 * g_ii e_j = x_i - g_i(x')
1143 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1144 * corresponding div (if g_kk != 1).
1146 * If there are any equalities not involving any div, then we
1147 * first apply a variable compression on the variables x:
1149 * x = C x'' x'' = C_2 x
1151 * and perform the above parameter compression on A C instead of on A.
1152 * The resulting compression is then of the form
1154 * x'' = T(x') = x_0 + G x'
1156 * and in constructing the new divs and the corresponding equalities,
1157 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1158 * by the corresponding row from C_2.
1160 static struct isl_basic_map *normalize_divs(
1161 struct isl_basic_map *bmap, int *progress)
1163 int i, j, k;
1164 int total;
1165 int div_eq;
1166 struct isl_mat *B;
1167 struct isl_vec *d;
1168 struct isl_mat *T = NULL;
1169 struct isl_mat *C = NULL;
1170 struct isl_mat *C2 = NULL;
1171 isl_int v;
1172 int *pos = NULL;
1173 int dropped, needed;
1175 if (!bmap)
1176 return NULL;
1178 if (bmap->n_div == 0)
1179 return bmap;
1181 if (bmap->n_eq == 0)
1182 return bmap;
1184 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1185 return bmap;
1187 total = isl_space_dim(bmap->dim, isl_dim_all);
1188 div_eq = n_pure_div_eq(bmap);
1189 if (div_eq == 0)
1190 return bmap;
1192 if (div_eq < bmap->n_eq) {
1193 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1194 bmap->n_eq - div_eq, 0, 1 + total);
1195 C = isl_mat_variable_compression(B, &C2);
1196 if (!C || !C2)
1197 goto error;
1198 if (C->n_col == 0) {
1199 bmap = isl_basic_map_set_to_empty(bmap);
1200 isl_mat_free(C);
1201 isl_mat_free(C2);
1202 goto done;
1206 d = isl_vec_alloc(bmap->ctx, div_eq);
1207 if (!d)
1208 goto error;
1209 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1210 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1211 --j;
1212 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1214 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1216 if (C) {
1217 B = isl_mat_product(B, C);
1218 C = NULL;
1221 T = isl_mat_parameter_compression(B, d);
1222 if (!T)
1223 goto error;
1224 if (T->n_col == 0) {
1225 bmap = isl_basic_map_set_to_empty(bmap);
1226 isl_mat_free(C2);
1227 isl_mat_free(T);
1228 goto done;
1230 isl_int_init(v);
1231 for (i = 0; i < T->n_row - 1; ++i) {
1232 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1233 if (isl_int_is_zero(v))
1234 continue;
1235 isl_mat_col_submul(T, 0, v, 1 + i);
1237 isl_int_clear(v);
1238 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1239 if (!pos)
1240 goto error;
1241 /* We have to be careful because dropping equalities may reorder them */
1242 dropped = 0;
1243 for (j = bmap->n_div - 1; j >= 0; --j) {
1244 for (i = 0; i < bmap->n_eq; ++i)
1245 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1246 break;
1247 if (i < bmap->n_eq) {
1248 bmap = isl_basic_map_drop_div(bmap, j);
1249 isl_basic_map_drop_equality(bmap, i);
1250 ++dropped;
1253 pos[0] = 0;
1254 needed = 0;
1255 for (i = 1; i < T->n_row; ++i) {
1256 if (isl_int_is_one(T->row[i][i]))
1257 pos[i] = i;
1258 else
1259 needed++;
1261 if (needed > dropped) {
1262 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1263 needed, needed, 0);
1264 if (!bmap)
1265 goto error;
1267 for (i = 1; i < T->n_row; ++i) {
1268 if (isl_int_is_one(T->row[i][i]))
1269 continue;
1270 k = isl_basic_map_alloc_div(bmap);
1271 pos[i] = 1 + total + k;
1272 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1273 isl_int_set(bmap->div[k][0], T->row[i][i]);
1274 if (C2)
1275 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1276 else
1277 isl_int_set_si(bmap->div[k][1 + i], 1);
1278 for (j = 0; j < i; ++j) {
1279 if (isl_int_is_zero(T->row[i][j]))
1280 continue;
1281 if (pos[j] < T->n_row && C2)
1282 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1283 C2->row[pos[j]], 1 + total);
1284 else
1285 isl_int_neg(bmap->div[k][1 + pos[j]],
1286 T->row[i][j]);
1288 j = isl_basic_map_alloc_equality(bmap);
1289 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1290 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1292 free(pos);
1293 isl_mat_free(C2);
1294 isl_mat_free(T);
1296 if (progress)
1297 *progress = 1;
1298 done:
1299 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1301 return bmap;
1302 error:
1303 free(pos);
1304 isl_mat_free(C);
1305 isl_mat_free(C2);
1306 isl_mat_free(T);
1307 return bmap;
1310 static struct isl_basic_map *set_div_from_lower_bound(
1311 struct isl_basic_map *bmap, int div, int ineq)
1313 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1315 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1316 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1317 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1318 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1319 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1321 return bmap;
1324 /* Check whether it is ok to define a div based on an inequality.
1325 * To avoid the introduction of circular definitions of divs, we
1326 * do not allow such a definition if the resulting expression would refer to
1327 * any other undefined divs or if any known div is defined in
1328 * terms of the unknown div.
1330 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1331 int div, int ineq)
1333 int j;
1334 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1336 /* Not defined in terms of unknown divs */
1337 for (j = 0; j < bmap->n_div; ++j) {
1338 if (div == j)
1339 continue;
1340 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1341 continue;
1342 if (isl_int_is_zero(bmap->div[j][0]))
1343 return 0;
1346 /* No other div defined in terms of this one => avoid loops */
1347 for (j = 0; j < bmap->n_div; ++j) {
1348 if (div == j)
1349 continue;
1350 if (isl_int_is_zero(bmap->div[j][0]))
1351 continue;
1352 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1353 return 0;
1356 return 1;
1359 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1360 * be a better expression than the current one?
1362 * If we do not have any expression yet, then any expression would be better.
1363 * Otherwise we check if the last variable involved in the inequality
1364 * (disregarding the div that it would define) is in an earlier position
1365 * than the last variable involved in the current div expression.
1367 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1368 int div, int ineq)
1370 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1371 int last_div;
1372 int last_ineq;
1374 if (isl_int_is_zero(bmap->div[div][0]))
1375 return 1;
1377 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1378 bmap->n_div - (div + 1)) >= 0)
1379 return 0;
1381 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1382 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1383 total + bmap->n_div);
1385 return last_ineq < last_div;
1388 /* Given two constraints "k" and "l" that are opposite to each other,
1389 * except for the constant term, check if we can use them
1390 * to obtain an expression for one of the hitherto unknown divs or
1391 * a "better" expression for a div for which we already have an expression.
1392 * "sum" is the sum of the constant terms of the constraints.
1393 * If this sum is strictly smaller than the coefficient of one
1394 * of the divs, then this pair can be used define the div.
1395 * To avoid the introduction of circular definitions of divs, we
1396 * do not use the pair if the resulting expression would refer to
1397 * any other undefined divs or if any known div is defined in
1398 * terms of the unknown div.
1400 static struct isl_basic_map *check_for_div_constraints(
1401 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1403 int i;
1404 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1406 for (i = 0; i < bmap->n_div; ++i) {
1407 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1408 continue;
1409 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1410 continue;
1411 if (!better_div_constraint(bmap, i, k))
1412 continue;
1413 if (!ok_to_set_div_from_bound(bmap, i, k))
1414 break;
1415 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1416 bmap = set_div_from_lower_bound(bmap, i, k);
1417 else
1418 bmap = set_div_from_lower_bound(bmap, i, l);
1419 if (progress)
1420 *progress = 1;
1421 break;
1423 return bmap;
1426 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1427 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1429 struct isl_constraint_index ci;
1430 int k, l, h;
1431 unsigned total = isl_basic_map_total_dim(bmap);
1432 isl_int sum;
1434 if (!bmap || bmap->n_ineq <= 1)
1435 return bmap;
1437 if (create_constraint_index(&ci, bmap) < 0)
1438 return bmap;
1440 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1441 ci.index[h] = &bmap->ineq[0];
1442 for (k = 1; k < bmap->n_ineq; ++k) {
1443 h = hash_index(&ci, bmap, k);
1444 if (!ci.index[h]) {
1445 ci.index[h] = &bmap->ineq[k];
1446 continue;
1448 if (progress)
1449 *progress = 1;
1450 l = ci.index[h] - &bmap->ineq[0];
1451 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1452 swap_inequality(bmap, k, l);
1453 isl_basic_map_drop_inequality(bmap, k);
1454 --k;
1456 isl_int_init(sum);
1457 for (k = 0; k < bmap->n_ineq-1; ++k) {
1458 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1459 h = hash_index(&ci, bmap, k);
1460 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1461 if (!ci.index[h])
1462 continue;
1463 l = ci.index[h] - &bmap->ineq[0];
1464 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1465 if (isl_int_is_pos(sum)) {
1466 if (detect_divs)
1467 bmap = check_for_div_constraints(bmap, k, l,
1468 sum, progress);
1469 continue;
1471 if (isl_int_is_zero(sum)) {
1472 /* We need to break out of the loop after these
1473 * changes since the contents of the hash
1474 * will no longer be valid.
1475 * Plus, we probably we want to regauss first.
1477 if (progress)
1478 *progress = 1;
1479 isl_basic_map_drop_inequality(bmap, l);
1480 isl_basic_map_inequality_to_equality(bmap, k);
1481 } else
1482 bmap = isl_basic_map_set_to_empty(bmap);
1483 break;
1485 isl_int_clear(sum);
1487 constraint_index_free(&ci);
1488 return bmap;
1491 /* Detect all pairs of inequalities that form an equality.
1493 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1494 * Call it repeatedly while it is making progress.
1496 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1497 __isl_take isl_basic_map *bmap, int *progress)
1499 int duplicate;
1501 do {
1502 duplicate = 0;
1503 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1504 &duplicate, 0);
1505 if (progress && duplicate)
1506 *progress = 1;
1507 } while (duplicate);
1509 return bmap;
1512 /* Eliminate knowns divs from constraints where they appear with
1513 * a (positive or negative) unit coefficient.
1515 * That is, replace
1517 * floor(e/m) + f >= 0
1519 * by
1521 * e + m f >= 0
1523 * and
1525 * -floor(e/m) + f >= 0
1527 * by
1529 * -e + m f + m - 1 >= 0
1531 * The first conversion is valid because floor(e/m) >= -f is equivalent
1532 * to e/m >= -f because -f is an integral expression.
1533 * The second conversion follows from the fact that
1535 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1538 * Note that one of the div constraints may have been eliminated
1539 * due to being redundant with respect to the constraint that is
1540 * being modified by this function. The modified constraint may
1541 * no longer imply this div constraint, so we add it back to make
1542 * sure we do not lose any information.
1544 * We skip integral divs, i.e., those with denominator 1, as we would
1545 * risk eliminating the div from the div constraints. We do not need
1546 * to handle those divs here anyway since the div constraints will turn
1547 * out to form an equality and this equality can then be used to eliminate
1548 * the div from all constraints.
1550 static __isl_give isl_basic_map *eliminate_unit_divs(
1551 __isl_take isl_basic_map *bmap, int *progress)
1553 int i, j;
1554 isl_ctx *ctx;
1555 unsigned total;
1557 if (!bmap)
1558 return NULL;
1560 ctx = isl_basic_map_get_ctx(bmap);
1561 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1563 for (i = 0; i < bmap->n_div; ++i) {
1564 if (isl_int_is_zero(bmap->div[i][0]))
1565 continue;
1566 if (isl_int_is_one(bmap->div[i][0]))
1567 continue;
1568 for (j = 0; j < bmap->n_ineq; ++j) {
1569 int s;
1571 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1572 !isl_int_is_negone(bmap->ineq[j][total + i]))
1573 continue;
1575 *progress = 1;
1577 s = isl_int_sgn(bmap->ineq[j][total + i]);
1578 isl_int_set_si(bmap->ineq[j][total + i], 0);
1579 if (s < 0)
1580 isl_seq_combine(bmap->ineq[j],
1581 ctx->negone, bmap->div[i] + 1,
1582 bmap->div[i][0], bmap->ineq[j],
1583 total + bmap->n_div);
1584 else
1585 isl_seq_combine(bmap->ineq[j],
1586 ctx->one, bmap->div[i] + 1,
1587 bmap->div[i][0], bmap->ineq[j],
1588 total + bmap->n_div);
1589 if (s < 0) {
1590 isl_int_add(bmap->ineq[j][0],
1591 bmap->ineq[j][0], bmap->div[i][0]);
1592 isl_int_sub_ui(bmap->ineq[j][0],
1593 bmap->ineq[j][0], 1);
1596 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1597 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1598 return isl_basic_map_free(bmap);
1602 return bmap;
1605 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1607 int progress = 1;
1608 if (!bmap)
1609 return NULL;
1610 while (progress) {
1611 isl_bool empty;
1613 progress = 0;
1614 empty = isl_basic_map_plain_is_empty(bmap);
1615 if (empty < 0)
1616 return isl_basic_map_free(bmap);
1617 if (empty)
1618 break;
1619 bmap = isl_basic_map_normalize_constraints(bmap);
1620 bmap = reduce_div_coefficients(bmap);
1621 bmap = normalize_div_expressions(bmap);
1622 bmap = remove_duplicate_divs(bmap, &progress);
1623 bmap = eliminate_unit_divs(bmap, &progress);
1624 bmap = eliminate_divs_eq(bmap, &progress);
1625 bmap = eliminate_divs_ineq(bmap, &progress);
1626 bmap = isl_basic_map_gauss(bmap, &progress);
1627 /* requires equalities in normal form */
1628 bmap = normalize_divs(bmap, &progress);
1629 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1630 &progress, 1);
1631 if (bmap && progress)
1632 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1634 return bmap;
1637 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1639 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1643 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1644 isl_int *constraint, unsigned div)
1646 unsigned pos;
1648 if (!bmap)
1649 return -1;
1651 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1653 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1654 int neg;
1655 isl_int_sub(bmap->div[div][1],
1656 bmap->div[div][1], bmap->div[div][0]);
1657 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1658 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1659 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1660 isl_int_add(bmap->div[div][1],
1661 bmap->div[div][1], bmap->div[div][0]);
1662 if (!neg)
1663 return 0;
1664 if (isl_seq_first_non_zero(constraint+pos+1,
1665 bmap->n_div-div-1) != -1)
1666 return 0;
1667 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1668 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1669 return 0;
1670 if (isl_seq_first_non_zero(constraint+pos+1,
1671 bmap->n_div-div-1) != -1)
1672 return 0;
1673 } else
1674 return 0;
1676 return 1;
1679 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1680 isl_int *constraint, unsigned div)
1682 return isl_basic_map_is_div_constraint(bset, constraint, div);
1686 /* If the only constraints a div d=floor(f/m)
1687 * appears in are its two defining constraints
1689 * f - m d >=0
1690 * -(f - (m - 1)) + m d >= 0
1692 * then it can safely be removed.
1694 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1696 int i;
1697 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1699 for (i = 0; i < bmap->n_eq; ++i)
1700 if (!isl_int_is_zero(bmap->eq[i][pos]))
1701 return 0;
1703 for (i = 0; i < bmap->n_ineq; ++i) {
1704 if (isl_int_is_zero(bmap->ineq[i][pos]))
1705 continue;
1706 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1707 return 0;
1710 for (i = 0; i < bmap->n_div; ++i) {
1711 if (isl_int_is_zero(bmap->div[i][0]))
1712 continue;
1713 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1714 return 0;
1717 return 1;
1721 * Remove divs that don't occur in any of the constraints or other divs.
1722 * These can arise when dropping constraints from a basic map or
1723 * when the divs of a basic map have been temporarily aligned
1724 * with the divs of another basic map.
1726 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1728 int i;
1730 if (!bmap)
1731 return NULL;
1733 for (i = bmap->n_div-1; i >= 0; --i) {
1734 if (!div_is_redundant(bmap, i))
1735 continue;
1736 bmap = isl_basic_map_drop_div(bmap, i);
1738 return bmap;
1741 /* Mark "bmap" as final, without checking for obviously redundant
1742 * integer divisions. This function should be used when "bmap"
1743 * is known not to involve any such integer divisions.
1745 __isl_give isl_basic_map *isl_basic_map_mark_final(
1746 __isl_take isl_basic_map *bmap)
1748 if (!bmap)
1749 return NULL;
1750 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1751 return bmap;
1754 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1756 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1758 bmap = remove_redundant_divs(bmap);
1759 bmap = isl_basic_map_mark_final(bmap);
1760 return bmap;
1763 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1765 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1768 struct isl_set *isl_set_finalize(struct isl_set *set)
1770 int i;
1772 if (!set)
1773 return NULL;
1774 for (i = 0; i < set->n; ++i) {
1775 set->p[i] = isl_basic_set_finalize(set->p[i]);
1776 if (!set->p[i])
1777 goto error;
1779 return set;
1780 error:
1781 isl_set_free(set);
1782 return NULL;
1785 struct isl_map *isl_map_finalize(struct isl_map *map)
1787 int i;
1789 if (!map)
1790 return NULL;
1791 for (i = 0; i < map->n; ++i) {
1792 map->p[i] = isl_basic_map_finalize(map->p[i]);
1793 if (!map->p[i])
1794 goto error;
1796 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1797 return map;
1798 error:
1799 isl_map_free(map);
1800 return NULL;
1804 /* Remove definition of any div that is defined in terms of the given variable.
1805 * The div itself is not removed. Functions such as
1806 * eliminate_divs_ineq depend on the other divs remaining in place.
1808 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1809 int pos)
1811 int i;
1813 if (!bmap)
1814 return NULL;
1816 for (i = 0; i < bmap->n_div; ++i) {
1817 if (isl_int_is_zero(bmap->div[i][0]))
1818 continue;
1819 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1820 continue;
1821 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1822 if (!bmap)
1823 return NULL;
1825 return bmap;
1828 /* Eliminate the specified variables from the constraints using
1829 * Fourier-Motzkin. The variables themselves are not removed.
1831 struct isl_basic_map *isl_basic_map_eliminate_vars(
1832 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1834 int d;
1835 int i, j, k;
1836 unsigned total;
1837 int need_gauss = 0;
1839 if (n == 0)
1840 return bmap;
1841 if (!bmap)
1842 return NULL;
1843 total = isl_basic_map_total_dim(bmap);
1845 bmap = isl_basic_map_cow(bmap);
1846 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1847 bmap = remove_dependent_vars(bmap, d);
1848 if (!bmap)
1849 return NULL;
1851 for (d = pos + n - 1;
1852 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1853 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1854 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1855 int n_lower, n_upper;
1856 if (!bmap)
1857 return NULL;
1858 for (i = 0; i < bmap->n_eq; ++i) {
1859 if (isl_int_is_zero(bmap->eq[i][1+d]))
1860 continue;
1861 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1862 isl_basic_map_drop_equality(bmap, i);
1863 need_gauss = 1;
1864 break;
1866 if (i < bmap->n_eq)
1867 continue;
1868 n_lower = 0;
1869 n_upper = 0;
1870 for (i = 0; i < bmap->n_ineq; ++i) {
1871 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1872 n_lower++;
1873 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1874 n_upper++;
1876 bmap = isl_basic_map_extend_constraints(bmap,
1877 0, n_lower * n_upper);
1878 if (!bmap)
1879 goto error;
1880 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1881 int last;
1882 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1883 continue;
1884 last = -1;
1885 for (j = 0; j < i; ++j) {
1886 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1887 continue;
1888 last = j;
1889 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1890 isl_int_sgn(bmap->ineq[j][1+d]))
1891 continue;
1892 k = isl_basic_map_alloc_inequality(bmap);
1893 if (k < 0)
1894 goto error;
1895 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1896 1+total);
1897 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1898 1+d, 1+total, NULL);
1900 isl_basic_map_drop_inequality(bmap, i);
1901 i = last + 1;
1903 if (n_lower > 0 && n_upper > 0) {
1904 bmap = isl_basic_map_normalize_constraints(bmap);
1905 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1906 NULL, 0);
1907 bmap = isl_basic_map_gauss(bmap, NULL);
1908 bmap = isl_basic_map_remove_redundancies(bmap);
1909 need_gauss = 0;
1910 if (!bmap)
1911 goto error;
1912 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1913 break;
1916 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1917 if (need_gauss)
1918 bmap = isl_basic_map_gauss(bmap, NULL);
1919 return bmap;
1920 error:
1921 isl_basic_map_free(bmap);
1922 return NULL;
1925 struct isl_basic_set *isl_basic_set_eliminate_vars(
1926 struct isl_basic_set *bset, unsigned pos, unsigned n)
1928 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1929 pos, n));
1932 /* Eliminate the specified n dimensions starting at first from the
1933 * constraints, without removing the dimensions from the space.
1934 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1935 * Otherwise, they are projected out and the original space is restored.
1937 __isl_give isl_basic_map *isl_basic_map_eliminate(
1938 __isl_take isl_basic_map *bmap,
1939 enum isl_dim_type type, unsigned first, unsigned n)
1941 isl_space *space;
1943 if (!bmap)
1944 return NULL;
1945 if (n == 0)
1946 return bmap;
1948 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1949 isl_die(bmap->ctx, isl_error_invalid,
1950 "index out of bounds", goto error);
1952 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1953 first += isl_basic_map_offset(bmap, type) - 1;
1954 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1955 return isl_basic_map_finalize(bmap);
1958 space = isl_basic_map_get_space(bmap);
1959 bmap = isl_basic_map_project_out(bmap, type, first, n);
1960 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1961 bmap = isl_basic_map_reset_space(bmap, space);
1962 return bmap;
1963 error:
1964 isl_basic_map_free(bmap);
1965 return NULL;
1968 __isl_give isl_basic_set *isl_basic_set_eliminate(
1969 __isl_take isl_basic_set *bset,
1970 enum isl_dim_type type, unsigned first, unsigned n)
1972 return isl_basic_map_eliminate(bset, type, first, n);
1975 /* Remove all constraints from "bmap" that reference any unknown local
1976 * variables (directly or indirectly).
1978 * Dropping all constraints on a local variable will make it redundant,
1979 * so it will get removed implicitly by
1980 * isl_basic_map_drop_constraints_involving_dims. Some other local
1981 * variables may also end up becoming redundant if they only appear
1982 * in constraints together with the unknown local variable.
1983 * Therefore, start over after calling
1984 * isl_basic_map_drop_constraints_involving_dims.
1986 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1987 __isl_take isl_basic_map *bmap)
1989 isl_bool known;
1990 int i, n_div, o_div;
1992 known = isl_basic_map_divs_known(bmap);
1993 if (known < 0)
1994 return isl_basic_map_free(bmap);
1995 if (known)
1996 return bmap;
1998 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1999 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
2001 for (i = 0; i < n_div; ++i) {
2002 known = isl_basic_map_div_is_known(bmap, i);
2003 if (known < 0)
2004 return isl_basic_map_free(bmap);
2005 if (known)
2006 continue;
2007 bmap = remove_dependent_vars(bmap, o_div + i);
2008 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
2009 isl_dim_div, i, 1);
2010 if (!bmap)
2011 return NULL;
2012 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2013 i = -1;
2016 return bmap;
2019 /* Remove all constraints from "map" that reference any unknown local
2020 * variables (directly or indirectly).
2022 * Since constraints may get dropped from the basic maps,
2023 * they may no longer be disjoint from each other.
2025 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
2026 __isl_take isl_map *map)
2028 int i;
2029 isl_bool known;
2031 known = isl_map_divs_known(map);
2032 if (known < 0)
2033 return isl_map_free(map);
2034 if (known)
2035 return map;
2037 map = isl_map_cow(map);
2038 if (!map)
2039 return NULL;
2041 for (i = 0; i < map->n; ++i) {
2042 map->p[i] =
2043 isl_basic_map_drop_constraint_involving_unknown_divs(
2044 map->p[i]);
2045 if (!map->p[i])
2046 return isl_map_free(map);
2049 if (map->n > 1)
2050 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2052 return map;
2055 /* Don't assume equalities are in order, because align_divs
2056 * may have changed the order of the divs.
2058 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
2060 int d, i;
2061 unsigned total;
2063 total = isl_space_dim(bmap->dim, isl_dim_all);
2064 for (d = 0; d < total; ++d)
2065 elim[d] = -1;
2066 for (i = 0; i < bmap->n_eq; ++i) {
2067 for (d = total - 1; d >= 0; --d) {
2068 if (isl_int_is_zero(bmap->eq[i][1+d]))
2069 continue;
2070 elim[d] = i;
2071 break;
2076 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
2078 compute_elimination_index(bset_to_bmap(bset), elim);
2081 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2082 struct isl_basic_map *bmap, int *elim)
2084 int d;
2085 int copied = 0;
2086 unsigned total;
2088 total = isl_space_dim(bmap->dim, isl_dim_all);
2089 for (d = total - 1; d >= 0; --d) {
2090 if (isl_int_is_zero(src[1+d]))
2091 continue;
2092 if (elim[d] == -1)
2093 continue;
2094 if (!copied) {
2095 isl_seq_cpy(dst, src, 1 + total);
2096 copied = 1;
2098 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2100 return copied;
2103 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2104 struct isl_basic_set *bset, int *elim)
2106 return reduced_using_equalities(dst, src,
2107 bset_to_bmap(bset), elim);
2110 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
2111 struct isl_basic_set *bset, struct isl_basic_set *context)
2113 int i;
2114 int *elim;
2116 if (!bset || !context)
2117 goto error;
2119 if (context->n_eq == 0) {
2120 isl_basic_set_free(context);
2121 return bset;
2124 bset = isl_basic_set_cow(bset);
2125 if (!bset)
2126 goto error;
2128 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2129 if (!elim)
2130 goto error;
2131 set_compute_elimination_index(context, elim);
2132 for (i = 0; i < bset->n_eq; ++i)
2133 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2134 context, elim);
2135 for (i = 0; i < bset->n_ineq; ++i)
2136 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2137 context, elim);
2138 isl_basic_set_free(context);
2139 free(elim);
2140 bset = isl_basic_set_simplify(bset);
2141 bset = isl_basic_set_finalize(bset);
2142 return bset;
2143 error:
2144 isl_basic_set_free(bset);
2145 isl_basic_set_free(context);
2146 return NULL;
2149 /* For each inequality in "ineq" that is a shifted (more relaxed)
2150 * copy of an inequality in "context", mark the corresponding entry
2151 * in "row" with -1.
2152 * If an inequality only has a non-negative constant term, then
2153 * mark it as well.
2155 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2156 __isl_keep isl_basic_set *context, int *row)
2158 struct isl_constraint_index ci;
2159 int n_ineq;
2160 unsigned total;
2161 int k;
2163 if (!ineq || !context)
2164 return isl_stat_error;
2165 if (context->n_ineq == 0)
2166 return isl_stat_ok;
2167 if (setup_constraint_index(&ci, context) < 0)
2168 return isl_stat_error;
2170 n_ineq = isl_mat_rows(ineq);
2171 total = isl_mat_cols(ineq) - 1;
2172 for (k = 0; k < n_ineq; ++k) {
2173 int l;
2174 isl_bool redundant;
2176 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2177 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2178 row[k] = -1;
2179 continue;
2181 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2182 if (redundant < 0)
2183 goto error;
2184 if (!redundant)
2185 continue;
2186 row[k] = -1;
2188 constraint_index_free(&ci);
2189 return isl_stat_ok;
2190 error:
2191 constraint_index_free(&ci);
2192 return isl_stat_error;
2195 static struct isl_basic_set *remove_shifted_constraints(
2196 struct isl_basic_set *bset, struct isl_basic_set *context)
2198 struct isl_constraint_index ci;
2199 int k;
2201 if (!bset || !context)
2202 return bset;
2204 if (context->n_ineq == 0)
2205 return bset;
2206 if (setup_constraint_index(&ci, context) < 0)
2207 return bset;
2209 for (k = 0; k < bset->n_ineq; ++k) {
2210 isl_bool redundant;
2212 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2213 if (redundant < 0)
2214 goto error;
2215 if (!redundant)
2216 continue;
2217 bset = isl_basic_set_cow(bset);
2218 if (!bset)
2219 goto error;
2220 isl_basic_set_drop_inequality(bset, k);
2221 --k;
2223 constraint_index_free(&ci);
2224 return bset;
2225 error:
2226 constraint_index_free(&ci);
2227 return bset;
2230 /* Remove constraints from "bmap" that are identical to constraints
2231 * in "context" or that are more relaxed (greater constant term).
2233 * We perform the test for shifted copies on the pure constraints
2234 * in remove_shifted_constraints.
2236 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2237 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2239 isl_basic_set *bset, *bset_context;
2241 if (!bmap || !context)
2242 goto error;
2244 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2245 isl_basic_map_free(context);
2246 return bmap;
2249 context = isl_basic_map_align_divs(context, bmap);
2250 bmap = isl_basic_map_align_divs(bmap, context);
2252 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2253 bset_context = isl_basic_map_underlying_set(context);
2254 bset = remove_shifted_constraints(bset, bset_context);
2255 isl_basic_set_free(bset_context);
2257 bmap = isl_basic_map_overlying_set(bset, bmap);
2259 return bmap;
2260 error:
2261 isl_basic_map_free(bmap);
2262 isl_basic_map_free(context);
2263 return NULL;
2266 /* Does the (linear part of a) constraint "c" involve any of the "len"
2267 * "relevant" dimensions?
2269 static int is_related(isl_int *c, int len, int *relevant)
2271 int i;
2273 for (i = 0; i < len; ++i) {
2274 if (!relevant[i])
2275 continue;
2276 if (!isl_int_is_zero(c[i]))
2277 return 1;
2280 return 0;
2283 /* Drop constraints from "bmap" that do not involve any of
2284 * the dimensions marked "relevant".
2286 static __isl_give isl_basic_map *drop_unrelated_constraints(
2287 __isl_take isl_basic_map *bmap, int *relevant)
2289 int i, dim;
2291 dim = isl_basic_map_dim(bmap, isl_dim_all);
2292 for (i = 0; i < dim; ++i)
2293 if (!relevant[i])
2294 break;
2295 if (i >= dim)
2296 return bmap;
2298 for (i = bmap->n_eq - 1; i >= 0; --i)
2299 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2300 bmap = isl_basic_map_cow(bmap);
2301 if (isl_basic_map_drop_equality(bmap, i) < 0)
2302 return isl_basic_map_free(bmap);
2305 for (i = bmap->n_ineq - 1; i >= 0; --i)
2306 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2307 bmap = isl_basic_map_cow(bmap);
2308 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2309 return isl_basic_map_free(bmap);
2312 return bmap;
2315 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2317 * In particular, for any variable involved in the constraint,
2318 * find the actual group id from before and replace the group
2319 * of the corresponding variable by the minimal group of all
2320 * the variables involved in the constraint considered so far
2321 * (if this minimum is smaller) or replace the minimum by this group
2322 * (if the minimum is larger).
2324 * At the end, all the variables in "c" will (indirectly) point
2325 * to the minimal of the groups that they referred to originally.
2327 static void update_groups(int dim, int *group, isl_int *c)
2329 int j;
2330 int min = dim;
2332 for (j = 0; j < dim; ++j) {
2333 if (isl_int_is_zero(c[j]))
2334 continue;
2335 while (group[j] >= 0 && group[group[j]] != group[j])
2336 group[j] = group[group[j]];
2337 if (group[j] == min)
2338 continue;
2339 if (group[j] < min) {
2340 if (min >= 0 && min < dim)
2341 group[min] = group[j];
2342 min = group[j];
2343 } else
2344 group[group[j]] = min;
2348 /* Allocate an array of groups of variables, one for each variable
2349 * in "context", initialized to zero.
2351 static int *alloc_groups(__isl_keep isl_basic_set *context)
2353 isl_ctx *ctx;
2354 int dim;
2356 dim = isl_basic_set_dim(context, isl_dim_set);
2357 ctx = isl_basic_set_get_ctx(context);
2358 return isl_calloc_array(ctx, int, dim);
2361 /* Drop constraints from "bmap" that only involve variables that are
2362 * not related to any of the variables marked with a "-1" in "group".
2364 * We construct groups of variables that collect variables that
2365 * (indirectly) appear in some common constraint of "bmap".
2366 * Each group is identified by the first variable in the group,
2367 * except for the special group of variables that was already identified
2368 * in the input as -1 (or are related to those variables).
2369 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2370 * otherwise the group of i is the group of group[i].
2372 * We first initialize groups for the remaining variables.
2373 * Then we iterate over the constraints of "bmap" and update the
2374 * group of the variables in the constraint by the smallest group.
2375 * Finally, we resolve indirect references to groups by running over
2376 * the variables.
2378 * After computing the groups, we drop constraints that do not involve
2379 * any variables in the -1 group.
2381 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2382 __isl_take isl_basic_map *bmap, __isl_take int *group)
2384 int dim;
2385 int i;
2386 int last;
2388 if (!bmap)
2389 return NULL;
2391 dim = isl_basic_map_dim(bmap, isl_dim_all);
2393 last = -1;
2394 for (i = 0; i < dim; ++i)
2395 if (group[i] >= 0)
2396 last = group[i] = i;
2397 if (last < 0) {
2398 free(group);
2399 return bmap;
2402 for (i = 0; i < bmap->n_eq; ++i)
2403 update_groups(dim, group, bmap->eq[i] + 1);
2404 for (i = 0; i < bmap->n_ineq; ++i)
2405 update_groups(dim, group, bmap->ineq[i] + 1);
2407 for (i = 0; i < dim; ++i)
2408 if (group[i] >= 0)
2409 group[i] = group[group[i]];
2411 for (i = 0; i < dim; ++i)
2412 group[i] = group[i] == -1;
2414 bmap = drop_unrelated_constraints(bmap, group);
2416 free(group);
2417 return bmap;
2420 /* Drop constraints from "context" that are irrelevant for computing
2421 * the gist of "bset".
2423 * In particular, drop constraints in variables that are not related
2424 * to any of the variables involved in the constraints of "bset"
2425 * in the sense that there is no sequence of constraints that connects them.
2427 * We first mark all variables that appear in "bset" as belonging
2428 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2430 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2431 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2433 int *group;
2434 int dim;
2435 int i, j;
2437 if (!context || !bset)
2438 return isl_basic_set_free(context);
2440 group = alloc_groups(context);
2442 if (!group)
2443 return isl_basic_set_free(context);
2445 dim = isl_basic_set_dim(bset, isl_dim_set);
2446 for (i = 0; i < dim; ++i) {
2447 for (j = 0; j < bset->n_eq; ++j)
2448 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2449 break;
2450 if (j < bset->n_eq) {
2451 group[i] = -1;
2452 continue;
2454 for (j = 0; j < bset->n_ineq; ++j)
2455 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2456 break;
2457 if (j < bset->n_ineq)
2458 group[i] = -1;
2461 return isl_basic_map_drop_unrelated_constraints(context, group);
2464 /* Drop constraints from "context" that are irrelevant for computing
2465 * the gist of the inequalities "ineq".
2466 * Inequalities in "ineq" for which the corresponding element of row
2467 * is set to -1 have already been marked for removal and should be ignored.
2469 * In particular, drop constraints in variables that are not related
2470 * to any of the variables involved in "ineq"
2471 * in the sense that there is no sequence of constraints that connects them.
2473 * We first mark all variables that appear in "bset" as belonging
2474 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2476 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2477 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2479 int *group;
2480 int dim;
2481 int i, j, n;
2483 if (!context || !ineq)
2484 return isl_basic_set_free(context);
2486 group = alloc_groups(context);
2488 if (!group)
2489 return isl_basic_set_free(context);
2491 dim = isl_basic_set_dim(context, isl_dim_set);
2492 n = isl_mat_rows(ineq);
2493 for (i = 0; i < dim; ++i) {
2494 for (j = 0; j < n; ++j) {
2495 if (row[j] < 0)
2496 continue;
2497 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2498 break;
2500 if (j < n)
2501 group[i] = -1;
2504 return isl_basic_map_drop_unrelated_constraints(context, group);
2507 /* Do all "n" entries of "row" contain a negative value?
2509 static int all_neg(int *row, int n)
2511 int i;
2513 for (i = 0; i < n; ++i)
2514 if (row[i] >= 0)
2515 return 0;
2517 return 1;
2520 /* Update the inequalities in "bset" based on the information in "row"
2521 * and "tab".
2523 * In particular, the array "row" contains either -1, meaning that
2524 * the corresponding inequality of "bset" is redundant, or the index
2525 * of an inequality in "tab".
2527 * If the row entry is -1, then drop the inequality.
2528 * Otherwise, if the constraint is marked redundant in the tableau,
2529 * then drop the inequality. Similarly, if it is marked as an equality
2530 * in the tableau, then turn the inequality into an equality and
2531 * perform Gaussian elimination.
2533 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2534 __isl_keep int *row, struct isl_tab *tab)
2536 int i;
2537 unsigned n_ineq;
2538 unsigned n_eq;
2539 int found_equality = 0;
2541 if (!bset)
2542 return NULL;
2543 if (tab && tab->empty)
2544 return isl_basic_set_set_to_empty(bset);
2546 n_ineq = bset->n_ineq;
2547 for (i = n_ineq - 1; i >= 0; --i) {
2548 if (row[i] < 0) {
2549 if (isl_basic_set_drop_inequality(bset, i) < 0)
2550 return isl_basic_set_free(bset);
2551 continue;
2553 if (!tab)
2554 continue;
2555 n_eq = tab->n_eq;
2556 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2557 isl_basic_map_inequality_to_equality(bset, i);
2558 found_equality = 1;
2559 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2560 if (isl_basic_set_drop_inequality(bset, i) < 0)
2561 return isl_basic_set_free(bset);
2565 if (found_equality)
2566 bset = isl_basic_set_gauss(bset, NULL);
2567 bset = isl_basic_set_finalize(bset);
2568 return bset;
2571 /* Update the inequalities in "bset" based on the information in "row"
2572 * and "tab" and free all arguments (other than "bset").
2574 static __isl_give isl_basic_set *update_ineq_free(
2575 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2576 __isl_take isl_basic_set *context, __isl_take int *row,
2577 struct isl_tab *tab)
2579 isl_mat_free(ineq);
2580 isl_basic_set_free(context);
2582 bset = update_ineq(bset, row, tab);
2584 free(row);
2585 isl_tab_free(tab);
2586 return bset;
2589 /* Remove all information from bset that is redundant in the context
2590 * of context.
2591 * "ineq" contains the (possibly transformed) inequalities of "bset",
2592 * in the same order.
2593 * The (explicit) equalities of "bset" are assumed to have been taken
2594 * into account by the transformation such that only the inequalities
2595 * are relevant.
2596 * "context" is assumed not to be empty.
2598 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2599 * A value of -1 means that the inequality is obviously redundant and may
2600 * not even appear in "tab".
2602 * We first mark the inequalities of "bset"
2603 * that are obviously redundant with respect to some inequality in "context".
2604 * Then we remove those constraints from "context" that have become
2605 * irrelevant for computing the gist of "bset".
2606 * Note that this removal of constraints cannot be replaced by
2607 * a factorization because factors in "bset" may still be connected
2608 * to each other through constraints in "context".
2610 * If there are any inequalities left, we construct a tableau for
2611 * the context and then add the inequalities of "bset".
2612 * Before adding these inequalities, we freeze all constraints such that
2613 * they won't be considered redundant in terms of the constraints of "bset".
2614 * Then we detect all redundant constraints (among the
2615 * constraints that weren't frozen), first by checking for redundancy in the
2616 * the tableau and then by checking if replacing a constraint by its negation
2617 * would lead to an empty set. This last step is fairly expensive
2618 * and could be optimized by more reuse of the tableau.
2619 * Finally, we update bset according to the results.
2621 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2622 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2624 int i, r;
2625 int *row = NULL;
2626 isl_ctx *ctx;
2627 isl_basic_set *combined = NULL;
2628 struct isl_tab *tab = NULL;
2629 unsigned n_eq, context_ineq;
2630 unsigned total;
2632 if (!bset || !ineq || !context)
2633 goto error;
2635 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2636 isl_basic_set_free(context);
2637 isl_mat_free(ineq);
2638 return bset;
2641 ctx = isl_basic_set_get_ctx(context);
2642 row = isl_calloc_array(ctx, int, bset->n_ineq);
2643 if (!row)
2644 goto error;
2646 if (mark_shifted_constraints(ineq, context, row) < 0)
2647 goto error;
2648 if (all_neg(row, bset->n_ineq))
2649 return update_ineq_free(bset, ineq, context, row, NULL);
2651 context = drop_irrelevant_constraints_marked(context, ineq, row);
2652 if (!context)
2653 goto error;
2654 if (isl_basic_set_plain_is_universe(context))
2655 return update_ineq_free(bset, ineq, context, row, NULL);
2657 n_eq = context->n_eq;
2658 context_ineq = context->n_ineq;
2659 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2660 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2661 tab = isl_tab_from_basic_set(combined, 0);
2662 for (i = 0; i < context_ineq; ++i)
2663 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2664 goto error;
2665 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2666 goto error;
2667 r = context_ineq;
2668 for (i = 0; i < bset->n_ineq; ++i) {
2669 if (row[i] < 0)
2670 continue;
2671 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2672 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2673 goto error;
2674 row[i] = r++;
2676 if (isl_tab_detect_implicit_equalities(tab) < 0)
2677 goto error;
2678 if (isl_tab_detect_redundant(tab) < 0)
2679 goto error;
2680 total = isl_basic_set_total_dim(bset);
2681 for (i = bset->n_ineq - 1; i >= 0; --i) {
2682 isl_basic_set *test;
2683 int is_empty;
2685 if (row[i] < 0)
2686 continue;
2687 r = row[i];
2688 if (tab->con[n_eq + r].is_redundant)
2689 continue;
2690 test = isl_basic_set_dup(combined);
2691 if (isl_inequality_negate(test, r) < 0)
2692 test = isl_basic_set_free(test);
2693 test = isl_basic_set_update_from_tab(test, tab);
2694 is_empty = isl_basic_set_is_empty(test);
2695 isl_basic_set_free(test);
2696 if (is_empty < 0)
2697 goto error;
2698 if (is_empty)
2699 tab->con[n_eq + r].is_redundant = 1;
2701 bset = update_ineq_free(bset, ineq, context, row, tab);
2702 if (bset) {
2703 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2704 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2707 isl_basic_set_free(combined);
2708 return bset;
2709 error:
2710 free(row);
2711 isl_mat_free(ineq);
2712 isl_tab_free(tab);
2713 isl_basic_set_free(combined);
2714 isl_basic_set_free(context);
2715 isl_basic_set_free(bset);
2716 return NULL;
2719 /* Extract the inequalities of "bset" as an isl_mat.
2721 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2723 unsigned total;
2724 isl_ctx *ctx;
2725 isl_mat *ineq;
2727 if (!bset)
2728 return NULL;
2730 ctx = isl_basic_set_get_ctx(bset);
2731 total = isl_basic_set_total_dim(bset);
2732 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2733 0, 1 + total);
2735 return ineq;
2738 /* Remove all information from "bset" that is redundant in the context
2739 * of "context", for the case where both "bset" and "context" are
2740 * full-dimensional.
2742 static __isl_give isl_basic_set *uset_gist_uncompressed(
2743 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2745 isl_mat *ineq;
2747 ineq = extract_ineq(bset);
2748 return uset_gist_full(bset, ineq, context);
2751 /* Remove all information from "bset" that is redundant in the context
2752 * of "context", for the case where the combined equalities of
2753 * "bset" and "context" allow for a compression that can be obtained
2754 * by preapplication of "T".
2756 * "bset" itself is not transformed by "T". Instead, the inequalities
2757 * are extracted from "bset" and those are transformed by "T".
2758 * uset_gist_full then determines which of the transformed inequalities
2759 * are redundant with respect to the transformed "context" and removes
2760 * the corresponding inequalities from "bset".
2762 * After preapplying "T" to the inequalities, any common factor is
2763 * removed from the coefficients. If this results in a tightening
2764 * of the constant term, then the same tightening is applied to
2765 * the corresponding untransformed inequality in "bset".
2766 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2768 * g f'(x) + r >= 0
2770 * with 0 <= r < g, then it is equivalent to
2772 * f'(x) >= 0
2774 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2775 * subspace compressed by T since the latter would be transformed to
2777 * g f'(x) >= 0
2779 static __isl_give isl_basic_set *uset_gist_compressed(
2780 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2781 __isl_take isl_mat *T)
2783 isl_ctx *ctx;
2784 isl_mat *ineq;
2785 int i, n_row, n_col;
2786 isl_int rem;
2788 ineq = extract_ineq(bset);
2789 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2790 context = isl_basic_set_preimage(context, T);
2792 if (!ineq || !context)
2793 goto error;
2794 if (isl_basic_set_plain_is_empty(context)) {
2795 isl_mat_free(ineq);
2796 isl_basic_set_free(context);
2797 return isl_basic_set_set_to_empty(bset);
2800 ctx = isl_mat_get_ctx(ineq);
2801 n_row = isl_mat_rows(ineq);
2802 n_col = isl_mat_cols(ineq);
2803 isl_int_init(rem);
2804 for (i = 0; i < n_row; ++i) {
2805 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2806 if (isl_int_is_zero(ctx->normalize_gcd))
2807 continue;
2808 if (isl_int_is_one(ctx->normalize_gcd))
2809 continue;
2810 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2811 ctx->normalize_gcd, n_col - 1);
2812 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2813 isl_int_fdiv_q(ineq->row[i][0],
2814 ineq->row[i][0], ctx->normalize_gcd);
2815 if (isl_int_is_zero(rem))
2816 continue;
2817 bset = isl_basic_set_cow(bset);
2818 if (!bset)
2819 break;
2820 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2822 isl_int_clear(rem);
2824 return uset_gist_full(bset, ineq, context);
2825 error:
2826 isl_mat_free(ineq);
2827 isl_basic_set_free(context);
2828 isl_basic_set_free(bset);
2829 return NULL;
2832 /* Project "bset" onto the variables that are involved in "template".
2834 static __isl_give isl_basic_set *project_onto_involved(
2835 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2837 int i, n;
2839 if (!bset || !template)
2840 return isl_basic_set_free(bset);
2842 n = isl_basic_set_dim(template, isl_dim_set);
2844 for (i = 0; i < n; ++i) {
2845 isl_bool involved;
2847 involved = isl_basic_set_involves_dims(template,
2848 isl_dim_set, i, 1);
2849 if (involved < 0)
2850 return isl_basic_set_free(bset);
2851 if (involved)
2852 continue;
2853 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2856 return bset;
2859 /* Remove all information from bset that is redundant in the context
2860 * of context. In particular, equalities that are linear combinations
2861 * of those in context are removed. Then the inequalities that are
2862 * redundant in the context of the equalities and inequalities of
2863 * context are removed.
2865 * First of all, we drop those constraints from "context"
2866 * that are irrelevant for computing the gist of "bset".
2867 * Alternatively, we could factorize the intersection of "context" and "bset".
2869 * We first compute the intersection of the integer affine hulls
2870 * of "bset" and "context",
2871 * compute the gist inside this intersection and then reduce
2872 * the constraints with respect to the equalities of the context
2873 * that only involve variables already involved in the input.
2875 * If two constraints are mutually redundant, then uset_gist_full
2876 * will remove the second of those constraints. We therefore first
2877 * sort the constraints so that constraints not involving existentially
2878 * quantified variables are given precedence over those that do.
2879 * We have to perform this sorting before the variable compression,
2880 * because that may effect the order of the variables.
2882 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2883 __isl_take isl_basic_set *context)
2885 isl_mat *eq;
2886 isl_mat *T;
2887 isl_basic_set *aff;
2888 isl_basic_set *aff_context;
2889 unsigned total;
2891 if (!bset || !context)
2892 goto error;
2894 context = drop_irrelevant_constraints(context, bset);
2896 bset = isl_basic_set_detect_equalities(bset);
2897 aff = isl_basic_set_copy(bset);
2898 aff = isl_basic_set_plain_affine_hull(aff);
2899 context = isl_basic_set_detect_equalities(context);
2900 aff_context = isl_basic_set_copy(context);
2901 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2902 aff = isl_basic_set_intersect(aff, aff_context);
2903 if (!aff)
2904 goto error;
2905 if (isl_basic_set_plain_is_empty(aff)) {
2906 isl_basic_set_free(bset);
2907 isl_basic_set_free(context);
2908 return aff;
2910 bset = isl_basic_set_sort_constraints(bset);
2911 if (aff->n_eq == 0) {
2912 isl_basic_set_free(aff);
2913 return uset_gist_uncompressed(bset, context);
2915 total = isl_basic_set_total_dim(bset);
2916 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2917 eq = isl_mat_cow(eq);
2918 T = isl_mat_variable_compression(eq, NULL);
2919 isl_basic_set_free(aff);
2920 if (T && T->n_col == 0) {
2921 isl_mat_free(T);
2922 isl_basic_set_free(context);
2923 return isl_basic_set_set_to_empty(bset);
2926 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2927 aff_context = project_onto_involved(aff_context, bset);
2929 bset = uset_gist_compressed(bset, context, T);
2930 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2932 if (bset) {
2933 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2934 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2937 return bset;
2938 error:
2939 isl_basic_set_free(bset);
2940 isl_basic_set_free(context);
2941 return NULL;
2944 /* Return the number of equality constraints in "bmap" that involve
2945 * local variables. This function assumes that Gaussian elimination
2946 * has been applied to the equality constraints.
2948 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2950 int i;
2951 int total, n_div;
2953 if (!bmap)
2954 return -1;
2956 if (bmap->n_eq == 0)
2957 return 0;
2959 total = isl_basic_map_dim(bmap, isl_dim_all);
2960 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2961 total -= n_div;
2963 for (i = 0; i < bmap->n_eq; ++i)
2964 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2965 n_div) == -1)
2966 return i;
2968 return bmap->n_eq;
2971 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2972 * The constraints are assumed not to involve any local variables.
2974 static __isl_give isl_basic_map *basic_map_from_equalities(
2975 __isl_take isl_space *space, __isl_take isl_mat *eq)
2977 int i, k;
2978 isl_basic_map *bmap = NULL;
2980 if (!space || !eq)
2981 goto error;
2983 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2984 isl_die(isl_space_get_ctx(space), isl_error_internal,
2985 "unexpected number of columns", goto error);
2987 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2988 0, eq->n_row, 0);
2989 for (i = 0; i < eq->n_row; ++i) {
2990 k = isl_basic_map_alloc_equality(bmap);
2991 if (k < 0)
2992 goto error;
2993 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2996 isl_space_free(space);
2997 isl_mat_free(eq);
2998 return bmap;
2999 error:
3000 isl_space_free(space);
3001 isl_mat_free(eq);
3002 isl_basic_map_free(bmap);
3003 return NULL;
3006 /* Construct and return a variable compression based on the equality
3007 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3008 * "n1" is the number of (initial) equality constraints in "bmap1"
3009 * that do involve local variables.
3010 * "n2" is the number of (initial) equality constraints in "bmap2"
3011 * that do involve local variables.
3012 * "total" is the total number of other variables.
3013 * This function assumes that Gaussian elimination
3014 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3015 * such that the equality constraints not involving local variables
3016 * are those that start at "n1" or "n2".
3018 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3019 * then simply compute the compression based on the equality constraints
3020 * in the other basic map.
3021 * Otherwise, combine the equality constraints from both into a new
3022 * basic map such that Gaussian elimination can be applied to this combination
3023 * and then construct a variable compression from the resulting
3024 * equality constraints.
3026 static __isl_give isl_mat *combined_variable_compression(
3027 __isl_keep isl_basic_map *bmap1, int n1,
3028 __isl_keep isl_basic_map *bmap2, int n2, int total)
3030 isl_ctx *ctx;
3031 isl_mat *E1, *E2, *V;
3032 isl_basic_map *bmap;
3034 ctx = isl_basic_map_get_ctx(bmap1);
3035 if (bmap1->n_eq == n1) {
3036 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3037 n2, bmap2->n_eq - n2, 0, 1 + total);
3038 return isl_mat_variable_compression(E2, NULL);
3040 if (bmap2->n_eq == n2) {
3041 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3042 n1, bmap1->n_eq - n1, 0, 1 + total);
3043 return isl_mat_variable_compression(E1, NULL);
3045 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3046 n1, bmap1->n_eq - n1, 0, 1 + total);
3047 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3048 n2, bmap2->n_eq - n2, 0, 1 + total);
3049 E1 = isl_mat_concat(E1, E2);
3050 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3051 bmap = isl_basic_map_gauss(bmap, NULL);
3052 if (!bmap)
3053 return NULL;
3054 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3055 V = isl_mat_variable_compression(E1, NULL);
3056 isl_basic_map_free(bmap);
3058 return V;
3061 /* Extract the stride constraints from "bmap", compressed
3062 * with respect to both the stride constraints in "context" and
3063 * the remaining equality constraints in both "bmap" and "context".
3064 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3065 * "context_n_eq" is the number of (initial) stride constraints in "context".
3067 * Let x be all variables in "bmap" (and "context") other than the local
3068 * variables. First compute a variable compression
3070 * x = V x'
3072 * based on the non-stride equality constraints in "bmap" and "context".
3073 * Consider the stride constraints of "context",
3075 * A(x) + B(y) = 0
3077 * with y the local variables and plug in the variable compression,
3078 * resulting in
3080 * A(V x') + B(y) = 0
3082 * Use these constraints to compute a parameter compression on x'
3084 * x' = T x''
3086 * Now consider the stride constraints of "bmap"
3088 * C(x) + D(y) = 0
3090 * and plug in x = V*T x''.
3091 * That is, return A = [C*V*T D].
3093 static __isl_give isl_mat *extract_compressed_stride_constraints(
3094 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3095 __isl_keep isl_basic_map *context, int context_n_eq)
3097 int total, n_div;
3098 isl_ctx *ctx;
3099 isl_mat *A, *B, *T, *V;
3101 total = isl_basic_map_dim(context, isl_dim_all);
3102 n_div = isl_basic_map_dim(context, isl_dim_div);
3103 total -= n_div;
3105 ctx = isl_basic_map_get_ctx(bmap);
3107 V = combined_variable_compression(bmap, bmap_n_eq,
3108 context, context_n_eq, total);
3110 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3111 B = isl_mat_sub_alloc6(ctx, context->eq,
3112 0, context_n_eq, 1 + total, n_div);
3113 A = isl_mat_product(A, isl_mat_copy(V));
3114 T = isl_mat_parameter_compression_ext(A, B);
3115 T = isl_mat_product(V, T);
3117 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3118 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3120 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3121 0, bmap_n_eq, 0, 1 + total + n_div);
3122 A = isl_mat_product(A, T);
3124 return A;
3127 /* Remove the prime factors from *g that have an exponent that
3128 * is strictly smaller than the exponent in "c".
3129 * All exponents in *g are known to be smaller than or equal
3130 * to those in "c".
3132 * That is, if *g is equal to
3134 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3136 * and "c" is equal to
3138 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3140 * then update *g to
3142 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3143 * p_n^{e_n * (e_n = f_n)}
3145 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3146 * neither does the gcd of *g and c / *g.
3147 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3148 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3149 * Dividing *g by this gcd therefore strictly reduces the exponent
3150 * of the prime factors that need to be removed, while leaving the
3151 * other prime factors untouched.
3152 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3153 * removes all undesired factors, without removing any others.
3155 static void remove_incomplete_powers(isl_int *g, isl_int c)
3157 isl_int t;
3159 isl_int_init(t);
3160 for (;;) {
3161 isl_int_divexact(t, c, *g);
3162 isl_int_gcd(t, t, *g);
3163 if (isl_int_is_one(t))
3164 break;
3165 isl_int_divexact(*g, *g, t);
3167 isl_int_clear(t);
3170 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3171 * of the same stride constraints in a compressed space that exploits
3172 * all equalities in the context and the other equalities in "bmap".
3174 * If the stride constraints of "bmap" are of the form
3176 * C(x) + D(y) = 0
3178 * then A is of the form
3180 * B(x') + D(y) = 0
3182 * If any of these constraints involves only a single local variable y,
3183 * then the constraint appears as
3185 * f(x) + m y_i = 0
3187 * in "bmap" and as
3189 * h(x') + m y_i = 0
3191 * in "A".
3193 * Let g be the gcd of m and the coefficients of h.
3194 * Then, in particular, g is a divisor of the coefficients of h and
3196 * f(x) = h(x')
3198 * is known to be a multiple of g.
3199 * If some prime factor in m appears with the same exponent in g,
3200 * then it can be removed from m because f(x) is already known
3201 * to be a multiple of g and therefore in particular of this power
3202 * of the prime factors.
3203 * Prime factors that appear with a smaller exponent in g cannot
3204 * be removed from m.
3205 * Let g' be the divisor of g containing all prime factors that
3206 * appear with the same exponent in m and g, then
3208 * f(x) + m y_i = 0
3210 * can be replaced by
3212 * f(x) + m/g' y_i' = 0
3214 * Note that (if g' != 1) this changes the explicit representation
3215 * of y_i to that of y_i', so the integer division at position i
3216 * is marked unknown and later recomputed by a call to
3217 * isl_basic_map_gauss.
3219 static __isl_give isl_basic_map *reduce_stride_constraints(
3220 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3222 int i;
3223 int total, n_div;
3224 int any = 0;
3225 isl_int gcd;
3227 if (!bmap || !A)
3228 return isl_basic_map_free(bmap);
3230 total = isl_basic_map_dim(bmap, isl_dim_all);
3231 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3232 total -= n_div;
3234 isl_int_init(gcd);
3235 for (i = 0; i < n; ++i) {
3236 int div;
3238 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3239 if (div < 0)
3240 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3241 "equality constraints modified unexpectedly",
3242 goto error);
3243 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3244 n_div - div - 1) != -1)
3245 continue;
3246 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3247 goto error;
3248 if (isl_int_is_one(gcd))
3249 continue;
3250 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3251 if (isl_int_is_one(gcd))
3252 continue;
3253 isl_int_divexact(bmap->eq[i][1 + total + div],
3254 bmap->eq[i][1 + total + div], gcd);
3255 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3256 if (!bmap)
3257 goto error;
3258 any = 1;
3260 isl_int_clear(gcd);
3262 if (any)
3263 bmap = isl_basic_map_gauss(bmap, NULL);
3265 return bmap;
3266 error:
3267 isl_int_clear(gcd);
3268 isl_basic_map_free(bmap);
3269 return NULL;
3272 /* Simplify the stride constraints in "bmap" based on
3273 * the remaining equality constraints in "bmap" and all equality
3274 * constraints in "context".
3275 * Only do this if both "bmap" and "context" have stride constraints.
3277 * First extract a copy of the stride constraints in "bmap" in a compressed
3278 * space exploiting all the other equality constraints and then
3279 * use this compressed copy to simplify the original stride constraints.
3281 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3282 __isl_keep isl_basic_map *context)
3284 int bmap_n_eq, context_n_eq;
3285 isl_mat *A;
3287 if (!bmap || !context)
3288 return isl_basic_map_free(bmap);
3290 bmap_n_eq = n_div_eq(bmap);
3291 context_n_eq = n_div_eq(context);
3293 if (bmap_n_eq < 0 || context_n_eq < 0)
3294 return isl_basic_map_free(bmap);
3295 if (bmap_n_eq == 0 || context_n_eq == 0)
3296 return bmap;
3298 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3299 context, context_n_eq);
3300 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3302 isl_mat_free(A);
3304 return bmap;
3307 /* Return a basic map that has the same intersection with "context" as "bmap"
3308 * and that is as "simple" as possible.
3310 * The core computation is performed on the pure constraints.
3311 * When we add back the meaning of the integer divisions, we need
3312 * to (re)introduce the div constraints. If we happen to have
3313 * discovered that some of these integer divisions are equal to
3314 * some affine combination of other variables, then these div
3315 * constraints may end up getting simplified in terms of the equalities,
3316 * resulting in extra inequalities on the other variables that
3317 * may have been removed already or that may not even have been
3318 * part of the input. We try and remove those constraints of
3319 * this form that are most obviously redundant with respect to
3320 * the context. We also remove those div constraints that are
3321 * redundant with respect to the other constraints in the result.
3323 * The stride constraints among the equality constraints in "bmap" are
3324 * also simplified with respecting to the other equality constraints
3325 * in "bmap" and with respect to all equality constraints in "context".
3327 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3328 struct isl_basic_map *context)
3330 isl_basic_set *bset, *eq;
3331 isl_basic_map *eq_bmap;
3332 unsigned total, n_div, extra, n_eq, n_ineq;
3334 if (!bmap || !context)
3335 goto error;
3337 if (isl_basic_map_plain_is_universe(bmap)) {
3338 isl_basic_map_free(context);
3339 return bmap;
3341 if (isl_basic_map_plain_is_empty(context)) {
3342 isl_space *space = isl_basic_map_get_space(bmap);
3343 isl_basic_map_free(bmap);
3344 isl_basic_map_free(context);
3345 return isl_basic_map_universe(space);
3347 if (isl_basic_map_plain_is_empty(bmap)) {
3348 isl_basic_map_free(context);
3349 return bmap;
3352 bmap = isl_basic_map_remove_redundancies(bmap);
3353 context = isl_basic_map_remove_redundancies(context);
3354 if (!context)
3355 goto error;
3357 context = isl_basic_map_align_divs(context, bmap);
3358 n_div = isl_basic_map_dim(context, isl_dim_div);
3359 total = isl_basic_map_dim(bmap, isl_dim_all);
3360 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3362 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3363 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3364 bset = uset_gist(bset,
3365 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3366 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3368 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3369 isl_basic_set_plain_is_empty(bset)) {
3370 isl_basic_map_free(context);
3371 return isl_basic_map_overlying_set(bset, bmap);
3374 n_eq = bset->n_eq;
3375 n_ineq = bset->n_ineq;
3376 eq = isl_basic_set_copy(bset);
3377 eq = isl_basic_set_cow(eq);
3378 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3379 eq = isl_basic_set_free(eq);
3380 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3381 bset = isl_basic_set_free(bset);
3383 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3384 eq_bmap = gist_strides(eq_bmap, context);
3385 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3386 bmap = isl_basic_map_overlying_set(bset, bmap);
3387 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3388 bmap = isl_basic_map_remove_redundancies(bmap);
3390 return bmap;
3391 error:
3392 isl_basic_map_free(bmap);
3393 isl_basic_map_free(context);
3394 return NULL;
3398 * Assumes context has no implicit divs.
3400 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3401 __isl_take isl_basic_map *context)
3403 int i;
3405 if (!map || !context)
3406 goto error;
3408 if (isl_basic_map_plain_is_empty(context)) {
3409 isl_space *space = isl_map_get_space(map);
3410 isl_map_free(map);
3411 isl_basic_map_free(context);
3412 return isl_map_universe(space);
3415 context = isl_basic_map_remove_redundancies(context);
3416 map = isl_map_cow(map);
3417 if (!map || !context)
3418 goto error;
3419 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3420 map = isl_map_compute_divs(map);
3421 if (!map)
3422 goto error;
3423 for (i = map->n - 1; i >= 0; --i) {
3424 map->p[i] = isl_basic_map_gist(map->p[i],
3425 isl_basic_map_copy(context));
3426 if (!map->p[i])
3427 goto error;
3428 if (isl_basic_map_plain_is_empty(map->p[i])) {
3429 isl_basic_map_free(map->p[i]);
3430 if (i != map->n - 1)
3431 map->p[i] = map->p[map->n - 1];
3432 map->n--;
3435 isl_basic_map_free(context);
3436 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3437 return map;
3438 error:
3439 isl_map_free(map);
3440 isl_basic_map_free(context);
3441 return NULL;
3444 /* Drop all inequalities from "bmap" that also appear in "context".
3445 * "context" is assumed to have only known local variables and
3446 * the initial local variables of "bmap" are assumed to be the same
3447 * as those of "context".
3448 * The constraints of both "bmap" and "context" are assumed
3449 * to have been sorted using isl_basic_map_sort_constraints.
3451 * Run through the inequality constraints of "bmap" and "context"
3452 * in sorted order.
3453 * If a constraint of "bmap" involves variables not in "context",
3454 * then it cannot appear in "context".
3455 * If a matching constraint is found, it is removed from "bmap".
3457 static __isl_give isl_basic_map *drop_inequalities(
3458 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3460 int i1, i2;
3461 unsigned total, extra;
3463 if (!bmap || !context)
3464 return isl_basic_map_free(bmap);
3466 total = isl_basic_map_total_dim(context);
3467 extra = isl_basic_map_total_dim(bmap) - total;
3469 i1 = bmap->n_ineq - 1;
3470 i2 = context->n_ineq - 1;
3471 while (bmap && i1 >= 0 && i2 >= 0) {
3472 int cmp;
3474 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3475 extra) != -1) {
3476 --i1;
3477 continue;
3479 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3480 context->ineq[i2]);
3481 if (cmp < 0) {
3482 --i2;
3483 continue;
3485 if (cmp > 0) {
3486 --i1;
3487 continue;
3489 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3490 bmap = isl_basic_map_cow(bmap);
3491 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3492 bmap = isl_basic_map_free(bmap);
3494 --i1;
3495 --i2;
3498 return bmap;
3501 /* Drop all equalities from "bmap" that also appear in "context".
3502 * "context" is assumed to have only known local variables and
3503 * the initial local variables of "bmap" are assumed to be the same
3504 * as those of "context".
3506 * Run through the equality constraints of "bmap" and "context"
3507 * in sorted order.
3508 * If a constraint of "bmap" involves variables not in "context",
3509 * then it cannot appear in "context".
3510 * If a matching constraint is found, it is removed from "bmap".
3512 static __isl_give isl_basic_map *drop_equalities(
3513 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3515 int i1, i2;
3516 unsigned total, extra;
3518 if (!bmap || !context)
3519 return isl_basic_map_free(bmap);
3521 total = isl_basic_map_total_dim(context);
3522 extra = isl_basic_map_total_dim(bmap) - total;
3524 i1 = bmap->n_eq - 1;
3525 i2 = context->n_eq - 1;
3527 while (bmap && i1 >= 0 && i2 >= 0) {
3528 int last1, last2;
3530 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3531 extra) != -1)
3532 break;
3533 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3534 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3535 if (last1 > last2) {
3536 --i2;
3537 continue;
3539 if (last1 < last2) {
3540 --i1;
3541 continue;
3543 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3544 bmap = isl_basic_map_cow(bmap);
3545 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3546 bmap = isl_basic_map_free(bmap);
3548 --i1;
3549 --i2;
3552 return bmap;
3555 /* Remove the constraints in "context" from "bmap".
3556 * "context" is assumed to have explicit representations
3557 * for all local variables.
3559 * First align the divs of "bmap" to those of "context" and
3560 * sort the constraints. Then drop all constraints from "bmap"
3561 * that appear in "context".
3563 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3564 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3566 isl_bool done, known;
3568 done = isl_basic_map_plain_is_universe(context);
3569 if (done == isl_bool_false)
3570 done = isl_basic_map_plain_is_universe(bmap);
3571 if (done == isl_bool_false)
3572 done = isl_basic_map_plain_is_empty(context);
3573 if (done == isl_bool_false)
3574 done = isl_basic_map_plain_is_empty(bmap);
3575 if (done < 0)
3576 goto error;
3577 if (done) {
3578 isl_basic_map_free(context);
3579 return bmap;
3581 known = isl_basic_map_divs_known(context);
3582 if (known < 0)
3583 goto error;
3584 if (!known)
3585 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3586 "context has unknown divs", goto error);
3588 bmap = isl_basic_map_align_divs(bmap, context);
3589 bmap = isl_basic_map_gauss(bmap, NULL);
3590 bmap = isl_basic_map_sort_constraints(bmap);
3591 context = isl_basic_map_sort_constraints(context);
3593 bmap = drop_inequalities(bmap, context);
3594 bmap = drop_equalities(bmap, context);
3596 isl_basic_map_free(context);
3597 bmap = isl_basic_map_finalize(bmap);
3598 return bmap;
3599 error:
3600 isl_basic_map_free(bmap);
3601 isl_basic_map_free(context);
3602 return NULL;
3605 /* Replace "map" by the disjunct at position "pos" and free "context".
3607 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3608 int pos, __isl_take isl_basic_map *context)
3610 isl_basic_map *bmap;
3612 bmap = isl_basic_map_copy(map->p[pos]);
3613 isl_map_free(map);
3614 isl_basic_map_free(context);
3615 return isl_map_from_basic_map(bmap);
3618 /* Remove the constraints in "context" from "map".
3619 * If any of the disjuncts in the result turns out to be the universe,
3620 * then return this universe.
3621 * "context" is assumed to have explicit representations
3622 * for all local variables.
3624 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3625 __isl_take isl_basic_map *context)
3627 int i;
3628 isl_bool univ, known;
3630 univ = isl_basic_map_plain_is_universe(context);
3631 if (univ < 0)
3632 goto error;
3633 if (univ) {
3634 isl_basic_map_free(context);
3635 return map;
3637 known = isl_basic_map_divs_known(context);
3638 if (known < 0)
3639 goto error;
3640 if (!known)
3641 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3642 "context has unknown divs", goto error);
3644 map = isl_map_cow(map);
3645 if (!map)
3646 goto error;
3647 for (i = 0; i < map->n; ++i) {
3648 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3649 isl_basic_map_copy(context));
3650 univ = isl_basic_map_plain_is_universe(map->p[i]);
3651 if (univ < 0)
3652 goto error;
3653 if (univ && map->n > 1)
3654 return replace_by_disjunct(map, i, context);
3657 isl_basic_map_free(context);
3658 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3659 if (map->n > 1)
3660 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3661 return map;
3662 error:
3663 isl_map_free(map);
3664 isl_basic_map_free(context);
3665 return NULL;
3668 /* Replace "map" by a universe map in the same space and free "drop".
3670 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3671 __isl_take isl_map *drop)
3673 isl_map *res;
3675 res = isl_map_universe(isl_map_get_space(map));
3676 isl_map_free(map);
3677 isl_map_free(drop);
3678 return res;
3681 /* Return a map that has the same intersection with "context" as "map"
3682 * and that is as "simple" as possible.
3684 * If "map" is already the universe, then we cannot make it any simpler.
3685 * Similarly, if "context" is the universe, then we cannot exploit it
3686 * to simplify "map"
3687 * If "map" and "context" are identical to each other, then we can
3688 * return the corresponding universe.
3690 * If either "map" or "context" consists of multiple disjuncts,
3691 * then check if "context" happens to be a subset of "map",
3692 * in which case all constraints can be removed.
3693 * In case of multiple disjuncts, the standard procedure
3694 * may not be able to detect that all constraints can be removed.
3696 * If none of these cases apply, we have to work a bit harder.
3697 * During this computation, we make use of a single disjunct context,
3698 * so if the original context consists of more than one disjunct
3699 * then we need to approximate the context by a single disjunct set.
3700 * Simply taking the simple hull may drop constraints that are
3701 * only implicitly available in each disjunct. We therefore also
3702 * look for constraints among those defining "map" that are valid
3703 * for the context. These can then be used to simplify away
3704 * the corresponding constraints in "map".
3706 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3707 __isl_take isl_map *context)
3709 int equal;
3710 int is_universe;
3711 int single_disjunct_map, single_disjunct_context;
3712 isl_bool subset;
3713 isl_basic_map *hull;
3715 is_universe = isl_map_plain_is_universe(map);
3716 if (is_universe >= 0 && !is_universe)
3717 is_universe = isl_map_plain_is_universe(context);
3718 if (is_universe < 0)
3719 goto error;
3720 if (is_universe) {
3721 isl_map_free(context);
3722 return map;
3725 equal = isl_map_plain_is_equal(map, context);
3726 if (equal < 0)
3727 goto error;
3728 if (equal)
3729 return replace_by_universe(map, context);
3731 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3732 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3733 if (!single_disjunct_map || !single_disjunct_context) {
3734 subset = isl_map_is_subset(context, map);
3735 if (subset < 0)
3736 goto error;
3737 if (subset)
3738 return replace_by_universe(map, context);
3741 context = isl_map_compute_divs(context);
3742 if (!context)
3743 goto error;
3744 if (single_disjunct_context) {
3745 hull = isl_map_simple_hull(context);
3746 } else {
3747 isl_ctx *ctx;
3748 isl_map_list *list;
3750 ctx = isl_map_get_ctx(map);
3751 list = isl_map_list_alloc(ctx, 2);
3752 list = isl_map_list_add(list, isl_map_copy(context));
3753 list = isl_map_list_add(list, isl_map_copy(map));
3754 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3755 list);
3757 return isl_map_gist_basic_map(map, hull);
3758 error:
3759 isl_map_free(map);
3760 isl_map_free(context);
3761 return NULL;
3764 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3765 __isl_take isl_map *context)
3767 return isl_map_align_params_map_map_and(map, context, &map_gist);
3770 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3771 struct isl_basic_set *context)
3773 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3774 bset_to_bmap(context)));
3777 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3778 __isl_take isl_basic_set *context)
3780 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3781 bset_to_bmap(context)));
3784 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3785 __isl_take isl_basic_set *context)
3787 isl_space *space = isl_set_get_space(set);
3788 isl_basic_set *dom_context = isl_basic_set_universe(space);
3789 dom_context = isl_basic_set_intersect_params(dom_context, context);
3790 return isl_set_gist_basic_set(set, dom_context);
3793 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3794 __isl_take isl_set *context)
3796 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3799 /* Compute the gist of "bmap" with respect to the constraints "context"
3800 * on the domain.
3802 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3803 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3805 isl_space *space = isl_basic_map_get_space(bmap);
3806 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3808 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3809 return isl_basic_map_gist(bmap, bmap_context);
3812 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3813 __isl_take isl_set *context)
3815 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3816 map_context = isl_map_intersect_domain(map_context, context);
3817 return isl_map_gist(map, map_context);
3820 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3821 __isl_take isl_set *context)
3823 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3824 map_context = isl_map_intersect_range(map_context, context);
3825 return isl_map_gist(map, map_context);
3828 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3829 __isl_take isl_set *context)
3831 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3832 map_context = isl_map_intersect_params(map_context, context);
3833 return isl_map_gist(map, map_context);
3836 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3837 __isl_take isl_set *context)
3839 return isl_map_gist_params(set, context);
3842 /* Quick check to see if two basic maps are disjoint.
3843 * In particular, we reduce the equalities and inequalities of
3844 * one basic map in the context of the equalities of the other
3845 * basic map and check if we get a contradiction.
3847 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3848 __isl_keep isl_basic_map *bmap2)
3850 struct isl_vec *v = NULL;
3851 int *elim = NULL;
3852 unsigned total;
3853 int i;
3855 if (!bmap1 || !bmap2)
3856 return isl_bool_error;
3857 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3858 return isl_bool_error);
3859 if (bmap1->n_div || bmap2->n_div)
3860 return isl_bool_false;
3861 if (!bmap1->n_eq && !bmap2->n_eq)
3862 return isl_bool_false;
3864 total = isl_space_dim(bmap1->dim, isl_dim_all);
3865 if (total == 0)
3866 return isl_bool_false;
3867 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3868 if (!v)
3869 goto error;
3870 elim = isl_alloc_array(bmap1->ctx, int, total);
3871 if (!elim)
3872 goto error;
3873 compute_elimination_index(bmap1, elim);
3874 for (i = 0; i < bmap2->n_eq; ++i) {
3875 int reduced;
3876 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3877 bmap1, elim);
3878 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3879 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3880 goto disjoint;
3882 for (i = 0; i < bmap2->n_ineq; ++i) {
3883 int reduced;
3884 reduced = reduced_using_equalities(v->block.data,
3885 bmap2->ineq[i], bmap1, elim);
3886 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3887 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3888 goto disjoint;
3890 compute_elimination_index(bmap2, elim);
3891 for (i = 0; i < bmap1->n_ineq; ++i) {
3892 int reduced;
3893 reduced = reduced_using_equalities(v->block.data,
3894 bmap1->ineq[i], bmap2, elim);
3895 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3896 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3897 goto disjoint;
3899 isl_vec_free(v);
3900 free(elim);
3901 return isl_bool_false;
3902 disjoint:
3903 isl_vec_free(v);
3904 free(elim);
3905 return isl_bool_true;
3906 error:
3907 isl_vec_free(v);
3908 free(elim);
3909 return isl_bool_error;
3912 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3913 __isl_keep isl_basic_set *bset2)
3915 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3916 bset_to_bmap(bset2));
3919 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3921 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3922 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3923 __isl_keep isl_basic_map *bmap2))
3925 int i, j;
3927 if (!map1 || !map2)
3928 return isl_bool_error;
3930 for (i = 0; i < map1->n; ++i) {
3931 for (j = 0; j < map2->n; ++j) {
3932 isl_bool d = test(map1->p[i], map2->p[j]);
3933 if (d != isl_bool_true)
3934 return d;
3938 return isl_bool_true;
3941 /* Are "map1" and "map2" obviously disjoint, based on information
3942 * that can be derived without looking at the individual basic maps?
3944 * In particular, if one of them is empty or if they live in different spaces
3945 * (ignoring parameters), then they are clearly disjoint.
3947 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3948 __isl_keep isl_map *map2)
3950 isl_bool disjoint;
3951 isl_bool match;
3953 if (!map1 || !map2)
3954 return isl_bool_error;
3956 disjoint = isl_map_plain_is_empty(map1);
3957 if (disjoint < 0 || disjoint)
3958 return disjoint;
3960 disjoint = isl_map_plain_is_empty(map2);
3961 if (disjoint < 0 || disjoint)
3962 return disjoint;
3964 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3965 map2->dim, isl_dim_in);
3966 if (match < 0 || !match)
3967 return match < 0 ? isl_bool_error : isl_bool_true;
3969 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3970 map2->dim, isl_dim_out);
3971 if (match < 0 || !match)
3972 return match < 0 ? isl_bool_error : isl_bool_true;
3974 return isl_bool_false;
3977 /* Are "map1" and "map2" obviously disjoint?
3979 * If one of them is empty or if they live in different spaces (ignoring
3980 * parameters), then they are clearly disjoint.
3981 * This is checked by isl_map_plain_is_disjoint_global.
3983 * If they have different parameters, then we skip any further tests.
3985 * If they are obviously equal, but not obviously empty, then we will
3986 * not be able to detect if they are disjoint.
3988 * Otherwise we check if each basic map in "map1" is obviously disjoint
3989 * from each basic map in "map2".
3991 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3992 __isl_keep isl_map *map2)
3994 isl_bool disjoint;
3995 isl_bool intersect;
3996 isl_bool match;
3998 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3999 if (disjoint < 0 || disjoint)
4000 return disjoint;
4002 match = isl_space_match(map1->dim, isl_dim_param,
4003 map2->dim, isl_dim_param);
4004 if (match < 0 || !match)
4005 return match < 0 ? isl_bool_error : isl_bool_false;
4007 intersect = isl_map_plain_is_equal(map1, map2);
4008 if (intersect < 0 || intersect)
4009 return intersect < 0 ? isl_bool_error : isl_bool_false;
4011 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4014 /* Are "map1" and "map2" disjoint?
4016 * They are disjoint if they are "obviously disjoint" or if one of them
4017 * is empty. Otherwise, they are not disjoint if one of them is universal.
4018 * If the two inputs are (obviously) equal and not empty, then they are
4019 * not disjoint.
4020 * If none of these cases apply, then check if all pairs of basic maps
4021 * are disjoint.
4023 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4025 isl_bool disjoint;
4026 isl_bool intersect;
4028 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4029 if (disjoint < 0 || disjoint)
4030 return disjoint;
4032 disjoint = isl_map_is_empty(map1);
4033 if (disjoint < 0 || disjoint)
4034 return disjoint;
4036 disjoint = isl_map_is_empty(map2);
4037 if (disjoint < 0 || disjoint)
4038 return disjoint;
4040 intersect = isl_map_plain_is_universe(map1);
4041 if (intersect < 0 || intersect)
4042 return intersect < 0 ? isl_bool_error : isl_bool_false;
4044 intersect = isl_map_plain_is_universe(map2);
4045 if (intersect < 0 || intersect)
4046 return intersect < 0 ? isl_bool_error : isl_bool_false;
4048 intersect = isl_map_plain_is_equal(map1, map2);
4049 if (intersect < 0 || intersect)
4050 return isl_bool_not(intersect);
4052 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4055 /* Are "bmap1" and "bmap2" disjoint?
4057 * They are disjoint if they are "obviously disjoint" or if one of them
4058 * is empty. Otherwise, they are not disjoint if one of them is universal.
4059 * If none of these cases apply, we compute the intersection and see if
4060 * the result is empty.
4062 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4063 __isl_keep isl_basic_map *bmap2)
4065 isl_bool disjoint;
4066 isl_bool intersect;
4067 isl_basic_map *test;
4069 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4070 if (disjoint < 0 || disjoint)
4071 return disjoint;
4073 disjoint = isl_basic_map_is_empty(bmap1);
4074 if (disjoint < 0 || disjoint)
4075 return disjoint;
4077 disjoint = isl_basic_map_is_empty(bmap2);
4078 if (disjoint < 0 || disjoint)
4079 return disjoint;
4081 intersect = isl_basic_map_plain_is_universe(bmap1);
4082 if (intersect < 0 || intersect)
4083 return intersect < 0 ? isl_bool_error : isl_bool_false;
4085 intersect = isl_basic_map_plain_is_universe(bmap2);
4086 if (intersect < 0 || intersect)
4087 return intersect < 0 ? isl_bool_error : isl_bool_false;
4089 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4090 isl_basic_map_copy(bmap2));
4091 disjoint = isl_basic_map_is_empty(test);
4092 isl_basic_map_free(test);
4094 return disjoint;
4097 /* Are "bset1" and "bset2" disjoint?
4099 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4100 __isl_keep isl_basic_set *bset2)
4102 return isl_basic_map_is_disjoint(bset1, bset2);
4105 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4106 __isl_keep isl_set *set2)
4108 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4111 /* Are "set1" and "set2" disjoint?
4113 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4115 return isl_map_is_disjoint(set1, set2);
4118 /* Is "v" equal to 0, 1 or -1?
4120 static int is_zero_or_one(isl_int v)
4122 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4125 /* Check if we can combine a given div with lower bound l and upper
4126 * bound u with some other div and if so return that other div.
4127 * Otherwise return -1.
4129 * We first check that
4130 * - the bounds are opposites of each other (except for the constant
4131 * term)
4132 * - the bounds do not reference any other div
4133 * - no div is defined in terms of this div
4135 * Let m be the size of the range allowed on the div by the bounds.
4136 * That is, the bounds are of the form
4138 * e <= a <= e + m - 1
4140 * with e some expression in the other variables.
4141 * We look for another div b such that no third div is defined in terms
4142 * of this second div b and such that in any constraint that contains
4143 * a (except for the given lower and upper bound), also contains b
4144 * with a coefficient that is m times that of b.
4145 * That is, all constraints (execpt for the lower and upper bound)
4146 * are of the form
4148 * e + f (a + m b) >= 0
4150 * Furthermore, in the constraints that only contain b, the coefficient
4151 * of b should be equal to 1 or -1.
4152 * If so, we return b so that "a + m b" can be replaced by
4153 * a single div "c = a + m b".
4155 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4156 unsigned div, unsigned l, unsigned u)
4158 int i, j;
4159 unsigned dim;
4160 int coalesce = -1;
4162 if (bmap->n_div <= 1)
4163 return -1;
4164 dim = isl_space_dim(bmap->dim, isl_dim_all);
4165 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4166 return -1;
4167 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4168 bmap->n_div - div - 1) != -1)
4169 return -1;
4170 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4171 dim + bmap->n_div))
4172 return -1;
4174 for (i = 0; i < bmap->n_div; ++i) {
4175 if (isl_int_is_zero(bmap->div[i][0]))
4176 continue;
4177 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4178 return -1;
4181 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4182 if (isl_int_is_neg(bmap->ineq[l][0])) {
4183 isl_int_sub(bmap->ineq[l][0],
4184 bmap->ineq[l][0], bmap->ineq[u][0]);
4185 bmap = isl_basic_map_copy(bmap);
4186 bmap = isl_basic_map_set_to_empty(bmap);
4187 isl_basic_map_free(bmap);
4188 return -1;
4190 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4191 for (i = 0; i < bmap->n_div; ++i) {
4192 if (i == div)
4193 continue;
4194 if (!pairs[i])
4195 continue;
4196 for (j = 0; j < bmap->n_div; ++j) {
4197 if (isl_int_is_zero(bmap->div[j][0]))
4198 continue;
4199 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4200 break;
4202 if (j < bmap->n_div)
4203 continue;
4204 for (j = 0; j < bmap->n_ineq; ++j) {
4205 int valid;
4206 if (j == l || j == u)
4207 continue;
4208 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4209 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4210 continue;
4211 break;
4213 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4214 break;
4215 isl_int_mul(bmap->ineq[j][1 + dim + div],
4216 bmap->ineq[j][1 + dim + div],
4217 bmap->ineq[l][0]);
4218 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4219 bmap->ineq[j][1 + dim + i]);
4220 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4221 bmap->ineq[j][1 + dim + div],
4222 bmap->ineq[l][0]);
4223 if (!valid)
4224 break;
4226 if (j < bmap->n_ineq)
4227 continue;
4228 coalesce = i;
4229 break;
4231 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4232 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4233 return coalesce;
4236 /* Internal data structure used during the construction and/or evaluation of
4237 * an inequality that ensures that a pair of bounds always allows
4238 * for an integer value.
4240 * "tab" is the tableau in which the inequality is evaluated. It may
4241 * be NULL until it is actually needed.
4242 * "v" contains the inequality coefficients.
4243 * "g", "fl" and "fu" are temporary scalars used during the construction and
4244 * evaluation.
4246 struct test_ineq_data {
4247 struct isl_tab *tab;
4248 isl_vec *v;
4249 isl_int g;
4250 isl_int fl;
4251 isl_int fu;
4254 /* Free all the memory allocated by the fields of "data".
4256 static void test_ineq_data_clear(struct test_ineq_data *data)
4258 isl_tab_free(data->tab);
4259 isl_vec_free(data->v);
4260 isl_int_clear(data->g);
4261 isl_int_clear(data->fl);
4262 isl_int_clear(data->fu);
4265 /* Is the inequality stored in data->v satisfied by "bmap"?
4266 * That is, does it only attain non-negative values?
4267 * data->tab is a tableau corresponding to "bmap".
4269 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4270 struct test_ineq_data *data)
4272 isl_ctx *ctx;
4273 enum isl_lp_result res;
4275 ctx = isl_basic_map_get_ctx(bmap);
4276 if (!data->tab)
4277 data->tab = isl_tab_from_basic_map(bmap, 0);
4278 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4279 if (res == isl_lp_error)
4280 return isl_bool_error;
4281 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4284 /* Given a lower and an upper bound on div i, do they always allow
4285 * for an integer value of the given div?
4286 * Determine this property by constructing an inequality
4287 * such that the property is guaranteed when the inequality is nonnegative.
4288 * The lower bound is inequality l, while the upper bound is inequality u.
4289 * The constructed inequality is stored in data->v.
4291 * Let the upper bound be
4293 * -n_u a + e_u >= 0
4295 * and the lower bound
4297 * n_l a + e_l >= 0
4299 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4300 * We have
4302 * - f_u e_l <= f_u f_l g a <= f_l e_u
4304 * Since all variables are integer valued, this is equivalent to
4306 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4308 * If this interval is at least f_u f_l g, then it contains at least
4309 * one integer value for a.
4310 * That is, the test constraint is
4312 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4314 * or
4316 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4318 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4319 * then the constraint can be scaled down by a factor g',
4320 * with the constant term replaced by
4321 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4322 * Note that the result of applying Fourier-Motzkin to this pair
4323 * of constraints is
4325 * f_l e_u + f_u e_l >= 0
4327 * If the constant term of the scaled down version of this constraint,
4328 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4329 * term of the scaled down test constraint, then the test constraint
4330 * is known to hold and no explicit evaluation is required.
4331 * This is essentially the Omega test.
4333 * If the test constraint consists of only a constant term, then
4334 * it is sufficient to look at the sign of this constant term.
4336 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4337 int l, int u, struct test_ineq_data *data)
4339 unsigned offset, n_div;
4340 offset = isl_basic_map_offset(bmap, isl_dim_div);
4341 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4343 isl_int_gcd(data->g,
4344 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4345 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4346 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4347 isl_int_neg(data->fu, data->fu);
4348 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4349 data->fu, bmap->ineq[l], offset + n_div);
4350 isl_int_mul(data->g, data->g, data->fl);
4351 isl_int_mul(data->g, data->g, data->fu);
4352 isl_int_sub(data->g, data->g, data->fl);
4353 isl_int_sub(data->g, data->g, data->fu);
4354 isl_int_add_ui(data->g, data->g, 1);
4355 isl_int_sub(data->fl, data->v->el[0], data->g);
4357 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4358 if (isl_int_is_zero(data->g))
4359 return isl_int_is_nonneg(data->fl);
4360 if (isl_int_is_one(data->g)) {
4361 isl_int_set(data->v->el[0], data->fl);
4362 return test_ineq_is_satisfied(bmap, data);
4364 isl_int_fdiv_q(data->fl, data->fl, data->g);
4365 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4366 if (isl_int_eq(data->fl, data->v->el[0]))
4367 return isl_bool_true;
4368 isl_int_set(data->v->el[0], data->fl);
4369 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4370 offset - 1 + n_div);
4372 return test_ineq_is_satisfied(bmap, data);
4375 /* Remove more kinds of divs that are not strictly needed.
4376 * In particular, if all pairs of lower and upper bounds on a div
4377 * are such that they allow at least one integer value of the div,
4378 * then we can eliminate the div using Fourier-Motzkin without
4379 * introducing any spurious solutions.
4381 * If at least one of the two constraints has a unit coefficient for the div,
4382 * then the presence of such a value is guaranteed so there is no need to check.
4383 * In particular, the value attained by the bound with unit coefficient
4384 * can serve as this intermediate value.
4386 static struct isl_basic_map *drop_more_redundant_divs(
4387 struct isl_basic_map *bmap, int *pairs, int n)
4389 isl_ctx *ctx;
4390 struct test_ineq_data data = { NULL, NULL };
4391 unsigned off, n_div;
4392 int remove = -1;
4394 isl_int_init(data.g);
4395 isl_int_init(data.fl);
4396 isl_int_init(data.fu);
4398 if (!bmap)
4399 goto error;
4401 ctx = isl_basic_map_get_ctx(bmap);
4402 off = isl_basic_map_offset(bmap, isl_dim_div);
4403 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4404 data.v = isl_vec_alloc(ctx, off + n_div);
4405 if (!data.v)
4406 goto error;
4408 while (n > 0) {
4409 int i, l, u;
4410 int best = -1;
4411 isl_bool has_int;
4413 for (i = 0; i < n_div; ++i) {
4414 if (!pairs[i])
4415 continue;
4416 if (best >= 0 && pairs[best] <= pairs[i])
4417 continue;
4418 best = i;
4421 i = best;
4422 for (l = 0; l < bmap->n_ineq; ++l) {
4423 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4424 continue;
4425 if (isl_int_is_one(bmap->ineq[l][off + i]))
4426 continue;
4427 for (u = 0; u < bmap->n_ineq; ++u) {
4428 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4429 continue;
4430 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4431 continue;
4432 has_int = int_between_bounds(bmap, i, l, u,
4433 &data);
4434 if (has_int < 0)
4435 goto error;
4436 if (data.tab && data.tab->empty)
4437 break;
4438 if (!has_int)
4439 break;
4441 if (u < bmap->n_ineq)
4442 break;
4444 if (data.tab && data.tab->empty) {
4445 bmap = isl_basic_map_set_to_empty(bmap);
4446 break;
4448 if (l == bmap->n_ineq) {
4449 remove = i;
4450 break;
4452 pairs[i] = 0;
4453 --n;
4456 test_ineq_data_clear(&data);
4458 free(pairs);
4460 if (remove < 0)
4461 return bmap;
4463 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4464 return isl_basic_map_drop_redundant_divs(bmap);
4465 error:
4466 free(pairs);
4467 isl_basic_map_free(bmap);
4468 test_ineq_data_clear(&data);
4469 return NULL;
4472 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4473 * and the upper bound u, div1 always occurs together with div2 in the form
4474 * (div1 + m div2), where m is the constant range on the variable div1
4475 * allowed by l and u, replace the pair div1 and div2 by a single
4476 * div that is equal to div1 + m div2.
4478 * The new div will appear in the location that contains div2.
4479 * We need to modify all constraints that contain
4480 * div2 = (div - div1) / m
4481 * The coefficient of div2 is known to be equal to 1 or -1.
4482 * (If a constraint does not contain div2, it will also not contain div1.)
4483 * If the constraint also contains div1, then we know they appear
4484 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4485 * i.e., the coefficient of div is f.
4487 * Otherwise, we first need to introduce div1 into the constraint.
4488 * Let the l be
4490 * div1 + f >=0
4492 * and u
4494 * -div1 + f' >= 0
4496 * A lower bound on div2
4498 * div2 + t >= 0
4500 * can be replaced by
4502 * m div2 + div1 + m t + f >= 0
4504 * An upper bound
4506 * -div2 + t >= 0
4508 * can be replaced by
4510 * -(m div2 + div1) + m t + f' >= 0
4512 * These constraint are those that we would obtain from eliminating
4513 * div1 using Fourier-Motzkin.
4515 * After all constraints have been modified, we drop the lower and upper
4516 * bound and then drop div1.
4518 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4519 unsigned div1, unsigned div2, unsigned l, unsigned u)
4521 isl_ctx *ctx;
4522 isl_int m;
4523 unsigned dim, total;
4524 int i;
4526 ctx = isl_basic_map_get_ctx(bmap);
4528 dim = isl_space_dim(bmap->dim, isl_dim_all);
4529 total = 1 + dim + bmap->n_div;
4531 isl_int_init(m);
4532 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4533 isl_int_add_ui(m, m, 1);
4535 for (i = 0; i < bmap->n_ineq; ++i) {
4536 if (i == l || i == u)
4537 continue;
4538 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4539 continue;
4540 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4541 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4542 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4543 ctx->one, bmap->ineq[l], total);
4544 else
4545 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4546 ctx->one, bmap->ineq[u], total);
4548 isl_int_set(bmap->ineq[i][1 + dim + div2],
4549 bmap->ineq[i][1 + dim + div1]);
4550 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4553 isl_int_clear(m);
4554 if (l > u) {
4555 isl_basic_map_drop_inequality(bmap, l);
4556 isl_basic_map_drop_inequality(bmap, u);
4557 } else {
4558 isl_basic_map_drop_inequality(bmap, u);
4559 isl_basic_map_drop_inequality(bmap, l);
4561 bmap = isl_basic_map_drop_div(bmap, div1);
4562 return bmap;
4565 /* First check if we can coalesce any pair of divs and
4566 * then continue with dropping more redundant divs.
4568 * We loop over all pairs of lower and upper bounds on a div
4569 * with coefficient 1 and -1, respectively, check if there
4570 * is any other div "c" with which we can coalesce the div
4571 * and if so, perform the coalescing.
4573 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4574 struct isl_basic_map *bmap, int *pairs, int n)
4576 int i, l, u;
4577 unsigned dim;
4579 dim = isl_space_dim(bmap->dim, isl_dim_all);
4581 for (i = 0; i < bmap->n_div; ++i) {
4582 if (!pairs[i])
4583 continue;
4584 for (l = 0; l < bmap->n_ineq; ++l) {
4585 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4586 continue;
4587 for (u = 0; u < bmap->n_ineq; ++u) {
4588 int c;
4590 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4591 continue;
4592 c = div_find_coalesce(bmap, pairs, i, l, u);
4593 if (c < 0)
4594 continue;
4595 free(pairs);
4596 bmap = coalesce_divs(bmap, i, c, l, u);
4597 return isl_basic_map_drop_redundant_divs(bmap);
4602 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4603 free(pairs);
4604 return bmap;
4607 return drop_more_redundant_divs(bmap, pairs, n);
4610 /* Are the "n" coefficients starting at "first" of inequality constraints
4611 * "i" and "j" of "bmap" equal to each other?
4613 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4614 int first, int n)
4616 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4619 /* Are the "n" coefficients starting at "first" of inequality constraints
4620 * "i" and "j" of "bmap" opposite to each other?
4622 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4623 int first, int n)
4625 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4628 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4629 * apart from the constant term?
4631 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4633 unsigned total;
4635 total = isl_basic_map_dim(bmap, isl_dim_all);
4636 return is_opposite_part(bmap, i, j, 1, total);
4639 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4640 * apart from the constant term and the coefficient at position "pos"?
4642 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4643 int pos)
4645 unsigned total;
4647 total = isl_basic_map_dim(bmap, isl_dim_all);
4648 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4649 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4652 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4653 * apart from the constant term and the coefficient at position "pos"?
4655 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4656 int pos)
4658 unsigned total;
4660 total = isl_basic_map_dim(bmap, isl_dim_all);
4661 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4662 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4665 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4666 * been modified, simplying it if "simplify" is set.
4667 * Free the temporary data structure "pairs" that was associated
4668 * to the old version of "bmap".
4670 static __isl_give isl_basic_map *drop_redundant_divs_again(
4671 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4673 if (simplify)
4674 bmap = isl_basic_map_simplify(bmap);
4675 free(pairs);
4676 return isl_basic_map_drop_redundant_divs(bmap);
4679 /* Is "div" the single unknown existentially quantified variable
4680 * in inequality constraint "ineq" of "bmap"?
4681 * "div" is known to have a non-zero coefficient in "ineq".
4683 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4685 int i;
4686 unsigned n_div, o_div;
4688 if (isl_basic_map_div_is_known(bmap, div))
4689 return 0;
4690 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4691 if (n_div == 1)
4692 return 1;
4693 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4694 for (i = 0; i < n_div; ++i) {
4695 if (i == div)
4696 continue;
4697 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4698 continue;
4699 if (!isl_basic_map_div_is_known(bmap, i))
4700 return 0;
4703 return 1;
4706 /* Does integer division "div" have coefficient 1 in inequality constraint
4707 * "ineq" of "map"?
4709 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4711 unsigned o_div;
4713 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4714 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4715 return 1;
4717 return 0;
4720 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4721 * then try and drop redundant divs again,
4722 * freeing the temporary data structure "pairs" that was associated
4723 * to the old version of "bmap".
4725 static __isl_give isl_basic_map *set_eq_and_try_again(
4726 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4728 bmap = isl_basic_map_cow(bmap);
4729 isl_basic_map_inequality_to_equality(bmap, ineq);
4730 return drop_redundant_divs_again(bmap, pairs, 1);
4733 /* Drop the integer division at position "div", along with the two
4734 * inequality constraints "ineq1" and "ineq2" in which it appears
4735 * from "bmap" and then try and drop redundant divs again,
4736 * freeing the temporary data structure "pairs" that was associated
4737 * to the old version of "bmap".
4739 static __isl_give isl_basic_map *drop_div_and_try_again(
4740 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4741 __isl_take int *pairs)
4743 if (ineq1 > ineq2) {
4744 isl_basic_map_drop_inequality(bmap, ineq1);
4745 isl_basic_map_drop_inequality(bmap, ineq2);
4746 } else {
4747 isl_basic_map_drop_inequality(bmap, ineq2);
4748 isl_basic_map_drop_inequality(bmap, ineq1);
4750 bmap = isl_basic_map_drop_div(bmap, div);
4751 return drop_redundant_divs_again(bmap, pairs, 0);
4754 /* Given two inequality constraints
4756 * f(x) + n d + c >= 0, (ineq)
4758 * with d the variable at position "pos", and
4760 * f(x) + c0 >= 0, (lower)
4762 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4763 * determined by the first constraint.
4764 * That is, store
4766 * ceil((c0 - c)/n)
4768 * in *l.
4770 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4771 int ineq, int lower, int pos, isl_int *l)
4773 isl_int_neg(*l, bmap->ineq[ineq][0]);
4774 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4775 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
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, (upper)
4786 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4787 * determined by the first constraint.
4788 * That is, store
4790 * ceil((-c1 - c)/n)
4792 * in *u.
4794 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4795 int ineq, int upper, int pos, isl_int *u)
4797 isl_int_neg(*u, bmap->ineq[ineq][0]);
4798 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4799 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4802 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4803 * does the corresponding lower bound have a fixed value in "bmap"?
4805 * In particular, "ineq" is of the form
4807 * f(x) + n d + c >= 0
4809 * with n > 0, c the constant term and
4810 * d the existentially quantified variable "div".
4811 * That is, the lower bound is
4813 * ceil((-f(x) - c)/n)
4815 * Look for a pair of constraints
4817 * f(x) + c0 >= 0
4818 * -f(x) + c1 >= 0
4820 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4821 * That is, check that
4823 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4825 * If so, return the index of inequality f(x) + c0 >= 0.
4826 * Otherwise, return -1.
4828 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4830 int i;
4831 int lower = -1, upper = -1;
4832 unsigned o_div, n_div;
4833 isl_int l, u;
4834 int equal;
4836 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4837 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4838 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4839 if (i == ineq)
4840 continue;
4841 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4842 continue;
4843 if (lower < 0 &&
4844 is_parallel_except(bmap, ineq, i, o_div + div)) {
4845 lower = i;
4846 continue;
4848 if (upper < 0 &&
4849 is_opposite_except(bmap, ineq, i, o_div + div)) {
4850 upper = i;
4854 if (lower < 0 || upper < 0)
4855 return -1;
4857 isl_int_init(l);
4858 isl_int_init(u);
4860 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4861 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4863 equal = isl_int_eq(l, u);
4865 isl_int_clear(l);
4866 isl_int_clear(u);
4868 return equal ? lower : -1;
4871 /* Given a lower bound constraint "ineq" on the existentially quantified
4872 * variable "div", such that the corresponding lower bound has
4873 * a fixed value in "bmap", assign this fixed value to the variable and
4874 * then try and drop redundant divs again,
4875 * freeing the temporary data structure "pairs" that was associated
4876 * to the old version of "bmap".
4877 * "lower" determines the constant value for the lower bound.
4879 * In particular, "ineq" is of the form
4881 * f(x) + n d + c >= 0,
4883 * while "lower" is of the form
4885 * f(x) + c0 >= 0
4887 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4888 * is ceil((c0 - c)/n).
4890 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4891 int div, int ineq, int lower, int *pairs)
4893 isl_int c;
4894 unsigned o_div;
4896 isl_int_init(c);
4898 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4899 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4900 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4901 free(pairs);
4903 isl_int_clear(c);
4905 return isl_basic_map_drop_redundant_divs(bmap);
4908 /* Remove divs that are not strictly needed based on the inequality
4909 * constraints.
4910 * In particular, if a div only occurs positively (or negatively)
4911 * in constraints, then it can simply be dropped.
4912 * Also, if a div occurs in only two constraints and if moreover
4913 * those two constraints are opposite to each other, except for the constant
4914 * term and if the sum of the constant terms is such that for any value
4915 * of the other values, there is always at least one integer value of the
4916 * div, i.e., if one plus this sum is greater than or equal to
4917 * the (absolute value) of the coefficient of the div in the constraints,
4918 * then we can also simply drop the div.
4920 * If an existentially quantified variable does not have an explicit
4921 * representation, appears in only a single lower bound that does not
4922 * involve any other such existentially quantified variables and appears
4923 * in this lower bound with coefficient 1,
4924 * then fix the variable to the value of the lower bound. That is,
4925 * turn the inequality into an equality.
4926 * If for any value of the other variables, there is any value
4927 * for the existentially quantified variable satisfying the constraints,
4928 * then this lower bound also satisfies the constraints.
4929 * It is therefore safe to pick this lower bound.
4931 * The same reasoning holds even if the coefficient is not one.
4932 * However, fixing the variable to the value of the lower bound may
4933 * in general introduce an extra integer division, in which case
4934 * it may be better to pick another value.
4935 * If this integer division has a known constant value, then plugging
4936 * in this constant value removes the existentially quantified variable
4937 * completely. In particular, if the lower bound is of the form
4938 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4939 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4940 * then the existentially quantified variable can be assigned this
4941 * shared value.
4943 * We skip divs that appear in equalities or in the definition of other divs.
4944 * Divs that appear in the definition of other divs usually occur in at least
4945 * 4 constraints, but the constraints may have been simplified.
4947 * If any divs are left after these simple checks then we move on
4948 * to more complicated cases in drop_more_redundant_divs.
4950 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4951 __isl_take isl_basic_map *bmap)
4953 int i, j;
4954 unsigned off;
4955 int *pairs = NULL;
4956 int n = 0;
4958 if (!bmap)
4959 goto error;
4960 if (bmap->n_div == 0)
4961 return bmap;
4963 off = isl_space_dim(bmap->dim, isl_dim_all);
4964 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4965 if (!pairs)
4966 goto error;
4968 for (i = 0; i < bmap->n_div; ++i) {
4969 int pos, neg;
4970 int last_pos, last_neg;
4971 int redundant;
4972 int defined;
4974 defined = !isl_int_is_zero(bmap->div[i][0]);
4975 for (j = i; j < bmap->n_div; ++j)
4976 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4977 break;
4978 if (j < bmap->n_div)
4979 continue;
4980 for (j = 0; j < bmap->n_eq; ++j)
4981 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4982 break;
4983 if (j < bmap->n_eq)
4984 continue;
4985 ++n;
4986 pos = neg = 0;
4987 for (j = 0; j < bmap->n_ineq; ++j) {
4988 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4989 last_pos = j;
4990 ++pos;
4992 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4993 last_neg = j;
4994 ++neg;
4997 pairs[i] = pos * neg;
4998 if (pairs[i] == 0) {
4999 for (j = bmap->n_ineq - 1; j >= 0; --j)
5000 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5001 isl_basic_map_drop_inequality(bmap, j);
5002 bmap = isl_basic_map_drop_div(bmap, i);
5003 return drop_redundant_divs_again(bmap, pairs, 0);
5005 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
5006 int single, lower;
5007 if (pos != 1)
5008 continue;
5009 single = single_unknown(bmap, last_pos, i);
5010 if (!single)
5011 continue;
5012 if (has_coef_one(bmap, i, last_pos))
5013 return set_eq_and_try_again(bmap, last_pos,
5014 pairs);
5015 lower = lower_bound_is_cst(bmap, i, last_pos);
5016 if (lower >= 0)
5017 return fix_cst_lower(bmap, i, last_pos, lower,
5018 pairs);
5019 continue;
5022 isl_int_add(bmap->ineq[last_pos][0],
5023 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5024 isl_int_add_ui(bmap->ineq[last_pos][0],
5025 bmap->ineq[last_pos][0], 1);
5026 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5027 bmap->ineq[last_pos][1+off+i]);
5028 isl_int_sub_ui(bmap->ineq[last_pos][0],
5029 bmap->ineq[last_pos][0], 1);
5030 isl_int_sub(bmap->ineq[last_pos][0],
5031 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5032 if (redundant)
5033 return drop_div_and_try_again(bmap, i,
5034 last_pos, last_neg, pairs);
5035 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
5036 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5037 return drop_redundant_divs_again(bmap, pairs, 1);
5039 pairs[i] = 0;
5040 --n;
5043 if (n > 0)
5044 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5046 free(pairs);
5047 return bmap;
5048 error:
5049 free(pairs);
5050 isl_basic_map_free(bmap);
5051 return NULL;
5054 /* Consider the coefficients at "c" as a row vector and replace
5055 * them with their product with "T". "T" is assumed to be a square matrix.
5057 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5059 int n;
5060 isl_ctx *ctx;
5061 isl_vec *v;
5063 if (!T)
5064 return isl_stat_error;
5065 n = isl_mat_rows(T);
5066 if (isl_seq_first_non_zero(c, n) == -1)
5067 return isl_stat_ok;
5068 ctx = isl_mat_get_ctx(T);
5069 v = isl_vec_alloc(ctx, n);
5070 if (!v)
5071 return isl_stat_error;
5072 isl_seq_swp_or_cpy(v->el, c, n);
5073 v = isl_vec_mat_product(v, isl_mat_copy(T));
5074 if (!v)
5075 return isl_stat_error;
5076 isl_seq_swp_or_cpy(c, v->el, n);
5077 isl_vec_free(v);
5079 return isl_stat_ok;
5082 /* Plug in T for the variables in "bmap" starting at "pos".
5083 * T is a linear unimodular matrix, i.e., without constant term.
5085 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5086 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5088 int i;
5089 unsigned n, total;
5091 bmap = isl_basic_map_cow(bmap);
5092 if (!bmap || !T)
5093 goto error;
5095 n = isl_mat_cols(T);
5096 if (n != isl_mat_rows(T))
5097 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5098 "expecting square matrix", goto error);
5100 total = isl_basic_map_dim(bmap, isl_dim_all);
5101 if (pos + n > total || pos + n < pos)
5102 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5103 "invalid range", goto error);
5105 for (i = 0; i < bmap->n_eq; ++i)
5106 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5107 goto error;
5108 for (i = 0; i < bmap->n_ineq; ++i)
5109 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5110 goto error;
5111 for (i = 0; i < bmap->n_div; ++i) {
5112 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5113 continue;
5114 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5115 goto error;
5118 isl_mat_free(T);
5119 return bmap;
5120 error:
5121 isl_basic_map_free(bmap);
5122 isl_mat_free(T);
5123 return NULL;
5126 /* Remove divs that are not strictly needed.
5128 * First look for an equality constraint involving two or more
5129 * existentially quantified variables without an explicit
5130 * representation. Replace the combination that appears
5131 * in the equality constraint by a single existentially quantified
5132 * variable such that the equality can be used to derive
5133 * an explicit representation for the variable.
5134 * If there are no more such equality constraints, then continue
5135 * with isl_basic_map_drop_redundant_divs_ineq.
5137 * In particular, if the equality constraint is of the form
5139 * f(x) + \sum_i c_i a_i = 0
5141 * with a_i existentially quantified variable without explicit
5142 * representation, then apply a transformation on the existentially
5143 * quantified variables to turn the constraint into
5145 * f(x) + g a_1' = 0
5147 * with g the gcd of the c_i.
5148 * In order to easily identify which existentially quantified variables
5149 * have a complete explicit representation, i.e., without being defined
5150 * in terms of other existentially quantified variables without
5151 * an explicit representation, the existentially quantified variables
5152 * are first sorted.
5154 * The variable transformation is computed by extending the row
5155 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5157 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5158 * [a_2'] [ a_2 ]
5159 * ... = U ....
5160 * [a_n'] [ a_n ]
5162 * with [c_1/g ... c_n/g] representing the first row of U.
5163 * The inverse of U is then plugged into the original constraints.
5164 * The call to isl_basic_map_simplify makes sure the explicit
5165 * representation for a_1' is extracted from the equality constraint.
5167 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5168 __isl_take isl_basic_map *bmap)
5170 int first;
5171 int i;
5172 unsigned o_div, n_div;
5173 int l;
5174 isl_ctx *ctx;
5175 isl_mat *T;
5177 if (!bmap)
5178 return NULL;
5179 if (isl_basic_map_divs_known(bmap))
5180 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5181 if (bmap->n_eq == 0)
5182 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5183 bmap = isl_basic_map_sort_divs(bmap);
5184 if (!bmap)
5185 return NULL;
5187 first = isl_basic_map_first_unknown_div(bmap);
5188 if (first < 0)
5189 return isl_basic_map_free(bmap);
5191 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5192 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5194 for (i = 0; i < bmap->n_eq; ++i) {
5195 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5196 n_div - (first));
5197 if (l < 0)
5198 continue;
5199 l += first;
5200 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5201 n_div - (l + 1)) == -1)
5202 continue;
5203 break;
5205 if (i >= bmap->n_eq)
5206 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5208 ctx = isl_basic_map_get_ctx(bmap);
5209 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5210 if (!T)
5211 return isl_basic_map_free(bmap);
5212 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5213 T = isl_mat_normalize_row(T, 0);
5214 T = isl_mat_unimodular_complete(T, 1);
5215 T = isl_mat_right_inverse(T);
5217 for (i = l; i < n_div; ++i)
5218 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5219 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5220 bmap = isl_basic_map_simplify(bmap);
5222 return isl_basic_map_drop_redundant_divs(bmap);
5225 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5226 struct isl_basic_set *bset)
5228 isl_basic_map *bmap = bset_to_bmap(bset);
5229 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5232 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5234 int i;
5236 if (!map)
5237 return NULL;
5238 for (i = 0; i < map->n; ++i) {
5239 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5240 if (!map->p[i])
5241 goto error;
5243 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5244 return map;
5245 error:
5246 isl_map_free(map);
5247 return NULL;
5250 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5252 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5255 /* Does "bmap" satisfy any equality that involves more than 2 variables
5256 * and/or has coefficients different from -1 and 1?
5258 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5260 int i;
5261 unsigned total;
5263 total = isl_basic_map_dim(bmap, isl_dim_all);
5265 for (i = 0; i < bmap->n_eq; ++i) {
5266 int j, k;
5268 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5269 if (j < 0)
5270 continue;
5271 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5272 !isl_int_is_negone(bmap->eq[i][1 + j]))
5273 return 1;
5275 j += 1;
5276 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5277 if (k < 0)
5278 continue;
5279 j += k;
5280 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5281 !isl_int_is_negone(bmap->eq[i][1 + j]))
5282 return 1;
5284 j += 1;
5285 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5286 if (k >= 0)
5287 return 1;
5290 return 0;
5293 /* Remove any common factor g from the constraint coefficients in "v".
5294 * The constant term is stored in the first position and is replaced
5295 * by floor(c/g). If any common factor is removed and if this results
5296 * in a tightening of the constraint, then set *tightened.
5298 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5299 int *tightened)
5301 isl_ctx *ctx;
5303 if (!v)
5304 return NULL;
5305 ctx = isl_vec_get_ctx(v);
5306 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5307 if (isl_int_is_zero(ctx->normalize_gcd))
5308 return v;
5309 if (isl_int_is_one(ctx->normalize_gcd))
5310 return v;
5311 v = isl_vec_cow(v);
5312 if (!v)
5313 return NULL;
5314 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5315 *tightened = 1;
5316 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5317 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5318 v->size - 1);
5319 return v;
5322 /* If "bmap" is an integer set that satisfies any equality involving
5323 * more than 2 variables and/or has coefficients different from -1 and 1,
5324 * then use variable compression to reduce the coefficients by removing
5325 * any (hidden) common factor.
5326 * In particular, apply the variable compression to each constraint,
5327 * factor out any common factor in the non-constant coefficients and
5328 * then apply the inverse of the compression.
5329 * At the end, we mark the basic map as having reduced constants.
5330 * If this flag is still set on the next invocation of this function,
5331 * then we skip the computation.
5333 * Removing a common factor may result in a tightening of some of
5334 * the constraints. If this happens, then we may end up with two
5335 * opposite inequalities that can be replaced by an equality.
5336 * We therefore call isl_basic_map_detect_inequality_pairs,
5337 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5338 * and isl_basic_map_gauss if such a pair was found.
5340 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5341 __isl_take isl_basic_map *bmap)
5343 unsigned total;
5344 isl_ctx *ctx;
5345 isl_vec *v;
5346 isl_mat *eq, *T, *T2;
5347 int i;
5348 int tightened;
5350 if (!bmap)
5351 return NULL;
5352 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5353 return bmap;
5354 if (isl_basic_map_is_rational(bmap))
5355 return bmap;
5356 if (bmap->n_eq == 0)
5357 return bmap;
5358 if (!has_multiple_var_equality(bmap))
5359 return bmap;
5361 total = isl_basic_map_dim(bmap, isl_dim_all);
5362 ctx = isl_basic_map_get_ctx(bmap);
5363 v = isl_vec_alloc(ctx, 1 + total);
5364 if (!v)
5365 return isl_basic_map_free(bmap);
5367 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5368 T = isl_mat_variable_compression(eq, &T2);
5369 if (!T || !T2)
5370 goto error;
5371 if (T->n_col == 0) {
5372 isl_mat_free(T);
5373 isl_mat_free(T2);
5374 isl_vec_free(v);
5375 return isl_basic_map_set_to_empty(bmap);
5378 tightened = 0;
5379 for (i = 0; i < bmap->n_ineq; ++i) {
5380 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5381 v = isl_vec_mat_product(v, isl_mat_copy(T));
5382 v = normalize_constraint(v, &tightened);
5383 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5384 if (!v)
5385 goto error;
5386 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5389 isl_mat_free(T);
5390 isl_mat_free(T2);
5391 isl_vec_free(v);
5393 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5395 if (tightened) {
5396 int progress = 0;
5398 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5399 if (progress) {
5400 bmap = eliminate_divs_eq(bmap, &progress);
5401 bmap = isl_basic_map_gauss(bmap, NULL);
5405 return bmap;
5406 error:
5407 isl_mat_free(T);
5408 isl_mat_free(T2);
5409 isl_vec_free(v);
5410 return isl_basic_map_free(bmap);
5413 /* Shift the integer division at position "div" of "bmap"
5414 * by "shift" times the variable at position "pos".
5415 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5416 * corresponds to the constant term.
5418 * That is, if the integer division has the form
5420 * floor(f(x)/d)
5422 * then replace it by
5424 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5426 __isl_give isl_basic_map *isl_basic_map_shift_div(
5427 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5429 int i;
5430 unsigned total;
5432 if (isl_int_is_zero(shift))
5433 return bmap;
5434 if (!bmap)
5435 return NULL;
5437 total = isl_basic_map_dim(bmap, isl_dim_all);
5438 total -= isl_basic_map_dim(bmap, isl_dim_div);
5440 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5442 for (i = 0; i < bmap->n_eq; ++i) {
5443 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5444 continue;
5445 isl_int_submul(bmap->eq[i][pos],
5446 shift, bmap->eq[i][1 + total + div]);
5448 for (i = 0; i < bmap->n_ineq; ++i) {
5449 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5450 continue;
5451 isl_int_submul(bmap->ineq[i][pos],
5452 shift, bmap->ineq[i][1 + total + div]);
5454 for (i = 0; i < bmap->n_div; ++i) {
5455 if (isl_int_is_zero(bmap->div[i][0]))
5456 continue;
5457 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5458 continue;
5459 isl_int_submul(bmap->div[i][1 + pos],
5460 shift, bmap->div[i][1 + 1 + total + div]);
5463 return bmap;