isl_map.c: room_for_ineq: add memory management annotation
[isl.git] / isl_map_simplify.c
blobe73e81abe4c954f62a891985dc34666225af39c0
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map *bmap)
50 int i;
51 isl_int gcd;
52 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
54 if (total < 0)
55 return isl_basic_map_free(bmap);
57 isl_int_init(gcd);
58 for (i = bmap->n_eq - 1; i >= 0; --i) {
59 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60 if (isl_int_is_zero(gcd)) {
61 if (!isl_int_is_zero(bmap->eq[i][0])) {
62 bmap = isl_basic_map_set_to_empty(bmap);
63 break;
65 if (isl_basic_map_drop_equality(bmap, i) < 0)
66 goto error;
67 continue;
69 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
70 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
71 if (isl_int_is_one(gcd))
72 continue;
73 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
74 bmap = isl_basic_map_set_to_empty(bmap);
75 break;
77 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
80 for (i = bmap->n_ineq - 1; i >= 0; --i) {
81 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
82 if (isl_int_is_zero(gcd)) {
83 if (isl_int_is_neg(bmap->ineq[i][0])) {
84 bmap = isl_basic_map_set_to_empty(bmap);
85 break;
87 if (isl_basic_map_drop_inequality(bmap, i) < 0)
88 goto error;
89 continue;
91 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
92 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
93 if (isl_int_is_one(gcd))
94 continue;
95 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
96 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
98 isl_int_clear(gcd);
100 return bmap;
101 error:
102 isl_int_clear(gcd);
103 isl_basic_map_free(bmap);
104 return NULL;
107 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
108 __isl_take isl_basic_set *bset)
110 isl_basic_map *bmap = bset_to_bmap(bset);
111 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
114 /* Reduce the coefficient of the variable at position "pos"
115 * in integer division "div", such that it lies in the half-open
116 * interval (1/2,1/2], extracting any excess value from this integer division.
117 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
118 * corresponds to the constant term.
120 * That is, the integer division is of the form
122 * floor((... + (c * d + r) * x_pos + ...)/d)
124 * with -d < 2 * r <= d.
125 * Replace it by
127 * floor((... + r * x_pos + ...)/d) + c * x_pos
129 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
130 * Otherwise, c = floor((c * d + r)/d) + 1.
132 * This is the same normalization that is performed by isl_aff_floor.
134 static __isl_give isl_basic_map *reduce_coefficient_in_div(
135 __isl_take isl_basic_map *bmap, int div, int pos)
137 isl_int shift;
138 int add_one;
140 isl_int_init(shift);
141 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
142 isl_int_mul_ui(shift, shift, 2);
143 add_one = isl_int_gt(shift, bmap->div[div][0]);
144 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
145 if (add_one)
146 isl_int_add_ui(shift, shift, 1);
147 isl_int_neg(shift, shift);
148 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
149 isl_int_clear(shift);
151 return bmap;
154 /* Does the coefficient of the variable at position "pos"
155 * in integer division "div" need to be reduced?
156 * That is, does it lie outside the half-open interval (1/2,1/2]?
157 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
158 * 2 * c != d.
160 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
161 int pos)
163 isl_bool r;
165 if (isl_int_is_zero(bmap->div[div][1 + pos]))
166 return isl_bool_false;
168 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
169 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
170 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
171 isl_int_divexact_ui(bmap->div[div][1 + pos],
172 bmap->div[div][1 + pos], 2);
174 return r;
177 /* Reduce the coefficients (including the constant term) of
178 * integer division "div", if needed.
179 * In particular, make sure all coefficients lie in
180 * the half-open interval (1/2,1/2].
182 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
183 __isl_take isl_basic_map *bmap, int div)
185 int i;
186 isl_size total;
188 total = isl_basic_map_dim(bmap, isl_dim_all);
189 if (total < 0)
190 return isl_basic_map_free(bmap);
191 for (i = 0; i < 1 + total; ++i) {
192 isl_bool reduce;
194 reduce = needs_reduction(bmap, div, i);
195 if (reduce < 0)
196 return isl_basic_map_free(bmap);
197 if (!reduce)
198 continue;
199 bmap = reduce_coefficient_in_div(bmap, div, i);
200 if (!bmap)
201 break;
204 return bmap;
207 /* Reduce the coefficients (including the constant term) of
208 * the known integer divisions, if needed
209 * In particular, make sure all coefficients lie in
210 * the half-open interval (1/2,1/2].
212 static __isl_give isl_basic_map *reduce_div_coefficients(
213 __isl_take isl_basic_map *bmap)
215 int i;
217 if (!bmap)
218 return NULL;
219 if (bmap->n_div == 0)
220 return bmap;
222 for (i = 0; i < bmap->n_div; ++i) {
223 if (isl_int_is_zero(bmap->div[i][0]))
224 continue;
225 bmap = reduce_div_coefficients_of_div(bmap, i);
226 if (!bmap)
227 break;
230 return bmap;
233 /* Remove any common factor in numerator and denominator of the div expression,
234 * not taking into account the constant term.
235 * That is, if the div is of the form
237 * floor((a + m f(x))/(m d))
239 * then replace it by
241 * floor((floor(a/m) + f(x))/d)
243 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
244 * and can therefore not influence the result of the floor.
246 static __isl_give isl_basic_map *normalize_div_expression(
247 __isl_take isl_basic_map *bmap, int div)
249 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
250 isl_ctx *ctx = bmap->ctx;
252 if (total < 0)
253 return isl_basic_map_free(bmap);
254 if (isl_int_is_zero(bmap->div[div][0]))
255 return bmap;
256 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
257 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
258 if (isl_int_is_one(ctx->normalize_gcd))
259 return bmap;
260 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
261 ctx->normalize_gcd);
262 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
263 ctx->normalize_gcd);
264 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
265 ctx->normalize_gcd, total);
267 return bmap;
270 /* Remove any common factor in numerator and denominator of a div expression,
271 * not taking into account the constant term.
272 * That is, look for any div of the form
274 * floor((a + m f(x))/(m d))
276 * and replace it by
278 * floor((floor(a/m) + f(x))/d)
280 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
281 * and can therefore not influence the result of the floor.
283 static __isl_give isl_basic_map *normalize_div_expressions(
284 __isl_take isl_basic_map *bmap)
286 int i;
288 if (!bmap)
289 return NULL;
290 if (bmap->n_div == 0)
291 return bmap;
293 for (i = 0; i < bmap->n_div; ++i)
294 bmap = normalize_div_expression(bmap, i);
296 return bmap;
299 /* Assumes divs have been ordered if keep_divs is set.
301 static __isl_give isl_basic_map *eliminate_var_using_equality(
302 __isl_take isl_basic_map *bmap,
303 unsigned pos, isl_int *eq, int keep_divs, int *progress)
305 isl_size total;
306 isl_size v_div;
307 int k;
308 int last_div;
310 total = isl_basic_map_dim(bmap, isl_dim_all);
311 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
312 if (total < 0 || v_div < 0)
313 return isl_basic_map_free(bmap);
314 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
315 for (k = 0; k < bmap->n_eq; ++k) {
316 if (bmap->eq[k] == eq)
317 continue;
318 if (isl_int_is_zero(bmap->eq[k][1+pos]))
319 continue;
320 if (progress)
321 *progress = 1;
322 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
323 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
326 for (k = 0; k < bmap->n_ineq; ++k) {
327 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
328 continue;
329 if (progress)
330 *progress = 1;
331 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
332 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
333 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
334 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
337 for (k = 0; k < bmap->n_div; ++k) {
338 if (isl_int_is_zero(bmap->div[k][0]))
339 continue;
340 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
341 continue;
342 if (progress)
343 *progress = 1;
344 /* We need to be careful about circular definitions,
345 * so for now we just remove the definition of div k
346 * if the equality contains any divs.
347 * If keep_divs is set, then the divs have been ordered
348 * and we can keep the definition as long as the result
349 * is still ordered.
351 if (last_div == -1 || (keep_divs && last_div < k)) {
352 isl_seq_elim(bmap->div[k]+1, eq,
353 1+pos, 1+total, &bmap->div[k][0]);
354 bmap = normalize_div_expression(bmap, k);
355 if (!bmap)
356 return NULL;
357 } else
358 isl_seq_clr(bmap->div[k], 1 + total);
361 return bmap;
364 /* Assumes divs have been ordered if keep_divs is set.
366 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
367 isl_int *eq, unsigned div, int keep_divs)
369 isl_size v_div;
370 unsigned pos;
372 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
373 if (v_div < 0)
374 return isl_basic_map_free(bmap);
375 pos = v_div + div;
376 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
378 bmap = isl_basic_map_drop_div(bmap, div);
380 return bmap;
383 /* Check if elimination of div "div" using equality "eq" would not
384 * result in a div depending on a later div.
386 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
387 unsigned div)
389 int k;
390 int last_div;
391 isl_size v_div;
392 unsigned pos;
394 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
395 if (v_div < 0)
396 return isl_bool_error;
397 pos = v_div + div;
399 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
400 if (last_div < 0 || last_div <= div)
401 return isl_bool_true;
403 for (k = 0; k <= last_div; ++k) {
404 if (isl_int_is_zero(bmap->div[k][0]))
405 continue;
406 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
407 return isl_bool_false;
410 return isl_bool_true;
413 /* Eliminate divs based on equalities
415 static __isl_give isl_basic_map *eliminate_divs_eq(
416 __isl_take isl_basic_map *bmap, int *progress)
418 int d;
419 int i;
420 int modified = 0;
421 unsigned off;
423 bmap = isl_basic_map_order_divs(bmap);
425 if (!bmap)
426 return NULL;
428 off = isl_basic_map_offset(bmap, isl_dim_div);
430 for (d = bmap->n_div - 1; d >= 0 ; --d) {
431 for (i = 0; i < bmap->n_eq; ++i) {
432 isl_bool ok;
434 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
435 !isl_int_is_negone(bmap->eq[i][off + d]))
436 continue;
437 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
438 if (ok < 0)
439 return isl_basic_map_free(bmap);
440 if (!ok)
441 continue;
442 modified = 1;
443 *progress = 1;
444 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
445 if (isl_basic_map_drop_equality(bmap, i) < 0)
446 return isl_basic_map_free(bmap);
447 break;
450 if (modified)
451 return eliminate_divs_eq(bmap, progress);
452 return bmap;
455 /* Eliminate divs based on inequalities
457 static __isl_give isl_basic_map *eliminate_divs_ineq(
458 __isl_take isl_basic_map *bmap, int *progress)
460 int d;
461 int i;
462 unsigned off;
463 struct isl_ctx *ctx;
465 if (!bmap)
466 return NULL;
468 ctx = bmap->ctx;
469 off = isl_basic_map_offset(bmap, isl_dim_div);
471 for (d = bmap->n_div - 1; d >= 0 ; --d) {
472 for (i = 0; i < bmap->n_eq; ++i)
473 if (!isl_int_is_zero(bmap->eq[i][off + d]))
474 break;
475 if (i < bmap->n_eq)
476 continue;
477 for (i = 0; i < bmap->n_ineq; ++i)
478 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
479 break;
480 if (i < bmap->n_ineq)
481 continue;
482 *progress = 1;
483 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
484 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
485 break;
486 bmap = isl_basic_map_drop_div(bmap, d);
487 if (!bmap)
488 break;
490 return bmap;
493 /* Does the equality constraint at position "eq" in "bmap" involve
494 * any local variables in the range [first, first + n)
495 * that are not marked as having an explicit representation?
497 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
498 int eq, unsigned first, unsigned n)
500 unsigned o_div;
501 int i;
503 if (!bmap)
504 return isl_bool_error;
506 o_div = isl_basic_map_offset(bmap, isl_dim_div);
507 for (i = 0; i < n; ++i) {
508 isl_bool unknown;
510 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
511 continue;
512 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
513 if (unknown < 0)
514 return isl_bool_error;
515 if (unknown)
516 return isl_bool_true;
519 return isl_bool_false;
522 /* The last local variable involved in the equality constraint
523 * at position "eq" in "bmap" is the local variable at position "div".
524 * It can therefore be used to extract an explicit representation
525 * for that variable.
526 * Do so unless the local variable already has an explicit representation or
527 * the explicit representation would involve any other local variables
528 * that in turn do not have an explicit representation.
529 * An equality constraint involving local variables without an explicit
530 * representation can be used in isl_basic_map_drop_redundant_divs
531 * to separate out an independent local variable. Introducing
532 * an explicit representation here would block this transformation,
533 * while the partial explicit representation in itself is not very useful.
534 * Set *progress if anything is changed.
536 * The equality constraint is of the form
538 * f(x) + n e >= 0
540 * with n a positive number. The explicit representation derived from
541 * this constraint is
543 * floor((-f(x))/n)
545 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
546 int div, int eq, int *progress)
548 isl_size total;
549 unsigned o_div;
550 isl_bool involves;
552 if (!bmap)
553 return NULL;
555 if (!isl_int_is_zero(bmap->div[div][0]))
556 return bmap;
558 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
559 if (involves < 0)
560 return isl_basic_map_free(bmap);
561 if (involves)
562 return bmap;
564 total = isl_basic_map_dim(bmap, isl_dim_all);
565 if (total < 0)
566 return isl_basic_map_free(bmap);
567 o_div = isl_basic_map_offset(bmap, isl_dim_div);
568 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
569 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
570 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
571 if (progress)
572 *progress = 1;
574 return bmap;
577 /* Perform fangcheng (Gaussian elimination) on the equality
578 * constraints of "bmap".
579 * That is, put them into row-echelon form, starting from the last column
580 * backward and use them to eliminate the corresponding coefficients
581 * from all constraints.
583 * If "progress" is not NULL, then it gets set if the elimination
584 * result in any changes.
585 * The elimination process may result in some equality constraints
586 * getting interchanged or removed.
587 * If "swap" or "drop" are not NULL, then they get called when
588 * two equality constraints get interchanged or
589 * when a number of final equality constraints get removed.
590 * As a special case, if the input turns out to be empty,
591 * then drop gets called with the number of removed equality
592 * constraints set to the total number of equality constraints.
593 * If "swap" or "drop" are not NULL, then the local variables (if any)
594 * are assumed to be in a valid order.
596 __isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
597 int *progress,
598 isl_stat (*swap)(unsigned a, unsigned b, void *user),
599 isl_stat (*drop)(unsigned n, void *user), void *user)
601 int k;
602 int done;
603 int last_var;
604 unsigned total_var;
605 isl_size total;
606 unsigned n_drop;
608 if (!swap && !drop)
609 bmap = isl_basic_map_order_divs(bmap);
611 total = isl_basic_map_dim(bmap, isl_dim_all);
612 if (total < 0)
613 return isl_basic_map_free(bmap);
615 total_var = total - bmap->n_div;
617 last_var = total - 1;
618 for (done = 0; done < bmap->n_eq; ++done) {
619 for (; last_var >= 0; --last_var) {
620 for (k = done; k < bmap->n_eq; ++k)
621 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
622 break;
623 if (k < bmap->n_eq)
624 break;
626 if (last_var < 0)
627 break;
628 if (k != done) {
629 swap_equality(bmap, k, done);
630 if (swap && swap(k, done, user) < 0)
631 return isl_basic_map_free(bmap);
633 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
634 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
636 bmap = eliminate_var_using_equality(bmap, last_var,
637 bmap->eq[done], 1, progress);
639 if (last_var >= total_var)
640 bmap = set_div_from_eq(bmap, last_var - total_var,
641 done, progress);
642 if (!bmap)
643 return NULL;
645 if (done == bmap->n_eq)
646 return bmap;
647 for (k = done; k < bmap->n_eq; ++k) {
648 if (isl_int_is_zero(bmap->eq[k][0]))
649 continue;
650 if (drop && drop(bmap->n_eq, user) < 0)
651 return isl_basic_map_free(bmap);
652 return isl_basic_map_set_to_empty(bmap);
654 n_drop = bmap->n_eq - done;
655 bmap = isl_basic_map_free_equality(bmap, n_drop);
656 if (drop && drop(n_drop, user) < 0)
657 return isl_basic_map_free(bmap);
658 return bmap;
661 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
662 int *progress)
664 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
667 __isl_give isl_basic_set *isl_basic_set_gauss(
668 __isl_take isl_basic_set *bset, int *progress)
670 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
671 progress));
675 static unsigned int round_up(unsigned int v)
677 int old_v = v;
679 while (v) {
680 old_v = v;
681 v ^= v & -v;
683 return old_v << 1;
686 /* Hash table of inequalities in a basic map.
687 * "index" is an array of addresses of inequalities in the basic map, some
688 * of which are NULL. The inequalities are hashed on the coefficients
689 * except the constant term.
690 * "size" is the number of elements in the array and is always a power of two
691 * "bits" is the number of bits need to represent an index into the array.
692 * "total" is the total dimension of the basic map.
694 struct isl_constraint_index {
695 unsigned int size;
696 int bits;
697 isl_int ***index;
698 isl_size total;
701 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
703 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
704 __isl_keep isl_basic_map *bmap)
706 isl_ctx *ctx;
708 ci->index = NULL;
709 if (!bmap)
710 return isl_stat_error;
711 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
712 if (ci->total < 0)
713 return isl_stat_error;
714 if (bmap->n_ineq == 0)
715 return isl_stat_ok;
716 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
717 ci->bits = ffs(ci->size) - 1;
718 ctx = isl_basic_map_get_ctx(bmap);
719 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
720 if (!ci->index)
721 return isl_stat_error;
723 return isl_stat_ok;
726 /* Free the memory allocated by create_constraint_index.
728 static void constraint_index_free(struct isl_constraint_index *ci)
730 free(ci->index);
733 /* Return the position in ci->index that contains the address of
734 * an inequality that is equal to *ineq up to the constant term,
735 * provided this address is not identical to "ineq".
736 * If there is no such inequality, then return the position where
737 * such an inequality should be inserted.
739 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
741 int h;
742 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
743 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
744 if (ineq != ci->index[h] &&
745 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
746 break;
747 return h;
750 /* Return the position in ci->index that contains the address of
751 * an inequality that is equal to the k'th inequality of "bmap"
752 * up to the constant term, provided it does not point to the very
753 * same inequality.
754 * If there is no such inequality, then return the position where
755 * such an inequality should be inserted.
757 static int hash_index(struct isl_constraint_index *ci,
758 __isl_keep isl_basic_map *bmap, int k)
760 return hash_index_ineq(ci, &bmap->ineq[k]);
763 static int set_hash_index(struct isl_constraint_index *ci,
764 __isl_keep isl_basic_set *bset, int k)
766 return hash_index(ci, bset, k);
769 /* Fill in the "ci" data structure with the inequalities of "bset".
771 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
772 __isl_keep isl_basic_set *bset)
774 int k, h;
776 if (create_constraint_index(ci, bset) < 0)
777 return isl_stat_error;
779 for (k = 0; k < bset->n_ineq; ++k) {
780 h = set_hash_index(ci, bset, k);
781 ci->index[h] = &bset->ineq[k];
784 return isl_stat_ok;
787 /* Is the inequality ineq (obviously) redundant with respect
788 * to the constraints in "ci"?
790 * Look for an inequality in "ci" with the same coefficients and then
791 * check if the contant term of "ineq" is greater than or equal
792 * to the constant term of that inequality. If so, "ineq" is clearly
793 * redundant.
795 * Note that hash_index_ineq ignores a stored constraint if it has
796 * the same address as the passed inequality. It is ok to pass
797 * the address of a local variable here since it will never be
798 * the same as the address of a constraint in "ci".
800 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
801 isl_int *ineq)
803 int h;
805 h = hash_index_ineq(ci, &ineq);
806 if (!ci->index[h])
807 return isl_bool_false;
808 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
811 /* If we can eliminate more than one div, then we need to make
812 * sure we do it from last div to first div, in order not to
813 * change the position of the other divs that still need to
814 * be removed.
816 static __isl_give isl_basic_map *remove_duplicate_divs(
817 __isl_take isl_basic_map *bmap, int *progress)
819 unsigned int size;
820 int *index;
821 int *elim_for;
822 int k, l, h;
823 int bits;
824 struct isl_blk eq;
825 isl_size v_div;
826 unsigned total;
827 struct isl_ctx *ctx;
829 bmap = isl_basic_map_order_divs(bmap);
830 if (!bmap || bmap->n_div <= 1)
831 return bmap;
833 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
834 if (v_div < 0)
835 return isl_basic_map_free(bmap);
836 total = v_div + bmap->n_div;
838 ctx = bmap->ctx;
839 for (k = bmap->n_div - 1; k >= 0; --k)
840 if (!isl_int_is_zero(bmap->div[k][0]))
841 break;
842 if (k <= 0)
843 return bmap;
845 size = round_up(4 * bmap->n_div / 3 - 1);
846 if (size == 0)
847 return bmap;
848 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
849 bits = ffs(size) - 1;
850 index = isl_calloc_array(ctx, int, size);
851 if (!elim_for || !index)
852 goto out;
853 eq = isl_blk_alloc(ctx, 1+total);
854 if (isl_blk_is_error(eq))
855 goto out;
857 isl_seq_clr(eq.data, 1+total);
858 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
859 for (--k; k >= 0; --k) {
860 uint32_t hash;
862 if (isl_int_is_zero(bmap->div[k][0]))
863 continue;
865 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
866 for (h = hash; index[h]; h = (h+1) % size)
867 if (isl_seq_eq(bmap->div[k],
868 bmap->div[index[h]-1], 2+total))
869 break;
870 if (index[h]) {
871 *progress = 1;
872 l = index[h] - 1;
873 elim_for[l] = k + 1;
875 index[h] = k+1;
877 for (l = bmap->n_div - 1; l >= 0; --l) {
878 if (!elim_for[l])
879 continue;
880 k = elim_for[l] - 1;
881 isl_int_set_si(eq.data[1 + v_div + k], -1);
882 isl_int_set_si(eq.data[1 + v_div + l], 1);
883 bmap = eliminate_div(bmap, eq.data, l, 1);
884 if (!bmap)
885 break;
886 isl_int_set_si(eq.data[1 + v_div + k], 0);
887 isl_int_set_si(eq.data[1 + v_div + l], 0);
890 isl_blk_free(ctx, eq);
891 out:
892 free(index);
893 free(elim_for);
894 return bmap;
897 static int n_pure_div_eq(struct isl_basic_map *bmap)
899 int i, j;
900 isl_size v_div;
902 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
903 if (v_div < 0)
904 return -1;
905 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
906 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
907 --j;
908 if (j < 0)
909 break;
910 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
911 return 0;
913 return i;
916 /* Normalize divs that appear in equalities.
918 * In particular, we assume that bmap contains some equalities
919 * of the form
921 * a x = m * e_i
923 * and we want to replace the set of e_i by a minimal set and
924 * such that the new e_i have a canonical representation in terms
925 * of the vector x.
926 * If any of the equalities involves more than one divs, then
927 * we currently simply bail out.
929 * Let us first additionally assume that all equalities involve
930 * a div. The equalities then express modulo constraints on the
931 * remaining variables and we can use "parameter compression"
932 * to find a minimal set of constraints. The result is a transformation
934 * x = T(x') = x_0 + G x'
936 * with G a lower-triangular matrix with all elements below the diagonal
937 * non-negative and smaller than the diagonal element on the same row.
938 * We first normalize x_0 by making the same property hold in the affine
939 * T matrix.
940 * The rows i of G with a 1 on the diagonal do not impose any modulo
941 * constraint and simply express x_i = x'_i.
942 * For each of the remaining rows i, we introduce a div and a corresponding
943 * equality. In particular
945 * g_ii e_j = x_i - g_i(x')
947 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
948 * corresponding div (if g_kk != 1).
950 * If there are any equalities not involving any div, then we
951 * first apply a variable compression on the variables x:
953 * x = C x'' x'' = C_2 x
955 * and perform the above parameter compression on A C instead of on A.
956 * The resulting compression is then of the form
958 * x'' = T(x') = x_0 + G x'
960 * and in constructing the new divs and the corresponding equalities,
961 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
962 * by the corresponding row from C_2.
964 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
965 int *progress)
967 int i, j, k;
968 isl_size v_div;
969 int div_eq;
970 struct isl_mat *B;
971 struct isl_vec *d;
972 struct isl_mat *T = NULL;
973 struct isl_mat *C = NULL;
974 struct isl_mat *C2 = NULL;
975 isl_int v;
976 int *pos = NULL;
977 int dropped, needed;
979 if (!bmap)
980 return NULL;
982 if (bmap->n_div == 0)
983 return bmap;
985 if (bmap->n_eq == 0)
986 return bmap;
988 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
989 return bmap;
991 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
992 div_eq = n_pure_div_eq(bmap);
993 if (v_div < 0 || div_eq < 0)
994 return isl_basic_map_free(bmap);
995 if (div_eq == 0)
996 return bmap;
998 if (div_eq < bmap->n_eq) {
999 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1000 bmap->n_eq - div_eq, 0, 1 + v_div);
1001 C = isl_mat_variable_compression(B, &C2);
1002 if (!C || !C2)
1003 goto error;
1004 if (C->n_col == 0) {
1005 bmap = isl_basic_map_set_to_empty(bmap);
1006 isl_mat_free(C);
1007 isl_mat_free(C2);
1008 goto done;
1012 d = isl_vec_alloc(bmap->ctx, div_eq);
1013 if (!d)
1014 goto error;
1015 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1016 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1017 --j;
1018 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1020 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1022 if (C) {
1023 B = isl_mat_product(B, C);
1024 C = NULL;
1027 T = isl_mat_parameter_compression(B, d);
1028 if (!T)
1029 goto error;
1030 if (T->n_col == 0) {
1031 bmap = isl_basic_map_set_to_empty(bmap);
1032 isl_mat_free(C2);
1033 isl_mat_free(T);
1034 goto done;
1036 isl_int_init(v);
1037 for (i = 0; i < T->n_row - 1; ++i) {
1038 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1039 if (isl_int_is_zero(v))
1040 continue;
1041 isl_mat_col_submul(T, 0, v, 1 + i);
1043 isl_int_clear(v);
1044 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1045 if (!pos)
1046 goto error;
1047 /* We have to be careful because dropping equalities may reorder them */
1048 dropped = 0;
1049 for (j = bmap->n_div - 1; j >= 0; --j) {
1050 for (i = 0; i < bmap->n_eq; ++i)
1051 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1052 break;
1053 if (i < bmap->n_eq) {
1054 bmap = isl_basic_map_drop_div(bmap, j);
1055 if (isl_basic_map_drop_equality(bmap, i) < 0)
1056 goto error;
1057 ++dropped;
1060 pos[0] = 0;
1061 needed = 0;
1062 for (i = 1; i < T->n_row; ++i) {
1063 if (isl_int_is_one(T->row[i][i]))
1064 pos[i] = i;
1065 else
1066 needed++;
1068 if (needed > dropped) {
1069 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1070 if (!bmap)
1071 goto error;
1073 for (i = 1; i < T->n_row; ++i) {
1074 if (isl_int_is_one(T->row[i][i]))
1075 continue;
1076 k = isl_basic_map_alloc_div(bmap);
1077 pos[i] = 1 + v_div + k;
1078 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1079 isl_int_set(bmap->div[k][0], T->row[i][i]);
1080 if (C2)
1081 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1082 else
1083 isl_int_set_si(bmap->div[k][1 + i], 1);
1084 for (j = 0; j < i; ++j) {
1085 if (isl_int_is_zero(T->row[i][j]))
1086 continue;
1087 if (pos[j] < T->n_row && C2)
1088 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1089 C2->row[pos[j]], 1 + v_div);
1090 else
1091 isl_int_neg(bmap->div[k][1 + pos[j]],
1092 T->row[i][j]);
1094 j = isl_basic_map_alloc_equality(bmap);
1095 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1096 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1098 free(pos);
1099 isl_mat_free(C2);
1100 isl_mat_free(T);
1102 if (progress)
1103 *progress = 1;
1104 done:
1105 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1107 return bmap;
1108 error:
1109 free(pos);
1110 isl_mat_free(C);
1111 isl_mat_free(C2);
1112 isl_mat_free(T);
1113 isl_basic_map_free(bmap);
1114 return NULL;
1117 static __isl_give isl_basic_map *set_div_from_lower_bound(
1118 __isl_take isl_basic_map *bmap, int div, int ineq)
1120 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1122 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1123 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1124 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1125 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1126 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1128 return bmap;
1131 /* Check whether it is ok to define a div based on an inequality.
1132 * To avoid the introduction of circular definitions of divs, we
1133 * do not allow such a definition if the resulting expression would refer to
1134 * any other undefined divs or if any known div is defined in
1135 * terms of the unknown div.
1137 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1138 int div, int ineq)
1140 int j;
1141 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1143 /* Not defined in terms of unknown divs */
1144 for (j = 0; j < bmap->n_div; ++j) {
1145 if (div == j)
1146 continue;
1147 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1148 continue;
1149 if (isl_int_is_zero(bmap->div[j][0]))
1150 return isl_bool_false;
1153 /* No other div defined in terms of this one => avoid loops */
1154 for (j = 0; j < bmap->n_div; ++j) {
1155 if (div == j)
1156 continue;
1157 if (isl_int_is_zero(bmap->div[j][0]))
1158 continue;
1159 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1160 return isl_bool_false;
1163 return isl_bool_true;
1166 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1167 * be a better expression than the current one?
1169 * If we do not have any expression yet, then any expression would be better.
1170 * Otherwise we check if the last variable involved in the inequality
1171 * (disregarding the div that it would define) is in an earlier position
1172 * than the last variable involved in the current div expression.
1174 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1175 int div, int ineq)
1177 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1178 int last_div;
1179 int last_ineq;
1181 if (isl_int_is_zero(bmap->div[div][0]))
1182 return isl_bool_true;
1184 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1185 bmap->n_div - (div + 1)) >= 0)
1186 return isl_bool_false;
1188 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1189 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1190 total + bmap->n_div);
1192 return last_ineq < last_div;
1195 /* Given two constraints "k" and "l" that are opposite to each other,
1196 * except for the constant term, check if we can use them
1197 * to obtain an expression for one of the hitherto unknown divs or
1198 * a "better" expression for a div for which we already have an expression.
1199 * "sum" is the sum of the constant terms of the constraints.
1200 * If this sum is strictly smaller than the coefficient of one
1201 * of the divs, then this pair can be used define the div.
1202 * To avoid the introduction of circular definitions of divs, we
1203 * do not use the pair if the resulting expression would refer to
1204 * any other undefined divs or if any known div is defined in
1205 * terms of the unknown div.
1207 static __isl_give isl_basic_map *check_for_div_constraints(
1208 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1209 int *progress)
1211 int i;
1212 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1214 for (i = 0; i < bmap->n_div; ++i) {
1215 isl_bool set_div;
1217 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1218 continue;
1219 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1220 continue;
1221 set_div = better_div_constraint(bmap, i, k);
1222 if (set_div >= 0 && set_div)
1223 set_div = ok_to_set_div_from_bound(bmap, i, k);
1224 if (set_div < 0)
1225 return isl_basic_map_free(bmap);
1226 if (!set_div)
1227 break;
1228 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1229 bmap = set_div_from_lower_bound(bmap, i, k);
1230 else
1231 bmap = set_div_from_lower_bound(bmap, i, l);
1232 if (progress)
1233 *progress = 1;
1234 break;
1236 return bmap;
1239 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1240 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1242 struct isl_constraint_index ci;
1243 int k, l, h;
1244 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1245 isl_int sum;
1247 if (total < 0 || bmap->n_ineq <= 1)
1248 return bmap;
1250 if (create_constraint_index(&ci, bmap) < 0)
1251 return bmap;
1253 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1254 ci.index[h] = &bmap->ineq[0];
1255 for (k = 1; k < bmap->n_ineq; ++k) {
1256 h = hash_index(&ci, bmap, k);
1257 if (!ci.index[h]) {
1258 ci.index[h] = &bmap->ineq[k];
1259 continue;
1261 if (progress)
1262 *progress = 1;
1263 l = ci.index[h] - &bmap->ineq[0];
1264 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1265 swap_inequality(bmap, k, l);
1266 isl_basic_map_drop_inequality(bmap, k);
1267 --k;
1269 isl_int_init(sum);
1270 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1271 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1272 h = hash_index(&ci, bmap, k);
1273 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1274 if (!ci.index[h])
1275 continue;
1276 l = ci.index[h] - &bmap->ineq[0];
1277 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1278 if (isl_int_is_pos(sum)) {
1279 if (detect_divs)
1280 bmap = check_for_div_constraints(bmap, k, l,
1281 sum, progress);
1282 continue;
1284 if (isl_int_is_zero(sum)) {
1285 /* We need to break out of the loop after these
1286 * changes since the contents of the hash
1287 * will no longer be valid.
1288 * Plus, we probably we want to regauss first.
1290 if (progress)
1291 *progress = 1;
1292 isl_basic_map_drop_inequality(bmap, l);
1293 isl_basic_map_inequality_to_equality(bmap, k);
1294 } else
1295 bmap = isl_basic_map_set_to_empty(bmap);
1296 break;
1298 isl_int_clear(sum);
1300 constraint_index_free(&ci);
1301 return bmap;
1304 /* Detect all pairs of inequalities that form an equality.
1306 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1307 * Call it repeatedly while it is making progress.
1309 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1310 __isl_take isl_basic_map *bmap, int *progress)
1312 int duplicate;
1314 do {
1315 duplicate = 0;
1316 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1317 &duplicate, 0);
1318 if (progress && duplicate)
1319 *progress = 1;
1320 } while (duplicate);
1322 return bmap;
1325 /* Eliminate knowns divs from constraints where they appear with
1326 * a (positive or negative) unit coefficient.
1328 * That is, replace
1330 * floor(e/m) + f >= 0
1332 * by
1334 * e + m f >= 0
1336 * and
1338 * -floor(e/m) + f >= 0
1340 * by
1342 * -e + m f + m - 1 >= 0
1344 * The first conversion is valid because floor(e/m) >= -f is equivalent
1345 * to e/m >= -f because -f is an integral expression.
1346 * The second conversion follows from the fact that
1348 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1351 * Note that one of the div constraints may have been eliminated
1352 * due to being redundant with respect to the constraint that is
1353 * being modified by this function. The modified constraint may
1354 * no longer imply this div constraint, so we add it back to make
1355 * sure we do not lose any information.
1357 * We skip integral divs, i.e., those with denominator 1, as we would
1358 * risk eliminating the div from the div constraints. We do not need
1359 * to handle those divs here anyway since the div constraints will turn
1360 * out to form an equality and this equality can then be used to eliminate
1361 * the div from all constraints.
1363 static __isl_give isl_basic_map *eliminate_unit_divs(
1364 __isl_take isl_basic_map *bmap, int *progress)
1366 int i, j;
1367 isl_ctx *ctx;
1368 unsigned total;
1370 if (!bmap)
1371 return NULL;
1373 ctx = isl_basic_map_get_ctx(bmap);
1374 total = isl_basic_map_offset(bmap, isl_dim_div);
1376 for (i = 0; i < bmap->n_div; ++i) {
1377 if (isl_int_is_zero(bmap->div[i][0]))
1378 continue;
1379 if (isl_int_is_one(bmap->div[i][0]))
1380 continue;
1381 for (j = 0; j < bmap->n_ineq; ++j) {
1382 int s;
1384 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1385 !isl_int_is_negone(bmap->ineq[j][total + i]))
1386 continue;
1388 *progress = 1;
1390 s = isl_int_sgn(bmap->ineq[j][total + i]);
1391 isl_int_set_si(bmap->ineq[j][total + i], 0);
1392 if (s < 0)
1393 isl_seq_combine(bmap->ineq[j],
1394 ctx->negone, bmap->div[i] + 1,
1395 bmap->div[i][0], bmap->ineq[j],
1396 total + bmap->n_div);
1397 else
1398 isl_seq_combine(bmap->ineq[j],
1399 ctx->one, bmap->div[i] + 1,
1400 bmap->div[i][0], bmap->ineq[j],
1401 total + bmap->n_div);
1402 if (s < 0) {
1403 isl_int_add(bmap->ineq[j][0],
1404 bmap->ineq[j][0], bmap->div[i][0]);
1405 isl_int_sub_ui(bmap->ineq[j][0],
1406 bmap->ineq[j][0], 1);
1409 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1410 bmap = isl_basic_map_add_div_constraint(bmap, i, s);
1411 if (!bmap)
1412 return NULL;
1416 return bmap;
1419 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1421 int progress = 1;
1422 if (!bmap)
1423 return NULL;
1424 while (progress) {
1425 isl_bool empty;
1427 progress = 0;
1428 empty = isl_basic_map_plain_is_empty(bmap);
1429 if (empty < 0)
1430 return isl_basic_map_free(bmap);
1431 if (empty)
1432 break;
1433 bmap = isl_basic_map_normalize_constraints(bmap);
1434 bmap = reduce_div_coefficients(bmap);
1435 bmap = normalize_div_expressions(bmap);
1436 bmap = remove_duplicate_divs(bmap, &progress);
1437 bmap = eliminate_unit_divs(bmap, &progress);
1438 bmap = eliminate_divs_eq(bmap, &progress);
1439 bmap = eliminate_divs_ineq(bmap, &progress);
1440 bmap = isl_basic_map_gauss(bmap, &progress);
1441 /* requires equalities in normal form */
1442 bmap = normalize_divs(bmap, &progress);
1443 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1444 &progress, 1);
1445 if (bmap && progress)
1446 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1448 return bmap;
1451 __isl_give isl_basic_set *isl_basic_set_simplify(
1452 __isl_take isl_basic_set *bset)
1454 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1458 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1459 isl_int *constraint, unsigned div)
1461 unsigned pos;
1463 if (!bmap)
1464 return isl_bool_error;
1466 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1468 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1469 int neg;
1470 isl_int_sub(bmap->div[div][1],
1471 bmap->div[div][1], bmap->div[div][0]);
1472 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1473 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1474 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1475 isl_int_add(bmap->div[div][1],
1476 bmap->div[div][1], bmap->div[div][0]);
1477 if (!neg)
1478 return isl_bool_false;
1479 if (isl_seq_first_non_zero(constraint+pos+1,
1480 bmap->n_div-div-1) != -1)
1481 return isl_bool_false;
1482 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1483 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1484 return isl_bool_false;
1485 if (isl_seq_first_non_zero(constraint+pos+1,
1486 bmap->n_div-div-1) != -1)
1487 return isl_bool_false;
1488 } else
1489 return isl_bool_false;
1491 return isl_bool_true;
1494 /* If the only constraints a div d=floor(f/m)
1495 * appears in are its two defining constraints
1497 * f - m d >=0
1498 * -(f - (m - 1)) + m d >= 0
1500 * then it can safely be removed.
1502 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1504 int i;
1505 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1506 unsigned pos = 1 + v_div + div;
1508 if (v_div < 0)
1509 return isl_bool_error;
1511 for (i = 0; i < bmap->n_eq; ++i)
1512 if (!isl_int_is_zero(bmap->eq[i][pos]))
1513 return isl_bool_false;
1515 for (i = 0; i < bmap->n_ineq; ++i) {
1516 isl_bool red;
1518 if (isl_int_is_zero(bmap->ineq[i][pos]))
1519 continue;
1520 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1521 if (red < 0 || !red)
1522 return red;
1525 for (i = 0; i < bmap->n_div; ++i) {
1526 if (isl_int_is_zero(bmap->div[i][0]))
1527 continue;
1528 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1529 return isl_bool_false;
1532 return isl_bool_true;
1536 * Remove divs that don't occur in any of the constraints or other divs.
1537 * These can arise when dropping constraints from a basic map or
1538 * when the divs of a basic map have been temporarily aligned
1539 * with the divs of another basic map.
1541 static __isl_give isl_basic_map *remove_redundant_divs(
1542 __isl_take isl_basic_map *bmap)
1544 int i;
1545 isl_size v_div;
1547 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1548 if (v_div < 0)
1549 return isl_basic_map_free(bmap);
1551 for (i = bmap->n_div-1; i >= 0; --i) {
1552 isl_bool redundant;
1554 redundant = div_is_redundant(bmap, i);
1555 if (redundant < 0)
1556 return isl_basic_map_free(bmap);
1557 if (!redundant)
1558 continue;
1559 bmap = isl_basic_map_drop_constraints_involving(bmap,
1560 v_div + i, 1);
1561 bmap = isl_basic_map_drop_div(bmap, i);
1563 return bmap;
1566 /* Mark "bmap" as final, without checking for obviously redundant
1567 * integer divisions. This function should be used when "bmap"
1568 * is known not to involve any such integer divisions.
1570 __isl_give isl_basic_map *isl_basic_map_mark_final(
1571 __isl_take isl_basic_map *bmap)
1573 if (!bmap)
1574 return NULL;
1575 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1576 return bmap;
1579 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1581 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1583 bmap = remove_redundant_divs(bmap);
1584 bmap = isl_basic_map_mark_final(bmap);
1585 return bmap;
1588 __isl_give isl_basic_set *isl_basic_set_finalize(
1589 __isl_take isl_basic_set *bset)
1591 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1594 /* Remove definition of any div that is defined in terms of the given variable.
1595 * The div itself is not removed. Functions such as
1596 * eliminate_divs_ineq depend on the other divs remaining in place.
1598 static __isl_give isl_basic_map *remove_dependent_vars(
1599 __isl_take isl_basic_map *bmap, int pos)
1601 int i;
1603 if (!bmap)
1604 return NULL;
1606 for (i = 0; i < bmap->n_div; ++i) {
1607 if (isl_int_is_zero(bmap->div[i][0]))
1608 continue;
1609 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1610 continue;
1611 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1612 if (!bmap)
1613 return NULL;
1615 return bmap;
1618 /* Eliminate the specified variables from the constraints using
1619 * Fourier-Motzkin. The variables themselves are not removed.
1621 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1622 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1624 int d;
1625 int i, j, k;
1626 isl_size total;
1627 int need_gauss = 0;
1629 if (n == 0)
1630 return bmap;
1631 total = isl_basic_map_dim(bmap, isl_dim_all);
1632 if (total < 0)
1633 return isl_basic_map_free(bmap);
1635 bmap = isl_basic_map_cow(bmap);
1636 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1637 bmap = remove_dependent_vars(bmap, d);
1638 if (!bmap)
1639 return NULL;
1641 for (d = pos + n - 1;
1642 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1643 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1644 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1645 int n_lower, n_upper;
1646 if (!bmap)
1647 return NULL;
1648 for (i = 0; i < bmap->n_eq; ++i) {
1649 if (isl_int_is_zero(bmap->eq[i][1+d]))
1650 continue;
1651 bmap = eliminate_var_using_equality(bmap, d,
1652 bmap->eq[i], 0, NULL);
1653 if (isl_basic_map_drop_equality(bmap, i) < 0)
1654 return isl_basic_map_free(bmap);
1655 need_gauss = 1;
1656 break;
1658 if (i < bmap->n_eq)
1659 continue;
1660 n_lower = 0;
1661 n_upper = 0;
1662 for (i = 0; i < bmap->n_ineq; ++i) {
1663 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1664 n_lower++;
1665 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1666 n_upper++;
1668 bmap = isl_basic_map_extend_constraints(bmap,
1669 0, n_lower * n_upper);
1670 if (!bmap)
1671 goto error;
1672 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1673 int last;
1674 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1675 continue;
1676 last = -1;
1677 for (j = 0; j < i; ++j) {
1678 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1679 continue;
1680 last = j;
1681 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1682 isl_int_sgn(bmap->ineq[j][1+d]))
1683 continue;
1684 k = isl_basic_map_alloc_inequality(bmap);
1685 if (k < 0)
1686 goto error;
1687 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1688 1+total);
1689 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1690 1+d, 1+total, NULL);
1692 isl_basic_map_drop_inequality(bmap, i);
1693 i = last + 1;
1695 if (n_lower > 0 && n_upper > 0) {
1696 bmap = isl_basic_map_normalize_constraints(bmap);
1697 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1698 NULL, 0);
1699 bmap = isl_basic_map_gauss(bmap, NULL);
1700 bmap = isl_basic_map_remove_redundancies(bmap);
1701 need_gauss = 0;
1702 if (!bmap)
1703 goto error;
1704 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1705 break;
1708 if (need_gauss)
1709 bmap = isl_basic_map_gauss(bmap, NULL);
1710 return bmap;
1711 error:
1712 isl_basic_map_free(bmap);
1713 return NULL;
1716 __isl_give isl_basic_set *isl_basic_set_eliminate_vars(
1717 __isl_take isl_basic_set *bset, unsigned pos, unsigned n)
1719 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1720 pos, n));
1723 /* Eliminate the specified n dimensions starting at first from the
1724 * constraints, without removing the dimensions from the space.
1725 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1726 * Otherwise, they are projected out and the original space is restored.
1728 __isl_give isl_basic_map *isl_basic_map_eliminate(
1729 __isl_take isl_basic_map *bmap,
1730 enum isl_dim_type type, unsigned first, unsigned n)
1732 isl_space *space;
1734 if (!bmap)
1735 return NULL;
1736 if (n == 0)
1737 return bmap;
1739 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1740 return isl_basic_map_free(bmap);
1742 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1743 first += isl_basic_map_offset(bmap, type) - 1;
1744 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1745 return isl_basic_map_finalize(bmap);
1748 space = isl_basic_map_get_space(bmap);
1749 bmap = isl_basic_map_project_out(bmap, type, first, n);
1750 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1751 bmap = isl_basic_map_reset_space(bmap, space);
1752 return bmap;
1755 __isl_give isl_basic_set *isl_basic_set_eliminate(
1756 __isl_take isl_basic_set *bset,
1757 enum isl_dim_type type, unsigned first, unsigned n)
1759 return isl_basic_map_eliminate(bset, type, first, n);
1762 /* Remove all constraints from "bmap" that reference any unknown local
1763 * variables (directly or indirectly).
1765 * Dropping all constraints on a local variable will make it redundant,
1766 * so it will get removed implicitly by
1767 * isl_basic_map_drop_constraints_involving_dims. Some other local
1768 * variables may also end up becoming redundant if they only appear
1769 * in constraints together with the unknown local variable.
1770 * Therefore, start over after calling
1771 * isl_basic_map_drop_constraints_involving_dims.
1773 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_unknown_divs(
1774 __isl_take isl_basic_map *bmap)
1776 isl_bool known;
1777 isl_size n_div;
1778 int i, o_div;
1780 known = isl_basic_map_divs_known(bmap);
1781 if (known < 0)
1782 return isl_basic_map_free(bmap);
1783 if (known)
1784 return bmap;
1786 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1787 if (n_div < 0)
1788 return isl_basic_map_free(bmap);
1789 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1791 for (i = 0; i < n_div; ++i) {
1792 known = isl_basic_map_div_is_known(bmap, i);
1793 if (known < 0)
1794 return isl_basic_map_free(bmap);
1795 if (known)
1796 continue;
1797 bmap = remove_dependent_vars(bmap, o_div + i);
1798 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1799 isl_dim_div, i, 1);
1800 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1801 if (n_div < 0)
1802 return isl_basic_map_free(bmap);
1803 i = -1;
1806 return bmap;
1809 /* Remove all constraints from "bset" that reference any unknown local
1810 * variables (directly or indirectly).
1812 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_unknown_divs(
1813 __isl_take isl_basic_set *bset)
1815 isl_basic_map *bmap;
1817 bmap = bset_to_bmap(bset);
1818 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
1819 return bset_from_bmap(bmap);
1822 /* Remove all constraints from "map" that reference any unknown local
1823 * variables (directly or indirectly).
1825 * Since constraints may get dropped from the basic maps,
1826 * they may no longer be disjoint from each other.
1828 __isl_give isl_map *isl_map_drop_constraints_involving_unknown_divs(
1829 __isl_take isl_map *map)
1831 int i;
1832 isl_bool known;
1834 known = isl_map_divs_known(map);
1835 if (known < 0)
1836 return isl_map_free(map);
1837 if (known)
1838 return map;
1840 map = isl_map_cow(map);
1841 if (!map)
1842 return NULL;
1844 for (i = 0; i < map->n; ++i) {
1845 map->p[i] =
1846 isl_basic_map_drop_constraints_involving_unknown_divs(
1847 map->p[i]);
1848 if (!map->p[i])
1849 return isl_map_free(map);
1852 if (map->n > 1)
1853 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1855 return map;
1858 /* Don't assume equalities are in order, because align_divs
1859 * may have changed the order of the divs.
1861 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1863 int d, i;
1864 unsigned total;
1866 total = isl_space_dim(bmap->dim, isl_dim_all);
1867 for (d = 0; d < total; ++d)
1868 elim[d] = -1;
1869 for (i = 0; i < bmap->n_eq; ++i) {
1870 for (d = total - 1; d >= 0; --d) {
1871 if (isl_int_is_zero(bmap->eq[i][1+d]))
1872 continue;
1873 elim[d] = i;
1874 break;
1879 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1880 int *elim)
1882 compute_elimination_index(bset_to_bmap(bset), elim);
1885 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1886 __isl_keep isl_basic_map *bmap, int *elim)
1888 int d;
1889 int copied = 0;
1890 unsigned total;
1892 total = isl_space_dim(bmap->dim, isl_dim_all);
1893 for (d = total - 1; d >= 0; --d) {
1894 if (isl_int_is_zero(src[1+d]))
1895 continue;
1896 if (elim[d] == -1)
1897 continue;
1898 if (!copied) {
1899 isl_seq_cpy(dst, src, 1 + total);
1900 copied = 1;
1902 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1904 return copied;
1907 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1908 __isl_keep isl_basic_set *bset, int *elim)
1910 return reduced_using_equalities(dst, src,
1911 bset_to_bmap(bset), elim);
1914 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1915 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1917 int i;
1918 int *elim;
1919 isl_size dim;
1921 if (!bset || !context)
1922 goto error;
1924 if (context->n_eq == 0) {
1925 isl_basic_set_free(context);
1926 return bset;
1929 bset = isl_basic_set_cow(bset);
1930 dim = isl_basic_set_dim(bset, isl_dim_set);
1931 if (dim < 0)
1932 goto error;
1934 elim = isl_alloc_array(bset->ctx, int, dim);
1935 if (!elim)
1936 goto error;
1937 set_compute_elimination_index(context, elim);
1938 for (i = 0; i < bset->n_eq; ++i)
1939 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1940 context, elim);
1941 for (i = 0; i < bset->n_ineq; ++i)
1942 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1943 context, elim);
1944 isl_basic_set_free(context);
1945 free(elim);
1946 bset = isl_basic_set_simplify(bset);
1947 bset = isl_basic_set_finalize(bset);
1948 return bset;
1949 error:
1950 isl_basic_set_free(bset);
1951 isl_basic_set_free(context);
1952 return NULL;
1955 /* For each inequality in "ineq" that is a shifted (more relaxed)
1956 * copy of an inequality in "context", mark the corresponding entry
1957 * in "row" with -1.
1958 * If an inequality only has a non-negative constant term, then
1959 * mark it as well.
1961 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1962 __isl_keep isl_basic_set *context, int *row)
1964 struct isl_constraint_index ci;
1965 isl_size n_ineq, cols;
1966 unsigned total;
1967 int k;
1969 if (!ineq || !context)
1970 return isl_stat_error;
1971 if (context->n_ineq == 0)
1972 return isl_stat_ok;
1973 if (setup_constraint_index(&ci, context) < 0)
1974 return isl_stat_error;
1976 n_ineq = isl_mat_rows(ineq);
1977 cols = isl_mat_cols(ineq);
1978 if (n_ineq < 0 || cols < 0)
1979 return isl_stat_error;
1980 total = cols - 1;
1981 for (k = 0; k < n_ineq; ++k) {
1982 int l;
1983 isl_bool redundant;
1985 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1986 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1987 row[k] = -1;
1988 continue;
1990 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1991 if (redundant < 0)
1992 goto error;
1993 if (!redundant)
1994 continue;
1995 row[k] = -1;
1997 constraint_index_free(&ci);
1998 return isl_stat_ok;
1999 error:
2000 constraint_index_free(&ci);
2001 return isl_stat_error;
2004 static __isl_give isl_basic_set *remove_shifted_constraints(
2005 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
2007 struct isl_constraint_index ci;
2008 int k;
2010 if (!bset || !context)
2011 return bset;
2013 if (context->n_ineq == 0)
2014 return bset;
2015 if (setup_constraint_index(&ci, context) < 0)
2016 return bset;
2018 for (k = 0; k < bset->n_ineq; ++k) {
2019 isl_bool redundant;
2021 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2022 if (redundant < 0)
2023 goto error;
2024 if (!redundant)
2025 continue;
2026 bset = isl_basic_set_cow(bset);
2027 if (!bset)
2028 goto error;
2029 isl_basic_set_drop_inequality(bset, k);
2030 --k;
2032 constraint_index_free(&ci);
2033 return bset;
2034 error:
2035 constraint_index_free(&ci);
2036 return bset;
2039 /* Remove constraints from "bmap" that are identical to constraints
2040 * in "context" or that are more relaxed (greater constant term).
2042 * We perform the test for shifted copies on the pure constraints
2043 * in remove_shifted_constraints.
2045 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2046 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2048 isl_basic_set *bset, *bset_context;
2050 if (!bmap || !context)
2051 goto error;
2053 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2054 isl_basic_map_free(context);
2055 return bmap;
2058 context = isl_basic_map_align_divs(context, bmap);
2059 bmap = isl_basic_map_align_divs(bmap, context);
2061 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2062 bset_context = isl_basic_map_underlying_set(context);
2063 bset = remove_shifted_constraints(bset, bset_context);
2064 isl_basic_set_free(bset_context);
2066 bmap = isl_basic_map_overlying_set(bset, bmap);
2068 return bmap;
2069 error:
2070 isl_basic_map_free(bmap);
2071 isl_basic_map_free(context);
2072 return NULL;
2075 /* Does the (linear part of a) constraint "c" involve any of the "len"
2076 * "relevant" dimensions?
2078 static int is_related(isl_int *c, int len, int *relevant)
2080 int i;
2082 for (i = 0; i < len; ++i) {
2083 if (!relevant[i])
2084 continue;
2085 if (!isl_int_is_zero(c[i]))
2086 return 1;
2089 return 0;
2092 /* Drop constraints from "bmap" that do not involve any of
2093 * the dimensions marked "relevant".
2095 static __isl_give isl_basic_map *drop_unrelated_constraints(
2096 __isl_take isl_basic_map *bmap, int *relevant)
2098 int i;
2099 isl_size dim;
2101 dim = isl_basic_map_dim(bmap, isl_dim_all);
2102 if (dim < 0)
2103 return isl_basic_map_free(bmap);
2104 for (i = 0; i < dim; ++i)
2105 if (!relevant[i])
2106 break;
2107 if (i >= dim)
2108 return bmap;
2110 for (i = bmap->n_eq - 1; i >= 0; --i)
2111 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2112 bmap = isl_basic_map_cow(bmap);
2113 if (isl_basic_map_drop_equality(bmap, i) < 0)
2114 return isl_basic_map_free(bmap);
2117 for (i = bmap->n_ineq - 1; i >= 0; --i)
2118 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2119 bmap = isl_basic_map_cow(bmap);
2120 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2121 return isl_basic_map_free(bmap);
2124 return bmap;
2127 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2129 * In particular, for any variable involved in the constraint,
2130 * find the actual group id from before and replace the group
2131 * of the corresponding variable by the minimal group of all
2132 * the variables involved in the constraint considered so far
2133 * (if this minimum is smaller) or replace the minimum by this group
2134 * (if the minimum is larger).
2136 * At the end, all the variables in "c" will (indirectly) point
2137 * to the minimal of the groups that they referred to originally.
2139 static void update_groups(int dim, int *group, isl_int *c)
2141 int j;
2142 int min = dim;
2144 for (j = 0; j < dim; ++j) {
2145 if (isl_int_is_zero(c[j]))
2146 continue;
2147 while (group[j] >= 0 && group[group[j]] != group[j])
2148 group[j] = group[group[j]];
2149 if (group[j] == min)
2150 continue;
2151 if (group[j] < min) {
2152 if (min >= 0 && min < dim)
2153 group[min] = group[j];
2154 min = group[j];
2155 } else
2156 group[group[j]] = min;
2160 /* Allocate an array of groups of variables, one for each variable
2161 * in "context", initialized to zero.
2163 static int *alloc_groups(__isl_keep isl_basic_set *context)
2165 isl_ctx *ctx;
2166 isl_size dim;
2168 dim = isl_basic_set_dim(context, isl_dim_set);
2169 if (dim < 0)
2170 return NULL;
2171 ctx = isl_basic_set_get_ctx(context);
2172 return isl_calloc_array(ctx, int, dim);
2175 /* Drop constraints from "bmap" that only involve variables that are
2176 * not related to any of the variables marked with a "-1" in "group".
2178 * We construct groups of variables that collect variables that
2179 * (indirectly) appear in some common constraint of "bmap".
2180 * Each group is identified by the first variable in the group,
2181 * except for the special group of variables that was already identified
2182 * in the input as -1 (or are related to those variables).
2183 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2184 * otherwise the group of i is the group of group[i].
2186 * We first initialize groups for the remaining variables.
2187 * Then we iterate over the constraints of "bmap" and update the
2188 * group of the variables in the constraint by the smallest group.
2189 * Finally, we resolve indirect references to groups by running over
2190 * the variables.
2192 * After computing the groups, we drop constraints that do not involve
2193 * any variables in the -1 group.
2195 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2196 __isl_take isl_basic_map *bmap, __isl_take int *group)
2198 isl_size dim;
2199 int i;
2200 int last;
2202 dim = isl_basic_map_dim(bmap, isl_dim_all);
2203 if (dim < 0)
2204 return isl_basic_map_free(bmap);
2206 last = -1;
2207 for (i = 0; i < dim; ++i)
2208 if (group[i] >= 0)
2209 last = group[i] = i;
2210 if (last < 0) {
2211 free(group);
2212 return bmap;
2215 for (i = 0; i < bmap->n_eq; ++i)
2216 update_groups(dim, group, bmap->eq[i] + 1);
2217 for (i = 0; i < bmap->n_ineq; ++i)
2218 update_groups(dim, group, bmap->ineq[i] + 1);
2220 for (i = 0; i < dim; ++i)
2221 if (group[i] >= 0)
2222 group[i] = group[group[i]];
2224 for (i = 0; i < dim; ++i)
2225 group[i] = group[i] == -1;
2227 bmap = drop_unrelated_constraints(bmap, group);
2229 free(group);
2230 return bmap;
2233 /* Drop constraints from "context" that are irrelevant for computing
2234 * the gist of "bset".
2236 * In particular, drop constraints in variables that are not related
2237 * to any of the variables involved in the constraints of "bset"
2238 * in the sense that there is no sequence of constraints that connects them.
2240 * We first mark all variables that appear in "bset" as belonging
2241 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2243 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2244 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2246 int *group;
2247 isl_size dim;
2248 int i, j;
2250 dim = isl_basic_set_dim(bset, isl_dim_set);
2251 if (!context || dim < 0)
2252 return isl_basic_set_free(context);
2254 group = alloc_groups(context);
2256 if (!group)
2257 return isl_basic_set_free(context);
2259 for (i = 0; i < dim; ++i) {
2260 for (j = 0; j < bset->n_eq; ++j)
2261 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2262 break;
2263 if (j < bset->n_eq) {
2264 group[i] = -1;
2265 continue;
2267 for (j = 0; j < bset->n_ineq; ++j)
2268 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2269 break;
2270 if (j < bset->n_ineq)
2271 group[i] = -1;
2274 return isl_basic_map_drop_unrelated_constraints(context, group);
2277 /* Drop constraints from "context" that are irrelevant for computing
2278 * the gist of the inequalities "ineq".
2279 * Inequalities in "ineq" for which the corresponding element of row
2280 * is set to -1 have already been marked for removal and should be ignored.
2282 * In particular, drop constraints in variables that are not related
2283 * to any of the variables involved in "ineq"
2284 * in the sense that there is no sequence of constraints that connects them.
2286 * We first mark all variables that appear in "bset" as belonging
2287 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2289 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2290 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2292 int *group;
2293 isl_size dim;
2294 int i, j;
2295 isl_size n;
2297 dim = isl_basic_set_dim(context, isl_dim_set);
2298 n = isl_mat_rows(ineq);
2299 if (dim < 0 || n < 0)
2300 return isl_basic_set_free(context);
2302 group = alloc_groups(context);
2304 if (!group)
2305 return isl_basic_set_free(context);
2307 for (i = 0; i < dim; ++i) {
2308 for (j = 0; j < n; ++j) {
2309 if (row[j] < 0)
2310 continue;
2311 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2312 break;
2314 if (j < n)
2315 group[i] = -1;
2318 return isl_basic_map_drop_unrelated_constraints(context, group);
2321 /* Do all "n" entries of "row" contain a negative value?
2323 static int all_neg(int *row, int n)
2325 int i;
2327 for (i = 0; i < n; ++i)
2328 if (row[i] >= 0)
2329 return 0;
2331 return 1;
2334 /* Update the inequalities in "bset" based on the information in "row"
2335 * and "tab".
2337 * In particular, the array "row" contains either -1, meaning that
2338 * the corresponding inequality of "bset" is redundant, or the index
2339 * of an inequality in "tab".
2341 * If the row entry is -1, then drop the inequality.
2342 * Otherwise, if the constraint is marked redundant in the tableau,
2343 * then drop the inequality. Similarly, if it is marked as an equality
2344 * in the tableau, then turn the inequality into an equality and
2345 * perform Gaussian elimination.
2347 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2348 __isl_keep int *row, struct isl_tab *tab)
2350 int i;
2351 unsigned n_ineq;
2352 unsigned n_eq;
2353 int found_equality = 0;
2355 if (!bset)
2356 return NULL;
2357 if (tab && tab->empty)
2358 return isl_basic_set_set_to_empty(bset);
2360 n_ineq = bset->n_ineq;
2361 for (i = n_ineq - 1; i >= 0; --i) {
2362 if (row[i] < 0) {
2363 if (isl_basic_set_drop_inequality(bset, i) < 0)
2364 return isl_basic_set_free(bset);
2365 continue;
2367 if (!tab)
2368 continue;
2369 n_eq = tab->n_eq;
2370 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2371 isl_basic_map_inequality_to_equality(bset, i);
2372 found_equality = 1;
2373 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2374 if (isl_basic_set_drop_inequality(bset, i) < 0)
2375 return isl_basic_set_free(bset);
2379 if (found_equality)
2380 bset = isl_basic_set_gauss(bset, NULL);
2381 bset = isl_basic_set_finalize(bset);
2382 return bset;
2385 /* Update the inequalities in "bset" based on the information in "row"
2386 * and "tab" and free all arguments (other than "bset").
2388 static __isl_give isl_basic_set *update_ineq_free(
2389 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2390 __isl_take isl_basic_set *context, __isl_take int *row,
2391 struct isl_tab *tab)
2393 isl_mat_free(ineq);
2394 isl_basic_set_free(context);
2396 bset = update_ineq(bset, row, tab);
2398 free(row);
2399 isl_tab_free(tab);
2400 return bset;
2403 /* Remove all information from bset that is redundant in the context
2404 * of context.
2405 * "ineq" contains the (possibly transformed) inequalities of "bset",
2406 * in the same order.
2407 * The (explicit) equalities of "bset" are assumed to have been taken
2408 * into account by the transformation such that only the inequalities
2409 * are relevant.
2410 * "context" is assumed not to be empty.
2412 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2413 * A value of -1 means that the inequality is obviously redundant and may
2414 * not even appear in "tab".
2416 * We first mark the inequalities of "bset"
2417 * that are obviously redundant with respect to some inequality in "context".
2418 * Then we remove those constraints from "context" that have become
2419 * irrelevant for computing the gist of "bset".
2420 * Note that this removal of constraints cannot be replaced by
2421 * a factorization because factors in "bset" may still be connected
2422 * to each other through constraints in "context".
2424 * If there are any inequalities left, we construct a tableau for
2425 * the context and then add the inequalities of "bset".
2426 * Before adding these inequalities, we freeze all constraints such that
2427 * they won't be considered redundant in terms of the constraints of "bset".
2428 * Then we detect all redundant constraints (among the
2429 * constraints that weren't frozen), first by checking for redundancy in the
2430 * the tableau and then by checking if replacing a constraint by its negation
2431 * would lead to an empty set. This last step is fairly expensive
2432 * and could be optimized by more reuse of the tableau.
2433 * Finally, we update bset according to the results.
2435 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2436 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2438 int i, r;
2439 int *row = NULL;
2440 isl_ctx *ctx;
2441 isl_basic_set *combined = NULL;
2442 struct isl_tab *tab = NULL;
2443 unsigned n_eq, context_ineq;
2445 if (!bset || !ineq || !context)
2446 goto error;
2448 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2449 isl_basic_set_free(context);
2450 isl_mat_free(ineq);
2451 return bset;
2454 ctx = isl_basic_set_get_ctx(context);
2455 row = isl_calloc_array(ctx, int, bset->n_ineq);
2456 if (!row)
2457 goto error;
2459 if (mark_shifted_constraints(ineq, context, row) < 0)
2460 goto error;
2461 if (all_neg(row, bset->n_ineq))
2462 return update_ineq_free(bset, ineq, context, row, NULL);
2464 context = drop_irrelevant_constraints_marked(context, ineq, row);
2465 if (!context)
2466 goto error;
2467 if (isl_basic_set_plain_is_universe(context))
2468 return update_ineq_free(bset, ineq, context, row, NULL);
2470 n_eq = context->n_eq;
2471 context_ineq = context->n_ineq;
2472 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2473 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2474 tab = isl_tab_from_basic_set(combined, 0);
2475 for (i = 0; i < context_ineq; ++i)
2476 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2477 goto error;
2478 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2479 goto error;
2480 r = context_ineq;
2481 for (i = 0; i < bset->n_ineq; ++i) {
2482 if (row[i] < 0)
2483 continue;
2484 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2485 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2486 goto error;
2487 row[i] = r++;
2489 if (isl_tab_detect_implicit_equalities(tab) < 0)
2490 goto error;
2491 if (isl_tab_detect_redundant(tab) < 0)
2492 goto error;
2493 for (i = bset->n_ineq - 1; i >= 0; --i) {
2494 isl_basic_set *test;
2495 int is_empty;
2497 if (row[i] < 0)
2498 continue;
2499 r = row[i];
2500 if (tab->con[n_eq + r].is_redundant)
2501 continue;
2502 test = isl_basic_set_dup(combined);
2503 test = isl_inequality_negate(test, r);
2504 test = isl_basic_set_update_from_tab(test, tab);
2505 is_empty = isl_basic_set_is_empty(test);
2506 isl_basic_set_free(test);
2507 if (is_empty < 0)
2508 goto error;
2509 if (is_empty)
2510 tab->con[n_eq + r].is_redundant = 1;
2512 bset = update_ineq_free(bset, ineq, context, row, tab);
2513 if (bset) {
2514 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2515 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2518 isl_basic_set_free(combined);
2519 return bset;
2520 error:
2521 free(row);
2522 isl_mat_free(ineq);
2523 isl_tab_free(tab);
2524 isl_basic_set_free(combined);
2525 isl_basic_set_free(context);
2526 isl_basic_set_free(bset);
2527 return NULL;
2530 /* Extract the inequalities of "bset" as an isl_mat.
2532 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2534 isl_size total;
2535 isl_ctx *ctx;
2536 isl_mat *ineq;
2538 total = isl_basic_set_dim(bset, isl_dim_all);
2539 if (total < 0)
2540 return NULL;
2542 ctx = isl_basic_set_get_ctx(bset);
2543 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2544 0, 1 + total);
2546 return ineq;
2549 /* Remove all information from "bset" that is redundant in the context
2550 * of "context", for the case where both "bset" and "context" are
2551 * full-dimensional.
2553 static __isl_give isl_basic_set *uset_gist_uncompressed(
2554 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2556 isl_mat *ineq;
2558 ineq = extract_ineq(bset);
2559 return uset_gist_full(bset, ineq, context);
2562 /* Replace "bset" by an empty basic set in the same space.
2564 static __isl_give isl_basic_set *replace_by_empty(
2565 __isl_take isl_basic_set *bset)
2567 isl_space *space;
2569 space = isl_basic_set_get_space(bset);
2570 isl_basic_set_free(bset);
2571 return isl_basic_set_empty(space);
2574 /* Remove all information from "bset" that is redundant in the context
2575 * of "context", for the case where the combined equalities of
2576 * "bset" and "context" allow for a compression that can be obtained
2577 * by preapplication of "T".
2578 * If the compression of "context" is empty, meaning that "bset" and
2579 * "context" do not intersect, then return the empty set.
2581 * "bset" itself is not transformed by "T". Instead, the inequalities
2582 * are extracted from "bset" and those are transformed by "T".
2583 * uset_gist_full then determines which of the transformed inequalities
2584 * are redundant with respect to the transformed "context" and removes
2585 * the corresponding inequalities from "bset".
2587 * After preapplying "T" to the inequalities, any common factor is
2588 * removed from the coefficients. If this results in a tightening
2589 * of the constant term, then the same tightening is applied to
2590 * the corresponding untransformed inequality in "bset".
2591 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2593 * g f'(x) + r >= 0
2595 * with 0 <= r < g, then it is equivalent to
2597 * f'(x) >= 0
2599 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2600 * subspace compressed by T since the latter would be transformed to
2602 * g f'(x) >= 0
2604 static __isl_give isl_basic_set *uset_gist_compressed(
2605 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2606 __isl_take isl_mat *T)
2608 isl_ctx *ctx;
2609 isl_mat *ineq;
2610 int i;
2611 isl_size n_row, n_col;
2612 isl_int rem;
2614 ineq = extract_ineq(bset);
2615 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2616 context = isl_basic_set_preimage(context, T);
2618 if (!ineq || !context)
2619 goto error;
2620 if (isl_basic_set_plain_is_empty(context)) {
2621 isl_mat_free(ineq);
2622 isl_basic_set_free(context);
2623 return replace_by_empty(bset);
2626 ctx = isl_mat_get_ctx(ineq);
2627 n_row = isl_mat_rows(ineq);
2628 n_col = isl_mat_cols(ineq);
2629 if (n_row < 0 || n_col < 0)
2630 goto error;
2631 isl_int_init(rem);
2632 for (i = 0; i < n_row; ++i) {
2633 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2634 if (isl_int_is_zero(ctx->normalize_gcd))
2635 continue;
2636 if (isl_int_is_one(ctx->normalize_gcd))
2637 continue;
2638 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2639 ctx->normalize_gcd, n_col - 1);
2640 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2641 isl_int_fdiv_q(ineq->row[i][0],
2642 ineq->row[i][0], ctx->normalize_gcd);
2643 if (isl_int_is_zero(rem))
2644 continue;
2645 bset = isl_basic_set_cow(bset);
2646 if (!bset)
2647 break;
2648 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2650 isl_int_clear(rem);
2652 return uset_gist_full(bset, ineq, context);
2653 error:
2654 isl_mat_free(ineq);
2655 isl_basic_set_free(context);
2656 isl_basic_set_free(bset);
2657 return NULL;
2660 /* Project "bset" onto the variables that are involved in "template".
2662 static __isl_give isl_basic_set *project_onto_involved(
2663 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2665 int i;
2666 isl_size n;
2668 n = isl_basic_set_dim(template, isl_dim_set);
2669 if (n < 0 || !template)
2670 return isl_basic_set_free(bset);
2672 for (i = 0; i < n; ++i) {
2673 isl_bool involved;
2675 involved = isl_basic_set_involves_dims(template,
2676 isl_dim_set, i, 1);
2677 if (involved < 0)
2678 return isl_basic_set_free(bset);
2679 if (involved)
2680 continue;
2681 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2684 return bset;
2687 /* Remove all information from bset that is redundant in the context
2688 * of context. In particular, equalities that are linear combinations
2689 * of those in context are removed. Then the inequalities that are
2690 * redundant in the context of the equalities and inequalities of
2691 * context are removed.
2693 * First of all, we drop those constraints from "context"
2694 * that are irrelevant for computing the gist of "bset".
2695 * Alternatively, we could factorize the intersection of "context" and "bset".
2697 * We first compute the intersection of the integer affine hulls
2698 * of "bset" and "context",
2699 * compute the gist inside this intersection and then reduce
2700 * the constraints with respect to the equalities of the context
2701 * that only involve variables already involved in the input.
2702 * If the intersection of the affine hulls turns out to be empty,
2703 * then return the empty set.
2705 * If two constraints are mutually redundant, then uset_gist_full
2706 * will remove the second of those constraints. We therefore first
2707 * sort the constraints so that constraints not involving existentially
2708 * quantified variables are given precedence over those that do.
2709 * We have to perform this sorting before the variable compression,
2710 * because that may effect the order of the variables.
2712 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2713 __isl_take isl_basic_set *context)
2715 isl_mat *eq;
2716 isl_mat *T;
2717 isl_basic_set *aff;
2718 isl_basic_set *aff_context;
2719 isl_size total;
2721 total = isl_basic_set_dim(bset, isl_dim_all);
2722 if (total < 0 || !context)
2723 goto error;
2725 context = drop_irrelevant_constraints(context, bset);
2727 bset = isl_basic_set_detect_equalities(bset);
2728 aff = isl_basic_set_copy(bset);
2729 aff = isl_basic_set_plain_affine_hull(aff);
2730 context = isl_basic_set_detect_equalities(context);
2731 aff_context = isl_basic_set_copy(context);
2732 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2733 aff = isl_basic_set_intersect(aff, aff_context);
2734 if (!aff)
2735 goto error;
2736 if (isl_basic_set_plain_is_empty(aff)) {
2737 isl_basic_set_free(bset);
2738 isl_basic_set_free(context);
2739 return aff;
2741 bset = isl_basic_set_sort_constraints(bset);
2742 if (aff->n_eq == 0) {
2743 isl_basic_set_free(aff);
2744 return uset_gist_uncompressed(bset, context);
2746 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2747 eq = isl_mat_cow(eq);
2748 T = isl_mat_variable_compression(eq, NULL);
2749 isl_basic_set_free(aff);
2750 if (T && T->n_col == 0) {
2751 isl_mat_free(T);
2752 isl_basic_set_free(context);
2753 return replace_by_empty(bset);
2756 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2757 aff_context = project_onto_involved(aff_context, bset);
2759 bset = uset_gist_compressed(bset, context, T);
2760 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2762 if (bset) {
2763 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2764 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2767 return bset;
2768 error:
2769 isl_basic_set_free(bset);
2770 isl_basic_set_free(context);
2771 return NULL;
2774 /* Return the number of equality constraints in "bmap" that involve
2775 * local variables. This function assumes that Gaussian elimination
2776 * has been applied to the equality constraints.
2778 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2780 int i;
2781 isl_size total, n_div;
2783 if (!bmap)
2784 return -1;
2786 if (bmap->n_eq == 0)
2787 return 0;
2789 total = isl_basic_map_dim(bmap, isl_dim_all);
2790 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2791 if (total < 0 || n_div < 0)
2792 return -1;
2793 total -= n_div;
2795 for (i = 0; i < bmap->n_eq; ++i)
2796 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2797 n_div) == -1)
2798 return i;
2800 return bmap->n_eq;
2803 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2804 * The constraints are assumed not to involve any local variables.
2806 static __isl_give isl_basic_map *basic_map_from_equalities(
2807 __isl_take isl_space *space, __isl_take isl_mat *eq)
2809 int i, k;
2810 isl_size total;
2811 isl_basic_map *bmap = NULL;
2813 total = isl_space_dim(space, isl_dim_all);
2814 if (total < 0 || !eq)
2815 goto error;
2817 if (1 + total != eq->n_col)
2818 isl_die(isl_space_get_ctx(space), isl_error_internal,
2819 "unexpected number of columns", goto error);
2821 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2822 0, eq->n_row, 0);
2823 for (i = 0; i < eq->n_row; ++i) {
2824 k = isl_basic_map_alloc_equality(bmap);
2825 if (k < 0)
2826 goto error;
2827 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2830 isl_space_free(space);
2831 isl_mat_free(eq);
2832 return bmap;
2833 error:
2834 isl_space_free(space);
2835 isl_mat_free(eq);
2836 isl_basic_map_free(bmap);
2837 return NULL;
2840 /* Construct and return a variable compression based on the equality
2841 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2842 * "n1" is the number of (initial) equality constraints in "bmap1"
2843 * that do involve local variables.
2844 * "n2" is the number of (initial) equality constraints in "bmap2"
2845 * that do involve local variables.
2846 * "total" is the total number of other variables.
2847 * This function assumes that Gaussian elimination
2848 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2849 * such that the equality constraints not involving local variables
2850 * are those that start at "n1" or "n2".
2852 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2853 * then simply compute the compression based on the equality constraints
2854 * in the other basic map.
2855 * Otherwise, combine the equality constraints from both into a new
2856 * basic map such that Gaussian elimination can be applied to this combination
2857 * and then construct a variable compression from the resulting
2858 * equality constraints.
2860 static __isl_give isl_mat *combined_variable_compression(
2861 __isl_keep isl_basic_map *bmap1, int n1,
2862 __isl_keep isl_basic_map *bmap2, int n2, int total)
2864 isl_ctx *ctx;
2865 isl_mat *E1, *E2, *V;
2866 isl_basic_map *bmap;
2868 ctx = isl_basic_map_get_ctx(bmap1);
2869 if (bmap1->n_eq == n1) {
2870 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2871 n2, bmap2->n_eq - n2, 0, 1 + total);
2872 return isl_mat_variable_compression(E2, NULL);
2874 if (bmap2->n_eq == n2) {
2875 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2876 n1, bmap1->n_eq - n1, 0, 1 + total);
2877 return isl_mat_variable_compression(E1, NULL);
2879 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2880 n1, bmap1->n_eq - n1, 0, 1 + total);
2881 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2882 n2, bmap2->n_eq - n2, 0, 1 + total);
2883 E1 = isl_mat_concat(E1, E2);
2884 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2885 bmap = isl_basic_map_gauss(bmap, NULL);
2886 if (!bmap)
2887 return NULL;
2888 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2889 V = isl_mat_variable_compression(E1, NULL);
2890 isl_basic_map_free(bmap);
2892 return V;
2895 /* Extract the stride constraints from "bmap", compressed
2896 * with respect to both the stride constraints in "context" and
2897 * the remaining equality constraints in both "bmap" and "context".
2898 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2899 * "context_n_eq" is the number of (initial) stride constraints in "context".
2901 * Let x be all variables in "bmap" (and "context") other than the local
2902 * variables. First compute a variable compression
2904 * x = V x'
2906 * based on the non-stride equality constraints in "bmap" and "context".
2907 * Consider the stride constraints of "context",
2909 * A(x) + B(y) = 0
2911 * with y the local variables and plug in the variable compression,
2912 * resulting in
2914 * A(V x') + B(y) = 0
2916 * Use these constraints to compute a parameter compression on x'
2918 * x' = T x''
2920 * Now consider the stride constraints of "bmap"
2922 * C(x) + D(y) = 0
2924 * and plug in x = V*T x''.
2925 * That is, return A = [C*V*T D].
2927 static __isl_give isl_mat *extract_compressed_stride_constraints(
2928 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2929 __isl_keep isl_basic_map *context, int context_n_eq)
2931 isl_size total, n_div;
2932 isl_ctx *ctx;
2933 isl_mat *A, *B, *T, *V;
2935 total = isl_basic_map_dim(context, isl_dim_all);
2936 n_div = isl_basic_map_dim(context, isl_dim_div);
2937 if (total < 0 || n_div < 0)
2938 return NULL;
2939 total -= n_div;
2941 ctx = isl_basic_map_get_ctx(bmap);
2943 V = combined_variable_compression(bmap, bmap_n_eq,
2944 context, context_n_eq, total);
2946 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2947 B = isl_mat_sub_alloc6(ctx, context->eq,
2948 0, context_n_eq, 1 + total, n_div);
2949 A = isl_mat_product(A, isl_mat_copy(V));
2950 T = isl_mat_parameter_compression_ext(A, B);
2951 T = isl_mat_product(V, T);
2953 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2954 if (n_div < 0)
2955 T = isl_mat_free(T);
2956 else
2957 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2959 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2960 0, bmap_n_eq, 0, 1 + total + n_div);
2961 A = isl_mat_product(A, T);
2963 return A;
2966 /* Remove the prime factors from *g that have an exponent that
2967 * is strictly smaller than the exponent in "c".
2968 * All exponents in *g are known to be smaller than or equal
2969 * to those in "c".
2971 * That is, if *g is equal to
2973 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2975 * and "c" is equal to
2977 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2979 * then update *g to
2981 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2982 * p_n^{e_n * (e_n = f_n)}
2984 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2985 * neither does the gcd of *g and c / *g.
2986 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2987 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2988 * Dividing *g by this gcd therefore strictly reduces the exponent
2989 * of the prime factors that need to be removed, while leaving the
2990 * other prime factors untouched.
2991 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2992 * removes all undesired factors, without removing any others.
2994 static void remove_incomplete_powers(isl_int *g, isl_int c)
2996 isl_int t;
2998 isl_int_init(t);
2999 for (;;) {
3000 isl_int_divexact(t, c, *g);
3001 isl_int_gcd(t, t, *g);
3002 if (isl_int_is_one(t))
3003 break;
3004 isl_int_divexact(*g, *g, t);
3006 isl_int_clear(t);
3009 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3010 * of the same stride constraints in a compressed space that exploits
3011 * all equalities in the context and the other equalities in "bmap".
3013 * If the stride constraints of "bmap" are of the form
3015 * C(x) + D(y) = 0
3017 * then A is of the form
3019 * B(x') + D(y) = 0
3021 * If any of these constraints involves only a single local variable y,
3022 * then the constraint appears as
3024 * f(x) + m y_i = 0
3026 * in "bmap" and as
3028 * h(x') + m y_i = 0
3030 * in "A".
3032 * Let g be the gcd of m and the coefficients of h.
3033 * Then, in particular, g is a divisor of the coefficients of h and
3035 * f(x) = h(x')
3037 * is known to be a multiple of g.
3038 * If some prime factor in m appears with the same exponent in g,
3039 * then it can be removed from m because f(x) is already known
3040 * to be a multiple of g and therefore in particular of this power
3041 * of the prime factors.
3042 * Prime factors that appear with a smaller exponent in g cannot
3043 * be removed from m.
3044 * Let g' be the divisor of g containing all prime factors that
3045 * appear with the same exponent in m and g, then
3047 * f(x) + m y_i = 0
3049 * can be replaced by
3051 * f(x) + m/g' y_i' = 0
3053 * Note that (if g' != 1) this changes the explicit representation
3054 * of y_i to that of y_i', so the integer division at position i
3055 * is marked unknown and later recomputed by a call to
3056 * isl_basic_map_gauss.
3058 static __isl_give isl_basic_map *reduce_stride_constraints(
3059 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3061 int i;
3062 isl_size total, n_div;
3063 int any = 0;
3064 isl_int gcd;
3066 total = isl_basic_map_dim(bmap, isl_dim_all);
3067 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3068 if (total < 0 || n_div < 0 || !A)
3069 return isl_basic_map_free(bmap);
3070 total -= n_div;
3072 isl_int_init(gcd);
3073 for (i = 0; i < n; ++i) {
3074 int div;
3076 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3077 if (div < 0)
3078 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3079 "equality constraints modified unexpectedly",
3080 goto error);
3081 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3082 n_div - div - 1) != -1)
3083 continue;
3084 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3085 goto error;
3086 if (isl_int_is_one(gcd))
3087 continue;
3088 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3089 if (isl_int_is_one(gcd))
3090 continue;
3091 isl_int_divexact(bmap->eq[i][1 + total + div],
3092 bmap->eq[i][1 + total + div], gcd);
3093 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3094 if (!bmap)
3095 goto error;
3096 any = 1;
3098 isl_int_clear(gcd);
3100 if (any)
3101 bmap = isl_basic_map_gauss(bmap, NULL);
3103 return bmap;
3104 error:
3105 isl_int_clear(gcd);
3106 isl_basic_map_free(bmap);
3107 return NULL;
3110 /* Simplify the stride constraints in "bmap" based on
3111 * the remaining equality constraints in "bmap" and all equality
3112 * constraints in "context".
3113 * Only do this if both "bmap" and "context" have stride constraints.
3115 * First extract a copy of the stride constraints in "bmap" in a compressed
3116 * space exploiting all the other equality constraints and then
3117 * use this compressed copy to simplify the original stride constraints.
3119 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3120 __isl_keep isl_basic_map *context)
3122 int bmap_n_eq, context_n_eq;
3123 isl_mat *A;
3125 if (!bmap || !context)
3126 return isl_basic_map_free(bmap);
3128 bmap_n_eq = n_div_eq(bmap);
3129 context_n_eq = n_div_eq(context);
3131 if (bmap_n_eq < 0 || context_n_eq < 0)
3132 return isl_basic_map_free(bmap);
3133 if (bmap_n_eq == 0 || context_n_eq == 0)
3134 return bmap;
3136 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3137 context, context_n_eq);
3138 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3140 isl_mat_free(A);
3142 return bmap;
3145 /* Return a basic map that has the same intersection with "context" as "bmap"
3146 * and that is as "simple" as possible.
3148 * The core computation is performed on the pure constraints.
3149 * When we add back the meaning of the integer divisions, we need
3150 * to (re)introduce the div constraints. If we happen to have
3151 * discovered that some of these integer divisions are equal to
3152 * some affine combination of other variables, then these div
3153 * constraints may end up getting simplified in terms of the equalities,
3154 * resulting in extra inequalities on the other variables that
3155 * may have been removed already or that may not even have been
3156 * part of the input. We try and remove those constraints of
3157 * this form that are most obviously redundant with respect to
3158 * the context. We also remove those div constraints that are
3159 * redundant with respect to the other constraints in the result.
3161 * The stride constraints among the equality constraints in "bmap" are
3162 * also simplified with respecting to the other equality constraints
3163 * in "bmap" and with respect to all equality constraints in "context".
3165 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3166 __isl_take isl_basic_map *context)
3168 isl_basic_set *bset, *eq;
3169 isl_basic_map *eq_bmap;
3170 isl_size total, n_div, n_div_bmap;
3171 unsigned extra, n_eq, n_ineq;
3173 if (!bmap || !context)
3174 goto error;
3176 if (isl_basic_map_plain_is_universe(bmap)) {
3177 isl_basic_map_free(context);
3178 return bmap;
3180 if (isl_basic_map_plain_is_empty(context)) {
3181 isl_space *space = isl_basic_map_get_space(bmap);
3182 isl_basic_map_free(bmap);
3183 isl_basic_map_free(context);
3184 return isl_basic_map_universe(space);
3186 if (isl_basic_map_plain_is_empty(bmap)) {
3187 isl_basic_map_free(context);
3188 return bmap;
3191 bmap = isl_basic_map_remove_redundancies(bmap);
3192 context = isl_basic_map_remove_redundancies(context);
3193 context = isl_basic_map_align_divs(context, bmap);
3195 n_div = isl_basic_map_dim(context, isl_dim_div);
3196 total = isl_basic_map_dim(bmap, isl_dim_all);
3197 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3198 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3199 goto error;
3200 extra = n_div - n_div_bmap;
3202 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3203 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3204 bset = uset_gist(bset,
3205 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3206 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3208 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3209 isl_basic_set_plain_is_empty(bset)) {
3210 isl_basic_map_free(context);
3211 return isl_basic_map_overlying_set(bset, bmap);
3214 n_eq = bset->n_eq;
3215 n_ineq = bset->n_ineq;
3216 eq = isl_basic_set_copy(bset);
3217 eq = isl_basic_set_cow(eq);
3218 eq = isl_basic_set_free_inequality(eq, n_ineq);
3219 bset = isl_basic_set_free_equality(bset, n_eq);
3221 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3222 eq_bmap = gist_strides(eq_bmap, context);
3223 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3224 bmap = isl_basic_map_overlying_set(bset, bmap);
3225 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3226 bmap = isl_basic_map_remove_redundancies(bmap);
3228 return bmap;
3229 error:
3230 isl_basic_map_free(bmap);
3231 isl_basic_map_free(context);
3232 return NULL;
3236 * Assumes context has no implicit divs.
3238 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3239 __isl_take isl_basic_map *context)
3241 int i;
3243 if (!map || !context)
3244 goto error;
3246 if (isl_basic_map_plain_is_empty(context)) {
3247 isl_space *space = isl_map_get_space(map);
3248 isl_map_free(map);
3249 isl_basic_map_free(context);
3250 return isl_map_universe(space);
3253 context = isl_basic_map_remove_redundancies(context);
3254 map = isl_map_cow(map);
3255 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3256 goto error;
3257 map = isl_map_compute_divs(map);
3258 if (!map)
3259 goto error;
3260 for (i = map->n - 1; i >= 0; --i) {
3261 map->p[i] = isl_basic_map_gist(map->p[i],
3262 isl_basic_map_copy(context));
3263 if (!map->p[i])
3264 goto error;
3265 if (isl_basic_map_plain_is_empty(map->p[i])) {
3266 isl_basic_map_free(map->p[i]);
3267 if (i != map->n - 1)
3268 map->p[i] = map->p[map->n - 1];
3269 map->n--;
3272 isl_basic_map_free(context);
3273 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3274 return map;
3275 error:
3276 isl_map_free(map);
3277 isl_basic_map_free(context);
3278 return NULL;
3281 /* Drop all inequalities from "bmap" that also appear in "context".
3282 * "context" is assumed to have only known local variables and
3283 * the initial local variables of "bmap" are assumed to be the same
3284 * as those of "context".
3285 * The constraints of both "bmap" and "context" are assumed
3286 * to have been sorted using isl_basic_map_sort_constraints.
3288 * Run through the inequality constraints of "bmap" and "context"
3289 * in sorted order.
3290 * If a constraint of "bmap" involves variables not in "context",
3291 * then it cannot appear in "context".
3292 * If a matching constraint is found, it is removed from "bmap".
3294 static __isl_give isl_basic_map *drop_inequalities(
3295 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3297 int i1, i2;
3298 isl_size total, bmap_total;
3299 unsigned extra;
3301 total = isl_basic_map_dim(context, isl_dim_all);
3302 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3303 if (total < 0 || bmap_total < 0)
3304 return isl_basic_map_free(bmap);
3306 extra = bmap_total - total;
3308 i1 = bmap->n_ineq - 1;
3309 i2 = context->n_ineq - 1;
3310 while (bmap && i1 >= 0 && i2 >= 0) {
3311 int cmp;
3313 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3314 extra) != -1) {
3315 --i1;
3316 continue;
3318 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3319 context->ineq[i2]);
3320 if (cmp < 0) {
3321 --i2;
3322 continue;
3324 if (cmp > 0) {
3325 --i1;
3326 continue;
3328 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3329 bmap = isl_basic_map_cow(bmap);
3330 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3331 bmap = isl_basic_map_free(bmap);
3333 --i1;
3334 --i2;
3337 return bmap;
3340 /* Drop all equalities from "bmap" that also appear in "context".
3341 * "context" is assumed to have only known local variables and
3342 * the initial local variables of "bmap" are assumed to be the same
3343 * as those of "context".
3345 * Run through the equality constraints of "bmap" and "context"
3346 * in sorted order.
3347 * If a constraint of "bmap" involves variables not in "context",
3348 * then it cannot appear in "context".
3349 * If a matching constraint is found, it is removed from "bmap".
3351 static __isl_give isl_basic_map *drop_equalities(
3352 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3354 int i1, i2;
3355 isl_size total, bmap_total;
3356 unsigned extra;
3358 total = isl_basic_map_dim(context, isl_dim_all);
3359 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3360 if (total < 0 || bmap_total < 0)
3361 return isl_basic_map_free(bmap);
3363 extra = bmap_total - total;
3365 i1 = bmap->n_eq - 1;
3366 i2 = context->n_eq - 1;
3368 while (bmap && i1 >= 0 && i2 >= 0) {
3369 int last1, last2;
3371 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3372 extra) != -1)
3373 break;
3374 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3375 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3376 if (last1 > last2) {
3377 --i2;
3378 continue;
3380 if (last1 < last2) {
3381 --i1;
3382 continue;
3384 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3385 bmap = isl_basic_map_cow(bmap);
3386 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3387 bmap = isl_basic_map_free(bmap);
3389 --i1;
3390 --i2;
3393 return bmap;
3396 /* Remove the constraints in "context" from "bmap".
3397 * "context" is assumed to have explicit representations
3398 * for all local variables.
3400 * First align the divs of "bmap" to those of "context" and
3401 * sort the constraints. Then drop all constraints from "bmap"
3402 * that appear in "context".
3404 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3405 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3407 isl_bool done, known;
3409 done = isl_basic_map_plain_is_universe(context);
3410 if (done == isl_bool_false)
3411 done = isl_basic_map_plain_is_universe(bmap);
3412 if (done == isl_bool_false)
3413 done = isl_basic_map_plain_is_empty(context);
3414 if (done == isl_bool_false)
3415 done = isl_basic_map_plain_is_empty(bmap);
3416 if (done < 0)
3417 goto error;
3418 if (done) {
3419 isl_basic_map_free(context);
3420 return bmap;
3422 known = isl_basic_map_divs_known(context);
3423 if (known < 0)
3424 goto error;
3425 if (!known)
3426 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3427 "context has unknown divs", goto error);
3429 bmap = isl_basic_map_align_divs(bmap, context);
3430 bmap = isl_basic_map_gauss(bmap, NULL);
3431 bmap = isl_basic_map_sort_constraints(bmap);
3432 context = isl_basic_map_sort_constraints(context);
3434 bmap = drop_inequalities(bmap, context);
3435 bmap = drop_equalities(bmap, context);
3437 isl_basic_map_free(context);
3438 bmap = isl_basic_map_finalize(bmap);
3439 return bmap;
3440 error:
3441 isl_basic_map_free(bmap);
3442 isl_basic_map_free(context);
3443 return NULL;
3446 /* Replace "map" by the disjunct at position "pos" and free "context".
3448 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3449 int pos, __isl_take isl_basic_map *context)
3451 isl_basic_map *bmap;
3453 bmap = isl_basic_map_copy(map->p[pos]);
3454 isl_map_free(map);
3455 isl_basic_map_free(context);
3456 return isl_map_from_basic_map(bmap);
3459 /* Remove the constraints in "context" from "map".
3460 * If any of the disjuncts in the result turns out to be the universe,
3461 * then return this universe.
3462 * "context" is assumed to have explicit representations
3463 * for all local variables.
3465 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3466 __isl_take isl_basic_map *context)
3468 int i;
3469 isl_bool univ, known;
3471 univ = isl_basic_map_plain_is_universe(context);
3472 if (univ < 0)
3473 goto error;
3474 if (univ) {
3475 isl_basic_map_free(context);
3476 return map;
3478 known = isl_basic_map_divs_known(context);
3479 if (known < 0)
3480 goto error;
3481 if (!known)
3482 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3483 "context has unknown divs", goto error);
3485 map = isl_map_cow(map);
3486 if (!map)
3487 goto error;
3488 for (i = 0; i < map->n; ++i) {
3489 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3490 isl_basic_map_copy(context));
3491 univ = isl_basic_map_plain_is_universe(map->p[i]);
3492 if (univ < 0)
3493 goto error;
3494 if (univ && map->n > 1)
3495 return replace_by_disjunct(map, i, context);
3498 isl_basic_map_free(context);
3499 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3500 if (map->n > 1)
3501 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3502 return map;
3503 error:
3504 isl_map_free(map);
3505 isl_basic_map_free(context);
3506 return NULL;
3509 /* Remove the constraints in "context" from "set".
3510 * If any of the disjuncts in the result turns out to be the universe,
3511 * then return this universe.
3512 * "context" is assumed to have explicit representations
3513 * for all local variables.
3515 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3516 __isl_take isl_basic_set *context)
3518 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3519 bset_to_bmap(context)));
3522 /* Remove the constraints in "context" from "map".
3523 * If any of the disjuncts in the result turns out to be the universe,
3524 * then return this universe.
3525 * "context" is assumed to consist of a single disjunct and
3526 * to have explicit representations for all local variables.
3528 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3529 __isl_take isl_map *context)
3531 isl_basic_map *hull;
3533 hull = isl_map_unshifted_simple_hull(context);
3534 return isl_map_plain_gist_basic_map(map, hull);
3537 /* Replace "map" by a universe map in the same space and free "drop".
3539 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3540 __isl_take isl_map *drop)
3542 isl_map *res;
3544 res = isl_map_universe(isl_map_get_space(map));
3545 isl_map_free(map);
3546 isl_map_free(drop);
3547 return res;
3550 /* Return a map that has the same intersection with "context" as "map"
3551 * and that is as "simple" as possible.
3553 * If "map" is already the universe, then we cannot make it any simpler.
3554 * Similarly, if "context" is the universe, then we cannot exploit it
3555 * to simplify "map"
3556 * If "map" and "context" are identical to each other, then we can
3557 * return the corresponding universe.
3559 * If either "map" or "context" consists of multiple disjuncts,
3560 * then check if "context" happens to be a subset of "map",
3561 * in which case all constraints can be removed.
3562 * In case of multiple disjuncts, the standard procedure
3563 * may not be able to detect that all constraints can be removed.
3565 * If none of these cases apply, we have to work a bit harder.
3566 * During this computation, we make use of a single disjunct context,
3567 * so if the original context consists of more than one disjunct
3568 * then we need to approximate the context by a single disjunct set.
3569 * Simply taking the simple hull may drop constraints that are
3570 * only implicitly available in each disjunct. We therefore also
3571 * look for constraints among those defining "map" that are valid
3572 * for the context. These can then be used to simplify away
3573 * the corresponding constraints in "map".
3575 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3576 __isl_take isl_map *context)
3578 int equal;
3579 int is_universe;
3580 isl_size n_disjunct_map, n_disjunct_context;
3581 isl_bool subset;
3582 isl_basic_map *hull;
3584 is_universe = isl_map_plain_is_universe(map);
3585 if (is_universe >= 0 && !is_universe)
3586 is_universe = isl_map_plain_is_universe(context);
3587 if (is_universe < 0)
3588 goto error;
3589 if (is_universe) {
3590 isl_map_free(context);
3591 return map;
3594 isl_map_align_params_bin(&map, &context);
3595 equal = isl_map_plain_is_equal(map, context);
3596 if (equal < 0)
3597 goto error;
3598 if (equal)
3599 return replace_by_universe(map, context);
3601 n_disjunct_map = isl_map_n_basic_map(map);
3602 n_disjunct_context = isl_map_n_basic_map(context);
3603 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3604 goto error;
3605 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3606 subset = isl_map_is_subset(context, map);
3607 if (subset < 0)
3608 goto error;
3609 if (subset)
3610 return replace_by_universe(map, context);
3613 context = isl_map_compute_divs(context);
3614 if (!context)
3615 goto error;
3616 if (n_disjunct_context == 1) {
3617 hull = isl_map_simple_hull(context);
3618 } else {
3619 isl_ctx *ctx;
3620 isl_map_list *list;
3622 ctx = isl_map_get_ctx(map);
3623 list = isl_map_list_alloc(ctx, 2);
3624 list = isl_map_list_add(list, isl_map_copy(context));
3625 list = isl_map_list_add(list, isl_map_copy(map));
3626 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3627 list);
3629 return isl_map_gist_basic_map(map, hull);
3630 error:
3631 isl_map_free(map);
3632 isl_map_free(context);
3633 return NULL;
3636 __isl_give isl_basic_set *isl_basic_set_gist(__isl_take isl_basic_set *bset,
3637 __isl_take isl_basic_set *context)
3639 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3640 bset_to_bmap(context)));
3643 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3644 __isl_take isl_basic_set *context)
3646 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3647 bset_to_bmap(context)));
3650 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3651 __isl_take isl_basic_set *context)
3653 isl_space *space = isl_set_get_space(set);
3654 isl_basic_set *dom_context = isl_basic_set_universe(space);
3655 dom_context = isl_basic_set_intersect_params(dom_context, context);
3656 return isl_set_gist_basic_set(set, dom_context);
3659 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3660 __isl_take isl_set *context)
3662 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3665 /* Compute the gist of "bmap" with respect to the constraints "context"
3666 * on the domain.
3668 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3669 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3671 isl_space *space = isl_basic_map_get_space(bmap);
3672 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3674 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3675 return isl_basic_map_gist(bmap, bmap_context);
3678 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3679 __isl_take isl_set *context)
3681 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3682 map_context = isl_map_intersect_domain(map_context, context);
3683 return isl_map_gist(map, map_context);
3686 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3687 __isl_take isl_set *context)
3689 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3690 map_context = isl_map_intersect_range(map_context, context);
3691 return isl_map_gist(map, map_context);
3694 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3695 __isl_take isl_set *context)
3697 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3698 map_context = isl_map_intersect_params(map_context, context);
3699 return isl_map_gist(map, map_context);
3702 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3703 __isl_take isl_set *context)
3705 return isl_map_gist_params(set, context);
3708 /* Quick check to see if two basic maps are disjoint.
3709 * In particular, we reduce the equalities and inequalities of
3710 * one basic map in the context of the equalities of the other
3711 * basic map and check if we get a contradiction.
3713 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3714 __isl_keep isl_basic_map *bmap2)
3716 struct isl_vec *v = NULL;
3717 int *elim = NULL;
3718 isl_size total;
3719 int i;
3721 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3722 return isl_bool_error;
3723 if (bmap1->n_div || bmap2->n_div)
3724 return isl_bool_false;
3725 if (!bmap1->n_eq && !bmap2->n_eq)
3726 return isl_bool_false;
3728 total = isl_space_dim(bmap1->dim, isl_dim_all);
3729 if (total < 0)
3730 return isl_bool_error;
3731 if (total == 0)
3732 return isl_bool_false;
3733 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3734 if (!v)
3735 goto error;
3736 elim = isl_alloc_array(bmap1->ctx, int, total);
3737 if (!elim)
3738 goto error;
3739 compute_elimination_index(bmap1, elim);
3740 for (i = 0; i < bmap2->n_eq; ++i) {
3741 int reduced;
3742 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3743 bmap1, elim);
3744 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3745 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3746 goto disjoint;
3748 for (i = 0; i < bmap2->n_ineq; ++i) {
3749 int reduced;
3750 reduced = reduced_using_equalities(v->block.data,
3751 bmap2->ineq[i], bmap1, elim);
3752 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3753 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3754 goto disjoint;
3756 compute_elimination_index(bmap2, elim);
3757 for (i = 0; i < bmap1->n_ineq; ++i) {
3758 int reduced;
3759 reduced = reduced_using_equalities(v->block.data,
3760 bmap1->ineq[i], bmap2, elim);
3761 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3762 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3763 goto disjoint;
3765 isl_vec_free(v);
3766 free(elim);
3767 return isl_bool_false;
3768 disjoint:
3769 isl_vec_free(v);
3770 free(elim);
3771 return isl_bool_true;
3772 error:
3773 isl_vec_free(v);
3774 free(elim);
3775 return isl_bool_error;
3778 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3779 __isl_keep isl_basic_set *bset2)
3781 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3782 bset_to_bmap(bset2));
3785 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3787 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3788 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3789 __isl_keep isl_basic_map *bmap2))
3791 int i, j;
3793 if (!map1 || !map2)
3794 return isl_bool_error;
3796 for (i = 0; i < map1->n; ++i) {
3797 for (j = 0; j < map2->n; ++j) {
3798 isl_bool d = test(map1->p[i], map2->p[j]);
3799 if (d != isl_bool_true)
3800 return d;
3804 return isl_bool_true;
3807 /* Are "map1" and "map2" obviously disjoint, based on information
3808 * that can be derived without looking at the individual basic maps?
3810 * In particular, if one of them is empty or if they live in different spaces
3811 * (ignoring parameters), then they are clearly disjoint.
3813 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3814 __isl_keep isl_map *map2)
3816 isl_bool disjoint;
3817 isl_bool match;
3819 if (!map1 || !map2)
3820 return isl_bool_error;
3822 disjoint = isl_map_plain_is_empty(map1);
3823 if (disjoint < 0 || disjoint)
3824 return disjoint;
3826 disjoint = isl_map_plain_is_empty(map2);
3827 if (disjoint < 0 || disjoint)
3828 return disjoint;
3830 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3831 map2->dim, isl_dim_in);
3832 if (match < 0 || !match)
3833 return match < 0 ? isl_bool_error : isl_bool_true;
3835 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3836 map2->dim, isl_dim_out);
3837 if (match < 0 || !match)
3838 return match < 0 ? isl_bool_error : isl_bool_true;
3840 return isl_bool_false;
3843 /* Are "map1" and "map2" obviously disjoint?
3845 * If one of them is empty or if they live in different spaces (ignoring
3846 * parameters), then they are clearly disjoint.
3847 * This is checked by isl_map_plain_is_disjoint_global.
3849 * If they have different parameters, then we skip any further tests.
3851 * If they are obviously equal, but not obviously empty, then we will
3852 * not be able to detect if they are disjoint.
3854 * Otherwise we check if each basic map in "map1" is obviously disjoint
3855 * from each basic map in "map2".
3857 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3858 __isl_keep isl_map *map2)
3860 isl_bool disjoint;
3861 isl_bool intersect;
3862 isl_bool match;
3864 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3865 if (disjoint < 0 || disjoint)
3866 return disjoint;
3868 match = isl_map_has_equal_params(map1, map2);
3869 if (match < 0 || !match)
3870 return match < 0 ? isl_bool_error : isl_bool_false;
3872 intersect = isl_map_plain_is_equal(map1, map2);
3873 if (intersect < 0 || intersect)
3874 return intersect < 0 ? isl_bool_error : isl_bool_false;
3876 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3879 /* Are "map1" and "map2" disjoint?
3880 * The parameters are assumed to have been aligned.
3882 * In particular, check whether all pairs of basic maps are disjoint.
3884 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3885 __isl_keep isl_map *map2)
3887 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3890 /* Are "map1" and "map2" disjoint?
3892 * They are disjoint if they are "obviously disjoint" or if one of them
3893 * is empty. Otherwise, they are not disjoint if one of them is universal.
3894 * If the two inputs are (obviously) equal and not empty, then they are
3895 * not disjoint.
3896 * If none of these cases apply, then check if all pairs of basic maps
3897 * are disjoint after aligning the parameters.
3899 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3901 isl_bool disjoint;
3902 isl_bool intersect;
3904 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3905 if (disjoint < 0 || disjoint)
3906 return disjoint;
3908 disjoint = isl_map_is_empty(map1);
3909 if (disjoint < 0 || disjoint)
3910 return disjoint;
3912 disjoint = isl_map_is_empty(map2);
3913 if (disjoint < 0 || disjoint)
3914 return disjoint;
3916 intersect = isl_map_plain_is_universe(map1);
3917 if (intersect < 0 || intersect)
3918 return isl_bool_not(intersect);
3920 intersect = isl_map_plain_is_universe(map2);
3921 if (intersect < 0 || intersect)
3922 return isl_bool_not(intersect);
3924 intersect = isl_map_plain_is_equal(map1, map2);
3925 if (intersect < 0 || intersect)
3926 return isl_bool_not(intersect);
3928 return isl_map_align_params_map_map_and_test(map1, map2,
3929 &isl_map_is_disjoint_aligned);
3932 /* Are "bmap1" and "bmap2" disjoint?
3934 * They are disjoint if they are "obviously disjoint" or if one of them
3935 * is empty. Otherwise, they are not disjoint if one of them is universal.
3936 * If none of these cases apply, we compute the intersection and see if
3937 * the result is empty.
3939 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3940 __isl_keep isl_basic_map *bmap2)
3942 isl_bool disjoint;
3943 isl_bool intersect;
3944 isl_basic_map *test;
3946 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3947 if (disjoint < 0 || disjoint)
3948 return disjoint;
3950 disjoint = isl_basic_map_is_empty(bmap1);
3951 if (disjoint < 0 || disjoint)
3952 return disjoint;
3954 disjoint = isl_basic_map_is_empty(bmap2);
3955 if (disjoint < 0 || disjoint)
3956 return disjoint;
3958 intersect = isl_basic_map_plain_is_universe(bmap1);
3959 if (intersect < 0 || intersect)
3960 return isl_bool_not(intersect);
3962 intersect = isl_basic_map_plain_is_universe(bmap2);
3963 if (intersect < 0 || intersect)
3964 return isl_bool_not(intersect);
3966 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3967 isl_basic_map_copy(bmap2));
3968 disjoint = isl_basic_map_is_empty(test);
3969 isl_basic_map_free(test);
3971 return disjoint;
3974 /* Are "bset1" and "bset2" disjoint?
3976 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3977 __isl_keep isl_basic_set *bset2)
3979 return isl_basic_map_is_disjoint(bset1, bset2);
3982 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3983 __isl_keep isl_set *set2)
3985 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3988 /* Are "set1" and "set2" disjoint?
3990 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3992 return isl_map_is_disjoint(set1, set2);
3995 /* Is "v" equal to 0, 1 or -1?
3997 static int is_zero_or_one(isl_int v)
3999 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4002 /* Are the "n" coefficients starting at "first" of inequality constraints
4003 * "i" and "j" of "bmap" opposite to each other?
4005 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4006 int first, int n)
4008 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4011 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4012 * apart from the constant term?
4014 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4016 isl_size total;
4018 total = isl_basic_map_dim(bmap, isl_dim_all);
4019 if (total < 0)
4020 return isl_bool_error;
4021 return is_opposite_part(bmap, i, j, 1, total);
4024 /* Check if we can combine a given div with lower bound l and upper
4025 * bound u with some other div and if so return that other div.
4026 * Otherwise, return a position beyond the integer divisions.
4027 * Return -1 on error.
4029 * We first check that
4030 * - the bounds are opposites of each other (except for the constant
4031 * term)
4032 * - the bounds do not reference any other div
4033 * - no div is defined in terms of this div
4035 * Let m be the size of the range allowed on the div by the bounds.
4036 * That is, the bounds are of the form
4038 * e <= a <= e + m - 1
4040 * with e some expression in the other variables.
4041 * We look for another div b such that no third div is defined in terms
4042 * of this second div b and such that in any constraint that contains
4043 * a (except for the given lower and upper bound), also contains b
4044 * with a coefficient that is m times that of b.
4045 * That is, all constraints (except for the lower and upper bound)
4046 * are of the form
4048 * e + f (a + m b) >= 0
4050 * Furthermore, in the constraints that only contain b, the coefficient
4051 * of b should be equal to 1 or -1.
4052 * If so, we return b so that "a + m b" can be replaced by
4053 * a single div "c = a + m b".
4055 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4056 unsigned div, unsigned l, unsigned u)
4058 int i, j;
4059 unsigned n_div;
4060 isl_size v_div;
4061 int coalesce;
4062 isl_bool opp;
4064 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4065 if (n_div <= 1)
4066 return n_div;
4067 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4068 if (v_div < 0)
4069 return -1;
4070 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4071 return n_div;
4072 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4073 n_div - div - 1) != -1)
4074 return n_div;
4075 opp = is_opposite(bmap, l, u);
4076 if (opp < 0 || !opp)
4077 return opp < 0 ? -1 : n_div;
4079 for (i = 0; i < n_div; ++i) {
4080 if (isl_int_is_zero(bmap->div[i][0]))
4081 continue;
4082 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4083 return n_div;
4086 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4087 if (isl_int_is_neg(bmap->ineq[l][0])) {
4088 isl_int_sub(bmap->ineq[l][0],
4089 bmap->ineq[l][0], bmap->ineq[u][0]);
4090 bmap = isl_basic_map_copy(bmap);
4091 bmap = isl_basic_map_set_to_empty(bmap);
4092 isl_basic_map_free(bmap);
4093 return n_div;
4095 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4096 coalesce = n_div;
4097 for (i = 0; i < n_div; ++i) {
4098 if (i == div)
4099 continue;
4100 if (!pairs[i])
4101 continue;
4102 for (j = 0; j < n_div; ++j) {
4103 if (isl_int_is_zero(bmap->div[j][0]))
4104 continue;
4105 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
4106 break;
4108 if (j < n_div)
4109 continue;
4110 for (j = 0; j < bmap->n_ineq; ++j) {
4111 int valid;
4112 if (j == l || j == u)
4113 continue;
4114 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4115 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4116 continue;
4117 break;
4119 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4120 break;
4121 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4122 bmap->ineq[j][1 + v_div + div],
4123 bmap->ineq[l][0]);
4124 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4125 bmap->ineq[j][1 + v_div + i]);
4126 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4127 bmap->ineq[j][1 + v_div + div],
4128 bmap->ineq[l][0]);
4129 if (!valid)
4130 break;
4132 if (j < bmap->n_ineq)
4133 continue;
4134 coalesce = i;
4135 break;
4137 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4138 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4139 return coalesce;
4142 /* Internal data structure used during the construction and/or evaluation of
4143 * an inequality that ensures that a pair of bounds always allows
4144 * for an integer value.
4146 * "tab" is the tableau in which the inequality is evaluated. It may
4147 * be NULL until it is actually needed.
4148 * "v" contains the inequality coefficients.
4149 * "g", "fl" and "fu" are temporary scalars used during the construction and
4150 * evaluation.
4152 struct test_ineq_data {
4153 struct isl_tab *tab;
4154 isl_vec *v;
4155 isl_int g;
4156 isl_int fl;
4157 isl_int fu;
4160 /* Free all the memory allocated by the fields of "data".
4162 static void test_ineq_data_clear(struct test_ineq_data *data)
4164 isl_tab_free(data->tab);
4165 isl_vec_free(data->v);
4166 isl_int_clear(data->g);
4167 isl_int_clear(data->fl);
4168 isl_int_clear(data->fu);
4171 /* Is the inequality stored in data->v satisfied by "bmap"?
4172 * That is, does it only attain non-negative values?
4173 * data->tab is a tableau corresponding to "bmap".
4175 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4176 struct test_ineq_data *data)
4178 isl_ctx *ctx;
4179 enum isl_lp_result res;
4181 ctx = isl_basic_map_get_ctx(bmap);
4182 if (!data->tab)
4183 data->tab = isl_tab_from_basic_map(bmap, 0);
4184 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4185 if (res == isl_lp_error)
4186 return isl_bool_error;
4187 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4190 /* Given a lower and an upper bound on div i, do they always allow
4191 * for an integer value of the given div?
4192 * Determine this property by constructing an inequality
4193 * such that the property is guaranteed when the inequality is nonnegative.
4194 * The lower bound is inequality l, while the upper bound is inequality u.
4195 * The constructed inequality is stored in data->v.
4197 * Let the upper bound be
4199 * -n_u a + e_u >= 0
4201 * and the lower bound
4203 * n_l a + e_l >= 0
4205 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4206 * We have
4208 * - f_u e_l <= f_u f_l g a <= f_l e_u
4210 * Since all variables are integer valued, this is equivalent to
4212 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4214 * If this interval is at least f_u f_l g, then it contains at least
4215 * one integer value for a.
4216 * That is, the test constraint is
4218 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4220 * or
4222 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4224 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4225 * then the constraint can be scaled down by a factor g',
4226 * with the constant term replaced by
4227 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4228 * Note that the result of applying Fourier-Motzkin to this pair
4229 * of constraints is
4231 * f_l e_u + f_u e_l >= 0
4233 * If the constant term of the scaled down version of this constraint,
4234 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4235 * term of the scaled down test constraint, then the test constraint
4236 * is known to hold and no explicit evaluation is required.
4237 * This is essentially the Omega test.
4239 * If the test constraint consists of only a constant term, then
4240 * it is sufficient to look at the sign of this constant term.
4242 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4243 int l, int u, struct test_ineq_data *data)
4245 unsigned offset;
4246 isl_size n_div;
4248 offset = isl_basic_map_offset(bmap, isl_dim_div);
4249 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4250 if (n_div < 0)
4251 return isl_bool_error;
4253 isl_int_gcd(data->g,
4254 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4255 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4256 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4257 isl_int_neg(data->fu, data->fu);
4258 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4259 data->fu, bmap->ineq[l], offset + n_div);
4260 isl_int_mul(data->g, data->g, data->fl);
4261 isl_int_mul(data->g, data->g, data->fu);
4262 isl_int_sub(data->g, data->g, data->fl);
4263 isl_int_sub(data->g, data->g, data->fu);
4264 isl_int_add_ui(data->g, data->g, 1);
4265 isl_int_sub(data->fl, data->v->el[0], data->g);
4267 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4268 if (isl_int_is_zero(data->g))
4269 return isl_int_is_nonneg(data->fl);
4270 if (isl_int_is_one(data->g)) {
4271 isl_int_set(data->v->el[0], data->fl);
4272 return test_ineq_is_satisfied(bmap, data);
4274 isl_int_fdiv_q(data->fl, data->fl, data->g);
4275 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4276 if (isl_int_eq(data->fl, data->v->el[0]))
4277 return isl_bool_true;
4278 isl_int_set(data->v->el[0], data->fl);
4279 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4280 offset - 1 + n_div);
4282 return test_ineq_is_satisfied(bmap, data);
4285 /* Remove more kinds of divs that are not strictly needed.
4286 * In particular, if all pairs of lower and upper bounds on a div
4287 * are such that they allow at least one integer value of the div,
4288 * then we can eliminate the div using Fourier-Motzkin without
4289 * introducing any spurious solutions.
4291 * If at least one of the two constraints has a unit coefficient for the div,
4292 * then the presence of such a value is guaranteed so there is no need to check.
4293 * In particular, the value attained by the bound with unit coefficient
4294 * can serve as this intermediate value.
4296 static __isl_give isl_basic_map *drop_more_redundant_divs(
4297 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4299 isl_ctx *ctx;
4300 struct test_ineq_data data = { NULL, NULL };
4301 unsigned off;
4302 isl_size n_div;
4303 int remove = -1;
4305 isl_int_init(data.g);
4306 isl_int_init(data.fl);
4307 isl_int_init(data.fu);
4309 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4310 if (n_div < 0)
4311 goto error;
4313 ctx = isl_basic_map_get_ctx(bmap);
4314 off = isl_basic_map_offset(bmap, isl_dim_div);
4315 data.v = isl_vec_alloc(ctx, off + n_div);
4316 if (!data.v)
4317 goto error;
4319 while (n > 0) {
4320 int i, l, u;
4321 int best = -1;
4322 isl_bool has_int;
4324 for (i = 0; i < n_div; ++i) {
4325 if (!pairs[i])
4326 continue;
4327 if (best >= 0 && pairs[best] <= pairs[i])
4328 continue;
4329 best = i;
4332 i = best;
4333 for (l = 0; l < bmap->n_ineq; ++l) {
4334 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4335 continue;
4336 if (isl_int_is_one(bmap->ineq[l][off + i]))
4337 continue;
4338 for (u = 0; u < bmap->n_ineq; ++u) {
4339 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4340 continue;
4341 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4342 continue;
4343 has_int = int_between_bounds(bmap, i, l, u,
4344 &data);
4345 if (has_int < 0)
4346 goto error;
4347 if (data.tab && data.tab->empty)
4348 break;
4349 if (!has_int)
4350 break;
4352 if (u < bmap->n_ineq)
4353 break;
4355 if (data.tab && data.tab->empty) {
4356 bmap = isl_basic_map_set_to_empty(bmap);
4357 break;
4359 if (l == bmap->n_ineq) {
4360 remove = i;
4361 break;
4363 pairs[i] = 0;
4364 --n;
4367 test_ineq_data_clear(&data);
4369 free(pairs);
4371 if (remove < 0)
4372 return bmap;
4374 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4375 return isl_basic_map_drop_redundant_divs(bmap);
4376 error:
4377 free(pairs);
4378 isl_basic_map_free(bmap);
4379 test_ineq_data_clear(&data);
4380 return NULL;
4383 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4384 * and the upper bound u, div1 always occurs together with div2 in the form
4385 * (div1 + m div2), where m is the constant range on the variable div1
4386 * allowed by l and u, replace the pair div1 and div2 by a single
4387 * div that is equal to div1 + m div2.
4389 * The new div will appear in the location that contains div2.
4390 * We need to modify all constraints that contain
4391 * div2 = (div - div1) / m
4392 * The coefficient of div2 is known to be equal to 1 or -1.
4393 * (If a constraint does not contain div2, it will also not contain div1.)
4394 * If the constraint also contains div1, then we know they appear
4395 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4396 * i.e., the coefficient of div is f.
4398 * Otherwise, we first need to introduce div1 into the constraint.
4399 * Let l be
4401 * div1 + f >=0
4403 * and u
4405 * -div1 + f' >= 0
4407 * A lower bound on div2
4409 * div2 + t >= 0
4411 * can be replaced by
4413 * m div2 + div1 + m t + f >= 0
4415 * An upper bound
4417 * -div2 + t >= 0
4419 * can be replaced by
4421 * -(m div2 + div1) + m t + f' >= 0
4423 * These constraint are those that we would obtain from eliminating
4424 * div1 using Fourier-Motzkin.
4426 * After all constraints have been modified, we drop the lower and upper
4427 * bound and then drop div1.
4428 * Since the new div is only placed in the same location that used
4429 * to store div2, but otherwise has a different meaning, any possible
4430 * explicit representation of the original div2 is removed.
4432 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4433 unsigned div1, unsigned div2, unsigned l, unsigned u)
4435 isl_ctx *ctx;
4436 isl_int m;
4437 isl_size v_div;
4438 unsigned total;
4439 int i;
4441 ctx = isl_basic_map_get_ctx(bmap);
4443 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4444 if (v_div < 0)
4445 return isl_basic_map_free(bmap);
4446 total = 1 + v_div + bmap->n_div;
4448 isl_int_init(m);
4449 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4450 isl_int_add_ui(m, m, 1);
4452 for (i = 0; i < bmap->n_ineq; ++i) {
4453 if (i == l || i == u)
4454 continue;
4455 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4456 continue;
4457 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4458 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4459 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4460 ctx->one, bmap->ineq[l], total);
4461 else
4462 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4463 ctx->one, bmap->ineq[u], total);
4465 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4466 bmap->ineq[i][1 + v_div + div1]);
4467 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4470 isl_int_clear(m);
4471 if (l > u) {
4472 isl_basic_map_drop_inequality(bmap, l);
4473 isl_basic_map_drop_inequality(bmap, u);
4474 } else {
4475 isl_basic_map_drop_inequality(bmap, u);
4476 isl_basic_map_drop_inequality(bmap, l);
4478 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4479 bmap = isl_basic_map_drop_div(bmap, div1);
4480 return bmap;
4483 /* First check if we can coalesce any pair of divs and
4484 * then continue with dropping more redundant divs.
4486 * We loop over all pairs of lower and upper bounds on a div
4487 * with coefficient 1 and -1, respectively, check if there
4488 * is any other div "c" with which we can coalesce the div
4489 * and if so, perform the coalescing.
4491 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4492 __isl_take isl_basic_map *bmap, int *pairs, int n)
4494 int i, l, u;
4495 isl_size v_div;
4496 isl_size n_div;
4498 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4499 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4500 if (v_div < 0 || n_div < 0)
4501 return isl_basic_map_free(bmap);
4503 for (i = 0; i < n_div; ++i) {
4504 if (!pairs[i])
4505 continue;
4506 for (l = 0; l < bmap->n_ineq; ++l) {
4507 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4508 continue;
4509 for (u = 0; u < bmap->n_ineq; ++u) {
4510 int c;
4512 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4513 continue;
4514 c = div_find_coalesce(bmap, pairs, i, l, u);
4515 if (c < 0)
4516 goto error;
4517 if (c >= n_div)
4518 continue;
4519 free(pairs);
4520 bmap = coalesce_divs(bmap, i, c, l, u);
4521 return isl_basic_map_drop_redundant_divs(bmap);
4526 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4527 free(pairs);
4528 return bmap;
4531 return drop_more_redundant_divs(bmap, pairs, n);
4532 error:
4533 free(pairs);
4534 isl_basic_map_free(bmap);
4535 return NULL;
4538 /* Are the "n" coefficients starting at "first" of inequality constraints
4539 * "i" and "j" of "bmap" equal to each other?
4541 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4542 int first, int n)
4544 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4547 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4548 * apart from the constant term and the coefficient at position "pos"?
4550 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4551 int pos)
4553 isl_size total;
4555 total = isl_basic_map_dim(bmap, isl_dim_all);
4556 if (total < 0)
4557 return isl_bool_error;
4558 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4559 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4562 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4563 * apart from the constant term and the coefficient at position "pos"?
4565 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4566 int pos)
4568 isl_size total;
4570 total = isl_basic_map_dim(bmap, isl_dim_all);
4571 if (total < 0)
4572 return isl_bool_error;
4573 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4574 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4577 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4578 * been modified, simplying it if "simplify" is set.
4579 * Free the temporary data structure "pairs" that was associated
4580 * to the old version of "bmap".
4582 static __isl_give isl_basic_map *drop_redundant_divs_again(
4583 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4585 if (simplify)
4586 bmap = isl_basic_map_simplify(bmap);
4587 free(pairs);
4588 return isl_basic_map_drop_redundant_divs(bmap);
4591 /* Is "div" the single unknown existentially quantified variable
4592 * in inequality constraint "ineq" of "bmap"?
4593 * "div" is known to have a non-zero coefficient in "ineq".
4595 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4596 int div)
4598 int i;
4599 isl_size n_div;
4600 unsigned o_div;
4601 isl_bool known;
4603 known = isl_basic_map_div_is_known(bmap, div);
4604 if (known < 0 || known)
4605 return isl_bool_not(known);
4606 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4607 if (n_div < 0)
4608 return isl_bool_error;
4609 if (n_div == 1)
4610 return isl_bool_true;
4611 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4612 for (i = 0; i < n_div; ++i) {
4613 isl_bool known;
4615 if (i == div)
4616 continue;
4617 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4618 continue;
4619 known = isl_basic_map_div_is_known(bmap, i);
4620 if (known < 0 || !known)
4621 return known;
4624 return isl_bool_true;
4627 /* Does integer division "div" have coefficient 1 in inequality constraint
4628 * "ineq" of "map"?
4630 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4632 unsigned o_div;
4634 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4635 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4636 return isl_bool_true;
4638 return isl_bool_false;
4641 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4642 * then try and drop redundant divs again,
4643 * freeing the temporary data structure "pairs" that was associated
4644 * to the old version of "bmap".
4646 static __isl_give isl_basic_map *set_eq_and_try_again(
4647 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4649 bmap = isl_basic_map_cow(bmap);
4650 isl_basic_map_inequality_to_equality(bmap, ineq);
4651 return drop_redundant_divs_again(bmap, pairs, 1);
4654 /* Drop the integer division at position "div", along with the two
4655 * inequality constraints "ineq1" and "ineq2" in which it appears
4656 * from "bmap" and then try and drop redundant divs again,
4657 * freeing the temporary data structure "pairs" that was associated
4658 * to the old version of "bmap".
4660 static __isl_give isl_basic_map *drop_div_and_try_again(
4661 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4662 __isl_take int *pairs)
4664 if (ineq1 > ineq2) {
4665 isl_basic_map_drop_inequality(bmap, ineq1);
4666 isl_basic_map_drop_inequality(bmap, ineq2);
4667 } else {
4668 isl_basic_map_drop_inequality(bmap, ineq2);
4669 isl_basic_map_drop_inequality(bmap, ineq1);
4671 bmap = isl_basic_map_drop_div(bmap, div);
4672 return drop_redundant_divs_again(bmap, pairs, 0);
4675 /* Given two inequality constraints
4677 * f(x) + n d + c >= 0, (ineq)
4679 * with d the variable at position "pos", and
4681 * f(x) + c0 >= 0, (lower)
4683 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4684 * determined by the first constraint.
4685 * That is, store
4687 * ceil((c0 - c)/n)
4689 * in *l.
4691 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4692 int ineq, int lower, int pos, isl_int *l)
4694 isl_int_neg(*l, bmap->ineq[ineq][0]);
4695 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4696 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4699 /* Given two inequality constraints
4701 * f(x) + n d + c >= 0, (ineq)
4703 * with d the variable at position "pos", and
4705 * -f(x) - c0 >= 0, (upper)
4707 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4708 * determined by the first constraint.
4709 * That is, store
4711 * ceil((-c1 - c)/n)
4713 * in *u.
4715 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4716 int ineq, int upper, int pos, isl_int *u)
4718 isl_int_neg(*u, bmap->ineq[ineq][0]);
4719 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4720 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4723 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4724 * does the corresponding lower bound have a fixed value in "bmap"?
4726 * In particular, "ineq" is of the form
4728 * f(x) + n d + c >= 0
4730 * with n > 0, c the constant term and
4731 * d the existentially quantified variable "div".
4732 * That is, the lower bound is
4734 * ceil((-f(x) - c)/n)
4736 * Look for a pair of constraints
4738 * f(x) + c0 >= 0
4739 * -f(x) + c1 >= 0
4741 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4742 * That is, check that
4744 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4746 * If so, return the index of inequality f(x) + c0 >= 0.
4747 * Otherwise, return bmap->n_ineq.
4748 * Return -1 on error.
4750 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4752 int i;
4753 int lower = -1, upper = -1;
4754 unsigned o_div;
4755 isl_int l, u;
4756 int equal;
4758 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4759 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4760 isl_bool par, opp;
4762 if (i == ineq)
4763 continue;
4764 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4765 continue;
4766 par = isl_bool_false;
4767 if (lower < 0)
4768 par = is_parallel_except(bmap, ineq, i, o_div + div);
4769 if (par < 0)
4770 return -1;
4771 if (par) {
4772 lower = i;
4773 continue;
4775 opp = isl_bool_false;
4776 if (upper < 0)
4777 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4778 if (opp < 0)
4779 return -1;
4780 if (opp)
4781 upper = i;
4784 if (lower < 0 || upper < 0)
4785 return bmap->n_ineq;
4787 isl_int_init(l);
4788 isl_int_init(u);
4790 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4791 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4793 equal = isl_int_eq(l, u);
4795 isl_int_clear(l);
4796 isl_int_clear(u);
4798 return equal ? lower : bmap->n_ineq;
4801 /* Given a lower bound constraint "ineq" on the existentially quantified
4802 * variable "div", such that the corresponding lower bound has
4803 * a fixed value in "bmap", assign this fixed value to the variable and
4804 * then try and drop redundant divs again,
4805 * freeing the temporary data structure "pairs" that was associated
4806 * to the old version of "bmap".
4807 * "lower" determines the constant value for the lower bound.
4809 * In particular, "ineq" is of the form
4811 * f(x) + n d + c >= 0,
4813 * while "lower" is of the form
4815 * f(x) + c0 >= 0
4817 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4818 * is ceil((c0 - c)/n).
4820 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4821 int div, int ineq, int lower, int *pairs)
4823 isl_int c;
4824 unsigned o_div;
4826 isl_int_init(c);
4828 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4829 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4830 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4831 free(pairs);
4833 isl_int_clear(c);
4835 return isl_basic_map_drop_redundant_divs(bmap);
4838 /* Remove divs that are not strictly needed based on the inequality
4839 * constraints.
4840 * In particular, if a div only occurs positively (or negatively)
4841 * in constraints, then it can simply be dropped.
4842 * Also, if a div occurs in only two constraints and if moreover
4843 * those two constraints are opposite to each other, except for the constant
4844 * term and if the sum of the constant terms is such that for any value
4845 * of the other values, there is always at least one integer value of the
4846 * div, i.e., if one plus this sum is greater than or equal to
4847 * the (absolute value) of the coefficient of the div in the constraints,
4848 * then we can also simply drop the div.
4850 * If an existentially quantified variable does not have an explicit
4851 * representation, appears in only a single lower bound that does not
4852 * involve any other such existentially quantified variables and appears
4853 * in this lower bound with coefficient 1,
4854 * then fix the variable to the value of the lower bound. That is,
4855 * turn the inequality into an equality.
4856 * If for any value of the other variables, there is any value
4857 * for the existentially quantified variable satisfying the constraints,
4858 * then this lower bound also satisfies the constraints.
4859 * It is therefore safe to pick this lower bound.
4861 * The same reasoning holds even if the coefficient is not one.
4862 * However, fixing the variable to the value of the lower bound may
4863 * in general introduce an extra integer division, in which case
4864 * it may be better to pick another value.
4865 * If this integer division has a known constant value, then plugging
4866 * in this constant value removes the existentially quantified variable
4867 * completely. In particular, if the lower bound is of the form
4868 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4869 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4870 * then the existentially quantified variable can be assigned this
4871 * shared value.
4873 * We skip divs that appear in equalities or in the definition of other divs.
4874 * Divs that appear in the definition of other divs usually occur in at least
4875 * 4 constraints, but the constraints may have been simplified.
4877 * If any divs are left after these simple checks then we move on
4878 * to more complicated cases in drop_more_redundant_divs.
4880 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4881 __isl_take isl_basic_map *bmap)
4883 int i, j;
4884 isl_size off;
4885 int *pairs = NULL;
4886 int n = 0;
4887 isl_size n_ineq;
4889 if (!bmap)
4890 goto error;
4891 if (bmap->n_div == 0)
4892 return bmap;
4894 off = isl_basic_map_var_offset(bmap, isl_dim_div);
4895 if (off < 0)
4896 return isl_basic_map_free(bmap);
4897 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4898 if (!pairs)
4899 goto error;
4901 n_ineq = isl_basic_map_n_inequality(bmap);
4902 if (n_ineq < 0)
4903 goto error;
4904 for (i = 0; i < bmap->n_div; ++i) {
4905 int pos, neg;
4906 int last_pos, last_neg;
4907 int redundant;
4908 int defined;
4909 isl_bool opp, set_div;
4911 defined = !isl_int_is_zero(bmap->div[i][0]);
4912 for (j = i; j < bmap->n_div; ++j)
4913 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4914 break;
4915 if (j < bmap->n_div)
4916 continue;
4917 for (j = 0; j < bmap->n_eq; ++j)
4918 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4919 break;
4920 if (j < bmap->n_eq)
4921 continue;
4922 ++n;
4923 pos = neg = 0;
4924 for (j = 0; j < bmap->n_ineq; ++j) {
4925 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4926 last_pos = j;
4927 ++pos;
4929 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4930 last_neg = j;
4931 ++neg;
4934 pairs[i] = pos * neg;
4935 if (pairs[i] == 0) {
4936 for (j = bmap->n_ineq - 1; j >= 0; --j)
4937 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4938 isl_basic_map_drop_inequality(bmap, j);
4939 bmap = isl_basic_map_drop_div(bmap, i);
4940 return drop_redundant_divs_again(bmap, pairs, 0);
4942 if (pairs[i] != 1)
4943 opp = isl_bool_false;
4944 else
4945 opp = is_opposite(bmap, last_pos, last_neg);
4946 if (opp < 0)
4947 goto error;
4948 if (!opp) {
4949 int lower;
4950 isl_bool single, one;
4952 if (pos != 1)
4953 continue;
4954 single = single_unknown(bmap, last_pos, i);
4955 if (single < 0)
4956 goto error;
4957 if (!single)
4958 continue;
4959 one = has_coef_one(bmap, i, last_pos);
4960 if (one < 0)
4961 goto error;
4962 if (one)
4963 return set_eq_and_try_again(bmap, last_pos,
4964 pairs);
4965 lower = lower_bound_is_cst(bmap, i, last_pos);
4966 if (lower < 0)
4967 goto error;
4968 if (lower < n_ineq)
4969 return fix_cst_lower(bmap, i, last_pos, lower,
4970 pairs);
4971 continue;
4974 isl_int_add(bmap->ineq[last_pos][0],
4975 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4976 isl_int_add_ui(bmap->ineq[last_pos][0],
4977 bmap->ineq[last_pos][0], 1);
4978 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4979 bmap->ineq[last_pos][1+off+i]);
4980 isl_int_sub_ui(bmap->ineq[last_pos][0],
4981 bmap->ineq[last_pos][0], 1);
4982 isl_int_sub(bmap->ineq[last_pos][0],
4983 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4984 if (redundant)
4985 return drop_div_and_try_again(bmap, i,
4986 last_pos, last_neg, pairs);
4987 if (defined)
4988 set_div = isl_bool_false;
4989 else
4990 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4991 if (set_div < 0)
4992 return isl_basic_map_free(bmap);
4993 if (set_div) {
4994 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4995 return drop_redundant_divs_again(bmap, pairs, 1);
4997 pairs[i] = 0;
4998 --n;
5001 if (n > 0)
5002 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5004 free(pairs);
5005 return bmap;
5006 error:
5007 free(pairs);
5008 isl_basic_map_free(bmap);
5009 return NULL;
5012 /* Consider the coefficients at "c" as a row vector and replace
5013 * them with their product with "T". "T" is assumed to be a square matrix.
5015 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5017 isl_size n;
5018 isl_ctx *ctx;
5019 isl_vec *v;
5021 n = isl_mat_rows(T);
5022 if (n < 0)
5023 return isl_stat_error;
5024 if (isl_seq_first_non_zero(c, n) == -1)
5025 return isl_stat_ok;
5026 ctx = isl_mat_get_ctx(T);
5027 v = isl_vec_alloc(ctx, n);
5028 if (!v)
5029 return isl_stat_error;
5030 isl_seq_swp_or_cpy(v->el, c, n);
5031 v = isl_vec_mat_product(v, isl_mat_copy(T));
5032 if (!v)
5033 return isl_stat_error;
5034 isl_seq_swp_or_cpy(c, v->el, n);
5035 isl_vec_free(v);
5037 return isl_stat_ok;
5040 /* Plug in T for the variables in "bmap" starting at "pos".
5041 * T is a linear unimodular matrix, i.e., without constant term.
5043 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5044 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5046 int i;
5047 isl_size n_row, n_col;
5049 bmap = isl_basic_map_cow(bmap);
5050 n_row = isl_mat_rows(T);
5051 n_col = isl_mat_cols(T);
5052 if (!bmap || n_row < 0 || n_col < 0)
5053 goto error;
5055 if (n_col != n_row)
5056 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5057 "expecting square matrix", goto error);
5059 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5060 goto error;
5062 for (i = 0; i < bmap->n_eq; ++i)
5063 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5064 goto error;
5065 for (i = 0; i < bmap->n_ineq; ++i)
5066 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5067 goto error;
5068 for (i = 0; i < bmap->n_div; ++i) {
5069 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5070 continue;
5071 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5072 goto error;
5075 isl_mat_free(T);
5076 return bmap;
5077 error:
5078 isl_basic_map_free(bmap);
5079 isl_mat_free(T);
5080 return NULL;
5083 /* Remove divs that are not strictly needed.
5085 * First look for an equality constraint involving two or more
5086 * existentially quantified variables without an explicit
5087 * representation. Replace the combination that appears
5088 * in the equality constraint by a single existentially quantified
5089 * variable such that the equality can be used to derive
5090 * an explicit representation for the variable.
5091 * If there are no more such equality constraints, then continue
5092 * with isl_basic_map_drop_redundant_divs_ineq.
5094 * In particular, if the equality constraint is of the form
5096 * f(x) + \sum_i c_i a_i = 0
5098 * with a_i existentially quantified variable without explicit
5099 * representation, then apply a transformation on the existentially
5100 * quantified variables to turn the constraint into
5102 * f(x) + g a_1' = 0
5104 * with g the gcd of the c_i.
5105 * In order to easily identify which existentially quantified variables
5106 * have a complete explicit representation, i.e., without being defined
5107 * in terms of other existentially quantified variables without
5108 * an explicit representation, the existentially quantified variables
5109 * are first sorted.
5111 * The variable transformation is computed by extending the row
5112 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5114 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5115 * [a_2'] [ a_2 ]
5116 * ... = U ....
5117 * [a_n'] [ a_n ]
5119 * with [c_1/g ... c_n/g] representing the first row of U.
5120 * The inverse of U is then plugged into the original constraints.
5121 * The call to isl_basic_map_simplify makes sure the explicit
5122 * representation for a_1' is extracted from the equality constraint.
5124 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5125 __isl_take isl_basic_map *bmap)
5127 int first;
5128 int i;
5129 unsigned o_div;
5130 isl_size n_div;
5131 int l;
5132 isl_ctx *ctx;
5133 isl_mat *T;
5135 if (!bmap)
5136 return NULL;
5137 if (isl_basic_map_divs_known(bmap))
5138 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5139 if (bmap->n_eq == 0)
5140 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5141 bmap = isl_basic_map_sort_divs(bmap);
5142 if (!bmap)
5143 return NULL;
5145 first = isl_basic_map_first_unknown_div(bmap);
5146 if (first < 0)
5147 return isl_basic_map_free(bmap);
5149 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5150 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5151 if (n_div < 0)
5152 return isl_basic_map_free(bmap);
5154 for (i = 0; i < bmap->n_eq; ++i) {
5155 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5156 n_div - (first));
5157 if (l < 0)
5158 continue;
5159 l += first;
5160 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5161 n_div - (l + 1)) == -1)
5162 continue;
5163 break;
5165 if (i >= bmap->n_eq)
5166 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5168 ctx = isl_basic_map_get_ctx(bmap);
5169 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5170 if (!T)
5171 return isl_basic_map_free(bmap);
5172 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5173 T = isl_mat_normalize_row(T, 0);
5174 T = isl_mat_unimodular_complete(T, 1);
5175 T = isl_mat_right_inverse(T);
5177 for (i = l; i < n_div; ++i)
5178 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5179 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5180 bmap = isl_basic_map_simplify(bmap);
5182 return isl_basic_map_drop_redundant_divs(bmap);
5185 /* Does "bmap" satisfy any equality that involves more than 2 variables
5186 * and/or has coefficients different from -1 and 1?
5188 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5190 int i;
5191 isl_size total;
5193 total = isl_basic_map_dim(bmap, isl_dim_all);
5194 if (total < 0)
5195 return isl_bool_error;
5197 for (i = 0; i < bmap->n_eq; ++i) {
5198 int j, k;
5200 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5201 if (j < 0)
5202 continue;
5203 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5204 !isl_int_is_negone(bmap->eq[i][1 + j]))
5205 return isl_bool_true;
5207 j += 1;
5208 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5209 if (k < 0)
5210 continue;
5211 j += k;
5212 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5213 !isl_int_is_negone(bmap->eq[i][1 + j]))
5214 return isl_bool_true;
5216 j += 1;
5217 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5218 if (k >= 0)
5219 return isl_bool_true;
5222 return isl_bool_false;
5225 /* Remove any common factor g from the constraint coefficients in "v".
5226 * The constant term is stored in the first position and is replaced
5227 * by floor(c/g). If any common factor is removed and if this results
5228 * in a tightening of the constraint, then set *tightened.
5230 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5231 int *tightened)
5233 isl_ctx *ctx;
5235 if (!v)
5236 return NULL;
5237 ctx = isl_vec_get_ctx(v);
5238 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5239 if (isl_int_is_zero(ctx->normalize_gcd))
5240 return v;
5241 if (isl_int_is_one(ctx->normalize_gcd))
5242 return v;
5243 v = isl_vec_cow(v);
5244 if (!v)
5245 return NULL;
5246 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5247 *tightened = 1;
5248 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5249 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5250 v->size - 1);
5251 return v;
5254 /* If "bmap" is an integer set that satisfies any equality involving
5255 * more than 2 variables and/or has coefficients different from -1 and 1,
5256 * then use variable compression to reduce the coefficients by removing
5257 * any (hidden) common factor.
5258 * In particular, apply the variable compression to each constraint,
5259 * factor out any common factor in the non-constant coefficients and
5260 * then apply the inverse of the compression.
5261 * At the end, we mark the basic map as having reduced constants.
5262 * If this flag is still set on the next invocation of this function,
5263 * then we skip the computation.
5265 * Removing a common factor may result in a tightening of some of
5266 * the constraints. If this happens, then we may end up with two
5267 * opposite inequalities that can be replaced by an equality.
5268 * We therefore call isl_basic_map_detect_inequality_pairs,
5269 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5270 * and isl_basic_map_gauss if such a pair was found.
5272 * Tightening may also result in some other constraints becoming
5273 * (rationally) redundant with respect to the tightened constraint
5274 * (in combination with other constraints). The basic map may
5275 * therefore no longer be assumed to have no redundant constraints.
5277 * Note that this function may leave the result in an inconsistent state.
5278 * In particular, the constraints may not be gaussed.
5279 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5280 * for some of the test cases to pass successfully.
5281 * Any potential modification of the representation is therefore only
5282 * performed on a single copy of the basic map.
5284 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5285 __isl_take isl_basic_map *bmap)
5287 isl_size total;
5288 isl_bool multi;
5289 isl_ctx *ctx;
5290 isl_vec *v;
5291 isl_mat *eq, *T, *T2;
5292 int i;
5293 int tightened;
5295 if (!bmap)
5296 return NULL;
5297 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5298 return bmap;
5299 if (isl_basic_map_is_rational(bmap))
5300 return bmap;
5301 if (bmap->n_eq == 0)
5302 return bmap;
5303 multi = has_multiple_var_equality(bmap);
5304 if (multi < 0)
5305 return isl_basic_map_free(bmap);
5306 if (!multi)
5307 return bmap;
5309 total = isl_basic_map_dim(bmap, isl_dim_all);
5310 if (total < 0)
5311 return isl_basic_map_free(bmap);
5312 ctx = isl_basic_map_get_ctx(bmap);
5313 v = isl_vec_alloc(ctx, 1 + total);
5314 if (!v)
5315 return isl_basic_map_free(bmap);
5317 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5318 T = isl_mat_variable_compression(eq, &T2);
5319 if (!T || !T2)
5320 goto error;
5321 if (T->n_col == 0) {
5322 isl_mat_free(T);
5323 isl_mat_free(T2);
5324 isl_vec_free(v);
5325 return isl_basic_map_set_to_empty(bmap);
5328 bmap = isl_basic_map_cow(bmap);
5329 if (!bmap)
5330 goto error;
5332 tightened = 0;
5333 for (i = 0; i < bmap->n_ineq; ++i) {
5334 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5335 v = isl_vec_mat_product(v, isl_mat_copy(T));
5336 v = normalize_constraint(v, &tightened);
5337 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5338 if (!v)
5339 goto error;
5340 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5343 isl_mat_free(T);
5344 isl_mat_free(T2);
5345 isl_vec_free(v);
5347 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5349 if (tightened) {
5350 int progress = 0;
5352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
5353 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5354 if (progress) {
5355 bmap = eliminate_divs_eq(bmap, &progress);
5356 bmap = isl_basic_map_gauss(bmap, NULL);
5360 return bmap;
5361 error:
5362 isl_mat_free(T);
5363 isl_mat_free(T2);
5364 isl_vec_free(v);
5365 return isl_basic_map_free(bmap);
5368 /* Shift the integer division at position "div" of "bmap"
5369 * by "shift" times the variable at position "pos".
5370 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5371 * corresponds to the constant term.
5373 * That is, if the integer division has the form
5375 * floor(f(x)/d)
5377 * then replace it by
5379 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5381 __isl_give isl_basic_map *isl_basic_map_shift_div(
5382 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5384 int i;
5385 isl_size total, n_div;
5387 if (isl_int_is_zero(shift))
5388 return bmap;
5389 total = isl_basic_map_dim(bmap, isl_dim_all);
5390 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5391 total -= n_div;
5392 if (total < 0 || n_div < 0)
5393 return isl_basic_map_free(bmap);
5395 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5397 for (i = 0; i < bmap->n_eq; ++i) {
5398 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5399 continue;
5400 isl_int_submul(bmap->eq[i][pos],
5401 shift, bmap->eq[i][1 + total + div]);
5403 for (i = 0; i < bmap->n_ineq; ++i) {
5404 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5405 continue;
5406 isl_int_submul(bmap->ineq[i][pos],
5407 shift, bmap->ineq[i][1 + total + div]);
5409 for (i = 0; i < bmap->n_div; ++i) {
5410 if (isl_int_is_zero(bmap->div[i][0]))
5411 continue;
5412 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5413 continue;
5414 isl_int_submul(bmap->div[i][1 + pos],
5415 shift, bmap->div[i][1 + 1 + total + div]);
5418 return bmap;