isl_aff.c: extract out pw_multi_aff_from_map_plug_in
[isl.git] / isl_map_simplify.c
blob79a8a2819dccf6ff4b7340e7c0f2cf80c0609018
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 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 /* Given two constraints "k" and "l" that are opposite to each other,
1344 * except for the constant term, check if we can use them
1345 * to obtain an expression for one of the hitherto unknown divs or
1346 * a "better" expression for a div for which we already have an expression.
1347 * "sum" is the sum of the constant terms of the constraints.
1348 * If this sum is strictly smaller than the coefficient of one
1349 * of the divs, then this pair can be used to define the div.
1350 * To avoid the introduction of circular definitions of divs, we
1351 * do not use the pair if the resulting expression would refer to
1352 * any other undefined divs or if any known div is defined in
1353 * terms of the unknown div.
1355 static __isl_give isl_basic_map *check_for_div_constraints(
1356 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1357 int *progress)
1359 int i;
1360 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1362 for (i = 0; i < bmap->n_div; ++i) {
1363 isl_bool set_div;
1365 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1366 continue;
1367 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1368 continue;
1369 set_div = better_div_constraint(bmap, i, k);
1370 if (set_div >= 0 && set_div)
1371 set_div = ok_to_set_div_from_bound(bmap, i, k);
1372 if (set_div < 0)
1373 return isl_basic_map_free(bmap);
1374 if (!set_div)
1375 break;
1376 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1377 bmap = set_div_from_lower_bound(bmap, i, k);
1378 else
1379 bmap = set_div_from_lower_bound(bmap, i, l);
1380 mark_progress(progress);
1381 break;
1383 return bmap;
1386 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1387 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1389 struct isl_constraint_index ci;
1390 int k, l, h;
1391 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1392 isl_int sum;
1394 if (total < 0 || bmap->n_ineq <= 1)
1395 return bmap;
1397 if (create_constraint_index(&ci, bmap) < 0)
1398 return bmap;
1400 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1401 ci.index[h] = &bmap->ineq[0];
1402 for (k = 1; k < bmap->n_ineq; ++k) {
1403 h = hash_index(&ci, bmap, k);
1404 if (!ci.index[h]) {
1405 ci.index[h] = &bmap->ineq[k];
1406 continue;
1408 l = ci.index[h] - &bmap->ineq[0];
1409 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1410 swap_inequality(bmap, k, l);
1411 isl_basic_map_drop_inequality(bmap, k);
1412 --k;
1414 isl_int_init(sum);
1415 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1416 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1417 h = hash_index(&ci, bmap, k);
1418 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1419 if (!ci.index[h])
1420 continue;
1421 l = ci.index[h] - &bmap->ineq[0];
1422 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1423 if (isl_int_is_pos(sum)) {
1424 if (detect_divs)
1425 bmap = check_for_div_constraints(bmap, k, l,
1426 sum, progress);
1427 continue;
1429 if (isl_int_is_zero(sum)) {
1430 /* We need to break out of the loop after these
1431 * changes since the contents of the hash
1432 * will no longer be valid.
1433 * Plus, we probably we want to regauss first.
1435 mark_progress(progress);
1436 isl_basic_map_drop_inequality(bmap, l);
1437 isl_basic_map_inequality_to_equality(bmap, k);
1438 } else
1439 bmap = isl_basic_map_set_to_empty(bmap);
1440 break;
1442 isl_int_clear(sum);
1444 constraint_index_free(&ci);
1445 return bmap;
1448 /* Detect all pairs of inequalities that form an equality.
1450 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1451 * Call it repeatedly while it is making progress.
1453 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1454 __isl_take isl_basic_map *bmap, int *progress)
1456 int duplicate;
1458 do {
1459 duplicate = 0;
1460 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1461 &duplicate, 0);
1462 if (duplicate)
1463 mark_progress(progress);
1464 } while (duplicate);
1466 return bmap;
1469 /* Given a known integer division "div" that is not integral
1470 * (with denominator 1), eliminate it from the constraints in "bmap"
1471 * where it appears with a (positive or negative) unit coefficient.
1472 * If "progress" is not NULL, then it gets set if the elimination
1473 * results in any changes.
1475 * That is, replace
1477 * floor(e/m) + f >= 0
1479 * by
1481 * e + m f >= 0
1483 * and
1485 * -floor(e/m) + f >= 0
1487 * by
1489 * -e + m f + m - 1 >= 0
1491 * The first conversion is valid because floor(e/m) >= -f is equivalent
1492 * to e/m >= -f because -f is an integral expression.
1493 * The second conversion follows from the fact that
1495 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1498 * Note that one of the div constraints may have been eliminated
1499 * due to being redundant with respect to the constraint that is
1500 * being modified by this function. The modified constraint may
1501 * no longer imply this div constraint, so we add it back to make
1502 * sure we do not lose any information.
1504 static __isl_give isl_basic_map *eliminate_unit_div(
1505 __isl_take isl_basic_map *bmap, int div, int *progress)
1507 int j;
1508 isl_size v_div, dim;
1509 isl_ctx *ctx;
1511 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1512 dim = isl_basic_map_dim(bmap, isl_dim_all);
1513 if (v_div < 0 || dim < 0)
1514 return isl_basic_map_free(bmap);
1516 ctx = isl_basic_map_get_ctx(bmap);
1518 for (j = 0; j < bmap->n_ineq; ++j) {
1519 int s;
1521 if (!isl_int_is_one(bmap->ineq[j][1 + v_div + div]) &&
1522 !isl_int_is_negone(bmap->ineq[j][1 + v_div + div]))
1523 continue;
1525 mark_progress(progress);
1527 s = isl_int_sgn(bmap->ineq[j][1 + v_div + div]);
1528 isl_int_set_si(bmap->ineq[j][1 + v_div + div], 0);
1529 if (s < 0)
1530 isl_seq_combine(bmap->ineq[j],
1531 ctx->negone, bmap->div[div] + 1,
1532 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1533 else
1534 isl_seq_combine(bmap->ineq[j],
1535 ctx->one, bmap->div[div] + 1,
1536 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1537 if (s < 0) {
1538 isl_int_add(bmap->ineq[j][0],
1539 bmap->ineq[j][0], bmap->div[div][0]);
1540 isl_int_sub_ui(bmap->ineq[j][0],
1541 bmap->ineq[j][0], 1);
1544 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1545 bmap = isl_basic_map_add_div_constraint(bmap, div, s);
1546 if (!bmap)
1547 return NULL;
1550 return bmap;
1553 /* Eliminate selected known divs from constraints where they appear with
1554 * a (positive or negative) unit coefficient.
1555 * In particular, only handle those for which "select" returns isl_bool_true.
1556 * If "progress" is not NULL, then it gets set if the elimination
1557 * results in any changes.
1559 * We skip integral divs, i.e., those with denominator 1, as we would
1560 * risk eliminating the div from the div constraints.
1561 * They are eliminated in eliminate_integral_divs instead.
1563 static __isl_give isl_basic_map *eliminate_selected_unit_divs(
1564 __isl_take isl_basic_map *bmap,
1565 isl_bool (*select)(__isl_keep isl_basic_map *bmap, int div),
1566 int *progress)
1568 int i;
1569 isl_size n_div;
1571 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1572 if (n_div < 0)
1573 return isl_basic_map_free(bmap);
1575 for (i = 0; i < n_div; ++i) {
1576 isl_bool skip;
1577 isl_bool selected;
1579 skip = isl_basic_map_div_is_marked_unknown(bmap, i);
1580 if (skip >= 0 && !skip)
1581 skip = isl_basic_map_div_is_integral(bmap, i);
1582 if (skip < 0)
1583 return isl_basic_map_free(bmap);
1584 if (skip)
1585 continue;
1586 selected = select(bmap, i);
1587 if (selected < 0)
1588 return isl_basic_map_free(bmap);
1589 if (!selected)
1590 continue;
1591 bmap = eliminate_unit_div(bmap, i, progress);
1592 if (!bmap)
1593 return NULL;
1596 return bmap;
1599 /* eliminate_selected_unit_divs callback that selects every
1600 * integer division.
1602 static isl_bool is_any_div(__isl_keep isl_basic_map *bmap, int div)
1604 return isl_bool_true;
1607 /* Eliminate known divs from constraints where they appear with
1608 * a (positive or negative) unit coefficient.
1609 * If "progress" is not NULL, then it gets set if the elimination
1610 * results in any changes.
1612 static __isl_give isl_basic_map *eliminate_unit_divs(
1613 __isl_take isl_basic_map *bmap, int *progress)
1615 return eliminate_selected_unit_divs(bmap, &is_any_div, progress);
1618 /* eliminate_selected_unit_divs callback that selects
1619 * integer divisions that only appear with
1620 * a (positive or negative) unit coefficient
1621 * (outside their div constraints).
1623 static isl_bool is_pure_unit_div(__isl_keep isl_basic_map *bmap, int div)
1625 int i;
1626 isl_size v_div, n_ineq;
1628 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1629 n_ineq = isl_basic_map_n_inequality(bmap);
1630 if (v_div < 0 || n_ineq < 0)
1631 return isl_bool_error;
1633 for (i = 0; i < n_ineq; ++i) {
1634 isl_bool skip;
1636 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
1637 continue;
1638 skip = isl_basic_map_is_div_constraint(bmap,
1639 bmap->ineq[i], div);
1640 if (skip < 0)
1641 return isl_bool_error;
1642 if (skip)
1643 continue;
1644 if (!isl_int_is_one(bmap->ineq[i][1 + v_div + div]) &&
1645 !isl_int_is_negone(bmap->ineq[i][1 + v_div + div]))
1646 return isl_bool_false;
1649 return isl_bool_true;
1652 /* Eliminate known divs from constraints where they appear with
1653 * a (positive or negative) unit coefficient,
1654 * but only if they do not appear in any other constraints
1655 * (other than the div constraints).
1657 __isl_give isl_basic_map *isl_basic_map_eliminate_pure_unit_divs(
1658 __isl_take isl_basic_map *bmap)
1660 return eliminate_selected_unit_divs(bmap, &is_pure_unit_div, NULL);
1663 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1665 int progress = 1;
1666 if (!bmap)
1667 return NULL;
1668 while (progress) {
1669 isl_bool empty;
1671 progress = 0;
1672 empty = isl_basic_map_plain_is_empty(bmap);
1673 if (empty < 0)
1674 return isl_basic_map_free(bmap);
1675 if (empty)
1676 break;
1677 bmap = isl_basic_map_normalize_constraints(bmap);
1678 bmap = reduce_div_coefficients(bmap);
1679 bmap = normalize_div_expressions(bmap);
1680 bmap = remove_duplicate_divs(bmap, &progress);
1681 bmap = eliminate_unit_divs(bmap, &progress);
1682 bmap = eliminate_divs_eq(bmap, &progress);
1683 bmap = eliminate_divs_ineq(bmap, &progress);
1684 bmap = eliminate_integral_divs(bmap, &progress);
1685 bmap = isl_basic_map_gauss(bmap, &progress);
1686 /* requires equalities in normal form */
1687 bmap = normalize_divs(bmap, &progress);
1688 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1689 &progress, 1);
1691 return bmap;
1694 __isl_give isl_basic_set *isl_basic_set_simplify(
1695 __isl_take isl_basic_set *bset)
1697 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1701 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1702 isl_int *constraint, unsigned div)
1704 unsigned pos;
1706 if (!bmap)
1707 return isl_bool_error;
1709 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1711 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1712 int neg;
1713 isl_int_sub(bmap->div[div][1],
1714 bmap->div[div][1], bmap->div[div][0]);
1715 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1716 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1717 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1718 isl_int_add(bmap->div[div][1],
1719 bmap->div[div][1], bmap->div[div][0]);
1720 if (!neg)
1721 return isl_bool_false;
1722 if (isl_seq_first_non_zero(constraint+pos+1,
1723 bmap->n_div-div-1) != -1)
1724 return isl_bool_false;
1725 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1726 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1727 return isl_bool_false;
1728 if (isl_seq_first_non_zero(constraint+pos+1,
1729 bmap->n_div-div-1) != -1)
1730 return isl_bool_false;
1731 } else
1732 return isl_bool_false;
1734 return isl_bool_true;
1737 /* If the only constraints a div d=floor(f/m)
1738 * appears in are its two defining constraints
1740 * f - m d >=0
1741 * -(f - (m - 1)) + m d >= 0
1743 * then it can safely be removed.
1745 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1747 int i;
1748 isl_bool involves;
1749 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1750 unsigned pos = 1 + v_div + div;
1752 if (v_div < 0)
1753 return isl_bool_error;
1755 for (i = 0; i < bmap->n_eq; ++i)
1756 if (!isl_int_is_zero(bmap->eq[i][pos]))
1757 return isl_bool_false;
1759 for (i = 0; i < bmap->n_ineq; ++i) {
1760 isl_bool red;
1762 if (isl_int_is_zero(bmap->ineq[i][pos]))
1763 continue;
1764 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1765 if (red < 0 || !red)
1766 return red;
1769 involves = isl_basic_map_any_div_involves_vars(bmap, v_div + div, 1);
1770 if (involves < 0 || involves)
1771 return isl_bool_not(involves);
1773 return isl_bool_true;
1777 * Remove divs that don't occur in any of the constraints or other divs.
1778 * These can arise when dropping constraints from a basic map or
1779 * when the divs of a basic map have been temporarily aligned
1780 * with the divs of another basic map.
1782 static __isl_give isl_basic_map *remove_redundant_divs(
1783 __isl_take isl_basic_map *bmap)
1785 int i;
1786 isl_size v_div;
1788 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1789 if (v_div < 0)
1790 return isl_basic_map_free(bmap);
1792 for (i = bmap->n_div-1; i >= 0; --i) {
1793 isl_bool redundant;
1795 redundant = div_is_redundant(bmap, i);
1796 if (redundant < 0)
1797 return isl_basic_map_free(bmap);
1798 if (!redundant)
1799 continue;
1800 bmap = isl_basic_map_drop_constraints_involving(bmap,
1801 v_div + i, 1);
1802 bmap = isl_basic_map_drop_div(bmap, i);
1804 return bmap;
1807 /* Mark "bmap" as final, without checking for obviously redundant
1808 * integer divisions. This function should be used when "bmap"
1809 * is known not to involve any such integer divisions.
1811 __isl_give isl_basic_map *isl_basic_map_mark_final(
1812 __isl_take isl_basic_map *bmap)
1814 if (!bmap)
1815 return NULL;
1816 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1817 return bmap;
1820 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1822 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1824 bmap = remove_redundant_divs(bmap);
1825 bmap = isl_basic_map_mark_final(bmap);
1826 return bmap;
1829 __isl_give isl_basic_set *isl_basic_set_finalize(
1830 __isl_take isl_basic_set *bset)
1832 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1835 /* Remove definition of any div that is defined in terms of the given variable.
1836 * The div itself is not removed. Functions such as
1837 * eliminate_divs_ineq depend on the other divs remaining in place.
1839 static __isl_give isl_basic_map *remove_dependent_vars(
1840 __isl_take isl_basic_map *bmap, int pos)
1842 int i;
1844 if (!bmap)
1845 return NULL;
1847 for (i = 0; i < bmap->n_div; ++i) {
1848 if (isl_int_is_zero(bmap->div[i][0]))
1849 continue;
1850 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1851 continue;
1852 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1853 if (!bmap)
1854 return NULL;
1856 return bmap;
1859 /* Eliminate the specified variables from the constraints using
1860 * Fourier-Motzkin. The variables themselves are not removed.
1862 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1863 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1865 int d;
1866 int i, j, k;
1867 isl_size total;
1868 int need_gauss = 0;
1870 if (n == 0)
1871 return bmap;
1872 total = isl_basic_map_dim(bmap, isl_dim_all);
1873 if (total < 0)
1874 return isl_basic_map_free(bmap);
1876 bmap = isl_basic_map_cow(bmap);
1877 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1878 bmap = remove_dependent_vars(bmap, d);
1879 if (!bmap)
1880 return NULL;
1882 for (d = pos + n - 1;
1883 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1884 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1885 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1886 int n_lower, n_upper;
1887 if (!bmap)
1888 return NULL;
1889 for (i = 0; i < bmap->n_eq; ++i) {
1890 if (isl_int_is_zero(bmap->eq[i][1+d]))
1891 continue;
1892 bmap = eliminate_var_using_equality(bmap, d,
1893 bmap->eq[i], 0, 1, NULL);
1894 if (isl_basic_map_drop_equality(bmap, i) < 0)
1895 return isl_basic_map_free(bmap);
1896 need_gauss = 1;
1897 break;
1899 if (i < bmap->n_eq)
1900 continue;
1901 n_lower = 0;
1902 n_upper = 0;
1903 for (i = 0; i < bmap->n_ineq; ++i) {
1904 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1905 n_lower++;
1906 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1907 n_upper++;
1909 bmap = isl_basic_map_extend_constraints(bmap,
1910 0, n_lower * n_upper);
1911 if (!bmap)
1912 goto error;
1913 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1914 int last;
1915 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1916 continue;
1917 last = -1;
1918 for (j = 0; j < i; ++j) {
1919 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1920 continue;
1921 last = j;
1922 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1923 isl_int_sgn(bmap->ineq[j][1+d]))
1924 continue;
1925 k = isl_basic_map_alloc_inequality(bmap);
1926 if (k < 0)
1927 goto error;
1928 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1929 1+total);
1930 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1931 1+d, 1+total, NULL);
1933 isl_basic_map_drop_inequality(bmap, i);
1934 i = last + 1;
1936 if (n_lower > 0 && n_upper > 0) {
1937 bmap = isl_basic_map_normalize_constraints(bmap);
1938 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1939 NULL, 0);
1940 bmap = isl_basic_map_gauss(bmap, NULL);
1941 bmap = isl_basic_map_remove_redundancies(bmap);
1942 need_gauss = 0;
1943 if (!bmap)
1944 goto error;
1945 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1946 break;
1949 if (need_gauss)
1950 bmap = isl_basic_map_gauss(bmap, NULL);
1951 return bmap;
1952 error:
1953 isl_basic_map_free(bmap);
1954 return NULL;
1957 __isl_give isl_basic_set *isl_basic_set_eliminate_vars(
1958 __isl_take isl_basic_set *bset, unsigned pos, unsigned n)
1960 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1961 pos, n));
1964 /* Eliminate the specified n dimensions starting at first from the
1965 * constraints, without removing the dimensions from the space.
1966 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1967 * Otherwise, they are projected out and the original space is restored.
1969 __isl_give isl_basic_map *isl_basic_map_eliminate(
1970 __isl_take isl_basic_map *bmap,
1971 enum isl_dim_type type, unsigned first, unsigned n)
1973 isl_space *space;
1975 if (!bmap)
1976 return NULL;
1977 if (n == 0)
1978 return bmap;
1980 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1981 return isl_basic_map_free(bmap);
1983 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1984 first += isl_basic_map_offset(bmap, type) - 1;
1985 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1986 return isl_basic_map_finalize(bmap);
1989 space = isl_basic_map_get_space(bmap);
1990 bmap = isl_basic_map_project_out(bmap, type, first, n);
1991 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1992 bmap = isl_basic_map_reset_space(bmap, space);
1993 return bmap;
1996 __isl_give isl_basic_set *isl_basic_set_eliminate(
1997 __isl_take isl_basic_set *bset,
1998 enum isl_dim_type type, unsigned first, unsigned n)
2000 return isl_basic_map_eliminate(bset, type, first, n);
2003 /* Remove all constraints from "bmap" that reference any unknown local
2004 * variables (directly or indirectly).
2006 * Dropping all constraints on a local variable will make it redundant,
2007 * so it will get removed implicitly by
2008 * isl_basic_map_drop_constraints_involving_dims. Some other local
2009 * variables may also end up becoming redundant if they only appear
2010 * in constraints together with the unknown local variable.
2011 * Therefore, start over after calling
2012 * isl_basic_map_drop_constraints_involving_dims.
2014 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_unknown_divs(
2015 __isl_take isl_basic_map *bmap)
2017 isl_bool known;
2018 isl_size n_div;
2019 int i, o_div;
2021 known = isl_basic_map_divs_known(bmap);
2022 if (known < 0)
2023 return isl_basic_map_free(bmap);
2024 if (known)
2025 return bmap;
2027 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2028 if (n_div < 0)
2029 return isl_basic_map_free(bmap);
2030 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
2032 for (i = 0; i < n_div; ++i) {
2033 known = isl_basic_map_div_is_known(bmap, i);
2034 if (known < 0)
2035 return isl_basic_map_free(bmap);
2036 if (known)
2037 continue;
2038 bmap = remove_dependent_vars(bmap, o_div + i);
2039 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
2040 isl_dim_div, i, 1);
2041 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2042 if (n_div < 0)
2043 return isl_basic_map_free(bmap);
2044 i = -1;
2047 return bmap;
2050 /* Remove all constraints from "bset" that reference any unknown local
2051 * variables (directly or indirectly).
2053 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_unknown_divs(
2054 __isl_take isl_basic_set *bset)
2056 isl_basic_map *bmap;
2058 bmap = bset_to_bmap(bset);
2059 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
2060 return bset_from_bmap(bmap);
2063 /* Remove all constraints from "map" that reference any unknown local
2064 * variables (directly or indirectly).
2066 * Since constraints may get dropped from the basic maps,
2067 * they may no longer be disjoint from each other.
2069 __isl_give isl_map *isl_map_drop_constraints_involving_unknown_divs(
2070 __isl_take isl_map *map)
2072 int i;
2073 isl_bool known;
2075 known = isl_map_divs_known(map);
2076 if (known < 0)
2077 return isl_map_free(map);
2078 if (known)
2079 return map;
2081 map = isl_map_cow(map);
2082 if (!map)
2083 return NULL;
2085 for (i = 0; i < map->n; ++i) {
2086 map->p[i] =
2087 isl_basic_map_drop_constraints_involving_unknown_divs(
2088 map->p[i]);
2089 if (!map->p[i])
2090 return isl_map_free(map);
2093 if (map->n > 1)
2094 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2096 return map;
2099 /* Don't assume equalities are in order, because align_divs
2100 * may have changed the order of the divs.
2102 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim,
2103 unsigned len)
2105 int d, i;
2107 for (d = 0; d < len; ++d)
2108 elim[d] = -1;
2109 for (i = 0; i < bmap->n_eq; ++i) {
2110 for (d = len - 1; d >= 0; --d) {
2111 if (isl_int_is_zero(bmap->eq[i][1+d]))
2112 continue;
2113 elim[d] = i;
2114 break;
2119 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
2120 int *elim, unsigned len)
2122 compute_elimination_index(bset_to_bmap(bset), elim, len);
2125 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2126 __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
2128 int d;
2129 int copied = 0;
2131 for (d = total - 1; d >= 0; --d) {
2132 if (isl_int_is_zero(src[1+d]))
2133 continue;
2134 if (elim[d] == -1)
2135 continue;
2136 if (!copied) {
2137 isl_seq_cpy(dst, src, 1 + total);
2138 copied = 1;
2140 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2142 return copied;
2145 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2146 __isl_keep isl_basic_set *bset, int *elim, unsigned total)
2148 return reduced_using_equalities(dst, src,
2149 bset_to_bmap(bset), elim, total);
2152 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
2153 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2155 int i;
2156 int *elim;
2157 isl_size dim;
2159 if (!bset || !context)
2160 goto error;
2162 if (context->n_eq == 0) {
2163 isl_basic_set_free(context);
2164 return bset;
2167 bset = isl_basic_set_cow(bset);
2168 dim = isl_basic_set_dim(bset, isl_dim_set);
2169 if (dim < 0)
2170 goto error;
2172 elim = isl_alloc_array(bset->ctx, int, dim);
2173 if (!elim)
2174 goto error;
2175 set_compute_elimination_index(context, elim, dim);
2176 for (i = 0; i < bset->n_eq; ++i)
2177 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2178 context, elim, dim);
2179 for (i = 0; i < bset->n_ineq; ++i)
2180 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2181 context, elim, dim);
2182 isl_basic_set_free(context);
2183 free(elim);
2184 bset = isl_basic_set_simplify(bset);
2185 bset = isl_basic_set_finalize(bset);
2186 return bset;
2187 error:
2188 isl_basic_set_free(bset);
2189 isl_basic_set_free(context);
2190 return NULL;
2193 /* For each inequality in "ineq" that is a shifted (more relaxed)
2194 * copy of an inequality in "context", mark the corresponding entry
2195 * in "row" with -1.
2196 * If an inequality only has a non-negative constant term, then
2197 * mark it as well.
2199 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2200 __isl_keep isl_basic_set *context, int *row)
2202 struct isl_constraint_index ci;
2203 isl_size n_ineq, cols;
2204 unsigned total;
2205 int k;
2207 if (!ineq || !context)
2208 return isl_stat_error;
2209 if (context->n_ineq == 0)
2210 return isl_stat_ok;
2211 if (setup_constraint_index(&ci, context) < 0)
2212 return isl_stat_error;
2214 n_ineq = isl_mat_rows(ineq);
2215 cols = isl_mat_cols(ineq);
2216 if (n_ineq < 0 || cols < 0)
2217 return isl_stat_error;
2218 total = cols - 1;
2219 for (k = 0; k < n_ineq; ++k) {
2220 int l;
2221 isl_bool redundant;
2223 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2224 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2225 row[k] = -1;
2226 continue;
2228 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2229 if (redundant < 0)
2230 goto error;
2231 if (!redundant)
2232 continue;
2233 row[k] = -1;
2235 constraint_index_free(&ci);
2236 return isl_stat_ok;
2237 error:
2238 constraint_index_free(&ci);
2239 return isl_stat_error;
2242 static __isl_give isl_basic_set *remove_shifted_constraints(
2243 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
2245 struct isl_constraint_index ci;
2246 int k;
2248 if (!bset || !context)
2249 return bset;
2251 if (context->n_ineq == 0)
2252 return bset;
2253 if (setup_constraint_index(&ci, context) < 0)
2254 return bset;
2256 for (k = 0; k < bset->n_ineq; ++k) {
2257 isl_bool redundant;
2259 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2260 if (redundant < 0)
2261 goto error;
2262 if (!redundant)
2263 continue;
2264 bset = isl_basic_set_cow(bset);
2265 if (!bset)
2266 goto error;
2267 isl_basic_set_drop_inequality(bset, k);
2268 --k;
2270 constraint_index_free(&ci);
2271 return bset;
2272 error:
2273 constraint_index_free(&ci);
2274 return bset;
2277 /* Remove constraints from "bmap" that are identical to constraints
2278 * in "context" or that are more relaxed (greater constant term).
2280 * We perform the test for shifted copies on the pure constraints
2281 * in remove_shifted_constraints.
2283 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2284 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2286 isl_basic_set *bset, *bset_context;
2288 if (!bmap || !context)
2289 goto error;
2291 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2292 isl_basic_map_free(context);
2293 return bmap;
2296 bmap = isl_basic_map_order_divs(bmap);
2297 context = isl_basic_map_align_divs(context, bmap);
2298 bmap = isl_basic_map_align_divs(bmap, context);
2300 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2301 bset_context = isl_basic_map_underlying_set(context);
2302 bset = remove_shifted_constraints(bset, bset_context);
2303 isl_basic_set_free(bset_context);
2305 bmap = isl_basic_map_overlying_set(bset, bmap);
2307 return bmap;
2308 error:
2309 isl_basic_map_free(bmap);
2310 isl_basic_map_free(context);
2311 return NULL;
2314 /* Does the (linear part of a) constraint "c" involve any of the "len"
2315 * "relevant" dimensions?
2317 static int is_related(isl_int *c, int len, int *relevant)
2319 int i;
2321 for (i = 0; i < len; ++i) {
2322 if (!relevant[i])
2323 continue;
2324 if (!isl_int_is_zero(c[i]))
2325 return 1;
2328 return 0;
2331 /* Drop constraints from "bmap" that do not involve any of
2332 * the dimensions marked "relevant".
2334 static __isl_give isl_basic_map *drop_unrelated_constraints(
2335 __isl_take isl_basic_map *bmap, int *relevant)
2337 int i;
2338 isl_size dim;
2340 dim = isl_basic_map_dim(bmap, isl_dim_all);
2341 if (dim < 0)
2342 return isl_basic_map_free(bmap);
2343 for (i = 0; i < dim; ++i)
2344 if (!relevant[i])
2345 break;
2346 if (i >= dim)
2347 return bmap;
2349 for (i = bmap->n_eq - 1; i >= 0; --i)
2350 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2351 bmap = isl_basic_map_cow(bmap);
2352 if (isl_basic_map_drop_equality(bmap, i) < 0)
2353 return isl_basic_map_free(bmap);
2356 for (i = bmap->n_ineq - 1; i >= 0; --i)
2357 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2358 bmap = isl_basic_map_cow(bmap);
2359 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2360 return isl_basic_map_free(bmap);
2363 return bmap;
2366 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2368 * In particular, for any variable involved in the constraint,
2369 * find the actual group id from before and replace the group
2370 * of the corresponding variable by the minimal group of all
2371 * the variables involved in the constraint considered so far
2372 * (if this minimum is smaller) or replace the minimum by this group
2373 * (if the minimum is larger).
2375 * At the end, all the variables in "c" will (indirectly) point
2376 * to the minimal of the groups that they referred to originally.
2378 static void update_groups(int dim, int *group, isl_int *c)
2380 int j;
2381 int min = dim;
2383 for (j = 0; j < dim; ++j) {
2384 if (isl_int_is_zero(c[j]))
2385 continue;
2386 while (group[j] >= 0 && group[group[j]] != group[j])
2387 group[j] = group[group[j]];
2388 if (group[j] == min)
2389 continue;
2390 if (group[j] < min) {
2391 if (min >= 0 && min < dim)
2392 group[min] = group[j];
2393 min = group[j];
2394 } else
2395 group[group[j]] = min;
2399 /* Allocate an array of groups of variables, one for each variable
2400 * in "context", initialized to zero.
2402 static int *alloc_groups(__isl_keep isl_basic_set *context)
2404 isl_ctx *ctx;
2405 isl_size dim;
2407 dim = isl_basic_set_dim(context, isl_dim_set);
2408 if (dim < 0)
2409 return NULL;
2410 ctx = isl_basic_set_get_ctx(context);
2411 return isl_calloc_array(ctx, int, dim);
2414 /* Drop constraints from "bmap" that only involve variables that are
2415 * not related to any of the variables marked with a "-1" in "group".
2417 * We construct groups of variables that collect variables that
2418 * (indirectly) appear in some common constraint of "bmap".
2419 * Each group is identified by the first variable in the group,
2420 * except for the special group of variables that was already identified
2421 * in the input as -1 (or are related to those variables).
2422 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2423 * otherwise the group of i is the group of group[i].
2425 * We first initialize groups for the remaining variables.
2426 * Then we iterate over the constraints of "bmap" and update the
2427 * group of the variables in the constraint by the smallest group.
2428 * Finally, we resolve indirect references to groups by running over
2429 * the variables.
2431 * After computing the groups, we drop constraints that do not involve
2432 * any variables in the -1 group.
2434 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2435 __isl_take isl_basic_map *bmap, __isl_take int *group)
2437 isl_size dim;
2438 int i;
2439 int last;
2441 dim = isl_basic_map_dim(bmap, isl_dim_all);
2442 if (dim < 0)
2443 return isl_basic_map_free(bmap);
2445 last = -1;
2446 for (i = 0; i < dim; ++i)
2447 if (group[i] >= 0)
2448 last = group[i] = i;
2449 if (last < 0) {
2450 free(group);
2451 return bmap;
2454 for (i = 0; i < bmap->n_eq; ++i)
2455 update_groups(dim, group, bmap->eq[i] + 1);
2456 for (i = 0; i < bmap->n_ineq; ++i)
2457 update_groups(dim, group, bmap->ineq[i] + 1);
2459 for (i = 0; i < dim; ++i)
2460 if (group[i] >= 0)
2461 group[i] = group[group[i]];
2463 for (i = 0; i < dim; ++i)
2464 group[i] = group[i] == -1;
2466 bmap = drop_unrelated_constraints(bmap, group);
2468 free(group);
2469 return bmap;
2472 /* Drop constraints from "context" that are irrelevant for computing
2473 * the gist of "bset".
2475 * In particular, drop constraints in variables that are not related
2476 * to any of the variables involved in the constraints of "bset"
2477 * in the sense that there is no sequence of constraints that connects them.
2479 * We first mark all variables that appear in "bset" as belonging
2480 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2482 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2483 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2485 int *group;
2486 isl_size dim;
2487 int i, j;
2489 dim = isl_basic_set_dim(bset, isl_dim_set);
2490 if (!context || dim < 0)
2491 return isl_basic_set_free(context);
2493 group = alloc_groups(context);
2495 if (!group)
2496 return isl_basic_set_free(context);
2498 for (i = 0; i < dim; ++i) {
2499 for (j = 0; j < bset->n_eq; ++j)
2500 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2501 break;
2502 if (j < bset->n_eq) {
2503 group[i] = -1;
2504 continue;
2506 for (j = 0; j < bset->n_ineq; ++j)
2507 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2508 break;
2509 if (j < bset->n_ineq)
2510 group[i] = -1;
2513 return isl_basic_map_drop_unrelated_constraints(context, group);
2516 /* Drop constraints from "context" that are irrelevant for computing
2517 * the gist of the inequalities "ineq".
2518 * Inequalities in "ineq" for which the corresponding element of row
2519 * is set to -1 have already been marked for removal and should be ignored.
2521 * In particular, drop constraints in variables that are not related
2522 * to any of the variables involved in "ineq"
2523 * in the sense that there is no sequence of constraints that connects them.
2525 * We first mark all variables that appear in "bset" as belonging
2526 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2528 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2529 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2531 int *group;
2532 isl_size dim;
2533 int i, j;
2534 isl_size n;
2536 dim = isl_basic_set_dim(context, isl_dim_set);
2537 n = isl_mat_rows(ineq);
2538 if (dim < 0 || n < 0)
2539 return isl_basic_set_free(context);
2541 group = alloc_groups(context);
2543 if (!group)
2544 return isl_basic_set_free(context);
2546 for (i = 0; i < dim; ++i) {
2547 for (j = 0; j < n; ++j) {
2548 if (row[j] < 0)
2549 continue;
2550 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2551 break;
2553 if (j < n)
2554 group[i] = -1;
2557 return isl_basic_map_drop_unrelated_constraints(context, group);
2560 /* Do all "n" entries of "row" contain a negative value?
2562 static int all_neg(int *row, int n)
2564 int i;
2566 for (i = 0; i < n; ++i)
2567 if (row[i] >= 0)
2568 return 0;
2570 return 1;
2573 /* Update the inequalities in "bset" based on the information in "row"
2574 * and "tab".
2576 * In particular, the array "row" contains either -1, meaning that
2577 * the corresponding inequality of "bset" is redundant, or the index
2578 * of an inequality in "tab".
2580 * If the row entry is -1, then drop the inequality.
2581 * Otherwise, if the constraint is marked redundant in the tableau,
2582 * then drop the inequality. Similarly, if it is marked as an equality
2583 * in the tableau, then turn the inequality into an equality and
2584 * perform Gaussian elimination.
2586 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2587 __isl_keep int *row, struct isl_tab *tab)
2589 int i;
2590 unsigned n_ineq;
2591 unsigned n_eq;
2592 int found_equality = 0;
2594 if (!bset)
2595 return NULL;
2596 if (tab && tab->empty)
2597 return isl_basic_set_set_to_empty(bset);
2599 n_ineq = bset->n_ineq;
2600 for (i = n_ineq - 1; i >= 0; --i) {
2601 if (row[i] < 0) {
2602 if (isl_basic_set_drop_inequality(bset, i) < 0)
2603 return isl_basic_set_free(bset);
2604 continue;
2606 if (!tab)
2607 continue;
2608 n_eq = tab->n_eq;
2609 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2610 isl_basic_map_inequality_to_equality(bset, i);
2611 found_equality = 1;
2612 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2613 if (isl_basic_set_drop_inequality(bset, i) < 0)
2614 return isl_basic_set_free(bset);
2618 if (found_equality)
2619 bset = isl_basic_set_gauss(bset, NULL);
2620 bset = isl_basic_set_finalize(bset);
2621 return bset;
2624 /* Update the inequalities in "bset" based on the information in "row"
2625 * and "tab" and free all arguments (other than "bset").
2627 static __isl_give isl_basic_set *update_ineq_free(
2628 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2629 __isl_take isl_basic_set *context, __isl_take int *row,
2630 struct isl_tab *tab)
2632 isl_mat_free(ineq);
2633 isl_basic_set_free(context);
2635 bset = update_ineq(bset, row, tab);
2637 free(row);
2638 isl_tab_free(tab);
2639 return bset;
2642 /* Remove all information from bset that is redundant in the context
2643 * of context.
2644 * "ineq" contains the (possibly transformed) inequalities of "bset",
2645 * in the same order.
2646 * The (explicit) equalities of "bset" are assumed to have been taken
2647 * into account by the transformation such that only the inequalities
2648 * are relevant.
2649 * "context" is assumed not to be empty.
2651 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2652 * A value of -1 means that the inequality is obviously redundant and may
2653 * not even appear in "tab".
2655 * We first mark the inequalities of "bset"
2656 * that are obviously redundant with respect to some inequality in "context".
2657 * Then we remove those constraints from "context" that have become
2658 * irrelevant for computing the gist of "bset".
2659 * Note that this removal of constraints cannot be replaced by
2660 * a factorization because factors in "bset" may still be connected
2661 * to each other through constraints in "context".
2663 * If there are any inequalities left, we construct a tableau for
2664 * the context and then add the inequalities of "bset".
2665 * Before adding these inequalities, we freeze all constraints such that
2666 * they won't be considered redundant in terms of the constraints of "bset".
2667 * Then we detect all redundant constraints (among the
2668 * constraints that weren't frozen), first by checking for redundancy in the
2669 * the tableau and then by checking if replacing a constraint by its negation
2670 * would lead to an empty set. This last step is fairly expensive
2671 * and could be optimized by more reuse of the tableau.
2672 * Finally, we update bset according to the results.
2674 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2675 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2677 int i, r;
2678 int *row = NULL;
2679 isl_ctx *ctx;
2680 isl_basic_set *combined = NULL;
2681 struct isl_tab *tab = NULL;
2682 unsigned n_eq, context_ineq;
2684 if (!bset || !ineq || !context)
2685 goto error;
2687 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2688 isl_basic_set_free(context);
2689 isl_mat_free(ineq);
2690 return bset;
2693 ctx = isl_basic_set_get_ctx(context);
2694 row = isl_calloc_array(ctx, int, bset->n_ineq);
2695 if (!row)
2696 goto error;
2698 if (mark_shifted_constraints(ineq, context, row) < 0)
2699 goto error;
2700 if (all_neg(row, bset->n_ineq))
2701 return update_ineq_free(bset, ineq, context, row, NULL);
2703 context = drop_irrelevant_constraints_marked(context, ineq, row);
2704 if (!context)
2705 goto error;
2706 if (isl_basic_set_plain_is_universe(context))
2707 return update_ineq_free(bset, ineq, context, row, NULL);
2709 n_eq = context->n_eq;
2710 context_ineq = context->n_ineq;
2711 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2712 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2713 tab = isl_tab_from_basic_set(combined, 0);
2714 for (i = 0; i < context_ineq; ++i)
2715 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2716 goto error;
2717 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2718 goto error;
2719 r = context_ineq;
2720 for (i = 0; i < bset->n_ineq; ++i) {
2721 if (row[i] < 0)
2722 continue;
2723 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2724 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2725 goto error;
2726 row[i] = r++;
2728 if (isl_tab_detect_implicit_equalities(tab) < 0)
2729 goto error;
2730 if (isl_tab_detect_redundant(tab) < 0)
2731 goto error;
2732 for (i = bset->n_ineq - 1; i >= 0; --i) {
2733 isl_basic_set *test;
2734 int is_empty;
2736 if (row[i] < 0)
2737 continue;
2738 r = row[i];
2739 if (tab->con[n_eq + r].is_redundant)
2740 continue;
2741 test = isl_basic_set_dup(combined);
2742 test = isl_inequality_negate(test, r);
2743 test = isl_basic_set_update_from_tab(test, tab);
2744 is_empty = isl_basic_set_is_empty(test);
2745 isl_basic_set_free(test);
2746 if (is_empty < 0)
2747 goto error;
2748 if (is_empty)
2749 tab->con[n_eq + r].is_redundant = 1;
2751 bset = update_ineq_free(bset, ineq, context, row, tab);
2752 if (bset) {
2753 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2754 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2757 isl_basic_set_free(combined);
2758 return bset;
2759 error:
2760 free(row);
2761 isl_mat_free(ineq);
2762 isl_tab_free(tab);
2763 isl_basic_set_free(combined);
2764 isl_basic_set_free(context);
2765 isl_basic_set_free(bset);
2766 return NULL;
2769 /* Extract the inequalities of "bset" as an isl_mat.
2771 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2773 isl_size total;
2774 isl_ctx *ctx;
2775 isl_mat *ineq;
2777 total = isl_basic_set_dim(bset, isl_dim_all);
2778 if (total < 0)
2779 return NULL;
2781 ctx = isl_basic_set_get_ctx(bset);
2782 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2783 0, 1 + total);
2785 return ineq;
2788 /* Remove all information from "bset" that is redundant in the context
2789 * of "context", for the case where both "bset" and "context" are
2790 * full-dimensional.
2792 static __isl_give isl_basic_set *uset_gist_uncompressed(
2793 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2795 isl_mat *ineq;
2797 ineq = extract_ineq(bset);
2798 return uset_gist_full(bset, ineq, context);
2801 /* Replace "bset" by an empty basic set in the same space.
2803 static __isl_give isl_basic_set *replace_by_empty(
2804 __isl_take isl_basic_set *bset)
2806 isl_space *space;
2808 space = isl_basic_set_get_space(bset);
2809 isl_basic_set_free(bset);
2810 return isl_basic_set_empty(space);
2813 /* Remove all information from "bset" that is redundant in the context
2814 * of "context", for the case where the combined equalities of
2815 * "bset" and "context" allow for a compression that can be obtained
2816 * by preapplication of "T".
2817 * If the compression of "context" is empty, meaning that "bset" and
2818 * "context" do not intersect, then return the empty set.
2820 * "bset" itself is not transformed by "T". Instead, the inequalities
2821 * are extracted from "bset" and those are transformed by "T".
2822 * uset_gist_full then determines which of the transformed inequalities
2823 * are redundant with respect to the transformed "context" and removes
2824 * the corresponding inequalities from "bset".
2826 * After preapplying "T" to the inequalities, any common factor is
2827 * removed from the coefficients. If this results in a tightening
2828 * of the constant term, then the same tightening is applied to
2829 * the corresponding untransformed inequality in "bset".
2830 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2832 * g f'(x) + r >= 0
2834 * with 0 <= r < g, then it is equivalent to
2836 * f'(x) >= 0
2838 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2839 * subspace compressed by T since the latter would be transformed to
2841 * g f'(x) >= 0
2843 static __isl_give isl_basic_set *uset_gist_compressed(
2844 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2845 __isl_take isl_mat *T)
2847 isl_ctx *ctx;
2848 isl_mat *ineq;
2849 int i;
2850 isl_size n_row, n_col;
2851 isl_int rem;
2853 ineq = extract_ineq(bset);
2854 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2855 context = isl_basic_set_preimage(context, T);
2857 if (!ineq || !context)
2858 goto error;
2859 if (isl_basic_set_plain_is_empty(context)) {
2860 isl_mat_free(ineq);
2861 isl_basic_set_free(context);
2862 return replace_by_empty(bset);
2865 ctx = isl_mat_get_ctx(ineq);
2866 n_row = isl_mat_rows(ineq);
2867 n_col = isl_mat_cols(ineq);
2868 if (n_row < 0 || n_col < 0)
2869 goto error;
2870 isl_int_init(rem);
2871 for (i = 0; i < n_row; ++i) {
2872 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2873 if (isl_int_is_zero(ctx->normalize_gcd))
2874 continue;
2875 if (isl_int_is_one(ctx->normalize_gcd))
2876 continue;
2877 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2878 ctx->normalize_gcd, n_col - 1);
2879 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2880 isl_int_fdiv_q(ineq->row[i][0],
2881 ineq->row[i][0], ctx->normalize_gcd);
2882 if (isl_int_is_zero(rem))
2883 continue;
2884 bset = isl_basic_set_cow(bset);
2885 if (!bset)
2886 break;
2887 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2889 isl_int_clear(rem);
2891 return uset_gist_full(bset, ineq, context);
2892 error:
2893 isl_mat_free(ineq);
2894 isl_basic_set_free(context);
2895 isl_basic_set_free(bset);
2896 return NULL;
2899 /* Project "bset" onto the variables that are involved in "template".
2901 static __isl_give isl_basic_set *project_onto_involved(
2902 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2904 int i;
2905 isl_size n;
2907 n = isl_basic_set_dim(template, isl_dim_set);
2908 if (n < 0 || !template)
2909 return isl_basic_set_free(bset);
2911 for (i = 0; i < n; ++i) {
2912 isl_bool involved;
2914 involved = isl_basic_set_involves_dims(template,
2915 isl_dim_set, i, 1);
2916 if (involved < 0)
2917 return isl_basic_set_free(bset);
2918 if (involved)
2919 continue;
2920 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2923 return bset;
2926 /* Remove all information from bset that is redundant in the context
2927 * of context. In particular, equalities that are linear combinations
2928 * of those in context are removed. Then the inequalities that are
2929 * redundant in the context of the equalities and inequalities of
2930 * context are removed.
2932 * First of all, we drop those constraints from "context"
2933 * that are irrelevant for computing the gist of "bset".
2934 * Alternatively, we could factorize the intersection of "context" and "bset".
2936 * We first compute the intersection of the integer affine hulls
2937 * of "bset" and "context",
2938 * compute the gist inside this intersection and then reduce
2939 * the constraints with respect to the equalities of the context
2940 * that only involve variables already involved in the input.
2941 * If the intersection of the affine hulls turns out to be empty,
2942 * then return the empty set.
2944 * If two constraints are mutually redundant, then uset_gist_full
2945 * will remove the second of those constraints. We therefore first
2946 * sort the constraints so that constraints not involving existentially
2947 * quantified variables are given precedence over those that do.
2948 * We have to perform this sorting before the variable compression,
2949 * because that may effect the order of the variables.
2951 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2952 __isl_take isl_basic_set *context)
2954 isl_mat *eq;
2955 isl_mat *T;
2956 isl_basic_set *aff;
2957 isl_basic_set *aff_context;
2958 isl_size total;
2960 total = isl_basic_set_dim(bset, isl_dim_all);
2961 if (total < 0 || !context)
2962 goto error;
2964 context = drop_irrelevant_constraints(context, bset);
2966 bset = isl_basic_set_detect_equalities(bset);
2967 aff = isl_basic_set_copy(bset);
2968 aff = isl_basic_set_plain_affine_hull(aff);
2969 context = isl_basic_set_detect_equalities(context);
2970 aff_context = isl_basic_set_copy(context);
2971 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2972 aff = isl_basic_set_intersect(aff, aff_context);
2973 if (!aff)
2974 goto error;
2975 if (isl_basic_set_plain_is_empty(aff)) {
2976 isl_basic_set_free(bset);
2977 isl_basic_set_free(context);
2978 return aff;
2980 bset = isl_basic_set_sort_constraints(bset);
2981 if (aff->n_eq == 0) {
2982 isl_basic_set_free(aff);
2983 return uset_gist_uncompressed(bset, context);
2985 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2986 eq = isl_mat_cow(eq);
2987 T = isl_mat_variable_compression(eq, NULL);
2988 isl_basic_set_free(aff);
2989 if (T && T->n_col == 0) {
2990 isl_mat_free(T);
2991 isl_basic_set_free(context);
2992 return replace_by_empty(bset);
2995 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2996 aff_context = project_onto_involved(aff_context, bset);
2998 bset = uset_gist_compressed(bset, context, T);
2999 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
3001 if (bset) {
3002 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
3003 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
3006 return bset;
3007 error:
3008 isl_basic_set_free(bset);
3009 isl_basic_set_free(context);
3010 return NULL;
3013 /* Return the number of equality constraints in "bmap" that involve
3014 * local variables. This function assumes that Gaussian elimination
3015 * has been applied to the equality constraints.
3017 static int n_div_eq(__isl_keep isl_basic_map *bmap)
3019 int i;
3020 isl_size total, n_div;
3022 if (!bmap)
3023 return -1;
3025 if (bmap->n_eq == 0)
3026 return 0;
3028 total = isl_basic_map_dim(bmap, isl_dim_all);
3029 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3030 if (total < 0 || n_div < 0)
3031 return -1;
3032 total -= n_div;
3034 for (i = 0; i < bmap->n_eq; ++i)
3035 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
3036 n_div) == -1)
3037 return i;
3039 return bmap->n_eq;
3042 /* Construct a basic map in "space" defined by the equality constraints in "eq".
3043 * The constraints are assumed not to involve any local variables.
3045 static __isl_give isl_basic_map *basic_map_from_equalities(
3046 __isl_take isl_space *space, __isl_take isl_mat *eq)
3048 int i, k;
3049 isl_size total;
3050 isl_basic_map *bmap = NULL;
3052 total = isl_space_dim(space, isl_dim_all);
3053 if (total < 0 || !eq)
3054 goto error;
3056 if (1 + total != eq->n_col)
3057 isl_die(isl_space_get_ctx(space), isl_error_internal,
3058 "unexpected number of columns", goto error);
3060 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
3061 0, eq->n_row, 0);
3062 for (i = 0; i < eq->n_row; ++i) {
3063 k = isl_basic_map_alloc_equality(bmap);
3064 if (k < 0)
3065 goto error;
3066 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
3069 isl_space_free(space);
3070 isl_mat_free(eq);
3071 return bmap;
3072 error:
3073 isl_space_free(space);
3074 isl_mat_free(eq);
3075 isl_basic_map_free(bmap);
3076 return NULL;
3079 /* Construct and return a variable compression based on the equality
3080 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3081 * "n1" is the number of (initial) equality constraints in "bmap1"
3082 * that do involve local variables.
3083 * "n2" is the number of (initial) equality constraints in "bmap2"
3084 * that do involve local variables.
3085 * "total" is the total number of other variables.
3086 * This function assumes that Gaussian elimination
3087 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3088 * such that the equality constraints not involving local variables
3089 * are those that start at "n1" or "n2".
3091 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3092 * then simply compute the compression based on the equality constraints
3093 * in the other basic map.
3094 * Otherwise, combine the equality constraints from both into a new
3095 * basic map such that Gaussian elimination can be applied to this combination
3096 * and then construct a variable compression from the resulting
3097 * equality constraints.
3099 static __isl_give isl_mat *combined_variable_compression(
3100 __isl_keep isl_basic_map *bmap1, int n1,
3101 __isl_keep isl_basic_map *bmap2, int n2, int total)
3103 isl_ctx *ctx;
3104 isl_mat *E1, *E2, *V;
3105 isl_basic_map *bmap;
3107 ctx = isl_basic_map_get_ctx(bmap1);
3108 if (bmap1->n_eq == n1) {
3109 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3110 n2, bmap2->n_eq - n2, 0, 1 + total);
3111 return isl_mat_variable_compression(E2, NULL);
3113 if (bmap2->n_eq == n2) {
3114 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3115 n1, bmap1->n_eq - n1, 0, 1 + total);
3116 return isl_mat_variable_compression(E1, NULL);
3118 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3119 n1, bmap1->n_eq - n1, 0, 1 + total);
3120 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3121 n2, bmap2->n_eq - n2, 0, 1 + total);
3122 E1 = isl_mat_concat(E1, E2);
3123 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3124 bmap = isl_basic_map_gauss(bmap, NULL);
3125 if (!bmap)
3126 return NULL;
3127 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3128 V = isl_mat_variable_compression(E1, NULL);
3129 isl_basic_map_free(bmap);
3131 return V;
3134 /* Extract the stride constraints from "bmap", compressed
3135 * with respect to both the stride constraints in "context" and
3136 * the remaining equality constraints in both "bmap" and "context".
3137 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3138 * "context_n_eq" is the number of (initial) stride constraints in "context".
3140 * Let x be all variables in "bmap" (and "context") other than the local
3141 * variables. First compute a variable compression
3143 * x = V x'
3145 * based on the non-stride equality constraints in "bmap" and "context".
3146 * Consider the stride constraints of "context",
3148 * A(x) + B(y) = 0
3150 * with y the local variables and plug in the variable compression,
3151 * resulting in
3153 * A(V x') + B(y) = 0
3155 * Use these constraints to compute a parameter compression on x'
3157 * x' = T x''
3159 * Now consider the stride constraints of "bmap"
3161 * C(x) + D(y) = 0
3163 * and plug in x = V*T x''.
3164 * That is, return A = [C*V*T D].
3166 static __isl_give isl_mat *extract_compressed_stride_constraints(
3167 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3168 __isl_keep isl_basic_map *context, int context_n_eq)
3170 isl_size total, n_div;
3171 isl_ctx *ctx;
3172 isl_mat *A, *B, *T, *V;
3174 total = isl_basic_map_dim(context, isl_dim_all);
3175 n_div = isl_basic_map_dim(context, isl_dim_div);
3176 if (total < 0 || n_div < 0)
3177 return NULL;
3178 total -= n_div;
3180 ctx = isl_basic_map_get_ctx(bmap);
3182 V = combined_variable_compression(bmap, bmap_n_eq,
3183 context, context_n_eq, total);
3185 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3186 B = isl_mat_sub_alloc6(ctx, context->eq,
3187 0, context_n_eq, 1 + total, n_div);
3188 A = isl_mat_product(A, isl_mat_copy(V));
3189 T = isl_mat_parameter_compression_ext(A, B);
3190 T = isl_mat_product(V, T);
3192 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3193 if (n_div < 0)
3194 T = isl_mat_free(T);
3195 else
3196 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3198 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3199 0, bmap_n_eq, 0, 1 + total + n_div);
3200 A = isl_mat_product(A, T);
3202 return A;
3205 /* Remove the prime factors from *g that have an exponent that
3206 * is strictly smaller than the exponent in "c".
3207 * All exponents in *g are known to be smaller than or equal
3208 * to those in "c".
3210 * That is, if *g is equal to
3212 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3214 * and "c" is equal to
3216 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3218 * then update *g to
3220 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3221 * p_n^{e_n * (e_n = f_n)}
3223 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3224 * neither does the gcd of *g and c / *g.
3225 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3226 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3227 * Dividing *g by this gcd therefore strictly reduces the exponent
3228 * of the prime factors that need to be removed, while leaving the
3229 * other prime factors untouched.
3230 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3231 * removes all undesired factors, without removing any others.
3233 static void remove_incomplete_powers(isl_int *g, isl_int c)
3235 isl_int t;
3237 isl_int_init(t);
3238 for (;;) {
3239 isl_int_divexact(t, c, *g);
3240 isl_int_gcd(t, t, *g);
3241 if (isl_int_is_one(t))
3242 break;
3243 isl_int_divexact(*g, *g, t);
3245 isl_int_clear(t);
3248 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3249 * of the same stride constraints in a compressed space that exploits
3250 * all equalities in the context and the other equalities in "bmap".
3252 * If the stride constraints of "bmap" are of the form
3254 * C(x) + D(y) = 0
3256 * then A is of the form
3258 * B(x') + D(y) = 0
3260 * If any of these constraints involves only a single local variable y,
3261 * then the constraint appears as
3263 * f(x) + m y_i = 0
3265 * in "bmap" and as
3267 * h(x') + m y_i = 0
3269 * in "A".
3271 * Let g be the gcd of m and the coefficients of h.
3272 * Then, in particular, g is a divisor of the coefficients of h and
3274 * f(x) = h(x')
3276 * is known to be a multiple of g.
3277 * If some prime factor in m appears with the same exponent in g,
3278 * then it can be removed from m because f(x) is already known
3279 * to be a multiple of g and therefore in particular of this power
3280 * of the prime factors.
3281 * Prime factors that appear with a smaller exponent in g cannot
3282 * be removed from m.
3283 * Let g' be the divisor of g containing all prime factors that
3284 * appear with the same exponent in m and g, then
3286 * f(x) + m y_i = 0
3288 * can be replaced by
3290 * f(x) + m/g' y_i' = 0
3292 * Note that (if g' != 1) this changes the explicit representation
3293 * of y_i to that of y_i', so the integer division at position i
3294 * is marked unknown and later recomputed by a call to
3295 * isl_basic_map_gauss.
3297 static __isl_give isl_basic_map *reduce_stride_constraints(
3298 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3300 int i;
3301 isl_size total, n_div;
3302 int any = 0;
3303 isl_int gcd;
3305 total = isl_basic_map_dim(bmap, isl_dim_all);
3306 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3307 if (total < 0 || n_div < 0 || !A)
3308 return isl_basic_map_free(bmap);
3309 total -= n_div;
3311 isl_int_init(gcd);
3312 for (i = 0; i < n; ++i) {
3313 int div;
3315 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3316 if (div < 0)
3317 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3318 "equality constraints modified unexpectedly",
3319 goto error);
3320 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3321 n_div - div - 1) != -1)
3322 continue;
3323 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3324 goto error;
3325 if (isl_int_is_one(gcd))
3326 continue;
3327 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3328 if (isl_int_is_one(gcd))
3329 continue;
3330 isl_int_divexact(bmap->eq[i][1 + total + div],
3331 bmap->eq[i][1 + total + div], gcd);
3332 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3333 if (!bmap)
3334 goto error;
3335 any = 1;
3337 isl_int_clear(gcd);
3339 if (any)
3340 bmap = isl_basic_map_gauss(bmap, NULL);
3342 return bmap;
3343 error:
3344 isl_int_clear(gcd);
3345 isl_basic_map_free(bmap);
3346 return NULL;
3349 /* Simplify the stride constraints in "bmap" based on
3350 * the remaining equality constraints in "bmap" and all equality
3351 * constraints in "context".
3352 * Only do this if both "bmap" and "context" have stride constraints.
3354 * First extract a copy of the stride constraints in "bmap" in a compressed
3355 * space exploiting all the other equality constraints and then
3356 * use this compressed copy to simplify the original stride constraints.
3358 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3359 __isl_keep isl_basic_map *context)
3361 int bmap_n_eq, context_n_eq;
3362 isl_mat *A;
3364 if (!bmap || !context)
3365 return isl_basic_map_free(bmap);
3367 bmap_n_eq = n_div_eq(bmap);
3368 context_n_eq = n_div_eq(context);
3370 if (bmap_n_eq < 0 || context_n_eq < 0)
3371 return isl_basic_map_free(bmap);
3372 if (bmap_n_eq == 0 || context_n_eq == 0)
3373 return bmap;
3375 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3376 context, context_n_eq);
3377 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3379 isl_mat_free(A);
3381 return bmap;
3384 /* Return a basic map that has the same intersection with "context" as "bmap"
3385 * and that is as "simple" as possible.
3387 * The core computation is performed on the pure constraints.
3388 * When we add back the meaning of the integer divisions, we need
3389 * to (re)introduce the div constraints. If we happen to have
3390 * discovered that some of these integer divisions are equal to
3391 * some affine combination of other variables, then these div
3392 * constraints may end up getting simplified in terms of the equalities,
3393 * resulting in extra inequalities on the other variables that
3394 * may have been removed already or that may not even have been
3395 * part of the input. We try and remove those constraints of
3396 * this form that are most obviously redundant with respect to
3397 * the context. We also remove those div constraints that are
3398 * redundant with respect to the other constraints in the result.
3400 * The stride constraints among the equality constraints in "bmap" are
3401 * also simplified with respecting to the other equality constraints
3402 * in "bmap" and with respect to all equality constraints in "context".
3404 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3405 __isl_take isl_basic_map *context)
3407 isl_basic_set *bset, *eq;
3408 isl_basic_map *eq_bmap;
3409 isl_size total, n_div, n_div_bmap;
3410 unsigned extra, n_eq, n_ineq;
3412 if (!bmap || !context)
3413 goto error;
3415 if (isl_basic_map_plain_is_universe(bmap)) {
3416 isl_basic_map_free(context);
3417 return bmap;
3419 if (isl_basic_map_plain_is_empty(context)) {
3420 isl_space *space = isl_basic_map_get_space(bmap);
3421 isl_basic_map_free(bmap);
3422 isl_basic_map_free(context);
3423 return isl_basic_map_universe(space);
3425 if (isl_basic_map_plain_is_empty(bmap)) {
3426 isl_basic_map_free(context);
3427 return bmap;
3430 bmap = isl_basic_map_remove_redundancies(bmap);
3431 context = isl_basic_map_remove_redundancies(context);
3432 bmap = isl_basic_map_order_divs(bmap);
3433 context = isl_basic_map_align_divs(context, bmap);
3435 n_div = isl_basic_map_dim(context, isl_dim_div);
3436 total = isl_basic_map_dim(bmap, isl_dim_all);
3437 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3438 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3439 goto error;
3440 extra = n_div - n_div_bmap;
3442 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3443 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3444 bset = uset_gist(bset,
3445 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3446 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3448 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3449 isl_basic_set_plain_is_empty(bset)) {
3450 isl_basic_map_free(context);
3451 return isl_basic_map_overlying_set(bset, bmap);
3454 n_eq = bset->n_eq;
3455 n_ineq = bset->n_ineq;
3456 eq = isl_basic_set_copy(bset);
3457 eq = isl_basic_set_cow(eq);
3458 eq = isl_basic_set_free_inequality(eq, n_ineq);
3459 bset = isl_basic_set_free_equality(bset, n_eq);
3461 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3462 eq_bmap = gist_strides(eq_bmap, context);
3463 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3464 bmap = isl_basic_map_overlying_set(bset, bmap);
3465 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3466 bmap = isl_basic_map_remove_redundancies(bmap);
3468 return bmap;
3469 error:
3470 isl_basic_map_free(bmap);
3471 isl_basic_map_free(context);
3472 return NULL;
3476 * Assumes context has no implicit divs.
3478 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3479 __isl_take isl_basic_map *context)
3481 int i;
3483 if (!map || !context)
3484 goto error;
3486 if (isl_basic_map_plain_is_empty(context)) {
3487 isl_space *space = isl_map_get_space(map);
3488 isl_map_free(map);
3489 isl_basic_map_free(context);
3490 return isl_map_universe(space);
3493 context = isl_basic_map_remove_redundancies(context);
3494 map = isl_map_cow(map);
3495 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3496 goto error;
3497 map = isl_map_compute_divs(map);
3498 if (!map)
3499 goto error;
3500 for (i = map->n - 1; i >= 0; --i) {
3501 map->p[i] = isl_basic_map_gist(map->p[i],
3502 isl_basic_map_copy(context));
3503 if (!map->p[i])
3504 goto error;
3505 if (isl_basic_map_plain_is_empty(map->p[i])) {
3506 isl_basic_map_free(map->p[i]);
3507 if (i != map->n - 1)
3508 map->p[i] = map->p[map->n - 1];
3509 map->n--;
3512 isl_basic_map_free(context);
3513 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3514 return map;
3515 error:
3516 isl_map_free(map);
3517 isl_basic_map_free(context);
3518 return NULL;
3521 /* Drop all inequalities from "bmap" that also appear in "context".
3522 * "context" is assumed to have only known local variables and
3523 * the initial local variables of "bmap" are assumed to be the same
3524 * as those of "context".
3525 * The constraints of both "bmap" and "context" are assumed
3526 * to have been sorted using isl_basic_map_sort_constraints.
3528 * Run through the inequality constraints of "bmap" and "context"
3529 * in sorted order.
3530 * If a constraint of "bmap" involves variables not in "context",
3531 * then it cannot appear in "context".
3532 * If a matching constraint is found, it is removed from "bmap".
3534 static __isl_give isl_basic_map *drop_inequalities(
3535 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3537 int i1, i2;
3538 isl_size total, bmap_total;
3539 unsigned extra;
3541 total = isl_basic_map_dim(context, isl_dim_all);
3542 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3543 if (total < 0 || bmap_total < 0)
3544 return isl_basic_map_free(bmap);
3546 extra = bmap_total - total;
3548 i1 = bmap->n_ineq - 1;
3549 i2 = context->n_ineq - 1;
3550 while (bmap && i1 >= 0 && i2 >= 0) {
3551 int cmp;
3553 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3554 extra) != -1) {
3555 --i1;
3556 continue;
3558 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3559 context->ineq[i2]);
3560 if (cmp < 0) {
3561 --i2;
3562 continue;
3564 if (cmp > 0) {
3565 --i1;
3566 continue;
3568 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3569 bmap = isl_basic_map_cow(bmap);
3570 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3571 bmap = isl_basic_map_free(bmap);
3573 --i1;
3574 --i2;
3577 return bmap;
3580 /* Drop all equalities from "bmap" that also appear in "context".
3581 * "context" is assumed to have only known local variables and
3582 * the initial local variables of "bmap" are assumed to be the same
3583 * as those of "context".
3585 * Run through the equality constraints of "bmap" and "context"
3586 * in sorted order.
3587 * If a constraint of "bmap" involves variables not in "context",
3588 * then it cannot appear in "context".
3589 * If a matching constraint is found, it is removed from "bmap".
3591 static __isl_give isl_basic_map *drop_equalities(
3592 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3594 int i1, i2;
3595 isl_size total, bmap_total;
3596 unsigned extra;
3598 total = isl_basic_map_dim(context, isl_dim_all);
3599 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3600 if (total < 0 || bmap_total < 0)
3601 return isl_basic_map_free(bmap);
3603 extra = bmap_total - total;
3605 i1 = bmap->n_eq - 1;
3606 i2 = context->n_eq - 1;
3608 while (bmap && i1 >= 0 && i2 >= 0) {
3609 int last1, last2;
3611 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3612 extra) != -1)
3613 break;
3614 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3615 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3616 if (last1 > last2) {
3617 --i2;
3618 continue;
3620 if (last1 < last2) {
3621 --i1;
3622 continue;
3624 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3625 bmap = isl_basic_map_cow(bmap);
3626 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3627 bmap = isl_basic_map_free(bmap);
3629 --i1;
3630 --i2;
3633 return bmap;
3636 /* Remove the constraints in "context" from "bmap".
3637 * "context" is assumed to have explicit representations
3638 * for all local variables.
3640 * First align the divs of "bmap" to those of "context" and
3641 * sort the constraints. Then drop all constraints from "bmap"
3642 * that appear in "context".
3644 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3645 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3647 isl_bool done, known;
3649 done = isl_basic_map_plain_is_universe(context);
3650 if (done == isl_bool_false)
3651 done = isl_basic_map_plain_is_universe(bmap);
3652 if (done == isl_bool_false)
3653 done = isl_basic_map_plain_is_empty(context);
3654 if (done == isl_bool_false)
3655 done = isl_basic_map_plain_is_empty(bmap);
3656 if (done < 0)
3657 goto error;
3658 if (done) {
3659 isl_basic_map_free(context);
3660 return bmap;
3662 known = isl_basic_map_divs_known(context);
3663 if (known < 0)
3664 goto error;
3665 if (!known)
3666 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3667 "context has unknown divs", goto error);
3669 context = isl_basic_map_order_divs(context);
3670 bmap = isl_basic_map_align_divs(bmap, context);
3671 bmap = isl_basic_map_gauss(bmap, NULL);
3672 bmap = isl_basic_map_sort_constraints(bmap);
3673 context = isl_basic_map_sort_constraints(context);
3675 bmap = drop_inequalities(bmap, context);
3676 bmap = drop_equalities(bmap, context);
3678 isl_basic_map_free(context);
3679 bmap = isl_basic_map_finalize(bmap);
3680 return bmap;
3681 error:
3682 isl_basic_map_free(bmap);
3683 isl_basic_map_free(context);
3684 return NULL;
3687 /* Replace "map" by the disjunct at position "pos" and free "context".
3689 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3690 int pos, __isl_take isl_basic_map *context)
3692 isl_basic_map *bmap;
3694 bmap = isl_basic_map_copy(map->p[pos]);
3695 isl_map_free(map);
3696 isl_basic_map_free(context);
3697 return isl_map_from_basic_map(bmap);
3700 /* Remove the constraints in "context" from "map".
3701 * If any of the disjuncts in the result turns out to be the universe,
3702 * then return this universe.
3703 * "context" is assumed to have explicit representations
3704 * for all local variables.
3706 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3707 __isl_take isl_basic_map *context)
3709 int i;
3710 isl_bool univ, known;
3712 univ = isl_basic_map_plain_is_universe(context);
3713 if (univ < 0)
3714 goto error;
3715 if (univ) {
3716 isl_basic_map_free(context);
3717 return map;
3719 known = isl_basic_map_divs_known(context);
3720 if (known < 0)
3721 goto error;
3722 if (!known)
3723 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3724 "context has unknown divs", goto error);
3726 map = isl_map_cow(map);
3727 if (!map)
3728 goto error;
3729 for (i = 0; i < map->n; ++i) {
3730 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3731 isl_basic_map_copy(context));
3732 univ = isl_basic_map_plain_is_universe(map->p[i]);
3733 if (univ < 0)
3734 goto error;
3735 if (univ && map->n > 1)
3736 return replace_by_disjunct(map, i, context);
3739 isl_basic_map_free(context);
3740 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3741 if (map->n > 1)
3742 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3743 return map;
3744 error:
3745 isl_map_free(map);
3746 isl_basic_map_free(context);
3747 return NULL;
3750 /* Remove the constraints in "context" from "set".
3751 * If any of the disjuncts in the result turns out to be the universe,
3752 * then return this universe.
3753 * "context" is assumed to have explicit representations
3754 * for all local variables.
3756 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3757 __isl_take isl_basic_set *context)
3759 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3760 bset_to_bmap(context)));
3763 /* Remove the constraints in "context" from "map".
3764 * If any of the disjuncts in the result turns out to be the universe,
3765 * then return this universe.
3766 * "context" is assumed to consist of a single disjunct and
3767 * to have explicit representations for all local variables.
3769 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3770 __isl_take isl_map *context)
3772 isl_basic_map *hull;
3774 hull = isl_map_unshifted_simple_hull(context);
3775 return isl_map_plain_gist_basic_map(map, hull);
3778 /* Replace "map" by a universe map in the same space and free "drop".
3780 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3781 __isl_take isl_map *drop)
3783 isl_map *res;
3785 res = isl_map_universe(isl_map_get_space(map));
3786 isl_map_free(map);
3787 isl_map_free(drop);
3788 return res;
3791 /* Return a map that has the same intersection with "context" as "map"
3792 * and that is as "simple" as possible.
3794 * If "map" is already the universe, then we cannot make it any simpler.
3795 * Similarly, if "context" is the universe, then we cannot exploit it
3796 * to simplify "map"
3797 * If "map" and "context" are identical to each other, then we can
3798 * return the corresponding universe.
3800 * If either "map" or "context" consists of multiple disjuncts,
3801 * then check if "context" happens to be a subset of "map",
3802 * in which case all constraints can be removed.
3803 * In case of multiple disjuncts, the standard procedure
3804 * may not be able to detect that all constraints can be removed.
3806 * If none of these cases apply, we have to work a bit harder.
3807 * During this computation, we make use of a single disjunct context,
3808 * so if the original context consists of more than one disjunct
3809 * then we need to approximate the context by a single disjunct set.
3810 * Simply taking the simple hull may drop constraints that are
3811 * only implicitly available in each disjunct. We therefore also
3812 * look for constraints among those defining "map" that are valid
3813 * for the context. These can then be used to simplify away
3814 * the corresponding constraints in "map".
3816 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3817 __isl_take isl_map *context)
3819 int equal;
3820 int is_universe;
3821 isl_size n_disjunct_map, n_disjunct_context;
3822 isl_bool subset;
3823 isl_basic_map *hull;
3825 is_universe = isl_map_plain_is_universe(map);
3826 if (is_universe >= 0 && !is_universe)
3827 is_universe = isl_map_plain_is_universe(context);
3828 if (is_universe < 0)
3829 goto error;
3830 if (is_universe) {
3831 isl_map_free(context);
3832 return map;
3835 isl_map_align_params_bin(&map, &context);
3836 equal = isl_map_plain_is_equal(map, context);
3837 if (equal < 0)
3838 goto error;
3839 if (equal)
3840 return replace_by_universe(map, context);
3842 n_disjunct_map = isl_map_n_basic_map(map);
3843 n_disjunct_context = isl_map_n_basic_map(context);
3844 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3845 goto error;
3846 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3847 subset = isl_map_is_subset(context, map);
3848 if (subset < 0)
3849 goto error;
3850 if (subset)
3851 return replace_by_universe(map, context);
3854 context = isl_map_compute_divs(context);
3855 if (!context)
3856 goto error;
3857 if (n_disjunct_context == 1) {
3858 hull = isl_map_simple_hull(context);
3859 } else {
3860 isl_ctx *ctx;
3861 isl_map_list *list;
3863 ctx = isl_map_get_ctx(map);
3864 list = isl_map_list_alloc(ctx, 2);
3865 list = isl_map_list_add(list, isl_map_copy(context));
3866 list = isl_map_list_add(list, isl_map_copy(map));
3867 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3868 list);
3870 return isl_map_gist_basic_map(map, hull);
3871 error:
3872 isl_map_free(map);
3873 isl_map_free(context);
3874 return NULL;
3877 __isl_give isl_basic_set *isl_basic_set_gist(__isl_take isl_basic_set *bset,
3878 __isl_take isl_basic_set *context)
3880 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3881 bset_to_bmap(context)));
3884 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3885 __isl_take isl_basic_set *context)
3887 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3888 bset_to_bmap(context)));
3891 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3892 __isl_take isl_basic_set *context)
3894 isl_space *space = isl_set_get_space(set);
3895 isl_basic_set *dom_context = isl_basic_set_universe(space);
3896 dom_context = isl_basic_set_intersect_params(dom_context, context);
3897 return isl_set_gist_basic_set(set, dom_context);
3900 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3901 __isl_take isl_set *context)
3903 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3906 /* Compute the gist of "bmap" with respect to the constraints "context"
3907 * on the domain.
3909 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3910 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3912 isl_space *space = isl_basic_map_get_space(bmap);
3913 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3915 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3916 return isl_basic_map_gist(bmap, bmap_context);
3919 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3920 __isl_take isl_set *context)
3922 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3923 map_context = isl_map_intersect_domain(map_context, context);
3924 return isl_map_gist(map, map_context);
3927 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3928 __isl_take isl_set *context)
3930 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3931 map_context = isl_map_intersect_range(map_context, context);
3932 return isl_map_gist(map, map_context);
3935 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3936 __isl_take isl_set *context)
3938 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3939 map_context = isl_map_intersect_params(map_context, context);
3940 return isl_map_gist(map, map_context);
3943 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3944 __isl_take isl_set *context)
3946 return isl_map_gist_params(set, context);
3949 /* Quick check to see if two basic maps are disjoint.
3950 * In particular, we reduce the equalities and inequalities of
3951 * one basic map in the context of the equalities of the other
3952 * basic map and check if we get a contradiction.
3954 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3955 __isl_keep isl_basic_map *bmap2)
3957 struct isl_vec *v = NULL;
3958 int *elim = NULL;
3959 isl_size total;
3960 int i;
3962 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3963 return isl_bool_error;
3964 if (bmap1->n_div || bmap2->n_div)
3965 return isl_bool_false;
3966 if (!bmap1->n_eq && !bmap2->n_eq)
3967 return isl_bool_false;
3969 total = isl_space_dim(bmap1->dim, isl_dim_all);
3970 if (total < 0)
3971 return isl_bool_error;
3972 if (total == 0)
3973 return isl_bool_false;
3974 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3975 if (!v)
3976 goto error;
3977 elim = isl_alloc_array(bmap1->ctx, int, total);
3978 if (!elim)
3979 goto error;
3980 compute_elimination_index(bmap1, elim, total);
3981 for (i = 0; i < bmap2->n_eq; ++i) {
3982 int reduced;
3983 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3984 bmap1, elim, total);
3985 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3986 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3987 goto disjoint;
3989 for (i = 0; i < bmap2->n_ineq; ++i) {
3990 int reduced;
3991 reduced = reduced_using_equalities(v->block.data,
3992 bmap2->ineq[i], bmap1, elim, total);
3993 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3994 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3995 goto disjoint;
3997 compute_elimination_index(bmap2, elim, total);
3998 for (i = 0; i < bmap1->n_ineq; ++i) {
3999 int reduced;
4000 reduced = reduced_using_equalities(v->block.data,
4001 bmap1->ineq[i], bmap2, elim, total);
4002 if (reduced && isl_int_is_neg(v->block.data[0]) &&
4003 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
4004 goto disjoint;
4006 isl_vec_free(v);
4007 free(elim);
4008 return isl_bool_false;
4009 disjoint:
4010 isl_vec_free(v);
4011 free(elim);
4012 return isl_bool_true;
4013 error:
4014 isl_vec_free(v);
4015 free(elim);
4016 return isl_bool_error;
4019 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
4020 __isl_keep isl_basic_set *bset2)
4022 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
4023 bset_to_bmap(bset2));
4026 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
4028 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
4029 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
4030 __isl_keep isl_basic_map *bmap2))
4032 int i, j;
4034 if (!map1 || !map2)
4035 return isl_bool_error;
4037 for (i = 0; i < map1->n; ++i) {
4038 for (j = 0; j < map2->n; ++j) {
4039 isl_bool d = test(map1->p[i], map2->p[j]);
4040 if (d != isl_bool_true)
4041 return d;
4045 return isl_bool_true;
4048 /* Are "map1" and "map2" obviously disjoint, based on information
4049 * that can be derived without looking at the individual basic maps?
4051 * In particular, if one of them is empty or if they live in different spaces
4052 * (ignoring parameters), then they are clearly disjoint.
4054 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
4055 __isl_keep isl_map *map2)
4057 isl_bool disjoint;
4058 isl_bool match;
4060 if (!map1 || !map2)
4061 return isl_bool_error;
4063 disjoint = isl_map_plain_is_empty(map1);
4064 if (disjoint < 0 || disjoint)
4065 return disjoint;
4067 disjoint = isl_map_plain_is_empty(map2);
4068 if (disjoint < 0 || disjoint)
4069 return disjoint;
4071 match = isl_map_tuple_is_equal(map1, isl_dim_in, map2, isl_dim_in);
4072 if (match < 0 || !match)
4073 return match < 0 ? isl_bool_error : isl_bool_true;
4075 match = isl_map_tuple_is_equal(map1, isl_dim_out, map2, isl_dim_out);
4076 if (match < 0 || !match)
4077 return match < 0 ? isl_bool_error : isl_bool_true;
4079 return isl_bool_false;
4082 /* Are "map1" and "map2" obviously disjoint?
4084 * If one of them is empty or if they live in different spaces (ignoring
4085 * parameters), then they are clearly disjoint.
4086 * This is checked by isl_map_plain_is_disjoint_global.
4088 * If they have different parameters, then we skip any further tests.
4090 * If they are obviously equal, but not obviously empty, then we will
4091 * not be able to detect if they are disjoint.
4093 * Otherwise we check if each basic map in "map1" is obviously disjoint
4094 * from each basic map in "map2".
4096 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
4097 __isl_keep isl_map *map2)
4099 isl_bool disjoint;
4100 isl_bool intersect;
4101 isl_bool match;
4103 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4104 if (disjoint < 0 || disjoint)
4105 return disjoint;
4107 match = isl_map_has_equal_params(map1, map2);
4108 if (match < 0 || !match)
4109 return match < 0 ? isl_bool_error : isl_bool_false;
4111 intersect = isl_map_plain_is_equal(map1, map2);
4112 if (intersect < 0 || intersect)
4113 return intersect < 0 ? isl_bool_error : isl_bool_false;
4115 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4118 /* Are "map1" and "map2" disjoint?
4119 * The parameters are assumed to have been aligned.
4121 * In particular, check whether all pairs of basic maps are disjoint.
4123 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
4124 __isl_keep isl_map *map2)
4126 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4129 /* Are "map1" and "map2" disjoint?
4131 * They are disjoint if they are "obviously disjoint" or if one of them
4132 * is empty. Otherwise, they are not disjoint if one of them is universal.
4133 * If the two inputs are (obviously) equal and not empty, then they are
4134 * not disjoint.
4135 * If none of these cases apply, then check if all pairs of basic maps
4136 * are disjoint after aligning the parameters.
4138 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4140 isl_bool disjoint;
4141 isl_bool intersect;
4143 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4144 if (disjoint < 0 || disjoint)
4145 return disjoint;
4147 disjoint = isl_map_is_empty(map1);
4148 if (disjoint < 0 || disjoint)
4149 return disjoint;
4151 disjoint = isl_map_is_empty(map2);
4152 if (disjoint < 0 || disjoint)
4153 return disjoint;
4155 intersect = isl_map_plain_is_universe(map1);
4156 if (intersect < 0 || intersect)
4157 return isl_bool_not(intersect);
4159 intersect = isl_map_plain_is_universe(map2);
4160 if (intersect < 0 || intersect)
4161 return isl_bool_not(intersect);
4163 intersect = isl_map_plain_is_equal(map1, map2);
4164 if (intersect < 0 || intersect)
4165 return isl_bool_not(intersect);
4167 return isl_map_align_params_map_map_and_test(map1, map2,
4168 &isl_map_is_disjoint_aligned);
4171 /* Are "bmap1" and "bmap2" disjoint?
4173 * They are disjoint if they are "obviously disjoint" or if one of them
4174 * is empty. Otherwise, they are not disjoint if one of them is universal.
4175 * If none of these cases apply, we compute the intersection and see if
4176 * the result is empty.
4178 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4179 __isl_keep isl_basic_map *bmap2)
4181 isl_bool disjoint;
4182 isl_bool intersect;
4183 isl_basic_map *test;
4185 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4186 if (disjoint < 0 || disjoint)
4187 return disjoint;
4189 disjoint = isl_basic_map_is_empty(bmap1);
4190 if (disjoint < 0 || disjoint)
4191 return disjoint;
4193 disjoint = isl_basic_map_is_empty(bmap2);
4194 if (disjoint < 0 || disjoint)
4195 return disjoint;
4197 intersect = isl_basic_map_plain_is_universe(bmap1);
4198 if (intersect < 0 || intersect)
4199 return isl_bool_not(intersect);
4201 intersect = isl_basic_map_plain_is_universe(bmap2);
4202 if (intersect < 0 || intersect)
4203 return isl_bool_not(intersect);
4205 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4206 isl_basic_map_copy(bmap2));
4207 disjoint = isl_basic_map_is_empty(test);
4208 isl_basic_map_free(test);
4210 return disjoint;
4213 /* Are "bset1" and "bset2" disjoint?
4215 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4216 __isl_keep isl_basic_set *bset2)
4218 return isl_basic_map_is_disjoint(bset1, bset2);
4221 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4222 __isl_keep isl_set *set2)
4224 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4227 /* Are "set1" and "set2" disjoint?
4229 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4231 return isl_map_is_disjoint(set1, set2);
4234 /* Is "v" equal to 0, 1 or -1?
4236 static int is_zero_or_one(isl_int v)
4238 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4241 /* Are the "n" coefficients starting at "first" of inequality constraints
4242 * "i" and "j" of "bmap" opposite to each other?
4244 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4245 int first, int n)
4247 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4250 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4251 * apart from the constant term?
4253 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4255 isl_size total;
4257 total = isl_basic_map_dim(bmap, isl_dim_all);
4258 if (total < 0)
4259 return isl_bool_error;
4260 return is_opposite_part(bmap, i, j, 1, total);
4263 /* Check if we can combine a given div with lower bound l and upper
4264 * bound u with some other div and if so return that other div.
4265 * Otherwise, return a position beyond the integer divisions.
4266 * Return isl_size_error on error.
4268 * We first check that
4269 * - the bounds are opposites of each other (except for the constant
4270 * term)
4271 * - the bounds do not reference any other div
4272 * - no div is defined in terms of this div
4274 * Let m be the size of the range allowed on the div by the bounds.
4275 * That is, the bounds are of the form
4277 * e <= a <= e + m - 1
4279 * with e some expression in the other variables.
4280 * We look for another div b such that no third div is defined in terms
4281 * of this second div b and such that in any constraint that contains
4282 * a (except for the given lower and upper bound), also contains b
4283 * with a coefficient that is m times that of b.
4284 * That is, all constraints (except for the lower and upper bound)
4285 * are of the form
4287 * e + f (a + m b) >= 0
4289 * Furthermore, in the constraints that only contain b, the coefficient
4290 * of b should be equal to 1 or -1.
4291 * If so, we return b so that "a + m b" can be replaced by
4292 * a single div "c = a + m b".
4294 static isl_size div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4295 unsigned div, unsigned l, unsigned u)
4297 int i, j;
4298 isl_size n_div;
4299 isl_size v_div;
4300 isl_size coalesce;
4301 isl_bool involves, opp;
4303 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4304 if (n_div <= 1)
4305 return n_div;
4306 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4307 if (v_div < 0)
4308 return isl_size_error;
4309 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4310 return n_div;
4311 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4312 n_div - div - 1) != -1)
4313 return n_div;
4314 opp = is_opposite(bmap, l, u);
4315 if (opp < 0 || !opp)
4316 return opp < 0 ? isl_size_error : n_div;
4318 involves = isl_basic_map_any_div_involves_vars(bmap, v_div + div, 1);
4319 if (involves < 0 || involves)
4320 return involves < 0 ? isl_size_error : n_div;
4322 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4323 if (isl_int_is_neg(bmap->ineq[l][0])) {
4324 isl_int_sub(bmap->ineq[l][0],
4325 bmap->ineq[l][0], bmap->ineq[u][0]);
4326 bmap = isl_basic_map_copy(bmap);
4327 bmap = isl_basic_map_set_to_empty(bmap);
4328 isl_basic_map_free(bmap);
4329 return n_div;
4331 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4332 coalesce = n_div;
4333 for (i = 0; i < n_div; ++i) {
4334 if (i == div)
4335 continue;
4336 if (!pairs[i])
4337 continue;
4338 involves = isl_basic_map_any_div_involves_vars(bmap,
4339 v_div + i, 1);
4340 if (involves < 0)
4341 goto error;
4342 if (involves)
4343 continue;
4344 for (j = 0; j < bmap->n_ineq; ++j) {
4345 int valid;
4346 if (j == l || j == u)
4347 continue;
4348 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4349 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4350 continue;
4351 break;
4353 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4354 break;
4355 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4356 bmap->ineq[j][1 + v_div + div],
4357 bmap->ineq[l][0]);
4358 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4359 bmap->ineq[j][1 + v_div + i]);
4360 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4361 bmap->ineq[j][1 + v_div + div],
4362 bmap->ineq[l][0]);
4363 if (!valid)
4364 break;
4366 if (j < bmap->n_ineq)
4367 continue;
4368 coalesce = i;
4369 break;
4371 if (0)
4372 error: coalesce = isl_size_error;
4373 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4374 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4375 return coalesce;
4378 /* Internal data structure used during the construction and/or evaluation of
4379 * an inequality that ensures that a pair of bounds always allows
4380 * for an integer value.
4382 * "tab" is the tableau in which the inequality is evaluated. It may
4383 * be NULL until it is actually needed.
4384 * "v" contains the inequality coefficients.
4385 * "g", "fl" and "fu" are temporary scalars used during the construction and
4386 * evaluation.
4388 struct test_ineq_data {
4389 struct isl_tab *tab;
4390 isl_vec *v;
4391 isl_int g;
4392 isl_int fl;
4393 isl_int fu;
4396 /* Free all the memory allocated by the fields of "data".
4398 static void test_ineq_data_clear(struct test_ineq_data *data)
4400 isl_tab_free(data->tab);
4401 isl_vec_free(data->v);
4402 isl_int_clear(data->g);
4403 isl_int_clear(data->fl);
4404 isl_int_clear(data->fu);
4407 /* Is the inequality stored in data->v satisfied by "bmap"?
4408 * That is, does it only attain non-negative values?
4409 * data->tab is a tableau corresponding to "bmap".
4411 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4412 struct test_ineq_data *data)
4414 isl_ctx *ctx;
4415 enum isl_lp_result res;
4417 ctx = isl_basic_map_get_ctx(bmap);
4418 if (!data->tab)
4419 data->tab = isl_tab_from_basic_map(bmap, 0);
4420 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4421 if (res == isl_lp_error)
4422 return isl_bool_error;
4423 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4426 /* Given a lower and an upper bound on div i, do they always allow
4427 * for an integer value of the given div?
4428 * Determine this property by constructing an inequality
4429 * such that the property is guaranteed when the inequality is nonnegative.
4430 * The lower bound is inequality l, while the upper bound is inequality u.
4431 * The constructed inequality is stored in data->v.
4433 * Let the upper bound be
4435 * -n_u a + e_u >= 0
4437 * and the lower bound
4439 * n_l a + e_l >= 0
4441 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4442 * We have
4444 * - f_u e_l <= f_u f_l g a <= f_l e_u
4446 * Since all variables are integer valued, this is equivalent to
4448 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4450 * If this interval is at least f_u f_l g, then it contains at least
4451 * one integer value for a.
4452 * That is, the test constraint is
4454 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4456 * or
4458 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4460 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4461 * then the constraint can be scaled down by a factor g',
4462 * with the constant term replaced by
4463 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4464 * Note that the result of applying Fourier-Motzkin to this pair
4465 * of constraints is
4467 * f_l e_u + f_u e_l >= 0
4469 * If the constant term of the scaled down version of this constraint,
4470 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4471 * term of the scaled down test constraint, then the test constraint
4472 * is known to hold and no explicit evaluation is required.
4473 * This is essentially the Omega test.
4475 * If the test constraint consists of only a constant term, then
4476 * it is sufficient to look at the sign of this constant term.
4478 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4479 int l, int u, struct test_ineq_data *data)
4481 unsigned offset;
4482 isl_size n_div;
4484 offset = isl_basic_map_offset(bmap, isl_dim_div);
4485 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4486 if (n_div < 0)
4487 return isl_bool_error;
4489 isl_int_gcd(data->g,
4490 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4491 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4492 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4493 isl_int_neg(data->fu, data->fu);
4494 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4495 data->fu, bmap->ineq[l], offset + n_div);
4496 isl_int_mul(data->g, data->g, data->fl);
4497 isl_int_mul(data->g, data->g, data->fu);
4498 isl_int_sub(data->g, data->g, data->fl);
4499 isl_int_sub(data->g, data->g, data->fu);
4500 isl_int_add_ui(data->g, data->g, 1);
4501 isl_int_sub(data->fl, data->v->el[0], data->g);
4503 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4504 if (isl_int_is_zero(data->g))
4505 return isl_int_is_nonneg(data->fl);
4506 if (isl_int_is_one(data->g)) {
4507 isl_int_set(data->v->el[0], data->fl);
4508 return test_ineq_is_satisfied(bmap, data);
4510 isl_int_fdiv_q(data->fl, data->fl, data->g);
4511 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4512 if (isl_int_eq(data->fl, data->v->el[0]))
4513 return isl_bool_true;
4514 isl_int_set(data->v->el[0], data->fl);
4515 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4516 offset - 1 + n_div);
4518 return test_ineq_is_satisfied(bmap, data);
4521 /* Remove more kinds of divs that are not strictly needed.
4522 * In particular, if all pairs of lower and upper bounds on a div
4523 * are such that they allow at least one integer value of the div,
4524 * then we can eliminate the div using Fourier-Motzkin without
4525 * introducing any spurious solutions.
4527 * If at least one of the two constraints has a unit coefficient for the div,
4528 * then the presence of such a value is guaranteed so there is no need to check.
4529 * In particular, the value attained by the bound with unit coefficient
4530 * can serve as this intermediate value.
4532 static __isl_give isl_basic_map *drop_more_redundant_divs(
4533 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4535 isl_ctx *ctx;
4536 struct test_ineq_data data = { NULL, NULL };
4537 unsigned off;
4538 isl_size n_div;
4539 int remove = -1;
4541 isl_int_init(data.g);
4542 isl_int_init(data.fl);
4543 isl_int_init(data.fu);
4545 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4546 if (n_div < 0)
4547 goto error;
4549 ctx = isl_basic_map_get_ctx(bmap);
4550 off = isl_basic_map_offset(bmap, isl_dim_div);
4551 data.v = isl_vec_alloc(ctx, off + n_div);
4552 if (!data.v)
4553 goto error;
4555 while (n > 0) {
4556 int i, l, u;
4557 int best = -1;
4558 isl_bool has_int;
4560 for (i = 0; i < n_div; ++i) {
4561 if (!pairs[i])
4562 continue;
4563 if (best >= 0 && pairs[best] <= pairs[i])
4564 continue;
4565 best = i;
4568 i = best;
4569 for (l = 0; l < bmap->n_ineq; ++l) {
4570 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4571 continue;
4572 if (isl_int_is_one(bmap->ineq[l][off + i]))
4573 continue;
4574 for (u = 0; u < bmap->n_ineq; ++u) {
4575 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4576 continue;
4577 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4578 continue;
4579 has_int = int_between_bounds(bmap, i, l, u,
4580 &data);
4581 if (has_int < 0)
4582 goto error;
4583 if (data.tab && data.tab->empty)
4584 break;
4585 if (!has_int)
4586 break;
4588 if (u < bmap->n_ineq)
4589 break;
4591 if (data.tab && data.tab->empty) {
4592 bmap = isl_basic_map_set_to_empty(bmap);
4593 break;
4595 if (l == bmap->n_ineq) {
4596 remove = i;
4597 break;
4599 pairs[i] = 0;
4600 --n;
4603 test_ineq_data_clear(&data);
4605 free(pairs);
4607 if (remove < 0)
4608 return bmap;
4610 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4611 return isl_basic_map_drop_redundant_divs(bmap);
4612 error:
4613 free(pairs);
4614 isl_basic_map_free(bmap);
4615 test_ineq_data_clear(&data);
4616 return NULL;
4619 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4620 * and the upper bound u, div1 always occurs together with div2 in the form
4621 * (div1 + m div2), where m is the constant range on the variable div1
4622 * allowed by l and u, replace the pair div1 and div2 by a single
4623 * div that is equal to div1 + m div2.
4625 * The new div will appear in the location that contains div2.
4626 * We need to modify all constraints that contain
4627 * div2 = (div - div1) / m
4628 * The coefficient of div2 is known to be equal to 1 or -1.
4629 * (If a constraint does not contain div2, it will also not contain div1.)
4630 * If the constraint also contains div1, then we know they appear
4631 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4632 * i.e., the coefficient of div is f.
4634 * Otherwise, we first need to introduce div1 into the constraint.
4635 * Let l be
4637 * div1 + f >=0
4639 * and u
4641 * -div1 + f' >= 0
4643 * A lower bound on div2
4645 * div2 + t >= 0
4647 * can be replaced by
4649 * m div2 + div1 + m t + f >= 0
4651 * An upper bound
4653 * -div2 + t >= 0
4655 * can be replaced by
4657 * -(m div2 + div1) + m t + f' >= 0
4659 * These constraint are those that we would obtain from eliminating
4660 * div1 using Fourier-Motzkin.
4662 * After all constraints have been modified, we drop the lower and upper
4663 * bound and then drop div1.
4664 * Since the new div is only placed in the same location that used
4665 * to store div2, but otherwise has a different meaning, any possible
4666 * explicit representation of the original div2 is removed.
4668 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4669 unsigned div1, unsigned div2, unsigned l, unsigned u)
4671 isl_ctx *ctx;
4672 isl_int m;
4673 isl_size v_div;
4674 unsigned total;
4675 int i;
4677 ctx = isl_basic_map_get_ctx(bmap);
4679 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4680 if (v_div < 0)
4681 return isl_basic_map_free(bmap);
4682 total = 1 + v_div + bmap->n_div;
4684 isl_int_init(m);
4685 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4686 isl_int_add_ui(m, m, 1);
4688 for (i = 0; i < bmap->n_ineq; ++i) {
4689 if (i == l || i == u)
4690 continue;
4691 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4692 continue;
4693 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4694 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4695 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4696 ctx->one, bmap->ineq[l], total);
4697 else
4698 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4699 ctx->one, bmap->ineq[u], total);
4701 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4702 bmap->ineq[i][1 + v_div + div1]);
4703 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4706 isl_int_clear(m);
4707 if (l > u) {
4708 isl_basic_map_drop_inequality(bmap, l);
4709 isl_basic_map_drop_inequality(bmap, u);
4710 } else {
4711 isl_basic_map_drop_inequality(bmap, u);
4712 isl_basic_map_drop_inequality(bmap, l);
4714 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4715 bmap = isl_basic_map_drop_div(bmap, div1);
4716 return bmap;
4719 /* First check if we can coalesce any pair of divs and
4720 * then continue with dropping more redundant divs.
4722 * We loop over all pairs of lower and upper bounds on a div
4723 * with coefficient 1 and -1, respectively, check if there
4724 * is any other div "c" with which we can coalesce the div
4725 * and if so, perform the coalescing.
4727 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4728 __isl_take isl_basic_map *bmap, int *pairs, int n)
4730 int i, l, u;
4731 isl_size v_div;
4732 isl_size n_div;
4734 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4735 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4736 if (v_div < 0 || n_div < 0)
4737 return isl_basic_map_free(bmap);
4739 for (i = 0; i < n_div; ++i) {
4740 if (!pairs[i])
4741 continue;
4742 for (l = 0; l < bmap->n_ineq; ++l) {
4743 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4744 continue;
4745 for (u = 0; u < bmap->n_ineq; ++u) {
4746 int c;
4748 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4749 continue;
4750 c = div_find_coalesce(bmap, pairs, i, l, u);
4751 if (c < 0)
4752 goto error;
4753 if (c >= n_div)
4754 continue;
4755 free(pairs);
4756 bmap = coalesce_divs(bmap, i, c, l, u);
4757 return isl_basic_map_drop_redundant_divs(bmap);
4762 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4763 free(pairs);
4764 return bmap;
4767 return drop_more_redundant_divs(bmap, pairs, n);
4768 error:
4769 free(pairs);
4770 isl_basic_map_free(bmap);
4771 return NULL;
4774 /* Are the "n" coefficients starting at "first" of inequality constraints
4775 * "i" and "j" of "bmap" equal to each other?
4777 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4778 int first, int n)
4780 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4783 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4784 * apart from the constant term and the coefficient at position "pos"?
4786 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4787 int pos)
4789 isl_size total;
4791 total = isl_basic_map_dim(bmap, isl_dim_all);
4792 if (total < 0)
4793 return isl_bool_error;
4794 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4795 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4798 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4799 * apart from the constant term and the coefficient at position "pos"?
4801 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4802 int pos)
4804 isl_size total;
4806 total = isl_basic_map_dim(bmap, isl_dim_all);
4807 if (total < 0)
4808 return isl_bool_error;
4809 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4810 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4813 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4814 * been modified, simplying it if "simplify" is set.
4815 * Free the temporary data structure "pairs" that was associated
4816 * to the old version of "bmap".
4818 static __isl_give isl_basic_map *drop_redundant_divs_again(
4819 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4821 if (simplify)
4822 bmap = isl_basic_map_simplify(bmap);
4823 free(pairs);
4824 return isl_basic_map_drop_redundant_divs(bmap);
4827 /* Is "div" the single unknown existentially quantified variable
4828 * in inequality constraint "ineq" of "bmap"?
4829 * "div" is known to have a non-zero coefficient in "ineq".
4831 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4832 int div)
4834 int i;
4835 isl_size n_div;
4836 unsigned o_div;
4837 isl_bool known;
4839 known = isl_basic_map_div_is_known(bmap, div);
4840 if (known < 0 || known)
4841 return isl_bool_not(known);
4842 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4843 if (n_div < 0)
4844 return isl_bool_error;
4845 if (n_div == 1)
4846 return isl_bool_true;
4847 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4848 for (i = 0; i < n_div; ++i) {
4849 isl_bool known;
4851 if (i == div)
4852 continue;
4853 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4854 continue;
4855 known = isl_basic_map_div_is_known(bmap, i);
4856 if (known < 0 || !known)
4857 return known;
4860 return isl_bool_true;
4863 /* Does integer division "div" have coefficient 1 in inequality constraint
4864 * "ineq" of "map"?
4866 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4868 unsigned o_div;
4870 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4871 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4872 return isl_bool_true;
4874 return isl_bool_false;
4877 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4878 * then try and drop redundant divs again,
4879 * freeing the temporary data structure "pairs" that was associated
4880 * to the old version of "bmap".
4882 static __isl_give isl_basic_map *set_eq_and_try_again(
4883 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4885 bmap = isl_basic_map_cow(bmap);
4886 isl_basic_map_inequality_to_equality(bmap, ineq);
4887 return drop_redundant_divs_again(bmap, pairs, 1);
4890 /* Drop the integer division at position "div", along with the two
4891 * inequality constraints "ineq1" and "ineq2" in which it appears
4892 * from "bmap" and then try and drop redundant divs again,
4893 * freeing the temporary data structure "pairs" that was associated
4894 * to the old version of "bmap".
4896 static __isl_give isl_basic_map *drop_div_and_try_again(
4897 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4898 __isl_take int *pairs)
4900 if (ineq1 > ineq2) {
4901 isl_basic_map_drop_inequality(bmap, ineq1);
4902 isl_basic_map_drop_inequality(bmap, ineq2);
4903 } else {
4904 isl_basic_map_drop_inequality(bmap, ineq2);
4905 isl_basic_map_drop_inequality(bmap, ineq1);
4907 bmap = isl_basic_map_drop_div(bmap, div);
4908 return drop_redundant_divs_again(bmap, pairs, 0);
4911 /* Given two inequality constraints
4913 * f(x) + n d + c >= 0, (ineq)
4915 * with d the variable at position "pos", and
4917 * f(x) + c0 >= 0, (lower)
4919 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4920 * determined by the first constraint.
4921 * That is, store
4923 * ceil((c0 - c)/n)
4925 * in *l.
4927 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4928 int ineq, int lower, int pos, isl_int *l)
4930 isl_int_neg(*l, bmap->ineq[ineq][0]);
4931 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4932 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4935 /* Given two inequality constraints
4937 * f(x) + n d + c >= 0, (ineq)
4939 * with d the variable at position "pos", and
4941 * -f(x) - c0 >= 0, (upper)
4943 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4944 * determined by the first constraint.
4945 * That is, store
4947 * ceil((-c1 - c)/n)
4949 * in *u.
4951 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4952 int ineq, int upper, int pos, isl_int *u)
4954 isl_int_neg(*u, bmap->ineq[ineq][0]);
4955 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4956 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4959 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4960 * does the corresponding lower bound have a fixed value in "bmap"?
4962 * In particular, "ineq" is of the form
4964 * f(x) + n d + c >= 0
4966 * with n > 0, c the constant term and
4967 * d the existentially quantified variable "div".
4968 * That is, the lower bound is
4970 * ceil((-f(x) - c)/n)
4972 * Look for a pair of constraints
4974 * f(x) + c0 >= 0
4975 * -f(x) + c1 >= 0
4977 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4978 * That is, check that
4980 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4982 * If so, return the index of inequality f(x) + c0 >= 0.
4983 * Otherwise, return bmap->n_ineq.
4984 * Return -1 on error.
4986 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4988 int i;
4989 int lower = -1, upper = -1;
4990 unsigned o_div;
4991 isl_int l, u;
4992 int equal;
4994 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4995 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4996 isl_bool par, opp;
4998 if (i == ineq)
4999 continue;
5000 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
5001 continue;
5002 par = isl_bool_false;
5003 if (lower < 0)
5004 par = is_parallel_except(bmap, ineq, i, o_div + div);
5005 if (par < 0)
5006 return -1;
5007 if (par) {
5008 lower = i;
5009 continue;
5011 opp = isl_bool_false;
5012 if (upper < 0)
5013 opp = is_opposite_except(bmap, ineq, i, o_div + div);
5014 if (opp < 0)
5015 return -1;
5016 if (opp)
5017 upper = i;
5020 if (lower < 0 || upper < 0)
5021 return bmap->n_ineq;
5023 isl_int_init(l);
5024 isl_int_init(u);
5026 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
5027 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
5029 equal = isl_int_eq(l, u);
5031 isl_int_clear(l);
5032 isl_int_clear(u);
5034 return equal ? lower : bmap->n_ineq;
5037 /* Given a lower bound constraint "ineq" on the existentially quantified
5038 * variable "div", such that the corresponding lower bound has
5039 * a fixed value in "bmap", assign this fixed value to the variable and
5040 * then try and drop redundant divs again,
5041 * freeing the temporary data structure "pairs" that was associated
5042 * to the old version of "bmap".
5043 * "lower" determines the constant value for the lower bound.
5045 * In particular, "ineq" is of the form
5047 * f(x) + n d + c >= 0,
5049 * while "lower" is of the form
5051 * f(x) + c0 >= 0
5053 * The lower bound is ceil((-f(x) - c)/n) and its constant value
5054 * is ceil((c0 - c)/n).
5056 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
5057 int div, int ineq, int lower, int *pairs)
5059 isl_int c;
5060 unsigned o_div;
5062 isl_int_init(c);
5064 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5065 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
5066 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
5067 free(pairs);
5069 isl_int_clear(c);
5071 return isl_basic_map_drop_redundant_divs(bmap);
5074 /* Do any of the integer divisions of "bmap" involve integer division "div"?
5076 * The integer division "div" could only ever appear in any later
5077 * integer division (with an explicit representation).
5079 static isl_bool any_div_involves_div(__isl_keep isl_basic_map *bmap, int div)
5081 int i;
5082 isl_size v_div, n_div;
5084 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5085 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5086 if (v_div < 0 || n_div < 0)
5087 return isl_bool_error;
5089 for (i = div + 1; i < n_div; ++i) {
5090 isl_bool involves;
5092 involves = isl_basic_map_div_expr_involves_vars(bmap, i,
5093 v_div + div, 1);
5094 if (involves < 0 || involves)
5095 return involves;
5098 return isl_bool_false;
5101 /* Remove divs that are not strictly needed based on the inequality
5102 * constraints.
5103 * In particular, if a div only occurs positively (or negatively)
5104 * in constraints, then it can simply be dropped.
5105 * Also, if a div occurs in only two constraints and if moreover
5106 * those two constraints are opposite to each other, except for the constant
5107 * term and if the sum of the constant terms is such that for any value
5108 * of the other values, there is always at least one integer value of the
5109 * div, i.e., if one plus this sum is greater than or equal to
5110 * the (absolute value) of the coefficient of the div in the constraints,
5111 * then we can also simply drop the div.
5113 * If an existentially quantified variable does not have an explicit
5114 * representation, appears in only a single lower bound that does not
5115 * involve any other such existentially quantified variables and appears
5116 * in this lower bound with coefficient 1,
5117 * then fix the variable to the value of the lower bound. That is,
5118 * turn the inequality into an equality.
5119 * If for any value of the other variables, there is any value
5120 * for the existentially quantified variable satisfying the constraints,
5121 * then this lower bound also satisfies the constraints.
5122 * It is therefore safe to pick this lower bound.
5124 * The same reasoning holds even if the coefficient is not one.
5125 * However, fixing the variable to the value of the lower bound may
5126 * in general introduce an extra integer division, in which case
5127 * it may be better to pick another value.
5128 * If this integer division has a known constant value, then plugging
5129 * in this constant value removes the existentially quantified variable
5130 * completely. In particular, if the lower bound is of the form
5131 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5132 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5133 * then the existentially quantified variable can be assigned this
5134 * shared value.
5136 * We skip divs that appear in equalities or in the definition of other divs.
5137 * Divs that appear in the definition of other divs usually occur in at least
5138 * 4 constraints, but the constraints may have been simplified.
5140 * If any divs are left after these simple checks then we move on
5141 * to more complicated cases in drop_more_redundant_divs.
5143 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
5144 __isl_take isl_basic_map *bmap)
5146 int i, j;
5147 isl_size off;
5148 int *pairs = NULL;
5149 int n = 0;
5150 isl_size n_ineq;
5152 if (!bmap)
5153 goto error;
5154 if (bmap->n_div == 0)
5155 return bmap;
5157 off = isl_basic_map_var_offset(bmap, isl_dim_div);
5158 if (off < 0)
5159 return isl_basic_map_free(bmap);
5160 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
5161 if (!pairs)
5162 goto error;
5164 n_ineq = isl_basic_map_n_inequality(bmap);
5165 if (n_ineq < 0)
5166 goto error;
5167 for (i = 0; i < bmap->n_div; ++i) {
5168 int pos, neg;
5169 int last_pos, last_neg;
5170 int redundant;
5171 int defined;
5172 isl_bool involves, opp, set_div;
5174 defined = !isl_int_is_zero(bmap->div[i][0]);
5175 involves = any_div_involves_div(bmap, i);
5176 if (involves < 0)
5177 goto error;
5178 if (involves)
5179 continue;
5180 for (j = 0; j < bmap->n_eq; ++j)
5181 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5182 break;
5183 if (j < bmap->n_eq)
5184 continue;
5185 ++n;
5186 pos = neg = 0;
5187 for (j = 0; j < bmap->n_ineq; ++j) {
5188 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5189 last_pos = j;
5190 ++pos;
5192 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5193 last_neg = j;
5194 ++neg;
5197 pairs[i] = pos * neg;
5198 if (pairs[i] == 0) {
5199 for (j = bmap->n_ineq - 1; j >= 0; --j)
5200 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5201 isl_basic_map_drop_inequality(bmap, j);
5202 bmap = isl_basic_map_drop_div(bmap, i);
5203 return drop_redundant_divs_again(bmap, pairs, 0);
5205 if (pairs[i] != 1)
5206 opp = isl_bool_false;
5207 else
5208 opp = is_opposite(bmap, last_pos, last_neg);
5209 if (opp < 0)
5210 goto error;
5211 if (!opp) {
5212 int lower;
5213 isl_bool single, one;
5215 if (pos != 1)
5216 continue;
5217 single = single_unknown(bmap, last_pos, i);
5218 if (single < 0)
5219 goto error;
5220 if (!single)
5221 continue;
5222 one = has_coef_one(bmap, i, last_pos);
5223 if (one < 0)
5224 goto error;
5225 if (one)
5226 return set_eq_and_try_again(bmap, last_pos,
5227 pairs);
5228 lower = lower_bound_is_cst(bmap, i, last_pos);
5229 if (lower < 0)
5230 goto error;
5231 if (lower < n_ineq)
5232 return fix_cst_lower(bmap, i, last_pos, lower,
5233 pairs);
5234 continue;
5237 isl_int_add(bmap->ineq[last_pos][0],
5238 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5239 isl_int_add_ui(bmap->ineq[last_pos][0],
5240 bmap->ineq[last_pos][0], 1);
5241 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5242 bmap->ineq[last_pos][1+off+i]);
5243 isl_int_sub_ui(bmap->ineq[last_pos][0],
5244 bmap->ineq[last_pos][0], 1);
5245 isl_int_sub(bmap->ineq[last_pos][0],
5246 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5247 if (redundant)
5248 return drop_div_and_try_again(bmap, i,
5249 last_pos, last_neg, pairs);
5250 if (defined)
5251 set_div = isl_bool_false;
5252 else
5253 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5254 if (set_div < 0)
5255 return isl_basic_map_free(bmap);
5256 if (set_div) {
5257 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5258 return drop_redundant_divs_again(bmap, pairs, 1);
5260 pairs[i] = 0;
5261 --n;
5264 if (n > 0)
5265 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5267 free(pairs);
5268 return bmap;
5269 error:
5270 free(pairs);
5271 isl_basic_map_free(bmap);
5272 return NULL;
5275 /* Consider the coefficients at "c" as a row vector and replace
5276 * them with their product with "T". "T" is assumed to be a square matrix.
5278 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5280 isl_size n;
5281 isl_ctx *ctx;
5282 isl_vec *v;
5284 n = isl_mat_rows(T);
5285 if (n < 0)
5286 return isl_stat_error;
5287 if (isl_seq_first_non_zero(c, n) == -1)
5288 return isl_stat_ok;
5289 ctx = isl_mat_get_ctx(T);
5290 v = isl_vec_alloc(ctx, n);
5291 if (!v)
5292 return isl_stat_error;
5293 isl_seq_swp_or_cpy(v->el, c, n);
5294 v = isl_vec_mat_product(v, isl_mat_copy(T));
5295 if (!v)
5296 return isl_stat_error;
5297 isl_seq_swp_or_cpy(c, v->el, n);
5298 isl_vec_free(v);
5300 return isl_stat_ok;
5303 /* Plug in T for the variables in "bmap" starting at "pos".
5304 * T is a linear unimodular matrix, i.e., without constant term.
5306 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5307 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5309 int i;
5310 isl_size n_row, n_col;
5312 bmap = isl_basic_map_cow(bmap);
5313 n_row = isl_mat_rows(T);
5314 n_col = isl_mat_cols(T);
5315 if (!bmap || n_row < 0 || n_col < 0)
5316 goto error;
5318 if (n_col != n_row)
5319 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5320 "expecting square matrix", goto error);
5322 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5323 goto error;
5325 for (i = 0; i < bmap->n_eq; ++i)
5326 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5327 goto error;
5328 for (i = 0; i < bmap->n_ineq; ++i)
5329 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5330 goto error;
5331 for (i = 0; i < bmap->n_div; ++i) {
5332 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5333 continue;
5334 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5335 goto error;
5338 isl_mat_free(T);
5339 return bmap;
5340 error:
5341 isl_basic_map_free(bmap);
5342 isl_mat_free(T);
5343 return NULL;
5346 /* Remove divs that are not strictly needed.
5348 * First look for an equality constraint involving two or more
5349 * existentially quantified variables without an explicit
5350 * representation. Replace the combination that appears
5351 * in the equality constraint by a single existentially quantified
5352 * variable such that the equality can be used to derive
5353 * an explicit representation for the variable.
5354 * If there are no more such equality constraints, then continue
5355 * with isl_basic_map_drop_redundant_divs_ineq.
5357 * In particular, if the equality constraint is of the form
5359 * f(x) + \sum_i c_i a_i = 0
5361 * with a_i existentially quantified variable without explicit
5362 * representation, then apply a transformation on the existentially
5363 * quantified variables to turn the constraint into
5365 * f(x) + g a_1' = 0
5367 * with g the gcd of the c_i.
5368 * In order to easily identify which existentially quantified variables
5369 * have a complete explicit representation, i.e., without being defined
5370 * in terms of other existentially quantified variables without
5371 * an explicit representation, the existentially quantified variables
5372 * are first sorted.
5374 * The variable transformation is computed by extending the row
5375 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5377 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5378 * [a_2'] [ a_2 ]
5379 * ... = U ....
5380 * [a_n'] [ a_n ]
5382 * with [c_1/g ... c_n/g] representing the first row of U.
5383 * The inverse of U is then plugged into the original constraints.
5384 * The call to isl_basic_map_simplify makes sure the explicit
5385 * representation for a_1' is extracted from the equality constraint.
5387 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5388 __isl_take isl_basic_map *bmap)
5390 int first;
5391 int i;
5392 unsigned o_div;
5393 isl_size n_div;
5394 int l;
5395 isl_ctx *ctx;
5396 isl_mat *T;
5398 if (!bmap)
5399 return NULL;
5400 if (isl_basic_map_divs_known(bmap))
5401 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5402 if (bmap->n_eq == 0)
5403 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5404 bmap = isl_basic_map_sort_divs(bmap);
5405 if (!bmap)
5406 return NULL;
5408 first = isl_basic_map_first_unknown_div(bmap);
5409 if (first < 0)
5410 return isl_basic_map_free(bmap);
5412 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5413 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5414 if (n_div < 0)
5415 return isl_basic_map_free(bmap);
5417 for (i = 0; i < bmap->n_eq; ++i) {
5418 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5419 n_div - (first));
5420 if (l < 0)
5421 continue;
5422 l += first;
5423 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5424 n_div - (l + 1)) == -1)
5425 continue;
5426 break;
5428 if (i >= bmap->n_eq)
5429 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5431 ctx = isl_basic_map_get_ctx(bmap);
5432 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5433 if (!T)
5434 return isl_basic_map_free(bmap);
5435 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5436 T = isl_mat_normalize_row(T, 0);
5437 T = isl_mat_unimodular_complete(T, 1);
5438 T = isl_mat_right_inverse(T);
5440 for (i = l; i < n_div; ++i)
5441 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5442 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5443 bmap = isl_basic_map_simplify(bmap);
5445 return isl_basic_map_drop_redundant_divs(bmap);
5448 /* Does "bmap" satisfy any equality that involves more than 2 variables
5449 * and/or has coefficients different from -1 and 1?
5451 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5453 int i;
5454 isl_size total;
5456 total = isl_basic_map_dim(bmap, isl_dim_all);
5457 if (total < 0)
5458 return isl_bool_error;
5460 for (i = 0; i < bmap->n_eq; ++i) {
5461 int j, k;
5463 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5464 if (j < 0)
5465 continue;
5466 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5467 !isl_int_is_negone(bmap->eq[i][1 + j]))
5468 return isl_bool_true;
5470 j += 1;
5471 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5472 if (k < 0)
5473 continue;
5474 j += k;
5475 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5476 !isl_int_is_negone(bmap->eq[i][1 + j]))
5477 return isl_bool_true;
5479 j += 1;
5480 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5481 if (k >= 0)
5482 return isl_bool_true;
5485 return isl_bool_false;
5488 /* Remove any common factor g from the constraint coefficients in "v".
5489 * The constant term is stored in the first position and is replaced
5490 * by floor(c/g). If any common factor is removed and if this results
5491 * in a tightening of the constraint, then set *tightened.
5493 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5494 int *tightened)
5496 isl_ctx *ctx;
5498 if (!v)
5499 return NULL;
5500 ctx = isl_vec_get_ctx(v);
5501 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5502 if (isl_int_is_zero(ctx->normalize_gcd))
5503 return v;
5504 if (isl_int_is_one(ctx->normalize_gcd))
5505 return v;
5506 v = isl_vec_cow(v);
5507 if (!v)
5508 return NULL;
5509 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5510 *tightened = 1;
5511 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5512 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5513 v->size - 1);
5514 return v;
5517 /* Internal representation used by isl_basic_map_reduce_coefficients.
5519 * "total" is the total dimensionality of the original basic map.
5520 * "v" is a temporary vector of size 1 + total that can be used
5521 * to store constraint coefficients.
5522 * "T" is the variable compression.
5523 * "T2" is the inverse transformation.
5524 * "tightened" is set if any constant term got tightened
5525 * while reducing the coefficients.
5527 struct isl_reduce_coefficients_data {
5528 isl_size total;
5529 isl_vec *v;
5530 isl_mat *T;
5531 isl_mat *T2;
5532 int tightened;
5535 /* Free all memory allocated in "data".
5537 static void isl_reduce_coefficients_data_clear(
5538 struct isl_reduce_coefficients_data *data)
5540 data->T = isl_mat_free(data->T);
5541 data->T2 = isl_mat_free(data->T2);
5542 data->v = isl_vec_free(data->v);
5545 /* Initialize "data" for "bmap", freeing all allocated memory
5546 * if anything goes wrong.
5548 * In particular, construct a variable compression
5549 * from the equality constraints of "bmap" and
5550 * allocate a temporary vector.
5552 static isl_stat isl_reduce_coefficients_data_init(
5553 __isl_keep isl_basic_map *bmap,
5554 struct isl_reduce_coefficients_data *data)
5556 isl_ctx *ctx;
5557 isl_mat *eq;
5559 data->v = NULL;
5560 data->T = NULL;
5561 data->T2 = NULL;
5562 data->tightened = 0;
5564 data->total = isl_basic_map_dim(bmap, isl_dim_all);
5565 if (data->total < 0)
5566 return isl_stat_error;
5567 ctx = isl_basic_map_get_ctx(bmap);
5568 data->v = isl_vec_alloc(ctx, 1 + data->total);
5569 if (!data->v)
5570 return isl_stat_error;
5572 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq,
5573 0, 1 + data->total);
5574 data->T = isl_mat_variable_compression(eq, &data->T2);
5575 if (!data->T || !data->T2)
5576 goto error;
5578 return isl_stat_ok;
5579 error:
5580 isl_reduce_coefficients_data_clear(data);
5581 return isl_stat_error;
5584 /* Reduce the coefficients of "bmap" by applying the variable compression
5585 * in "data".
5586 * In particular, apply the variable compression to each constraint,
5587 * factor out any common factor in the non-constant coefficients and
5588 * then apply the inverse of the compression.
5590 * Only apply the reduction on a single copy of the basic map
5591 * since the reduction may leave the result in an inconsistent state.
5592 * In particular, the constraints may not be gaussed.
5594 static __isl_give isl_basic_map *reduce_coefficients(
5595 __isl_take isl_basic_map *bmap,
5596 struct isl_reduce_coefficients_data *data)
5598 int i;
5599 isl_size total;
5601 total = isl_basic_map_dim(bmap, isl_dim_all);
5602 if (total < 0)
5603 return isl_basic_map_free(bmap);
5604 if (total != data->total)
5605 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
5606 "total dimensionality changed unexpectedly",
5607 return isl_basic_map_free(bmap));
5609 bmap = isl_basic_map_cow(bmap);
5610 if (!bmap)
5611 return NULL;
5613 for (i = 0; i < bmap->n_ineq; ++i) {
5614 isl_seq_cpy(data->v->el, bmap->ineq[i], 1 + data->total);
5615 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T));
5616 data->v = normalize_constraint(data->v, &data->tightened);
5617 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T2));
5618 if (!data->v)
5619 return isl_basic_map_free(bmap);
5620 isl_seq_cpy(bmap->ineq[i], data->v->el, 1 + data->total);
5623 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5625 return bmap;
5628 /* If "bmap" is an integer set that satisfies any equality involving
5629 * more than 2 variables and/or has coefficients different from -1 and 1,
5630 * then use variable compression to reduce the coefficients by removing
5631 * any (hidden) common factor.
5632 * In particular, apply the variable compression to each constraint,
5633 * factor out any common factor in the non-constant coefficients and
5634 * then apply the inverse of the compression.
5635 * At the end, we mark the basic map as having reduced constants.
5636 * If this flag is still set on the next invocation of this function,
5637 * then we skip the computation.
5639 * Removing a common factor may result in a tightening of some of
5640 * the constraints. If this happens, then we may end up with two
5641 * opposite inequalities that can be replaced by an equality.
5642 * We therefore call isl_basic_map_detect_inequality_pairs,
5643 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5644 * and isl_basic_map_gauss if such a pair was found.
5645 * This call to isl_basic_map_gauss may undo much of the effect
5646 * of the reduction on which isl_map_coalesce depends.
5647 * In particular, constraints in terms of (compressed) local variables
5648 * get reformulated in terms of the set variables again.
5649 * The reduction is therefore applied again afterwards.
5650 * This has to be done before the call to eliminate_divs_eq, however,
5651 * since that may remove some local variables, while
5652 * the data used during the reduction is formulated in terms
5653 * of the original variables.
5655 * Tightening may also result in some other constraints becoming
5656 * (rationally) redundant with respect to the tightened constraint
5657 * (in combination with other constraints). The basic map may
5658 * therefore no longer be assumed to have no redundant constraints.
5660 * Note that this function may leave the result in an inconsistent state.
5661 * In particular, the constraints may not be gaussed.
5662 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5663 * for some of the test cases to pass successfully.
5664 * Any potential modification of the representation is therefore only
5665 * performed on a single copy of the basic map.
5667 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5668 __isl_take isl_basic_map *bmap)
5670 struct isl_reduce_coefficients_data data;
5671 isl_bool multi;
5673 if (!bmap)
5674 return NULL;
5675 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5676 return bmap;
5677 if (isl_basic_map_is_rational(bmap))
5678 return bmap;
5679 if (bmap->n_eq == 0)
5680 return bmap;
5681 multi = has_multiple_var_equality(bmap);
5682 if (multi < 0)
5683 return isl_basic_map_free(bmap);
5684 if (!multi)
5685 return bmap;
5687 if (isl_reduce_coefficients_data_init(bmap, &data) < 0)
5688 return isl_basic_map_free(bmap);
5690 if (data.T->n_col == 0) {
5691 isl_reduce_coefficients_data_clear(&data);
5692 return isl_basic_map_set_to_empty(bmap);
5695 bmap = reduce_coefficients(bmap, &data);
5696 if (!bmap)
5697 goto error;
5699 if (data.tightened) {
5700 int progress = 0;
5702 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
5703 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5704 if (progress) {
5705 bmap = isl_basic_map_gauss(bmap, NULL);
5706 bmap = reduce_coefficients(bmap, &data);
5707 bmap = eliminate_divs_eq(bmap, &progress);
5711 isl_reduce_coefficients_data_clear(&data);
5713 return bmap;
5714 error:
5715 isl_reduce_coefficients_data_clear(&data);
5716 return isl_basic_map_free(bmap);
5719 /* Shift the integer division at position "div" of "bmap"
5720 * by "shift" times the variable at position "pos".
5721 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5722 * corresponds to the constant term.
5724 * That is, if the integer division has the form
5726 * floor(f(x)/d)
5728 * then replace it by
5730 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5732 __isl_give isl_basic_map *isl_basic_map_shift_div(
5733 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5735 int i;
5736 isl_size total, n_div;
5738 if (isl_int_is_zero(shift))
5739 return bmap;
5740 total = isl_basic_map_dim(bmap, isl_dim_all);
5741 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5742 total -= n_div;
5743 if (total < 0 || n_div < 0)
5744 return isl_basic_map_free(bmap);
5746 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5748 for (i = 0; i < bmap->n_eq; ++i) {
5749 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5750 continue;
5751 isl_int_submul(bmap->eq[i][pos],
5752 shift, bmap->eq[i][1 + total + div]);
5754 for (i = 0; i < bmap->n_ineq; ++i) {
5755 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5756 continue;
5757 isl_int_submul(bmap->ineq[i][pos],
5758 shift, bmap->ineq[i][1 + total + div]);
5760 for (i = 0; i < bmap->n_div; ++i) {
5761 if (isl_int_is_zero(bmap->div[i][0]))
5762 continue;
5763 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5764 continue;
5765 isl_int_submul(bmap->div[i][1 + pos],
5766 shift, bmap->div[i][1 + 1 + total + div]);
5769 return bmap;