extract out shared set_to_map
[isl.git] / isl_map_simplify.c
blob430e5ae6d370016093b9eddd9c00918aa9af3ea8
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>
30 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
32 isl_int *t = bmap->eq[a];
33 bmap->eq[a] = bmap->eq[b];
34 bmap->eq[b] = t;
37 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
39 if (a != b) {
40 isl_int *t = bmap->ineq[a];
41 bmap->ineq[a] = bmap->ineq[b];
42 bmap->ineq[b] = t;
46 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
48 isl_seq_cpy(c, c + n, rem);
49 isl_seq_clr(c + rem, n);
52 /* Drop n dimensions starting at first.
54 * In principle, this frees up some extra variables as the number
55 * of columns remains constant, but we would have to extend
56 * the div array too as the number of rows in this array is assumed
57 * to be equal to extra.
59 struct isl_basic_set *isl_basic_set_drop_dims(
60 struct isl_basic_set *bset, unsigned first, unsigned n)
62 int i;
64 if (!bset)
65 goto error;
67 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
69 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
70 return bset;
72 bset = isl_basic_set_cow(bset);
73 if (!bset)
74 return NULL;
76 for (i = 0; i < bset->n_eq; ++i)
77 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
78 (bset->dim->n_out-first-n)+bset->extra);
80 for (i = 0; i < bset->n_ineq; ++i)
81 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
82 (bset->dim->n_out-first-n)+bset->extra);
84 for (i = 0; i < bset->n_div; ++i)
85 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
86 (bset->dim->n_out-first-n)+bset->extra);
88 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
89 if (!bset->dim)
90 goto error;
92 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
93 bset = isl_basic_set_simplify(bset);
94 return isl_basic_set_finalize(bset);
95 error:
96 isl_basic_set_free(bset);
97 return NULL;
100 struct isl_set *isl_set_drop_dims(
101 struct isl_set *set, unsigned first, unsigned n)
103 int i;
105 if (!set)
106 goto error;
108 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
110 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
111 return set;
112 set = isl_set_cow(set);
113 if (!set)
114 goto error;
115 set->dim = isl_space_drop_outputs(set->dim, first, n);
116 if (!set->dim)
117 goto error;
119 for (i = 0; i < set->n; ++i) {
120 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
121 if (!set->p[i])
122 goto error;
125 ISL_F_CLR(set, ISL_SET_NORMALIZED);
126 return set;
127 error:
128 isl_set_free(set);
129 return NULL;
132 /* Move "n" divs starting at "first" to the end of the list of divs.
134 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
135 unsigned first, unsigned n)
137 isl_int **div;
138 int i;
140 if (first + n == bmap->n_div)
141 return bmap;
143 div = isl_alloc_array(bmap->ctx, isl_int *, n);
144 if (!div)
145 goto error;
146 for (i = 0; i < n; ++i)
147 div[i] = bmap->div[first + i];
148 for (i = 0; i < bmap->n_div - first - n; ++i)
149 bmap->div[first + i] = bmap->div[first + n + i];
150 for (i = 0; i < n; ++i)
151 bmap->div[bmap->n_div - n + i] = div[i];
152 free(div);
153 return bmap;
154 error:
155 isl_basic_map_free(bmap);
156 return NULL;
159 /* Drop "n" dimensions of type "type" starting at "first".
161 * In principle, this frees up some extra variables as the number
162 * of columns remains constant, but we would have to extend
163 * the div array too as the number of rows in this array is assumed
164 * to be equal to extra.
166 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
167 enum isl_dim_type type, unsigned first, unsigned n)
169 int i;
170 unsigned dim;
171 unsigned offset;
172 unsigned left;
174 if (!bmap)
175 goto error;
177 dim = isl_basic_map_dim(bmap, type);
178 isl_assert(bmap->ctx, first + n <= dim, goto error);
180 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
181 return bmap;
183 bmap = isl_basic_map_cow(bmap);
184 if (!bmap)
185 return NULL;
187 offset = isl_basic_map_offset(bmap, type) + first;
188 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
189 for (i = 0; i < bmap->n_eq; ++i)
190 constraint_drop_vars(bmap->eq[i]+offset, n, left);
192 for (i = 0; i < bmap->n_ineq; ++i)
193 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
195 for (i = 0; i < bmap->n_div; ++i)
196 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
198 if (type == isl_dim_div) {
199 bmap = move_divs_last(bmap, first, n);
200 if (!bmap)
201 goto error;
202 isl_basic_map_free_div(bmap, n);
203 } else
204 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
205 if (!bmap->dim)
206 goto error;
208 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
209 bmap = isl_basic_map_simplify(bmap);
210 return isl_basic_map_finalize(bmap);
211 error:
212 isl_basic_map_free(bmap);
213 return NULL;
216 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
217 enum isl_dim_type type, unsigned first, unsigned n)
219 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
220 type, first, n));
223 struct isl_basic_map *isl_basic_map_drop_inputs(
224 struct isl_basic_map *bmap, unsigned first, unsigned n)
226 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
229 struct isl_map *isl_map_drop(struct isl_map *map,
230 enum isl_dim_type type, unsigned first, unsigned n)
232 int i;
234 if (!map)
235 goto error;
237 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
239 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
240 return map;
241 map = isl_map_cow(map);
242 if (!map)
243 goto error;
244 map->dim = isl_space_drop_dims(map->dim, type, first, n);
245 if (!map->dim)
246 goto error;
248 for (i = 0; i < map->n; ++i) {
249 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
250 if (!map->p[i])
251 goto error;
253 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
255 return map;
256 error:
257 isl_map_free(map);
258 return NULL;
261 struct isl_set *isl_set_drop(struct isl_set *set,
262 enum isl_dim_type type, unsigned first, unsigned n)
264 return (isl_set *)isl_map_drop(set_to_map(set), type, first, n);
267 struct isl_map *isl_map_drop_inputs(
268 struct isl_map *map, unsigned first, unsigned n)
270 return isl_map_drop(map, isl_dim_in, first, n);
274 * We don't cow, as the div is assumed to be redundant.
276 __isl_give isl_basic_map *isl_basic_map_drop_div(
277 __isl_take isl_basic_map *bmap, unsigned div)
279 int i;
280 unsigned pos;
282 if (!bmap)
283 goto error;
285 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
287 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
289 for (i = 0; i < bmap->n_eq; ++i)
290 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
292 for (i = 0; i < bmap->n_ineq; ++i) {
293 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
294 isl_basic_map_drop_inequality(bmap, i);
295 --i;
296 continue;
298 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
301 for (i = 0; i < bmap->n_div; ++i)
302 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
304 if (div != bmap->n_div - 1) {
305 int j;
306 isl_int *t = bmap->div[div];
308 for (j = div; j < bmap->n_div - 1; ++j)
309 bmap->div[j] = bmap->div[j+1];
311 bmap->div[bmap->n_div - 1] = t;
313 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
314 isl_basic_map_free_div(bmap, 1);
316 return bmap;
317 error:
318 isl_basic_map_free(bmap);
319 return NULL;
322 struct isl_basic_map *isl_basic_map_normalize_constraints(
323 struct isl_basic_map *bmap)
325 int i;
326 isl_int gcd;
327 unsigned total = isl_basic_map_total_dim(bmap);
329 if (!bmap)
330 return NULL;
332 isl_int_init(gcd);
333 for (i = bmap->n_eq - 1; i >= 0; --i) {
334 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
335 if (isl_int_is_zero(gcd)) {
336 if (!isl_int_is_zero(bmap->eq[i][0])) {
337 bmap = isl_basic_map_set_to_empty(bmap);
338 break;
340 isl_basic_map_drop_equality(bmap, i);
341 continue;
343 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
344 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
345 if (isl_int_is_one(gcd))
346 continue;
347 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
348 bmap = isl_basic_map_set_to_empty(bmap);
349 break;
351 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
354 for (i = bmap->n_ineq - 1; i >= 0; --i) {
355 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
356 if (isl_int_is_zero(gcd)) {
357 if (isl_int_is_neg(bmap->ineq[i][0])) {
358 bmap = isl_basic_map_set_to_empty(bmap);
359 break;
361 isl_basic_map_drop_inequality(bmap, i);
362 continue;
364 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
365 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
366 if (isl_int_is_one(gcd))
367 continue;
368 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
369 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
371 isl_int_clear(gcd);
373 return bmap;
376 struct isl_basic_set *isl_basic_set_normalize_constraints(
377 struct isl_basic_set *bset)
379 isl_basic_map *bmap = bset_to_bmap(bset);
380 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
383 /* Assuming the variable at position "pos" has an integer coefficient
384 * in integer division "div", extract it from this integer division.
385 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
386 * corresponds to the constant term.
388 * That is, the integer division is of the form
390 * floor((... + c * d * x_pos + ...)/d)
392 * Replace it by
394 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
396 static __isl_give isl_basic_map *remove_var_from_div(
397 __isl_take isl_basic_map *bmap, int div, int pos)
399 isl_int shift;
401 isl_int_init(shift);
402 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
403 isl_int_neg(shift, shift);
404 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
405 isl_int_clear(shift);
407 return bmap;
410 /* Check if integer division "div" has any integral coefficient
411 * (or constant term). If so, extract them from the integer division.
413 static __isl_give isl_basic_map *remove_independent_vars_from_div(
414 __isl_take isl_basic_map *bmap, int div)
416 int i;
417 unsigned total = 1 + isl_basic_map_total_dim(bmap);
419 for (i = 0; i < total; ++i) {
420 if (isl_int_is_zero(bmap->div[div][1 + i]))
421 continue;
422 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
423 bmap->div[div][0]))
424 continue;
425 bmap = remove_var_from_div(bmap, div, i);
426 if (!bmap)
427 break;
430 return bmap;
433 /* Check if any known integer division has any integral coefficient
434 * (or constant term). If so, extract them from the integer division.
436 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
437 __isl_take isl_basic_map *bmap)
439 int i;
441 if (!bmap)
442 return NULL;
443 if (bmap->n_div == 0)
444 return bmap;
446 for (i = 0; i < bmap->n_div; ++i) {
447 if (isl_int_is_zero(bmap->div[i][0]))
448 continue;
449 bmap = remove_independent_vars_from_div(bmap, i);
450 if (!bmap)
451 break;
454 return bmap;
457 /* Remove any common factor in numerator and denominator of the div expression,
458 * not taking into account the constant term.
459 * That is, if the div is of the form
461 * floor((a + m f(x))/(m d))
463 * then replace it by
465 * floor((floor(a/m) + f(x))/d)
467 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
468 * and can therefore not influence the result of the floor.
470 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
472 unsigned total = isl_basic_map_total_dim(bmap);
473 isl_ctx *ctx = bmap->ctx;
475 if (isl_int_is_zero(bmap->div[div][0]))
476 return;
477 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
478 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
479 if (isl_int_is_one(ctx->normalize_gcd))
480 return;
481 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
482 ctx->normalize_gcd);
483 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
484 ctx->normalize_gcd);
485 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
486 ctx->normalize_gcd, total);
489 /* Remove any common factor in numerator and denominator of a div expression,
490 * not taking into account the constant term.
491 * That is, look for any div of the form
493 * floor((a + m f(x))/(m d))
495 * and replace it by
497 * floor((floor(a/m) + f(x))/d)
499 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
500 * and can therefore not influence the result of the floor.
502 static __isl_give isl_basic_map *normalize_div_expressions(
503 __isl_take isl_basic_map *bmap)
505 int i;
507 if (!bmap)
508 return NULL;
509 if (bmap->n_div == 0)
510 return bmap;
512 for (i = 0; i < bmap->n_div; ++i)
513 normalize_div_expression(bmap, i);
515 return bmap;
518 /* Assumes divs have been ordered if keep_divs is set.
520 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
521 unsigned pos, isl_int *eq, int keep_divs, int *progress)
523 unsigned total;
524 unsigned space_total;
525 int k;
526 int last_div;
528 total = isl_basic_map_total_dim(bmap);
529 space_total = isl_space_dim(bmap->dim, isl_dim_all);
530 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
531 for (k = 0; k < bmap->n_eq; ++k) {
532 if (bmap->eq[k] == eq)
533 continue;
534 if (isl_int_is_zero(bmap->eq[k][1+pos]))
535 continue;
536 if (progress)
537 *progress = 1;
538 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
539 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
542 for (k = 0; k < bmap->n_ineq; ++k) {
543 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
544 continue;
545 if (progress)
546 *progress = 1;
547 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
548 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
549 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
552 for (k = 0; k < bmap->n_div; ++k) {
553 if (isl_int_is_zero(bmap->div[k][0]))
554 continue;
555 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
556 continue;
557 if (progress)
558 *progress = 1;
559 /* We need to be careful about circular definitions,
560 * so for now we just remove the definition of div k
561 * if the equality contains any divs.
562 * If keep_divs is set, then the divs have been ordered
563 * and we can keep the definition as long as the result
564 * is still ordered.
566 if (last_div == -1 || (keep_divs && last_div < k)) {
567 isl_seq_elim(bmap->div[k]+1, eq,
568 1+pos, 1+total, &bmap->div[k][0]);
569 normalize_div_expression(bmap, k);
570 } else
571 isl_seq_clr(bmap->div[k], 1 + total);
572 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
576 /* Assumes divs have been ordered if keep_divs is set.
578 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
579 isl_int *eq, unsigned div, int keep_divs)
581 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
583 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
585 bmap = isl_basic_map_drop_div(bmap, div);
587 return bmap;
590 /* Check if elimination of div "div" using equality "eq" would not
591 * result in a div depending on a later div.
593 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
594 unsigned div)
596 int k;
597 int last_div;
598 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
599 unsigned pos = space_total + div;
601 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
602 if (last_div < 0 || last_div <= div)
603 return 1;
605 for (k = 0; k <= last_div; ++k) {
606 if (isl_int_is_zero(bmap->div[k][0]))
607 return 1;
608 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
609 return 0;
612 return 1;
615 /* Elimininate divs based on equalities
617 static struct isl_basic_map *eliminate_divs_eq(
618 struct isl_basic_map *bmap, int *progress)
620 int d;
621 int i;
622 int modified = 0;
623 unsigned off;
625 bmap = isl_basic_map_order_divs(bmap);
627 if (!bmap)
628 return NULL;
630 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
632 for (d = bmap->n_div - 1; d >= 0 ; --d) {
633 for (i = 0; i < bmap->n_eq; ++i) {
634 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
635 !isl_int_is_negone(bmap->eq[i][off + d]))
636 continue;
637 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
638 continue;
639 modified = 1;
640 *progress = 1;
641 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
642 if (isl_basic_map_drop_equality(bmap, i) < 0)
643 return isl_basic_map_free(bmap);
644 break;
647 if (modified)
648 return eliminate_divs_eq(bmap, progress);
649 return bmap;
652 /* Elimininate divs based on inequalities
654 static struct isl_basic_map *eliminate_divs_ineq(
655 struct isl_basic_map *bmap, int *progress)
657 int d;
658 int i;
659 unsigned off;
660 struct isl_ctx *ctx;
662 if (!bmap)
663 return NULL;
665 ctx = bmap->ctx;
666 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
668 for (d = bmap->n_div - 1; d >= 0 ; --d) {
669 for (i = 0; i < bmap->n_eq; ++i)
670 if (!isl_int_is_zero(bmap->eq[i][off + d]))
671 break;
672 if (i < bmap->n_eq)
673 continue;
674 for (i = 0; i < bmap->n_ineq; ++i)
675 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
676 break;
677 if (i < bmap->n_ineq)
678 continue;
679 *progress = 1;
680 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
681 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
682 break;
683 bmap = isl_basic_map_drop_div(bmap, d);
684 if (!bmap)
685 break;
687 return bmap;
690 struct isl_basic_map *isl_basic_map_gauss(
691 struct isl_basic_map *bmap, int *progress)
693 int k;
694 int done;
695 int last_var;
696 unsigned total_var;
697 unsigned total;
699 bmap = isl_basic_map_order_divs(bmap);
701 if (!bmap)
702 return NULL;
704 total = isl_basic_map_total_dim(bmap);
705 total_var = total - bmap->n_div;
707 last_var = total - 1;
708 for (done = 0; done < bmap->n_eq; ++done) {
709 for (; last_var >= 0; --last_var) {
710 for (k = done; k < bmap->n_eq; ++k)
711 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
712 break;
713 if (k < bmap->n_eq)
714 break;
716 if (last_var < 0)
717 break;
718 if (k != done)
719 swap_equality(bmap, k, done);
720 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
721 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
723 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
724 progress);
726 if (last_var >= total_var &&
727 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
728 unsigned div = last_var - total_var;
729 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
730 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
731 isl_int_set(bmap->div[div][0],
732 bmap->eq[done][1+last_var]);
733 if (progress)
734 *progress = 1;
735 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
738 if (done == bmap->n_eq)
739 return bmap;
740 for (k = done; k < bmap->n_eq; ++k) {
741 if (isl_int_is_zero(bmap->eq[k][0]))
742 continue;
743 return isl_basic_map_set_to_empty(bmap);
745 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
746 return bmap;
749 struct isl_basic_set *isl_basic_set_gauss(
750 struct isl_basic_set *bset, int *progress)
752 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
753 progress));
757 static unsigned int round_up(unsigned int v)
759 int old_v = v;
761 while (v) {
762 old_v = v;
763 v ^= v & -v;
765 return old_v << 1;
768 /* Hash table of inequalities in a basic map.
769 * "index" is an array of addresses of inequalities in the basic map, some
770 * of which are NULL. The inequalities are hashed on the coefficients
771 * except the constant term.
772 * "size" is the number of elements in the array and is always a power of two
773 * "bits" is the number of bits need to represent an index into the array.
774 * "total" is the total dimension of the basic map.
776 struct isl_constraint_index {
777 unsigned int size;
778 int bits;
779 isl_int ***index;
780 unsigned total;
783 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
785 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
786 __isl_keep isl_basic_map *bmap)
788 isl_ctx *ctx;
790 ci->index = NULL;
791 if (!bmap)
792 return isl_stat_error;
793 ci->total = isl_basic_set_total_dim(bmap);
794 if (bmap->n_ineq == 0)
795 return isl_stat_ok;
796 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
797 ci->bits = ffs(ci->size) - 1;
798 ctx = isl_basic_map_get_ctx(bmap);
799 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
800 if (!ci->index)
801 return isl_stat_error;
803 return isl_stat_ok;
806 /* Free the memory allocated by create_constraint_index.
808 static void constraint_index_free(struct isl_constraint_index *ci)
810 free(ci->index);
813 /* Return the position in ci->index that contains the address of
814 * an inequality that is equal to *ineq up to the constant term,
815 * provided this address is not identical to "ineq".
816 * If there is no such inequality, then return the position where
817 * such an inequality should be inserted.
819 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
821 int h;
822 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
823 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
824 if (ineq != ci->index[h] &&
825 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
826 break;
827 return h;
830 /* Return the position in ci->index that contains the address of
831 * an inequality that is equal to the k'th inequality of "bmap"
832 * up to the constant term, provided it does not point to the very
833 * same inequality.
834 * If there is no such inequality, then return the position where
835 * such an inequality should be inserted.
837 static int hash_index(struct isl_constraint_index *ci,
838 __isl_keep isl_basic_map *bmap, int k)
840 return hash_index_ineq(ci, &bmap->ineq[k]);
843 static int set_hash_index(struct isl_constraint_index *ci,
844 struct isl_basic_set *bset, int k)
846 return hash_index(ci, bset, k);
849 /* Fill in the "ci" data structure with the inequalities of "bset".
851 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
852 __isl_keep isl_basic_set *bset)
854 int k, h;
856 if (create_constraint_index(ci, bset) < 0)
857 return isl_stat_error;
859 for (k = 0; k < bset->n_ineq; ++k) {
860 h = set_hash_index(ci, bset, k);
861 ci->index[h] = &bset->ineq[k];
864 return isl_stat_ok;
867 /* Is the inequality ineq (obviously) redundant with respect
868 * to the constraints in "ci"?
870 * Look for an inequality in "ci" with the same coefficients and then
871 * check if the contant term of "ineq" is greater than or equal
872 * to the constant term of that inequality. If so, "ineq" is clearly
873 * redundant.
875 * Note that hash_index_ineq ignores a stored constraint if it has
876 * the same address as the passed inequality. It is ok to pass
877 * the address of a local variable here since it will never be
878 * the same as the address of a constraint in "ci".
880 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
881 isl_int *ineq)
883 int h;
885 h = hash_index_ineq(ci, &ineq);
886 if (!ci->index[h])
887 return isl_bool_false;
888 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
891 /* If we can eliminate more than one div, then we need to make
892 * sure we do it from last div to first div, in order not to
893 * change the position of the other divs that still need to
894 * be removed.
896 static struct isl_basic_map *remove_duplicate_divs(
897 struct isl_basic_map *bmap, int *progress)
899 unsigned int size;
900 int *index;
901 int *elim_for;
902 int k, l, h;
903 int bits;
904 struct isl_blk eq;
905 unsigned total_var;
906 unsigned total;
907 struct isl_ctx *ctx;
909 bmap = isl_basic_map_order_divs(bmap);
910 if (!bmap || bmap->n_div <= 1)
911 return bmap;
913 total_var = isl_space_dim(bmap->dim, isl_dim_all);
914 total = total_var + bmap->n_div;
916 ctx = bmap->ctx;
917 for (k = bmap->n_div - 1; k >= 0; --k)
918 if (!isl_int_is_zero(bmap->div[k][0]))
919 break;
920 if (k <= 0)
921 return bmap;
923 size = round_up(4 * bmap->n_div / 3 - 1);
924 if (size == 0)
925 return bmap;
926 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
927 bits = ffs(size) - 1;
928 index = isl_calloc_array(ctx, int, size);
929 if (!elim_for || !index)
930 goto out;
931 eq = isl_blk_alloc(ctx, 1+total);
932 if (isl_blk_is_error(eq))
933 goto out;
935 isl_seq_clr(eq.data, 1+total);
936 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
937 for (--k; k >= 0; --k) {
938 uint32_t hash;
940 if (isl_int_is_zero(bmap->div[k][0]))
941 continue;
943 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
944 for (h = hash; index[h]; h = (h+1) % size)
945 if (isl_seq_eq(bmap->div[k],
946 bmap->div[index[h]-1], 2+total))
947 break;
948 if (index[h]) {
949 *progress = 1;
950 l = index[h] - 1;
951 elim_for[l] = k + 1;
953 index[h] = k+1;
955 for (l = bmap->n_div - 1; l >= 0; --l) {
956 if (!elim_for[l])
957 continue;
958 k = elim_for[l] - 1;
959 isl_int_set_si(eq.data[1+total_var+k], -1);
960 isl_int_set_si(eq.data[1+total_var+l], 1);
961 bmap = eliminate_div(bmap, eq.data, l, 1);
962 if (!bmap)
963 break;
964 isl_int_set_si(eq.data[1+total_var+k], 0);
965 isl_int_set_si(eq.data[1+total_var+l], 0);
968 isl_blk_free(ctx, eq);
969 out:
970 free(index);
971 free(elim_for);
972 return bmap;
975 static int n_pure_div_eq(struct isl_basic_map *bmap)
977 int i, j;
978 unsigned total;
980 total = isl_space_dim(bmap->dim, isl_dim_all);
981 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
982 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
983 --j;
984 if (j < 0)
985 break;
986 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
987 return 0;
989 return i;
992 /* Normalize divs that appear in equalities.
994 * In particular, we assume that bmap contains some equalities
995 * of the form
997 * a x = m * e_i
999 * and we want to replace the set of e_i by a minimal set and
1000 * such that the new e_i have a canonical representation in terms
1001 * of the vector x.
1002 * If any of the equalities involves more than one divs, then
1003 * we currently simply bail out.
1005 * Let us first additionally assume that all equalities involve
1006 * a div. The equalities then express modulo constraints on the
1007 * remaining variables and we can use "parameter compression"
1008 * to find a minimal set of constraints. The result is a transformation
1010 * x = T(x') = x_0 + G x'
1012 * with G a lower-triangular matrix with all elements below the diagonal
1013 * non-negative and smaller than the diagonal element on the same row.
1014 * We first normalize x_0 by making the same property hold in the affine
1015 * T matrix.
1016 * The rows i of G with a 1 on the diagonal do not impose any modulo
1017 * constraint and simply express x_i = x'_i.
1018 * For each of the remaining rows i, we introduce a div and a corresponding
1019 * equality. In particular
1021 * g_ii e_j = x_i - g_i(x')
1023 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1024 * corresponding div (if g_kk != 1).
1026 * If there are any equalities not involving any div, then we
1027 * first apply a variable compression on the variables x:
1029 * x = C x'' x'' = C_2 x
1031 * and perform the above parameter compression on A C instead of on A.
1032 * The resulting compression is then of the form
1034 * x'' = T(x') = x_0 + G x'
1036 * and in constructing the new divs and the corresponding equalities,
1037 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1038 * by the corresponding row from C_2.
1040 static struct isl_basic_map *normalize_divs(
1041 struct isl_basic_map *bmap, int *progress)
1043 int i, j, k;
1044 int total;
1045 int div_eq;
1046 struct isl_mat *B;
1047 struct isl_vec *d;
1048 struct isl_mat *T = NULL;
1049 struct isl_mat *C = NULL;
1050 struct isl_mat *C2 = NULL;
1051 isl_int v;
1052 int *pos;
1053 int dropped, needed;
1055 if (!bmap)
1056 return NULL;
1058 if (bmap->n_div == 0)
1059 return bmap;
1061 if (bmap->n_eq == 0)
1062 return bmap;
1064 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1065 return bmap;
1067 total = isl_space_dim(bmap->dim, isl_dim_all);
1068 div_eq = n_pure_div_eq(bmap);
1069 if (div_eq == 0)
1070 return bmap;
1072 if (div_eq < bmap->n_eq) {
1073 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1074 bmap->n_eq - div_eq, 0, 1 + total);
1075 C = isl_mat_variable_compression(B, &C2);
1076 if (!C || !C2)
1077 goto error;
1078 if (C->n_col == 0) {
1079 bmap = isl_basic_map_set_to_empty(bmap);
1080 isl_mat_free(C);
1081 isl_mat_free(C2);
1082 goto done;
1086 d = isl_vec_alloc(bmap->ctx, div_eq);
1087 if (!d)
1088 goto error;
1089 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1090 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1091 --j;
1092 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1094 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1096 if (C) {
1097 B = isl_mat_product(B, C);
1098 C = NULL;
1101 T = isl_mat_parameter_compression(B, d);
1102 if (!T)
1103 goto error;
1104 if (T->n_col == 0) {
1105 bmap = isl_basic_map_set_to_empty(bmap);
1106 isl_mat_free(C2);
1107 isl_mat_free(T);
1108 goto done;
1110 isl_int_init(v);
1111 for (i = 0; i < T->n_row - 1; ++i) {
1112 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1113 if (isl_int_is_zero(v))
1114 continue;
1115 isl_mat_col_submul(T, 0, v, 1 + i);
1117 isl_int_clear(v);
1118 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1119 if (!pos)
1120 goto error;
1121 /* We have to be careful because dropping equalities may reorder them */
1122 dropped = 0;
1123 for (j = bmap->n_div - 1; j >= 0; --j) {
1124 for (i = 0; i < bmap->n_eq; ++i)
1125 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1126 break;
1127 if (i < bmap->n_eq) {
1128 bmap = isl_basic_map_drop_div(bmap, j);
1129 isl_basic_map_drop_equality(bmap, i);
1130 ++dropped;
1133 pos[0] = 0;
1134 needed = 0;
1135 for (i = 1; i < T->n_row; ++i) {
1136 if (isl_int_is_one(T->row[i][i]))
1137 pos[i] = i;
1138 else
1139 needed++;
1141 if (needed > dropped) {
1142 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1143 needed, needed, 0);
1144 if (!bmap)
1145 goto error;
1147 for (i = 1; i < T->n_row; ++i) {
1148 if (isl_int_is_one(T->row[i][i]))
1149 continue;
1150 k = isl_basic_map_alloc_div(bmap);
1151 pos[i] = 1 + total + k;
1152 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1153 isl_int_set(bmap->div[k][0], T->row[i][i]);
1154 if (C2)
1155 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1156 else
1157 isl_int_set_si(bmap->div[k][1 + i], 1);
1158 for (j = 0; j < i; ++j) {
1159 if (isl_int_is_zero(T->row[i][j]))
1160 continue;
1161 if (pos[j] < T->n_row && C2)
1162 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1163 C2->row[pos[j]], 1 + total);
1164 else
1165 isl_int_neg(bmap->div[k][1 + pos[j]],
1166 T->row[i][j]);
1168 j = isl_basic_map_alloc_equality(bmap);
1169 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1170 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1172 free(pos);
1173 isl_mat_free(C2);
1174 isl_mat_free(T);
1176 if (progress)
1177 *progress = 1;
1178 done:
1179 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1181 return bmap;
1182 error:
1183 isl_mat_free(C);
1184 isl_mat_free(C2);
1185 isl_mat_free(T);
1186 return bmap;
1189 static struct isl_basic_map *set_div_from_lower_bound(
1190 struct isl_basic_map *bmap, int div, int ineq)
1192 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1194 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1195 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1196 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1197 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1198 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1200 return bmap;
1203 /* Check whether it is ok to define a div based on an inequality.
1204 * To avoid the introduction of circular definitions of divs, we
1205 * do not allow such a definition if the resulting expression would refer to
1206 * any other undefined divs or if any known div is defined in
1207 * terms of the unknown div.
1209 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1210 int div, int ineq)
1212 int j;
1213 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1215 /* Not defined in terms of unknown divs */
1216 for (j = 0; j < bmap->n_div; ++j) {
1217 if (div == j)
1218 continue;
1219 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1220 continue;
1221 if (isl_int_is_zero(bmap->div[j][0]))
1222 return 0;
1225 /* No other div defined in terms of this one => avoid loops */
1226 for (j = 0; j < bmap->n_div; ++j) {
1227 if (div == j)
1228 continue;
1229 if (isl_int_is_zero(bmap->div[j][0]))
1230 continue;
1231 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1232 return 0;
1235 return 1;
1238 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1239 * be a better expression than the current one?
1241 * If we do not have any expression yet, then any expression would be better.
1242 * Otherwise we check if the last variable involved in the inequality
1243 * (disregarding the div that it would define) is in an earlier position
1244 * than the last variable involved in the current div expression.
1246 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1247 int div, int ineq)
1249 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1250 int last_div;
1251 int last_ineq;
1253 if (isl_int_is_zero(bmap->div[div][0]))
1254 return 1;
1256 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1257 bmap->n_div - (div + 1)) >= 0)
1258 return 0;
1260 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1261 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1262 total + bmap->n_div);
1264 return last_ineq < last_div;
1267 /* Given two constraints "k" and "l" that are opposite to each other,
1268 * except for the constant term, check if we can use them
1269 * to obtain an expression for one of the hitherto unknown divs or
1270 * a "better" expression for a div for which we already have an expression.
1271 * "sum" is the sum of the constant terms of the constraints.
1272 * If this sum is strictly smaller than the coefficient of one
1273 * of the divs, then this pair can be used define the div.
1274 * To avoid the introduction of circular definitions of divs, we
1275 * do not use the pair if the resulting expression would refer to
1276 * any other undefined divs or if any known div is defined in
1277 * terms of the unknown div.
1279 static struct isl_basic_map *check_for_div_constraints(
1280 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1282 int i;
1283 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1285 for (i = 0; i < bmap->n_div; ++i) {
1286 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1287 continue;
1288 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1289 continue;
1290 if (!better_div_constraint(bmap, i, k))
1291 continue;
1292 if (!ok_to_set_div_from_bound(bmap, i, k))
1293 break;
1294 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1295 bmap = set_div_from_lower_bound(bmap, i, k);
1296 else
1297 bmap = set_div_from_lower_bound(bmap, i, l);
1298 if (progress)
1299 *progress = 1;
1300 break;
1302 return bmap;
1305 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1306 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1308 struct isl_constraint_index ci;
1309 int k, l, h;
1310 unsigned total = isl_basic_map_total_dim(bmap);
1311 isl_int sum;
1313 if (!bmap || bmap->n_ineq <= 1)
1314 return bmap;
1316 if (create_constraint_index(&ci, bmap) < 0)
1317 return bmap;
1319 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1320 ci.index[h] = &bmap->ineq[0];
1321 for (k = 1; k < bmap->n_ineq; ++k) {
1322 h = hash_index(&ci, bmap, k);
1323 if (!ci.index[h]) {
1324 ci.index[h] = &bmap->ineq[k];
1325 continue;
1327 if (progress)
1328 *progress = 1;
1329 l = ci.index[h] - &bmap->ineq[0];
1330 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1331 swap_inequality(bmap, k, l);
1332 isl_basic_map_drop_inequality(bmap, k);
1333 --k;
1335 isl_int_init(sum);
1336 for (k = 0; k < bmap->n_ineq-1; ++k) {
1337 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1338 h = hash_index(&ci, bmap, k);
1339 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1340 if (!ci.index[h])
1341 continue;
1342 l = ci.index[h] - &bmap->ineq[0];
1343 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1344 if (isl_int_is_pos(sum)) {
1345 if (detect_divs)
1346 bmap = check_for_div_constraints(bmap, k, l,
1347 sum, progress);
1348 continue;
1350 if (isl_int_is_zero(sum)) {
1351 /* We need to break out of the loop after these
1352 * changes since the contents of the hash
1353 * will no longer be valid.
1354 * Plus, we probably we want to regauss first.
1356 if (progress)
1357 *progress = 1;
1358 isl_basic_map_drop_inequality(bmap, l);
1359 isl_basic_map_inequality_to_equality(bmap, k);
1360 } else
1361 bmap = isl_basic_map_set_to_empty(bmap);
1362 break;
1364 isl_int_clear(sum);
1366 constraint_index_free(&ci);
1367 return bmap;
1370 /* Detect all pairs of inequalities that form an equality.
1372 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1373 * Call it repeatedly while it is making progress.
1375 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1376 __isl_take isl_basic_map *bmap, int *progress)
1378 int duplicate;
1380 do {
1381 duplicate = 0;
1382 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1383 &duplicate, 0);
1384 if (progress && duplicate)
1385 *progress = 1;
1386 } while (duplicate);
1388 return bmap;
1391 /* Eliminate knowns divs from constraints where they appear with
1392 * a (positive or negative) unit coefficient.
1394 * That is, replace
1396 * floor(e/m) + f >= 0
1398 * by
1400 * e + m f >= 0
1402 * and
1404 * -floor(e/m) + f >= 0
1406 * by
1408 * -e + m f + m - 1 >= 0
1410 * The first conversion is valid because floor(e/m) >= -f is equivalent
1411 * to e/m >= -f because -f is an integral expression.
1412 * The second conversion follows from the fact that
1414 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1417 * Note that one of the div constraints may have been eliminated
1418 * due to being redundant with respect to the constraint that is
1419 * being modified by this function. The modified constraint may
1420 * no longer imply this div constraint, so we add it back to make
1421 * sure we do not lose any information.
1423 * We skip integral divs, i.e., those with denominator 1, as we would
1424 * risk eliminating the div from the div constraints. We do not need
1425 * to handle those divs here anyway since the div constraints will turn
1426 * out to form an equality and this equality can then be use to eliminate
1427 * the div from all constraints.
1429 static __isl_give isl_basic_map *eliminate_unit_divs(
1430 __isl_take isl_basic_map *bmap, int *progress)
1432 int i, j;
1433 isl_ctx *ctx;
1434 unsigned total;
1436 if (!bmap)
1437 return NULL;
1439 ctx = isl_basic_map_get_ctx(bmap);
1440 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1442 for (i = 0; i < bmap->n_div; ++i) {
1443 if (isl_int_is_zero(bmap->div[i][0]))
1444 continue;
1445 if (isl_int_is_one(bmap->div[i][0]))
1446 continue;
1447 for (j = 0; j < bmap->n_ineq; ++j) {
1448 int s;
1450 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1451 !isl_int_is_negone(bmap->ineq[j][total + i]))
1452 continue;
1454 *progress = 1;
1456 s = isl_int_sgn(bmap->ineq[j][total + i]);
1457 isl_int_set_si(bmap->ineq[j][total + i], 0);
1458 if (s < 0)
1459 isl_seq_combine(bmap->ineq[j],
1460 ctx->negone, bmap->div[i] + 1,
1461 bmap->div[i][0], bmap->ineq[j],
1462 total + bmap->n_div);
1463 else
1464 isl_seq_combine(bmap->ineq[j],
1465 ctx->one, bmap->div[i] + 1,
1466 bmap->div[i][0], bmap->ineq[j],
1467 total + bmap->n_div);
1468 if (s < 0) {
1469 isl_int_add(bmap->ineq[j][0],
1470 bmap->ineq[j][0], bmap->div[i][0]);
1471 isl_int_sub_ui(bmap->ineq[j][0],
1472 bmap->ineq[j][0], 1);
1475 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1476 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1477 return isl_basic_map_free(bmap);
1481 return bmap;
1484 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1486 int progress = 1;
1487 if (!bmap)
1488 return NULL;
1489 while (progress) {
1490 progress = 0;
1491 if (!bmap)
1492 break;
1493 if (isl_basic_map_plain_is_empty(bmap))
1494 break;
1495 bmap = isl_basic_map_normalize_constraints(bmap);
1496 bmap = remove_independent_vars_from_divs(bmap);
1497 bmap = normalize_div_expressions(bmap);
1498 bmap = remove_duplicate_divs(bmap, &progress);
1499 bmap = eliminate_unit_divs(bmap, &progress);
1500 bmap = eliminate_divs_eq(bmap, &progress);
1501 bmap = eliminate_divs_ineq(bmap, &progress);
1502 bmap = isl_basic_map_gauss(bmap, &progress);
1503 /* requires equalities in normal form */
1504 bmap = normalize_divs(bmap, &progress);
1505 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1506 &progress, 1);
1507 if (bmap && progress)
1508 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1510 return bmap;
1513 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1515 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1519 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1520 isl_int *constraint, unsigned div)
1522 unsigned pos;
1524 if (!bmap)
1525 return -1;
1527 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1529 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1530 int neg;
1531 isl_int_sub(bmap->div[div][1],
1532 bmap->div[div][1], bmap->div[div][0]);
1533 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1534 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1535 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1536 isl_int_add(bmap->div[div][1],
1537 bmap->div[div][1], bmap->div[div][0]);
1538 if (!neg)
1539 return 0;
1540 if (isl_seq_first_non_zero(constraint+pos+1,
1541 bmap->n_div-div-1) != -1)
1542 return 0;
1543 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1544 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
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
1550 return 0;
1552 return 1;
1555 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1556 isl_int *constraint, unsigned div)
1558 return isl_basic_map_is_div_constraint(bset, constraint, div);
1562 /* If the only constraints a div d=floor(f/m)
1563 * appears in are its two defining constraints
1565 * f - m d >=0
1566 * -(f - (m - 1)) + m d >= 0
1568 * then it can safely be removed.
1570 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1572 int i;
1573 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1575 for (i = 0; i < bmap->n_eq; ++i)
1576 if (!isl_int_is_zero(bmap->eq[i][pos]))
1577 return 0;
1579 for (i = 0; i < bmap->n_ineq; ++i) {
1580 if (isl_int_is_zero(bmap->ineq[i][pos]))
1581 continue;
1582 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1583 return 0;
1586 for (i = 0; i < bmap->n_div; ++i) {
1587 if (isl_int_is_zero(bmap->div[i][0]))
1588 continue;
1589 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1590 return 0;
1593 return 1;
1597 * Remove divs that don't occur in any of the constraints or other divs.
1598 * These can arise when dropping constraints from a basic map or
1599 * when the divs of a basic map have been temporarily aligned
1600 * with the divs of another basic map.
1602 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1604 int i;
1606 if (!bmap)
1607 return NULL;
1609 for (i = bmap->n_div-1; i >= 0; --i) {
1610 if (!div_is_redundant(bmap, i))
1611 continue;
1612 bmap = isl_basic_map_drop_div(bmap, i);
1614 return bmap;
1617 /* Mark "bmap" as final, without checking for obviously redundant
1618 * integer divisions. This function should be used when "bmap"
1619 * is known not to involve any such integer divisions.
1621 __isl_give isl_basic_map *isl_basic_map_mark_final(
1622 __isl_take isl_basic_map *bmap)
1624 if (!bmap)
1625 return NULL;
1626 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1627 return bmap;
1630 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1632 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1634 bmap = remove_redundant_divs(bmap);
1635 bmap = isl_basic_map_mark_final(bmap);
1636 return bmap;
1639 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1641 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1644 struct isl_set *isl_set_finalize(struct isl_set *set)
1646 int i;
1648 if (!set)
1649 return NULL;
1650 for (i = 0; i < set->n; ++i) {
1651 set->p[i] = isl_basic_set_finalize(set->p[i]);
1652 if (!set->p[i])
1653 goto error;
1655 return set;
1656 error:
1657 isl_set_free(set);
1658 return NULL;
1661 struct isl_map *isl_map_finalize(struct isl_map *map)
1663 int i;
1665 if (!map)
1666 return NULL;
1667 for (i = 0; i < map->n; ++i) {
1668 map->p[i] = isl_basic_map_finalize(map->p[i]);
1669 if (!map->p[i])
1670 goto error;
1672 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1673 return map;
1674 error:
1675 isl_map_free(map);
1676 return NULL;
1680 /* Remove definition of any div that is defined in terms of the given variable.
1681 * The div itself is not removed. Functions such as
1682 * eliminate_divs_ineq depend on the other divs remaining in place.
1684 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1685 int pos)
1687 int i;
1689 if (!bmap)
1690 return NULL;
1692 for (i = 0; i < bmap->n_div; ++i) {
1693 if (isl_int_is_zero(bmap->div[i][0]))
1694 continue;
1695 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1696 continue;
1697 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1698 if (!bmap)
1699 return NULL;
1701 return bmap;
1704 /* Eliminate the specified variables from the constraints using
1705 * Fourier-Motzkin. The variables themselves are not removed.
1707 struct isl_basic_map *isl_basic_map_eliminate_vars(
1708 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1710 int d;
1711 int i, j, k;
1712 unsigned total;
1713 int need_gauss = 0;
1715 if (n == 0)
1716 return bmap;
1717 if (!bmap)
1718 return NULL;
1719 total = isl_basic_map_total_dim(bmap);
1721 bmap = isl_basic_map_cow(bmap);
1722 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1723 bmap = remove_dependent_vars(bmap, d);
1724 if (!bmap)
1725 return NULL;
1727 for (d = pos + n - 1;
1728 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1729 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1730 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1731 int n_lower, n_upper;
1732 if (!bmap)
1733 return NULL;
1734 for (i = 0; i < bmap->n_eq; ++i) {
1735 if (isl_int_is_zero(bmap->eq[i][1+d]))
1736 continue;
1737 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1738 isl_basic_map_drop_equality(bmap, i);
1739 need_gauss = 1;
1740 break;
1742 if (i < bmap->n_eq)
1743 continue;
1744 n_lower = 0;
1745 n_upper = 0;
1746 for (i = 0; i < bmap->n_ineq; ++i) {
1747 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1748 n_lower++;
1749 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1750 n_upper++;
1752 bmap = isl_basic_map_extend_constraints(bmap,
1753 0, n_lower * n_upper);
1754 if (!bmap)
1755 goto error;
1756 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1757 int last;
1758 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1759 continue;
1760 last = -1;
1761 for (j = 0; j < i; ++j) {
1762 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1763 continue;
1764 last = j;
1765 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1766 isl_int_sgn(bmap->ineq[j][1+d]))
1767 continue;
1768 k = isl_basic_map_alloc_inequality(bmap);
1769 if (k < 0)
1770 goto error;
1771 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1772 1+total);
1773 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1774 1+d, 1+total, NULL);
1776 isl_basic_map_drop_inequality(bmap, i);
1777 i = last + 1;
1779 if (n_lower > 0 && n_upper > 0) {
1780 bmap = isl_basic_map_normalize_constraints(bmap);
1781 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1782 NULL, 0);
1783 bmap = isl_basic_map_gauss(bmap, NULL);
1784 bmap = isl_basic_map_remove_redundancies(bmap);
1785 need_gauss = 0;
1786 if (!bmap)
1787 goto error;
1788 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1789 break;
1792 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1793 if (need_gauss)
1794 bmap = isl_basic_map_gauss(bmap, NULL);
1795 return bmap;
1796 error:
1797 isl_basic_map_free(bmap);
1798 return NULL;
1801 struct isl_basic_set *isl_basic_set_eliminate_vars(
1802 struct isl_basic_set *bset, unsigned pos, unsigned n)
1804 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1805 pos, n));
1808 /* Eliminate the specified n dimensions starting at first from the
1809 * constraints, without removing the dimensions from the space.
1810 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1811 * Otherwise, they are projected out and the original space is restored.
1813 __isl_give isl_basic_map *isl_basic_map_eliminate(
1814 __isl_take isl_basic_map *bmap,
1815 enum isl_dim_type type, unsigned first, unsigned n)
1817 isl_space *space;
1819 if (!bmap)
1820 return NULL;
1821 if (n == 0)
1822 return bmap;
1824 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1825 isl_die(bmap->ctx, isl_error_invalid,
1826 "index out of bounds", goto error);
1828 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1829 first += isl_basic_map_offset(bmap, type) - 1;
1830 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1831 return isl_basic_map_finalize(bmap);
1834 space = isl_basic_map_get_space(bmap);
1835 bmap = isl_basic_map_project_out(bmap, type, first, n);
1836 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1837 bmap = isl_basic_map_reset_space(bmap, space);
1838 return bmap;
1839 error:
1840 isl_basic_map_free(bmap);
1841 return NULL;
1844 __isl_give isl_basic_set *isl_basic_set_eliminate(
1845 __isl_take isl_basic_set *bset,
1846 enum isl_dim_type type, unsigned first, unsigned n)
1848 return isl_basic_map_eliminate(bset, type, first, n);
1851 /* Remove all constraints from "bmap" that reference any unknown local
1852 * variables (directly or indirectly).
1854 * Dropping all constraints on a local variable will make it redundant,
1855 * so it will get removed implicitly by
1856 * isl_basic_map_drop_constraints_involving_dims. Some other local
1857 * variables may also end up becoming redundant if they only appear
1858 * in constraints together with the unknown local variable.
1859 * Therefore, start over after calling
1860 * isl_basic_map_drop_constraints_involving_dims.
1862 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1863 __isl_take isl_basic_map *bmap)
1865 isl_bool known;
1866 int i, n_div, o_div;
1868 known = isl_basic_map_divs_known(bmap);
1869 if (known < 0)
1870 return isl_basic_map_free(bmap);
1871 if (known)
1872 return bmap;
1874 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1875 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1877 for (i = 0; i < n_div; ++i) {
1878 known = isl_basic_map_div_is_known(bmap, i);
1879 if (known < 0)
1880 return isl_basic_map_free(bmap);
1881 if (known)
1882 continue;
1883 bmap = remove_dependent_vars(bmap, o_div + i);
1884 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1885 isl_dim_div, i, 1);
1886 if (!bmap)
1887 return NULL;
1888 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1889 i = -1;
1892 return bmap;
1895 /* Remove all constraints from "map" that reference any unknown local
1896 * variables (directly or indirectly).
1898 * Since constraints may get dropped from the basic maps,
1899 * they may no longer be disjoint from each other.
1901 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1902 __isl_take isl_map *map)
1904 int i;
1905 isl_bool known;
1907 known = isl_map_divs_known(map);
1908 if (known < 0)
1909 return isl_map_free(map);
1910 if (known)
1911 return map;
1913 map = isl_map_cow(map);
1914 if (!map)
1915 return NULL;
1917 for (i = 0; i < map->n; ++i) {
1918 map->p[i] =
1919 isl_basic_map_drop_constraint_involving_unknown_divs(
1920 map->p[i]);
1921 if (!map->p[i])
1922 return isl_map_free(map);
1925 if (map->n > 1)
1926 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1928 return map;
1931 /* Don't assume equalities are in order, because align_divs
1932 * may have changed the order of the divs.
1934 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1936 int d, i;
1937 unsigned total;
1939 total = isl_space_dim(bmap->dim, isl_dim_all);
1940 for (d = 0; d < total; ++d)
1941 elim[d] = -1;
1942 for (i = 0; i < bmap->n_eq; ++i) {
1943 for (d = total - 1; d >= 0; --d) {
1944 if (isl_int_is_zero(bmap->eq[i][1+d]))
1945 continue;
1946 elim[d] = i;
1947 break;
1952 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1954 compute_elimination_index(bset_to_bmap(bset), elim);
1957 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1958 struct isl_basic_map *bmap, int *elim)
1960 int d;
1961 int copied = 0;
1962 unsigned total;
1964 total = isl_space_dim(bmap->dim, isl_dim_all);
1965 for (d = total - 1; d >= 0; --d) {
1966 if (isl_int_is_zero(src[1+d]))
1967 continue;
1968 if (elim[d] == -1)
1969 continue;
1970 if (!copied) {
1971 isl_seq_cpy(dst, src, 1 + total);
1972 copied = 1;
1974 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1976 return copied;
1979 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1980 struct isl_basic_set *bset, int *elim)
1982 return reduced_using_equalities(dst, src,
1983 bset_to_bmap(bset), elim);
1986 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1987 struct isl_basic_set *bset, struct isl_basic_set *context)
1989 int i;
1990 int *elim;
1992 if (!bset || !context)
1993 goto error;
1995 if (context->n_eq == 0) {
1996 isl_basic_set_free(context);
1997 return bset;
2000 bset = isl_basic_set_cow(bset);
2001 if (!bset)
2002 goto error;
2004 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2005 if (!elim)
2006 goto error;
2007 set_compute_elimination_index(context, elim);
2008 for (i = 0; i < bset->n_eq; ++i)
2009 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2010 context, elim);
2011 for (i = 0; i < bset->n_ineq; ++i)
2012 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2013 context, elim);
2014 isl_basic_set_free(context);
2015 free(elim);
2016 bset = isl_basic_set_simplify(bset);
2017 bset = isl_basic_set_finalize(bset);
2018 return bset;
2019 error:
2020 isl_basic_set_free(bset);
2021 isl_basic_set_free(context);
2022 return NULL;
2025 /* For each inequality in "ineq" that is a shifted (more relaxed)
2026 * copy of an inequality in "context", mark the corresponding entry
2027 * in "row" with -1.
2028 * If an inequality only has a non-negative constant term, then
2029 * mark it as well.
2031 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2032 __isl_keep isl_basic_set *context, int *row)
2034 struct isl_constraint_index ci;
2035 int n_ineq;
2036 unsigned total;
2037 int k;
2039 if (!ineq || !context)
2040 return isl_stat_error;
2041 if (context->n_ineq == 0)
2042 return isl_stat_ok;
2043 if (setup_constraint_index(&ci, context) < 0)
2044 return isl_stat_error;
2046 n_ineq = isl_mat_rows(ineq);
2047 total = isl_mat_cols(ineq) - 1;
2048 for (k = 0; k < n_ineq; ++k) {
2049 int l;
2050 isl_bool redundant;
2052 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2053 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2054 row[k] = -1;
2055 continue;
2057 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2058 if (redundant < 0)
2059 goto error;
2060 if (!redundant)
2061 continue;
2062 row[k] = -1;
2064 constraint_index_free(&ci);
2065 return isl_stat_ok;
2066 error:
2067 constraint_index_free(&ci);
2068 return isl_stat_error;
2071 static struct isl_basic_set *remove_shifted_constraints(
2072 struct isl_basic_set *bset, struct isl_basic_set *context)
2074 struct isl_constraint_index ci;
2075 int k;
2077 if (!bset || !context)
2078 return bset;
2080 if (context->n_ineq == 0)
2081 return bset;
2082 if (setup_constraint_index(&ci, context) < 0)
2083 return bset;
2085 for (k = 0; k < bset->n_ineq; ++k) {
2086 isl_bool redundant;
2088 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2089 if (redundant < 0)
2090 goto error;
2091 if (!redundant)
2092 continue;
2093 bset = isl_basic_set_cow(bset);
2094 if (!bset)
2095 goto error;
2096 isl_basic_set_drop_inequality(bset, k);
2097 --k;
2099 constraint_index_free(&ci);
2100 return bset;
2101 error:
2102 constraint_index_free(&ci);
2103 return bset;
2106 /* Remove constraints from "bmap" that are identical to constraints
2107 * in "context" or that are more relaxed (greater constant term).
2109 * We perform the test for shifted copies on the pure constraints
2110 * in remove_shifted_constraints.
2112 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2113 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2115 isl_basic_set *bset, *bset_context;
2117 if (!bmap || !context)
2118 goto error;
2120 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2121 isl_basic_map_free(context);
2122 return bmap;
2125 context = isl_basic_map_align_divs(context, bmap);
2126 bmap = isl_basic_map_align_divs(bmap, context);
2128 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2129 bset_context = isl_basic_map_underlying_set(context);
2130 bset = remove_shifted_constraints(bset, bset_context);
2131 isl_basic_set_free(bset_context);
2133 bmap = isl_basic_map_overlying_set(bset, bmap);
2135 return bmap;
2136 error:
2137 isl_basic_map_free(bmap);
2138 isl_basic_map_free(context);
2139 return NULL;
2142 /* Does the (linear part of a) constraint "c" involve any of the "len"
2143 * "relevant" dimensions?
2145 static int is_related(isl_int *c, int len, int *relevant)
2147 int i;
2149 for (i = 0; i < len; ++i) {
2150 if (!relevant[i])
2151 continue;
2152 if (!isl_int_is_zero(c[i]))
2153 return 1;
2156 return 0;
2159 /* Drop constraints from "bmap" that do not involve any of
2160 * the dimensions marked "relevant".
2162 static __isl_give isl_basic_map *drop_unrelated_constraints(
2163 __isl_take isl_basic_map *bmap, int *relevant)
2165 int i, dim;
2167 dim = isl_basic_map_dim(bmap, isl_dim_all);
2168 for (i = 0; i < dim; ++i)
2169 if (!relevant[i])
2170 break;
2171 if (i >= dim)
2172 return bmap;
2174 for (i = bmap->n_eq - 1; i >= 0; --i)
2175 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2176 bmap = isl_basic_map_cow(bmap);
2177 if (isl_basic_map_drop_equality(bmap, i) < 0)
2178 return isl_basic_map_free(bmap);
2181 for (i = bmap->n_ineq - 1; i >= 0; --i)
2182 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2183 bmap = isl_basic_map_cow(bmap);
2184 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2185 return isl_basic_map_free(bmap);
2188 return bmap;
2191 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2193 * In particular, for any variable involved in the constraint,
2194 * find the actual group id from before and replace the group
2195 * of the corresponding variable by the minimal group of all
2196 * the variables involved in the constraint considered so far
2197 * (if this minimum is smaller) or replace the minimum by this group
2198 * (if the minimum is larger).
2200 * At the end, all the variables in "c" will (indirectly) point
2201 * to the minimal of the groups that they referred to originally.
2203 static void update_groups(int dim, int *group, isl_int *c)
2205 int j;
2206 int min = dim;
2208 for (j = 0; j < dim; ++j) {
2209 if (isl_int_is_zero(c[j]))
2210 continue;
2211 while (group[j] >= 0 && group[group[j]] != group[j])
2212 group[j] = group[group[j]];
2213 if (group[j] == min)
2214 continue;
2215 if (group[j] < min) {
2216 if (min >= 0 && min < dim)
2217 group[min] = group[j];
2218 min = group[j];
2219 } else
2220 group[group[j]] = min;
2224 /* Allocate an array of groups of variables, one for each variable
2225 * in "context", initialized to zero.
2227 static int *alloc_groups(__isl_keep isl_basic_set *context)
2229 isl_ctx *ctx;
2230 int dim;
2232 dim = isl_basic_set_dim(context, isl_dim_set);
2233 ctx = isl_basic_set_get_ctx(context);
2234 return isl_calloc_array(ctx, int, dim);
2237 /* Drop constraints from "bmap" that only involve variables that are
2238 * not related to any of the variables marked with a "-1" in "group".
2240 * We construct groups of variables that collect variables that
2241 * (indirectly) appear in some common constraint of "bmap".
2242 * Each group is identified by the first variable in the group,
2243 * except for the special group of variables that was already identified
2244 * in the input as -1 (or are related to those variables).
2245 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2246 * otherwise the group of i is the group of group[i].
2248 * We first initialize groups for the remaining variables.
2249 * Then we iterate over the constraints of "bmap" and update the
2250 * group of the variables in the constraint by the smallest group.
2251 * Finally, we resolve indirect references to groups by running over
2252 * the variables.
2254 * After computing the groups, we drop constraints that do not involve
2255 * any variables in the -1 group.
2257 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2258 __isl_take isl_basic_map *bmap, __isl_take int *group)
2260 int dim;
2261 int i;
2262 int last;
2264 if (!bmap)
2265 return NULL;
2267 dim = isl_basic_map_dim(bmap, isl_dim_all);
2269 last = -1;
2270 for (i = 0; i < dim; ++i)
2271 if (group[i] >= 0)
2272 last = group[i] = i;
2273 if (last < 0) {
2274 free(group);
2275 return bmap;
2278 for (i = 0; i < bmap->n_eq; ++i)
2279 update_groups(dim, group, bmap->eq[i] + 1);
2280 for (i = 0; i < bmap->n_ineq; ++i)
2281 update_groups(dim, group, bmap->ineq[i] + 1);
2283 for (i = 0; i < dim; ++i)
2284 if (group[i] >= 0)
2285 group[i] = group[group[i]];
2287 for (i = 0; i < dim; ++i)
2288 group[i] = group[i] == -1;
2290 bmap = drop_unrelated_constraints(bmap, group);
2292 free(group);
2293 return bmap;
2296 /* Drop constraints from "context" that are irrelevant for computing
2297 * the gist of "bset".
2299 * In particular, drop constraints in variables that are not related
2300 * to any of the variables involved in the constraints of "bset"
2301 * in the sense that there is no sequence of constraints that connects them.
2303 * We first mark all variables that appear in "bset" as belonging
2304 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2306 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2307 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2309 int *group;
2310 int dim;
2311 int i, j;
2313 if (!context || !bset)
2314 return isl_basic_set_free(context);
2316 group = alloc_groups(context);
2318 if (!group)
2319 return isl_basic_set_free(context);
2321 dim = isl_basic_set_dim(bset, isl_dim_set);
2322 for (i = 0; i < dim; ++i) {
2323 for (j = 0; j < bset->n_eq; ++j)
2324 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2325 break;
2326 if (j < bset->n_eq) {
2327 group[i] = -1;
2328 continue;
2330 for (j = 0; j < bset->n_ineq; ++j)
2331 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2332 break;
2333 if (j < bset->n_ineq)
2334 group[i] = -1;
2337 return isl_basic_map_drop_unrelated_constraints(context, group);
2340 /* Drop constraints from "context" that are irrelevant for computing
2341 * the gist of the inequalities "ineq".
2342 * Inequalities in "ineq" for which the corresponding element of row
2343 * is set to -1 have already been marked for removal and should be ignored.
2345 * In particular, drop constraints in variables that are not related
2346 * to any of the variables involved in "ineq"
2347 * in the sense that there is no sequence of constraints that connects them.
2349 * We first mark all variables that appear in "bset" as belonging
2350 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2352 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2353 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2355 int *group;
2356 int dim;
2357 int i, j, n;
2359 if (!context || !ineq)
2360 return isl_basic_set_free(context);
2362 group = alloc_groups(context);
2364 if (!group)
2365 return isl_basic_set_free(context);
2367 dim = isl_basic_set_dim(context, isl_dim_set);
2368 n = isl_mat_rows(ineq);
2369 for (i = 0; i < dim; ++i) {
2370 for (j = 0; j < n; ++j) {
2371 if (row[j] < 0)
2372 continue;
2373 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2374 break;
2376 if (j < n)
2377 group[i] = -1;
2380 return isl_basic_map_drop_unrelated_constraints(context, group);
2383 /* Do all "n" entries of "row" contain a negative value?
2385 static int all_neg(int *row, int n)
2387 int i;
2389 for (i = 0; i < n; ++i)
2390 if (row[i] >= 0)
2391 return 0;
2393 return 1;
2396 /* Update the inequalities in "bset" based on the information in "row"
2397 * and "tab".
2399 * In particular, the array "row" contains either -1, meaning that
2400 * the corresponding inequality of "bset" is redundant, or the index
2401 * of an inequality in "tab".
2403 * If the row entry is -1, then drop the inequality.
2404 * Otherwise, if the constraint is marked redundant in the tableau,
2405 * then drop the inequality. Similarly, if it is marked as an equality
2406 * in the tableau, then turn the inequality into an equality and
2407 * perform Gaussian elimination.
2409 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2410 __isl_keep int *row, struct isl_tab *tab)
2412 int i;
2413 unsigned n_ineq;
2414 unsigned n_eq;
2415 int found_equality = 0;
2417 if (!bset)
2418 return NULL;
2419 if (tab && tab->empty)
2420 return isl_basic_set_set_to_empty(bset);
2422 n_ineq = bset->n_ineq;
2423 for (i = n_ineq - 1; i >= 0; --i) {
2424 if (row[i] < 0) {
2425 if (isl_basic_set_drop_inequality(bset, i) < 0)
2426 return isl_basic_set_free(bset);
2427 continue;
2429 if (!tab)
2430 continue;
2431 n_eq = tab->n_eq;
2432 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2433 isl_basic_map_inequality_to_equality(bset, i);
2434 found_equality = 1;
2435 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2436 if (isl_basic_set_drop_inequality(bset, i) < 0)
2437 return isl_basic_set_free(bset);
2441 if (found_equality)
2442 bset = isl_basic_set_gauss(bset, NULL);
2443 bset = isl_basic_set_finalize(bset);
2444 return bset;
2447 /* Update the inequalities in "bset" based on the information in "row"
2448 * and "tab" and free all arguments (other than "bset").
2450 static __isl_give isl_basic_set *update_ineq_free(
2451 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2452 __isl_take isl_basic_set *context, __isl_take int *row,
2453 struct isl_tab *tab)
2455 isl_mat_free(ineq);
2456 isl_basic_set_free(context);
2458 bset = update_ineq(bset, row, tab);
2460 free(row);
2461 isl_tab_free(tab);
2462 return bset;
2465 /* Remove all information from bset that is redundant in the context
2466 * of context.
2467 * "ineq" contains the (possibly transformed) inequalities of "bset",
2468 * in the same order.
2469 * The (explicit) equalities of "bset" are assumed to have been taken
2470 * into account by the transformation such that only the inequalities
2471 * are relevant.
2472 * "context" is assumed not to be empty.
2474 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2475 * A value of -1 means that the inequality is obviously redundant and may
2476 * not even appear in "tab".
2478 * We first mark the inequalities of "bset"
2479 * that are obviously redundant with respect to some inequality in "context".
2480 * Then we remove those constraints from "context" that have become
2481 * irrelevant for computing the gist of "bset".
2482 * Note that this removal of constraints cannot be replaced by
2483 * a factorization because factors in "bset" may still be connected
2484 * to each other through constraints in "context".
2486 * If there are any inequalities left, we construct a tableau for
2487 * the context and then add the inequalities of "bset".
2488 * Before adding these inequalities, we freeze all constraints such that
2489 * they won't be considered redundant in terms of the constraints of "bset".
2490 * Then we detect all redundant constraints (among the
2491 * constraints that weren't frozen), first by checking for redundancy in the
2492 * the tableau and then by checking if replacing a constraint by its negation
2493 * would lead to an empty set. This last step is fairly expensive
2494 * and could be optimized by more reuse of the tableau.
2495 * Finally, we update bset according to the results.
2497 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2498 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2500 int i, r;
2501 int *row = NULL;
2502 isl_ctx *ctx;
2503 isl_basic_set *combined = NULL;
2504 struct isl_tab *tab = NULL;
2505 unsigned n_eq, context_ineq;
2506 unsigned total;
2508 if (!bset || !ineq || !context)
2509 goto error;
2511 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2512 isl_basic_set_free(context);
2513 isl_mat_free(ineq);
2514 return bset;
2517 ctx = isl_basic_set_get_ctx(context);
2518 row = isl_calloc_array(ctx, int, bset->n_ineq);
2519 if (!row)
2520 goto error;
2522 if (mark_shifted_constraints(ineq, context, row) < 0)
2523 goto error;
2524 if (all_neg(row, bset->n_ineq))
2525 return update_ineq_free(bset, ineq, context, row, NULL);
2527 context = drop_irrelevant_constraints_marked(context, ineq, row);
2528 if (!context)
2529 goto error;
2530 if (isl_basic_set_plain_is_universe(context))
2531 return update_ineq_free(bset, ineq, context, row, NULL);
2533 n_eq = context->n_eq;
2534 context_ineq = context->n_ineq;
2535 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2536 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2537 tab = isl_tab_from_basic_set(combined, 0);
2538 for (i = 0; i < context_ineq; ++i)
2539 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2540 goto error;
2541 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2542 goto error;
2543 r = context_ineq;
2544 for (i = 0; i < bset->n_ineq; ++i) {
2545 if (row[i] < 0)
2546 continue;
2547 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2548 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2549 goto error;
2550 row[i] = r++;
2552 if (isl_tab_detect_implicit_equalities(tab) < 0)
2553 goto error;
2554 if (isl_tab_detect_redundant(tab) < 0)
2555 goto error;
2556 total = isl_basic_set_total_dim(bset);
2557 for (i = bset->n_ineq - 1; i >= 0; --i) {
2558 isl_basic_set *test;
2559 int is_empty;
2561 if (row[i] < 0)
2562 continue;
2563 r = row[i];
2564 if (tab->con[n_eq + r].is_redundant)
2565 continue;
2566 test = isl_basic_set_dup(combined);
2567 if (isl_inequality_negate(test, r) < 0)
2568 test = isl_basic_set_free(test);
2569 test = isl_basic_set_update_from_tab(test, tab);
2570 is_empty = isl_basic_set_is_empty(test);
2571 isl_basic_set_free(test);
2572 if (is_empty < 0)
2573 goto error;
2574 if (is_empty)
2575 tab->con[n_eq + r].is_redundant = 1;
2577 bset = update_ineq_free(bset, ineq, context, row, tab);
2578 if (bset) {
2579 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2580 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2583 isl_basic_set_free(combined);
2584 return bset;
2585 error:
2586 free(row);
2587 isl_mat_free(ineq);
2588 isl_tab_free(tab);
2589 isl_basic_set_free(combined);
2590 isl_basic_set_free(context);
2591 isl_basic_set_free(bset);
2592 return NULL;
2595 /* Extract the inequalities of "bset" as an isl_mat.
2597 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2599 unsigned total;
2600 isl_ctx *ctx;
2601 isl_mat *ineq;
2603 if (!bset)
2604 return NULL;
2606 ctx = isl_basic_set_get_ctx(bset);
2607 total = isl_basic_set_total_dim(bset);
2608 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2609 0, 1 + total);
2611 return ineq;
2614 /* Remove all information from "bset" that is redundant in the context
2615 * of "context", for the case where both "bset" and "context" are
2616 * full-dimensional.
2618 static __isl_give isl_basic_set *uset_gist_uncompressed(
2619 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2621 isl_mat *ineq;
2623 ineq = extract_ineq(bset);
2624 return uset_gist_full(bset, ineq, context);
2627 /* Remove all information from "bset" that is redundant in the context
2628 * of "context", for the case where the combined equalities of
2629 * "bset" and "context" allow for a compression that can be obtained
2630 * by preapplication of "T".
2632 * "bset" itself is not transformed by "T". Instead, the inequalities
2633 * are extracted from "bset" and those are transformed by "T".
2634 * uset_gist_full then determines which of the transformed inequalities
2635 * are redundant with respect to the transformed "context" and removes
2636 * the corresponding inequalities from "bset".
2638 * After preapplying "T" to the inequalities, any common factor is
2639 * removed from the coefficients. If this results in a tightening
2640 * of the constant term, then the same tightening is applied to
2641 * the corresponding untransformed inequality in "bset".
2642 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2644 * g f'(x) + r >= 0
2646 * with 0 <= r < g, then it is equivalent to
2648 * f'(x) >= 0
2650 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2651 * subspace compressed by T since the latter would be transformed to
2653 * g f'(x) >= 0
2655 static __isl_give isl_basic_set *uset_gist_compressed(
2656 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2657 __isl_take isl_mat *T)
2659 isl_ctx *ctx;
2660 isl_mat *ineq;
2661 int i, n_row, n_col;
2662 isl_int rem;
2664 ineq = extract_ineq(bset);
2665 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2666 context = isl_basic_set_preimage(context, T);
2668 if (!ineq || !context)
2669 goto error;
2670 if (isl_basic_set_plain_is_empty(context)) {
2671 isl_mat_free(ineq);
2672 isl_basic_set_free(context);
2673 return isl_basic_set_set_to_empty(bset);
2676 ctx = isl_mat_get_ctx(ineq);
2677 n_row = isl_mat_rows(ineq);
2678 n_col = isl_mat_cols(ineq);
2679 isl_int_init(rem);
2680 for (i = 0; i < n_row; ++i) {
2681 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2682 if (isl_int_is_zero(ctx->normalize_gcd))
2683 continue;
2684 if (isl_int_is_one(ctx->normalize_gcd))
2685 continue;
2686 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2687 ctx->normalize_gcd, n_col - 1);
2688 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2689 isl_int_fdiv_q(ineq->row[i][0],
2690 ineq->row[i][0], ctx->normalize_gcd);
2691 if (isl_int_is_zero(rem))
2692 continue;
2693 bset = isl_basic_set_cow(bset);
2694 if (!bset)
2695 break;
2696 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2698 isl_int_clear(rem);
2700 return uset_gist_full(bset, ineq, context);
2701 error:
2702 isl_mat_free(ineq);
2703 isl_basic_set_free(context);
2704 isl_basic_set_free(bset);
2705 return NULL;
2708 /* Project "bset" onto the variables that are involved in "template".
2710 static __isl_give isl_basic_set *project_onto_involved(
2711 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2713 int i, n;
2715 if (!bset || !template)
2716 return isl_basic_set_free(bset);
2718 n = isl_basic_set_dim(template, isl_dim_set);
2720 for (i = 0; i < n; ++i) {
2721 isl_bool involved;
2723 involved = isl_basic_set_involves_dims(template,
2724 isl_dim_set, i, 1);
2725 if (involved < 0)
2726 return isl_basic_set_free(bset);
2727 if (involved)
2728 continue;
2729 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2732 return bset;
2735 /* Remove all information from bset that is redundant in the context
2736 * of context. In particular, equalities that are linear combinations
2737 * of those in context are removed. Then the inequalities that are
2738 * redundant in the context of the equalities and inequalities of
2739 * context are removed.
2741 * First of all, we drop those constraints from "context"
2742 * that are irrelevant for computing the gist of "bset".
2743 * Alternatively, we could factorize the intersection of "context" and "bset".
2745 * We first compute the intersection of the integer affine hulls
2746 * of "bset" and "context",
2747 * compute the gist inside this intersection and then reduce
2748 * the constraints with respect to the equalities of the context
2749 * that only involve variables already involved in the input.
2751 * If two constraints are mutually redundant, then uset_gist_full
2752 * will remove the second of those constraints. We therefore first
2753 * sort the constraints so that constraints not involving existentially
2754 * quantified variables are given precedence over those that do.
2755 * We have to perform this sorting before the variable compression,
2756 * because that may effect the order of the variables.
2758 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2759 __isl_take isl_basic_set *context)
2761 isl_mat *eq;
2762 isl_mat *T;
2763 isl_basic_set *aff;
2764 isl_basic_set *aff_context;
2765 unsigned total;
2767 if (!bset || !context)
2768 goto error;
2770 context = drop_irrelevant_constraints(context, bset);
2772 bset = isl_basic_set_detect_equalities(bset);
2773 aff = isl_basic_set_copy(bset);
2774 aff = isl_basic_set_plain_affine_hull(aff);
2775 context = isl_basic_set_detect_equalities(context);
2776 aff_context = isl_basic_set_copy(context);
2777 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2778 aff = isl_basic_set_intersect(aff, aff_context);
2779 if (!aff)
2780 goto error;
2781 if (isl_basic_set_plain_is_empty(aff)) {
2782 isl_basic_set_free(bset);
2783 isl_basic_set_free(context);
2784 return aff;
2786 bset = isl_basic_set_sort_constraints(bset);
2787 if (aff->n_eq == 0) {
2788 isl_basic_set_free(aff);
2789 return uset_gist_uncompressed(bset, context);
2791 total = isl_basic_set_total_dim(bset);
2792 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2793 eq = isl_mat_cow(eq);
2794 T = isl_mat_variable_compression(eq, NULL);
2795 isl_basic_set_free(aff);
2796 if (T && T->n_col == 0) {
2797 isl_mat_free(T);
2798 isl_basic_set_free(context);
2799 return isl_basic_set_set_to_empty(bset);
2802 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2803 aff_context = project_onto_involved(aff_context, bset);
2805 bset = uset_gist_compressed(bset, context, T);
2806 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2808 if (bset) {
2809 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2810 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2813 return bset;
2814 error:
2815 isl_basic_set_free(bset);
2816 isl_basic_set_free(context);
2817 return NULL;
2820 /* Return the number of equality constraints in "bmap" that involve
2821 * local variables. This function assumes that Gaussian elimination
2822 * has been applied to the equality constraints.
2824 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2826 int i;
2827 int total, n_div;
2829 if (!bmap)
2830 return -1;
2832 if (bmap->n_eq == 0)
2833 return 0;
2835 total = isl_basic_map_dim(bmap, isl_dim_all);
2836 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2837 total -= n_div;
2839 for (i = 0; i < bmap->n_eq; ++i)
2840 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2841 n_div) == -1)
2842 return i;
2844 return bmap->n_eq;
2847 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2848 * The constraints are assumed not to involve any local variables.
2850 static __isl_give isl_basic_map *basic_map_from_equalities(
2851 __isl_take isl_space *space, __isl_take isl_mat *eq)
2853 int i, k;
2854 isl_basic_map *bmap = NULL;
2856 if (!space || !eq)
2857 goto error;
2859 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2860 isl_die(isl_space_get_ctx(space), isl_error_internal,
2861 "unexpected number of columns", goto error);
2863 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2864 0, eq->n_row, 0);
2865 for (i = 0; i < eq->n_row; ++i) {
2866 k = isl_basic_map_alloc_equality(bmap);
2867 if (k < 0)
2868 goto error;
2869 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2872 isl_space_free(space);
2873 isl_mat_free(eq);
2874 return bmap;
2875 error:
2876 isl_space_free(space);
2877 isl_mat_free(eq);
2878 isl_basic_map_free(bmap);
2879 return NULL;
2882 /* Construct and return a variable compression based on the equality
2883 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2884 * "n1" is the number of (initial) equality constraints in "bmap1"
2885 * that do involve local variables.
2886 * "n2" is the number of (initial) equality constraints in "bmap2"
2887 * that do involve local variables.
2888 * "total" is the total number of other variables.
2889 * This function assumes that Gaussian elimination
2890 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2891 * such that the equality constraints not involving local variables
2892 * are those that start at "n1" or "n2".
2894 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2895 * then simply compute the compression based on the equality constraints
2896 * in the other basic map.
2897 * Otherwise, combine the equality constraints from both into a new
2898 * basic map such that Gaussian elimination can be applied to this combination
2899 * and then construct a variable compression from the resulting
2900 * equality constraints.
2902 static __isl_give isl_mat *combined_variable_compression(
2903 __isl_keep isl_basic_map *bmap1, int n1,
2904 __isl_keep isl_basic_map *bmap2, int n2, int total)
2906 isl_ctx *ctx;
2907 isl_mat *E1, *E2, *V;
2908 isl_basic_map *bmap;
2910 ctx = isl_basic_map_get_ctx(bmap1);
2911 if (bmap1->n_eq == n1) {
2912 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2913 n2, bmap2->n_eq - n2, 0, 1 + total);
2914 return isl_mat_variable_compression(E2, NULL);
2916 if (bmap2->n_eq == n2) {
2917 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2918 n1, bmap1->n_eq - n1, 0, 1 + total);
2919 return isl_mat_variable_compression(E1, NULL);
2921 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2922 n1, bmap1->n_eq - n1, 0, 1 + total);
2923 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2924 n2, bmap2->n_eq - n2, 0, 1 + total);
2925 E1 = isl_mat_concat(E1, E2);
2926 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2927 bmap = isl_basic_map_gauss(bmap, NULL);
2928 if (!bmap)
2929 return NULL;
2930 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2931 V = isl_mat_variable_compression(E1, NULL);
2932 isl_basic_map_free(bmap);
2934 return V;
2937 /* Extract the stride constraints from "bmap", compressed
2938 * with respect to both the stride constraints in "context" and
2939 * the remaining equality constraints in both "bmap" and "context".
2940 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2941 * "context_n_eq" is the number of (initial) stride constraints in "context".
2943 * Let x be all variables in "bmap" (and "context") other than the local
2944 * variables. First compute a variable compression
2946 * x = V x'
2948 * based on the non-stride equality constraints in "bmap" and "context".
2949 * Consider the stride constraints of "context",
2951 * A(x) + B(y) = 0
2953 * with y the local variables and plug in the variable compression,
2954 * resulting in
2956 * A(V x') + B(y) = 0
2958 * Use these constraints to compute a parameter compression on x'
2960 * x' = T x''
2962 * Now consider the stride constraints of "bmap"
2964 * C(x) + D(y) = 0
2966 * and plug in x = V*T x''.
2967 * That is, return A = [C*V*T D].
2969 static __isl_give isl_mat *extract_compressed_stride_constraints(
2970 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2971 __isl_keep isl_basic_map *context, int context_n_eq)
2973 int total, n_div;
2974 isl_ctx *ctx;
2975 isl_mat *A, *B, *T, *V;
2977 total = isl_basic_map_dim(context, isl_dim_all);
2978 n_div = isl_basic_map_dim(context, isl_dim_div);
2979 total -= n_div;
2981 ctx = isl_basic_map_get_ctx(bmap);
2983 V = combined_variable_compression(bmap, bmap_n_eq,
2984 context, context_n_eq, total);
2986 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2987 B = isl_mat_sub_alloc6(ctx, context->eq,
2988 0, context_n_eq, 1 + total, n_div);
2989 A = isl_mat_product(A, isl_mat_copy(V));
2990 T = isl_mat_parameter_compression_ext(A, B);
2991 T = isl_mat_product(V, T);
2993 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2994 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2996 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2997 0, bmap_n_eq, 0, 1 + total + n_div);
2998 A = isl_mat_product(A, T);
3000 return A;
3003 /* Remove the prime factors from *g that have an exponent that
3004 * is strictly smaller than the exponent in "c".
3005 * All exponents in *g are known to be smaller than or equal
3006 * to those in "c".
3008 * That is, if *g is equal to
3010 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3012 * and "c" is equal to
3014 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3016 * then update *g to
3018 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3019 * p_n^{e_n * (e_n = f_n)}
3021 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3022 * neither does the gcd of *g and c / *g.
3023 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3024 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3025 * Dividing *g by this gcd therefore strictly reduces the exponent
3026 * of the prime factors that need to be removed, while leaving the
3027 * other prime factors untouched.
3028 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3029 * removes all undesired factors, without removing any others.
3031 static void remove_incomplete_powers(isl_int *g, isl_int c)
3033 isl_int t;
3035 isl_int_init(t);
3036 for (;;) {
3037 isl_int_divexact(t, c, *g);
3038 isl_int_gcd(t, t, *g);
3039 if (isl_int_is_one(t))
3040 break;
3041 isl_int_divexact(*g, *g, t);
3043 isl_int_clear(t);
3046 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3047 * of the same stride constraints in a compressed space that exploits
3048 * all equalities in the context and the other equalities in "bmap".
3050 * If the stride constraints of "bmap" are of the form
3052 * C(x) + D(y) = 0
3054 * then A is of the form
3056 * B(x') + D(y) = 0
3058 * If any of these constraints involves only a single local variable y,
3059 * then the constraint appears as
3061 * f(x) + m y_i = 0
3063 * in "bmap" and as
3065 * h(x') + m y_i = 0
3067 * in "A".
3069 * Let g be the gcd of m and the coefficients of h.
3070 * Then, in particular, g is a divisor of the coefficients of h and
3072 * f(x) = h(x')
3074 * is known to be a multiple of g.
3075 * If some prime factor in m appears with the same exponent in g,
3076 * then it can be removed from m because f(x) is already known
3077 * to be a multiple of g and therefore in particular of this power
3078 * of the prime factors.
3079 * Prime factors that appear with a smaller exponent in g cannot
3080 * be removed from m.
3081 * Let g' be the divisor of g containing all prime factors that
3082 * appear with the same exponent in m and g, then
3084 * f(x) + m y_i = 0
3086 * can be replaced by
3088 * f(x) + m/g' y_i' = 0
3090 * Note that (if g' != 1) this changes the explicit representation
3091 * of y_i to that of y_i', so the integer division at position i
3092 * is marked unknown and later recomputed by a call to
3093 * isl_basic_map_gauss.
3095 static __isl_give isl_basic_map *reduce_stride_constraints(
3096 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3098 int i;
3099 int total, n_div;
3100 int any = 0;
3101 isl_int gcd;
3103 if (!bmap || !A)
3104 return isl_basic_map_free(bmap);
3106 total = isl_basic_map_dim(bmap, isl_dim_all);
3107 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3108 total -= n_div;
3110 isl_int_init(gcd);
3111 for (i = 0; i < n; ++i) {
3112 int div;
3114 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3115 if (div < 0)
3116 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3117 "equality constraints modified unexpectedly",
3118 goto error);
3119 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3120 n_div - div - 1) != -1)
3121 continue;
3122 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3123 goto error;
3124 if (isl_int_is_one(gcd))
3125 continue;
3126 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3127 if (isl_int_is_one(gcd))
3128 continue;
3129 isl_int_divexact(bmap->eq[i][1 + total + div],
3130 bmap->eq[i][1 + total + div], gcd);
3131 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3132 if (!bmap)
3133 goto error;
3134 any = 1;
3136 isl_int_clear(gcd);
3138 if (any)
3139 bmap = isl_basic_map_gauss(bmap, NULL);
3141 return bmap;
3142 error:
3143 isl_int_clear(gcd);
3144 isl_basic_map_free(bmap);
3145 return NULL;
3148 /* Simplify the stride constraints in "bmap" based on
3149 * the remaining equality constraints in "bmap" and all equality
3150 * constraints in "context".
3151 * Only do this if both "bmap" and "context" have stride constraints.
3153 * First extract a copy of the stride constraints in "bmap" in a compressed
3154 * space exploiting all the other equality constraints and then
3155 * use this compressed copy to simplify the original stride constraints.
3157 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3158 __isl_keep isl_basic_map *context)
3160 int bmap_n_eq, context_n_eq;
3161 isl_mat *A;
3163 if (!bmap || !context)
3164 return isl_basic_map_free(bmap);
3166 bmap_n_eq = n_div_eq(bmap);
3167 context_n_eq = n_div_eq(context);
3169 if (bmap_n_eq < 0 || context_n_eq < 0)
3170 return isl_basic_map_free(bmap);
3171 if (bmap_n_eq == 0 || context_n_eq == 0)
3172 return bmap;
3174 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3175 context, context_n_eq);
3176 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3178 isl_mat_free(A);
3180 return bmap;
3183 /* Return a basic map that has the same intersection with "context" as "bmap"
3184 * and that is as "simple" as possible.
3186 * The core computation is performed on the pure constraints.
3187 * When we add back the meaning of the integer divisions, we need
3188 * to (re)introduce the div constraints. If we happen to have
3189 * discovered that some of these integer divisions are equal to
3190 * some affine combination of other variables, then these div
3191 * constraints may end up getting simplified in terms of the equalities,
3192 * resulting in extra inequalities on the other variables that
3193 * may have been removed already or that may not even have been
3194 * part of the input. We try and remove those constraints of
3195 * this form that are most obviously redundant with respect to
3196 * the context. We also remove those div constraints that are
3197 * redundant with respect to the other constraints in the result.
3199 * The stride constraints among the equality constraints in "bmap" are
3200 * also simplified with respecting to the other equality constraints
3201 * in "bmap" and with respect to all equality constraints in "context".
3203 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3204 struct isl_basic_map *context)
3206 isl_basic_set *bset, *eq;
3207 isl_basic_map *eq_bmap;
3208 unsigned total, n_div, extra, n_eq, n_ineq;
3210 if (!bmap || !context)
3211 goto error;
3213 if (isl_basic_map_plain_is_universe(bmap)) {
3214 isl_basic_map_free(context);
3215 return bmap;
3217 if (isl_basic_map_plain_is_empty(context)) {
3218 isl_space *space = isl_basic_map_get_space(bmap);
3219 isl_basic_map_free(bmap);
3220 isl_basic_map_free(context);
3221 return isl_basic_map_universe(space);
3223 if (isl_basic_map_plain_is_empty(bmap)) {
3224 isl_basic_map_free(context);
3225 return bmap;
3228 bmap = isl_basic_map_remove_redundancies(bmap);
3229 context = isl_basic_map_remove_redundancies(context);
3230 if (!context)
3231 goto error;
3233 context = isl_basic_map_align_divs(context, bmap);
3234 n_div = isl_basic_map_dim(context, isl_dim_div);
3235 total = isl_basic_map_dim(bmap, isl_dim_all);
3236 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3238 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3239 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3240 bset = uset_gist(bset,
3241 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3242 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3244 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3245 isl_basic_set_plain_is_empty(bset)) {
3246 isl_basic_map_free(context);
3247 return isl_basic_map_overlying_set(bset, bmap);
3250 n_eq = bset->n_eq;
3251 n_ineq = bset->n_ineq;
3252 eq = isl_basic_set_copy(bset);
3253 eq = isl_basic_set_cow(eq);
3254 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3255 eq = isl_basic_set_free(eq);
3256 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3257 bset = isl_basic_set_free(bset);
3259 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3260 eq_bmap = gist_strides(eq_bmap, context);
3261 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3262 bmap = isl_basic_map_overlying_set(bset, bmap);
3263 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3264 bmap = isl_basic_map_remove_redundancies(bmap);
3266 return bmap;
3267 error:
3268 isl_basic_map_free(bmap);
3269 isl_basic_map_free(context);
3270 return NULL;
3274 * Assumes context has no implicit divs.
3276 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3277 __isl_take isl_basic_map *context)
3279 int i;
3281 if (!map || !context)
3282 goto error;
3284 if (isl_basic_map_plain_is_empty(context)) {
3285 isl_space *space = isl_map_get_space(map);
3286 isl_map_free(map);
3287 isl_basic_map_free(context);
3288 return isl_map_universe(space);
3291 context = isl_basic_map_remove_redundancies(context);
3292 map = isl_map_cow(map);
3293 if (!map || !context)
3294 goto error;
3295 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3296 map = isl_map_compute_divs(map);
3297 if (!map)
3298 goto error;
3299 for (i = map->n - 1; i >= 0; --i) {
3300 map->p[i] = isl_basic_map_gist(map->p[i],
3301 isl_basic_map_copy(context));
3302 if (!map->p[i])
3303 goto error;
3304 if (isl_basic_map_plain_is_empty(map->p[i])) {
3305 isl_basic_map_free(map->p[i]);
3306 if (i != map->n - 1)
3307 map->p[i] = map->p[map->n - 1];
3308 map->n--;
3311 isl_basic_map_free(context);
3312 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3313 return map;
3314 error:
3315 isl_map_free(map);
3316 isl_basic_map_free(context);
3317 return NULL;
3320 /* Drop all inequalities from "bmap" that also appear in "context".
3321 * "context" is assumed to have only known local variables and
3322 * the initial local variables of "bmap" are assumed to be the same
3323 * as those of "context".
3324 * The constraints of both "bmap" and "context" are assumed
3325 * to have been sorted using isl_basic_map_sort_constraints.
3327 * Run through the inequality constraints of "bmap" and "context"
3328 * in sorted order.
3329 * If a constraint of "bmap" involves variables not in "context",
3330 * then it cannot appear in "context".
3331 * If a matching constraint is found, it is removed from "bmap".
3333 static __isl_give isl_basic_map *drop_inequalities(
3334 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3336 int i1, i2;
3337 unsigned total, extra;
3339 if (!bmap || !context)
3340 return isl_basic_map_free(bmap);
3342 total = isl_basic_map_total_dim(context);
3343 extra = isl_basic_map_total_dim(bmap) - total;
3345 i1 = bmap->n_ineq - 1;
3346 i2 = context->n_ineq - 1;
3347 while (bmap && i1 >= 0 && i2 >= 0) {
3348 int cmp;
3350 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3351 extra) != -1) {
3352 --i1;
3353 continue;
3355 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3356 context->ineq[i2]);
3357 if (cmp < 0) {
3358 --i2;
3359 continue;
3361 if (cmp > 0) {
3362 --i1;
3363 continue;
3365 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3366 bmap = isl_basic_map_cow(bmap);
3367 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3368 bmap = isl_basic_map_free(bmap);
3370 --i1;
3371 --i2;
3374 return bmap;
3377 /* Drop all equalities from "bmap" that also appear in "context".
3378 * "context" is assumed to have only known local variables and
3379 * the initial local variables of "bmap" are assumed to be the same
3380 * as those of "context".
3382 * Run through the equality constraints of "bmap" and "context"
3383 * in sorted order.
3384 * If a constraint of "bmap" involves variables not in "context",
3385 * then it cannot appear in "context".
3386 * If a matching constraint is found, it is removed from "bmap".
3388 static __isl_give isl_basic_map *drop_equalities(
3389 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3391 int i1, i2;
3392 unsigned total, extra;
3394 if (!bmap || !context)
3395 return isl_basic_map_free(bmap);
3397 total = isl_basic_map_total_dim(context);
3398 extra = isl_basic_map_total_dim(bmap) - total;
3400 i1 = bmap->n_eq - 1;
3401 i2 = context->n_eq - 1;
3403 while (bmap && i1 >= 0 && i2 >= 0) {
3404 int last1, last2;
3406 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3407 extra) != -1)
3408 break;
3409 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3410 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3411 if (last1 > last2) {
3412 --i2;
3413 continue;
3415 if (last1 < last2) {
3416 --i1;
3417 continue;
3419 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3420 bmap = isl_basic_map_cow(bmap);
3421 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3422 bmap = isl_basic_map_free(bmap);
3424 --i1;
3425 --i2;
3428 return bmap;
3431 /* Remove the constraints in "context" from "bmap".
3432 * "context" is assumed to have explicit representations
3433 * for all local variables.
3435 * First align the divs of "bmap" to those of "context" and
3436 * sort the constraints. Then drop all constraints from "bmap"
3437 * that appear in "context".
3439 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3440 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3442 isl_bool done, known;
3444 done = isl_basic_map_plain_is_universe(context);
3445 if (done == isl_bool_false)
3446 done = isl_basic_map_plain_is_universe(bmap);
3447 if (done == isl_bool_false)
3448 done = isl_basic_map_plain_is_empty(context);
3449 if (done == isl_bool_false)
3450 done = isl_basic_map_plain_is_empty(bmap);
3451 if (done < 0)
3452 goto error;
3453 if (done) {
3454 isl_basic_map_free(context);
3455 return bmap;
3457 known = isl_basic_map_divs_known(context);
3458 if (known < 0)
3459 goto error;
3460 if (!known)
3461 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3462 "context has unknown divs", goto error);
3464 bmap = isl_basic_map_align_divs(bmap, context);
3465 bmap = isl_basic_map_gauss(bmap, NULL);
3466 bmap = isl_basic_map_sort_constraints(bmap);
3467 context = isl_basic_map_sort_constraints(context);
3469 bmap = drop_inequalities(bmap, context);
3470 bmap = drop_equalities(bmap, context);
3472 isl_basic_map_free(context);
3473 bmap = isl_basic_map_finalize(bmap);
3474 return bmap;
3475 error:
3476 isl_basic_map_free(bmap);
3477 isl_basic_map_free(context);
3478 return NULL;
3481 /* Replace "map" by the disjunct at position "pos" and free "context".
3483 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3484 int pos, __isl_take isl_basic_map *context)
3486 isl_basic_map *bmap;
3488 bmap = isl_basic_map_copy(map->p[pos]);
3489 isl_map_free(map);
3490 isl_basic_map_free(context);
3491 return isl_map_from_basic_map(bmap);
3494 /* Remove the constraints in "context" from "map".
3495 * If any of the disjuncts in the result turns out to be the universe,
3496 * then return this universe.
3497 * "context" is assumed to have explicit representations
3498 * for all local variables.
3500 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3501 __isl_take isl_basic_map *context)
3503 int i;
3504 isl_bool univ, known;
3506 univ = isl_basic_map_plain_is_universe(context);
3507 if (univ < 0)
3508 goto error;
3509 if (univ) {
3510 isl_basic_map_free(context);
3511 return map;
3513 known = isl_basic_map_divs_known(context);
3514 if (known < 0)
3515 goto error;
3516 if (!known)
3517 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3518 "context has unknown divs", goto error);
3520 map = isl_map_cow(map);
3521 if (!map)
3522 goto error;
3523 for (i = 0; i < map->n; ++i) {
3524 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3525 isl_basic_map_copy(context));
3526 univ = isl_basic_map_plain_is_universe(map->p[i]);
3527 if (univ < 0)
3528 goto error;
3529 if (univ && map->n > 1)
3530 return replace_by_disjunct(map, i, context);
3533 isl_basic_map_free(context);
3534 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3535 if (map->n > 1)
3536 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3537 return map;
3538 error:
3539 isl_map_free(map);
3540 isl_basic_map_free(context);
3541 return NULL;
3544 /* Replace "map" by a universe map in the same space and free "drop".
3546 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3547 __isl_take isl_map *drop)
3549 isl_map *res;
3551 res = isl_map_universe(isl_map_get_space(map));
3552 isl_map_free(map);
3553 isl_map_free(drop);
3554 return res;
3557 /* Return a map that has the same intersection with "context" as "map"
3558 * and that is as "simple" as possible.
3560 * If "map" is already the universe, then we cannot make it any simpler.
3561 * Similarly, if "context" is the universe, then we cannot exploit it
3562 * to simplify "map"
3563 * If "map" and "context" are identical to each other, then we can
3564 * return the corresponding universe.
3566 * If either "map" or "context" consists of multiple disjuncts,
3567 * then check if "context" happens to be a subset of "map",
3568 * in which case all constraints can be removed.
3569 * In case of multiple disjuncts, the standard procedure
3570 * may not be able to detect that all constraints can be removed.
3572 * If none of these cases apply, we have to work a bit harder.
3573 * During this computation, we make use of a single disjunct context,
3574 * so if the original context consists of more than one disjunct
3575 * then we need to approximate the context by a single disjunct set.
3576 * Simply taking the simple hull may drop constraints that are
3577 * only implicitly available in each disjunct. We therefore also
3578 * look for constraints among those defining "map" that are valid
3579 * for the context. These can then be used to simplify away
3580 * the corresponding constraints in "map".
3582 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3583 __isl_take isl_map *context)
3585 int equal;
3586 int is_universe;
3587 int single_disjunct_map, single_disjunct_context;
3588 isl_bool subset;
3589 isl_basic_map *hull;
3591 is_universe = isl_map_plain_is_universe(map);
3592 if (is_universe >= 0 && !is_universe)
3593 is_universe = isl_map_plain_is_universe(context);
3594 if (is_universe < 0)
3595 goto error;
3596 if (is_universe) {
3597 isl_map_free(context);
3598 return map;
3601 equal = isl_map_plain_is_equal(map, context);
3602 if (equal < 0)
3603 goto error;
3604 if (equal)
3605 return replace_by_universe(map, context);
3607 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3608 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3609 if (!single_disjunct_map || !single_disjunct_context) {
3610 subset = isl_map_is_subset(context, map);
3611 if (subset < 0)
3612 goto error;
3613 if (subset)
3614 return replace_by_universe(map, context);
3617 context = isl_map_compute_divs(context);
3618 if (!context)
3619 goto error;
3620 if (single_disjunct_context) {
3621 hull = isl_map_simple_hull(context);
3622 } else {
3623 isl_ctx *ctx;
3624 isl_map_list *list;
3626 ctx = isl_map_get_ctx(map);
3627 list = isl_map_list_alloc(ctx, 2);
3628 list = isl_map_list_add(list, isl_map_copy(context));
3629 list = isl_map_list_add(list, isl_map_copy(map));
3630 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3631 list);
3633 return isl_map_gist_basic_map(map, hull);
3634 error:
3635 isl_map_free(map);
3636 isl_map_free(context);
3637 return NULL;
3640 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3641 __isl_take isl_map *context)
3643 return isl_map_align_params_map_map_and(map, context, &map_gist);
3646 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3647 struct isl_basic_set *context)
3649 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3650 bset_to_bmap(context)));
3653 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3654 __isl_take isl_basic_set *context)
3656 return (struct isl_set *)isl_map_gist_basic_map(set_to_map(set),
3657 bset_to_bmap(context));
3660 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3661 __isl_take isl_basic_set *context)
3663 isl_space *space = isl_set_get_space(set);
3664 isl_basic_set *dom_context = isl_basic_set_universe(space);
3665 dom_context = isl_basic_set_intersect_params(dom_context, context);
3666 return isl_set_gist_basic_set(set, dom_context);
3669 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3670 __isl_take isl_set *context)
3672 return (struct isl_set *)isl_map_gist(set_to_map(set),
3673 set_to_map(context));
3676 /* Compute the gist of "bmap" with respect to the constraints "context"
3677 * on the domain.
3679 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3680 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3682 isl_space *space = isl_basic_map_get_space(bmap);
3683 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3685 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3686 return isl_basic_map_gist(bmap, bmap_context);
3689 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3690 __isl_take isl_set *context)
3692 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3693 map_context = isl_map_intersect_domain(map_context, context);
3694 return isl_map_gist(map, map_context);
3697 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3698 __isl_take isl_set *context)
3700 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3701 map_context = isl_map_intersect_range(map_context, context);
3702 return isl_map_gist(map, map_context);
3705 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3706 __isl_take isl_set *context)
3708 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3709 map_context = isl_map_intersect_params(map_context, context);
3710 return isl_map_gist(map, map_context);
3713 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3714 __isl_take isl_set *context)
3716 return isl_map_gist_params(set, context);
3719 /* Quick check to see if two basic maps are disjoint.
3720 * In particular, we reduce the equalities and inequalities of
3721 * one basic map in the context of the equalities of the other
3722 * basic map and check if we get a contradiction.
3724 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3725 __isl_keep isl_basic_map *bmap2)
3727 struct isl_vec *v = NULL;
3728 int *elim = NULL;
3729 unsigned total;
3730 int i;
3732 if (!bmap1 || !bmap2)
3733 return isl_bool_error;
3734 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3735 return isl_bool_error);
3736 if (bmap1->n_div || bmap2->n_div)
3737 return isl_bool_false;
3738 if (!bmap1->n_eq && !bmap2->n_eq)
3739 return isl_bool_false;
3741 total = isl_space_dim(bmap1->dim, isl_dim_all);
3742 if (total == 0)
3743 return isl_bool_false;
3744 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3745 if (!v)
3746 goto error;
3747 elim = isl_alloc_array(bmap1->ctx, int, total);
3748 if (!elim)
3749 goto error;
3750 compute_elimination_index(bmap1, elim);
3751 for (i = 0; i < bmap2->n_eq; ++i) {
3752 int reduced;
3753 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3754 bmap1, elim);
3755 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3756 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3757 goto disjoint;
3759 for (i = 0; i < bmap2->n_ineq; ++i) {
3760 int reduced;
3761 reduced = reduced_using_equalities(v->block.data,
3762 bmap2->ineq[i], bmap1, elim);
3763 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3764 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3765 goto disjoint;
3767 compute_elimination_index(bmap2, elim);
3768 for (i = 0; i < bmap1->n_ineq; ++i) {
3769 int reduced;
3770 reduced = reduced_using_equalities(v->block.data,
3771 bmap1->ineq[i], bmap2, elim);
3772 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3773 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3774 goto disjoint;
3776 isl_vec_free(v);
3777 free(elim);
3778 return isl_bool_false;
3779 disjoint:
3780 isl_vec_free(v);
3781 free(elim);
3782 return isl_bool_true;
3783 error:
3784 isl_vec_free(v);
3785 free(elim);
3786 return isl_bool_error;
3789 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3790 __isl_keep isl_basic_set *bset2)
3792 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3793 bset_to_bmap(bset2));
3796 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3798 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3799 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3800 __isl_keep isl_basic_map *bmap2))
3802 int i, j;
3804 if (!map1 || !map2)
3805 return isl_bool_error;
3807 for (i = 0; i < map1->n; ++i) {
3808 for (j = 0; j < map2->n; ++j) {
3809 isl_bool d = test(map1->p[i], map2->p[j]);
3810 if (d != isl_bool_true)
3811 return d;
3815 return isl_bool_true;
3818 /* Are "map1" and "map2" obviously disjoint, based on information
3819 * that can be derived without looking at the individual basic maps?
3821 * In particular, if one of them is empty or if they live in different spaces
3822 * (ignoring parameters), then they are clearly disjoint.
3824 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3825 __isl_keep isl_map *map2)
3827 isl_bool disjoint;
3828 isl_bool match;
3830 if (!map1 || !map2)
3831 return isl_bool_error;
3833 disjoint = isl_map_plain_is_empty(map1);
3834 if (disjoint < 0 || disjoint)
3835 return disjoint;
3837 disjoint = isl_map_plain_is_empty(map2);
3838 if (disjoint < 0 || disjoint)
3839 return disjoint;
3841 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3842 map2->dim, isl_dim_in);
3843 if (match < 0 || !match)
3844 return match < 0 ? isl_bool_error : isl_bool_true;
3846 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3847 map2->dim, isl_dim_out);
3848 if (match < 0 || !match)
3849 return match < 0 ? isl_bool_error : isl_bool_true;
3851 return isl_bool_false;
3854 /* Are "map1" and "map2" obviously disjoint?
3856 * If one of them is empty or if they live in different spaces (ignoring
3857 * parameters), then they are clearly disjoint.
3858 * This is checked by isl_map_plain_is_disjoint_global.
3860 * If they have different parameters, then we skip any further tests.
3862 * If they are obviously equal, but not obviously empty, then we will
3863 * not be able to detect if they are disjoint.
3865 * Otherwise we check if each basic map in "map1" is obviously disjoint
3866 * from each basic map in "map2".
3868 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3869 __isl_keep isl_map *map2)
3871 isl_bool disjoint;
3872 isl_bool intersect;
3873 isl_bool match;
3875 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3876 if (disjoint < 0 || disjoint)
3877 return disjoint;
3879 match = isl_space_match(map1->dim, isl_dim_param,
3880 map2->dim, isl_dim_param);
3881 if (match < 0 || !match)
3882 return match < 0 ? isl_bool_error : isl_bool_false;
3884 intersect = isl_map_plain_is_equal(map1, map2);
3885 if (intersect < 0 || intersect)
3886 return intersect < 0 ? isl_bool_error : isl_bool_false;
3888 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3891 /* Are "map1" and "map2" disjoint?
3893 * They are disjoint if they are "obviously disjoint" or if one of them
3894 * is empty. Otherwise, they are not disjoint if one of them is universal.
3895 * If the two inputs are (obviously) equal and not empty, then they are
3896 * not disjoint.
3897 * If none of these cases apply, then check if all pairs of basic maps
3898 * are disjoint.
3900 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3902 isl_bool disjoint;
3903 isl_bool intersect;
3905 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3906 if (disjoint < 0 || disjoint)
3907 return disjoint;
3909 disjoint = isl_map_is_empty(map1);
3910 if (disjoint < 0 || disjoint)
3911 return disjoint;
3913 disjoint = isl_map_is_empty(map2);
3914 if (disjoint < 0 || disjoint)
3915 return disjoint;
3917 intersect = isl_map_plain_is_universe(map1);
3918 if (intersect < 0 || intersect)
3919 return intersect < 0 ? isl_bool_error : isl_bool_false;
3921 intersect = isl_map_plain_is_universe(map2);
3922 if (intersect < 0 || intersect)
3923 return intersect < 0 ? isl_bool_error : isl_bool_false;
3925 intersect = isl_map_plain_is_equal(map1, map2);
3926 if (intersect < 0 || intersect)
3927 return isl_bool_not(intersect);
3929 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3932 /* Are "bmap1" and "bmap2" disjoint?
3934 * They are disjoint if they are "obviously disjoint" or if one of them
3935 * is empty. Otherwise, they are not disjoint if one of them is universal.
3936 * If none of these cases apply, we compute the intersection and see if
3937 * the result is empty.
3939 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3940 __isl_keep isl_basic_map *bmap2)
3942 isl_bool disjoint;
3943 isl_bool intersect;
3944 isl_basic_map *test;
3946 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3947 if (disjoint < 0 || disjoint)
3948 return disjoint;
3950 disjoint = isl_basic_map_is_empty(bmap1);
3951 if (disjoint < 0 || disjoint)
3952 return disjoint;
3954 disjoint = isl_basic_map_is_empty(bmap2);
3955 if (disjoint < 0 || disjoint)
3956 return disjoint;
3958 intersect = isl_basic_map_plain_is_universe(bmap1);
3959 if (intersect < 0 || intersect)
3960 return intersect < 0 ? isl_bool_error : isl_bool_false;
3962 intersect = isl_basic_map_plain_is_universe(bmap2);
3963 if (intersect < 0 || intersect)
3964 return intersect < 0 ? isl_bool_error : isl_bool_false;
3966 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3967 isl_basic_map_copy(bmap2));
3968 disjoint = isl_basic_map_is_empty(test);
3969 isl_basic_map_free(test);
3971 return disjoint;
3974 /* Are "bset1" and "bset2" disjoint?
3976 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3977 __isl_keep isl_basic_set *bset2)
3979 return isl_basic_map_is_disjoint(bset1, bset2);
3982 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3983 __isl_keep isl_set *set2)
3985 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3988 /* Are "set1" and "set2" disjoint?
3990 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3992 return isl_map_is_disjoint(set1, set2);
3995 /* Is "v" equal to 0, 1 or -1?
3997 static int is_zero_or_one(isl_int v)
3999 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4002 /* Check if we can combine a given div with lower bound l and upper
4003 * bound u with some other div and if so return that other div.
4004 * Otherwise return -1.
4006 * We first check that
4007 * - the bounds are opposites of each other (except for the constant
4008 * term)
4009 * - the bounds do not reference any other div
4010 * - no div is defined in terms of this div
4012 * Let m be the size of the range allowed on the div by the bounds.
4013 * That is, the bounds are of the form
4015 * e <= a <= e + m - 1
4017 * with e some expression in the other variables.
4018 * We look for another div b such that no third div is defined in terms
4019 * of this second div b and such that in any constraint that contains
4020 * a (except for the given lower and upper bound), also contains b
4021 * with a coefficient that is m times that of b.
4022 * That is, all constraints (execpt for the lower and upper bound)
4023 * are of the form
4025 * e + f (a + m b) >= 0
4027 * Furthermore, in the constraints that only contain b, the coefficient
4028 * of b should be equal to 1 or -1.
4029 * If so, we return b so that "a + m b" can be replaced by
4030 * a single div "c = a + m b".
4032 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4033 unsigned div, unsigned l, unsigned u)
4035 int i, j;
4036 unsigned dim;
4037 int coalesce = -1;
4039 if (bmap->n_div <= 1)
4040 return -1;
4041 dim = isl_space_dim(bmap->dim, isl_dim_all);
4042 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4043 return -1;
4044 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4045 bmap->n_div - div - 1) != -1)
4046 return -1;
4047 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4048 dim + bmap->n_div))
4049 return -1;
4051 for (i = 0; i < bmap->n_div; ++i) {
4052 if (isl_int_is_zero(bmap->div[i][0]))
4053 continue;
4054 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4055 return -1;
4058 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4059 if (isl_int_is_neg(bmap->ineq[l][0])) {
4060 isl_int_sub(bmap->ineq[l][0],
4061 bmap->ineq[l][0], bmap->ineq[u][0]);
4062 bmap = isl_basic_map_copy(bmap);
4063 bmap = isl_basic_map_set_to_empty(bmap);
4064 isl_basic_map_free(bmap);
4065 return -1;
4067 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4068 for (i = 0; i < bmap->n_div; ++i) {
4069 if (i == div)
4070 continue;
4071 if (!pairs[i])
4072 continue;
4073 for (j = 0; j < bmap->n_div; ++j) {
4074 if (isl_int_is_zero(bmap->div[j][0]))
4075 continue;
4076 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4077 break;
4079 if (j < bmap->n_div)
4080 continue;
4081 for (j = 0; j < bmap->n_ineq; ++j) {
4082 int valid;
4083 if (j == l || j == u)
4084 continue;
4085 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4086 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4087 continue;
4088 break;
4090 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4091 break;
4092 isl_int_mul(bmap->ineq[j][1 + dim + div],
4093 bmap->ineq[j][1 + dim + div],
4094 bmap->ineq[l][0]);
4095 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4096 bmap->ineq[j][1 + dim + i]);
4097 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4098 bmap->ineq[j][1 + dim + div],
4099 bmap->ineq[l][0]);
4100 if (!valid)
4101 break;
4103 if (j < bmap->n_ineq)
4104 continue;
4105 coalesce = i;
4106 break;
4108 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4109 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4110 return coalesce;
4113 /* Internal data structure used during the construction and/or evaluation of
4114 * an inequality that ensures that a pair of bounds always allows
4115 * for an integer value.
4117 * "tab" is the tableau in which the inequality is evaluated. It may
4118 * be NULL until it is actually needed.
4119 * "v" contains the inequality coefficients.
4120 * "g", "fl" and "fu" are temporary scalars used during the construction and
4121 * evaluation.
4123 struct test_ineq_data {
4124 struct isl_tab *tab;
4125 isl_vec *v;
4126 isl_int g;
4127 isl_int fl;
4128 isl_int fu;
4131 /* Free all the memory allocated by the fields of "data".
4133 static void test_ineq_data_clear(struct test_ineq_data *data)
4135 isl_tab_free(data->tab);
4136 isl_vec_free(data->v);
4137 isl_int_clear(data->g);
4138 isl_int_clear(data->fl);
4139 isl_int_clear(data->fu);
4142 /* Is the inequality stored in data->v satisfied by "bmap"?
4143 * That is, does it only attain non-negative values?
4144 * data->tab is a tableau corresponding to "bmap".
4146 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4147 struct test_ineq_data *data)
4149 isl_ctx *ctx;
4150 enum isl_lp_result res;
4152 ctx = isl_basic_map_get_ctx(bmap);
4153 if (!data->tab)
4154 data->tab = isl_tab_from_basic_map(bmap, 0);
4155 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4156 if (res == isl_lp_error)
4157 return isl_bool_error;
4158 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4161 /* Given a lower and an upper bound on div i, do they always allow
4162 * for an integer value of the given div?
4163 * Determine this property by constructing an inequality
4164 * such that the property is guaranteed when the inequality is nonnegative.
4165 * The lower bound is inequality l, while the upper bound is inequality u.
4166 * The constructed inequality is stored in data->v.
4168 * Let the upper bound be
4170 * -n_u a + e_u >= 0
4172 * and the lower bound
4174 * n_l a + e_l >= 0
4176 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4177 * We have
4179 * - f_u e_l <= f_u f_l g a <= f_l e_u
4181 * Since all variables are integer valued, this is equivalent to
4183 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4185 * If this interval is at least f_u f_l g, then it contains at least
4186 * one integer value for a.
4187 * That is, the test constraint is
4189 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4191 * or
4193 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4195 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4196 * then the constraint can be scaled down by a factor g',
4197 * with the constant term replaced by
4198 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4199 * Note that the result of applying Fourier-Motzkin to this pair
4200 * of constraints is
4202 * f_l e_u + f_u e_l >= 0
4204 * If the constant term of the scaled down version of this constraint,
4205 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4206 * term of the scaled down test constraint, then the test constraint
4207 * is known to hold and no explicit evaluation is required.
4208 * This is essentially the Omega test.
4210 * If the test constraint consists of only a constant term, then
4211 * it is sufficient to look at the sign of this constant term.
4213 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4214 int l, int u, struct test_ineq_data *data)
4216 unsigned offset, n_div;
4217 offset = isl_basic_map_offset(bmap, isl_dim_div);
4218 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4220 isl_int_gcd(data->g,
4221 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4222 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4223 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4224 isl_int_neg(data->fu, data->fu);
4225 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4226 data->fu, bmap->ineq[l], offset + n_div);
4227 isl_int_mul(data->g, data->g, data->fl);
4228 isl_int_mul(data->g, data->g, data->fu);
4229 isl_int_sub(data->g, data->g, data->fl);
4230 isl_int_sub(data->g, data->g, data->fu);
4231 isl_int_add_ui(data->g, data->g, 1);
4232 isl_int_sub(data->fl, data->v->el[0], data->g);
4234 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4235 if (isl_int_is_zero(data->g))
4236 return isl_int_is_nonneg(data->fl);
4237 if (isl_int_is_one(data->g)) {
4238 isl_int_set(data->v->el[0], data->fl);
4239 return test_ineq_is_satisfied(bmap, data);
4241 isl_int_fdiv_q(data->fl, data->fl, data->g);
4242 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4243 if (isl_int_eq(data->fl, data->v->el[0]))
4244 return isl_bool_true;
4245 isl_int_set(data->v->el[0], data->fl);
4246 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4247 offset - 1 + n_div);
4249 return test_ineq_is_satisfied(bmap, data);
4252 /* Remove more kinds of divs that are not strictly needed.
4253 * In particular, if all pairs of lower and upper bounds on a div
4254 * are such that they allow at least one integer value of the div,
4255 * then we can eliminate the div using Fourier-Motzkin without
4256 * introducing any spurious solutions.
4258 * If at least one of the two constraints has a unit coefficient for the div,
4259 * then the presence of such a value is guaranteed so there is no need to check.
4260 * In particular, the value attained by the bound with unit coefficient
4261 * can serve as this intermediate value.
4263 static struct isl_basic_map *drop_more_redundant_divs(
4264 struct isl_basic_map *bmap, int *pairs, int n)
4266 isl_ctx *ctx;
4267 struct test_ineq_data data = { NULL, NULL };
4268 unsigned off, n_div;
4269 int remove = -1;
4271 isl_int_init(data.g);
4272 isl_int_init(data.fl);
4273 isl_int_init(data.fu);
4275 if (!bmap)
4276 goto error;
4278 ctx = isl_basic_map_get_ctx(bmap);
4279 off = isl_basic_map_offset(bmap, isl_dim_div);
4280 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4281 data.v = isl_vec_alloc(ctx, off + n_div);
4282 if (!data.v)
4283 goto error;
4285 while (n > 0) {
4286 int i, l, u;
4287 int best = -1;
4288 isl_bool has_int;
4290 for (i = 0; i < n_div; ++i) {
4291 if (!pairs[i])
4292 continue;
4293 if (best >= 0 && pairs[best] <= pairs[i])
4294 continue;
4295 best = i;
4298 i = best;
4299 for (l = 0; l < bmap->n_ineq; ++l) {
4300 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4301 continue;
4302 if (isl_int_is_one(bmap->ineq[l][off + i]))
4303 continue;
4304 for (u = 0; u < bmap->n_ineq; ++u) {
4305 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4306 continue;
4307 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4308 continue;
4309 has_int = int_between_bounds(bmap, i, l, u,
4310 &data);
4311 if (has_int < 0)
4312 goto error;
4313 if (data.tab && data.tab->empty)
4314 break;
4315 if (!has_int)
4316 break;
4318 if (u < bmap->n_ineq)
4319 break;
4321 if (data.tab && data.tab->empty) {
4322 bmap = isl_basic_map_set_to_empty(bmap);
4323 break;
4325 if (l == bmap->n_ineq) {
4326 remove = i;
4327 break;
4329 pairs[i] = 0;
4330 --n;
4333 test_ineq_data_clear(&data);
4335 free(pairs);
4337 if (remove < 0)
4338 return bmap;
4340 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4341 return isl_basic_map_drop_redundant_divs(bmap);
4342 error:
4343 free(pairs);
4344 isl_basic_map_free(bmap);
4345 test_ineq_data_clear(&data);
4346 return NULL;
4349 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4350 * and the upper bound u, div1 always occurs together with div2 in the form
4351 * (div1 + m div2), where m is the constant range on the variable div1
4352 * allowed by l and u, replace the pair div1 and div2 by a single
4353 * div that is equal to div1 + m div2.
4355 * The new div will appear in the location that contains div2.
4356 * We need to modify all constraints that contain
4357 * div2 = (div - div1) / m
4358 * The coefficient of div2 is known to be equal to 1 or -1.
4359 * (If a constraint does not contain div2, it will also not contain div1.)
4360 * If the constraint also contains div1, then we know they appear
4361 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4362 * i.e., the coefficient of div is f.
4364 * Otherwise, we first need to introduce div1 into the constraint.
4365 * Let the l be
4367 * div1 + f >=0
4369 * and u
4371 * -div1 + f' >= 0
4373 * A lower bound on div2
4375 * div2 + t >= 0
4377 * can be replaced by
4379 * m div2 + div1 + m t + f >= 0
4381 * An upper bound
4383 * -div2 + t >= 0
4385 * can be replaced by
4387 * -(m div2 + div1) + m t + f' >= 0
4389 * These constraint are those that we would obtain from eliminating
4390 * div1 using Fourier-Motzkin.
4392 * After all constraints have been modified, we drop the lower and upper
4393 * bound and then drop div1.
4395 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4396 unsigned div1, unsigned div2, unsigned l, unsigned u)
4398 isl_ctx *ctx;
4399 isl_int m;
4400 unsigned dim, total;
4401 int i;
4403 ctx = isl_basic_map_get_ctx(bmap);
4405 dim = isl_space_dim(bmap->dim, isl_dim_all);
4406 total = 1 + dim + bmap->n_div;
4408 isl_int_init(m);
4409 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4410 isl_int_add_ui(m, m, 1);
4412 for (i = 0; i < bmap->n_ineq; ++i) {
4413 if (i == l || i == u)
4414 continue;
4415 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4416 continue;
4417 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4418 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4419 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4420 ctx->one, bmap->ineq[l], total);
4421 else
4422 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4423 ctx->one, bmap->ineq[u], total);
4425 isl_int_set(bmap->ineq[i][1 + dim + div2],
4426 bmap->ineq[i][1 + dim + div1]);
4427 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4430 isl_int_clear(m);
4431 if (l > u) {
4432 isl_basic_map_drop_inequality(bmap, l);
4433 isl_basic_map_drop_inequality(bmap, u);
4434 } else {
4435 isl_basic_map_drop_inequality(bmap, u);
4436 isl_basic_map_drop_inequality(bmap, l);
4438 bmap = isl_basic_map_drop_div(bmap, div1);
4439 return bmap;
4442 /* First check if we can coalesce any pair of divs and
4443 * then continue with dropping more redundant divs.
4445 * We loop over all pairs of lower and upper bounds on a div
4446 * with coefficient 1 and -1, respectively, check if there
4447 * is any other div "c" with which we can coalesce the div
4448 * and if so, perform the coalescing.
4450 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4451 struct isl_basic_map *bmap, int *pairs, int n)
4453 int i, l, u;
4454 unsigned dim;
4456 dim = isl_space_dim(bmap->dim, isl_dim_all);
4458 for (i = 0; i < bmap->n_div; ++i) {
4459 if (!pairs[i])
4460 continue;
4461 for (l = 0; l < bmap->n_ineq; ++l) {
4462 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4463 continue;
4464 for (u = 0; u < bmap->n_ineq; ++u) {
4465 int c;
4467 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4468 continue;
4469 c = div_find_coalesce(bmap, pairs, i, l, u);
4470 if (c < 0)
4471 continue;
4472 free(pairs);
4473 bmap = coalesce_divs(bmap, i, c, l, u);
4474 return isl_basic_map_drop_redundant_divs(bmap);
4479 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4480 return bmap;
4482 return drop_more_redundant_divs(bmap, pairs, n);
4485 /* Are the "n" coefficients starting at "first" of inequality constraints
4486 * "i" and "j" of "bmap" equal to each other?
4488 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4489 int first, int n)
4491 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4494 /* Are the "n" coefficients starting at "first" of inequality constraints
4495 * "i" and "j" of "bmap" opposite to each other?
4497 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4498 int first, int n)
4500 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4503 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4504 * apart from the constant term?
4506 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4508 unsigned total;
4510 total = isl_basic_map_dim(bmap, isl_dim_all);
4511 return is_opposite_part(bmap, i, j, 1, total);
4514 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4515 * apart from the constant term and the coefficient at position "pos"?
4517 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4518 int pos)
4520 unsigned total;
4522 total = isl_basic_map_dim(bmap, isl_dim_all);
4523 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4524 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4527 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4528 * apart from the constant term and the coefficient at position "pos"?
4530 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4531 int pos)
4533 unsigned total;
4535 total = isl_basic_map_dim(bmap, isl_dim_all);
4536 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4537 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4540 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4541 * been modified, simplying it if "simplify" is set.
4542 * Free the temporary data structure "pairs" that was associated
4543 * to the old version of "bmap".
4545 static __isl_give isl_basic_map *drop_redundant_divs_again(
4546 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4548 if (simplify)
4549 bmap = isl_basic_map_simplify(bmap);
4550 free(pairs);
4551 return isl_basic_map_drop_redundant_divs(bmap);
4554 /* Is "div" the single unknown existentially quantified variable
4555 * in inequality constraint "ineq" of "bmap"?
4556 * "div" is known to have a non-zero coefficient in "ineq".
4558 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4560 int i;
4561 unsigned n_div, o_div;
4563 if (isl_basic_map_div_is_known(bmap, div))
4564 return 0;
4565 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4566 if (n_div == 1)
4567 return 1;
4568 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4569 for (i = 0; i < n_div; ++i) {
4570 if (i == div)
4571 continue;
4572 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4573 continue;
4574 if (!isl_basic_map_div_is_known(bmap, i))
4575 return 0;
4578 return 1;
4581 /* Does integer division "div" have coefficient 1 in inequality constraint
4582 * "ineq" of "map"?
4584 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4586 unsigned o_div;
4588 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4589 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4590 return 1;
4592 return 0;
4595 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4596 * then try and drop redundant divs again,
4597 * freeing the temporary data structure "pairs" that was associated
4598 * to the old version of "bmap".
4600 static __isl_give isl_basic_map *set_eq_and_try_again(
4601 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4603 bmap = isl_basic_map_cow(bmap);
4604 isl_basic_map_inequality_to_equality(bmap, ineq);
4605 return drop_redundant_divs_again(bmap, pairs, 1);
4608 /* Drop the integer division at position "div", along with the two
4609 * inequality constraints "ineq1" and "ineq2" in which it appears
4610 * from "bmap" and then try and drop redundant divs again,
4611 * freeing the temporary data structure "pairs" that was associated
4612 * to the old version of "bmap".
4614 static __isl_give isl_basic_map *drop_div_and_try_again(
4615 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4616 __isl_take int *pairs)
4618 if (ineq1 > ineq2) {
4619 isl_basic_map_drop_inequality(bmap, ineq1);
4620 isl_basic_map_drop_inequality(bmap, ineq2);
4621 } else {
4622 isl_basic_map_drop_inequality(bmap, ineq2);
4623 isl_basic_map_drop_inequality(bmap, ineq1);
4625 bmap = isl_basic_map_drop_div(bmap, div);
4626 return drop_redundant_divs_again(bmap, pairs, 0);
4629 /* Given two inequality constraints
4631 * f(x) + n d + c >= 0, (ineq)
4633 * with d the variable at position "pos", and
4635 * f(x) + c0 >= 0, (lower)
4637 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4638 * determined by the first constraint.
4639 * That is, store
4641 * ceil((c0 - c)/n)
4643 * in *l.
4645 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4646 int ineq, int lower, int pos, isl_int *l)
4648 isl_int_neg(*l, bmap->ineq[ineq][0]);
4649 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4650 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4653 /* Given two inequality constraints
4655 * f(x) + n d + c >= 0, (ineq)
4657 * with d the variable at position "pos", and
4659 * -f(x) - c0 >= 0, (upper)
4661 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4662 * determined by the first constraint.
4663 * That is, store
4665 * ceil((-c1 - c)/n)
4667 * in *u.
4669 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4670 int ineq, int upper, int pos, isl_int *u)
4672 isl_int_neg(*u, bmap->ineq[ineq][0]);
4673 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4674 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4677 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4678 * does the corresponding lower bound have a fixed value in "bmap"?
4680 * In particular, "ineq" is of the form
4682 * f(x) + n d + c >= 0
4684 * with n > 0, c the constant term and
4685 * d the existentially quantified variable "div".
4686 * That is, the lower bound is
4688 * ceil((-f(x) - c)/n)
4690 * Look for a pair of constraints
4692 * f(x) + c0 >= 0
4693 * -f(x) + c1 >= 0
4695 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4696 * That is, check that
4698 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4700 * If so, return the index of inequality f(x) + c0 >= 0.
4701 * Otherwise, return -1.
4703 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4705 int i;
4706 int lower = -1, upper = -1;
4707 unsigned o_div, n_div;
4708 isl_int l, u;
4709 int equal;
4711 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4712 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4713 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4714 if (i == ineq)
4715 continue;
4716 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4717 continue;
4718 if (lower < 0 &&
4719 is_parallel_except(bmap, ineq, i, o_div + div)) {
4720 lower = i;
4721 continue;
4723 if (upper < 0 &&
4724 is_opposite_except(bmap, ineq, i, o_div + div)) {
4725 upper = i;
4729 if (lower < 0 || upper < 0)
4730 return -1;
4732 isl_int_init(l);
4733 isl_int_init(u);
4735 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4736 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4738 equal = isl_int_eq(l, u);
4740 isl_int_clear(l);
4741 isl_int_clear(u);
4743 return equal ? lower : -1;
4746 /* Given a lower bound constraint "ineq" on the existentially quantified
4747 * variable "div", such that the corresponding lower bound has
4748 * a fixed value in "bmap", assign this fixed value to the variable and
4749 * then try and drop redundant divs again,
4750 * freeing the temporary data structure "pairs" that was associated
4751 * to the old version of "bmap".
4752 * "lower" determines the constant value for the lower bound.
4754 * In particular, "ineq" is of the form
4756 * f(x) + n d + c >= 0,
4758 * while "lower" is of the form
4760 * f(x) + c0 >= 0
4762 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4763 * is ceil((c0 - c)/n).
4765 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4766 int div, int ineq, int lower, int *pairs)
4768 isl_int c;
4769 unsigned o_div;
4771 isl_int_init(c);
4773 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4774 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4775 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4776 free(pairs);
4778 isl_int_clear(c);
4780 return isl_basic_map_drop_redundant_divs(bmap);
4783 /* Remove divs that are not strictly needed based on the inequality
4784 * constraints.
4785 * In particular, if a div only occurs positively (or negatively)
4786 * in constraints, then it can simply be dropped.
4787 * Also, if a div occurs in only two constraints and if moreover
4788 * those two constraints are opposite to each other, except for the constant
4789 * term and if the sum of the constant terms is such that for any value
4790 * of the other values, there is always at least one integer value of the
4791 * div, i.e., if one plus this sum is greater than or equal to
4792 * the (absolute value) of the coefficient of the div in the constraints,
4793 * then we can also simply drop the div.
4795 * If an existentially quantified variable does not have an explicit
4796 * representation, appears in only a single lower bound that does not
4797 * involve any other such existentially quantified variables and appears
4798 * in this lower bound with coefficient 1,
4799 * then fix the variable to the value of the lower bound. That is,
4800 * turn the inequality into an equality.
4801 * If for any value of the other variables, there is any value
4802 * for the existentially quantified variable satisfying the constraints,
4803 * then this lower bound also satisfies the constraints.
4804 * It is therefore safe to pick this lower bound.
4806 * The same reasoning holds even if the coefficient is not one.
4807 * However, fixing the variable to the value of the lower bound may
4808 * in general introduce an extra integer division, in which case
4809 * it may be better to pick another value.
4810 * If this integer division has a known constant value, then plugging
4811 * in this constant value removes the existentially quantified variable
4812 * completely. In particular, if the lower bound is of the form
4813 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4814 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4815 * then the existentially quantified variable can be assigned this
4816 * shared value.
4818 * We skip divs that appear in equalities or in the definition of other divs.
4819 * Divs that appear in the definition of other divs usually occur in at least
4820 * 4 constraints, but the constraints may have been simplified.
4822 * If any divs are left after these simple checks then we move on
4823 * to more complicated cases in drop_more_redundant_divs.
4825 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4826 __isl_take isl_basic_map *bmap)
4828 int i, j;
4829 unsigned off;
4830 int *pairs = NULL;
4831 int n = 0;
4833 if (!bmap)
4834 goto error;
4835 if (bmap->n_div == 0)
4836 return bmap;
4838 off = isl_space_dim(bmap->dim, isl_dim_all);
4839 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4840 if (!pairs)
4841 goto error;
4843 for (i = 0; i < bmap->n_div; ++i) {
4844 int pos, neg;
4845 int last_pos, last_neg;
4846 int redundant;
4847 int defined;
4849 defined = !isl_int_is_zero(bmap->div[i][0]);
4850 for (j = i; j < bmap->n_div; ++j)
4851 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4852 break;
4853 if (j < bmap->n_div)
4854 continue;
4855 for (j = 0; j < bmap->n_eq; ++j)
4856 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4857 break;
4858 if (j < bmap->n_eq)
4859 continue;
4860 ++n;
4861 pos = neg = 0;
4862 for (j = 0; j < bmap->n_ineq; ++j) {
4863 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4864 last_pos = j;
4865 ++pos;
4867 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4868 last_neg = j;
4869 ++neg;
4872 pairs[i] = pos * neg;
4873 if (pairs[i] == 0) {
4874 for (j = bmap->n_ineq - 1; j >= 0; --j)
4875 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4876 isl_basic_map_drop_inequality(bmap, j);
4877 bmap = isl_basic_map_drop_div(bmap, i);
4878 return drop_redundant_divs_again(bmap, pairs, 0);
4880 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4881 int single, lower;
4882 if (pos != 1)
4883 continue;
4884 single = single_unknown(bmap, last_pos, i);
4885 if (!single)
4886 continue;
4887 if (has_coef_one(bmap, i, last_pos))
4888 return set_eq_and_try_again(bmap, last_pos,
4889 pairs);
4890 lower = lower_bound_is_cst(bmap, i, last_pos);
4891 if (lower >= 0)
4892 return fix_cst_lower(bmap, i, last_pos, lower,
4893 pairs);
4894 continue;
4897 isl_int_add(bmap->ineq[last_pos][0],
4898 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4899 isl_int_add_ui(bmap->ineq[last_pos][0],
4900 bmap->ineq[last_pos][0], 1);
4901 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4902 bmap->ineq[last_pos][1+off+i]);
4903 isl_int_sub_ui(bmap->ineq[last_pos][0],
4904 bmap->ineq[last_pos][0], 1);
4905 isl_int_sub(bmap->ineq[last_pos][0],
4906 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4907 if (redundant)
4908 return drop_div_and_try_again(bmap, i,
4909 last_pos, last_neg, pairs);
4910 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
4911 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4912 return drop_redundant_divs_again(bmap, pairs, 1);
4914 pairs[i] = 0;
4915 --n;
4918 if (n > 0)
4919 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4921 free(pairs);
4922 return bmap;
4923 error:
4924 free(pairs);
4925 isl_basic_map_free(bmap);
4926 return NULL;
4929 /* Consider the coefficients at "c" as a row vector and replace
4930 * them with their product with "T". "T" is assumed to be a square matrix.
4932 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4934 int n;
4935 isl_ctx *ctx;
4936 isl_vec *v;
4938 if (!T)
4939 return isl_stat_error;
4940 n = isl_mat_rows(T);
4941 if (isl_seq_first_non_zero(c, n) == -1)
4942 return isl_stat_ok;
4943 ctx = isl_mat_get_ctx(T);
4944 v = isl_vec_alloc(ctx, n);
4945 if (!v)
4946 return isl_stat_error;
4947 isl_seq_swp_or_cpy(v->el, c, n);
4948 v = isl_vec_mat_product(v, isl_mat_copy(T));
4949 if (!v)
4950 return isl_stat_error;
4951 isl_seq_swp_or_cpy(c, v->el, n);
4952 isl_vec_free(v);
4954 return isl_stat_ok;
4957 /* Plug in T for the variables in "bmap" starting at "pos".
4958 * T is a linear unimodular matrix, i.e., without constant term.
4960 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4961 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4963 int i;
4964 unsigned n, total;
4966 bmap = isl_basic_map_cow(bmap);
4967 if (!bmap || !T)
4968 goto error;
4970 n = isl_mat_cols(T);
4971 if (n != isl_mat_rows(T))
4972 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4973 "expecting square matrix", goto error);
4975 total = isl_basic_map_dim(bmap, isl_dim_all);
4976 if (pos + n > total || pos + n < pos)
4977 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4978 "invalid range", goto error);
4980 for (i = 0; i < bmap->n_eq; ++i)
4981 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4982 goto error;
4983 for (i = 0; i < bmap->n_ineq; ++i)
4984 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4985 goto error;
4986 for (i = 0; i < bmap->n_div; ++i) {
4987 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4988 continue;
4989 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4990 goto error;
4993 isl_mat_free(T);
4994 return bmap;
4995 error:
4996 isl_basic_map_free(bmap);
4997 isl_mat_free(T);
4998 return NULL;
5001 /* Remove divs that are not strictly needed.
5003 * First look for an equality constraint involving two or more
5004 * existentially quantified variables without an explicit
5005 * representation. Replace the combination that appears
5006 * in the equality constraint by a single existentially quantified
5007 * variable such that the equality can be used to derive
5008 * an explicit representation for the variable.
5009 * If there are no more such equality constraints, then continue
5010 * with isl_basic_map_drop_redundant_divs_ineq.
5012 * In particular, if the equality constraint is of the form
5014 * f(x) + \sum_i c_i a_i = 0
5016 * with a_i existentially quantified variable without explicit
5017 * representation, then apply a transformation on the existentially
5018 * quantified variables to turn the constraint into
5020 * f(x) + g a_1' = 0
5022 * with g the gcd of the c_i.
5023 * In order to easily identify which existentially quantified variables
5024 * have a complete explicit representation, i.e., without being defined
5025 * in terms of other existentially quantified variables without
5026 * an explicit representation, the existentially quantified variables
5027 * are first sorted.
5029 * The variable transformation is computed by extending the row
5030 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5032 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5033 * [a_2'] [ a_2 ]
5034 * ... = U ....
5035 * [a_n'] [ a_n ]
5037 * with [c_1/g ... c_n/g] representing the first row of U.
5038 * The inverse of U is then plugged into the original constraints.
5039 * The call to isl_basic_map_simplify makes sure the explicit
5040 * representation for a_1' is extracted from the equality constraint.
5042 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5043 __isl_take isl_basic_map *bmap)
5045 int first;
5046 int i;
5047 unsigned o_div, n_div;
5048 int l;
5049 isl_ctx *ctx;
5050 isl_mat *T;
5052 if (!bmap)
5053 return NULL;
5054 if (isl_basic_map_divs_known(bmap))
5055 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5056 if (bmap->n_eq == 0)
5057 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5058 bmap = isl_basic_map_sort_divs(bmap);
5059 if (!bmap)
5060 return NULL;
5062 first = isl_basic_map_first_unknown_div(bmap);
5063 if (first < 0)
5064 return isl_basic_map_free(bmap);
5066 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5067 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5069 for (i = 0; i < bmap->n_eq; ++i) {
5070 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5071 n_div - (first));
5072 if (l < 0)
5073 continue;
5074 l += first;
5075 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5076 n_div - (l + 1)) == -1)
5077 continue;
5078 break;
5080 if (i >= bmap->n_eq)
5081 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5083 ctx = isl_basic_map_get_ctx(bmap);
5084 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5085 if (!T)
5086 return isl_basic_map_free(bmap);
5087 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5088 T = isl_mat_normalize_row(T, 0);
5089 T = isl_mat_unimodular_complete(T, 1);
5090 T = isl_mat_right_inverse(T);
5092 for (i = l; i < n_div; ++i)
5093 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5094 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5095 bmap = isl_basic_map_simplify(bmap);
5097 return isl_basic_map_drop_redundant_divs(bmap);
5100 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5101 struct isl_basic_set *bset)
5103 isl_basic_map *bmap = bset_to_bmap(bset);
5104 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5107 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5109 int i;
5111 if (!map)
5112 return NULL;
5113 for (i = 0; i < map->n; ++i) {
5114 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5115 if (!map->p[i])
5116 goto error;
5118 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5119 return map;
5120 error:
5121 isl_map_free(map);
5122 return NULL;
5125 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5127 return (struct isl_set *)
5128 isl_map_drop_redundant_divs(set_to_map(set));
5131 /* Does "bmap" satisfy any equality that involves more than 2 variables
5132 * and/or has coefficients different from -1 and 1?
5134 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5136 int i;
5137 unsigned total;
5139 total = isl_basic_map_dim(bmap, isl_dim_all);
5141 for (i = 0; i < bmap->n_eq; ++i) {
5142 int j, k;
5144 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5145 if (j < 0)
5146 continue;
5147 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5148 !isl_int_is_negone(bmap->eq[i][1 + j]))
5149 return 1;
5151 j += 1;
5152 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5153 if (k < 0)
5154 continue;
5155 j += k;
5156 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5157 !isl_int_is_negone(bmap->eq[i][1 + j]))
5158 return 1;
5160 j += 1;
5161 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5162 if (k >= 0)
5163 return 1;
5166 return 0;
5169 /* Remove any common factor g from the constraint coefficients in "v".
5170 * The constant term is stored in the first position and is replaced
5171 * by floor(c/g). If any common factor is removed and if this results
5172 * in a tightening of the constraint, then set *tightened.
5174 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5175 int *tightened)
5177 isl_ctx *ctx;
5179 if (!v)
5180 return NULL;
5181 ctx = isl_vec_get_ctx(v);
5182 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5183 if (isl_int_is_zero(ctx->normalize_gcd))
5184 return v;
5185 if (isl_int_is_one(ctx->normalize_gcd))
5186 return v;
5187 v = isl_vec_cow(v);
5188 if (!v)
5189 return NULL;
5190 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5191 *tightened = 1;
5192 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5193 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5194 v->size - 1);
5195 return v;
5198 /* If "bmap" is an integer set that satisfies any equality involving
5199 * more than 2 variables and/or has coefficients different from -1 and 1,
5200 * then use variable compression to reduce the coefficients by removing
5201 * any (hidden) common factor.
5202 * In particular, apply the variable compression to each constraint,
5203 * factor out any common factor in the non-constant coefficients and
5204 * then apply the inverse of the compression.
5205 * At the end, we mark the basic map as having reduced constants.
5206 * If this flag is still set on the next invocation of this function,
5207 * then we skip the computation.
5209 * Removing a common factor may result in a tightening of some of
5210 * the constraints. If this happens, then we may end up with two
5211 * opposite inequalities that can be replaced by an equality.
5212 * We therefore call isl_basic_map_detect_inequality_pairs,
5213 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5214 * and isl_basic_map_gauss if such a pair was found.
5216 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5217 __isl_take isl_basic_map *bmap)
5219 unsigned total;
5220 isl_ctx *ctx;
5221 isl_vec *v;
5222 isl_mat *eq, *T, *T2;
5223 int i;
5224 int tightened;
5226 if (!bmap)
5227 return NULL;
5228 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5229 return bmap;
5230 if (isl_basic_map_is_rational(bmap))
5231 return bmap;
5232 if (bmap->n_eq == 0)
5233 return bmap;
5234 if (!has_multiple_var_equality(bmap))
5235 return bmap;
5237 total = isl_basic_map_dim(bmap, isl_dim_all);
5238 ctx = isl_basic_map_get_ctx(bmap);
5239 v = isl_vec_alloc(ctx, 1 + total);
5240 if (!v)
5241 return isl_basic_map_free(bmap);
5243 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5244 T = isl_mat_variable_compression(eq, &T2);
5245 if (!T || !T2)
5246 goto error;
5247 if (T->n_col == 0) {
5248 isl_mat_free(T);
5249 isl_mat_free(T2);
5250 isl_vec_free(v);
5251 return isl_basic_map_set_to_empty(bmap);
5254 tightened = 0;
5255 for (i = 0; i < bmap->n_ineq; ++i) {
5256 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5257 v = isl_vec_mat_product(v, isl_mat_copy(T));
5258 v = normalize_constraint(v, &tightened);
5259 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5260 if (!v)
5261 goto error;
5262 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5265 isl_mat_free(T);
5266 isl_mat_free(T2);
5267 isl_vec_free(v);
5269 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5271 if (tightened) {
5272 int progress = 0;
5274 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5275 if (progress) {
5276 bmap = eliminate_divs_eq(bmap, &progress);
5277 bmap = isl_basic_map_gauss(bmap, NULL);
5281 return bmap;
5282 error:
5283 isl_mat_free(T);
5284 isl_mat_free(T2);
5285 isl_vec_free(v);
5286 return isl_basic_map_free(bmap);
5289 /* Shift the integer division at position "div" of "bmap"
5290 * by "shift" times the variable at position "pos".
5291 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5292 * corresponds to the constant term.
5294 * That is, if the integer division has the form
5296 * floor(f(x)/d)
5298 * then replace it by
5300 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5302 __isl_give isl_basic_map *isl_basic_map_shift_div(
5303 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5305 int i;
5306 unsigned total;
5308 if (!bmap)
5309 return NULL;
5311 total = isl_basic_map_dim(bmap, isl_dim_all);
5312 total -= isl_basic_map_dim(bmap, isl_dim_div);
5314 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5316 for (i = 0; i < bmap->n_eq; ++i) {
5317 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5318 continue;
5319 isl_int_submul(bmap->eq[i][pos],
5320 shift, bmap->eq[i][1 + total + div]);
5322 for (i = 0; i < bmap->n_ineq; ++i) {
5323 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5324 continue;
5325 isl_int_submul(bmap->ineq[i][pos],
5326 shift, bmap->ineq[i][1 + total + div]);
5328 for (i = 0; i < bmap->n_div; ++i) {
5329 if (isl_int_is_zero(bmap->div[i][0]))
5330 continue;
5331 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5332 continue;
5333 isl_int_submul(bmap->div[i][1 + pos],
5334 shift, bmap->div[i][1 + 1 + total + div]);
5337 return bmap;