isl_basic_{map,set}_free_inequality: return modified result
[isl.git] / isl_map_simplify.c
blob3d05c51675fd421e2326362d54f3058964fd8563
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map *bmap)
50 int i;
51 isl_int gcd;
52 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
54 if (total < 0)
55 return isl_basic_map_free(bmap);
57 isl_int_init(gcd);
58 for (i = bmap->n_eq - 1; i >= 0; --i) {
59 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60 if (isl_int_is_zero(gcd)) {
61 if (!isl_int_is_zero(bmap->eq[i][0])) {
62 bmap = isl_basic_map_set_to_empty(bmap);
63 break;
65 isl_basic_map_drop_equality(bmap, i);
66 continue;
68 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
69 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70 if (isl_int_is_one(gcd))
71 continue;
72 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
73 bmap = isl_basic_map_set_to_empty(bmap);
74 break;
76 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
79 for (i = bmap->n_ineq - 1; i >= 0; --i) {
80 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81 if (isl_int_is_zero(gcd)) {
82 if (isl_int_is_neg(bmap->ineq[i][0])) {
83 bmap = isl_basic_map_set_to_empty(bmap);
84 break;
86 isl_basic_map_drop_inequality(bmap, i);
87 continue;
89 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
90 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91 if (isl_int_is_one(gcd))
92 continue;
93 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
94 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
96 isl_int_clear(gcd);
98 return bmap;
101 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
102 __isl_take isl_basic_set *bset)
104 isl_basic_map *bmap = bset_to_bmap(bset);
105 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
108 /* Reduce the coefficient of the variable at position "pos"
109 * in integer division "div", such that it lies in the half-open
110 * interval (1/2,1/2], extracting any excess value from this integer division.
111 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112 * corresponds to the constant term.
114 * That is, the integer division is of the form
116 * floor((... + (c * d + r) * x_pos + ...)/d)
118 * with -d < 2 * r <= d.
119 * Replace it by
121 * floor((... + r * x_pos + ...)/d) + c * x_pos
123 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124 * Otherwise, c = floor((c * d + r)/d) + 1.
126 * This is the same normalization that is performed by isl_aff_floor.
128 static __isl_give isl_basic_map *reduce_coefficient_in_div(
129 __isl_take isl_basic_map *bmap, int div, int pos)
131 isl_int shift;
132 int add_one;
134 isl_int_init(shift);
135 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136 isl_int_mul_ui(shift, shift, 2);
137 add_one = isl_int_gt(shift, bmap->div[div][0]);
138 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139 if (add_one)
140 isl_int_add_ui(shift, shift, 1);
141 isl_int_neg(shift, shift);
142 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143 isl_int_clear(shift);
145 return bmap;
148 /* Does the coefficient of the variable at position "pos"
149 * in integer division "div" need to be reduced?
150 * That is, does it lie outside the half-open interval (1/2,1/2]?
151 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
152 * 2 * c != d.
154 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
155 int pos)
157 isl_bool r;
159 if (isl_int_is_zero(bmap->div[div][1 + pos]))
160 return isl_bool_false;
162 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
163 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
165 isl_int_divexact_ui(bmap->div[div][1 + pos],
166 bmap->div[div][1 + pos], 2);
168 return r;
171 /* Reduce the coefficients (including the constant term) of
172 * integer division "div", if needed.
173 * In particular, make sure all coefficients lie in
174 * the half-open interval (1/2,1/2].
176 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
177 __isl_take isl_basic_map *bmap, int div)
179 int i;
180 isl_size total;
182 total = isl_basic_map_dim(bmap, isl_dim_all);
183 if (total < 0)
184 return isl_basic_map_free(bmap);
185 for (i = 0; i < 1 + total; ++i) {
186 isl_bool reduce;
188 reduce = needs_reduction(bmap, div, i);
189 if (reduce < 0)
190 return isl_basic_map_free(bmap);
191 if (!reduce)
192 continue;
193 bmap = reduce_coefficient_in_div(bmap, div, i);
194 if (!bmap)
195 break;
198 return bmap;
201 /* Reduce the coefficients (including the constant term) of
202 * the known integer divisions, if needed
203 * In particular, make sure all coefficients lie in
204 * the half-open interval (1/2,1/2].
206 static __isl_give isl_basic_map *reduce_div_coefficients(
207 __isl_take isl_basic_map *bmap)
209 int i;
211 if (!bmap)
212 return NULL;
213 if (bmap->n_div == 0)
214 return bmap;
216 for (i = 0; i < bmap->n_div; ++i) {
217 if (isl_int_is_zero(bmap->div[i][0]))
218 continue;
219 bmap = reduce_div_coefficients_of_div(bmap, i);
220 if (!bmap)
221 break;
224 return bmap;
227 /* Remove any common factor in numerator and denominator of the div expression,
228 * not taking into account the constant term.
229 * That is, if the div is of the form
231 * floor((a + m f(x))/(m d))
233 * then replace it by
235 * floor((floor(a/m) + f(x))/d)
237 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
238 * and can therefore not influence the result of the floor.
240 static __isl_give isl_basic_map *normalize_div_expression(
241 __isl_take isl_basic_map *bmap, int div)
243 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
244 isl_ctx *ctx = bmap->ctx;
246 if (total < 0)
247 return isl_basic_map_free(bmap);
248 if (isl_int_is_zero(bmap->div[div][0]))
249 return bmap;
250 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
251 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
252 if (isl_int_is_one(ctx->normalize_gcd))
253 return bmap;
254 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
255 ctx->normalize_gcd);
256 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
257 ctx->normalize_gcd);
258 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
259 ctx->normalize_gcd, total);
261 return bmap;
264 /* Remove any common factor in numerator and denominator of a div expression,
265 * not taking into account the constant term.
266 * That is, look for any div of the form
268 * floor((a + m f(x))/(m d))
270 * and replace it by
272 * floor((floor(a/m) + f(x))/d)
274 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
275 * and can therefore not influence the result of the floor.
277 static __isl_give isl_basic_map *normalize_div_expressions(
278 __isl_take isl_basic_map *bmap)
280 int i;
282 if (!bmap)
283 return NULL;
284 if (bmap->n_div == 0)
285 return bmap;
287 for (i = 0; i < bmap->n_div; ++i)
288 bmap = normalize_div_expression(bmap, i);
290 return bmap;
293 /* Assumes divs have been ordered if keep_divs is set.
295 static __isl_give isl_basic_map *eliminate_var_using_equality(
296 __isl_take isl_basic_map *bmap,
297 unsigned pos, isl_int *eq, int keep_divs, int *progress)
299 isl_size total;
300 isl_size v_div;
301 int k;
302 int last_div;
304 total = isl_basic_map_dim(bmap, isl_dim_all);
305 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
306 if (total < 0 || v_div < 0)
307 return isl_basic_map_free(bmap);
308 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
309 for (k = 0; k < bmap->n_eq; ++k) {
310 if (bmap->eq[k] == eq)
311 continue;
312 if (isl_int_is_zero(bmap->eq[k][1+pos]))
313 continue;
314 if (progress)
315 *progress = 1;
316 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
317 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
320 for (k = 0; k < bmap->n_ineq; ++k) {
321 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
322 continue;
323 if (progress)
324 *progress = 1;
325 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
326 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
327 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
328 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
331 for (k = 0; k < bmap->n_div; ++k) {
332 if (isl_int_is_zero(bmap->div[k][0]))
333 continue;
334 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
335 continue;
336 if (progress)
337 *progress = 1;
338 /* We need to be careful about circular definitions,
339 * so for now we just remove the definition of div k
340 * if the equality contains any divs.
341 * If keep_divs is set, then the divs have been ordered
342 * and we can keep the definition as long as the result
343 * is still ordered.
345 if (last_div == -1 || (keep_divs && last_div < k)) {
346 isl_seq_elim(bmap->div[k]+1, eq,
347 1+pos, 1+total, &bmap->div[k][0]);
348 bmap = normalize_div_expression(bmap, k);
349 if (!bmap)
350 return NULL;
351 } else
352 isl_seq_clr(bmap->div[k], 1 + total);
355 return bmap;
358 /* Assumes divs have been ordered if keep_divs is set.
360 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
361 isl_int *eq, unsigned div, int keep_divs)
363 isl_size v_div;
364 unsigned pos;
366 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
367 if (v_div < 0)
368 return isl_basic_map_free(bmap);
369 pos = v_div + div;
370 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
372 bmap = isl_basic_map_drop_div(bmap, div);
374 return bmap;
377 /* Check if elimination of div "div" using equality "eq" would not
378 * result in a div depending on a later div.
380 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
381 unsigned div)
383 int k;
384 int last_div;
385 isl_size v_div;
386 unsigned pos;
388 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
389 if (v_div < 0)
390 return isl_bool_error;
391 pos = v_div + div;
393 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
394 if (last_div < 0 || last_div <= div)
395 return isl_bool_true;
397 for (k = 0; k <= last_div; ++k) {
398 if (isl_int_is_zero(bmap->div[k][0]))
399 continue;
400 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
401 return isl_bool_false;
404 return isl_bool_true;
407 /* Eliminate divs based on equalities
409 static __isl_give isl_basic_map *eliminate_divs_eq(
410 __isl_take isl_basic_map *bmap, int *progress)
412 int d;
413 int i;
414 int modified = 0;
415 unsigned off;
417 bmap = isl_basic_map_order_divs(bmap);
419 if (!bmap)
420 return NULL;
422 off = isl_basic_map_offset(bmap, isl_dim_div);
424 for (d = bmap->n_div - 1; d >= 0 ; --d) {
425 for (i = 0; i < bmap->n_eq; ++i) {
426 isl_bool ok;
428 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
429 !isl_int_is_negone(bmap->eq[i][off + d]))
430 continue;
431 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
432 if (ok < 0)
433 return isl_basic_map_free(bmap);
434 if (!ok)
435 continue;
436 modified = 1;
437 *progress = 1;
438 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
439 if (isl_basic_map_drop_equality(bmap, i) < 0)
440 return isl_basic_map_free(bmap);
441 break;
444 if (modified)
445 return eliminate_divs_eq(bmap, progress);
446 return bmap;
449 /* Eliminate divs based on inequalities
451 static __isl_give isl_basic_map *eliminate_divs_ineq(
452 __isl_take isl_basic_map *bmap, int *progress)
454 int d;
455 int i;
456 unsigned off;
457 struct isl_ctx *ctx;
459 if (!bmap)
460 return NULL;
462 ctx = bmap->ctx;
463 off = isl_basic_map_offset(bmap, isl_dim_div);
465 for (d = bmap->n_div - 1; d >= 0 ; --d) {
466 for (i = 0; i < bmap->n_eq; ++i)
467 if (!isl_int_is_zero(bmap->eq[i][off + d]))
468 break;
469 if (i < bmap->n_eq)
470 continue;
471 for (i = 0; i < bmap->n_ineq; ++i)
472 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
473 break;
474 if (i < bmap->n_ineq)
475 continue;
476 *progress = 1;
477 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
478 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
479 break;
480 bmap = isl_basic_map_drop_div(bmap, d);
481 if (!bmap)
482 break;
484 return bmap;
487 /* Does the equality constraint at position "eq" in "bmap" involve
488 * any local variables in the range [first, first + n)
489 * that are not marked as having an explicit representation?
491 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
492 int eq, unsigned first, unsigned n)
494 unsigned o_div;
495 int i;
497 if (!bmap)
498 return isl_bool_error;
500 o_div = isl_basic_map_offset(bmap, isl_dim_div);
501 for (i = 0; i < n; ++i) {
502 isl_bool unknown;
504 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
505 continue;
506 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
507 if (unknown < 0)
508 return isl_bool_error;
509 if (unknown)
510 return isl_bool_true;
513 return isl_bool_false;
516 /* The last local variable involved in the equality constraint
517 * at position "eq" in "bmap" is the local variable at position "div".
518 * It can therefore be used to extract an explicit representation
519 * for that variable.
520 * Do so unless the local variable already has an explicit representation or
521 * the explicit representation would involve any other local variables
522 * that in turn do not have an explicit representation.
523 * An equality constraint involving local variables without an explicit
524 * representation can be used in isl_basic_map_drop_redundant_divs
525 * to separate out an independent local variable. Introducing
526 * an explicit representation here would block this transformation,
527 * while the partial explicit representation in itself is not very useful.
528 * Set *progress if anything is changed.
530 * The equality constraint is of the form
532 * f(x) + n e >= 0
534 * with n a positive number. The explicit representation derived from
535 * this constraint is
537 * floor((-f(x))/n)
539 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
540 int div, int eq, int *progress)
542 isl_size total;
543 unsigned o_div;
544 isl_bool involves;
546 if (!bmap)
547 return NULL;
549 if (!isl_int_is_zero(bmap->div[div][0]))
550 return bmap;
552 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
553 if (involves < 0)
554 return isl_basic_map_free(bmap);
555 if (involves)
556 return bmap;
558 total = isl_basic_map_dim(bmap, isl_dim_all);
559 if (total < 0)
560 return isl_basic_map_free(bmap);
561 o_div = isl_basic_map_offset(bmap, isl_dim_div);
562 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
563 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
564 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
565 if (progress)
566 *progress = 1;
568 return bmap;
571 /* Perform fangcheng (Gaussian elimination) on the equality
572 * constraints of "bmap".
573 * That is, put them into row-echelon form, starting from the last column
574 * backward and use them to eliminate the corresponding coefficients
575 * from all constraints.
577 * If "progress" is not NULL, then it gets set if the elimination
578 * result in any changes.
579 * The elimination process may result in some equality constraints
580 * getting interchanged or removed.
581 * If "swap" or "drop" are not NULL, then they get called when
582 * two equality constraints get interchanged or
583 * when a number of final equality constraints get removed.
584 * As a special case, if the input turns out to be empty,
585 * then drop gets called with the number of removed equality
586 * constraints set to the total number of equality constraints.
587 * If "swap" or "drop" are not NULL, then the local variables (if any)
588 * are assumed to be in a valid order.
590 __isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
591 int *progress,
592 isl_stat (*swap)(unsigned a, unsigned b, void *user),
593 isl_stat (*drop)(unsigned n, void *user), void *user)
595 int k;
596 int done;
597 int last_var;
598 unsigned total_var;
599 isl_size total;
600 unsigned n_drop;
602 if (!swap && !drop)
603 bmap = isl_basic_map_order_divs(bmap);
605 total = isl_basic_map_dim(bmap, isl_dim_all);
606 if (total < 0)
607 return isl_basic_map_free(bmap);
609 total_var = total - bmap->n_div;
611 last_var = total - 1;
612 for (done = 0; done < bmap->n_eq; ++done) {
613 for (; last_var >= 0; --last_var) {
614 for (k = done; k < bmap->n_eq; ++k)
615 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
616 break;
617 if (k < bmap->n_eq)
618 break;
620 if (last_var < 0)
621 break;
622 if (k != done) {
623 swap_equality(bmap, k, done);
624 if (swap && swap(k, done, user) < 0)
625 return isl_basic_map_free(bmap);
627 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
628 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
630 bmap = eliminate_var_using_equality(bmap, last_var,
631 bmap->eq[done], 1, progress);
633 if (last_var >= total_var)
634 bmap = set_div_from_eq(bmap, last_var - total_var,
635 done, progress);
636 if (!bmap)
637 return NULL;
639 if (done == bmap->n_eq)
640 return bmap;
641 for (k = done; k < bmap->n_eq; ++k) {
642 if (isl_int_is_zero(bmap->eq[k][0]))
643 continue;
644 if (drop && drop(bmap->n_eq, user) < 0)
645 return isl_basic_map_free(bmap);
646 return isl_basic_map_set_to_empty(bmap);
648 n_drop = bmap->n_eq - done;
649 bmap = isl_basic_map_free_equality(bmap, n_drop);
650 if (drop && drop(n_drop, user) < 0)
651 return isl_basic_map_free(bmap);
652 return bmap;
655 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
656 int *progress)
658 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
661 __isl_give isl_basic_set *isl_basic_set_gauss(
662 __isl_take isl_basic_set *bset, int *progress)
664 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
665 progress));
669 static unsigned int round_up(unsigned int v)
671 int old_v = v;
673 while (v) {
674 old_v = v;
675 v ^= v & -v;
677 return old_v << 1;
680 /* Hash table of inequalities in a basic map.
681 * "index" is an array of addresses of inequalities in the basic map, some
682 * of which are NULL. The inequalities are hashed on the coefficients
683 * except the constant term.
684 * "size" is the number of elements in the array and is always a power of two
685 * "bits" is the number of bits need to represent an index into the array.
686 * "total" is the total dimension of the basic map.
688 struct isl_constraint_index {
689 unsigned int size;
690 int bits;
691 isl_int ***index;
692 isl_size total;
695 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
697 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
698 __isl_keep isl_basic_map *bmap)
700 isl_ctx *ctx;
702 ci->index = NULL;
703 if (!bmap)
704 return isl_stat_error;
705 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
706 if (ci->total < 0)
707 return isl_stat_error;
708 if (bmap->n_ineq == 0)
709 return isl_stat_ok;
710 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
711 ci->bits = ffs(ci->size) - 1;
712 ctx = isl_basic_map_get_ctx(bmap);
713 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
714 if (!ci->index)
715 return isl_stat_error;
717 return isl_stat_ok;
720 /* Free the memory allocated by create_constraint_index.
722 static void constraint_index_free(struct isl_constraint_index *ci)
724 free(ci->index);
727 /* Return the position in ci->index that contains the address of
728 * an inequality that is equal to *ineq up to the constant term,
729 * provided this address is not identical to "ineq".
730 * If there is no such inequality, then return the position where
731 * such an inequality should be inserted.
733 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
735 int h;
736 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
737 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
738 if (ineq != ci->index[h] &&
739 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
740 break;
741 return h;
744 /* Return the position in ci->index that contains the address of
745 * an inequality that is equal to the k'th inequality of "bmap"
746 * up to the constant term, provided it does not point to the very
747 * same inequality.
748 * If there is no such inequality, then return the position where
749 * such an inequality should be inserted.
751 static int hash_index(struct isl_constraint_index *ci,
752 __isl_keep isl_basic_map *bmap, int k)
754 return hash_index_ineq(ci, &bmap->ineq[k]);
757 static int set_hash_index(struct isl_constraint_index *ci,
758 __isl_keep isl_basic_set *bset, int k)
760 return hash_index(ci, bset, k);
763 /* Fill in the "ci" data structure with the inequalities of "bset".
765 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
766 __isl_keep isl_basic_set *bset)
768 int k, h;
770 if (create_constraint_index(ci, bset) < 0)
771 return isl_stat_error;
773 for (k = 0; k < bset->n_ineq; ++k) {
774 h = set_hash_index(ci, bset, k);
775 ci->index[h] = &bset->ineq[k];
778 return isl_stat_ok;
781 /* Is the inequality ineq (obviously) redundant with respect
782 * to the constraints in "ci"?
784 * Look for an inequality in "ci" with the same coefficients and then
785 * check if the contant term of "ineq" is greater than or equal
786 * to the constant term of that inequality. If so, "ineq" is clearly
787 * redundant.
789 * Note that hash_index_ineq ignores a stored constraint if it has
790 * the same address as the passed inequality. It is ok to pass
791 * the address of a local variable here since it will never be
792 * the same as the address of a constraint in "ci".
794 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
795 isl_int *ineq)
797 int h;
799 h = hash_index_ineq(ci, &ineq);
800 if (!ci->index[h])
801 return isl_bool_false;
802 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
805 /* If we can eliminate more than one div, then we need to make
806 * sure we do it from last div to first div, in order not to
807 * change the position of the other divs that still need to
808 * be removed.
810 static __isl_give isl_basic_map *remove_duplicate_divs(
811 __isl_take isl_basic_map *bmap, int *progress)
813 unsigned int size;
814 int *index;
815 int *elim_for;
816 int k, l, h;
817 int bits;
818 struct isl_blk eq;
819 isl_size v_div;
820 unsigned total;
821 struct isl_ctx *ctx;
823 bmap = isl_basic_map_order_divs(bmap);
824 if (!bmap || bmap->n_div <= 1)
825 return bmap;
827 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
828 if (v_div < 0)
829 return isl_basic_map_free(bmap);
830 total = v_div + bmap->n_div;
832 ctx = bmap->ctx;
833 for (k = bmap->n_div - 1; k >= 0; --k)
834 if (!isl_int_is_zero(bmap->div[k][0]))
835 break;
836 if (k <= 0)
837 return bmap;
839 size = round_up(4 * bmap->n_div / 3 - 1);
840 if (size == 0)
841 return bmap;
842 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
843 bits = ffs(size) - 1;
844 index = isl_calloc_array(ctx, int, size);
845 if (!elim_for || !index)
846 goto out;
847 eq = isl_blk_alloc(ctx, 1+total);
848 if (isl_blk_is_error(eq))
849 goto out;
851 isl_seq_clr(eq.data, 1+total);
852 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
853 for (--k; k >= 0; --k) {
854 uint32_t hash;
856 if (isl_int_is_zero(bmap->div[k][0]))
857 continue;
859 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
860 for (h = hash; index[h]; h = (h+1) % size)
861 if (isl_seq_eq(bmap->div[k],
862 bmap->div[index[h]-1], 2+total))
863 break;
864 if (index[h]) {
865 *progress = 1;
866 l = index[h] - 1;
867 elim_for[l] = k + 1;
869 index[h] = k+1;
871 for (l = bmap->n_div - 1; l >= 0; --l) {
872 if (!elim_for[l])
873 continue;
874 k = elim_for[l] - 1;
875 isl_int_set_si(eq.data[1 + v_div + k], -1);
876 isl_int_set_si(eq.data[1 + v_div + l], 1);
877 bmap = eliminate_div(bmap, eq.data, l, 1);
878 if (!bmap)
879 break;
880 isl_int_set_si(eq.data[1 + v_div + k], 0);
881 isl_int_set_si(eq.data[1 + v_div + l], 0);
884 isl_blk_free(ctx, eq);
885 out:
886 free(index);
887 free(elim_for);
888 return bmap;
891 static int n_pure_div_eq(struct isl_basic_map *bmap)
893 int i, j;
894 isl_size v_div;
896 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
897 if (v_div < 0)
898 return -1;
899 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
900 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
901 --j;
902 if (j < 0)
903 break;
904 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
905 return 0;
907 return i;
910 /* Normalize divs that appear in equalities.
912 * In particular, we assume that bmap contains some equalities
913 * of the form
915 * a x = m * e_i
917 * and we want to replace the set of e_i by a minimal set and
918 * such that the new e_i have a canonical representation in terms
919 * of the vector x.
920 * If any of the equalities involves more than one divs, then
921 * we currently simply bail out.
923 * Let us first additionally assume that all equalities involve
924 * a div. The equalities then express modulo constraints on the
925 * remaining variables and we can use "parameter compression"
926 * to find a minimal set of constraints. The result is a transformation
928 * x = T(x') = x_0 + G x'
930 * with G a lower-triangular matrix with all elements below the diagonal
931 * non-negative and smaller than the diagonal element on the same row.
932 * We first normalize x_0 by making the same property hold in the affine
933 * T matrix.
934 * The rows i of G with a 1 on the diagonal do not impose any modulo
935 * constraint and simply express x_i = x'_i.
936 * For each of the remaining rows i, we introduce a div and a corresponding
937 * equality. In particular
939 * g_ii e_j = x_i - g_i(x')
941 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
942 * corresponding div (if g_kk != 1).
944 * If there are any equalities not involving any div, then we
945 * first apply a variable compression on the variables x:
947 * x = C x'' x'' = C_2 x
949 * and perform the above parameter compression on A C instead of on A.
950 * The resulting compression is then of the form
952 * x'' = T(x') = x_0 + G x'
954 * and in constructing the new divs and the corresponding equalities,
955 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
956 * by the corresponding row from C_2.
958 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
959 int *progress)
961 int i, j, k;
962 isl_size v_div;
963 int div_eq;
964 struct isl_mat *B;
965 struct isl_vec *d;
966 struct isl_mat *T = NULL;
967 struct isl_mat *C = NULL;
968 struct isl_mat *C2 = NULL;
969 isl_int v;
970 int *pos = NULL;
971 int dropped, needed;
973 if (!bmap)
974 return NULL;
976 if (bmap->n_div == 0)
977 return bmap;
979 if (bmap->n_eq == 0)
980 return bmap;
982 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
983 return bmap;
985 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
986 div_eq = n_pure_div_eq(bmap);
987 if (v_div < 0 || div_eq < 0)
988 return isl_basic_map_free(bmap);
989 if (div_eq == 0)
990 return bmap;
992 if (div_eq < bmap->n_eq) {
993 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
994 bmap->n_eq - div_eq, 0, 1 + v_div);
995 C = isl_mat_variable_compression(B, &C2);
996 if (!C || !C2)
997 goto error;
998 if (C->n_col == 0) {
999 bmap = isl_basic_map_set_to_empty(bmap);
1000 isl_mat_free(C);
1001 isl_mat_free(C2);
1002 goto done;
1006 d = isl_vec_alloc(bmap->ctx, div_eq);
1007 if (!d)
1008 goto error;
1009 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1010 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1011 --j;
1012 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1014 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1016 if (C) {
1017 B = isl_mat_product(B, C);
1018 C = NULL;
1021 T = isl_mat_parameter_compression(B, d);
1022 if (!T)
1023 goto error;
1024 if (T->n_col == 0) {
1025 bmap = isl_basic_map_set_to_empty(bmap);
1026 isl_mat_free(C2);
1027 isl_mat_free(T);
1028 goto done;
1030 isl_int_init(v);
1031 for (i = 0; i < T->n_row - 1; ++i) {
1032 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1033 if (isl_int_is_zero(v))
1034 continue;
1035 isl_mat_col_submul(T, 0, v, 1 + i);
1037 isl_int_clear(v);
1038 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1039 if (!pos)
1040 goto error;
1041 /* We have to be careful because dropping equalities may reorder them */
1042 dropped = 0;
1043 for (j = bmap->n_div - 1; j >= 0; --j) {
1044 for (i = 0; i < bmap->n_eq; ++i)
1045 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1046 break;
1047 if (i < bmap->n_eq) {
1048 bmap = isl_basic_map_drop_div(bmap, j);
1049 isl_basic_map_drop_equality(bmap, i);
1050 ++dropped;
1053 pos[0] = 0;
1054 needed = 0;
1055 for (i = 1; i < T->n_row; ++i) {
1056 if (isl_int_is_one(T->row[i][i]))
1057 pos[i] = i;
1058 else
1059 needed++;
1061 if (needed > dropped) {
1062 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1063 needed, needed, 0);
1064 if (!bmap)
1065 goto error;
1067 for (i = 1; i < T->n_row; ++i) {
1068 if (isl_int_is_one(T->row[i][i]))
1069 continue;
1070 k = isl_basic_map_alloc_div(bmap);
1071 pos[i] = 1 + v_div + k;
1072 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1073 isl_int_set(bmap->div[k][0], T->row[i][i]);
1074 if (C2)
1075 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1076 else
1077 isl_int_set_si(bmap->div[k][1 + i], 1);
1078 for (j = 0; j < i; ++j) {
1079 if (isl_int_is_zero(T->row[i][j]))
1080 continue;
1081 if (pos[j] < T->n_row && C2)
1082 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1083 C2->row[pos[j]], 1 + v_div);
1084 else
1085 isl_int_neg(bmap->div[k][1 + pos[j]],
1086 T->row[i][j]);
1088 j = isl_basic_map_alloc_equality(bmap);
1089 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1090 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1092 free(pos);
1093 isl_mat_free(C2);
1094 isl_mat_free(T);
1096 if (progress)
1097 *progress = 1;
1098 done:
1099 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1101 return bmap;
1102 error:
1103 free(pos);
1104 isl_mat_free(C);
1105 isl_mat_free(C2);
1106 isl_mat_free(T);
1107 return bmap;
1110 static __isl_give isl_basic_map *set_div_from_lower_bound(
1111 __isl_take isl_basic_map *bmap, int div, int ineq)
1113 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1115 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1116 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1117 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1118 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1119 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1121 return bmap;
1124 /* Check whether it is ok to define a div based on an inequality.
1125 * To avoid the introduction of circular definitions of divs, we
1126 * do not allow such a definition if the resulting expression would refer to
1127 * any other undefined divs or if any known div is defined in
1128 * terms of the unknown div.
1130 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1131 int div, int ineq)
1133 int j;
1134 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1136 /* Not defined in terms of unknown divs */
1137 for (j = 0; j < bmap->n_div; ++j) {
1138 if (div == j)
1139 continue;
1140 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1141 continue;
1142 if (isl_int_is_zero(bmap->div[j][0]))
1143 return isl_bool_false;
1146 /* No other div defined in terms of this one => avoid loops */
1147 for (j = 0; j < bmap->n_div; ++j) {
1148 if (div == j)
1149 continue;
1150 if (isl_int_is_zero(bmap->div[j][0]))
1151 continue;
1152 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1153 return isl_bool_false;
1156 return isl_bool_true;
1159 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1160 * be a better expression than the current one?
1162 * If we do not have any expression yet, then any expression would be better.
1163 * Otherwise we check if the last variable involved in the inequality
1164 * (disregarding the div that it would define) is in an earlier position
1165 * than the last variable involved in the current div expression.
1167 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1168 int div, int ineq)
1170 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1171 int last_div;
1172 int last_ineq;
1174 if (isl_int_is_zero(bmap->div[div][0]))
1175 return isl_bool_true;
1177 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1178 bmap->n_div - (div + 1)) >= 0)
1179 return isl_bool_false;
1181 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1182 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1183 total + bmap->n_div);
1185 return last_ineq < last_div;
1188 /* Given two constraints "k" and "l" that are opposite to each other,
1189 * except for the constant term, check if we can use them
1190 * to obtain an expression for one of the hitherto unknown divs or
1191 * a "better" expression for a div for which we already have an expression.
1192 * "sum" is the sum of the constant terms of the constraints.
1193 * If this sum is strictly smaller than the coefficient of one
1194 * of the divs, then this pair can be used define the div.
1195 * To avoid the introduction of circular definitions of divs, we
1196 * do not use the pair if the resulting expression would refer to
1197 * any other undefined divs or if any known div is defined in
1198 * terms of the unknown div.
1200 static __isl_give isl_basic_map *check_for_div_constraints(
1201 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1202 int *progress)
1204 int i;
1205 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1207 for (i = 0; i < bmap->n_div; ++i) {
1208 isl_bool set_div;
1210 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1211 continue;
1212 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1213 continue;
1214 set_div = better_div_constraint(bmap, i, k);
1215 if (set_div >= 0 && set_div)
1216 set_div = ok_to_set_div_from_bound(bmap, i, k);
1217 if (set_div < 0)
1218 return isl_basic_map_free(bmap);
1219 if (!set_div)
1220 break;
1221 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1222 bmap = set_div_from_lower_bound(bmap, i, k);
1223 else
1224 bmap = set_div_from_lower_bound(bmap, i, l);
1225 if (progress)
1226 *progress = 1;
1227 break;
1229 return bmap;
1232 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1233 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1235 struct isl_constraint_index ci;
1236 int k, l, h;
1237 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1238 isl_int sum;
1240 if (total < 0 || bmap->n_ineq <= 1)
1241 return bmap;
1243 if (create_constraint_index(&ci, bmap) < 0)
1244 return bmap;
1246 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1247 ci.index[h] = &bmap->ineq[0];
1248 for (k = 1; k < bmap->n_ineq; ++k) {
1249 h = hash_index(&ci, bmap, k);
1250 if (!ci.index[h]) {
1251 ci.index[h] = &bmap->ineq[k];
1252 continue;
1254 if (progress)
1255 *progress = 1;
1256 l = ci.index[h] - &bmap->ineq[0];
1257 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1258 swap_inequality(bmap, k, l);
1259 isl_basic_map_drop_inequality(bmap, k);
1260 --k;
1262 isl_int_init(sum);
1263 for (k = 0; k < bmap->n_ineq-1; ++k) {
1264 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1265 h = hash_index(&ci, bmap, k);
1266 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1267 if (!ci.index[h])
1268 continue;
1269 l = ci.index[h] - &bmap->ineq[0];
1270 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1271 if (isl_int_is_pos(sum)) {
1272 if (detect_divs)
1273 bmap = check_for_div_constraints(bmap, k, l,
1274 sum, progress);
1275 continue;
1277 if (isl_int_is_zero(sum)) {
1278 /* We need to break out of the loop after these
1279 * changes since the contents of the hash
1280 * will no longer be valid.
1281 * Plus, we probably we want to regauss first.
1283 if (progress)
1284 *progress = 1;
1285 isl_basic_map_drop_inequality(bmap, l);
1286 isl_basic_map_inequality_to_equality(bmap, k);
1287 } else
1288 bmap = isl_basic_map_set_to_empty(bmap);
1289 break;
1291 isl_int_clear(sum);
1293 constraint_index_free(&ci);
1294 return bmap;
1297 /* Detect all pairs of inequalities that form an equality.
1299 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1300 * Call it repeatedly while it is making progress.
1302 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1303 __isl_take isl_basic_map *bmap, int *progress)
1305 int duplicate;
1307 do {
1308 duplicate = 0;
1309 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1310 &duplicate, 0);
1311 if (progress && duplicate)
1312 *progress = 1;
1313 } while (duplicate);
1315 return bmap;
1318 /* Eliminate knowns divs from constraints where they appear with
1319 * a (positive or negative) unit coefficient.
1321 * That is, replace
1323 * floor(e/m) + f >= 0
1325 * by
1327 * e + m f >= 0
1329 * and
1331 * -floor(e/m) + f >= 0
1333 * by
1335 * -e + m f + m - 1 >= 0
1337 * The first conversion is valid because floor(e/m) >= -f is equivalent
1338 * to e/m >= -f because -f is an integral expression.
1339 * The second conversion follows from the fact that
1341 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1344 * Note that one of the div constraints may have been eliminated
1345 * due to being redundant with respect to the constraint that is
1346 * being modified by this function. The modified constraint may
1347 * no longer imply this div constraint, so we add it back to make
1348 * sure we do not lose any information.
1350 * We skip integral divs, i.e., those with denominator 1, as we would
1351 * risk eliminating the div from the div constraints. We do not need
1352 * to handle those divs here anyway since the div constraints will turn
1353 * out to form an equality and this equality can then be used to eliminate
1354 * the div from all constraints.
1356 static __isl_give isl_basic_map *eliminate_unit_divs(
1357 __isl_take isl_basic_map *bmap, int *progress)
1359 int i, j;
1360 isl_ctx *ctx;
1361 unsigned total;
1363 if (!bmap)
1364 return NULL;
1366 ctx = isl_basic_map_get_ctx(bmap);
1367 total = isl_basic_map_offset(bmap, isl_dim_div);
1369 for (i = 0; i < bmap->n_div; ++i) {
1370 if (isl_int_is_zero(bmap->div[i][0]))
1371 continue;
1372 if (isl_int_is_one(bmap->div[i][0]))
1373 continue;
1374 for (j = 0; j < bmap->n_ineq; ++j) {
1375 int s;
1377 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1378 !isl_int_is_negone(bmap->ineq[j][total + i]))
1379 continue;
1381 *progress = 1;
1383 s = isl_int_sgn(bmap->ineq[j][total + i]);
1384 isl_int_set_si(bmap->ineq[j][total + i], 0);
1385 if (s < 0)
1386 isl_seq_combine(bmap->ineq[j],
1387 ctx->negone, bmap->div[i] + 1,
1388 bmap->div[i][0], bmap->ineq[j],
1389 total + bmap->n_div);
1390 else
1391 isl_seq_combine(bmap->ineq[j],
1392 ctx->one, bmap->div[i] + 1,
1393 bmap->div[i][0], bmap->ineq[j],
1394 total + bmap->n_div);
1395 if (s < 0) {
1396 isl_int_add(bmap->ineq[j][0],
1397 bmap->ineq[j][0], bmap->div[i][0]);
1398 isl_int_sub_ui(bmap->ineq[j][0],
1399 bmap->ineq[j][0], 1);
1402 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1403 bmap = isl_basic_map_add_div_constraint(bmap, i, s);
1404 if (!bmap)
1405 return NULL;
1409 return bmap;
1412 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1414 int progress = 1;
1415 if (!bmap)
1416 return NULL;
1417 while (progress) {
1418 isl_bool empty;
1420 progress = 0;
1421 empty = isl_basic_map_plain_is_empty(bmap);
1422 if (empty < 0)
1423 return isl_basic_map_free(bmap);
1424 if (empty)
1425 break;
1426 bmap = isl_basic_map_normalize_constraints(bmap);
1427 bmap = reduce_div_coefficients(bmap);
1428 bmap = normalize_div_expressions(bmap);
1429 bmap = remove_duplicate_divs(bmap, &progress);
1430 bmap = eliminate_unit_divs(bmap, &progress);
1431 bmap = eliminate_divs_eq(bmap, &progress);
1432 bmap = eliminate_divs_ineq(bmap, &progress);
1433 bmap = isl_basic_map_gauss(bmap, &progress);
1434 /* requires equalities in normal form */
1435 bmap = normalize_divs(bmap, &progress);
1436 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1437 &progress, 1);
1438 if (bmap && progress)
1439 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1441 return bmap;
1444 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1446 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1450 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1451 isl_int *constraint, unsigned div)
1453 unsigned pos;
1455 if (!bmap)
1456 return isl_bool_error;
1458 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1460 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1461 int neg;
1462 isl_int_sub(bmap->div[div][1],
1463 bmap->div[div][1], bmap->div[div][0]);
1464 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1465 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1466 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1467 isl_int_add(bmap->div[div][1],
1468 bmap->div[div][1], bmap->div[div][0]);
1469 if (!neg)
1470 return isl_bool_false;
1471 if (isl_seq_first_non_zero(constraint+pos+1,
1472 bmap->n_div-div-1) != -1)
1473 return isl_bool_false;
1474 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1475 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1476 return isl_bool_false;
1477 if (isl_seq_first_non_zero(constraint+pos+1,
1478 bmap->n_div-div-1) != -1)
1479 return isl_bool_false;
1480 } else
1481 return isl_bool_false;
1483 return isl_bool_true;
1486 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1487 isl_int *constraint, unsigned div)
1489 return isl_basic_map_is_div_constraint(bset, constraint, div);
1493 /* If the only constraints a div d=floor(f/m)
1494 * appears in are its two defining constraints
1496 * f - m d >=0
1497 * -(f - (m - 1)) + m d >= 0
1499 * then it can safely be removed.
1501 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1503 int i;
1504 unsigned pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1506 for (i = 0; i < bmap->n_eq; ++i)
1507 if (!isl_int_is_zero(bmap->eq[i][pos]))
1508 return isl_bool_false;
1510 for (i = 0; i < bmap->n_ineq; ++i) {
1511 isl_bool red;
1513 if (isl_int_is_zero(bmap->ineq[i][pos]))
1514 continue;
1515 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1516 if (red < 0 || !red)
1517 return red;
1520 for (i = 0; i < bmap->n_div; ++i) {
1521 if (isl_int_is_zero(bmap->div[i][0]))
1522 continue;
1523 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1524 return isl_bool_false;
1527 return isl_bool_true;
1531 * Remove divs that don't occur in any of the constraints or other divs.
1532 * These can arise when dropping constraints from a basic map or
1533 * when the divs of a basic map have been temporarily aligned
1534 * with the divs of another basic map.
1536 static __isl_give isl_basic_map *remove_redundant_divs(
1537 __isl_take isl_basic_map *bmap)
1539 int i;
1540 isl_size v_div;
1542 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1543 if (v_div < 0)
1544 return isl_basic_map_free(bmap);
1546 for (i = bmap->n_div-1; i >= 0; --i) {
1547 isl_bool redundant;
1549 redundant = div_is_redundant(bmap, i);
1550 if (redundant < 0)
1551 return isl_basic_map_free(bmap);
1552 if (!redundant)
1553 continue;
1554 bmap = isl_basic_map_drop_constraints_involving(bmap,
1555 v_div + i, 1);
1556 bmap = isl_basic_map_drop_div(bmap, i);
1558 return bmap;
1561 /* Mark "bmap" as final, without checking for obviously redundant
1562 * integer divisions. This function should be used when "bmap"
1563 * is known not to involve any such integer divisions.
1565 __isl_give isl_basic_map *isl_basic_map_mark_final(
1566 __isl_take isl_basic_map *bmap)
1568 if (!bmap)
1569 return NULL;
1570 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1571 return bmap;
1574 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1576 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1578 bmap = remove_redundant_divs(bmap);
1579 bmap = isl_basic_map_mark_final(bmap);
1580 return bmap;
1583 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1585 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1588 /* Remove definition of any div that is defined in terms of the given variable.
1589 * The div itself is not removed. Functions such as
1590 * eliminate_divs_ineq depend on the other divs remaining in place.
1592 static __isl_give isl_basic_map *remove_dependent_vars(
1593 __isl_take isl_basic_map *bmap, int pos)
1595 int i;
1597 if (!bmap)
1598 return NULL;
1600 for (i = 0; i < bmap->n_div; ++i) {
1601 if (isl_int_is_zero(bmap->div[i][0]))
1602 continue;
1603 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1604 continue;
1605 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1606 if (!bmap)
1607 return NULL;
1609 return bmap;
1612 /* Eliminate the specified variables from the constraints using
1613 * Fourier-Motzkin. The variables themselves are not removed.
1615 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1616 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1618 int d;
1619 int i, j, k;
1620 isl_size total;
1621 int need_gauss = 0;
1623 if (n == 0)
1624 return bmap;
1625 total = isl_basic_map_dim(bmap, isl_dim_all);
1626 if (total < 0)
1627 return isl_basic_map_free(bmap);
1629 bmap = isl_basic_map_cow(bmap);
1630 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1631 bmap = remove_dependent_vars(bmap, d);
1632 if (!bmap)
1633 return NULL;
1635 for (d = pos + n - 1;
1636 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1637 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1638 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1639 int n_lower, n_upper;
1640 if (!bmap)
1641 return NULL;
1642 for (i = 0; i < bmap->n_eq; ++i) {
1643 if (isl_int_is_zero(bmap->eq[i][1+d]))
1644 continue;
1645 bmap = eliminate_var_using_equality(bmap, d,
1646 bmap->eq[i], 0, NULL);
1647 if (isl_basic_map_drop_equality(bmap, i) < 0)
1648 return isl_basic_map_free(bmap);
1649 need_gauss = 1;
1650 break;
1652 if (i < bmap->n_eq)
1653 continue;
1654 n_lower = 0;
1655 n_upper = 0;
1656 for (i = 0; i < bmap->n_ineq; ++i) {
1657 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1658 n_lower++;
1659 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1660 n_upper++;
1662 bmap = isl_basic_map_extend_constraints(bmap,
1663 0, n_lower * n_upper);
1664 if (!bmap)
1665 goto error;
1666 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1667 int last;
1668 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1669 continue;
1670 last = -1;
1671 for (j = 0; j < i; ++j) {
1672 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1673 continue;
1674 last = j;
1675 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1676 isl_int_sgn(bmap->ineq[j][1+d]))
1677 continue;
1678 k = isl_basic_map_alloc_inequality(bmap);
1679 if (k < 0)
1680 goto error;
1681 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1682 1+total);
1683 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1684 1+d, 1+total, NULL);
1686 isl_basic_map_drop_inequality(bmap, i);
1687 i = last + 1;
1689 if (n_lower > 0 && n_upper > 0) {
1690 bmap = isl_basic_map_normalize_constraints(bmap);
1691 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1692 NULL, 0);
1693 bmap = isl_basic_map_gauss(bmap, NULL);
1694 bmap = isl_basic_map_remove_redundancies(bmap);
1695 need_gauss = 0;
1696 if (!bmap)
1697 goto error;
1698 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1699 break;
1702 if (need_gauss)
1703 bmap = isl_basic_map_gauss(bmap, NULL);
1704 return bmap;
1705 error:
1706 isl_basic_map_free(bmap);
1707 return NULL;
1710 struct isl_basic_set *isl_basic_set_eliminate_vars(
1711 struct isl_basic_set *bset, unsigned pos, unsigned n)
1713 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1714 pos, n));
1717 /* Eliminate the specified n dimensions starting at first from the
1718 * constraints, without removing the dimensions from the space.
1719 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1720 * Otherwise, they are projected out and the original space is restored.
1722 __isl_give isl_basic_map *isl_basic_map_eliminate(
1723 __isl_take isl_basic_map *bmap,
1724 enum isl_dim_type type, unsigned first, unsigned n)
1726 isl_space *space;
1728 if (!bmap)
1729 return NULL;
1730 if (n == 0)
1731 return bmap;
1733 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1734 return isl_basic_map_free(bmap);
1736 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1737 first += isl_basic_map_offset(bmap, type) - 1;
1738 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1739 return isl_basic_map_finalize(bmap);
1742 space = isl_basic_map_get_space(bmap);
1743 bmap = isl_basic_map_project_out(bmap, type, first, n);
1744 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1745 bmap = isl_basic_map_reset_space(bmap, space);
1746 return bmap;
1749 __isl_give isl_basic_set *isl_basic_set_eliminate(
1750 __isl_take isl_basic_set *bset,
1751 enum isl_dim_type type, unsigned first, unsigned n)
1753 return isl_basic_map_eliminate(bset, type, first, n);
1756 /* Remove all constraints from "bmap" that reference any unknown local
1757 * variables (directly or indirectly).
1759 * Dropping all constraints on a local variable will make it redundant,
1760 * so it will get removed implicitly by
1761 * isl_basic_map_drop_constraints_involving_dims. Some other local
1762 * variables may also end up becoming redundant if they only appear
1763 * in constraints together with the unknown local variable.
1764 * Therefore, start over after calling
1765 * isl_basic_map_drop_constraints_involving_dims.
1767 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1768 __isl_take isl_basic_map *bmap)
1770 isl_bool known;
1771 isl_size n_div;
1772 int i, o_div;
1774 known = isl_basic_map_divs_known(bmap);
1775 if (known < 0)
1776 return isl_basic_map_free(bmap);
1777 if (known)
1778 return bmap;
1780 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1781 if (n_div < 0)
1782 return isl_basic_map_free(bmap);
1783 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1785 for (i = 0; i < n_div; ++i) {
1786 known = isl_basic_map_div_is_known(bmap, i);
1787 if (known < 0)
1788 return isl_basic_map_free(bmap);
1789 if (known)
1790 continue;
1791 bmap = remove_dependent_vars(bmap, o_div + i);
1792 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1793 isl_dim_div, i, 1);
1794 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1795 if (n_div < 0)
1796 return isl_basic_map_free(bmap);
1797 i = -1;
1800 return bmap;
1803 /* Remove all constraints from "map" that reference any unknown local
1804 * variables (directly or indirectly).
1806 * Since constraints may get dropped from the basic maps,
1807 * they may no longer be disjoint from each other.
1809 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1810 __isl_take isl_map *map)
1812 int i;
1813 isl_bool known;
1815 known = isl_map_divs_known(map);
1816 if (known < 0)
1817 return isl_map_free(map);
1818 if (known)
1819 return map;
1821 map = isl_map_cow(map);
1822 if (!map)
1823 return NULL;
1825 for (i = 0; i < map->n; ++i) {
1826 map->p[i] =
1827 isl_basic_map_drop_constraint_involving_unknown_divs(
1828 map->p[i]);
1829 if (!map->p[i])
1830 return isl_map_free(map);
1833 if (map->n > 1)
1834 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1836 return map;
1839 /* Don't assume equalities are in order, because align_divs
1840 * may have changed the order of the divs.
1842 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1844 int d, i;
1845 unsigned total;
1847 total = isl_space_dim(bmap->dim, isl_dim_all);
1848 for (d = 0; d < total; ++d)
1849 elim[d] = -1;
1850 for (i = 0; i < bmap->n_eq; ++i) {
1851 for (d = total - 1; d >= 0; --d) {
1852 if (isl_int_is_zero(bmap->eq[i][1+d]))
1853 continue;
1854 elim[d] = i;
1855 break;
1860 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1861 int *elim)
1863 compute_elimination_index(bset_to_bmap(bset), elim);
1866 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1867 __isl_keep isl_basic_map *bmap, int *elim)
1869 int d;
1870 int copied = 0;
1871 unsigned total;
1873 total = isl_space_dim(bmap->dim, isl_dim_all);
1874 for (d = total - 1; d >= 0; --d) {
1875 if (isl_int_is_zero(src[1+d]))
1876 continue;
1877 if (elim[d] == -1)
1878 continue;
1879 if (!copied) {
1880 isl_seq_cpy(dst, src, 1 + total);
1881 copied = 1;
1883 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1885 return copied;
1888 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1889 __isl_keep isl_basic_set *bset, int *elim)
1891 return reduced_using_equalities(dst, src,
1892 bset_to_bmap(bset), elim);
1895 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1896 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1898 int i;
1899 int *elim;
1900 isl_size dim;
1902 if (!bset || !context)
1903 goto error;
1905 if (context->n_eq == 0) {
1906 isl_basic_set_free(context);
1907 return bset;
1910 bset = isl_basic_set_cow(bset);
1911 dim = isl_basic_set_dim(bset, isl_dim_set);
1912 if (dim < 0)
1913 goto error;
1915 elim = isl_alloc_array(bset->ctx, int, dim);
1916 if (!elim)
1917 goto error;
1918 set_compute_elimination_index(context, elim);
1919 for (i = 0; i < bset->n_eq; ++i)
1920 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1921 context, elim);
1922 for (i = 0; i < bset->n_ineq; ++i)
1923 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1924 context, elim);
1925 isl_basic_set_free(context);
1926 free(elim);
1927 bset = isl_basic_set_simplify(bset);
1928 bset = isl_basic_set_finalize(bset);
1929 return bset;
1930 error:
1931 isl_basic_set_free(bset);
1932 isl_basic_set_free(context);
1933 return NULL;
1936 /* For each inequality in "ineq" that is a shifted (more relaxed)
1937 * copy of an inequality in "context", mark the corresponding entry
1938 * in "row" with -1.
1939 * If an inequality only has a non-negative constant term, then
1940 * mark it as well.
1942 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1943 __isl_keep isl_basic_set *context, int *row)
1945 struct isl_constraint_index ci;
1946 isl_size n_ineq, cols;
1947 unsigned total;
1948 int k;
1950 if (!ineq || !context)
1951 return isl_stat_error;
1952 if (context->n_ineq == 0)
1953 return isl_stat_ok;
1954 if (setup_constraint_index(&ci, context) < 0)
1955 return isl_stat_error;
1957 n_ineq = isl_mat_rows(ineq);
1958 cols = isl_mat_cols(ineq);
1959 if (n_ineq < 0 || cols < 0)
1960 return isl_stat_error;
1961 total = cols - 1;
1962 for (k = 0; k < n_ineq; ++k) {
1963 int l;
1964 isl_bool redundant;
1966 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1967 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1968 row[k] = -1;
1969 continue;
1971 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1972 if (redundant < 0)
1973 goto error;
1974 if (!redundant)
1975 continue;
1976 row[k] = -1;
1978 constraint_index_free(&ci);
1979 return isl_stat_ok;
1980 error:
1981 constraint_index_free(&ci);
1982 return isl_stat_error;
1985 static __isl_give isl_basic_set *remove_shifted_constraints(
1986 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1988 struct isl_constraint_index ci;
1989 int k;
1991 if (!bset || !context)
1992 return bset;
1994 if (context->n_ineq == 0)
1995 return bset;
1996 if (setup_constraint_index(&ci, context) < 0)
1997 return bset;
1999 for (k = 0; k < bset->n_ineq; ++k) {
2000 isl_bool redundant;
2002 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2003 if (redundant < 0)
2004 goto error;
2005 if (!redundant)
2006 continue;
2007 bset = isl_basic_set_cow(bset);
2008 if (!bset)
2009 goto error;
2010 isl_basic_set_drop_inequality(bset, k);
2011 --k;
2013 constraint_index_free(&ci);
2014 return bset;
2015 error:
2016 constraint_index_free(&ci);
2017 return bset;
2020 /* Remove constraints from "bmap" that are identical to constraints
2021 * in "context" or that are more relaxed (greater constant term).
2023 * We perform the test for shifted copies on the pure constraints
2024 * in remove_shifted_constraints.
2026 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2027 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2029 isl_basic_set *bset, *bset_context;
2031 if (!bmap || !context)
2032 goto error;
2034 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2035 isl_basic_map_free(context);
2036 return bmap;
2039 context = isl_basic_map_align_divs(context, bmap);
2040 bmap = isl_basic_map_align_divs(bmap, context);
2042 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2043 bset_context = isl_basic_map_underlying_set(context);
2044 bset = remove_shifted_constraints(bset, bset_context);
2045 isl_basic_set_free(bset_context);
2047 bmap = isl_basic_map_overlying_set(bset, bmap);
2049 return bmap;
2050 error:
2051 isl_basic_map_free(bmap);
2052 isl_basic_map_free(context);
2053 return NULL;
2056 /* Does the (linear part of a) constraint "c" involve any of the "len"
2057 * "relevant" dimensions?
2059 static int is_related(isl_int *c, int len, int *relevant)
2061 int i;
2063 for (i = 0; i < len; ++i) {
2064 if (!relevant[i])
2065 continue;
2066 if (!isl_int_is_zero(c[i]))
2067 return 1;
2070 return 0;
2073 /* Drop constraints from "bmap" that do not involve any of
2074 * the dimensions marked "relevant".
2076 static __isl_give isl_basic_map *drop_unrelated_constraints(
2077 __isl_take isl_basic_map *bmap, int *relevant)
2079 int i;
2080 isl_size dim;
2082 dim = isl_basic_map_dim(bmap, isl_dim_all);
2083 if (dim < 0)
2084 return isl_basic_map_free(bmap);
2085 for (i = 0; i < dim; ++i)
2086 if (!relevant[i])
2087 break;
2088 if (i >= dim)
2089 return bmap;
2091 for (i = bmap->n_eq - 1; i >= 0; --i)
2092 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2093 bmap = isl_basic_map_cow(bmap);
2094 if (isl_basic_map_drop_equality(bmap, i) < 0)
2095 return isl_basic_map_free(bmap);
2098 for (i = bmap->n_ineq - 1; i >= 0; --i)
2099 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2100 bmap = isl_basic_map_cow(bmap);
2101 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2102 return isl_basic_map_free(bmap);
2105 return bmap;
2108 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2110 * In particular, for any variable involved in the constraint,
2111 * find the actual group id from before and replace the group
2112 * of the corresponding variable by the minimal group of all
2113 * the variables involved in the constraint considered so far
2114 * (if this minimum is smaller) or replace the minimum by this group
2115 * (if the minimum is larger).
2117 * At the end, all the variables in "c" will (indirectly) point
2118 * to the minimal of the groups that they referred to originally.
2120 static void update_groups(int dim, int *group, isl_int *c)
2122 int j;
2123 int min = dim;
2125 for (j = 0; j < dim; ++j) {
2126 if (isl_int_is_zero(c[j]))
2127 continue;
2128 while (group[j] >= 0 && group[group[j]] != group[j])
2129 group[j] = group[group[j]];
2130 if (group[j] == min)
2131 continue;
2132 if (group[j] < min) {
2133 if (min >= 0 && min < dim)
2134 group[min] = group[j];
2135 min = group[j];
2136 } else
2137 group[group[j]] = min;
2141 /* Allocate an array of groups of variables, one for each variable
2142 * in "context", initialized to zero.
2144 static int *alloc_groups(__isl_keep isl_basic_set *context)
2146 isl_ctx *ctx;
2147 isl_size dim;
2149 dim = isl_basic_set_dim(context, isl_dim_set);
2150 if (dim < 0)
2151 return NULL;
2152 ctx = isl_basic_set_get_ctx(context);
2153 return isl_calloc_array(ctx, int, dim);
2156 /* Drop constraints from "bmap" that only involve variables that are
2157 * not related to any of the variables marked with a "-1" in "group".
2159 * We construct groups of variables that collect variables that
2160 * (indirectly) appear in some common constraint of "bmap".
2161 * Each group is identified by the first variable in the group,
2162 * except for the special group of variables that was already identified
2163 * in the input as -1 (or are related to those variables).
2164 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2165 * otherwise the group of i is the group of group[i].
2167 * We first initialize groups for the remaining variables.
2168 * Then we iterate over the constraints of "bmap" and update the
2169 * group of the variables in the constraint by the smallest group.
2170 * Finally, we resolve indirect references to groups by running over
2171 * the variables.
2173 * After computing the groups, we drop constraints that do not involve
2174 * any variables in the -1 group.
2176 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2177 __isl_take isl_basic_map *bmap, __isl_take int *group)
2179 isl_size dim;
2180 int i;
2181 int last;
2183 dim = isl_basic_map_dim(bmap, isl_dim_all);
2184 if (dim < 0)
2185 return isl_basic_map_free(bmap);
2187 last = -1;
2188 for (i = 0; i < dim; ++i)
2189 if (group[i] >= 0)
2190 last = group[i] = i;
2191 if (last < 0) {
2192 free(group);
2193 return bmap;
2196 for (i = 0; i < bmap->n_eq; ++i)
2197 update_groups(dim, group, bmap->eq[i] + 1);
2198 for (i = 0; i < bmap->n_ineq; ++i)
2199 update_groups(dim, group, bmap->ineq[i] + 1);
2201 for (i = 0; i < dim; ++i)
2202 if (group[i] >= 0)
2203 group[i] = group[group[i]];
2205 for (i = 0; i < dim; ++i)
2206 group[i] = group[i] == -1;
2208 bmap = drop_unrelated_constraints(bmap, group);
2210 free(group);
2211 return bmap;
2214 /* Drop constraints from "context" that are irrelevant for computing
2215 * the gist of "bset".
2217 * In particular, drop constraints in variables that are not related
2218 * to any of the variables involved in the constraints of "bset"
2219 * in the sense that there is no sequence of constraints that connects them.
2221 * We first mark all variables that appear in "bset" as belonging
2222 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2224 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2225 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2227 int *group;
2228 isl_size dim;
2229 int i, j;
2231 dim = isl_basic_set_dim(bset, isl_dim_set);
2232 if (!context || dim < 0)
2233 return isl_basic_set_free(context);
2235 group = alloc_groups(context);
2237 if (!group)
2238 return isl_basic_set_free(context);
2240 for (i = 0; i < dim; ++i) {
2241 for (j = 0; j < bset->n_eq; ++j)
2242 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2243 break;
2244 if (j < bset->n_eq) {
2245 group[i] = -1;
2246 continue;
2248 for (j = 0; j < bset->n_ineq; ++j)
2249 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2250 break;
2251 if (j < bset->n_ineq)
2252 group[i] = -1;
2255 return isl_basic_map_drop_unrelated_constraints(context, group);
2258 /* Drop constraints from "context" that are irrelevant for computing
2259 * the gist of the inequalities "ineq".
2260 * Inequalities in "ineq" for which the corresponding element of row
2261 * is set to -1 have already been marked for removal and should be ignored.
2263 * In particular, drop constraints in variables that are not related
2264 * to any of the variables involved in "ineq"
2265 * in the sense that there is no sequence of constraints that connects them.
2267 * We first mark all variables that appear in "bset" as belonging
2268 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2270 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2271 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2273 int *group;
2274 isl_size dim;
2275 int i, j;
2276 isl_size n;
2278 dim = isl_basic_set_dim(context, isl_dim_set);
2279 n = isl_mat_rows(ineq);
2280 if (dim < 0 || n < 0)
2281 return isl_basic_set_free(context);
2283 group = alloc_groups(context);
2285 if (!group)
2286 return isl_basic_set_free(context);
2288 for (i = 0; i < dim; ++i) {
2289 for (j = 0; j < n; ++j) {
2290 if (row[j] < 0)
2291 continue;
2292 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2293 break;
2295 if (j < n)
2296 group[i] = -1;
2299 return isl_basic_map_drop_unrelated_constraints(context, group);
2302 /* Do all "n" entries of "row" contain a negative value?
2304 static int all_neg(int *row, int n)
2306 int i;
2308 for (i = 0; i < n; ++i)
2309 if (row[i] >= 0)
2310 return 0;
2312 return 1;
2315 /* Update the inequalities in "bset" based on the information in "row"
2316 * and "tab".
2318 * In particular, the array "row" contains either -1, meaning that
2319 * the corresponding inequality of "bset" is redundant, or the index
2320 * of an inequality in "tab".
2322 * If the row entry is -1, then drop the inequality.
2323 * Otherwise, if the constraint is marked redundant in the tableau,
2324 * then drop the inequality. Similarly, if it is marked as an equality
2325 * in the tableau, then turn the inequality into an equality and
2326 * perform Gaussian elimination.
2328 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2329 __isl_keep int *row, struct isl_tab *tab)
2331 int i;
2332 unsigned n_ineq;
2333 unsigned n_eq;
2334 int found_equality = 0;
2336 if (!bset)
2337 return NULL;
2338 if (tab && tab->empty)
2339 return isl_basic_set_set_to_empty(bset);
2341 n_ineq = bset->n_ineq;
2342 for (i = n_ineq - 1; i >= 0; --i) {
2343 if (row[i] < 0) {
2344 if (isl_basic_set_drop_inequality(bset, i) < 0)
2345 return isl_basic_set_free(bset);
2346 continue;
2348 if (!tab)
2349 continue;
2350 n_eq = tab->n_eq;
2351 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2352 isl_basic_map_inequality_to_equality(bset, i);
2353 found_equality = 1;
2354 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2355 if (isl_basic_set_drop_inequality(bset, i) < 0)
2356 return isl_basic_set_free(bset);
2360 if (found_equality)
2361 bset = isl_basic_set_gauss(bset, NULL);
2362 bset = isl_basic_set_finalize(bset);
2363 return bset;
2366 /* Update the inequalities in "bset" based on the information in "row"
2367 * and "tab" and free all arguments (other than "bset").
2369 static __isl_give isl_basic_set *update_ineq_free(
2370 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2371 __isl_take isl_basic_set *context, __isl_take int *row,
2372 struct isl_tab *tab)
2374 isl_mat_free(ineq);
2375 isl_basic_set_free(context);
2377 bset = update_ineq(bset, row, tab);
2379 free(row);
2380 isl_tab_free(tab);
2381 return bset;
2384 /* Remove all information from bset that is redundant in the context
2385 * of context.
2386 * "ineq" contains the (possibly transformed) inequalities of "bset",
2387 * in the same order.
2388 * The (explicit) equalities of "bset" are assumed to have been taken
2389 * into account by the transformation such that only the inequalities
2390 * are relevant.
2391 * "context" is assumed not to be empty.
2393 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2394 * A value of -1 means that the inequality is obviously redundant and may
2395 * not even appear in "tab".
2397 * We first mark the inequalities of "bset"
2398 * that are obviously redundant with respect to some inequality in "context".
2399 * Then we remove those constraints from "context" that have become
2400 * irrelevant for computing the gist of "bset".
2401 * Note that this removal of constraints cannot be replaced by
2402 * a factorization because factors in "bset" may still be connected
2403 * to each other through constraints in "context".
2405 * If there are any inequalities left, we construct a tableau for
2406 * the context and then add the inequalities of "bset".
2407 * Before adding these inequalities, we freeze all constraints such that
2408 * they won't be considered redundant in terms of the constraints of "bset".
2409 * Then we detect all redundant constraints (among the
2410 * constraints that weren't frozen), first by checking for redundancy in the
2411 * the tableau and then by checking if replacing a constraint by its negation
2412 * would lead to an empty set. This last step is fairly expensive
2413 * and could be optimized by more reuse of the tableau.
2414 * Finally, we update bset according to the results.
2416 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2417 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2419 int i, r;
2420 int *row = NULL;
2421 isl_ctx *ctx;
2422 isl_basic_set *combined = NULL;
2423 struct isl_tab *tab = NULL;
2424 unsigned n_eq, context_ineq;
2426 if (!bset || !ineq || !context)
2427 goto error;
2429 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2430 isl_basic_set_free(context);
2431 isl_mat_free(ineq);
2432 return bset;
2435 ctx = isl_basic_set_get_ctx(context);
2436 row = isl_calloc_array(ctx, int, bset->n_ineq);
2437 if (!row)
2438 goto error;
2440 if (mark_shifted_constraints(ineq, context, row) < 0)
2441 goto error;
2442 if (all_neg(row, bset->n_ineq))
2443 return update_ineq_free(bset, ineq, context, row, NULL);
2445 context = drop_irrelevant_constraints_marked(context, ineq, row);
2446 if (!context)
2447 goto error;
2448 if (isl_basic_set_plain_is_universe(context))
2449 return update_ineq_free(bset, ineq, context, row, NULL);
2451 n_eq = context->n_eq;
2452 context_ineq = context->n_ineq;
2453 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2454 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2455 tab = isl_tab_from_basic_set(combined, 0);
2456 for (i = 0; i < context_ineq; ++i)
2457 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2458 goto error;
2459 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2460 goto error;
2461 r = context_ineq;
2462 for (i = 0; i < bset->n_ineq; ++i) {
2463 if (row[i] < 0)
2464 continue;
2465 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2466 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2467 goto error;
2468 row[i] = r++;
2470 if (isl_tab_detect_implicit_equalities(tab) < 0)
2471 goto error;
2472 if (isl_tab_detect_redundant(tab) < 0)
2473 goto error;
2474 for (i = bset->n_ineq - 1; i >= 0; --i) {
2475 isl_basic_set *test;
2476 int is_empty;
2478 if (row[i] < 0)
2479 continue;
2480 r = row[i];
2481 if (tab->con[n_eq + r].is_redundant)
2482 continue;
2483 test = isl_basic_set_dup(combined);
2484 test = isl_inequality_negate(test, r);
2485 test = isl_basic_set_update_from_tab(test, tab);
2486 is_empty = isl_basic_set_is_empty(test);
2487 isl_basic_set_free(test);
2488 if (is_empty < 0)
2489 goto error;
2490 if (is_empty)
2491 tab->con[n_eq + r].is_redundant = 1;
2493 bset = update_ineq_free(bset, ineq, context, row, tab);
2494 if (bset) {
2495 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2496 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2499 isl_basic_set_free(combined);
2500 return bset;
2501 error:
2502 free(row);
2503 isl_mat_free(ineq);
2504 isl_tab_free(tab);
2505 isl_basic_set_free(combined);
2506 isl_basic_set_free(context);
2507 isl_basic_set_free(bset);
2508 return NULL;
2511 /* Extract the inequalities of "bset" as an isl_mat.
2513 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2515 isl_size total;
2516 isl_ctx *ctx;
2517 isl_mat *ineq;
2519 total = isl_basic_set_dim(bset, isl_dim_all);
2520 if (total < 0)
2521 return NULL;
2523 ctx = isl_basic_set_get_ctx(bset);
2524 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2525 0, 1 + total);
2527 return ineq;
2530 /* Remove all information from "bset" that is redundant in the context
2531 * of "context", for the case where both "bset" and "context" are
2532 * full-dimensional.
2534 static __isl_give isl_basic_set *uset_gist_uncompressed(
2535 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2537 isl_mat *ineq;
2539 ineq = extract_ineq(bset);
2540 return uset_gist_full(bset, ineq, context);
2543 /* Remove all information from "bset" that is redundant in the context
2544 * of "context", for the case where the combined equalities of
2545 * "bset" and "context" allow for a compression that can be obtained
2546 * by preapplication of "T".
2548 * "bset" itself is not transformed by "T". Instead, the inequalities
2549 * are extracted from "bset" and those are transformed by "T".
2550 * uset_gist_full then determines which of the transformed inequalities
2551 * are redundant with respect to the transformed "context" and removes
2552 * the corresponding inequalities from "bset".
2554 * After preapplying "T" to the inequalities, any common factor is
2555 * removed from the coefficients. If this results in a tightening
2556 * of the constant term, then the same tightening is applied to
2557 * the corresponding untransformed inequality in "bset".
2558 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2560 * g f'(x) + r >= 0
2562 * with 0 <= r < g, then it is equivalent to
2564 * f'(x) >= 0
2566 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2567 * subspace compressed by T since the latter would be transformed to
2569 * g f'(x) >= 0
2571 static __isl_give isl_basic_set *uset_gist_compressed(
2572 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2573 __isl_take isl_mat *T)
2575 isl_ctx *ctx;
2576 isl_mat *ineq;
2577 int i;
2578 isl_size n_row, n_col;
2579 isl_int rem;
2581 ineq = extract_ineq(bset);
2582 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2583 context = isl_basic_set_preimage(context, T);
2585 if (!ineq || !context)
2586 goto error;
2587 if (isl_basic_set_plain_is_empty(context)) {
2588 isl_mat_free(ineq);
2589 isl_basic_set_free(context);
2590 return isl_basic_set_set_to_empty(bset);
2593 ctx = isl_mat_get_ctx(ineq);
2594 n_row = isl_mat_rows(ineq);
2595 n_col = isl_mat_cols(ineq);
2596 if (n_row < 0 || n_col < 0)
2597 goto error;
2598 isl_int_init(rem);
2599 for (i = 0; i < n_row; ++i) {
2600 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2601 if (isl_int_is_zero(ctx->normalize_gcd))
2602 continue;
2603 if (isl_int_is_one(ctx->normalize_gcd))
2604 continue;
2605 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2606 ctx->normalize_gcd, n_col - 1);
2607 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2608 isl_int_fdiv_q(ineq->row[i][0],
2609 ineq->row[i][0], ctx->normalize_gcd);
2610 if (isl_int_is_zero(rem))
2611 continue;
2612 bset = isl_basic_set_cow(bset);
2613 if (!bset)
2614 break;
2615 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2617 isl_int_clear(rem);
2619 return uset_gist_full(bset, ineq, context);
2620 error:
2621 isl_mat_free(ineq);
2622 isl_basic_set_free(context);
2623 isl_basic_set_free(bset);
2624 return NULL;
2627 /* Project "bset" onto the variables that are involved in "template".
2629 static __isl_give isl_basic_set *project_onto_involved(
2630 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2632 int i;
2633 isl_size n;
2635 n = isl_basic_set_dim(template, isl_dim_set);
2636 if (n < 0 || !template)
2637 return isl_basic_set_free(bset);
2639 for (i = 0; i < n; ++i) {
2640 isl_bool involved;
2642 involved = isl_basic_set_involves_dims(template,
2643 isl_dim_set, i, 1);
2644 if (involved < 0)
2645 return isl_basic_set_free(bset);
2646 if (involved)
2647 continue;
2648 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2651 return bset;
2654 /* Remove all information from bset that is redundant in the context
2655 * of context. In particular, equalities that are linear combinations
2656 * of those in context are removed. Then the inequalities that are
2657 * redundant in the context of the equalities and inequalities of
2658 * context are removed.
2660 * First of all, we drop those constraints from "context"
2661 * that are irrelevant for computing the gist of "bset".
2662 * Alternatively, we could factorize the intersection of "context" and "bset".
2664 * We first compute the intersection of the integer affine hulls
2665 * of "bset" and "context",
2666 * compute the gist inside this intersection and then reduce
2667 * the constraints with respect to the equalities of the context
2668 * that only involve variables already involved in the input.
2670 * If two constraints are mutually redundant, then uset_gist_full
2671 * will remove the second of those constraints. We therefore first
2672 * sort the constraints so that constraints not involving existentially
2673 * quantified variables are given precedence over those that do.
2674 * We have to perform this sorting before the variable compression,
2675 * because that may effect the order of the variables.
2677 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2678 __isl_take isl_basic_set *context)
2680 isl_mat *eq;
2681 isl_mat *T;
2682 isl_basic_set *aff;
2683 isl_basic_set *aff_context;
2684 isl_size total;
2686 total = isl_basic_set_dim(bset, isl_dim_all);
2687 if (total < 0 || !context)
2688 goto error;
2690 context = drop_irrelevant_constraints(context, bset);
2692 bset = isl_basic_set_detect_equalities(bset);
2693 aff = isl_basic_set_copy(bset);
2694 aff = isl_basic_set_plain_affine_hull(aff);
2695 context = isl_basic_set_detect_equalities(context);
2696 aff_context = isl_basic_set_copy(context);
2697 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2698 aff = isl_basic_set_intersect(aff, aff_context);
2699 if (!aff)
2700 goto error;
2701 if (isl_basic_set_plain_is_empty(aff)) {
2702 isl_basic_set_free(bset);
2703 isl_basic_set_free(context);
2704 return aff;
2706 bset = isl_basic_set_sort_constraints(bset);
2707 if (aff->n_eq == 0) {
2708 isl_basic_set_free(aff);
2709 return uset_gist_uncompressed(bset, context);
2711 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2712 eq = isl_mat_cow(eq);
2713 T = isl_mat_variable_compression(eq, NULL);
2714 isl_basic_set_free(aff);
2715 if (T && T->n_col == 0) {
2716 isl_mat_free(T);
2717 isl_basic_set_free(context);
2718 return isl_basic_set_set_to_empty(bset);
2721 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2722 aff_context = project_onto_involved(aff_context, bset);
2724 bset = uset_gist_compressed(bset, context, T);
2725 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2727 if (bset) {
2728 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2729 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2732 return bset;
2733 error:
2734 isl_basic_set_free(bset);
2735 isl_basic_set_free(context);
2736 return NULL;
2739 /* Return the number of equality constraints in "bmap" that involve
2740 * local variables. This function assumes that Gaussian elimination
2741 * has been applied to the equality constraints.
2743 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2745 int i;
2746 isl_size total, n_div;
2748 if (!bmap)
2749 return -1;
2751 if (bmap->n_eq == 0)
2752 return 0;
2754 total = isl_basic_map_dim(bmap, isl_dim_all);
2755 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2756 if (total < 0 || n_div < 0)
2757 return -1;
2758 total -= n_div;
2760 for (i = 0; i < bmap->n_eq; ++i)
2761 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2762 n_div) == -1)
2763 return i;
2765 return bmap->n_eq;
2768 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2769 * The constraints are assumed not to involve any local variables.
2771 static __isl_give isl_basic_map *basic_map_from_equalities(
2772 __isl_take isl_space *space, __isl_take isl_mat *eq)
2774 int i, k;
2775 isl_size total;
2776 isl_basic_map *bmap = NULL;
2778 total = isl_space_dim(space, isl_dim_all);
2779 if (total < 0 || !eq)
2780 goto error;
2782 if (1 + total != eq->n_col)
2783 isl_die(isl_space_get_ctx(space), isl_error_internal,
2784 "unexpected number of columns", goto error);
2786 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2787 0, eq->n_row, 0);
2788 for (i = 0; i < eq->n_row; ++i) {
2789 k = isl_basic_map_alloc_equality(bmap);
2790 if (k < 0)
2791 goto error;
2792 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2795 isl_space_free(space);
2796 isl_mat_free(eq);
2797 return bmap;
2798 error:
2799 isl_space_free(space);
2800 isl_mat_free(eq);
2801 isl_basic_map_free(bmap);
2802 return NULL;
2805 /* Construct and return a variable compression based on the equality
2806 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2807 * "n1" is the number of (initial) equality constraints in "bmap1"
2808 * that do involve local variables.
2809 * "n2" is the number of (initial) equality constraints in "bmap2"
2810 * that do involve local variables.
2811 * "total" is the total number of other variables.
2812 * This function assumes that Gaussian elimination
2813 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2814 * such that the equality constraints not involving local variables
2815 * are those that start at "n1" or "n2".
2817 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2818 * then simply compute the compression based on the equality constraints
2819 * in the other basic map.
2820 * Otherwise, combine the equality constraints from both into a new
2821 * basic map such that Gaussian elimination can be applied to this combination
2822 * and then construct a variable compression from the resulting
2823 * equality constraints.
2825 static __isl_give isl_mat *combined_variable_compression(
2826 __isl_keep isl_basic_map *bmap1, int n1,
2827 __isl_keep isl_basic_map *bmap2, int n2, int total)
2829 isl_ctx *ctx;
2830 isl_mat *E1, *E2, *V;
2831 isl_basic_map *bmap;
2833 ctx = isl_basic_map_get_ctx(bmap1);
2834 if (bmap1->n_eq == n1) {
2835 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2836 n2, bmap2->n_eq - n2, 0, 1 + total);
2837 return isl_mat_variable_compression(E2, NULL);
2839 if (bmap2->n_eq == n2) {
2840 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2841 n1, bmap1->n_eq - n1, 0, 1 + total);
2842 return isl_mat_variable_compression(E1, NULL);
2844 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2845 n1, bmap1->n_eq - n1, 0, 1 + total);
2846 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2847 n2, bmap2->n_eq - n2, 0, 1 + total);
2848 E1 = isl_mat_concat(E1, E2);
2849 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2850 bmap = isl_basic_map_gauss(bmap, NULL);
2851 if (!bmap)
2852 return NULL;
2853 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2854 V = isl_mat_variable_compression(E1, NULL);
2855 isl_basic_map_free(bmap);
2857 return V;
2860 /* Extract the stride constraints from "bmap", compressed
2861 * with respect to both the stride constraints in "context" and
2862 * the remaining equality constraints in both "bmap" and "context".
2863 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2864 * "context_n_eq" is the number of (initial) stride constraints in "context".
2866 * Let x be all variables in "bmap" (and "context") other than the local
2867 * variables. First compute a variable compression
2869 * x = V x'
2871 * based on the non-stride equality constraints in "bmap" and "context".
2872 * Consider the stride constraints of "context",
2874 * A(x) + B(y) = 0
2876 * with y the local variables and plug in the variable compression,
2877 * resulting in
2879 * A(V x') + B(y) = 0
2881 * Use these constraints to compute a parameter compression on x'
2883 * x' = T x''
2885 * Now consider the stride constraints of "bmap"
2887 * C(x) + D(y) = 0
2889 * and plug in x = V*T x''.
2890 * That is, return A = [C*V*T D].
2892 static __isl_give isl_mat *extract_compressed_stride_constraints(
2893 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2894 __isl_keep isl_basic_map *context, int context_n_eq)
2896 isl_size total, n_div;
2897 isl_ctx *ctx;
2898 isl_mat *A, *B, *T, *V;
2900 total = isl_basic_map_dim(context, isl_dim_all);
2901 n_div = isl_basic_map_dim(context, isl_dim_div);
2902 if (total < 0 || n_div < 0)
2903 return NULL;
2904 total -= n_div;
2906 ctx = isl_basic_map_get_ctx(bmap);
2908 V = combined_variable_compression(bmap, bmap_n_eq,
2909 context, context_n_eq, total);
2911 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2912 B = isl_mat_sub_alloc6(ctx, context->eq,
2913 0, context_n_eq, 1 + total, n_div);
2914 A = isl_mat_product(A, isl_mat_copy(V));
2915 T = isl_mat_parameter_compression_ext(A, B);
2916 T = isl_mat_product(V, T);
2918 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2919 if (n_div < 0)
2920 T = isl_mat_free(T);
2921 else
2922 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2924 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2925 0, bmap_n_eq, 0, 1 + total + n_div);
2926 A = isl_mat_product(A, T);
2928 return A;
2931 /* Remove the prime factors from *g that have an exponent that
2932 * is strictly smaller than the exponent in "c".
2933 * All exponents in *g are known to be smaller than or equal
2934 * to those in "c".
2936 * That is, if *g is equal to
2938 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2940 * and "c" is equal to
2942 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2944 * then update *g to
2946 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2947 * p_n^{e_n * (e_n = f_n)}
2949 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2950 * neither does the gcd of *g and c / *g.
2951 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2952 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2953 * Dividing *g by this gcd therefore strictly reduces the exponent
2954 * of the prime factors that need to be removed, while leaving the
2955 * other prime factors untouched.
2956 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2957 * removes all undesired factors, without removing any others.
2959 static void remove_incomplete_powers(isl_int *g, isl_int c)
2961 isl_int t;
2963 isl_int_init(t);
2964 for (;;) {
2965 isl_int_divexact(t, c, *g);
2966 isl_int_gcd(t, t, *g);
2967 if (isl_int_is_one(t))
2968 break;
2969 isl_int_divexact(*g, *g, t);
2971 isl_int_clear(t);
2974 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2975 * of the same stride constraints in a compressed space that exploits
2976 * all equalities in the context and the other equalities in "bmap".
2978 * If the stride constraints of "bmap" are of the form
2980 * C(x) + D(y) = 0
2982 * then A is of the form
2984 * B(x') + D(y) = 0
2986 * If any of these constraints involves only a single local variable y,
2987 * then the constraint appears as
2989 * f(x) + m y_i = 0
2991 * in "bmap" and as
2993 * h(x') + m y_i = 0
2995 * in "A".
2997 * Let g be the gcd of m and the coefficients of h.
2998 * Then, in particular, g is a divisor of the coefficients of h and
3000 * f(x) = h(x')
3002 * is known to be a multiple of g.
3003 * If some prime factor in m appears with the same exponent in g,
3004 * then it can be removed from m because f(x) is already known
3005 * to be a multiple of g and therefore in particular of this power
3006 * of the prime factors.
3007 * Prime factors that appear with a smaller exponent in g cannot
3008 * be removed from m.
3009 * Let g' be the divisor of g containing all prime factors that
3010 * appear with the same exponent in m and g, then
3012 * f(x) + m y_i = 0
3014 * can be replaced by
3016 * f(x) + m/g' y_i' = 0
3018 * Note that (if g' != 1) this changes the explicit representation
3019 * of y_i to that of y_i', so the integer division at position i
3020 * is marked unknown and later recomputed by a call to
3021 * isl_basic_map_gauss.
3023 static __isl_give isl_basic_map *reduce_stride_constraints(
3024 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3026 int i;
3027 isl_size total, n_div;
3028 int any = 0;
3029 isl_int gcd;
3031 total = isl_basic_map_dim(bmap, isl_dim_all);
3032 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3033 if (total < 0 || n_div < 0 || !A)
3034 return isl_basic_map_free(bmap);
3035 total -= n_div;
3037 isl_int_init(gcd);
3038 for (i = 0; i < n; ++i) {
3039 int div;
3041 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3042 if (div < 0)
3043 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3044 "equality constraints modified unexpectedly",
3045 goto error);
3046 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3047 n_div - div - 1) != -1)
3048 continue;
3049 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3050 goto error;
3051 if (isl_int_is_one(gcd))
3052 continue;
3053 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3054 if (isl_int_is_one(gcd))
3055 continue;
3056 isl_int_divexact(bmap->eq[i][1 + total + div],
3057 bmap->eq[i][1 + total + div], gcd);
3058 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3059 if (!bmap)
3060 goto error;
3061 any = 1;
3063 isl_int_clear(gcd);
3065 if (any)
3066 bmap = isl_basic_map_gauss(bmap, NULL);
3068 return bmap;
3069 error:
3070 isl_int_clear(gcd);
3071 isl_basic_map_free(bmap);
3072 return NULL;
3075 /* Simplify the stride constraints in "bmap" based on
3076 * the remaining equality constraints in "bmap" and all equality
3077 * constraints in "context".
3078 * Only do this if both "bmap" and "context" have stride constraints.
3080 * First extract a copy of the stride constraints in "bmap" in a compressed
3081 * space exploiting all the other equality constraints and then
3082 * use this compressed copy to simplify the original stride constraints.
3084 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3085 __isl_keep isl_basic_map *context)
3087 int bmap_n_eq, context_n_eq;
3088 isl_mat *A;
3090 if (!bmap || !context)
3091 return isl_basic_map_free(bmap);
3093 bmap_n_eq = n_div_eq(bmap);
3094 context_n_eq = n_div_eq(context);
3096 if (bmap_n_eq < 0 || context_n_eq < 0)
3097 return isl_basic_map_free(bmap);
3098 if (bmap_n_eq == 0 || context_n_eq == 0)
3099 return bmap;
3101 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3102 context, context_n_eq);
3103 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3105 isl_mat_free(A);
3107 return bmap;
3110 /* Return a basic map that has the same intersection with "context" as "bmap"
3111 * and that is as "simple" as possible.
3113 * The core computation is performed on the pure constraints.
3114 * When we add back the meaning of the integer divisions, we need
3115 * to (re)introduce the div constraints. If we happen to have
3116 * discovered that some of these integer divisions are equal to
3117 * some affine combination of other variables, then these div
3118 * constraints may end up getting simplified in terms of the equalities,
3119 * resulting in extra inequalities on the other variables that
3120 * may have been removed already or that may not even have been
3121 * part of the input. We try and remove those constraints of
3122 * this form that are most obviously redundant with respect to
3123 * the context. We also remove those div constraints that are
3124 * redundant with respect to the other constraints in the result.
3126 * The stride constraints among the equality constraints in "bmap" are
3127 * also simplified with respecting to the other equality constraints
3128 * in "bmap" and with respect to all equality constraints in "context".
3130 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3131 __isl_take isl_basic_map *context)
3133 isl_basic_set *bset, *eq;
3134 isl_basic_map *eq_bmap;
3135 isl_size total, n_div, n_div_bmap;
3136 unsigned extra, n_eq, n_ineq;
3138 if (!bmap || !context)
3139 goto error;
3141 if (isl_basic_map_plain_is_universe(bmap)) {
3142 isl_basic_map_free(context);
3143 return bmap;
3145 if (isl_basic_map_plain_is_empty(context)) {
3146 isl_space *space = isl_basic_map_get_space(bmap);
3147 isl_basic_map_free(bmap);
3148 isl_basic_map_free(context);
3149 return isl_basic_map_universe(space);
3151 if (isl_basic_map_plain_is_empty(bmap)) {
3152 isl_basic_map_free(context);
3153 return bmap;
3156 bmap = isl_basic_map_remove_redundancies(bmap);
3157 context = isl_basic_map_remove_redundancies(context);
3158 context = isl_basic_map_align_divs(context, bmap);
3160 n_div = isl_basic_map_dim(context, isl_dim_div);
3161 total = isl_basic_map_dim(bmap, isl_dim_all);
3162 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3163 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3164 goto error;
3165 extra = n_div - n_div_bmap;
3167 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3168 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3169 bset = uset_gist(bset,
3170 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3171 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3173 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3174 isl_basic_set_plain_is_empty(bset)) {
3175 isl_basic_map_free(context);
3176 return isl_basic_map_overlying_set(bset, bmap);
3179 n_eq = bset->n_eq;
3180 n_ineq = bset->n_ineq;
3181 eq = isl_basic_set_copy(bset);
3182 eq = isl_basic_set_cow(eq);
3183 eq = isl_basic_set_free_inequality(eq, n_ineq);
3184 bset = isl_basic_set_free_equality(bset, n_eq);
3186 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3187 eq_bmap = gist_strides(eq_bmap, context);
3188 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3189 bmap = isl_basic_map_overlying_set(bset, bmap);
3190 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3191 bmap = isl_basic_map_remove_redundancies(bmap);
3193 return bmap;
3194 error:
3195 isl_basic_map_free(bmap);
3196 isl_basic_map_free(context);
3197 return NULL;
3201 * Assumes context has no implicit divs.
3203 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3204 __isl_take isl_basic_map *context)
3206 int i;
3208 if (!map || !context)
3209 goto error;
3211 if (isl_basic_map_plain_is_empty(context)) {
3212 isl_space *space = isl_map_get_space(map);
3213 isl_map_free(map);
3214 isl_basic_map_free(context);
3215 return isl_map_universe(space);
3218 context = isl_basic_map_remove_redundancies(context);
3219 map = isl_map_cow(map);
3220 if (!map || !context)
3221 goto error;
3222 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3223 map = isl_map_compute_divs(map);
3224 if (!map)
3225 goto error;
3226 for (i = map->n - 1; i >= 0; --i) {
3227 map->p[i] = isl_basic_map_gist(map->p[i],
3228 isl_basic_map_copy(context));
3229 if (!map->p[i])
3230 goto error;
3231 if (isl_basic_map_plain_is_empty(map->p[i])) {
3232 isl_basic_map_free(map->p[i]);
3233 if (i != map->n - 1)
3234 map->p[i] = map->p[map->n - 1];
3235 map->n--;
3238 isl_basic_map_free(context);
3239 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3240 return map;
3241 error:
3242 isl_map_free(map);
3243 isl_basic_map_free(context);
3244 return NULL;
3247 /* Drop all inequalities from "bmap" that also appear in "context".
3248 * "context" is assumed to have only known local variables and
3249 * the initial local variables of "bmap" are assumed to be the same
3250 * as those of "context".
3251 * The constraints of both "bmap" and "context" are assumed
3252 * to have been sorted using isl_basic_map_sort_constraints.
3254 * Run through the inequality constraints of "bmap" and "context"
3255 * in sorted order.
3256 * If a constraint of "bmap" involves variables not in "context",
3257 * then it cannot appear in "context".
3258 * If a matching constraint is found, it is removed from "bmap".
3260 static __isl_give isl_basic_map *drop_inequalities(
3261 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3263 int i1, i2;
3264 isl_size total, bmap_total;
3265 unsigned extra;
3267 total = isl_basic_map_dim(context, isl_dim_all);
3268 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3269 if (total < 0 || bmap_total < 0)
3270 return isl_basic_map_free(bmap);
3272 extra = bmap_total - total;
3274 i1 = bmap->n_ineq - 1;
3275 i2 = context->n_ineq - 1;
3276 while (bmap && i1 >= 0 && i2 >= 0) {
3277 int cmp;
3279 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3280 extra) != -1) {
3281 --i1;
3282 continue;
3284 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3285 context->ineq[i2]);
3286 if (cmp < 0) {
3287 --i2;
3288 continue;
3290 if (cmp > 0) {
3291 --i1;
3292 continue;
3294 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3295 bmap = isl_basic_map_cow(bmap);
3296 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3297 bmap = isl_basic_map_free(bmap);
3299 --i1;
3300 --i2;
3303 return bmap;
3306 /* Drop all equalities from "bmap" that also appear in "context".
3307 * "context" is assumed to have only known local variables and
3308 * the initial local variables of "bmap" are assumed to be the same
3309 * as those of "context".
3311 * Run through the equality constraints of "bmap" and "context"
3312 * in sorted order.
3313 * If a constraint of "bmap" involves variables not in "context",
3314 * then it cannot appear in "context".
3315 * If a matching constraint is found, it is removed from "bmap".
3317 static __isl_give isl_basic_map *drop_equalities(
3318 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3320 int i1, i2;
3321 isl_size total, bmap_total;
3322 unsigned extra;
3324 total = isl_basic_map_dim(context, isl_dim_all);
3325 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3326 if (total < 0 || bmap_total < 0)
3327 return isl_basic_map_free(bmap);
3329 extra = bmap_total - total;
3331 i1 = bmap->n_eq - 1;
3332 i2 = context->n_eq - 1;
3334 while (bmap && i1 >= 0 && i2 >= 0) {
3335 int last1, last2;
3337 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3338 extra) != -1)
3339 break;
3340 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3341 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3342 if (last1 > last2) {
3343 --i2;
3344 continue;
3346 if (last1 < last2) {
3347 --i1;
3348 continue;
3350 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3351 bmap = isl_basic_map_cow(bmap);
3352 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3353 bmap = isl_basic_map_free(bmap);
3355 --i1;
3356 --i2;
3359 return bmap;
3362 /* Remove the constraints in "context" from "bmap".
3363 * "context" is assumed to have explicit representations
3364 * for all local variables.
3366 * First align the divs of "bmap" to those of "context" and
3367 * sort the constraints. Then drop all constraints from "bmap"
3368 * that appear in "context".
3370 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3371 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3373 isl_bool done, known;
3375 done = isl_basic_map_plain_is_universe(context);
3376 if (done == isl_bool_false)
3377 done = isl_basic_map_plain_is_universe(bmap);
3378 if (done == isl_bool_false)
3379 done = isl_basic_map_plain_is_empty(context);
3380 if (done == isl_bool_false)
3381 done = isl_basic_map_plain_is_empty(bmap);
3382 if (done < 0)
3383 goto error;
3384 if (done) {
3385 isl_basic_map_free(context);
3386 return bmap;
3388 known = isl_basic_map_divs_known(context);
3389 if (known < 0)
3390 goto error;
3391 if (!known)
3392 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3393 "context has unknown divs", goto error);
3395 bmap = isl_basic_map_align_divs(bmap, context);
3396 bmap = isl_basic_map_gauss(bmap, NULL);
3397 bmap = isl_basic_map_sort_constraints(bmap);
3398 context = isl_basic_map_sort_constraints(context);
3400 bmap = drop_inequalities(bmap, context);
3401 bmap = drop_equalities(bmap, context);
3403 isl_basic_map_free(context);
3404 bmap = isl_basic_map_finalize(bmap);
3405 return bmap;
3406 error:
3407 isl_basic_map_free(bmap);
3408 isl_basic_map_free(context);
3409 return NULL;
3412 /* Replace "map" by the disjunct at position "pos" and free "context".
3414 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3415 int pos, __isl_take isl_basic_map *context)
3417 isl_basic_map *bmap;
3419 bmap = isl_basic_map_copy(map->p[pos]);
3420 isl_map_free(map);
3421 isl_basic_map_free(context);
3422 return isl_map_from_basic_map(bmap);
3425 /* Remove the constraints in "context" from "map".
3426 * If any of the disjuncts in the result turns out to be the universe,
3427 * then return this universe.
3428 * "context" is assumed to have explicit representations
3429 * for all local variables.
3431 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3432 __isl_take isl_basic_map *context)
3434 int i;
3435 isl_bool univ, known;
3437 univ = isl_basic_map_plain_is_universe(context);
3438 if (univ < 0)
3439 goto error;
3440 if (univ) {
3441 isl_basic_map_free(context);
3442 return map;
3444 known = isl_basic_map_divs_known(context);
3445 if (known < 0)
3446 goto error;
3447 if (!known)
3448 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3449 "context has unknown divs", goto error);
3451 map = isl_map_cow(map);
3452 if (!map)
3453 goto error;
3454 for (i = 0; i < map->n; ++i) {
3455 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3456 isl_basic_map_copy(context));
3457 univ = isl_basic_map_plain_is_universe(map->p[i]);
3458 if (univ < 0)
3459 goto error;
3460 if (univ && map->n > 1)
3461 return replace_by_disjunct(map, i, context);
3464 isl_basic_map_free(context);
3465 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3466 if (map->n > 1)
3467 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3468 return map;
3469 error:
3470 isl_map_free(map);
3471 isl_basic_map_free(context);
3472 return NULL;
3475 /* Remove the constraints in "context" from "set".
3476 * If any of the disjuncts in the result turns out to be the universe,
3477 * then return this universe.
3478 * "context" is assumed to have explicit representations
3479 * for all local variables.
3481 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3482 __isl_take isl_basic_set *context)
3484 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3485 bset_to_bmap(context)));
3488 /* Remove the constraints in "context" from "map".
3489 * If any of the disjuncts in the result turns out to be the universe,
3490 * then return this universe.
3491 * "context" is assumed to consist of a single disjunct and
3492 * to have explicit representations for all local variables.
3494 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3495 __isl_take isl_map *context)
3497 isl_basic_map *hull;
3499 hull = isl_map_unshifted_simple_hull(context);
3500 return isl_map_plain_gist_basic_map(map, hull);
3503 /* Replace "map" by a universe map in the same space and free "drop".
3505 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3506 __isl_take isl_map *drop)
3508 isl_map *res;
3510 res = isl_map_universe(isl_map_get_space(map));
3511 isl_map_free(map);
3512 isl_map_free(drop);
3513 return res;
3516 /* Return a map that has the same intersection with "context" as "map"
3517 * and that is as "simple" as possible.
3519 * If "map" is already the universe, then we cannot make it any simpler.
3520 * Similarly, if "context" is the universe, then we cannot exploit it
3521 * to simplify "map"
3522 * If "map" and "context" are identical to each other, then we can
3523 * return the corresponding universe.
3525 * If either "map" or "context" consists of multiple disjuncts,
3526 * then check if "context" happens to be a subset of "map",
3527 * in which case all constraints can be removed.
3528 * In case of multiple disjuncts, the standard procedure
3529 * may not be able to detect that all constraints can be removed.
3531 * If none of these cases apply, we have to work a bit harder.
3532 * During this computation, we make use of a single disjunct context,
3533 * so if the original context consists of more than one disjunct
3534 * then we need to approximate the context by a single disjunct set.
3535 * Simply taking the simple hull may drop constraints that are
3536 * only implicitly available in each disjunct. We therefore also
3537 * look for constraints among those defining "map" that are valid
3538 * for the context. These can then be used to simplify away
3539 * the corresponding constraints in "map".
3541 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3542 __isl_take isl_map *context)
3544 int equal;
3545 int is_universe;
3546 isl_size n_disjunct_map, n_disjunct_context;
3547 isl_bool subset;
3548 isl_basic_map *hull;
3550 is_universe = isl_map_plain_is_universe(map);
3551 if (is_universe >= 0 && !is_universe)
3552 is_universe = isl_map_plain_is_universe(context);
3553 if (is_universe < 0)
3554 goto error;
3555 if (is_universe) {
3556 isl_map_free(context);
3557 return map;
3560 equal = isl_map_plain_is_equal(map, context);
3561 if (equal < 0)
3562 goto error;
3563 if (equal)
3564 return replace_by_universe(map, context);
3566 n_disjunct_map = isl_map_n_basic_map(map);
3567 n_disjunct_context = isl_map_n_basic_map(context);
3568 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3569 goto error;
3570 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3571 subset = isl_map_is_subset(context, map);
3572 if (subset < 0)
3573 goto error;
3574 if (subset)
3575 return replace_by_universe(map, context);
3578 context = isl_map_compute_divs(context);
3579 if (!context)
3580 goto error;
3581 if (n_disjunct_context == 1) {
3582 hull = isl_map_simple_hull(context);
3583 } else {
3584 isl_ctx *ctx;
3585 isl_map_list *list;
3587 ctx = isl_map_get_ctx(map);
3588 list = isl_map_list_alloc(ctx, 2);
3589 list = isl_map_list_add(list, isl_map_copy(context));
3590 list = isl_map_list_add(list, isl_map_copy(map));
3591 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3592 list);
3594 return isl_map_gist_basic_map(map, hull);
3595 error:
3596 isl_map_free(map);
3597 isl_map_free(context);
3598 return NULL;
3601 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3602 __isl_take isl_map *context)
3604 return isl_map_align_params_map_map_and(map, context, &map_gist);
3607 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3608 struct isl_basic_set *context)
3610 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3611 bset_to_bmap(context)));
3614 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3615 __isl_take isl_basic_set *context)
3617 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3618 bset_to_bmap(context)));
3621 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3622 __isl_take isl_basic_set *context)
3624 isl_space *space = isl_set_get_space(set);
3625 isl_basic_set *dom_context = isl_basic_set_universe(space);
3626 dom_context = isl_basic_set_intersect_params(dom_context, context);
3627 return isl_set_gist_basic_set(set, dom_context);
3630 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3631 __isl_take isl_set *context)
3633 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3636 /* Compute the gist of "bmap" with respect to the constraints "context"
3637 * on the domain.
3639 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3640 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3642 isl_space *space = isl_basic_map_get_space(bmap);
3643 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3645 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3646 return isl_basic_map_gist(bmap, bmap_context);
3649 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3650 __isl_take isl_set *context)
3652 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3653 map_context = isl_map_intersect_domain(map_context, context);
3654 return isl_map_gist(map, map_context);
3657 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3658 __isl_take isl_set *context)
3660 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3661 map_context = isl_map_intersect_range(map_context, context);
3662 return isl_map_gist(map, map_context);
3665 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3666 __isl_take isl_set *context)
3668 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3669 map_context = isl_map_intersect_params(map_context, context);
3670 return isl_map_gist(map, map_context);
3673 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3674 __isl_take isl_set *context)
3676 return isl_map_gist_params(set, context);
3679 /* Quick check to see if two basic maps are disjoint.
3680 * In particular, we reduce the equalities and inequalities of
3681 * one basic map in the context of the equalities of the other
3682 * basic map and check if we get a contradiction.
3684 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3685 __isl_keep isl_basic_map *bmap2)
3687 struct isl_vec *v = NULL;
3688 int *elim = NULL;
3689 isl_size total;
3690 int i;
3692 if (!bmap1 || !bmap2)
3693 return isl_bool_error;
3694 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3695 return isl_bool_error);
3696 if (bmap1->n_div || bmap2->n_div)
3697 return isl_bool_false;
3698 if (!bmap1->n_eq && !bmap2->n_eq)
3699 return isl_bool_false;
3701 total = isl_space_dim(bmap1->dim, isl_dim_all);
3702 if (total < 0)
3703 return isl_bool_error;
3704 if (total == 0)
3705 return isl_bool_false;
3706 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3707 if (!v)
3708 goto error;
3709 elim = isl_alloc_array(bmap1->ctx, int, total);
3710 if (!elim)
3711 goto error;
3712 compute_elimination_index(bmap1, elim);
3713 for (i = 0; i < bmap2->n_eq; ++i) {
3714 int reduced;
3715 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3716 bmap1, elim);
3717 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3718 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3719 goto disjoint;
3721 for (i = 0; i < bmap2->n_ineq; ++i) {
3722 int reduced;
3723 reduced = reduced_using_equalities(v->block.data,
3724 bmap2->ineq[i], bmap1, elim);
3725 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3726 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3727 goto disjoint;
3729 compute_elimination_index(bmap2, elim);
3730 for (i = 0; i < bmap1->n_ineq; ++i) {
3731 int reduced;
3732 reduced = reduced_using_equalities(v->block.data,
3733 bmap1->ineq[i], bmap2, elim);
3734 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3735 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3736 goto disjoint;
3738 isl_vec_free(v);
3739 free(elim);
3740 return isl_bool_false;
3741 disjoint:
3742 isl_vec_free(v);
3743 free(elim);
3744 return isl_bool_true;
3745 error:
3746 isl_vec_free(v);
3747 free(elim);
3748 return isl_bool_error;
3751 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3752 __isl_keep isl_basic_set *bset2)
3754 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3755 bset_to_bmap(bset2));
3758 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3760 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3761 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3762 __isl_keep isl_basic_map *bmap2))
3764 int i, j;
3766 if (!map1 || !map2)
3767 return isl_bool_error;
3769 for (i = 0; i < map1->n; ++i) {
3770 for (j = 0; j < map2->n; ++j) {
3771 isl_bool d = test(map1->p[i], map2->p[j]);
3772 if (d != isl_bool_true)
3773 return d;
3777 return isl_bool_true;
3780 /* Are "map1" and "map2" obviously disjoint, based on information
3781 * that can be derived without looking at the individual basic maps?
3783 * In particular, if one of them is empty or if they live in different spaces
3784 * (ignoring parameters), then they are clearly disjoint.
3786 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3787 __isl_keep isl_map *map2)
3789 isl_bool disjoint;
3790 isl_bool match;
3792 if (!map1 || !map2)
3793 return isl_bool_error;
3795 disjoint = isl_map_plain_is_empty(map1);
3796 if (disjoint < 0 || disjoint)
3797 return disjoint;
3799 disjoint = isl_map_plain_is_empty(map2);
3800 if (disjoint < 0 || disjoint)
3801 return disjoint;
3803 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3804 map2->dim, isl_dim_in);
3805 if (match < 0 || !match)
3806 return match < 0 ? isl_bool_error : isl_bool_true;
3808 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3809 map2->dim, isl_dim_out);
3810 if (match < 0 || !match)
3811 return match < 0 ? isl_bool_error : isl_bool_true;
3813 return isl_bool_false;
3816 /* Are "map1" and "map2" obviously disjoint?
3818 * If one of them is empty or if they live in different spaces (ignoring
3819 * parameters), then they are clearly disjoint.
3820 * This is checked by isl_map_plain_is_disjoint_global.
3822 * If they have different parameters, then we skip any further tests.
3824 * If they are obviously equal, but not obviously empty, then we will
3825 * not be able to detect if they are disjoint.
3827 * Otherwise we check if each basic map in "map1" is obviously disjoint
3828 * from each basic map in "map2".
3830 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3831 __isl_keep isl_map *map2)
3833 isl_bool disjoint;
3834 isl_bool intersect;
3835 isl_bool match;
3837 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3838 if (disjoint < 0 || disjoint)
3839 return disjoint;
3841 match = isl_map_has_equal_params(map1, map2);
3842 if (match < 0 || !match)
3843 return match < 0 ? isl_bool_error : isl_bool_false;
3845 intersect = isl_map_plain_is_equal(map1, map2);
3846 if (intersect < 0 || intersect)
3847 return intersect < 0 ? isl_bool_error : isl_bool_false;
3849 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3852 /* Are "map1" and "map2" disjoint?
3853 * The parameters are assumed to have been aligned.
3855 * In particular, check whether all pairs of basic maps are disjoint.
3857 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3858 __isl_keep isl_map *map2)
3860 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3863 /* Are "map1" and "map2" disjoint?
3865 * They are disjoint if they are "obviously disjoint" or if one of them
3866 * is empty. Otherwise, they are not disjoint if one of them is universal.
3867 * If the two inputs are (obviously) equal and not empty, then they are
3868 * not disjoint.
3869 * If none of these cases apply, then check if all pairs of basic maps
3870 * are disjoint after aligning the parameters.
3872 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3874 isl_bool disjoint;
3875 isl_bool intersect;
3877 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3878 if (disjoint < 0 || disjoint)
3879 return disjoint;
3881 disjoint = isl_map_is_empty(map1);
3882 if (disjoint < 0 || disjoint)
3883 return disjoint;
3885 disjoint = isl_map_is_empty(map2);
3886 if (disjoint < 0 || disjoint)
3887 return disjoint;
3889 intersect = isl_map_plain_is_universe(map1);
3890 if (intersect < 0 || intersect)
3891 return intersect < 0 ? isl_bool_error : isl_bool_false;
3893 intersect = isl_map_plain_is_universe(map2);
3894 if (intersect < 0 || intersect)
3895 return intersect < 0 ? isl_bool_error : isl_bool_false;
3897 intersect = isl_map_plain_is_equal(map1, map2);
3898 if (intersect < 0 || intersect)
3899 return isl_bool_not(intersect);
3901 return isl_map_align_params_map_map_and_test(map1, map2,
3902 &isl_map_is_disjoint_aligned);
3905 /* Are "bmap1" and "bmap2" disjoint?
3907 * They are disjoint if they are "obviously disjoint" or if one of them
3908 * is empty. Otherwise, they are not disjoint if one of them is universal.
3909 * If none of these cases apply, we compute the intersection and see if
3910 * the result is empty.
3912 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3913 __isl_keep isl_basic_map *bmap2)
3915 isl_bool disjoint;
3916 isl_bool intersect;
3917 isl_basic_map *test;
3919 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3920 if (disjoint < 0 || disjoint)
3921 return disjoint;
3923 disjoint = isl_basic_map_is_empty(bmap1);
3924 if (disjoint < 0 || disjoint)
3925 return disjoint;
3927 disjoint = isl_basic_map_is_empty(bmap2);
3928 if (disjoint < 0 || disjoint)
3929 return disjoint;
3931 intersect = isl_basic_map_plain_is_universe(bmap1);
3932 if (intersect < 0 || intersect)
3933 return intersect < 0 ? isl_bool_error : isl_bool_false;
3935 intersect = isl_basic_map_plain_is_universe(bmap2);
3936 if (intersect < 0 || intersect)
3937 return intersect < 0 ? isl_bool_error : isl_bool_false;
3939 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3940 isl_basic_map_copy(bmap2));
3941 disjoint = isl_basic_map_is_empty(test);
3942 isl_basic_map_free(test);
3944 return disjoint;
3947 /* Are "bset1" and "bset2" disjoint?
3949 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3950 __isl_keep isl_basic_set *bset2)
3952 return isl_basic_map_is_disjoint(bset1, bset2);
3955 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3956 __isl_keep isl_set *set2)
3958 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3961 /* Are "set1" and "set2" disjoint?
3963 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3965 return isl_map_is_disjoint(set1, set2);
3968 /* Is "v" equal to 0, 1 or -1?
3970 static int is_zero_or_one(isl_int v)
3972 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3975 /* Are the "n" coefficients starting at "first" of inequality constraints
3976 * "i" and "j" of "bmap" opposite to each other?
3978 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
3979 int first, int n)
3981 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
3984 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3985 * apart from the constant term?
3987 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
3989 isl_size total;
3991 total = isl_basic_map_dim(bmap, isl_dim_all);
3992 if (total < 0)
3993 return isl_bool_error;
3994 return is_opposite_part(bmap, i, j, 1, total);
3997 /* Check if we can combine a given div with lower bound l and upper
3998 * bound u with some other div and if so return that other div.
3999 * Otherwise, return a position beyond the integer divisions.
4000 * Return -1 on error.
4002 * We first check that
4003 * - the bounds are opposites of each other (except for the constant
4004 * term)
4005 * - the bounds do not reference any other div
4006 * - no div is defined in terms of this div
4008 * Let m be the size of the range allowed on the div by the bounds.
4009 * That is, the bounds are of the form
4011 * e <= a <= e + m - 1
4013 * with e some expression in the other variables.
4014 * We look for another div b such that no third div is defined in terms
4015 * of this second div b and such that in any constraint that contains
4016 * a (except for the given lower and upper bound), also contains b
4017 * with a coefficient that is m times that of b.
4018 * That is, all constraints (except for the lower and upper bound)
4019 * are of the form
4021 * e + f (a + m b) >= 0
4023 * Furthermore, in the constraints that only contain b, the coefficient
4024 * of b should be equal to 1 or -1.
4025 * If so, we return b so that "a + m b" can be replaced by
4026 * a single div "c = a + m b".
4028 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4029 unsigned div, unsigned l, unsigned u)
4031 int i, j;
4032 unsigned n_div;
4033 isl_size v_div;
4034 int coalesce;
4035 isl_bool opp;
4037 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4038 if (n_div <= 1)
4039 return n_div;
4040 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4041 if (v_div < 0)
4042 return -1;
4043 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4044 return n_div;
4045 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4046 n_div - div - 1) != -1)
4047 return n_div;
4048 opp = is_opposite(bmap, l, u);
4049 if (opp < 0 || !opp)
4050 return opp < 0 ? -1 : n_div;
4052 for (i = 0; i < n_div; ++i) {
4053 if (isl_int_is_zero(bmap->div[i][0]))
4054 continue;
4055 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4056 return n_div;
4059 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4060 if (isl_int_is_neg(bmap->ineq[l][0])) {
4061 isl_int_sub(bmap->ineq[l][0],
4062 bmap->ineq[l][0], bmap->ineq[u][0]);
4063 bmap = isl_basic_map_copy(bmap);
4064 bmap = isl_basic_map_set_to_empty(bmap);
4065 isl_basic_map_free(bmap);
4066 return n_div;
4068 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4069 coalesce = n_div;
4070 for (i = 0; i < n_div; ++i) {
4071 if (i == div)
4072 continue;
4073 if (!pairs[i])
4074 continue;
4075 for (j = 0; j < n_div; ++j) {
4076 if (isl_int_is_zero(bmap->div[j][0]))
4077 continue;
4078 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
4079 break;
4081 if (j < n_div)
4082 continue;
4083 for (j = 0; j < bmap->n_ineq; ++j) {
4084 int valid;
4085 if (j == l || j == u)
4086 continue;
4087 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4088 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4089 continue;
4090 break;
4092 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4093 break;
4094 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4095 bmap->ineq[j][1 + v_div + div],
4096 bmap->ineq[l][0]);
4097 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4098 bmap->ineq[j][1 + v_div + i]);
4099 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4100 bmap->ineq[j][1 + v_div + div],
4101 bmap->ineq[l][0]);
4102 if (!valid)
4103 break;
4105 if (j < bmap->n_ineq)
4106 continue;
4107 coalesce = i;
4108 break;
4110 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4111 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4112 return coalesce;
4115 /* Internal data structure used during the construction and/or evaluation of
4116 * an inequality that ensures that a pair of bounds always allows
4117 * for an integer value.
4119 * "tab" is the tableau in which the inequality is evaluated. It may
4120 * be NULL until it is actually needed.
4121 * "v" contains the inequality coefficients.
4122 * "g", "fl" and "fu" are temporary scalars used during the construction and
4123 * evaluation.
4125 struct test_ineq_data {
4126 struct isl_tab *tab;
4127 isl_vec *v;
4128 isl_int g;
4129 isl_int fl;
4130 isl_int fu;
4133 /* Free all the memory allocated by the fields of "data".
4135 static void test_ineq_data_clear(struct test_ineq_data *data)
4137 isl_tab_free(data->tab);
4138 isl_vec_free(data->v);
4139 isl_int_clear(data->g);
4140 isl_int_clear(data->fl);
4141 isl_int_clear(data->fu);
4144 /* Is the inequality stored in data->v satisfied by "bmap"?
4145 * That is, does it only attain non-negative values?
4146 * data->tab is a tableau corresponding to "bmap".
4148 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4149 struct test_ineq_data *data)
4151 isl_ctx *ctx;
4152 enum isl_lp_result res;
4154 ctx = isl_basic_map_get_ctx(bmap);
4155 if (!data->tab)
4156 data->tab = isl_tab_from_basic_map(bmap, 0);
4157 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4158 if (res == isl_lp_error)
4159 return isl_bool_error;
4160 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4163 /* Given a lower and an upper bound on div i, do they always allow
4164 * for an integer value of the given div?
4165 * Determine this property by constructing an inequality
4166 * such that the property is guaranteed when the inequality is nonnegative.
4167 * The lower bound is inequality l, while the upper bound is inequality u.
4168 * The constructed inequality is stored in data->v.
4170 * Let the upper bound be
4172 * -n_u a + e_u >= 0
4174 * and the lower bound
4176 * n_l a + e_l >= 0
4178 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4179 * We have
4181 * - f_u e_l <= f_u f_l g a <= f_l e_u
4183 * Since all variables are integer valued, this is equivalent to
4185 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4187 * If this interval is at least f_u f_l g, then it contains at least
4188 * one integer value for a.
4189 * That is, the test constraint is
4191 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4193 * or
4195 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4197 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4198 * then the constraint can be scaled down by a factor g',
4199 * with the constant term replaced by
4200 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4201 * Note that the result of applying Fourier-Motzkin to this pair
4202 * of constraints is
4204 * f_l e_u + f_u e_l >= 0
4206 * If the constant term of the scaled down version of this constraint,
4207 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4208 * term of the scaled down test constraint, then the test constraint
4209 * is known to hold and no explicit evaluation is required.
4210 * This is essentially the Omega test.
4212 * If the test constraint consists of only a constant term, then
4213 * it is sufficient to look at the sign of this constant term.
4215 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4216 int l, int u, struct test_ineq_data *data)
4218 unsigned offset;
4219 isl_size n_div;
4221 offset = isl_basic_map_offset(bmap, isl_dim_div);
4222 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4223 if (n_div < 0)
4224 return isl_bool_error;
4226 isl_int_gcd(data->g,
4227 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4228 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4229 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4230 isl_int_neg(data->fu, data->fu);
4231 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4232 data->fu, bmap->ineq[l], offset + n_div);
4233 isl_int_mul(data->g, data->g, data->fl);
4234 isl_int_mul(data->g, data->g, data->fu);
4235 isl_int_sub(data->g, data->g, data->fl);
4236 isl_int_sub(data->g, data->g, data->fu);
4237 isl_int_add_ui(data->g, data->g, 1);
4238 isl_int_sub(data->fl, data->v->el[0], data->g);
4240 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4241 if (isl_int_is_zero(data->g))
4242 return isl_int_is_nonneg(data->fl);
4243 if (isl_int_is_one(data->g)) {
4244 isl_int_set(data->v->el[0], data->fl);
4245 return test_ineq_is_satisfied(bmap, data);
4247 isl_int_fdiv_q(data->fl, data->fl, data->g);
4248 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4249 if (isl_int_eq(data->fl, data->v->el[0]))
4250 return isl_bool_true;
4251 isl_int_set(data->v->el[0], data->fl);
4252 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4253 offset - 1 + n_div);
4255 return test_ineq_is_satisfied(bmap, data);
4258 /* Remove more kinds of divs that are not strictly needed.
4259 * In particular, if all pairs of lower and upper bounds on a div
4260 * are such that they allow at least one integer value of the div,
4261 * then we can eliminate the div using Fourier-Motzkin without
4262 * introducing any spurious solutions.
4264 * If at least one of the two constraints has a unit coefficient for the div,
4265 * then the presence of such a value is guaranteed so there is no need to check.
4266 * In particular, the value attained by the bound with unit coefficient
4267 * can serve as this intermediate value.
4269 static __isl_give isl_basic_map *drop_more_redundant_divs(
4270 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4272 isl_ctx *ctx;
4273 struct test_ineq_data data = { NULL, NULL };
4274 unsigned off;
4275 isl_size n_div;
4276 int remove = -1;
4278 isl_int_init(data.g);
4279 isl_int_init(data.fl);
4280 isl_int_init(data.fu);
4282 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4283 if (n_div < 0)
4284 goto error;
4286 ctx = isl_basic_map_get_ctx(bmap);
4287 off = isl_basic_map_offset(bmap, isl_dim_div);
4288 data.v = isl_vec_alloc(ctx, off + n_div);
4289 if (!data.v)
4290 goto error;
4292 while (n > 0) {
4293 int i, l, u;
4294 int best = -1;
4295 isl_bool has_int;
4297 for (i = 0; i < n_div; ++i) {
4298 if (!pairs[i])
4299 continue;
4300 if (best >= 0 && pairs[best] <= pairs[i])
4301 continue;
4302 best = i;
4305 i = best;
4306 for (l = 0; l < bmap->n_ineq; ++l) {
4307 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4308 continue;
4309 if (isl_int_is_one(bmap->ineq[l][off + i]))
4310 continue;
4311 for (u = 0; u < bmap->n_ineq; ++u) {
4312 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4313 continue;
4314 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4315 continue;
4316 has_int = int_between_bounds(bmap, i, l, u,
4317 &data);
4318 if (has_int < 0)
4319 goto error;
4320 if (data.tab && data.tab->empty)
4321 break;
4322 if (!has_int)
4323 break;
4325 if (u < bmap->n_ineq)
4326 break;
4328 if (data.tab && data.tab->empty) {
4329 bmap = isl_basic_map_set_to_empty(bmap);
4330 break;
4332 if (l == bmap->n_ineq) {
4333 remove = i;
4334 break;
4336 pairs[i] = 0;
4337 --n;
4340 test_ineq_data_clear(&data);
4342 free(pairs);
4344 if (remove < 0)
4345 return bmap;
4347 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4348 return isl_basic_map_drop_redundant_divs(bmap);
4349 error:
4350 free(pairs);
4351 isl_basic_map_free(bmap);
4352 test_ineq_data_clear(&data);
4353 return NULL;
4356 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4357 * and the upper bound u, div1 always occurs together with div2 in the form
4358 * (div1 + m div2), where m is the constant range on the variable div1
4359 * allowed by l and u, replace the pair div1 and div2 by a single
4360 * div that is equal to div1 + m div2.
4362 * The new div will appear in the location that contains div2.
4363 * We need to modify all constraints that contain
4364 * div2 = (div - div1) / m
4365 * The coefficient of div2 is known to be equal to 1 or -1.
4366 * (If a constraint does not contain div2, it will also not contain div1.)
4367 * If the constraint also contains div1, then we know they appear
4368 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4369 * i.e., the coefficient of div is f.
4371 * Otherwise, we first need to introduce div1 into the constraint.
4372 * Let l be
4374 * div1 + f >=0
4376 * and u
4378 * -div1 + f' >= 0
4380 * A lower bound on div2
4382 * div2 + t >= 0
4384 * can be replaced by
4386 * m div2 + div1 + m t + f >= 0
4388 * An upper bound
4390 * -div2 + t >= 0
4392 * can be replaced by
4394 * -(m div2 + div1) + m t + f' >= 0
4396 * These constraint are those that we would obtain from eliminating
4397 * div1 using Fourier-Motzkin.
4399 * After all constraints have been modified, we drop the lower and upper
4400 * bound and then drop div1.
4401 * Since the new div is only placed in the same location that used
4402 * to store div2, but otherwise has a different meaning, any possible
4403 * explicit representation of the original div2 is removed.
4405 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4406 unsigned div1, unsigned div2, unsigned l, unsigned u)
4408 isl_ctx *ctx;
4409 isl_int m;
4410 isl_size v_div;
4411 unsigned total;
4412 int i;
4414 ctx = isl_basic_map_get_ctx(bmap);
4416 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4417 if (v_div < 0)
4418 return isl_basic_map_free(bmap);
4419 total = 1 + v_div + bmap->n_div;
4421 isl_int_init(m);
4422 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4423 isl_int_add_ui(m, m, 1);
4425 for (i = 0; i < bmap->n_ineq; ++i) {
4426 if (i == l || i == u)
4427 continue;
4428 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4429 continue;
4430 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4431 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4432 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4433 ctx->one, bmap->ineq[l], total);
4434 else
4435 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4436 ctx->one, bmap->ineq[u], total);
4438 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4439 bmap->ineq[i][1 + v_div + div1]);
4440 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4443 isl_int_clear(m);
4444 if (l > u) {
4445 isl_basic_map_drop_inequality(bmap, l);
4446 isl_basic_map_drop_inequality(bmap, u);
4447 } else {
4448 isl_basic_map_drop_inequality(bmap, u);
4449 isl_basic_map_drop_inequality(bmap, l);
4451 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4452 bmap = isl_basic_map_drop_div(bmap, div1);
4453 return bmap;
4456 /* First check if we can coalesce any pair of divs and
4457 * then continue with dropping more redundant divs.
4459 * We loop over all pairs of lower and upper bounds on a div
4460 * with coefficient 1 and -1, respectively, check if there
4461 * is any other div "c" with which we can coalesce the div
4462 * and if so, perform the coalescing.
4464 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4465 __isl_take isl_basic_map *bmap, int *pairs, int n)
4467 int i, l, u;
4468 isl_size v_div;
4469 isl_size n_div;
4471 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4472 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4473 if (v_div < 0 || n_div < 0)
4474 return isl_basic_map_free(bmap);
4476 for (i = 0; i < n_div; ++i) {
4477 if (!pairs[i])
4478 continue;
4479 for (l = 0; l < bmap->n_ineq; ++l) {
4480 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4481 continue;
4482 for (u = 0; u < bmap->n_ineq; ++u) {
4483 int c;
4485 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4486 continue;
4487 c = div_find_coalesce(bmap, pairs, i, l, u);
4488 if (c < 0)
4489 goto error;
4490 if (c >= n_div)
4491 continue;
4492 free(pairs);
4493 bmap = coalesce_divs(bmap, i, c, l, u);
4494 return isl_basic_map_drop_redundant_divs(bmap);
4499 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4500 free(pairs);
4501 return bmap;
4504 return drop_more_redundant_divs(bmap, pairs, n);
4505 error:
4506 free(pairs);
4507 isl_basic_map_free(bmap);
4508 return NULL;
4511 /* Are the "n" coefficients starting at "first" of inequality constraints
4512 * "i" and "j" of "bmap" equal to each other?
4514 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4515 int first, int n)
4517 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4520 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4521 * apart from the constant term and the coefficient at position "pos"?
4523 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4524 int pos)
4526 isl_size total;
4528 total = isl_basic_map_dim(bmap, isl_dim_all);
4529 if (total < 0)
4530 return isl_bool_error;
4531 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4532 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4535 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4536 * apart from the constant term and the coefficient at position "pos"?
4538 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4539 int pos)
4541 isl_size total;
4543 total = isl_basic_map_dim(bmap, isl_dim_all);
4544 if (total < 0)
4545 return isl_bool_error;
4546 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4547 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4550 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4551 * been modified, simplying it if "simplify" is set.
4552 * Free the temporary data structure "pairs" that was associated
4553 * to the old version of "bmap".
4555 static __isl_give isl_basic_map *drop_redundant_divs_again(
4556 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4558 if (simplify)
4559 bmap = isl_basic_map_simplify(bmap);
4560 free(pairs);
4561 return isl_basic_map_drop_redundant_divs(bmap);
4564 /* Is "div" the single unknown existentially quantified variable
4565 * in inequality constraint "ineq" of "bmap"?
4566 * "div" is known to have a non-zero coefficient in "ineq".
4568 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4569 int div)
4571 int i;
4572 isl_size n_div;
4573 unsigned o_div;
4574 isl_bool known;
4576 known = isl_basic_map_div_is_known(bmap, div);
4577 if (known < 0 || known)
4578 return isl_bool_not(known);
4579 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4580 if (n_div < 0)
4581 return isl_bool_error;
4582 if (n_div == 1)
4583 return isl_bool_true;
4584 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4585 for (i = 0; i < n_div; ++i) {
4586 isl_bool known;
4588 if (i == div)
4589 continue;
4590 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4591 continue;
4592 known = isl_basic_map_div_is_known(bmap, i);
4593 if (known < 0 || !known)
4594 return known;
4597 return isl_bool_true;
4600 /* Does integer division "div" have coefficient 1 in inequality constraint
4601 * "ineq" of "map"?
4603 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4605 unsigned o_div;
4607 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4608 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4609 return isl_bool_true;
4611 return isl_bool_false;
4614 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4615 * then try and drop redundant divs again,
4616 * freeing the temporary data structure "pairs" that was associated
4617 * to the old version of "bmap".
4619 static __isl_give isl_basic_map *set_eq_and_try_again(
4620 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4622 bmap = isl_basic_map_cow(bmap);
4623 isl_basic_map_inequality_to_equality(bmap, ineq);
4624 return drop_redundant_divs_again(bmap, pairs, 1);
4627 /* Drop the integer division at position "div", along with the two
4628 * inequality constraints "ineq1" and "ineq2" in which it appears
4629 * from "bmap" and then try and drop redundant divs again,
4630 * freeing the temporary data structure "pairs" that was associated
4631 * to the old version of "bmap".
4633 static __isl_give isl_basic_map *drop_div_and_try_again(
4634 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4635 __isl_take int *pairs)
4637 if (ineq1 > ineq2) {
4638 isl_basic_map_drop_inequality(bmap, ineq1);
4639 isl_basic_map_drop_inequality(bmap, ineq2);
4640 } else {
4641 isl_basic_map_drop_inequality(bmap, ineq2);
4642 isl_basic_map_drop_inequality(bmap, ineq1);
4644 bmap = isl_basic_map_drop_div(bmap, div);
4645 return drop_redundant_divs_again(bmap, pairs, 0);
4648 /* Given two inequality constraints
4650 * f(x) + n d + c >= 0, (ineq)
4652 * with d the variable at position "pos", and
4654 * f(x) + c0 >= 0, (lower)
4656 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4657 * determined by the first constraint.
4658 * That is, store
4660 * ceil((c0 - c)/n)
4662 * in *l.
4664 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4665 int ineq, int lower, int pos, isl_int *l)
4667 isl_int_neg(*l, bmap->ineq[ineq][0]);
4668 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4669 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4672 /* Given two inequality constraints
4674 * f(x) + n d + c >= 0, (ineq)
4676 * with d the variable at position "pos", and
4678 * -f(x) - c0 >= 0, (upper)
4680 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4681 * determined by the first constraint.
4682 * That is, store
4684 * ceil((-c1 - c)/n)
4686 * in *u.
4688 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4689 int ineq, int upper, int pos, isl_int *u)
4691 isl_int_neg(*u, bmap->ineq[ineq][0]);
4692 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4693 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4696 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4697 * does the corresponding lower bound have a fixed value in "bmap"?
4699 * In particular, "ineq" is of the form
4701 * f(x) + n d + c >= 0
4703 * with n > 0, c the constant term and
4704 * d the existentially quantified variable "div".
4705 * That is, the lower bound is
4707 * ceil((-f(x) - c)/n)
4709 * Look for a pair of constraints
4711 * f(x) + c0 >= 0
4712 * -f(x) + c1 >= 0
4714 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4715 * That is, check that
4717 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4719 * If so, return the index of inequality f(x) + c0 >= 0.
4720 * Otherwise, return bmap->n_ineq.
4721 * Return -1 on error.
4723 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4725 int i;
4726 int lower = -1, upper = -1;
4727 unsigned o_div;
4728 isl_int l, u;
4729 int equal;
4731 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4732 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4733 isl_bool par, opp;
4735 if (i == ineq)
4736 continue;
4737 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4738 continue;
4739 par = isl_bool_false;
4740 if (lower < 0)
4741 par = is_parallel_except(bmap, ineq, i, o_div + div);
4742 if (par < 0)
4743 return -1;
4744 if (par) {
4745 lower = i;
4746 continue;
4748 opp = isl_bool_false;
4749 if (upper < 0)
4750 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4751 if (opp < 0)
4752 return -1;
4753 if (opp)
4754 upper = i;
4757 if (lower < 0 || upper < 0)
4758 return bmap->n_ineq;
4760 isl_int_init(l);
4761 isl_int_init(u);
4763 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4764 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4766 equal = isl_int_eq(l, u);
4768 isl_int_clear(l);
4769 isl_int_clear(u);
4771 return equal ? lower : bmap->n_ineq;
4774 /* Given a lower bound constraint "ineq" on the existentially quantified
4775 * variable "div", such that the corresponding lower bound has
4776 * a fixed value in "bmap", assign this fixed value to the variable and
4777 * then try and drop redundant divs again,
4778 * freeing the temporary data structure "pairs" that was associated
4779 * to the old version of "bmap".
4780 * "lower" determines the constant value for the lower bound.
4782 * In particular, "ineq" is of the form
4784 * f(x) + n d + c >= 0,
4786 * while "lower" is of the form
4788 * f(x) + c0 >= 0
4790 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4791 * is ceil((c0 - c)/n).
4793 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4794 int div, int ineq, int lower, int *pairs)
4796 isl_int c;
4797 unsigned o_div;
4799 isl_int_init(c);
4801 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4802 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4803 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4804 free(pairs);
4806 isl_int_clear(c);
4808 return isl_basic_map_drop_redundant_divs(bmap);
4811 /* Remove divs that are not strictly needed based on the inequality
4812 * constraints.
4813 * In particular, if a div only occurs positively (or negatively)
4814 * in constraints, then it can simply be dropped.
4815 * Also, if a div occurs in only two constraints and if moreover
4816 * those two constraints are opposite to each other, except for the constant
4817 * term and if the sum of the constant terms is such that for any value
4818 * of the other values, there is always at least one integer value of the
4819 * div, i.e., if one plus this sum is greater than or equal to
4820 * the (absolute value) of the coefficient of the div in the constraints,
4821 * then we can also simply drop the div.
4823 * If an existentially quantified variable does not have an explicit
4824 * representation, appears in only a single lower bound that does not
4825 * involve any other such existentially quantified variables and appears
4826 * in this lower bound with coefficient 1,
4827 * then fix the variable to the value of the lower bound. That is,
4828 * turn the inequality into an equality.
4829 * If for any value of the other variables, there is any value
4830 * for the existentially quantified variable satisfying the constraints,
4831 * then this lower bound also satisfies the constraints.
4832 * It is therefore safe to pick this lower bound.
4834 * The same reasoning holds even if the coefficient is not one.
4835 * However, fixing the variable to the value of the lower bound may
4836 * in general introduce an extra integer division, in which case
4837 * it may be better to pick another value.
4838 * If this integer division has a known constant value, then plugging
4839 * in this constant value removes the existentially quantified variable
4840 * completely. In particular, if the lower bound is of the form
4841 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4842 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4843 * then the existentially quantified variable can be assigned this
4844 * shared value.
4846 * We skip divs that appear in equalities or in the definition of other divs.
4847 * Divs that appear in the definition of other divs usually occur in at least
4848 * 4 constraints, but the constraints may have been simplified.
4850 * If any divs are left after these simple checks then we move on
4851 * to more complicated cases in drop_more_redundant_divs.
4853 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4854 __isl_take isl_basic_map *bmap)
4856 int i, j;
4857 isl_size off;
4858 int *pairs = NULL;
4859 int n = 0;
4860 int n_ineq;
4862 if (!bmap)
4863 goto error;
4864 if (bmap->n_div == 0)
4865 return bmap;
4867 off = isl_basic_map_var_offset(bmap, isl_dim_div);
4868 if (off < 0)
4869 return isl_basic_map_free(bmap);
4870 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4871 if (!pairs)
4872 goto error;
4874 n_ineq = isl_basic_map_n_inequality(bmap);
4875 for (i = 0; i < bmap->n_div; ++i) {
4876 int pos, neg;
4877 int last_pos, last_neg;
4878 int redundant;
4879 int defined;
4880 isl_bool opp, set_div;
4882 defined = !isl_int_is_zero(bmap->div[i][0]);
4883 for (j = i; j < bmap->n_div; ++j)
4884 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4885 break;
4886 if (j < bmap->n_div)
4887 continue;
4888 for (j = 0; j < bmap->n_eq; ++j)
4889 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4890 break;
4891 if (j < bmap->n_eq)
4892 continue;
4893 ++n;
4894 pos = neg = 0;
4895 for (j = 0; j < bmap->n_ineq; ++j) {
4896 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4897 last_pos = j;
4898 ++pos;
4900 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4901 last_neg = j;
4902 ++neg;
4905 pairs[i] = pos * neg;
4906 if (pairs[i] == 0) {
4907 for (j = bmap->n_ineq - 1; j >= 0; --j)
4908 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4909 isl_basic_map_drop_inequality(bmap, j);
4910 bmap = isl_basic_map_drop_div(bmap, i);
4911 return drop_redundant_divs_again(bmap, pairs, 0);
4913 if (pairs[i] != 1)
4914 opp = isl_bool_false;
4915 else
4916 opp = is_opposite(bmap, last_pos, last_neg);
4917 if (opp < 0)
4918 goto error;
4919 if (!opp) {
4920 int lower;
4921 isl_bool single, one;
4923 if (pos != 1)
4924 continue;
4925 single = single_unknown(bmap, last_pos, i);
4926 if (single < 0)
4927 goto error;
4928 if (!single)
4929 continue;
4930 one = has_coef_one(bmap, i, last_pos);
4931 if (one < 0)
4932 goto error;
4933 if (one)
4934 return set_eq_and_try_again(bmap, last_pos,
4935 pairs);
4936 lower = lower_bound_is_cst(bmap, i, last_pos);
4937 if (lower < 0)
4938 goto error;
4939 if (lower < n_ineq)
4940 return fix_cst_lower(bmap, i, last_pos, lower,
4941 pairs);
4942 continue;
4945 isl_int_add(bmap->ineq[last_pos][0],
4946 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4947 isl_int_add_ui(bmap->ineq[last_pos][0],
4948 bmap->ineq[last_pos][0], 1);
4949 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4950 bmap->ineq[last_pos][1+off+i]);
4951 isl_int_sub_ui(bmap->ineq[last_pos][0],
4952 bmap->ineq[last_pos][0], 1);
4953 isl_int_sub(bmap->ineq[last_pos][0],
4954 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4955 if (redundant)
4956 return drop_div_and_try_again(bmap, i,
4957 last_pos, last_neg, pairs);
4958 if (defined)
4959 set_div = isl_bool_false;
4960 else
4961 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4962 if (set_div < 0)
4963 return isl_basic_map_free(bmap);
4964 if (set_div) {
4965 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4966 return drop_redundant_divs_again(bmap, pairs, 1);
4968 pairs[i] = 0;
4969 --n;
4972 if (n > 0)
4973 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4975 free(pairs);
4976 return bmap;
4977 error:
4978 free(pairs);
4979 isl_basic_map_free(bmap);
4980 return NULL;
4983 /* Consider the coefficients at "c" as a row vector and replace
4984 * them with their product with "T". "T" is assumed to be a square matrix.
4986 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4988 isl_size n;
4989 isl_ctx *ctx;
4990 isl_vec *v;
4992 n = isl_mat_rows(T);
4993 if (n < 0)
4994 return isl_stat_error;
4995 if (isl_seq_first_non_zero(c, n) == -1)
4996 return isl_stat_ok;
4997 ctx = isl_mat_get_ctx(T);
4998 v = isl_vec_alloc(ctx, n);
4999 if (!v)
5000 return isl_stat_error;
5001 isl_seq_swp_or_cpy(v->el, c, n);
5002 v = isl_vec_mat_product(v, isl_mat_copy(T));
5003 if (!v)
5004 return isl_stat_error;
5005 isl_seq_swp_or_cpy(c, v->el, n);
5006 isl_vec_free(v);
5008 return isl_stat_ok;
5011 /* Plug in T for the variables in "bmap" starting at "pos".
5012 * T is a linear unimodular matrix, i.e., without constant term.
5014 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5015 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5017 int i;
5018 isl_size n_row, n_col;
5020 bmap = isl_basic_map_cow(bmap);
5021 n_row = isl_mat_rows(T);
5022 n_col = isl_mat_cols(T);
5023 if (!bmap || n_row < 0 || n_col < 0)
5024 goto error;
5026 if (n_col != n_row)
5027 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5028 "expecting square matrix", goto error);
5030 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5031 goto error;
5033 for (i = 0; i < bmap->n_eq; ++i)
5034 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5035 goto error;
5036 for (i = 0; i < bmap->n_ineq; ++i)
5037 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5038 goto error;
5039 for (i = 0; i < bmap->n_div; ++i) {
5040 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5041 continue;
5042 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5043 goto error;
5046 isl_mat_free(T);
5047 return bmap;
5048 error:
5049 isl_basic_map_free(bmap);
5050 isl_mat_free(T);
5051 return NULL;
5054 /* Remove divs that are not strictly needed.
5056 * First look for an equality constraint involving two or more
5057 * existentially quantified variables without an explicit
5058 * representation. Replace the combination that appears
5059 * in the equality constraint by a single existentially quantified
5060 * variable such that the equality can be used to derive
5061 * an explicit representation for the variable.
5062 * If there are no more such equality constraints, then continue
5063 * with isl_basic_map_drop_redundant_divs_ineq.
5065 * In particular, if the equality constraint is of the form
5067 * f(x) + \sum_i c_i a_i = 0
5069 * with a_i existentially quantified variable without explicit
5070 * representation, then apply a transformation on the existentially
5071 * quantified variables to turn the constraint into
5073 * f(x) + g a_1' = 0
5075 * with g the gcd of the c_i.
5076 * In order to easily identify which existentially quantified variables
5077 * have a complete explicit representation, i.e., without being defined
5078 * in terms of other existentially quantified variables without
5079 * an explicit representation, the existentially quantified variables
5080 * are first sorted.
5082 * The variable transformation is computed by extending the row
5083 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5085 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5086 * [a_2'] [ a_2 ]
5087 * ... = U ....
5088 * [a_n'] [ a_n ]
5090 * with [c_1/g ... c_n/g] representing the first row of U.
5091 * The inverse of U is then plugged into the original constraints.
5092 * The call to isl_basic_map_simplify makes sure the explicit
5093 * representation for a_1' is extracted from the equality constraint.
5095 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5096 __isl_take isl_basic_map *bmap)
5098 int first;
5099 int i;
5100 unsigned o_div;
5101 isl_size n_div;
5102 int l;
5103 isl_ctx *ctx;
5104 isl_mat *T;
5106 if (!bmap)
5107 return NULL;
5108 if (isl_basic_map_divs_known(bmap))
5109 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5110 if (bmap->n_eq == 0)
5111 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5112 bmap = isl_basic_map_sort_divs(bmap);
5113 if (!bmap)
5114 return NULL;
5116 first = isl_basic_map_first_unknown_div(bmap);
5117 if (first < 0)
5118 return isl_basic_map_free(bmap);
5120 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5121 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5122 if (n_div < 0)
5123 return isl_basic_map_free(bmap);
5125 for (i = 0; i < bmap->n_eq; ++i) {
5126 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5127 n_div - (first));
5128 if (l < 0)
5129 continue;
5130 l += first;
5131 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5132 n_div - (l + 1)) == -1)
5133 continue;
5134 break;
5136 if (i >= bmap->n_eq)
5137 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5139 ctx = isl_basic_map_get_ctx(bmap);
5140 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5141 if (!T)
5142 return isl_basic_map_free(bmap);
5143 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5144 T = isl_mat_normalize_row(T, 0);
5145 T = isl_mat_unimodular_complete(T, 1);
5146 T = isl_mat_right_inverse(T);
5148 for (i = l; i < n_div; ++i)
5149 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5150 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5151 bmap = isl_basic_map_simplify(bmap);
5153 return isl_basic_map_drop_redundant_divs(bmap);
5156 /* Does "bmap" satisfy any equality that involves more than 2 variables
5157 * and/or has coefficients different from -1 and 1?
5159 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5161 int i;
5162 isl_size total;
5164 total = isl_basic_map_dim(bmap, isl_dim_all);
5165 if (total < 0)
5166 return isl_bool_error;
5168 for (i = 0; i < bmap->n_eq; ++i) {
5169 int j, k;
5171 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5172 if (j < 0)
5173 continue;
5174 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5175 !isl_int_is_negone(bmap->eq[i][1 + j]))
5176 return isl_bool_true;
5178 j += 1;
5179 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5180 if (k < 0)
5181 continue;
5182 j += k;
5183 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5184 !isl_int_is_negone(bmap->eq[i][1 + j]))
5185 return isl_bool_true;
5187 j += 1;
5188 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5189 if (k >= 0)
5190 return isl_bool_true;
5193 return isl_bool_false;
5196 /* Remove any common factor g from the constraint coefficients in "v".
5197 * The constant term is stored in the first position and is replaced
5198 * by floor(c/g). If any common factor is removed and if this results
5199 * in a tightening of the constraint, then set *tightened.
5201 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5202 int *tightened)
5204 isl_ctx *ctx;
5206 if (!v)
5207 return NULL;
5208 ctx = isl_vec_get_ctx(v);
5209 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5210 if (isl_int_is_zero(ctx->normalize_gcd))
5211 return v;
5212 if (isl_int_is_one(ctx->normalize_gcd))
5213 return v;
5214 v = isl_vec_cow(v);
5215 if (!v)
5216 return NULL;
5217 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5218 *tightened = 1;
5219 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5220 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5221 v->size - 1);
5222 return v;
5225 /* If "bmap" is an integer set that satisfies any equality involving
5226 * more than 2 variables and/or has coefficients different from -1 and 1,
5227 * then use variable compression to reduce the coefficients by removing
5228 * any (hidden) common factor.
5229 * In particular, apply the variable compression to each constraint,
5230 * factor out any common factor in the non-constant coefficients and
5231 * then apply the inverse of the compression.
5232 * At the end, we mark the basic map as having reduced constants.
5233 * If this flag is still set on the next invocation of this function,
5234 * then we skip the computation.
5236 * Removing a common factor may result in a tightening of some of
5237 * the constraints. If this happens, then we may end up with two
5238 * opposite inequalities that can be replaced by an equality.
5239 * We therefore call isl_basic_map_detect_inequality_pairs,
5240 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5241 * and isl_basic_map_gauss if such a pair was found.
5243 * Tightening may also result in some other constraints becoming
5244 * (rationally) redundant with respect to the tightened constraint
5245 * (in combination with other constraints). The basic map may
5246 * therefore no longer be assumed to have no redundant constraints.
5248 * Note that this function may leave the result in an inconsistent state.
5249 * In particular, the constraints may not be gaussed.
5250 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5251 * for some of the test cases to pass successfully.
5252 * Any potential modification of the representation is therefore only
5253 * performed on a single copy of the basic map.
5255 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5256 __isl_take isl_basic_map *bmap)
5258 isl_size total;
5259 isl_bool multi;
5260 isl_ctx *ctx;
5261 isl_vec *v;
5262 isl_mat *eq, *T, *T2;
5263 int i;
5264 int tightened;
5266 if (!bmap)
5267 return NULL;
5268 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5269 return bmap;
5270 if (isl_basic_map_is_rational(bmap))
5271 return bmap;
5272 if (bmap->n_eq == 0)
5273 return bmap;
5274 multi = has_multiple_var_equality(bmap);
5275 if (multi < 0)
5276 return isl_basic_map_free(bmap);
5277 if (!multi)
5278 return bmap;
5280 total = isl_basic_map_dim(bmap, isl_dim_all);
5281 if (total < 0)
5282 return isl_basic_map_free(bmap);
5283 ctx = isl_basic_map_get_ctx(bmap);
5284 v = isl_vec_alloc(ctx, 1 + total);
5285 if (!v)
5286 return isl_basic_map_free(bmap);
5288 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5289 T = isl_mat_variable_compression(eq, &T2);
5290 if (!T || !T2)
5291 goto error;
5292 if (T->n_col == 0) {
5293 isl_mat_free(T);
5294 isl_mat_free(T2);
5295 isl_vec_free(v);
5296 return isl_basic_map_set_to_empty(bmap);
5299 bmap = isl_basic_map_cow(bmap);
5300 if (!bmap)
5301 goto error;
5303 tightened = 0;
5304 for (i = 0; i < bmap->n_ineq; ++i) {
5305 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5306 v = isl_vec_mat_product(v, isl_mat_copy(T));
5307 v = normalize_constraint(v, &tightened);
5308 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5309 if (!v)
5310 goto error;
5311 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5314 isl_mat_free(T);
5315 isl_mat_free(T2);
5316 isl_vec_free(v);
5318 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5320 if (tightened) {
5321 int progress = 0;
5323 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
5324 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5325 if (progress) {
5326 bmap = eliminate_divs_eq(bmap, &progress);
5327 bmap = isl_basic_map_gauss(bmap, NULL);
5331 return bmap;
5332 error:
5333 isl_mat_free(T);
5334 isl_mat_free(T2);
5335 isl_vec_free(v);
5336 return isl_basic_map_free(bmap);
5339 /* Shift the integer division at position "div" of "bmap"
5340 * by "shift" times the variable at position "pos".
5341 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5342 * corresponds to the constant term.
5344 * That is, if the integer division has the form
5346 * floor(f(x)/d)
5348 * then replace it by
5350 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5352 __isl_give isl_basic_map *isl_basic_map_shift_div(
5353 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5355 int i;
5356 isl_size total, n_div;
5358 if (isl_int_is_zero(shift))
5359 return bmap;
5360 total = isl_basic_map_dim(bmap, isl_dim_all);
5361 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5362 total -= n_div;
5363 if (total < 0 || n_div < 0)
5364 return isl_basic_map_free(bmap);
5366 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5368 for (i = 0; i < bmap->n_eq; ++i) {
5369 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5370 continue;
5371 isl_int_submul(bmap->eq[i][pos],
5372 shift, bmap->eq[i][1 + total + div]);
5374 for (i = 0; i < bmap->n_ineq; ++i) {
5375 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5376 continue;
5377 isl_int_submul(bmap->ineq[i][pos],
5378 shift, bmap->ineq[i][1 + total + div]);
5380 for (i = 0; i < bmap->n_div; ++i) {
5381 if (isl_int_is_zero(bmap->div[i][0]))
5382 continue;
5383 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5384 continue;
5385 isl_int_submul(bmap->div[i][1 + pos],
5386 shift, bmap->div[i][1 + 1 + total + div]);
5389 return bmap;