isl_basic_map_normalize_constraints: extract out scale_down_inequality
[isl.git] / isl_map_simplify.c
blob82892f2b89724ac98516d07649b9d7a45d41a761
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(__isl_keep 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(__isl_keep 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 /* Scale down the inequality constraint "ineq" of length "len"
48 * by a factor of "f".
49 * All the coefficients, except the constant term,
50 * are assumed to be multiples of "f".
52 * If the factor is 1, then no scaling needs to be performed.
54 static __isl_give isl_basic_map *scale_down_inequality(
55 __isl_take isl_basic_map *bmap, int ineq, isl_int f, unsigned len)
57 if (!bmap)
58 return NULL;
60 if (isl_int_is_one(f))
61 return bmap;
63 isl_int_fdiv_q(bmap->ineq[ineq][0], bmap->ineq[ineq][0], f);
64 isl_seq_scale_down(bmap->ineq[ineq] + 1, bmap->ineq[ineq] + 1, f, len);
66 return bmap;
69 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
70 __isl_take isl_basic_map *bmap)
72 int i;
73 isl_int gcd;
74 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
76 if (total < 0)
77 return isl_basic_map_free(bmap);
79 isl_int_init(gcd);
80 for (i = bmap->n_eq - 1; i >= 0; --i) {
81 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
82 if (isl_int_is_zero(gcd)) {
83 if (!isl_int_is_zero(bmap->eq[i][0])) {
84 bmap = isl_basic_map_set_to_empty(bmap);
85 break;
87 if (isl_basic_map_drop_equality(bmap, i) < 0)
88 goto error;
89 continue;
91 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
92 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
93 if (isl_int_is_one(gcd))
94 continue;
95 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
96 bmap = isl_basic_map_set_to_empty(bmap);
97 break;
99 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
102 for (i = bmap->n_ineq - 1; i >= 0; --i) {
103 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
104 if (isl_int_is_zero(gcd)) {
105 if (isl_int_is_neg(bmap->ineq[i][0])) {
106 bmap = isl_basic_map_set_to_empty(bmap);
107 break;
109 if (isl_basic_map_drop_inequality(bmap, i) < 0)
110 goto error;
111 continue;
113 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
114 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
115 bmap = scale_down_inequality(bmap, i, gcd, total);
116 if (!bmap)
117 goto error;
119 isl_int_clear(gcd);
121 return bmap;
122 error:
123 isl_int_clear(gcd);
124 isl_basic_map_free(bmap);
125 return NULL;
128 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
129 __isl_take isl_basic_set *bset)
131 isl_basic_map *bmap = bset_to_bmap(bset);
132 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
135 /* Reduce the coefficient of the variable at position "pos"
136 * in integer division "div", such that it lies in the half-open
137 * interval (1/2,1/2], extracting any excess value from this integer division.
138 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
139 * corresponds to the constant term.
141 * That is, the integer division is of the form
143 * floor((... + (c * d + r) * x_pos + ...)/d)
145 * with -d < 2 * r <= d.
146 * Replace it by
148 * floor((... + r * x_pos + ...)/d) + c * x_pos
150 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
151 * Otherwise, c = floor((c * d + r)/d) + 1.
153 * This is the same normalization that is performed by isl_aff_floor.
155 static __isl_give isl_basic_map *reduce_coefficient_in_div(
156 __isl_take isl_basic_map *bmap, int div, int pos)
158 isl_int shift;
159 int add_one;
161 isl_int_init(shift);
162 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
163 isl_int_mul_ui(shift, shift, 2);
164 add_one = isl_int_gt(shift, bmap->div[div][0]);
165 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
166 if (add_one)
167 isl_int_add_ui(shift, shift, 1);
168 isl_int_neg(shift, shift);
169 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
170 isl_int_clear(shift);
172 return bmap;
175 /* Does the coefficient of the variable at position "pos"
176 * in integer division "div" need to be reduced?
177 * That is, does it lie outside the half-open interval (1/2,1/2]?
178 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
179 * 2 * c != d.
181 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
182 int pos)
184 isl_bool r;
186 if (isl_int_is_zero(bmap->div[div][1 + pos]))
187 return isl_bool_false;
189 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
190 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
191 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
192 isl_int_divexact_ui(bmap->div[div][1 + pos],
193 bmap->div[div][1 + pos], 2);
195 return r;
198 /* Reduce the coefficients (including the constant term) of
199 * integer division "div", if needed.
200 * In particular, make sure all coefficients lie in
201 * the half-open interval (1/2,1/2].
203 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
204 __isl_take isl_basic_map *bmap, int div)
206 int i;
207 isl_size total;
209 total = isl_basic_map_dim(bmap, isl_dim_all);
210 if (total < 0)
211 return isl_basic_map_free(bmap);
212 for (i = 0; i < 1 + total; ++i) {
213 isl_bool reduce;
215 reduce = needs_reduction(bmap, div, i);
216 if (reduce < 0)
217 return isl_basic_map_free(bmap);
218 if (!reduce)
219 continue;
220 bmap = reduce_coefficient_in_div(bmap, div, i);
221 if (!bmap)
222 break;
225 return bmap;
228 /* Reduce the coefficients (including the constant term) of
229 * the known integer divisions, if needed
230 * In particular, make sure all coefficients lie in
231 * the half-open interval (1/2,1/2].
233 static __isl_give isl_basic_map *reduce_div_coefficients(
234 __isl_take isl_basic_map *bmap)
236 int i;
238 if (!bmap)
239 return NULL;
240 if (bmap->n_div == 0)
241 return bmap;
243 for (i = 0; i < bmap->n_div; ++i) {
244 if (isl_int_is_zero(bmap->div[i][0]))
245 continue;
246 bmap = reduce_div_coefficients_of_div(bmap, i);
247 if (!bmap)
248 break;
251 return bmap;
254 /* Remove any common factor in numerator and denominator of the div expression,
255 * not taking into account the constant term.
256 * That is, if the div is of the form
258 * floor((a + m f(x))/(m d))
260 * then replace it by
262 * floor((floor(a/m) + f(x))/d)
264 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
265 * and can therefore not influence the result of the floor.
267 static __isl_give isl_basic_map *normalize_div_expression(
268 __isl_take isl_basic_map *bmap, int div)
270 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
271 isl_ctx *ctx = bmap->ctx;
273 if (total < 0)
274 return isl_basic_map_free(bmap);
275 if (isl_int_is_zero(bmap->div[div][0]))
276 return bmap;
277 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
278 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
279 if (isl_int_is_one(ctx->normalize_gcd))
280 return bmap;
281 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
282 ctx->normalize_gcd);
283 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
284 ctx->normalize_gcd);
285 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
286 ctx->normalize_gcd, total);
288 return bmap;
291 /* Remove any common factor in numerator and denominator of a div expression,
292 * not taking into account the constant term.
293 * That is, look for any div of the form
295 * floor((a + m f(x))/(m d))
297 * and replace it by
299 * floor((floor(a/m) + f(x))/d)
301 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
302 * and can therefore not influence the result of the floor.
304 static __isl_give isl_basic_map *normalize_div_expressions(
305 __isl_take isl_basic_map *bmap)
307 int i;
309 if (!bmap)
310 return NULL;
311 if (bmap->n_div == 0)
312 return bmap;
314 for (i = 0; i < bmap->n_div; ++i)
315 bmap = normalize_div_expression(bmap, i);
317 return bmap;
320 /* Assumes divs have been ordered if keep_divs is set.
322 static __isl_give isl_basic_map *eliminate_var_using_equality(
323 __isl_take isl_basic_map *bmap,
324 unsigned pos, isl_int *eq, int keep_divs, int *progress)
326 isl_size total;
327 isl_size v_div;
328 int k;
329 int last_div;
330 isl_ctx *ctx;
332 total = isl_basic_map_dim(bmap, isl_dim_all);
333 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
334 if (total < 0 || v_div < 0)
335 return isl_basic_map_free(bmap);
336 ctx = isl_basic_map_get_ctx(bmap);
337 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
338 for (k = 0; k < bmap->n_eq; ++k) {
339 if (bmap->eq[k] == eq)
340 continue;
341 if (isl_int_is_zero(bmap->eq[k][1+pos]))
342 continue;
343 if (progress)
344 *progress = 1;
345 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
346 isl_seq_normalize(ctx, bmap->eq[k], 1 + total);
349 for (k = 0; k < bmap->n_ineq; ++k) {
350 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
351 continue;
352 if (progress)
353 *progress = 1;
354 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
355 isl_seq_normalize(ctx, bmap->ineq[k], 1 + total);
356 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
357 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
358 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
359 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
362 for (k = 0; k < bmap->n_div; ++k) {
363 if (isl_int_is_zero(bmap->div[k][0]))
364 continue;
365 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
366 continue;
367 if (progress)
368 *progress = 1;
369 /* We need to be careful about circular definitions,
370 * so for now we just remove the definition of div k
371 * if the equality contains any divs.
372 * If keep_divs is set, then the divs have been ordered
373 * and we can keep the definition as long as the result
374 * is still ordered.
376 if (last_div == -1 || (keep_divs && last_div < k)) {
377 isl_seq_elim(bmap->div[k]+1, eq,
378 1+pos, 1+total, &bmap->div[k][0]);
379 bmap = normalize_div_expression(bmap, k);
380 if (!bmap)
381 return NULL;
382 } else
383 isl_seq_clr(bmap->div[k], 1 + total);
386 return bmap;
389 /* Assumes divs have been ordered if keep_divs is set.
391 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
392 isl_int *eq, unsigned div, int keep_divs)
394 isl_size v_div;
395 unsigned pos;
397 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
398 if (v_div < 0)
399 return isl_basic_map_free(bmap);
400 pos = v_div + div;
401 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
403 bmap = isl_basic_map_drop_div(bmap, div);
405 return bmap;
408 /* Check if elimination of div "div" using equality "eq" would not
409 * result in a div depending on a later div.
411 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
412 unsigned div)
414 int k;
415 int last_div;
416 isl_size v_div;
417 unsigned pos;
419 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
420 if (v_div < 0)
421 return isl_bool_error;
422 pos = v_div + div;
424 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
425 if (last_div < 0 || last_div <= div)
426 return isl_bool_true;
428 for (k = 0; k <= last_div; ++k) {
429 if (isl_int_is_zero(bmap->div[k][0]))
430 continue;
431 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
432 return isl_bool_false;
435 return isl_bool_true;
438 /* Eliminate divs based on equalities
440 static __isl_give isl_basic_map *eliminate_divs_eq(
441 __isl_take isl_basic_map *bmap, int *progress)
443 int d;
444 int i;
445 int modified = 0;
446 unsigned off;
448 bmap = isl_basic_map_order_divs(bmap);
450 if (!bmap)
451 return NULL;
453 off = isl_basic_map_offset(bmap, isl_dim_div);
455 for (d = bmap->n_div - 1; d >= 0 ; --d) {
456 for (i = 0; i < bmap->n_eq; ++i) {
457 isl_bool ok;
459 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
460 !isl_int_is_negone(bmap->eq[i][off + d]))
461 continue;
462 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
463 if (ok < 0)
464 return isl_basic_map_free(bmap);
465 if (!ok)
466 continue;
467 modified = 1;
468 *progress = 1;
469 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
470 if (isl_basic_map_drop_equality(bmap, i) < 0)
471 return isl_basic_map_free(bmap);
472 break;
475 if (modified)
476 return eliminate_divs_eq(bmap, progress);
477 return bmap;
480 /* Eliminate divs based on inequalities
482 static __isl_give isl_basic_map *eliminate_divs_ineq(
483 __isl_take isl_basic_map *bmap, int *progress)
485 int d;
486 int i;
487 unsigned off;
488 struct isl_ctx *ctx;
490 if (!bmap)
491 return NULL;
493 ctx = bmap->ctx;
494 off = isl_basic_map_offset(bmap, isl_dim_div);
496 for (d = bmap->n_div - 1; d >= 0 ; --d) {
497 for (i = 0; i < bmap->n_eq; ++i)
498 if (!isl_int_is_zero(bmap->eq[i][off + d]))
499 break;
500 if (i < bmap->n_eq)
501 continue;
502 for (i = 0; i < bmap->n_ineq; ++i)
503 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
504 break;
505 if (i < bmap->n_ineq)
506 continue;
507 *progress = 1;
508 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
509 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
510 break;
511 bmap = isl_basic_map_drop_div(bmap, d);
512 if (!bmap)
513 break;
515 return bmap;
518 /* Does the equality constraint at position "eq" in "bmap" involve
519 * any local variables in the range [first, first + n)
520 * that are not marked as having an explicit representation?
522 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
523 int eq, unsigned first, unsigned n)
525 unsigned o_div;
526 int i;
528 if (!bmap)
529 return isl_bool_error;
531 o_div = isl_basic_map_offset(bmap, isl_dim_div);
532 for (i = 0; i < n; ++i) {
533 isl_bool unknown;
535 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
536 continue;
537 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
538 if (unknown < 0)
539 return isl_bool_error;
540 if (unknown)
541 return isl_bool_true;
544 return isl_bool_false;
547 /* The last local variable involved in the equality constraint
548 * at position "eq" in "bmap" is the local variable at position "div".
549 * It can therefore be used to extract an explicit representation
550 * for that variable.
551 * Do so unless the local variable already has an explicit representation or
552 * the explicit representation would involve any other local variables
553 * that in turn do not have an explicit representation.
554 * An equality constraint involving local variables without an explicit
555 * representation can be used in isl_basic_map_drop_redundant_divs
556 * to separate out an independent local variable. Introducing
557 * an explicit representation here would block this transformation,
558 * while the partial explicit representation in itself is not very useful.
559 * Set *progress if anything is changed.
561 * The equality constraint is of the form
563 * f(x) + n e >= 0
565 * with n a positive number. The explicit representation derived from
566 * this constraint is
568 * floor((-f(x))/n)
570 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
571 int div, int eq, int *progress)
573 isl_size total;
574 unsigned o_div;
575 isl_bool involves;
577 if (!bmap)
578 return NULL;
580 if (!isl_int_is_zero(bmap->div[div][0]))
581 return bmap;
583 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
584 if (involves < 0)
585 return isl_basic_map_free(bmap);
586 if (involves)
587 return bmap;
589 total = isl_basic_map_dim(bmap, isl_dim_all);
590 if (total < 0)
591 return isl_basic_map_free(bmap);
592 o_div = isl_basic_map_offset(bmap, isl_dim_div);
593 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
594 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
595 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
596 if (progress)
597 *progress = 1;
599 return bmap;
602 /* Perform fangcheng (Gaussian elimination) on the equality
603 * constraints of "bmap".
604 * That is, put them into row-echelon form, starting from the last column
605 * backward and use them to eliminate the corresponding coefficients
606 * from all constraints.
608 * If "progress" is not NULL, then it gets set if the elimination
609 * results in any changes.
610 * The elimination process may result in some equality constraints
611 * getting interchanged or removed.
612 * If "swap" or "drop" are not NULL, then they get called when
613 * two equality constraints get interchanged or
614 * when a number of final equality constraints get removed.
615 * As a special case, if the input turns out to be empty,
616 * then drop gets called with the number of removed equality
617 * constraints set to the total number of equality constraints.
618 * If "swap" or "drop" are not NULL, then the local variables (if any)
619 * are assumed to be in a valid order.
621 __isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
622 int *progress,
623 isl_stat (*swap)(unsigned a, unsigned b, void *user),
624 isl_stat (*drop)(unsigned n, void *user), void *user)
626 int k;
627 int done;
628 int last_var;
629 unsigned total_var;
630 isl_size total;
631 unsigned n_drop;
633 if (!swap && !drop)
634 bmap = isl_basic_map_order_divs(bmap);
636 total = isl_basic_map_dim(bmap, isl_dim_all);
637 if (total < 0)
638 return isl_basic_map_free(bmap);
640 total_var = total - bmap->n_div;
642 last_var = total - 1;
643 for (done = 0; done < bmap->n_eq; ++done) {
644 for (; last_var >= 0; --last_var) {
645 for (k = done; k < bmap->n_eq; ++k)
646 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
647 break;
648 if (k < bmap->n_eq)
649 break;
651 if (last_var < 0)
652 break;
653 if (k != done) {
654 swap_equality(bmap, k, done);
655 if (swap && swap(k, done, user) < 0)
656 return isl_basic_map_free(bmap);
658 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
659 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
661 bmap = eliminate_var_using_equality(bmap, last_var,
662 bmap->eq[done], 1, progress);
664 if (last_var >= total_var)
665 bmap = set_div_from_eq(bmap, last_var - total_var,
666 done, progress);
667 if (!bmap)
668 return NULL;
670 if (done == bmap->n_eq)
671 return bmap;
672 for (k = done; k < bmap->n_eq; ++k) {
673 if (isl_int_is_zero(bmap->eq[k][0]))
674 continue;
675 if (drop && drop(bmap->n_eq, user) < 0)
676 return isl_basic_map_free(bmap);
677 return isl_basic_map_set_to_empty(bmap);
679 n_drop = bmap->n_eq - done;
680 bmap = isl_basic_map_free_equality(bmap, n_drop);
681 if (drop && drop(n_drop, user) < 0)
682 return isl_basic_map_free(bmap);
683 return bmap;
686 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
687 int *progress)
689 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
692 __isl_give isl_basic_set *isl_basic_set_gauss(
693 __isl_take isl_basic_set *bset, int *progress)
695 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
696 progress));
700 static unsigned int round_up(unsigned int v)
702 int old_v = v;
704 while (v) {
705 old_v = v;
706 v ^= v & -v;
708 return old_v << 1;
711 /* Hash table of inequalities in a basic map.
712 * "index" is an array of addresses of inequalities in the basic map, some
713 * of which are NULL. The inequalities are hashed on the coefficients
714 * except the constant term.
715 * "size" is the number of elements in the array and is always a power of two
716 * "bits" is the number of bits need to represent an index into the array.
717 * "total" is the total dimension of the basic map.
719 struct isl_constraint_index {
720 unsigned int size;
721 int bits;
722 isl_int ***index;
723 isl_size total;
726 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
728 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
729 __isl_keep isl_basic_map *bmap)
731 isl_ctx *ctx;
733 ci->index = NULL;
734 if (!bmap)
735 return isl_stat_error;
736 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
737 if (ci->total < 0)
738 return isl_stat_error;
739 if (bmap->n_ineq == 0)
740 return isl_stat_ok;
741 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
742 ci->bits = ffs(ci->size) - 1;
743 ctx = isl_basic_map_get_ctx(bmap);
744 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
745 if (!ci->index)
746 return isl_stat_error;
748 return isl_stat_ok;
751 /* Free the memory allocated by create_constraint_index.
753 static void constraint_index_free(struct isl_constraint_index *ci)
755 free(ci->index);
758 /* Return the position in ci->index that contains the address of
759 * an inequality that is equal to *ineq up to the constant term,
760 * provided this address is not identical to "ineq".
761 * If there is no such inequality, then return the position where
762 * such an inequality should be inserted.
764 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
766 int h;
767 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
768 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
769 if (ineq != ci->index[h] &&
770 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
771 break;
772 return h;
775 /* Return the position in ci->index that contains the address of
776 * an inequality that is equal to the k'th inequality of "bmap"
777 * up to the constant term, provided it does not point to the very
778 * same inequality.
779 * If there is no such inequality, then return the position where
780 * such an inequality should be inserted.
782 static int hash_index(struct isl_constraint_index *ci,
783 __isl_keep isl_basic_map *bmap, int k)
785 return hash_index_ineq(ci, &bmap->ineq[k]);
788 static int set_hash_index(struct isl_constraint_index *ci,
789 __isl_keep isl_basic_set *bset, int k)
791 return hash_index(ci, bset, k);
794 /* Fill in the "ci" data structure with the inequalities of "bset".
796 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
797 __isl_keep isl_basic_set *bset)
799 int k, h;
801 if (create_constraint_index(ci, bset) < 0)
802 return isl_stat_error;
804 for (k = 0; k < bset->n_ineq; ++k) {
805 h = set_hash_index(ci, bset, k);
806 ci->index[h] = &bset->ineq[k];
809 return isl_stat_ok;
812 /* Is the inequality ineq (obviously) redundant with respect
813 * to the constraints in "ci"?
815 * Look for an inequality in "ci" with the same coefficients and then
816 * check if the contant term of "ineq" is greater than or equal
817 * to the constant term of that inequality. If so, "ineq" is clearly
818 * redundant.
820 * Note that hash_index_ineq ignores a stored constraint if it has
821 * the same address as the passed inequality. It is ok to pass
822 * the address of a local variable here since it will never be
823 * the same as the address of a constraint in "ci".
825 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
826 isl_int *ineq)
828 int h;
830 h = hash_index_ineq(ci, &ineq);
831 if (!ci->index[h])
832 return isl_bool_false;
833 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
836 /* If we can eliminate more than one div, then we need to make
837 * sure we do it from last div to first div, in order not to
838 * change the position of the other divs that still need to
839 * be removed.
841 static __isl_give isl_basic_map *remove_duplicate_divs(
842 __isl_take isl_basic_map *bmap, int *progress)
844 unsigned int size;
845 int *index;
846 int *elim_for;
847 int k, l, h;
848 int bits;
849 struct isl_blk eq;
850 isl_size v_div;
851 unsigned total;
852 struct isl_ctx *ctx;
854 bmap = isl_basic_map_order_divs(bmap);
855 if (!bmap || bmap->n_div <= 1)
856 return bmap;
858 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
859 if (v_div < 0)
860 return isl_basic_map_free(bmap);
861 total = v_div + bmap->n_div;
863 ctx = bmap->ctx;
864 for (k = bmap->n_div - 1; k >= 0; --k)
865 if (!isl_int_is_zero(bmap->div[k][0]))
866 break;
867 if (k <= 0)
868 return bmap;
870 size = round_up(4 * bmap->n_div / 3 - 1);
871 if (size == 0)
872 return bmap;
873 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
874 bits = ffs(size) - 1;
875 index = isl_calloc_array(ctx, int, size);
876 if (!elim_for || !index)
877 goto out;
878 eq = isl_blk_alloc(ctx, 1+total);
879 if (isl_blk_is_error(eq))
880 goto out;
882 isl_seq_clr(eq.data, 1+total);
883 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
884 for (--k; k >= 0; --k) {
885 uint32_t hash;
887 if (isl_int_is_zero(bmap->div[k][0]))
888 continue;
890 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
891 for (h = hash; index[h]; h = (h+1) % size)
892 if (isl_seq_eq(bmap->div[k],
893 bmap->div[index[h]-1], 2+total))
894 break;
895 if (index[h]) {
896 *progress = 1;
897 l = index[h] - 1;
898 elim_for[l] = k + 1;
900 index[h] = k+1;
902 for (l = bmap->n_div - 1; l >= 0; --l) {
903 if (!elim_for[l])
904 continue;
905 k = elim_for[l] - 1;
906 isl_int_set_si(eq.data[1 + v_div + k], -1);
907 isl_int_set_si(eq.data[1 + v_div + l], 1);
908 bmap = eliminate_div(bmap, eq.data, l, 1);
909 if (!bmap)
910 break;
911 isl_int_set_si(eq.data[1 + v_div + k], 0);
912 isl_int_set_si(eq.data[1 + v_div + l], 0);
915 isl_blk_free(ctx, eq);
916 out:
917 free(index);
918 free(elim_for);
919 return bmap;
922 static int n_pure_div_eq(__isl_keep isl_basic_map *bmap)
924 int i, j;
925 isl_size v_div;
927 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
928 if (v_div < 0)
929 return -1;
930 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
931 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
932 --j;
933 if (j < 0)
934 break;
935 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
936 return 0;
938 return i;
941 /* Normalize divs that appear in equalities.
943 * In particular, we assume that bmap contains some equalities
944 * of the form
946 * a x = m * e_i
948 * and we want to replace the set of e_i by a minimal set and
949 * such that the new e_i have a canonical representation in terms
950 * of the vector x.
951 * If any of the equalities involves more than one divs, then
952 * we currently simply bail out.
954 * Let us first additionally assume that all equalities involve
955 * a div. The equalities then express modulo constraints on the
956 * remaining variables and we can use "parameter compression"
957 * to find a minimal set of constraints. The result is a transformation
959 * x = T(x') = x_0 + G x'
961 * with G a lower-triangular matrix with all elements below the diagonal
962 * non-negative and smaller than the diagonal element on the same row.
963 * We first normalize x_0 by making the same property hold in the affine
964 * T matrix.
965 * The rows i of G with a 1 on the diagonal do not impose any modulo
966 * constraint and simply express x_i = x'_i.
967 * For each of the remaining rows i, we introduce a div and a corresponding
968 * equality. In particular
970 * g_ii e_j = x_i - g_i(x')
972 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
973 * corresponding div (if g_kk != 1).
975 * If there are any equalities not involving any div, then we
976 * first apply a variable compression on the variables x:
978 * x = C x'' x'' = C_2 x
980 * and perform the above parameter compression on A C instead of on A.
981 * The resulting compression is then of the form
983 * x'' = T(x') = x_0 + G x'
985 * and in constructing the new divs and the corresponding equalities,
986 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
987 * by the corresponding row from C_2.
989 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
990 int *progress)
992 int i, j, k;
993 isl_size v_div;
994 int div_eq;
995 struct isl_mat *B;
996 struct isl_vec *d;
997 struct isl_mat *T = NULL;
998 struct isl_mat *C = NULL;
999 struct isl_mat *C2 = NULL;
1000 isl_int v;
1001 int *pos = NULL;
1002 int dropped, needed;
1004 if (!bmap)
1005 return NULL;
1007 if (bmap->n_div == 0)
1008 return bmap;
1010 if (bmap->n_eq == 0)
1011 return bmap;
1013 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1014 return bmap;
1016 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1017 div_eq = n_pure_div_eq(bmap);
1018 if (v_div < 0 || div_eq < 0)
1019 return isl_basic_map_free(bmap);
1020 if (div_eq == 0)
1021 return bmap;
1023 if (div_eq < bmap->n_eq) {
1024 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1025 bmap->n_eq - div_eq, 0, 1 + v_div);
1026 C = isl_mat_variable_compression(B, &C2);
1027 if (!C || !C2)
1028 goto error;
1029 if (C->n_col == 0) {
1030 bmap = isl_basic_map_set_to_empty(bmap);
1031 isl_mat_free(C);
1032 isl_mat_free(C2);
1033 goto done;
1037 d = isl_vec_alloc(bmap->ctx, div_eq);
1038 if (!d)
1039 goto error;
1040 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1041 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1042 --j;
1043 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1045 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1047 if (C) {
1048 B = isl_mat_product(B, C);
1049 C = NULL;
1052 T = isl_mat_parameter_compression(B, d);
1053 if (!T)
1054 goto error;
1055 if (T->n_col == 0) {
1056 bmap = isl_basic_map_set_to_empty(bmap);
1057 isl_mat_free(C2);
1058 isl_mat_free(T);
1059 goto done;
1061 isl_int_init(v);
1062 for (i = 0; i < T->n_row - 1; ++i) {
1063 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1064 if (isl_int_is_zero(v))
1065 continue;
1066 isl_mat_col_submul(T, 0, v, 1 + i);
1068 isl_int_clear(v);
1069 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1070 if (!pos)
1071 goto error;
1072 /* We have to be careful because dropping equalities may reorder them */
1073 dropped = 0;
1074 for (j = bmap->n_div - 1; j >= 0; --j) {
1075 for (i = 0; i < bmap->n_eq; ++i)
1076 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1077 break;
1078 if (i < bmap->n_eq) {
1079 bmap = isl_basic_map_drop_div(bmap, j);
1080 if (isl_basic_map_drop_equality(bmap, i) < 0)
1081 goto error;
1082 ++dropped;
1085 pos[0] = 0;
1086 needed = 0;
1087 for (i = 1; i < T->n_row; ++i) {
1088 if (isl_int_is_one(T->row[i][i]))
1089 pos[i] = i;
1090 else
1091 needed++;
1093 if (needed > dropped) {
1094 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1095 if (!bmap)
1096 goto error;
1098 for (i = 1; i < T->n_row; ++i) {
1099 if (isl_int_is_one(T->row[i][i]))
1100 continue;
1101 k = isl_basic_map_alloc_div(bmap);
1102 pos[i] = 1 + v_div + k;
1103 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1104 isl_int_set(bmap->div[k][0], T->row[i][i]);
1105 if (C2)
1106 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1107 else
1108 isl_int_set_si(bmap->div[k][1 + i], 1);
1109 for (j = 0; j < i; ++j) {
1110 if (isl_int_is_zero(T->row[i][j]))
1111 continue;
1112 if (pos[j] < T->n_row && C2)
1113 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1114 C2->row[pos[j]], 1 + v_div);
1115 else
1116 isl_int_neg(bmap->div[k][1 + pos[j]],
1117 T->row[i][j]);
1119 j = isl_basic_map_alloc_equality(bmap);
1120 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1121 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1123 free(pos);
1124 isl_mat_free(C2);
1125 isl_mat_free(T);
1127 if (progress)
1128 *progress = 1;
1129 done:
1130 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1132 return bmap;
1133 error:
1134 free(pos);
1135 isl_mat_free(C);
1136 isl_mat_free(C2);
1137 isl_mat_free(T);
1138 isl_basic_map_free(bmap);
1139 return NULL;
1142 static __isl_give isl_basic_map *set_div_from_lower_bound(
1143 __isl_take isl_basic_map *bmap, int div, int ineq)
1145 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1147 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1148 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1149 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1150 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1151 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1153 return bmap;
1156 /* Check whether it is ok to define a div based on an inequality.
1157 * To avoid the introduction of circular definitions of divs, we
1158 * do not allow such a definition if the resulting expression would refer to
1159 * any other undefined divs or if any known div is defined in
1160 * terms of the unknown div.
1162 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1163 int div, int ineq)
1165 int j;
1166 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1168 /* Not defined in terms of unknown divs */
1169 for (j = 0; j < bmap->n_div; ++j) {
1170 if (div == j)
1171 continue;
1172 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1173 continue;
1174 if (isl_int_is_zero(bmap->div[j][0]))
1175 return isl_bool_false;
1178 /* No other div defined in terms of this one => avoid loops */
1179 for (j = 0; j < bmap->n_div; ++j) {
1180 if (div == j)
1181 continue;
1182 if (isl_int_is_zero(bmap->div[j][0]))
1183 continue;
1184 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1185 return isl_bool_false;
1188 return isl_bool_true;
1191 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1192 * be a better expression than the current one?
1194 * If we do not have any expression yet, then any expression would be better.
1195 * Otherwise we check if the last variable involved in the inequality
1196 * (disregarding the div that it would define) is in an earlier position
1197 * than the last variable involved in the current div expression.
1199 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1200 int div, int ineq)
1202 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1203 int last_div;
1204 int last_ineq;
1206 if (isl_int_is_zero(bmap->div[div][0]))
1207 return isl_bool_true;
1209 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1210 bmap->n_div - (div + 1)) >= 0)
1211 return isl_bool_false;
1213 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1214 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1215 total + bmap->n_div);
1217 return last_ineq < last_div;
1220 /* Given two constraints "k" and "l" that are opposite to each other,
1221 * except for the constant term, check if we can use them
1222 * to obtain an expression for one of the hitherto unknown divs or
1223 * a "better" expression for a div for which we already have an expression.
1224 * "sum" is the sum of the constant terms of the constraints.
1225 * If this sum is strictly smaller than the coefficient of one
1226 * of the divs, then this pair can be used to define the div.
1227 * To avoid the introduction of circular definitions of divs, we
1228 * do not use the pair if the resulting expression would refer to
1229 * any other undefined divs or if any known div is defined in
1230 * terms of the unknown div.
1232 static __isl_give isl_basic_map *check_for_div_constraints(
1233 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1234 int *progress)
1236 int i;
1237 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1239 for (i = 0; i < bmap->n_div; ++i) {
1240 isl_bool set_div;
1242 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1243 continue;
1244 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1245 continue;
1246 set_div = better_div_constraint(bmap, i, k);
1247 if (set_div >= 0 && set_div)
1248 set_div = ok_to_set_div_from_bound(bmap, i, k);
1249 if (set_div < 0)
1250 return isl_basic_map_free(bmap);
1251 if (!set_div)
1252 break;
1253 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1254 bmap = set_div_from_lower_bound(bmap, i, k);
1255 else
1256 bmap = set_div_from_lower_bound(bmap, i, l);
1257 if (progress)
1258 *progress = 1;
1259 break;
1261 return bmap;
1264 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1265 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1267 struct isl_constraint_index ci;
1268 int k, l, h;
1269 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1270 isl_int sum;
1272 if (total < 0 || bmap->n_ineq <= 1)
1273 return bmap;
1275 if (create_constraint_index(&ci, bmap) < 0)
1276 return bmap;
1278 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1279 ci.index[h] = &bmap->ineq[0];
1280 for (k = 1; k < bmap->n_ineq; ++k) {
1281 h = hash_index(&ci, bmap, k);
1282 if (!ci.index[h]) {
1283 ci.index[h] = &bmap->ineq[k];
1284 continue;
1286 if (progress)
1287 *progress = 1;
1288 l = ci.index[h] - &bmap->ineq[0];
1289 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1290 swap_inequality(bmap, k, l);
1291 isl_basic_map_drop_inequality(bmap, k);
1292 --k;
1294 isl_int_init(sum);
1295 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1296 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1297 h = hash_index(&ci, bmap, k);
1298 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1299 if (!ci.index[h])
1300 continue;
1301 l = ci.index[h] - &bmap->ineq[0];
1302 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1303 if (isl_int_is_pos(sum)) {
1304 if (detect_divs)
1305 bmap = check_for_div_constraints(bmap, k, l,
1306 sum, progress);
1307 continue;
1309 if (isl_int_is_zero(sum)) {
1310 /* We need to break out of the loop after these
1311 * changes since the contents of the hash
1312 * will no longer be valid.
1313 * Plus, we probably we want to regauss first.
1315 if (progress)
1316 *progress = 1;
1317 isl_basic_map_drop_inequality(bmap, l);
1318 isl_basic_map_inequality_to_equality(bmap, k);
1319 } else
1320 bmap = isl_basic_map_set_to_empty(bmap);
1321 break;
1323 isl_int_clear(sum);
1325 constraint_index_free(&ci);
1326 return bmap;
1329 /* Detect all pairs of inequalities that form an equality.
1331 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1332 * Call it repeatedly while it is making progress.
1334 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1335 __isl_take isl_basic_map *bmap, int *progress)
1337 int duplicate;
1339 do {
1340 duplicate = 0;
1341 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1342 &duplicate, 0);
1343 if (progress && duplicate)
1344 *progress = 1;
1345 } while (duplicate);
1347 return bmap;
1350 /* Given a known integer division "div" that is not integral
1351 * (with denominator 1), eliminate it from the constraints in "bmap"
1352 * where it appears with a (positive or negative) unit coefficient.
1353 * If "progress" is not NULL, then it gets set if the elimination
1354 * results in any changes.
1356 * That is, replace
1358 * floor(e/m) + f >= 0
1360 * by
1362 * e + m f >= 0
1364 * and
1366 * -floor(e/m) + f >= 0
1368 * by
1370 * -e + m f + m - 1 >= 0
1372 * The first conversion is valid because floor(e/m) >= -f is equivalent
1373 * to e/m >= -f because -f is an integral expression.
1374 * The second conversion follows from the fact that
1376 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1379 * Note that one of the div constraints may have been eliminated
1380 * due to being redundant with respect to the constraint that is
1381 * being modified by this function. The modified constraint may
1382 * no longer imply this div constraint, so we add it back to make
1383 * sure we do not lose any information.
1385 static __isl_give isl_basic_map *eliminate_unit_div(
1386 __isl_take isl_basic_map *bmap, int div, int *progress)
1388 int j;
1389 isl_size v_div, dim;
1390 isl_ctx *ctx;
1392 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1393 dim = isl_basic_map_dim(bmap, isl_dim_all);
1394 if (v_div < 0 || dim < 0)
1395 return isl_basic_map_free(bmap);
1397 ctx = isl_basic_map_get_ctx(bmap);
1399 for (j = 0; j < bmap->n_ineq; ++j) {
1400 int s;
1402 if (!isl_int_is_one(bmap->ineq[j][1 + v_div + div]) &&
1403 !isl_int_is_negone(bmap->ineq[j][1 + v_div + div]))
1404 continue;
1406 if (progress)
1407 *progress = 1;
1409 s = isl_int_sgn(bmap->ineq[j][1 + v_div + div]);
1410 isl_int_set_si(bmap->ineq[j][1 + v_div + div], 0);
1411 if (s < 0)
1412 isl_seq_combine(bmap->ineq[j],
1413 ctx->negone, bmap->div[div] + 1,
1414 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1415 else
1416 isl_seq_combine(bmap->ineq[j],
1417 ctx->one, bmap->div[div] + 1,
1418 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1419 if (s < 0) {
1420 isl_int_add(bmap->ineq[j][0],
1421 bmap->ineq[j][0], bmap->div[div][0]);
1422 isl_int_sub_ui(bmap->ineq[j][0],
1423 bmap->ineq[j][0], 1);
1426 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1427 bmap = isl_basic_map_add_div_constraint(bmap, div, s);
1428 if (!bmap)
1429 return NULL;
1432 return bmap;
1435 /* Eliminate selected known divs from constraints where they appear with
1436 * a (positive or negative) unit coefficient.
1437 * In particular, only handle those for which "select" returns isl_bool_true.
1438 * If "progress" is not NULL, then it gets set if the elimination
1439 * results in any changes.
1441 * We skip integral divs, i.e., those with denominator 1, as we would
1442 * risk eliminating the div from the div constraints. We do not need
1443 * to handle those divs here anyway since the div constraints will turn
1444 * out to form an equality and this equality can then be used to eliminate
1445 * the div from all constraints.
1447 static __isl_give isl_basic_map *eliminate_selected_unit_divs(
1448 __isl_take isl_basic_map *bmap,
1449 isl_bool (*select)(__isl_keep isl_basic_map *bmap, int div),
1450 int *progress)
1452 int i;
1454 if (!bmap)
1455 return NULL;
1457 for (i = 0; i < bmap->n_div; ++i) {
1458 isl_bool selected;
1460 if (isl_int_is_zero(bmap->div[i][0]))
1461 continue;
1462 if (isl_int_is_one(bmap->div[i][0]))
1463 continue;
1464 selected = select(bmap, i);
1465 if (selected < 0)
1466 return isl_basic_map_free(bmap);
1467 if (!selected)
1468 continue;
1469 bmap = eliminate_unit_div(bmap, i, progress);
1470 if (!bmap)
1471 return NULL;
1474 return bmap;
1477 /* eliminate_selected_unit_divs callback that selects every
1478 * integer division.
1480 static isl_bool is_any_div(__isl_keep isl_basic_map *bmap, int div)
1482 return isl_bool_true;
1485 /* Eliminate known divs from constraints where they appear with
1486 * a (positive or negative) unit coefficient.
1487 * If "progress" is not NULL, then it gets set if the elimination
1488 * results in any changes.
1490 static __isl_give isl_basic_map *eliminate_unit_divs(
1491 __isl_take isl_basic_map *bmap, int *progress)
1493 return eliminate_selected_unit_divs(bmap, &is_any_div, progress);
1496 /* eliminate_selected_unit_divs callback that selects
1497 * integer divisions that only appear with
1498 * a (positive or negative) unit coefficient
1499 * (outside their div constraints).
1501 static isl_bool is_pure_unit_div(__isl_keep isl_basic_map *bmap, int div)
1503 int i;
1504 isl_size v_div, n_ineq;
1506 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1507 n_ineq = isl_basic_map_n_inequality(bmap);
1508 if (v_div < 0 || n_ineq < 0)
1509 return isl_bool_error;
1511 for (i = 0; i < n_ineq; ++i) {
1512 isl_bool skip;
1514 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
1515 continue;
1516 skip = isl_basic_map_is_div_constraint(bmap,
1517 bmap->ineq[i], div);
1518 if (skip < 0)
1519 return isl_bool_error;
1520 if (skip)
1521 continue;
1522 if (!isl_int_is_one(bmap->ineq[i][1 + v_div + div]) &&
1523 !isl_int_is_negone(bmap->ineq[i][1 + v_div + div]))
1524 return isl_bool_false;
1527 return isl_bool_true;
1530 /* Eliminate known divs from constraints where they appear with
1531 * a (positive or negative) unit coefficient,
1532 * but only if they do not appear in any other constraints
1533 * (other than the div constraints).
1535 __isl_give isl_basic_map *isl_basic_map_eliminate_pure_unit_divs(
1536 __isl_take isl_basic_map *bmap)
1538 return eliminate_selected_unit_divs(bmap, &is_pure_unit_div, NULL);
1541 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1543 int progress = 1;
1544 if (!bmap)
1545 return NULL;
1546 while (progress) {
1547 isl_bool empty;
1549 progress = 0;
1550 empty = isl_basic_map_plain_is_empty(bmap);
1551 if (empty < 0)
1552 return isl_basic_map_free(bmap);
1553 if (empty)
1554 break;
1555 bmap = isl_basic_map_normalize_constraints(bmap);
1556 bmap = reduce_div_coefficients(bmap);
1557 bmap = normalize_div_expressions(bmap);
1558 bmap = remove_duplicate_divs(bmap, &progress);
1559 bmap = eliminate_unit_divs(bmap, &progress);
1560 bmap = eliminate_divs_eq(bmap, &progress);
1561 bmap = eliminate_divs_ineq(bmap, &progress);
1562 bmap = isl_basic_map_gauss(bmap, &progress);
1563 /* requires equalities in normal form */
1564 bmap = normalize_divs(bmap, &progress);
1565 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1566 &progress, 1);
1568 return bmap;
1571 __isl_give isl_basic_set *isl_basic_set_simplify(
1572 __isl_take isl_basic_set *bset)
1574 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1578 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1579 isl_int *constraint, unsigned div)
1581 unsigned pos;
1583 if (!bmap)
1584 return isl_bool_error;
1586 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1588 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1589 int neg;
1590 isl_int_sub(bmap->div[div][1],
1591 bmap->div[div][1], bmap->div[div][0]);
1592 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1593 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1594 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1595 isl_int_add(bmap->div[div][1],
1596 bmap->div[div][1], bmap->div[div][0]);
1597 if (!neg)
1598 return isl_bool_false;
1599 if (isl_seq_first_non_zero(constraint+pos+1,
1600 bmap->n_div-div-1) != -1)
1601 return isl_bool_false;
1602 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1603 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1604 return isl_bool_false;
1605 if (isl_seq_first_non_zero(constraint+pos+1,
1606 bmap->n_div-div-1) != -1)
1607 return isl_bool_false;
1608 } else
1609 return isl_bool_false;
1611 return isl_bool_true;
1614 /* If the only constraints a div d=floor(f/m)
1615 * appears in are its two defining constraints
1617 * f - m d >=0
1618 * -(f - (m - 1)) + m d >= 0
1620 * then it can safely be removed.
1622 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1624 int i;
1625 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1626 unsigned pos = 1 + v_div + div;
1628 if (v_div < 0)
1629 return isl_bool_error;
1631 for (i = 0; i < bmap->n_eq; ++i)
1632 if (!isl_int_is_zero(bmap->eq[i][pos]))
1633 return isl_bool_false;
1635 for (i = 0; i < bmap->n_ineq; ++i) {
1636 isl_bool red;
1638 if (isl_int_is_zero(bmap->ineq[i][pos]))
1639 continue;
1640 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1641 if (red < 0 || !red)
1642 return red;
1645 for (i = 0; i < bmap->n_div; ++i) {
1646 if (isl_int_is_zero(bmap->div[i][0]))
1647 continue;
1648 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1649 return isl_bool_false;
1652 return isl_bool_true;
1656 * Remove divs that don't occur in any of the constraints or other divs.
1657 * These can arise when dropping constraints from a basic map or
1658 * when the divs of a basic map have been temporarily aligned
1659 * with the divs of another basic map.
1661 static __isl_give isl_basic_map *remove_redundant_divs(
1662 __isl_take isl_basic_map *bmap)
1664 int i;
1665 isl_size v_div;
1667 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1668 if (v_div < 0)
1669 return isl_basic_map_free(bmap);
1671 for (i = bmap->n_div-1; i >= 0; --i) {
1672 isl_bool redundant;
1674 redundant = div_is_redundant(bmap, i);
1675 if (redundant < 0)
1676 return isl_basic_map_free(bmap);
1677 if (!redundant)
1678 continue;
1679 bmap = isl_basic_map_drop_constraints_involving(bmap,
1680 v_div + i, 1);
1681 bmap = isl_basic_map_drop_div(bmap, i);
1683 return bmap;
1686 /* Mark "bmap" as final, without checking for obviously redundant
1687 * integer divisions. This function should be used when "bmap"
1688 * is known not to involve any such integer divisions.
1690 __isl_give isl_basic_map *isl_basic_map_mark_final(
1691 __isl_take isl_basic_map *bmap)
1693 if (!bmap)
1694 return NULL;
1695 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1696 return bmap;
1699 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1701 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1703 bmap = remove_redundant_divs(bmap);
1704 bmap = isl_basic_map_mark_final(bmap);
1705 return bmap;
1708 __isl_give isl_basic_set *isl_basic_set_finalize(
1709 __isl_take isl_basic_set *bset)
1711 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1714 /* Remove definition of any div that is defined in terms of the given variable.
1715 * The div itself is not removed. Functions such as
1716 * eliminate_divs_ineq depend on the other divs remaining in place.
1718 static __isl_give isl_basic_map *remove_dependent_vars(
1719 __isl_take isl_basic_map *bmap, int pos)
1721 int i;
1723 if (!bmap)
1724 return NULL;
1726 for (i = 0; i < bmap->n_div; ++i) {
1727 if (isl_int_is_zero(bmap->div[i][0]))
1728 continue;
1729 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1730 continue;
1731 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1732 if (!bmap)
1733 return NULL;
1735 return bmap;
1738 /* Eliminate the specified variables from the constraints using
1739 * Fourier-Motzkin. The variables themselves are not removed.
1741 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1742 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1744 int d;
1745 int i, j, k;
1746 isl_size total;
1747 int need_gauss = 0;
1749 if (n == 0)
1750 return bmap;
1751 total = isl_basic_map_dim(bmap, isl_dim_all);
1752 if (total < 0)
1753 return isl_basic_map_free(bmap);
1755 bmap = isl_basic_map_cow(bmap);
1756 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1757 bmap = remove_dependent_vars(bmap, d);
1758 if (!bmap)
1759 return NULL;
1761 for (d = pos + n - 1;
1762 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1763 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1764 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1765 int n_lower, n_upper;
1766 if (!bmap)
1767 return NULL;
1768 for (i = 0; i < bmap->n_eq; ++i) {
1769 if (isl_int_is_zero(bmap->eq[i][1+d]))
1770 continue;
1771 bmap = eliminate_var_using_equality(bmap, d,
1772 bmap->eq[i], 0, NULL);
1773 if (isl_basic_map_drop_equality(bmap, i) < 0)
1774 return isl_basic_map_free(bmap);
1775 need_gauss = 1;
1776 break;
1778 if (i < bmap->n_eq)
1779 continue;
1780 n_lower = 0;
1781 n_upper = 0;
1782 for (i = 0; i < bmap->n_ineq; ++i) {
1783 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1784 n_lower++;
1785 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1786 n_upper++;
1788 bmap = isl_basic_map_extend_constraints(bmap,
1789 0, n_lower * n_upper);
1790 if (!bmap)
1791 goto error;
1792 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1793 int last;
1794 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1795 continue;
1796 last = -1;
1797 for (j = 0; j < i; ++j) {
1798 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1799 continue;
1800 last = j;
1801 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1802 isl_int_sgn(bmap->ineq[j][1+d]))
1803 continue;
1804 k = isl_basic_map_alloc_inequality(bmap);
1805 if (k < 0)
1806 goto error;
1807 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1808 1+total);
1809 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1810 1+d, 1+total, NULL);
1812 isl_basic_map_drop_inequality(bmap, i);
1813 i = last + 1;
1815 if (n_lower > 0 && n_upper > 0) {
1816 bmap = isl_basic_map_normalize_constraints(bmap);
1817 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1818 NULL, 0);
1819 bmap = isl_basic_map_gauss(bmap, NULL);
1820 bmap = isl_basic_map_remove_redundancies(bmap);
1821 need_gauss = 0;
1822 if (!bmap)
1823 goto error;
1824 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1825 break;
1828 if (need_gauss)
1829 bmap = isl_basic_map_gauss(bmap, NULL);
1830 return bmap;
1831 error:
1832 isl_basic_map_free(bmap);
1833 return NULL;
1836 __isl_give isl_basic_set *isl_basic_set_eliminate_vars(
1837 __isl_take isl_basic_set *bset, unsigned pos, unsigned n)
1839 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1840 pos, n));
1843 /* Eliminate the specified n dimensions starting at first from the
1844 * constraints, without removing the dimensions from the space.
1845 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1846 * Otherwise, they are projected out and the original space is restored.
1848 __isl_give isl_basic_map *isl_basic_map_eliminate(
1849 __isl_take isl_basic_map *bmap,
1850 enum isl_dim_type type, unsigned first, unsigned n)
1852 isl_space *space;
1854 if (!bmap)
1855 return NULL;
1856 if (n == 0)
1857 return bmap;
1859 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1860 return isl_basic_map_free(bmap);
1862 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1863 first += isl_basic_map_offset(bmap, type) - 1;
1864 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1865 return isl_basic_map_finalize(bmap);
1868 space = isl_basic_map_get_space(bmap);
1869 bmap = isl_basic_map_project_out(bmap, type, first, n);
1870 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1871 bmap = isl_basic_map_reset_space(bmap, space);
1872 return bmap;
1875 __isl_give isl_basic_set *isl_basic_set_eliminate(
1876 __isl_take isl_basic_set *bset,
1877 enum isl_dim_type type, unsigned first, unsigned n)
1879 return isl_basic_map_eliminate(bset, type, first, n);
1882 /* Remove all constraints from "bmap" that reference any unknown local
1883 * variables (directly or indirectly).
1885 * Dropping all constraints on a local variable will make it redundant,
1886 * so it will get removed implicitly by
1887 * isl_basic_map_drop_constraints_involving_dims. Some other local
1888 * variables may also end up becoming redundant if they only appear
1889 * in constraints together with the unknown local variable.
1890 * Therefore, start over after calling
1891 * isl_basic_map_drop_constraints_involving_dims.
1893 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_unknown_divs(
1894 __isl_take isl_basic_map *bmap)
1896 isl_bool known;
1897 isl_size n_div;
1898 int i, o_div;
1900 known = isl_basic_map_divs_known(bmap);
1901 if (known < 0)
1902 return isl_basic_map_free(bmap);
1903 if (known)
1904 return bmap;
1906 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1907 if (n_div < 0)
1908 return isl_basic_map_free(bmap);
1909 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1911 for (i = 0; i < n_div; ++i) {
1912 known = isl_basic_map_div_is_known(bmap, i);
1913 if (known < 0)
1914 return isl_basic_map_free(bmap);
1915 if (known)
1916 continue;
1917 bmap = remove_dependent_vars(bmap, o_div + i);
1918 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1919 isl_dim_div, i, 1);
1920 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1921 if (n_div < 0)
1922 return isl_basic_map_free(bmap);
1923 i = -1;
1926 return bmap;
1929 /* Remove all constraints from "bset" that reference any unknown local
1930 * variables (directly or indirectly).
1932 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_unknown_divs(
1933 __isl_take isl_basic_set *bset)
1935 isl_basic_map *bmap;
1937 bmap = bset_to_bmap(bset);
1938 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
1939 return bset_from_bmap(bmap);
1942 /* Remove all constraints from "map" that reference any unknown local
1943 * variables (directly or indirectly).
1945 * Since constraints may get dropped from the basic maps,
1946 * they may no longer be disjoint from each other.
1948 __isl_give isl_map *isl_map_drop_constraints_involving_unknown_divs(
1949 __isl_take isl_map *map)
1951 int i;
1952 isl_bool known;
1954 known = isl_map_divs_known(map);
1955 if (known < 0)
1956 return isl_map_free(map);
1957 if (known)
1958 return map;
1960 map = isl_map_cow(map);
1961 if (!map)
1962 return NULL;
1964 for (i = 0; i < map->n; ++i) {
1965 map->p[i] =
1966 isl_basic_map_drop_constraints_involving_unknown_divs(
1967 map->p[i]);
1968 if (!map->p[i])
1969 return isl_map_free(map);
1972 if (map->n > 1)
1973 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1975 return map;
1978 /* Don't assume equalities are in order, because align_divs
1979 * may have changed the order of the divs.
1981 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim,
1982 unsigned len)
1984 int d, i;
1986 for (d = 0; d < len; ++d)
1987 elim[d] = -1;
1988 for (i = 0; i < bmap->n_eq; ++i) {
1989 for (d = len - 1; d >= 0; --d) {
1990 if (isl_int_is_zero(bmap->eq[i][1+d]))
1991 continue;
1992 elim[d] = i;
1993 break;
1998 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1999 int *elim, unsigned len)
2001 compute_elimination_index(bset_to_bmap(bset), elim, len);
2004 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2005 __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
2007 int d;
2008 int copied = 0;
2010 for (d = total - 1; d >= 0; --d) {
2011 if (isl_int_is_zero(src[1+d]))
2012 continue;
2013 if (elim[d] == -1)
2014 continue;
2015 if (!copied) {
2016 isl_seq_cpy(dst, src, 1 + total);
2017 copied = 1;
2019 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2021 return copied;
2024 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2025 __isl_keep isl_basic_set *bset, int *elim, unsigned total)
2027 return reduced_using_equalities(dst, src,
2028 bset_to_bmap(bset), elim, total);
2031 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
2032 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2034 int i;
2035 int *elim;
2036 isl_size dim;
2038 if (!bset || !context)
2039 goto error;
2041 if (context->n_eq == 0) {
2042 isl_basic_set_free(context);
2043 return bset;
2046 bset = isl_basic_set_cow(bset);
2047 dim = isl_basic_set_dim(bset, isl_dim_set);
2048 if (dim < 0)
2049 goto error;
2051 elim = isl_alloc_array(bset->ctx, int, dim);
2052 if (!elim)
2053 goto error;
2054 set_compute_elimination_index(context, elim, dim);
2055 for (i = 0; i < bset->n_eq; ++i)
2056 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2057 context, elim, dim);
2058 for (i = 0; i < bset->n_ineq; ++i)
2059 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2060 context, elim, dim);
2061 isl_basic_set_free(context);
2062 free(elim);
2063 bset = isl_basic_set_simplify(bset);
2064 bset = isl_basic_set_finalize(bset);
2065 return bset;
2066 error:
2067 isl_basic_set_free(bset);
2068 isl_basic_set_free(context);
2069 return NULL;
2072 /* For each inequality in "ineq" that is a shifted (more relaxed)
2073 * copy of an inequality in "context", mark the corresponding entry
2074 * in "row" with -1.
2075 * If an inequality only has a non-negative constant term, then
2076 * mark it as well.
2078 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2079 __isl_keep isl_basic_set *context, int *row)
2081 struct isl_constraint_index ci;
2082 isl_size n_ineq, cols;
2083 unsigned total;
2084 int k;
2086 if (!ineq || !context)
2087 return isl_stat_error;
2088 if (context->n_ineq == 0)
2089 return isl_stat_ok;
2090 if (setup_constraint_index(&ci, context) < 0)
2091 return isl_stat_error;
2093 n_ineq = isl_mat_rows(ineq);
2094 cols = isl_mat_cols(ineq);
2095 if (n_ineq < 0 || cols < 0)
2096 return isl_stat_error;
2097 total = cols - 1;
2098 for (k = 0; k < n_ineq; ++k) {
2099 int l;
2100 isl_bool redundant;
2102 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2103 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2104 row[k] = -1;
2105 continue;
2107 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2108 if (redundant < 0)
2109 goto error;
2110 if (!redundant)
2111 continue;
2112 row[k] = -1;
2114 constraint_index_free(&ci);
2115 return isl_stat_ok;
2116 error:
2117 constraint_index_free(&ci);
2118 return isl_stat_error;
2121 static __isl_give isl_basic_set *remove_shifted_constraints(
2122 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
2124 struct isl_constraint_index ci;
2125 int k;
2127 if (!bset || !context)
2128 return bset;
2130 if (context->n_ineq == 0)
2131 return bset;
2132 if (setup_constraint_index(&ci, context) < 0)
2133 return bset;
2135 for (k = 0; k < bset->n_ineq; ++k) {
2136 isl_bool redundant;
2138 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2139 if (redundant < 0)
2140 goto error;
2141 if (!redundant)
2142 continue;
2143 bset = isl_basic_set_cow(bset);
2144 if (!bset)
2145 goto error;
2146 isl_basic_set_drop_inequality(bset, k);
2147 --k;
2149 constraint_index_free(&ci);
2150 return bset;
2151 error:
2152 constraint_index_free(&ci);
2153 return bset;
2156 /* Remove constraints from "bmap" that are identical to constraints
2157 * in "context" or that are more relaxed (greater constant term).
2159 * We perform the test for shifted copies on the pure constraints
2160 * in remove_shifted_constraints.
2162 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2163 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2165 isl_basic_set *bset, *bset_context;
2167 if (!bmap || !context)
2168 goto error;
2170 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2171 isl_basic_map_free(context);
2172 return bmap;
2175 bmap = isl_basic_map_order_divs(bmap);
2176 context = isl_basic_map_align_divs(context, bmap);
2177 bmap = isl_basic_map_align_divs(bmap, context);
2179 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2180 bset_context = isl_basic_map_underlying_set(context);
2181 bset = remove_shifted_constraints(bset, bset_context);
2182 isl_basic_set_free(bset_context);
2184 bmap = isl_basic_map_overlying_set(bset, bmap);
2186 return bmap;
2187 error:
2188 isl_basic_map_free(bmap);
2189 isl_basic_map_free(context);
2190 return NULL;
2193 /* Does the (linear part of a) constraint "c" involve any of the "len"
2194 * "relevant" dimensions?
2196 static int is_related(isl_int *c, int len, int *relevant)
2198 int i;
2200 for (i = 0; i < len; ++i) {
2201 if (!relevant[i])
2202 continue;
2203 if (!isl_int_is_zero(c[i]))
2204 return 1;
2207 return 0;
2210 /* Drop constraints from "bmap" that do not involve any of
2211 * the dimensions marked "relevant".
2213 static __isl_give isl_basic_map *drop_unrelated_constraints(
2214 __isl_take isl_basic_map *bmap, int *relevant)
2216 int i;
2217 isl_size dim;
2219 dim = isl_basic_map_dim(bmap, isl_dim_all);
2220 if (dim < 0)
2221 return isl_basic_map_free(bmap);
2222 for (i = 0; i < dim; ++i)
2223 if (!relevant[i])
2224 break;
2225 if (i >= dim)
2226 return bmap;
2228 for (i = bmap->n_eq - 1; i >= 0; --i)
2229 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2230 bmap = isl_basic_map_cow(bmap);
2231 if (isl_basic_map_drop_equality(bmap, i) < 0)
2232 return isl_basic_map_free(bmap);
2235 for (i = bmap->n_ineq - 1; i >= 0; --i)
2236 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2237 bmap = isl_basic_map_cow(bmap);
2238 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2239 return isl_basic_map_free(bmap);
2242 return bmap;
2245 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2247 * In particular, for any variable involved in the constraint,
2248 * find the actual group id from before and replace the group
2249 * of the corresponding variable by the minimal group of all
2250 * the variables involved in the constraint considered so far
2251 * (if this minimum is smaller) or replace the minimum by this group
2252 * (if the minimum is larger).
2254 * At the end, all the variables in "c" will (indirectly) point
2255 * to the minimal of the groups that they referred to originally.
2257 static void update_groups(int dim, int *group, isl_int *c)
2259 int j;
2260 int min = dim;
2262 for (j = 0; j < dim; ++j) {
2263 if (isl_int_is_zero(c[j]))
2264 continue;
2265 while (group[j] >= 0 && group[group[j]] != group[j])
2266 group[j] = group[group[j]];
2267 if (group[j] == min)
2268 continue;
2269 if (group[j] < min) {
2270 if (min >= 0 && min < dim)
2271 group[min] = group[j];
2272 min = group[j];
2273 } else
2274 group[group[j]] = min;
2278 /* Allocate an array of groups of variables, one for each variable
2279 * in "context", initialized to zero.
2281 static int *alloc_groups(__isl_keep isl_basic_set *context)
2283 isl_ctx *ctx;
2284 isl_size dim;
2286 dim = isl_basic_set_dim(context, isl_dim_set);
2287 if (dim < 0)
2288 return NULL;
2289 ctx = isl_basic_set_get_ctx(context);
2290 return isl_calloc_array(ctx, int, dim);
2293 /* Drop constraints from "bmap" that only involve variables that are
2294 * not related to any of the variables marked with a "-1" in "group".
2296 * We construct groups of variables that collect variables that
2297 * (indirectly) appear in some common constraint of "bmap".
2298 * Each group is identified by the first variable in the group,
2299 * except for the special group of variables that was already identified
2300 * in the input as -1 (or are related to those variables).
2301 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2302 * otherwise the group of i is the group of group[i].
2304 * We first initialize groups for the remaining variables.
2305 * Then we iterate over the constraints of "bmap" and update the
2306 * group of the variables in the constraint by the smallest group.
2307 * Finally, we resolve indirect references to groups by running over
2308 * the variables.
2310 * After computing the groups, we drop constraints that do not involve
2311 * any variables in the -1 group.
2313 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2314 __isl_take isl_basic_map *bmap, __isl_take int *group)
2316 isl_size dim;
2317 int i;
2318 int last;
2320 dim = isl_basic_map_dim(bmap, isl_dim_all);
2321 if (dim < 0)
2322 return isl_basic_map_free(bmap);
2324 last = -1;
2325 for (i = 0; i < dim; ++i)
2326 if (group[i] >= 0)
2327 last = group[i] = i;
2328 if (last < 0) {
2329 free(group);
2330 return bmap;
2333 for (i = 0; i < bmap->n_eq; ++i)
2334 update_groups(dim, group, bmap->eq[i] + 1);
2335 for (i = 0; i < bmap->n_ineq; ++i)
2336 update_groups(dim, group, bmap->ineq[i] + 1);
2338 for (i = 0; i < dim; ++i)
2339 if (group[i] >= 0)
2340 group[i] = group[group[i]];
2342 for (i = 0; i < dim; ++i)
2343 group[i] = group[i] == -1;
2345 bmap = drop_unrelated_constraints(bmap, group);
2347 free(group);
2348 return bmap;
2351 /* Drop constraints from "context" that are irrelevant for computing
2352 * the gist of "bset".
2354 * In particular, drop constraints in variables that are not related
2355 * to any of the variables involved in the constraints of "bset"
2356 * in the sense that there is no sequence of constraints that connects them.
2358 * We first mark all variables that appear in "bset" as belonging
2359 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2361 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2362 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2364 int *group;
2365 isl_size dim;
2366 int i, j;
2368 dim = isl_basic_set_dim(bset, isl_dim_set);
2369 if (!context || dim < 0)
2370 return isl_basic_set_free(context);
2372 group = alloc_groups(context);
2374 if (!group)
2375 return isl_basic_set_free(context);
2377 for (i = 0; i < dim; ++i) {
2378 for (j = 0; j < bset->n_eq; ++j)
2379 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2380 break;
2381 if (j < bset->n_eq) {
2382 group[i] = -1;
2383 continue;
2385 for (j = 0; j < bset->n_ineq; ++j)
2386 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2387 break;
2388 if (j < bset->n_ineq)
2389 group[i] = -1;
2392 return isl_basic_map_drop_unrelated_constraints(context, group);
2395 /* Drop constraints from "context" that are irrelevant for computing
2396 * the gist of the inequalities "ineq".
2397 * Inequalities in "ineq" for which the corresponding element of row
2398 * is set to -1 have already been marked for removal and should be ignored.
2400 * In particular, drop constraints in variables that are not related
2401 * to any of the variables involved in "ineq"
2402 * in the sense that there is no sequence of constraints that connects them.
2404 * We first mark all variables that appear in "bset" as belonging
2405 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2407 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2408 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2410 int *group;
2411 isl_size dim;
2412 int i, j;
2413 isl_size n;
2415 dim = isl_basic_set_dim(context, isl_dim_set);
2416 n = isl_mat_rows(ineq);
2417 if (dim < 0 || n < 0)
2418 return isl_basic_set_free(context);
2420 group = alloc_groups(context);
2422 if (!group)
2423 return isl_basic_set_free(context);
2425 for (i = 0; i < dim; ++i) {
2426 for (j = 0; j < n; ++j) {
2427 if (row[j] < 0)
2428 continue;
2429 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2430 break;
2432 if (j < n)
2433 group[i] = -1;
2436 return isl_basic_map_drop_unrelated_constraints(context, group);
2439 /* Do all "n" entries of "row" contain a negative value?
2441 static int all_neg(int *row, int n)
2443 int i;
2445 for (i = 0; i < n; ++i)
2446 if (row[i] >= 0)
2447 return 0;
2449 return 1;
2452 /* Update the inequalities in "bset" based on the information in "row"
2453 * and "tab".
2455 * In particular, the array "row" contains either -1, meaning that
2456 * the corresponding inequality of "bset" is redundant, or the index
2457 * of an inequality in "tab".
2459 * If the row entry is -1, then drop the inequality.
2460 * Otherwise, if the constraint is marked redundant in the tableau,
2461 * then drop the inequality. Similarly, if it is marked as an equality
2462 * in the tableau, then turn the inequality into an equality and
2463 * perform Gaussian elimination.
2465 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2466 __isl_keep int *row, struct isl_tab *tab)
2468 int i;
2469 unsigned n_ineq;
2470 unsigned n_eq;
2471 int found_equality = 0;
2473 if (!bset)
2474 return NULL;
2475 if (tab && tab->empty)
2476 return isl_basic_set_set_to_empty(bset);
2478 n_ineq = bset->n_ineq;
2479 for (i = n_ineq - 1; i >= 0; --i) {
2480 if (row[i] < 0) {
2481 if (isl_basic_set_drop_inequality(bset, i) < 0)
2482 return isl_basic_set_free(bset);
2483 continue;
2485 if (!tab)
2486 continue;
2487 n_eq = tab->n_eq;
2488 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2489 isl_basic_map_inequality_to_equality(bset, i);
2490 found_equality = 1;
2491 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2492 if (isl_basic_set_drop_inequality(bset, i) < 0)
2493 return isl_basic_set_free(bset);
2497 if (found_equality)
2498 bset = isl_basic_set_gauss(bset, NULL);
2499 bset = isl_basic_set_finalize(bset);
2500 return bset;
2503 /* Update the inequalities in "bset" based on the information in "row"
2504 * and "tab" and free all arguments (other than "bset").
2506 static __isl_give isl_basic_set *update_ineq_free(
2507 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2508 __isl_take isl_basic_set *context, __isl_take int *row,
2509 struct isl_tab *tab)
2511 isl_mat_free(ineq);
2512 isl_basic_set_free(context);
2514 bset = update_ineq(bset, row, tab);
2516 free(row);
2517 isl_tab_free(tab);
2518 return bset;
2521 /* Remove all information from bset that is redundant in the context
2522 * of context.
2523 * "ineq" contains the (possibly transformed) inequalities of "bset",
2524 * in the same order.
2525 * The (explicit) equalities of "bset" are assumed to have been taken
2526 * into account by the transformation such that only the inequalities
2527 * are relevant.
2528 * "context" is assumed not to be empty.
2530 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2531 * A value of -1 means that the inequality is obviously redundant and may
2532 * not even appear in "tab".
2534 * We first mark the inequalities of "bset"
2535 * that are obviously redundant with respect to some inequality in "context".
2536 * Then we remove those constraints from "context" that have become
2537 * irrelevant for computing the gist of "bset".
2538 * Note that this removal of constraints cannot be replaced by
2539 * a factorization because factors in "bset" may still be connected
2540 * to each other through constraints in "context".
2542 * If there are any inequalities left, we construct a tableau for
2543 * the context and then add the inequalities of "bset".
2544 * Before adding these inequalities, we freeze all constraints such that
2545 * they won't be considered redundant in terms of the constraints of "bset".
2546 * Then we detect all redundant constraints (among the
2547 * constraints that weren't frozen), first by checking for redundancy in the
2548 * the tableau and then by checking if replacing a constraint by its negation
2549 * would lead to an empty set. This last step is fairly expensive
2550 * and could be optimized by more reuse of the tableau.
2551 * Finally, we update bset according to the results.
2553 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2554 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2556 int i, r;
2557 int *row = NULL;
2558 isl_ctx *ctx;
2559 isl_basic_set *combined = NULL;
2560 struct isl_tab *tab = NULL;
2561 unsigned n_eq, context_ineq;
2563 if (!bset || !ineq || !context)
2564 goto error;
2566 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2567 isl_basic_set_free(context);
2568 isl_mat_free(ineq);
2569 return bset;
2572 ctx = isl_basic_set_get_ctx(context);
2573 row = isl_calloc_array(ctx, int, bset->n_ineq);
2574 if (!row)
2575 goto error;
2577 if (mark_shifted_constraints(ineq, context, row) < 0)
2578 goto error;
2579 if (all_neg(row, bset->n_ineq))
2580 return update_ineq_free(bset, ineq, context, row, NULL);
2582 context = drop_irrelevant_constraints_marked(context, ineq, row);
2583 if (!context)
2584 goto error;
2585 if (isl_basic_set_plain_is_universe(context))
2586 return update_ineq_free(bset, ineq, context, row, NULL);
2588 n_eq = context->n_eq;
2589 context_ineq = context->n_ineq;
2590 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2591 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2592 tab = isl_tab_from_basic_set(combined, 0);
2593 for (i = 0; i < context_ineq; ++i)
2594 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2595 goto error;
2596 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2597 goto error;
2598 r = context_ineq;
2599 for (i = 0; i < bset->n_ineq; ++i) {
2600 if (row[i] < 0)
2601 continue;
2602 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2603 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2604 goto error;
2605 row[i] = r++;
2607 if (isl_tab_detect_implicit_equalities(tab) < 0)
2608 goto error;
2609 if (isl_tab_detect_redundant(tab) < 0)
2610 goto error;
2611 for (i = bset->n_ineq - 1; i >= 0; --i) {
2612 isl_basic_set *test;
2613 int is_empty;
2615 if (row[i] < 0)
2616 continue;
2617 r = row[i];
2618 if (tab->con[n_eq + r].is_redundant)
2619 continue;
2620 test = isl_basic_set_dup(combined);
2621 test = isl_inequality_negate(test, r);
2622 test = isl_basic_set_update_from_tab(test, tab);
2623 is_empty = isl_basic_set_is_empty(test);
2624 isl_basic_set_free(test);
2625 if (is_empty < 0)
2626 goto error;
2627 if (is_empty)
2628 tab->con[n_eq + r].is_redundant = 1;
2630 bset = update_ineq_free(bset, ineq, context, row, tab);
2631 if (bset) {
2632 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2633 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2636 isl_basic_set_free(combined);
2637 return bset;
2638 error:
2639 free(row);
2640 isl_mat_free(ineq);
2641 isl_tab_free(tab);
2642 isl_basic_set_free(combined);
2643 isl_basic_set_free(context);
2644 isl_basic_set_free(bset);
2645 return NULL;
2648 /* Extract the inequalities of "bset" as an isl_mat.
2650 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2652 isl_size total;
2653 isl_ctx *ctx;
2654 isl_mat *ineq;
2656 total = isl_basic_set_dim(bset, isl_dim_all);
2657 if (total < 0)
2658 return NULL;
2660 ctx = isl_basic_set_get_ctx(bset);
2661 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2662 0, 1 + total);
2664 return ineq;
2667 /* Remove all information from "bset" that is redundant in the context
2668 * of "context", for the case where both "bset" and "context" are
2669 * full-dimensional.
2671 static __isl_give isl_basic_set *uset_gist_uncompressed(
2672 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2674 isl_mat *ineq;
2676 ineq = extract_ineq(bset);
2677 return uset_gist_full(bset, ineq, context);
2680 /* Replace "bset" by an empty basic set in the same space.
2682 static __isl_give isl_basic_set *replace_by_empty(
2683 __isl_take isl_basic_set *bset)
2685 isl_space *space;
2687 space = isl_basic_set_get_space(bset);
2688 isl_basic_set_free(bset);
2689 return isl_basic_set_empty(space);
2692 /* Remove all information from "bset" that is redundant in the context
2693 * of "context", for the case where the combined equalities of
2694 * "bset" and "context" allow for a compression that can be obtained
2695 * by preapplication of "T".
2696 * If the compression of "context" is empty, meaning that "bset" and
2697 * "context" do not intersect, then return the empty set.
2699 * "bset" itself is not transformed by "T". Instead, the inequalities
2700 * are extracted from "bset" and those are transformed by "T".
2701 * uset_gist_full then determines which of the transformed inequalities
2702 * are redundant with respect to the transformed "context" and removes
2703 * the corresponding inequalities from "bset".
2705 * After preapplying "T" to the inequalities, any common factor is
2706 * removed from the coefficients. If this results in a tightening
2707 * of the constant term, then the same tightening is applied to
2708 * the corresponding untransformed inequality in "bset".
2709 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2711 * g f'(x) + r >= 0
2713 * with 0 <= r < g, then it is equivalent to
2715 * f'(x) >= 0
2717 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2718 * subspace compressed by T since the latter would be transformed to
2720 * g f'(x) >= 0
2722 static __isl_give isl_basic_set *uset_gist_compressed(
2723 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2724 __isl_take isl_mat *T)
2726 isl_ctx *ctx;
2727 isl_mat *ineq;
2728 int i;
2729 isl_size n_row, n_col;
2730 isl_int rem;
2732 ineq = extract_ineq(bset);
2733 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2734 context = isl_basic_set_preimage(context, T);
2736 if (!ineq || !context)
2737 goto error;
2738 if (isl_basic_set_plain_is_empty(context)) {
2739 isl_mat_free(ineq);
2740 isl_basic_set_free(context);
2741 return replace_by_empty(bset);
2744 ctx = isl_mat_get_ctx(ineq);
2745 n_row = isl_mat_rows(ineq);
2746 n_col = isl_mat_cols(ineq);
2747 if (n_row < 0 || n_col < 0)
2748 goto error;
2749 isl_int_init(rem);
2750 for (i = 0; i < n_row; ++i) {
2751 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2752 if (isl_int_is_zero(ctx->normalize_gcd))
2753 continue;
2754 if (isl_int_is_one(ctx->normalize_gcd))
2755 continue;
2756 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2757 ctx->normalize_gcd, n_col - 1);
2758 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2759 isl_int_fdiv_q(ineq->row[i][0],
2760 ineq->row[i][0], ctx->normalize_gcd);
2761 if (isl_int_is_zero(rem))
2762 continue;
2763 bset = isl_basic_set_cow(bset);
2764 if (!bset)
2765 break;
2766 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2768 isl_int_clear(rem);
2770 return uset_gist_full(bset, ineq, context);
2771 error:
2772 isl_mat_free(ineq);
2773 isl_basic_set_free(context);
2774 isl_basic_set_free(bset);
2775 return NULL;
2778 /* Project "bset" onto the variables that are involved in "template".
2780 static __isl_give isl_basic_set *project_onto_involved(
2781 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2783 int i;
2784 isl_size n;
2786 n = isl_basic_set_dim(template, isl_dim_set);
2787 if (n < 0 || !template)
2788 return isl_basic_set_free(bset);
2790 for (i = 0; i < n; ++i) {
2791 isl_bool involved;
2793 involved = isl_basic_set_involves_dims(template,
2794 isl_dim_set, i, 1);
2795 if (involved < 0)
2796 return isl_basic_set_free(bset);
2797 if (involved)
2798 continue;
2799 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2802 return bset;
2805 /* Remove all information from bset that is redundant in the context
2806 * of context. In particular, equalities that are linear combinations
2807 * of those in context are removed. Then the inequalities that are
2808 * redundant in the context of the equalities and inequalities of
2809 * context are removed.
2811 * First of all, we drop those constraints from "context"
2812 * that are irrelevant for computing the gist of "bset".
2813 * Alternatively, we could factorize the intersection of "context" and "bset".
2815 * We first compute the intersection of the integer affine hulls
2816 * of "bset" and "context",
2817 * compute the gist inside this intersection and then reduce
2818 * the constraints with respect to the equalities of the context
2819 * that only involve variables already involved in the input.
2820 * If the intersection of the affine hulls turns out to be empty,
2821 * then return the empty set.
2823 * If two constraints are mutually redundant, then uset_gist_full
2824 * will remove the second of those constraints. We therefore first
2825 * sort the constraints so that constraints not involving existentially
2826 * quantified variables are given precedence over those that do.
2827 * We have to perform this sorting before the variable compression,
2828 * because that may effect the order of the variables.
2830 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2831 __isl_take isl_basic_set *context)
2833 isl_mat *eq;
2834 isl_mat *T;
2835 isl_basic_set *aff;
2836 isl_basic_set *aff_context;
2837 isl_size total;
2839 total = isl_basic_set_dim(bset, isl_dim_all);
2840 if (total < 0 || !context)
2841 goto error;
2843 context = drop_irrelevant_constraints(context, bset);
2845 bset = isl_basic_set_detect_equalities(bset);
2846 aff = isl_basic_set_copy(bset);
2847 aff = isl_basic_set_plain_affine_hull(aff);
2848 context = isl_basic_set_detect_equalities(context);
2849 aff_context = isl_basic_set_copy(context);
2850 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2851 aff = isl_basic_set_intersect(aff, aff_context);
2852 if (!aff)
2853 goto error;
2854 if (isl_basic_set_plain_is_empty(aff)) {
2855 isl_basic_set_free(bset);
2856 isl_basic_set_free(context);
2857 return aff;
2859 bset = isl_basic_set_sort_constraints(bset);
2860 if (aff->n_eq == 0) {
2861 isl_basic_set_free(aff);
2862 return uset_gist_uncompressed(bset, context);
2864 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2865 eq = isl_mat_cow(eq);
2866 T = isl_mat_variable_compression(eq, NULL);
2867 isl_basic_set_free(aff);
2868 if (T && T->n_col == 0) {
2869 isl_mat_free(T);
2870 isl_basic_set_free(context);
2871 return replace_by_empty(bset);
2874 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2875 aff_context = project_onto_involved(aff_context, bset);
2877 bset = uset_gist_compressed(bset, context, T);
2878 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2880 if (bset) {
2881 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2882 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2885 return bset;
2886 error:
2887 isl_basic_set_free(bset);
2888 isl_basic_set_free(context);
2889 return NULL;
2892 /* Return the number of equality constraints in "bmap" that involve
2893 * local variables. This function assumes that Gaussian elimination
2894 * has been applied to the equality constraints.
2896 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2898 int i;
2899 isl_size total, n_div;
2901 if (!bmap)
2902 return -1;
2904 if (bmap->n_eq == 0)
2905 return 0;
2907 total = isl_basic_map_dim(bmap, isl_dim_all);
2908 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2909 if (total < 0 || n_div < 0)
2910 return -1;
2911 total -= n_div;
2913 for (i = 0; i < bmap->n_eq; ++i)
2914 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2915 n_div) == -1)
2916 return i;
2918 return bmap->n_eq;
2921 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2922 * The constraints are assumed not to involve any local variables.
2924 static __isl_give isl_basic_map *basic_map_from_equalities(
2925 __isl_take isl_space *space, __isl_take isl_mat *eq)
2927 int i, k;
2928 isl_size total;
2929 isl_basic_map *bmap = NULL;
2931 total = isl_space_dim(space, isl_dim_all);
2932 if (total < 0 || !eq)
2933 goto error;
2935 if (1 + total != eq->n_col)
2936 isl_die(isl_space_get_ctx(space), isl_error_internal,
2937 "unexpected number of columns", goto error);
2939 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2940 0, eq->n_row, 0);
2941 for (i = 0; i < eq->n_row; ++i) {
2942 k = isl_basic_map_alloc_equality(bmap);
2943 if (k < 0)
2944 goto error;
2945 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2948 isl_space_free(space);
2949 isl_mat_free(eq);
2950 return bmap;
2951 error:
2952 isl_space_free(space);
2953 isl_mat_free(eq);
2954 isl_basic_map_free(bmap);
2955 return NULL;
2958 /* Construct and return a variable compression based on the equality
2959 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2960 * "n1" is the number of (initial) equality constraints in "bmap1"
2961 * that do involve local variables.
2962 * "n2" is the number of (initial) equality constraints in "bmap2"
2963 * that do involve local variables.
2964 * "total" is the total number of other variables.
2965 * This function assumes that Gaussian elimination
2966 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2967 * such that the equality constraints not involving local variables
2968 * are those that start at "n1" or "n2".
2970 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2971 * then simply compute the compression based on the equality constraints
2972 * in the other basic map.
2973 * Otherwise, combine the equality constraints from both into a new
2974 * basic map such that Gaussian elimination can be applied to this combination
2975 * and then construct a variable compression from the resulting
2976 * equality constraints.
2978 static __isl_give isl_mat *combined_variable_compression(
2979 __isl_keep isl_basic_map *bmap1, int n1,
2980 __isl_keep isl_basic_map *bmap2, int n2, int total)
2982 isl_ctx *ctx;
2983 isl_mat *E1, *E2, *V;
2984 isl_basic_map *bmap;
2986 ctx = isl_basic_map_get_ctx(bmap1);
2987 if (bmap1->n_eq == n1) {
2988 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2989 n2, bmap2->n_eq - n2, 0, 1 + total);
2990 return isl_mat_variable_compression(E2, NULL);
2992 if (bmap2->n_eq == n2) {
2993 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2994 n1, bmap1->n_eq - n1, 0, 1 + total);
2995 return isl_mat_variable_compression(E1, NULL);
2997 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2998 n1, bmap1->n_eq - n1, 0, 1 + total);
2999 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3000 n2, bmap2->n_eq - n2, 0, 1 + total);
3001 E1 = isl_mat_concat(E1, E2);
3002 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3003 bmap = isl_basic_map_gauss(bmap, NULL);
3004 if (!bmap)
3005 return NULL;
3006 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3007 V = isl_mat_variable_compression(E1, NULL);
3008 isl_basic_map_free(bmap);
3010 return V;
3013 /* Extract the stride constraints from "bmap", compressed
3014 * with respect to both the stride constraints in "context" and
3015 * the remaining equality constraints in both "bmap" and "context".
3016 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3017 * "context_n_eq" is the number of (initial) stride constraints in "context".
3019 * Let x be all variables in "bmap" (and "context") other than the local
3020 * variables. First compute a variable compression
3022 * x = V x'
3024 * based on the non-stride equality constraints in "bmap" and "context".
3025 * Consider the stride constraints of "context",
3027 * A(x) + B(y) = 0
3029 * with y the local variables and plug in the variable compression,
3030 * resulting in
3032 * A(V x') + B(y) = 0
3034 * Use these constraints to compute a parameter compression on x'
3036 * x' = T x''
3038 * Now consider the stride constraints of "bmap"
3040 * C(x) + D(y) = 0
3042 * and plug in x = V*T x''.
3043 * That is, return A = [C*V*T D].
3045 static __isl_give isl_mat *extract_compressed_stride_constraints(
3046 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3047 __isl_keep isl_basic_map *context, int context_n_eq)
3049 isl_size total, n_div;
3050 isl_ctx *ctx;
3051 isl_mat *A, *B, *T, *V;
3053 total = isl_basic_map_dim(context, isl_dim_all);
3054 n_div = isl_basic_map_dim(context, isl_dim_div);
3055 if (total < 0 || n_div < 0)
3056 return NULL;
3057 total -= n_div;
3059 ctx = isl_basic_map_get_ctx(bmap);
3061 V = combined_variable_compression(bmap, bmap_n_eq,
3062 context, context_n_eq, total);
3064 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3065 B = isl_mat_sub_alloc6(ctx, context->eq,
3066 0, context_n_eq, 1 + total, n_div);
3067 A = isl_mat_product(A, isl_mat_copy(V));
3068 T = isl_mat_parameter_compression_ext(A, B);
3069 T = isl_mat_product(V, T);
3071 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3072 if (n_div < 0)
3073 T = isl_mat_free(T);
3074 else
3075 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3077 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3078 0, bmap_n_eq, 0, 1 + total + n_div);
3079 A = isl_mat_product(A, T);
3081 return A;
3084 /* Remove the prime factors from *g that have an exponent that
3085 * is strictly smaller than the exponent in "c".
3086 * All exponents in *g are known to be smaller than or equal
3087 * to those in "c".
3089 * That is, if *g is equal to
3091 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3093 * and "c" is equal to
3095 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3097 * then update *g to
3099 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3100 * p_n^{e_n * (e_n = f_n)}
3102 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3103 * neither does the gcd of *g and c / *g.
3104 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3105 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3106 * Dividing *g by this gcd therefore strictly reduces the exponent
3107 * of the prime factors that need to be removed, while leaving the
3108 * other prime factors untouched.
3109 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3110 * removes all undesired factors, without removing any others.
3112 static void remove_incomplete_powers(isl_int *g, isl_int c)
3114 isl_int t;
3116 isl_int_init(t);
3117 for (;;) {
3118 isl_int_divexact(t, c, *g);
3119 isl_int_gcd(t, t, *g);
3120 if (isl_int_is_one(t))
3121 break;
3122 isl_int_divexact(*g, *g, t);
3124 isl_int_clear(t);
3127 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3128 * of the same stride constraints in a compressed space that exploits
3129 * all equalities in the context and the other equalities in "bmap".
3131 * If the stride constraints of "bmap" are of the form
3133 * C(x) + D(y) = 0
3135 * then A is of the form
3137 * B(x') + D(y) = 0
3139 * If any of these constraints involves only a single local variable y,
3140 * then the constraint appears as
3142 * f(x) + m y_i = 0
3144 * in "bmap" and as
3146 * h(x') + m y_i = 0
3148 * in "A".
3150 * Let g be the gcd of m and the coefficients of h.
3151 * Then, in particular, g is a divisor of the coefficients of h and
3153 * f(x) = h(x')
3155 * is known to be a multiple of g.
3156 * If some prime factor in m appears with the same exponent in g,
3157 * then it can be removed from m because f(x) is already known
3158 * to be a multiple of g and therefore in particular of this power
3159 * of the prime factors.
3160 * Prime factors that appear with a smaller exponent in g cannot
3161 * be removed from m.
3162 * Let g' be the divisor of g containing all prime factors that
3163 * appear with the same exponent in m and g, then
3165 * f(x) + m y_i = 0
3167 * can be replaced by
3169 * f(x) + m/g' y_i' = 0
3171 * Note that (if g' != 1) this changes the explicit representation
3172 * of y_i to that of y_i', so the integer division at position i
3173 * is marked unknown and later recomputed by a call to
3174 * isl_basic_map_gauss.
3176 static __isl_give isl_basic_map *reduce_stride_constraints(
3177 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3179 int i;
3180 isl_size total, n_div;
3181 int any = 0;
3182 isl_int gcd;
3184 total = isl_basic_map_dim(bmap, isl_dim_all);
3185 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3186 if (total < 0 || n_div < 0 || !A)
3187 return isl_basic_map_free(bmap);
3188 total -= n_div;
3190 isl_int_init(gcd);
3191 for (i = 0; i < n; ++i) {
3192 int div;
3194 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3195 if (div < 0)
3196 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3197 "equality constraints modified unexpectedly",
3198 goto error);
3199 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3200 n_div - div - 1) != -1)
3201 continue;
3202 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3203 goto error;
3204 if (isl_int_is_one(gcd))
3205 continue;
3206 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3207 if (isl_int_is_one(gcd))
3208 continue;
3209 isl_int_divexact(bmap->eq[i][1 + total + div],
3210 bmap->eq[i][1 + total + div], gcd);
3211 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3212 if (!bmap)
3213 goto error;
3214 any = 1;
3216 isl_int_clear(gcd);
3218 if (any)
3219 bmap = isl_basic_map_gauss(bmap, NULL);
3221 return bmap;
3222 error:
3223 isl_int_clear(gcd);
3224 isl_basic_map_free(bmap);
3225 return NULL;
3228 /* Simplify the stride constraints in "bmap" based on
3229 * the remaining equality constraints in "bmap" and all equality
3230 * constraints in "context".
3231 * Only do this if both "bmap" and "context" have stride constraints.
3233 * First extract a copy of the stride constraints in "bmap" in a compressed
3234 * space exploiting all the other equality constraints and then
3235 * use this compressed copy to simplify the original stride constraints.
3237 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3238 __isl_keep isl_basic_map *context)
3240 int bmap_n_eq, context_n_eq;
3241 isl_mat *A;
3243 if (!bmap || !context)
3244 return isl_basic_map_free(bmap);
3246 bmap_n_eq = n_div_eq(bmap);
3247 context_n_eq = n_div_eq(context);
3249 if (bmap_n_eq < 0 || context_n_eq < 0)
3250 return isl_basic_map_free(bmap);
3251 if (bmap_n_eq == 0 || context_n_eq == 0)
3252 return bmap;
3254 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3255 context, context_n_eq);
3256 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3258 isl_mat_free(A);
3260 return bmap;
3263 /* Return a basic map that has the same intersection with "context" as "bmap"
3264 * and that is as "simple" as possible.
3266 * The core computation is performed on the pure constraints.
3267 * When we add back the meaning of the integer divisions, we need
3268 * to (re)introduce the div constraints. If we happen to have
3269 * discovered that some of these integer divisions are equal to
3270 * some affine combination of other variables, then these div
3271 * constraints may end up getting simplified in terms of the equalities,
3272 * resulting in extra inequalities on the other variables that
3273 * may have been removed already or that may not even have been
3274 * part of the input. We try and remove those constraints of
3275 * this form that are most obviously redundant with respect to
3276 * the context. We also remove those div constraints that are
3277 * redundant with respect to the other constraints in the result.
3279 * The stride constraints among the equality constraints in "bmap" are
3280 * also simplified with respecting to the other equality constraints
3281 * in "bmap" and with respect to all equality constraints in "context".
3283 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3284 __isl_take isl_basic_map *context)
3286 isl_basic_set *bset, *eq;
3287 isl_basic_map *eq_bmap;
3288 isl_size total, n_div, n_div_bmap;
3289 unsigned extra, n_eq, n_ineq;
3291 if (!bmap || !context)
3292 goto error;
3294 if (isl_basic_map_plain_is_universe(bmap)) {
3295 isl_basic_map_free(context);
3296 return bmap;
3298 if (isl_basic_map_plain_is_empty(context)) {
3299 isl_space *space = isl_basic_map_get_space(bmap);
3300 isl_basic_map_free(bmap);
3301 isl_basic_map_free(context);
3302 return isl_basic_map_universe(space);
3304 if (isl_basic_map_plain_is_empty(bmap)) {
3305 isl_basic_map_free(context);
3306 return bmap;
3309 bmap = isl_basic_map_remove_redundancies(bmap);
3310 context = isl_basic_map_remove_redundancies(context);
3311 bmap = isl_basic_map_order_divs(bmap);
3312 context = isl_basic_map_align_divs(context, bmap);
3314 n_div = isl_basic_map_dim(context, isl_dim_div);
3315 total = isl_basic_map_dim(bmap, isl_dim_all);
3316 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3317 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3318 goto error;
3319 extra = n_div - n_div_bmap;
3321 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3322 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3323 bset = uset_gist(bset,
3324 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3325 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3327 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3328 isl_basic_set_plain_is_empty(bset)) {
3329 isl_basic_map_free(context);
3330 return isl_basic_map_overlying_set(bset, bmap);
3333 n_eq = bset->n_eq;
3334 n_ineq = bset->n_ineq;
3335 eq = isl_basic_set_copy(bset);
3336 eq = isl_basic_set_cow(eq);
3337 eq = isl_basic_set_free_inequality(eq, n_ineq);
3338 bset = isl_basic_set_free_equality(bset, n_eq);
3340 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3341 eq_bmap = gist_strides(eq_bmap, context);
3342 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3343 bmap = isl_basic_map_overlying_set(bset, bmap);
3344 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3345 bmap = isl_basic_map_remove_redundancies(bmap);
3347 return bmap;
3348 error:
3349 isl_basic_map_free(bmap);
3350 isl_basic_map_free(context);
3351 return NULL;
3355 * Assumes context has no implicit divs.
3357 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3358 __isl_take isl_basic_map *context)
3360 int i;
3362 if (!map || !context)
3363 goto error;
3365 if (isl_basic_map_plain_is_empty(context)) {
3366 isl_space *space = isl_map_get_space(map);
3367 isl_map_free(map);
3368 isl_basic_map_free(context);
3369 return isl_map_universe(space);
3372 context = isl_basic_map_remove_redundancies(context);
3373 map = isl_map_cow(map);
3374 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3375 goto error;
3376 map = isl_map_compute_divs(map);
3377 if (!map)
3378 goto error;
3379 for (i = map->n - 1; i >= 0; --i) {
3380 map->p[i] = isl_basic_map_gist(map->p[i],
3381 isl_basic_map_copy(context));
3382 if (!map->p[i])
3383 goto error;
3384 if (isl_basic_map_plain_is_empty(map->p[i])) {
3385 isl_basic_map_free(map->p[i]);
3386 if (i != map->n - 1)
3387 map->p[i] = map->p[map->n - 1];
3388 map->n--;
3391 isl_basic_map_free(context);
3392 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3393 return map;
3394 error:
3395 isl_map_free(map);
3396 isl_basic_map_free(context);
3397 return NULL;
3400 /* Drop all inequalities from "bmap" that also appear in "context".
3401 * "context" is assumed to have only known local variables and
3402 * the initial local variables of "bmap" are assumed to be the same
3403 * as those of "context".
3404 * The constraints of both "bmap" and "context" are assumed
3405 * to have been sorted using isl_basic_map_sort_constraints.
3407 * Run through the inequality constraints of "bmap" and "context"
3408 * in sorted order.
3409 * If a constraint of "bmap" involves variables not in "context",
3410 * then it cannot appear in "context".
3411 * If a matching constraint is found, it is removed from "bmap".
3413 static __isl_give isl_basic_map *drop_inequalities(
3414 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3416 int i1, i2;
3417 isl_size total, bmap_total;
3418 unsigned extra;
3420 total = isl_basic_map_dim(context, isl_dim_all);
3421 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3422 if (total < 0 || bmap_total < 0)
3423 return isl_basic_map_free(bmap);
3425 extra = bmap_total - total;
3427 i1 = bmap->n_ineq - 1;
3428 i2 = context->n_ineq - 1;
3429 while (bmap && i1 >= 0 && i2 >= 0) {
3430 int cmp;
3432 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3433 extra) != -1) {
3434 --i1;
3435 continue;
3437 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3438 context->ineq[i2]);
3439 if (cmp < 0) {
3440 --i2;
3441 continue;
3443 if (cmp > 0) {
3444 --i1;
3445 continue;
3447 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3448 bmap = isl_basic_map_cow(bmap);
3449 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3450 bmap = isl_basic_map_free(bmap);
3452 --i1;
3453 --i2;
3456 return bmap;
3459 /* Drop all equalities from "bmap" that also appear in "context".
3460 * "context" is assumed to have only known local variables and
3461 * the initial local variables of "bmap" are assumed to be the same
3462 * as those of "context".
3464 * Run through the equality constraints of "bmap" and "context"
3465 * in sorted order.
3466 * If a constraint of "bmap" involves variables not in "context",
3467 * then it cannot appear in "context".
3468 * If a matching constraint is found, it is removed from "bmap".
3470 static __isl_give isl_basic_map *drop_equalities(
3471 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3473 int i1, i2;
3474 isl_size total, bmap_total;
3475 unsigned extra;
3477 total = isl_basic_map_dim(context, isl_dim_all);
3478 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3479 if (total < 0 || bmap_total < 0)
3480 return isl_basic_map_free(bmap);
3482 extra = bmap_total - total;
3484 i1 = bmap->n_eq - 1;
3485 i2 = context->n_eq - 1;
3487 while (bmap && i1 >= 0 && i2 >= 0) {
3488 int last1, last2;
3490 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3491 extra) != -1)
3492 break;
3493 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3494 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3495 if (last1 > last2) {
3496 --i2;
3497 continue;
3499 if (last1 < last2) {
3500 --i1;
3501 continue;
3503 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3504 bmap = isl_basic_map_cow(bmap);
3505 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3506 bmap = isl_basic_map_free(bmap);
3508 --i1;
3509 --i2;
3512 return bmap;
3515 /* Remove the constraints in "context" from "bmap".
3516 * "context" is assumed to have explicit representations
3517 * for all local variables.
3519 * First align the divs of "bmap" to those of "context" and
3520 * sort the constraints. Then drop all constraints from "bmap"
3521 * that appear in "context".
3523 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3524 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3526 isl_bool done, known;
3528 done = isl_basic_map_plain_is_universe(context);
3529 if (done == isl_bool_false)
3530 done = isl_basic_map_plain_is_universe(bmap);
3531 if (done == isl_bool_false)
3532 done = isl_basic_map_plain_is_empty(context);
3533 if (done == isl_bool_false)
3534 done = isl_basic_map_plain_is_empty(bmap);
3535 if (done < 0)
3536 goto error;
3537 if (done) {
3538 isl_basic_map_free(context);
3539 return bmap;
3541 known = isl_basic_map_divs_known(context);
3542 if (known < 0)
3543 goto error;
3544 if (!known)
3545 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3546 "context has unknown divs", goto error);
3548 context = isl_basic_map_order_divs(context);
3549 bmap = isl_basic_map_align_divs(bmap, context);
3550 bmap = isl_basic_map_gauss(bmap, NULL);
3551 bmap = isl_basic_map_sort_constraints(bmap);
3552 context = isl_basic_map_sort_constraints(context);
3554 bmap = drop_inequalities(bmap, context);
3555 bmap = drop_equalities(bmap, context);
3557 isl_basic_map_free(context);
3558 bmap = isl_basic_map_finalize(bmap);
3559 return bmap;
3560 error:
3561 isl_basic_map_free(bmap);
3562 isl_basic_map_free(context);
3563 return NULL;
3566 /* Replace "map" by the disjunct at position "pos" and free "context".
3568 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3569 int pos, __isl_take isl_basic_map *context)
3571 isl_basic_map *bmap;
3573 bmap = isl_basic_map_copy(map->p[pos]);
3574 isl_map_free(map);
3575 isl_basic_map_free(context);
3576 return isl_map_from_basic_map(bmap);
3579 /* Remove the constraints in "context" from "map".
3580 * If any of the disjuncts in the result turns out to be the universe,
3581 * then return this universe.
3582 * "context" is assumed to have explicit representations
3583 * for all local variables.
3585 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3586 __isl_take isl_basic_map *context)
3588 int i;
3589 isl_bool univ, known;
3591 univ = isl_basic_map_plain_is_universe(context);
3592 if (univ < 0)
3593 goto error;
3594 if (univ) {
3595 isl_basic_map_free(context);
3596 return map;
3598 known = isl_basic_map_divs_known(context);
3599 if (known < 0)
3600 goto error;
3601 if (!known)
3602 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3603 "context has unknown divs", goto error);
3605 map = isl_map_cow(map);
3606 if (!map)
3607 goto error;
3608 for (i = 0; i < map->n; ++i) {
3609 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3610 isl_basic_map_copy(context));
3611 univ = isl_basic_map_plain_is_universe(map->p[i]);
3612 if (univ < 0)
3613 goto error;
3614 if (univ && map->n > 1)
3615 return replace_by_disjunct(map, i, context);
3618 isl_basic_map_free(context);
3619 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3620 if (map->n > 1)
3621 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3622 return map;
3623 error:
3624 isl_map_free(map);
3625 isl_basic_map_free(context);
3626 return NULL;
3629 /* Remove the constraints in "context" from "set".
3630 * If any of the disjuncts in the result turns out to be the universe,
3631 * then return this universe.
3632 * "context" is assumed to have explicit representations
3633 * for all local variables.
3635 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3636 __isl_take isl_basic_set *context)
3638 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3639 bset_to_bmap(context)));
3642 /* Remove the constraints in "context" from "map".
3643 * If any of the disjuncts in the result turns out to be the universe,
3644 * then return this universe.
3645 * "context" is assumed to consist of a single disjunct and
3646 * to have explicit representations for all local variables.
3648 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3649 __isl_take isl_map *context)
3651 isl_basic_map *hull;
3653 hull = isl_map_unshifted_simple_hull(context);
3654 return isl_map_plain_gist_basic_map(map, hull);
3657 /* Replace "map" by a universe map in the same space and free "drop".
3659 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3660 __isl_take isl_map *drop)
3662 isl_map *res;
3664 res = isl_map_universe(isl_map_get_space(map));
3665 isl_map_free(map);
3666 isl_map_free(drop);
3667 return res;
3670 /* Return a map that has the same intersection with "context" as "map"
3671 * and that is as "simple" as possible.
3673 * If "map" is already the universe, then we cannot make it any simpler.
3674 * Similarly, if "context" is the universe, then we cannot exploit it
3675 * to simplify "map"
3676 * If "map" and "context" are identical to each other, then we can
3677 * return the corresponding universe.
3679 * If either "map" or "context" consists of multiple disjuncts,
3680 * then check if "context" happens to be a subset of "map",
3681 * in which case all constraints can be removed.
3682 * In case of multiple disjuncts, the standard procedure
3683 * may not be able to detect that all constraints can be removed.
3685 * If none of these cases apply, we have to work a bit harder.
3686 * During this computation, we make use of a single disjunct context,
3687 * so if the original context consists of more than one disjunct
3688 * then we need to approximate the context by a single disjunct set.
3689 * Simply taking the simple hull may drop constraints that are
3690 * only implicitly available in each disjunct. We therefore also
3691 * look for constraints among those defining "map" that are valid
3692 * for the context. These can then be used to simplify away
3693 * the corresponding constraints in "map".
3695 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3696 __isl_take isl_map *context)
3698 int equal;
3699 int is_universe;
3700 isl_size n_disjunct_map, n_disjunct_context;
3701 isl_bool subset;
3702 isl_basic_map *hull;
3704 is_universe = isl_map_plain_is_universe(map);
3705 if (is_universe >= 0 && !is_universe)
3706 is_universe = isl_map_plain_is_universe(context);
3707 if (is_universe < 0)
3708 goto error;
3709 if (is_universe) {
3710 isl_map_free(context);
3711 return map;
3714 isl_map_align_params_bin(&map, &context);
3715 equal = isl_map_plain_is_equal(map, context);
3716 if (equal < 0)
3717 goto error;
3718 if (equal)
3719 return replace_by_universe(map, context);
3721 n_disjunct_map = isl_map_n_basic_map(map);
3722 n_disjunct_context = isl_map_n_basic_map(context);
3723 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3724 goto error;
3725 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3726 subset = isl_map_is_subset(context, map);
3727 if (subset < 0)
3728 goto error;
3729 if (subset)
3730 return replace_by_universe(map, context);
3733 context = isl_map_compute_divs(context);
3734 if (!context)
3735 goto error;
3736 if (n_disjunct_context == 1) {
3737 hull = isl_map_simple_hull(context);
3738 } else {
3739 isl_ctx *ctx;
3740 isl_map_list *list;
3742 ctx = isl_map_get_ctx(map);
3743 list = isl_map_list_alloc(ctx, 2);
3744 list = isl_map_list_add(list, isl_map_copy(context));
3745 list = isl_map_list_add(list, isl_map_copy(map));
3746 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3747 list);
3749 return isl_map_gist_basic_map(map, hull);
3750 error:
3751 isl_map_free(map);
3752 isl_map_free(context);
3753 return NULL;
3756 __isl_give isl_basic_set *isl_basic_set_gist(__isl_take isl_basic_set *bset,
3757 __isl_take isl_basic_set *context)
3759 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3760 bset_to_bmap(context)));
3763 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3764 __isl_take isl_basic_set *context)
3766 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3767 bset_to_bmap(context)));
3770 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3771 __isl_take isl_basic_set *context)
3773 isl_space *space = isl_set_get_space(set);
3774 isl_basic_set *dom_context = isl_basic_set_universe(space);
3775 dom_context = isl_basic_set_intersect_params(dom_context, context);
3776 return isl_set_gist_basic_set(set, dom_context);
3779 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3780 __isl_take isl_set *context)
3782 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3785 /* Compute the gist of "bmap" with respect to the constraints "context"
3786 * on the domain.
3788 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3789 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3791 isl_space *space = isl_basic_map_get_space(bmap);
3792 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3794 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3795 return isl_basic_map_gist(bmap, bmap_context);
3798 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3799 __isl_take isl_set *context)
3801 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3802 map_context = isl_map_intersect_domain(map_context, context);
3803 return isl_map_gist(map, map_context);
3806 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3807 __isl_take isl_set *context)
3809 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3810 map_context = isl_map_intersect_range(map_context, context);
3811 return isl_map_gist(map, map_context);
3814 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3815 __isl_take isl_set *context)
3817 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3818 map_context = isl_map_intersect_params(map_context, context);
3819 return isl_map_gist(map, map_context);
3822 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3823 __isl_take isl_set *context)
3825 return isl_map_gist_params(set, context);
3828 /* Quick check to see if two basic maps are disjoint.
3829 * In particular, we reduce the equalities and inequalities of
3830 * one basic map in the context of the equalities of the other
3831 * basic map and check if we get a contradiction.
3833 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3834 __isl_keep isl_basic_map *bmap2)
3836 struct isl_vec *v = NULL;
3837 int *elim = NULL;
3838 isl_size total;
3839 int i;
3841 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3842 return isl_bool_error;
3843 if (bmap1->n_div || bmap2->n_div)
3844 return isl_bool_false;
3845 if (!bmap1->n_eq && !bmap2->n_eq)
3846 return isl_bool_false;
3848 total = isl_space_dim(bmap1->dim, isl_dim_all);
3849 if (total < 0)
3850 return isl_bool_error;
3851 if (total == 0)
3852 return isl_bool_false;
3853 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3854 if (!v)
3855 goto error;
3856 elim = isl_alloc_array(bmap1->ctx, int, total);
3857 if (!elim)
3858 goto error;
3859 compute_elimination_index(bmap1, elim, total);
3860 for (i = 0; i < bmap2->n_eq; ++i) {
3861 int reduced;
3862 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3863 bmap1, elim, total);
3864 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3865 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3866 goto disjoint;
3868 for (i = 0; i < bmap2->n_ineq; ++i) {
3869 int reduced;
3870 reduced = reduced_using_equalities(v->block.data,
3871 bmap2->ineq[i], bmap1, elim, total);
3872 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3873 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3874 goto disjoint;
3876 compute_elimination_index(bmap2, elim, total);
3877 for (i = 0; i < bmap1->n_ineq; ++i) {
3878 int reduced;
3879 reduced = reduced_using_equalities(v->block.data,
3880 bmap1->ineq[i], bmap2, elim, total);
3881 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3882 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3883 goto disjoint;
3885 isl_vec_free(v);
3886 free(elim);
3887 return isl_bool_false;
3888 disjoint:
3889 isl_vec_free(v);
3890 free(elim);
3891 return isl_bool_true;
3892 error:
3893 isl_vec_free(v);
3894 free(elim);
3895 return isl_bool_error;
3898 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3899 __isl_keep isl_basic_set *bset2)
3901 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3902 bset_to_bmap(bset2));
3905 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3907 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3908 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3909 __isl_keep isl_basic_map *bmap2))
3911 int i, j;
3913 if (!map1 || !map2)
3914 return isl_bool_error;
3916 for (i = 0; i < map1->n; ++i) {
3917 for (j = 0; j < map2->n; ++j) {
3918 isl_bool d = test(map1->p[i], map2->p[j]);
3919 if (d != isl_bool_true)
3920 return d;
3924 return isl_bool_true;
3927 /* Are "map1" and "map2" obviously disjoint, based on information
3928 * that can be derived without looking at the individual basic maps?
3930 * In particular, if one of them is empty or if they live in different spaces
3931 * (ignoring parameters), then they are clearly disjoint.
3933 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3934 __isl_keep isl_map *map2)
3936 isl_bool disjoint;
3937 isl_bool match;
3939 if (!map1 || !map2)
3940 return isl_bool_error;
3942 disjoint = isl_map_plain_is_empty(map1);
3943 if (disjoint < 0 || disjoint)
3944 return disjoint;
3946 disjoint = isl_map_plain_is_empty(map2);
3947 if (disjoint < 0 || disjoint)
3948 return disjoint;
3950 match = isl_map_tuple_is_equal(map1, isl_dim_in, map2, isl_dim_in);
3951 if (match < 0 || !match)
3952 return match < 0 ? isl_bool_error : isl_bool_true;
3954 match = isl_map_tuple_is_equal(map1, isl_dim_out, map2, isl_dim_out);
3955 if (match < 0 || !match)
3956 return match < 0 ? isl_bool_error : isl_bool_true;
3958 return isl_bool_false;
3961 /* Are "map1" and "map2" obviously disjoint?
3963 * If one of them is empty or if they live in different spaces (ignoring
3964 * parameters), then they are clearly disjoint.
3965 * This is checked by isl_map_plain_is_disjoint_global.
3967 * If they have different parameters, then we skip any further tests.
3969 * If they are obviously equal, but not obviously empty, then we will
3970 * not be able to detect if they are disjoint.
3972 * Otherwise we check if each basic map in "map1" is obviously disjoint
3973 * from each basic map in "map2".
3975 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3976 __isl_keep isl_map *map2)
3978 isl_bool disjoint;
3979 isl_bool intersect;
3980 isl_bool match;
3982 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3983 if (disjoint < 0 || disjoint)
3984 return disjoint;
3986 match = isl_map_has_equal_params(map1, map2);
3987 if (match < 0 || !match)
3988 return match < 0 ? isl_bool_error : isl_bool_false;
3990 intersect = isl_map_plain_is_equal(map1, map2);
3991 if (intersect < 0 || intersect)
3992 return intersect < 0 ? isl_bool_error : isl_bool_false;
3994 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3997 /* Are "map1" and "map2" disjoint?
3998 * The parameters are assumed to have been aligned.
4000 * In particular, check whether all pairs of basic maps are disjoint.
4002 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
4003 __isl_keep isl_map *map2)
4005 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4008 /* Are "map1" and "map2" disjoint?
4010 * They are disjoint if they are "obviously disjoint" or if one of them
4011 * is empty. Otherwise, they are not disjoint if one of them is universal.
4012 * If the two inputs are (obviously) equal and not empty, then they are
4013 * not disjoint.
4014 * If none of these cases apply, then check if all pairs of basic maps
4015 * are disjoint after aligning the parameters.
4017 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4019 isl_bool disjoint;
4020 isl_bool intersect;
4022 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4023 if (disjoint < 0 || disjoint)
4024 return disjoint;
4026 disjoint = isl_map_is_empty(map1);
4027 if (disjoint < 0 || disjoint)
4028 return disjoint;
4030 disjoint = isl_map_is_empty(map2);
4031 if (disjoint < 0 || disjoint)
4032 return disjoint;
4034 intersect = isl_map_plain_is_universe(map1);
4035 if (intersect < 0 || intersect)
4036 return isl_bool_not(intersect);
4038 intersect = isl_map_plain_is_universe(map2);
4039 if (intersect < 0 || intersect)
4040 return isl_bool_not(intersect);
4042 intersect = isl_map_plain_is_equal(map1, map2);
4043 if (intersect < 0 || intersect)
4044 return isl_bool_not(intersect);
4046 return isl_map_align_params_map_map_and_test(map1, map2,
4047 &isl_map_is_disjoint_aligned);
4050 /* Are "bmap1" and "bmap2" disjoint?
4052 * They are disjoint if they are "obviously disjoint" or if one of them
4053 * is empty. Otherwise, they are not disjoint if one of them is universal.
4054 * If none of these cases apply, we compute the intersection and see if
4055 * the result is empty.
4057 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4058 __isl_keep isl_basic_map *bmap2)
4060 isl_bool disjoint;
4061 isl_bool intersect;
4062 isl_basic_map *test;
4064 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4065 if (disjoint < 0 || disjoint)
4066 return disjoint;
4068 disjoint = isl_basic_map_is_empty(bmap1);
4069 if (disjoint < 0 || disjoint)
4070 return disjoint;
4072 disjoint = isl_basic_map_is_empty(bmap2);
4073 if (disjoint < 0 || disjoint)
4074 return disjoint;
4076 intersect = isl_basic_map_plain_is_universe(bmap1);
4077 if (intersect < 0 || intersect)
4078 return isl_bool_not(intersect);
4080 intersect = isl_basic_map_plain_is_universe(bmap2);
4081 if (intersect < 0 || intersect)
4082 return isl_bool_not(intersect);
4084 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4085 isl_basic_map_copy(bmap2));
4086 disjoint = isl_basic_map_is_empty(test);
4087 isl_basic_map_free(test);
4089 return disjoint;
4092 /* Are "bset1" and "bset2" disjoint?
4094 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4095 __isl_keep isl_basic_set *bset2)
4097 return isl_basic_map_is_disjoint(bset1, bset2);
4100 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4101 __isl_keep isl_set *set2)
4103 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4106 /* Are "set1" and "set2" disjoint?
4108 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4110 return isl_map_is_disjoint(set1, set2);
4113 /* Is "v" equal to 0, 1 or -1?
4115 static int is_zero_or_one(isl_int v)
4117 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4120 /* Are the "n" coefficients starting at "first" of inequality constraints
4121 * "i" and "j" of "bmap" opposite to each other?
4123 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4124 int first, int n)
4126 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4129 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4130 * apart from the constant term?
4132 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4134 isl_size total;
4136 total = isl_basic_map_dim(bmap, isl_dim_all);
4137 if (total < 0)
4138 return isl_bool_error;
4139 return is_opposite_part(bmap, i, j, 1, total);
4142 /* Check if we can combine a given div with lower bound l and upper
4143 * bound u with some other div and if so return that other div.
4144 * Otherwise, return a position beyond the integer divisions.
4145 * Return -1 on error.
4147 * We first check that
4148 * - the bounds are opposites of each other (except for the constant
4149 * term)
4150 * - the bounds do not reference any other div
4151 * - no div is defined in terms of this div
4153 * Let m be the size of the range allowed on the div by the bounds.
4154 * That is, the bounds are of the form
4156 * e <= a <= e + m - 1
4158 * with e some expression in the other variables.
4159 * We look for another div b such that no third div is defined in terms
4160 * of this second div b and such that in any constraint that contains
4161 * a (except for the given lower and upper bound), also contains b
4162 * with a coefficient that is m times that of b.
4163 * That is, all constraints (except for the lower and upper bound)
4164 * are of the form
4166 * e + f (a + m b) >= 0
4168 * Furthermore, in the constraints that only contain b, the coefficient
4169 * of b should be equal to 1 or -1.
4170 * If so, we return b so that "a + m b" can be replaced by
4171 * a single div "c = a + m b".
4173 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4174 unsigned div, unsigned l, unsigned u)
4176 int i, j;
4177 unsigned n_div;
4178 isl_size v_div;
4179 int coalesce;
4180 isl_bool opp;
4182 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4183 if (n_div <= 1)
4184 return n_div;
4185 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4186 if (v_div < 0)
4187 return -1;
4188 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4189 return n_div;
4190 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4191 n_div - div - 1) != -1)
4192 return n_div;
4193 opp = is_opposite(bmap, l, u);
4194 if (opp < 0 || !opp)
4195 return opp < 0 ? -1 : n_div;
4197 for (i = 0; i < n_div; ++i) {
4198 if (isl_int_is_zero(bmap->div[i][0]))
4199 continue;
4200 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4201 return n_div;
4204 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4205 if (isl_int_is_neg(bmap->ineq[l][0])) {
4206 isl_int_sub(bmap->ineq[l][0],
4207 bmap->ineq[l][0], bmap->ineq[u][0]);
4208 bmap = isl_basic_map_copy(bmap);
4209 bmap = isl_basic_map_set_to_empty(bmap);
4210 isl_basic_map_free(bmap);
4211 return n_div;
4213 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4214 coalesce = n_div;
4215 for (i = 0; i < n_div; ++i) {
4216 if (i == div)
4217 continue;
4218 if (!pairs[i])
4219 continue;
4220 for (j = 0; j < n_div; ++j) {
4221 if (isl_int_is_zero(bmap->div[j][0]))
4222 continue;
4223 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
4224 break;
4226 if (j < n_div)
4227 continue;
4228 for (j = 0; j < bmap->n_ineq; ++j) {
4229 int valid;
4230 if (j == l || j == u)
4231 continue;
4232 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4233 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4234 continue;
4235 break;
4237 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4238 break;
4239 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4240 bmap->ineq[j][1 + v_div + div],
4241 bmap->ineq[l][0]);
4242 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4243 bmap->ineq[j][1 + v_div + i]);
4244 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4245 bmap->ineq[j][1 + v_div + div],
4246 bmap->ineq[l][0]);
4247 if (!valid)
4248 break;
4250 if (j < bmap->n_ineq)
4251 continue;
4252 coalesce = i;
4253 break;
4255 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4256 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4257 return coalesce;
4260 /* Internal data structure used during the construction and/or evaluation of
4261 * an inequality that ensures that a pair of bounds always allows
4262 * for an integer value.
4264 * "tab" is the tableau in which the inequality is evaluated. It may
4265 * be NULL until it is actually needed.
4266 * "v" contains the inequality coefficients.
4267 * "g", "fl" and "fu" are temporary scalars used during the construction and
4268 * evaluation.
4270 struct test_ineq_data {
4271 struct isl_tab *tab;
4272 isl_vec *v;
4273 isl_int g;
4274 isl_int fl;
4275 isl_int fu;
4278 /* Free all the memory allocated by the fields of "data".
4280 static void test_ineq_data_clear(struct test_ineq_data *data)
4282 isl_tab_free(data->tab);
4283 isl_vec_free(data->v);
4284 isl_int_clear(data->g);
4285 isl_int_clear(data->fl);
4286 isl_int_clear(data->fu);
4289 /* Is the inequality stored in data->v satisfied by "bmap"?
4290 * That is, does it only attain non-negative values?
4291 * data->tab is a tableau corresponding to "bmap".
4293 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4294 struct test_ineq_data *data)
4296 isl_ctx *ctx;
4297 enum isl_lp_result res;
4299 ctx = isl_basic_map_get_ctx(bmap);
4300 if (!data->tab)
4301 data->tab = isl_tab_from_basic_map(bmap, 0);
4302 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4303 if (res == isl_lp_error)
4304 return isl_bool_error;
4305 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4308 /* Given a lower and an upper bound on div i, do they always allow
4309 * for an integer value of the given div?
4310 * Determine this property by constructing an inequality
4311 * such that the property is guaranteed when the inequality is nonnegative.
4312 * The lower bound is inequality l, while the upper bound is inequality u.
4313 * The constructed inequality is stored in data->v.
4315 * Let the upper bound be
4317 * -n_u a + e_u >= 0
4319 * and the lower bound
4321 * n_l a + e_l >= 0
4323 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4324 * We have
4326 * - f_u e_l <= f_u f_l g a <= f_l e_u
4328 * Since all variables are integer valued, this is equivalent to
4330 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4332 * If this interval is at least f_u f_l g, then it contains at least
4333 * one integer value for a.
4334 * That is, the test constraint is
4336 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4338 * or
4340 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4342 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4343 * then the constraint can be scaled down by a factor g',
4344 * with the constant term replaced by
4345 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4346 * Note that the result of applying Fourier-Motzkin to this pair
4347 * of constraints is
4349 * f_l e_u + f_u e_l >= 0
4351 * If the constant term of the scaled down version of this constraint,
4352 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4353 * term of the scaled down test constraint, then the test constraint
4354 * is known to hold and no explicit evaluation is required.
4355 * This is essentially the Omega test.
4357 * If the test constraint consists of only a constant term, then
4358 * it is sufficient to look at the sign of this constant term.
4360 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4361 int l, int u, struct test_ineq_data *data)
4363 unsigned offset;
4364 isl_size n_div;
4366 offset = isl_basic_map_offset(bmap, isl_dim_div);
4367 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4368 if (n_div < 0)
4369 return isl_bool_error;
4371 isl_int_gcd(data->g,
4372 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4373 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4374 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4375 isl_int_neg(data->fu, data->fu);
4376 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4377 data->fu, bmap->ineq[l], offset + n_div);
4378 isl_int_mul(data->g, data->g, data->fl);
4379 isl_int_mul(data->g, data->g, data->fu);
4380 isl_int_sub(data->g, data->g, data->fl);
4381 isl_int_sub(data->g, data->g, data->fu);
4382 isl_int_add_ui(data->g, data->g, 1);
4383 isl_int_sub(data->fl, data->v->el[0], data->g);
4385 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4386 if (isl_int_is_zero(data->g))
4387 return isl_int_is_nonneg(data->fl);
4388 if (isl_int_is_one(data->g)) {
4389 isl_int_set(data->v->el[0], data->fl);
4390 return test_ineq_is_satisfied(bmap, data);
4392 isl_int_fdiv_q(data->fl, data->fl, data->g);
4393 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4394 if (isl_int_eq(data->fl, data->v->el[0]))
4395 return isl_bool_true;
4396 isl_int_set(data->v->el[0], data->fl);
4397 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4398 offset - 1 + n_div);
4400 return test_ineq_is_satisfied(bmap, data);
4403 /* Remove more kinds of divs that are not strictly needed.
4404 * In particular, if all pairs of lower and upper bounds on a div
4405 * are such that they allow at least one integer value of the div,
4406 * then we can eliminate the div using Fourier-Motzkin without
4407 * introducing any spurious solutions.
4409 * If at least one of the two constraints has a unit coefficient for the div,
4410 * then the presence of such a value is guaranteed so there is no need to check.
4411 * In particular, the value attained by the bound with unit coefficient
4412 * can serve as this intermediate value.
4414 static __isl_give isl_basic_map *drop_more_redundant_divs(
4415 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4417 isl_ctx *ctx;
4418 struct test_ineq_data data = { NULL, NULL };
4419 unsigned off;
4420 isl_size n_div;
4421 int remove = -1;
4423 isl_int_init(data.g);
4424 isl_int_init(data.fl);
4425 isl_int_init(data.fu);
4427 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4428 if (n_div < 0)
4429 goto error;
4431 ctx = isl_basic_map_get_ctx(bmap);
4432 off = isl_basic_map_offset(bmap, isl_dim_div);
4433 data.v = isl_vec_alloc(ctx, off + n_div);
4434 if (!data.v)
4435 goto error;
4437 while (n > 0) {
4438 int i, l, u;
4439 int best = -1;
4440 isl_bool has_int;
4442 for (i = 0; i < n_div; ++i) {
4443 if (!pairs[i])
4444 continue;
4445 if (best >= 0 && pairs[best] <= pairs[i])
4446 continue;
4447 best = i;
4450 i = best;
4451 for (l = 0; l < bmap->n_ineq; ++l) {
4452 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4453 continue;
4454 if (isl_int_is_one(bmap->ineq[l][off + i]))
4455 continue;
4456 for (u = 0; u < bmap->n_ineq; ++u) {
4457 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4458 continue;
4459 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4460 continue;
4461 has_int = int_between_bounds(bmap, i, l, u,
4462 &data);
4463 if (has_int < 0)
4464 goto error;
4465 if (data.tab && data.tab->empty)
4466 break;
4467 if (!has_int)
4468 break;
4470 if (u < bmap->n_ineq)
4471 break;
4473 if (data.tab && data.tab->empty) {
4474 bmap = isl_basic_map_set_to_empty(bmap);
4475 break;
4477 if (l == bmap->n_ineq) {
4478 remove = i;
4479 break;
4481 pairs[i] = 0;
4482 --n;
4485 test_ineq_data_clear(&data);
4487 free(pairs);
4489 if (remove < 0)
4490 return bmap;
4492 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4493 return isl_basic_map_drop_redundant_divs(bmap);
4494 error:
4495 free(pairs);
4496 isl_basic_map_free(bmap);
4497 test_ineq_data_clear(&data);
4498 return NULL;
4501 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4502 * and the upper bound u, div1 always occurs together with div2 in the form
4503 * (div1 + m div2), where m is the constant range on the variable div1
4504 * allowed by l and u, replace the pair div1 and div2 by a single
4505 * div that is equal to div1 + m div2.
4507 * The new div will appear in the location that contains div2.
4508 * We need to modify all constraints that contain
4509 * div2 = (div - div1) / m
4510 * The coefficient of div2 is known to be equal to 1 or -1.
4511 * (If a constraint does not contain div2, it will also not contain div1.)
4512 * If the constraint also contains div1, then we know they appear
4513 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4514 * i.e., the coefficient of div is f.
4516 * Otherwise, we first need to introduce div1 into the constraint.
4517 * Let l be
4519 * div1 + f >=0
4521 * and u
4523 * -div1 + f' >= 0
4525 * A lower bound on div2
4527 * div2 + t >= 0
4529 * can be replaced by
4531 * m div2 + div1 + m t + f >= 0
4533 * An upper bound
4535 * -div2 + t >= 0
4537 * can be replaced by
4539 * -(m div2 + div1) + m t + f' >= 0
4541 * These constraint are those that we would obtain from eliminating
4542 * div1 using Fourier-Motzkin.
4544 * After all constraints have been modified, we drop the lower and upper
4545 * bound and then drop div1.
4546 * Since the new div is only placed in the same location that used
4547 * to store div2, but otherwise has a different meaning, any possible
4548 * explicit representation of the original div2 is removed.
4550 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4551 unsigned div1, unsigned div2, unsigned l, unsigned u)
4553 isl_ctx *ctx;
4554 isl_int m;
4555 isl_size v_div;
4556 unsigned total;
4557 int i;
4559 ctx = isl_basic_map_get_ctx(bmap);
4561 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4562 if (v_div < 0)
4563 return isl_basic_map_free(bmap);
4564 total = 1 + v_div + bmap->n_div;
4566 isl_int_init(m);
4567 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4568 isl_int_add_ui(m, m, 1);
4570 for (i = 0; i < bmap->n_ineq; ++i) {
4571 if (i == l || i == u)
4572 continue;
4573 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4574 continue;
4575 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4576 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4577 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4578 ctx->one, bmap->ineq[l], total);
4579 else
4580 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4581 ctx->one, bmap->ineq[u], total);
4583 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4584 bmap->ineq[i][1 + v_div + div1]);
4585 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4588 isl_int_clear(m);
4589 if (l > u) {
4590 isl_basic_map_drop_inequality(bmap, l);
4591 isl_basic_map_drop_inequality(bmap, u);
4592 } else {
4593 isl_basic_map_drop_inequality(bmap, u);
4594 isl_basic_map_drop_inequality(bmap, l);
4596 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4597 bmap = isl_basic_map_drop_div(bmap, div1);
4598 return bmap;
4601 /* First check if we can coalesce any pair of divs and
4602 * then continue with dropping more redundant divs.
4604 * We loop over all pairs of lower and upper bounds on a div
4605 * with coefficient 1 and -1, respectively, check if there
4606 * is any other div "c" with which we can coalesce the div
4607 * and if so, perform the coalescing.
4609 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4610 __isl_take isl_basic_map *bmap, int *pairs, int n)
4612 int i, l, u;
4613 isl_size v_div;
4614 isl_size n_div;
4616 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4617 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4618 if (v_div < 0 || n_div < 0)
4619 return isl_basic_map_free(bmap);
4621 for (i = 0; i < n_div; ++i) {
4622 if (!pairs[i])
4623 continue;
4624 for (l = 0; l < bmap->n_ineq; ++l) {
4625 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4626 continue;
4627 for (u = 0; u < bmap->n_ineq; ++u) {
4628 int c;
4630 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4631 continue;
4632 c = div_find_coalesce(bmap, pairs, i, l, u);
4633 if (c < 0)
4634 goto error;
4635 if (c >= n_div)
4636 continue;
4637 free(pairs);
4638 bmap = coalesce_divs(bmap, i, c, l, u);
4639 return isl_basic_map_drop_redundant_divs(bmap);
4644 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4645 free(pairs);
4646 return bmap;
4649 return drop_more_redundant_divs(bmap, pairs, n);
4650 error:
4651 free(pairs);
4652 isl_basic_map_free(bmap);
4653 return NULL;
4656 /* Are the "n" coefficients starting at "first" of inequality constraints
4657 * "i" and "j" of "bmap" equal to each other?
4659 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4660 int first, int n)
4662 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4665 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4666 * apart from the constant term and the coefficient at position "pos"?
4668 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4669 int pos)
4671 isl_size total;
4673 total = isl_basic_map_dim(bmap, isl_dim_all);
4674 if (total < 0)
4675 return isl_bool_error;
4676 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4677 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4680 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4681 * apart from the constant term and the coefficient at position "pos"?
4683 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4684 int pos)
4686 isl_size total;
4688 total = isl_basic_map_dim(bmap, isl_dim_all);
4689 if (total < 0)
4690 return isl_bool_error;
4691 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4692 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4695 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4696 * been modified, simplying it if "simplify" is set.
4697 * Free the temporary data structure "pairs" that was associated
4698 * to the old version of "bmap".
4700 static __isl_give isl_basic_map *drop_redundant_divs_again(
4701 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4703 if (simplify)
4704 bmap = isl_basic_map_simplify(bmap);
4705 free(pairs);
4706 return isl_basic_map_drop_redundant_divs(bmap);
4709 /* Is "div" the single unknown existentially quantified variable
4710 * in inequality constraint "ineq" of "bmap"?
4711 * "div" is known to have a non-zero coefficient in "ineq".
4713 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4714 int div)
4716 int i;
4717 isl_size n_div;
4718 unsigned o_div;
4719 isl_bool known;
4721 known = isl_basic_map_div_is_known(bmap, div);
4722 if (known < 0 || known)
4723 return isl_bool_not(known);
4724 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4725 if (n_div < 0)
4726 return isl_bool_error;
4727 if (n_div == 1)
4728 return isl_bool_true;
4729 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4730 for (i = 0; i < n_div; ++i) {
4731 isl_bool known;
4733 if (i == div)
4734 continue;
4735 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4736 continue;
4737 known = isl_basic_map_div_is_known(bmap, i);
4738 if (known < 0 || !known)
4739 return known;
4742 return isl_bool_true;
4745 /* Does integer division "div" have coefficient 1 in inequality constraint
4746 * "ineq" of "map"?
4748 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4750 unsigned o_div;
4752 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4753 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4754 return isl_bool_true;
4756 return isl_bool_false;
4759 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4760 * then try and drop redundant divs again,
4761 * freeing the temporary data structure "pairs" that was associated
4762 * to the old version of "bmap".
4764 static __isl_give isl_basic_map *set_eq_and_try_again(
4765 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4767 bmap = isl_basic_map_cow(bmap);
4768 isl_basic_map_inequality_to_equality(bmap, ineq);
4769 return drop_redundant_divs_again(bmap, pairs, 1);
4772 /* Drop the integer division at position "div", along with the two
4773 * inequality constraints "ineq1" and "ineq2" in which it appears
4774 * from "bmap" and then try and drop redundant divs again,
4775 * freeing the temporary data structure "pairs" that was associated
4776 * to the old version of "bmap".
4778 static __isl_give isl_basic_map *drop_div_and_try_again(
4779 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4780 __isl_take int *pairs)
4782 if (ineq1 > ineq2) {
4783 isl_basic_map_drop_inequality(bmap, ineq1);
4784 isl_basic_map_drop_inequality(bmap, ineq2);
4785 } else {
4786 isl_basic_map_drop_inequality(bmap, ineq2);
4787 isl_basic_map_drop_inequality(bmap, ineq1);
4789 bmap = isl_basic_map_drop_div(bmap, div);
4790 return drop_redundant_divs_again(bmap, pairs, 0);
4793 /* Given two inequality constraints
4795 * f(x) + n d + c >= 0, (ineq)
4797 * with d the variable at position "pos", and
4799 * f(x) + c0 >= 0, (lower)
4801 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4802 * determined by the first constraint.
4803 * That is, store
4805 * ceil((c0 - c)/n)
4807 * in *l.
4809 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4810 int ineq, int lower, int pos, isl_int *l)
4812 isl_int_neg(*l, bmap->ineq[ineq][0]);
4813 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4814 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4817 /* Given two inequality constraints
4819 * f(x) + n d + c >= 0, (ineq)
4821 * with d the variable at position "pos", and
4823 * -f(x) - c0 >= 0, (upper)
4825 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4826 * determined by the first constraint.
4827 * That is, store
4829 * ceil((-c1 - c)/n)
4831 * in *u.
4833 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4834 int ineq, int upper, int pos, isl_int *u)
4836 isl_int_neg(*u, bmap->ineq[ineq][0]);
4837 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4838 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4841 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4842 * does the corresponding lower bound have a fixed value in "bmap"?
4844 * In particular, "ineq" is of the form
4846 * f(x) + n d + c >= 0
4848 * with n > 0, c the constant term and
4849 * d the existentially quantified variable "div".
4850 * That is, the lower bound is
4852 * ceil((-f(x) - c)/n)
4854 * Look for a pair of constraints
4856 * f(x) + c0 >= 0
4857 * -f(x) + c1 >= 0
4859 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4860 * That is, check that
4862 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4864 * If so, return the index of inequality f(x) + c0 >= 0.
4865 * Otherwise, return bmap->n_ineq.
4866 * Return -1 on error.
4868 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4870 int i;
4871 int lower = -1, upper = -1;
4872 unsigned o_div;
4873 isl_int l, u;
4874 int equal;
4876 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4877 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4878 isl_bool par, opp;
4880 if (i == ineq)
4881 continue;
4882 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4883 continue;
4884 par = isl_bool_false;
4885 if (lower < 0)
4886 par = is_parallel_except(bmap, ineq, i, o_div + div);
4887 if (par < 0)
4888 return -1;
4889 if (par) {
4890 lower = i;
4891 continue;
4893 opp = isl_bool_false;
4894 if (upper < 0)
4895 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4896 if (opp < 0)
4897 return -1;
4898 if (opp)
4899 upper = i;
4902 if (lower < 0 || upper < 0)
4903 return bmap->n_ineq;
4905 isl_int_init(l);
4906 isl_int_init(u);
4908 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4909 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4911 equal = isl_int_eq(l, u);
4913 isl_int_clear(l);
4914 isl_int_clear(u);
4916 return equal ? lower : bmap->n_ineq;
4919 /* Given a lower bound constraint "ineq" on the existentially quantified
4920 * variable "div", such that the corresponding lower bound has
4921 * a fixed value in "bmap", assign this fixed value to the variable and
4922 * then try and drop redundant divs again,
4923 * freeing the temporary data structure "pairs" that was associated
4924 * to the old version of "bmap".
4925 * "lower" determines the constant value for the lower bound.
4927 * In particular, "ineq" is of the form
4929 * f(x) + n d + c >= 0,
4931 * while "lower" is of the form
4933 * f(x) + c0 >= 0
4935 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4936 * is ceil((c0 - c)/n).
4938 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4939 int div, int ineq, int lower, int *pairs)
4941 isl_int c;
4942 unsigned o_div;
4944 isl_int_init(c);
4946 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4947 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4948 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4949 free(pairs);
4951 isl_int_clear(c);
4953 return isl_basic_map_drop_redundant_divs(bmap);
4956 /* Do any of the integer divisions of "bmap" involve integer division "div"?
4958 * The integer division "div" could only ever appear in any later
4959 * integer division (with an explicit representation).
4961 static isl_bool any_div_involves_div(__isl_keep isl_basic_map *bmap, int div)
4963 int i;
4964 isl_size v_div, n_div;
4966 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4967 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4968 if (v_div < 0 || n_div < 0)
4969 return isl_bool_error;
4971 for (i = div + 1; i < n_div; ++i) {
4972 isl_bool unknown;
4974 unknown = isl_basic_map_div_is_marked_unknown(bmap, i);
4975 if (unknown < 0)
4976 return isl_bool_error;
4977 if (unknown)
4978 continue;
4979 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4980 return isl_bool_true;
4983 return isl_bool_false;
4986 /* Remove divs that are not strictly needed based on the inequality
4987 * constraints.
4988 * In particular, if a div only occurs positively (or negatively)
4989 * in constraints, then it can simply be dropped.
4990 * Also, if a div occurs in only two constraints and if moreover
4991 * those two constraints are opposite to each other, except for the constant
4992 * term and if the sum of the constant terms is such that for any value
4993 * of the other values, there is always at least one integer value of the
4994 * div, i.e., if one plus this sum is greater than or equal to
4995 * the (absolute value) of the coefficient of the div in the constraints,
4996 * then we can also simply drop the div.
4998 * If an existentially quantified variable does not have an explicit
4999 * representation, appears in only a single lower bound that does not
5000 * involve any other such existentially quantified variables and appears
5001 * in this lower bound with coefficient 1,
5002 * then fix the variable to the value of the lower bound. That is,
5003 * turn the inequality into an equality.
5004 * If for any value of the other variables, there is any value
5005 * for the existentially quantified variable satisfying the constraints,
5006 * then this lower bound also satisfies the constraints.
5007 * It is therefore safe to pick this lower bound.
5009 * The same reasoning holds even if the coefficient is not one.
5010 * However, fixing the variable to the value of the lower bound may
5011 * in general introduce an extra integer division, in which case
5012 * it may be better to pick another value.
5013 * If this integer division has a known constant value, then plugging
5014 * in this constant value removes the existentially quantified variable
5015 * completely. In particular, if the lower bound is of the form
5016 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5017 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5018 * then the existentially quantified variable can be assigned this
5019 * shared value.
5021 * We skip divs that appear in equalities or in the definition of other divs.
5022 * Divs that appear in the definition of other divs usually occur in at least
5023 * 4 constraints, but the constraints may have been simplified.
5025 * If any divs are left after these simple checks then we move on
5026 * to more complicated cases in drop_more_redundant_divs.
5028 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
5029 __isl_take isl_basic_map *bmap)
5031 int i, j;
5032 isl_size off;
5033 int *pairs = NULL;
5034 int n = 0;
5035 isl_size n_ineq;
5037 if (!bmap)
5038 goto error;
5039 if (bmap->n_div == 0)
5040 return bmap;
5042 off = isl_basic_map_var_offset(bmap, isl_dim_div);
5043 if (off < 0)
5044 return isl_basic_map_free(bmap);
5045 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
5046 if (!pairs)
5047 goto error;
5049 n_ineq = isl_basic_map_n_inequality(bmap);
5050 if (n_ineq < 0)
5051 goto error;
5052 for (i = 0; i < bmap->n_div; ++i) {
5053 int pos, neg;
5054 int last_pos, last_neg;
5055 int redundant;
5056 int defined;
5057 isl_bool involves, opp, set_div;
5059 defined = !isl_int_is_zero(bmap->div[i][0]);
5060 involves = any_div_involves_div(bmap, i);
5061 if (involves < 0)
5062 goto error;
5063 if (involves)
5064 continue;
5065 for (j = 0; j < bmap->n_eq; ++j)
5066 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5067 break;
5068 if (j < bmap->n_eq)
5069 continue;
5070 ++n;
5071 pos = neg = 0;
5072 for (j = 0; j < bmap->n_ineq; ++j) {
5073 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5074 last_pos = j;
5075 ++pos;
5077 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5078 last_neg = j;
5079 ++neg;
5082 pairs[i] = pos * neg;
5083 if (pairs[i] == 0) {
5084 for (j = bmap->n_ineq - 1; j >= 0; --j)
5085 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5086 isl_basic_map_drop_inequality(bmap, j);
5087 bmap = isl_basic_map_drop_div(bmap, i);
5088 return drop_redundant_divs_again(bmap, pairs, 0);
5090 if (pairs[i] != 1)
5091 opp = isl_bool_false;
5092 else
5093 opp = is_opposite(bmap, last_pos, last_neg);
5094 if (opp < 0)
5095 goto error;
5096 if (!opp) {
5097 int lower;
5098 isl_bool single, one;
5100 if (pos != 1)
5101 continue;
5102 single = single_unknown(bmap, last_pos, i);
5103 if (single < 0)
5104 goto error;
5105 if (!single)
5106 continue;
5107 one = has_coef_one(bmap, i, last_pos);
5108 if (one < 0)
5109 goto error;
5110 if (one)
5111 return set_eq_and_try_again(bmap, last_pos,
5112 pairs);
5113 lower = lower_bound_is_cst(bmap, i, last_pos);
5114 if (lower < 0)
5115 goto error;
5116 if (lower < n_ineq)
5117 return fix_cst_lower(bmap, i, last_pos, lower,
5118 pairs);
5119 continue;
5122 isl_int_add(bmap->ineq[last_pos][0],
5123 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5124 isl_int_add_ui(bmap->ineq[last_pos][0],
5125 bmap->ineq[last_pos][0], 1);
5126 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5127 bmap->ineq[last_pos][1+off+i]);
5128 isl_int_sub_ui(bmap->ineq[last_pos][0],
5129 bmap->ineq[last_pos][0], 1);
5130 isl_int_sub(bmap->ineq[last_pos][0],
5131 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5132 if (redundant)
5133 return drop_div_and_try_again(bmap, i,
5134 last_pos, last_neg, pairs);
5135 if (defined)
5136 set_div = isl_bool_false;
5137 else
5138 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5139 if (set_div < 0)
5140 return isl_basic_map_free(bmap);
5141 if (set_div) {
5142 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5143 return drop_redundant_divs_again(bmap, pairs, 1);
5145 pairs[i] = 0;
5146 --n;
5149 if (n > 0)
5150 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5152 free(pairs);
5153 return bmap;
5154 error:
5155 free(pairs);
5156 isl_basic_map_free(bmap);
5157 return NULL;
5160 /* Consider the coefficients at "c" as a row vector and replace
5161 * them with their product with "T". "T" is assumed to be a square matrix.
5163 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5165 isl_size n;
5166 isl_ctx *ctx;
5167 isl_vec *v;
5169 n = isl_mat_rows(T);
5170 if (n < 0)
5171 return isl_stat_error;
5172 if (isl_seq_first_non_zero(c, n) == -1)
5173 return isl_stat_ok;
5174 ctx = isl_mat_get_ctx(T);
5175 v = isl_vec_alloc(ctx, n);
5176 if (!v)
5177 return isl_stat_error;
5178 isl_seq_swp_or_cpy(v->el, c, n);
5179 v = isl_vec_mat_product(v, isl_mat_copy(T));
5180 if (!v)
5181 return isl_stat_error;
5182 isl_seq_swp_or_cpy(c, v->el, n);
5183 isl_vec_free(v);
5185 return isl_stat_ok;
5188 /* Plug in T for the variables in "bmap" starting at "pos".
5189 * T is a linear unimodular matrix, i.e., without constant term.
5191 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5192 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5194 int i;
5195 isl_size n_row, n_col;
5197 bmap = isl_basic_map_cow(bmap);
5198 n_row = isl_mat_rows(T);
5199 n_col = isl_mat_cols(T);
5200 if (!bmap || n_row < 0 || n_col < 0)
5201 goto error;
5203 if (n_col != n_row)
5204 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5205 "expecting square matrix", goto error);
5207 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5208 goto error;
5210 for (i = 0; i < bmap->n_eq; ++i)
5211 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5212 goto error;
5213 for (i = 0; i < bmap->n_ineq; ++i)
5214 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5215 goto error;
5216 for (i = 0; i < bmap->n_div; ++i) {
5217 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5218 continue;
5219 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5220 goto error;
5223 isl_mat_free(T);
5224 return bmap;
5225 error:
5226 isl_basic_map_free(bmap);
5227 isl_mat_free(T);
5228 return NULL;
5231 /* Remove divs that are not strictly needed.
5233 * First look for an equality constraint involving two or more
5234 * existentially quantified variables without an explicit
5235 * representation. Replace the combination that appears
5236 * in the equality constraint by a single existentially quantified
5237 * variable such that the equality can be used to derive
5238 * an explicit representation for the variable.
5239 * If there are no more such equality constraints, then continue
5240 * with isl_basic_map_drop_redundant_divs_ineq.
5242 * In particular, if the equality constraint is of the form
5244 * f(x) + \sum_i c_i a_i = 0
5246 * with a_i existentially quantified variable without explicit
5247 * representation, then apply a transformation on the existentially
5248 * quantified variables to turn the constraint into
5250 * f(x) + g a_1' = 0
5252 * with g the gcd of the c_i.
5253 * In order to easily identify which existentially quantified variables
5254 * have a complete explicit representation, i.e., without being defined
5255 * in terms of other existentially quantified variables without
5256 * an explicit representation, the existentially quantified variables
5257 * are first sorted.
5259 * The variable transformation is computed by extending the row
5260 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5262 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5263 * [a_2'] [ a_2 ]
5264 * ... = U ....
5265 * [a_n'] [ a_n ]
5267 * with [c_1/g ... c_n/g] representing the first row of U.
5268 * The inverse of U is then plugged into the original constraints.
5269 * The call to isl_basic_map_simplify makes sure the explicit
5270 * representation for a_1' is extracted from the equality constraint.
5272 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5273 __isl_take isl_basic_map *bmap)
5275 int first;
5276 int i;
5277 unsigned o_div;
5278 isl_size n_div;
5279 int l;
5280 isl_ctx *ctx;
5281 isl_mat *T;
5283 if (!bmap)
5284 return NULL;
5285 if (isl_basic_map_divs_known(bmap))
5286 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5287 if (bmap->n_eq == 0)
5288 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5289 bmap = isl_basic_map_sort_divs(bmap);
5290 if (!bmap)
5291 return NULL;
5293 first = isl_basic_map_first_unknown_div(bmap);
5294 if (first < 0)
5295 return isl_basic_map_free(bmap);
5297 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5298 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5299 if (n_div < 0)
5300 return isl_basic_map_free(bmap);
5302 for (i = 0; i < bmap->n_eq; ++i) {
5303 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5304 n_div - (first));
5305 if (l < 0)
5306 continue;
5307 l += first;
5308 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5309 n_div - (l + 1)) == -1)
5310 continue;
5311 break;
5313 if (i >= bmap->n_eq)
5314 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5316 ctx = isl_basic_map_get_ctx(bmap);
5317 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5318 if (!T)
5319 return isl_basic_map_free(bmap);
5320 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5321 T = isl_mat_normalize_row(T, 0);
5322 T = isl_mat_unimodular_complete(T, 1);
5323 T = isl_mat_right_inverse(T);
5325 for (i = l; i < n_div; ++i)
5326 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5327 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5328 bmap = isl_basic_map_simplify(bmap);
5330 return isl_basic_map_drop_redundant_divs(bmap);
5333 /* Does "bmap" satisfy any equality that involves more than 2 variables
5334 * and/or has coefficients different from -1 and 1?
5336 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5338 int i;
5339 isl_size total;
5341 total = isl_basic_map_dim(bmap, isl_dim_all);
5342 if (total < 0)
5343 return isl_bool_error;
5345 for (i = 0; i < bmap->n_eq; ++i) {
5346 int j, k;
5348 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5349 if (j < 0)
5350 continue;
5351 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5352 !isl_int_is_negone(bmap->eq[i][1 + j]))
5353 return isl_bool_true;
5355 j += 1;
5356 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5357 if (k < 0)
5358 continue;
5359 j += k;
5360 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5361 !isl_int_is_negone(bmap->eq[i][1 + j]))
5362 return isl_bool_true;
5364 j += 1;
5365 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5366 if (k >= 0)
5367 return isl_bool_true;
5370 return isl_bool_false;
5373 /* Remove any common factor g from the constraint coefficients in "v".
5374 * The constant term is stored in the first position and is replaced
5375 * by floor(c/g). If any common factor is removed and if this results
5376 * in a tightening of the constraint, then set *tightened.
5378 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5379 int *tightened)
5381 isl_ctx *ctx;
5383 if (!v)
5384 return NULL;
5385 ctx = isl_vec_get_ctx(v);
5386 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5387 if (isl_int_is_zero(ctx->normalize_gcd))
5388 return v;
5389 if (isl_int_is_one(ctx->normalize_gcd))
5390 return v;
5391 v = isl_vec_cow(v);
5392 if (!v)
5393 return NULL;
5394 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5395 *tightened = 1;
5396 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5397 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5398 v->size - 1);
5399 return v;
5402 /* Internal representation used by isl_basic_map_reduce_coefficients.
5404 * "total" is the total dimensionality of the original basic map.
5405 * "v" is a temporary vector of size 1 + total that can be used
5406 * to store constraint coefficients.
5407 * "T" is the variable compression.
5408 * "T2" is the inverse transformation.
5409 * "tightened" is set if any constant term got tightened
5410 * while reducing the coefficients.
5412 struct isl_reduce_coefficients_data {
5413 isl_size total;
5414 isl_vec *v;
5415 isl_mat *T;
5416 isl_mat *T2;
5417 int tightened;
5420 /* Free all memory allocated in "data".
5422 static void isl_reduce_coefficients_data_clear(
5423 struct isl_reduce_coefficients_data *data)
5425 data->T = isl_mat_free(data->T);
5426 data->T2 = isl_mat_free(data->T2);
5427 data->v = isl_vec_free(data->v);
5430 /* Initialize "data" for "bmap", freeing all allocated memory
5431 * if anything goes wrong.
5433 * In particular, construct a variable compression
5434 * from the equality constraints of "bmap" and
5435 * allocate a temporary vector.
5437 static isl_stat isl_reduce_coefficients_data_init(
5438 __isl_keep isl_basic_map *bmap,
5439 struct isl_reduce_coefficients_data *data)
5441 isl_ctx *ctx;
5442 isl_mat *eq;
5444 data->v = NULL;
5445 data->T = NULL;
5446 data->T2 = NULL;
5447 data->tightened = 0;
5449 data->total = isl_basic_map_dim(bmap, isl_dim_all);
5450 if (data->total < 0)
5451 return isl_stat_error;
5452 ctx = isl_basic_map_get_ctx(bmap);
5453 data->v = isl_vec_alloc(ctx, 1 + data->total);
5454 if (!data->v)
5455 return isl_stat_error;
5457 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq,
5458 0, 1 + data->total);
5459 data->T = isl_mat_variable_compression(eq, &data->T2);
5460 if (!data->T || !data->T2)
5461 goto error;
5463 return isl_stat_ok;
5464 error:
5465 isl_reduce_coefficients_data_clear(data);
5466 return isl_stat_error;
5469 /* Reduce the coefficients of "bmap" by applying the variable compression
5470 * in "data".
5471 * In particular, apply the variable compression to each constraint,
5472 * factor out any common factor in the non-constant coefficients and
5473 * then apply the inverse of the compression.
5475 * Only apply the reduction on a single copy of the basic map
5476 * since the reduction may leave the result in an inconsistent state.
5477 * In particular, the constraints may not be gaussed.
5479 static __isl_give isl_basic_map *reduce_coefficients(
5480 __isl_take isl_basic_map *bmap,
5481 struct isl_reduce_coefficients_data *data)
5483 int i;
5484 isl_size total;
5486 total = isl_basic_map_dim(bmap, isl_dim_all);
5487 if (total < 0)
5488 return isl_basic_map_free(bmap);
5489 if (total != data->total)
5490 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
5491 "total dimensionality changed unexpectedly",
5492 return isl_basic_map_free(bmap));
5494 bmap = isl_basic_map_cow(bmap);
5495 if (!bmap)
5496 return NULL;
5498 for (i = 0; i < bmap->n_ineq; ++i) {
5499 isl_seq_cpy(data->v->el, bmap->ineq[i], 1 + data->total);
5500 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T));
5501 data->v = normalize_constraint(data->v, &data->tightened);
5502 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T2));
5503 if (!data->v)
5504 return isl_basic_map_free(bmap);
5505 isl_seq_cpy(bmap->ineq[i], data->v->el, 1 + data->total);
5508 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5510 return bmap;
5513 /* If "bmap" is an integer set that satisfies any equality involving
5514 * more than 2 variables and/or has coefficients different from -1 and 1,
5515 * then use variable compression to reduce the coefficients by removing
5516 * any (hidden) common factor.
5517 * In particular, apply the variable compression to each constraint,
5518 * factor out any common factor in the non-constant coefficients and
5519 * then apply the inverse of the compression.
5520 * At the end, we mark the basic map as having reduced constants.
5521 * If this flag is still set on the next invocation of this function,
5522 * then we skip the computation.
5524 * Removing a common factor may result in a tightening of some of
5525 * the constraints. If this happens, then we may end up with two
5526 * opposite inequalities that can be replaced by an equality.
5527 * We therefore call isl_basic_map_detect_inequality_pairs,
5528 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5529 * and isl_basic_map_gauss if such a pair was found.
5530 * This call to isl_basic_map_gauss may undo much of the effect
5531 * of the reduction on which isl_map_coalesce depends.
5532 * In particular, constraints in terms of (compressed) local variables
5533 * get reformulated in terms of the set variables again.
5534 * The reduction is therefore applied again afterwards.
5535 * This has to be done before the call to eliminate_divs_eq, however,
5536 * since that may remove some local variables, while
5537 * the data used during the reduction is formulated in terms
5538 * of the original variables.
5540 * Tightening may also result in some other constraints becoming
5541 * (rationally) redundant with respect to the tightened constraint
5542 * (in combination with other constraints). The basic map may
5543 * therefore no longer be assumed to have no redundant constraints.
5545 * Note that this function may leave the result in an inconsistent state.
5546 * In particular, the constraints may not be gaussed.
5547 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5548 * for some of the test cases to pass successfully.
5549 * Any potential modification of the representation is therefore only
5550 * performed on a single copy of the basic map.
5552 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5553 __isl_take isl_basic_map *bmap)
5555 struct isl_reduce_coefficients_data data;
5556 isl_bool multi;
5558 if (!bmap)
5559 return NULL;
5560 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5561 return bmap;
5562 if (isl_basic_map_is_rational(bmap))
5563 return bmap;
5564 if (bmap->n_eq == 0)
5565 return bmap;
5566 multi = has_multiple_var_equality(bmap);
5567 if (multi < 0)
5568 return isl_basic_map_free(bmap);
5569 if (!multi)
5570 return bmap;
5572 if (isl_reduce_coefficients_data_init(bmap, &data) < 0)
5573 return isl_basic_map_free(bmap);
5575 if (data.T->n_col == 0) {
5576 isl_reduce_coefficients_data_clear(&data);
5577 return isl_basic_map_set_to_empty(bmap);
5580 bmap = reduce_coefficients(bmap, &data);
5581 if (!bmap)
5582 goto error;
5584 if (data.tightened) {
5585 int progress = 0;
5587 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
5588 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5589 if (progress) {
5590 bmap = isl_basic_map_gauss(bmap, NULL);
5591 bmap = reduce_coefficients(bmap, &data);
5592 bmap = eliminate_divs_eq(bmap, &progress);
5596 isl_reduce_coefficients_data_clear(&data);
5598 return bmap;
5599 error:
5600 isl_reduce_coefficients_data_clear(&data);
5601 return isl_basic_map_free(bmap);
5604 /* Shift the integer division at position "div" of "bmap"
5605 * by "shift" times the variable at position "pos".
5606 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5607 * corresponds to the constant term.
5609 * That is, if the integer division has the form
5611 * floor(f(x)/d)
5613 * then replace it by
5615 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5617 __isl_give isl_basic_map *isl_basic_map_shift_div(
5618 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5620 int i;
5621 isl_size total, n_div;
5623 if (isl_int_is_zero(shift))
5624 return bmap;
5625 total = isl_basic_map_dim(bmap, isl_dim_all);
5626 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5627 total -= n_div;
5628 if (total < 0 || n_div < 0)
5629 return isl_basic_map_free(bmap);
5631 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5633 for (i = 0; i < bmap->n_eq; ++i) {
5634 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5635 continue;
5636 isl_int_submul(bmap->eq[i][pos],
5637 shift, bmap->eq[i][1 + total + div]);
5639 for (i = 0; i < bmap->n_ineq; ++i) {
5640 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5641 continue;
5642 isl_int_submul(bmap->ineq[i][pos],
5643 shift, bmap->ineq[i][1 + total + div]);
5645 for (i = 0; i < bmap->n_div; ++i) {
5646 if (isl_int_is_zero(bmap->div[i][0]))
5647 continue;
5648 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5649 continue;
5650 isl_int_submul(bmap->div[i][1 + pos],
5651 shift, bmap->div[i][1 + 1 + total + div]);
5654 return bmap;