isl_map.c: check_basic_map_compatible_range_multi_aff: return isl_stat
[isl.git] / isl_map_simplify.c
blob1a1f839be285301a54f9471d5866c56541954c88
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 if (isl_basic_map_free_div(bmap, 1) < 0)
317 return isl_basic_map_free(bmap);
319 return bmap;
320 error:
321 isl_basic_map_free(bmap);
322 return NULL;
325 struct isl_basic_map *isl_basic_map_normalize_constraints(
326 struct isl_basic_map *bmap)
328 int i;
329 isl_int gcd;
330 unsigned total = isl_basic_map_total_dim(bmap);
332 if (!bmap)
333 return NULL;
335 isl_int_init(gcd);
336 for (i = bmap->n_eq - 1; i >= 0; --i) {
337 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
338 if (isl_int_is_zero(gcd)) {
339 if (!isl_int_is_zero(bmap->eq[i][0])) {
340 bmap = isl_basic_map_set_to_empty(bmap);
341 break;
343 isl_basic_map_drop_equality(bmap, i);
344 continue;
346 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
347 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
348 if (isl_int_is_one(gcd))
349 continue;
350 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
351 bmap = isl_basic_map_set_to_empty(bmap);
352 break;
354 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
357 for (i = bmap->n_ineq - 1; i >= 0; --i) {
358 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
359 if (isl_int_is_zero(gcd)) {
360 if (isl_int_is_neg(bmap->ineq[i][0])) {
361 bmap = isl_basic_map_set_to_empty(bmap);
362 break;
364 isl_basic_map_drop_inequality(bmap, i);
365 continue;
367 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
368 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
369 if (isl_int_is_one(gcd))
370 continue;
371 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
372 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
374 isl_int_clear(gcd);
376 return bmap;
379 struct isl_basic_set *isl_basic_set_normalize_constraints(
380 struct isl_basic_set *bset)
382 isl_basic_map *bmap = bset_to_bmap(bset);
383 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
386 /* Reduce the coefficient of the variable at position "pos"
387 * in integer division "div", such that it lies in the half-open
388 * interval (1/2,1/2], extracting any excess value from this integer division.
389 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
390 * corresponds to the constant term.
392 * That is, the integer division is of the form
394 * floor((... + (c * d + r) * x_pos + ...)/d)
396 * with -d < 2 * r <= d.
397 * Replace it by
399 * floor((... + r * x_pos + ...)/d) + c * x_pos
401 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
402 * Otherwise, c = floor((c * d + r)/d) + 1.
404 * This is the same normalization that is performed by isl_aff_floor.
406 static __isl_give isl_basic_map *reduce_coefficient_in_div(
407 __isl_take isl_basic_map *bmap, int div, int pos)
409 isl_int shift;
410 int add_one;
412 isl_int_init(shift);
413 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
414 isl_int_mul_ui(shift, shift, 2);
415 add_one = isl_int_gt(shift, bmap->div[div][0]);
416 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
417 if (add_one)
418 isl_int_add_ui(shift, shift, 1);
419 isl_int_neg(shift, shift);
420 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
421 isl_int_clear(shift);
423 return bmap;
426 /* Does the coefficient of the variable at position "pos"
427 * in integer division "div" need to be reduced?
428 * That is, does it lie outside the half-open interval (1/2,1/2]?
429 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
430 * 2 * c != d.
432 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
433 int pos)
435 isl_bool r;
437 if (isl_int_is_zero(bmap->div[div][1 + pos]))
438 return isl_bool_false;
440 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
441 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
442 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
443 isl_int_divexact_ui(bmap->div[div][1 + pos],
444 bmap->div[div][1 + pos], 2);
446 return r;
449 /* Reduce the coefficients (including the constant term) of
450 * integer division "div", if needed.
451 * In particular, make sure all coefficients lie in
452 * the half-open interval (1/2,1/2].
454 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
455 __isl_take isl_basic_map *bmap, int div)
457 int i;
458 unsigned total = 1 + isl_basic_map_total_dim(bmap);
460 for (i = 0; i < total; ++i) {
461 isl_bool reduce;
463 reduce = needs_reduction(bmap, div, i);
464 if (reduce < 0)
465 return isl_basic_map_free(bmap);
466 if (!reduce)
467 continue;
468 bmap = reduce_coefficient_in_div(bmap, div, i);
469 if (!bmap)
470 break;
473 return bmap;
476 /* Reduce the coefficients (including the constant term) of
477 * the known integer divisions, if needed
478 * In particular, make sure all coefficients lie in
479 * the half-open interval (1/2,1/2].
481 static __isl_give isl_basic_map *reduce_div_coefficients(
482 __isl_take isl_basic_map *bmap)
484 int i;
486 if (!bmap)
487 return NULL;
488 if (bmap->n_div == 0)
489 return bmap;
491 for (i = 0; i < bmap->n_div; ++i) {
492 if (isl_int_is_zero(bmap->div[i][0]))
493 continue;
494 bmap = reduce_div_coefficients_of_div(bmap, i);
495 if (!bmap)
496 break;
499 return bmap;
502 /* Remove any common factor in numerator and denominator of the div expression,
503 * not taking into account the constant term.
504 * That is, if the div is of the form
506 * floor((a + m f(x))/(m d))
508 * then replace it by
510 * floor((floor(a/m) + f(x))/d)
512 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
513 * and can therefore not influence the result of the floor.
515 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
517 unsigned total = isl_basic_map_total_dim(bmap);
518 isl_ctx *ctx = bmap->ctx;
520 if (isl_int_is_zero(bmap->div[div][0]))
521 return;
522 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
523 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
524 if (isl_int_is_one(ctx->normalize_gcd))
525 return;
526 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
527 ctx->normalize_gcd);
528 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
529 ctx->normalize_gcd);
530 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
531 ctx->normalize_gcd, total);
534 /* Remove any common factor in numerator and denominator of a div expression,
535 * not taking into account the constant term.
536 * That is, look for any div of the form
538 * floor((a + m f(x))/(m d))
540 * and replace it by
542 * floor((floor(a/m) + f(x))/d)
544 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
545 * and can therefore not influence the result of the floor.
547 static __isl_give isl_basic_map *normalize_div_expressions(
548 __isl_take isl_basic_map *bmap)
550 int i;
552 if (!bmap)
553 return NULL;
554 if (bmap->n_div == 0)
555 return bmap;
557 for (i = 0; i < bmap->n_div; ++i)
558 normalize_div_expression(bmap, i);
560 return bmap;
563 /* Assumes divs have been ordered if keep_divs is set.
565 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
566 unsigned pos, isl_int *eq, int keep_divs, int *progress)
568 unsigned total;
569 unsigned space_total;
570 int k;
571 int last_div;
573 total = isl_basic_map_total_dim(bmap);
574 space_total = isl_space_dim(bmap->dim, isl_dim_all);
575 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
576 for (k = 0; k < bmap->n_eq; ++k) {
577 if (bmap->eq[k] == eq)
578 continue;
579 if (isl_int_is_zero(bmap->eq[k][1+pos]))
580 continue;
581 if (progress)
582 *progress = 1;
583 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
584 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
587 for (k = 0; k < bmap->n_ineq; ++k) {
588 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
589 continue;
590 if (progress)
591 *progress = 1;
592 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
593 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
597 for (k = 0; k < bmap->n_div; ++k) {
598 if (isl_int_is_zero(bmap->div[k][0]))
599 continue;
600 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
601 continue;
602 if (progress)
603 *progress = 1;
604 /* We need to be careful about circular definitions,
605 * so for now we just remove the definition of div k
606 * if the equality contains any divs.
607 * If keep_divs is set, then the divs have been ordered
608 * and we can keep the definition as long as the result
609 * is still ordered.
611 if (last_div == -1 || (keep_divs && last_div < k)) {
612 isl_seq_elim(bmap->div[k]+1, eq,
613 1+pos, 1+total, &bmap->div[k][0]);
614 normalize_div_expression(bmap, k);
615 } else
616 isl_seq_clr(bmap->div[k], 1 + total);
617 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
621 /* Assumes divs have been ordered if keep_divs is set.
623 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
624 isl_int *eq, unsigned div, int keep_divs)
626 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
628 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
630 bmap = isl_basic_map_drop_div(bmap, div);
632 return bmap;
635 /* Check if elimination of div "div" using equality "eq" would not
636 * result in a div depending on a later div.
638 static isl_bool ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
639 unsigned div)
641 int k;
642 int last_div;
643 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
644 unsigned pos = space_total + div;
646 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
647 if (last_div < 0 || last_div <= div)
648 return isl_bool_true;
650 for (k = 0; k <= last_div; ++k) {
651 if (isl_int_is_zero(bmap->div[k][0]))
652 continue;
653 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
654 return isl_bool_false;
657 return isl_bool_true;
660 /* Eliminate divs based on equalities
662 static struct isl_basic_map *eliminate_divs_eq(
663 struct isl_basic_map *bmap, int *progress)
665 int d;
666 int i;
667 int modified = 0;
668 unsigned off;
670 bmap = isl_basic_map_order_divs(bmap);
672 if (!bmap)
673 return NULL;
675 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
677 for (d = bmap->n_div - 1; d >= 0 ; --d) {
678 for (i = 0; i < bmap->n_eq; ++i) {
679 isl_bool ok;
681 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
682 !isl_int_is_negone(bmap->eq[i][off + d]))
683 continue;
684 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
685 if (ok < 0)
686 return isl_basic_map_free(bmap);
687 if (!ok)
688 continue;
689 modified = 1;
690 *progress = 1;
691 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
692 if (isl_basic_map_drop_equality(bmap, i) < 0)
693 return isl_basic_map_free(bmap);
694 break;
697 if (modified)
698 return eliminate_divs_eq(bmap, progress);
699 return bmap;
702 /* Elimininate divs based on inequalities
704 static struct isl_basic_map *eliminate_divs_ineq(
705 struct isl_basic_map *bmap, int *progress)
707 int d;
708 int i;
709 unsigned off;
710 struct isl_ctx *ctx;
712 if (!bmap)
713 return NULL;
715 ctx = bmap->ctx;
716 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
718 for (d = bmap->n_div - 1; d >= 0 ; --d) {
719 for (i = 0; i < bmap->n_eq; ++i)
720 if (!isl_int_is_zero(bmap->eq[i][off + d]))
721 break;
722 if (i < bmap->n_eq)
723 continue;
724 for (i = 0; i < bmap->n_ineq; ++i)
725 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
726 break;
727 if (i < bmap->n_ineq)
728 continue;
729 *progress = 1;
730 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
731 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
732 break;
733 bmap = isl_basic_map_drop_div(bmap, d);
734 if (!bmap)
735 break;
737 return bmap;
740 /* Does the equality constraint at position "eq" in "bmap" involve
741 * any local variables in the range [first, first + n)
742 * that are not marked as having an explicit representation?
744 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
745 int eq, unsigned first, unsigned n)
747 unsigned o_div;
748 int i;
750 if (!bmap)
751 return isl_bool_error;
753 o_div = isl_basic_map_offset(bmap, isl_dim_div);
754 for (i = 0; i < n; ++i) {
755 isl_bool unknown;
757 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
758 continue;
759 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
760 if (unknown < 0)
761 return isl_bool_error;
762 if (unknown)
763 return isl_bool_true;
766 return isl_bool_false;
769 /* The last local variable involved in the equality constraint
770 * at position "eq" in "bmap" is the local variable at position "div".
771 * It can therefore be used to extract an explicit representation
772 * for that variable.
773 * Do so unless the local variable already has an explicit representation or
774 * the explicit representation would involve any other local variables
775 * that in turn do not have an explicit representation.
776 * An equality constraint involving local variables without an explicit
777 * representation can be used in isl_basic_map_drop_redundant_divs
778 * to separate out an independent local variable. Introducing
779 * an explicit representation here would block this transformation,
780 * while the partial explicit representation in itself is not very useful.
781 * Set *progress if anything is changed.
783 * The equality constraint is of the form
785 * f(x) + n e >= 0
787 * with n a positive number. The explicit representation derived from
788 * this constraint is
790 * floor((-f(x))/n)
792 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
793 int div, int eq, int *progress)
795 unsigned total, o_div;
796 isl_bool involves;
798 if (!bmap)
799 return NULL;
801 if (!isl_int_is_zero(bmap->div[div][0]))
802 return bmap;
804 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
805 if (involves < 0)
806 return isl_basic_map_free(bmap);
807 if (involves)
808 return bmap;
810 total = isl_basic_map_dim(bmap, isl_dim_all);
811 o_div = isl_basic_map_offset(bmap, isl_dim_div);
812 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
813 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
814 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
815 if (progress)
816 *progress = 1;
817 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
819 return bmap;
822 struct isl_basic_map *isl_basic_map_gauss(
823 struct isl_basic_map *bmap, int *progress)
825 int k;
826 int done;
827 int last_var;
828 unsigned total_var;
829 unsigned total;
831 bmap = isl_basic_map_order_divs(bmap);
833 if (!bmap)
834 return NULL;
836 total = isl_basic_map_total_dim(bmap);
837 total_var = total - bmap->n_div;
839 last_var = total - 1;
840 for (done = 0; done < bmap->n_eq; ++done) {
841 for (; last_var >= 0; --last_var) {
842 for (k = done; k < bmap->n_eq; ++k)
843 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
844 break;
845 if (k < bmap->n_eq)
846 break;
848 if (last_var < 0)
849 break;
850 if (k != done)
851 swap_equality(bmap, k, done);
852 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
853 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
855 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
856 progress);
858 if (last_var >= total_var)
859 bmap = set_div_from_eq(bmap, last_var - total_var,
860 done, progress);
861 if (!bmap)
862 return NULL;
864 if (done == bmap->n_eq)
865 return bmap;
866 for (k = done; k < bmap->n_eq; ++k) {
867 if (isl_int_is_zero(bmap->eq[k][0]))
868 continue;
869 return isl_basic_map_set_to_empty(bmap);
871 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
872 return bmap;
875 struct isl_basic_set *isl_basic_set_gauss(
876 struct isl_basic_set *bset, int *progress)
878 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
879 progress));
883 static unsigned int round_up(unsigned int v)
885 int old_v = v;
887 while (v) {
888 old_v = v;
889 v ^= v & -v;
891 return old_v << 1;
894 /* Hash table of inequalities in a basic map.
895 * "index" is an array of addresses of inequalities in the basic map, some
896 * of which are NULL. The inequalities are hashed on the coefficients
897 * except the constant term.
898 * "size" is the number of elements in the array and is always a power of two
899 * "bits" is the number of bits need to represent an index into the array.
900 * "total" is the total dimension of the basic map.
902 struct isl_constraint_index {
903 unsigned int size;
904 int bits;
905 isl_int ***index;
906 unsigned total;
909 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
911 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
912 __isl_keep isl_basic_map *bmap)
914 isl_ctx *ctx;
916 ci->index = NULL;
917 if (!bmap)
918 return isl_stat_error;
919 ci->total = isl_basic_set_total_dim(bmap);
920 if (bmap->n_ineq == 0)
921 return isl_stat_ok;
922 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
923 ci->bits = ffs(ci->size) - 1;
924 ctx = isl_basic_map_get_ctx(bmap);
925 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
926 if (!ci->index)
927 return isl_stat_error;
929 return isl_stat_ok;
932 /* Free the memory allocated by create_constraint_index.
934 static void constraint_index_free(struct isl_constraint_index *ci)
936 free(ci->index);
939 /* Return the position in ci->index that contains the address of
940 * an inequality that is equal to *ineq up to the constant term,
941 * provided this address is not identical to "ineq".
942 * If there is no such inequality, then return the position where
943 * such an inequality should be inserted.
945 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
947 int h;
948 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
949 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
950 if (ineq != ci->index[h] &&
951 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
952 break;
953 return h;
956 /* Return the position in ci->index that contains the address of
957 * an inequality that is equal to the k'th inequality of "bmap"
958 * up to the constant term, provided it does not point to the very
959 * same inequality.
960 * If there is no such inequality, then return the position where
961 * such an inequality should be inserted.
963 static int hash_index(struct isl_constraint_index *ci,
964 __isl_keep isl_basic_map *bmap, int k)
966 return hash_index_ineq(ci, &bmap->ineq[k]);
969 static int set_hash_index(struct isl_constraint_index *ci,
970 struct isl_basic_set *bset, int k)
972 return hash_index(ci, bset, k);
975 /* Fill in the "ci" data structure with the inequalities of "bset".
977 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
978 __isl_keep isl_basic_set *bset)
980 int k, h;
982 if (create_constraint_index(ci, bset) < 0)
983 return isl_stat_error;
985 for (k = 0; k < bset->n_ineq; ++k) {
986 h = set_hash_index(ci, bset, k);
987 ci->index[h] = &bset->ineq[k];
990 return isl_stat_ok;
993 /* Is the inequality ineq (obviously) redundant with respect
994 * to the constraints in "ci"?
996 * Look for an inequality in "ci" with the same coefficients and then
997 * check if the contant term of "ineq" is greater than or equal
998 * to the constant term of that inequality. If so, "ineq" is clearly
999 * redundant.
1001 * Note that hash_index_ineq ignores a stored constraint if it has
1002 * the same address as the passed inequality. It is ok to pass
1003 * the address of a local variable here since it will never be
1004 * the same as the address of a constraint in "ci".
1006 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
1007 isl_int *ineq)
1009 int h;
1011 h = hash_index_ineq(ci, &ineq);
1012 if (!ci->index[h])
1013 return isl_bool_false;
1014 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
1017 /* If we can eliminate more than one div, then we need to make
1018 * sure we do it from last div to first div, in order not to
1019 * change the position of the other divs that still need to
1020 * be removed.
1022 static struct isl_basic_map *remove_duplicate_divs(
1023 struct isl_basic_map *bmap, int *progress)
1025 unsigned int size;
1026 int *index;
1027 int *elim_for;
1028 int k, l, h;
1029 int bits;
1030 struct isl_blk eq;
1031 unsigned total_var;
1032 unsigned total;
1033 struct isl_ctx *ctx;
1035 bmap = isl_basic_map_order_divs(bmap);
1036 if (!bmap || bmap->n_div <= 1)
1037 return bmap;
1039 total_var = isl_space_dim(bmap->dim, isl_dim_all);
1040 total = total_var + bmap->n_div;
1042 ctx = bmap->ctx;
1043 for (k = bmap->n_div - 1; k >= 0; --k)
1044 if (!isl_int_is_zero(bmap->div[k][0]))
1045 break;
1046 if (k <= 0)
1047 return bmap;
1049 size = round_up(4 * bmap->n_div / 3 - 1);
1050 if (size == 0)
1051 return bmap;
1052 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
1053 bits = ffs(size) - 1;
1054 index = isl_calloc_array(ctx, int, size);
1055 if (!elim_for || !index)
1056 goto out;
1057 eq = isl_blk_alloc(ctx, 1+total);
1058 if (isl_blk_is_error(eq))
1059 goto out;
1061 isl_seq_clr(eq.data, 1+total);
1062 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
1063 for (--k; k >= 0; --k) {
1064 uint32_t hash;
1066 if (isl_int_is_zero(bmap->div[k][0]))
1067 continue;
1069 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
1070 for (h = hash; index[h]; h = (h+1) % size)
1071 if (isl_seq_eq(bmap->div[k],
1072 bmap->div[index[h]-1], 2+total))
1073 break;
1074 if (index[h]) {
1075 *progress = 1;
1076 l = index[h] - 1;
1077 elim_for[l] = k + 1;
1079 index[h] = k+1;
1081 for (l = bmap->n_div - 1; l >= 0; --l) {
1082 if (!elim_for[l])
1083 continue;
1084 k = elim_for[l] - 1;
1085 isl_int_set_si(eq.data[1+total_var+k], -1);
1086 isl_int_set_si(eq.data[1+total_var+l], 1);
1087 bmap = eliminate_div(bmap, eq.data, l, 1);
1088 if (!bmap)
1089 break;
1090 isl_int_set_si(eq.data[1+total_var+k], 0);
1091 isl_int_set_si(eq.data[1+total_var+l], 0);
1094 isl_blk_free(ctx, eq);
1095 out:
1096 free(index);
1097 free(elim_for);
1098 return bmap;
1101 static int n_pure_div_eq(struct isl_basic_map *bmap)
1103 int i, j;
1104 unsigned total;
1106 total = isl_space_dim(bmap->dim, isl_dim_all);
1107 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1108 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1109 --j;
1110 if (j < 0)
1111 break;
1112 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
1113 return 0;
1115 return i;
1118 /* Normalize divs that appear in equalities.
1120 * In particular, we assume that bmap contains some equalities
1121 * of the form
1123 * a x = m * e_i
1125 * and we want to replace the set of e_i by a minimal set and
1126 * such that the new e_i have a canonical representation in terms
1127 * of the vector x.
1128 * If any of the equalities involves more than one divs, then
1129 * we currently simply bail out.
1131 * Let us first additionally assume that all equalities involve
1132 * a div. The equalities then express modulo constraints on the
1133 * remaining variables and we can use "parameter compression"
1134 * to find a minimal set of constraints. The result is a transformation
1136 * x = T(x') = x_0 + G x'
1138 * with G a lower-triangular matrix with all elements below the diagonal
1139 * non-negative and smaller than the diagonal element on the same row.
1140 * We first normalize x_0 by making the same property hold in the affine
1141 * T matrix.
1142 * The rows i of G with a 1 on the diagonal do not impose any modulo
1143 * constraint and simply express x_i = x'_i.
1144 * For each of the remaining rows i, we introduce a div and a corresponding
1145 * equality. In particular
1147 * g_ii e_j = x_i - g_i(x')
1149 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1150 * corresponding div (if g_kk != 1).
1152 * If there are any equalities not involving any div, then we
1153 * first apply a variable compression on the variables x:
1155 * x = C x'' x'' = C_2 x
1157 * and perform the above parameter compression on A C instead of on A.
1158 * The resulting compression is then of the form
1160 * x'' = T(x') = x_0 + G x'
1162 * and in constructing the new divs and the corresponding equalities,
1163 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1164 * by the corresponding row from C_2.
1166 static struct isl_basic_map *normalize_divs(
1167 struct isl_basic_map *bmap, int *progress)
1169 int i, j, k;
1170 int total;
1171 int div_eq;
1172 struct isl_mat *B;
1173 struct isl_vec *d;
1174 struct isl_mat *T = NULL;
1175 struct isl_mat *C = NULL;
1176 struct isl_mat *C2 = NULL;
1177 isl_int v;
1178 int *pos = NULL;
1179 int dropped, needed;
1181 if (!bmap)
1182 return NULL;
1184 if (bmap->n_div == 0)
1185 return bmap;
1187 if (bmap->n_eq == 0)
1188 return bmap;
1190 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1191 return bmap;
1193 total = isl_space_dim(bmap->dim, isl_dim_all);
1194 div_eq = n_pure_div_eq(bmap);
1195 if (div_eq == 0)
1196 return bmap;
1198 if (div_eq < bmap->n_eq) {
1199 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1200 bmap->n_eq - div_eq, 0, 1 + total);
1201 C = isl_mat_variable_compression(B, &C2);
1202 if (!C || !C2)
1203 goto error;
1204 if (C->n_col == 0) {
1205 bmap = isl_basic_map_set_to_empty(bmap);
1206 isl_mat_free(C);
1207 isl_mat_free(C2);
1208 goto done;
1212 d = isl_vec_alloc(bmap->ctx, div_eq);
1213 if (!d)
1214 goto error;
1215 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1216 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1217 --j;
1218 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1220 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1222 if (C) {
1223 B = isl_mat_product(B, C);
1224 C = NULL;
1227 T = isl_mat_parameter_compression(B, d);
1228 if (!T)
1229 goto error;
1230 if (T->n_col == 0) {
1231 bmap = isl_basic_map_set_to_empty(bmap);
1232 isl_mat_free(C2);
1233 isl_mat_free(T);
1234 goto done;
1236 isl_int_init(v);
1237 for (i = 0; i < T->n_row - 1; ++i) {
1238 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1239 if (isl_int_is_zero(v))
1240 continue;
1241 isl_mat_col_submul(T, 0, v, 1 + i);
1243 isl_int_clear(v);
1244 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1245 if (!pos)
1246 goto error;
1247 /* We have to be careful because dropping equalities may reorder them */
1248 dropped = 0;
1249 for (j = bmap->n_div - 1; j >= 0; --j) {
1250 for (i = 0; i < bmap->n_eq; ++i)
1251 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1252 break;
1253 if (i < bmap->n_eq) {
1254 bmap = isl_basic_map_drop_div(bmap, j);
1255 isl_basic_map_drop_equality(bmap, i);
1256 ++dropped;
1259 pos[0] = 0;
1260 needed = 0;
1261 for (i = 1; i < T->n_row; ++i) {
1262 if (isl_int_is_one(T->row[i][i]))
1263 pos[i] = i;
1264 else
1265 needed++;
1267 if (needed > dropped) {
1268 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1269 needed, needed, 0);
1270 if (!bmap)
1271 goto error;
1273 for (i = 1; i < T->n_row; ++i) {
1274 if (isl_int_is_one(T->row[i][i]))
1275 continue;
1276 k = isl_basic_map_alloc_div(bmap);
1277 pos[i] = 1 + total + k;
1278 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1279 isl_int_set(bmap->div[k][0], T->row[i][i]);
1280 if (C2)
1281 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1282 else
1283 isl_int_set_si(bmap->div[k][1 + i], 1);
1284 for (j = 0; j < i; ++j) {
1285 if (isl_int_is_zero(T->row[i][j]))
1286 continue;
1287 if (pos[j] < T->n_row && C2)
1288 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1289 C2->row[pos[j]], 1 + total);
1290 else
1291 isl_int_neg(bmap->div[k][1 + pos[j]],
1292 T->row[i][j]);
1294 j = isl_basic_map_alloc_equality(bmap);
1295 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1296 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1298 free(pos);
1299 isl_mat_free(C2);
1300 isl_mat_free(T);
1302 if (progress)
1303 *progress = 1;
1304 done:
1305 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1307 return bmap;
1308 error:
1309 free(pos);
1310 isl_mat_free(C);
1311 isl_mat_free(C2);
1312 isl_mat_free(T);
1313 return bmap;
1316 static struct isl_basic_map *set_div_from_lower_bound(
1317 struct isl_basic_map *bmap, int div, int ineq)
1319 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1321 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1322 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1323 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1324 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1325 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1327 return bmap;
1330 /* Check whether it is ok to define a div based on an inequality.
1331 * To avoid the introduction of circular definitions of divs, we
1332 * do not allow such a definition if the resulting expression would refer to
1333 * any other undefined divs or if any known div is defined in
1334 * terms of the unknown div.
1336 static isl_bool ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1337 int div, int ineq)
1339 int j;
1340 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1342 /* Not defined in terms of unknown divs */
1343 for (j = 0; j < bmap->n_div; ++j) {
1344 if (div == j)
1345 continue;
1346 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1347 continue;
1348 if (isl_int_is_zero(bmap->div[j][0]))
1349 return isl_bool_false;
1352 /* No other div defined in terms of this one => avoid loops */
1353 for (j = 0; j < bmap->n_div; ++j) {
1354 if (div == j)
1355 continue;
1356 if (isl_int_is_zero(bmap->div[j][0]))
1357 continue;
1358 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1359 return isl_bool_false;
1362 return isl_bool_true;
1365 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1366 * be a better expression than the current one?
1368 * If we do not have any expression yet, then any expression would be better.
1369 * Otherwise we check if the last variable involved in the inequality
1370 * (disregarding the div that it would define) is in an earlier position
1371 * than the last variable involved in the current div expression.
1373 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1374 int div, int ineq)
1376 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1377 int last_div;
1378 int last_ineq;
1380 if (isl_int_is_zero(bmap->div[div][0]))
1381 return isl_bool_true;
1383 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1384 bmap->n_div - (div + 1)) >= 0)
1385 return isl_bool_false;
1387 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1388 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1389 total + bmap->n_div);
1391 return last_ineq < last_div;
1394 /* Given two constraints "k" and "l" that are opposite to each other,
1395 * except for the constant term, check if we can use them
1396 * to obtain an expression for one of the hitherto unknown divs or
1397 * a "better" expression for a div for which we already have an expression.
1398 * "sum" is the sum of the constant terms of the constraints.
1399 * If this sum is strictly smaller than the coefficient of one
1400 * of the divs, then this pair can be used define the div.
1401 * To avoid the introduction of circular definitions of divs, we
1402 * do not use the pair if the resulting expression would refer to
1403 * any other undefined divs or if any known div is defined in
1404 * terms of the unknown div.
1406 static struct isl_basic_map *check_for_div_constraints(
1407 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1409 int i;
1410 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1412 for (i = 0; i < bmap->n_div; ++i) {
1413 isl_bool set_div;
1415 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1416 continue;
1417 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1418 continue;
1419 set_div = better_div_constraint(bmap, i, k);
1420 if (set_div >= 0 && set_div)
1421 set_div = ok_to_set_div_from_bound(bmap, i, k);
1422 if (set_div < 0)
1423 return isl_basic_map_free(bmap);
1424 if (!set_div)
1425 break;
1426 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1427 bmap = set_div_from_lower_bound(bmap, i, k);
1428 else
1429 bmap = set_div_from_lower_bound(bmap, i, l);
1430 if (progress)
1431 *progress = 1;
1432 break;
1434 return bmap;
1437 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1438 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1440 struct isl_constraint_index ci;
1441 int k, l, h;
1442 unsigned total = isl_basic_map_total_dim(bmap);
1443 isl_int sum;
1445 if (!bmap || bmap->n_ineq <= 1)
1446 return bmap;
1448 if (create_constraint_index(&ci, bmap) < 0)
1449 return bmap;
1451 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1452 ci.index[h] = &bmap->ineq[0];
1453 for (k = 1; k < bmap->n_ineq; ++k) {
1454 h = hash_index(&ci, bmap, k);
1455 if (!ci.index[h]) {
1456 ci.index[h] = &bmap->ineq[k];
1457 continue;
1459 if (progress)
1460 *progress = 1;
1461 l = ci.index[h] - &bmap->ineq[0];
1462 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1463 swap_inequality(bmap, k, l);
1464 isl_basic_map_drop_inequality(bmap, k);
1465 --k;
1467 isl_int_init(sum);
1468 for (k = 0; k < bmap->n_ineq-1; ++k) {
1469 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1470 h = hash_index(&ci, bmap, k);
1471 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1472 if (!ci.index[h])
1473 continue;
1474 l = ci.index[h] - &bmap->ineq[0];
1475 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1476 if (isl_int_is_pos(sum)) {
1477 if (detect_divs)
1478 bmap = check_for_div_constraints(bmap, k, l,
1479 sum, progress);
1480 continue;
1482 if (isl_int_is_zero(sum)) {
1483 /* We need to break out of the loop after these
1484 * changes since the contents of the hash
1485 * will no longer be valid.
1486 * Plus, we probably we want to regauss first.
1488 if (progress)
1489 *progress = 1;
1490 isl_basic_map_drop_inequality(bmap, l);
1491 isl_basic_map_inequality_to_equality(bmap, k);
1492 } else
1493 bmap = isl_basic_map_set_to_empty(bmap);
1494 break;
1496 isl_int_clear(sum);
1498 constraint_index_free(&ci);
1499 return bmap;
1502 /* Detect all pairs of inequalities that form an equality.
1504 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1505 * Call it repeatedly while it is making progress.
1507 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1508 __isl_take isl_basic_map *bmap, int *progress)
1510 int duplicate;
1512 do {
1513 duplicate = 0;
1514 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1515 &duplicate, 0);
1516 if (progress && duplicate)
1517 *progress = 1;
1518 } while (duplicate);
1520 return bmap;
1523 /* Eliminate knowns divs from constraints where they appear with
1524 * a (positive or negative) unit coefficient.
1526 * That is, replace
1528 * floor(e/m) + f >= 0
1530 * by
1532 * e + m f >= 0
1534 * and
1536 * -floor(e/m) + f >= 0
1538 * by
1540 * -e + m f + m - 1 >= 0
1542 * The first conversion is valid because floor(e/m) >= -f is equivalent
1543 * to e/m >= -f because -f is an integral expression.
1544 * The second conversion follows from the fact that
1546 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1549 * Note that one of the div constraints may have been eliminated
1550 * due to being redundant with respect to the constraint that is
1551 * being modified by this function. The modified constraint may
1552 * no longer imply this div constraint, so we add it back to make
1553 * sure we do not lose any information.
1555 * We skip integral divs, i.e., those with denominator 1, as we would
1556 * risk eliminating the div from the div constraints. We do not need
1557 * to handle those divs here anyway since the div constraints will turn
1558 * out to form an equality and this equality can then be used to eliminate
1559 * the div from all constraints.
1561 static __isl_give isl_basic_map *eliminate_unit_divs(
1562 __isl_take isl_basic_map *bmap, int *progress)
1564 int i, j;
1565 isl_ctx *ctx;
1566 unsigned total;
1568 if (!bmap)
1569 return NULL;
1571 ctx = isl_basic_map_get_ctx(bmap);
1572 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1574 for (i = 0; i < bmap->n_div; ++i) {
1575 if (isl_int_is_zero(bmap->div[i][0]))
1576 continue;
1577 if (isl_int_is_one(bmap->div[i][0]))
1578 continue;
1579 for (j = 0; j < bmap->n_ineq; ++j) {
1580 int s;
1582 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1583 !isl_int_is_negone(bmap->ineq[j][total + i]))
1584 continue;
1586 *progress = 1;
1588 s = isl_int_sgn(bmap->ineq[j][total + i]);
1589 isl_int_set_si(bmap->ineq[j][total + i], 0);
1590 if (s < 0)
1591 isl_seq_combine(bmap->ineq[j],
1592 ctx->negone, bmap->div[i] + 1,
1593 bmap->div[i][0], bmap->ineq[j],
1594 total + bmap->n_div);
1595 else
1596 isl_seq_combine(bmap->ineq[j],
1597 ctx->one, bmap->div[i] + 1,
1598 bmap->div[i][0], bmap->ineq[j],
1599 total + bmap->n_div);
1600 if (s < 0) {
1601 isl_int_add(bmap->ineq[j][0],
1602 bmap->ineq[j][0], bmap->div[i][0]);
1603 isl_int_sub_ui(bmap->ineq[j][0],
1604 bmap->ineq[j][0], 1);
1607 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1608 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1609 return isl_basic_map_free(bmap);
1613 return bmap;
1616 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1618 int progress = 1;
1619 if (!bmap)
1620 return NULL;
1621 while (progress) {
1622 isl_bool empty;
1624 progress = 0;
1625 empty = isl_basic_map_plain_is_empty(bmap);
1626 if (empty < 0)
1627 return isl_basic_map_free(bmap);
1628 if (empty)
1629 break;
1630 bmap = isl_basic_map_normalize_constraints(bmap);
1631 bmap = reduce_div_coefficients(bmap);
1632 bmap = normalize_div_expressions(bmap);
1633 bmap = remove_duplicate_divs(bmap, &progress);
1634 bmap = eliminate_unit_divs(bmap, &progress);
1635 bmap = eliminate_divs_eq(bmap, &progress);
1636 bmap = eliminate_divs_ineq(bmap, &progress);
1637 bmap = isl_basic_map_gauss(bmap, &progress);
1638 /* requires equalities in normal form */
1639 bmap = normalize_divs(bmap, &progress);
1640 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1641 &progress, 1);
1642 if (bmap && progress)
1643 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1645 return bmap;
1648 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1650 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1654 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1655 isl_int *constraint, unsigned div)
1657 unsigned pos;
1659 if (!bmap)
1660 return isl_bool_error;
1662 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1664 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1665 int neg;
1666 isl_int_sub(bmap->div[div][1],
1667 bmap->div[div][1], bmap->div[div][0]);
1668 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1669 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1670 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1671 isl_int_add(bmap->div[div][1],
1672 bmap->div[div][1], bmap->div[div][0]);
1673 if (!neg)
1674 return isl_bool_false;
1675 if (isl_seq_first_non_zero(constraint+pos+1,
1676 bmap->n_div-div-1) != -1)
1677 return isl_bool_false;
1678 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1679 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1680 return isl_bool_false;
1681 if (isl_seq_first_non_zero(constraint+pos+1,
1682 bmap->n_div-div-1) != -1)
1683 return isl_bool_false;
1684 } else
1685 return isl_bool_false;
1687 return isl_bool_true;
1690 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1691 isl_int *constraint, unsigned div)
1693 return isl_basic_map_is_div_constraint(bset, constraint, div);
1697 /* If the only constraints a div d=floor(f/m)
1698 * appears in are its two defining constraints
1700 * f - m d >=0
1701 * -(f - (m - 1)) + m d >= 0
1703 * then it can safely be removed.
1705 static isl_bool div_is_redundant(struct isl_basic_map *bmap, int div)
1707 int i;
1708 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1710 for (i = 0; i < bmap->n_eq; ++i)
1711 if (!isl_int_is_zero(bmap->eq[i][pos]))
1712 return isl_bool_false;
1714 for (i = 0; i < bmap->n_ineq; ++i) {
1715 isl_bool red;
1717 if (isl_int_is_zero(bmap->ineq[i][pos]))
1718 continue;
1719 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1720 if (red < 0 || !red)
1721 return red;
1724 for (i = 0; i < bmap->n_div; ++i) {
1725 if (isl_int_is_zero(bmap->div[i][0]))
1726 continue;
1727 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1728 return isl_bool_false;
1731 return isl_bool_true;
1735 * Remove divs that don't occur in any of the constraints or other divs.
1736 * These can arise when dropping constraints from a basic map or
1737 * when the divs of a basic map have been temporarily aligned
1738 * with the divs of another basic map.
1740 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1742 int i;
1744 if (!bmap)
1745 return NULL;
1747 for (i = bmap->n_div-1; i >= 0; --i) {
1748 isl_bool redundant;
1750 redundant = div_is_redundant(bmap, i);
1751 if (redundant < 0)
1752 return isl_basic_map_free(bmap);
1753 if (!redundant)
1754 continue;
1755 bmap = isl_basic_map_drop_div(bmap, i);
1757 return bmap;
1760 /* Mark "bmap" as final, without checking for obviously redundant
1761 * integer divisions. This function should be used when "bmap"
1762 * is known not to involve any such integer divisions.
1764 __isl_give isl_basic_map *isl_basic_map_mark_final(
1765 __isl_take isl_basic_map *bmap)
1767 if (!bmap)
1768 return NULL;
1769 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1770 return bmap;
1773 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1775 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1777 bmap = remove_redundant_divs(bmap);
1778 bmap = isl_basic_map_mark_final(bmap);
1779 return bmap;
1782 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1784 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1787 struct isl_set *isl_set_finalize(struct isl_set *set)
1789 int i;
1791 if (!set)
1792 return NULL;
1793 for (i = 0; i < set->n; ++i) {
1794 set->p[i] = isl_basic_set_finalize(set->p[i]);
1795 if (!set->p[i])
1796 goto error;
1798 return set;
1799 error:
1800 isl_set_free(set);
1801 return NULL;
1804 struct isl_map *isl_map_finalize(struct isl_map *map)
1806 int i;
1808 if (!map)
1809 return NULL;
1810 for (i = 0; i < map->n; ++i) {
1811 map->p[i] = isl_basic_map_finalize(map->p[i]);
1812 if (!map->p[i])
1813 goto error;
1815 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1816 return map;
1817 error:
1818 isl_map_free(map);
1819 return NULL;
1823 /* Remove definition of any div that is defined in terms of the given variable.
1824 * The div itself is not removed. Functions such as
1825 * eliminate_divs_ineq depend on the other divs remaining in place.
1827 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1828 int pos)
1830 int i;
1832 if (!bmap)
1833 return NULL;
1835 for (i = 0; i < bmap->n_div; ++i) {
1836 if (isl_int_is_zero(bmap->div[i][0]))
1837 continue;
1838 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1839 continue;
1840 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1841 if (!bmap)
1842 return NULL;
1844 return bmap;
1847 /* Eliminate the specified variables from the constraints using
1848 * Fourier-Motzkin. The variables themselves are not removed.
1850 struct isl_basic_map *isl_basic_map_eliminate_vars(
1851 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1853 int d;
1854 int i, j, k;
1855 unsigned total;
1856 int need_gauss = 0;
1858 if (n == 0)
1859 return bmap;
1860 if (!bmap)
1861 return NULL;
1862 total = isl_basic_map_total_dim(bmap);
1864 bmap = isl_basic_map_cow(bmap);
1865 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1866 bmap = remove_dependent_vars(bmap, d);
1867 if (!bmap)
1868 return NULL;
1870 for (d = pos + n - 1;
1871 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1872 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1873 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1874 int n_lower, n_upper;
1875 if (!bmap)
1876 return NULL;
1877 for (i = 0; i < bmap->n_eq; ++i) {
1878 if (isl_int_is_zero(bmap->eq[i][1+d]))
1879 continue;
1880 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1881 isl_basic_map_drop_equality(bmap, i);
1882 need_gauss = 1;
1883 break;
1885 if (i < bmap->n_eq)
1886 continue;
1887 n_lower = 0;
1888 n_upper = 0;
1889 for (i = 0; i < bmap->n_ineq; ++i) {
1890 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1891 n_lower++;
1892 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1893 n_upper++;
1895 bmap = isl_basic_map_extend_constraints(bmap,
1896 0, n_lower * n_upper);
1897 if (!bmap)
1898 goto error;
1899 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1900 int last;
1901 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1902 continue;
1903 last = -1;
1904 for (j = 0; j < i; ++j) {
1905 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1906 continue;
1907 last = j;
1908 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1909 isl_int_sgn(bmap->ineq[j][1+d]))
1910 continue;
1911 k = isl_basic_map_alloc_inequality(bmap);
1912 if (k < 0)
1913 goto error;
1914 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1915 1+total);
1916 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1917 1+d, 1+total, NULL);
1919 isl_basic_map_drop_inequality(bmap, i);
1920 i = last + 1;
1922 if (n_lower > 0 && n_upper > 0) {
1923 bmap = isl_basic_map_normalize_constraints(bmap);
1924 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1925 NULL, 0);
1926 bmap = isl_basic_map_gauss(bmap, NULL);
1927 bmap = isl_basic_map_remove_redundancies(bmap);
1928 need_gauss = 0;
1929 if (!bmap)
1930 goto error;
1931 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1932 break;
1935 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1936 if (need_gauss)
1937 bmap = isl_basic_map_gauss(bmap, NULL);
1938 return bmap;
1939 error:
1940 isl_basic_map_free(bmap);
1941 return NULL;
1944 struct isl_basic_set *isl_basic_set_eliminate_vars(
1945 struct isl_basic_set *bset, unsigned pos, unsigned n)
1947 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1948 pos, n));
1951 /* Eliminate the specified n dimensions starting at first from the
1952 * constraints, without removing the dimensions from the space.
1953 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1954 * Otherwise, they are projected out and the original space is restored.
1956 __isl_give isl_basic_map *isl_basic_map_eliminate(
1957 __isl_take isl_basic_map *bmap,
1958 enum isl_dim_type type, unsigned first, unsigned n)
1960 isl_space *space;
1962 if (!bmap)
1963 return NULL;
1964 if (n == 0)
1965 return bmap;
1967 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1968 isl_die(bmap->ctx, isl_error_invalid,
1969 "index out of bounds", goto error);
1971 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1972 first += isl_basic_map_offset(bmap, type) - 1;
1973 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1974 return isl_basic_map_finalize(bmap);
1977 space = isl_basic_map_get_space(bmap);
1978 bmap = isl_basic_map_project_out(bmap, type, first, n);
1979 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1980 bmap = isl_basic_map_reset_space(bmap, space);
1981 return bmap;
1982 error:
1983 isl_basic_map_free(bmap);
1984 return NULL;
1987 __isl_give isl_basic_set *isl_basic_set_eliminate(
1988 __isl_take isl_basic_set *bset,
1989 enum isl_dim_type type, unsigned first, unsigned n)
1991 return isl_basic_map_eliminate(bset, type, first, n);
1994 /* Remove all constraints from "bmap" that reference any unknown local
1995 * variables (directly or indirectly).
1997 * Dropping all constraints on a local variable will make it redundant,
1998 * so it will get removed implicitly by
1999 * isl_basic_map_drop_constraints_involving_dims. Some other local
2000 * variables may also end up becoming redundant if they only appear
2001 * in constraints together with the unknown local variable.
2002 * Therefore, start over after calling
2003 * isl_basic_map_drop_constraints_involving_dims.
2005 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
2006 __isl_take isl_basic_map *bmap)
2008 isl_bool known;
2009 int i, n_div, o_div;
2011 known = isl_basic_map_divs_known(bmap);
2012 if (known < 0)
2013 return isl_basic_map_free(bmap);
2014 if (known)
2015 return bmap;
2017 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2018 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
2020 for (i = 0; i < n_div; ++i) {
2021 known = isl_basic_map_div_is_known(bmap, i);
2022 if (known < 0)
2023 return isl_basic_map_free(bmap);
2024 if (known)
2025 continue;
2026 bmap = remove_dependent_vars(bmap, o_div + i);
2027 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
2028 isl_dim_div, i, 1);
2029 if (!bmap)
2030 return NULL;
2031 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2032 i = -1;
2035 return bmap;
2038 /* Remove all constraints from "map" that reference any unknown local
2039 * variables (directly or indirectly).
2041 * Since constraints may get dropped from the basic maps,
2042 * they may no longer be disjoint from each other.
2044 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
2045 __isl_take isl_map *map)
2047 int i;
2048 isl_bool known;
2050 known = isl_map_divs_known(map);
2051 if (known < 0)
2052 return isl_map_free(map);
2053 if (known)
2054 return map;
2056 map = isl_map_cow(map);
2057 if (!map)
2058 return NULL;
2060 for (i = 0; i < map->n; ++i) {
2061 map->p[i] =
2062 isl_basic_map_drop_constraint_involving_unknown_divs(
2063 map->p[i]);
2064 if (!map->p[i])
2065 return isl_map_free(map);
2068 if (map->n > 1)
2069 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2071 return map;
2074 /* Don't assume equalities are in order, because align_divs
2075 * may have changed the order of the divs.
2077 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
2079 int d, i;
2080 unsigned total;
2082 total = isl_space_dim(bmap->dim, isl_dim_all);
2083 for (d = 0; d < total; ++d)
2084 elim[d] = -1;
2085 for (i = 0; i < bmap->n_eq; ++i) {
2086 for (d = total - 1; d >= 0; --d) {
2087 if (isl_int_is_zero(bmap->eq[i][1+d]))
2088 continue;
2089 elim[d] = i;
2090 break;
2095 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
2097 compute_elimination_index(bset_to_bmap(bset), elim);
2100 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2101 struct isl_basic_map *bmap, int *elim)
2103 int d;
2104 int copied = 0;
2105 unsigned total;
2107 total = isl_space_dim(bmap->dim, isl_dim_all);
2108 for (d = total - 1; d >= 0; --d) {
2109 if (isl_int_is_zero(src[1+d]))
2110 continue;
2111 if (elim[d] == -1)
2112 continue;
2113 if (!copied) {
2114 isl_seq_cpy(dst, src, 1 + total);
2115 copied = 1;
2117 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2119 return copied;
2122 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2123 struct isl_basic_set *bset, int *elim)
2125 return reduced_using_equalities(dst, src,
2126 bset_to_bmap(bset), elim);
2129 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
2130 struct isl_basic_set *bset, struct isl_basic_set *context)
2132 int i;
2133 int *elim;
2135 if (!bset || !context)
2136 goto error;
2138 if (context->n_eq == 0) {
2139 isl_basic_set_free(context);
2140 return bset;
2143 bset = isl_basic_set_cow(bset);
2144 if (!bset)
2145 goto error;
2147 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2148 if (!elim)
2149 goto error;
2150 set_compute_elimination_index(context, elim);
2151 for (i = 0; i < bset->n_eq; ++i)
2152 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2153 context, elim);
2154 for (i = 0; i < bset->n_ineq; ++i)
2155 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2156 context, elim);
2157 isl_basic_set_free(context);
2158 free(elim);
2159 bset = isl_basic_set_simplify(bset);
2160 bset = isl_basic_set_finalize(bset);
2161 return bset;
2162 error:
2163 isl_basic_set_free(bset);
2164 isl_basic_set_free(context);
2165 return NULL;
2168 /* For each inequality in "ineq" that is a shifted (more relaxed)
2169 * copy of an inequality in "context", mark the corresponding entry
2170 * in "row" with -1.
2171 * If an inequality only has a non-negative constant term, then
2172 * mark it as well.
2174 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2175 __isl_keep isl_basic_set *context, int *row)
2177 struct isl_constraint_index ci;
2178 int n_ineq;
2179 unsigned total;
2180 int k;
2182 if (!ineq || !context)
2183 return isl_stat_error;
2184 if (context->n_ineq == 0)
2185 return isl_stat_ok;
2186 if (setup_constraint_index(&ci, context) < 0)
2187 return isl_stat_error;
2189 n_ineq = isl_mat_rows(ineq);
2190 total = isl_mat_cols(ineq) - 1;
2191 for (k = 0; k < n_ineq; ++k) {
2192 int l;
2193 isl_bool redundant;
2195 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2196 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2197 row[k] = -1;
2198 continue;
2200 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2201 if (redundant < 0)
2202 goto error;
2203 if (!redundant)
2204 continue;
2205 row[k] = -1;
2207 constraint_index_free(&ci);
2208 return isl_stat_ok;
2209 error:
2210 constraint_index_free(&ci);
2211 return isl_stat_error;
2214 static struct isl_basic_set *remove_shifted_constraints(
2215 struct isl_basic_set *bset, struct isl_basic_set *context)
2217 struct isl_constraint_index ci;
2218 int k;
2220 if (!bset || !context)
2221 return bset;
2223 if (context->n_ineq == 0)
2224 return bset;
2225 if (setup_constraint_index(&ci, context) < 0)
2226 return bset;
2228 for (k = 0; k < bset->n_ineq; ++k) {
2229 isl_bool redundant;
2231 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2232 if (redundant < 0)
2233 goto error;
2234 if (!redundant)
2235 continue;
2236 bset = isl_basic_set_cow(bset);
2237 if (!bset)
2238 goto error;
2239 isl_basic_set_drop_inequality(bset, k);
2240 --k;
2242 constraint_index_free(&ci);
2243 return bset;
2244 error:
2245 constraint_index_free(&ci);
2246 return bset;
2249 /* Remove constraints from "bmap" that are identical to constraints
2250 * in "context" or that are more relaxed (greater constant term).
2252 * We perform the test for shifted copies on the pure constraints
2253 * in remove_shifted_constraints.
2255 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2256 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2258 isl_basic_set *bset, *bset_context;
2260 if (!bmap || !context)
2261 goto error;
2263 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2264 isl_basic_map_free(context);
2265 return bmap;
2268 context = isl_basic_map_align_divs(context, bmap);
2269 bmap = isl_basic_map_align_divs(bmap, context);
2271 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2272 bset_context = isl_basic_map_underlying_set(context);
2273 bset = remove_shifted_constraints(bset, bset_context);
2274 isl_basic_set_free(bset_context);
2276 bmap = isl_basic_map_overlying_set(bset, bmap);
2278 return bmap;
2279 error:
2280 isl_basic_map_free(bmap);
2281 isl_basic_map_free(context);
2282 return NULL;
2285 /* Does the (linear part of a) constraint "c" involve any of the "len"
2286 * "relevant" dimensions?
2288 static int is_related(isl_int *c, int len, int *relevant)
2290 int i;
2292 for (i = 0; i < len; ++i) {
2293 if (!relevant[i])
2294 continue;
2295 if (!isl_int_is_zero(c[i]))
2296 return 1;
2299 return 0;
2302 /* Drop constraints from "bmap" that do not involve any of
2303 * the dimensions marked "relevant".
2305 static __isl_give isl_basic_map *drop_unrelated_constraints(
2306 __isl_take isl_basic_map *bmap, int *relevant)
2308 int i, dim;
2310 dim = isl_basic_map_dim(bmap, isl_dim_all);
2311 for (i = 0; i < dim; ++i)
2312 if (!relevant[i])
2313 break;
2314 if (i >= dim)
2315 return bmap;
2317 for (i = bmap->n_eq - 1; i >= 0; --i)
2318 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2319 bmap = isl_basic_map_cow(bmap);
2320 if (isl_basic_map_drop_equality(bmap, i) < 0)
2321 return isl_basic_map_free(bmap);
2324 for (i = bmap->n_ineq - 1; i >= 0; --i)
2325 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2326 bmap = isl_basic_map_cow(bmap);
2327 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2328 return isl_basic_map_free(bmap);
2331 return bmap;
2334 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2336 * In particular, for any variable involved in the constraint,
2337 * find the actual group id from before and replace the group
2338 * of the corresponding variable by the minimal group of all
2339 * the variables involved in the constraint considered so far
2340 * (if this minimum is smaller) or replace the minimum by this group
2341 * (if the minimum is larger).
2343 * At the end, all the variables in "c" will (indirectly) point
2344 * to the minimal of the groups that they referred to originally.
2346 static void update_groups(int dim, int *group, isl_int *c)
2348 int j;
2349 int min = dim;
2351 for (j = 0; j < dim; ++j) {
2352 if (isl_int_is_zero(c[j]))
2353 continue;
2354 while (group[j] >= 0 && group[group[j]] != group[j])
2355 group[j] = group[group[j]];
2356 if (group[j] == min)
2357 continue;
2358 if (group[j] < min) {
2359 if (min >= 0 && min < dim)
2360 group[min] = group[j];
2361 min = group[j];
2362 } else
2363 group[group[j]] = min;
2367 /* Allocate an array of groups of variables, one for each variable
2368 * in "context", initialized to zero.
2370 static int *alloc_groups(__isl_keep isl_basic_set *context)
2372 isl_ctx *ctx;
2373 int dim;
2375 dim = isl_basic_set_dim(context, isl_dim_set);
2376 ctx = isl_basic_set_get_ctx(context);
2377 return isl_calloc_array(ctx, int, dim);
2380 /* Drop constraints from "bmap" that only involve variables that are
2381 * not related to any of the variables marked with a "-1" in "group".
2383 * We construct groups of variables that collect variables that
2384 * (indirectly) appear in some common constraint of "bmap".
2385 * Each group is identified by the first variable in the group,
2386 * except for the special group of variables that was already identified
2387 * in the input as -1 (or are related to those variables).
2388 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2389 * otherwise the group of i is the group of group[i].
2391 * We first initialize groups for the remaining variables.
2392 * Then we iterate over the constraints of "bmap" and update the
2393 * group of the variables in the constraint by the smallest group.
2394 * Finally, we resolve indirect references to groups by running over
2395 * the variables.
2397 * After computing the groups, we drop constraints that do not involve
2398 * any variables in the -1 group.
2400 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2401 __isl_take isl_basic_map *bmap, __isl_take int *group)
2403 int dim;
2404 int i;
2405 int last;
2407 if (!bmap)
2408 return NULL;
2410 dim = isl_basic_map_dim(bmap, isl_dim_all);
2412 last = -1;
2413 for (i = 0; i < dim; ++i)
2414 if (group[i] >= 0)
2415 last = group[i] = i;
2416 if (last < 0) {
2417 free(group);
2418 return bmap;
2421 for (i = 0; i < bmap->n_eq; ++i)
2422 update_groups(dim, group, bmap->eq[i] + 1);
2423 for (i = 0; i < bmap->n_ineq; ++i)
2424 update_groups(dim, group, bmap->ineq[i] + 1);
2426 for (i = 0; i < dim; ++i)
2427 if (group[i] >= 0)
2428 group[i] = group[group[i]];
2430 for (i = 0; i < dim; ++i)
2431 group[i] = group[i] == -1;
2433 bmap = drop_unrelated_constraints(bmap, group);
2435 free(group);
2436 return bmap;
2439 /* Drop constraints from "context" that are irrelevant for computing
2440 * the gist of "bset".
2442 * In particular, drop constraints in variables that are not related
2443 * to any of the variables involved in the constraints of "bset"
2444 * in the sense that there is no sequence of constraints that connects them.
2446 * We first mark all variables that appear in "bset" as belonging
2447 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2449 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2450 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2452 int *group;
2453 int dim;
2454 int i, j;
2456 if (!context || !bset)
2457 return isl_basic_set_free(context);
2459 group = alloc_groups(context);
2461 if (!group)
2462 return isl_basic_set_free(context);
2464 dim = isl_basic_set_dim(bset, isl_dim_set);
2465 for (i = 0; i < dim; ++i) {
2466 for (j = 0; j < bset->n_eq; ++j)
2467 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2468 break;
2469 if (j < bset->n_eq) {
2470 group[i] = -1;
2471 continue;
2473 for (j = 0; j < bset->n_ineq; ++j)
2474 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2475 break;
2476 if (j < bset->n_ineq)
2477 group[i] = -1;
2480 return isl_basic_map_drop_unrelated_constraints(context, group);
2483 /* Drop constraints from "context" that are irrelevant for computing
2484 * the gist of the inequalities "ineq".
2485 * Inequalities in "ineq" for which the corresponding element of row
2486 * is set to -1 have already been marked for removal and should be ignored.
2488 * In particular, drop constraints in variables that are not related
2489 * to any of the variables involved in "ineq"
2490 * in the sense that there is no sequence of constraints that connects them.
2492 * We first mark all variables that appear in "bset" as belonging
2493 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2495 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2496 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2498 int *group;
2499 int dim;
2500 int i, j, n;
2502 if (!context || !ineq)
2503 return isl_basic_set_free(context);
2505 group = alloc_groups(context);
2507 if (!group)
2508 return isl_basic_set_free(context);
2510 dim = isl_basic_set_dim(context, isl_dim_set);
2511 n = isl_mat_rows(ineq);
2512 for (i = 0; i < dim; ++i) {
2513 for (j = 0; j < n; ++j) {
2514 if (row[j] < 0)
2515 continue;
2516 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2517 break;
2519 if (j < n)
2520 group[i] = -1;
2523 return isl_basic_map_drop_unrelated_constraints(context, group);
2526 /* Do all "n" entries of "row" contain a negative value?
2528 static int all_neg(int *row, int n)
2530 int i;
2532 for (i = 0; i < n; ++i)
2533 if (row[i] >= 0)
2534 return 0;
2536 return 1;
2539 /* Update the inequalities in "bset" based on the information in "row"
2540 * and "tab".
2542 * In particular, the array "row" contains either -1, meaning that
2543 * the corresponding inequality of "bset" is redundant, or the index
2544 * of an inequality in "tab".
2546 * If the row entry is -1, then drop the inequality.
2547 * Otherwise, if the constraint is marked redundant in the tableau,
2548 * then drop the inequality. Similarly, if it is marked as an equality
2549 * in the tableau, then turn the inequality into an equality and
2550 * perform Gaussian elimination.
2552 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2553 __isl_keep int *row, struct isl_tab *tab)
2555 int i;
2556 unsigned n_ineq;
2557 unsigned n_eq;
2558 int found_equality = 0;
2560 if (!bset)
2561 return NULL;
2562 if (tab && tab->empty)
2563 return isl_basic_set_set_to_empty(bset);
2565 n_ineq = bset->n_ineq;
2566 for (i = n_ineq - 1; i >= 0; --i) {
2567 if (row[i] < 0) {
2568 if (isl_basic_set_drop_inequality(bset, i) < 0)
2569 return isl_basic_set_free(bset);
2570 continue;
2572 if (!tab)
2573 continue;
2574 n_eq = tab->n_eq;
2575 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2576 isl_basic_map_inequality_to_equality(bset, i);
2577 found_equality = 1;
2578 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2579 if (isl_basic_set_drop_inequality(bset, i) < 0)
2580 return isl_basic_set_free(bset);
2584 if (found_equality)
2585 bset = isl_basic_set_gauss(bset, NULL);
2586 bset = isl_basic_set_finalize(bset);
2587 return bset;
2590 /* Update the inequalities in "bset" based on the information in "row"
2591 * and "tab" and free all arguments (other than "bset").
2593 static __isl_give isl_basic_set *update_ineq_free(
2594 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2595 __isl_take isl_basic_set *context, __isl_take int *row,
2596 struct isl_tab *tab)
2598 isl_mat_free(ineq);
2599 isl_basic_set_free(context);
2601 bset = update_ineq(bset, row, tab);
2603 free(row);
2604 isl_tab_free(tab);
2605 return bset;
2608 /* Remove all information from bset that is redundant in the context
2609 * of context.
2610 * "ineq" contains the (possibly transformed) inequalities of "bset",
2611 * in the same order.
2612 * The (explicit) equalities of "bset" are assumed to have been taken
2613 * into account by the transformation such that only the inequalities
2614 * are relevant.
2615 * "context" is assumed not to be empty.
2617 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2618 * A value of -1 means that the inequality is obviously redundant and may
2619 * not even appear in "tab".
2621 * We first mark the inequalities of "bset"
2622 * that are obviously redundant with respect to some inequality in "context".
2623 * Then we remove those constraints from "context" that have become
2624 * irrelevant for computing the gist of "bset".
2625 * Note that this removal of constraints cannot be replaced by
2626 * a factorization because factors in "bset" may still be connected
2627 * to each other through constraints in "context".
2629 * If there are any inequalities left, we construct a tableau for
2630 * the context and then add the inequalities of "bset".
2631 * Before adding these inequalities, we freeze all constraints such that
2632 * they won't be considered redundant in terms of the constraints of "bset".
2633 * Then we detect all redundant constraints (among the
2634 * constraints that weren't frozen), first by checking for redundancy in the
2635 * the tableau and then by checking if replacing a constraint by its negation
2636 * would lead to an empty set. This last step is fairly expensive
2637 * and could be optimized by more reuse of the tableau.
2638 * Finally, we update bset according to the results.
2640 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2641 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2643 int i, r;
2644 int *row = NULL;
2645 isl_ctx *ctx;
2646 isl_basic_set *combined = NULL;
2647 struct isl_tab *tab = NULL;
2648 unsigned n_eq, context_ineq;
2649 unsigned total;
2651 if (!bset || !ineq || !context)
2652 goto error;
2654 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2655 isl_basic_set_free(context);
2656 isl_mat_free(ineq);
2657 return bset;
2660 ctx = isl_basic_set_get_ctx(context);
2661 row = isl_calloc_array(ctx, int, bset->n_ineq);
2662 if (!row)
2663 goto error;
2665 if (mark_shifted_constraints(ineq, context, row) < 0)
2666 goto error;
2667 if (all_neg(row, bset->n_ineq))
2668 return update_ineq_free(bset, ineq, context, row, NULL);
2670 context = drop_irrelevant_constraints_marked(context, ineq, row);
2671 if (!context)
2672 goto error;
2673 if (isl_basic_set_plain_is_universe(context))
2674 return update_ineq_free(bset, ineq, context, row, NULL);
2676 n_eq = context->n_eq;
2677 context_ineq = context->n_ineq;
2678 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2679 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2680 tab = isl_tab_from_basic_set(combined, 0);
2681 for (i = 0; i < context_ineq; ++i)
2682 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2683 goto error;
2684 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2685 goto error;
2686 r = context_ineq;
2687 for (i = 0; i < bset->n_ineq; ++i) {
2688 if (row[i] < 0)
2689 continue;
2690 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2691 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2692 goto error;
2693 row[i] = r++;
2695 if (isl_tab_detect_implicit_equalities(tab) < 0)
2696 goto error;
2697 if (isl_tab_detect_redundant(tab) < 0)
2698 goto error;
2699 total = isl_basic_set_total_dim(bset);
2700 for (i = bset->n_ineq - 1; i >= 0; --i) {
2701 isl_basic_set *test;
2702 int is_empty;
2704 if (row[i] < 0)
2705 continue;
2706 r = row[i];
2707 if (tab->con[n_eq + r].is_redundant)
2708 continue;
2709 test = isl_basic_set_dup(combined);
2710 if (isl_inequality_negate(test, r) < 0)
2711 test = isl_basic_set_free(test);
2712 test = isl_basic_set_update_from_tab(test, tab);
2713 is_empty = isl_basic_set_is_empty(test);
2714 isl_basic_set_free(test);
2715 if (is_empty < 0)
2716 goto error;
2717 if (is_empty)
2718 tab->con[n_eq + r].is_redundant = 1;
2720 bset = update_ineq_free(bset, ineq, context, row, tab);
2721 if (bset) {
2722 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2723 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2726 isl_basic_set_free(combined);
2727 return bset;
2728 error:
2729 free(row);
2730 isl_mat_free(ineq);
2731 isl_tab_free(tab);
2732 isl_basic_set_free(combined);
2733 isl_basic_set_free(context);
2734 isl_basic_set_free(bset);
2735 return NULL;
2738 /* Extract the inequalities of "bset" as an isl_mat.
2740 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2742 unsigned total;
2743 isl_ctx *ctx;
2744 isl_mat *ineq;
2746 if (!bset)
2747 return NULL;
2749 ctx = isl_basic_set_get_ctx(bset);
2750 total = isl_basic_set_total_dim(bset);
2751 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2752 0, 1 + total);
2754 return ineq;
2757 /* Remove all information from "bset" that is redundant in the context
2758 * of "context", for the case where both "bset" and "context" are
2759 * full-dimensional.
2761 static __isl_give isl_basic_set *uset_gist_uncompressed(
2762 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2764 isl_mat *ineq;
2766 ineq = extract_ineq(bset);
2767 return uset_gist_full(bset, ineq, context);
2770 /* Remove all information from "bset" that is redundant in the context
2771 * of "context", for the case where the combined equalities of
2772 * "bset" and "context" allow for a compression that can be obtained
2773 * by preapplication of "T".
2775 * "bset" itself is not transformed by "T". Instead, the inequalities
2776 * are extracted from "bset" and those are transformed by "T".
2777 * uset_gist_full then determines which of the transformed inequalities
2778 * are redundant with respect to the transformed "context" and removes
2779 * the corresponding inequalities from "bset".
2781 * After preapplying "T" to the inequalities, any common factor is
2782 * removed from the coefficients. If this results in a tightening
2783 * of the constant term, then the same tightening is applied to
2784 * the corresponding untransformed inequality in "bset".
2785 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2787 * g f'(x) + r >= 0
2789 * with 0 <= r < g, then it is equivalent to
2791 * f'(x) >= 0
2793 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2794 * subspace compressed by T since the latter would be transformed to
2796 * g f'(x) >= 0
2798 static __isl_give isl_basic_set *uset_gist_compressed(
2799 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2800 __isl_take isl_mat *T)
2802 isl_ctx *ctx;
2803 isl_mat *ineq;
2804 int i, n_row, n_col;
2805 isl_int rem;
2807 ineq = extract_ineq(bset);
2808 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2809 context = isl_basic_set_preimage(context, T);
2811 if (!ineq || !context)
2812 goto error;
2813 if (isl_basic_set_plain_is_empty(context)) {
2814 isl_mat_free(ineq);
2815 isl_basic_set_free(context);
2816 return isl_basic_set_set_to_empty(bset);
2819 ctx = isl_mat_get_ctx(ineq);
2820 n_row = isl_mat_rows(ineq);
2821 n_col = isl_mat_cols(ineq);
2822 isl_int_init(rem);
2823 for (i = 0; i < n_row; ++i) {
2824 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2825 if (isl_int_is_zero(ctx->normalize_gcd))
2826 continue;
2827 if (isl_int_is_one(ctx->normalize_gcd))
2828 continue;
2829 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2830 ctx->normalize_gcd, n_col - 1);
2831 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2832 isl_int_fdiv_q(ineq->row[i][0],
2833 ineq->row[i][0], ctx->normalize_gcd);
2834 if (isl_int_is_zero(rem))
2835 continue;
2836 bset = isl_basic_set_cow(bset);
2837 if (!bset)
2838 break;
2839 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2841 isl_int_clear(rem);
2843 return uset_gist_full(bset, ineq, context);
2844 error:
2845 isl_mat_free(ineq);
2846 isl_basic_set_free(context);
2847 isl_basic_set_free(bset);
2848 return NULL;
2851 /* Project "bset" onto the variables that are involved in "template".
2853 static __isl_give isl_basic_set *project_onto_involved(
2854 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2856 int i, n;
2858 if (!bset || !template)
2859 return isl_basic_set_free(bset);
2861 n = isl_basic_set_dim(template, isl_dim_set);
2863 for (i = 0; i < n; ++i) {
2864 isl_bool involved;
2866 involved = isl_basic_set_involves_dims(template,
2867 isl_dim_set, i, 1);
2868 if (involved < 0)
2869 return isl_basic_set_free(bset);
2870 if (involved)
2871 continue;
2872 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2875 return bset;
2878 /* Remove all information from bset that is redundant in the context
2879 * of context. In particular, equalities that are linear combinations
2880 * of those in context are removed. Then the inequalities that are
2881 * redundant in the context of the equalities and inequalities of
2882 * context are removed.
2884 * First of all, we drop those constraints from "context"
2885 * that are irrelevant for computing the gist of "bset".
2886 * Alternatively, we could factorize the intersection of "context" and "bset".
2888 * We first compute the intersection of the integer affine hulls
2889 * of "bset" and "context",
2890 * compute the gist inside this intersection and then reduce
2891 * the constraints with respect to the equalities of the context
2892 * that only involve variables already involved in the input.
2894 * If two constraints are mutually redundant, then uset_gist_full
2895 * will remove the second of those constraints. We therefore first
2896 * sort the constraints so that constraints not involving existentially
2897 * quantified variables are given precedence over those that do.
2898 * We have to perform this sorting before the variable compression,
2899 * because that may effect the order of the variables.
2901 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2902 __isl_take isl_basic_set *context)
2904 isl_mat *eq;
2905 isl_mat *T;
2906 isl_basic_set *aff;
2907 isl_basic_set *aff_context;
2908 unsigned total;
2910 if (!bset || !context)
2911 goto error;
2913 context = drop_irrelevant_constraints(context, bset);
2915 bset = isl_basic_set_detect_equalities(bset);
2916 aff = isl_basic_set_copy(bset);
2917 aff = isl_basic_set_plain_affine_hull(aff);
2918 context = isl_basic_set_detect_equalities(context);
2919 aff_context = isl_basic_set_copy(context);
2920 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2921 aff = isl_basic_set_intersect(aff, aff_context);
2922 if (!aff)
2923 goto error;
2924 if (isl_basic_set_plain_is_empty(aff)) {
2925 isl_basic_set_free(bset);
2926 isl_basic_set_free(context);
2927 return aff;
2929 bset = isl_basic_set_sort_constraints(bset);
2930 if (aff->n_eq == 0) {
2931 isl_basic_set_free(aff);
2932 return uset_gist_uncompressed(bset, context);
2934 total = isl_basic_set_total_dim(bset);
2935 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2936 eq = isl_mat_cow(eq);
2937 T = isl_mat_variable_compression(eq, NULL);
2938 isl_basic_set_free(aff);
2939 if (T && T->n_col == 0) {
2940 isl_mat_free(T);
2941 isl_basic_set_free(context);
2942 return isl_basic_set_set_to_empty(bset);
2945 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2946 aff_context = project_onto_involved(aff_context, bset);
2948 bset = uset_gist_compressed(bset, context, T);
2949 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2951 if (bset) {
2952 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2953 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2956 return bset;
2957 error:
2958 isl_basic_set_free(bset);
2959 isl_basic_set_free(context);
2960 return NULL;
2963 /* Return the number of equality constraints in "bmap" that involve
2964 * local variables. This function assumes that Gaussian elimination
2965 * has been applied to the equality constraints.
2967 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2969 int i;
2970 int total, n_div;
2972 if (!bmap)
2973 return -1;
2975 if (bmap->n_eq == 0)
2976 return 0;
2978 total = isl_basic_map_dim(bmap, isl_dim_all);
2979 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2980 total -= n_div;
2982 for (i = 0; i < bmap->n_eq; ++i)
2983 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2984 n_div) == -1)
2985 return i;
2987 return bmap->n_eq;
2990 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2991 * The constraints are assumed not to involve any local variables.
2993 static __isl_give isl_basic_map *basic_map_from_equalities(
2994 __isl_take isl_space *space, __isl_take isl_mat *eq)
2996 int i, k;
2997 isl_basic_map *bmap = NULL;
2999 if (!space || !eq)
3000 goto error;
3002 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
3003 isl_die(isl_space_get_ctx(space), isl_error_internal,
3004 "unexpected number of columns", goto error);
3006 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
3007 0, eq->n_row, 0);
3008 for (i = 0; i < eq->n_row; ++i) {
3009 k = isl_basic_map_alloc_equality(bmap);
3010 if (k < 0)
3011 goto error;
3012 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
3015 isl_space_free(space);
3016 isl_mat_free(eq);
3017 return bmap;
3018 error:
3019 isl_space_free(space);
3020 isl_mat_free(eq);
3021 isl_basic_map_free(bmap);
3022 return NULL;
3025 /* Construct and return a variable compression based on the equality
3026 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3027 * "n1" is the number of (initial) equality constraints in "bmap1"
3028 * that do involve local variables.
3029 * "n2" is the number of (initial) equality constraints in "bmap2"
3030 * that do involve local variables.
3031 * "total" is the total number of other variables.
3032 * This function assumes that Gaussian elimination
3033 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3034 * such that the equality constraints not involving local variables
3035 * are those that start at "n1" or "n2".
3037 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3038 * then simply compute the compression based on the equality constraints
3039 * in the other basic map.
3040 * Otherwise, combine the equality constraints from both into a new
3041 * basic map such that Gaussian elimination can be applied to this combination
3042 * and then construct a variable compression from the resulting
3043 * equality constraints.
3045 static __isl_give isl_mat *combined_variable_compression(
3046 __isl_keep isl_basic_map *bmap1, int n1,
3047 __isl_keep isl_basic_map *bmap2, int n2, int total)
3049 isl_ctx *ctx;
3050 isl_mat *E1, *E2, *V;
3051 isl_basic_map *bmap;
3053 ctx = isl_basic_map_get_ctx(bmap1);
3054 if (bmap1->n_eq == n1) {
3055 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3056 n2, bmap2->n_eq - n2, 0, 1 + total);
3057 return isl_mat_variable_compression(E2, NULL);
3059 if (bmap2->n_eq == n2) {
3060 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3061 n1, bmap1->n_eq - n1, 0, 1 + total);
3062 return isl_mat_variable_compression(E1, NULL);
3064 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3065 n1, bmap1->n_eq - n1, 0, 1 + total);
3066 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3067 n2, bmap2->n_eq - n2, 0, 1 + total);
3068 E1 = isl_mat_concat(E1, E2);
3069 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3070 bmap = isl_basic_map_gauss(bmap, NULL);
3071 if (!bmap)
3072 return NULL;
3073 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3074 V = isl_mat_variable_compression(E1, NULL);
3075 isl_basic_map_free(bmap);
3077 return V;
3080 /* Extract the stride constraints from "bmap", compressed
3081 * with respect to both the stride constraints in "context" and
3082 * the remaining equality constraints in both "bmap" and "context".
3083 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3084 * "context_n_eq" is the number of (initial) stride constraints in "context".
3086 * Let x be all variables in "bmap" (and "context") other than the local
3087 * variables. First compute a variable compression
3089 * x = V x'
3091 * based on the non-stride equality constraints in "bmap" and "context".
3092 * Consider the stride constraints of "context",
3094 * A(x) + B(y) = 0
3096 * with y the local variables and plug in the variable compression,
3097 * resulting in
3099 * A(V x') + B(y) = 0
3101 * Use these constraints to compute a parameter compression on x'
3103 * x' = T x''
3105 * Now consider the stride constraints of "bmap"
3107 * C(x) + D(y) = 0
3109 * and plug in x = V*T x''.
3110 * That is, return A = [C*V*T D].
3112 static __isl_give isl_mat *extract_compressed_stride_constraints(
3113 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3114 __isl_keep isl_basic_map *context, int context_n_eq)
3116 int total, n_div;
3117 isl_ctx *ctx;
3118 isl_mat *A, *B, *T, *V;
3120 total = isl_basic_map_dim(context, isl_dim_all);
3121 n_div = isl_basic_map_dim(context, isl_dim_div);
3122 total -= n_div;
3124 ctx = isl_basic_map_get_ctx(bmap);
3126 V = combined_variable_compression(bmap, bmap_n_eq,
3127 context, context_n_eq, total);
3129 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3130 B = isl_mat_sub_alloc6(ctx, context->eq,
3131 0, context_n_eq, 1 + total, n_div);
3132 A = isl_mat_product(A, isl_mat_copy(V));
3133 T = isl_mat_parameter_compression_ext(A, B);
3134 T = isl_mat_product(V, T);
3136 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3137 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3139 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3140 0, bmap_n_eq, 0, 1 + total + n_div);
3141 A = isl_mat_product(A, T);
3143 return A;
3146 /* Remove the prime factors from *g that have an exponent that
3147 * is strictly smaller than the exponent in "c".
3148 * All exponents in *g are known to be smaller than or equal
3149 * to those in "c".
3151 * That is, if *g is equal to
3153 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3155 * and "c" is equal to
3157 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3159 * then update *g to
3161 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3162 * p_n^{e_n * (e_n = f_n)}
3164 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3165 * neither does the gcd of *g and c / *g.
3166 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3167 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3168 * Dividing *g by this gcd therefore strictly reduces the exponent
3169 * of the prime factors that need to be removed, while leaving the
3170 * other prime factors untouched.
3171 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3172 * removes all undesired factors, without removing any others.
3174 static void remove_incomplete_powers(isl_int *g, isl_int c)
3176 isl_int t;
3178 isl_int_init(t);
3179 for (;;) {
3180 isl_int_divexact(t, c, *g);
3181 isl_int_gcd(t, t, *g);
3182 if (isl_int_is_one(t))
3183 break;
3184 isl_int_divexact(*g, *g, t);
3186 isl_int_clear(t);
3189 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3190 * of the same stride constraints in a compressed space that exploits
3191 * all equalities in the context and the other equalities in "bmap".
3193 * If the stride constraints of "bmap" are of the form
3195 * C(x) + D(y) = 0
3197 * then A is of the form
3199 * B(x') + D(y) = 0
3201 * If any of these constraints involves only a single local variable y,
3202 * then the constraint appears as
3204 * f(x) + m y_i = 0
3206 * in "bmap" and as
3208 * h(x') + m y_i = 0
3210 * in "A".
3212 * Let g be the gcd of m and the coefficients of h.
3213 * Then, in particular, g is a divisor of the coefficients of h and
3215 * f(x) = h(x')
3217 * is known to be a multiple of g.
3218 * If some prime factor in m appears with the same exponent in g,
3219 * then it can be removed from m because f(x) is already known
3220 * to be a multiple of g and therefore in particular of this power
3221 * of the prime factors.
3222 * Prime factors that appear with a smaller exponent in g cannot
3223 * be removed from m.
3224 * Let g' be the divisor of g containing all prime factors that
3225 * appear with the same exponent in m and g, then
3227 * f(x) + m y_i = 0
3229 * can be replaced by
3231 * f(x) + m/g' y_i' = 0
3233 * Note that (if g' != 1) this changes the explicit representation
3234 * of y_i to that of y_i', so the integer division at position i
3235 * is marked unknown and later recomputed by a call to
3236 * isl_basic_map_gauss.
3238 static __isl_give isl_basic_map *reduce_stride_constraints(
3239 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3241 int i;
3242 int total, n_div;
3243 int any = 0;
3244 isl_int gcd;
3246 if (!bmap || !A)
3247 return isl_basic_map_free(bmap);
3249 total = isl_basic_map_dim(bmap, isl_dim_all);
3250 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3251 total -= n_div;
3253 isl_int_init(gcd);
3254 for (i = 0; i < n; ++i) {
3255 int div;
3257 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3258 if (div < 0)
3259 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3260 "equality constraints modified unexpectedly",
3261 goto error);
3262 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3263 n_div - div - 1) != -1)
3264 continue;
3265 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3266 goto error;
3267 if (isl_int_is_one(gcd))
3268 continue;
3269 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3270 if (isl_int_is_one(gcd))
3271 continue;
3272 isl_int_divexact(bmap->eq[i][1 + total + div],
3273 bmap->eq[i][1 + total + div], gcd);
3274 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3275 if (!bmap)
3276 goto error;
3277 any = 1;
3279 isl_int_clear(gcd);
3281 if (any)
3282 bmap = isl_basic_map_gauss(bmap, NULL);
3284 return bmap;
3285 error:
3286 isl_int_clear(gcd);
3287 isl_basic_map_free(bmap);
3288 return NULL;
3291 /* Simplify the stride constraints in "bmap" based on
3292 * the remaining equality constraints in "bmap" and all equality
3293 * constraints in "context".
3294 * Only do this if both "bmap" and "context" have stride constraints.
3296 * First extract a copy of the stride constraints in "bmap" in a compressed
3297 * space exploiting all the other equality constraints and then
3298 * use this compressed copy to simplify the original stride constraints.
3300 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3301 __isl_keep isl_basic_map *context)
3303 int bmap_n_eq, context_n_eq;
3304 isl_mat *A;
3306 if (!bmap || !context)
3307 return isl_basic_map_free(bmap);
3309 bmap_n_eq = n_div_eq(bmap);
3310 context_n_eq = n_div_eq(context);
3312 if (bmap_n_eq < 0 || context_n_eq < 0)
3313 return isl_basic_map_free(bmap);
3314 if (bmap_n_eq == 0 || context_n_eq == 0)
3315 return bmap;
3317 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3318 context, context_n_eq);
3319 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3321 isl_mat_free(A);
3323 return bmap;
3326 /* Return a basic map that has the same intersection with "context" as "bmap"
3327 * and that is as "simple" as possible.
3329 * The core computation is performed on the pure constraints.
3330 * When we add back the meaning of the integer divisions, we need
3331 * to (re)introduce the div constraints. If we happen to have
3332 * discovered that some of these integer divisions are equal to
3333 * some affine combination of other variables, then these div
3334 * constraints may end up getting simplified in terms of the equalities,
3335 * resulting in extra inequalities on the other variables that
3336 * may have been removed already or that may not even have been
3337 * part of the input. We try and remove those constraints of
3338 * this form that are most obviously redundant with respect to
3339 * the context. We also remove those div constraints that are
3340 * redundant with respect to the other constraints in the result.
3342 * The stride constraints among the equality constraints in "bmap" are
3343 * also simplified with respecting to the other equality constraints
3344 * in "bmap" and with respect to all equality constraints in "context".
3346 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3347 struct isl_basic_map *context)
3349 isl_basic_set *bset, *eq;
3350 isl_basic_map *eq_bmap;
3351 unsigned total, n_div, extra, n_eq, n_ineq;
3353 if (!bmap || !context)
3354 goto error;
3356 if (isl_basic_map_plain_is_universe(bmap)) {
3357 isl_basic_map_free(context);
3358 return bmap;
3360 if (isl_basic_map_plain_is_empty(context)) {
3361 isl_space *space = isl_basic_map_get_space(bmap);
3362 isl_basic_map_free(bmap);
3363 isl_basic_map_free(context);
3364 return isl_basic_map_universe(space);
3366 if (isl_basic_map_plain_is_empty(bmap)) {
3367 isl_basic_map_free(context);
3368 return bmap;
3371 bmap = isl_basic_map_remove_redundancies(bmap);
3372 context = isl_basic_map_remove_redundancies(context);
3373 if (!context)
3374 goto error;
3376 context = isl_basic_map_align_divs(context, bmap);
3377 n_div = isl_basic_map_dim(context, isl_dim_div);
3378 total = isl_basic_map_dim(bmap, isl_dim_all);
3379 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3381 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3382 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3383 bset = uset_gist(bset,
3384 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3385 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3387 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3388 isl_basic_set_plain_is_empty(bset)) {
3389 isl_basic_map_free(context);
3390 return isl_basic_map_overlying_set(bset, bmap);
3393 n_eq = bset->n_eq;
3394 n_ineq = bset->n_ineq;
3395 eq = isl_basic_set_copy(bset);
3396 eq = isl_basic_set_cow(eq);
3397 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3398 eq = isl_basic_set_free(eq);
3399 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3400 bset = isl_basic_set_free(bset);
3402 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3403 eq_bmap = gist_strides(eq_bmap, context);
3404 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3405 bmap = isl_basic_map_overlying_set(bset, bmap);
3406 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3407 bmap = isl_basic_map_remove_redundancies(bmap);
3409 return bmap;
3410 error:
3411 isl_basic_map_free(bmap);
3412 isl_basic_map_free(context);
3413 return NULL;
3417 * Assumes context has no implicit divs.
3419 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3420 __isl_take isl_basic_map *context)
3422 int i;
3424 if (!map || !context)
3425 goto error;
3427 if (isl_basic_map_plain_is_empty(context)) {
3428 isl_space *space = isl_map_get_space(map);
3429 isl_map_free(map);
3430 isl_basic_map_free(context);
3431 return isl_map_universe(space);
3434 context = isl_basic_map_remove_redundancies(context);
3435 map = isl_map_cow(map);
3436 if (!map || !context)
3437 goto error;
3438 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3439 map = isl_map_compute_divs(map);
3440 if (!map)
3441 goto error;
3442 for (i = map->n - 1; i >= 0; --i) {
3443 map->p[i] = isl_basic_map_gist(map->p[i],
3444 isl_basic_map_copy(context));
3445 if (!map->p[i])
3446 goto error;
3447 if (isl_basic_map_plain_is_empty(map->p[i])) {
3448 isl_basic_map_free(map->p[i]);
3449 if (i != map->n - 1)
3450 map->p[i] = map->p[map->n - 1];
3451 map->n--;
3454 isl_basic_map_free(context);
3455 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3456 return map;
3457 error:
3458 isl_map_free(map);
3459 isl_basic_map_free(context);
3460 return NULL;
3463 /* Drop all inequalities from "bmap" that also appear in "context".
3464 * "context" is assumed to have only known local variables and
3465 * the initial local variables of "bmap" are assumed to be the same
3466 * as those of "context".
3467 * The constraints of both "bmap" and "context" are assumed
3468 * to have been sorted using isl_basic_map_sort_constraints.
3470 * Run through the inequality constraints of "bmap" and "context"
3471 * in sorted order.
3472 * If a constraint of "bmap" involves variables not in "context",
3473 * then it cannot appear in "context".
3474 * If a matching constraint is found, it is removed from "bmap".
3476 static __isl_give isl_basic_map *drop_inequalities(
3477 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3479 int i1, i2;
3480 unsigned total, extra;
3482 if (!bmap || !context)
3483 return isl_basic_map_free(bmap);
3485 total = isl_basic_map_total_dim(context);
3486 extra = isl_basic_map_total_dim(bmap) - total;
3488 i1 = bmap->n_ineq - 1;
3489 i2 = context->n_ineq - 1;
3490 while (bmap && i1 >= 0 && i2 >= 0) {
3491 int cmp;
3493 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3494 extra) != -1) {
3495 --i1;
3496 continue;
3498 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3499 context->ineq[i2]);
3500 if (cmp < 0) {
3501 --i2;
3502 continue;
3504 if (cmp > 0) {
3505 --i1;
3506 continue;
3508 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3509 bmap = isl_basic_map_cow(bmap);
3510 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3511 bmap = isl_basic_map_free(bmap);
3513 --i1;
3514 --i2;
3517 return bmap;
3520 /* Drop all equalities from "bmap" that also appear in "context".
3521 * "context" is assumed to have only known local variables and
3522 * the initial local variables of "bmap" are assumed to be the same
3523 * as those of "context".
3525 * Run through the equality constraints of "bmap" and "context"
3526 * in sorted order.
3527 * If a constraint of "bmap" involves variables not in "context",
3528 * then it cannot appear in "context".
3529 * If a matching constraint is found, it is removed from "bmap".
3531 static __isl_give isl_basic_map *drop_equalities(
3532 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3534 int i1, i2;
3535 unsigned total, extra;
3537 if (!bmap || !context)
3538 return isl_basic_map_free(bmap);
3540 total = isl_basic_map_total_dim(context);
3541 extra = isl_basic_map_total_dim(bmap) - total;
3543 i1 = bmap->n_eq - 1;
3544 i2 = context->n_eq - 1;
3546 while (bmap && i1 >= 0 && i2 >= 0) {
3547 int last1, last2;
3549 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3550 extra) != -1)
3551 break;
3552 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3553 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3554 if (last1 > last2) {
3555 --i2;
3556 continue;
3558 if (last1 < last2) {
3559 --i1;
3560 continue;
3562 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3563 bmap = isl_basic_map_cow(bmap);
3564 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3565 bmap = isl_basic_map_free(bmap);
3567 --i1;
3568 --i2;
3571 return bmap;
3574 /* Remove the constraints in "context" from "bmap".
3575 * "context" is assumed to have explicit representations
3576 * for all local variables.
3578 * First align the divs of "bmap" to those of "context" and
3579 * sort the constraints. Then drop all constraints from "bmap"
3580 * that appear in "context".
3582 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3583 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3585 isl_bool done, known;
3587 done = isl_basic_map_plain_is_universe(context);
3588 if (done == isl_bool_false)
3589 done = isl_basic_map_plain_is_universe(bmap);
3590 if (done == isl_bool_false)
3591 done = isl_basic_map_plain_is_empty(context);
3592 if (done == isl_bool_false)
3593 done = isl_basic_map_plain_is_empty(bmap);
3594 if (done < 0)
3595 goto error;
3596 if (done) {
3597 isl_basic_map_free(context);
3598 return bmap;
3600 known = isl_basic_map_divs_known(context);
3601 if (known < 0)
3602 goto error;
3603 if (!known)
3604 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3605 "context has unknown divs", goto error);
3607 bmap = isl_basic_map_align_divs(bmap, context);
3608 bmap = isl_basic_map_gauss(bmap, NULL);
3609 bmap = isl_basic_map_sort_constraints(bmap);
3610 context = isl_basic_map_sort_constraints(context);
3612 bmap = drop_inequalities(bmap, context);
3613 bmap = drop_equalities(bmap, context);
3615 isl_basic_map_free(context);
3616 bmap = isl_basic_map_finalize(bmap);
3617 return bmap;
3618 error:
3619 isl_basic_map_free(bmap);
3620 isl_basic_map_free(context);
3621 return NULL;
3624 /* Replace "map" by the disjunct at position "pos" and free "context".
3626 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3627 int pos, __isl_take isl_basic_map *context)
3629 isl_basic_map *bmap;
3631 bmap = isl_basic_map_copy(map->p[pos]);
3632 isl_map_free(map);
3633 isl_basic_map_free(context);
3634 return isl_map_from_basic_map(bmap);
3637 /* Remove the constraints in "context" from "map".
3638 * If any of the disjuncts in the result turns out to be the universe,
3639 * then return this universe.
3640 * "context" is assumed to have explicit representations
3641 * for all local variables.
3643 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3644 __isl_take isl_basic_map *context)
3646 int i;
3647 isl_bool univ, known;
3649 univ = isl_basic_map_plain_is_universe(context);
3650 if (univ < 0)
3651 goto error;
3652 if (univ) {
3653 isl_basic_map_free(context);
3654 return map;
3656 known = isl_basic_map_divs_known(context);
3657 if (known < 0)
3658 goto error;
3659 if (!known)
3660 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3661 "context has unknown divs", goto error);
3663 map = isl_map_cow(map);
3664 if (!map)
3665 goto error;
3666 for (i = 0; i < map->n; ++i) {
3667 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3668 isl_basic_map_copy(context));
3669 univ = isl_basic_map_plain_is_universe(map->p[i]);
3670 if (univ < 0)
3671 goto error;
3672 if (univ && map->n > 1)
3673 return replace_by_disjunct(map, i, context);
3676 isl_basic_map_free(context);
3677 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3678 if (map->n > 1)
3679 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3680 return map;
3681 error:
3682 isl_map_free(map);
3683 isl_basic_map_free(context);
3684 return NULL;
3687 /* Replace "map" by a universe map in the same space and free "drop".
3689 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3690 __isl_take isl_map *drop)
3692 isl_map *res;
3694 res = isl_map_universe(isl_map_get_space(map));
3695 isl_map_free(map);
3696 isl_map_free(drop);
3697 return res;
3700 /* Return a map that has the same intersection with "context" as "map"
3701 * and that is as "simple" as possible.
3703 * If "map" is already the universe, then we cannot make it any simpler.
3704 * Similarly, if "context" is the universe, then we cannot exploit it
3705 * to simplify "map"
3706 * If "map" and "context" are identical to each other, then we can
3707 * return the corresponding universe.
3709 * If either "map" or "context" consists of multiple disjuncts,
3710 * then check if "context" happens to be a subset of "map",
3711 * in which case all constraints can be removed.
3712 * In case of multiple disjuncts, the standard procedure
3713 * may not be able to detect that all constraints can be removed.
3715 * If none of these cases apply, we have to work a bit harder.
3716 * During this computation, we make use of a single disjunct context,
3717 * so if the original context consists of more than one disjunct
3718 * then we need to approximate the context by a single disjunct set.
3719 * Simply taking the simple hull may drop constraints that are
3720 * only implicitly available in each disjunct. We therefore also
3721 * look for constraints among those defining "map" that are valid
3722 * for the context. These can then be used to simplify away
3723 * the corresponding constraints in "map".
3725 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3726 __isl_take isl_map *context)
3728 int equal;
3729 int is_universe;
3730 int single_disjunct_map, single_disjunct_context;
3731 isl_bool subset;
3732 isl_basic_map *hull;
3734 is_universe = isl_map_plain_is_universe(map);
3735 if (is_universe >= 0 && !is_universe)
3736 is_universe = isl_map_plain_is_universe(context);
3737 if (is_universe < 0)
3738 goto error;
3739 if (is_universe) {
3740 isl_map_free(context);
3741 return map;
3744 equal = isl_map_plain_is_equal(map, context);
3745 if (equal < 0)
3746 goto error;
3747 if (equal)
3748 return replace_by_universe(map, context);
3750 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3751 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3752 if (!single_disjunct_map || !single_disjunct_context) {
3753 subset = isl_map_is_subset(context, map);
3754 if (subset < 0)
3755 goto error;
3756 if (subset)
3757 return replace_by_universe(map, context);
3760 context = isl_map_compute_divs(context);
3761 if (!context)
3762 goto error;
3763 if (single_disjunct_context) {
3764 hull = isl_map_simple_hull(context);
3765 } else {
3766 isl_ctx *ctx;
3767 isl_map_list *list;
3769 ctx = isl_map_get_ctx(map);
3770 list = isl_map_list_alloc(ctx, 2);
3771 list = isl_map_list_add(list, isl_map_copy(context));
3772 list = isl_map_list_add(list, isl_map_copy(map));
3773 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3774 list);
3776 return isl_map_gist_basic_map(map, hull);
3777 error:
3778 isl_map_free(map);
3779 isl_map_free(context);
3780 return NULL;
3783 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3784 __isl_take isl_map *context)
3786 return isl_map_align_params_map_map_and(map, context, &map_gist);
3789 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3790 struct isl_basic_set *context)
3792 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3793 bset_to_bmap(context)));
3796 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3797 __isl_take isl_basic_set *context)
3799 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3800 bset_to_bmap(context)));
3803 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3804 __isl_take isl_basic_set *context)
3806 isl_space *space = isl_set_get_space(set);
3807 isl_basic_set *dom_context = isl_basic_set_universe(space);
3808 dom_context = isl_basic_set_intersect_params(dom_context, context);
3809 return isl_set_gist_basic_set(set, dom_context);
3812 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3813 __isl_take isl_set *context)
3815 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3818 /* Compute the gist of "bmap" with respect to the constraints "context"
3819 * on the domain.
3821 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3822 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3824 isl_space *space = isl_basic_map_get_space(bmap);
3825 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3827 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3828 return isl_basic_map_gist(bmap, bmap_context);
3831 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3832 __isl_take isl_set *context)
3834 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3835 map_context = isl_map_intersect_domain(map_context, context);
3836 return isl_map_gist(map, map_context);
3839 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3840 __isl_take isl_set *context)
3842 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3843 map_context = isl_map_intersect_range(map_context, context);
3844 return isl_map_gist(map, map_context);
3847 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3848 __isl_take isl_set *context)
3850 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3851 map_context = isl_map_intersect_params(map_context, context);
3852 return isl_map_gist(map, map_context);
3855 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3856 __isl_take isl_set *context)
3858 return isl_map_gist_params(set, context);
3861 /* Quick check to see if two basic maps are disjoint.
3862 * In particular, we reduce the equalities and inequalities of
3863 * one basic map in the context of the equalities of the other
3864 * basic map and check if we get a contradiction.
3866 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3867 __isl_keep isl_basic_map *bmap2)
3869 struct isl_vec *v = NULL;
3870 int *elim = NULL;
3871 unsigned total;
3872 int i;
3874 if (!bmap1 || !bmap2)
3875 return isl_bool_error;
3876 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3877 return isl_bool_error);
3878 if (bmap1->n_div || bmap2->n_div)
3879 return isl_bool_false;
3880 if (!bmap1->n_eq && !bmap2->n_eq)
3881 return isl_bool_false;
3883 total = isl_space_dim(bmap1->dim, isl_dim_all);
3884 if (total == 0)
3885 return isl_bool_false;
3886 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3887 if (!v)
3888 goto error;
3889 elim = isl_alloc_array(bmap1->ctx, int, total);
3890 if (!elim)
3891 goto error;
3892 compute_elimination_index(bmap1, elim);
3893 for (i = 0; i < bmap2->n_eq; ++i) {
3894 int reduced;
3895 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3896 bmap1, elim);
3897 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3898 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3899 goto disjoint;
3901 for (i = 0; i < bmap2->n_ineq; ++i) {
3902 int reduced;
3903 reduced = reduced_using_equalities(v->block.data,
3904 bmap2->ineq[i], bmap1, elim);
3905 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3906 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3907 goto disjoint;
3909 compute_elimination_index(bmap2, elim);
3910 for (i = 0; i < bmap1->n_ineq; ++i) {
3911 int reduced;
3912 reduced = reduced_using_equalities(v->block.data,
3913 bmap1->ineq[i], bmap2, elim);
3914 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3915 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3916 goto disjoint;
3918 isl_vec_free(v);
3919 free(elim);
3920 return isl_bool_false;
3921 disjoint:
3922 isl_vec_free(v);
3923 free(elim);
3924 return isl_bool_true;
3925 error:
3926 isl_vec_free(v);
3927 free(elim);
3928 return isl_bool_error;
3931 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3932 __isl_keep isl_basic_set *bset2)
3934 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3935 bset_to_bmap(bset2));
3938 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3940 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3941 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3942 __isl_keep isl_basic_map *bmap2))
3944 int i, j;
3946 if (!map1 || !map2)
3947 return isl_bool_error;
3949 for (i = 0; i < map1->n; ++i) {
3950 for (j = 0; j < map2->n; ++j) {
3951 isl_bool d = test(map1->p[i], map2->p[j]);
3952 if (d != isl_bool_true)
3953 return d;
3957 return isl_bool_true;
3960 /* Are "map1" and "map2" obviously disjoint, based on information
3961 * that can be derived without looking at the individual basic maps?
3963 * In particular, if one of them is empty or if they live in different spaces
3964 * (ignoring parameters), then they are clearly disjoint.
3966 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3967 __isl_keep isl_map *map2)
3969 isl_bool disjoint;
3970 isl_bool match;
3972 if (!map1 || !map2)
3973 return isl_bool_error;
3975 disjoint = isl_map_plain_is_empty(map1);
3976 if (disjoint < 0 || disjoint)
3977 return disjoint;
3979 disjoint = isl_map_plain_is_empty(map2);
3980 if (disjoint < 0 || disjoint)
3981 return disjoint;
3983 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3984 map2->dim, isl_dim_in);
3985 if (match < 0 || !match)
3986 return match < 0 ? isl_bool_error : isl_bool_true;
3988 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3989 map2->dim, isl_dim_out);
3990 if (match < 0 || !match)
3991 return match < 0 ? isl_bool_error : isl_bool_true;
3993 return isl_bool_false;
3996 /* Are "map1" and "map2" obviously disjoint?
3998 * If one of them is empty or if they live in different spaces (ignoring
3999 * parameters), then they are clearly disjoint.
4000 * This is checked by isl_map_plain_is_disjoint_global.
4002 * If they have different parameters, then we skip any further tests.
4004 * If they are obviously equal, but not obviously empty, then we will
4005 * not be able to detect if they are disjoint.
4007 * Otherwise we check if each basic map in "map1" is obviously disjoint
4008 * from each basic map in "map2".
4010 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
4011 __isl_keep isl_map *map2)
4013 isl_bool disjoint;
4014 isl_bool intersect;
4015 isl_bool match;
4017 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4018 if (disjoint < 0 || disjoint)
4019 return disjoint;
4021 match = isl_space_match(map1->dim, isl_dim_param,
4022 map2->dim, isl_dim_param);
4023 if (match < 0 || !match)
4024 return match < 0 ? isl_bool_error : isl_bool_false;
4026 intersect = isl_map_plain_is_equal(map1, map2);
4027 if (intersect < 0 || intersect)
4028 return intersect < 0 ? isl_bool_error : isl_bool_false;
4030 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4033 /* Are "map1" and "map2" disjoint?
4035 * They are disjoint if they are "obviously disjoint" or if one of them
4036 * is empty. Otherwise, they are not disjoint if one of them is universal.
4037 * If the two inputs are (obviously) equal and not empty, then they are
4038 * not disjoint.
4039 * If none of these cases apply, then check if all pairs of basic maps
4040 * are disjoint.
4042 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4044 isl_bool disjoint;
4045 isl_bool intersect;
4047 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4048 if (disjoint < 0 || disjoint)
4049 return disjoint;
4051 disjoint = isl_map_is_empty(map1);
4052 if (disjoint < 0 || disjoint)
4053 return disjoint;
4055 disjoint = isl_map_is_empty(map2);
4056 if (disjoint < 0 || disjoint)
4057 return disjoint;
4059 intersect = isl_map_plain_is_universe(map1);
4060 if (intersect < 0 || intersect)
4061 return intersect < 0 ? isl_bool_error : isl_bool_false;
4063 intersect = isl_map_plain_is_universe(map2);
4064 if (intersect < 0 || intersect)
4065 return intersect < 0 ? isl_bool_error : isl_bool_false;
4067 intersect = isl_map_plain_is_equal(map1, map2);
4068 if (intersect < 0 || intersect)
4069 return isl_bool_not(intersect);
4071 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4074 /* Are "bmap1" and "bmap2" disjoint?
4076 * They are disjoint if they are "obviously disjoint" or if one of them
4077 * is empty. Otherwise, they are not disjoint if one of them is universal.
4078 * If none of these cases apply, we compute the intersection and see if
4079 * the result is empty.
4081 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4082 __isl_keep isl_basic_map *bmap2)
4084 isl_bool disjoint;
4085 isl_bool intersect;
4086 isl_basic_map *test;
4088 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4089 if (disjoint < 0 || disjoint)
4090 return disjoint;
4092 disjoint = isl_basic_map_is_empty(bmap1);
4093 if (disjoint < 0 || disjoint)
4094 return disjoint;
4096 disjoint = isl_basic_map_is_empty(bmap2);
4097 if (disjoint < 0 || disjoint)
4098 return disjoint;
4100 intersect = isl_basic_map_plain_is_universe(bmap1);
4101 if (intersect < 0 || intersect)
4102 return intersect < 0 ? isl_bool_error : isl_bool_false;
4104 intersect = isl_basic_map_plain_is_universe(bmap2);
4105 if (intersect < 0 || intersect)
4106 return intersect < 0 ? isl_bool_error : isl_bool_false;
4108 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4109 isl_basic_map_copy(bmap2));
4110 disjoint = isl_basic_map_is_empty(test);
4111 isl_basic_map_free(test);
4113 return disjoint;
4116 /* Are "bset1" and "bset2" disjoint?
4118 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4119 __isl_keep isl_basic_set *bset2)
4121 return isl_basic_map_is_disjoint(bset1, bset2);
4124 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4125 __isl_keep isl_set *set2)
4127 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4130 /* Are "set1" and "set2" disjoint?
4132 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4134 return isl_map_is_disjoint(set1, set2);
4137 /* Is "v" equal to 0, 1 or -1?
4139 static int is_zero_or_one(isl_int v)
4141 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4144 /* Check if we can combine a given div with lower bound l and upper
4145 * bound u with some other div and if so return that other div.
4146 * Otherwise return -1.
4148 * We first check that
4149 * - the bounds are opposites of each other (except for the constant
4150 * term)
4151 * - the bounds do not reference any other div
4152 * - no div is defined in terms of this div
4154 * Let m be the size of the range allowed on the div by the bounds.
4155 * That is, the bounds are of the form
4157 * e <= a <= e + m - 1
4159 * with e some expression in the other variables.
4160 * We look for another div b such that no third div is defined in terms
4161 * of this second div b and such that in any constraint that contains
4162 * a (except for the given lower and upper bound), also contains b
4163 * with a coefficient that is m times that of b.
4164 * That is, all constraints (execpt for the lower and upper bound)
4165 * are of the form
4167 * e + f (a + m b) >= 0
4169 * Furthermore, in the constraints that only contain b, the coefficient
4170 * of b should be equal to 1 or -1.
4171 * If so, we return b so that "a + m b" can be replaced by
4172 * a single div "c = a + m b".
4174 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4175 unsigned div, unsigned l, unsigned u)
4177 int i, j;
4178 unsigned dim;
4179 int coalesce = -1;
4181 if (bmap->n_div <= 1)
4182 return -1;
4183 dim = isl_space_dim(bmap->dim, isl_dim_all);
4184 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4185 return -1;
4186 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4187 bmap->n_div - div - 1) != -1)
4188 return -1;
4189 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4190 dim + bmap->n_div))
4191 return -1;
4193 for (i = 0; i < bmap->n_div; ++i) {
4194 if (isl_int_is_zero(bmap->div[i][0]))
4195 continue;
4196 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4197 return -1;
4200 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4201 if (isl_int_is_neg(bmap->ineq[l][0])) {
4202 isl_int_sub(bmap->ineq[l][0],
4203 bmap->ineq[l][0], bmap->ineq[u][0]);
4204 bmap = isl_basic_map_copy(bmap);
4205 bmap = isl_basic_map_set_to_empty(bmap);
4206 isl_basic_map_free(bmap);
4207 return -1;
4209 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4210 for (i = 0; i < bmap->n_div; ++i) {
4211 if (i == div)
4212 continue;
4213 if (!pairs[i])
4214 continue;
4215 for (j = 0; j < bmap->n_div; ++j) {
4216 if (isl_int_is_zero(bmap->div[j][0]))
4217 continue;
4218 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4219 break;
4221 if (j < bmap->n_div)
4222 continue;
4223 for (j = 0; j < bmap->n_ineq; ++j) {
4224 int valid;
4225 if (j == l || j == u)
4226 continue;
4227 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4228 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4229 continue;
4230 break;
4232 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4233 break;
4234 isl_int_mul(bmap->ineq[j][1 + dim + div],
4235 bmap->ineq[j][1 + dim + div],
4236 bmap->ineq[l][0]);
4237 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4238 bmap->ineq[j][1 + dim + i]);
4239 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4240 bmap->ineq[j][1 + dim + div],
4241 bmap->ineq[l][0]);
4242 if (!valid)
4243 break;
4245 if (j < bmap->n_ineq)
4246 continue;
4247 coalesce = i;
4248 break;
4250 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4251 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4252 return coalesce;
4255 /* Internal data structure used during the construction and/or evaluation of
4256 * an inequality that ensures that a pair of bounds always allows
4257 * for an integer value.
4259 * "tab" is the tableau in which the inequality is evaluated. It may
4260 * be NULL until it is actually needed.
4261 * "v" contains the inequality coefficients.
4262 * "g", "fl" and "fu" are temporary scalars used during the construction and
4263 * evaluation.
4265 struct test_ineq_data {
4266 struct isl_tab *tab;
4267 isl_vec *v;
4268 isl_int g;
4269 isl_int fl;
4270 isl_int fu;
4273 /* Free all the memory allocated by the fields of "data".
4275 static void test_ineq_data_clear(struct test_ineq_data *data)
4277 isl_tab_free(data->tab);
4278 isl_vec_free(data->v);
4279 isl_int_clear(data->g);
4280 isl_int_clear(data->fl);
4281 isl_int_clear(data->fu);
4284 /* Is the inequality stored in data->v satisfied by "bmap"?
4285 * That is, does it only attain non-negative values?
4286 * data->tab is a tableau corresponding to "bmap".
4288 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4289 struct test_ineq_data *data)
4291 isl_ctx *ctx;
4292 enum isl_lp_result res;
4294 ctx = isl_basic_map_get_ctx(bmap);
4295 if (!data->tab)
4296 data->tab = isl_tab_from_basic_map(bmap, 0);
4297 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4298 if (res == isl_lp_error)
4299 return isl_bool_error;
4300 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4303 /* Given a lower and an upper bound on div i, do they always allow
4304 * for an integer value of the given div?
4305 * Determine this property by constructing an inequality
4306 * such that the property is guaranteed when the inequality is nonnegative.
4307 * The lower bound is inequality l, while the upper bound is inequality u.
4308 * The constructed inequality is stored in data->v.
4310 * Let the upper bound be
4312 * -n_u a + e_u >= 0
4314 * and the lower bound
4316 * n_l a + e_l >= 0
4318 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4319 * We have
4321 * - f_u e_l <= f_u f_l g a <= f_l e_u
4323 * Since all variables are integer valued, this is equivalent to
4325 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4327 * If this interval is at least f_u f_l g, then it contains at least
4328 * one integer value for a.
4329 * That is, the test constraint is
4331 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4333 * or
4335 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4337 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4338 * then the constraint can be scaled down by a factor g',
4339 * with the constant term replaced by
4340 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4341 * Note that the result of applying Fourier-Motzkin to this pair
4342 * of constraints is
4344 * f_l e_u + f_u e_l >= 0
4346 * If the constant term of the scaled down version of this constraint,
4347 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4348 * term of the scaled down test constraint, then the test constraint
4349 * is known to hold and no explicit evaluation is required.
4350 * This is essentially the Omega test.
4352 * If the test constraint consists of only a constant term, then
4353 * it is sufficient to look at the sign of this constant term.
4355 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4356 int l, int u, struct test_ineq_data *data)
4358 unsigned offset, n_div;
4359 offset = isl_basic_map_offset(bmap, isl_dim_div);
4360 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4362 isl_int_gcd(data->g,
4363 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4364 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4365 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4366 isl_int_neg(data->fu, data->fu);
4367 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4368 data->fu, bmap->ineq[l], offset + n_div);
4369 isl_int_mul(data->g, data->g, data->fl);
4370 isl_int_mul(data->g, data->g, data->fu);
4371 isl_int_sub(data->g, data->g, data->fl);
4372 isl_int_sub(data->g, data->g, data->fu);
4373 isl_int_add_ui(data->g, data->g, 1);
4374 isl_int_sub(data->fl, data->v->el[0], data->g);
4376 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4377 if (isl_int_is_zero(data->g))
4378 return isl_int_is_nonneg(data->fl);
4379 if (isl_int_is_one(data->g)) {
4380 isl_int_set(data->v->el[0], data->fl);
4381 return test_ineq_is_satisfied(bmap, data);
4383 isl_int_fdiv_q(data->fl, data->fl, data->g);
4384 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4385 if (isl_int_eq(data->fl, data->v->el[0]))
4386 return isl_bool_true;
4387 isl_int_set(data->v->el[0], data->fl);
4388 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4389 offset - 1 + n_div);
4391 return test_ineq_is_satisfied(bmap, data);
4394 /* Remove more kinds of divs that are not strictly needed.
4395 * In particular, if all pairs of lower and upper bounds on a div
4396 * are such that they allow at least one integer value of the div,
4397 * then we can eliminate the div using Fourier-Motzkin without
4398 * introducing any spurious solutions.
4400 * If at least one of the two constraints has a unit coefficient for the div,
4401 * then the presence of such a value is guaranteed so there is no need to check.
4402 * In particular, the value attained by the bound with unit coefficient
4403 * can serve as this intermediate value.
4405 static struct isl_basic_map *drop_more_redundant_divs(
4406 struct isl_basic_map *bmap, int *pairs, int n)
4408 isl_ctx *ctx;
4409 struct test_ineq_data data = { NULL, NULL };
4410 unsigned off, n_div;
4411 int remove = -1;
4413 isl_int_init(data.g);
4414 isl_int_init(data.fl);
4415 isl_int_init(data.fu);
4417 if (!bmap)
4418 goto error;
4420 ctx = isl_basic_map_get_ctx(bmap);
4421 off = isl_basic_map_offset(bmap, isl_dim_div);
4422 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4423 data.v = isl_vec_alloc(ctx, off + n_div);
4424 if (!data.v)
4425 goto error;
4427 while (n > 0) {
4428 int i, l, u;
4429 int best = -1;
4430 isl_bool has_int;
4432 for (i = 0; i < n_div; ++i) {
4433 if (!pairs[i])
4434 continue;
4435 if (best >= 0 && pairs[best] <= pairs[i])
4436 continue;
4437 best = i;
4440 i = best;
4441 for (l = 0; l < bmap->n_ineq; ++l) {
4442 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4443 continue;
4444 if (isl_int_is_one(bmap->ineq[l][off + i]))
4445 continue;
4446 for (u = 0; u < bmap->n_ineq; ++u) {
4447 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4448 continue;
4449 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4450 continue;
4451 has_int = int_between_bounds(bmap, i, l, u,
4452 &data);
4453 if (has_int < 0)
4454 goto error;
4455 if (data.tab && data.tab->empty)
4456 break;
4457 if (!has_int)
4458 break;
4460 if (u < bmap->n_ineq)
4461 break;
4463 if (data.tab && data.tab->empty) {
4464 bmap = isl_basic_map_set_to_empty(bmap);
4465 break;
4467 if (l == bmap->n_ineq) {
4468 remove = i;
4469 break;
4471 pairs[i] = 0;
4472 --n;
4475 test_ineq_data_clear(&data);
4477 free(pairs);
4479 if (remove < 0)
4480 return bmap;
4482 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4483 return isl_basic_map_drop_redundant_divs(bmap);
4484 error:
4485 free(pairs);
4486 isl_basic_map_free(bmap);
4487 test_ineq_data_clear(&data);
4488 return NULL;
4491 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4492 * and the upper bound u, div1 always occurs together with div2 in the form
4493 * (div1 + m div2), where m is the constant range on the variable div1
4494 * allowed by l and u, replace the pair div1 and div2 by a single
4495 * div that is equal to div1 + m div2.
4497 * The new div will appear in the location that contains div2.
4498 * We need to modify all constraints that contain
4499 * div2 = (div - div1) / m
4500 * The coefficient of div2 is known to be equal to 1 or -1.
4501 * (If a constraint does not contain div2, it will also not contain div1.)
4502 * If the constraint also contains div1, then we know they appear
4503 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4504 * i.e., the coefficient of div is f.
4506 * Otherwise, we first need to introduce div1 into the constraint.
4507 * Let the l be
4509 * div1 + f >=0
4511 * and u
4513 * -div1 + f' >= 0
4515 * A lower bound on div2
4517 * div2 + t >= 0
4519 * can be replaced by
4521 * m div2 + div1 + m t + f >= 0
4523 * An upper bound
4525 * -div2 + t >= 0
4527 * can be replaced by
4529 * -(m div2 + div1) + m t + f' >= 0
4531 * These constraint are those that we would obtain from eliminating
4532 * div1 using Fourier-Motzkin.
4534 * After all constraints have been modified, we drop the lower and upper
4535 * bound and then drop div1.
4537 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4538 unsigned div1, unsigned div2, unsigned l, unsigned u)
4540 isl_ctx *ctx;
4541 isl_int m;
4542 unsigned dim, total;
4543 int i;
4545 ctx = isl_basic_map_get_ctx(bmap);
4547 dim = isl_space_dim(bmap->dim, isl_dim_all);
4548 total = 1 + dim + bmap->n_div;
4550 isl_int_init(m);
4551 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4552 isl_int_add_ui(m, m, 1);
4554 for (i = 0; i < bmap->n_ineq; ++i) {
4555 if (i == l || i == u)
4556 continue;
4557 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4558 continue;
4559 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4560 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4561 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4562 ctx->one, bmap->ineq[l], total);
4563 else
4564 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4565 ctx->one, bmap->ineq[u], total);
4567 isl_int_set(bmap->ineq[i][1 + dim + div2],
4568 bmap->ineq[i][1 + dim + div1]);
4569 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4572 isl_int_clear(m);
4573 if (l > u) {
4574 isl_basic_map_drop_inequality(bmap, l);
4575 isl_basic_map_drop_inequality(bmap, u);
4576 } else {
4577 isl_basic_map_drop_inequality(bmap, u);
4578 isl_basic_map_drop_inequality(bmap, l);
4580 bmap = isl_basic_map_drop_div(bmap, div1);
4581 return bmap;
4584 /* First check if we can coalesce any pair of divs and
4585 * then continue with dropping more redundant divs.
4587 * We loop over all pairs of lower and upper bounds on a div
4588 * with coefficient 1 and -1, respectively, check if there
4589 * is any other div "c" with which we can coalesce the div
4590 * and if so, perform the coalescing.
4592 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4593 struct isl_basic_map *bmap, int *pairs, int n)
4595 int i, l, u;
4596 unsigned dim;
4598 dim = isl_space_dim(bmap->dim, isl_dim_all);
4600 for (i = 0; i < bmap->n_div; ++i) {
4601 if (!pairs[i])
4602 continue;
4603 for (l = 0; l < bmap->n_ineq; ++l) {
4604 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4605 continue;
4606 for (u = 0; u < bmap->n_ineq; ++u) {
4607 int c;
4609 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4610 continue;
4611 c = div_find_coalesce(bmap, pairs, i, l, u);
4612 if (c < 0)
4613 continue;
4614 free(pairs);
4615 bmap = coalesce_divs(bmap, i, c, l, u);
4616 return isl_basic_map_drop_redundant_divs(bmap);
4621 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4622 free(pairs);
4623 return bmap;
4626 return drop_more_redundant_divs(bmap, pairs, n);
4629 /* Are the "n" coefficients starting at "first" of inequality constraints
4630 * "i" and "j" of "bmap" equal to each other?
4632 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4633 int first, int n)
4635 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4638 /* Are the "n" coefficients starting at "first" of inequality constraints
4639 * "i" and "j" of "bmap" opposite to each other?
4641 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4642 int first, int n)
4644 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4647 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4648 * apart from the constant term?
4650 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4652 unsigned total;
4654 total = isl_basic_map_dim(bmap, isl_dim_all);
4655 return is_opposite_part(bmap, i, j, 1, total);
4658 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4659 * apart from the constant term and the coefficient at position "pos"?
4661 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4662 int pos)
4664 unsigned total;
4666 total = isl_basic_map_dim(bmap, isl_dim_all);
4667 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4668 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4671 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4672 * apart from the constant term and the coefficient at position "pos"?
4674 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4675 int pos)
4677 unsigned total;
4679 total = isl_basic_map_dim(bmap, isl_dim_all);
4680 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4681 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4684 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4685 * been modified, simplying it if "simplify" is set.
4686 * Free the temporary data structure "pairs" that was associated
4687 * to the old version of "bmap".
4689 static __isl_give isl_basic_map *drop_redundant_divs_again(
4690 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4692 if (simplify)
4693 bmap = isl_basic_map_simplify(bmap);
4694 free(pairs);
4695 return isl_basic_map_drop_redundant_divs(bmap);
4698 /* Is "div" the single unknown existentially quantified variable
4699 * in inequality constraint "ineq" of "bmap"?
4700 * "div" is known to have a non-zero coefficient in "ineq".
4702 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4703 int div)
4705 int i;
4706 unsigned n_div, o_div;
4707 isl_bool known;
4709 known = isl_basic_map_div_is_known(bmap, div);
4710 if (known < 0 || known)
4711 return isl_bool_not(known);
4712 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4713 if (n_div == 1)
4714 return isl_bool_true;
4715 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4716 for (i = 0; i < n_div; ++i) {
4717 isl_bool known;
4719 if (i == div)
4720 continue;
4721 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4722 continue;
4723 known = isl_basic_map_div_is_known(bmap, i);
4724 if (known < 0 || !known)
4725 return known;
4728 return isl_bool_true;
4731 /* Does integer division "div" have coefficient 1 in inequality constraint
4732 * "ineq" of "map"?
4734 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4736 unsigned o_div;
4738 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4739 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4740 return isl_bool_true;
4742 return isl_bool_false;
4745 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4746 * then try and drop redundant divs again,
4747 * freeing the temporary data structure "pairs" that was associated
4748 * to the old version of "bmap".
4750 static __isl_give isl_basic_map *set_eq_and_try_again(
4751 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4753 bmap = isl_basic_map_cow(bmap);
4754 isl_basic_map_inequality_to_equality(bmap, ineq);
4755 return drop_redundant_divs_again(bmap, pairs, 1);
4758 /* Drop the integer division at position "div", along with the two
4759 * inequality constraints "ineq1" and "ineq2" in which it appears
4760 * from "bmap" and then try and drop redundant divs again,
4761 * freeing the temporary data structure "pairs" that was associated
4762 * to the old version of "bmap".
4764 static __isl_give isl_basic_map *drop_div_and_try_again(
4765 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4766 __isl_take int *pairs)
4768 if (ineq1 > ineq2) {
4769 isl_basic_map_drop_inequality(bmap, ineq1);
4770 isl_basic_map_drop_inequality(bmap, ineq2);
4771 } else {
4772 isl_basic_map_drop_inequality(bmap, ineq2);
4773 isl_basic_map_drop_inequality(bmap, ineq1);
4775 bmap = isl_basic_map_drop_div(bmap, div);
4776 return drop_redundant_divs_again(bmap, pairs, 0);
4779 /* Given two inequality constraints
4781 * f(x) + n d + c >= 0, (ineq)
4783 * with d the variable at position "pos", and
4785 * f(x) + c0 >= 0, (lower)
4787 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4788 * determined by the first constraint.
4789 * That is, store
4791 * ceil((c0 - c)/n)
4793 * in *l.
4795 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4796 int ineq, int lower, int pos, isl_int *l)
4798 isl_int_neg(*l, bmap->ineq[ineq][0]);
4799 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4800 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4803 /* Given two inequality constraints
4805 * f(x) + n d + c >= 0, (ineq)
4807 * with d the variable at position "pos", and
4809 * -f(x) - c0 >= 0, (upper)
4811 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4812 * determined by the first constraint.
4813 * That is, store
4815 * ceil((-c1 - c)/n)
4817 * in *u.
4819 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4820 int ineq, int upper, int pos, isl_int *u)
4822 isl_int_neg(*u, bmap->ineq[ineq][0]);
4823 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4824 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4827 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4828 * does the corresponding lower bound have a fixed value in "bmap"?
4830 * In particular, "ineq" is of the form
4832 * f(x) + n d + c >= 0
4834 * with n > 0, c the constant term and
4835 * d the existentially quantified variable "div".
4836 * That is, the lower bound is
4838 * ceil((-f(x) - c)/n)
4840 * Look for a pair of constraints
4842 * f(x) + c0 >= 0
4843 * -f(x) + c1 >= 0
4845 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4846 * That is, check that
4848 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4850 * If so, return the index of inequality f(x) + c0 >= 0.
4851 * Otherwise, return -1.
4853 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4855 int i;
4856 int lower = -1, upper = -1;
4857 unsigned o_div, n_div;
4858 isl_int l, u;
4859 int equal;
4861 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4862 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4863 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4864 if (i == ineq)
4865 continue;
4866 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4867 continue;
4868 if (lower < 0 &&
4869 is_parallel_except(bmap, ineq, i, o_div + div)) {
4870 lower = i;
4871 continue;
4873 if (upper < 0 &&
4874 is_opposite_except(bmap, ineq, i, o_div + div)) {
4875 upper = i;
4879 if (lower < 0 || upper < 0)
4880 return -1;
4882 isl_int_init(l);
4883 isl_int_init(u);
4885 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4886 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4888 equal = isl_int_eq(l, u);
4890 isl_int_clear(l);
4891 isl_int_clear(u);
4893 return equal ? lower : -1;
4896 /* Given a lower bound constraint "ineq" on the existentially quantified
4897 * variable "div", such that the corresponding lower bound has
4898 * a fixed value in "bmap", assign this fixed value to the variable and
4899 * then try and drop redundant divs again,
4900 * freeing the temporary data structure "pairs" that was associated
4901 * to the old version of "bmap".
4902 * "lower" determines the constant value for the lower bound.
4904 * In particular, "ineq" is of the form
4906 * f(x) + n d + c >= 0,
4908 * while "lower" is of the form
4910 * f(x) + c0 >= 0
4912 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4913 * is ceil((c0 - c)/n).
4915 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4916 int div, int ineq, int lower, int *pairs)
4918 isl_int c;
4919 unsigned o_div;
4921 isl_int_init(c);
4923 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4924 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4925 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4926 free(pairs);
4928 isl_int_clear(c);
4930 return isl_basic_map_drop_redundant_divs(bmap);
4933 /* Remove divs that are not strictly needed based on the inequality
4934 * constraints.
4935 * In particular, if a div only occurs positively (or negatively)
4936 * in constraints, then it can simply be dropped.
4937 * Also, if a div occurs in only two constraints and if moreover
4938 * those two constraints are opposite to each other, except for the constant
4939 * term and if the sum of the constant terms is such that for any value
4940 * of the other values, there is always at least one integer value of the
4941 * div, i.e., if one plus this sum is greater than or equal to
4942 * the (absolute value) of the coefficient of the div in the constraints,
4943 * then we can also simply drop the div.
4945 * If an existentially quantified variable does not have an explicit
4946 * representation, appears in only a single lower bound that does not
4947 * involve any other such existentially quantified variables and appears
4948 * in this lower bound with coefficient 1,
4949 * then fix the variable to the value of the lower bound. That is,
4950 * turn the inequality into an equality.
4951 * If for any value of the other variables, there is any value
4952 * for the existentially quantified variable satisfying the constraints,
4953 * then this lower bound also satisfies the constraints.
4954 * It is therefore safe to pick this lower bound.
4956 * The same reasoning holds even if the coefficient is not one.
4957 * However, fixing the variable to the value of the lower bound may
4958 * in general introduce an extra integer division, in which case
4959 * it may be better to pick another value.
4960 * If this integer division has a known constant value, then plugging
4961 * in this constant value removes the existentially quantified variable
4962 * completely. In particular, if the lower bound is of the form
4963 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4964 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4965 * then the existentially quantified variable can be assigned this
4966 * shared value.
4968 * We skip divs that appear in equalities or in the definition of other divs.
4969 * Divs that appear in the definition of other divs usually occur in at least
4970 * 4 constraints, but the constraints may have been simplified.
4972 * If any divs are left after these simple checks then we move on
4973 * to more complicated cases in drop_more_redundant_divs.
4975 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4976 __isl_take isl_basic_map *bmap)
4978 int i, j;
4979 unsigned off;
4980 int *pairs = NULL;
4981 int n = 0;
4983 if (!bmap)
4984 goto error;
4985 if (bmap->n_div == 0)
4986 return bmap;
4988 off = isl_space_dim(bmap->dim, isl_dim_all);
4989 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4990 if (!pairs)
4991 goto error;
4993 for (i = 0; i < bmap->n_div; ++i) {
4994 int pos, neg;
4995 int last_pos, last_neg;
4996 int redundant;
4997 int defined;
4998 isl_bool opp, set_div;
5000 defined = !isl_int_is_zero(bmap->div[i][0]);
5001 for (j = i; j < bmap->n_div; ++j)
5002 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
5003 break;
5004 if (j < bmap->n_div)
5005 continue;
5006 for (j = 0; j < bmap->n_eq; ++j)
5007 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5008 break;
5009 if (j < bmap->n_eq)
5010 continue;
5011 ++n;
5012 pos = neg = 0;
5013 for (j = 0; j < bmap->n_ineq; ++j) {
5014 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5015 last_pos = j;
5016 ++pos;
5018 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5019 last_neg = j;
5020 ++neg;
5023 pairs[i] = pos * neg;
5024 if (pairs[i] == 0) {
5025 for (j = bmap->n_ineq - 1; j >= 0; --j)
5026 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5027 isl_basic_map_drop_inequality(bmap, j);
5028 bmap = isl_basic_map_drop_div(bmap, i);
5029 return drop_redundant_divs_again(bmap, pairs, 0);
5031 if (pairs[i] != 1)
5032 opp = isl_bool_false;
5033 else
5034 opp = is_opposite(bmap, last_pos, last_neg);
5035 if (opp < 0)
5036 goto error;
5037 if (!opp) {
5038 int lower;
5039 isl_bool single, one;
5041 if (pos != 1)
5042 continue;
5043 single = single_unknown(bmap, last_pos, i);
5044 if (single < 0)
5045 goto error;
5046 if (!single)
5047 continue;
5048 one = has_coef_one(bmap, i, last_pos);
5049 if (one < 0)
5050 goto error;
5051 if (one)
5052 return set_eq_and_try_again(bmap, last_pos,
5053 pairs);
5054 lower = lower_bound_is_cst(bmap, i, last_pos);
5055 if (lower >= 0)
5056 return fix_cst_lower(bmap, i, last_pos, lower,
5057 pairs);
5058 continue;
5061 isl_int_add(bmap->ineq[last_pos][0],
5062 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5063 isl_int_add_ui(bmap->ineq[last_pos][0],
5064 bmap->ineq[last_pos][0], 1);
5065 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5066 bmap->ineq[last_pos][1+off+i]);
5067 isl_int_sub_ui(bmap->ineq[last_pos][0],
5068 bmap->ineq[last_pos][0], 1);
5069 isl_int_sub(bmap->ineq[last_pos][0],
5070 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5071 if (redundant)
5072 return drop_div_and_try_again(bmap, i,
5073 last_pos, last_neg, pairs);
5074 if (defined)
5075 set_div = isl_bool_false;
5076 else
5077 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5078 if (set_div < 0)
5079 return isl_basic_map_free(bmap);
5080 if (set_div) {
5081 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5082 return drop_redundant_divs_again(bmap, pairs, 1);
5084 pairs[i] = 0;
5085 --n;
5088 if (n > 0)
5089 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5091 free(pairs);
5092 return bmap;
5093 error:
5094 free(pairs);
5095 isl_basic_map_free(bmap);
5096 return NULL;
5099 /* Consider the coefficients at "c" as a row vector and replace
5100 * them with their product with "T". "T" is assumed to be a square matrix.
5102 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5104 int n;
5105 isl_ctx *ctx;
5106 isl_vec *v;
5108 if (!T)
5109 return isl_stat_error;
5110 n = isl_mat_rows(T);
5111 if (isl_seq_first_non_zero(c, n) == -1)
5112 return isl_stat_ok;
5113 ctx = isl_mat_get_ctx(T);
5114 v = isl_vec_alloc(ctx, n);
5115 if (!v)
5116 return isl_stat_error;
5117 isl_seq_swp_or_cpy(v->el, c, n);
5118 v = isl_vec_mat_product(v, isl_mat_copy(T));
5119 if (!v)
5120 return isl_stat_error;
5121 isl_seq_swp_or_cpy(c, v->el, n);
5122 isl_vec_free(v);
5124 return isl_stat_ok;
5127 /* Plug in T for the variables in "bmap" starting at "pos".
5128 * T is a linear unimodular matrix, i.e., without constant term.
5130 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5131 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5133 int i;
5134 unsigned n, total;
5136 bmap = isl_basic_map_cow(bmap);
5137 if (!bmap || !T)
5138 goto error;
5140 n = isl_mat_cols(T);
5141 if (n != isl_mat_rows(T))
5142 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5143 "expecting square matrix", goto error);
5145 total = isl_basic_map_dim(bmap, isl_dim_all);
5146 if (pos + n > total || pos + n < pos)
5147 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5148 "invalid range", goto error);
5150 for (i = 0; i < bmap->n_eq; ++i)
5151 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5152 goto error;
5153 for (i = 0; i < bmap->n_ineq; ++i)
5154 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5155 goto error;
5156 for (i = 0; i < bmap->n_div; ++i) {
5157 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5158 continue;
5159 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5160 goto error;
5163 isl_mat_free(T);
5164 return bmap;
5165 error:
5166 isl_basic_map_free(bmap);
5167 isl_mat_free(T);
5168 return NULL;
5171 /* Remove divs that are not strictly needed.
5173 * First look for an equality constraint involving two or more
5174 * existentially quantified variables without an explicit
5175 * representation. Replace the combination that appears
5176 * in the equality constraint by a single existentially quantified
5177 * variable such that the equality can be used to derive
5178 * an explicit representation for the variable.
5179 * If there are no more such equality constraints, then continue
5180 * with isl_basic_map_drop_redundant_divs_ineq.
5182 * In particular, if the equality constraint is of the form
5184 * f(x) + \sum_i c_i a_i = 0
5186 * with a_i existentially quantified variable without explicit
5187 * representation, then apply a transformation on the existentially
5188 * quantified variables to turn the constraint into
5190 * f(x) + g a_1' = 0
5192 * with g the gcd of the c_i.
5193 * In order to easily identify which existentially quantified variables
5194 * have a complete explicit representation, i.e., without being defined
5195 * in terms of other existentially quantified variables without
5196 * an explicit representation, the existentially quantified variables
5197 * are first sorted.
5199 * The variable transformation is computed by extending the row
5200 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5202 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5203 * [a_2'] [ a_2 ]
5204 * ... = U ....
5205 * [a_n'] [ a_n ]
5207 * with [c_1/g ... c_n/g] representing the first row of U.
5208 * The inverse of U is then plugged into the original constraints.
5209 * The call to isl_basic_map_simplify makes sure the explicit
5210 * representation for a_1' is extracted from the equality constraint.
5212 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5213 __isl_take isl_basic_map *bmap)
5215 int first;
5216 int i;
5217 unsigned o_div, n_div;
5218 int l;
5219 isl_ctx *ctx;
5220 isl_mat *T;
5222 if (!bmap)
5223 return NULL;
5224 if (isl_basic_map_divs_known(bmap))
5225 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5226 if (bmap->n_eq == 0)
5227 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5228 bmap = isl_basic_map_sort_divs(bmap);
5229 if (!bmap)
5230 return NULL;
5232 first = isl_basic_map_first_unknown_div(bmap);
5233 if (first < 0)
5234 return isl_basic_map_free(bmap);
5236 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5237 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5239 for (i = 0; i < bmap->n_eq; ++i) {
5240 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5241 n_div - (first));
5242 if (l < 0)
5243 continue;
5244 l += first;
5245 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5246 n_div - (l + 1)) == -1)
5247 continue;
5248 break;
5250 if (i >= bmap->n_eq)
5251 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5253 ctx = isl_basic_map_get_ctx(bmap);
5254 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5255 if (!T)
5256 return isl_basic_map_free(bmap);
5257 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5258 T = isl_mat_normalize_row(T, 0);
5259 T = isl_mat_unimodular_complete(T, 1);
5260 T = isl_mat_right_inverse(T);
5262 for (i = l; i < n_div; ++i)
5263 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5264 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5265 bmap = isl_basic_map_simplify(bmap);
5267 return isl_basic_map_drop_redundant_divs(bmap);
5270 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5271 struct isl_basic_set *bset)
5273 isl_basic_map *bmap = bset_to_bmap(bset);
5274 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5277 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5279 int i;
5281 if (!map)
5282 return NULL;
5283 for (i = 0; i < map->n; ++i) {
5284 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5285 if (!map->p[i])
5286 goto error;
5288 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5289 return map;
5290 error:
5291 isl_map_free(map);
5292 return NULL;
5295 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5297 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5300 /* Does "bmap" satisfy any equality that involves more than 2 variables
5301 * and/or has coefficients different from -1 and 1?
5303 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5305 int i;
5306 unsigned total;
5308 total = isl_basic_map_dim(bmap, isl_dim_all);
5310 for (i = 0; i < bmap->n_eq; ++i) {
5311 int j, k;
5313 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5314 if (j < 0)
5315 continue;
5316 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5317 !isl_int_is_negone(bmap->eq[i][1 + j]))
5318 return 1;
5320 j += 1;
5321 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5322 if (k < 0)
5323 continue;
5324 j += k;
5325 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5326 !isl_int_is_negone(bmap->eq[i][1 + j]))
5327 return 1;
5329 j += 1;
5330 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5331 if (k >= 0)
5332 return 1;
5335 return 0;
5338 /* Remove any common factor g from the constraint coefficients in "v".
5339 * The constant term is stored in the first position and is replaced
5340 * by floor(c/g). If any common factor is removed and if this results
5341 * in a tightening of the constraint, then set *tightened.
5343 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5344 int *tightened)
5346 isl_ctx *ctx;
5348 if (!v)
5349 return NULL;
5350 ctx = isl_vec_get_ctx(v);
5351 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5352 if (isl_int_is_zero(ctx->normalize_gcd))
5353 return v;
5354 if (isl_int_is_one(ctx->normalize_gcd))
5355 return v;
5356 v = isl_vec_cow(v);
5357 if (!v)
5358 return NULL;
5359 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5360 *tightened = 1;
5361 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5362 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5363 v->size - 1);
5364 return v;
5367 /* If "bmap" is an integer set that satisfies any equality involving
5368 * more than 2 variables and/or has coefficients different from -1 and 1,
5369 * then use variable compression to reduce the coefficients by removing
5370 * any (hidden) common factor.
5371 * In particular, apply the variable compression to each constraint,
5372 * factor out any common factor in the non-constant coefficients and
5373 * then apply the inverse of the compression.
5374 * At the end, we mark the basic map as having reduced constants.
5375 * If this flag is still set on the next invocation of this function,
5376 * then we skip the computation.
5378 * Removing a common factor may result in a tightening of some of
5379 * the constraints. If this happens, then we may end up with two
5380 * opposite inequalities that can be replaced by an equality.
5381 * We therefore call isl_basic_map_detect_inequality_pairs,
5382 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5383 * and isl_basic_map_gauss if such a pair was found.
5385 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5386 __isl_take isl_basic_map *bmap)
5388 unsigned total;
5389 isl_ctx *ctx;
5390 isl_vec *v;
5391 isl_mat *eq, *T, *T2;
5392 int i;
5393 int tightened;
5395 if (!bmap)
5396 return NULL;
5397 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5398 return bmap;
5399 if (isl_basic_map_is_rational(bmap))
5400 return bmap;
5401 if (bmap->n_eq == 0)
5402 return bmap;
5403 if (!has_multiple_var_equality(bmap))
5404 return bmap;
5406 total = isl_basic_map_dim(bmap, isl_dim_all);
5407 ctx = isl_basic_map_get_ctx(bmap);
5408 v = isl_vec_alloc(ctx, 1 + total);
5409 if (!v)
5410 return isl_basic_map_free(bmap);
5412 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5413 T = isl_mat_variable_compression(eq, &T2);
5414 if (!T || !T2)
5415 goto error;
5416 if (T->n_col == 0) {
5417 isl_mat_free(T);
5418 isl_mat_free(T2);
5419 isl_vec_free(v);
5420 return isl_basic_map_set_to_empty(bmap);
5423 tightened = 0;
5424 for (i = 0; i < bmap->n_ineq; ++i) {
5425 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5426 v = isl_vec_mat_product(v, isl_mat_copy(T));
5427 v = normalize_constraint(v, &tightened);
5428 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5429 if (!v)
5430 goto error;
5431 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5434 isl_mat_free(T);
5435 isl_mat_free(T2);
5436 isl_vec_free(v);
5438 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5440 if (tightened) {
5441 int progress = 0;
5443 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5444 if (progress) {
5445 bmap = eliminate_divs_eq(bmap, &progress);
5446 bmap = isl_basic_map_gauss(bmap, NULL);
5450 return bmap;
5451 error:
5452 isl_mat_free(T);
5453 isl_mat_free(T2);
5454 isl_vec_free(v);
5455 return isl_basic_map_free(bmap);
5458 /* Shift the integer division at position "div" of "bmap"
5459 * by "shift" times the variable at position "pos".
5460 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5461 * corresponds to the constant term.
5463 * That is, if the integer division has the form
5465 * floor(f(x)/d)
5467 * then replace it by
5469 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5471 __isl_give isl_basic_map *isl_basic_map_shift_div(
5472 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5474 int i;
5475 unsigned total;
5477 if (isl_int_is_zero(shift))
5478 return bmap;
5479 if (!bmap)
5480 return NULL;
5482 total = isl_basic_map_dim(bmap, isl_dim_all);
5483 total -= isl_basic_map_dim(bmap, isl_dim_div);
5485 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5487 for (i = 0; i < bmap->n_eq; ++i) {
5488 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5489 continue;
5490 isl_int_submul(bmap->eq[i][pos],
5491 shift, bmap->eq[i][1 + total + div]);
5493 for (i = 0; i < bmap->n_ineq; ++i) {
5494 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5495 continue;
5496 isl_int_submul(bmap->ineq[i][pos],
5497 shift, bmap->ineq[i][1 + total + div]);
5499 for (i = 0; i < bmap->n_div; ++i) {
5500 if (isl_int_is_zero(bmap->div[i][0]))
5501 continue;
5502 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5503 continue;
5504 isl_int_submul(bmap->div[i][1 + pos],
5505 shift, bmap->div[i][1 + 1 + total + div]);
5508 return bmap;