isl_basic_map_remove_duplicate_constraints: do not mark progress on removal
[isl.git] / isl_map_simplify.c
blobd3764c234680c6ee99e57df7335c87286760ea43
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 /* Mark "bmap" as having one or more inequality constraints modified.
32 * If "equivalent" is set, then this modification was done based
33 * on an equality constraint already available in "bmap".
35 * Any modification may result in the constraints no longer being sorted and
36 * may also undo the effect of reduce_coefficients.
38 * A modification that uses extra information may also result
39 * in the modified constraint(s) becoming redundant or
40 * turning into an implicit equality constraint.
42 static __isl_give isl_basic_map *isl_basic_map_modify_inequality(
43 __isl_take isl_basic_map *bmap, int equivalent)
45 if (!bmap)
46 return NULL;
47 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
48 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
49 if (equivalent)
50 return bmap;
51 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
52 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
53 return bmap;
56 static void swap_equality(__isl_keep isl_basic_map *bmap, int a, int b)
58 isl_int *t = bmap->eq[a];
59 bmap->eq[a] = bmap->eq[b];
60 bmap->eq[b] = t;
63 static void swap_inequality(__isl_keep isl_basic_map *bmap, int a, int b)
65 if (a != b) {
66 isl_int *t = bmap->ineq[a];
67 bmap->ineq[a] = bmap->ineq[b];
68 bmap->ineq[b] = t;
72 /* Scale down the inequality constraint "ineq" of length "len"
73 * by a factor of "f".
74 * All the coefficients, except the constant term,
75 * are assumed to be multiples of "f".
77 * If the factor is 0 or 1, then no scaling needs to be performed.
79 * If scaling is performed then take into account that the constraint
80 * is modified (not simply based on an equality constraint).
82 static __isl_give isl_basic_map *scale_down_inequality(
83 __isl_take isl_basic_map *bmap, int ineq, isl_int f, unsigned len)
85 if (!bmap)
86 return NULL;
88 if (isl_int_is_zero(f) || isl_int_is_one(f))
89 return bmap;
91 isl_int_fdiv_q(bmap->ineq[ineq][0], bmap->ineq[ineq][0], f);
92 isl_seq_scale_down(bmap->ineq[ineq] + 1, bmap->ineq[ineq] + 1, f, len);
94 bmap = isl_basic_map_modify_inequality(bmap, 0);
96 return bmap;
99 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
100 __isl_take isl_basic_map *bmap)
102 int i;
103 isl_int gcd;
104 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
106 if (total < 0)
107 return isl_basic_map_free(bmap);
109 isl_int_init(gcd);
110 for (i = bmap->n_eq - 1; i >= 0; --i) {
111 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
112 if (isl_int_is_zero(gcd)) {
113 if (!isl_int_is_zero(bmap->eq[i][0])) {
114 bmap = isl_basic_map_set_to_empty(bmap);
115 break;
117 if (isl_basic_map_drop_equality(bmap, i) < 0)
118 goto error;
119 continue;
121 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
122 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
123 if (isl_int_is_one(gcd))
124 continue;
125 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
126 bmap = isl_basic_map_set_to_empty(bmap);
127 break;
129 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
132 for (i = bmap->n_ineq - 1; i >= 0; --i) {
133 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
134 if (isl_int_is_zero(gcd)) {
135 if (isl_int_is_neg(bmap->ineq[i][0])) {
136 bmap = isl_basic_map_set_to_empty(bmap);
137 break;
139 if (isl_basic_map_drop_inequality(bmap, i) < 0)
140 goto error;
141 continue;
143 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
144 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
145 bmap = scale_down_inequality(bmap, i, gcd, total);
146 if (!bmap)
147 goto error;
149 isl_int_clear(gcd);
151 return bmap;
152 error:
153 isl_int_clear(gcd);
154 isl_basic_map_free(bmap);
155 return NULL;
158 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
159 __isl_take isl_basic_set *bset)
161 isl_basic_map *bmap = bset_to_bmap(bset);
162 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
165 /* Reduce the coefficient of the variable at position "pos"
166 * in integer division "div", such that it lies in the half-open
167 * interval (1/2,1/2], extracting any excess value from this integer division.
168 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
169 * corresponds to the constant term.
171 * That is, the integer division is of the form
173 * floor((... + (c * d + r) * x_pos + ...)/d)
175 * with -d < 2 * r <= d.
176 * Replace it by
178 * floor((... + r * x_pos + ...)/d) + c * x_pos
180 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
181 * Otherwise, c = floor((c * d + r)/d) + 1.
183 * This is the same normalization that is performed by isl_aff_floor.
185 static __isl_give isl_basic_map *reduce_coefficient_in_div(
186 __isl_take isl_basic_map *bmap, int div, int pos)
188 isl_int shift;
189 int add_one;
191 isl_int_init(shift);
192 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
193 isl_int_mul_ui(shift, shift, 2);
194 add_one = isl_int_gt(shift, bmap->div[div][0]);
195 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
196 if (add_one)
197 isl_int_add_ui(shift, shift, 1);
198 isl_int_neg(shift, shift);
199 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
200 isl_int_clear(shift);
202 return bmap;
205 /* Does the coefficient of the variable at position "pos"
206 * in integer division "div" need to be reduced?
207 * That is, does it lie outside the half-open interval (1/2,1/2]?
208 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
209 * 2 * c != d.
211 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
212 int pos)
214 isl_bool r;
216 if (isl_int_is_zero(bmap->div[div][1 + pos]))
217 return isl_bool_false;
219 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
220 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
221 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
222 isl_int_divexact_ui(bmap->div[div][1 + pos],
223 bmap->div[div][1 + pos], 2);
225 return r;
228 /* Reduce the coefficients (including the constant term) of
229 * integer division "div", if needed.
230 * In particular, make sure all coefficients lie in
231 * the half-open interval (1/2,1/2].
233 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
234 __isl_take isl_basic_map *bmap, int div)
236 int i;
237 isl_size total;
239 total = isl_basic_map_dim(bmap, isl_dim_all);
240 if (total < 0)
241 return isl_basic_map_free(bmap);
242 for (i = 0; i < 1 + total; ++i) {
243 isl_bool reduce;
245 reduce = needs_reduction(bmap, div, i);
246 if (reduce < 0)
247 return isl_basic_map_free(bmap);
248 if (!reduce)
249 continue;
250 bmap = reduce_coefficient_in_div(bmap, div, i);
251 if (!bmap)
252 break;
255 return bmap;
258 /* Reduce the coefficients (including the constant term) of
259 * the known integer divisions, if needed
260 * In particular, make sure all coefficients lie in
261 * the half-open interval (1/2,1/2].
263 static __isl_give isl_basic_map *reduce_div_coefficients(
264 __isl_take isl_basic_map *bmap)
266 int i;
268 if (!bmap)
269 return NULL;
270 if (bmap->n_div == 0)
271 return bmap;
273 for (i = 0; i < bmap->n_div; ++i) {
274 if (isl_int_is_zero(bmap->div[i][0]))
275 continue;
276 bmap = reduce_div_coefficients_of_div(bmap, i);
277 if (!bmap)
278 break;
281 return bmap;
284 /* Remove any common factor in numerator and denominator of the div expression,
285 * not taking into account the constant term.
286 * That is, if the div is of the form
288 * floor((a + m f(x))/(m d))
290 * then replace it by
292 * floor((floor(a/m) + f(x))/d)
294 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
295 * and can therefore not influence the result of the floor.
297 static __isl_give isl_basic_map *normalize_div_expression(
298 __isl_take isl_basic_map *bmap, int div)
300 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
301 isl_ctx *ctx = bmap->ctx;
303 if (total < 0)
304 return isl_basic_map_free(bmap);
305 if (isl_int_is_zero(bmap->div[div][0]))
306 return bmap;
307 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
308 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
309 if (isl_int_is_one(ctx->normalize_gcd))
310 return bmap;
311 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
312 ctx->normalize_gcd);
313 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
314 ctx->normalize_gcd);
315 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
316 ctx->normalize_gcd, total);
318 return bmap;
321 /* Remove any common factor in numerator and denominator of a div expression,
322 * not taking into account the constant term.
323 * That is, look for any div of the form
325 * floor((a + m f(x))/(m d))
327 * and replace it by
329 * floor((floor(a/m) + f(x))/d)
331 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
332 * and can therefore not influence the result of the floor.
334 static __isl_give isl_basic_map *normalize_div_expressions(
335 __isl_take isl_basic_map *bmap)
337 int i;
339 if (!bmap)
340 return NULL;
341 if (bmap->n_div == 0)
342 return bmap;
344 for (i = 0; i < bmap->n_div; ++i)
345 bmap = normalize_div_expression(bmap, i);
347 return bmap;
350 /* Eliminate the variable at position "pos" from the constraints of "bmap"
351 * using the equality constraint "eq".
352 * If "keep_divs" is set, then try and preserve
353 * the integer division expressions. In this case, these expressions
354 * are assumed to have been ordered.
355 * If "equivalent" is set, then the elimination is performed
356 * using an equality constraint of "bmap", meaning that the meaning
357 * of the constraints is preserved.
359 static __isl_give isl_basic_map *eliminate_var_using_equality(
360 __isl_take isl_basic_map *bmap,
361 unsigned pos, isl_int *eq, int keep_divs, int equivalent, int *progress)
363 isl_size total;
364 isl_size v_div;
365 int k;
366 int last_div;
367 isl_ctx *ctx;
369 total = isl_basic_map_dim(bmap, isl_dim_all);
370 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
371 if (total < 0 || v_div < 0)
372 return isl_basic_map_free(bmap);
373 ctx = isl_basic_map_get_ctx(bmap);
374 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
375 for (k = 0; k < bmap->n_eq; ++k) {
376 if (bmap->eq[k] == eq)
377 continue;
378 if (isl_int_is_zero(bmap->eq[k][1+pos]))
379 continue;
380 if (progress)
381 *progress = 1;
382 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
383 isl_seq_normalize(ctx, bmap->eq[k], 1 + total);
386 for (k = 0; k < bmap->n_ineq; ++k) {
387 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
388 continue;
389 if (progress)
390 *progress = 1;
391 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
392 isl_seq_gcd(bmap->ineq[k], 1 + total, &ctx->normalize_gcd);
393 bmap = scale_down_inequality(bmap, k, ctx->normalize_gcd,
394 total);
395 bmap = isl_basic_map_modify_inequality(bmap, equivalent);
396 if (!bmap)
397 return NULL;
400 for (k = 0; k < bmap->n_div; ++k) {
401 if (isl_int_is_zero(bmap->div[k][0]))
402 continue;
403 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
404 continue;
405 if (progress)
406 *progress = 1;
407 /* We need to be careful about circular definitions,
408 * so for now we just remove the definition of div k
409 * if the equality contains any divs.
410 * If keep_divs is set, then the divs have been ordered
411 * and we can keep the definition as long as the result
412 * is still ordered.
414 if (last_div == -1 || (keep_divs && last_div < k)) {
415 isl_seq_elim(bmap->div[k]+1, eq,
416 1+pos, 1+total, &bmap->div[k][0]);
417 bmap = normalize_div_expression(bmap, k);
418 if (!bmap)
419 return NULL;
420 } else
421 isl_seq_clr(bmap->div[k], 1 + total);
424 return bmap;
427 /* Eliminate and remove the local variable at position "pos" of "bmap"
428 * using the equality constraint "eq".
429 * If "keep_divs" is set, then try and preserve
430 * the integer division expressions. In this case, these expressions
431 * are assumed to have been ordered.
432 * If "equivalent" is set, then the elimination is performed
433 * using an equality constraint of "bmap", meaning that the meaning
434 * of the constraints is preserved.
436 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
437 isl_int *eq, unsigned div, int keep_divs, int equivalent)
439 isl_size v_div;
440 unsigned pos;
442 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
443 if (v_div < 0)
444 return isl_basic_map_free(bmap);
445 pos = v_div + div;
446 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs,
447 equivalent, NULL);
449 bmap = isl_basic_map_drop_div(bmap, div);
451 return bmap;
454 /* Check if elimination of div "div" using equality "eq" would not
455 * result in a div depending on a later div.
457 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
458 unsigned div)
460 int k;
461 int last_div;
462 isl_size v_div;
463 unsigned pos;
465 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
466 if (v_div < 0)
467 return isl_bool_error;
468 pos = v_div + div;
470 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
471 if (last_div < 0 || last_div <= div)
472 return isl_bool_true;
474 for (k = 0; k <= last_div; ++k) {
475 if (isl_int_is_zero(bmap->div[k][0]))
476 continue;
477 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
478 return isl_bool_false;
481 return isl_bool_true;
484 /* Eliminate divs based on equalities
486 static __isl_give isl_basic_map *eliminate_divs_eq(
487 __isl_take isl_basic_map *bmap, int *progress)
489 int d;
490 int i;
491 int modified = 0;
492 unsigned off;
494 bmap = isl_basic_map_order_divs(bmap);
496 if (!bmap)
497 return NULL;
499 off = isl_basic_map_offset(bmap, isl_dim_div);
501 for (d = bmap->n_div - 1; d >= 0 ; --d) {
502 for (i = 0; i < bmap->n_eq; ++i) {
503 isl_bool ok;
505 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
506 !isl_int_is_negone(bmap->eq[i][off + d]))
507 continue;
508 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
509 if (ok < 0)
510 return isl_basic_map_free(bmap);
511 if (!ok)
512 continue;
513 modified = 1;
514 *progress = 1;
515 bmap = eliminate_div(bmap, bmap->eq[i], d, 1, 1);
516 if (isl_basic_map_drop_equality(bmap, i) < 0)
517 return isl_basic_map_free(bmap);
518 break;
521 if (modified)
522 return eliminate_divs_eq(bmap, progress);
523 return bmap;
526 /* Eliminate divs based on inequalities
528 static __isl_give isl_basic_map *eliminate_divs_ineq(
529 __isl_take isl_basic_map *bmap, int *progress)
531 int d;
532 int i;
533 unsigned off;
534 struct isl_ctx *ctx;
536 if (!bmap)
537 return NULL;
539 ctx = bmap->ctx;
540 off = isl_basic_map_offset(bmap, isl_dim_div);
542 for (d = bmap->n_div - 1; d >= 0 ; --d) {
543 for (i = 0; i < bmap->n_eq; ++i)
544 if (!isl_int_is_zero(bmap->eq[i][off + d]))
545 break;
546 if (i < bmap->n_eq)
547 continue;
548 for (i = 0; i < bmap->n_ineq; ++i)
549 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
550 break;
551 if (i < bmap->n_ineq)
552 continue;
553 *progress = 1;
554 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
555 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
556 break;
557 bmap = isl_basic_map_drop_div(bmap, d);
558 if (!bmap)
559 break;
561 return bmap;
564 /* Does the equality constraint at position "eq" in "bmap" involve
565 * any local variables in the range [first, first + n)
566 * that are not marked as having an explicit representation?
568 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
569 int eq, unsigned first, unsigned n)
571 unsigned o_div;
572 int i;
574 if (!bmap)
575 return isl_bool_error;
577 o_div = isl_basic_map_offset(bmap, isl_dim_div);
578 for (i = 0; i < n; ++i) {
579 isl_bool unknown;
581 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
582 continue;
583 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
584 if (unknown < 0)
585 return isl_bool_error;
586 if (unknown)
587 return isl_bool_true;
590 return isl_bool_false;
593 /* The last local variable involved in the equality constraint
594 * at position "eq" in "bmap" is the local variable at position "div".
595 * It can therefore be used to extract an explicit representation
596 * for that variable.
597 * Do so unless the local variable already has an explicit representation or
598 * the explicit representation would involve any other local variables
599 * that in turn do not have an explicit representation.
600 * An equality constraint involving local variables without an explicit
601 * representation can be used in isl_basic_map_drop_redundant_divs
602 * to separate out an independent local variable. Introducing
603 * an explicit representation here would block this transformation,
604 * while the partial explicit representation in itself is not very useful.
605 * Set *progress if anything is changed.
607 * The equality constraint is of the form
609 * f(x) + n e >= 0
611 * with n a positive number. The explicit representation derived from
612 * this constraint is
614 * floor((-f(x))/n)
616 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
617 int div, int eq, int *progress)
619 isl_size total;
620 unsigned o_div;
621 isl_bool involves;
623 if (!bmap)
624 return NULL;
626 if (!isl_int_is_zero(bmap->div[div][0]))
627 return bmap;
629 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
630 if (involves < 0)
631 return isl_basic_map_free(bmap);
632 if (involves)
633 return bmap;
635 total = isl_basic_map_dim(bmap, isl_dim_all);
636 if (total < 0)
637 return isl_basic_map_free(bmap);
638 o_div = isl_basic_map_offset(bmap, isl_dim_div);
639 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
640 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
641 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
642 if (progress)
643 *progress = 1;
645 return bmap;
648 /* Perform fangcheng (Gaussian elimination) on the equality
649 * constraints of "bmap".
650 * That is, put them into row-echelon form, starting from the last column
651 * backward and use them to eliminate the corresponding coefficients
652 * from all constraints.
654 * If "progress" is not NULL, then it gets set if the elimination
655 * results in any changes.
656 * The elimination process may result in some equality constraints
657 * getting interchanged or removed.
658 * If "swap" or "drop" are not NULL, then they get called when
659 * two equality constraints get interchanged or
660 * when a number of final equality constraints get removed.
661 * As a special case, if the input turns out to be empty,
662 * then drop gets called with the number of removed equality
663 * constraints set to the total number of equality constraints.
664 * If "swap" or "drop" are not NULL, then the local variables (if any)
665 * are assumed to be in a valid order.
667 __isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
668 int *progress,
669 isl_stat (*swap)(unsigned a, unsigned b, void *user),
670 isl_stat (*drop)(unsigned n, void *user), void *user)
672 int k;
673 int done;
674 int last_var;
675 unsigned total_var;
676 isl_size total;
677 unsigned n_drop;
679 if (!swap && !drop)
680 bmap = isl_basic_map_order_divs(bmap);
682 total = isl_basic_map_dim(bmap, isl_dim_all);
683 if (total < 0)
684 return isl_basic_map_free(bmap);
686 total_var = total - bmap->n_div;
688 last_var = total - 1;
689 for (done = 0; done < bmap->n_eq; ++done) {
690 for (; last_var >= 0; --last_var) {
691 for (k = done; k < bmap->n_eq; ++k)
692 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
693 break;
694 if (k < bmap->n_eq)
695 break;
697 if (last_var < 0)
698 break;
699 if (k != done) {
700 swap_equality(bmap, k, done);
701 if (swap && swap(k, done, user) < 0)
702 return isl_basic_map_free(bmap);
704 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
705 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
707 bmap = eliminate_var_using_equality(bmap, last_var,
708 bmap->eq[done], 1, 1, progress);
710 if (last_var >= total_var)
711 bmap = set_div_from_eq(bmap, last_var - total_var,
712 done, progress);
713 if (!bmap)
714 return NULL;
716 if (done == bmap->n_eq)
717 return bmap;
718 for (k = done; k < bmap->n_eq; ++k) {
719 if (isl_int_is_zero(bmap->eq[k][0]))
720 continue;
721 if (drop && drop(bmap->n_eq, user) < 0)
722 return isl_basic_map_free(bmap);
723 return isl_basic_map_set_to_empty(bmap);
725 n_drop = bmap->n_eq - done;
726 bmap = isl_basic_map_free_equality(bmap, n_drop);
727 if (drop && drop(n_drop, user) < 0)
728 return isl_basic_map_free(bmap);
729 return bmap;
732 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
733 int *progress)
735 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
738 __isl_give isl_basic_set *isl_basic_set_gauss(
739 __isl_take isl_basic_set *bset, int *progress)
741 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
742 progress));
746 static unsigned int round_up(unsigned int v)
748 int old_v = v;
750 while (v) {
751 old_v = v;
752 v ^= v & -v;
754 return old_v << 1;
757 /* Hash table of inequalities in a basic map.
758 * "index" is an array of addresses of inequalities in the basic map, some
759 * of which are NULL. The inequalities are hashed on the coefficients
760 * except the constant term.
761 * "size" is the number of elements in the array and is always a power of two
762 * "bits" is the number of bits need to represent an index into the array.
763 * "total" is the total dimension of the basic map.
765 struct isl_constraint_index {
766 unsigned int size;
767 int bits;
768 isl_int ***index;
769 isl_size total;
772 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
774 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
775 __isl_keep isl_basic_map *bmap)
777 isl_ctx *ctx;
779 ci->index = NULL;
780 if (!bmap)
781 return isl_stat_error;
782 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
783 if (ci->total < 0)
784 return isl_stat_error;
785 if (bmap->n_ineq == 0)
786 return isl_stat_ok;
787 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
788 ci->bits = ffs(ci->size) - 1;
789 ctx = isl_basic_map_get_ctx(bmap);
790 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
791 if (!ci->index)
792 return isl_stat_error;
794 return isl_stat_ok;
797 /* Free the memory allocated by create_constraint_index.
799 static void constraint_index_free(struct isl_constraint_index *ci)
801 free(ci->index);
804 /* Return the position in ci->index that contains the address of
805 * an inequality that is equal to *ineq up to the constant term,
806 * provided this address is not identical to "ineq".
807 * If there is no such inequality, then return the position where
808 * such an inequality should be inserted.
810 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
812 int h;
813 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
814 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
815 if (ineq != ci->index[h] &&
816 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
817 break;
818 return h;
821 /* Return the position in ci->index that contains the address of
822 * an inequality that is equal to the k'th inequality of "bmap"
823 * up to the constant term, provided it does not point to the very
824 * same inequality.
825 * If there is no such inequality, then return the position where
826 * such an inequality should be inserted.
828 static int hash_index(struct isl_constraint_index *ci,
829 __isl_keep isl_basic_map *bmap, int k)
831 return hash_index_ineq(ci, &bmap->ineq[k]);
834 static int set_hash_index(struct isl_constraint_index *ci,
835 __isl_keep isl_basic_set *bset, int k)
837 return hash_index(ci, bset, k);
840 /* Fill in the "ci" data structure with the inequalities of "bset".
842 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
843 __isl_keep isl_basic_set *bset)
845 int k, h;
847 if (create_constraint_index(ci, bset) < 0)
848 return isl_stat_error;
850 for (k = 0; k < bset->n_ineq; ++k) {
851 h = set_hash_index(ci, bset, k);
852 ci->index[h] = &bset->ineq[k];
855 return isl_stat_ok;
858 /* Is the inequality ineq (obviously) redundant with respect
859 * to the constraints in "ci"?
861 * Look for an inequality in "ci" with the same coefficients and then
862 * check if the contant term of "ineq" is greater than or equal
863 * to the constant term of that inequality. If so, "ineq" is clearly
864 * redundant.
866 * Note that hash_index_ineq ignores a stored constraint if it has
867 * the same address as the passed inequality. It is ok to pass
868 * the address of a local variable here since it will never be
869 * the same as the address of a constraint in "ci".
871 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
872 isl_int *ineq)
874 int h;
876 h = hash_index_ineq(ci, &ineq);
877 if (!ci->index[h])
878 return isl_bool_false;
879 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
882 /* If we can eliminate more than one div, then we need to make
883 * sure we do it from last div to first div, in order not to
884 * change the position of the other divs that still need to
885 * be removed.
887 static __isl_give isl_basic_map *remove_duplicate_divs(
888 __isl_take isl_basic_map *bmap, int *progress)
890 unsigned int size;
891 int *index;
892 int *elim_for;
893 int k, l, h;
894 int bits;
895 struct isl_blk eq;
896 isl_size v_div;
897 unsigned total;
898 struct isl_ctx *ctx;
900 bmap = isl_basic_map_order_divs(bmap);
901 if (!bmap || bmap->n_div <= 1)
902 return bmap;
904 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
905 if (v_div < 0)
906 return isl_basic_map_free(bmap);
907 total = v_div + bmap->n_div;
909 ctx = bmap->ctx;
910 for (k = bmap->n_div - 1; k >= 0; --k)
911 if (!isl_int_is_zero(bmap->div[k][0]))
912 break;
913 if (k <= 0)
914 return bmap;
916 size = round_up(4 * bmap->n_div / 3 - 1);
917 if (size == 0)
918 return bmap;
919 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
920 bits = ffs(size) - 1;
921 index = isl_calloc_array(ctx, int, size);
922 if (!elim_for || !index)
923 goto out;
924 eq = isl_blk_alloc(ctx, 1+total);
925 if (isl_blk_is_error(eq))
926 goto out;
928 isl_seq_clr(eq.data, 1+total);
929 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
930 for (--k; k >= 0; --k) {
931 uint32_t hash;
933 if (isl_int_is_zero(bmap->div[k][0]))
934 continue;
936 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
937 for (h = hash; index[h]; h = (h+1) % size)
938 if (isl_seq_eq(bmap->div[k],
939 bmap->div[index[h]-1], 2+total))
940 break;
941 if (index[h]) {
942 *progress = 1;
943 l = index[h] - 1;
944 elim_for[l] = k + 1;
946 index[h] = k+1;
948 for (l = bmap->n_div - 1; l >= 0; --l) {
949 if (!elim_for[l])
950 continue;
951 k = elim_for[l] - 1;
952 isl_int_set_si(eq.data[1 + v_div + k], -1);
953 isl_int_set_si(eq.data[1 + v_div + l], 1);
954 bmap = eliminate_div(bmap, eq.data, l, 1, 0);
955 if (!bmap)
956 break;
957 isl_int_set_si(eq.data[1 + v_div + k], 0);
958 isl_int_set_si(eq.data[1 + v_div + l], 0);
961 isl_blk_free(ctx, eq);
962 out:
963 free(index);
964 free(elim_for);
965 return bmap;
968 static int n_pure_div_eq(__isl_keep isl_basic_map *bmap)
970 int i, j;
971 isl_size v_div;
973 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
974 if (v_div < 0)
975 return -1;
976 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
977 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
978 --j;
979 if (j < 0)
980 break;
981 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
982 return 0;
984 return i;
987 /* Normalize divs that appear in equalities.
989 * In particular, we assume that bmap contains some equalities
990 * of the form
992 * a x = m * e_i
994 * and we want to replace the set of e_i by a minimal set and
995 * such that the new e_i have a canonical representation in terms
996 * of the vector x.
997 * If any of the equalities involves more than one divs, then
998 * we currently simply bail out.
1000 * Let us first additionally assume that all equalities involve
1001 * a div. The equalities then express modulo constraints on the
1002 * remaining variables and we can use "parameter compression"
1003 * to find a minimal set of constraints. The result is a transformation
1005 * x = T(x') = x_0 + G x'
1007 * with G a lower-triangular matrix with all elements below the diagonal
1008 * non-negative and smaller than the diagonal element on the same row.
1009 * We first normalize x_0 by making the same property hold in the affine
1010 * T matrix.
1011 * The rows i of G with a 1 on the diagonal do not impose any modulo
1012 * constraint and simply express x_i = x'_i.
1013 * For each of the remaining rows i, we introduce a div and a corresponding
1014 * equality. In particular
1016 * g_ii e_j = x_i - g_i(x')
1018 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1019 * corresponding div (if g_kk != 1).
1021 * If there are any equalities not involving any div, then we
1022 * first apply a variable compression on the variables x:
1024 * x = C x'' x'' = C_2 x
1026 * and perform the above parameter compression on A C instead of on A.
1027 * The resulting compression is then of the form
1029 * x'' = T(x') = x_0 + G x'
1031 * and in constructing the new divs and the corresponding equalities,
1032 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1033 * by the corresponding row from C_2.
1035 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
1036 int *progress)
1038 int i, j, k;
1039 isl_size v_div;
1040 int div_eq;
1041 struct isl_mat *B;
1042 struct isl_vec *d;
1043 struct isl_mat *T = NULL;
1044 struct isl_mat *C = NULL;
1045 struct isl_mat *C2 = NULL;
1046 isl_int v;
1047 int *pos = NULL;
1048 int dropped, needed;
1050 if (!bmap)
1051 return NULL;
1053 if (bmap->n_div == 0)
1054 return bmap;
1056 if (bmap->n_eq == 0)
1057 return bmap;
1059 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1060 return bmap;
1062 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1063 div_eq = n_pure_div_eq(bmap);
1064 if (v_div < 0 || div_eq < 0)
1065 return isl_basic_map_free(bmap);
1066 if (div_eq == 0)
1067 return bmap;
1069 if (div_eq < bmap->n_eq) {
1070 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1071 bmap->n_eq - div_eq, 0, 1 + v_div);
1072 C = isl_mat_variable_compression(B, &C2);
1073 if (!C || !C2)
1074 goto error;
1075 if (C->n_col == 0) {
1076 bmap = isl_basic_map_set_to_empty(bmap);
1077 isl_mat_free(C);
1078 isl_mat_free(C2);
1079 goto done;
1083 d = isl_vec_alloc(bmap->ctx, div_eq);
1084 if (!d)
1085 goto error;
1086 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1087 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1088 --j;
1089 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1091 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1093 if (C) {
1094 B = isl_mat_product(B, C);
1095 C = NULL;
1098 T = isl_mat_parameter_compression(B, d);
1099 if (!T)
1100 goto error;
1101 if (T->n_col == 0) {
1102 bmap = isl_basic_map_set_to_empty(bmap);
1103 isl_mat_free(C2);
1104 isl_mat_free(T);
1105 goto done;
1107 isl_int_init(v);
1108 for (i = 0; i < T->n_row - 1; ++i) {
1109 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1110 if (isl_int_is_zero(v))
1111 continue;
1112 isl_mat_col_submul(T, 0, v, 1 + i);
1114 isl_int_clear(v);
1115 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1116 if (!pos)
1117 goto error;
1118 /* We have to be careful because dropping equalities may reorder them */
1119 dropped = 0;
1120 for (j = bmap->n_div - 1; j >= 0; --j) {
1121 for (i = 0; i < bmap->n_eq; ++i)
1122 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1123 break;
1124 if (i < bmap->n_eq) {
1125 bmap = isl_basic_map_drop_div(bmap, j);
1126 if (isl_basic_map_drop_equality(bmap, i) < 0)
1127 goto error;
1128 ++dropped;
1131 pos[0] = 0;
1132 needed = 0;
1133 for (i = 1; i < T->n_row; ++i) {
1134 if (isl_int_is_one(T->row[i][i]))
1135 pos[i] = i;
1136 else
1137 needed++;
1139 if (needed > dropped) {
1140 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1141 if (!bmap)
1142 goto error;
1144 for (i = 1; i < T->n_row; ++i) {
1145 if (isl_int_is_one(T->row[i][i]))
1146 continue;
1147 k = isl_basic_map_alloc_div(bmap);
1148 pos[i] = 1 + v_div + k;
1149 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1150 isl_int_set(bmap->div[k][0], T->row[i][i]);
1151 if (C2)
1152 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1153 else
1154 isl_int_set_si(bmap->div[k][1 + i], 1);
1155 for (j = 0; j < i; ++j) {
1156 if (isl_int_is_zero(T->row[i][j]))
1157 continue;
1158 if (pos[j] < T->n_row && C2)
1159 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1160 C2->row[pos[j]], 1 + v_div);
1161 else
1162 isl_int_neg(bmap->div[k][1 + pos[j]],
1163 T->row[i][j]);
1165 j = isl_basic_map_alloc_equality(bmap);
1166 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1167 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1169 free(pos);
1170 isl_mat_free(C2);
1171 isl_mat_free(T);
1173 if (progress)
1174 *progress = 1;
1175 done:
1176 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1178 return bmap;
1179 error:
1180 free(pos);
1181 isl_mat_free(C);
1182 isl_mat_free(C2);
1183 isl_mat_free(T);
1184 isl_basic_map_free(bmap);
1185 return NULL;
1188 static __isl_give isl_basic_map *set_div_from_lower_bound(
1189 __isl_take isl_basic_map *bmap, int div, int ineq)
1191 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1193 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1194 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1195 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1196 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1197 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1199 return bmap;
1202 /* Check whether it is ok to define a div based on an inequality.
1203 * To avoid the introduction of circular definitions of divs, we
1204 * do not allow such a definition if the resulting expression would refer to
1205 * any other undefined divs or if any known div is defined in
1206 * terms of the unknown div.
1208 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1209 int div, int ineq)
1211 int j;
1212 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1214 /* Not defined in terms of unknown divs */
1215 for (j = 0; j < bmap->n_div; ++j) {
1216 if (div == j)
1217 continue;
1218 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1219 continue;
1220 if (isl_int_is_zero(bmap->div[j][0]))
1221 return isl_bool_false;
1224 /* No other div defined in terms of this one => avoid loops */
1225 for (j = 0; j < bmap->n_div; ++j) {
1226 if (div == j)
1227 continue;
1228 if (isl_int_is_zero(bmap->div[j][0]))
1229 continue;
1230 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1231 return isl_bool_false;
1234 return isl_bool_true;
1237 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1238 * be a better expression than the current one?
1240 * If we do not have any expression yet, then any expression would be better.
1241 * Otherwise we check if the last variable involved in the inequality
1242 * (disregarding the div that it would define) is in an earlier position
1243 * than the last variable involved in the current div expression.
1245 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1246 int div, int ineq)
1248 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1249 int last_div;
1250 int last_ineq;
1252 if (isl_int_is_zero(bmap->div[div][0]))
1253 return isl_bool_true;
1255 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1256 bmap->n_div - (div + 1)) >= 0)
1257 return isl_bool_false;
1259 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1260 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1261 total + bmap->n_div);
1263 return last_ineq < last_div;
1266 /* Given two constraints "k" and "l" that are opposite to each other,
1267 * except for the constant term, check if we can use them
1268 * to obtain an expression for one of the hitherto unknown divs or
1269 * a "better" expression for a div for which we already have an expression.
1270 * "sum" is the sum of the constant terms of the constraints.
1271 * If this sum is strictly smaller than the coefficient of one
1272 * of the divs, then this pair can be used to define the div.
1273 * To avoid the introduction of circular definitions of divs, we
1274 * do not use the pair if the resulting expression would refer to
1275 * any other undefined divs or if any known div is defined in
1276 * terms of the unknown div.
1278 static __isl_give isl_basic_map *check_for_div_constraints(
1279 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1280 int *progress)
1282 int i;
1283 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1285 for (i = 0; i < bmap->n_div; ++i) {
1286 isl_bool set_div;
1288 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1289 continue;
1290 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1291 continue;
1292 set_div = better_div_constraint(bmap, i, k);
1293 if (set_div >= 0 && set_div)
1294 set_div = ok_to_set_div_from_bound(bmap, i, k);
1295 if (set_div < 0)
1296 return isl_basic_map_free(bmap);
1297 if (!set_div)
1298 break;
1299 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1300 bmap = set_div_from_lower_bound(bmap, i, k);
1301 else
1302 bmap = set_div_from_lower_bound(bmap, i, l);
1303 if (progress)
1304 *progress = 1;
1305 break;
1307 return bmap;
1310 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1311 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1313 struct isl_constraint_index ci;
1314 int k, l, h;
1315 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1316 isl_int sum;
1318 if (total < 0 || bmap->n_ineq <= 1)
1319 return bmap;
1321 if (create_constraint_index(&ci, bmap) < 0)
1322 return bmap;
1324 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1325 ci.index[h] = &bmap->ineq[0];
1326 for (k = 1; k < bmap->n_ineq; ++k) {
1327 h = hash_index(&ci, bmap, k);
1328 if (!ci.index[h]) {
1329 ci.index[h] = &bmap->ineq[k];
1330 continue;
1332 l = ci.index[h] - &bmap->ineq[0];
1333 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1334 swap_inequality(bmap, k, l);
1335 isl_basic_map_drop_inequality(bmap, k);
1336 --k;
1338 isl_int_init(sum);
1339 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1340 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1341 h = hash_index(&ci, bmap, k);
1342 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1343 if (!ci.index[h])
1344 continue;
1345 l = ci.index[h] - &bmap->ineq[0];
1346 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1347 if (isl_int_is_pos(sum)) {
1348 if (detect_divs)
1349 bmap = check_for_div_constraints(bmap, k, l,
1350 sum, progress);
1351 continue;
1353 if (isl_int_is_zero(sum)) {
1354 /* We need to break out of the loop after these
1355 * changes since the contents of the hash
1356 * will no longer be valid.
1357 * Plus, we probably we want to regauss first.
1359 if (progress)
1360 *progress = 1;
1361 isl_basic_map_drop_inequality(bmap, l);
1362 isl_basic_map_inequality_to_equality(bmap, k);
1363 } else
1364 bmap = isl_basic_map_set_to_empty(bmap);
1365 break;
1367 isl_int_clear(sum);
1369 constraint_index_free(&ci);
1370 return bmap;
1373 /* Detect all pairs of inequalities that form an equality.
1375 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1376 * Call it repeatedly while it is making progress.
1378 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1379 __isl_take isl_basic_map *bmap, int *progress)
1381 int duplicate;
1383 do {
1384 duplicate = 0;
1385 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1386 &duplicate, 0);
1387 if (progress && duplicate)
1388 *progress = 1;
1389 } while (duplicate);
1391 return bmap;
1394 /* Given a known integer division "div" that is not integral
1395 * (with denominator 1), eliminate it from the constraints in "bmap"
1396 * where it appears with a (positive or negative) unit coefficient.
1397 * If "progress" is not NULL, then it gets set if the elimination
1398 * results in any changes.
1400 * That is, replace
1402 * floor(e/m) + f >= 0
1404 * by
1406 * e + m f >= 0
1408 * and
1410 * -floor(e/m) + f >= 0
1412 * by
1414 * -e + m f + m - 1 >= 0
1416 * The first conversion is valid because floor(e/m) >= -f is equivalent
1417 * to e/m >= -f because -f is an integral expression.
1418 * The second conversion follows from the fact that
1420 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1423 * Note that one of the div constraints may have been eliminated
1424 * due to being redundant with respect to the constraint that is
1425 * being modified by this function. The modified constraint may
1426 * no longer imply this div constraint, so we add it back to make
1427 * sure we do not lose any information.
1429 static __isl_give isl_basic_map *eliminate_unit_div(
1430 __isl_take isl_basic_map *bmap, int div, int *progress)
1432 int j;
1433 isl_size v_div, dim;
1434 isl_ctx *ctx;
1436 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1437 dim = isl_basic_map_dim(bmap, isl_dim_all);
1438 if (v_div < 0 || dim < 0)
1439 return isl_basic_map_free(bmap);
1441 ctx = isl_basic_map_get_ctx(bmap);
1443 for (j = 0; j < bmap->n_ineq; ++j) {
1444 int s;
1446 if (!isl_int_is_one(bmap->ineq[j][1 + v_div + div]) &&
1447 !isl_int_is_negone(bmap->ineq[j][1 + v_div + div]))
1448 continue;
1450 if (progress)
1451 *progress = 1;
1453 s = isl_int_sgn(bmap->ineq[j][1 + v_div + div]);
1454 isl_int_set_si(bmap->ineq[j][1 + v_div + div], 0);
1455 if (s < 0)
1456 isl_seq_combine(bmap->ineq[j],
1457 ctx->negone, bmap->div[div] + 1,
1458 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1459 else
1460 isl_seq_combine(bmap->ineq[j],
1461 ctx->one, bmap->div[div] + 1,
1462 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1463 if (s < 0) {
1464 isl_int_add(bmap->ineq[j][0],
1465 bmap->ineq[j][0], bmap->div[div][0]);
1466 isl_int_sub_ui(bmap->ineq[j][0],
1467 bmap->ineq[j][0], 1);
1470 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1471 bmap = isl_basic_map_add_div_constraint(bmap, div, s);
1472 if (!bmap)
1473 return NULL;
1476 return bmap;
1479 /* Eliminate selected known divs from constraints where they appear with
1480 * a (positive or negative) unit coefficient.
1481 * In particular, only handle those for which "select" returns isl_bool_true.
1482 * If "progress" is not NULL, then it gets set if the elimination
1483 * results in any changes.
1485 * We skip integral divs, i.e., those with denominator 1, as we would
1486 * risk eliminating the div from the div constraints. We do not need
1487 * to handle those divs here anyway since the div constraints will turn
1488 * out to form an equality and this equality can then be used to eliminate
1489 * the div from all constraints.
1491 static __isl_give isl_basic_map *eliminate_selected_unit_divs(
1492 __isl_take isl_basic_map *bmap,
1493 isl_bool (*select)(__isl_keep isl_basic_map *bmap, int div),
1494 int *progress)
1496 int i;
1497 isl_size n_div;
1499 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1500 if (n_div < 0)
1501 return isl_basic_map_free(bmap);
1503 for (i = 0; i < n_div; ++i) {
1504 isl_bool skip;
1505 isl_bool selected;
1507 skip = isl_basic_map_div_is_marked_unknown(bmap, i);
1508 if (skip >= 0 && !skip)
1509 skip = isl_basic_map_div_is_integral(bmap, i);
1510 if (skip < 0)
1511 return isl_basic_map_free(bmap);
1512 if (skip)
1513 continue;
1514 selected = select(bmap, i);
1515 if (selected < 0)
1516 return isl_basic_map_free(bmap);
1517 if (!selected)
1518 continue;
1519 bmap = eliminate_unit_div(bmap, i, progress);
1520 if (!bmap)
1521 return NULL;
1524 return bmap;
1527 /* eliminate_selected_unit_divs callback that selects every
1528 * integer division.
1530 static isl_bool is_any_div(__isl_keep isl_basic_map *bmap, int div)
1532 return isl_bool_true;
1535 /* Eliminate known divs from constraints where they appear with
1536 * a (positive or negative) unit coefficient.
1537 * If "progress" is not NULL, then it gets set if the elimination
1538 * results in any changes.
1540 static __isl_give isl_basic_map *eliminate_unit_divs(
1541 __isl_take isl_basic_map *bmap, int *progress)
1543 return eliminate_selected_unit_divs(bmap, &is_any_div, progress);
1546 /* eliminate_selected_unit_divs callback that selects
1547 * integer divisions that only appear with
1548 * a (positive or negative) unit coefficient
1549 * (outside their div constraints).
1551 static isl_bool is_pure_unit_div(__isl_keep isl_basic_map *bmap, int div)
1553 int i;
1554 isl_size v_div, n_ineq;
1556 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1557 n_ineq = isl_basic_map_n_inequality(bmap);
1558 if (v_div < 0 || n_ineq < 0)
1559 return isl_bool_error;
1561 for (i = 0; i < n_ineq; ++i) {
1562 isl_bool skip;
1564 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
1565 continue;
1566 skip = isl_basic_map_is_div_constraint(bmap,
1567 bmap->ineq[i], div);
1568 if (skip < 0)
1569 return isl_bool_error;
1570 if (skip)
1571 continue;
1572 if (!isl_int_is_one(bmap->ineq[i][1 + v_div + div]) &&
1573 !isl_int_is_negone(bmap->ineq[i][1 + v_div + div]))
1574 return isl_bool_false;
1577 return isl_bool_true;
1580 /* Eliminate known divs from constraints where they appear with
1581 * a (positive or negative) unit coefficient,
1582 * but only if they do not appear in any other constraints
1583 * (other than the div constraints).
1585 __isl_give isl_basic_map *isl_basic_map_eliminate_pure_unit_divs(
1586 __isl_take isl_basic_map *bmap)
1588 return eliminate_selected_unit_divs(bmap, &is_pure_unit_div, NULL);
1591 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1593 int progress = 1;
1594 if (!bmap)
1595 return NULL;
1596 while (progress) {
1597 isl_bool empty;
1599 progress = 0;
1600 empty = isl_basic_map_plain_is_empty(bmap);
1601 if (empty < 0)
1602 return isl_basic_map_free(bmap);
1603 if (empty)
1604 break;
1605 bmap = isl_basic_map_normalize_constraints(bmap);
1606 bmap = reduce_div_coefficients(bmap);
1607 bmap = normalize_div_expressions(bmap);
1608 bmap = remove_duplicate_divs(bmap, &progress);
1609 bmap = eliminate_unit_divs(bmap, &progress);
1610 bmap = eliminate_divs_eq(bmap, &progress);
1611 bmap = eliminate_divs_ineq(bmap, &progress);
1612 bmap = isl_basic_map_gauss(bmap, &progress);
1613 /* requires equalities in normal form */
1614 bmap = normalize_divs(bmap, &progress);
1615 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1616 &progress, 1);
1618 return bmap;
1621 __isl_give isl_basic_set *isl_basic_set_simplify(
1622 __isl_take isl_basic_set *bset)
1624 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1628 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1629 isl_int *constraint, unsigned div)
1631 unsigned pos;
1633 if (!bmap)
1634 return isl_bool_error;
1636 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1638 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1639 int neg;
1640 isl_int_sub(bmap->div[div][1],
1641 bmap->div[div][1], bmap->div[div][0]);
1642 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1643 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1644 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1645 isl_int_add(bmap->div[div][1],
1646 bmap->div[div][1], bmap->div[div][0]);
1647 if (!neg)
1648 return isl_bool_false;
1649 if (isl_seq_first_non_zero(constraint+pos+1,
1650 bmap->n_div-div-1) != -1)
1651 return isl_bool_false;
1652 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1653 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1654 return isl_bool_false;
1655 if (isl_seq_first_non_zero(constraint+pos+1,
1656 bmap->n_div-div-1) != -1)
1657 return isl_bool_false;
1658 } else
1659 return isl_bool_false;
1661 return isl_bool_true;
1664 /* If the only constraints a div d=floor(f/m)
1665 * appears in are its two defining constraints
1667 * f - m d >=0
1668 * -(f - (m - 1)) + m d >= 0
1670 * then it can safely be removed.
1672 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1674 int i;
1675 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1676 unsigned pos = 1 + v_div + div;
1678 if (v_div < 0)
1679 return isl_bool_error;
1681 for (i = 0; i < bmap->n_eq; ++i)
1682 if (!isl_int_is_zero(bmap->eq[i][pos]))
1683 return isl_bool_false;
1685 for (i = 0; i < bmap->n_ineq; ++i) {
1686 isl_bool red;
1688 if (isl_int_is_zero(bmap->ineq[i][pos]))
1689 continue;
1690 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1691 if (red < 0 || !red)
1692 return red;
1695 for (i = 0; i < bmap->n_div; ++i) {
1696 if (isl_int_is_zero(bmap->div[i][0]))
1697 continue;
1698 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1699 return isl_bool_false;
1702 return isl_bool_true;
1706 * Remove divs that don't occur in any of the constraints or other divs.
1707 * These can arise when dropping constraints from a basic map or
1708 * when the divs of a basic map have been temporarily aligned
1709 * with the divs of another basic map.
1711 static __isl_give isl_basic_map *remove_redundant_divs(
1712 __isl_take isl_basic_map *bmap)
1714 int i;
1715 isl_size v_div;
1717 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1718 if (v_div < 0)
1719 return isl_basic_map_free(bmap);
1721 for (i = bmap->n_div-1; i >= 0; --i) {
1722 isl_bool redundant;
1724 redundant = div_is_redundant(bmap, i);
1725 if (redundant < 0)
1726 return isl_basic_map_free(bmap);
1727 if (!redundant)
1728 continue;
1729 bmap = isl_basic_map_drop_constraints_involving(bmap,
1730 v_div + i, 1);
1731 bmap = isl_basic_map_drop_div(bmap, i);
1733 return bmap;
1736 /* Mark "bmap" as final, without checking for obviously redundant
1737 * integer divisions. This function should be used when "bmap"
1738 * is known not to involve any such integer divisions.
1740 __isl_give isl_basic_map *isl_basic_map_mark_final(
1741 __isl_take isl_basic_map *bmap)
1743 if (!bmap)
1744 return NULL;
1745 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1746 return bmap;
1749 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1751 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1753 bmap = remove_redundant_divs(bmap);
1754 bmap = isl_basic_map_mark_final(bmap);
1755 return bmap;
1758 __isl_give isl_basic_set *isl_basic_set_finalize(
1759 __isl_take isl_basic_set *bset)
1761 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1764 /* Remove definition of any div that is defined in terms of the given variable.
1765 * The div itself is not removed. Functions such as
1766 * eliminate_divs_ineq depend on the other divs remaining in place.
1768 static __isl_give isl_basic_map *remove_dependent_vars(
1769 __isl_take isl_basic_map *bmap, int pos)
1771 int i;
1773 if (!bmap)
1774 return NULL;
1776 for (i = 0; i < bmap->n_div; ++i) {
1777 if (isl_int_is_zero(bmap->div[i][0]))
1778 continue;
1779 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1780 continue;
1781 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1782 if (!bmap)
1783 return NULL;
1785 return bmap;
1788 /* Eliminate the specified variables from the constraints using
1789 * Fourier-Motzkin. The variables themselves are not removed.
1791 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1792 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1794 int d;
1795 int i, j, k;
1796 isl_size total;
1797 int need_gauss = 0;
1799 if (n == 0)
1800 return bmap;
1801 total = isl_basic_map_dim(bmap, isl_dim_all);
1802 if (total < 0)
1803 return isl_basic_map_free(bmap);
1805 bmap = isl_basic_map_cow(bmap);
1806 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1807 bmap = remove_dependent_vars(bmap, d);
1808 if (!bmap)
1809 return NULL;
1811 for (d = pos + n - 1;
1812 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1813 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1814 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1815 int n_lower, n_upper;
1816 if (!bmap)
1817 return NULL;
1818 for (i = 0; i < bmap->n_eq; ++i) {
1819 if (isl_int_is_zero(bmap->eq[i][1+d]))
1820 continue;
1821 bmap = eliminate_var_using_equality(bmap, d,
1822 bmap->eq[i], 0, 1, NULL);
1823 if (isl_basic_map_drop_equality(bmap, i) < 0)
1824 return isl_basic_map_free(bmap);
1825 need_gauss = 1;
1826 break;
1828 if (i < bmap->n_eq)
1829 continue;
1830 n_lower = 0;
1831 n_upper = 0;
1832 for (i = 0; i < bmap->n_ineq; ++i) {
1833 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1834 n_lower++;
1835 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1836 n_upper++;
1838 bmap = isl_basic_map_extend_constraints(bmap,
1839 0, n_lower * n_upper);
1840 if (!bmap)
1841 goto error;
1842 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1843 int last;
1844 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1845 continue;
1846 last = -1;
1847 for (j = 0; j < i; ++j) {
1848 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1849 continue;
1850 last = j;
1851 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1852 isl_int_sgn(bmap->ineq[j][1+d]))
1853 continue;
1854 k = isl_basic_map_alloc_inequality(bmap);
1855 if (k < 0)
1856 goto error;
1857 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1858 1+total);
1859 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1860 1+d, 1+total, NULL);
1862 isl_basic_map_drop_inequality(bmap, i);
1863 i = last + 1;
1865 if (n_lower > 0 && n_upper > 0) {
1866 bmap = isl_basic_map_normalize_constraints(bmap);
1867 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1868 NULL, 0);
1869 bmap = isl_basic_map_gauss(bmap, NULL);
1870 bmap = isl_basic_map_remove_redundancies(bmap);
1871 need_gauss = 0;
1872 if (!bmap)
1873 goto error;
1874 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1875 break;
1878 if (need_gauss)
1879 bmap = isl_basic_map_gauss(bmap, NULL);
1880 return bmap;
1881 error:
1882 isl_basic_map_free(bmap);
1883 return NULL;
1886 __isl_give isl_basic_set *isl_basic_set_eliminate_vars(
1887 __isl_take isl_basic_set *bset, unsigned pos, unsigned n)
1889 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1890 pos, n));
1893 /* Eliminate the specified n dimensions starting at first from the
1894 * constraints, without removing the dimensions from the space.
1895 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1896 * Otherwise, they are projected out and the original space is restored.
1898 __isl_give isl_basic_map *isl_basic_map_eliminate(
1899 __isl_take isl_basic_map *bmap,
1900 enum isl_dim_type type, unsigned first, unsigned n)
1902 isl_space *space;
1904 if (!bmap)
1905 return NULL;
1906 if (n == 0)
1907 return bmap;
1909 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1910 return isl_basic_map_free(bmap);
1912 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1913 first += isl_basic_map_offset(bmap, type) - 1;
1914 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1915 return isl_basic_map_finalize(bmap);
1918 space = isl_basic_map_get_space(bmap);
1919 bmap = isl_basic_map_project_out(bmap, type, first, n);
1920 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1921 bmap = isl_basic_map_reset_space(bmap, space);
1922 return bmap;
1925 __isl_give isl_basic_set *isl_basic_set_eliminate(
1926 __isl_take isl_basic_set *bset,
1927 enum isl_dim_type type, unsigned first, unsigned n)
1929 return isl_basic_map_eliminate(bset, type, first, n);
1932 /* Remove all constraints from "bmap" that reference any unknown local
1933 * variables (directly or indirectly).
1935 * Dropping all constraints on a local variable will make it redundant,
1936 * so it will get removed implicitly by
1937 * isl_basic_map_drop_constraints_involving_dims. Some other local
1938 * variables may also end up becoming redundant if they only appear
1939 * in constraints together with the unknown local variable.
1940 * Therefore, start over after calling
1941 * isl_basic_map_drop_constraints_involving_dims.
1943 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_unknown_divs(
1944 __isl_take isl_basic_map *bmap)
1946 isl_bool known;
1947 isl_size n_div;
1948 int i, o_div;
1950 known = isl_basic_map_divs_known(bmap);
1951 if (known < 0)
1952 return isl_basic_map_free(bmap);
1953 if (known)
1954 return bmap;
1956 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1957 if (n_div < 0)
1958 return isl_basic_map_free(bmap);
1959 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1961 for (i = 0; i < n_div; ++i) {
1962 known = isl_basic_map_div_is_known(bmap, i);
1963 if (known < 0)
1964 return isl_basic_map_free(bmap);
1965 if (known)
1966 continue;
1967 bmap = remove_dependent_vars(bmap, o_div + i);
1968 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1969 isl_dim_div, i, 1);
1970 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1971 if (n_div < 0)
1972 return isl_basic_map_free(bmap);
1973 i = -1;
1976 return bmap;
1979 /* Remove all constraints from "bset" that reference any unknown local
1980 * variables (directly or indirectly).
1982 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_unknown_divs(
1983 __isl_take isl_basic_set *bset)
1985 isl_basic_map *bmap;
1987 bmap = bset_to_bmap(bset);
1988 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
1989 return bset_from_bmap(bmap);
1992 /* Remove all constraints from "map" that reference any unknown local
1993 * variables (directly or indirectly).
1995 * Since constraints may get dropped from the basic maps,
1996 * they may no longer be disjoint from each other.
1998 __isl_give isl_map *isl_map_drop_constraints_involving_unknown_divs(
1999 __isl_take isl_map *map)
2001 int i;
2002 isl_bool known;
2004 known = isl_map_divs_known(map);
2005 if (known < 0)
2006 return isl_map_free(map);
2007 if (known)
2008 return map;
2010 map = isl_map_cow(map);
2011 if (!map)
2012 return NULL;
2014 for (i = 0; i < map->n; ++i) {
2015 map->p[i] =
2016 isl_basic_map_drop_constraints_involving_unknown_divs(
2017 map->p[i]);
2018 if (!map->p[i])
2019 return isl_map_free(map);
2022 if (map->n > 1)
2023 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2025 return map;
2028 /* Don't assume equalities are in order, because align_divs
2029 * may have changed the order of the divs.
2031 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim,
2032 unsigned len)
2034 int d, i;
2036 for (d = 0; d < len; ++d)
2037 elim[d] = -1;
2038 for (i = 0; i < bmap->n_eq; ++i) {
2039 for (d = len - 1; d >= 0; --d) {
2040 if (isl_int_is_zero(bmap->eq[i][1+d]))
2041 continue;
2042 elim[d] = i;
2043 break;
2048 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
2049 int *elim, unsigned len)
2051 compute_elimination_index(bset_to_bmap(bset), elim, len);
2054 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2055 __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
2057 int d;
2058 int copied = 0;
2060 for (d = total - 1; d >= 0; --d) {
2061 if (isl_int_is_zero(src[1+d]))
2062 continue;
2063 if (elim[d] == -1)
2064 continue;
2065 if (!copied) {
2066 isl_seq_cpy(dst, src, 1 + total);
2067 copied = 1;
2069 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2071 return copied;
2074 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2075 __isl_keep isl_basic_set *bset, int *elim, unsigned total)
2077 return reduced_using_equalities(dst, src,
2078 bset_to_bmap(bset), elim, total);
2081 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
2082 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2084 int i;
2085 int *elim;
2086 isl_size dim;
2088 if (!bset || !context)
2089 goto error;
2091 if (context->n_eq == 0) {
2092 isl_basic_set_free(context);
2093 return bset;
2096 bset = isl_basic_set_cow(bset);
2097 dim = isl_basic_set_dim(bset, isl_dim_set);
2098 if (dim < 0)
2099 goto error;
2101 elim = isl_alloc_array(bset->ctx, int, dim);
2102 if (!elim)
2103 goto error;
2104 set_compute_elimination_index(context, elim, dim);
2105 for (i = 0; i < bset->n_eq; ++i)
2106 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2107 context, elim, dim);
2108 for (i = 0; i < bset->n_ineq; ++i)
2109 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2110 context, elim, dim);
2111 isl_basic_set_free(context);
2112 free(elim);
2113 bset = isl_basic_set_simplify(bset);
2114 bset = isl_basic_set_finalize(bset);
2115 return bset;
2116 error:
2117 isl_basic_set_free(bset);
2118 isl_basic_set_free(context);
2119 return NULL;
2122 /* For each inequality in "ineq" that is a shifted (more relaxed)
2123 * copy of an inequality in "context", mark the corresponding entry
2124 * in "row" with -1.
2125 * If an inequality only has a non-negative constant term, then
2126 * mark it as well.
2128 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2129 __isl_keep isl_basic_set *context, int *row)
2131 struct isl_constraint_index ci;
2132 isl_size n_ineq, cols;
2133 unsigned total;
2134 int k;
2136 if (!ineq || !context)
2137 return isl_stat_error;
2138 if (context->n_ineq == 0)
2139 return isl_stat_ok;
2140 if (setup_constraint_index(&ci, context) < 0)
2141 return isl_stat_error;
2143 n_ineq = isl_mat_rows(ineq);
2144 cols = isl_mat_cols(ineq);
2145 if (n_ineq < 0 || cols < 0)
2146 return isl_stat_error;
2147 total = cols - 1;
2148 for (k = 0; k < n_ineq; ++k) {
2149 int l;
2150 isl_bool redundant;
2152 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2153 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2154 row[k] = -1;
2155 continue;
2157 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2158 if (redundant < 0)
2159 goto error;
2160 if (!redundant)
2161 continue;
2162 row[k] = -1;
2164 constraint_index_free(&ci);
2165 return isl_stat_ok;
2166 error:
2167 constraint_index_free(&ci);
2168 return isl_stat_error;
2171 static __isl_give isl_basic_set *remove_shifted_constraints(
2172 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
2174 struct isl_constraint_index ci;
2175 int k;
2177 if (!bset || !context)
2178 return bset;
2180 if (context->n_ineq == 0)
2181 return bset;
2182 if (setup_constraint_index(&ci, context) < 0)
2183 return bset;
2185 for (k = 0; k < bset->n_ineq; ++k) {
2186 isl_bool redundant;
2188 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2189 if (redundant < 0)
2190 goto error;
2191 if (!redundant)
2192 continue;
2193 bset = isl_basic_set_cow(bset);
2194 if (!bset)
2195 goto error;
2196 isl_basic_set_drop_inequality(bset, k);
2197 --k;
2199 constraint_index_free(&ci);
2200 return bset;
2201 error:
2202 constraint_index_free(&ci);
2203 return bset;
2206 /* Remove constraints from "bmap" that are identical to constraints
2207 * in "context" or that are more relaxed (greater constant term).
2209 * We perform the test for shifted copies on the pure constraints
2210 * in remove_shifted_constraints.
2212 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2213 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2215 isl_basic_set *bset, *bset_context;
2217 if (!bmap || !context)
2218 goto error;
2220 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2221 isl_basic_map_free(context);
2222 return bmap;
2225 bmap = isl_basic_map_order_divs(bmap);
2226 context = isl_basic_map_align_divs(context, bmap);
2227 bmap = isl_basic_map_align_divs(bmap, context);
2229 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2230 bset_context = isl_basic_map_underlying_set(context);
2231 bset = remove_shifted_constraints(bset, bset_context);
2232 isl_basic_set_free(bset_context);
2234 bmap = isl_basic_map_overlying_set(bset, bmap);
2236 return bmap;
2237 error:
2238 isl_basic_map_free(bmap);
2239 isl_basic_map_free(context);
2240 return NULL;
2243 /* Does the (linear part of a) constraint "c" involve any of the "len"
2244 * "relevant" dimensions?
2246 static int is_related(isl_int *c, int len, int *relevant)
2248 int i;
2250 for (i = 0; i < len; ++i) {
2251 if (!relevant[i])
2252 continue;
2253 if (!isl_int_is_zero(c[i]))
2254 return 1;
2257 return 0;
2260 /* Drop constraints from "bmap" that do not involve any of
2261 * the dimensions marked "relevant".
2263 static __isl_give isl_basic_map *drop_unrelated_constraints(
2264 __isl_take isl_basic_map *bmap, int *relevant)
2266 int i;
2267 isl_size dim;
2269 dim = isl_basic_map_dim(bmap, isl_dim_all);
2270 if (dim < 0)
2271 return isl_basic_map_free(bmap);
2272 for (i = 0; i < dim; ++i)
2273 if (!relevant[i])
2274 break;
2275 if (i >= dim)
2276 return bmap;
2278 for (i = bmap->n_eq - 1; i >= 0; --i)
2279 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2280 bmap = isl_basic_map_cow(bmap);
2281 if (isl_basic_map_drop_equality(bmap, i) < 0)
2282 return isl_basic_map_free(bmap);
2285 for (i = bmap->n_ineq - 1; i >= 0; --i)
2286 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2287 bmap = isl_basic_map_cow(bmap);
2288 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2289 return isl_basic_map_free(bmap);
2292 return bmap;
2295 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2297 * In particular, for any variable involved in the constraint,
2298 * find the actual group id from before and replace the group
2299 * of the corresponding variable by the minimal group of all
2300 * the variables involved in the constraint considered so far
2301 * (if this minimum is smaller) or replace the minimum by this group
2302 * (if the minimum is larger).
2304 * At the end, all the variables in "c" will (indirectly) point
2305 * to the minimal of the groups that they referred to originally.
2307 static void update_groups(int dim, int *group, isl_int *c)
2309 int j;
2310 int min = dim;
2312 for (j = 0; j < dim; ++j) {
2313 if (isl_int_is_zero(c[j]))
2314 continue;
2315 while (group[j] >= 0 && group[group[j]] != group[j])
2316 group[j] = group[group[j]];
2317 if (group[j] == min)
2318 continue;
2319 if (group[j] < min) {
2320 if (min >= 0 && min < dim)
2321 group[min] = group[j];
2322 min = group[j];
2323 } else
2324 group[group[j]] = min;
2328 /* Allocate an array of groups of variables, one for each variable
2329 * in "context", initialized to zero.
2331 static int *alloc_groups(__isl_keep isl_basic_set *context)
2333 isl_ctx *ctx;
2334 isl_size dim;
2336 dim = isl_basic_set_dim(context, isl_dim_set);
2337 if (dim < 0)
2338 return NULL;
2339 ctx = isl_basic_set_get_ctx(context);
2340 return isl_calloc_array(ctx, int, dim);
2343 /* Drop constraints from "bmap" that only involve variables that are
2344 * not related to any of the variables marked with a "-1" in "group".
2346 * We construct groups of variables that collect variables that
2347 * (indirectly) appear in some common constraint of "bmap".
2348 * Each group is identified by the first variable in the group,
2349 * except for the special group of variables that was already identified
2350 * in the input as -1 (or are related to those variables).
2351 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2352 * otherwise the group of i is the group of group[i].
2354 * We first initialize groups for the remaining variables.
2355 * Then we iterate over the constraints of "bmap" and update the
2356 * group of the variables in the constraint by the smallest group.
2357 * Finally, we resolve indirect references to groups by running over
2358 * the variables.
2360 * After computing the groups, we drop constraints that do not involve
2361 * any variables in the -1 group.
2363 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2364 __isl_take isl_basic_map *bmap, __isl_take int *group)
2366 isl_size dim;
2367 int i;
2368 int last;
2370 dim = isl_basic_map_dim(bmap, isl_dim_all);
2371 if (dim < 0)
2372 return isl_basic_map_free(bmap);
2374 last = -1;
2375 for (i = 0; i < dim; ++i)
2376 if (group[i] >= 0)
2377 last = group[i] = i;
2378 if (last < 0) {
2379 free(group);
2380 return bmap;
2383 for (i = 0; i < bmap->n_eq; ++i)
2384 update_groups(dim, group, bmap->eq[i] + 1);
2385 for (i = 0; i < bmap->n_ineq; ++i)
2386 update_groups(dim, group, bmap->ineq[i] + 1);
2388 for (i = 0; i < dim; ++i)
2389 if (group[i] >= 0)
2390 group[i] = group[group[i]];
2392 for (i = 0; i < dim; ++i)
2393 group[i] = group[i] == -1;
2395 bmap = drop_unrelated_constraints(bmap, group);
2397 free(group);
2398 return bmap;
2401 /* Drop constraints from "context" that are irrelevant for computing
2402 * the gist of "bset".
2404 * In particular, drop constraints in variables that are not related
2405 * to any of the variables involved in the constraints of "bset"
2406 * in the sense that there is no sequence of constraints that connects them.
2408 * We first mark all variables that appear in "bset" as belonging
2409 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2411 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2412 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2414 int *group;
2415 isl_size dim;
2416 int i, j;
2418 dim = isl_basic_set_dim(bset, isl_dim_set);
2419 if (!context || dim < 0)
2420 return isl_basic_set_free(context);
2422 group = alloc_groups(context);
2424 if (!group)
2425 return isl_basic_set_free(context);
2427 for (i = 0; i < dim; ++i) {
2428 for (j = 0; j < bset->n_eq; ++j)
2429 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2430 break;
2431 if (j < bset->n_eq) {
2432 group[i] = -1;
2433 continue;
2435 for (j = 0; j < bset->n_ineq; ++j)
2436 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2437 break;
2438 if (j < bset->n_ineq)
2439 group[i] = -1;
2442 return isl_basic_map_drop_unrelated_constraints(context, group);
2445 /* Drop constraints from "context" that are irrelevant for computing
2446 * the gist of the inequalities "ineq".
2447 * Inequalities in "ineq" for which the corresponding element of row
2448 * is set to -1 have already been marked for removal and should be ignored.
2450 * In particular, drop constraints in variables that are not related
2451 * to any of the variables involved in "ineq"
2452 * in the sense that there is no sequence of constraints that connects them.
2454 * We first mark all variables that appear in "bset" as belonging
2455 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2457 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2458 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2460 int *group;
2461 isl_size dim;
2462 int i, j;
2463 isl_size n;
2465 dim = isl_basic_set_dim(context, isl_dim_set);
2466 n = isl_mat_rows(ineq);
2467 if (dim < 0 || n < 0)
2468 return isl_basic_set_free(context);
2470 group = alloc_groups(context);
2472 if (!group)
2473 return isl_basic_set_free(context);
2475 for (i = 0; i < dim; ++i) {
2476 for (j = 0; j < n; ++j) {
2477 if (row[j] < 0)
2478 continue;
2479 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2480 break;
2482 if (j < n)
2483 group[i] = -1;
2486 return isl_basic_map_drop_unrelated_constraints(context, group);
2489 /* Do all "n" entries of "row" contain a negative value?
2491 static int all_neg(int *row, int n)
2493 int i;
2495 for (i = 0; i < n; ++i)
2496 if (row[i] >= 0)
2497 return 0;
2499 return 1;
2502 /* Update the inequalities in "bset" based on the information in "row"
2503 * and "tab".
2505 * In particular, the array "row" contains either -1, meaning that
2506 * the corresponding inequality of "bset" is redundant, or the index
2507 * of an inequality in "tab".
2509 * If the row entry is -1, then drop the inequality.
2510 * Otherwise, if the constraint is marked redundant in the tableau,
2511 * then drop the inequality. Similarly, if it is marked as an equality
2512 * in the tableau, then turn the inequality into an equality and
2513 * perform Gaussian elimination.
2515 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2516 __isl_keep int *row, struct isl_tab *tab)
2518 int i;
2519 unsigned n_ineq;
2520 unsigned n_eq;
2521 int found_equality = 0;
2523 if (!bset)
2524 return NULL;
2525 if (tab && tab->empty)
2526 return isl_basic_set_set_to_empty(bset);
2528 n_ineq = bset->n_ineq;
2529 for (i = n_ineq - 1; i >= 0; --i) {
2530 if (row[i] < 0) {
2531 if (isl_basic_set_drop_inequality(bset, i) < 0)
2532 return isl_basic_set_free(bset);
2533 continue;
2535 if (!tab)
2536 continue;
2537 n_eq = tab->n_eq;
2538 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2539 isl_basic_map_inequality_to_equality(bset, i);
2540 found_equality = 1;
2541 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2542 if (isl_basic_set_drop_inequality(bset, i) < 0)
2543 return isl_basic_set_free(bset);
2547 if (found_equality)
2548 bset = isl_basic_set_gauss(bset, NULL);
2549 bset = isl_basic_set_finalize(bset);
2550 return bset;
2553 /* Update the inequalities in "bset" based on the information in "row"
2554 * and "tab" and free all arguments (other than "bset").
2556 static __isl_give isl_basic_set *update_ineq_free(
2557 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2558 __isl_take isl_basic_set *context, __isl_take int *row,
2559 struct isl_tab *tab)
2561 isl_mat_free(ineq);
2562 isl_basic_set_free(context);
2564 bset = update_ineq(bset, row, tab);
2566 free(row);
2567 isl_tab_free(tab);
2568 return bset;
2571 /* Remove all information from bset that is redundant in the context
2572 * of context.
2573 * "ineq" contains the (possibly transformed) inequalities of "bset",
2574 * in the same order.
2575 * The (explicit) equalities of "bset" are assumed to have been taken
2576 * into account by the transformation such that only the inequalities
2577 * are relevant.
2578 * "context" is assumed not to be empty.
2580 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2581 * A value of -1 means that the inequality is obviously redundant and may
2582 * not even appear in "tab".
2584 * We first mark the inequalities of "bset"
2585 * that are obviously redundant with respect to some inequality in "context".
2586 * Then we remove those constraints from "context" that have become
2587 * irrelevant for computing the gist of "bset".
2588 * Note that this removal of constraints cannot be replaced by
2589 * a factorization because factors in "bset" may still be connected
2590 * to each other through constraints in "context".
2592 * If there are any inequalities left, we construct a tableau for
2593 * the context and then add the inequalities of "bset".
2594 * Before adding these inequalities, we freeze all constraints such that
2595 * they won't be considered redundant in terms of the constraints of "bset".
2596 * Then we detect all redundant constraints (among the
2597 * constraints that weren't frozen), first by checking for redundancy in the
2598 * the tableau and then by checking if replacing a constraint by its negation
2599 * would lead to an empty set. This last step is fairly expensive
2600 * and could be optimized by more reuse of the tableau.
2601 * Finally, we update bset according to the results.
2603 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2604 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2606 int i, r;
2607 int *row = NULL;
2608 isl_ctx *ctx;
2609 isl_basic_set *combined = NULL;
2610 struct isl_tab *tab = NULL;
2611 unsigned n_eq, context_ineq;
2613 if (!bset || !ineq || !context)
2614 goto error;
2616 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2617 isl_basic_set_free(context);
2618 isl_mat_free(ineq);
2619 return bset;
2622 ctx = isl_basic_set_get_ctx(context);
2623 row = isl_calloc_array(ctx, int, bset->n_ineq);
2624 if (!row)
2625 goto error;
2627 if (mark_shifted_constraints(ineq, context, row) < 0)
2628 goto error;
2629 if (all_neg(row, bset->n_ineq))
2630 return update_ineq_free(bset, ineq, context, row, NULL);
2632 context = drop_irrelevant_constraints_marked(context, ineq, row);
2633 if (!context)
2634 goto error;
2635 if (isl_basic_set_plain_is_universe(context))
2636 return update_ineq_free(bset, ineq, context, row, NULL);
2638 n_eq = context->n_eq;
2639 context_ineq = context->n_ineq;
2640 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2641 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2642 tab = isl_tab_from_basic_set(combined, 0);
2643 for (i = 0; i < context_ineq; ++i)
2644 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2645 goto error;
2646 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2647 goto error;
2648 r = context_ineq;
2649 for (i = 0; i < bset->n_ineq; ++i) {
2650 if (row[i] < 0)
2651 continue;
2652 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2653 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2654 goto error;
2655 row[i] = r++;
2657 if (isl_tab_detect_implicit_equalities(tab) < 0)
2658 goto error;
2659 if (isl_tab_detect_redundant(tab) < 0)
2660 goto error;
2661 for (i = bset->n_ineq - 1; i >= 0; --i) {
2662 isl_basic_set *test;
2663 int is_empty;
2665 if (row[i] < 0)
2666 continue;
2667 r = row[i];
2668 if (tab->con[n_eq + r].is_redundant)
2669 continue;
2670 test = isl_basic_set_dup(combined);
2671 test = isl_inequality_negate(test, r);
2672 test = isl_basic_set_update_from_tab(test, tab);
2673 is_empty = isl_basic_set_is_empty(test);
2674 isl_basic_set_free(test);
2675 if (is_empty < 0)
2676 goto error;
2677 if (is_empty)
2678 tab->con[n_eq + r].is_redundant = 1;
2680 bset = update_ineq_free(bset, ineq, context, row, tab);
2681 if (bset) {
2682 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2683 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2686 isl_basic_set_free(combined);
2687 return bset;
2688 error:
2689 free(row);
2690 isl_mat_free(ineq);
2691 isl_tab_free(tab);
2692 isl_basic_set_free(combined);
2693 isl_basic_set_free(context);
2694 isl_basic_set_free(bset);
2695 return NULL;
2698 /* Extract the inequalities of "bset" as an isl_mat.
2700 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2702 isl_size total;
2703 isl_ctx *ctx;
2704 isl_mat *ineq;
2706 total = isl_basic_set_dim(bset, isl_dim_all);
2707 if (total < 0)
2708 return NULL;
2710 ctx = isl_basic_set_get_ctx(bset);
2711 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2712 0, 1 + total);
2714 return ineq;
2717 /* Remove all information from "bset" that is redundant in the context
2718 * of "context", for the case where both "bset" and "context" are
2719 * full-dimensional.
2721 static __isl_give isl_basic_set *uset_gist_uncompressed(
2722 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2724 isl_mat *ineq;
2726 ineq = extract_ineq(bset);
2727 return uset_gist_full(bset, ineq, context);
2730 /* Replace "bset" by an empty basic set in the same space.
2732 static __isl_give isl_basic_set *replace_by_empty(
2733 __isl_take isl_basic_set *bset)
2735 isl_space *space;
2737 space = isl_basic_set_get_space(bset);
2738 isl_basic_set_free(bset);
2739 return isl_basic_set_empty(space);
2742 /* Remove all information from "bset" that is redundant in the context
2743 * of "context", for the case where the combined equalities of
2744 * "bset" and "context" allow for a compression that can be obtained
2745 * by preapplication of "T".
2746 * If the compression of "context" is empty, meaning that "bset" and
2747 * "context" do not intersect, then return the empty set.
2749 * "bset" itself is not transformed by "T". Instead, the inequalities
2750 * are extracted from "bset" and those are transformed by "T".
2751 * uset_gist_full then determines which of the transformed inequalities
2752 * are redundant with respect to the transformed "context" and removes
2753 * the corresponding inequalities from "bset".
2755 * After preapplying "T" to the inequalities, any common factor is
2756 * removed from the coefficients. If this results in a tightening
2757 * of the constant term, then the same tightening is applied to
2758 * the corresponding untransformed inequality in "bset".
2759 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2761 * g f'(x) + r >= 0
2763 * with 0 <= r < g, then it is equivalent to
2765 * f'(x) >= 0
2767 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2768 * subspace compressed by T since the latter would be transformed to
2770 * g f'(x) >= 0
2772 static __isl_give isl_basic_set *uset_gist_compressed(
2773 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2774 __isl_take isl_mat *T)
2776 isl_ctx *ctx;
2777 isl_mat *ineq;
2778 int i;
2779 isl_size n_row, n_col;
2780 isl_int rem;
2782 ineq = extract_ineq(bset);
2783 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2784 context = isl_basic_set_preimage(context, T);
2786 if (!ineq || !context)
2787 goto error;
2788 if (isl_basic_set_plain_is_empty(context)) {
2789 isl_mat_free(ineq);
2790 isl_basic_set_free(context);
2791 return replace_by_empty(bset);
2794 ctx = isl_mat_get_ctx(ineq);
2795 n_row = isl_mat_rows(ineq);
2796 n_col = isl_mat_cols(ineq);
2797 if (n_row < 0 || n_col < 0)
2798 goto error;
2799 isl_int_init(rem);
2800 for (i = 0; i < n_row; ++i) {
2801 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2802 if (isl_int_is_zero(ctx->normalize_gcd))
2803 continue;
2804 if (isl_int_is_one(ctx->normalize_gcd))
2805 continue;
2806 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2807 ctx->normalize_gcd, n_col - 1);
2808 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2809 isl_int_fdiv_q(ineq->row[i][0],
2810 ineq->row[i][0], ctx->normalize_gcd);
2811 if (isl_int_is_zero(rem))
2812 continue;
2813 bset = isl_basic_set_cow(bset);
2814 if (!bset)
2815 break;
2816 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2818 isl_int_clear(rem);
2820 return uset_gist_full(bset, ineq, context);
2821 error:
2822 isl_mat_free(ineq);
2823 isl_basic_set_free(context);
2824 isl_basic_set_free(bset);
2825 return NULL;
2828 /* Project "bset" onto the variables that are involved in "template".
2830 static __isl_give isl_basic_set *project_onto_involved(
2831 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2833 int i;
2834 isl_size n;
2836 n = isl_basic_set_dim(template, isl_dim_set);
2837 if (n < 0 || !template)
2838 return isl_basic_set_free(bset);
2840 for (i = 0; i < n; ++i) {
2841 isl_bool involved;
2843 involved = isl_basic_set_involves_dims(template,
2844 isl_dim_set, i, 1);
2845 if (involved < 0)
2846 return isl_basic_set_free(bset);
2847 if (involved)
2848 continue;
2849 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2852 return bset;
2855 /* Remove all information from bset that is redundant in the context
2856 * of context. In particular, equalities that are linear combinations
2857 * of those in context are removed. Then the inequalities that are
2858 * redundant in the context of the equalities and inequalities of
2859 * context are removed.
2861 * First of all, we drop those constraints from "context"
2862 * that are irrelevant for computing the gist of "bset".
2863 * Alternatively, we could factorize the intersection of "context" and "bset".
2865 * We first compute the intersection of the integer affine hulls
2866 * of "bset" and "context",
2867 * compute the gist inside this intersection and then reduce
2868 * the constraints with respect to the equalities of the context
2869 * that only involve variables already involved in the input.
2870 * If the intersection of the affine hulls turns out to be empty,
2871 * then return the empty set.
2873 * If two constraints are mutually redundant, then uset_gist_full
2874 * will remove the second of those constraints. We therefore first
2875 * sort the constraints so that constraints not involving existentially
2876 * quantified variables are given precedence over those that do.
2877 * We have to perform this sorting before the variable compression,
2878 * because that may effect the order of the variables.
2880 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2881 __isl_take isl_basic_set *context)
2883 isl_mat *eq;
2884 isl_mat *T;
2885 isl_basic_set *aff;
2886 isl_basic_set *aff_context;
2887 isl_size total;
2889 total = isl_basic_set_dim(bset, isl_dim_all);
2890 if (total < 0 || !context)
2891 goto error;
2893 context = drop_irrelevant_constraints(context, bset);
2895 bset = isl_basic_set_detect_equalities(bset);
2896 aff = isl_basic_set_copy(bset);
2897 aff = isl_basic_set_plain_affine_hull(aff);
2898 context = isl_basic_set_detect_equalities(context);
2899 aff_context = isl_basic_set_copy(context);
2900 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2901 aff = isl_basic_set_intersect(aff, aff_context);
2902 if (!aff)
2903 goto error;
2904 if (isl_basic_set_plain_is_empty(aff)) {
2905 isl_basic_set_free(bset);
2906 isl_basic_set_free(context);
2907 return aff;
2909 bset = isl_basic_set_sort_constraints(bset);
2910 if (aff->n_eq == 0) {
2911 isl_basic_set_free(aff);
2912 return uset_gist_uncompressed(bset, context);
2914 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2915 eq = isl_mat_cow(eq);
2916 T = isl_mat_variable_compression(eq, NULL);
2917 isl_basic_set_free(aff);
2918 if (T && T->n_col == 0) {
2919 isl_mat_free(T);
2920 isl_basic_set_free(context);
2921 return replace_by_empty(bset);
2924 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2925 aff_context = project_onto_involved(aff_context, bset);
2927 bset = uset_gist_compressed(bset, context, T);
2928 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2930 if (bset) {
2931 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2932 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2935 return bset;
2936 error:
2937 isl_basic_set_free(bset);
2938 isl_basic_set_free(context);
2939 return NULL;
2942 /* Return the number of equality constraints in "bmap" that involve
2943 * local variables. This function assumes that Gaussian elimination
2944 * has been applied to the equality constraints.
2946 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2948 int i;
2949 isl_size total, n_div;
2951 if (!bmap)
2952 return -1;
2954 if (bmap->n_eq == 0)
2955 return 0;
2957 total = isl_basic_map_dim(bmap, isl_dim_all);
2958 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2959 if (total < 0 || n_div < 0)
2960 return -1;
2961 total -= n_div;
2963 for (i = 0; i < bmap->n_eq; ++i)
2964 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2965 n_div) == -1)
2966 return i;
2968 return bmap->n_eq;
2971 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2972 * The constraints are assumed not to involve any local variables.
2974 static __isl_give isl_basic_map *basic_map_from_equalities(
2975 __isl_take isl_space *space, __isl_take isl_mat *eq)
2977 int i, k;
2978 isl_size total;
2979 isl_basic_map *bmap = NULL;
2981 total = isl_space_dim(space, isl_dim_all);
2982 if (total < 0 || !eq)
2983 goto error;
2985 if (1 + total != eq->n_col)
2986 isl_die(isl_space_get_ctx(space), isl_error_internal,
2987 "unexpected number of columns", goto error);
2989 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2990 0, eq->n_row, 0);
2991 for (i = 0; i < eq->n_row; ++i) {
2992 k = isl_basic_map_alloc_equality(bmap);
2993 if (k < 0)
2994 goto error;
2995 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2998 isl_space_free(space);
2999 isl_mat_free(eq);
3000 return bmap;
3001 error:
3002 isl_space_free(space);
3003 isl_mat_free(eq);
3004 isl_basic_map_free(bmap);
3005 return NULL;
3008 /* Construct and return a variable compression based on the equality
3009 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3010 * "n1" is the number of (initial) equality constraints in "bmap1"
3011 * that do involve local variables.
3012 * "n2" is the number of (initial) equality constraints in "bmap2"
3013 * that do involve local variables.
3014 * "total" is the total number of other variables.
3015 * This function assumes that Gaussian elimination
3016 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3017 * such that the equality constraints not involving local variables
3018 * are those that start at "n1" or "n2".
3020 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3021 * then simply compute the compression based on the equality constraints
3022 * in the other basic map.
3023 * Otherwise, combine the equality constraints from both into a new
3024 * basic map such that Gaussian elimination can be applied to this combination
3025 * and then construct a variable compression from the resulting
3026 * equality constraints.
3028 static __isl_give isl_mat *combined_variable_compression(
3029 __isl_keep isl_basic_map *bmap1, int n1,
3030 __isl_keep isl_basic_map *bmap2, int n2, int total)
3032 isl_ctx *ctx;
3033 isl_mat *E1, *E2, *V;
3034 isl_basic_map *bmap;
3036 ctx = isl_basic_map_get_ctx(bmap1);
3037 if (bmap1->n_eq == n1) {
3038 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3039 n2, bmap2->n_eq - n2, 0, 1 + total);
3040 return isl_mat_variable_compression(E2, NULL);
3042 if (bmap2->n_eq == n2) {
3043 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3044 n1, bmap1->n_eq - n1, 0, 1 + total);
3045 return isl_mat_variable_compression(E1, NULL);
3047 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3048 n1, bmap1->n_eq - n1, 0, 1 + total);
3049 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3050 n2, bmap2->n_eq - n2, 0, 1 + total);
3051 E1 = isl_mat_concat(E1, E2);
3052 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3053 bmap = isl_basic_map_gauss(bmap, NULL);
3054 if (!bmap)
3055 return NULL;
3056 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3057 V = isl_mat_variable_compression(E1, NULL);
3058 isl_basic_map_free(bmap);
3060 return V;
3063 /* Extract the stride constraints from "bmap", compressed
3064 * with respect to both the stride constraints in "context" and
3065 * the remaining equality constraints in both "bmap" and "context".
3066 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3067 * "context_n_eq" is the number of (initial) stride constraints in "context".
3069 * Let x be all variables in "bmap" (and "context") other than the local
3070 * variables. First compute a variable compression
3072 * x = V x'
3074 * based on the non-stride equality constraints in "bmap" and "context".
3075 * Consider the stride constraints of "context",
3077 * A(x) + B(y) = 0
3079 * with y the local variables and plug in the variable compression,
3080 * resulting in
3082 * A(V x') + B(y) = 0
3084 * Use these constraints to compute a parameter compression on x'
3086 * x' = T x''
3088 * Now consider the stride constraints of "bmap"
3090 * C(x) + D(y) = 0
3092 * and plug in x = V*T x''.
3093 * That is, return A = [C*V*T D].
3095 static __isl_give isl_mat *extract_compressed_stride_constraints(
3096 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3097 __isl_keep isl_basic_map *context, int context_n_eq)
3099 isl_size total, n_div;
3100 isl_ctx *ctx;
3101 isl_mat *A, *B, *T, *V;
3103 total = isl_basic_map_dim(context, isl_dim_all);
3104 n_div = isl_basic_map_dim(context, isl_dim_div);
3105 if (total < 0 || n_div < 0)
3106 return NULL;
3107 total -= n_div;
3109 ctx = isl_basic_map_get_ctx(bmap);
3111 V = combined_variable_compression(bmap, bmap_n_eq,
3112 context, context_n_eq, total);
3114 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3115 B = isl_mat_sub_alloc6(ctx, context->eq,
3116 0, context_n_eq, 1 + total, n_div);
3117 A = isl_mat_product(A, isl_mat_copy(V));
3118 T = isl_mat_parameter_compression_ext(A, B);
3119 T = isl_mat_product(V, T);
3121 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3122 if (n_div < 0)
3123 T = isl_mat_free(T);
3124 else
3125 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3127 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3128 0, bmap_n_eq, 0, 1 + total + n_div);
3129 A = isl_mat_product(A, T);
3131 return A;
3134 /* Remove the prime factors from *g that have an exponent that
3135 * is strictly smaller than the exponent in "c".
3136 * All exponents in *g are known to be smaller than or equal
3137 * to those in "c".
3139 * That is, if *g is equal to
3141 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3143 * and "c" is equal to
3145 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3147 * then update *g to
3149 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3150 * p_n^{e_n * (e_n = f_n)}
3152 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3153 * neither does the gcd of *g and c / *g.
3154 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3155 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3156 * Dividing *g by this gcd therefore strictly reduces the exponent
3157 * of the prime factors that need to be removed, while leaving the
3158 * other prime factors untouched.
3159 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3160 * removes all undesired factors, without removing any others.
3162 static void remove_incomplete_powers(isl_int *g, isl_int c)
3164 isl_int t;
3166 isl_int_init(t);
3167 for (;;) {
3168 isl_int_divexact(t, c, *g);
3169 isl_int_gcd(t, t, *g);
3170 if (isl_int_is_one(t))
3171 break;
3172 isl_int_divexact(*g, *g, t);
3174 isl_int_clear(t);
3177 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3178 * of the same stride constraints in a compressed space that exploits
3179 * all equalities in the context and the other equalities in "bmap".
3181 * If the stride constraints of "bmap" are of the form
3183 * C(x) + D(y) = 0
3185 * then A is of the form
3187 * B(x') + D(y) = 0
3189 * If any of these constraints involves only a single local variable y,
3190 * then the constraint appears as
3192 * f(x) + m y_i = 0
3194 * in "bmap" and as
3196 * h(x') + m y_i = 0
3198 * in "A".
3200 * Let g be the gcd of m and the coefficients of h.
3201 * Then, in particular, g is a divisor of the coefficients of h and
3203 * f(x) = h(x')
3205 * is known to be a multiple of g.
3206 * If some prime factor in m appears with the same exponent in g,
3207 * then it can be removed from m because f(x) is already known
3208 * to be a multiple of g and therefore in particular of this power
3209 * of the prime factors.
3210 * Prime factors that appear with a smaller exponent in g cannot
3211 * be removed from m.
3212 * Let g' be the divisor of g containing all prime factors that
3213 * appear with the same exponent in m and g, then
3215 * f(x) + m y_i = 0
3217 * can be replaced by
3219 * f(x) + m/g' y_i' = 0
3221 * Note that (if g' != 1) this changes the explicit representation
3222 * of y_i to that of y_i', so the integer division at position i
3223 * is marked unknown and later recomputed by a call to
3224 * isl_basic_map_gauss.
3226 static __isl_give isl_basic_map *reduce_stride_constraints(
3227 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3229 int i;
3230 isl_size total, n_div;
3231 int any = 0;
3232 isl_int gcd;
3234 total = isl_basic_map_dim(bmap, isl_dim_all);
3235 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3236 if (total < 0 || n_div < 0 || !A)
3237 return isl_basic_map_free(bmap);
3238 total -= n_div;
3240 isl_int_init(gcd);
3241 for (i = 0; i < n; ++i) {
3242 int div;
3244 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3245 if (div < 0)
3246 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3247 "equality constraints modified unexpectedly",
3248 goto error);
3249 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3250 n_div - div - 1) != -1)
3251 continue;
3252 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3253 goto error;
3254 if (isl_int_is_one(gcd))
3255 continue;
3256 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3257 if (isl_int_is_one(gcd))
3258 continue;
3259 isl_int_divexact(bmap->eq[i][1 + total + div],
3260 bmap->eq[i][1 + total + div], gcd);
3261 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3262 if (!bmap)
3263 goto error;
3264 any = 1;
3266 isl_int_clear(gcd);
3268 if (any)
3269 bmap = isl_basic_map_gauss(bmap, NULL);
3271 return bmap;
3272 error:
3273 isl_int_clear(gcd);
3274 isl_basic_map_free(bmap);
3275 return NULL;
3278 /* Simplify the stride constraints in "bmap" based on
3279 * the remaining equality constraints in "bmap" and all equality
3280 * constraints in "context".
3281 * Only do this if both "bmap" and "context" have stride constraints.
3283 * First extract a copy of the stride constraints in "bmap" in a compressed
3284 * space exploiting all the other equality constraints and then
3285 * use this compressed copy to simplify the original stride constraints.
3287 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3288 __isl_keep isl_basic_map *context)
3290 int bmap_n_eq, context_n_eq;
3291 isl_mat *A;
3293 if (!bmap || !context)
3294 return isl_basic_map_free(bmap);
3296 bmap_n_eq = n_div_eq(bmap);
3297 context_n_eq = n_div_eq(context);
3299 if (bmap_n_eq < 0 || context_n_eq < 0)
3300 return isl_basic_map_free(bmap);
3301 if (bmap_n_eq == 0 || context_n_eq == 0)
3302 return bmap;
3304 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3305 context, context_n_eq);
3306 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3308 isl_mat_free(A);
3310 return bmap;
3313 /* Return a basic map that has the same intersection with "context" as "bmap"
3314 * and that is as "simple" as possible.
3316 * The core computation is performed on the pure constraints.
3317 * When we add back the meaning of the integer divisions, we need
3318 * to (re)introduce the div constraints. If we happen to have
3319 * discovered that some of these integer divisions are equal to
3320 * some affine combination of other variables, then these div
3321 * constraints may end up getting simplified in terms of the equalities,
3322 * resulting in extra inequalities on the other variables that
3323 * may have been removed already or that may not even have been
3324 * part of the input. We try and remove those constraints of
3325 * this form that are most obviously redundant with respect to
3326 * the context. We also remove those div constraints that are
3327 * redundant with respect to the other constraints in the result.
3329 * The stride constraints among the equality constraints in "bmap" are
3330 * also simplified with respecting to the other equality constraints
3331 * in "bmap" and with respect to all equality constraints in "context".
3333 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3334 __isl_take isl_basic_map *context)
3336 isl_basic_set *bset, *eq;
3337 isl_basic_map *eq_bmap;
3338 isl_size total, n_div, n_div_bmap;
3339 unsigned extra, n_eq, n_ineq;
3341 if (!bmap || !context)
3342 goto error;
3344 if (isl_basic_map_plain_is_universe(bmap)) {
3345 isl_basic_map_free(context);
3346 return bmap;
3348 if (isl_basic_map_plain_is_empty(context)) {
3349 isl_space *space = isl_basic_map_get_space(bmap);
3350 isl_basic_map_free(bmap);
3351 isl_basic_map_free(context);
3352 return isl_basic_map_universe(space);
3354 if (isl_basic_map_plain_is_empty(bmap)) {
3355 isl_basic_map_free(context);
3356 return bmap;
3359 bmap = isl_basic_map_remove_redundancies(bmap);
3360 context = isl_basic_map_remove_redundancies(context);
3361 bmap = isl_basic_map_order_divs(bmap);
3362 context = isl_basic_map_align_divs(context, bmap);
3364 n_div = isl_basic_map_dim(context, isl_dim_div);
3365 total = isl_basic_map_dim(bmap, isl_dim_all);
3366 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3367 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3368 goto error;
3369 extra = n_div - n_div_bmap;
3371 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3372 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3373 bset = uset_gist(bset,
3374 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3375 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3377 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3378 isl_basic_set_plain_is_empty(bset)) {
3379 isl_basic_map_free(context);
3380 return isl_basic_map_overlying_set(bset, bmap);
3383 n_eq = bset->n_eq;
3384 n_ineq = bset->n_ineq;
3385 eq = isl_basic_set_copy(bset);
3386 eq = isl_basic_set_cow(eq);
3387 eq = isl_basic_set_free_inequality(eq, n_ineq);
3388 bset = isl_basic_set_free_equality(bset, n_eq);
3390 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3391 eq_bmap = gist_strides(eq_bmap, context);
3392 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3393 bmap = isl_basic_map_overlying_set(bset, bmap);
3394 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3395 bmap = isl_basic_map_remove_redundancies(bmap);
3397 return bmap;
3398 error:
3399 isl_basic_map_free(bmap);
3400 isl_basic_map_free(context);
3401 return NULL;
3405 * Assumes context has no implicit divs.
3407 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3408 __isl_take isl_basic_map *context)
3410 int i;
3412 if (!map || !context)
3413 goto error;
3415 if (isl_basic_map_plain_is_empty(context)) {
3416 isl_space *space = isl_map_get_space(map);
3417 isl_map_free(map);
3418 isl_basic_map_free(context);
3419 return isl_map_universe(space);
3422 context = isl_basic_map_remove_redundancies(context);
3423 map = isl_map_cow(map);
3424 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3425 goto error;
3426 map = isl_map_compute_divs(map);
3427 if (!map)
3428 goto error;
3429 for (i = map->n - 1; i >= 0; --i) {
3430 map->p[i] = isl_basic_map_gist(map->p[i],
3431 isl_basic_map_copy(context));
3432 if (!map->p[i])
3433 goto error;
3434 if (isl_basic_map_plain_is_empty(map->p[i])) {
3435 isl_basic_map_free(map->p[i]);
3436 if (i != map->n - 1)
3437 map->p[i] = map->p[map->n - 1];
3438 map->n--;
3441 isl_basic_map_free(context);
3442 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3443 return map;
3444 error:
3445 isl_map_free(map);
3446 isl_basic_map_free(context);
3447 return NULL;
3450 /* Drop all inequalities from "bmap" that also appear in "context".
3451 * "context" is assumed to have only known local variables and
3452 * the initial local variables of "bmap" are assumed to be the same
3453 * as those of "context".
3454 * The constraints of both "bmap" and "context" are assumed
3455 * to have been sorted using isl_basic_map_sort_constraints.
3457 * Run through the inequality constraints of "bmap" and "context"
3458 * in sorted order.
3459 * If a constraint of "bmap" involves variables not in "context",
3460 * then it cannot appear in "context".
3461 * If a matching constraint is found, it is removed from "bmap".
3463 static __isl_give isl_basic_map *drop_inequalities(
3464 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3466 int i1, i2;
3467 isl_size total, bmap_total;
3468 unsigned extra;
3470 total = isl_basic_map_dim(context, isl_dim_all);
3471 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3472 if (total < 0 || bmap_total < 0)
3473 return isl_basic_map_free(bmap);
3475 extra = bmap_total - total;
3477 i1 = bmap->n_ineq - 1;
3478 i2 = context->n_ineq - 1;
3479 while (bmap && i1 >= 0 && i2 >= 0) {
3480 int cmp;
3482 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3483 extra) != -1) {
3484 --i1;
3485 continue;
3487 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3488 context->ineq[i2]);
3489 if (cmp < 0) {
3490 --i2;
3491 continue;
3493 if (cmp > 0) {
3494 --i1;
3495 continue;
3497 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3498 bmap = isl_basic_map_cow(bmap);
3499 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3500 bmap = isl_basic_map_free(bmap);
3502 --i1;
3503 --i2;
3506 return bmap;
3509 /* Drop all equalities from "bmap" that also appear in "context".
3510 * "context" is assumed to have only known local variables and
3511 * the initial local variables of "bmap" are assumed to be the same
3512 * as those of "context".
3514 * Run through the equality constraints of "bmap" and "context"
3515 * in sorted order.
3516 * If a constraint of "bmap" involves variables not in "context",
3517 * then it cannot appear in "context".
3518 * If a matching constraint is found, it is removed from "bmap".
3520 static __isl_give isl_basic_map *drop_equalities(
3521 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3523 int i1, i2;
3524 isl_size total, bmap_total;
3525 unsigned extra;
3527 total = isl_basic_map_dim(context, isl_dim_all);
3528 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3529 if (total < 0 || bmap_total < 0)
3530 return isl_basic_map_free(bmap);
3532 extra = bmap_total - total;
3534 i1 = bmap->n_eq - 1;
3535 i2 = context->n_eq - 1;
3537 while (bmap && i1 >= 0 && i2 >= 0) {
3538 int last1, last2;
3540 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3541 extra) != -1)
3542 break;
3543 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3544 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3545 if (last1 > last2) {
3546 --i2;
3547 continue;
3549 if (last1 < last2) {
3550 --i1;
3551 continue;
3553 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3554 bmap = isl_basic_map_cow(bmap);
3555 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3556 bmap = isl_basic_map_free(bmap);
3558 --i1;
3559 --i2;
3562 return bmap;
3565 /* Remove the constraints in "context" from "bmap".
3566 * "context" is assumed to have explicit representations
3567 * for all local variables.
3569 * First align the divs of "bmap" to those of "context" and
3570 * sort the constraints. Then drop all constraints from "bmap"
3571 * that appear in "context".
3573 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3574 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3576 isl_bool done, known;
3578 done = isl_basic_map_plain_is_universe(context);
3579 if (done == isl_bool_false)
3580 done = isl_basic_map_plain_is_universe(bmap);
3581 if (done == isl_bool_false)
3582 done = isl_basic_map_plain_is_empty(context);
3583 if (done == isl_bool_false)
3584 done = isl_basic_map_plain_is_empty(bmap);
3585 if (done < 0)
3586 goto error;
3587 if (done) {
3588 isl_basic_map_free(context);
3589 return bmap;
3591 known = isl_basic_map_divs_known(context);
3592 if (known < 0)
3593 goto error;
3594 if (!known)
3595 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3596 "context has unknown divs", goto error);
3598 context = isl_basic_map_order_divs(context);
3599 bmap = isl_basic_map_align_divs(bmap, context);
3600 bmap = isl_basic_map_gauss(bmap, NULL);
3601 bmap = isl_basic_map_sort_constraints(bmap);
3602 context = isl_basic_map_sort_constraints(context);
3604 bmap = drop_inequalities(bmap, context);
3605 bmap = drop_equalities(bmap, context);
3607 isl_basic_map_free(context);
3608 bmap = isl_basic_map_finalize(bmap);
3609 return bmap;
3610 error:
3611 isl_basic_map_free(bmap);
3612 isl_basic_map_free(context);
3613 return NULL;
3616 /* Replace "map" by the disjunct at position "pos" and free "context".
3618 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3619 int pos, __isl_take isl_basic_map *context)
3621 isl_basic_map *bmap;
3623 bmap = isl_basic_map_copy(map->p[pos]);
3624 isl_map_free(map);
3625 isl_basic_map_free(context);
3626 return isl_map_from_basic_map(bmap);
3629 /* Remove the constraints in "context" from "map".
3630 * If any of the disjuncts in the result turns out to be the universe,
3631 * then return this universe.
3632 * "context" is assumed to have explicit representations
3633 * for all local variables.
3635 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3636 __isl_take isl_basic_map *context)
3638 int i;
3639 isl_bool univ, known;
3641 univ = isl_basic_map_plain_is_universe(context);
3642 if (univ < 0)
3643 goto error;
3644 if (univ) {
3645 isl_basic_map_free(context);
3646 return map;
3648 known = isl_basic_map_divs_known(context);
3649 if (known < 0)
3650 goto error;
3651 if (!known)
3652 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3653 "context has unknown divs", goto error);
3655 map = isl_map_cow(map);
3656 if (!map)
3657 goto error;
3658 for (i = 0; i < map->n; ++i) {
3659 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3660 isl_basic_map_copy(context));
3661 univ = isl_basic_map_plain_is_universe(map->p[i]);
3662 if (univ < 0)
3663 goto error;
3664 if (univ && map->n > 1)
3665 return replace_by_disjunct(map, i, context);
3668 isl_basic_map_free(context);
3669 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3670 if (map->n > 1)
3671 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3672 return map;
3673 error:
3674 isl_map_free(map);
3675 isl_basic_map_free(context);
3676 return NULL;
3679 /* Remove the constraints in "context" from "set".
3680 * If any of the disjuncts in the result turns out to be the universe,
3681 * then return this universe.
3682 * "context" is assumed to have explicit representations
3683 * for all local variables.
3685 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3686 __isl_take isl_basic_set *context)
3688 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3689 bset_to_bmap(context)));
3692 /* Remove the constraints in "context" from "map".
3693 * If any of the disjuncts in the result turns out to be the universe,
3694 * then return this universe.
3695 * "context" is assumed to consist of a single disjunct and
3696 * to have explicit representations for all local variables.
3698 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3699 __isl_take isl_map *context)
3701 isl_basic_map *hull;
3703 hull = isl_map_unshifted_simple_hull(context);
3704 return isl_map_plain_gist_basic_map(map, hull);
3707 /* Replace "map" by a universe map in the same space and free "drop".
3709 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3710 __isl_take isl_map *drop)
3712 isl_map *res;
3714 res = isl_map_universe(isl_map_get_space(map));
3715 isl_map_free(map);
3716 isl_map_free(drop);
3717 return res;
3720 /* Return a map that has the same intersection with "context" as "map"
3721 * and that is as "simple" as possible.
3723 * If "map" is already the universe, then we cannot make it any simpler.
3724 * Similarly, if "context" is the universe, then we cannot exploit it
3725 * to simplify "map"
3726 * If "map" and "context" are identical to each other, then we can
3727 * return the corresponding universe.
3729 * If either "map" or "context" consists of multiple disjuncts,
3730 * then check if "context" happens to be a subset of "map",
3731 * in which case all constraints can be removed.
3732 * In case of multiple disjuncts, the standard procedure
3733 * may not be able to detect that all constraints can be removed.
3735 * If none of these cases apply, we have to work a bit harder.
3736 * During this computation, we make use of a single disjunct context,
3737 * so if the original context consists of more than one disjunct
3738 * then we need to approximate the context by a single disjunct set.
3739 * Simply taking the simple hull may drop constraints that are
3740 * only implicitly available in each disjunct. We therefore also
3741 * look for constraints among those defining "map" that are valid
3742 * for the context. These can then be used to simplify away
3743 * the corresponding constraints in "map".
3745 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3746 __isl_take isl_map *context)
3748 int equal;
3749 int is_universe;
3750 isl_size n_disjunct_map, n_disjunct_context;
3751 isl_bool subset;
3752 isl_basic_map *hull;
3754 is_universe = isl_map_plain_is_universe(map);
3755 if (is_universe >= 0 && !is_universe)
3756 is_universe = isl_map_plain_is_universe(context);
3757 if (is_universe < 0)
3758 goto error;
3759 if (is_universe) {
3760 isl_map_free(context);
3761 return map;
3764 isl_map_align_params_bin(&map, &context);
3765 equal = isl_map_plain_is_equal(map, context);
3766 if (equal < 0)
3767 goto error;
3768 if (equal)
3769 return replace_by_universe(map, context);
3771 n_disjunct_map = isl_map_n_basic_map(map);
3772 n_disjunct_context = isl_map_n_basic_map(context);
3773 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3774 goto error;
3775 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3776 subset = isl_map_is_subset(context, map);
3777 if (subset < 0)
3778 goto error;
3779 if (subset)
3780 return replace_by_universe(map, context);
3783 context = isl_map_compute_divs(context);
3784 if (!context)
3785 goto error;
3786 if (n_disjunct_context == 1) {
3787 hull = isl_map_simple_hull(context);
3788 } else {
3789 isl_ctx *ctx;
3790 isl_map_list *list;
3792 ctx = isl_map_get_ctx(map);
3793 list = isl_map_list_alloc(ctx, 2);
3794 list = isl_map_list_add(list, isl_map_copy(context));
3795 list = isl_map_list_add(list, isl_map_copy(map));
3796 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3797 list);
3799 return isl_map_gist_basic_map(map, hull);
3800 error:
3801 isl_map_free(map);
3802 isl_map_free(context);
3803 return NULL;
3806 __isl_give isl_basic_set *isl_basic_set_gist(__isl_take isl_basic_set *bset,
3807 __isl_take isl_basic_set *context)
3809 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3810 bset_to_bmap(context)));
3813 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3814 __isl_take isl_basic_set *context)
3816 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3817 bset_to_bmap(context)));
3820 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3821 __isl_take isl_basic_set *context)
3823 isl_space *space = isl_set_get_space(set);
3824 isl_basic_set *dom_context = isl_basic_set_universe(space);
3825 dom_context = isl_basic_set_intersect_params(dom_context, context);
3826 return isl_set_gist_basic_set(set, dom_context);
3829 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3830 __isl_take isl_set *context)
3832 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3835 /* Compute the gist of "bmap" with respect to the constraints "context"
3836 * on the domain.
3838 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3839 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3841 isl_space *space = isl_basic_map_get_space(bmap);
3842 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3844 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3845 return isl_basic_map_gist(bmap, bmap_context);
3848 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3849 __isl_take isl_set *context)
3851 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3852 map_context = isl_map_intersect_domain(map_context, context);
3853 return isl_map_gist(map, map_context);
3856 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3857 __isl_take isl_set *context)
3859 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3860 map_context = isl_map_intersect_range(map_context, context);
3861 return isl_map_gist(map, map_context);
3864 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3865 __isl_take isl_set *context)
3867 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3868 map_context = isl_map_intersect_params(map_context, context);
3869 return isl_map_gist(map, map_context);
3872 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3873 __isl_take isl_set *context)
3875 return isl_map_gist_params(set, context);
3878 /* Quick check to see if two basic maps are disjoint.
3879 * In particular, we reduce the equalities and inequalities of
3880 * one basic map in the context of the equalities of the other
3881 * basic map and check if we get a contradiction.
3883 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3884 __isl_keep isl_basic_map *bmap2)
3886 struct isl_vec *v = NULL;
3887 int *elim = NULL;
3888 isl_size total;
3889 int i;
3891 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3892 return isl_bool_error;
3893 if (bmap1->n_div || bmap2->n_div)
3894 return isl_bool_false;
3895 if (!bmap1->n_eq && !bmap2->n_eq)
3896 return isl_bool_false;
3898 total = isl_space_dim(bmap1->dim, isl_dim_all);
3899 if (total < 0)
3900 return isl_bool_error;
3901 if (total == 0)
3902 return isl_bool_false;
3903 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3904 if (!v)
3905 goto error;
3906 elim = isl_alloc_array(bmap1->ctx, int, total);
3907 if (!elim)
3908 goto error;
3909 compute_elimination_index(bmap1, elim, total);
3910 for (i = 0; i < bmap2->n_eq; ++i) {
3911 int reduced;
3912 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3913 bmap1, elim, total);
3914 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3915 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3916 goto disjoint;
3918 for (i = 0; i < bmap2->n_ineq; ++i) {
3919 int reduced;
3920 reduced = reduced_using_equalities(v->block.data,
3921 bmap2->ineq[i], bmap1, elim, total);
3922 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3923 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3924 goto disjoint;
3926 compute_elimination_index(bmap2, elim, total);
3927 for (i = 0; i < bmap1->n_ineq; ++i) {
3928 int reduced;
3929 reduced = reduced_using_equalities(v->block.data,
3930 bmap1->ineq[i], bmap2, elim, total);
3931 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3932 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3933 goto disjoint;
3935 isl_vec_free(v);
3936 free(elim);
3937 return isl_bool_false;
3938 disjoint:
3939 isl_vec_free(v);
3940 free(elim);
3941 return isl_bool_true;
3942 error:
3943 isl_vec_free(v);
3944 free(elim);
3945 return isl_bool_error;
3948 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3949 __isl_keep isl_basic_set *bset2)
3951 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3952 bset_to_bmap(bset2));
3955 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3957 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3958 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3959 __isl_keep isl_basic_map *bmap2))
3961 int i, j;
3963 if (!map1 || !map2)
3964 return isl_bool_error;
3966 for (i = 0; i < map1->n; ++i) {
3967 for (j = 0; j < map2->n; ++j) {
3968 isl_bool d = test(map1->p[i], map2->p[j]);
3969 if (d != isl_bool_true)
3970 return d;
3974 return isl_bool_true;
3977 /* Are "map1" and "map2" obviously disjoint, based on information
3978 * that can be derived without looking at the individual basic maps?
3980 * In particular, if one of them is empty or if they live in different spaces
3981 * (ignoring parameters), then they are clearly disjoint.
3983 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3984 __isl_keep isl_map *map2)
3986 isl_bool disjoint;
3987 isl_bool match;
3989 if (!map1 || !map2)
3990 return isl_bool_error;
3992 disjoint = isl_map_plain_is_empty(map1);
3993 if (disjoint < 0 || disjoint)
3994 return disjoint;
3996 disjoint = isl_map_plain_is_empty(map2);
3997 if (disjoint < 0 || disjoint)
3998 return disjoint;
4000 match = isl_map_tuple_is_equal(map1, isl_dim_in, map2, isl_dim_in);
4001 if (match < 0 || !match)
4002 return match < 0 ? isl_bool_error : isl_bool_true;
4004 match = isl_map_tuple_is_equal(map1, isl_dim_out, map2, isl_dim_out);
4005 if (match < 0 || !match)
4006 return match < 0 ? isl_bool_error : isl_bool_true;
4008 return isl_bool_false;
4011 /* Are "map1" and "map2" obviously disjoint?
4013 * If one of them is empty or if they live in different spaces (ignoring
4014 * parameters), then they are clearly disjoint.
4015 * This is checked by isl_map_plain_is_disjoint_global.
4017 * If they have different parameters, then we skip any further tests.
4019 * If they are obviously equal, but not obviously empty, then we will
4020 * not be able to detect if they are disjoint.
4022 * Otherwise we check if each basic map in "map1" is obviously disjoint
4023 * from each basic map in "map2".
4025 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
4026 __isl_keep isl_map *map2)
4028 isl_bool disjoint;
4029 isl_bool intersect;
4030 isl_bool match;
4032 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4033 if (disjoint < 0 || disjoint)
4034 return disjoint;
4036 match = isl_map_has_equal_params(map1, map2);
4037 if (match < 0 || !match)
4038 return match < 0 ? isl_bool_error : isl_bool_false;
4040 intersect = isl_map_plain_is_equal(map1, map2);
4041 if (intersect < 0 || intersect)
4042 return intersect < 0 ? isl_bool_error : isl_bool_false;
4044 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4047 /* Are "map1" and "map2" disjoint?
4048 * The parameters are assumed to have been aligned.
4050 * In particular, check whether all pairs of basic maps are disjoint.
4052 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
4053 __isl_keep isl_map *map2)
4055 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4058 /* Are "map1" and "map2" disjoint?
4060 * They are disjoint if they are "obviously disjoint" or if one of them
4061 * is empty. Otherwise, they are not disjoint if one of them is universal.
4062 * If the two inputs are (obviously) equal and not empty, then they are
4063 * not disjoint.
4064 * If none of these cases apply, then check if all pairs of basic maps
4065 * are disjoint after aligning the parameters.
4067 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4069 isl_bool disjoint;
4070 isl_bool intersect;
4072 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4073 if (disjoint < 0 || disjoint)
4074 return disjoint;
4076 disjoint = isl_map_is_empty(map1);
4077 if (disjoint < 0 || disjoint)
4078 return disjoint;
4080 disjoint = isl_map_is_empty(map2);
4081 if (disjoint < 0 || disjoint)
4082 return disjoint;
4084 intersect = isl_map_plain_is_universe(map1);
4085 if (intersect < 0 || intersect)
4086 return isl_bool_not(intersect);
4088 intersect = isl_map_plain_is_universe(map2);
4089 if (intersect < 0 || intersect)
4090 return isl_bool_not(intersect);
4092 intersect = isl_map_plain_is_equal(map1, map2);
4093 if (intersect < 0 || intersect)
4094 return isl_bool_not(intersect);
4096 return isl_map_align_params_map_map_and_test(map1, map2,
4097 &isl_map_is_disjoint_aligned);
4100 /* Are "bmap1" and "bmap2" disjoint?
4102 * They are disjoint if they are "obviously disjoint" or if one of them
4103 * is empty. Otherwise, they are not disjoint if one of them is universal.
4104 * If none of these cases apply, we compute the intersection and see if
4105 * the result is empty.
4107 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4108 __isl_keep isl_basic_map *bmap2)
4110 isl_bool disjoint;
4111 isl_bool intersect;
4112 isl_basic_map *test;
4114 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4115 if (disjoint < 0 || disjoint)
4116 return disjoint;
4118 disjoint = isl_basic_map_is_empty(bmap1);
4119 if (disjoint < 0 || disjoint)
4120 return disjoint;
4122 disjoint = isl_basic_map_is_empty(bmap2);
4123 if (disjoint < 0 || disjoint)
4124 return disjoint;
4126 intersect = isl_basic_map_plain_is_universe(bmap1);
4127 if (intersect < 0 || intersect)
4128 return isl_bool_not(intersect);
4130 intersect = isl_basic_map_plain_is_universe(bmap2);
4131 if (intersect < 0 || intersect)
4132 return isl_bool_not(intersect);
4134 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4135 isl_basic_map_copy(bmap2));
4136 disjoint = isl_basic_map_is_empty(test);
4137 isl_basic_map_free(test);
4139 return disjoint;
4142 /* Are "bset1" and "bset2" disjoint?
4144 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4145 __isl_keep isl_basic_set *bset2)
4147 return isl_basic_map_is_disjoint(bset1, bset2);
4150 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4151 __isl_keep isl_set *set2)
4153 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4156 /* Are "set1" and "set2" disjoint?
4158 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4160 return isl_map_is_disjoint(set1, set2);
4163 /* Is "v" equal to 0, 1 or -1?
4165 static int is_zero_or_one(isl_int v)
4167 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4170 /* Are the "n" coefficients starting at "first" of inequality constraints
4171 * "i" and "j" of "bmap" opposite to each other?
4173 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4174 int first, int n)
4176 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4179 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4180 * apart from the constant term?
4182 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4184 isl_size total;
4186 total = isl_basic_map_dim(bmap, isl_dim_all);
4187 if (total < 0)
4188 return isl_bool_error;
4189 return is_opposite_part(bmap, i, j, 1, total);
4192 /* Check if we can combine a given div with lower bound l and upper
4193 * bound u with some other div and if so return that other div.
4194 * Otherwise, return a position beyond the integer divisions.
4195 * Return -1 on error.
4197 * We first check that
4198 * - the bounds are opposites of each other (except for the constant
4199 * term)
4200 * - the bounds do not reference any other div
4201 * - no div is defined in terms of this div
4203 * Let m be the size of the range allowed on the div by the bounds.
4204 * That is, the bounds are of the form
4206 * e <= a <= e + m - 1
4208 * with e some expression in the other variables.
4209 * We look for another div b such that no third div is defined in terms
4210 * of this second div b and such that in any constraint that contains
4211 * a (except for the given lower and upper bound), also contains b
4212 * with a coefficient that is m times that of b.
4213 * That is, all constraints (except for the lower and upper bound)
4214 * are of the form
4216 * e + f (a + m b) >= 0
4218 * Furthermore, in the constraints that only contain b, the coefficient
4219 * of b should be equal to 1 or -1.
4220 * If so, we return b so that "a + m b" can be replaced by
4221 * a single div "c = a + m b".
4223 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4224 unsigned div, unsigned l, unsigned u)
4226 int i, j;
4227 unsigned n_div;
4228 isl_size v_div;
4229 int coalesce;
4230 isl_bool opp;
4232 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4233 if (n_div <= 1)
4234 return n_div;
4235 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4236 if (v_div < 0)
4237 return -1;
4238 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4239 return n_div;
4240 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4241 n_div - div - 1) != -1)
4242 return n_div;
4243 opp = is_opposite(bmap, l, u);
4244 if (opp < 0 || !opp)
4245 return opp < 0 ? -1 : n_div;
4247 for (i = 0; i < n_div; ++i) {
4248 if (isl_int_is_zero(bmap->div[i][0]))
4249 continue;
4250 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4251 return n_div;
4254 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4255 if (isl_int_is_neg(bmap->ineq[l][0])) {
4256 isl_int_sub(bmap->ineq[l][0],
4257 bmap->ineq[l][0], bmap->ineq[u][0]);
4258 bmap = isl_basic_map_copy(bmap);
4259 bmap = isl_basic_map_set_to_empty(bmap);
4260 isl_basic_map_free(bmap);
4261 return n_div;
4263 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4264 coalesce = n_div;
4265 for (i = 0; i < n_div; ++i) {
4266 if (i == div)
4267 continue;
4268 if (!pairs[i])
4269 continue;
4270 for (j = 0; j < n_div; ++j) {
4271 if (isl_int_is_zero(bmap->div[j][0]))
4272 continue;
4273 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
4274 break;
4276 if (j < n_div)
4277 continue;
4278 for (j = 0; j < bmap->n_ineq; ++j) {
4279 int valid;
4280 if (j == l || j == u)
4281 continue;
4282 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4283 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4284 continue;
4285 break;
4287 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4288 break;
4289 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4290 bmap->ineq[j][1 + v_div + div],
4291 bmap->ineq[l][0]);
4292 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4293 bmap->ineq[j][1 + v_div + i]);
4294 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4295 bmap->ineq[j][1 + v_div + div],
4296 bmap->ineq[l][0]);
4297 if (!valid)
4298 break;
4300 if (j < bmap->n_ineq)
4301 continue;
4302 coalesce = i;
4303 break;
4305 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4306 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4307 return coalesce;
4310 /* Internal data structure used during the construction and/or evaluation of
4311 * an inequality that ensures that a pair of bounds always allows
4312 * for an integer value.
4314 * "tab" is the tableau in which the inequality is evaluated. It may
4315 * be NULL until it is actually needed.
4316 * "v" contains the inequality coefficients.
4317 * "g", "fl" and "fu" are temporary scalars used during the construction and
4318 * evaluation.
4320 struct test_ineq_data {
4321 struct isl_tab *tab;
4322 isl_vec *v;
4323 isl_int g;
4324 isl_int fl;
4325 isl_int fu;
4328 /* Free all the memory allocated by the fields of "data".
4330 static void test_ineq_data_clear(struct test_ineq_data *data)
4332 isl_tab_free(data->tab);
4333 isl_vec_free(data->v);
4334 isl_int_clear(data->g);
4335 isl_int_clear(data->fl);
4336 isl_int_clear(data->fu);
4339 /* Is the inequality stored in data->v satisfied by "bmap"?
4340 * That is, does it only attain non-negative values?
4341 * data->tab is a tableau corresponding to "bmap".
4343 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4344 struct test_ineq_data *data)
4346 isl_ctx *ctx;
4347 enum isl_lp_result res;
4349 ctx = isl_basic_map_get_ctx(bmap);
4350 if (!data->tab)
4351 data->tab = isl_tab_from_basic_map(bmap, 0);
4352 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4353 if (res == isl_lp_error)
4354 return isl_bool_error;
4355 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4358 /* Given a lower and an upper bound on div i, do they always allow
4359 * for an integer value of the given div?
4360 * Determine this property by constructing an inequality
4361 * such that the property is guaranteed when the inequality is nonnegative.
4362 * The lower bound is inequality l, while the upper bound is inequality u.
4363 * The constructed inequality is stored in data->v.
4365 * Let the upper bound be
4367 * -n_u a + e_u >= 0
4369 * and the lower bound
4371 * n_l a + e_l >= 0
4373 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4374 * We have
4376 * - f_u e_l <= f_u f_l g a <= f_l e_u
4378 * Since all variables are integer valued, this is equivalent to
4380 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4382 * If this interval is at least f_u f_l g, then it contains at least
4383 * one integer value for a.
4384 * That is, the test constraint is
4386 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4388 * or
4390 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4392 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4393 * then the constraint can be scaled down by a factor g',
4394 * with the constant term replaced by
4395 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4396 * Note that the result of applying Fourier-Motzkin to this pair
4397 * of constraints is
4399 * f_l e_u + f_u e_l >= 0
4401 * If the constant term of the scaled down version of this constraint,
4402 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4403 * term of the scaled down test constraint, then the test constraint
4404 * is known to hold and no explicit evaluation is required.
4405 * This is essentially the Omega test.
4407 * If the test constraint consists of only a constant term, then
4408 * it is sufficient to look at the sign of this constant term.
4410 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4411 int l, int u, struct test_ineq_data *data)
4413 unsigned offset;
4414 isl_size n_div;
4416 offset = isl_basic_map_offset(bmap, isl_dim_div);
4417 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4418 if (n_div < 0)
4419 return isl_bool_error;
4421 isl_int_gcd(data->g,
4422 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4423 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4424 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4425 isl_int_neg(data->fu, data->fu);
4426 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4427 data->fu, bmap->ineq[l], offset + n_div);
4428 isl_int_mul(data->g, data->g, data->fl);
4429 isl_int_mul(data->g, data->g, data->fu);
4430 isl_int_sub(data->g, data->g, data->fl);
4431 isl_int_sub(data->g, data->g, data->fu);
4432 isl_int_add_ui(data->g, data->g, 1);
4433 isl_int_sub(data->fl, data->v->el[0], data->g);
4435 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4436 if (isl_int_is_zero(data->g))
4437 return isl_int_is_nonneg(data->fl);
4438 if (isl_int_is_one(data->g)) {
4439 isl_int_set(data->v->el[0], data->fl);
4440 return test_ineq_is_satisfied(bmap, data);
4442 isl_int_fdiv_q(data->fl, data->fl, data->g);
4443 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4444 if (isl_int_eq(data->fl, data->v->el[0]))
4445 return isl_bool_true;
4446 isl_int_set(data->v->el[0], data->fl);
4447 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4448 offset - 1 + n_div);
4450 return test_ineq_is_satisfied(bmap, data);
4453 /* Remove more kinds of divs that are not strictly needed.
4454 * In particular, if all pairs of lower and upper bounds on a div
4455 * are such that they allow at least one integer value of the div,
4456 * then we can eliminate the div using Fourier-Motzkin without
4457 * introducing any spurious solutions.
4459 * If at least one of the two constraints has a unit coefficient for the div,
4460 * then the presence of such a value is guaranteed so there is no need to check.
4461 * In particular, the value attained by the bound with unit coefficient
4462 * can serve as this intermediate value.
4464 static __isl_give isl_basic_map *drop_more_redundant_divs(
4465 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4467 isl_ctx *ctx;
4468 struct test_ineq_data data = { NULL, NULL };
4469 unsigned off;
4470 isl_size n_div;
4471 int remove = -1;
4473 isl_int_init(data.g);
4474 isl_int_init(data.fl);
4475 isl_int_init(data.fu);
4477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4478 if (n_div < 0)
4479 goto error;
4481 ctx = isl_basic_map_get_ctx(bmap);
4482 off = isl_basic_map_offset(bmap, isl_dim_div);
4483 data.v = isl_vec_alloc(ctx, off + n_div);
4484 if (!data.v)
4485 goto error;
4487 while (n > 0) {
4488 int i, l, u;
4489 int best = -1;
4490 isl_bool has_int;
4492 for (i = 0; i < n_div; ++i) {
4493 if (!pairs[i])
4494 continue;
4495 if (best >= 0 && pairs[best] <= pairs[i])
4496 continue;
4497 best = i;
4500 i = best;
4501 for (l = 0; l < bmap->n_ineq; ++l) {
4502 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4503 continue;
4504 if (isl_int_is_one(bmap->ineq[l][off + i]))
4505 continue;
4506 for (u = 0; u < bmap->n_ineq; ++u) {
4507 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4508 continue;
4509 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4510 continue;
4511 has_int = int_between_bounds(bmap, i, l, u,
4512 &data);
4513 if (has_int < 0)
4514 goto error;
4515 if (data.tab && data.tab->empty)
4516 break;
4517 if (!has_int)
4518 break;
4520 if (u < bmap->n_ineq)
4521 break;
4523 if (data.tab && data.tab->empty) {
4524 bmap = isl_basic_map_set_to_empty(bmap);
4525 break;
4527 if (l == bmap->n_ineq) {
4528 remove = i;
4529 break;
4531 pairs[i] = 0;
4532 --n;
4535 test_ineq_data_clear(&data);
4537 free(pairs);
4539 if (remove < 0)
4540 return bmap;
4542 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4543 return isl_basic_map_drop_redundant_divs(bmap);
4544 error:
4545 free(pairs);
4546 isl_basic_map_free(bmap);
4547 test_ineq_data_clear(&data);
4548 return NULL;
4551 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4552 * and the upper bound u, div1 always occurs together with div2 in the form
4553 * (div1 + m div2), where m is the constant range on the variable div1
4554 * allowed by l and u, replace the pair div1 and div2 by a single
4555 * div that is equal to div1 + m div2.
4557 * The new div will appear in the location that contains div2.
4558 * We need to modify all constraints that contain
4559 * div2 = (div - div1) / m
4560 * The coefficient of div2 is known to be equal to 1 or -1.
4561 * (If a constraint does not contain div2, it will also not contain div1.)
4562 * If the constraint also contains div1, then we know they appear
4563 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4564 * i.e., the coefficient of div is f.
4566 * Otherwise, we first need to introduce div1 into the constraint.
4567 * Let l be
4569 * div1 + f >=0
4571 * and u
4573 * -div1 + f' >= 0
4575 * A lower bound on div2
4577 * div2 + t >= 0
4579 * can be replaced by
4581 * m div2 + div1 + m t + f >= 0
4583 * An upper bound
4585 * -div2 + t >= 0
4587 * can be replaced by
4589 * -(m div2 + div1) + m t + f' >= 0
4591 * These constraint are those that we would obtain from eliminating
4592 * div1 using Fourier-Motzkin.
4594 * After all constraints have been modified, we drop the lower and upper
4595 * bound and then drop div1.
4596 * Since the new div is only placed in the same location that used
4597 * to store div2, but otherwise has a different meaning, any possible
4598 * explicit representation of the original div2 is removed.
4600 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4601 unsigned div1, unsigned div2, unsigned l, unsigned u)
4603 isl_ctx *ctx;
4604 isl_int m;
4605 isl_size v_div;
4606 unsigned total;
4607 int i;
4609 ctx = isl_basic_map_get_ctx(bmap);
4611 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4612 if (v_div < 0)
4613 return isl_basic_map_free(bmap);
4614 total = 1 + v_div + bmap->n_div;
4616 isl_int_init(m);
4617 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4618 isl_int_add_ui(m, m, 1);
4620 for (i = 0; i < bmap->n_ineq; ++i) {
4621 if (i == l || i == u)
4622 continue;
4623 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4624 continue;
4625 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4626 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4627 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4628 ctx->one, bmap->ineq[l], total);
4629 else
4630 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4631 ctx->one, bmap->ineq[u], total);
4633 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4634 bmap->ineq[i][1 + v_div + div1]);
4635 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4638 isl_int_clear(m);
4639 if (l > u) {
4640 isl_basic_map_drop_inequality(bmap, l);
4641 isl_basic_map_drop_inequality(bmap, u);
4642 } else {
4643 isl_basic_map_drop_inequality(bmap, u);
4644 isl_basic_map_drop_inequality(bmap, l);
4646 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4647 bmap = isl_basic_map_drop_div(bmap, div1);
4648 return bmap;
4651 /* First check if we can coalesce any pair of divs and
4652 * then continue with dropping more redundant divs.
4654 * We loop over all pairs of lower and upper bounds on a div
4655 * with coefficient 1 and -1, respectively, check if there
4656 * is any other div "c" with which we can coalesce the div
4657 * and if so, perform the coalescing.
4659 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4660 __isl_take isl_basic_map *bmap, int *pairs, int n)
4662 int i, l, u;
4663 isl_size v_div;
4664 isl_size n_div;
4666 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4667 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4668 if (v_div < 0 || n_div < 0)
4669 return isl_basic_map_free(bmap);
4671 for (i = 0; i < n_div; ++i) {
4672 if (!pairs[i])
4673 continue;
4674 for (l = 0; l < bmap->n_ineq; ++l) {
4675 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4676 continue;
4677 for (u = 0; u < bmap->n_ineq; ++u) {
4678 int c;
4680 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4681 continue;
4682 c = div_find_coalesce(bmap, pairs, i, l, u);
4683 if (c < 0)
4684 goto error;
4685 if (c >= n_div)
4686 continue;
4687 free(pairs);
4688 bmap = coalesce_divs(bmap, i, c, l, u);
4689 return isl_basic_map_drop_redundant_divs(bmap);
4694 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4695 free(pairs);
4696 return bmap;
4699 return drop_more_redundant_divs(bmap, pairs, n);
4700 error:
4701 free(pairs);
4702 isl_basic_map_free(bmap);
4703 return NULL;
4706 /* Are the "n" coefficients starting at "first" of inequality constraints
4707 * "i" and "j" of "bmap" equal to each other?
4709 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4710 int first, int n)
4712 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4715 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4716 * apart from the constant term and the coefficient at position "pos"?
4718 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4719 int pos)
4721 isl_size total;
4723 total = isl_basic_map_dim(bmap, isl_dim_all);
4724 if (total < 0)
4725 return isl_bool_error;
4726 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4727 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4730 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4731 * apart from the constant term and the coefficient at position "pos"?
4733 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4734 int pos)
4736 isl_size total;
4738 total = isl_basic_map_dim(bmap, isl_dim_all);
4739 if (total < 0)
4740 return isl_bool_error;
4741 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4742 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4745 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4746 * been modified, simplying it if "simplify" is set.
4747 * Free the temporary data structure "pairs" that was associated
4748 * to the old version of "bmap".
4750 static __isl_give isl_basic_map *drop_redundant_divs_again(
4751 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4753 if (simplify)
4754 bmap = isl_basic_map_simplify(bmap);
4755 free(pairs);
4756 return isl_basic_map_drop_redundant_divs(bmap);
4759 /* Is "div" the single unknown existentially quantified variable
4760 * in inequality constraint "ineq" of "bmap"?
4761 * "div" is known to have a non-zero coefficient in "ineq".
4763 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4764 int div)
4766 int i;
4767 isl_size n_div;
4768 unsigned o_div;
4769 isl_bool known;
4771 known = isl_basic_map_div_is_known(bmap, div);
4772 if (known < 0 || known)
4773 return isl_bool_not(known);
4774 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4775 if (n_div < 0)
4776 return isl_bool_error;
4777 if (n_div == 1)
4778 return isl_bool_true;
4779 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4780 for (i = 0; i < n_div; ++i) {
4781 isl_bool known;
4783 if (i == div)
4784 continue;
4785 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4786 continue;
4787 known = isl_basic_map_div_is_known(bmap, i);
4788 if (known < 0 || !known)
4789 return known;
4792 return isl_bool_true;
4795 /* Does integer division "div" have coefficient 1 in inequality constraint
4796 * "ineq" of "map"?
4798 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4800 unsigned o_div;
4802 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4803 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4804 return isl_bool_true;
4806 return isl_bool_false;
4809 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4810 * then try and drop redundant divs again,
4811 * freeing the temporary data structure "pairs" that was associated
4812 * to the old version of "bmap".
4814 static __isl_give isl_basic_map *set_eq_and_try_again(
4815 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4817 bmap = isl_basic_map_cow(bmap);
4818 isl_basic_map_inequality_to_equality(bmap, ineq);
4819 return drop_redundant_divs_again(bmap, pairs, 1);
4822 /* Drop the integer division at position "div", along with the two
4823 * inequality constraints "ineq1" and "ineq2" in which it appears
4824 * from "bmap" and then try and drop redundant divs again,
4825 * freeing the temporary data structure "pairs" that was associated
4826 * to the old version of "bmap".
4828 static __isl_give isl_basic_map *drop_div_and_try_again(
4829 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4830 __isl_take int *pairs)
4832 if (ineq1 > ineq2) {
4833 isl_basic_map_drop_inequality(bmap, ineq1);
4834 isl_basic_map_drop_inequality(bmap, ineq2);
4835 } else {
4836 isl_basic_map_drop_inequality(bmap, ineq2);
4837 isl_basic_map_drop_inequality(bmap, ineq1);
4839 bmap = isl_basic_map_drop_div(bmap, div);
4840 return drop_redundant_divs_again(bmap, pairs, 0);
4843 /* Given two inequality constraints
4845 * f(x) + n d + c >= 0, (ineq)
4847 * with d the variable at position "pos", and
4849 * f(x) + c0 >= 0, (lower)
4851 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4852 * determined by the first constraint.
4853 * That is, store
4855 * ceil((c0 - c)/n)
4857 * in *l.
4859 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4860 int ineq, int lower, int pos, isl_int *l)
4862 isl_int_neg(*l, bmap->ineq[ineq][0]);
4863 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4864 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4867 /* Given two inequality constraints
4869 * f(x) + n d + c >= 0, (ineq)
4871 * with d the variable at position "pos", and
4873 * -f(x) - c0 >= 0, (upper)
4875 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4876 * determined by the first constraint.
4877 * That is, store
4879 * ceil((-c1 - c)/n)
4881 * in *u.
4883 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4884 int ineq, int upper, int pos, isl_int *u)
4886 isl_int_neg(*u, bmap->ineq[ineq][0]);
4887 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4888 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4891 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4892 * does the corresponding lower bound have a fixed value in "bmap"?
4894 * In particular, "ineq" is of the form
4896 * f(x) + n d + c >= 0
4898 * with n > 0, c the constant term and
4899 * d the existentially quantified variable "div".
4900 * That is, the lower bound is
4902 * ceil((-f(x) - c)/n)
4904 * Look for a pair of constraints
4906 * f(x) + c0 >= 0
4907 * -f(x) + c1 >= 0
4909 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4910 * That is, check that
4912 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4914 * If so, return the index of inequality f(x) + c0 >= 0.
4915 * Otherwise, return bmap->n_ineq.
4916 * Return -1 on error.
4918 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4920 int i;
4921 int lower = -1, upper = -1;
4922 unsigned o_div;
4923 isl_int l, u;
4924 int equal;
4926 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4927 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4928 isl_bool par, opp;
4930 if (i == ineq)
4931 continue;
4932 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4933 continue;
4934 par = isl_bool_false;
4935 if (lower < 0)
4936 par = is_parallel_except(bmap, ineq, i, o_div + div);
4937 if (par < 0)
4938 return -1;
4939 if (par) {
4940 lower = i;
4941 continue;
4943 opp = isl_bool_false;
4944 if (upper < 0)
4945 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4946 if (opp < 0)
4947 return -1;
4948 if (opp)
4949 upper = i;
4952 if (lower < 0 || upper < 0)
4953 return bmap->n_ineq;
4955 isl_int_init(l);
4956 isl_int_init(u);
4958 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4959 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4961 equal = isl_int_eq(l, u);
4963 isl_int_clear(l);
4964 isl_int_clear(u);
4966 return equal ? lower : bmap->n_ineq;
4969 /* Given a lower bound constraint "ineq" on the existentially quantified
4970 * variable "div", such that the corresponding lower bound has
4971 * a fixed value in "bmap", assign this fixed value to the variable and
4972 * then try and drop redundant divs again,
4973 * freeing the temporary data structure "pairs" that was associated
4974 * to the old version of "bmap".
4975 * "lower" determines the constant value for the lower bound.
4977 * In particular, "ineq" is of the form
4979 * f(x) + n d + c >= 0,
4981 * while "lower" is of the form
4983 * f(x) + c0 >= 0
4985 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4986 * is ceil((c0 - c)/n).
4988 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4989 int div, int ineq, int lower, int *pairs)
4991 isl_int c;
4992 unsigned o_div;
4994 isl_int_init(c);
4996 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4997 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4998 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4999 free(pairs);
5001 isl_int_clear(c);
5003 return isl_basic_map_drop_redundant_divs(bmap);
5006 /* Do any of the integer divisions of "bmap" involve integer division "div"?
5008 * The integer division "div" could only ever appear in any later
5009 * integer division (with an explicit representation).
5011 static isl_bool any_div_involves_div(__isl_keep isl_basic_map *bmap, int div)
5013 int i;
5014 isl_size v_div, n_div;
5016 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5017 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5018 if (v_div < 0 || n_div < 0)
5019 return isl_bool_error;
5021 for (i = div + 1; i < n_div; ++i) {
5022 isl_bool unknown;
5024 unknown = isl_basic_map_div_is_marked_unknown(bmap, i);
5025 if (unknown < 0)
5026 return isl_bool_error;
5027 if (unknown)
5028 continue;
5029 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
5030 return isl_bool_true;
5033 return isl_bool_false;
5036 /* Remove divs that are not strictly needed based on the inequality
5037 * constraints.
5038 * In particular, if a div only occurs positively (or negatively)
5039 * in constraints, then it can simply be dropped.
5040 * Also, if a div occurs in only two constraints and if moreover
5041 * those two constraints are opposite to each other, except for the constant
5042 * term and if the sum of the constant terms is such that for any value
5043 * of the other values, there is always at least one integer value of the
5044 * div, i.e., if one plus this sum is greater than or equal to
5045 * the (absolute value) of the coefficient of the div in the constraints,
5046 * then we can also simply drop the div.
5048 * If an existentially quantified variable does not have an explicit
5049 * representation, appears in only a single lower bound that does not
5050 * involve any other such existentially quantified variables and appears
5051 * in this lower bound with coefficient 1,
5052 * then fix the variable to the value of the lower bound. That is,
5053 * turn the inequality into an equality.
5054 * If for any value of the other variables, there is any value
5055 * for the existentially quantified variable satisfying the constraints,
5056 * then this lower bound also satisfies the constraints.
5057 * It is therefore safe to pick this lower bound.
5059 * The same reasoning holds even if the coefficient is not one.
5060 * However, fixing the variable to the value of the lower bound may
5061 * in general introduce an extra integer division, in which case
5062 * it may be better to pick another value.
5063 * If this integer division has a known constant value, then plugging
5064 * in this constant value removes the existentially quantified variable
5065 * completely. In particular, if the lower bound is of the form
5066 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5067 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5068 * then the existentially quantified variable can be assigned this
5069 * shared value.
5071 * We skip divs that appear in equalities or in the definition of other divs.
5072 * Divs that appear in the definition of other divs usually occur in at least
5073 * 4 constraints, but the constraints may have been simplified.
5075 * If any divs are left after these simple checks then we move on
5076 * to more complicated cases in drop_more_redundant_divs.
5078 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
5079 __isl_take isl_basic_map *bmap)
5081 int i, j;
5082 isl_size off;
5083 int *pairs = NULL;
5084 int n = 0;
5085 isl_size n_ineq;
5087 if (!bmap)
5088 goto error;
5089 if (bmap->n_div == 0)
5090 return bmap;
5092 off = isl_basic_map_var_offset(bmap, isl_dim_div);
5093 if (off < 0)
5094 return isl_basic_map_free(bmap);
5095 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
5096 if (!pairs)
5097 goto error;
5099 n_ineq = isl_basic_map_n_inequality(bmap);
5100 if (n_ineq < 0)
5101 goto error;
5102 for (i = 0; i < bmap->n_div; ++i) {
5103 int pos, neg;
5104 int last_pos, last_neg;
5105 int redundant;
5106 int defined;
5107 isl_bool involves, opp, set_div;
5109 defined = !isl_int_is_zero(bmap->div[i][0]);
5110 involves = any_div_involves_div(bmap, i);
5111 if (involves < 0)
5112 goto error;
5113 if (involves)
5114 continue;
5115 for (j = 0; j < bmap->n_eq; ++j)
5116 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5117 break;
5118 if (j < bmap->n_eq)
5119 continue;
5120 ++n;
5121 pos = neg = 0;
5122 for (j = 0; j < bmap->n_ineq; ++j) {
5123 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5124 last_pos = j;
5125 ++pos;
5127 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5128 last_neg = j;
5129 ++neg;
5132 pairs[i] = pos * neg;
5133 if (pairs[i] == 0) {
5134 for (j = bmap->n_ineq - 1; j >= 0; --j)
5135 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5136 isl_basic_map_drop_inequality(bmap, j);
5137 bmap = isl_basic_map_drop_div(bmap, i);
5138 return drop_redundant_divs_again(bmap, pairs, 0);
5140 if (pairs[i] != 1)
5141 opp = isl_bool_false;
5142 else
5143 opp = is_opposite(bmap, last_pos, last_neg);
5144 if (opp < 0)
5145 goto error;
5146 if (!opp) {
5147 int lower;
5148 isl_bool single, one;
5150 if (pos != 1)
5151 continue;
5152 single = single_unknown(bmap, last_pos, i);
5153 if (single < 0)
5154 goto error;
5155 if (!single)
5156 continue;
5157 one = has_coef_one(bmap, i, last_pos);
5158 if (one < 0)
5159 goto error;
5160 if (one)
5161 return set_eq_and_try_again(bmap, last_pos,
5162 pairs);
5163 lower = lower_bound_is_cst(bmap, i, last_pos);
5164 if (lower < 0)
5165 goto error;
5166 if (lower < n_ineq)
5167 return fix_cst_lower(bmap, i, last_pos, lower,
5168 pairs);
5169 continue;
5172 isl_int_add(bmap->ineq[last_pos][0],
5173 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5174 isl_int_add_ui(bmap->ineq[last_pos][0],
5175 bmap->ineq[last_pos][0], 1);
5176 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5177 bmap->ineq[last_pos][1+off+i]);
5178 isl_int_sub_ui(bmap->ineq[last_pos][0],
5179 bmap->ineq[last_pos][0], 1);
5180 isl_int_sub(bmap->ineq[last_pos][0],
5181 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5182 if (redundant)
5183 return drop_div_and_try_again(bmap, i,
5184 last_pos, last_neg, pairs);
5185 if (defined)
5186 set_div = isl_bool_false;
5187 else
5188 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5189 if (set_div < 0)
5190 return isl_basic_map_free(bmap);
5191 if (set_div) {
5192 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5193 return drop_redundant_divs_again(bmap, pairs, 1);
5195 pairs[i] = 0;
5196 --n;
5199 if (n > 0)
5200 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5202 free(pairs);
5203 return bmap;
5204 error:
5205 free(pairs);
5206 isl_basic_map_free(bmap);
5207 return NULL;
5210 /* Consider the coefficients at "c" as a row vector and replace
5211 * them with their product with "T". "T" is assumed to be a square matrix.
5213 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5215 isl_size n;
5216 isl_ctx *ctx;
5217 isl_vec *v;
5219 n = isl_mat_rows(T);
5220 if (n < 0)
5221 return isl_stat_error;
5222 if (isl_seq_first_non_zero(c, n) == -1)
5223 return isl_stat_ok;
5224 ctx = isl_mat_get_ctx(T);
5225 v = isl_vec_alloc(ctx, n);
5226 if (!v)
5227 return isl_stat_error;
5228 isl_seq_swp_or_cpy(v->el, c, n);
5229 v = isl_vec_mat_product(v, isl_mat_copy(T));
5230 if (!v)
5231 return isl_stat_error;
5232 isl_seq_swp_or_cpy(c, v->el, n);
5233 isl_vec_free(v);
5235 return isl_stat_ok;
5238 /* Plug in T for the variables in "bmap" starting at "pos".
5239 * T is a linear unimodular matrix, i.e., without constant term.
5241 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5242 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5244 int i;
5245 isl_size n_row, n_col;
5247 bmap = isl_basic_map_cow(bmap);
5248 n_row = isl_mat_rows(T);
5249 n_col = isl_mat_cols(T);
5250 if (!bmap || n_row < 0 || n_col < 0)
5251 goto error;
5253 if (n_col != n_row)
5254 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5255 "expecting square matrix", goto error);
5257 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5258 goto error;
5260 for (i = 0; i < bmap->n_eq; ++i)
5261 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5262 goto error;
5263 for (i = 0; i < bmap->n_ineq; ++i)
5264 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5265 goto error;
5266 for (i = 0; i < bmap->n_div; ++i) {
5267 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5268 continue;
5269 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5270 goto error;
5273 isl_mat_free(T);
5274 return bmap;
5275 error:
5276 isl_basic_map_free(bmap);
5277 isl_mat_free(T);
5278 return NULL;
5281 /* Remove divs that are not strictly needed.
5283 * First look for an equality constraint involving two or more
5284 * existentially quantified variables without an explicit
5285 * representation. Replace the combination that appears
5286 * in the equality constraint by a single existentially quantified
5287 * variable such that the equality can be used to derive
5288 * an explicit representation for the variable.
5289 * If there are no more such equality constraints, then continue
5290 * with isl_basic_map_drop_redundant_divs_ineq.
5292 * In particular, if the equality constraint is of the form
5294 * f(x) + \sum_i c_i a_i = 0
5296 * with a_i existentially quantified variable without explicit
5297 * representation, then apply a transformation on the existentially
5298 * quantified variables to turn the constraint into
5300 * f(x) + g a_1' = 0
5302 * with g the gcd of the c_i.
5303 * In order to easily identify which existentially quantified variables
5304 * have a complete explicit representation, i.e., without being defined
5305 * in terms of other existentially quantified variables without
5306 * an explicit representation, the existentially quantified variables
5307 * are first sorted.
5309 * The variable transformation is computed by extending the row
5310 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5312 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5313 * [a_2'] [ a_2 ]
5314 * ... = U ....
5315 * [a_n'] [ a_n ]
5317 * with [c_1/g ... c_n/g] representing the first row of U.
5318 * The inverse of U is then plugged into the original constraints.
5319 * The call to isl_basic_map_simplify makes sure the explicit
5320 * representation for a_1' is extracted from the equality constraint.
5322 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5323 __isl_take isl_basic_map *bmap)
5325 int first;
5326 int i;
5327 unsigned o_div;
5328 isl_size n_div;
5329 int l;
5330 isl_ctx *ctx;
5331 isl_mat *T;
5333 if (!bmap)
5334 return NULL;
5335 if (isl_basic_map_divs_known(bmap))
5336 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5337 if (bmap->n_eq == 0)
5338 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5339 bmap = isl_basic_map_sort_divs(bmap);
5340 if (!bmap)
5341 return NULL;
5343 first = isl_basic_map_first_unknown_div(bmap);
5344 if (first < 0)
5345 return isl_basic_map_free(bmap);
5347 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5348 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5349 if (n_div < 0)
5350 return isl_basic_map_free(bmap);
5352 for (i = 0; i < bmap->n_eq; ++i) {
5353 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5354 n_div - (first));
5355 if (l < 0)
5356 continue;
5357 l += first;
5358 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5359 n_div - (l + 1)) == -1)
5360 continue;
5361 break;
5363 if (i >= bmap->n_eq)
5364 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5366 ctx = isl_basic_map_get_ctx(bmap);
5367 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5368 if (!T)
5369 return isl_basic_map_free(bmap);
5370 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5371 T = isl_mat_normalize_row(T, 0);
5372 T = isl_mat_unimodular_complete(T, 1);
5373 T = isl_mat_right_inverse(T);
5375 for (i = l; i < n_div; ++i)
5376 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5377 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5378 bmap = isl_basic_map_simplify(bmap);
5380 return isl_basic_map_drop_redundant_divs(bmap);
5383 /* Does "bmap" satisfy any equality that involves more than 2 variables
5384 * and/or has coefficients different from -1 and 1?
5386 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5388 int i;
5389 isl_size total;
5391 total = isl_basic_map_dim(bmap, isl_dim_all);
5392 if (total < 0)
5393 return isl_bool_error;
5395 for (i = 0; i < bmap->n_eq; ++i) {
5396 int j, k;
5398 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5399 if (j < 0)
5400 continue;
5401 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5402 !isl_int_is_negone(bmap->eq[i][1 + j]))
5403 return isl_bool_true;
5405 j += 1;
5406 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5407 if (k < 0)
5408 continue;
5409 j += k;
5410 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5411 !isl_int_is_negone(bmap->eq[i][1 + j]))
5412 return isl_bool_true;
5414 j += 1;
5415 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5416 if (k >= 0)
5417 return isl_bool_true;
5420 return isl_bool_false;
5423 /* Remove any common factor g from the constraint coefficients in "v".
5424 * The constant term is stored in the first position and is replaced
5425 * by floor(c/g). If any common factor is removed and if this results
5426 * in a tightening of the constraint, then set *tightened.
5428 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5429 int *tightened)
5431 isl_ctx *ctx;
5433 if (!v)
5434 return NULL;
5435 ctx = isl_vec_get_ctx(v);
5436 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5437 if (isl_int_is_zero(ctx->normalize_gcd))
5438 return v;
5439 if (isl_int_is_one(ctx->normalize_gcd))
5440 return v;
5441 v = isl_vec_cow(v);
5442 if (!v)
5443 return NULL;
5444 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5445 *tightened = 1;
5446 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5447 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5448 v->size - 1);
5449 return v;
5452 /* Internal representation used by isl_basic_map_reduce_coefficients.
5454 * "total" is the total dimensionality of the original basic map.
5455 * "v" is a temporary vector of size 1 + total that can be used
5456 * to store constraint coefficients.
5457 * "T" is the variable compression.
5458 * "T2" is the inverse transformation.
5459 * "tightened" is set if any constant term got tightened
5460 * while reducing the coefficients.
5462 struct isl_reduce_coefficients_data {
5463 isl_size total;
5464 isl_vec *v;
5465 isl_mat *T;
5466 isl_mat *T2;
5467 int tightened;
5470 /* Free all memory allocated in "data".
5472 static void isl_reduce_coefficients_data_clear(
5473 struct isl_reduce_coefficients_data *data)
5475 data->T = isl_mat_free(data->T);
5476 data->T2 = isl_mat_free(data->T2);
5477 data->v = isl_vec_free(data->v);
5480 /* Initialize "data" for "bmap", freeing all allocated memory
5481 * if anything goes wrong.
5483 * In particular, construct a variable compression
5484 * from the equality constraints of "bmap" and
5485 * allocate a temporary vector.
5487 static isl_stat isl_reduce_coefficients_data_init(
5488 __isl_keep isl_basic_map *bmap,
5489 struct isl_reduce_coefficients_data *data)
5491 isl_ctx *ctx;
5492 isl_mat *eq;
5494 data->v = NULL;
5495 data->T = NULL;
5496 data->T2 = NULL;
5497 data->tightened = 0;
5499 data->total = isl_basic_map_dim(bmap, isl_dim_all);
5500 if (data->total < 0)
5501 return isl_stat_error;
5502 ctx = isl_basic_map_get_ctx(bmap);
5503 data->v = isl_vec_alloc(ctx, 1 + data->total);
5504 if (!data->v)
5505 return isl_stat_error;
5507 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq,
5508 0, 1 + data->total);
5509 data->T = isl_mat_variable_compression(eq, &data->T2);
5510 if (!data->T || !data->T2)
5511 goto error;
5513 return isl_stat_ok;
5514 error:
5515 isl_reduce_coefficients_data_clear(data);
5516 return isl_stat_error;
5519 /* Reduce the coefficients of "bmap" by applying the variable compression
5520 * in "data".
5521 * In particular, apply the variable compression to each constraint,
5522 * factor out any common factor in the non-constant coefficients and
5523 * then apply the inverse of the compression.
5525 * Only apply the reduction on a single copy of the basic map
5526 * since the reduction may leave the result in an inconsistent state.
5527 * In particular, the constraints may not be gaussed.
5529 static __isl_give isl_basic_map *reduce_coefficients(
5530 __isl_take isl_basic_map *bmap,
5531 struct isl_reduce_coefficients_data *data)
5533 int i;
5534 isl_size total;
5536 total = isl_basic_map_dim(bmap, isl_dim_all);
5537 if (total < 0)
5538 return isl_basic_map_free(bmap);
5539 if (total != data->total)
5540 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
5541 "total dimensionality changed unexpectedly",
5542 return isl_basic_map_free(bmap));
5544 bmap = isl_basic_map_cow(bmap);
5545 if (!bmap)
5546 return NULL;
5548 for (i = 0; i < bmap->n_ineq; ++i) {
5549 isl_seq_cpy(data->v->el, bmap->ineq[i], 1 + data->total);
5550 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T));
5551 data->v = normalize_constraint(data->v, &data->tightened);
5552 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T2));
5553 if (!data->v)
5554 return isl_basic_map_free(bmap);
5555 isl_seq_cpy(bmap->ineq[i], data->v->el, 1 + data->total);
5558 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5560 return bmap;
5563 /* If "bmap" is an integer set that satisfies any equality involving
5564 * more than 2 variables and/or has coefficients different from -1 and 1,
5565 * then use variable compression to reduce the coefficients by removing
5566 * any (hidden) common factor.
5567 * In particular, apply the variable compression to each constraint,
5568 * factor out any common factor in the non-constant coefficients and
5569 * then apply the inverse of the compression.
5570 * At the end, we mark the basic map as having reduced constants.
5571 * If this flag is still set on the next invocation of this function,
5572 * then we skip the computation.
5574 * Removing a common factor may result in a tightening of some of
5575 * the constraints. If this happens, then we may end up with two
5576 * opposite inequalities that can be replaced by an equality.
5577 * We therefore call isl_basic_map_detect_inequality_pairs,
5578 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5579 * and isl_basic_map_gauss if such a pair was found.
5580 * This call to isl_basic_map_gauss may undo much of the effect
5581 * of the reduction on which isl_map_coalesce depends.
5582 * In particular, constraints in terms of (compressed) local variables
5583 * get reformulated in terms of the set variables again.
5584 * The reduction is therefore applied again afterwards.
5585 * This has to be done before the call to eliminate_divs_eq, however,
5586 * since that may remove some local variables, while
5587 * the data used during the reduction is formulated in terms
5588 * of the original variables.
5590 * Tightening may also result in some other constraints becoming
5591 * (rationally) redundant with respect to the tightened constraint
5592 * (in combination with other constraints). The basic map may
5593 * therefore no longer be assumed to have no redundant constraints.
5595 * Note that this function may leave the result in an inconsistent state.
5596 * In particular, the constraints may not be gaussed.
5597 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5598 * for some of the test cases to pass successfully.
5599 * Any potential modification of the representation is therefore only
5600 * performed on a single copy of the basic map.
5602 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5603 __isl_take isl_basic_map *bmap)
5605 struct isl_reduce_coefficients_data data;
5606 isl_bool multi;
5608 if (!bmap)
5609 return NULL;
5610 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5611 return bmap;
5612 if (isl_basic_map_is_rational(bmap))
5613 return bmap;
5614 if (bmap->n_eq == 0)
5615 return bmap;
5616 multi = has_multiple_var_equality(bmap);
5617 if (multi < 0)
5618 return isl_basic_map_free(bmap);
5619 if (!multi)
5620 return bmap;
5622 if (isl_reduce_coefficients_data_init(bmap, &data) < 0)
5623 return isl_basic_map_free(bmap);
5625 if (data.T->n_col == 0) {
5626 isl_reduce_coefficients_data_clear(&data);
5627 return isl_basic_map_set_to_empty(bmap);
5630 bmap = reduce_coefficients(bmap, &data);
5631 if (!bmap)
5632 goto error;
5634 if (data.tightened) {
5635 int progress = 0;
5637 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
5638 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5639 if (progress) {
5640 bmap = isl_basic_map_gauss(bmap, NULL);
5641 bmap = reduce_coefficients(bmap, &data);
5642 bmap = eliminate_divs_eq(bmap, &progress);
5646 isl_reduce_coefficients_data_clear(&data);
5648 return bmap;
5649 error:
5650 isl_reduce_coefficients_data_clear(&data);
5651 return isl_basic_map_free(bmap);
5654 /* Shift the integer division at position "div" of "bmap"
5655 * by "shift" times the variable at position "pos".
5656 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5657 * corresponds to the constant term.
5659 * That is, if the integer division has the form
5661 * floor(f(x)/d)
5663 * then replace it by
5665 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5667 __isl_give isl_basic_map *isl_basic_map_shift_div(
5668 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5670 int i;
5671 isl_size total, n_div;
5673 if (isl_int_is_zero(shift))
5674 return bmap;
5675 total = isl_basic_map_dim(bmap, isl_dim_all);
5676 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5677 total -= n_div;
5678 if (total < 0 || n_div < 0)
5679 return isl_basic_map_free(bmap);
5681 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5683 for (i = 0; i < bmap->n_eq; ++i) {
5684 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5685 continue;
5686 isl_int_submul(bmap->eq[i][pos],
5687 shift, bmap->eq[i][1 + total + div]);
5689 for (i = 0; i < bmap->n_ineq; ++i) {
5690 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5691 continue;
5692 isl_int_submul(bmap->ineq[i][pos],
5693 shift, bmap->ineq[i][1 + total + div]);
5695 for (i = 0; i < bmap->n_div; ++i) {
5696 if (isl_int_is_zero(bmap->div[i][0]))
5697 continue;
5698 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5699 continue;
5700 isl_int_submul(bmap->div[i][1 + pos],
5701 shift, bmap->div[i][1 + 1 + total + div]);
5704 return bmap;