simplify constraints by checking for residues
[isl.git] / isl_map_simplify.c
blob45496336a02f2e174cc13e9a5c0aa3ac2826bf6a
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
6 * Copyright 2021,2023 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
15 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
18 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include "isl_equalities.h"
21 #include <isl/map.h>
22 #include <isl_seq.h>
23 #include "isl_tab.h"
24 #include <isl_space_private.h>
25 #include <isl_mat_private.h>
26 #include <isl_vec_private.h>
28 #include <bset_to_bmap.c>
29 #include <bset_from_bmap.c>
30 #include <set_to_map.c>
31 #include <set_from_map.c>
33 /* Mark "bmap" as having one or more inequality constraints modified.
34 * If "equivalent" is set, then this modification was done based
35 * on an equality constraint already available in "bmap".
37 * Any modification may result in the constraints no longer being sorted and
38 * may also undo the effect of reduce_coefficients.
40 * A modification that uses extra information may also result
41 * in the modified constraint(s) becoming redundant or
42 * turning into an implicit equality constraint.
44 static __isl_give isl_basic_map *isl_basic_map_modify_inequality(
45 __isl_take isl_basic_map *bmap, int equivalent)
47 if (!bmap)
48 return NULL;
49 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
50 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
51 if (equivalent)
52 return bmap;
53 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
54 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
55 return bmap;
58 static void swap_equality(__isl_keep isl_basic_map *bmap, int a, int b)
60 isl_int *t = bmap->eq[a];
61 bmap->eq[a] = bmap->eq[b];
62 bmap->eq[b] = t;
65 static void swap_inequality(__isl_keep isl_basic_map *bmap, int a, int b)
67 if (a != b) {
68 isl_int *t = bmap->ineq[a];
69 bmap->ineq[a] = bmap->ineq[b];
70 bmap->ineq[b] = t;
74 /* Scale down the inequality constraint "ineq" of length "len"
75 * by a factor of "f".
76 * All the coefficients, except the constant term,
77 * are assumed to be multiples of "f".
79 * If the factor is 0 or 1, then no scaling needs to be performed.
81 * If scaling is performed then take into account that the constraint
82 * is modified (not simply based on an equality constraint).
84 static __isl_give isl_basic_map *scale_down_inequality(
85 __isl_take isl_basic_map *bmap, int ineq, isl_int f, unsigned len)
87 if (!bmap)
88 return NULL;
90 if (isl_int_is_zero(f) || isl_int_is_one(f))
91 return bmap;
93 isl_int_fdiv_q(bmap->ineq[ineq][0], bmap->ineq[ineq][0], f);
94 isl_seq_scale_down(bmap->ineq[ineq] + 1, bmap->ineq[ineq] + 1, f, len);
96 bmap = isl_basic_map_modify_inequality(bmap, 0);
98 return bmap;
101 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
102 __isl_take isl_basic_map *bmap)
104 int i;
105 isl_int gcd;
106 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
108 if (total < 0)
109 return isl_basic_map_free(bmap);
111 isl_int_init(gcd);
112 for (i = bmap->n_eq - 1; i >= 0; --i) {
113 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
114 if (isl_int_is_zero(gcd)) {
115 if (!isl_int_is_zero(bmap->eq[i][0])) {
116 bmap = isl_basic_map_set_to_empty(bmap);
117 break;
119 if (isl_basic_map_drop_equality(bmap, i) < 0)
120 goto error;
121 continue;
123 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
124 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
125 if (isl_int_is_one(gcd))
126 continue;
127 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
128 bmap = isl_basic_map_set_to_empty(bmap);
129 break;
131 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
134 for (i = bmap->n_ineq - 1; i >= 0; --i) {
135 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
136 if (isl_int_is_zero(gcd)) {
137 if (isl_int_is_neg(bmap->ineq[i][0])) {
138 bmap = isl_basic_map_set_to_empty(bmap);
139 break;
141 if (isl_basic_map_drop_inequality(bmap, i) < 0)
142 goto error;
143 continue;
145 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
146 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
147 bmap = scale_down_inequality(bmap, i, gcd, total);
148 if (!bmap)
149 goto error;
151 isl_int_clear(gcd);
153 return bmap;
154 error:
155 isl_int_clear(gcd);
156 isl_basic_map_free(bmap);
157 return NULL;
160 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
161 __isl_take isl_basic_set *bset)
163 isl_basic_map *bmap = bset_to_bmap(bset);
164 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
167 /* Reduce the coefficient of the variable at position "pos"
168 * in integer division "div", such that it lies in the half-open
169 * interval (1/2,1/2], extracting any excess value from this integer division.
170 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
171 * corresponds to the constant term.
173 * That is, the integer division is of the form
175 * floor((... + (c * d + r) * x_pos + ...)/d)
177 * with -d < 2 * r <= d.
178 * Replace it by
180 * floor((... + r * x_pos + ...)/d) + c * x_pos
182 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
183 * Otherwise, c = floor((c * d + r)/d) + 1.
185 * This is the same normalization that is performed by isl_aff_floor.
187 static __isl_give isl_basic_map *reduce_coefficient_in_div(
188 __isl_take isl_basic_map *bmap, int div, int pos)
190 isl_int shift;
191 int add_one;
193 isl_int_init(shift);
194 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
195 isl_int_mul_ui(shift, shift, 2);
196 add_one = isl_int_gt(shift, bmap->div[div][0]);
197 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
198 if (add_one)
199 isl_int_add_ui(shift, shift, 1);
200 isl_int_neg(shift, shift);
201 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
202 isl_int_clear(shift);
204 return bmap;
207 /* Does the coefficient of the variable at position "pos"
208 * in integer division "div" need to be reduced?
209 * That is, does it lie outside the half-open interval (1/2,1/2]?
210 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
211 * 2 * c != d.
213 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
214 int pos)
216 isl_bool r;
218 if (isl_int_is_zero(bmap->div[div][1 + pos]))
219 return isl_bool_false;
221 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
222 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
223 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
224 isl_int_divexact_ui(bmap->div[div][1 + pos],
225 bmap->div[div][1 + pos], 2);
227 return r;
230 /* Reduce the coefficients (including the constant term) of
231 * integer division "div", if needed.
232 * In particular, make sure all coefficients lie in
233 * the half-open interval (1/2,1/2].
235 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
236 __isl_take isl_basic_map *bmap, int div)
238 int i;
239 isl_size total;
241 total = isl_basic_map_dim(bmap, isl_dim_all);
242 if (total < 0)
243 return isl_basic_map_free(bmap);
244 for (i = 0; i < 1 + total; ++i) {
245 isl_bool reduce;
247 reduce = needs_reduction(bmap, div, i);
248 if (reduce < 0)
249 return isl_basic_map_free(bmap);
250 if (!reduce)
251 continue;
252 bmap = reduce_coefficient_in_div(bmap, div, i);
253 if (!bmap)
254 break;
257 return bmap;
260 /* Reduce the coefficients (including the constant term) of
261 * the known integer divisions, if needed
262 * In particular, make sure all coefficients lie in
263 * the half-open interval (1/2,1/2].
265 static __isl_give isl_basic_map *reduce_div_coefficients(
266 __isl_take isl_basic_map *bmap)
268 int i;
270 if (!bmap)
271 return NULL;
272 if (bmap->n_div == 0)
273 return bmap;
275 for (i = 0; i < bmap->n_div; ++i) {
276 if (isl_int_is_zero(bmap->div[i][0]))
277 continue;
278 bmap = reduce_div_coefficients_of_div(bmap, i);
279 if (!bmap)
280 break;
283 return bmap;
286 /* Remove any common factor in numerator and denominator of the div expression,
287 * not taking into account the constant term.
288 * That is, if the div is of the form
290 * floor((a + m f(x))/(m d))
292 * then replace it by
294 * floor((floor(a/m) + f(x))/d)
296 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
297 * and can therefore not influence the result of the floor.
299 static __isl_give isl_basic_map *normalize_div_expression(
300 __isl_take isl_basic_map *bmap, int div)
302 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
303 isl_ctx *ctx = bmap->ctx;
305 if (total < 0)
306 return isl_basic_map_free(bmap);
307 if (isl_int_is_zero(bmap->div[div][0]))
308 return bmap;
309 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
310 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
311 if (isl_int_is_one(ctx->normalize_gcd))
312 return bmap;
313 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
314 ctx->normalize_gcd);
315 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
316 ctx->normalize_gcd);
317 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
318 ctx->normalize_gcd, total);
320 return bmap;
323 /* Remove any common factor in numerator and denominator of a div expression,
324 * not taking into account the constant term.
325 * That is, look for any div of the form
327 * floor((a + m f(x))/(m d))
329 * and replace it by
331 * floor((floor(a/m) + f(x))/d)
333 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
334 * and can therefore not influence the result of the floor.
336 static __isl_give isl_basic_map *normalize_div_expressions(
337 __isl_take isl_basic_map *bmap)
339 int i;
341 if (!bmap)
342 return NULL;
343 if (bmap->n_div == 0)
344 return bmap;
346 for (i = 0; i < bmap->n_div; ++i)
347 bmap = normalize_div_expression(bmap, i);
349 return bmap;
352 /* Some progress has been made.
353 * Set *progress if "progress" is not NULL.
355 static void mark_progress(int *progress)
357 if (progress)
358 *progress = 1;
361 /* Eliminate the variable at position "pos" from the constraints of "bmap"
362 * using the equality constraint "eq".
363 * If "keep_divs" is set, then try and preserve
364 * the integer division expressions. In this case, these expressions
365 * are assumed to have been ordered.
366 * If "equivalent" is set, then the elimination is performed
367 * using an equality constraint of "bmap", meaning that the meaning
368 * of the constraints is preserved.
370 static __isl_give isl_basic_map *eliminate_var_using_equality(
371 __isl_take isl_basic_map *bmap,
372 unsigned pos, isl_int *eq, int keep_divs, int equivalent, int *progress)
374 isl_size total;
375 isl_size v_div;
376 int k;
377 int last_div;
378 isl_ctx *ctx;
380 total = isl_basic_map_dim(bmap, isl_dim_all);
381 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
382 if (total < 0 || v_div < 0)
383 return isl_basic_map_free(bmap);
384 ctx = isl_basic_map_get_ctx(bmap);
385 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
386 for (k = 0; k < bmap->n_eq; ++k) {
387 if (bmap->eq[k] == eq)
388 continue;
389 if (isl_int_is_zero(bmap->eq[k][1+pos]))
390 continue;
391 mark_progress(progress);
392 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
393 isl_seq_normalize(ctx, bmap->eq[k], 1 + total);
396 for (k = 0; k < bmap->n_ineq; ++k) {
397 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
398 continue;
399 mark_progress(progress);
400 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
401 isl_seq_gcd(bmap->ineq[k], 1 + total, &ctx->normalize_gcd);
402 bmap = scale_down_inequality(bmap, k, ctx->normalize_gcd,
403 total);
404 bmap = isl_basic_map_modify_inequality(bmap, equivalent);
405 if (!bmap)
406 return NULL;
409 for (k = 0; k < bmap->n_div; ++k) {
410 if (isl_int_is_zero(bmap->div[k][0]))
411 continue;
412 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
413 continue;
414 mark_progress(progress);
415 /* We need to be careful about circular definitions,
416 * so for now we just remove the definition of div k
417 * if the equality contains any divs.
418 * If keep_divs is set, then the divs have been ordered
419 * and we can keep the definition as long as the result
420 * is still ordered.
422 if (last_div == -1 || (keep_divs && last_div < k)) {
423 isl_seq_elim(bmap->div[k]+1, eq,
424 1+pos, 1+total, &bmap->div[k][0]);
425 bmap = normalize_div_expression(bmap, k);
426 if (!bmap)
427 return NULL;
428 } else
429 isl_seq_clr(bmap->div[k], 1 + total);
432 return bmap;
435 /* Eliminate and remove the local variable at position "pos" of "bmap"
436 * using the equality constraint "eq".
437 * If "keep_divs" is set, then try and preserve
438 * the integer division expressions. In this case, these expressions
439 * are assumed to have been ordered.
440 * If "equivalent" is set, then the elimination is performed
441 * using an equality constraint of "bmap", meaning that the meaning
442 * of the constraints is preserved.
444 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
445 isl_int *eq, unsigned div, int keep_divs, int equivalent)
447 isl_size v_div;
448 unsigned pos;
450 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
451 if (v_div < 0)
452 return isl_basic_map_free(bmap);
453 pos = v_div + div;
454 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs,
455 equivalent, NULL);
457 bmap = isl_basic_map_drop_div(bmap, div);
459 return bmap;
462 /* Check if elimination of div "div" using equality "eq" would not
463 * result in a div depending on a later div.
465 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
466 unsigned div)
468 int k;
469 int last_div;
470 isl_size v_div;
471 unsigned pos;
473 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
474 if (v_div < 0)
475 return isl_bool_error;
476 pos = v_div + div;
478 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
479 if (last_div < 0 || last_div <= div)
480 return isl_bool_true;
482 for (k = 0; k <= last_div; ++k) {
483 if (isl_int_is_zero(bmap->div[k][0]))
484 continue;
485 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
486 return isl_bool_false;
489 return isl_bool_true;
492 /* Eliminate divs based on equalities
494 static __isl_give isl_basic_map *eliminate_divs_eq(
495 __isl_take isl_basic_map *bmap, int *progress)
497 int d;
498 int i;
499 int modified = 0;
500 unsigned off;
502 bmap = isl_basic_map_order_divs(bmap);
504 if (!bmap)
505 return NULL;
507 off = isl_basic_map_offset(bmap, isl_dim_div);
509 for (d = bmap->n_div - 1; d >= 0 ; --d) {
510 for (i = 0; i < bmap->n_eq; ++i) {
511 isl_bool ok;
513 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
514 !isl_int_is_negone(bmap->eq[i][off + d]))
515 continue;
516 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
517 if (ok < 0)
518 return isl_basic_map_free(bmap);
519 if (!ok)
520 continue;
521 modified = 1;
522 mark_progress(progress);
523 bmap = eliminate_div(bmap, bmap->eq[i], d, 1, 1);
524 if (isl_basic_map_drop_equality(bmap, i) < 0)
525 return isl_basic_map_free(bmap);
526 break;
529 if (modified)
530 return eliminate_divs_eq(bmap, progress);
531 return bmap;
534 /* Eliminate divs based on inequalities
536 static __isl_give isl_basic_map *eliminate_divs_ineq(
537 __isl_take isl_basic_map *bmap, int *progress)
539 int d;
540 int i;
541 unsigned off;
542 struct isl_ctx *ctx;
544 if (!bmap)
545 return NULL;
547 ctx = bmap->ctx;
548 off = isl_basic_map_offset(bmap, isl_dim_div);
550 for (d = bmap->n_div - 1; d >= 0 ; --d) {
551 for (i = 0; i < bmap->n_eq; ++i)
552 if (!isl_int_is_zero(bmap->eq[i][off + d]))
553 break;
554 if (i < bmap->n_eq)
555 continue;
556 for (i = 0; i < bmap->n_ineq; ++i)
557 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
558 break;
559 if (i < bmap->n_ineq)
560 continue;
561 mark_progress(progress);
562 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
563 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
564 break;
565 bmap = isl_basic_map_drop_div(bmap, d);
566 if (!bmap)
567 break;
569 return bmap;
572 /* Does the equality constraint at position "eq" in "bmap" involve
573 * any local variables in the range [first, first + n)
574 * that are not marked as having an explicit representation?
576 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
577 int eq, unsigned first, unsigned n)
579 unsigned o_div;
580 int i;
582 if (!bmap)
583 return isl_bool_error;
585 o_div = isl_basic_map_offset(bmap, isl_dim_div);
586 for (i = 0; i < n; ++i) {
587 isl_bool unknown;
589 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
590 continue;
591 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
592 if (unknown < 0)
593 return isl_bool_error;
594 if (unknown)
595 return isl_bool_true;
598 return isl_bool_false;
601 /* The last local variable involved in the equality constraint
602 * at position "eq" in "bmap" is the local variable at position "div".
603 * It can therefore be used to extract an explicit representation
604 * for that variable.
605 * Do so unless the local variable already has an explicit representation or
606 * the explicit representation would involve any other local variables
607 * that in turn do not have an explicit representation.
608 * An equality constraint involving local variables without an explicit
609 * representation can be used in isl_basic_map_drop_redundant_divs
610 * to separate out an independent local variable. Introducing
611 * an explicit representation here would block this transformation,
612 * while the partial explicit representation in itself is not very useful.
613 * Set *progress if anything is changed.
615 * The equality constraint is of the form
617 * f(x) + n e >= 0
619 * with n a positive number. The explicit representation derived from
620 * this constraint is
622 * floor((-f(x))/n)
624 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
625 int div, int eq, int *progress)
627 isl_size total;
628 unsigned o_div;
629 isl_bool involves;
631 if (!bmap)
632 return NULL;
634 if (!isl_int_is_zero(bmap->div[div][0]))
635 return bmap;
637 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
638 if (involves < 0)
639 return isl_basic_map_free(bmap);
640 if (involves)
641 return bmap;
643 total = isl_basic_map_dim(bmap, isl_dim_all);
644 if (total < 0)
645 return isl_basic_map_free(bmap);
646 o_div = isl_basic_map_offset(bmap, isl_dim_div);
647 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
648 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
649 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
650 mark_progress(progress);
652 return bmap;
655 /* Perform fangcheng (Gaussian elimination) on the equality
656 * constraints of "bmap".
657 * That is, put them into row-echelon form, starting from the last column
658 * backward and use them to eliminate the corresponding coefficients
659 * from all constraints.
661 * If "progress" is not NULL, then it gets set if the elimination
662 * results in any changes.
663 * The elimination process may result in some equality constraints
664 * getting interchanged or removed.
665 * If "swap" or "drop" are not NULL, then they get called when
666 * two equality constraints get interchanged or
667 * when a number of final equality constraints get removed.
668 * As a special case, if the input turns out to be empty,
669 * then drop gets called with the number of removed equality
670 * constraints set to the total number of equality constraints.
671 * If "swap" or "drop" are not NULL, then the local variables (if any)
672 * are assumed to be in a valid order.
674 __isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
675 int *progress,
676 isl_stat (*swap)(unsigned a, unsigned b, void *user),
677 isl_stat (*drop)(unsigned n, void *user), void *user)
679 int k;
680 int done;
681 int last_var;
682 unsigned total_var;
683 isl_size total;
684 unsigned n_drop;
686 if (!swap && !drop)
687 bmap = isl_basic_map_order_divs(bmap);
689 total = isl_basic_map_dim(bmap, isl_dim_all);
690 if (total < 0)
691 return isl_basic_map_free(bmap);
693 total_var = total - bmap->n_div;
695 last_var = total - 1;
696 for (done = 0; done < bmap->n_eq; ++done) {
697 for (; last_var >= 0; --last_var) {
698 for (k = done; k < bmap->n_eq; ++k)
699 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
700 break;
701 if (k < bmap->n_eq)
702 break;
704 if (last_var < 0)
705 break;
706 if (k != done) {
707 swap_equality(bmap, k, done);
708 if (swap && swap(k, done, user) < 0)
709 return isl_basic_map_free(bmap);
711 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
712 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
714 bmap = eliminate_var_using_equality(bmap, last_var,
715 bmap->eq[done], 1, 1, progress);
717 if (last_var >= total_var)
718 bmap = set_div_from_eq(bmap, last_var - total_var,
719 done, progress);
720 if (!bmap)
721 return NULL;
723 if (done == bmap->n_eq)
724 return bmap;
725 for (k = done; k < bmap->n_eq; ++k) {
726 if (isl_int_is_zero(bmap->eq[k][0]))
727 continue;
728 if (drop && drop(bmap->n_eq, user) < 0)
729 return isl_basic_map_free(bmap);
730 return isl_basic_map_set_to_empty(bmap);
732 n_drop = bmap->n_eq - done;
733 bmap = isl_basic_map_free_equality(bmap, n_drop);
734 if (drop && drop(n_drop, user) < 0)
735 return isl_basic_map_free(bmap);
736 return bmap;
739 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
740 int *progress)
742 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
745 __isl_give isl_basic_set *isl_basic_set_gauss(
746 __isl_take isl_basic_set *bset, int *progress)
748 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
749 progress));
753 static unsigned int round_up(unsigned int v)
755 int old_v = v;
757 while (v) {
758 old_v = v;
759 v ^= v & -v;
761 return old_v << 1;
764 /* Hash table of inequalities in a basic map.
765 * "index" is an array of addresses of inequalities in the basic map, some
766 * of which are NULL. The inequalities are hashed on the coefficients
767 * except the constant term.
768 * "size" is the number of elements in the array and is always a power of two
769 * "bits" is the number of bits need to represent an index into the array.
770 * "total" is the total dimension of the basic map.
772 struct isl_constraint_index {
773 unsigned int size;
774 int bits;
775 isl_int ***index;
776 isl_size total;
779 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
781 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
782 __isl_keep isl_basic_map *bmap)
784 isl_ctx *ctx;
786 ci->index = NULL;
787 if (!bmap)
788 return isl_stat_error;
789 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
790 if (ci->total < 0)
791 return isl_stat_error;
792 if (bmap->n_ineq == 0)
793 return isl_stat_ok;
794 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
795 ci->bits = ffs(ci->size) - 1;
796 ctx = isl_basic_map_get_ctx(bmap);
797 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
798 if (!ci->index)
799 return isl_stat_error;
801 return isl_stat_ok;
804 /* Free the memory allocated by create_constraint_index.
806 static void constraint_index_free(struct isl_constraint_index *ci)
808 free(ci->index);
811 /* Return the position in ci->index that contains the address of
812 * an inequality that is equal to *ineq up to the constant term,
813 * provided this address is not identical to "ineq".
814 * If there is no such inequality, then return the position where
815 * such an inequality should be inserted.
817 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
819 int h;
820 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
821 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
822 if (ineq != ci->index[h] &&
823 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
824 break;
825 return h;
828 /* Return the position in ci->index that contains the address of
829 * an inequality that is equal to the k'th inequality of "bmap"
830 * up to the constant term, provided it does not point to the very
831 * same inequality.
832 * If there is no such inequality, then return the position where
833 * such an inequality should be inserted.
835 static int hash_index(struct isl_constraint_index *ci,
836 __isl_keep isl_basic_map *bmap, int k)
838 return hash_index_ineq(ci, &bmap->ineq[k]);
841 static int set_hash_index(struct isl_constraint_index *ci,
842 __isl_keep isl_basic_set *bset, int k)
844 return hash_index(ci, bset, k);
847 /* Fill in the "ci" data structure with the inequalities of "bset".
849 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
850 __isl_keep isl_basic_set *bset)
852 int k, h;
854 if (create_constraint_index(ci, bset) < 0)
855 return isl_stat_error;
857 for (k = 0; k < bset->n_ineq; ++k) {
858 h = set_hash_index(ci, bset, k);
859 ci->index[h] = &bset->ineq[k];
862 return isl_stat_ok;
865 /* Is the inequality ineq (obviously) redundant with respect
866 * to the constraints in "ci"?
868 * Look for an inequality in "ci" with the same coefficients and then
869 * check if the contant term of "ineq" is greater than or equal
870 * to the constant term of that inequality. If so, "ineq" is clearly
871 * redundant.
873 * Note that hash_index_ineq ignores a stored constraint if it has
874 * the same address as the passed inequality. It is ok to pass
875 * the address of a local variable here since it will never be
876 * the same as the address of a constraint in "ci".
878 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
879 isl_int *ineq)
881 int h;
883 h = hash_index_ineq(ci, &ineq);
884 if (!ci->index[h])
885 return isl_bool_false;
886 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
889 /* If we can eliminate more than one div, then we need to make
890 * sure we do it from last div to first div, in order not to
891 * change the position of the other divs that still need to
892 * be removed.
894 static __isl_give isl_basic_map *remove_duplicate_divs(
895 __isl_take isl_basic_map *bmap, int *progress)
897 unsigned int size;
898 int *index;
899 int *elim_for;
900 int k, l, h;
901 int bits;
902 struct isl_blk eq;
903 isl_size v_div;
904 unsigned total;
905 struct isl_ctx *ctx;
907 bmap = isl_basic_map_order_divs(bmap);
908 if (!bmap || bmap->n_div <= 1)
909 return bmap;
911 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
912 if (v_div < 0)
913 return isl_basic_map_free(bmap);
914 total = v_div + bmap->n_div;
916 ctx = bmap->ctx;
917 for (k = bmap->n_div - 1; k >= 0; --k)
918 if (!isl_int_is_zero(bmap->div[k][0]))
919 break;
920 if (k <= 0)
921 return bmap;
923 size = round_up(4 * bmap->n_div / 3 - 1);
924 if (size == 0)
925 return bmap;
926 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
927 bits = ffs(size) - 1;
928 index = isl_calloc_array(ctx, int, size);
929 if (!elim_for || !index)
930 goto out;
931 eq = isl_blk_alloc(ctx, 1+total);
932 if (isl_blk_is_error(eq))
933 goto out;
935 isl_seq_clr(eq.data, 1+total);
936 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
937 for (--k; k >= 0; --k) {
938 uint32_t hash;
940 if (isl_int_is_zero(bmap->div[k][0]))
941 continue;
943 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
944 for (h = hash; index[h]; h = (h+1) % size)
945 if (isl_seq_eq(bmap->div[k],
946 bmap->div[index[h]-1], 2+total))
947 break;
948 if (index[h]) {
949 mark_progress(progress);
950 l = index[h] - 1;
951 elim_for[l] = k + 1;
953 index[h] = k+1;
955 for (l = bmap->n_div - 1; l >= 0; --l) {
956 if (!elim_for[l])
957 continue;
958 k = elim_for[l] - 1;
959 isl_int_set_si(eq.data[1 + v_div + k], -1);
960 isl_int_set_si(eq.data[1 + v_div + l], 1);
961 bmap = eliminate_div(bmap, eq.data, l, 1, 0);
962 if (!bmap)
963 break;
964 isl_int_set_si(eq.data[1 + v_div + k], 0);
965 isl_int_set_si(eq.data[1 + v_div + l], 0);
968 isl_blk_free(ctx, eq);
969 out:
970 free(index);
971 free(elim_for);
972 return bmap;
975 /* Is the local variable at position "div" of "bmap"
976 * an integral integer division?
978 static isl_bool is_known_integral_div(__isl_keep isl_basic_map *bmap, int div)
980 isl_bool unknown;
982 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
983 if (unknown < 0 || unknown)
984 return isl_bool_not(unknown);
985 return isl_basic_map_div_is_integral(bmap, div);
988 /* Eliminate local variable "div" from "bmap", given
989 * that it represents an integer division with denominator 1.
991 * Construct an equality constraint that equates the local variable
992 * to the argument of the integer division and use that to eliminate
993 * the local variable.
995 static __isl_give isl_basic_map *eliminate_integral_div(
996 __isl_take isl_basic_map *bmap, int div)
998 isl_size total, v_div;
999 isl_vec *v;
1001 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1002 total = isl_basic_map_dim(bmap, isl_dim_all);
1003 if (v_div < 0 || total < 0)
1004 return isl_basic_map_free(bmap);
1005 v = isl_vec_alloc(isl_basic_map_get_ctx(bmap), 1 + total);
1006 if (!v)
1007 return isl_basic_map_free(bmap);
1008 isl_seq_cpy(v->el, bmap->div[div] + 1, 1 + total);
1009 isl_int_set_si(v->el[1 + v_div + div], -1);
1010 bmap = eliminate_div(bmap, v->el, div, 1, 0);
1011 isl_vec_free(v);
1013 return bmap;
1016 /* Eliminate all integer divisions with denominator 1.
1018 static __isl_give isl_basic_map *eliminate_integral_divs(
1019 __isl_take isl_basic_map *bmap, int *progress)
1021 int i;
1022 isl_size n_div;
1024 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1025 if (n_div < 0)
1026 return isl_basic_map_free(bmap);
1028 for (i = 0; i < n_div; ++i) {
1029 isl_bool eliminate;
1031 eliminate = is_known_integral_div(bmap, i);
1032 if (eliminate < 0)
1033 return isl_basic_map_free(bmap);
1034 if (!eliminate)
1035 continue;
1037 bmap = eliminate_integral_div(bmap, i);
1038 mark_progress(progress);
1039 i--;
1040 n_div--;
1043 return bmap;
1046 static int n_pure_div_eq(__isl_keep isl_basic_map *bmap)
1048 int i, j;
1049 isl_size v_div;
1051 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1052 if (v_div < 0)
1053 return -1;
1054 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1055 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1056 --j;
1057 if (j < 0)
1058 break;
1059 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
1060 return 0;
1062 return i;
1065 /* Normalize divs that appear in equalities.
1067 * In particular, we assume that bmap contains some equalities
1068 * of the form
1070 * a x = m * e_i
1072 * and we want to replace the set of e_i by a minimal set and
1073 * such that the new e_i have a canonical representation in terms
1074 * of the vector x.
1075 * If any of the equalities involves more than one divs, then
1076 * we currently simply bail out.
1078 * Let us first additionally assume that all equalities involve
1079 * a div. The equalities then express modulo constraints on the
1080 * remaining variables and we can use "parameter compression"
1081 * to find a minimal set of constraints. The result is a transformation
1083 * x = T(x') = x_0 + G x'
1085 * with G a lower-triangular matrix with all elements below the diagonal
1086 * non-negative and smaller than the diagonal element on the same row.
1087 * We first normalize x_0 by making the same property hold in the affine
1088 * T matrix.
1089 * The rows i of G with a 1 on the diagonal do not impose any modulo
1090 * constraint and simply express x_i = x'_i.
1091 * For each of the remaining rows i, we introduce a div and a corresponding
1092 * equality. In particular
1094 * g_ii e_j = x_i - g_i(x')
1096 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1097 * corresponding div (if g_kk != 1).
1099 * If there are any equalities not involving any div, then we
1100 * first apply a variable compression on the variables x:
1102 * x = C x'' x'' = C_2 x
1104 * and perform the above parameter compression on A C instead of on A.
1105 * The resulting compression is then of the form
1107 * x'' = T(x') = x_0 + G x'
1109 * and in constructing the new divs and the corresponding equalities,
1110 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1111 * by the corresponding row from C_2.
1113 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
1114 int *progress)
1116 int i, j, k;
1117 isl_size v_div;
1118 int div_eq;
1119 struct isl_mat *B;
1120 struct isl_vec *d;
1121 struct isl_mat *T = NULL;
1122 struct isl_mat *C = NULL;
1123 struct isl_mat *C2 = NULL;
1124 isl_int v;
1125 int *pos = NULL;
1126 int dropped, needed;
1128 if (!bmap)
1129 return NULL;
1131 if (bmap->n_div == 0)
1132 return bmap;
1134 if (bmap->n_eq == 0)
1135 return bmap;
1137 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1138 return bmap;
1140 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1141 div_eq = n_pure_div_eq(bmap);
1142 if (v_div < 0 || div_eq < 0)
1143 return isl_basic_map_free(bmap);
1144 if (div_eq == 0)
1145 return bmap;
1147 if (div_eq < bmap->n_eq) {
1148 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1149 bmap->n_eq - div_eq, 0, 1 + v_div);
1150 C = isl_mat_variable_compression(B, &C2);
1151 if (!C || !C2)
1152 goto error;
1153 if (C->n_col == 0) {
1154 bmap = isl_basic_map_set_to_empty(bmap);
1155 isl_mat_free(C);
1156 isl_mat_free(C2);
1157 goto done;
1161 d = isl_vec_alloc(bmap->ctx, div_eq);
1162 if (!d)
1163 goto error;
1164 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1165 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1166 --j;
1167 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1169 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1171 if (C) {
1172 B = isl_mat_product(B, C);
1173 C = NULL;
1176 T = isl_mat_parameter_compression(B, d);
1177 if (!T)
1178 goto error;
1179 if (T->n_col == 0) {
1180 bmap = isl_basic_map_set_to_empty(bmap);
1181 isl_mat_free(C2);
1182 isl_mat_free(T);
1183 goto done;
1185 isl_int_init(v);
1186 for (i = 0; i < T->n_row - 1; ++i) {
1187 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1188 if (isl_int_is_zero(v))
1189 continue;
1190 isl_mat_col_submul(T, 0, v, 1 + i);
1192 isl_int_clear(v);
1193 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1194 if (!pos)
1195 goto error;
1196 /* We have to be careful because dropping equalities may reorder them */
1197 dropped = 0;
1198 for (j = bmap->n_div - 1; j >= 0; --j) {
1199 for (i = 0; i < bmap->n_eq; ++i)
1200 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1201 break;
1202 if (i < bmap->n_eq) {
1203 bmap = isl_basic_map_drop_div(bmap, j);
1204 if (isl_basic_map_drop_equality(bmap, i) < 0)
1205 goto error;
1206 ++dropped;
1209 pos[0] = 0;
1210 needed = 0;
1211 for (i = 1; i < T->n_row; ++i) {
1212 if (isl_int_is_one(T->row[i][i]))
1213 pos[i] = i;
1214 else
1215 needed++;
1217 if (needed > dropped) {
1218 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1219 if (!bmap)
1220 goto error;
1222 for (i = 1; i < T->n_row; ++i) {
1223 if (isl_int_is_one(T->row[i][i]))
1224 continue;
1225 k = isl_basic_map_alloc_div(bmap);
1226 pos[i] = 1 + v_div + k;
1227 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1228 isl_int_set(bmap->div[k][0], T->row[i][i]);
1229 if (C2)
1230 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1231 else
1232 isl_int_set_si(bmap->div[k][1 + i], 1);
1233 for (j = 0; j < i; ++j) {
1234 if (isl_int_is_zero(T->row[i][j]))
1235 continue;
1236 if (pos[j] < T->n_row && C2)
1237 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1238 C2->row[pos[j]], 1 + v_div);
1239 else
1240 isl_int_neg(bmap->div[k][1 + pos[j]],
1241 T->row[i][j]);
1243 j = isl_basic_map_alloc_equality(bmap);
1244 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1245 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1247 free(pos);
1248 isl_mat_free(C2);
1249 isl_mat_free(T);
1251 mark_progress(progress);
1252 done:
1253 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1255 return bmap;
1256 error:
1257 free(pos);
1258 isl_mat_free(C);
1259 isl_mat_free(C2);
1260 isl_mat_free(T);
1261 isl_basic_map_free(bmap);
1262 return NULL;
1265 static __isl_give isl_basic_map *set_div_from_lower_bound(
1266 __isl_take isl_basic_map *bmap, int div, int ineq)
1268 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1270 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1271 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1272 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1273 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1274 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1276 return bmap;
1279 /* Check whether it is ok to define a div based on an inequality.
1280 * To avoid the introduction of circular definitions of divs, we
1281 * do not allow such a definition if the resulting expression would refer to
1282 * any other undefined divs or if any known div is defined in
1283 * terms of the unknown div.
1285 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1286 int div, int ineq)
1288 int j;
1289 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1291 /* Not defined in terms of unknown divs */
1292 for (j = 0; j < bmap->n_div; ++j) {
1293 if (div == j)
1294 continue;
1295 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1296 continue;
1297 if (isl_int_is_zero(bmap->div[j][0]))
1298 return isl_bool_false;
1301 /* No other div defined in terms of this one => avoid loops */
1302 for (j = 0; j < bmap->n_div; ++j) {
1303 if (div == j)
1304 continue;
1305 if (isl_int_is_zero(bmap->div[j][0]))
1306 continue;
1307 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1308 return isl_bool_false;
1311 return isl_bool_true;
1314 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1315 * be a better expression than the current one?
1317 * If we do not have any expression yet, then any expression would be better.
1318 * Otherwise we check if the last variable involved in the inequality
1319 * (disregarding the div that it would define) is in an earlier position
1320 * than the last variable involved in the current div expression.
1322 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1323 int div, int ineq)
1325 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1326 int last_div;
1327 int last_ineq;
1329 if (isl_int_is_zero(bmap->div[div][0]))
1330 return isl_bool_true;
1332 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1333 bmap->n_div - (div + 1)) >= 0)
1334 return isl_bool_false;
1336 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1337 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1338 total + bmap->n_div);
1340 return last_ineq < last_div;
1343 /* Is the sequence of "len" coefficients "ineq" equal to "res"
1344 * plus some non-trivial coefficients that are all a multiple of some number
1345 * greater than "sum"?
1346 * If so, this factor is stored in "gcd".
1348 * The current implementation requires the coefficients
1349 * in "res" to appear directly in "ineq", so that "gcd"
1350 * is the gcd of the remaining coefficients.
1351 * The same assumption is used in has_nested_unit_div.
1353 static int is_residue(isl_int *res, isl_int *ineq, isl_int sum, unsigned len,
1354 isl_int *gcd)
1356 int j;
1358 isl_int_set_si(*gcd, 0);
1359 for (j = 0; j < len; ++j) {
1360 if (!isl_int_is_zero(res[1 + j])) {
1361 if (isl_int_eq(res[1 + j], ineq[1 + j]))
1362 continue;
1363 return 0;
1365 if (!isl_int_is_zero(ineq[1 + j])) {
1366 isl_int_gcd(*gcd, *gcd, ineq[1 + j]);
1367 if (isl_int_le(*gcd, sum))
1368 return 0;
1372 return !isl_int_is_zero(*gcd);
1375 /* Is
1377 * (cst - cst2) mod n + sum
1379 * greater than or equal to n?
1381 static int residue_exceeded(isl_int cst, isl_int cst2, isl_int n, isl_int sum)
1383 int exceeded;
1384 isl_int t;
1386 isl_int_init(t);
1387 isl_int_sub(t, cst, cst2);
1388 isl_int_fdiv_r(t, t, n);
1389 isl_int_add(t, t, sum);
1390 exceeded = isl_int_ge(t, n);
1391 isl_int_clear(t);
1393 return exceeded;
1396 /* Given two constraints "k" and "l" that are opposite to each other,
1397 * except for the constant term, with "sum" the sum of these constant terms,
1398 * check if they can be used to simplify any integer division expression.
1400 * In particular, let "k" and "l" be of the form
1402 * f(x) >= 0
1403 * -f(x) + c >= 0
1405 * That is,
1407 * 0 <= f(x) <= c
1409 * Note that the same constraint holds for "k" and "l" interchanged, i.e.,
1411 * 0 <= -f(x) + c <= c
1413 * That is, the reasoning below holds for both "f(x)" and "-f(x) + c".
1415 * If there is an integer division definition of the form
1417 * floor((f(x) + n h(x) + c')/(n * m))
1419 * with
1421 * c' % n < n - c
1423 * then it is equal to
1425 * floor((h(x) + floor(c'/n))/m)
1427 * because
1429 * floor((f(x) + n h(x) + c')/(n * m))
1430 * = floor((f(x) + c' % n + n (h(x) + floor(c'/n)))/(n * m))
1432 * and
1434 * 0 <= f(x) + c' % n < n
1436 * Note that h(x) may be equal to zero, in which case the denominator
1437 * of the integer division can be used as n (and m = 1).
1439 static __isl_give isl_basic_map *check_for_residues_in_divs(
1440 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1441 int *progress)
1443 int i;
1444 int p;
1445 isl_ctx *ctx;
1446 isl_size n_div, total;
1448 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1449 total = isl_basic_map_dim(bmap, isl_dim_all);
1450 if (n_div < 0 || total < 0)
1451 return isl_basic_map_free(bmap);
1453 ctx = isl_basic_map_get_ctx(bmap);
1454 p = isl_seq_last_non_zero(bmap->ineq[k] + 1, total);
1455 for (i = 0; i < n_div; ++i) {
1456 int c;
1457 isl_bool unknown;
1459 unknown = isl_basic_map_div_is_marked_unknown(bmap, i);
1460 if (unknown < 0)
1461 return isl_basic_map_free(bmap);
1462 if (unknown)
1463 continue;
1464 if (isl_int_le(bmap->div[i][0], sum))
1465 continue;
1466 if (isl_int_eq(bmap->div[i][2 + p], bmap->ineq[k][1 + p]))
1467 c = k;
1468 else if (isl_int_eq(bmap->div[i][2 + p], bmap->ineq[l][1 + p]))
1469 c = l;
1470 else
1471 continue;
1473 if (isl_seq_eq(bmap->div[i] + 2, bmap->ineq[c] + 1, total))
1474 isl_int_set(ctx->normalize_gcd, bmap->div[i][0]);
1475 else if (!is_residue(bmap->ineq[c], bmap->div[i] + 1, sum,
1476 total, &ctx->normalize_gcd))
1477 continue;
1479 if (residue_exceeded(bmap->div[i][1], bmap->ineq[c][0],
1480 ctx->normalize_gcd, sum))
1481 continue;
1483 if (!isl_int_is_divisible_by(bmap->div[i][0],
1484 ctx->normalize_gcd))
1485 continue;
1487 isl_seq_sub(bmap->div[i] + 1, bmap->ineq[c], 1 + total);
1488 mark_progress(progress);
1491 return bmap;
1494 /* Is inequality "ineq" of "bmap" a constraint defining an integer division?
1495 * "v_div" is the position of the first local variable.
1496 * "n_div" is the number of local variables.
1498 * A constraint defining an integer division must involve some local variable
1499 * and could only possibly define the last local variable involved since
1500 * it can only be defined in terms of earlier variables.
1502 static isl_bool is_div_constraint(__isl_keep isl_basic_map *bmap, int ineq,
1503 unsigned v_div, unsigned n_div)
1505 int last;
1507 last = isl_seq_last_non_zero(bmap->ineq[ineq] + 1 + v_div, n_div);
1508 if (last < 0)
1509 return isl_bool_false;
1510 return isl_basic_map_is_div_constraint(bmap, bmap->ineq[ineq], last);
1513 /* Does the inequality constraint "ineq" of "bmap" involve nested integer
1514 * divisions with unit coefficient after removing the coefficients
1515 * of inequality constraint "base"?
1516 * "v_div" is the position of the first local variable.
1517 * "n_div" is the number of local variables.
1518 * "n" is the factor with which the constraint will be scaled down.
1520 * Use the same simplifying assumption as "is_residue" that
1521 * the coefficients in "base" appear directly in "ineq".
1522 * Look for an integer division involving nested integer divisions
1523 * that does not appear in "base" and does appear in "ineq"
1524 * with a coefficient equal to "n" (up to a change of sign).
1526 static isl_bool has_nested_unit_div(__isl_keep isl_basic_map *bmap,
1527 int base, int ineq, unsigned v_div, unsigned n_div, isl_int n)
1529 int j;
1531 for (j = 0; j < n_div; ++j) {
1532 isl_bool nested;
1534 if (!isl_int_is_zero(bmap->ineq[base][1 + v_div + j]))
1535 continue;
1536 if (!isl_int_abs_eq(bmap->ineq[ineq][1 + v_div + j], n))
1537 continue;
1538 nested = isl_basic_map_div_expr_involves_vars(bmap, j,
1539 v_div, n_div);
1540 if (nested < 0 || nested)
1541 return nested;
1544 return isl_bool_false;
1547 /* Given two constraints "k" and "l" that are opposite to each other,
1548 * except for the constant term, with "sum" the sum of these constant terms,
1549 * check if they can be used to simplify other constraints.
1550 * Only do this for integer basic maps.
1552 * In particular, let "k" and "l" be of the form
1554 * f(x) >= 0
1555 * -f(x) + c >= 0
1557 * That is,
1559 * 0 <= f(x) <= c
1561 * Note that the same constraint holds for "k" and "l" interchanged, i.e.,
1563 * 0 <= -f(x) + c <= c
1565 * That is, the reasoning below holds for both "f(x)" and "-f(x) + c".
1567 * If there is some other constraint
1569 * g(x) >= 0
1571 * such that
1573 * g(x) - f(x) = n h(x) + c'
1575 * with
1577 * 0 <= c' < n - c
1579 * in particular, for the constant term,
1581 * (g(x) - f(x)) mod n + c < n
1583 * then this other constraint is equivalent to
1585 * h(x) >= 0
1587 * (given "k" and "l") since
1589 * 0 <= f(x) + c' < n
1591 * Note that the constraint does not necessarily need to be scaled down here
1592 * since it would otherwise also be scaled down
1593 * by isl_basic_map_normalize_constraints, but since the scaling factor
1594 * is already known here, it might as well be done immediately.
1595 * Also note that the current implementation only checks for constraints
1596 * where the coefficients of f(x) appear directly in g(x), while it would
1597 * be sufficient for the differences with the corresponding coefficients
1598 * in g(x) to be multiples of n.
1600 * Similarly, if there is an integer division definition of the form
1602 * floor((f(x) + n h(x) + c')/(n * m))
1604 * with
1606 * c' % n < n - c
1608 * then it is equal to
1610 * floor((h(x) + floor(c'/n))/m)
1613 * Do not apply any simplification to constraint(s) defining integer divisions.
1614 * Such constraints would first be simplified to be of the form
1616 * e + c'' >= 0
1618 * and then the integer division definition would be plugged into
1619 * this constraint, resulting in the original constraint and
1620 * causing an infinite loop.
1621 * Simplifying the integer division definitions as well mitigates
1622 * some (possibly all) of this effect, but it is too fragile to rely on
1623 * for avoiding infinite loops.
1625 * If any nested integer divisions are involved, then a similar effect
1626 * may be obtained even on constraints that do not (obviously)
1627 * define an integer division through multiple steps of such substitutions.
1628 * Any constraint that would result in an integer division with nested
1629 * integer divisions and a unit coefficient is therefore also left untouched.
1631 static __isl_give isl_basic_map *check_for_residues(
1632 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1633 int *progress)
1635 int i;
1636 int p;
1637 isl_ctx *ctx;
1638 isl_bool rat;
1639 isl_size n_ineq, total, v_div, n_div;
1641 rat = isl_basic_map_is_rational(bmap);
1642 n_ineq = isl_basic_map_n_inequality(bmap);
1643 total = isl_basic_map_dim(bmap, isl_dim_all);
1644 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1645 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1646 if (rat < 0 || n_ineq < 0 || total < 0 || v_div < 0 || n_div < 0)
1647 return isl_basic_map_free(bmap);
1648 if (rat)
1649 return bmap;
1651 bmap = check_for_residues_in_divs(bmap, k, l, sum, progress);
1652 if (!bmap)
1653 return bmap;
1655 ctx = isl_basic_map_get_ctx(bmap);
1656 p = isl_seq_last_non_zero(bmap->ineq[k] + 1, total);
1657 for (i = 0; i < n_ineq; ++i) {
1658 isl_bool skip;
1659 int c;
1661 if (i == k || i == l)
1662 continue;
1663 if (isl_int_is_zero(bmap->ineq[i][1 + p]))
1664 continue;
1665 skip = is_div_constraint(bmap, i, v_div, n_div);
1666 if (skip < 0)
1667 return isl_basic_map_free(bmap);
1668 if (skip)
1669 continue;
1670 if (isl_int_eq(bmap->ineq[i][1 + p], bmap->ineq[k][1 + p]))
1671 c = k;
1672 else if (isl_int_eq(bmap->ineq[i][1 + p], bmap->ineq[l][1 + p]))
1673 c = l;
1674 else
1675 continue;
1677 if (!is_residue(bmap->ineq[c], bmap->ineq[i], sum, total,
1678 &ctx->normalize_gcd))
1679 continue;
1681 skip = has_nested_unit_div(bmap, c, i, v_div, n_div,
1682 ctx->normalize_gcd);
1683 if (skip < 0)
1684 return isl_basic_map_free(bmap);
1685 if (skip)
1686 continue;
1687 if (residue_exceeded(bmap->ineq[i][0], bmap->ineq[c][0],
1688 ctx->normalize_gcd, sum))
1689 continue;
1691 isl_seq_sub(bmap->ineq[i], bmap->ineq[c], 1 + total);
1692 bmap = scale_down_inequality(bmap, i, ctx->normalize_gcd,
1693 total);
1694 if (!bmap)
1695 return NULL;
1696 mark_progress(progress);
1699 return bmap;
1702 /* Given two constraints "k" and "l" that are opposite to each other,
1703 * except for the constant term, check if we can use them
1704 * to obtain an expression for one of the hitherto unknown divs or
1705 * a "better" expression for a div for which we already have an expression.
1706 * "sum" is the sum of the constant terms of the constraints.
1707 * If this sum is strictly smaller than the coefficient of one
1708 * of the divs, then this pair can be used to define the div.
1709 * To avoid the introduction of circular definitions of divs, we
1710 * do not use the pair if the resulting expression would refer to
1711 * any other undefined divs or if any known div is defined in
1712 * terms of the unknown div.
1714 static __isl_give isl_basic_map *check_for_div_constraints(
1715 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1716 int *progress)
1718 int i;
1719 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1721 for (i = 0; i < bmap->n_div; ++i) {
1722 isl_bool set_div;
1724 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1725 continue;
1726 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1727 continue;
1728 set_div = better_div_constraint(bmap, i, k);
1729 if (set_div >= 0 && set_div)
1730 set_div = ok_to_set_div_from_bound(bmap, i, k);
1731 if (set_div < 0)
1732 return isl_basic_map_free(bmap);
1733 if (!set_div)
1734 break;
1735 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1736 bmap = set_div_from_lower_bound(bmap, i, k);
1737 else
1738 bmap = set_div_from_lower_bound(bmap, i, l);
1739 mark_progress(progress);
1740 break;
1742 return bmap;
1745 /* Look for pairs of constraints that have equal or opposite coefficients.
1746 * For each pair of constraints with equal coefficients, only keep
1747 * the one which imposes the most stringent constraint, i.e.,
1748 * the one with the smallest constant term.
1749 * For each pair of constraints with opposite coefficients,
1750 * consider the sum of the constant terms.
1751 * If the sum is smaller than zero, then the constraints conflict.
1752 * If the sum is equal to zero, then the constraints form
1753 * an equality constraint.
1754 * If the sum is greater than zero, then check whether this pair
1755 * can be used to simplify any other constraints and/or,
1756 * if "detect_divs" is set, whether a (better) integer division definition
1757 * can be read off from the pair.
1759 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1760 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1762 struct isl_constraint_index ci;
1763 int k, l, h;
1764 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1765 isl_int sum;
1767 if (total < 0 || bmap->n_ineq <= 1)
1768 return bmap;
1770 if (create_constraint_index(&ci, bmap) < 0)
1771 return bmap;
1773 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1774 ci.index[h] = &bmap->ineq[0];
1775 for (k = 1; k < bmap->n_ineq; ++k) {
1776 h = hash_index(&ci, bmap, k);
1777 if (!ci.index[h]) {
1778 ci.index[h] = &bmap->ineq[k];
1779 continue;
1781 l = ci.index[h] - &bmap->ineq[0];
1782 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1783 swap_inequality(bmap, k, l);
1784 isl_basic_map_drop_inequality(bmap, k);
1785 --k;
1787 isl_int_init(sum);
1788 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1789 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1790 h = hash_index(&ci, bmap, k);
1791 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1792 if (!ci.index[h])
1793 continue;
1794 l = ci.index[h] - &bmap->ineq[0];
1795 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1796 if (isl_int_is_pos(sum)) {
1797 int residue = 0;
1799 bmap = check_for_residues(bmap, k, l, sum, &residue);
1800 if (detect_divs)
1801 bmap = check_for_div_constraints(bmap, k, l,
1802 sum, progress);
1803 if (!residue)
1804 continue;
1805 mark_progress(progress);
1806 break;
1808 if (isl_int_is_zero(sum)) {
1809 /* We need to break out of the loop after these
1810 * changes since the contents of the hash
1811 * will no longer be valid.
1812 * Plus, we probably we want to regauss first.
1814 mark_progress(progress);
1815 isl_basic_map_drop_inequality(bmap, l);
1816 isl_basic_map_inequality_to_equality(bmap, k);
1817 } else
1818 bmap = isl_basic_map_set_to_empty(bmap);
1819 break;
1821 isl_int_clear(sum);
1823 constraint_index_free(&ci);
1824 return bmap;
1827 /* Detect all pairs of inequalities that form an equality.
1829 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1830 * Call it repeatedly while it is making progress.
1832 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1833 __isl_take isl_basic_map *bmap, int *progress)
1835 int duplicate;
1837 do {
1838 duplicate = 0;
1839 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1840 &duplicate, 0);
1841 if (duplicate)
1842 mark_progress(progress);
1843 } while (duplicate);
1845 return bmap;
1848 /* Given a known integer division "div" that is not integral
1849 * (with denominator 1), eliminate it from the constraints in "bmap"
1850 * where it appears with a (positive or negative) unit coefficient.
1851 * If "progress" is not NULL, then it gets set if the elimination
1852 * results in any changes.
1854 * That is, replace
1856 * floor(e/m) + f >= 0
1858 * by
1860 * e + m f >= 0
1862 * and
1864 * -floor(e/m) + f >= 0
1866 * by
1868 * -e + m f + m - 1 >= 0
1870 * The first conversion is valid because floor(e/m) >= -f is equivalent
1871 * to e/m >= -f because -f is an integral expression.
1872 * The second conversion follows from the fact that
1874 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1877 * Note that one of the div constraints may have been eliminated
1878 * due to being redundant with respect to the constraint that is
1879 * being modified by this function. The modified constraint may
1880 * no longer imply this div constraint, so we add it back to make
1881 * sure we do not lose any information.
1883 static __isl_give isl_basic_map *eliminate_unit_div(
1884 __isl_take isl_basic_map *bmap, int div, int *progress)
1886 int j;
1887 isl_size v_div, dim;
1888 isl_ctx *ctx;
1890 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1891 dim = isl_basic_map_dim(bmap, isl_dim_all);
1892 if (v_div < 0 || dim < 0)
1893 return isl_basic_map_free(bmap);
1895 ctx = isl_basic_map_get_ctx(bmap);
1897 for (j = 0; j < bmap->n_ineq; ++j) {
1898 int s;
1900 if (!isl_int_is_one(bmap->ineq[j][1 + v_div + div]) &&
1901 !isl_int_is_negone(bmap->ineq[j][1 + v_div + div]))
1902 continue;
1904 mark_progress(progress);
1906 s = isl_int_sgn(bmap->ineq[j][1 + v_div + div]);
1907 isl_int_set_si(bmap->ineq[j][1 + v_div + div], 0);
1908 if (s < 0)
1909 isl_seq_combine(bmap->ineq[j],
1910 ctx->negone, bmap->div[div] + 1,
1911 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1912 else
1913 isl_seq_combine(bmap->ineq[j],
1914 ctx->one, bmap->div[div] + 1,
1915 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1916 if (s < 0) {
1917 isl_int_add(bmap->ineq[j][0],
1918 bmap->ineq[j][0], bmap->div[div][0]);
1919 isl_int_sub_ui(bmap->ineq[j][0],
1920 bmap->ineq[j][0], 1);
1923 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1924 bmap = isl_basic_map_add_div_constraint(bmap, div, s);
1925 if (!bmap)
1926 return NULL;
1929 return bmap;
1932 /* Eliminate selected known divs from constraints where they appear with
1933 * a (positive or negative) unit coefficient.
1934 * In particular, only handle those for which "select" returns isl_bool_true.
1935 * If "progress" is not NULL, then it gets set if the elimination
1936 * results in any changes.
1938 * We skip integral divs, i.e., those with denominator 1, as we would
1939 * risk eliminating the div from the div constraints.
1940 * They are eliminated in eliminate_integral_divs instead.
1942 static __isl_give isl_basic_map *eliminate_selected_unit_divs(
1943 __isl_take isl_basic_map *bmap,
1944 isl_bool (*select)(__isl_keep isl_basic_map *bmap, int div),
1945 int *progress)
1947 int i;
1948 isl_size n_div;
1950 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1951 if (n_div < 0)
1952 return isl_basic_map_free(bmap);
1954 for (i = 0; i < n_div; ++i) {
1955 isl_bool skip;
1956 isl_bool selected;
1958 skip = isl_basic_map_div_is_marked_unknown(bmap, i);
1959 if (skip >= 0 && !skip)
1960 skip = isl_basic_map_div_is_integral(bmap, i);
1961 if (skip < 0)
1962 return isl_basic_map_free(bmap);
1963 if (skip)
1964 continue;
1965 selected = select(bmap, i);
1966 if (selected < 0)
1967 return isl_basic_map_free(bmap);
1968 if (!selected)
1969 continue;
1970 bmap = eliminate_unit_div(bmap, i, progress);
1971 if (!bmap)
1972 return NULL;
1975 return bmap;
1978 /* eliminate_selected_unit_divs callback that selects every
1979 * integer division.
1981 static isl_bool is_any_div(__isl_keep isl_basic_map *bmap, int div)
1983 return isl_bool_true;
1986 /* Eliminate known divs from constraints where they appear with
1987 * a (positive or negative) unit coefficient.
1988 * If "progress" is not NULL, then it gets set if the elimination
1989 * results in any changes.
1991 static __isl_give isl_basic_map *eliminate_unit_divs(
1992 __isl_take isl_basic_map *bmap, int *progress)
1994 return eliminate_selected_unit_divs(bmap, &is_any_div, progress);
1997 /* eliminate_selected_unit_divs callback that selects
1998 * integer divisions that only appear with
1999 * a (positive or negative) unit coefficient
2000 * (outside their div constraints).
2002 static isl_bool is_pure_unit_div(__isl_keep isl_basic_map *bmap, int div)
2004 int i;
2005 isl_size v_div, n_ineq;
2007 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2008 n_ineq = isl_basic_map_n_inequality(bmap);
2009 if (v_div < 0 || n_ineq < 0)
2010 return isl_bool_error;
2012 for (i = 0; i < n_ineq; ++i) {
2013 isl_bool skip;
2015 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2016 continue;
2017 skip = isl_basic_map_is_div_constraint(bmap,
2018 bmap->ineq[i], div);
2019 if (skip < 0)
2020 return isl_bool_error;
2021 if (skip)
2022 continue;
2023 if (!isl_int_is_one(bmap->ineq[i][1 + v_div + div]) &&
2024 !isl_int_is_negone(bmap->ineq[i][1 + v_div + div]))
2025 return isl_bool_false;
2028 return isl_bool_true;
2031 /* Eliminate known divs from constraints where they appear with
2032 * a (positive or negative) unit coefficient,
2033 * but only if they do not appear in any other constraints
2034 * (other than the div constraints).
2036 __isl_give isl_basic_map *isl_basic_map_eliminate_pure_unit_divs(
2037 __isl_take isl_basic_map *bmap)
2039 return eliminate_selected_unit_divs(bmap, &is_pure_unit_div, NULL);
2042 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
2044 int progress = 1;
2045 if (!bmap)
2046 return NULL;
2047 while (progress) {
2048 isl_bool empty;
2050 progress = 0;
2051 empty = isl_basic_map_plain_is_empty(bmap);
2052 if (empty < 0)
2053 return isl_basic_map_free(bmap);
2054 if (empty)
2055 break;
2056 bmap = isl_basic_map_normalize_constraints(bmap);
2057 bmap = reduce_div_coefficients(bmap);
2058 bmap = normalize_div_expressions(bmap);
2059 bmap = remove_duplicate_divs(bmap, &progress);
2060 bmap = eliminate_unit_divs(bmap, &progress);
2061 bmap = eliminate_divs_eq(bmap, &progress);
2062 bmap = eliminate_divs_ineq(bmap, &progress);
2063 bmap = eliminate_integral_divs(bmap, &progress);
2064 bmap = isl_basic_map_gauss(bmap, &progress);
2065 /* requires equalities in normal form */
2066 bmap = normalize_divs(bmap, &progress);
2067 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
2068 &progress, 1);
2070 return bmap;
2073 __isl_give isl_basic_set *isl_basic_set_simplify(
2074 __isl_take isl_basic_set *bset)
2076 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
2080 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
2081 isl_int *constraint, unsigned div)
2083 unsigned pos;
2085 if (!bmap)
2086 return isl_bool_error;
2088 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
2090 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
2091 int neg;
2092 isl_int_sub(bmap->div[div][1],
2093 bmap->div[div][1], bmap->div[div][0]);
2094 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
2095 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
2096 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
2097 isl_int_add(bmap->div[div][1],
2098 bmap->div[div][1], bmap->div[div][0]);
2099 if (!neg)
2100 return isl_bool_false;
2101 if (isl_seq_first_non_zero(constraint+pos+1,
2102 bmap->n_div-div-1) != -1)
2103 return isl_bool_false;
2104 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
2105 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
2106 return isl_bool_false;
2107 if (isl_seq_first_non_zero(constraint+pos+1,
2108 bmap->n_div-div-1) != -1)
2109 return isl_bool_false;
2110 } else
2111 return isl_bool_false;
2113 return isl_bool_true;
2116 /* If the only constraints a div d=floor(f/m)
2117 * appears in are its two defining constraints
2119 * f - m d >=0
2120 * -(f - (m - 1)) + m d >= 0
2122 * then it can safely be removed.
2124 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
2126 int i;
2127 isl_bool involves;
2128 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2129 unsigned pos = 1 + v_div + div;
2131 if (v_div < 0)
2132 return isl_bool_error;
2134 for (i = 0; i < bmap->n_eq; ++i)
2135 if (!isl_int_is_zero(bmap->eq[i][pos]))
2136 return isl_bool_false;
2138 for (i = 0; i < bmap->n_ineq; ++i) {
2139 isl_bool red;
2141 if (isl_int_is_zero(bmap->ineq[i][pos]))
2142 continue;
2143 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
2144 if (red < 0 || !red)
2145 return red;
2148 involves = isl_basic_map_any_div_involves_vars(bmap, v_div + div, 1);
2149 if (involves < 0 || involves)
2150 return isl_bool_not(involves);
2152 return isl_bool_true;
2156 * Remove divs that don't occur in any of the constraints or other divs.
2157 * These can arise when dropping constraints from a basic map or
2158 * when the divs of a basic map have been temporarily aligned
2159 * with the divs of another basic map.
2161 static __isl_give isl_basic_map *remove_redundant_divs(
2162 __isl_take isl_basic_map *bmap)
2164 int i;
2165 isl_size v_div;
2167 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2168 if (v_div < 0)
2169 return isl_basic_map_free(bmap);
2171 for (i = bmap->n_div-1; i >= 0; --i) {
2172 isl_bool redundant;
2174 redundant = div_is_redundant(bmap, i);
2175 if (redundant < 0)
2176 return isl_basic_map_free(bmap);
2177 if (!redundant)
2178 continue;
2179 bmap = isl_basic_map_drop_constraints_involving(bmap,
2180 v_div + i, 1);
2181 bmap = isl_basic_map_drop_div(bmap, i);
2183 return bmap;
2186 /* Mark "bmap" as final, without checking for obviously redundant
2187 * integer divisions. This function should be used when "bmap"
2188 * is known not to involve any such integer divisions.
2190 __isl_give isl_basic_map *isl_basic_map_mark_final(
2191 __isl_take isl_basic_map *bmap)
2193 if (!bmap)
2194 return NULL;
2195 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
2196 return bmap;
2199 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
2201 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
2203 bmap = remove_redundant_divs(bmap);
2204 bmap = isl_basic_map_mark_final(bmap);
2205 return bmap;
2208 __isl_give isl_basic_set *isl_basic_set_finalize(
2209 __isl_take isl_basic_set *bset)
2211 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
2214 /* Remove definition of any div that is defined in terms of the given variable.
2215 * The div itself is not removed. Functions such as
2216 * eliminate_divs_ineq depend on the other divs remaining in place.
2218 static __isl_give isl_basic_map *remove_dependent_vars(
2219 __isl_take isl_basic_map *bmap, int pos)
2221 int i;
2223 if (!bmap)
2224 return NULL;
2226 for (i = 0; i < bmap->n_div; ++i) {
2227 if (isl_int_is_zero(bmap->div[i][0]))
2228 continue;
2229 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
2230 continue;
2231 bmap = isl_basic_map_mark_div_unknown(bmap, i);
2232 if (!bmap)
2233 return NULL;
2235 return bmap;
2238 /* Eliminate the specified variables from the constraints using
2239 * Fourier-Motzkin. The variables themselves are not removed.
2241 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
2242 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
2244 int d;
2245 int i, j, k;
2246 isl_size total;
2247 int need_gauss = 0;
2249 if (n == 0)
2250 return bmap;
2251 total = isl_basic_map_dim(bmap, isl_dim_all);
2252 if (total < 0)
2253 return isl_basic_map_free(bmap);
2255 bmap = isl_basic_map_cow(bmap);
2256 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
2257 bmap = remove_dependent_vars(bmap, d);
2258 if (!bmap)
2259 return NULL;
2261 for (d = pos + n - 1;
2262 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
2263 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
2264 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
2265 int n_lower, n_upper;
2266 if (!bmap)
2267 return NULL;
2268 for (i = 0; i < bmap->n_eq; ++i) {
2269 if (isl_int_is_zero(bmap->eq[i][1+d]))
2270 continue;
2271 bmap = eliminate_var_using_equality(bmap, d,
2272 bmap->eq[i], 0, 1, NULL);
2273 if (isl_basic_map_drop_equality(bmap, i) < 0)
2274 return isl_basic_map_free(bmap);
2275 need_gauss = 1;
2276 break;
2278 if (i < bmap->n_eq)
2279 continue;
2280 n_lower = 0;
2281 n_upper = 0;
2282 for (i = 0; i < bmap->n_ineq; ++i) {
2283 if (isl_int_is_pos(bmap->ineq[i][1+d]))
2284 n_lower++;
2285 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
2286 n_upper++;
2288 bmap = isl_basic_map_extend_constraints(bmap,
2289 0, n_lower * n_upper);
2290 if (!bmap)
2291 goto error;
2292 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2293 int last;
2294 if (isl_int_is_zero(bmap->ineq[i][1+d]))
2295 continue;
2296 last = -1;
2297 for (j = 0; j < i; ++j) {
2298 if (isl_int_is_zero(bmap->ineq[j][1+d]))
2299 continue;
2300 last = j;
2301 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
2302 isl_int_sgn(bmap->ineq[j][1+d]))
2303 continue;
2304 k = isl_basic_map_alloc_inequality(bmap);
2305 if (k < 0)
2306 goto error;
2307 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
2308 1+total);
2309 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
2310 1+d, 1+total, NULL);
2312 isl_basic_map_drop_inequality(bmap, i);
2313 i = last + 1;
2315 if (n_lower > 0 && n_upper > 0) {
2316 bmap = isl_basic_map_normalize_constraints(bmap);
2317 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
2318 NULL, 0);
2319 bmap = isl_basic_map_gauss(bmap, NULL);
2320 bmap = isl_basic_map_remove_redundancies(bmap);
2321 need_gauss = 0;
2322 if (!bmap)
2323 goto error;
2324 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
2325 break;
2328 if (need_gauss)
2329 bmap = isl_basic_map_gauss(bmap, NULL);
2330 return bmap;
2331 error:
2332 isl_basic_map_free(bmap);
2333 return NULL;
2336 __isl_give isl_basic_set *isl_basic_set_eliminate_vars(
2337 __isl_take isl_basic_set *bset, unsigned pos, unsigned n)
2339 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
2340 pos, n));
2343 /* Eliminate the specified n dimensions starting at first from the
2344 * constraints, without removing the dimensions from the space.
2345 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2346 * Otherwise, they are projected out and the original space is restored.
2348 __isl_give isl_basic_map *isl_basic_map_eliminate(
2349 __isl_take isl_basic_map *bmap,
2350 enum isl_dim_type type, unsigned first, unsigned n)
2352 isl_space *space;
2354 if (!bmap)
2355 return NULL;
2356 if (n == 0)
2357 return bmap;
2359 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2360 return isl_basic_map_free(bmap);
2362 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
2363 first += isl_basic_map_offset(bmap, type) - 1;
2364 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
2365 return isl_basic_map_finalize(bmap);
2368 space = isl_basic_map_get_space(bmap);
2369 bmap = isl_basic_map_project_out(bmap, type, first, n);
2370 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
2371 bmap = isl_basic_map_reset_space(bmap, space);
2372 return bmap;
2375 __isl_give isl_basic_set *isl_basic_set_eliminate(
2376 __isl_take isl_basic_set *bset,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 return isl_basic_map_eliminate(bset, type, first, n);
2382 /* Remove all constraints from "bmap" that reference any unknown local
2383 * variables (directly or indirectly).
2385 * Dropping all constraints on a local variable will make it redundant,
2386 * so it will get removed implicitly by
2387 * isl_basic_map_drop_constraints_involving_dims. Some other local
2388 * variables may also end up becoming redundant if they only appear
2389 * in constraints together with the unknown local variable.
2390 * Therefore, start over after calling
2391 * isl_basic_map_drop_constraints_involving_dims.
2393 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_unknown_divs(
2394 __isl_take isl_basic_map *bmap)
2396 isl_bool known;
2397 isl_size n_div;
2398 int i, o_div;
2400 known = isl_basic_map_divs_known(bmap);
2401 if (known < 0)
2402 return isl_basic_map_free(bmap);
2403 if (known)
2404 return bmap;
2406 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2407 if (n_div < 0)
2408 return isl_basic_map_free(bmap);
2409 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
2411 for (i = 0; i < n_div; ++i) {
2412 known = isl_basic_map_div_is_known(bmap, i);
2413 if (known < 0)
2414 return isl_basic_map_free(bmap);
2415 if (known)
2416 continue;
2417 bmap = remove_dependent_vars(bmap, o_div + i);
2418 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
2419 isl_dim_div, i, 1);
2420 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2421 if (n_div < 0)
2422 return isl_basic_map_free(bmap);
2423 i = -1;
2426 return bmap;
2429 /* Remove all constraints from "bset" that reference any unknown local
2430 * variables (directly or indirectly).
2432 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_unknown_divs(
2433 __isl_take isl_basic_set *bset)
2435 isl_basic_map *bmap;
2437 bmap = bset_to_bmap(bset);
2438 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
2439 return bset_from_bmap(bmap);
2442 /* Remove all constraints from "map" that reference any unknown local
2443 * variables (directly or indirectly).
2445 * Since constraints may get dropped from the basic maps,
2446 * they may no longer be disjoint from each other.
2448 __isl_give isl_map *isl_map_drop_constraints_involving_unknown_divs(
2449 __isl_take isl_map *map)
2451 int i;
2452 isl_bool known;
2454 known = isl_map_divs_known(map);
2455 if (known < 0)
2456 return isl_map_free(map);
2457 if (known)
2458 return map;
2460 map = isl_map_cow(map);
2461 if (!map)
2462 return NULL;
2464 for (i = 0; i < map->n; ++i) {
2465 map->p[i] =
2466 isl_basic_map_drop_constraints_involving_unknown_divs(
2467 map->p[i]);
2468 if (!map->p[i])
2469 return isl_map_free(map);
2472 if (map->n > 1)
2473 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2475 return map;
2478 /* Don't assume equalities are in order, because align_divs
2479 * may have changed the order of the divs.
2481 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim,
2482 unsigned len)
2484 int d, i;
2486 for (d = 0; d < len; ++d)
2487 elim[d] = -1;
2488 for (i = 0; i < bmap->n_eq; ++i) {
2489 for (d = len - 1; d >= 0; --d) {
2490 if (isl_int_is_zero(bmap->eq[i][1+d]))
2491 continue;
2492 elim[d] = i;
2493 break;
2498 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
2499 int *elim, unsigned len)
2501 compute_elimination_index(bset_to_bmap(bset), elim, len);
2504 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2505 __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
2507 int d;
2508 int copied = 0;
2510 for (d = total - 1; d >= 0; --d) {
2511 if (isl_int_is_zero(src[1+d]))
2512 continue;
2513 if (elim[d] == -1)
2514 continue;
2515 if (!copied) {
2516 isl_seq_cpy(dst, src, 1 + total);
2517 copied = 1;
2519 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2521 return copied;
2524 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2525 __isl_keep isl_basic_set *bset, int *elim, unsigned total)
2527 return reduced_using_equalities(dst, src,
2528 bset_to_bmap(bset), elim, total);
2531 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
2532 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2534 int i;
2535 int *elim;
2536 isl_size dim;
2538 if (!bset || !context)
2539 goto error;
2541 if (context->n_eq == 0) {
2542 isl_basic_set_free(context);
2543 return bset;
2546 bset = isl_basic_set_cow(bset);
2547 dim = isl_basic_set_dim(bset, isl_dim_set);
2548 if (dim < 0)
2549 goto error;
2551 elim = isl_alloc_array(bset->ctx, int, dim);
2552 if (!elim)
2553 goto error;
2554 set_compute_elimination_index(context, elim, dim);
2555 for (i = 0; i < bset->n_eq; ++i)
2556 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2557 context, elim, dim);
2558 for (i = 0; i < bset->n_ineq; ++i)
2559 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2560 context, elim, dim);
2561 isl_basic_set_free(context);
2562 free(elim);
2563 bset = isl_basic_set_simplify(bset);
2564 bset = isl_basic_set_finalize(bset);
2565 return bset;
2566 error:
2567 isl_basic_set_free(bset);
2568 isl_basic_set_free(context);
2569 return NULL;
2572 /* For each inequality in "ineq" that is a shifted (more relaxed)
2573 * copy of an inequality in "context", mark the corresponding entry
2574 * in "row" with -1.
2575 * If an inequality only has a non-negative constant term, then
2576 * mark it as well.
2578 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2579 __isl_keep isl_basic_set *context, int *row)
2581 struct isl_constraint_index ci;
2582 isl_size n_ineq, cols;
2583 unsigned total;
2584 int k;
2586 if (!ineq || !context)
2587 return isl_stat_error;
2588 if (context->n_ineq == 0)
2589 return isl_stat_ok;
2590 if (setup_constraint_index(&ci, context) < 0)
2591 return isl_stat_error;
2593 n_ineq = isl_mat_rows(ineq);
2594 cols = isl_mat_cols(ineq);
2595 if (n_ineq < 0 || cols < 0)
2596 return isl_stat_error;
2597 total = cols - 1;
2598 for (k = 0; k < n_ineq; ++k) {
2599 int l;
2600 isl_bool redundant;
2602 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2603 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2604 row[k] = -1;
2605 continue;
2607 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2608 if (redundant < 0)
2609 goto error;
2610 if (!redundant)
2611 continue;
2612 row[k] = -1;
2614 constraint_index_free(&ci);
2615 return isl_stat_ok;
2616 error:
2617 constraint_index_free(&ci);
2618 return isl_stat_error;
2621 static __isl_give isl_basic_set *remove_shifted_constraints(
2622 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
2624 struct isl_constraint_index ci;
2625 int k;
2627 if (!bset || !context)
2628 return bset;
2630 if (context->n_ineq == 0)
2631 return bset;
2632 if (setup_constraint_index(&ci, context) < 0)
2633 return bset;
2635 for (k = 0; k < bset->n_ineq; ++k) {
2636 isl_bool redundant;
2638 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2639 if (redundant < 0)
2640 goto error;
2641 if (!redundant)
2642 continue;
2643 bset = isl_basic_set_cow(bset);
2644 if (!bset)
2645 goto error;
2646 isl_basic_set_drop_inequality(bset, k);
2647 --k;
2649 constraint_index_free(&ci);
2650 return bset;
2651 error:
2652 constraint_index_free(&ci);
2653 return bset;
2656 /* Remove constraints from "bmap" that are identical to constraints
2657 * in "context" or that are more relaxed (greater constant term).
2659 * We perform the test for shifted copies on the pure constraints
2660 * in remove_shifted_constraints.
2662 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2663 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2665 isl_basic_set *bset, *bset_context;
2667 if (!bmap || !context)
2668 goto error;
2670 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2671 isl_basic_map_free(context);
2672 return bmap;
2675 context = isl_basic_map_drop_constraints_involving_unknown_divs(
2676 context);
2677 context = isl_basic_map_remove_unknown_divs(context);
2679 context = isl_basic_map_order_divs(context);
2680 bmap = isl_basic_map_align_divs(bmap, context);
2681 context = isl_basic_map_align_divs(context, bmap);
2683 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2684 bset_context = isl_basic_map_underlying_set(context);
2685 bset = remove_shifted_constraints(bset, bset_context);
2686 isl_basic_set_free(bset_context);
2688 bmap = isl_basic_map_overlying_set(bset, bmap);
2690 return bmap;
2691 error:
2692 isl_basic_map_free(bmap);
2693 isl_basic_map_free(context);
2694 return NULL;
2697 /* Does the (linear part of a) constraint "c" involve any of the "len"
2698 * "relevant" dimensions?
2700 static int is_related(isl_int *c, int len, int *relevant)
2702 int i;
2704 for (i = 0; i < len; ++i) {
2705 if (!relevant[i])
2706 continue;
2707 if (!isl_int_is_zero(c[i]))
2708 return 1;
2711 return 0;
2714 /* Drop constraints from "bmap" that do not involve any of
2715 * the dimensions marked "relevant".
2717 static __isl_give isl_basic_map *drop_unrelated_constraints(
2718 __isl_take isl_basic_map *bmap, int *relevant)
2720 int i;
2721 isl_size dim;
2723 dim = isl_basic_map_dim(bmap, isl_dim_all);
2724 if (dim < 0)
2725 return isl_basic_map_free(bmap);
2726 for (i = 0; i < dim; ++i)
2727 if (!relevant[i])
2728 break;
2729 if (i >= dim)
2730 return bmap;
2732 for (i = bmap->n_eq - 1; i >= 0; --i)
2733 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2734 bmap = isl_basic_map_cow(bmap);
2735 if (isl_basic_map_drop_equality(bmap, i) < 0)
2736 return isl_basic_map_free(bmap);
2739 for (i = bmap->n_ineq - 1; i >= 0; --i)
2740 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2741 bmap = isl_basic_map_cow(bmap);
2742 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2743 return isl_basic_map_free(bmap);
2746 return bmap;
2749 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2751 * In particular, for any variable involved in the constraint,
2752 * find the actual group id from before and replace the group
2753 * of the corresponding variable by the minimal group of all
2754 * the variables involved in the constraint considered so far
2755 * (if this minimum is smaller) or replace the minimum by this group
2756 * (if the minimum is larger).
2758 * At the end, all the variables in "c" will (indirectly) point
2759 * to the minimal of the groups that they referred to originally.
2761 static void update_groups(int dim, int *group, isl_int *c)
2763 int j;
2764 int min = dim;
2766 for (j = 0; j < dim; ++j) {
2767 if (isl_int_is_zero(c[j]))
2768 continue;
2769 while (group[j] >= 0 && group[group[j]] != group[j])
2770 group[j] = group[group[j]];
2771 if (group[j] == min)
2772 continue;
2773 if (group[j] < min) {
2774 if (min >= 0 && min < dim)
2775 group[min] = group[j];
2776 min = group[j];
2777 } else
2778 group[group[j]] = min;
2782 /* Allocate an array of groups of variables, one for each variable
2783 * in "context", initialized to zero.
2785 static int *alloc_groups(__isl_keep isl_basic_set *context)
2787 isl_ctx *ctx;
2788 isl_size dim;
2790 dim = isl_basic_set_dim(context, isl_dim_set);
2791 if (dim < 0)
2792 return NULL;
2793 ctx = isl_basic_set_get_ctx(context);
2794 return isl_calloc_array(ctx, int, dim);
2797 /* Drop constraints from "bmap" that only involve variables that are
2798 * not related to any of the variables marked with a "-1" in "group".
2800 * We construct groups of variables that collect variables that
2801 * (indirectly) appear in some common constraint of "bmap".
2802 * Each group is identified by the first variable in the group,
2803 * except for the special group of variables that was already identified
2804 * in the input as -1 (or are related to those variables).
2805 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2806 * otherwise the group of i is the group of group[i].
2808 * We first initialize groups for the remaining variables.
2809 * Then we iterate over the constraints of "bmap" and update the
2810 * group of the variables in the constraint by the smallest group.
2811 * Finally, we resolve indirect references to groups by running over
2812 * the variables.
2814 * After computing the groups, we drop constraints that do not involve
2815 * any variables in the -1 group.
2817 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2818 __isl_take isl_basic_map *bmap, __isl_take int *group)
2820 isl_size dim;
2821 int i;
2822 int last;
2824 dim = isl_basic_map_dim(bmap, isl_dim_all);
2825 if (dim < 0)
2826 return isl_basic_map_free(bmap);
2828 last = -1;
2829 for (i = 0; i < dim; ++i)
2830 if (group[i] >= 0)
2831 last = group[i] = i;
2832 if (last < 0) {
2833 free(group);
2834 return bmap;
2837 for (i = 0; i < bmap->n_eq; ++i)
2838 update_groups(dim, group, bmap->eq[i] + 1);
2839 for (i = 0; i < bmap->n_ineq; ++i)
2840 update_groups(dim, group, bmap->ineq[i] + 1);
2842 for (i = 0; i < dim; ++i)
2843 if (group[i] >= 0)
2844 group[i] = group[group[i]];
2846 for (i = 0; i < dim; ++i)
2847 group[i] = group[i] == -1;
2849 bmap = drop_unrelated_constraints(bmap, group);
2851 free(group);
2852 return bmap;
2855 /* Drop constraints from "context" that are irrelevant for computing
2856 * the gist of "bset".
2858 * In particular, drop constraints in variables that are not related
2859 * to any of the variables involved in the constraints of "bset"
2860 * in the sense that there is no sequence of constraints that connects them.
2862 * We first mark all variables that appear in "bset" as belonging
2863 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2865 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2866 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2868 int *group;
2869 isl_size dim;
2870 int i, j;
2872 dim = isl_basic_set_dim(bset, isl_dim_set);
2873 if (!context || dim < 0)
2874 return isl_basic_set_free(context);
2876 group = alloc_groups(context);
2878 if (!group)
2879 return isl_basic_set_free(context);
2881 for (i = 0; i < dim; ++i) {
2882 for (j = 0; j < bset->n_eq; ++j)
2883 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2884 break;
2885 if (j < bset->n_eq) {
2886 group[i] = -1;
2887 continue;
2889 for (j = 0; j < bset->n_ineq; ++j)
2890 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2891 break;
2892 if (j < bset->n_ineq)
2893 group[i] = -1;
2896 return isl_basic_map_drop_unrelated_constraints(context, group);
2899 /* Drop constraints from "context" that are irrelevant for computing
2900 * the gist of the inequalities "ineq".
2901 * Inequalities in "ineq" for which the corresponding element of row
2902 * is set to -1 have already been marked for removal and should be ignored.
2904 * In particular, drop constraints in variables that are not related
2905 * to any of the variables involved in "ineq"
2906 * in the sense that there is no sequence of constraints that connects them.
2908 * We first mark all variables that appear in "bset" as belonging
2909 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2911 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2912 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2914 int *group;
2915 isl_size dim;
2916 int i, j;
2917 isl_size n;
2919 dim = isl_basic_set_dim(context, isl_dim_set);
2920 n = isl_mat_rows(ineq);
2921 if (dim < 0 || n < 0)
2922 return isl_basic_set_free(context);
2924 group = alloc_groups(context);
2926 if (!group)
2927 return isl_basic_set_free(context);
2929 for (i = 0; i < dim; ++i) {
2930 for (j = 0; j < n; ++j) {
2931 if (row[j] < 0)
2932 continue;
2933 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2934 break;
2936 if (j < n)
2937 group[i] = -1;
2940 return isl_basic_map_drop_unrelated_constraints(context, group);
2943 /* Do all "n" entries of "row" contain a negative value?
2945 static int all_neg(int *row, int n)
2947 int i;
2949 for (i = 0; i < n; ++i)
2950 if (row[i] >= 0)
2951 return 0;
2953 return 1;
2956 /* Update the inequalities in "bset" based on the information in "row"
2957 * and "tab".
2959 * In particular, the array "row" contains either -1, meaning that
2960 * the corresponding inequality of "bset" is redundant, or the index
2961 * of an inequality in "tab".
2963 * If the row entry is -1, then drop the inequality.
2964 * Otherwise, if the constraint is marked redundant in the tableau,
2965 * then drop the inequality. Similarly, if it is marked as an equality
2966 * in the tableau, then turn the inequality into an equality and
2967 * perform Gaussian elimination.
2969 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2970 __isl_keep int *row, struct isl_tab *tab)
2972 int i;
2973 unsigned n_ineq;
2974 unsigned n_eq;
2975 int found_equality = 0;
2977 if (!bset)
2978 return NULL;
2979 if (tab && tab->empty)
2980 return isl_basic_set_set_to_empty(bset);
2982 n_ineq = bset->n_ineq;
2983 for (i = n_ineq - 1; i >= 0; --i) {
2984 if (row[i] < 0) {
2985 if (isl_basic_set_drop_inequality(bset, i) < 0)
2986 return isl_basic_set_free(bset);
2987 continue;
2989 if (!tab)
2990 continue;
2991 n_eq = tab->n_eq;
2992 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2993 isl_basic_map_inequality_to_equality(bset, i);
2994 found_equality = 1;
2995 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2996 if (isl_basic_set_drop_inequality(bset, i) < 0)
2997 return isl_basic_set_free(bset);
3001 if (found_equality)
3002 bset = isl_basic_set_gauss(bset, NULL);
3003 bset = isl_basic_set_finalize(bset);
3004 return bset;
3007 /* Update the inequalities in "bset" based on the information in "row"
3008 * and "tab" and free all arguments (other than "bset").
3010 static __isl_give isl_basic_set *update_ineq_free(
3011 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
3012 __isl_take isl_basic_set *context, __isl_take int *row,
3013 struct isl_tab *tab)
3015 isl_mat_free(ineq);
3016 isl_basic_set_free(context);
3018 bset = update_ineq(bset, row, tab);
3020 free(row);
3021 isl_tab_free(tab);
3022 return bset;
3025 /* Remove all information from bset that is redundant in the context
3026 * of context.
3027 * "ineq" contains the (possibly transformed) inequalities of "bset",
3028 * in the same order.
3029 * The (explicit) equalities of "bset" are assumed to have been taken
3030 * into account by the transformation such that only the inequalities
3031 * are relevant.
3032 * "context" is assumed not to be empty.
3034 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
3035 * A value of -1 means that the inequality is obviously redundant and may
3036 * not even appear in "tab".
3038 * We first mark the inequalities of "bset"
3039 * that are obviously redundant with respect to some inequality in "context".
3040 * Then we remove those constraints from "context" that have become
3041 * irrelevant for computing the gist of "bset".
3042 * Note that this removal of constraints cannot be replaced by
3043 * a factorization because factors in "bset" may still be connected
3044 * to each other through constraints in "context".
3046 * If there are any inequalities left, we construct a tableau for
3047 * the context and then add the inequalities of "bset".
3048 * Before adding these inequalities, we freeze all constraints such that
3049 * they won't be considered redundant in terms of the constraints of "bset".
3050 * Then we detect all redundant constraints (among the
3051 * constraints that weren't frozen), first by checking for redundancy in the
3052 * the tableau and then by checking if replacing a constraint by its negation
3053 * would lead to an empty set. This last step is fairly expensive
3054 * and could be optimized by more reuse of the tableau.
3055 * Finally, we update bset according to the results.
3057 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
3058 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
3060 int i, r;
3061 int *row = NULL;
3062 isl_ctx *ctx;
3063 isl_basic_set *combined = NULL;
3064 struct isl_tab *tab = NULL;
3065 unsigned n_eq, context_ineq;
3067 if (!bset || !ineq || !context)
3068 goto error;
3070 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
3071 isl_basic_set_free(context);
3072 isl_mat_free(ineq);
3073 return bset;
3076 ctx = isl_basic_set_get_ctx(context);
3077 row = isl_calloc_array(ctx, int, bset->n_ineq);
3078 if (!row)
3079 goto error;
3081 if (mark_shifted_constraints(ineq, context, row) < 0)
3082 goto error;
3083 if (all_neg(row, bset->n_ineq))
3084 return update_ineq_free(bset, ineq, context, row, NULL);
3086 context = drop_irrelevant_constraints_marked(context, ineq, row);
3087 if (!context)
3088 goto error;
3089 if (isl_basic_set_plain_is_universe(context))
3090 return update_ineq_free(bset, ineq, context, row, NULL);
3092 n_eq = context->n_eq;
3093 context_ineq = context->n_ineq;
3094 combined = isl_basic_set_cow(isl_basic_set_copy(context));
3095 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
3096 tab = isl_tab_from_basic_set(combined, 0);
3097 for (i = 0; i < context_ineq; ++i)
3098 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
3099 goto error;
3100 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
3101 goto error;
3102 r = context_ineq;
3103 for (i = 0; i < bset->n_ineq; ++i) {
3104 if (row[i] < 0)
3105 continue;
3106 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
3107 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
3108 goto error;
3109 row[i] = r++;
3111 if (isl_tab_detect_implicit_equalities(tab) < 0)
3112 goto error;
3113 if (isl_tab_detect_redundant(tab) < 0)
3114 goto error;
3115 for (i = bset->n_ineq - 1; i >= 0; --i) {
3116 isl_basic_set *test;
3117 int is_empty;
3119 if (row[i] < 0)
3120 continue;
3121 r = row[i];
3122 if (tab->con[n_eq + r].is_redundant)
3123 continue;
3124 test = isl_basic_set_dup(combined);
3125 test = isl_inequality_negate(test, r);
3126 test = isl_basic_set_update_from_tab(test, tab);
3127 is_empty = isl_basic_set_is_empty(test);
3128 isl_basic_set_free(test);
3129 if (is_empty < 0)
3130 goto error;
3131 if (is_empty)
3132 tab->con[n_eq + r].is_redundant = 1;
3134 bset = update_ineq_free(bset, ineq, context, row, tab);
3135 if (bset) {
3136 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
3137 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
3140 isl_basic_set_free(combined);
3141 return bset;
3142 error:
3143 free(row);
3144 isl_mat_free(ineq);
3145 isl_tab_free(tab);
3146 isl_basic_set_free(combined);
3147 isl_basic_set_free(context);
3148 isl_basic_set_free(bset);
3149 return NULL;
3152 /* Extract the inequalities of "bset" as an isl_mat.
3154 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
3156 isl_size total;
3157 isl_ctx *ctx;
3158 isl_mat *ineq;
3160 total = isl_basic_set_dim(bset, isl_dim_all);
3161 if (total < 0)
3162 return NULL;
3164 ctx = isl_basic_set_get_ctx(bset);
3165 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
3166 0, 1 + total);
3168 return ineq;
3171 /* Remove all information from "bset" that is redundant in the context
3172 * of "context", for the case where both "bset" and "context" are
3173 * full-dimensional.
3175 static __isl_give isl_basic_set *uset_gist_uncompressed(
3176 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
3178 isl_mat *ineq;
3180 ineq = extract_ineq(bset);
3181 return uset_gist_full(bset, ineq, context);
3184 /* Replace "bset" by an empty basic set in the same space.
3186 static __isl_give isl_basic_set *replace_by_empty(
3187 __isl_take isl_basic_set *bset)
3189 isl_space *space;
3191 space = isl_basic_set_get_space(bset);
3192 isl_basic_set_free(bset);
3193 return isl_basic_set_empty(space);
3196 /* Remove all information from "bset" that is redundant in the context
3197 * of "context", for the case where the combined equalities of
3198 * "bset" and "context" allow for a compression that can be obtained
3199 * by preapplication of "T".
3200 * If the compression of "context" is empty, meaning that "bset" and
3201 * "context" do not intersect, then return the empty set.
3203 * "bset" itself is not transformed by "T". Instead, the inequalities
3204 * are extracted from "bset" and those are transformed by "T".
3205 * uset_gist_full then determines which of the transformed inequalities
3206 * are redundant with respect to the transformed "context" and removes
3207 * the corresponding inequalities from "bset".
3209 * After preapplying "T" to the inequalities, any common factor is
3210 * removed from the coefficients. If this results in a tightening
3211 * of the constant term, then the same tightening is applied to
3212 * the corresponding untransformed inequality in "bset".
3213 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
3215 * g f'(x) + r >= 0
3217 * with 0 <= r < g, then it is equivalent to
3219 * f'(x) >= 0
3221 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
3222 * subspace compressed by T since the latter would be transformed to
3224 * g f'(x) >= 0
3226 static __isl_give isl_basic_set *uset_gist_compressed(
3227 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
3228 __isl_take isl_mat *T)
3230 isl_ctx *ctx;
3231 isl_mat *ineq;
3232 int i;
3233 isl_size n_row, n_col;
3234 isl_int rem;
3236 ineq = extract_ineq(bset);
3237 ineq = isl_mat_product(ineq, isl_mat_copy(T));
3238 context = isl_basic_set_preimage(context, T);
3240 if (!ineq || !context)
3241 goto error;
3242 if (isl_basic_set_plain_is_empty(context)) {
3243 isl_mat_free(ineq);
3244 isl_basic_set_free(context);
3245 return replace_by_empty(bset);
3248 ctx = isl_mat_get_ctx(ineq);
3249 n_row = isl_mat_rows(ineq);
3250 n_col = isl_mat_cols(ineq);
3251 if (n_row < 0 || n_col < 0)
3252 goto error;
3253 isl_int_init(rem);
3254 for (i = 0; i < n_row; ++i) {
3255 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
3256 if (isl_int_is_zero(ctx->normalize_gcd))
3257 continue;
3258 if (isl_int_is_one(ctx->normalize_gcd))
3259 continue;
3260 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
3261 ctx->normalize_gcd, n_col - 1);
3262 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
3263 isl_int_fdiv_q(ineq->row[i][0],
3264 ineq->row[i][0], ctx->normalize_gcd);
3265 if (isl_int_is_zero(rem))
3266 continue;
3267 bset = isl_basic_set_cow(bset);
3268 if (!bset)
3269 break;
3270 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
3272 isl_int_clear(rem);
3274 return uset_gist_full(bset, ineq, context);
3275 error:
3276 isl_mat_free(ineq);
3277 isl_basic_set_free(context);
3278 isl_basic_set_free(bset);
3279 return NULL;
3282 /* Project "bset" onto the variables that are involved in "template".
3284 static __isl_give isl_basic_set *project_onto_involved(
3285 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
3287 int i;
3288 isl_size n;
3290 n = isl_basic_set_dim(template, isl_dim_set);
3291 if (n < 0 || !template)
3292 return isl_basic_set_free(bset);
3294 for (i = 0; i < n; ++i) {
3295 isl_bool involved;
3297 involved = isl_basic_set_involves_dims(template,
3298 isl_dim_set, i, 1);
3299 if (involved < 0)
3300 return isl_basic_set_free(bset);
3301 if (involved)
3302 continue;
3303 bset = isl_basic_set_eliminate_vars(bset, i, 1);
3306 return bset;
3309 /* Remove all information from bset that is redundant in the context
3310 * of context. In particular, equalities that are linear combinations
3311 * of those in context are removed. Then the inequalities that are
3312 * redundant in the context of the equalities and inequalities of
3313 * context are removed.
3315 * First of all, we drop those constraints from "context"
3316 * that are irrelevant for computing the gist of "bset".
3317 * Alternatively, we could factorize the intersection of "context" and "bset".
3319 * We first compute the intersection of the integer affine hulls
3320 * of "bset" and "context",
3321 * compute the gist inside this intersection and then reduce
3322 * the constraints with respect to the equalities of the context
3323 * that only involve variables already involved in the input.
3324 * If the intersection of the affine hulls turns out to be empty,
3325 * then return the empty set.
3327 * If two constraints are mutually redundant, then uset_gist_full
3328 * will remove the second of those constraints. We therefore first
3329 * sort the constraints so that constraints not involving existentially
3330 * quantified variables are given precedence over those that do.
3331 * We have to perform this sorting before the variable compression,
3332 * because that may effect the order of the variables.
3334 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
3335 __isl_take isl_basic_set *context)
3337 isl_mat *eq;
3338 isl_mat *T;
3339 isl_basic_set *aff;
3340 isl_basic_set *aff_context;
3341 isl_size total;
3343 total = isl_basic_set_dim(bset, isl_dim_all);
3344 if (total < 0 || !context)
3345 goto error;
3347 context = drop_irrelevant_constraints(context, bset);
3349 bset = isl_basic_set_detect_equalities(bset);
3350 aff = isl_basic_set_copy(bset);
3351 aff = isl_basic_set_plain_affine_hull(aff);
3352 context = isl_basic_set_detect_equalities(context);
3353 aff_context = isl_basic_set_copy(context);
3354 aff_context = isl_basic_set_plain_affine_hull(aff_context);
3355 aff = isl_basic_set_intersect(aff, aff_context);
3356 if (!aff)
3357 goto error;
3358 if (isl_basic_set_plain_is_empty(aff)) {
3359 isl_basic_set_free(bset);
3360 isl_basic_set_free(context);
3361 return aff;
3363 bset = isl_basic_set_sort_constraints(bset);
3364 if (aff->n_eq == 0) {
3365 isl_basic_set_free(aff);
3366 return uset_gist_uncompressed(bset, context);
3368 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
3369 eq = isl_mat_cow(eq);
3370 T = isl_mat_variable_compression(eq, NULL);
3371 isl_basic_set_free(aff);
3372 if (T && T->n_col == 0) {
3373 isl_mat_free(T);
3374 isl_basic_set_free(context);
3375 return replace_by_empty(bset);
3378 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
3379 aff_context = project_onto_involved(aff_context, bset);
3381 bset = uset_gist_compressed(bset, context, T);
3382 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
3384 if (bset) {
3385 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
3386 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
3389 return bset;
3390 error:
3391 isl_basic_set_free(bset);
3392 isl_basic_set_free(context);
3393 return NULL;
3396 /* Return the number of equality constraints in "bmap" that involve
3397 * local variables. This function assumes that Gaussian elimination
3398 * has been applied to the equality constraints.
3400 static int n_div_eq(__isl_keep isl_basic_map *bmap)
3402 int i;
3403 isl_size total, n_div;
3405 if (!bmap)
3406 return -1;
3408 if (bmap->n_eq == 0)
3409 return 0;
3411 total = isl_basic_map_dim(bmap, isl_dim_all);
3412 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3413 if (total < 0 || n_div < 0)
3414 return -1;
3415 total -= n_div;
3417 for (i = 0; i < bmap->n_eq; ++i)
3418 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
3419 n_div) == -1)
3420 return i;
3422 return bmap->n_eq;
3425 /* Construct a basic map in "space" defined by the equality constraints in "eq".
3426 * The constraints are assumed not to involve any local variables.
3428 static __isl_give isl_basic_map *basic_map_from_equalities(
3429 __isl_take isl_space *space, __isl_take isl_mat *eq)
3431 int i, k;
3432 isl_size total;
3433 isl_basic_map *bmap = NULL;
3435 total = isl_space_dim(space, isl_dim_all);
3436 if (total < 0 || !eq)
3437 goto error;
3439 if (1 + total != eq->n_col)
3440 isl_die(isl_space_get_ctx(space), isl_error_internal,
3441 "unexpected number of columns", goto error);
3443 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
3444 0, eq->n_row, 0);
3445 for (i = 0; i < eq->n_row; ++i) {
3446 k = isl_basic_map_alloc_equality(bmap);
3447 if (k < 0)
3448 goto error;
3449 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
3452 isl_space_free(space);
3453 isl_mat_free(eq);
3454 return bmap;
3455 error:
3456 isl_space_free(space);
3457 isl_mat_free(eq);
3458 isl_basic_map_free(bmap);
3459 return NULL;
3462 /* Construct and return a variable compression based on the equality
3463 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3464 * "n1" is the number of (initial) equality constraints in "bmap1"
3465 * that do involve local variables.
3466 * "n2" is the number of (initial) equality constraints in "bmap2"
3467 * that do involve local variables.
3468 * "total" is the total number of other variables.
3469 * This function assumes that Gaussian elimination
3470 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3471 * such that the equality constraints not involving local variables
3472 * are those that start at "n1" or "n2".
3474 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3475 * then simply compute the compression based on the equality constraints
3476 * in the other basic map.
3477 * Otherwise, combine the equality constraints from both into a new
3478 * basic map such that Gaussian elimination can be applied to this combination
3479 * and then construct a variable compression from the resulting
3480 * equality constraints.
3482 static __isl_give isl_mat *combined_variable_compression(
3483 __isl_keep isl_basic_map *bmap1, int n1,
3484 __isl_keep isl_basic_map *bmap2, int n2, int total)
3486 isl_ctx *ctx;
3487 isl_mat *E1, *E2, *V;
3488 isl_basic_map *bmap;
3490 ctx = isl_basic_map_get_ctx(bmap1);
3491 if (bmap1->n_eq == n1) {
3492 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3493 n2, bmap2->n_eq - n2, 0, 1 + total);
3494 return isl_mat_variable_compression(E2, NULL);
3496 if (bmap2->n_eq == n2) {
3497 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3498 n1, bmap1->n_eq - n1, 0, 1 + total);
3499 return isl_mat_variable_compression(E1, NULL);
3501 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3502 n1, bmap1->n_eq - n1, 0, 1 + total);
3503 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3504 n2, bmap2->n_eq - n2, 0, 1 + total);
3505 E1 = isl_mat_concat(E1, E2);
3506 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3507 bmap = isl_basic_map_gauss(bmap, NULL);
3508 if (!bmap)
3509 return NULL;
3510 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3511 V = isl_mat_variable_compression(E1, NULL);
3512 isl_basic_map_free(bmap);
3514 return V;
3517 /* Extract the stride constraints from "bmap", compressed
3518 * with respect to both the stride constraints in "context" and
3519 * the remaining equality constraints in both "bmap" and "context".
3520 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3521 * "context_n_eq" is the number of (initial) stride constraints in "context".
3523 * Let x be all variables in "bmap" (and "context") other than the local
3524 * variables. First compute a variable compression
3526 * x = V x'
3528 * based on the non-stride equality constraints in "bmap" and "context".
3529 * Consider the stride constraints of "context",
3531 * A(x) + B(y) = 0
3533 * with y the local variables and plug in the variable compression,
3534 * resulting in
3536 * A(V x') + B(y) = 0
3538 * Use these constraints to compute a parameter compression on x'
3540 * x' = T x''
3542 * Now consider the stride constraints of "bmap"
3544 * C(x) + D(y) = 0
3546 * and plug in x = V*T x''.
3547 * That is, return A = [C*V*T D].
3549 static __isl_give isl_mat *extract_compressed_stride_constraints(
3550 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3551 __isl_keep isl_basic_map *context, int context_n_eq)
3553 isl_size total, n_div;
3554 isl_ctx *ctx;
3555 isl_mat *A, *B, *T, *V;
3557 total = isl_basic_map_dim(context, isl_dim_all);
3558 n_div = isl_basic_map_dim(context, isl_dim_div);
3559 if (total < 0 || n_div < 0)
3560 return NULL;
3561 total -= n_div;
3563 ctx = isl_basic_map_get_ctx(bmap);
3565 V = combined_variable_compression(bmap, bmap_n_eq,
3566 context, context_n_eq, total);
3568 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3569 B = isl_mat_sub_alloc6(ctx, context->eq,
3570 0, context_n_eq, 1 + total, n_div);
3571 A = isl_mat_product(A, isl_mat_copy(V));
3572 T = isl_mat_parameter_compression_ext(A, B);
3573 T = isl_mat_product(V, T);
3575 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3576 if (n_div < 0)
3577 T = isl_mat_free(T);
3578 else
3579 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3581 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3582 0, bmap_n_eq, 0, 1 + total + n_div);
3583 A = isl_mat_product(A, T);
3585 return A;
3588 /* Remove the prime factors from *g that have an exponent that
3589 * is strictly smaller than the exponent in "c".
3590 * All exponents in *g are known to be smaller than or equal
3591 * to those in "c".
3593 * That is, if *g is equal to
3595 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3597 * and "c" is equal to
3599 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3601 * then update *g to
3603 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3604 * p_n^{e_n * (e_n = f_n)}
3606 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3607 * neither does the gcd of *g and c / *g.
3608 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3609 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3610 * Dividing *g by this gcd therefore strictly reduces the exponent
3611 * of the prime factors that need to be removed, while leaving the
3612 * other prime factors untouched.
3613 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3614 * removes all undesired factors, without removing any others.
3616 static void remove_incomplete_powers(isl_int *g, isl_int c)
3618 isl_int t;
3620 isl_int_init(t);
3621 for (;;) {
3622 isl_int_divexact(t, c, *g);
3623 isl_int_gcd(t, t, *g);
3624 if (isl_int_is_one(t))
3625 break;
3626 isl_int_divexact(*g, *g, t);
3628 isl_int_clear(t);
3631 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3632 * of the same stride constraints in a compressed space that exploits
3633 * all equalities in the context and the other equalities in "bmap".
3635 * If the stride constraints of "bmap" are of the form
3637 * C(x) + D(y) = 0
3639 * then A is of the form
3641 * B(x') + D(y) = 0
3643 * If any of these constraints involves only a single local variable y,
3644 * then the constraint appears as
3646 * f(x) + m y_i = 0
3648 * in "bmap" and as
3650 * h(x') + m y_i = 0
3652 * in "A".
3654 * Let g be the gcd of m and the coefficients of h.
3655 * Then, in particular, g is a divisor of the coefficients of h and
3657 * f(x) = h(x')
3659 * is known to be a multiple of g.
3660 * If some prime factor in m appears with the same exponent in g,
3661 * then it can be removed from m because f(x) is already known
3662 * to be a multiple of g and therefore in particular of this power
3663 * of the prime factors.
3664 * Prime factors that appear with a smaller exponent in g cannot
3665 * be removed from m.
3666 * Let g' be the divisor of g containing all prime factors that
3667 * appear with the same exponent in m and g, then
3669 * f(x) + m y_i = 0
3671 * can be replaced by
3673 * f(x) + m/g' y_i' = 0
3675 * Note that (if g' != 1) this changes the explicit representation
3676 * of y_i to that of y_i', so the integer division at position i
3677 * is marked unknown and later recomputed by a call to
3678 * isl_basic_map_gauss.
3680 static __isl_give isl_basic_map *reduce_stride_constraints(
3681 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3683 int i;
3684 isl_size total, n_div;
3685 int any = 0;
3686 isl_int gcd;
3688 total = isl_basic_map_dim(bmap, isl_dim_all);
3689 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3690 if (total < 0 || n_div < 0 || !A)
3691 return isl_basic_map_free(bmap);
3692 total -= n_div;
3694 isl_int_init(gcd);
3695 for (i = 0; i < n; ++i) {
3696 int div;
3698 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3699 if (div < 0)
3700 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3701 "equality constraints modified unexpectedly",
3702 goto error);
3703 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3704 n_div - div - 1) != -1)
3705 continue;
3706 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3707 goto error;
3708 if (isl_int_is_one(gcd))
3709 continue;
3710 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3711 if (isl_int_is_one(gcd))
3712 continue;
3713 isl_int_divexact(bmap->eq[i][1 + total + div],
3714 bmap->eq[i][1 + total + div], gcd);
3715 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3716 if (!bmap)
3717 goto error;
3718 any = 1;
3720 isl_int_clear(gcd);
3722 if (any)
3723 bmap = isl_basic_map_gauss(bmap, NULL);
3725 return bmap;
3726 error:
3727 isl_int_clear(gcd);
3728 isl_basic_map_free(bmap);
3729 return NULL;
3732 /* Simplify the stride constraints in "bmap" based on
3733 * the remaining equality constraints in "bmap" and all equality
3734 * constraints in "context".
3735 * Only do this if both "bmap" and "context" have stride constraints.
3737 * First extract a copy of the stride constraints in "bmap" in a compressed
3738 * space exploiting all the other equality constraints and then
3739 * use this compressed copy to simplify the original stride constraints.
3741 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3742 __isl_keep isl_basic_map *context)
3744 int bmap_n_eq, context_n_eq;
3745 isl_mat *A;
3747 if (!bmap || !context)
3748 return isl_basic_map_free(bmap);
3750 bmap_n_eq = n_div_eq(bmap);
3751 context_n_eq = n_div_eq(context);
3753 if (bmap_n_eq < 0 || context_n_eq < 0)
3754 return isl_basic_map_free(bmap);
3755 if (bmap_n_eq == 0 || context_n_eq == 0)
3756 return bmap;
3758 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3759 context, context_n_eq);
3760 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3762 isl_mat_free(A);
3764 return bmap;
3767 /* Return a basic map that has the same intersection with "context" as "bmap"
3768 * and that is as "simple" as possible.
3770 * The core computation is performed on the pure constraints.
3771 * When we add back the meaning of the integer divisions, we need
3772 * to (re)introduce the div constraints. If we happen to have
3773 * discovered that some of these integer divisions are equal to
3774 * some affine combination of other variables, then these div
3775 * constraints may end up getting simplified in terms of the equalities,
3776 * resulting in extra inequalities on the other variables that
3777 * may have been removed already or that may not even have been
3778 * part of the input. We try and remove those constraints of
3779 * this form that are most obviously redundant with respect to
3780 * the context. We also remove those div constraints that are
3781 * redundant with respect to the other constraints in the result.
3783 * The stride constraints among the equality constraints in "bmap" are
3784 * also simplified with respecting to the other equality constraints
3785 * in "bmap" and with respect to all equality constraints in "context".
3787 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3788 __isl_take isl_basic_map *context)
3790 isl_basic_set *bset, *eq;
3791 isl_basic_map *eq_bmap;
3792 isl_size total, n_div, n_div_bmap;
3793 unsigned extra, n_eq, n_ineq;
3795 if (!bmap || !context)
3796 goto error;
3798 if (isl_basic_map_plain_is_universe(bmap)) {
3799 isl_basic_map_free(context);
3800 return bmap;
3802 if (isl_basic_map_plain_is_empty(context)) {
3803 isl_space *space = isl_basic_map_get_space(bmap);
3804 isl_basic_map_free(bmap);
3805 isl_basic_map_free(context);
3806 return isl_basic_map_universe(space);
3808 if (isl_basic_map_plain_is_empty(bmap)) {
3809 isl_basic_map_free(context);
3810 return bmap;
3813 bmap = isl_basic_map_remove_redundancies(bmap);
3814 context = isl_basic_map_remove_redundancies(context);
3815 bmap = isl_basic_map_order_divs(bmap);
3816 context = isl_basic_map_align_divs(context, bmap);
3818 n_div = isl_basic_map_dim(context, isl_dim_div);
3819 total = isl_basic_map_dim(bmap, isl_dim_all);
3820 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3821 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3822 goto error;
3823 extra = n_div - n_div_bmap;
3825 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3826 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3827 bset = uset_gist(bset,
3828 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3829 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3831 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3832 isl_basic_set_plain_is_empty(bset)) {
3833 isl_basic_map_free(context);
3834 return isl_basic_map_overlying_set(bset, bmap);
3837 n_eq = bset->n_eq;
3838 n_ineq = bset->n_ineq;
3839 eq = isl_basic_set_copy(bset);
3840 eq = isl_basic_set_cow(eq);
3841 eq = isl_basic_set_free_inequality(eq, n_ineq);
3842 bset = isl_basic_set_free_equality(bset, n_eq);
3844 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3845 eq_bmap = gist_strides(eq_bmap, context);
3846 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3847 bmap = isl_basic_map_overlying_set(bset, bmap);
3848 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3849 bmap = isl_basic_map_remove_redundancies(bmap);
3851 return bmap;
3852 error:
3853 isl_basic_map_free(bmap);
3854 isl_basic_map_free(context);
3855 return NULL;
3859 * Assumes context has no implicit divs.
3861 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3862 __isl_take isl_basic_map *context)
3864 int i;
3866 if (!map || !context)
3867 goto error;
3869 if (isl_basic_map_plain_is_empty(context)) {
3870 isl_space *space = isl_map_get_space(map);
3871 isl_map_free(map);
3872 isl_basic_map_free(context);
3873 return isl_map_universe(space);
3876 context = isl_basic_map_remove_redundancies(context);
3877 map = isl_map_cow(map);
3878 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3879 goto error;
3880 map = isl_map_compute_divs(map);
3881 if (!map)
3882 goto error;
3883 for (i = map->n - 1; i >= 0; --i) {
3884 map->p[i] = isl_basic_map_gist(map->p[i],
3885 isl_basic_map_copy(context));
3886 if (!map->p[i])
3887 goto error;
3888 if (isl_basic_map_plain_is_empty(map->p[i])) {
3889 isl_basic_map_free(map->p[i]);
3890 if (i != map->n - 1)
3891 map->p[i] = map->p[map->n - 1];
3892 map->n--;
3895 isl_basic_map_free(context);
3896 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3897 return map;
3898 error:
3899 isl_map_free(map);
3900 isl_basic_map_free(context);
3901 return NULL;
3904 /* Drop all inequalities from "bmap" that also appear in "context".
3905 * "context" is assumed to have only known local variables and
3906 * the initial local variables of "bmap" are assumed to be the same
3907 * as those of "context".
3908 * The constraints of both "bmap" and "context" are assumed
3909 * to have been sorted using isl_basic_map_sort_constraints.
3911 * Run through the inequality constraints of "bmap" and "context"
3912 * in sorted order.
3913 * If a constraint of "bmap" involves variables not in "context",
3914 * then it cannot appear in "context".
3915 * If a matching constraint is found, it is removed from "bmap".
3917 static __isl_give isl_basic_map *drop_inequalities(
3918 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3920 int i1, i2;
3921 isl_size total, bmap_total;
3922 unsigned extra;
3924 total = isl_basic_map_dim(context, isl_dim_all);
3925 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3926 if (total < 0 || bmap_total < 0)
3927 return isl_basic_map_free(bmap);
3929 extra = bmap_total - total;
3931 i1 = bmap->n_ineq - 1;
3932 i2 = context->n_ineq - 1;
3933 while (bmap && i1 >= 0 && i2 >= 0) {
3934 int cmp;
3936 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3937 extra) != -1) {
3938 --i1;
3939 continue;
3941 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3942 context->ineq[i2]);
3943 if (cmp < 0) {
3944 --i2;
3945 continue;
3947 if (cmp > 0) {
3948 --i1;
3949 continue;
3951 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3952 bmap = isl_basic_map_cow(bmap);
3953 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3954 bmap = isl_basic_map_free(bmap);
3956 --i1;
3957 --i2;
3960 return bmap;
3963 /* Drop all equalities from "bmap" that also appear in "context".
3964 * "context" is assumed to have only known local variables and
3965 * the initial local variables of "bmap" are assumed to be the same
3966 * as those of "context".
3968 * Run through the equality constraints of "bmap" and "context"
3969 * in sorted order.
3970 * If a constraint of "bmap" involves variables not in "context",
3971 * then it cannot appear in "context".
3972 * If a matching constraint is found, it is removed from "bmap".
3974 static __isl_give isl_basic_map *drop_equalities(
3975 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3977 int i1, i2;
3978 isl_size total, bmap_total;
3979 unsigned extra;
3981 total = isl_basic_map_dim(context, isl_dim_all);
3982 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3983 if (total < 0 || bmap_total < 0)
3984 return isl_basic_map_free(bmap);
3986 extra = bmap_total - total;
3988 i1 = bmap->n_eq - 1;
3989 i2 = context->n_eq - 1;
3991 while (bmap && i1 >= 0 && i2 >= 0) {
3992 int last1, last2;
3994 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3995 extra) != -1)
3996 break;
3997 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3998 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3999 if (last1 > last2) {
4000 --i2;
4001 continue;
4003 if (last1 < last2) {
4004 --i1;
4005 continue;
4007 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
4008 bmap = isl_basic_map_cow(bmap);
4009 if (isl_basic_map_drop_equality(bmap, i1) < 0)
4010 bmap = isl_basic_map_free(bmap);
4012 --i1;
4013 --i2;
4016 return bmap;
4019 /* Remove the constraints in "context" from "bmap".
4020 * "context" is assumed to have explicit representations
4021 * for all local variables.
4023 * First align the divs of "bmap" to those of "context" and
4024 * sort the constraints. Then drop all constraints from "bmap"
4025 * that appear in "context".
4027 __isl_give isl_basic_map *isl_basic_map_plain_gist(
4028 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
4030 isl_bool done, known;
4032 done = isl_basic_map_plain_is_universe(context);
4033 if (done == isl_bool_false)
4034 done = isl_basic_map_plain_is_universe(bmap);
4035 if (done == isl_bool_false)
4036 done = isl_basic_map_plain_is_empty(context);
4037 if (done == isl_bool_false)
4038 done = isl_basic_map_plain_is_empty(bmap);
4039 if (done < 0)
4040 goto error;
4041 if (done) {
4042 isl_basic_map_free(context);
4043 return bmap;
4045 known = isl_basic_map_divs_known(context);
4046 if (known < 0)
4047 goto error;
4048 if (!known)
4049 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4050 "context has unknown divs", goto error);
4052 context = isl_basic_map_order_divs(context);
4053 bmap = isl_basic_map_align_divs(bmap, context);
4054 bmap = isl_basic_map_gauss(bmap, NULL);
4055 bmap = isl_basic_map_sort_constraints(bmap);
4056 context = isl_basic_map_sort_constraints(context);
4058 bmap = drop_inequalities(bmap, context);
4059 bmap = drop_equalities(bmap, context);
4061 isl_basic_map_free(context);
4062 bmap = isl_basic_map_finalize(bmap);
4063 return bmap;
4064 error:
4065 isl_basic_map_free(bmap);
4066 isl_basic_map_free(context);
4067 return NULL;
4070 /* Replace "map" by the disjunct at position "pos" and free "context".
4072 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
4073 int pos, __isl_take isl_basic_map *context)
4075 isl_basic_map *bmap;
4077 bmap = isl_basic_map_copy(map->p[pos]);
4078 isl_map_free(map);
4079 isl_basic_map_free(context);
4080 return isl_map_from_basic_map(bmap);
4083 /* Remove the constraints in "context" from "map".
4084 * If any of the disjuncts in the result turns out to be the universe,
4085 * then return this universe.
4086 * "context" is assumed to have explicit representations
4087 * for all local variables.
4089 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
4090 __isl_take isl_basic_map *context)
4092 int i;
4093 isl_bool univ, known;
4095 univ = isl_basic_map_plain_is_universe(context);
4096 if (univ < 0)
4097 goto error;
4098 if (univ) {
4099 isl_basic_map_free(context);
4100 return map;
4102 known = isl_basic_map_divs_known(context);
4103 if (known < 0)
4104 goto error;
4105 if (!known)
4106 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4107 "context has unknown divs", goto error);
4109 map = isl_map_cow(map);
4110 if (!map)
4111 goto error;
4112 for (i = 0; i < map->n; ++i) {
4113 map->p[i] = isl_basic_map_plain_gist(map->p[i],
4114 isl_basic_map_copy(context));
4115 univ = isl_basic_map_plain_is_universe(map->p[i]);
4116 if (univ < 0)
4117 goto error;
4118 if (univ && map->n > 1)
4119 return replace_by_disjunct(map, i, context);
4122 isl_basic_map_free(context);
4123 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4124 if (map->n > 1)
4125 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4126 return map;
4127 error:
4128 isl_map_free(map);
4129 isl_basic_map_free(context);
4130 return NULL;
4133 /* Remove the constraints in "context" from "set".
4134 * If any of the disjuncts in the result turns out to be the universe,
4135 * then return this universe.
4136 * "context" is assumed to have explicit representations
4137 * for all local variables.
4139 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
4140 __isl_take isl_basic_set *context)
4142 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
4143 bset_to_bmap(context)));
4146 /* Remove the constraints in "context" from "map".
4147 * If any of the disjuncts in the result turns out to be the universe,
4148 * then return this universe.
4149 * "context" is assumed to consist of a single disjunct and
4150 * to have explicit representations for all local variables.
4152 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
4153 __isl_take isl_map *context)
4155 isl_basic_map *hull;
4157 hull = isl_map_unshifted_simple_hull(context);
4158 return isl_map_plain_gist_basic_map(map, hull);
4161 /* Replace "map" by a universe map in the same space and free "drop".
4163 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
4164 __isl_take isl_map *drop)
4166 isl_map *res;
4168 res = isl_map_universe(isl_map_get_space(map));
4169 isl_map_free(map);
4170 isl_map_free(drop);
4171 return res;
4174 /* Return a map that has the same intersection with "context" as "map"
4175 * and that is as "simple" as possible.
4177 * If "map" is already the universe, then we cannot make it any simpler.
4178 * Similarly, if "context" is the universe, then we cannot exploit it
4179 * to simplify "map"
4180 * If "map" and "context" are identical to each other, then we can
4181 * return the corresponding universe.
4183 * If either "map" or "context" consists of multiple disjuncts,
4184 * then check if "context" happens to be a subset of "map",
4185 * in which case all constraints can be removed.
4186 * In case of multiple disjuncts, the standard procedure
4187 * may not be able to detect that all constraints can be removed.
4189 * If none of these cases apply, we have to work a bit harder.
4190 * During this computation, we make use of a single disjunct context,
4191 * so if the original context consists of more than one disjunct
4192 * then we need to approximate the context by a single disjunct set.
4193 * Simply taking the simple hull may drop constraints that are
4194 * only implicitly available in each disjunct. We therefore also
4195 * look for constraints among those defining "map" that are valid
4196 * for the context. These can then be used to simplify away
4197 * the corresponding constraints in "map".
4199 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
4200 __isl_take isl_map *context)
4202 int equal;
4203 int is_universe;
4204 isl_size n_disjunct_map, n_disjunct_context;
4205 isl_bool subset;
4206 isl_basic_map *hull;
4208 is_universe = isl_map_plain_is_universe(map);
4209 if (is_universe >= 0 && !is_universe)
4210 is_universe = isl_map_plain_is_universe(context);
4211 if (is_universe < 0)
4212 goto error;
4213 if (is_universe) {
4214 isl_map_free(context);
4215 return map;
4218 isl_map_align_params_bin(&map, &context);
4219 equal = isl_map_plain_is_equal(map, context);
4220 if (equal < 0)
4221 goto error;
4222 if (equal)
4223 return replace_by_universe(map, context);
4225 n_disjunct_map = isl_map_n_basic_map(map);
4226 n_disjunct_context = isl_map_n_basic_map(context);
4227 if (n_disjunct_map < 0 || n_disjunct_context < 0)
4228 goto error;
4229 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
4230 subset = isl_map_is_subset(context, map);
4231 if (subset < 0)
4232 goto error;
4233 if (subset)
4234 return replace_by_universe(map, context);
4237 context = isl_map_compute_divs(context);
4238 if (!context)
4239 goto error;
4240 if (n_disjunct_context == 1) {
4241 hull = isl_map_simple_hull(context);
4242 } else {
4243 isl_ctx *ctx;
4244 isl_map_list *list;
4246 ctx = isl_map_get_ctx(map);
4247 list = isl_map_list_alloc(ctx, 2);
4248 list = isl_map_list_add(list, isl_map_copy(context));
4249 list = isl_map_list_add(list, isl_map_copy(map));
4250 hull = isl_map_unshifted_simple_hull_from_map_list(context,
4251 list);
4253 return isl_map_gist_basic_map(map, hull);
4254 error:
4255 isl_map_free(map);
4256 isl_map_free(context);
4257 return NULL;
4260 __isl_give isl_basic_set *isl_basic_set_gist(__isl_take isl_basic_set *bset,
4261 __isl_take isl_basic_set *context)
4263 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
4264 bset_to_bmap(context)));
4267 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
4268 __isl_take isl_basic_set *context)
4270 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
4271 bset_to_bmap(context)));
4274 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
4275 __isl_take isl_basic_set *context)
4277 isl_space *space = isl_set_get_space(set);
4278 isl_basic_set *dom_context = isl_basic_set_universe(space);
4279 dom_context = isl_basic_set_intersect_params(dom_context, context);
4280 return isl_set_gist_basic_set(set, dom_context);
4283 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
4284 __isl_take isl_set *context)
4286 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
4289 /* Compute the gist of "bmap" with respect to the constraints "context"
4290 * on the domain.
4292 __isl_give isl_basic_map *isl_basic_map_gist_domain(
4293 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
4295 isl_space *space = isl_basic_map_get_space(bmap);
4296 isl_basic_map *bmap_context = isl_basic_map_universe(space);
4298 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
4299 return isl_basic_map_gist(bmap, bmap_context);
4302 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
4303 __isl_take isl_set *context)
4305 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
4306 map_context = isl_map_intersect_domain(map_context, context);
4307 return isl_map_gist(map, map_context);
4310 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
4311 __isl_take isl_set *context)
4313 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
4314 map_context = isl_map_intersect_range(map_context, context);
4315 return isl_map_gist(map, map_context);
4318 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
4319 __isl_take isl_set *context)
4321 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
4322 map_context = isl_map_intersect_params(map_context, context);
4323 return isl_map_gist(map, map_context);
4326 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
4327 __isl_take isl_set *context)
4329 return isl_map_gist_params(set, context);
4332 /* Quick check to see if two basic maps are disjoint.
4333 * In particular, we reduce the equalities and inequalities of
4334 * one basic map in the context of the equalities of the other
4335 * basic map and check if we get a contradiction.
4337 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
4338 __isl_keep isl_basic_map *bmap2)
4340 struct isl_vec *v = NULL;
4341 int *elim = NULL;
4342 isl_size total;
4343 int i;
4345 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4346 return isl_bool_error;
4347 if (bmap1->n_div || bmap2->n_div)
4348 return isl_bool_false;
4349 if (!bmap1->n_eq && !bmap2->n_eq)
4350 return isl_bool_false;
4352 total = isl_space_dim(bmap1->dim, isl_dim_all);
4353 if (total < 0)
4354 return isl_bool_error;
4355 if (total == 0)
4356 return isl_bool_false;
4357 v = isl_vec_alloc(bmap1->ctx, 1 + total);
4358 if (!v)
4359 goto error;
4360 elim = isl_alloc_array(bmap1->ctx, int, total);
4361 if (!elim)
4362 goto error;
4363 compute_elimination_index(bmap1, elim, total);
4364 for (i = 0; i < bmap2->n_eq; ++i) {
4365 int reduced;
4366 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
4367 bmap1, elim, total);
4368 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
4369 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
4370 goto disjoint;
4372 for (i = 0; i < bmap2->n_ineq; ++i) {
4373 int reduced;
4374 reduced = reduced_using_equalities(v->block.data,
4375 bmap2->ineq[i], bmap1, elim, total);
4376 if (reduced && isl_int_is_neg(v->block.data[0]) &&
4377 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
4378 goto disjoint;
4380 compute_elimination_index(bmap2, elim, total);
4381 for (i = 0; i < bmap1->n_ineq; ++i) {
4382 int reduced;
4383 reduced = reduced_using_equalities(v->block.data,
4384 bmap1->ineq[i], bmap2, elim, total);
4385 if (reduced && isl_int_is_neg(v->block.data[0]) &&
4386 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
4387 goto disjoint;
4389 isl_vec_free(v);
4390 free(elim);
4391 return isl_bool_false;
4392 disjoint:
4393 isl_vec_free(v);
4394 free(elim);
4395 return isl_bool_true;
4396 error:
4397 isl_vec_free(v);
4398 free(elim);
4399 return isl_bool_error;
4402 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
4403 __isl_keep isl_basic_set *bset2)
4405 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
4406 bset_to_bmap(bset2));
4409 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
4411 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
4412 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
4413 __isl_keep isl_basic_map *bmap2))
4415 int i, j;
4417 if (!map1 || !map2)
4418 return isl_bool_error;
4420 for (i = 0; i < map1->n; ++i) {
4421 for (j = 0; j < map2->n; ++j) {
4422 isl_bool d = test(map1->p[i], map2->p[j]);
4423 if (d != isl_bool_true)
4424 return d;
4428 return isl_bool_true;
4431 /* Are "map1" and "map2" obviously disjoint, based on information
4432 * that can be derived without looking at the individual basic maps?
4434 * In particular, if one of them is empty or if they live in different spaces
4435 * (ignoring parameters), then they are clearly disjoint.
4437 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
4438 __isl_keep isl_map *map2)
4440 isl_bool disjoint;
4441 isl_bool match;
4443 if (!map1 || !map2)
4444 return isl_bool_error;
4446 disjoint = isl_map_plain_is_empty(map1);
4447 if (disjoint < 0 || disjoint)
4448 return disjoint;
4450 disjoint = isl_map_plain_is_empty(map2);
4451 if (disjoint < 0 || disjoint)
4452 return disjoint;
4454 match = isl_map_tuple_is_equal(map1, isl_dim_in, map2, isl_dim_in);
4455 if (match < 0 || !match)
4456 return match < 0 ? isl_bool_error : isl_bool_true;
4458 match = isl_map_tuple_is_equal(map1, isl_dim_out, map2, isl_dim_out);
4459 if (match < 0 || !match)
4460 return match < 0 ? isl_bool_error : isl_bool_true;
4462 return isl_bool_false;
4465 /* Are "map1" and "map2" obviously disjoint?
4467 * If one of them is empty or if they live in different spaces (ignoring
4468 * parameters), then they are clearly disjoint.
4469 * This is checked by isl_map_plain_is_disjoint_global.
4471 * If they have different parameters, then we skip any further tests.
4473 * If they are obviously equal, but not obviously empty, then we will
4474 * not be able to detect if they are disjoint.
4476 * Otherwise we check if each basic map in "map1" is obviously disjoint
4477 * from each basic map in "map2".
4479 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
4480 __isl_keep isl_map *map2)
4482 isl_bool disjoint;
4483 isl_bool intersect;
4484 isl_bool match;
4486 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4487 if (disjoint < 0 || disjoint)
4488 return disjoint;
4490 match = isl_map_has_equal_params(map1, map2);
4491 if (match < 0 || !match)
4492 return match < 0 ? isl_bool_error : isl_bool_false;
4494 intersect = isl_map_plain_is_equal(map1, map2);
4495 if (intersect < 0 || intersect)
4496 return intersect < 0 ? isl_bool_error : isl_bool_false;
4498 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4501 /* Are "map1" and "map2" disjoint?
4502 * The parameters are assumed to have been aligned.
4504 * In particular, check whether all pairs of basic maps are disjoint.
4506 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
4507 __isl_keep isl_map *map2)
4509 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4512 /* Are "map1" and "map2" disjoint?
4514 * They are disjoint if they are "obviously disjoint" or if one of them
4515 * is empty. Otherwise, they are not disjoint if one of them is universal.
4516 * If the two inputs are (obviously) equal and not empty, then they are
4517 * not disjoint.
4518 * If none of these cases apply, then check if all pairs of basic maps
4519 * are disjoint after aligning the parameters.
4521 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4523 isl_bool disjoint;
4524 isl_bool intersect;
4526 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4527 if (disjoint < 0 || disjoint)
4528 return disjoint;
4530 disjoint = isl_map_is_empty(map1);
4531 if (disjoint < 0 || disjoint)
4532 return disjoint;
4534 disjoint = isl_map_is_empty(map2);
4535 if (disjoint < 0 || disjoint)
4536 return disjoint;
4538 intersect = isl_map_plain_is_universe(map1);
4539 if (intersect < 0 || intersect)
4540 return isl_bool_not(intersect);
4542 intersect = isl_map_plain_is_universe(map2);
4543 if (intersect < 0 || intersect)
4544 return isl_bool_not(intersect);
4546 intersect = isl_map_plain_is_equal(map1, map2);
4547 if (intersect < 0 || intersect)
4548 return isl_bool_not(intersect);
4550 return isl_map_align_params_map_map_and_test(map1, map2,
4551 &isl_map_is_disjoint_aligned);
4554 /* Are "bmap1" and "bmap2" disjoint?
4556 * They are disjoint if they are "obviously disjoint" or if one of them
4557 * is empty. Otherwise, they are not disjoint if one of them is universal.
4558 * If none of these cases apply, we compute the intersection and see if
4559 * the result is empty.
4561 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4562 __isl_keep isl_basic_map *bmap2)
4564 isl_bool disjoint;
4565 isl_bool intersect;
4566 isl_basic_map *test;
4568 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4569 if (disjoint < 0 || disjoint)
4570 return disjoint;
4572 disjoint = isl_basic_map_is_empty(bmap1);
4573 if (disjoint < 0 || disjoint)
4574 return disjoint;
4576 disjoint = isl_basic_map_is_empty(bmap2);
4577 if (disjoint < 0 || disjoint)
4578 return disjoint;
4580 intersect = isl_basic_map_plain_is_universe(bmap1);
4581 if (intersect < 0 || intersect)
4582 return isl_bool_not(intersect);
4584 intersect = isl_basic_map_plain_is_universe(bmap2);
4585 if (intersect < 0 || intersect)
4586 return isl_bool_not(intersect);
4588 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4589 isl_basic_map_copy(bmap2));
4590 disjoint = isl_basic_map_is_empty(test);
4591 isl_basic_map_free(test);
4593 return disjoint;
4596 /* Are "bset1" and "bset2" disjoint?
4598 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4599 __isl_keep isl_basic_set *bset2)
4601 return isl_basic_map_is_disjoint(bset1, bset2);
4604 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4605 __isl_keep isl_set *set2)
4607 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4610 /* Are "set1" and "set2" disjoint?
4612 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4614 return isl_map_is_disjoint(set1, set2);
4617 /* Is "v" equal to 0, 1 or -1?
4619 static int is_zero_or_one(isl_int v)
4621 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4624 /* Are the "n" coefficients starting at "first" of inequality constraints
4625 * "i" and "j" of "bmap" opposite to each other?
4627 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4628 int first, int n)
4630 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4633 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4634 * apart from the constant term?
4636 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4638 isl_size total;
4640 total = isl_basic_map_dim(bmap, isl_dim_all);
4641 if (total < 0)
4642 return isl_bool_error;
4643 return is_opposite_part(bmap, i, j, 1, total);
4646 /* Check if we can combine a given div with lower bound l and upper
4647 * bound u with some other div and if so return that other div.
4648 * Otherwise, return a position beyond the integer divisions.
4649 * Return isl_size_error on error.
4651 * We first check that
4652 * - the bounds are opposites of each other (except for the constant
4653 * term)
4654 * - the bounds do not reference any other div
4655 * - no div is defined in terms of this div
4657 * Let m be the size of the range allowed on the div by the bounds.
4658 * That is, the bounds are of the form
4660 * e <= a <= e + m - 1
4662 * with e some expression in the other variables.
4663 * We look for another div b such that no third div is defined in terms
4664 * of this second div b and such that in any constraint that contains
4665 * a (except for the given lower and upper bound), also contains b
4666 * with a coefficient that is m times that of b.
4667 * That is, all constraints (except for the lower and upper bound)
4668 * are of the form
4670 * e + f (a + m b) >= 0
4672 * Furthermore, in the constraints that only contain b, the coefficient
4673 * of b should be equal to 1 or -1.
4674 * If so, we return b so that "a + m b" can be replaced by
4675 * a single div "c = a + m b".
4677 static isl_size div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4678 unsigned div, unsigned l, unsigned u)
4680 int i, j;
4681 isl_size n_div;
4682 isl_size v_div;
4683 isl_size coalesce;
4684 isl_bool involves, opp;
4686 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4687 if (n_div <= 1)
4688 return n_div;
4689 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4690 if (v_div < 0)
4691 return isl_size_error;
4692 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4693 return n_div;
4694 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4695 n_div - div - 1) != -1)
4696 return n_div;
4697 opp = is_opposite(bmap, l, u);
4698 if (opp < 0 || !opp)
4699 return opp < 0 ? isl_size_error : n_div;
4701 involves = isl_basic_map_any_div_involves_vars(bmap, v_div + div, 1);
4702 if (involves < 0 || involves)
4703 return involves < 0 ? isl_size_error : n_div;
4705 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4706 if (isl_int_is_neg(bmap->ineq[l][0])) {
4707 isl_int_sub(bmap->ineq[l][0],
4708 bmap->ineq[l][0], bmap->ineq[u][0]);
4709 bmap = isl_basic_map_copy(bmap);
4710 bmap = isl_basic_map_set_to_empty(bmap);
4711 isl_basic_map_free(bmap);
4712 return n_div;
4714 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4715 coalesce = n_div;
4716 for (i = 0; i < n_div; ++i) {
4717 if (i == div)
4718 continue;
4719 if (!pairs[i])
4720 continue;
4721 involves = isl_basic_map_any_div_involves_vars(bmap,
4722 v_div + i, 1);
4723 if (involves < 0)
4724 goto error;
4725 if (involves)
4726 continue;
4727 for (j = 0; j < bmap->n_ineq; ++j) {
4728 int valid;
4729 if (j == l || j == u)
4730 continue;
4731 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4732 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4733 continue;
4734 break;
4736 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4737 break;
4738 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4739 bmap->ineq[j][1 + v_div + div],
4740 bmap->ineq[l][0]);
4741 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4742 bmap->ineq[j][1 + v_div + i]);
4743 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4744 bmap->ineq[j][1 + v_div + div],
4745 bmap->ineq[l][0]);
4746 if (!valid)
4747 break;
4749 if (j < bmap->n_ineq)
4750 continue;
4751 coalesce = i;
4752 break;
4754 if (0)
4755 error: coalesce = isl_size_error;
4756 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4757 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4758 return coalesce;
4761 /* Internal data structure used during the construction and/or evaluation of
4762 * an inequality that ensures that a pair of bounds always allows
4763 * for an integer value.
4765 * "tab" is the tableau in which the inequality is evaluated. It may
4766 * be NULL until it is actually needed.
4767 * "v" contains the inequality coefficients.
4768 * "g", "fl" and "fu" are temporary scalars used during the construction and
4769 * evaluation.
4771 struct test_ineq_data {
4772 struct isl_tab *tab;
4773 isl_vec *v;
4774 isl_int g;
4775 isl_int fl;
4776 isl_int fu;
4779 /* Free all the memory allocated by the fields of "data".
4781 static void test_ineq_data_clear(struct test_ineq_data *data)
4783 isl_tab_free(data->tab);
4784 isl_vec_free(data->v);
4785 isl_int_clear(data->g);
4786 isl_int_clear(data->fl);
4787 isl_int_clear(data->fu);
4790 /* Is the inequality stored in data->v satisfied by "bmap"?
4791 * That is, does it only attain non-negative values?
4792 * data->tab is a tableau corresponding to "bmap".
4794 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4795 struct test_ineq_data *data)
4797 isl_ctx *ctx;
4798 enum isl_lp_result res;
4800 ctx = isl_basic_map_get_ctx(bmap);
4801 if (!data->tab)
4802 data->tab = isl_tab_from_basic_map(bmap, 0);
4803 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4804 if (res == isl_lp_error)
4805 return isl_bool_error;
4806 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4809 /* Given a lower and an upper bound on div i, do they always allow
4810 * for an integer value of the given div?
4811 * Determine this property by constructing an inequality
4812 * such that the property is guaranteed when the inequality is nonnegative.
4813 * The lower bound is inequality l, while the upper bound is inequality u.
4814 * The constructed inequality is stored in data->v.
4816 * Let the upper bound be
4818 * -n_u a + e_u >= 0
4820 * and the lower bound
4822 * n_l a + e_l >= 0
4824 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4825 * We have
4827 * - f_u e_l <= f_u f_l g a <= f_l e_u
4829 * Since all variables are integer valued, this is equivalent to
4831 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4833 * If this interval is at least f_u f_l g, then it contains at least
4834 * one integer value for a.
4835 * That is, the test constraint is
4837 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4839 * or
4841 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4843 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4844 * then the constraint can be scaled down by a factor g',
4845 * with the constant term replaced by
4846 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4847 * Note that the result of applying Fourier-Motzkin to this pair
4848 * of constraints is
4850 * f_l e_u + f_u e_l >= 0
4852 * If the constant term of the scaled down version of this constraint,
4853 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4854 * term of the scaled down test constraint, then the test constraint
4855 * is known to hold and no explicit evaluation is required.
4856 * This is essentially the Omega test.
4858 * If the test constraint consists of only a constant term, then
4859 * it is sufficient to look at the sign of this constant term.
4861 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4862 int l, int u, struct test_ineq_data *data)
4864 unsigned offset;
4865 isl_size n_div;
4867 offset = isl_basic_map_offset(bmap, isl_dim_div);
4868 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4869 if (n_div < 0)
4870 return isl_bool_error;
4872 isl_int_gcd(data->g,
4873 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4874 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4875 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4876 isl_int_neg(data->fu, data->fu);
4877 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4878 data->fu, bmap->ineq[l], offset + n_div);
4879 isl_int_mul(data->g, data->g, data->fl);
4880 isl_int_mul(data->g, data->g, data->fu);
4881 isl_int_sub(data->g, data->g, data->fl);
4882 isl_int_sub(data->g, data->g, data->fu);
4883 isl_int_add_ui(data->g, data->g, 1);
4884 isl_int_sub(data->fl, data->v->el[0], data->g);
4886 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4887 if (isl_int_is_zero(data->g))
4888 return isl_int_is_nonneg(data->fl);
4889 if (isl_int_is_one(data->g)) {
4890 isl_int_set(data->v->el[0], data->fl);
4891 return test_ineq_is_satisfied(bmap, data);
4893 isl_int_fdiv_q(data->fl, data->fl, data->g);
4894 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4895 if (isl_int_eq(data->fl, data->v->el[0]))
4896 return isl_bool_true;
4897 isl_int_set(data->v->el[0], data->fl);
4898 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4899 offset - 1 + n_div);
4901 return test_ineq_is_satisfied(bmap, data);
4904 /* Remove more kinds of divs that are not strictly needed.
4905 * In particular, if all pairs of lower and upper bounds on a div
4906 * are such that they allow at least one integer value of the div,
4907 * then we can eliminate the div using Fourier-Motzkin without
4908 * introducing any spurious solutions.
4910 * If at least one of the two constraints has a unit coefficient for the div,
4911 * then the presence of such a value is guaranteed so there is no need to check.
4912 * In particular, the value attained by the bound with unit coefficient
4913 * can serve as this intermediate value.
4915 static __isl_give isl_basic_map *drop_more_redundant_divs(
4916 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4918 isl_ctx *ctx;
4919 struct test_ineq_data data = { NULL, NULL };
4920 unsigned off;
4921 isl_size n_div;
4922 int remove = -1;
4924 isl_int_init(data.g);
4925 isl_int_init(data.fl);
4926 isl_int_init(data.fu);
4928 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4929 if (n_div < 0)
4930 goto error;
4932 ctx = isl_basic_map_get_ctx(bmap);
4933 off = isl_basic_map_offset(bmap, isl_dim_div);
4934 data.v = isl_vec_alloc(ctx, off + n_div);
4935 if (!data.v)
4936 goto error;
4938 while (n > 0) {
4939 int i, l, u;
4940 int best = -1;
4941 isl_bool has_int;
4943 for (i = 0; i < n_div; ++i) {
4944 if (!pairs[i])
4945 continue;
4946 if (best >= 0 && pairs[best] <= pairs[i])
4947 continue;
4948 best = i;
4951 i = best;
4952 for (l = 0; l < bmap->n_ineq; ++l) {
4953 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4954 continue;
4955 if (isl_int_is_one(bmap->ineq[l][off + i]))
4956 continue;
4957 for (u = 0; u < bmap->n_ineq; ++u) {
4958 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4959 continue;
4960 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4961 continue;
4962 has_int = int_between_bounds(bmap, i, l, u,
4963 &data);
4964 if (has_int < 0)
4965 goto error;
4966 if (data.tab && data.tab->empty)
4967 break;
4968 if (!has_int)
4969 break;
4971 if (u < bmap->n_ineq)
4972 break;
4974 if (data.tab && data.tab->empty) {
4975 bmap = isl_basic_map_set_to_empty(bmap);
4976 break;
4978 if (l == bmap->n_ineq) {
4979 remove = i;
4980 break;
4982 pairs[i] = 0;
4983 --n;
4986 test_ineq_data_clear(&data);
4988 free(pairs);
4990 if (remove < 0)
4991 return bmap;
4993 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4994 return isl_basic_map_drop_redundant_divs(bmap);
4995 error:
4996 free(pairs);
4997 isl_basic_map_free(bmap);
4998 test_ineq_data_clear(&data);
4999 return NULL;
5002 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
5003 * and the upper bound u, div1 always occurs together with div2 in the form
5004 * (div1 + m div2), where m is the constant range on the variable div1
5005 * allowed by l and u, replace the pair div1 and div2 by a single
5006 * div that is equal to div1 + m div2.
5008 * The new div will appear in the location that contains div2.
5009 * We need to modify all constraints that contain
5010 * div2 = (div - div1) / m
5011 * The coefficient of div2 is known to be equal to 1 or -1.
5012 * (If a constraint does not contain div2, it will also not contain div1.)
5013 * If the constraint also contains div1, then we know they appear
5014 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
5015 * i.e., the coefficient of div is f.
5017 * Otherwise, we first need to introduce div1 into the constraint.
5018 * Let l be
5020 * div1 + f >=0
5022 * and u
5024 * -div1 + f' >= 0
5026 * A lower bound on div2
5028 * div2 + t >= 0
5030 * can be replaced by
5032 * m div2 + div1 + m t + f >= 0
5034 * An upper bound
5036 * -div2 + t >= 0
5038 * can be replaced by
5040 * -(m div2 + div1) + m t + f' >= 0
5042 * These constraint are those that we would obtain from eliminating
5043 * div1 using Fourier-Motzkin.
5045 * After all constraints have been modified, we drop the lower and upper
5046 * bound and then drop div1.
5047 * Since the new div is only placed in the same location that used
5048 * to store div2, but otherwise has a different meaning, any possible
5049 * explicit representation of the original div2 is removed.
5051 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
5052 unsigned div1, unsigned div2, unsigned l, unsigned u)
5054 isl_ctx *ctx;
5055 isl_int m;
5056 isl_size v_div;
5057 unsigned total;
5058 int i;
5060 ctx = isl_basic_map_get_ctx(bmap);
5062 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5063 if (v_div < 0)
5064 return isl_basic_map_free(bmap);
5065 total = 1 + v_div + bmap->n_div;
5067 isl_int_init(m);
5068 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
5069 isl_int_add_ui(m, m, 1);
5071 for (i = 0; i < bmap->n_ineq; ++i) {
5072 if (i == l || i == u)
5073 continue;
5074 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
5075 continue;
5076 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
5077 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
5078 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
5079 ctx->one, bmap->ineq[l], total);
5080 else
5081 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
5082 ctx->one, bmap->ineq[u], total);
5084 isl_int_set(bmap->ineq[i][1 + v_div + div2],
5085 bmap->ineq[i][1 + v_div + div1]);
5086 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
5089 isl_int_clear(m);
5090 if (l > u) {
5091 isl_basic_map_drop_inequality(bmap, l);
5092 isl_basic_map_drop_inequality(bmap, u);
5093 } else {
5094 isl_basic_map_drop_inequality(bmap, u);
5095 isl_basic_map_drop_inequality(bmap, l);
5097 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
5098 bmap = isl_basic_map_drop_div(bmap, div1);
5099 return bmap;
5102 /* First check if we can coalesce any pair of divs and
5103 * then continue with dropping more redundant divs.
5105 * We loop over all pairs of lower and upper bounds on a div
5106 * with coefficient 1 and -1, respectively, check if there
5107 * is any other div "c" with which we can coalesce the div
5108 * and if so, perform the coalescing.
5110 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
5111 __isl_take isl_basic_map *bmap, int *pairs, int n)
5113 int i, l, u;
5114 isl_size v_div;
5115 isl_size n_div;
5117 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5118 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5119 if (v_div < 0 || n_div < 0)
5120 return isl_basic_map_free(bmap);
5122 for (i = 0; i < n_div; ++i) {
5123 if (!pairs[i])
5124 continue;
5125 for (l = 0; l < bmap->n_ineq; ++l) {
5126 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
5127 continue;
5128 for (u = 0; u < bmap->n_ineq; ++u) {
5129 int c;
5131 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
5132 continue;
5133 c = div_find_coalesce(bmap, pairs, i, l, u);
5134 if (c < 0)
5135 goto error;
5136 if (c >= n_div)
5137 continue;
5138 free(pairs);
5139 bmap = coalesce_divs(bmap, i, c, l, u);
5140 return isl_basic_map_drop_redundant_divs(bmap);
5145 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
5146 free(pairs);
5147 return bmap;
5150 return drop_more_redundant_divs(bmap, pairs, n);
5151 error:
5152 free(pairs);
5153 isl_basic_map_free(bmap);
5154 return NULL;
5157 /* Are the "n" coefficients starting at "first" of inequality constraints
5158 * "i" and "j" of "bmap" equal to each other?
5160 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
5161 int first, int n)
5163 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
5166 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
5167 * apart from the constant term and the coefficient at position "pos"?
5169 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
5170 int pos)
5172 isl_size total;
5174 total = isl_basic_map_dim(bmap, isl_dim_all);
5175 if (total < 0)
5176 return isl_bool_error;
5177 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
5178 is_parallel_part(bmap, i, j, pos + 1, total - pos);
5181 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
5182 * apart from the constant term and the coefficient at position "pos"?
5184 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
5185 int pos)
5187 isl_size total;
5189 total = isl_basic_map_dim(bmap, isl_dim_all);
5190 if (total < 0)
5191 return isl_bool_error;
5192 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
5193 is_opposite_part(bmap, i, j, pos + 1, total - pos);
5196 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
5197 * been modified, simplying it if "simplify" is set.
5198 * Free the temporary data structure "pairs" that was associated
5199 * to the old version of "bmap".
5201 static __isl_give isl_basic_map *drop_redundant_divs_again(
5202 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
5204 if (simplify)
5205 bmap = isl_basic_map_simplify(bmap);
5206 free(pairs);
5207 return isl_basic_map_drop_redundant_divs(bmap);
5210 /* Is "div" the single unknown existentially quantified variable
5211 * in inequality constraint "ineq" of "bmap"?
5212 * "div" is known to have a non-zero coefficient in "ineq".
5214 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
5215 int div)
5217 int i;
5218 isl_size n_div;
5219 unsigned o_div;
5220 isl_bool known;
5222 known = isl_basic_map_div_is_known(bmap, div);
5223 if (known < 0 || known)
5224 return isl_bool_not(known);
5225 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5226 if (n_div < 0)
5227 return isl_bool_error;
5228 if (n_div == 1)
5229 return isl_bool_true;
5230 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5231 for (i = 0; i < n_div; ++i) {
5232 isl_bool known;
5234 if (i == div)
5235 continue;
5236 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
5237 continue;
5238 known = isl_basic_map_div_is_known(bmap, i);
5239 if (known < 0 || !known)
5240 return known;
5243 return isl_bool_true;
5246 /* Does integer division "div" have coefficient 1 in inequality constraint
5247 * "ineq" of "map"?
5249 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
5251 unsigned o_div;
5253 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5254 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
5255 return isl_bool_true;
5257 return isl_bool_false;
5260 /* Turn inequality constraint "ineq" of "bmap" into an equality and
5261 * then try and drop redundant divs again,
5262 * freeing the temporary data structure "pairs" that was associated
5263 * to the old version of "bmap".
5265 static __isl_give isl_basic_map *set_eq_and_try_again(
5266 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
5268 bmap = isl_basic_map_cow(bmap);
5269 isl_basic_map_inequality_to_equality(bmap, ineq);
5270 return drop_redundant_divs_again(bmap, pairs, 1);
5273 /* Drop the integer division at position "div", along with the two
5274 * inequality constraints "ineq1" and "ineq2" in which it appears
5275 * from "bmap" and then try and drop redundant divs again,
5276 * freeing the temporary data structure "pairs" that was associated
5277 * to the old version of "bmap".
5279 static __isl_give isl_basic_map *drop_div_and_try_again(
5280 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
5281 __isl_take int *pairs)
5283 if (ineq1 > ineq2) {
5284 isl_basic_map_drop_inequality(bmap, ineq1);
5285 isl_basic_map_drop_inequality(bmap, ineq2);
5286 } else {
5287 isl_basic_map_drop_inequality(bmap, ineq2);
5288 isl_basic_map_drop_inequality(bmap, ineq1);
5290 bmap = isl_basic_map_drop_div(bmap, div);
5291 return drop_redundant_divs_again(bmap, pairs, 0);
5294 /* Given two inequality constraints
5296 * f(x) + n d + c >= 0, (ineq)
5298 * with d the variable at position "pos", and
5300 * f(x) + c0 >= 0, (lower)
5302 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
5303 * determined by the first constraint.
5304 * That is, store
5306 * ceil((c0 - c)/n)
5308 * in *l.
5310 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
5311 int ineq, int lower, int pos, isl_int *l)
5313 isl_int_neg(*l, bmap->ineq[ineq][0]);
5314 isl_int_add(*l, *l, bmap->ineq[lower][0]);
5315 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
5318 /* Given two inequality constraints
5320 * f(x) + n d + c >= 0, (ineq)
5322 * with d the variable at position "pos", and
5324 * -f(x) - c0 >= 0, (upper)
5326 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
5327 * determined by the first constraint.
5328 * That is, store
5330 * ceil((-c1 - c)/n)
5332 * in *u.
5334 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
5335 int ineq, int upper, int pos, isl_int *u)
5337 isl_int_neg(*u, bmap->ineq[ineq][0]);
5338 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
5339 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
5342 /* Given a lower bound constraint "ineq" on "div" in "bmap",
5343 * does the corresponding lower bound have a fixed value in "bmap"?
5345 * In particular, "ineq" is of the form
5347 * f(x) + n d + c >= 0
5349 * with n > 0, c the constant term and
5350 * d the existentially quantified variable "div".
5351 * That is, the lower bound is
5353 * ceil((-f(x) - c)/n)
5355 * Look for a pair of constraints
5357 * f(x) + c0 >= 0
5358 * -f(x) + c1 >= 0
5360 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
5361 * That is, check that
5363 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
5365 * If so, return the index of inequality f(x) + c0 >= 0.
5366 * Otherwise, return bmap->n_ineq.
5367 * Return -1 on error.
5369 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
5371 int i;
5372 int lower = -1, upper = -1;
5373 unsigned o_div;
5374 isl_int l, u;
5375 int equal;
5377 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5378 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
5379 isl_bool par, opp;
5381 if (i == ineq)
5382 continue;
5383 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
5384 continue;
5385 par = isl_bool_false;
5386 if (lower < 0)
5387 par = is_parallel_except(bmap, ineq, i, o_div + div);
5388 if (par < 0)
5389 return -1;
5390 if (par) {
5391 lower = i;
5392 continue;
5394 opp = isl_bool_false;
5395 if (upper < 0)
5396 opp = is_opposite_except(bmap, ineq, i, o_div + div);
5397 if (opp < 0)
5398 return -1;
5399 if (opp)
5400 upper = i;
5403 if (lower < 0 || upper < 0)
5404 return bmap->n_ineq;
5406 isl_int_init(l);
5407 isl_int_init(u);
5409 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
5410 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
5412 equal = isl_int_eq(l, u);
5414 isl_int_clear(l);
5415 isl_int_clear(u);
5417 return equal ? lower : bmap->n_ineq;
5420 /* Given a lower bound constraint "ineq" on the existentially quantified
5421 * variable "div", such that the corresponding lower bound has
5422 * a fixed value in "bmap", assign this fixed value to the variable and
5423 * then try and drop redundant divs again,
5424 * freeing the temporary data structure "pairs" that was associated
5425 * to the old version of "bmap".
5426 * "lower" determines the constant value for the lower bound.
5428 * In particular, "ineq" is of the form
5430 * f(x) + n d + c >= 0,
5432 * while "lower" is of the form
5434 * f(x) + c0 >= 0
5436 * The lower bound is ceil((-f(x) - c)/n) and its constant value
5437 * is ceil((c0 - c)/n).
5439 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
5440 int div, int ineq, int lower, int *pairs)
5442 isl_int c;
5443 unsigned o_div;
5445 isl_int_init(c);
5447 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5448 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
5449 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
5450 free(pairs);
5452 isl_int_clear(c);
5454 return isl_basic_map_drop_redundant_divs(bmap);
5457 /* Do any of the integer divisions of "bmap" involve integer division "div"?
5459 * The integer division "div" could only ever appear in any later
5460 * integer division (with an explicit representation).
5462 static isl_bool any_div_involves_div(__isl_keep isl_basic_map *bmap, int div)
5464 int i;
5465 isl_size v_div, n_div;
5467 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5468 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5469 if (v_div < 0 || n_div < 0)
5470 return isl_bool_error;
5472 for (i = div + 1; i < n_div; ++i) {
5473 isl_bool involves;
5475 involves = isl_basic_map_div_expr_involves_vars(bmap, i,
5476 v_div + div, 1);
5477 if (involves < 0 || involves)
5478 return involves;
5481 return isl_bool_false;
5484 /* Remove divs that are not strictly needed based on the inequality
5485 * constraints.
5486 * In particular, if a div only occurs positively (or negatively)
5487 * in constraints, then it can simply be dropped.
5488 * Also, if a div occurs in only two constraints and if moreover
5489 * those two constraints are opposite to each other, except for the constant
5490 * term and if the sum of the constant terms is such that for any value
5491 * of the other values, there is always at least one integer value of the
5492 * div, i.e., if one plus this sum is greater than or equal to
5493 * the (absolute value) of the coefficient of the div in the constraints,
5494 * then we can also simply drop the div.
5496 * If an existentially quantified variable does not have an explicit
5497 * representation, appears in only a single lower bound that does not
5498 * involve any other such existentially quantified variables and appears
5499 * in this lower bound with coefficient 1,
5500 * then fix the variable to the value of the lower bound. That is,
5501 * turn the inequality into an equality.
5502 * If for any value of the other variables, there is any value
5503 * for the existentially quantified variable satisfying the constraints,
5504 * then this lower bound also satisfies the constraints.
5505 * It is therefore safe to pick this lower bound.
5507 * The same reasoning holds even if the coefficient is not one.
5508 * However, fixing the variable to the value of the lower bound may
5509 * in general introduce an extra integer division, in which case
5510 * it may be better to pick another value.
5511 * If this integer division has a known constant value, then plugging
5512 * in this constant value removes the existentially quantified variable
5513 * completely. In particular, if the lower bound is of the form
5514 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5515 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5516 * then the existentially quantified variable can be assigned this
5517 * shared value.
5519 * We skip divs that appear in equalities or in the definition of other divs.
5520 * Divs that appear in the definition of other divs usually occur in at least
5521 * 4 constraints, but the constraints may have been simplified.
5523 * If any divs are left after these simple checks then we move on
5524 * to more complicated cases in drop_more_redundant_divs.
5526 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
5527 __isl_take isl_basic_map *bmap)
5529 int i, j;
5530 isl_size off;
5531 int *pairs = NULL;
5532 int n = 0;
5533 isl_size n_ineq;
5535 if (!bmap)
5536 goto error;
5537 if (bmap->n_div == 0)
5538 return bmap;
5540 off = isl_basic_map_var_offset(bmap, isl_dim_div);
5541 if (off < 0)
5542 return isl_basic_map_free(bmap);
5543 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
5544 if (!pairs)
5545 goto error;
5547 n_ineq = isl_basic_map_n_inequality(bmap);
5548 if (n_ineq < 0)
5549 goto error;
5550 for (i = 0; i < bmap->n_div; ++i) {
5551 int pos, neg;
5552 int last_pos, last_neg;
5553 int redundant;
5554 int defined;
5555 isl_bool involves, opp, set_div;
5557 defined = !isl_int_is_zero(bmap->div[i][0]);
5558 involves = any_div_involves_div(bmap, i);
5559 if (involves < 0)
5560 goto error;
5561 if (involves)
5562 continue;
5563 for (j = 0; j < bmap->n_eq; ++j)
5564 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5565 break;
5566 if (j < bmap->n_eq)
5567 continue;
5568 ++n;
5569 pos = neg = 0;
5570 for (j = 0; j < bmap->n_ineq; ++j) {
5571 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5572 last_pos = j;
5573 ++pos;
5575 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5576 last_neg = j;
5577 ++neg;
5580 pairs[i] = pos * neg;
5581 if (pairs[i] == 0) {
5582 for (j = bmap->n_ineq - 1; j >= 0; --j)
5583 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5584 isl_basic_map_drop_inequality(bmap, j);
5585 bmap = isl_basic_map_drop_div(bmap, i);
5586 return drop_redundant_divs_again(bmap, pairs, 0);
5588 if (pairs[i] != 1)
5589 opp = isl_bool_false;
5590 else
5591 opp = is_opposite(bmap, last_pos, last_neg);
5592 if (opp < 0)
5593 goto error;
5594 if (!opp) {
5595 int lower;
5596 isl_bool single, one;
5598 if (pos != 1)
5599 continue;
5600 single = single_unknown(bmap, last_pos, i);
5601 if (single < 0)
5602 goto error;
5603 if (!single)
5604 continue;
5605 one = has_coef_one(bmap, i, last_pos);
5606 if (one < 0)
5607 goto error;
5608 if (one)
5609 return set_eq_and_try_again(bmap, last_pos,
5610 pairs);
5611 lower = lower_bound_is_cst(bmap, i, last_pos);
5612 if (lower < 0)
5613 goto error;
5614 if (lower < n_ineq)
5615 return fix_cst_lower(bmap, i, last_pos, lower,
5616 pairs);
5617 continue;
5620 isl_int_add(bmap->ineq[last_pos][0],
5621 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5622 isl_int_add_ui(bmap->ineq[last_pos][0],
5623 bmap->ineq[last_pos][0], 1);
5624 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5625 bmap->ineq[last_pos][1+off+i]);
5626 isl_int_sub_ui(bmap->ineq[last_pos][0],
5627 bmap->ineq[last_pos][0], 1);
5628 isl_int_sub(bmap->ineq[last_pos][0],
5629 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5630 if (redundant)
5631 return drop_div_and_try_again(bmap, i,
5632 last_pos, last_neg, pairs);
5633 if (defined)
5634 set_div = isl_bool_false;
5635 else
5636 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5637 if (set_div < 0)
5638 return isl_basic_map_free(bmap);
5639 if (set_div) {
5640 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5641 return drop_redundant_divs_again(bmap, pairs, 1);
5643 pairs[i] = 0;
5644 --n;
5647 if (n > 0)
5648 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5650 free(pairs);
5651 return bmap;
5652 error:
5653 free(pairs);
5654 isl_basic_map_free(bmap);
5655 return NULL;
5658 /* Consider the coefficients at "c" as a row vector and replace
5659 * them with their product with "T". "T" is assumed to be a square matrix.
5661 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5663 isl_size n;
5664 isl_ctx *ctx;
5665 isl_vec *v;
5667 n = isl_mat_rows(T);
5668 if (n < 0)
5669 return isl_stat_error;
5670 if (isl_seq_first_non_zero(c, n) == -1)
5671 return isl_stat_ok;
5672 ctx = isl_mat_get_ctx(T);
5673 v = isl_vec_alloc(ctx, n);
5674 if (!v)
5675 return isl_stat_error;
5676 isl_seq_swp_or_cpy(v->el, c, n);
5677 v = isl_vec_mat_product(v, isl_mat_copy(T));
5678 if (!v)
5679 return isl_stat_error;
5680 isl_seq_swp_or_cpy(c, v->el, n);
5681 isl_vec_free(v);
5683 return isl_stat_ok;
5686 /* Plug in T for the variables in "bmap" starting at "pos".
5687 * T is a linear unimodular matrix, i.e., without constant term.
5689 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5690 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5692 int i;
5693 isl_size n_row, n_col;
5695 bmap = isl_basic_map_cow(bmap);
5696 n_row = isl_mat_rows(T);
5697 n_col = isl_mat_cols(T);
5698 if (!bmap || n_row < 0 || n_col < 0)
5699 goto error;
5701 if (n_col != n_row)
5702 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5703 "expecting square matrix", goto error);
5705 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5706 goto error;
5708 for (i = 0; i < bmap->n_eq; ++i)
5709 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5710 goto error;
5711 for (i = 0; i < bmap->n_ineq; ++i)
5712 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5713 goto error;
5714 for (i = 0; i < bmap->n_div; ++i) {
5715 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5716 continue;
5717 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5718 goto error;
5721 isl_mat_free(T);
5722 return bmap;
5723 error:
5724 isl_basic_map_free(bmap);
5725 isl_mat_free(T);
5726 return NULL;
5729 /* Remove divs that are not strictly needed.
5731 * First look for an equality constraint involving two or more
5732 * existentially quantified variables without an explicit
5733 * representation. Replace the combination that appears
5734 * in the equality constraint by a single existentially quantified
5735 * variable such that the equality can be used to derive
5736 * an explicit representation for the variable.
5737 * If there are no more such equality constraints, then continue
5738 * with isl_basic_map_drop_redundant_divs_ineq.
5740 * In particular, if the equality constraint is of the form
5742 * f(x) + \sum_i c_i a_i = 0
5744 * with a_i existentially quantified variable without explicit
5745 * representation, then apply a transformation on the existentially
5746 * quantified variables to turn the constraint into
5748 * f(x) + g a_1' = 0
5750 * with g the gcd of the c_i.
5751 * In order to easily identify which existentially quantified variables
5752 * have a complete explicit representation, i.e., without being defined
5753 * in terms of other existentially quantified variables without
5754 * an explicit representation, the existentially quantified variables
5755 * are first sorted.
5757 * The variable transformation is computed by extending the row
5758 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5760 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5761 * [a_2'] [ a_2 ]
5762 * ... = U ....
5763 * [a_n'] [ a_n ]
5765 * with [c_1/g ... c_n/g] representing the first row of U.
5766 * The inverse of U is then plugged into the original constraints.
5767 * The call to isl_basic_map_simplify makes sure the explicit
5768 * representation for a_1' is extracted from the equality constraint.
5770 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5771 __isl_take isl_basic_map *bmap)
5773 int first;
5774 int i;
5775 unsigned o_div;
5776 isl_size n_div;
5777 int l;
5778 isl_ctx *ctx;
5779 isl_mat *T;
5781 if (!bmap)
5782 return NULL;
5783 if (isl_basic_map_divs_known(bmap))
5784 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5785 if (bmap->n_eq == 0)
5786 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5787 bmap = isl_basic_map_sort_divs(bmap);
5788 if (!bmap)
5789 return NULL;
5791 first = isl_basic_map_first_unknown_div(bmap);
5792 if (first < 0)
5793 return isl_basic_map_free(bmap);
5795 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5796 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5797 if (n_div < 0)
5798 return isl_basic_map_free(bmap);
5800 for (i = 0; i < bmap->n_eq; ++i) {
5801 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5802 n_div - (first));
5803 if (l < 0)
5804 continue;
5805 l += first;
5806 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5807 n_div - (l + 1)) == -1)
5808 continue;
5809 break;
5811 if (i >= bmap->n_eq)
5812 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5814 ctx = isl_basic_map_get_ctx(bmap);
5815 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5816 if (!T)
5817 return isl_basic_map_free(bmap);
5818 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5819 T = isl_mat_normalize_row(T, 0);
5820 T = isl_mat_unimodular_complete(T, 1);
5821 T = isl_mat_right_inverse(T);
5823 for (i = l; i < n_div; ++i)
5824 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5825 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5826 bmap = isl_basic_map_simplify(bmap);
5828 return isl_basic_map_drop_redundant_divs(bmap);
5831 /* Does "bmap" satisfy any equality that involves more than 2 variables
5832 * and/or has coefficients different from -1 and 1?
5834 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5836 int i;
5837 isl_size total;
5839 total = isl_basic_map_dim(bmap, isl_dim_all);
5840 if (total < 0)
5841 return isl_bool_error;
5843 for (i = 0; i < bmap->n_eq; ++i) {
5844 int j, k;
5846 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5847 if (j < 0)
5848 continue;
5849 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5850 !isl_int_is_negone(bmap->eq[i][1 + j]))
5851 return isl_bool_true;
5853 j += 1;
5854 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5855 if (k < 0)
5856 continue;
5857 j += k;
5858 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5859 !isl_int_is_negone(bmap->eq[i][1 + j]))
5860 return isl_bool_true;
5862 j += 1;
5863 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5864 if (k >= 0)
5865 return isl_bool_true;
5868 return isl_bool_false;
5871 /* Remove any common factor g from the constraint coefficients in "v".
5872 * The constant term is stored in the first position and is replaced
5873 * by floor(c/g). If any common factor is removed and if this results
5874 * in a tightening of the constraint, then set *tightened.
5876 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5877 int *tightened)
5879 isl_ctx *ctx;
5881 if (!v)
5882 return NULL;
5883 ctx = isl_vec_get_ctx(v);
5884 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5885 if (isl_int_is_zero(ctx->normalize_gcd))
5886 return v;
5887 if (isl_int_is_one(ctx->normalize_gcd))
5888 return v;
5889 v = isl_vec_cow(v);
5890 if (!v)
5891 return NULL;
5892 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5893 *tightened = 1;
5894 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5895 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5896 v->size - 1);
5897 return v;
5900 /* Internal representation used by isl_basic_map_reduce_coefficients.
5902 * "total" is the total dimensionality of the original basic map.
5903 * "v" is a temporary vector of size 1 + total that can be used
5904 * to store constraint coefficients.
5905 * "T" is the variable compression.
5906 * "T2" is the inverse transformation.
5907 * "tightened" is set if any constant term got tightened
5908 * while reducing the coefficients.
5910 struct isl_reduce_coefficients_data {
5911 isl_size total;
5912 isl_vec *v;
5913 isl_mat *T;
5914 isl_mat *T2;
5915 int tightened;
5918 /* Free all memory allocated in "data".
5920 static void isl_reduce_coefficients_data_clear(
5921 struct isl_reduce_coefficients_data *data)
5923 data->T = isl_mat_free(data->T);
5924 data->T2 = isl_mat_free(data->T2);
5925 data->v = isl_vec_free(data->v);
5928 /* Initialize "data" for "bmap", freeing all allocated memory
5929 * if anything goes wrong.
5931 * In particular, construct a variable compression
5932 * from the equality constraints of "bmap" and
5933 * allocate a temporary vector.
5935 static isl_stat isl_reduce_coefficients_data_init(
5936 __isl_keep isl_basic_map *bmap,
5937 struct isl_reduce_coefficients_data *data)
5939 isl_ctx *ctx;
5940 isl_mat *eq;
5942 data->v = NULL;
5943 data->T = NULL;
5944 data->T2 = NULL;
5945 data->tightened = 0;
5947 data->total = isl_basic_map_dim(bmap, isl_dim_all);
5948 if (data->total < 0)
5949 return isl_stat_error;
5950 ctx = isl_basic_map_get_ctx(bmap);
5951 data->v = isl_vec_alloc(ctx, 1 + data->total);
5952 if (!data->v)
5953 return isl_stat_error;
5955 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq,
5956 0, 1 + data->total);
5957 data->T = isl_mat_variable_compression(eq, &data->T2);
5958 if (!data->T || !data->T2)
5959 goto error;
5961 return isl_stat_ok;
5962 error:
5963 isl_reduce_coefficients_data_clear(data);
5964 return isl_stat_error;
5967 /* Reduce the coefficients of "bmap" by applying the variable compression
5968 * in "data".
5969 * In particular, apply the variable compression to each constraint,
5970 * factor out any common factor in the non-constant coefficients and
5971 * then apply the inverse of the compression.
5973 * Only apply the reduction on a single copy of the basic map
5974 * since the reduction may leave the result in an inconsistent state.
5975 * In particular, the constraints may not be gaussed.
5977 static __isl_give isl_basic_map *reduce_coefficients(
5978 __isl_take isl_basic_map *bmap,
5979 struct isl_reduce_coefficients_data *data)
5981 int i;
5982 isl_size total;
5984 total = isl_basic_map_dim(bmap, isl_dim_all);
5985 if (total < 0)
5986 return isl_basic_map_free(bmap);
5987 if (total != data->total)
5988 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
5989 "total dimensionality changed unexpectedly",
5990 return isl_basic_map_free(bmap));
5992 bmap = isl_basic_map_cow(bmap);
5993 if (!bmap)
5994 return NULL;
5996 for (i = 0; i < bmap->n_ineq; ++i) {
5997 isl_seq_cpy(data->v->el, bmap->ineq[i], 1 + data->total);
5998 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T));
5999 data->v = normalize_constraint(data->v, &data->tightened);
6000 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T2));
6001 if (!data->v)
6002 return isl_basic_map_free(bmap);
6003 isl_seq_cpy(bmap->ineq[i], data->v->el, 1 + data->total);
6006 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
6008 return bmap;
6011 /* If "bmap" is an integer set that satisfies any equality involving
6012 * more than 2 variables and/or has coefficients different from -1 and 1,
6013 * then use variable compression to reduce the coefficients by removing
6014 * any (hidden) common factor.
6015 * In particular, apply the variable compression to each constraint,
6016 * factor out any common factor in the non-constant coefficients and
6017 * then apply the inverse of the compression.
6018 * At the end, we mark the basic map as having reduced constants.
6019 * If this flag is still set on the next invocation of this function,
6020 * then we skip the computation.
6022 * Removing a common factor may result in a tightening of some of
6023 * the constraints. If this happens, then we may end up with two
6024 * opposite inequalities that can be replaced by an equality.
6025 * We therefore call isl_basic_map_detect_inequality_pairs,
6026 * which checks for such pairs of inequalities as well as eliminate_divs_eq
6027 * and isl_basic_map_gauss if such a pair was found.
6028 * This call to isl_basic_map_gauss may undo much of the effect
6029 * of the reduction on which isl_map_coalesce depends.
6030 * In particular, constraints in terms of (compressed) local variables
6031 * get reformulated in terms of the set variables again.
6032 * The reduction is therefore applied again afterwards.
6033 * This has to be done before the call to eliminate_divs_eq, however,
6034 * since that may remove some local variables, while
6035 * the data used during the reduction is formulated in terms
6036 * of the original variables.
6038 * Tightening may also result in some other constraints becoming
6039 * (rationally) redundant with respect to the tightened constraint
6040 * (in combination with other constraints). The basic map may
6041 * therefore no longer be assumed to have no redundant constraints.
6043 * Note that this function may leave the result in an inconsistent state.
6044 * In particular, the constraints may not be gaussed.
6045 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
6046 * for some of the test cases to pass successfully.
6047 * Any potential modification of the representation is therefore only
6048 * performed on a single copy of the basic map.
6050 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
6051 __isl_take isl_basic_map *bmap)
6053 struct isl_reduce_coefficients_data data;
6054 isl_bool multi;
6056 if (!bmap)
6057 return NULL;
6058 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
6059 return bmap;
6060 if (isl_basic_map_is_rational(bmap))
6061 return bmap;
6062 if (bmap->n_eq == 0)
6063 return bmap;
6064 multi = has_multiple_var_equality(bmap);
6065 if (multi < 0)
6066 return isl_basic_map_free(bmap);
6067 if (!multi)
6068 return bmap;
6070 if (isl_reduce_coefficients_data_init(bmap, &data) < 0)
6071 return isl_basic_map_free(bmap);
6073 if (data.T->n_col == 0) {
6074 isl_reduce_coefficients_data_clear(&data);
6075 return isl_basic_map_set_to_empty(bmap);
6078 bmap = reduce_coefficients(bmap, &data);
6079 if (!bmap)
6080 goto error;
6082 if (data.tightened) {
6083 int progress = 0;
6085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
6086 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
6087 if (progress) {
6088 bmap = isl_basic_map_gauss(bmap, NULL);
6089 bmap = reduce_coefficients(bmap, &data);
6090 bmap = eliminate_divs_eq(bmap, &progress);
6094 isl_reduce_coefficients_data_clear(&data);
6096 return bmap;
6097 error:
6098 isl_reduce_coefficients_data_clear(&data);
6099 return isl_basic_map_free(bmap);
6102 /* Shift the integer division at position "div" of "bmap"
6103 * by "shift" times the variable at position "pos".
6104 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
6105 * corresponds to the constant term.
6107 * That is, if the integer division has the form
6109 * floor(f(x)/d)
6111 * then replace it by
6113 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
6115 __isl_give isl_basic_map *isl_basic_map_shift_div(
6116 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
6118 int i;
6119 isl_size total, n_div;
6121 if (isl_int_is_zero(shift))
6122 return bmap;
6123 total = isl_basic_map_dim(bmap, isl_dim_all);
6124 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6125 total -= n_div;
6126 if (total < 0 || n_div < 0)
6127 return isl_basic_map_free(bmap);
6129 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
6131 for (i = 0; i < bmap->n_eq; ++i) {
6132 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
6133 continue;
6134 isl_int_submul(bmap->eq[i][pos],
6135 shift, bmap->eq[i][1 + total + div]);
6137 for (i = 0; i < bmap->n_ineq; ++i) {
6138 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
6139 continue;
6140 isl_int_submul(bmap->ineq[i][pos],
6141 shift, bmap->ineq[i][1 + total + div]);
6143 for (i = 0; i < bmap->n_div; ++i) {
6144 if (isl_int_is_zero(bmap->div[i][0]))
6145 continue;
6146 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
6147 continue;
6148 isl_int_submul(bmap->div[i][1 + pos],
6149 shift, bmap->div[i][1 + 1 + total + div]);
6152 return bmap;