Handle error conditions returned by level_before in isl_flow
[isl.git] / isl_map_simplify.c
blobbca9e93391cb2d350f91cb6130ccc4ddb1ffcc8e
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_is_named_or_nested(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_is_named_or_nested(map->dim, type))
242 return map;
243 map = isl_map_cow(map);
244 if (!map)
245 goto error;
246 map->dim = isl_space_drop_dims(map->dim, type, first, n);
247 if (!map->dim)
248 goto error;
250 for (i = 0; i < map->n; ++i) {
251 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
252 if (!map->p[i])
253 goto error;
255 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
257 return map;
258 error:
259 isl_map_free(map);
260 return NULL;
263 struct isl_set *isl_set_drop(struct isl_set *set,
264 enum isl_dim_type type, unsigned first, unsigned n)
266 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
269 struct isl_map *isl_map_drop_inputs(
270 struct isl_map *map, unsigned first, unsigned n)
272 return isl_map_drop(map, isl_dim_in, first, n);
276 * We don't cow, as the div is assumed to be redundant.
278 __isl_give isl_basic_map *isl_basic_map_drop_div(
279 __isl_take isl_basic_map *bmap, unsigned div)
281 int i;
282 unsigned pos;
284 if (!bmap)
285 goto error;
287 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
289 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
291 for (i = 0; i < bmap->n_eq; ++i)
292 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
294 for (i = 0; i < bmap->n_ineq; ++i) {
295 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
296 isl_basic_map_drop_inequality(bmap, i);
297 --i;
298 continue;
300 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
303 for (i = 0; i < bmap->n_div; ++i)
304 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
306 if (div != bmap->n_div - 1) {
307 int j;
308 isl_int *t = bmap->div[div];
310 for (j = div; j < bmap->n_div - 1; ++j)
311 bmap->div[j] = bmap->div[j+1];
313 bmap->div[bmap->n_div - 1] = t;
315 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
316 isl_basic_map_free_div(bmap, 1);
318 return bmap;
319 error:
320 isl_basic_map_free(bmap);
321 return NULL;
324 struct isl_basic_map *isl_basic_map_normalize_constraints(
325 struct isl_basic_map *bmap)
327 int i;
328 isl_int gcd;
329 unsigned total = isl_basic_map_total_dim(bmap);
331 if (!bmap)
332 return NULL;
334 isl_int_init(gcd);
335 for (i = bmap->n_eq - 1; i >= 0; --i) {
336 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
337 if (isl_int_is_zero(gcd)) {
338 if (!isl_int_is_zero(bmap->eq[i][0])) {
339 bmap = isl_basic_map_set_to_empty(bmap);
340 break;
342 isl_basic_map_drop_equality(bmap, i);
343 continue;
345 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
346 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
347 if (isl_int_is_one(gcd))
348 continue;
349 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
350 bmap = isl_basic_map_set_to_empty(bmap);
351 break;
353 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
356 for (i = bmap->n_ineq - 1; i >= 0; --i) {
357 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
358 if (isl_int_is_zero(gcd)) {
359 if (isl_int_is_neg(bmap->ineq[i][0])) {
360 bmap = isl_basic_map_set_to_empty(bmap);
361 break;
363 isl_basic_map_drop_inequality(bmap, i);
364 continue;
366 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
367 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
368 if (isl_int_is_one(gcd))
369 continue;
370 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
371 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
373 isl_int_clear(gcd);
375 return bmap;
378 struct isl_basic_set *isl_basic_set_normalize_constraints(
379 struct isl_basic_set *bset)
381 isl_basic_map *bmap = bset_to_bmap(bset);
382 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
385 /* Assuming the variable at position "pos" has an integer coefficient
386 * in integer division "div", extract it from this integer division.
387 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
388 * corresponds to the constant term.
390 * That is, the integer division is of the form
392 * floor((... + c * d * x_pos + ...)/d)
394 * Replace it by
396 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
398 static __isl_give isl_basic_map *remove_var_from_div(
399 __isl_take isl_basic_map *bmap, int div, int pos)
401 isl_int shift;
403 isl_int_init(shift);
404 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
405 isl_int_neg(shift, shift);
406 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
407 isl_int_clear(shift);
409 return bmap;
412 /* Check if integer division "div" has any integral coefficient
413 * (or constant term). If so, extract them from the integer division.
415 static __isl_give isl_basic_map *remove_independent_vars_from_div(
416 __isl_take isl_basic_map *bmap, int div)
418 int i;
419 unsigned total = 1 + isl_basic_map_total_dim(bmap);
421 for (i = 0; i < total; ++i) {
422 if (isl_int_is_zero(bmap->div[div][1 + i]))
423 continue;
424 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
425 bmap->div[div][0]))
426 continue;
427 bmap = remove_var_from_div(bmap, div, i);
428 if (!bmap)
429 break;
432 return bmap;
435 /* Check if any known integer division has any integral coefficient
436 * (or constant term). If so, extract them from the integer division.
438 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
439 __isl_take isl_basic_map *bmap)
441 int i;
443 if (!bmap)
444 return NULL;
445 if (bmap->n_div == 0)
446 return bmap;
448 for (i = 0; i < bmap->n_div; ++i) {
449 if (isl_int_is_zero(bmap->div[i][0]))
450 continue;
451 bmap = remove_independent_vars_from_div(bmap, i);
452 if (!bmap)
453 break;
456 return bmap;
459 /* Remove any common factor in numerator and denominator of the div expression,
460 * not taking into account the constant term.
461 * That is, if the div is of the form
463 * floor((a + m f(x))/(m d))
465 * then replace it by
467 * floor((floor(a/m) + f(x))/d)
469 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
470 * and can therefore not influence the result of the floor.
472 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
474 unsigned total = isl_basic_map_total_dim(bmap);
475 isl_ctx *ctx = bmap->ctx;
477 if (isl_int_is_zero(bmap->div[div][0]))
478 return;
479 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
480 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
481 if (isl_int_is_one(ctx->normalize_gcd))
482 return;
483 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
484 ctx->normalize_gcd);
485 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
486 ctx->normalize_gcd);
487 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
488 ctx->normalize_gcd, total);
491 /* Remove any common factor in numerator and denominator of a div expression,
492 * not taking into account the constant term.
493 * That is, look for any div of the form
495 * floor((a + m f(x))/(m d))
497 * and replace it by
499 * floor((floor(a/m) + f(x))/d)
501 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
502 * and can therefore not influence the result of the floor.
504 static __isl_give isl_basic_map *normalize_div_expressions(
505 __isl_take isl_basic_map *bmap)
507 int i;
509 if (!bmap)
510 return NULL;
511 if (bmap->n_div == 0)
512 return bmap;
514 for (i = 0; i < bmap->n_div; ++i)
515 normalize_div_expression(bmap, i);
517 return bmap;
520 /* Assumes divs have been ordered if keep_divs is set.
522 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
523 unsigned pos, isl_int *eq, int keep_divs, int *progress)
525 unsigned total;
526 unsigned space_total;
527 int k;
528 int last_div;
530 total = isl_basic_map_total_dim(bmap);
531 space_total = isl_space_dim(bmap->dim, isl_dim_all);
532 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
533 for (k = 0; k < bmap->n_eq; ++k) {
534 if (bmap->eq[k] == eq)
535 continue;
536 if (isl_int_is_zero(bmap->eq[k][1+pos]))
537 continue;
538 if (progress)
539 *progress = 1;
540 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
541 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
544 for (k = 0; k < bmap->n_ineq; ++k) {
545 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
546 continue;
547 if (progress)
548 *progress = 1;
549 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
550 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
551 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
554 for (k = 0; k < bmap->n_div; ++k) {
555 if (isl_int_is_zero(bmap->div[k][0]))
556 continue;
557 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
558 continue;
559 if (progress)
560 *progress = 1;
561 /* We need to be careful about circular definitions,
562 * so for now we just remove the definition of div k
563 * if the equality contains any divs.
564 * If keep_divs is set, then the divs have been ordered
565 * and we can keep the definition as long as the result
566 * is still ordered.
568 if (last_div == -1 || (keep_divs && last_div < k)) {
569 isl_seq_elim(bmap->div[k]+1, eq,
570 1+pos, 1+total, &bmap->div[k][0]);
571 normalize_div_expression(bmap, k);
572 } else
573 isl_seq_clr(bmap->div[k], 1 + total);
574 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
578 /* Assumes divs have been ordered if keep_divs is set.
580 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
581 isl_int *eq, unsigned div, int keep_divs)
583 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
585 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
587 bmap = isl_basic_map_drop_div(bmap, div);
589 return bmap;
592 /* Check if elimination of div "div" using equality "eq" would not
593 * result in a div depending on a later div.
595 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
596 unsigned div)
598 int k;
599 int last_div;
600 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
601 unsigned pos = space_total + div;
603 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
604 if (last_div < 0 || last_div <= div)
605 return 1;
607 for (k = 0; k <= last_div; ++k) {
608 if (isl_int_is_zero(bmap->div[k][0]))
609 continue;
610 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
611 return 0;
614 return 1;
617 /* Elimininate divs based on equalities
619 static struct isl_basic_map *eliminate_divs_eq(
620 struct isl_basic_map *bmap, int *progress)
622 int d;
623 int i;
624 int modified = 0;
625 unsigned off;
627 bmap = isl_basic_map_order_divs(bmap);
629 if (!bmap)
630 return NULL;
632 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
634 for (d = bmap->n_div - 1; d >= 0 ; --d) {
635 for (i = 0; i < bmap->n_eq; ++i) {
636 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
637 !isl_int_is_negone(bmap->eq[i][off + d]))
638 continue;
639 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
640 continue;
641 modified = 1;
642 *progress = 1;
643 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
644 if (isl_basic_map_drop_equality(bmap, i) < 0)
645 return isl_basic_map_free(bmap);
646 break;
649 if (modified)
650 return eliminate_divs_eq(bmap, progress);
651 return bmap;
654 /* Elimininate divs based on inequalities
656 static struct isl_basic_map *eliminate_divs_ineq(
657 struct isl_basic_map *bmap, int *progress)
659 int d;
660 int i;
661 unsigned off;
662 struct isl_ctx *ctx;
664 if (!bmap)
665 return NULL;
667 ctx = bmap->ctx;
668 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
670 for (d = bmap->n_div - 1; d >= 0 ; --d) {
671 for (i = 0; i < bmap->n_eq; ++i)
672 if (!isl_int_is_zero(bmap->eq[i][off + d]))
673 break;
674 if (i < bmap->n_eq)
675 continue;
676 for (i = 0; i < bmap->n_ineq; ++i)
677 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
678 break;
679 if (i < bmap->n_ineq)
680 continue;
681 *progress = 1;
682 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
683 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
684 break;
685 bmap = isl_basic_map_drop_div(bmap, d);
686 if (!bmap)
687 break;
689 return bmap;
692 struct isl_basic_map *isl_basic_map_gauss(
693 struct isl_basic_map *bmap, int *progress)
695 int k;
696 int done;
697 int last_var;
698 unsigned total_var;
699 unsigned total;
701 bmap = isl_basic_map_order_divs(bmap);
703 if (!bmap)
704 return NULL;
706 total = isl_basic_map_total_dim(bmap);
707 total_var = total - bmap->n_div;
709 last_var = total - 1;
710 for (done = 0; done < bmap->n_eq; ++done) {
711 for (; last_var >= 0; --last_var) {
712 for (k = done; k < bmap->n_eq; ++k)
713 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
714 break;
715 if (k < bmap->n_eq)
716 break;
718 if (last_var < 0)
719 break;
720 if (k != done)
721 swap_equality(bmap, k, done);
722 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
723 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
725 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
726 progress);
728 if (last_var >= total_var &&
729 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
730 unsigned div = last_var - total_var;
731 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
732 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
733 isl_int_set(bmap->div[div][0],
734 bmap->eq[done][1+last_var]);
735 if (progress)
736 *progress = 1;
737 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
740 if (done == bmap->n_eq)
741 return bmap;
742 for (k = done; k < bmap->n_eq; ++k) {
743 if (isl_int_is_zero(bmap->eq[k][0]))
744 continue;
745 return isl_basic_map_set_to_empty(bmap);
747 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
748 return bmap;
751 struct isl_basic_set *isl_basic_set_gauss(
752 struct isl_basic_set *bset, int *progress)
754 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
755 progress));
759 static unsigned int round_up(unsigned int v)
761 int old_v = v;
763 while (v) {
764 old_v = v;
765 v ^= v & -v;
767 return old_v << 1;
770 /* Hash table of inequalities in a basic map.
771 * "index" is an array of addresses of inequalities in the basic map, some
772 * of which are NULL. The inequalities are hashed on the coefficients
773 * except the constant term.
774 * "size" is the number of elements in the array and is always a power of two
775 * "bits" is the number of bits need to represent an index into the array.
776 * "total" is the total dimension of the basic map.
778 struct isl_constraint_index {
779 unsigned int size;
780 int bits;
781 isl_int ***index;
782 unsigned total;
785 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
787 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
788 __isl_keep isl_basic_map *bmap)
790 isl_ctx *ctx;
792 ci->index = NULL;
793 if (!bmap)
794 return isl_stat_error;
795 ci->total = isl_basic_set_total_dim(bmap);
796 if (bmap->n_ineq == 0)
797 return isl_stat_ok;
798 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
799 ci->bits = ffs(ci->size) - 1;
800 ctx = isl_basic_map_get_ctx(bmap);
801 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
802 if (!ci->index)
803 return isl_stat_error;
805 return isl_stat_ok;
808 /* Free the memory allocated by create_constraint_index.
810 static void constraint_index_free(struct isl_constraint_index *ci)
812 free(ci->index);
815 /* Return the position in ci->index that contains the address of
816 * an inequality that is equal to *ineq up to the constant term,
817 * provided this address is not identical to "ineq".
818 * If there is no such inequality, then return the position where
819 * such an inequality should be inserted.
821 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
823 int h;
824 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
825 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
826 if (ineq != ci->index[h] &&
827 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
828 break;
829 return h;
832 /* Return the position in ci->index that contains the address of
833 * an inequality that is equal to the k'th inequality of "bmap"
834 * up to the constant term, provided it does not point to the very
835 * same inequality.
836 * If there is no such inequality, then return the position where
837 * such an inequality should be inserted.
839 static int hash_index(struct isl_constraint_index *ci,
840 __isl_keep isl_basic_map *bmap, int k)
842 return hash_index_ineq(ci, &bmap->ineq[k]);
845 static int set_hash_index(struct isl_constraint_index *ci,
846 struct isl_basic_set *bset, int k)
848 return hash_index(ci, bset, k);
851 /* Fill in the "ci" data structure with the inequalities of "bset".
853 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
854 __isl_keep isl_basic_set *bset)
856 int k, h;
858 if (create_constraint_index(ci, bset) < 0)
859 return isl_stat_error;
861 for (k = 0; k < bset->n_ineq; ++k) {
862 h = set_hash_index(ci, bset, k);
863 ci->index[h] = &bset->ineq[k];
866 return isl_stat_ok;
869 /* Is the inequality ineq (obviously) redundant with respect
870 * to the constraints in "ci"?
872 * Look for an inequality in "ci" with the same coefficients and then
873 * check if the contant term of "ineq" is greater than or equal
874 * to the constant term of that inequality. If so, "ineq" is clearly
875 * redundant.
877 * Note that hash_index_ineq ignores a stored constraint if it has
878 * the same address as the passed inequality. It is ok to pass
879 * the address of a local variable here since it will never be
880 * the same as the address of a constraint in "ci".
882 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
883 isl_int *ineq)
885 int h;
887 h = hash_index_ineq(ci, &ineq);
888 if (!ci->index[h])
889 return isl_bool_false;
890 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
893 /* If we can eliminate more than one div, then we need to make
894 * sure we do it from last div to first div, in order not to
895 * change the position of the other divs that still need to
896 * be removed.
898 static struct isl_basic_map *remove_duplicate_divs(
899 struct isl_basic_map *bmap, int *progress)
901 unsigned int size;
902 int *index;
903 int *elim_for;
904 int k, l, h;
905 int bits;
906 struct isl_blk eq;
907 unsigned total_var;
908 unsigned total;
909 struct isl_ctx *ctx;
911 bmap = isl_basic_map_order_divs(bmap);
912 if (!bmap || bmap->n_div <= 1)
913 return bmap;
915 total_var = isl_space_dim(bmap->dim, isl_dim_all);
916 total = total_var + bmap->n_div;
918 ctx = bmap->ctx;
919 for (k = bmap->n_div - 1; k >= 0; --k)
920 if (!isl_int_is_zero(bmap->div[k][0]))
921 break;
922 if (k <= 0)
923 return bmap;
925 size = round_up(4 * bmap->n_div / 3 - 1);
926 if (size == 0)
927 return bmap;
928 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
929 bits = ffs(size) - 1;
930 index = isl_calloc_array(ctx, int, size);
931 if (!elim_for || !index)
932 goto out;
933 eq = isl_blk_alloc(ctx, 1+total);
934 if (isl_blk_is_error(eq))
935 goto out;
937 isl_seq_clr(eq.data, 1+total);
938 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
939 for (--k; k >= 0; --k) {
940 uint32_t hash;
942 if (isl_int_is_zero(bmap->div[k][0]))
943 continue;
945 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
946 for (h = hash; index[h]; h = (h+1) % size)
947 if (isl_seq_eq(bmap->div[k],
948 bmap->div[index[h]-1], 2+total))
949 break;
950 if (index[h]) {
951 *progress = 1;
952 l = index[h] - 1;
953 elim_for[l] = k + 1;
955 index[h] = k+1;
957 for (l = bmap->n_div - 1; l >= 0; --l) {
958 if (!elim_for[l])
959 continue;
960 k = elim_for[l] - 1;
961 isl_int_set_si(eq.data[1+total_var+k], -1);
962 isl_int_set_si(eq.data[1+total_var+l], 1);
963 bmap = eliminate_div(bmap, eq.data, l, 1);
964 if (!bmap)
965 break;
966 isl_int_set_si(eq.data[1+total_var+k], 0);
967 isl_int_set_si(eq.data[1+total_var+l], 0);
970 isl_blk_free(ctx, eq);
971 out:
972 free(index);
973 free(elim_for);
974 return bmap;
977 static int n_pure_div_eq(struct isl_basic_map *bmap)
979 int i, j;
980 unsigned total;
982 total = isl_space_dim(bmap->dim, isl_dim_all);
983 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
984 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
985 --j;
986 if (j < 0)
987 break;
988 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
989 return 0;
991 return i;
994 /* Normalize divs that appear in equalities.
996 * In particular, we assume that bmap contains some equalities
997 * of the form
999 * a x = m * e_i
1001 * and we want to replace the set of e_i by a minimal set and
1002 * such that the new e_i have a canonical representation in terms
1003 * of the vector x.
1004 * If any of the equalities involves more than one divs, then
1005 * we currently simply bail out.
1007 * Let us first additionally assume that all equalities involve
1008 * a div. The equalities then express modulo constraints on the
1009 * remaining variables and we can use "parameter compression"
1010 * to find a minimal set of constraints. The result is a transformation
1012 * x = T(x') = x_0 + G x'
1014 * with G a lower-triangular matrix with all elements below the diagonal
1015 * non-negative and smaller than the diagonal element on the same row.
1016 * We first normalize x_0 by making the same property hold in the affine
1017 * T matrix.
1018 * The rows i of G with a 1 on the diagonal do not impose any modulo
1019 * constraint and simply express x_i = x'_i.
1020 * For each of the remaining rows i, we introduce a div and a corresponding
1021 * equality. In particular
1023 * g_ii e_j = x_i - g_i(x')
1025 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1026 * corresponding div (if g_kk != 1).
1028 * If there are any equalities not involving any div, then we
1029 * first apply a variable compression on the variables x:
1031 * x = C x'' x'' = C_2 x
1033 * and perform the above parameter compression on A C instead of on A.
1034 * The resulting compression is then of the form
1036 * x'' = T(x') = x_0 + G x'
1038 * and in constructing the new divs and the corresponding equalities,
1039 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1040 * by the corresponding row from C_2.
1042 static struct isl_basic_map *normalize_divs(
1043 struct isl_basic_map *bmap, int *progress)
1045 int i, j, k;
1046 int total;
1047 int div_eq;
1048 struct isl_mat *B;
1049 struct isl_vec *d;
1050 struct isl_mat *T = NULL;
1051 struct isl_mat *C = NULL;
1052 struct isl_mat *C2 = NULL;
1053 isl_int v;
1054 int *pos = NULL;
1055 int dropped, needed;
1057 if (!bmap)
1058 return NULL;
1060 if (bmap->n_div == 0)
1061 return bmap;
1063 if (bmap->n_eq == 0)
1064 return bmap;
1066 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1067 return bmap;
1069 total = isl_space_dim(bmap->dim, isl_dim_all);
1070 div_eq = n_pure_div_eq(bmap);
1071 if (div_eq == 0)
1072 return bmap;
1074 if (div_eq < bmap->n_eq) {
1075 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1076 bmap->n_eq - div_eq, 0, 1 + total);
1077 C = isl_mat_variable_compression(B, &C2);
1078 if (!C || !C2)
1079 goto error;
1080 if (C->n_col == 0) {
1081 bmap = isl_basic_map_set_to_empty(bmap);
1082 isl_mat_free(C);
1083 isl_mat_free(C2);
1084 goto done;
1088 d = isl_vec_alloc(bmap->ctx, div_eq);
1089 if (!d)
1090 goto error;
1091 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1092 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1093 --j;
1094 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1096 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1098 if (C) {
1099 B = isl_mat_product(B, C);
1100 C = NULL;
1103 T = isl_mat_parameter_compression(B, d);
1104 if (!T)
1105 goto error;
1106 if (T->n_col == 0) {
1107 bmap = isl_basic_map_set_to_empty(bmap);
1108 isl_mat_free(C2);
1109 isl_mat_free(T);
1110 goto done;
1112 isl_int_init(v);
1113 for (i = 0; i < T->n_row - 1; ++i) {
1114 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1115 if (isl_int_is_zero(v))
1116 continue;
1117 isl_mat_col_submul(T, 0, v, 1 + i);
1119 isl_int_clear(v);
1120 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1121 if (!pos)
1122 goto error;
1123 /* We have to be careful because dropping equalities may reorder them */
1124 dropped = 0;
1125 for (j = bmap->n_div - 1; j >= 0; --j) {
1126 for (i = 0; i < bmap->n_eq; ++i)
1127 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1128 break;
1129 if (i < bmap->n_eq) {
1130 bmap = isl_basic_map_drop_div(bmap, j);
1131 isl_basic_map_drop_equality(bmap, i);
1132 ++dropped;
1135 pos[0] = 0;
1136 needed = 0;
1137 for (i = 1; i < T->n_row; ++i) {
1138 if (isl_int_is_one(T->row[i][i]))
1139 pos[i] = i;
1140 else
1141 needed++;
1143 if (needed > dropped) {
1144 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1145 needed, needed, 0);
1146 if (!bmap)
1147 goto error;
1149 for (i = 1; i < T->n_row; ++i) {
1150 if (isl_int_is_one(T->row[i][i]))
1151 continue;
1152 k = isl_basic_map_alloc_div(bmap);
1153 pos[i] = 1 + total + k;
1154 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1155 isl_int_set(bmap->div[k][0], T->row[i][i]);
1156 if (C2)
1157 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1158 else
1159 isl_int_set_si(bmap->div[k][1 + i], 1);
1160 for (j = 0; j < i; ++j) {
1161 if (isl_int_is_zero(T->row[i][j]))
1162 continue;
1163 if (pos[j] < T->n_row && C2)
1164 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1165 C2->row[pos[j]], 1 + total);
1166 else
1167 isl_int_neg(bmap->div[k][1 + pos[j]],
1168 T->row[i][j]);
1170 j = isl_basic_map_alloc_equality(bmap);
1171 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1172 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1174 free(pos);
1175 isl_mat_free(C2);
1176 isl_mat_free(T);
1178 if (progress)
1179 *progress = 1;
1180 done:
1181 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1183 return bmap;
1184 error:
1185 free(pos);
1186 isl_mat_free(C);
1187 isl_mat_free(C2);
1188 isl_mat_free(T);
1189 return bmap;
1192 static struct isl_basic_map *set_div_from_lower_bound(
1193 struct isl_basic_map *bmap, int div, int ineq)
1195 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1197 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1198 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1199 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1200 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1201 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1203 return bmap;
1206 /* Check whether it is ok to define a div based on an inequality.
1207 * To avoid the introduction of circular definitions of divs, we
1208 * do not allow such a definition if the resulting expression would refer to
1209 * any other undefined divs or if any known div is defined in
1210 * terms of the unknown div.
1212 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1213 int div, int ineq)
1215 int j;
1216 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1218 /* Not defined in terms of unknown divs */
1219 for (j = 0; j < bmap->n_div; ++j) {
1220 if (div == j)
1221 continue;
1222 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1223 continue;
1224 if (isl_int_is_zero(bmap->div[j][0]))
1225 return 0;
1228 /* No other div defined in terms of this one => avoid loops */
1229 for (j = 0; j < bmap->n_div; ++j) {
1230 if (div == j)
1231 continue;
1232 if (isl_int_is_zero(bmap->div[j][0]))
1233 continue;
1234 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1235 return 0;
1238 return 1;
1241 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1242 * be a better expression than the current one?
1244 * If we do not have any expression yet, then any expression would be better.
1245 * Otherwise we check if the last variable involved in the inequality
1246 * (disregarding the div that it would define) is in an earlier position
1247 * than the last variable involved in the current div expression.
1249 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1250 int div, int ineq)
1252 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1253 int last_div;
1254 int last_ineq;
1256 if (isl_int_is_zero(bmap->div[div][0]))
1257 return 1;
1259 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1260 bmap->n_div - (div + 1)) >= 0)
1261 return 0;
1263 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1264 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1265 total + bmap->n_div);
1267 return last_ineq < last_div;
1270 /* Given two constraints "k" and "l" that are opposite to each other,
1271 * except for the constant term, check if we can use them
1272 * to obtain an expression for one of the hitherto unknown divs or
1273 * a "better" expression for a div for which we already have an expression.
1274 * "sum" is the sum of the constant terms of the constraints.
1275 * If this sum is strictly smaller than the coefficient of one
1276 * of the divs, then this pair can be used define the div.
1277 * To avoid the introduction of circular definitions of divs, we
1278 * do not use the pair if the resulting expression would refer to
1279 * any other undefined divs or if any known div is defined in
1280 * terms of the unknown div.
1282 static struct isl_basic_map *check_for_div_constraints(
1283 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1285 int i;
1286 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1288 for (i = 0; i < bmap->n_div; ++i) {
1289 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1290 continue;
1291 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1292 continue;
1293 if (!better_div_constraint(bmap, i, k))
1294 continue;
1295 if (!ok_to_set_div_from_bound(bmap, i, k))
1296 break;
1297 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1298 bmap = set_div_from_lower_bound(bmap, i, k);
1299 else
1300 bmap = set_div_from_lower_bound(bmap, i, l);
1301 if (progress)
1302 *progress = 1;
1303 break;
1305 return bmap;
1308 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1309 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1311 struct isl_constraint_index ci;
1312 int k, l, h;
1313 unsigned total = isl_basic_map_total_dim(bmap);
1314 isl_int sum;
1316 if (!bmap || bmap->n_ineq <= 1)
1317 return bmap;
1319 if (create_constraint_index(&ci, bmap) < 0)
1320 return bmap;
1322 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1323 ci.index[h] = &bmap->ineq[0];
1324 for (k = 1; k < bmap->n_ineq; ++k) {
1325 h = hash_index(&ci, bmap, k);
1326 if (!ci.index[h]) {
1327 ci.index[h] = &bmap->ineq[k];
1328 continue;
1330 if (progress)
1331 *progress = 1;
1332 l = ci.index[h] - &bmap->ineq[0];
1333 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1334 swap_inequality(bmap, k, l);
1335 isl_basic_map_drop_inequality(bmap, k);
1336 --k;
1338 isl_int_init(sum);
1339 for (k = 0; k < bmap->n_ineq-1; ++k) {
1340 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1341 h = hash_index(&ci, bmap, k);
1342 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1343 if (!ci.index[h])
1344 continue;
1345 l = ci.index[h] - &bmap->ineq[0];
1346 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1347 if (isl_int_is_pos(sum)) {
1348 if (detect_divs)
1349 bmap = check_for_div_constraints(bmap, k, l,
1350 sum, progress);
1351 continue;
1353 if (isl_int_is_zero(sum)) {
1354 /* We need to break out of the loop after these
1355 * changes since the contents of the hash
1356 * will no longer be valid.
1357 * Plus, we probably we want to regauss first.
1359 if (progress)
1360 *progress = 1;
1361 isl_basic_map_drop_inequality(bmap, l);
1362 isl_basic_map_inequality_to_equality(bmap, k);
1363 } else
1364 bmap = isl_basic_map_set_to_empty(bmap);
1365 break;
1367 isl_int_clear(sum);
1369 constraint_index_free(&ci);
1370 return bmap;
1373 /* Detect all pairs of inequalities that form an equality.
1375 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1376 * Call it repeatedly while it is making progress.
1378 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1379 __isl_take isl_basic_map *bmap, int *progress)
1381 int duplicate;
1383 do {
1384 duplicate = 0;
1385 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1386 &duplicate, 0);
1387 if (progress && duplicate)
1388 *progress = 1;
1389 } while (duplicate);
1391 return bmap;
1394 /* Eliminate knowns divs from constraints where they appear with
1395 * a (positive or negative) unit coefficient.
1397 * That is, replace
1399 * floor(e/m) + f >= 0
1401 * by
1403 * e + m f >= 0
1405 * and
1407 * -floor(e/m) + f >= 0
1409 * by
1411 * -e + m f + m - 1 >= 0
1413 * The first conversion is valid because floor(e/m) >= -f is equivalent
1414 * to e/m >= -f because -f is an integral expression.
1415 * The second conversion follows from the fact that
1417 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1420 * Note that one of the div constraints may have been eliminated
1421 * due to being redundant with respect to the constraint that is
1422 * being modified by this function. The modified constraint may
1423 * no longer imply this div constraint, so we add it back to make
1424 * sure we do not lose any information.
1426 * We skip integral divs, i.e., those with denominator 1, as we would
1427 * risk eliminating the div from the div constraints. We do not need
1428 * to handle those divs here anyway since the div constraints will turn
1429 * out to form an equality and this equality can then be used to eliminate
1430 * the div from all constraints.
1432 static __isl_give isl_basic_map *eliminate_unit_divs(
1433 __isl_take isl_basic_map *bmap, int *progress)
1435 int i, j;
1436 isl_ctx *ctx;
1437 unsigned total;
1439 if (!bmap)
1440 return NULL;
1442 ctx = isl_basic_map_get_ctx(bmap);
1443 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1445 for (i = 0; i < bmap->n_div; ++i) {
1446 if (isl_int_is_zero(bmap->div[i][0]))
1447 continue;
1448 if (isl_int_is_one(bmap->div[i][0]))
1449 continue;
1450 for (j = 0; j < bmap->n_ineq; ++j) {
1451 int s;
1453 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1454 !isl_int_is_negone(bmap->ineq[j][total + i]))
1455 continue;
1457 *progress = 1;
1459 s = isl_int_sgn(bmap->ineq[j][total + i]);
1460 isl_int_set_si(bmap->ineq[j][total + i], 0);
1461 if (s < 0)
1462 isl_seq_combine(bmap->ineq[j],
1463 ctx->negone, bmap->div[i] + 1,
1464 bmap->div[i][0], bmap->ineq[j],
1465 total + bmap->n_div);
1466 else
1467 isl_seq_combine(bmap->ineq[j],
1468 ctx->one, bmap->div[i] + 1,
1469 bmap->div[i][0], bmap->ineq[j],
1470 total + bmap->n_div);
1471 if (s < 0) {
1472 isl_int_add(bmap->ineq[j][0],
1473 bmap->ineq[j][0], bmap->div[i][0]);
1474 isl_int_sub_ui(bmap->ineq[j][0],
1475 bmap->ineq[j][0], 1);
1478 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1479 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1480 return isl_basic_map_free(bmap);
1484 return bmap;
1487 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1489 int progress = 1;
1490 if (!bmap)
1491 return NULL;
1492 while (progress) {
1493 isl_bool empty;
1495 progress = 0;
1496 empty = isl_basic_map_plain_is_empty(bmap);
1497 if (empty < 0)
1498 return isl_basic_map_free(bmap);
1499 if (empty)
1500 break;
1501 bmap = isl_basic_map_normalize_constraints(bmap);
1502 bmap = remove_independent_vars_from_divs(bmap);
1503 bmap = normalize_div_expressions(bmap);
1504 bmap = remove_duplicate_divs(bmap, &progress);
1505 bmap = eliminate_unit_divs(bmap, &progress);
1506 bmap = eliminate_divs_eq(bmap, &progress);
1507 bmap = eliminate_divs_ineq(bmap, &progress);
1508 bmap = isl_basic_map_gauss(bmap, &progress);
1509 /* requires equalities in normal form */
1510 bmap = normalize_divs(bmap, &progress);
1511 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1512 &progress, 1);
1513 if (bmap && progress)
1514 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1516 return bmap;
1519 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1521 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1525 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1526 isl_int *constraint, unsigned div)
1528 unsigned pos;
1530 if (!bmap)
1531 return -1;
1533 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1535 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1536 int neg;
1537 isl_int_sub(bmap->div[div][1],
1538 bmap->div[div][1], bmap->div[div][0]);
1539 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1540 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1541 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1542 isl_int_add(bmap->div[div][1],
1543 bmap->div[div][1], bmap->div[div][0]);
1544 if (!neg)
1545 return 0;
1546 if (isl_seq_first_non_zero(constraint+pos+1,
1547 bmap->n_div-div-1) != -1)
1548 return 0;
1549 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1550 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1551 return 0;
1552 if (isl_seq_first_non_zero(constraint+pos+1,
1553 bmap->n_div-div-1) != -1)
1554 return 0;
1555 } else
1556 return 0;
1558 return 1;
1561 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1562 isl_int *constraint, unsigned div)
1564 return isl_basic_map_is_div_constraint(bset, constraint, div);
1568 /* If the only constraints a div d=floor(f/m)
1569 * appears in are its two defining constraints
1571 * f - m d >=0
1572 * -(f - (m - 1)) + m d >= 0
1574 * then it can safely be removed.
1576 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1578 int i;
1579 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1581 for (i = 0; i < bmap->n_eq; ++i)
1582 if (!isl_int_is_zero(bmap->eq[i][pos]))
1583 return 0;
1585 for (i = 0; i < bmap->n_ineq; ++i) {
1586 if (isl_int_is_zero(bmap->ineq[i][pos]))
1587 continue;
1588 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1589 return 0;
1592 for (i = 0; i < bmap->n_div; ++i) {
1593 if (isl_int_is_zero(bmap->div[i][0]))
1594 continue;
1595 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1596 return 0;
1599 return 1;
1603 * Remove divs that don't occur in any of the constraints or other divs.
1604 * These can arise when dropping constraints from a basic map or
1605 * when the divs of a basic map have been temporarily aligned
1606 * with the divs of another basic map.
1608 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1610 int i;
1612 if (!bmap)
1613 return NULL;
1615 for (i = bmap->n_div-1; i >= 0; --i) {
1616 if (!div_is_redundant(bmap, i))
1617 continue;
1618 bmap = isl_basic_map_drop_div(bmap, i);
1620 return bmap;
1623 /* Mark "bmap" as final, without checking for obviously redundant
1624 * integer divisions. This function should be used when "bmap"
1625 * is known not to involve any such integer divisions.
1627 __isl_give isl_basic_map *isl_basic_map_mark_final(
1628 __isl_take isl_basic_map *bmap)
1630 if (!bmap)
1631 return NULL;
1632 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1633 return bmap;
1636 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1638 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1640 bmap = remove_redundant_divs(bmap);
1641 bmap = isl_basic_map_mark_final(bmap);
1642 return bmap;
1645 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1647 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1650 struct isl_set *isl_set_finalize(struct isl_set *set)
1652 int i;
1654 if (!set)
1655 return NULL;
1656 for (i = 0; i < set->n; ++i) {
1657 set->p[i] = isl_basic_set_finalize(set->p[i]);
1658 if (!set->p[i])
1659 goto error;
1661 return set;
1662 error:
1663 isl_set_free(set);
1664 return NULL;
1667 struct isl_map *isl_map_finalize(struct isl_map *map)
1669 int i;
1671 if (!map)
1672 return NULL;
1673 for (i = 0; i < map->n; ++i) {
1674 map->p[i] = isl_basic_map_finalize(map->p[i]);
1675 if (!map->p[i])
1676 goto error;
1678 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1679 return map;
1680 error:
1681 isl_map_free(map);
1682 return NULL;
1686 /* Remove definition of any div that is defined in terms of the given variable.
1687 * The div itself is not removed. Functions such as
1688 * eliminate_divs_ineq depend on the other divs remaining in place.
1690 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1691 int pos)
1693 int i;
1695 if (!bmap)
1696 return NULL;
1698 for (i = 0; i < bmap->n_div; ++i) {
1699 if (isl_int_is_zero(bmap->div[i][0]))
1700 continue;
1701 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1702 continue;
1703 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1704 if (!bmap)
1705 return NULL;
1707 return bmap;
1710 /* Eliminate the specified variables from the constraints using
1711 * Fourier-Motzkin. The variables themselves are not removed.
1713 struct isl_basic_map *isl_basic_map_eliminate_vars(
1714 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1716 int d;
1717 int i, j, k;
1718 unsigned total;
1719 int need_gauss = 0;
1721 if (n == 0)
1722 return bmap;
1723 if (!bmap)
1724 return NULL;
1725 total = isl_basic_map_total_dim(bmap);
1727 bmap = isl_basic_map_cow(bmap);
1728 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1729 bmap = remove_dependent_vars(bmap, d);
1730 if (!bmap)
1731 return NULL;
1733 for (d = pos + n - 1;
1734 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1735 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1736 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1737 int n_lower, n_upper;
1738 if (!bmap)
1739 return NULL;
1740 for (i = 0; i < bmap->n_eq; ++i) {
1741 if (isl_int_is_zero(bmap->eq[i][1+d]))
1742 continue;
1743 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1744 isl_basic_map_drop_equality(bmap, i);
1745 need_gauss = 1;
1746 break;
1748 if (i < bmap->n_eq)
1749 continue;
1750 n_lower = 0;
1751 n_upper = 0;
1752 for (i = 0; i < bmap->n_ineq; ++i) {
1753 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1754 n_lower++;
1755 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1756 n_upper++;
1758 bmap = isl_basic_map_extend_constraints(bmap,
1759 0, n_lower * n_upper);
1760 if (!bmap)
1761 goto error;
1762 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1763 int last;
1764 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1765 continue;
1766 last = -1;
1767 for (j = 0; j < i; ++j) {
1768 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1769 continue;
1770 last = j;
1771 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1772 isl_int_sgn(bmap->ineq[j][1+d]))
1773 continue;
1774 k = isl_basic_map_alloc_inequality(bmap);
1775 if (k < 0)
1776 goto error;
1777 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1778 1+total);
1779 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1780 1+d, 1+total, NULL);
1782 isl_basic_map_drop_inequality(bmap, i);
1783 i = last + 1;
1785 if (n_lower > 0 && n_upper > 0) {
1786 bmap = isl_basic_map_normalize_constraints(bmap);
1787 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1788 NULL, 0);
1789 bmap = isl_basic_map_gauss(bmap, NULL);
1790 bmap = isl_basic_map_remove_redundancies(bmap);
1791 need_gauss = 0;
1792 if (!bmap)
1793 goto error;
1794 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1795 break;
1798 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1799 if (need_gauss)
1800 bmap = isl_basic_map_gauss(bmap, NULL);
1801 return bmap;
1802 error:
1803 isl_basic_map_free(bmap);
1804 return NULL;
1807 struct isl_basic_set *isl_basic_set_eliminate_vars(
1808 struct isl_basic_set *bset, unsigned pos, unsigned n)
1810 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1811 pos, n));
1814 /* Eliminate the specified n dimensions starting at first from the
1815 * constraints, without removing the dimensions from the space.
1816 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1817 * Otherwise, they are projected out and the original space is restored.
1819 __isl_give isl_basic_map *isl_basic_map_eliminate(
1820 __isl_take isl_basic_map *bmap,
1821 enum isl_dim_type type, unsigned first, unsigned n)
1823 isl_space *space;
1825 if (!bmap)
1826 return NULL;
1827 if (n == 0)
1828 return bmap;
1830 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1831 isl_die(bmap->ctx, isl_error_invalid,
1832 "index out of bounds", goto error);
1834 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1835 first += isl_basic_map_offset(bmap, type) - 1;
1836 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1837 return isl_basic_map_finalize(bmap);
1840 space = isl_basic_map_get_space(bmap);
1841 bmap = isl_basic_map_project_out(bmap, type, first, n);
1842 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1843 bmap = isl_basic_map_reset_space(bmap, space);
1844 return bmap;
1845 error:
1846 isl_basic_map_free(bmap);
1847 return NULL;
1850 __isl_give isl_basic_set *isl_basic_set_eliminate(
1851 __isl_take isl_basic_set *bset,
1852 enum isl_dim_type type, unsigned first, unsigned n)
1854 return isl_basic_map_eliminate(bset, type, first, n);
1857 /* Remove all constraints from "bmap" that reference any unknown local
1858 * variables (directly or indirectly).
1860 * Dropping all constraints on a local variable will make it redundant,
1861 * so it will get removed implicitly by
1862 * isl_basic_map_drop_constraints_involving_dims. Some other local
1863 * variables may also end up becoming redundant if they only appear
1864 * in constraints together with the unknown local variable.
1865 * Therefore, start over after calling
1866 * isl_basic_map_drop_constraints_involving_dims.
1868 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1869 __isl_take isl_basic_map *bmap)
1871 isl_bool known;
1872 int i, n_div, o_div;
1874 known = isl_basic_map_divs_known(bmap);
1875 if (known < 0)
1876 return isl_basic_map_free(bmap);
1877 if (known)
1878 return bmap;
1880 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1881 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1883 for (i = 0; i < n_div; ++i) {
1884 known = isl_basic_map_div_is_known(bmap, i);
1885 if (known < 0)
1886 return isl_basic_map_free(bmap);
1887 if (known)
1888 continue;
1889 bmap = remove_dependent_vars(bmap, o_div + i);
1890 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1891 isl_dim_div, i, 1);
1892 if (!bmap)
1893 return NULL;
1894 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1895 i = -1;
1898 return bmap;
1901 /* Remove all constraints from "map" that reference any unknown local
1902 * variables (directly or indirectly).
1904 * Since constraints may get dropped from the basic maps,
1905 * they may no longer be disjoint from each other.
1907 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1908 __isl_take isl_map *map)
1910 int i;
1911 isl_bool known;
1913 known = isl_map_divs_known(map);
1914 if (known < 0)
1915 return isl_map_free(map);
1916 if (known)
1917 return map;
1919 map = isl_map_cow(map);
1920 if (!map)
1921 return NULL;
1923 for (i = 0; i < map->n; ++i) {
1924 map->p[i] =
1925 isl_basic_map_drop_constraint_involving_unknown_divs(
1926 map->p[i]);
1927 if (!map->p[i])
1928 return isl_map_free(map);
1931 if (map->n > 1)
1932 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1934 return map;
1937 /* Don't assume equalities are in order, because align_divs
1938 * may have changed the order of the divs.
1940 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1942 int d, i;
1943 unsigned total;
1945 total = isl_space_dim(bmap->dim, isl_dim_all);
1946 for (d = 0; d < total; ++d)
1947 elim[d] = -1;
1948 for (i = 0; i < bmap->n_eq; ++i) {
1949 for (d = total - 1; d >= 0; --d) {
1950 if (isl_int_is_zero(bmap->eq[i][1+d]))
1951 continue;
1952 elim[d] = i;
1953 break;
1958 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1960 compute_elimination_index(bset_to_bmap(bset), elim);
1963 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1964 struct isl_basic_map *bmap, int *elim)
1966 int d;
1967 int copied = 0;
1968 unsigned total;
1970 total = isl_space_dim(bmap->dim, isl_dim_all);
1971 for (d = total - 1; d >= 0; --d) {
1972 if (isl_int_is_zero(src[1+d]))
1973 continue;
1974 if (elim[d] == -1)
1975 continue;
1976 if (!copied) {
1977 isl_seq_cpy(dst, src, 1 + total);
1978 copied = 1;
1980 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1982 return copied;
1985 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1986 struct isl_basic_set *bset, int *elim)
1988 return reduced_using_equalities(dst, src,
1989 bset_to_bmap(bset), elim);
1992 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1993 struct isl_basic_set *bset, struct isl_basic_set *context)
1995 int i;
1996 int *elim;
1998 if (!bset || !context)
1999 goto error;
2001 if (context->n_eq == 0) {
2002 isl_basic_set_free(context);
2003 return bset;
2006 bset = isl_basic_set_cow(bset);
2007 if (!bset)
2008 goto error;
2010 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2011 if (!elim)
2012 goto error;
2013 set_compute_elimination_index(context, elim);
2014 for (i = 0; i < bset->n_eq; ++i)
2015 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2016 context, elim);
2017 for (i = 0; i < bset->n_ineq; ++i)
2018 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2019 context, elim);
2020 isl_basic_set_free(context);
2021 free(elim);
2022 bset = isl_basic_set_simplify(bset);
2023 bset = isl_basic_set_finalize(bset);
2024 return bset;
2025 error:
2026 isl_basic_set_free(bset);
2027 isl_basic_set_free(context);
2028 return NULL;
2031 /* For each inequality in "ineq" that is a shifted (more relaxed)
2032 * copy of an inequality in "context", mark the corresponding entry
2033 * in "row" with -1.
2034 * If an inequality only has a non-negative constant term, then
2035 * mark it as well.
2037 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2038 __isl_keep isl_basic_set *context, int *row)
2040 struct isl_constraint_index ci;
2041 int n_ineq;
2042 unsigned total;
2043 int k;
2045 if (!ineq || !context)
2046 return isl_stat_error;
2047 if (context->n_ineq == 0)
2048 return isl_stat_ok;
2049 if (setup_constraint_index(&ci, context) < 0)
2050 return isl_stat_error;
2052 n_ineq = isl_mat_rows(ineq);
2053 total = isl_mat_cols(ineq) - 1;
2054 for (k = 0; k < n_ineq; ++k) {
2055 int l;
2056 isl_bool redundant;
2058 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2059 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2060 row[k] = -1;
2061 continue;
2063 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2064 if (redundant < 0)
2065 goto error;
2066 if (!redundant)
2067 continue;
2068 row[k] = -1;
2070 constraint_index_free(&ci);
2071 return isl_stat_ok;
2072 error:
2073 constraint_index_free(&ci);
2074 return isl_stat_error;
2077 static struct isl_basic_set *remove_shifted_constraints(
2078 struct isl_basic_set *bset, struct isl_basic_set *context)
2080 struct isl_constraint_index ci;
2081 int k;
2083 if (!bset || !context)
2084 return bset;
2086 if (context->n_ineq == 0)
2087 return bset;
2088 if (setup_constraint_index(&ci, context) < 0)
2089 return bset;
2091 for (k = 0; k < bset->n_ineq; ++k) {
2092 isl_bool redundant;
2094 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2095 if (redundant < 0)
2096 goto error;
2097 if (!redundant)
2098 continue;
2099 bset = isl_basic_set_cow(bset);
2100 if (!bset)
2101 goto error;
2102 isl_basic_set_drop_inequality(bset, k);
2103 --k;
2105 constraint_index_free(&ci);
2106 return bset;
2107 error:
2108 constraint_index_free(&ci);
2109 return bset;
2112 /* Remove constraints from "bmap" that are identical to constraints
2113 * in "context" or that are more relaxed (greater constant term).
2115 * We perform the test for shifted copies on the pure constraints
2116 * in remove_shifted_constraints.
2118 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2119 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2121 isl_basic_set *bset, *bset_context;
2123 if (!bmap || !context)
2124 goto error;
2126 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2127 isl_basic_map_free(context);
2128 return bmap;
2131 context = isl_basic_map_align_divs(context, bmap);
2132 bmap = isl_basic_map_align_divs(bmap, context);
2134 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2135 bset_context = isl_basic_map_underlying_set(context);
2136 bset = remove_shifted_constraints(bset, bset_context);
2137 isl_basic_set_free(bset_context);
2139 bmap = isl_basic_map_overlying_set(bset, bmap);
2141 return bmap;
2142 error:
2143 isl_basic_map_free(bmap);
2144 isl_basic_map_free(context);
2145 return NULL;
2148 /* Does the (linear part of a) constraint "c" involve any of the "len"
2149 * "relevant" dimensions?
2151 static int is_related(isl_int *c, int len, int *relevant)
2153 int i;
2155 for (i = 0; i < len; ++i) {
2156 if (!relevant[i])
2157 continue;
2158 if (!isl_int_is_zero(c[i]))
2159 return 1;
2162 return 0;
2165 /* Drop constraints from "bmap" that do not involve any of
2166 * the dimensions marked "relevant".
2168 static __isl_give isl_basic_map *drop_unrelated_constraints(
2169 __isl_take isl_basic_map *bmap, int *relevant)
2171 int i, dim;
2173 dim = isl_basic_map_dim(bmap, isl_dim_all);
2174 for (i = 0; i < dim; ++i)
2175 if (!relevant[i])
2176 break;
2177 if (i >= dim)
2178 return bmap;
2180 for (i = bmap->n_eq - 1; i >= 0; --i)
2181 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2182 bmap = isl_basic_map_cow(bmap);
2183 if (isl_basic_map_drop_equality(bmap, i) < 0)
2184 return isl_basic_map_free(bmap);
2187 for (i = bmap->n_ineq - 1; i >= 0; --i)
2188 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2189 bmap = isl_basic_map_cow(bmap);
2190 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2191 return isl_basic_map_free(bmap);
2194 return bmap;
2197 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2199 * In particular, for any variable involved in the constraint,
2200 * find the actual group id from before and replace the group
2201 * of the corresponding variable by the minimal group of all
2202 * the variables involved in the constraint considered so far
2203 * (if this minimum is smaller) or replace the minimum by this group
2204 * (if the minimum is larger).
2206 * At the end, all the variables in "c" will (indirectly) point
2207 * to the minimal of the groups that they referred to originally.
2209 static void update_groups(int dim, int *group, isl_int *c)
2211 int j;
2212 int min = dim;
2214 for (j = 0; j < dim; ++j) {
2215 if (isl_int_is_zero(c[j]))
2216 continue;
2217 while (group[j] >= 0 && group[group[j]] != group[j])
2218 group[j] = group[group[j]];
2219 if (group[j] == min)
2220 continue;
2221 if (group[j] < min) {
2222 if (min >= 0 && min < dim)
2223 group[min] = group[j];
2224 min = group[j];
2225 } else
2226 group[group[j]] = min;
2230 /* Allocate an array of groups of variables, one for each variable
2231 * in "context", initialized to zero.
2233 static int *alloc_groups(__isl_keep isl_basic_set *context)
2235 isl_ctx *ctx;
2236 int dim;
2238 dim = isl_basic_set_dim(context, isl_dim_set);
2239 ctx = isl_basic_set_get_ctx(context);
2240 return isl_calloc_array(ctx, int, dim);
2243 /* Drop constraints from "bmap" that only involve variables that are
2244 * not related to any of the variables marked with a "-1" in "group".
2246 * We construct groups of variables that collect variables that
2247 * (indirectly) appear in some common constraint of "bmap".
2248 * Each group is identified by the first variable in the group,
2249 * except for the special group of variables that was already identified
2250 * in the input as -1 (or are related to those variables).
2251 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2252 * otherwise the group of i is the group of group[i].
2254 * We first initialize groups for the remaining variables.
2255 * Then we iterate over the constraints of "bmap" and update the
2256 * group of the variables in the constraint by the smallest group.
2257 * Finally, we resolve indirect references to groups by running over
2258 * the variables.
2260 * After computing the groups, we drop constraints that do not involve
2261 * any variables in the -1 group.
2263 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2264 __isl_take isl_basic_map *bmap, __isl_take int *group)
2266 int dim;
2267 int i;
2268 int last;
2270 if (!bmap)
2271 return NULL;
2273 dim = isl_basic_map_dim(bmap, isl_dim_all);
2275 last = -1;
2276 for (i = 0; i < dim; ++i)
2277 if (group[i] >= 0)
2278 last = group[i] = i;
2279 if (last < 0) {
2280 free(group);
2281 return bmap;
2284 for (i = 0; i < bmap->n_eq; ++i)
2285 update_groups(dim, group, bmap->eq[i] + 1);
2286 for (i = 0; i < bmap->n_ineq; ++i)
2287 update_groups(dim, group, bmap->ineq[i] + 1);
2289 for (i = 0; i < dim; ++i)
2290 if (group[i] >= 0)
2291 group[i] = group[group[i]];
2293 for (i = 0; i < dim; ++i)
2294 group[i] = group[i] == -1;
2296 bmap = drop_unrelated_constraints(bmap, group);
2298 free(group);
2299 return bmap;
2302 /* Drop constraints from "context" that are irrelevant for computing
2303 * the gist of "bset".
2305 * In particular, drop constraints in variables that are not related
2306 * to any of the variables involved in the constraints of "bset"
2307 * in the sense that there is no sequence of constraints that connects them.
2309 * We first mark all variables that appear in "bset" as belonging
2310 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2312 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2313 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2315 int *group;
2316 int dim;
2317 int i, j;
2319 if (!context || !bset)
2320 return isl_basic_set_free(context);
2322 group = alloc_groups(context);
2324 if (!group)
2325 return isl_basic_set_free(context);
2327 dim = isl_basic_set_dim(bset, isl_dim_set);
2328 for (i = 0; i < dim; ++i) {
2329 for (j = 0; j < bset->n_eq; ++j)
2330 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2331 break;
2332 if (j < bset->n_eq) {
2333 group[i] = -1;
2334 continue;
2336 for (j = 0; j < bset->n_ineq; ++j)
2337 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2338 break;
2339 if (j < bset->n_ineq)
2340 group[i] = -1;
2343 return isl_basic_map_drop_unrelated_constraints(context, group);
2346 /* Drop constraints from "context" that are irrelevant for computing
2347 * the gist of the inequalities "ineq".
2348 * Inequalities in "ineq" for which the corresponding element of row
2349 * is set to -1 have already been marked for removal and should be ignored.
2351 * In particular, drop constraints in variables that are not related
2352 * to any of the variables involved in "ineq"
2353 * in the sense that there is no sequence of constraints that connects them.
2355 * We first mark all variables that appear in "bset" as belonging
2356 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2358 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2359 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2361 int *group;
2362 int dim;
2363 int i, j, n;
2365 if (!context || !ineq)
2366 return isl_basic_set_free(context);
2368 group = alloc_groups(context);
2370 if (!group)
2371 return isl_basic_set_free(context);
2373 dim = isl_basic_set_dim(context, isl_dim_set);
2374 n = isl_mat_rows(ineq);
2375 for (i = 0; i < dim; ++i) {
2376 for (j = 0; j < n; ++j) {
2377 if (row[j] < 0)
2378 continue;
2379 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2380 break;
2382 if (j < n)
2383 group[i] = -1;
2386 return isl_basic_map_drop_unrelated_constraints(context, group);
2389 /* Do all "n" entries of "row" contain a negative value?
2391 static int all_neg(int *row, int n)
2393 int i;
2395 for (i = 0; i < n; ++i)
2396 if (row[i] >= 0)
2397 return 0;
2399 return 1;
2402 /* Update the inequalities in "bset" based on the information in "row"
2403 * and "tab".
2405 * In particular, the array "row" contains either -1, meaning that
2406 * the corresponding inequality of "bset" is redundant, or the index
2407 * of an inequality in "tab".
2409 * If the row entry is -1, then drop the inequality.
2410 * Otherwise, if the constraint is marked redundant in the tableau,
2411 * then drop the inequality. Similarly, if it is marked as an equality
2412 * in the tableau, then turn the inequality into an equality and
2413 * perform Gaussian elimination.
2415 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2416 __isl_keep int *row, struct isl_tab *tab)
2418 int i;
2419 unsigned n_ineq;
2420 unsigned n_eq;
2421 int found_equality = 0;
2423 if (!bset)
2424 return NULL;
2425 if (tab && tab->empty)
2426 return isl_basic_set_set_to_empty(bset);
2428 n_ineq = bset->n_ineq;
2429 for (i = n_ineq - 1; i >= 0; --i) {
2430 if (row[i] < 0) {
2431 if (isl_basic_set_drop_inequality(bset, i) < 0)
2432 return isl_basic_set_free(bset);
2433 continue;
2435 if (!tab)
2436 continue;
2437 n_eq = tab->n_eq;
2438 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2439 isl_basic_map_inequality_to_equality(bset, i);
2440 found_equality = 1;
2441 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2442 if (isl_basic_set_drop_inequality(bset, i) < 0)
2443 return isl_basic_set_free(bset);
2447 if (found_equality)
2448 bset = isl_basic_set_gauss(bset, NULL);
2449 bset = isl_basic_set_finalize(bset);
2450 return bset;
2453 /* Update the inequalities in "bset" based on the information in "row"
2454 * and "tab" and free all arguments (other than "bset").
2456 static __isl_give isl_basic_set *update_ineq_free(
2457 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2458 __isl_take isl_basic_set *context, __isl_take int *row,
2459 struct isl_tab *tab)
2461 isl_mat_free(ineq);
2462 isl_basic_set_free(context);
2464 bset = update_ineq(bset, row, tab);
2466 free(row);
2467 isl_tab_free(tab);
2468 return bset;
2471 /* Remove all information from bset that is redundant in the context
2472 * of context.
2473 * "ineq" contains the (possibly transformed) inequalities of "bset",
2474 * in the same order.
2475 * The (explicit) equalities of "bset" are assumed to have been taken
2476 * into account by the transformation such that only the inequalities
2477 * are relevant.
2478 * "context" is assumed not to be empty.
2480 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2481 * A value of -1 means that the inequality is obviously redundant and may
2482 * not even appear in "tab".
2484 * We first mark the inequalities of "bset"
2485 * that are obviously redundant with respect to some inequality in "context".
2486 * Then we remove those constraints from "context" that have become
2487 * irrelevant for computing the gist of "bset".
2488 * Note that this removal of constraints cannot be replaced by
2489 * a factorization because factors in "bset" may still be connected
2490 * to each other through constraints in "context".
2492 * If there are any inequalities left, we construct a tableau for
2493 * the context and then add the inequalities of "bset".
2494 * Before adding these inequalities, we freeze all constraints such that
2495 * they won't be considered redundant in terms of the constraints of "bset".
2496 * Then we detect all redundant constraints (among the
2497 * constraints that weren't frozen), first by checking for redundancy in the
2498 * the tableau and then by checking if replacing a constraint by its negation
2499 * would lead to an empty set. This last step is fairly expensive
2500 * and could be optimized by more reuse of the tableau.
2501 * Finally, we update bset according to the results.
2503 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2504 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2506 int i, r;
2507 int *row = NULL;
2508 isl_ctx *ctx;
2509 isl_basic_set *combined = NULL;
2510 struct isl_tab *tab = NULL;
2511 unsigned n_eq, context_ineq;
2512 unsigned total;
2514 if (!bset || !ineq || !context)
2515 goto error;
2517 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2518 isl_basic_set_free(context);
2519 isl_mat_free(ineq);
2520 return bset;
2523 ctx = isl_basic_set_get_ctx(context);
2524 row = isl_calloc_array(ctx, int, bset->n_ineq);
2525 if (!row)
2526 goto error;
2528 if (mark_shifted_constraints(ineq, context, row) < 0)
2529 goto error;
2530 if (all_neg(row, bset->n_ineq))
2531 return update_ineq_free(bset, ineq, context, row, NULL);
2533 context = drop_irrelevant_constraints_marked(context, ineq, row);
2534 if (!context)
2535 goto error;
2536 if (isl_basic_set_plain_is_universe(context))
2537 return update_ineq_free(bset, ineq, context, row, NULL);
2539 n_eq = context->n_eq;
2540 context_ineq = context->n_ineq;
2541 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2542 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2543 tab = isl_tab_from_basic_set(combined, 0);
2544 for (i = 0; i < context_ineq; ++i)
2545 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2546 goto error;
2547 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2548 goto error;
2549 r = context_ineq;
2550 for (i = 0; i < bset->n_ineq; ++i) {
2551 if (row[i] < 0)
2552 continue;
2553 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2554 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2555 goto error;
2556 row[i] = r++;
2558 if (isl_tab_detect_implicit_equalities(tab) < 0)
2559 goto error;
2560 if (isl_tab_detect_redundant(tab) < 0)
2561 goto error;
2562 total = isl_basic_set_total_dim(bset);
2563 for (i = bset->n_ineq - 1; i >= 0; --i) {
2564 isl_basic_set *test;
2565 int is_empty;
2567 if (row[i] < 0)
2568 continue;
2569 r = row[i];
2570 if (tab->con[n_eq + r].is_redundant)
2571 continue;
2572 test = isl_basic_set_dup(combined);
2573 if (isl_inequality_negate(test, r) < 0)
2574 test = isl_basic_set_free(test);
2575 test = isl_basic_set_update_from_tab(test, tab);
2576 is_empty = isl_basic_set_is_empty(test);
2577 isl_basic_set_free(test);
2578 if (is_empty < 0)
2579 goto error;
2580 if (is_empty)
2581 tab->con[n_eq + r].is_redundant = 1;
2583 bset = update_ineq_free(bset, ineq, context, row, tab);
2584 if (bset) {
2585 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2586 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2589 isl_basic_set_free(combined);
2590 return bset;
2591 error:
2592 free(row);
2593 isl_mat_free(ineq);
2594 isl_tab_free(tab);
2595 isl_basic_set_free(combined);
2596 isl_basic_set_free(context);
2597 isl_basic_set_free(bset);
2598 return NULL;
2601 /* Extract the inequalities of "bset" as an isl_mat.
2603 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2605 unsigned total;
2606 isl_ctx *ctx;
2607 isl_mat *ineq;
2609 if (!bset)
2610 return NULL;
2612 ctx = isl_basic_set_get_ctx(bset);
2613 total = isl_basic_set_total_dim(bset);
2614 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2615 0, 1 + total);
2617 return ineq;
2620 /* Remove all information from "bset" that is redundant in the context
2621 * of "context", for the case where both "bset" and "context" are
2622 * full-dimensional.
2624 static __isl_give isl_basic_set *uset_gist_uncompressed(
2625 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2627 isl_mat *ineq;
2629 ineq = extract_ineq(bset);
2630 return uset_gist_full(bset, ineq, context);
2633 /* Remove all information from "bset" that is redundant in the context
2634 * of "context", for the case where the combined equalities of
2635 * "bset" and "context" allow for a compression that can be obtained
2636 * by preapplication of "T".
2638 * "bset" itself is not transformed by "T". Instead, the inequalities
2639 * are extracted from "bset" and those are transformed by "T".
2640 * uset_gist_full then determines which of the transformed inequalities
2641 * are redundant with respect to the transformed "context" and removes
2642 * the corresponding inequalities from "bset".
2644 * After preapplying "T" to the inequalities, any common factor is
2645 * removed from the coefficients. If this results in a tightening
2646 * of the constant term, then the same tightening is applied to
2647 * the corresponding untransformed inequality in "bset".
2648 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2650 * g f'(x) + r >= 0
2652 * with 0 <= r < g, then it is equivalent to
2654 * f'(x) >= 0
2656 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2657 * subspace compressed by T since the latter would be transformed to
2659 * g f'(x) >= 0
2661 static __isl_give isl_basic_set *uset_gist_compressed(
2662 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2663 __isl_take isl_mat *T)
2665 isl_ctx *ctx;
2666 isl_mat *ineq;
2667 int i, n_row, n_col;
2668 isl_int rem;
2670 ineq = extract_ineq(bset);
2671 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2672 context = isl_basic_set_preimage(context, T);
2674 if (!ineq || !context)
2675 goto error;
2676 if (isl_basic_set_plain_is_empty(context)) {
2677 isl_mat_free(ineq);
2678 isl_basic_set_free(context);
2679 return isl_basic_set_set_to_empty(bset);
2682 ctx = isl_mat_get_ctx(ineq);
2683 n_row = isl_mat_rows(ineq);
2684 n_col = isl_mat_cols(ineq);
2685 isl_int_init(rem);
2686 for (i = 0; i < n_row; ++i) {
2687 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2688 if (isl_int_is_zero(ctx->normalize_gcd))
2689 continue;
2690 if (isl_int_is_one(ctx->normalize_gcd))
2691 continue;
2692 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2693 ctx->normalize_gcd, n_col - 1);
2694 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2695 isl_int_fdiv_q(ineq->row[i][0],
2696 ineq->row[i][0], ctx->normalize_gcd);
2697 if (isl_int_is_zero(rem))
2698 continue;
2699 bset = isl_basic_set_cow(bset);
2700 if (!bset)
2701 break;
2702 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2704 isl_int_clear(rem);
2706 return uset_gist_full(bset, ineq, context);
2707 error:
2708 isl_mat_free(ineq);
2709 isl_basic_set_free(context);
2710 isl_basic_set_free(bset);
2711 return NULL;
2714 /* Project "bset" onto the variables that are involved in "template".
2716 static __isl_give isl_basic_set *project_onto_involved(
2717 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2719 int i, n;
2721 if (!bset || !template)
2722 return isl_basic_set_free(bset);
2724 n = isl_basic_set_dim(template, isl_dim_set);
2726 for (i = 0; i < n; ++i) {
2727 isl_bool involved;
2729 involved = isl_basic_set_involves_dims(template,
2730 isl_dim_set, i, 1);
2731 if (involved < 0)
2732 return isl_basic_set_free(bset);
2733 if (involved)
2734 continue;
2735 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2738 return bset;
2741 /* Remove all information from bset that is redundant in the context
2742 * of context. In particular, equalities that are linear combinations
2743 * of those in context are removed. Then the inequalities that are
2744 * redundant in the context of the equalities and inequalities of
2745 * context are removed.
2747 * First of all, we drop those constraints from "context"
2748 * that are irrelevant for computing the gist of "bset".
2749 * Alternatively, we could factorize the intersection of "context" and "bset".
2751 * We first compute the intersection of the integer affine hulls
2752 * of "bset" and "context",
2753 * compute the gist inside this intersection and then reduce
2754 * the constraints with respect to the equalities of the context
2755 * that only involve variables already involved in the input.
2757 * If two constraints are mutually redundant, then uset_gist_full
2758 * will remove the second of those constraints. We therefore first
2759 * sort the constraints so that constraints not involving existentially
2760 * quantified variables are given precedence over those that do.
2761 * We have to perform this sorting before the variable compression,
2762 * because that may effect the order of the variables.
2764 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2765 __isl_take isl_basic_set *context)
2767 isl_mat *eq;
2768 isl_mat *T;
2769 isl_basic_set *aff;
2770 isl_basic_set *aff_context;
2771 unsigned total;
2773 if (!bset || !context)
2774 goto error;
2776 context = drop_irrelevant_constraints(context, bset);
2778 bset = isl_basic_set_detect_equalities(bset);
2779 aff = isl_basic_set_copy(bset);
2780 aff = isl_basic_set_plain_affine_hull(aff);
2781 context = isl_basic_set_detect_equalities(context);
2782 aff_context = isl_basic_set_copy(context);
2783 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2784 aff = isl_basic_set_intersect(aff, aff_context);
2785 if (!aff)
2786 goto error;
2787 if (isl_basic_set_plain_is_empty(aff)) {
2788 isl_basic_set_free(bset);
2789 isl_basic_set_free(context);
2790 return aff;
2792 bset = isl_basic_set_sort_constraints(bset);
2793 if (aff->n_eq == 0) {
2794 isl_basic_set_free(aff);
2795 return uset_gist_uncompressed(bset, context);
2797 total = isl_basic_set_total_dim(bset);
2798 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2799 eq = isl_mat_cow(eq);
2800 T = isl_mat_variable_compression(eq, NULL);
2801 isl_basic_set_free(aff);
2802 if (T && T->n_col == 0) {
2803 isl_mat_free(T);
2804 isl_basic_set_free(context);
2805 return isl_basic_set_set_to_empty(bset);
2808 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2809 aff_context = project_onto_involved(aff_context, bset);
2811 bset = uset_gist_compressed(bset, context, T);
2812 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2814 if (bset) {
2815 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2816 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2819 return bset;
2820 error:
2821 isl_basic_set_free(bset);
2822 isl_basic_set_free(context);
2823 return NULL;
2826 /* Return the number of equality constraints in "bmap" that involve
2827 * local variables. This function assumes that Gaussian elimination
2828 * has been applied to the equality constraints.
2830 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2832 int i;
2833 int total, n_div;
2835 if (!bmap)
2836 return -1;
2838 if (bmap->n_eq == 0)
2839 return 0;
2841 total = isl_basic_map_dim(bmap, isl_dim_all);
2842 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2843 total -= n_div;
2845 for (i = 0; i < bmap->n_eq; ++i)
2846 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2847 n_div) == -1)
2848 return i;
2850 return bmap->n_eq;
2853 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2854 * The constraints are assumed not to involve any local variables.
2856 static __isl_give isl_basic_map *basic_map_from_equalities(
2857 __isl_take isl_space *space, __isl_take isl_mat *eq)
2859 int i, k;
2860 isl_basic_map *bmap = NULL;
2862 if (!space || !eq)
2863 goto error;
2865 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2866 isl_die(isl_space_get_ctx(space), isl_error_internal,
2867 "unexpected number of columns", goto error);
2869 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2870 0, eq->n_row, 0);
2871 for (i = 0; i < eq->n_row; ++i) {
2872 k = isl_basic_map_alloc_equality(bmap);
2873 if (k < 0)
2874 goto error;
2875 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2878 isl_space_free(space);
2879 isl_mat_free(eq);
2880 return bmap;
2881 error:
2882 isl_space_free(space);
2883 isl_mat_free(eq);
2884 isl_basic_map_free(bmap);
2885 return NULL;
2888 /* Construct and return a variable compression based on the equality
2889 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2890 * "n1" is the number of (initial) equality constraints in "bmap1"
2891 * that do involve local variables.
2892 * "n2" is the number of (initial) equality constraints in "bmap2"
2893 * that do involve local variables.
2894 * "total" is the total number of other variables.
2895 * This function assumes that Gaussian elimination
2896 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2897 * such that the equality constraints not involving local variables
2898 * are those that start at "n1" or "n2".
2900 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2901 * then simply compute the compression based on the equality constraints
2902 * in the other basic map.
2903 * Otherwise, combine the equality constraints from both into a new
2904 * basic map such that Gaussian elimination can be applied to this combination
2905 * and then construct a variable compression from the resulting
2906 * equality constraints.
2908 static __isl_give isl_mat *combined_variable_compression(
2909 __isl_keep isl_basic_map *bmap1, int n1,
2910 __isl_keep isl_basic_map *bmap2, int n2, int total)
2912 isl_ctx *ctx;
2913 isl_mat *E1, *E2, *V;
2914 isl_basic_map *bmap;
2916 ctx = isl_basic_map_get_ctx(bmap1);
2917 if (bmap1->n_eq == n1) {
2918 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2919 n2, bmap2->n_eq - n2, 0, 1 + total);
2920 return isl_mat_variable_compression(E2, NULL);
2922 if (bmap2->n_eq == n2) {
2923 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2924 n1, bmap1->n_eq - n1, 0, 1 + total);
2925 return isl_mat_variable_compression(E1, NULL);
2927 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2928 n1, bmap1->n_eq - n1, 0, 1 + total);
2929 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2930 n2, bmap2->n_eq - n2, 0, 1 + total);
2931 E1 = isl_mat_concat(E1, E2);
2932 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2933 bmap = isl_basic_map_gauss(bmap, NULL);
2934 if (!bmap)
2935 return NULL;
2936 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2937 V = isl_mat_variable_compression(E1, NULL);
2938 isl_basic_map_free(bmap);
2940 return V;
2943 /* Extract the stride constraints from "bmap", compressed
2944 * with respect to both the stride constraints in "context" and
2945 * the remaining equality constraints in both "bmap" and "context".
2946 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2947 * "context_n_eq" is the number of (initial) stride constraints in "context".
2949 * Let x be all variables in "bmap" (and "context") other than the local
2950 * variables. First compute a variable compression
2952 * x = V x'
2954 * based on the non-stride equality constraints in "bmap" and "context".
2955 * Consider the stride constraints of "context",
2957 * A(x) + B(y) = 0
2959 * with y the local variables and plug in the variable compression,
2960 * resulting in
2962 * A(V x') + B(y) = 0
2964 * Use these constraints to compute a parameter compression on x'
2966 * x' = T x''
2968 * Now consider the stride constraints of "bmap"
2970 * C(x) + D(y) = 0
2972 * and plug in x = V*T x''.
2973 * That is, return A = [C*V*T D].
2975 static __isl_give isl_mat *extract_compressed_stride_constraints(
2976 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2977 __isl_keep isl_basic_map *context, int context_n_eq)
2979 int total, n_div;
2980 isl_ctx *ctx;
2981 isl_mat *A, *B, *T, *V;
2983 total = isl_basic_map_dim(context, isl_dim_all);
2984 n_div = isl_basic_map_dim(context, isl_dim_div);
2985 total -= n_div;
2987 ctx = isl_basic_map_get_ctx(bmap);
2989 V = combined_variable_compression(bmap, bmap_n_eq,
2990 context, context_n_eq, total);
2992 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2993 B = isl_mat_sub_alloc6(ctx, context->eq,
2994 0, context_n_eq, 1 + total, n_div);
2995 A = isl_mat_product(A, isl_mat_copy(V));
2996 T = isl_mat_parameter_compression_ext(A, B);
2997 T = isl_mat_product(V, T);
2999 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3000 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3002 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3003 0, bmap_n_eq, 0, 1 + total + n_div);
3004 A = isl_mat_product(A, T);
3006 return A;
3009 /* Remove the prime factors from *g that have an exponent that
3010 * is strictly smaller than the exponent in "c".
3011 * All exponents in *g are known to be smaller than or equal
3012 * to those in "c".
3014 * That is, if *g is equal to
3016 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3018 * and "c" is equal to
3020 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3022 * then update *g to
3024 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3025 * p_n^{e_n * (e_n = f_n)}
3027 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3028 * neither does the gcd of *g and c / *g.
3029 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3030 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3031 * Dividing *g by this gcd therefore strictly reduces the exponent
3032 * of the prime factors that need to be removed, while leaving the
3033 * other prime factors untouched.
3034 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3035 * removes all undesired factors, without removing any others.
3037 static void remove_incomplete_powers(isl_int *g, isl_int c)
3039 isl_int t;
3041 isl_int_init(t);
3042 for (;;) {
3043 isl_int_divexact(t, c, *g);
3044 isl_int_gcd(t, t, *g);
3045 if (isl_int_is_one(t))
3046 break;
3047 isl_int_divexact(*g, *g, t);
3049 isl_int_clear(t);
3052 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3053 * of the same stride constraints in a compressed space that exploits
3054 * all equalities in the context and the other equalities in "bmap".
3056 * If the stride constraints of "bmap" are of the form
3058 * C(x) + D(y) = 0
3060 * then A is of the form
3062 * B(x') + D(y) = 0
3064 * If any of these constraints involves only a single local variable y,
3065 * then the constraint appears as
3067 * f(x) + m y_i = 0
3069 * in "bmap" and as
3071 * h(x') + m y_i = 0
3073 * in "A".
3075 * Let g be the gcd of m and the coefficients of h.
3076 * Then, in particular, g is a divisor of the coefficients of h and
3078 * f(x) = h(x')
3080 * is known to be a multiple of g.
3081 * If some prime factor in m appears with the same exponent in g,
3082 * then it can be removed from m because f(x) is already known
3083 * to be a multiple of g and therefore in particular of this power
3084 * of the prime factors.
3085 * Prime factors that appear with a smaller exponent in g cannot
3086 * be removed from m.
3087 * Let g' be the divisor of g containing all prime factors that
3088 * appear with the same exponent in m and g, then
3090 * f(x) + m y_i = 0
3092 * can be replaced by
3094 * f(x) + m/g' y_i' = 0
3096 * Note that (if g' != 1) this changes the explicit representation
3097 * of y_i to that of y_i', so the integer division at position i
3098 * is marked unknown and later recomputed by a call to
3099 * isl_basic_map_gauss.
3101 static __isl_give isl_basic_map *reduce_stride_constraints(
3102 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3104 int i;
3105 int total, n_div;
3106 int any = 0;
3107 isl_int gcd;
3109 if (!bmap || !A)
3110 return isl_basic_map_free(bmap);
3112 total = isl_basic_map_dim(bmap, isl_dim_all);
3113 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3114 total -= n_div;
3116 isl_int_init(gcd);
3117 for (i = 0; i < n; ++i) {
3118 int div;
3120 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3121 if (div < 0)
3122 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3123 "equality constraints modified unexpectedly",
3124 goto error);
3125 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3126 n_div - div - 1) != -1)
3127 continue;
3128 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3129 goto error;
3130 if (isl_int_is_one(gcd))
3131 continue;
3132 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3133 if (isl_int_is_one(gcd))
3134 continue;
3135 isl_int_divexact(bmap->eq[i][1 + total + div],
3136 bmap->eq[i][1 + total + div], gcd);
3137 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3138 if (!bmap)
3139 goto error;
3140 any = 1;
3142 isl_int_clear(gcd);
3144 if (any)
3145 bmap = isl_basic_map_gauss(bmap, NULL);
3147 return bmap;
3148 error:
3149 isl_int_clear(gcd);
3150 isl_basic_map_free(bmap);
3151 return NULL;
3154 /* Simplify the stride constraints in "bmap" based on
3155 * the remaining equality constraints in "bmap" and all equality
3156 * constraints in "context".
3157 * Only do this if both "bmap" and "context" have stride constraints.
3159 * First extract a copy of the stride constraints in "bmap" in a compressed
3160 * space exploiting all the other equality constraints and then
3161 * use this compressed copy to simplify the original stride constraints.
3163 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3164 __isl_keep isl_basic_map *context)
3166 int bmap_n_eq, context_n_eq;
3167 isl_mat *A;
3169 if (!bmap || !context)
3170 return isl_basic_map_free(bmap);
3172 bmap_n_eq = n_div_eq(bmap);
3173 context_n_eq = n_div_eq(context);
3175 if (bmap_n_eq < 0 || context_n_eq < 0)
3176 return isl_basic_map_free(bmap);
3177 if (bmap_n_eq == 0 || context_n_eq == 0)
3178 return bmap;
3180 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3181 context, context_n_eq);
3182 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3184 isl_mat_free(A);
3186 return bmap;
3189 /* Return a basic map that has the same intersection with "context" as "bmap"
3190 * and that is as "simple" as possible.
3192 * The core computation is performed on the pure constraints.
3193 * When we add back the meaning of the integer divisions, we need
3194 * to (re)introduce the div constraints. If we happen to have
3195 * discovered that some of these integer divisions are equal to
3196 * some affine combination of other variables, then these div
3197 * constraints may end up getting simplified in terms of the equalities,
3198 * resulting in extra inequalities on the other variables that
3199 * may have been removed already or that may not even have been
3200 * part of the input. We try and remove those constraints of
3201 * this form that are most obviously redundant with respect to
3202 * the context. We also remove those div constraints that are
3203 * redundant with respect to the other constraints in the result.
3205 * The stride constraints among the equality constraints in "bmap" are
3206 * also simplified with respecting to the other equality constraints
3207 * in "bmap" and with respect to all equality constraints in "context".
3209 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3210 struct isl_basic_map *context)
3212 isl_basic_set *bset, *eq;
3213 isl_basic_map *eq_bmap;
3214 unsigned total, n_div, extra, n_eq, n_ineq;
3216 if (!bmap || !context)
3217 goto error;
3219 if (isl_basic_map_plain_is_universe(bmap)) {
3220 isl_basic_map_free(context);
3221 return bmap;
3223 if (isl_basic_map_plain_is_empty(context)) {
3224 isl_space *space = isl_basic_map_get_space(bmap);
3225 isl_basic_map_free(bmap);
3226 isl_basic_map_free(context);
3227 return isl_basic_map_universe(space);
3229 if (isl_basic_map_plain_is_empty(bmap)) {
3230 isl_basic_map_free(context);
3231 return bmap;
3234 bmap = isl_basic_map_remove_redundancies(bmap);
3235 context = isl_basic_map_remove_redundancies(context);
3236 if (!context)
3237 goto error;
3239 context = isl_basic_map_align_divs(context, bmap);
3240 n_div = isl_basic_map_dim(context, isl_dim_div);
3241 total = isl_basic_map_dim(bmap, isl_dim_all);
3242 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3244 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3245 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3246 bset = uset_gist(bset,
3247 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3248 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3250 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3251 isl_basic_set_plain_is_empty(bset)) {
3252 isl_basic_map_free(context);
3253 return isl_basic_map_overlying_set(bset, bmap);
3256 n_eq = bset->n_eq;
3257 n_ineq = bset->n_ineq;
3258 eq = isl_basic_set_copy(bset);
3259 eq = isl_basic_set_cow(eq);
3260 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3261 eq = isl_basic_set_free(eq);
3262 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3263 bset = isl_basic_set_free(bset);
3265 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3266 eq_bmap = gist_strides(eq_bmap, context);
3267 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3268 bmap = isl_basic_map_overlying_set(bset, bmap);
3269 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3270 bmap = isl_basic_map_remove_redundancies(bmap);
3272 return bmap;
3273 error:
3274 isl_basic_map_free(bmap);
3275 isl_basic_map_free(context);
3276 return NULL;
3280 * Assumes context has no implicit divs.
3282 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3283 __isl_take isl_basic_map *context)
3285 int i;
3287 if (!map || !context)
3288 goto error;
3290 if (isl_basic_map_plain_is_empty(context)) {
3291 isl_space *space = isl_map_get_space(map);
3292 isl_map_free(map);
3293 isl_basic_map_free(context);
3294 return isl_map_universe(space);
3297 context = isl_basic_map_remove_redundancies(context);
3298 map = isl_map_cow(map);
3299 if (!map || !context)
3300 goto error;
3301 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3302 map = isl_map_compute_divs(map);
3303 if (!map)
3304 goto error;
3305 for (i = map->n - 1; i >= 0; --i) {
3306 map->p[i] = isl_basic_map_gist(map->p[i],
3307 isl_basic_map_copy(context));
3308 if (!map->p[i])
3309 goto error;
3310 if (isl_basic_map_plain_is_empty(map->p[i])) {
3311 isl_basic_map_free(map->p[i]);
3312 if (i != map->n - 1)
3313 map->p[i] = map->p[map->n - 1];
3314 map->n--;
3317 isl_basic_map_free(context);
3318 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3319 return map;
3320 error:
3321 isl_map_free(map);
3322 isl_basic_map_free(context);
3323 return NULL;
3326 /* Drop all inequalities from "bmap" that also appear in "context".
3327 * "context" is assumed to have only known local variables and
3328 * the initial local variables of "bmap" are assumed to be the same
3329 * as those of "context".
3330 * The constraints of both "bmap" and "context" are assumed
3331 * to have been sorted using isl_basic_map_sort_constraints.
3333 * Run through the inequality constraints of "bmap" and "context"
3334 * in sorted order.
3335 * If a constraint of "bmap" involves variables not in "context",
3336 * then it cannot appear in "context".
3337 * If a matching constraint is found, it is removed from "bmap".
3339 static __isl_give isl_basic_map *drop_inequalities(
3340 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3342 int i1, i2;
3343 unsigned total, extra;
3345 if (!bmap || !context)
3346 return isl_basic_map_free(bmap);
3348 total = isl_basic_map_total_dim(context);
3349 extra = isl_basic_map_total_dim(bmap) - total;
3351 i1 = bmap->n_ineq - 1;
3352 i2 = context->n_ineq - 1;
3353 while (bmap && i1 >= 0 && i2 >= 0) {
3354 int cmp;
3356 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3357 extra) != -1) {
3358 --i1;
3359 continue;
3361 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3362 context->ineq[i2]);
3363 if (cmp < 0) {
3364 --i2;
3365 continue;
3367 if (cmp > 0) {
3368 --i1;
3369 continue;
3371 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3372 bmap = isl_basic_map_cow(bmap);
3373 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3374 bmap = isl_basic_map_free(bmap);
3376 --i1;
3377 --i2;
3380 return bmap;
3383 /* Drop all equalities from "bmap" that also appear in "context".
3384 * "context" is assumed to have only known local variables and
3385 * the initial local variables of "bmap" are assumed to be the same
3386 * as those of "context".
3388 * Run through the equality constraints of "bmap" and "context"
3389 * in sorted order.
3390 * If a constraint of "bmap" involves variables not in "context",
3391 * then it cannot appear in "context".
3392 * If a matching constraint is found, it is removed from "bmap".
3394 static __isl_give isl_basic_map *drop_equalities(
3395 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3397 int i1, i2;
3398 unsigned total, extra;
3400 if (!bmap || !context)
3401 return isl_basic_map_free(bmap);
3403 total = isl_basic_map_total_dim(context);
3404 extra = isl_basic_map_total_dim(bmap) - total;
3406 i1 = bmap->n_eq - 1;
3407 i2 = context->n_eq - 1;
3409 while (bmap && i1 >= 0 && i2 >= 0) {
3410 int last1, last2;
3412 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3413 extra) != -1)
3414 break;
3415 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3416 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3417 if (last1 > last2) {
3418 --i2;
3419 continue;
3421 if (last1 < last2) {
3422 --i1;
3423 continue;
3425 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3426 bmap = isl_basic_map_cow(bmap);
3427 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3428 bmap = isl_basic_map_free(bmap);
3430 --i1;
3431 --i2;
3434 return bmap;
3437 /* Remove the constraints in "context" from "bmap".
3438 * "context" is assumed to have explicit representations
3439 * for all local variables.
3441 * First align the divs of "bmap" to those of "context" and
3442 * sort the constraints. Then drop all constraints from "bmap"
3443 * that appear in "context".
3445 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3446 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3448 isl_bool done, known;
3450 done = isl_basic_map_plain_is_universe(context);
3451 if (done == isl_bool_false)
3452 done = isl_basic_map_plain_is_universe(bmap);
3453 if (done == isl_bool_false)
3454 done = isl_basic_map_plain_is_empty(context);
3455 if (done == isl_bool_false)
3456 done = isl_basic_map_plain_is_empty(bmap);
3457 if (done < 0)
3458 goto error;
3459 if (done) {
3460 isl_basic_map_free(context);
3461 return bmap;
3463 known = isl_basic_map_divs_known(context);
3464 if (known < 0)
3465 goto error;
3466 if (!known)
3467 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3468 "context has unknown divs", goto error);
3470 bmap = isl_basic_map_align_divs(bmap, context);
3471 bmap = isl_basic_map_gauss(bmap, NULL);
3472 bmap = isl_basic_map_sort_constraints(bmap);
3473 context = isl_basic_map_sort_constraints(context);
3475 bmap = drop_inequalities(bmap, context);
3476 bmap = drop_equalities(bmap, context);
3478 isl_basic_map_free(context);
3479 bmap = isl_basic_map_finalize(bmap);
3480 return bmap;
3481 error:
3482 isl_basic_map_free(bmap);
3483 isl_basic_map_free(context);
3484 return NULL;
3487 /* Replace "map" by the disjunct at position "pos" and free "context".
3489 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3490 int pos, __isl_take isl_basic_map *context)
3492 isl_basic_map *bmap;
3494 bmap = isl_basic_map_copy(map->p[pos]);
3495 isl_map_free(map);
3496 isl_basic_map_free(context);
3497 return isl_map_from_basic_map(bmap);
3500 /* Remove the constraints in "context" from "map".
3501 * If any of the disjuncts in the result turns out to be the universe,
3502 * then return this universe.
3503 * "context" is assumed to have explicit representations
3504 * for all local variables.
3506 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3507 __isl_take isl_basic_map *context)
3509 int i;
3510 isl_bool univ, known;
3512 univ = isl_basic_map_plain_is_universe(context);
3513 if (univ < 0)
3514 goto error;
3515 if (univ) {
3516 isl_basic_map_free(context);
3517 return map;
3519 known = isl_basic_map_divs_known(context);
3520 if (known < 0)
3521 goto error;
3522 if (!known)
3523 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3524 "context has unknown divs", goto error);
3526 map = isl_map_cow(map);
3527 if (!map)
3528 goto error;
3529 for (i = 0; i < map->n; ++i) {
3530 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3531 isl_basic_map_copy(context));
3532 univ = isl_basic_map_plain_is_universe(map->p[i]);
3533 if (univ < 0)
3534 goto error;
3535 if (univ && map->n > 1)
3536 return replace_by_disjunct(map, i, context);
3539 isl_basic_map_free(context);
3540 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3541 if (map->n > 1)
3542 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3543 return map;
3544 error:
3545 isl_map_free(map);
3546 isl_basic_map_free(context);
3547 return NULL;
3550 /* Replace "map" by a universe map in the same space and free "drop".
3552 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3553 __isl_take isl_map *drop)
3555 isl_map *res;
3557 res = isl_map_universe(isl_map_get_space(map));
3558 isl_map_free(map);
3559 isl_map_free(drop);
3560 return res;
3563 /* Return a map that has the same intersection with "context" as "map"
3564 * and that is as "simple" as possible.
3566 * If "map" is already the universe, then we cannot make it any simpler.
3567 * Similarly, if "context" is the universe, then we cannot exploit it
3568 * to simplify "map"
3569 * If "map" and "context" are identical to each other, then we can
3570 * return the corresponding universe.
3572 * If either "map" or "context" consists of multiple disjuncts,
3573 * then check if "context" happens to be a subset of "map",
3574 * in which case all constraints can be removed.
3575 * In case of multiple disjuncts, the standard procedure
3576 * may not be able to detect that all constraints can be removed.
3578 * If none of these cases apply, we have to work a bit harder.
3579 * During this computation, we make use of a single disjunct context,
3580 * so if the original context consists of more than one disjunct
3581 * then we need to approximate the context by a single disjunct set.
3582 * Simply taking the simple hull may drop constraints that are
3583 * only implicitly available in each disjunct. We therefore also
3584 * look for constraints among those defining "map" that are valid
3585 * for the context. These can then be used to simplify away
3586 * the corresponding constraints in "map".
3588 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3589 __isl_take isl_map *context)
3591 int equal;
3592 int is_universe;
3593 int single_disjunct_map, single_disjunct_context;
3594 isl_bool subset;
3595 isl_basic_map *hull;
3597 is_universe = isl_map_plain_is_universe(map);
3598 if (is_universe >= 0 && !is_universe)
3599 is_universe = isl_map_plain_is_universe(context);
3600 if (is_universe < 0)
3601 goto error;
3602 if (is_universe) {
3603 isl_map_free(context);
3604 return map;
3607 equal = isl_map_plain_is_equal(map, context);
3608 if (equal < 0)
3609 goto error;
3610 if (equal)
3611 return replace_by_universe(map, context);
3613 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3614 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3615 if (!single_disjunct_map || !single_disjunct_context) {
3616 subset = isl_map_is_subset(context, map);
3617 if (subset < 0)
3618 goto error;
3619 if (subset)
3620 return replace_by_universe(map, context);
3623 context = isl_map_compute_divs(context);
3624 if (!context)
3625 goto error;
3626 if (single_disjunct_context) {
3627 hull = isl_map_simple_hull(context);
3628 } else {
3629 isl_ctx *ctx;
3630 isl_map_list *list;
3632 ctx = isl_map_get_ctx(map);
3633 list = isl_map_list_alloc(ctx, 2);
3634 list = isl_map_list_add(list, isl_map_copy(context));
3635 list = isl_map_list_add(list, isl_map_copy(map));
3636 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3637 list);
3639 return isl_map_gist_basic_map(map, hull);
3640 error:
3641 isl_map_free(map);
3642 isl_map_free(context);
3643 return NULL;
3646 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3647 __isl_take isl_map *context)
3649 return isl_map_align_params_map_map_and(map, context, &map_gist);
3652 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3653 struct isl_basic_set *context)
3655 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3656 bset_to_bmap(context)));
3659 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3660 __isl_take isl_basic_set *context)
3662 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3663 bset_to_bmap(context)));
3666 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3667 __isl_take isl_basic_set *context)
3669 isl_space *space = isl_set_get_space(set);
3670 isl_basic_set *dom_context = isl_basic_set_universe(space);
3671 dom_context = isl_basic_set_intersect_params(dom_context, context);
3672 return isl_set_gist_basic_set(set, dom_context);
3675 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3676 __isl_take isl_set *context)
3678 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3681 /* Compute the gist of "bmap" with respect to the constraints "context"
3682 * on the domain.
3684 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3685 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3687 isl_space *space = isl_basic_map_get_space(bmap);
3688 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3690 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3691 return isl_basic_map_gist(bmap, bmap_context);
3694 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3695 __isl_take isl_set *context)
3697 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3698 map_context = isl_map_intersect_domain(map_context, context);
3699 return isl_map_gist(map, map_context);
3702 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3703 __isl_take isl_set *context)
3705 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3706 map_context = isl_map_intersect_range(map_context, context);
3707 return isl_map_gist(map, map_context);
3710 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3711 __isl_take isl_set *context)
3713 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3714 map_context = isl_map_intersect_params(map_context, context);
3715 return isl_map_gist(map, map_context);
3718 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3719 __isl_take isl_set *context)
3721 return isl_map_gist_params(set, context);
3724 /* Quick check to see if two basic maps are disjoint.
3725 * In particular, we reduce the equalities and inequalities of
3726 * one basic map in the context of the equalities of the other
3727 * basic map and check if we get a contradiction.
3729 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3730 __isl_keep isl_basic_map *bmap2)
3732 struct isl_vec *v = NULL;
3733 int *elim = NULL;
3734 unsigned total;
3735 int i;
3737 if (!bmap1 || !bmap2)
3738 return isl_bool_error;
3739 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3740 return isl_bool_error);
3741 if (bmap1->n_div || bmap2->n_div)
3742 return isl_bool_false;
3743 if (!bmap1->n_eq && !bmap2->n_eq)
3744 return isl_bool_false;
3746 total = isl_space_dim(bmap1->dim, isl_dim_all);
3747 if (total == 0)
3748 return isl_bool_false;
3749 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3750 if (!v)
3751 goto error;
3752 elim = isl_alloc_array(bmap1->ctx, int, total);
3753 if (!elim)
3754 goto error;
3755 compute_elimination_index(bmap1, elim);
3756 for (i = 0; i < bmap2->n_eq; ++i) {
3757 int reduced;
3758 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3759 bmap1, elim);
3760 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3761 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3762 goto disjoint;
3764 for (i = 0; i < bmap2->n_ineq; ++i) {
3765 int reduced;
3766 reduced = reduced_using_equalities(v->block.data,
3767 bmap2->ineq[i], bmap1, elim);
3768 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3769 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3770 goto disjoint;
3772 compute_elimination_index(bmap2, elim);
3773 for (i = 0; i < bmap1->n_ineq; ++i) {
3774 int reduced;
3775 reduced = reduced_using_equalities(v->block.data,
3776 bmap1->ineq[i], bmap2, elim);
3777 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3778 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3779 goto disjoint;
3781 isl_vec_free(v);
3782 free(elim);
3783 return isl_bool_false;
3784 disjoint:
3785 isl_vec_free(v);
3786 free(elim);
3787 return isl_bool_true;
3788 error:
3789 isl_vec_free(v);
3790 free(elim);
3791 return isl_bool_error;
3794 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3795 __isl_keep isl_basic_set *bset2)
3797 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3798 bset_to_bmap(bset2));
3801 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3803 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3804 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3805 __isl_keep isl_basic_map *bmap2))
3807 int i, j;
3809 if (!map1 || !map2)
3810 return isl_bool_error;
3812 for (i = 0; i < map1->n; ++i) {
3813 for (j = 0; j < map2->n; ++j) {
3814 isl_bool d = test(map1->p[i], map2->p[j]);
3815 if (d != isl_bool_true)
3816 return d;
3820 return isl_bool_true;
3823 /* Are "map1" and "map2" obviously disjoint, based on information
3824 * that can be derived without looking at the individual basic maps?
3826 * In particular, if one of them is empty or if they live in different spaces
3827 * (ignoring parameters), then they are clearly disjoint.
3829 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3830 __isl_keep isl_map *map2)
3832 isl_bool disjoint;
3833 isl_bool match;
3835 if (!map1 || !map2)
3836 return isl_bool_error;
3838 disjoint = isl_map_plain_is_empty(map1);
3839 if (disjoint < 0 || disjoint)
3840 return disjoint;
3842 disjoint = isl_map_plain_is_empty(map2);
3843 if (disjoint < 0 || disjoint)
3844 return disjoint;
3846 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3847 map2->dim, isl_dim_in);
3848 if (match < 0 || !match)
3849 return match < 0 ? isl_bool_error : isl_bool_true;
3851 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3852 map2->dim, isl_dim_out);
3853 if (match < 0 || !match)
3854 return match < 0 ? isl_bool_error : isl_bool_true;
3856 return isl_bool_false;
3859 /* Are "map1" and "map2" obviously disjoint?
3861 * If one of them is empty or if they live in different spaces (ignoring
3862 * parameters), then they are clearly disjoint.
3863 * This is checked by isl_map_plain_is_disjoint_global.
3865 * If they have different parameters, then we skip any further tests.
3867 * If they are obviously equal, but not obviously empty, then we will
3868 * not be able to detect if they are disjoint.
3870 * Otherwise we check if each basic map in "map1" is obviously disjoint
3871 * from each basic map in "map2".
3873 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3874 __isl_keep isl_map *map2)
3876 isl_bool disjoint;
3877 isl_bool intersect;
3878 isl_bool match;
3880 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3881 if (disjoint < 0 || disjoint)
3882 return disjoint;
3884 match = isl_space_match(map1->dim, isl_dim_param,
3885 map2->dim, isl_dim_param);
3886 if (match < 0 || !match)
3887 return match < 0 ? isl_bool_error : isl_bool_false;
3889 intersect = isl_map_plain_is_equal(map1, map2);
3890 if (intersect < 0 || intersect)
3891 return intersect < 0 ? isl_bool_error : isl_bool_false;
3893 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3896 /* Are "map1" and "map2" disjoint?
3897 * The parameters are assumed to have been aligned.
3899 * In particular, check whether all pairs of basic maps are disjoint.
3901 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3902 __isl_keep isl_map *map2)
3904 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3907 /* Are "map1" and "map2" disjoint?
3909 * They are disjoint if they are "obviously disjoint" or if one of them
3910 * is empty. Otherwise, they are not disjoint if one of them is universal.
3911 * If the two inputs are (obviously) equal and not empty, then they are
3912 * not disjoint.
3913 * If none of these cases apply, then check if all pairs of basic maps
3914 * are disjoint after aligning the parameters.
3916 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3918 isl_bool disjoint;
3919 isl_bool intersect;
3921 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3922 if (disjoint < 0 || disjoint)
3923 return disjoint;
3925 disjoint = isl_map_is_empty(map1);
3926 if (disjoint < 0 || disjoint)
3927 return disjoint;
3929 disjoint = isl_map_is_empty(map2);
3930 if (disjoint < 0 || disjoint)
3931 return disjoint;
3933 intersect = isl_map_plain_is_universe(map1);
3934 if (intersect < 0 || intersect)
3935 return intersect < 0 ? isl_bool_error : isl_bool_false;
3937 intersect = isl_map_plain_is_universe(map2);
3938 if (intersect < 0 || intersect)
3939 return intersect < 0 ? isl_bool_error : isl_bool_false;
3941 intersect = isl_map_plain_is_equal(map1, map2);
3942 if (intersect < 0 || intersect)
3943 return isl_bool_not(intersect);
3945 return isl_map_align_params_map_map_and_test(map1, map2,
3946 &isl_map_is_disjoint_aligned);
3949 /* Are "bmap1" and "bmap2" disjoint?
3951 * They are disjoint if they are "obviously disjoint" or if one of them
3952 * is empty. Otherwise, they are not disjoint if one of them is universal.
3953 * If none of these cases apply, we compute the intersection and see if
3954 * the result is empty.
3956 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3957 __isl_keep isl_basic_map *bmap2)
3959 isl_bool disjoint;
3960 isl_bool intersect;
3961 isl_basic_map *test;
3963 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3964 if (disjoint < 0 || disjoint)
3965 return disjoint;
3967 disjoint = isl_basic_map_is_empty(bmap1);
3968 if (disjoint < 0 || disjoint)
3969 return disjoint;
3971 disjoint = isl_basic_map_is_empty(bmap2);
3972 if (disjoint < 0 || disjoint)
3973 return disjoint;
3975 intersect = isl_basic_map_plain_is_universe(bmap1);
3976 if (intersect < 0 || intersect)
3977 return intersect < 0 ? isl_bool_error : isl_bool_false;
3979 intersect = isl_basic_map_plain_is_universe(bmap2);
3980 if (intersect < 0 || intersect)
3981 return intersect < 0 ? isl_bool_error : isl_bool_false;
3983 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3984 isl_basic_map_copy(bmap2));
3985 disjoint = isl_basic_map_is_empty(test);
3986 isl_basic_map_free(test);
3988 return disjoint;
3991 /* Are "bset1" and "bset2" disjoint?
3993 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3994 __isl_keep isl_basic_set *bset2)
3996 return isl_basic_map_is_disjoint(bset1, bset2);
3999 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4000 __isl_keep isl_set *set2)
4002 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4005 /* Are "set1" and "set2" disjoint?
4007 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4009 return isl_map_is_disjoint(set1, set2);
4012 /* Is "v" equal to 0, 1 or -1?
4014 static int is_zero_or_one(isl_int v)
4016 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4019 /* Check if we can combine a given div with lower bound l and upper
4020 * bound u with some other div and if so return that other div.
4021 * Otherwise return -1.
4023 * We first check that
4024 * - the bounds are opposites of each other (except for the constant
4025 * term)
4026 * - the bounds do not reference any other div
4027 * - no div is defined in terms of this div
4029 * Let m be the size of the range allowed on the div by the bounds.
4030 * That is, the bounds are of the form
4032 * e <= a <= e + m - 1
4034 * with e some expression in the other variables.
4035 * We look for another div b such that no third div is defined in terms
4036 * of this second div b and such that in any constraint that contains
4037 * a (except for the given lower and upper bound), also contains b
4038 * with a coefficient that is m times that of b.
4039 * That is, all constraints (execpt for the lower and upper bound)
4040 * are of the form
4042 * e + f (a + m b) >= 0
4044 * Furthermore, in the constraints that only contain b, the coefficient
4045 * of b should be equal to 1 or -1.
4046 * If so, we return b so that "a + m b" can be replaced by
4047 * a single div "c = a + m b".
4049 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4050 unsigned div, unsigned l, unsigned u)
4052 int i, j;
4053 unsigned dim;
4054 int coalesce = -1;
4056 if (bmap->n_div <= 1)
4057 return -1;
4058 dim = isl_space_dim(bmap->dim, isl_dim_all);
4059 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4060 return -1;
4061 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4062 bmap->n_div - div - 1) != -1)
4063 return -1;
4064 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4065 dim + bmap->n_div))
4066 return -1;
4068 for (i = 0; i < bmap->n_div; ++i) {
4069 if (isl_int_is_zero(bmap->div[i][0]))
4070 continue;
4071 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4072 return -1;
4075 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4076 if (isl_int_is_neg(bmap->ineq[l][0])) {
4077 isl_int_sub(bmap->ineq[l][0],
4078 bmap->ineq[l][0], bmap->ineq[u][0]);
4079 bmap = isl_basic_map_copy(bmap);
4080 bmap = isl_basic_map_set_to_empty(bmap);
4081 isl_basic_map_free(bmap);
4082 return -1;
4084 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4085 for (i = 0; i < bmap->n_div; ++i) {
4086 if (i == div)
4087 continue;
4088 if (!pairs[i])
4089 continue;
4090 for (j = 0; j < bmap->n_div; ++j) {
4091 if (isl_int_is_zero(bmap->div[j][0]))
4092 continue;
4093 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4094 break;
4096 if (j < bmap->n_div)
4097 continue;
4098 for (j = 0; j < bmap->n_ineq; ++j) {
4099 int valid;
4100 if (j == l || j == u)
4101 continue;
4102 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4103 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4104 continue;
4105 break;
4107 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4108 break;
4109 isl_int_mul(bmap->ineq[j][1 + dim + div],
4110 bmap->ineq[j][1 + dim + div],
4111 bmap->ineq[l][0]);
4112 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4113 bmap->ineq[j][1 + dim + i]);
4114 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4115 bmap->ineq[j][1 + dim + div],
4116 bmap->ineq[l][0]);
4117 if (!valid)
4118 break;
4120 if (j < bmap->n_ineq)
4121 continue;
4122 coalesce = i;
4123 break;
4125 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4126 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4127 return coalesce;
4130 /* Internal data structure used during the construction and/or evaluation of
4131 * an inequality that ensures that a pair of bounds always allows
4132 * for an integer value.
4134 * "tab" is the tableau in which the inequality is evaluated. It may
4135 * be NULL until it is actually needed.
4136 * "v" contains the inequality coefficients.
4137 * "g", "fl" and "fu" are temporary scalars used during the construction and
4138 * evaluation.
4140 struct test_ineq_data {
4141 struct isl_tab *tab;
4142 isl_vec *v;
4143 isl_int g;
4144 isl_int fl;
4145 isl_int fu;
4148 /* Free all the memory allocated by the fields of "data".
4150 static void test_ineq_data_clear(struct test_ineq_data *data)
4152 isl_tab_free(data->tab);
4153 isl_vec_free(data->v);
4154 isl_int_clear(data->g);
4155 isl_int_clear(data->fl);
4156 isl_int_clear(data->fu);
4159 /* Is the inequality stored in data->v satisfied by "bmap"?
4160 * That is, does it only attain non-negative values?
4161 * data->tab is a tableau corresponding to "bmap".
4163 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4164 struct test_ineq_data *data)
4166 isl_ctx *ctx;
4167 enum isl_lp_result res;
4169 ctx = isl_basic_map_get_ctx(bmap);
4170 if (!data->tab)
4171 data->tab = isl_tab_from_basic_map(bmap, 0);
4172 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4173 if (res == isl_lp_error)
4174 return isl_bool_error;
4175 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4178 /* Given a lower and an upper bound on div i, do they always allow
4179 * for an integer value of the given div?
4180 * Determine this property by constructing an inequality
4181 * such that the property is guaranteed when the inequality is nonnegative.
4182 * The lower bound is inequality l, while the upper bound is inequality u.
4183 * The constructed inequality is stored in data->v.
4185 * Let the upper bound be
4187 * -n_u a + e_u >= 0
4189 * and the lower bound
4191 * n_l a + e_l >= 0
4193 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4194 * We have
4196 * - f_u e_l <= f_u f_l g a <= f_l e_u
4198 * Since all variables are integer valued, this is equivalent to
4200 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4202 * If this interval is at least f_u f_l g, then it contains at least
4203 * one integer value for a.
4204 * That is, the test constraint is
4206 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4208 * or
4210 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4212 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4213 * then the constraint can be scaled down by a factor g',
4214 * with the constant term replaced by
4215 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4216 * Note that the result of applying Fourier-Motzkin to this pair
4217 * of constraints is
4219 * f_l e_u + f_u e_l >= 0
4221 * If the constant term of the scaled down version of this constraint,
4222 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4223 * term of the scaled down test constraint, then the test constraint
4224 * is known to hold and no explicit evaluation is required.
4225 * This is essentially the Omega test.
4227 * If the test constraint consists of only a constant term, then
4228 * it is sufficient to look at the sign of this constant term.
4230 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4231 int l, int u, struct test_ineq_data *data)
4233 unsigned offset, n_div;
4234 offset = isl_basic_map_offset(bmap, isl_dim_div);
4235 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4237 isl_int_gcd(data->g,
4238 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4239 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4240 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4241 isl_int_neg(data->fu, data->fu);
4242 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4243 data->fu, bmap->ineq[l], offset + n_div);
4244 isl_int_mul(data->g, data->g, data->fl);
4245 isl_int_mul(data->g, data->g, data->fu);
4246 isl_int_sub(data->g, data->g, data->fl);
4247 isl_int_sub(data->g, data->g, data->fu);
4248 isl_int_add_ui(data->g, data->g, 1);
4249 isl_int_sub(data->fl, data->v->el[0], data->g);
4251 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4252 if (isl_int_is_zero(data->g))
4253 return isl_int_is_nonneg(data->fl);
4254 if (isl_int_is_one(data->g)) {
4255 isl_int_set(data->v->el[0], data->fl);
4256 return test_ineq_is_satisfied(bmap, data);
4258 isl_int_fdiv_q(data->fl, data->fl, data->g);
4259 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4260 if (isl_int_eq(data->fl, data->v->el[0]))
4261 return isl_bool_true;
4262 isl_int_set(data->v->el[0], data->fl);
4263 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4264 offset - 1 + n_div);
4266 return test_ineq_is_satisfied(bmap, data);
4269 /* Remove more kinds of divs that are not strictly needed.
4270 * In particular, if all pairs of lower and upper bounds on a div
4271 * are such that they allow at least one integer value of the div,
4272 * then we can eliminate the div using Fourier-Motzkin without
4273 * introducing any spurious solutions.
4275 * If at least one of the two constraints has a unit coefficient for the div,
4276 * then the presence of such a value is guaranteed so there is no need to check.
4277 * In particular, the value attained by the bound with unit coefficient
4278 * can serve as this intermediate value.
4280 static struct isl_basic_map *drop_more_redundant_divs(
4281 struct isl_basic_map *bmap, int *pairs, int n)
4283 isl_ctx *ctx;
4284 struct test_ineq_data data = { NULL, NULL };
4285 unsigned off, n_div;
4286 int remove = -1;
4288 isl_int_init(data.g);
4289 isl_int_init(data.fl);
4290 isl_int_init(data.fu);
4292 if (!bmap)
4293 goto error;
4295 ctx = isl_basic_map_get_ctx(bmap);
4296 off = isl_basic_map_offset(bmap, isl_dim_div);
4297 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4298 data.v = isl_vec_alloc(ctx, off + n_div);
4299 if (!data.v)
4300 goto error;
4302 while (n > 0) {
4303 int i, l, u;
4304 int best = -1;
4305 isl_bool has_int;
4307 for (i = 0; i < n_div; ++i) {
4308 if (!pairs[i])
4309 continue;
4310 if (best >= 0 && pairs[best] <= pairs[i])
4311 continue;
4312 best = i;
4315 i = best;
4316 for (l = 0; l < bmap->n_ineq; ++l) {
4317 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4318 continue;
4319 if (isl_int_is_one(bmap->ineq[l][off + i]))
4320 continue;
4321 for (u = 0; u < bmap->n_ineq; ++u) {
4322 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4323 continue;
4324 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4325 continue;
4326 has_int = int_between_bounds(bmap, i, l, u,
4327 &data);
4328 if (has_int < 0)
4329 goto error;
4330 if (data.tab && data.tab->empty)
4331 break;
4332 if (!has_int)
4333 break;
4335 if (u < bmap->n_ineq)
4336 break;
4338 if (data.tab && data.tab->empty) {
4339 bmap = isl_basic_map_set_to_empty(bmap);
4340 break;
4342 if (l == bmap->n_ineq) {
4343 remove = i;
4344 break;
4346 pairs[i] = 0;
4347 --n;
4350 test_ineq_data_clear(&data);
4352 free(pairs);
4354 if (remove < 0)
4355 return bmap;
4357 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4358 return isl_basic_map_drop_redundant_divs(bmap);
4359 error:
4360 free(pairs);
4361 isl_basic_map_free(bmap);
4362 test_ineq_data_clear(&data);
4363 return NULL;
4366 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4367 * and the upper bound u, div1 always occurs together with div2 in the form
4368 * (div1 + m div2), where m is the constant range on the variable div1
4369 * allowed by l and u, replace the pair div1 and div2 by a single
4370 * div that is equal to div1 + m div2.
4372 * The new div will appear in the location that contains div2.
4373 * We need to modify all constraints that contain
4374 * div2 = (div - div1) / m
4375 * The coefficient of div2 is known to be equal to 1 or -1.
4376 * (If a constraint does not contain div2, it will also not contain div1.)
4377 * If the constraint also contains div1, then we know they appear
4378 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4379 * i.e., the coefficient of div is f.
4381 * Otherwise, we first need to introduce div1 into the constraint.
4382 * Let the l be
4384 * div1 + f >=0
4386 * and u
4388 * -div1 + f' >= 0
4390 * A lower bound on div2
4392 * div2 + t >= 0
4394 * can be replaced by
4396 * m div2 + div1 + m t + f >= 0
4398 * An upper bound
4400 * -div2 + t >= 0
4402 * can be replaced by
4404 * -(m div2 + div1) + m t + f' >= 0
4406 * These constraint are those that we would obtain from eliminating
4407 * div1 using Fourier-Motzkin.
4409 * After all constraints have been modified, we drop the lower and upper
4410 * bound and then drop div1.
4411 * Since the new div is only placed in the same location that used
4412 * to store div2, but otherwise has a different meaning, any possible
4413 * explicit representation of the original div2 is removed.
4415 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4416 unsigned div1, unsigned div2, unsigned l, unsigned u)
4418 isl_ctx *ctx;
4419 isl_int m;
4420 unsigned dim, total;
4421 int i;
4423 ctx = isl_basic_map_get_ctx(bmap);
4425 dim = isl_space_dim(bmap->dim, isl_dim_all);
4426 total = 1 + dim + bmap->n_div;
4428 isl_int_init(m);
4429 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4430 isl_int_add_ui(m, m, 1);
4432 for (i = 0; i < bmap->n_ineq; ++i) {
4433 if (i == l || i == u)
4434 continue;
4435 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4436 continue;
4437 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4438 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4439 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4440 ctx->one, bmap->ineq[l], total);
4441 else
4442 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4443 ctx->one, bmap->ineq[u], total);
4445 isl_int_set(bmap->ineq[i][1 + dim + div2],
4446 bmap->ineq[i][1 + dim + div1]);
4447 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4450 isl_int_clear(m);
4451 if (l > u) {
4452 isl_basic_map_drop_inequality(bmap, l);
4453 isl_basic_map_drop_inequality(bmap, u);
4454 } else {
4455 isl_basic_map_drop_inequality(bmap, u);
4456 isl_basic_map_drop_inequality(bmap, l);
4458 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4459 bmap = isl_basic_map_drop_div(bmap, div1);
4460 return bmap;
4463 /* First check if we can coalesce any pair of divs and
4464 * then continue with dropping more redundant divs.
4466 * We loop over all pairs of lower and upper bounds on a div
4467 * with coefficient 1 and -1, respectively, check if there
4468 * is any other div "c" with which we can coalesce the div
4469 * and if so, perform the coalescing.
4471 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4472 struct isl_basic_map *bmap, int *pairs, int n)
4474 int i, l, u;
4475 unsigned dim;
4477 dim = isl_space_dim(bmap->dim, isl_dim_all);
4479 for (i = 0; i < bmap->n_div; ++i) {
4480 if (!pairs[i])
4481 continue;
4482 for (l = 0; l < bmap->n_ineq; ++l) {
4483 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4484 continue;
4485 for (u = 0; u < bmap->n_ineq; ++u) {
4486 int c;
4488 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4489 continue;
4490 c = div_find_coalesce(bmap, pairs, i, l, u);
4491 if (c < 0)
4492 continue;
4493 free(pairs);
4494 bmap = coalesce_divs(bmap, i, c, l, u);
4495 return isl_basic_map_drop_redundant_divs(bmap);
4500 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4501 free(pairs);
4502 return bmap;
4505 return drop_more_redundant_divs(bmap, pairs, n);
4508 /* Are the "n" coefficients starting at "first" of inequality constraints
4509 * "i" and "j" of "bmap" equal to each other?
4511 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4512 int first, int n)
4514 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4517 /* Are the "n" coefficients starting at "first" of inequality constraints
4518 * "i" and "j" of "bmap" opposite to each other?
4520 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4521 int first, int n)
4523 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4526 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4527 * apart from the constant term?
4529 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4531 unsigned total;
4533 total = isl_basic_map_dim(bmap, isl_dim_all);
4534 return is_opposite_part(bmap, i, j, 1, total);
4537 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4538 * apart from the constant term and the coefficient at position "pos"?
4540 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4541 int pos)
4543 unsigned total;
4545 total = isl_basic_map_dim(bmap, isl_dim_all);
4546 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4547 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4550 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4551 * apart from the constant term and the coefficient at position "pos"?
4553 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4554 int pos)
4556 unsigned total;
4558 total = isl_basic_map_dim(bmap, isl_dim_all);
4559 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4560 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4563 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4564 * been modified, simplying it if "simplify" is set.
4565 * Free the temporary data structure "pairs" that was associated
4566 * to the old version of "bmap".
4568 static __isl_give isl_basic_map *drop_redundant_divs_again(
4569 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4571 if (simplify)
4572 bmap = isl_basic_map_simplify(bmap);
4573 free(pairs);
4574 return isl_basic_map_drop_redundant_divs(bmap);
4577 /* Is "div" the single unknown existentially quantified variable
4578 * in inequality constraint "ineq" of "bmap"?
4579 * "div" is known to have a non-zero coefficient in "ineq".
4581 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4583 int i;
4584 unsigned n_div, o_div;
4586 if (isl_basic_map_div_is_known(bmap, div))
4587 return 0;
4588 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4589 if (n_div == 1)
4590 return 1;
4591 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4592 for (i = 0; i < n_div; ++i) {
4593 if (i == div)
4594 continue;
4595 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4596 continue;
4597 if (!isl_basic_map_div_is_known(bmap, i))
4598 return 0;
4601 return 1;
4604 /* Does integer division "div" have coefficient 1 in inequality constraint
4605 * "ineq" of "map"?
4607 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4609 unsigned o_div;
4611 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4612 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4613 return 1;
4615 return 0;
4618 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4619 * then try and drop redundant divs again,
4620 * freeing the temporary data structure "pairs" that was associated
4621 * to the old version of "bmap".
4623 static __isl_give isl_basic_map *set_eq_and_try_again(
4624 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4626 bmap = isl_basic_map_cow(bmap);
4627 isl_basic_map_inequality_to_equality(bmap, ineq);
4628 return drop_redundant_divs_again(bmap, pairs, 1);
4631 /* Drop the integer division at position "div", along with the two
4632 * inequality constraints "ineq1" and "ineq2" in which it appears
4633 * from "bmap" and then try and drop redundant divs again,
4634 * freeing the temporary data structure "pairs" that was associated
4635 * to the old version of "bmap".
4637 static __isl_give isl_basic_map *drop_div_and_try_again(
4638 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4639 __isl_take int *pairs)
4641 if (ineq1 > ineq2) {
4642 isl_basic_map_drop_inequality(bmap, ineq1);
4643 isl_basic_map_drop_inequality(bmap, ineq2);
4644 } else {
4645 isl_basic_map_drop_inequality(bmap, ineq2);
4646 isl_basic_map_drop_inequality(bmap, ineq1);
4648 bmap = isl_basic_map_drop_div(bmap, div);
4649 return drop_redundant_divs_again(bmap, pairs, 0);
4652 /* Given two inequality constraints
4654 * f(x) + n d + c >= 0, (ineq)
4656 * with d the variable at position "pos", and
4658 * f(x) + c0 >= 0, (lower)
4660 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4661 * determined by the first constraint.
4662 * That is, store
4664 * ceil((c0 - c)/n)
4666 * in *l.
4668 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4669 int ineq, int lower, int pos, isl_int *l)
4671 isl_int_neg(*l, bmap->ineq[ineq][0]);
4672 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4673 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4676 /* Given two inequality constraints
4678 * f(x) + n d + c >= 0, (ineq)
4680 * with d the variable at position "pos", and
4682 * -f(x) - c0 >= 0, (upper)
4684 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4685 * determined by the first constraint.
4686 * That is, store
4688 * ceil((-c1 - c)/n)
4690 * in *u.
4692 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4693 int ineq, int upper, int pos, isl_int *u)
4695 isl_int_neg(*u, bmap->ineq[ineq][0]);
4696 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4697 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4700 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4701 * does the corresponding lower bound have a fixed value in "bmap"?
4703 * In particular, "ineq" is of the form
4705 * f(x) + n d + c >= 0
4707 * with n > 0, c the constant term and
4708 * d the existentially quantified variable "div".
4709 * That is, the lower bound is
4711 * ceil((-f(x) - c)/n)
4713 * Look for a pair of constraints
4715 * f(x) + c0 >= 0
4716 * -f(x) + c1 >= 0
4718 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4719 * That is, check that
4721 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4723 * If so, return the index of inequality f(x) + c0 >= 0.
4724 * Otherwise, return -1.
4726 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4728 int i;
4729 int lower = -1, upper = -1;
4730 unsigned o_div, n_div;
4731 isl_int l, u;
4732 int equal;
4734 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4735 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4736 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4737 if (i == ineq)
4738 continue;
4739 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4740 continue;
4741 if (lower < 0 &&
4742 is_parallel_except(bmap, ineq, i, o_div + div)) {
4743 lower = i;
4744 continue;
4746 if (upper < 0 &&
4747 is_opposite_except(bmap, ineq, i, o_div + div)) {
4748 upper = i;
4752 if (lower < 0 || upper < 0)
4753 return -1;
4755 isl_int_init(l);
4756 isl_int_init(u);
4758 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4759 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4761 equal = isl_int_eq(l, u);
4763 isl_int_clear(l);
4764 isl_int_clear(u);
4766 return equal ? lower : -1;
4769 /* Given a lower bound constraint "ineq" on the existentially quantified
4770 * variable "div", such that the corresponding lower bound has
4771 * a fixed value in "bmap", assign this fixed value to the variable and
4772 * then try and drop redundant divs again,
4773 * freeing the temporary data structure "pairs" that was associated
4774 * to the old version of "bmap".
4775 * "lower" determines the constant value for the lower bound.
4777 * In particular, "ineq" is of the form
4779 * f(x) + n d + c >= 0,
4781 * while "lower" is of the form
4783 * f(x) + c0 >= 0
4785 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4786 * is ceil((c0 - c)/n).
4788 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4789 int div, int ineq, int lower, int *pairs)
4791 isl_int c;
4792 unsigned o_div;
4794 isl_int_init(c);
4796 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4797 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4798 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4799 free(pairs);
4801 isl_int_clear(c);
4803 return isl_basic_map_drop_redundant_divs(bmap);
4806 /* Remove divs that are not strictly needed based on the inequality
4807 * constraints.
4808 * In particular, if a div only occurs positively (or negatively)
4809 * in constraints, then it can simply be dropped.
4810 * Also, if a div occurs in only two constraints and if moreover
4811 * those two constraints are opposite to each other, except for the constant
4812 * term and if the sum of the constant terms is such that for any value
4813 * of the other values, there is always at least one integer value of the
4814 * div, i.e., if one plus this sum is greater than or equal to
4815 * the (absolute value) of the coefficient of the div in the constraints,
4816 * then we can also simply drop the div.
4818 * If an existentially quantified variable does not have an explicit
4819 * representation, appears in only a single lower bound that does not
4820 * involve any other such existentially quantified variables and appears
4821 * in this lower bound with coefficient 1,
4822 * then fix the variable to the value of the lower bound. That is,
4823 * turn the inequality into an equality.
4824 * If for any value of the other variables, there is any value
4825 * for the existentially quantified variable satisfying the constraints,
4826 * then this lower bound also satisfies the constraints.
4827 * It is therefore safe to pick this lower bound.
4829 * The same reasoning holds even if the coefficient is not one.
4830 * However, fixing the variable to the value of the lower bound may
4831 * in general introduce an extra integer division, in which case
4832 * it may be better to pick another value.
4833 * If this integer division has a known constant value, then plugging
4834 * in this constant value removes the existentially quantified variable
4835 * completely. In particular, if the lower bound is of the form
4836 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4837 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4838 * then the existentially quantified variable can be assigned this
4839 * shared value.
4841 * We skip divs that appear in equalities or in the definition of other divs.
4842 * Divs that appear in the definition of other divs usually occur in at least
4843 * 4 constraints, but the constraints may have been simplified.
4845 * If any divs are left after these simple checks then we move on
4846 * to more complicated cases in drop_more_redundant_divs.
4848 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4849 __isl_take isl_basic_map *bmap)
4851 int i, j;
4852 unsigned off;
4853 int *pairs = NULL;
4854 int n = 0;
4856 if (!bmap)
4857 goto error;
4858 if (bmap->n_div == 0)
4859 return bmap;
4861 off = isl_space_dim(bmap->dim, isl_dim_all);
4862 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4863 if (!pairs)
4864 goto error;
4866 for (i = 0; i < bmap->n_div; ++i) {
4867 int pos, neg;
4868 int last_pos, last_neg;
4869 int redundant;
4870 int defined;
4872 defined = !isl_int_is_zero(bmap->div[i][0]);
4873 for (j = i; j < bmap->n_div; ++j)
4874 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4875 break;
4876 if (j < bmap->n_div)
4877 continue;
4878 for (j = 0; j < bmap->n_eq; ++j)
4879 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4880 break;
4881 if (j < bmap->n_eq)
4882 continue;
4883 ++n;
4884 pos = neg = 0;
4885 for (j = 0; j < bmap->n_ineq; ++j) {
4886 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4887 last_pos = j;
4888 ++pos;
4890 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4891 last_neg = j;
4892 ++neg;
4895 pairs[i] = pos * neg;
4896 if (pairs[i] == 0) {
4897 for (j = bmap->n_ineq - 1; j >= 0; --j)
4898 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4899 isl_basic_map_drop_inequality(bmap, j);
4900 bmap = isl_basic_map_drop_div(bmap, i);
4901 return drop_redundant_divs_again(bmap, pairs, 0);
4903 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4904 int single, lower;
4905 if (pos != 1)
4906 continue;
4907 single = single_unknown(bmap, last_pos, i);
4908 if (!single)
4909 continue;
4910 if (has_coef_one(bmap, i, last_pos))
4911 return set_eq_and_try_again(bmap, last_pos,
4912 pairs);
4913 lower = lower_bound_is_cst(bmap, i, last_pos);
4914 if (lower >= 0)
4915 return fix_cst_lower(bmap, i, last_pos, lower,
4916 pairs);
4917 continue;
4920 isl_int_add(bmap->ineq[last_pos][0],
4921 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4922 isl_int_add_ui(bmap->ineq[last_pos][0],
4923 bmap->ineq[last_pos][0], 1);
4924 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4925 bmap->ineq[last_pos][1+off+i]);
4926 isl_int_sub_ui(bmap->ineq[last_pos][0],
4927 bmap->ineq[last_pos][0], 1);
4928 isl_int_sub(bmap->ineq[last_pos][0],
4929 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4930 if (redundant)
4931 return drop_div_and_try_again(bmap, i,
4932 last_pos, last_neg, pairs);
4933 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
4934 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4935 return drop_redundant_divs_again(bmap, pairs, 1);
4937 pairs[i] = 0;
4938 --n;
4941 if (n > 0)
4942 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4944 free(pairs);
4945 return bmap;
4946 error:
4947 free(pairs);
4948 isl_basic_map_free(bmap);
4949 return NULL;
4952 /* Consider the coefficients at "c" as a row vector and replace
4953 * them with their product with "T". "T" is assumed to be a square matrix.
4955 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4957 int n;
4958 isl_ctx *ctx;
4959 isl_vec *v;
4961 if (!T)
4962 return isl_stat_error;
4963 n = isl_mat_rows(T);
4964 if (isl_seq_first_non_zero(c, n) == -1)
4965 return isl_stat_ok;
4966 ctx = isl_mat_get_ctx(T);
4967 v = isl_vec_alloc(ctx, n);
4968 if (!v)
4969 return isl_stat_error;
4970 isl_seq_swp_or_cpy(v->el, c, n);
4971 v = isl_vec_mat_product(v, isl_mat_copy(T));
4972 if (!v)
4973 return isl_stat_error;
4974 isl_seq_swp_or_cpy(c, v->el, n);
4975 isl_vec_free(v);
4977 return isl_stat_ok;
4980 /* Plug in T for the variables in "bmap" starting at "pos".
4981 * T is a linear unimodular matrix, i.e., without constant term.
4983 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4984 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4986 int i;
4987 unsigned n, total;
4989 bmap = isl_basic_map_cow(bmap);
4990 if (!bmap || !T)
4991 goto error;
4993 n = isl_mat_cols(T);
4994 if (n != isl_mat_rows(T))
4995 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4996 "expecting square matrix", goto error);
4998 total = isl_basic_map_dim(bmap, isl_dim_all);
4999 if (pos + n > total || pos + n < pos)
5000 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5001 "invalid range", goto error);
5003 for (i = 0; i < bmap->n_eq; ++i)
5004 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5005 goto error;
5006 for (i = 0; i < bmap->n_ineq; ++i)
5007 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5008 goto error;
5009 for (i = 0; i < bmap->n_div; ++i) {
5010 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5011 continue;
5012 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5013 goto error;
5016 isl_mat_free(T);
5017 return bmap;
5018 error:
5019 isl_basic_map_free(bmap);
5020 isl_mat_free(T);
5021 return NULL;
5024 /* Remove divs that are not strictly needed.
5026 * First look for an equality constraint involving two or more
5027 * existentially quantified variables without an explicit
5028 * representation. Replace the combination that appears
5029 * in the equality constraint by a single existentially quantified
5030 * variable such that the equality can be used to derive
5031 * an explicit representation for the variable.
5032 * If there are no more such equality constraints, then continue
5033 * with isl_basic_map_drop_redundant_divs_ineq.
5035 * In particular, if the equality constraint is of the form
5037 * f(x) + \sum_i c_i a_i = 0
5039 * with a_i existentially quantified variable without explicit
5040 * representation, then apply a transformation on the existentially
5041 * quantified variables to turn the constraint into
5043 * f(x) + g a_1' = 0
5045 * with g the gcd of the c_i.
5046 * In order to easily identify which existentially quantified variables
5047 * have a complete explicit representation, i.e., without being defined
5048 * in terms of other existentially quantified variables without
5049 * an explicit representation, the existentially quantified variables
5050 * are first sorted.
5052 * The variable transformation is computed by extending the row
5053 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5055 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5056 * [a_2'] [ a_2 ]
5057 * ... = U ....
5058 * [a_n'] [ a_n ]
5060 * with [c_1/g ... c_n/g] representing the first row of U.
5061 * The inverse of U is then plugged into the original constraints.
5062 * The call to isl_basic_map_simplify makes sure the explicit
5063 * representation for a_1' is extracted from the equality constraint.
5065 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5066 __isl_take isl_basic_map *bmap)
5068 int first;
5069 int i;
5070 unsigned o_div, n_div;
5071 int l;
5072 isl_ctx *ctx;
5073 isl_mat *T;
5075 if (!bmap)
5076 return NULL;
5077 if (isl_basic_map_divs_known(bmap))
5078 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5079 if (bmap->n_eq == 0)
5080 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5081 bmap = isl_basic_map_sort_divs(bmap);
5082 if (!bmap)
5083 return NULL;
5085 first = isl_basic_map_first_unknown_div(bmap);
5086 if (first < 0)
5087 return isl_basic_map_free(bmap);
5089 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5090 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5092 for (i = 0; i < bmap->n_eq; ++i) {
5093 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5094 n_div - (first));
5095 if (l < 0)
5096 continue;
5097 l += first;
5098 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5099 n_div - (l + 1)) == -1)
5100 continue;
5101 break;
5103 if (i >= bmap->n_eq)
5104 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5106 ctx = isl_basic_map_get_ctx(bmap);
5107 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5108 if (!T)
5109 return isl_basic_map_free(bmap);
5110 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5111 T = isl_mat_normalize_row(T, 0);
5112 T = isl_mat_unimodular_complete(T, 1);
5113 T = isl_mat_right_inverse(T);
5115 for (i = l; i < n_div; ++i)
5116 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5117 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5118 bmap = isl_basic_map_simplify(bmap);
5120 return isl_basic_map_drop_redundant_divs(bmap);
5123 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5124 struct isl_basic_set *bset)
5126 isl_basic_map *bmap = bset_to_bmap(bset);
5127 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5130 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5132 int i;
5134 if (!map)
5135 return NULL;
5136 for (i = 0; i < map->n; ++i) {
5137 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5138 if (!map->p[i])
5139 goto error;
5141 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5142 return map;
5143 error:
5144 isl_map_free(map);
5145 return NULL;
5148 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5150 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5153 /* Does "bmap" satisfy any equality that involves more than 2 variables
5154 * and/or has coefficients different from -1 and 1?
5156 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5158 int i;
5159 unsigned total;
5161 total = isl_basic_map_dim(bmap, isl_dim_all);
5163 for (i = 0; i < bmap->n_eq; ++i) {
5164 int j, k;
5166 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5167 if (j < 0)
5168 continue;
5169 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5170 !isl_int_is_negone(bmap->eq[i][1 + j]))
5171 return 1;
5173 j += 1;
5174 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5175 if (k < 0)
5176 continue;
5177 j += k;
5178 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5179 !isl_int_is_negone(bmap->eq[i][1 + j]))
5180 return 1;
5182 j += 1;
5183 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5184 if (k >= 0)
5185 return 1;
5188 return 0;
5191 /* Remove any common factor g from the constraint coefficients in "v".
5192 * The constant term is stored in the first position and is replaced
5193 * by floor(c/g). If any common factor is removed and if this results
5194 * in a tightening of the constraint, then set *tightened.
5196 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5197 int *tightened)
5199 isl_ctx *ctx;
5201 if (!v)
5202 return NULL;
5203 ctx = isl_vec_get_ctx(v);
5204 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5205 if (isl_int_is_zero(ctx->normalize_gcd))
5206 return v;
5207 if (isl_int_is_one(ctx->normalize_gcd))
5208 return v;
5209 v = isl_vec_cow(v);
5210 if (!v)
5211 return NULL;
5212 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5213 *tightened = 1;
5214 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5215 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5216 v->size - 1);
5217 return v;
5220 /* If "bmap" is an integer set that satisfies any equality involving
5221 * more than 2 variables and/or has coefficients different from -1 and 1,
5222 * then use variable compression to reduce the coefficients by removing
5223 * any (hidden) common factor.
5224 * In particular, apply the variable compression to each constraint,
5225 * factor out any common factor in the non-constant coefficients and
5226 * then apply the inverse of the compression.
5227 * At the end, we mark the basic map as having reduced constants.
5228 * If this flag is still set on the next invocation of this function,
5229 * then we skip the computation.
5231 * Removing a common factor may result in a tightening of some of
5232 * the constraints. If this happens, then we may end up with two
5233 * opposite inequalities that can be replaced by an equality.
5234 * We therefore call isl_basic_map_detect_inequality_pairs,
5235 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5236 * and isl_basic_map_gauss if such a pair was found.
5238 * Note that this function may leave the result in an inconsistent state.
5239 * In particular, the constraints may not be gaussed.
5240 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5241 * for some of the test cases to pass successfully.
5242 * Any potential modification of the representation is therefore only
5243 * performed on a single copy of the basic map.
5245 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5246 __isl_take isl_basic_map *bmap)
5248 unsigned total;
5249 isl_ctx *ctx;
5250 isl_vec *v;
5251 isl_mat *eq, *T, *T2;
5252 int i;
5253 int tightened;
5255 if (!bmap)
5256 return NULL;
5257 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5258 return bmap;
5259 if (isl_basic_map_is_rational(bmap))
5260 return bmap;
5261 if (bmap->n_eq == 0)
5262 return bmap;
5263 if (!has_multiple_var_equality(bmap))
5264 return bmap;
5266 total = isl_basic_map_dim(bmap, isl_dim_all);
5267 ctx = isl_basic_map_get_ctx(bmap);
5268 v = isl_vec_alloc(ctx, 1 + total);
5269 if (!v)
5270 return isl_basic_map_free(bmap);
5272 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5273 T = isl_mat_variable_compression(eq, &T2);
5274 if (!T || !T2)
5275 goto error;
5276 if (T->n_col == 0) {
5277 isl_mat_free(T);
5278 isl_mat_free(T2);
5279 isl_vec_free(v);
5280 return isl_basic_map_set_to_empty(bmap);
5283 bmap = isl_basic_map_cow(bmap);
5284 if (!bmap)
5285 goto error;
5287 tightened = 0;
5288 for (i = 0; i < bmap->n_ineq; ++i) {
5289 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5290 v = isl_vec_mat_product(v, isl_mat_copy(T));
5291 v = normalize_constraint(v, &tightened);
5292 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5293 if (!v)
5294 goto error;
5295 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5298 isl_mat_free(T);
5299 isl_mat_free(T2);
5300 isl_vec_free(v);
5302 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5304 if (tightened) {
5305 int progress = 0;
5307 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5308 if (progress) {
5309 bmap = eliminate_divs_eq(bmap, &progress);
5310 bmap = isl_basic_map_gauss(bmap, NULL);
5314 return bmap;
5315 error:
5316 isl_mat_free(T);
5317 isl_mat_free(T2);
5318 isl_vec_free(v);
5319 return isl_basic_map_free(bmap);
5322 /* Shift the integer division at position "div" of "bmap"
5323 * by "shift" times the variable at position "pos".
5324 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5325 * corresponds to the constant term.
5327 * That is, if the integer division has the form
5329 * floor(f(x)/d)
5331 * then replace it by
5333 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5335 __isl_give isl_basic_map *isl_basic_map_shift_div(
5336 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5338 int i;
5339 unsigned total;
5341 if (!bmap)
5342 return NULL;
5344 total = isl_basic_map_dim(bmap, isl_dim_all);
5345 total -= isl_basic_map_dim(bmap, isl_dim_div);
5347 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5349 for (i = 0; i < bmap->n_eq; ++i) {
5350 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5351 continue;
5352 isl_int_submul(bmap->eq[i][pos],
5353 shift, bmap->eq[i][1 + total + div]);
5355 for (i = 0; i < bmap->n_ineq; ++i) {
5356 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5357 continue;
5358 isl_int_submul(bmap->ineq[i][pos],
5359 shift, bmap->ineq[i][1 + total + div]);
5361 for (i = 0; i < bmap->n_div; ++i) {
5362 if (isl_int_is_zero(bmap->div[i][0]))
5363 continue;
5364 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5365 continue;
5366 isl_int_submul(bmap->div[i][1 + pos],
5367 shift, bmap->div[i][1 + 1 + total + div]);
5370 return bmap;