isl_basic_map_reduce_coefficients: move dropping variables last
[isl.git] / isl_map_simplify.c
blob1927059e672e27fc2a13ed7fc8c09068500d3e8f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(__isl_keep isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(__isl_keep isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 __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);
335 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
338 for (k = 0; k < bmap->n_div; ++k) {
339 if (isl_int_is_zero(bmap->div[k][0]))
340 continue;
341 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
342 continue;
343 if (progress)
344 *progress = 1;
345 /* We need to be careful about circular definitions,
346 * so for now we just remove the definition of div k
347 * if the equality contains any divs.
348 * If keep_divs is set, then the divs have been ordered
349 * and we can keep the definition as long as the result
350 * is still ordered.
352 if (last_div == -1 || (keep_divs && last_div < k)) {
353 isl_seq_elim(bmap->div[k]+1, eq,
354 1+pos, 1+total, &bmap->div[k][0]);
355 bmap = normalize_div_expression(bmap, k);
356 if (!bmap)
357 return NULL;
358 } else
359 isl_seq_clr(bmap->div[k], 1 + total);
362 return bmap;
365 /* Assumes divs have been ordered if keep_divs is set.
367 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
368 isl_int *eq, unsigned div, int keep_divs)
370 isl_size v_div;
371 unsigned pos;
373 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
374 if (v_div < 0)
375 return isl_basic_map_free(bmap);
376 pos = v_div + div;
377 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
379 bmap = isl_basic_map_drop_div(bmap, div);
381 return bmap;
384 /* Check if elimination of div "div" using equality "eq" would not
385 * result in a div depending on a later div.
387 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
388 unsigned div)
390 int k;
391 int last_div;
392 isl_size v_div;
393 unsigned pos;
395 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
396 if (v_div < 0)
397 return isl_bool_error;
398 pos = v_div + div;
400 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
401 if (last_div < 0 || last_div <= div)
402 return isl_bool_true;
404 for (k = 0; k <= last_div; ++k) {
405 if (isl_int_is_zero(bmap->div[k][0]))
406 continue;
407 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
408 return isl_bool_false;
411 return isl_bool_true;
414 /* Eliminate divs based on equalities
416 static __isl_give isl_basic_map *eliminate_divs_eq(
417 __isl_take isl_basic_map *bmap, int *progress)
419 int d;
420 int i;
421 int modified = 0;
422 unsigned off;
424 bmap = isl_basic_map_order_divs(bmap);
426 if (!bmap)
427 return NULL;
429 off = isl_basic_map_offset(bmap, isl_dim_div);
431 for (d = bmap->n_div - 1; d >= 0 ; --d) {
432 for (i = 0; i < bmap->n_eq; ++i) {
433 isl_bool ok;
435 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
436 !isl_int_is_negone(bmap->eq[i][off + d]))
437 continue;
438 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
439 if (ok < 0)
440 return isl_basic_map_free(bmap);
441 if (!ok)
442 continue;
443 modified = 1;
444 *progress = 1;
445 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
446 if (isl_basic_map_drop_equality(bmap, i) < 0)
447 return isl_basic_map_free(bmap);
448 break;
451 if (modified)
452 return eliminate_divs_eq(bmap, progress);
453 return bmap;
456 /* Eliminate divs based on inequalities
458 static __isl_give isl_basic_map *eliminate_divs_ineq(
459 __isl_take isl_basic_map *bmap, int *progress)
461 int d;
462 int i;
463 unsigned off;
464 struct isl_ctx *ctx;
466 if (!bmap)
467 return NULL;
469 ctx = bmap->ctx;
470 off = isl_basic_map_offset(bmap, isl_dim_div);
472 for (d = bmap->n_div - 1; d >= 0 ; --d) {
473 for (i = 0; i < bmap->n_eq; ++i)
474 if (!isl_int_is_zero(bmap->eq[i][off + d]))
475 break;
476 if (i < bmap->n_eq)
477 continue;
478 for (i = 0; i < bmap->n_ineq; ++i)
479 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
480 break;
481 if (i < bmap->n_ineq)
482 continue;
483 *progress = 1;
484 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
485 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
486 break;
487 bmap = isl_basic_map_drop_div(bmap, d);
488 if (!bmap)
489 break;
491 return bmap;
494 /* Does the equality constraint at position "eq" in "bmap" involve
495 * any local variables in the range [first, first + n)
496 * that are not marked as having an explicit representation?
498 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
499 int eq, unsigned first, unsigned n)
501 unsigned o_div;
502 int i;
504 if (!bmap)
505 return isl_bool_error;
507 o_div = isl_basic_map_offset(bmap, isl_dim_div);
508 for (i = 0; i < n; ++i) {
509 isl_bool unknown;
511 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
512 continue;
513 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
514 if (unknown < 0)
515 return isl_bool_error;
516 if (unknown)
517 return isl_bool_true;
520 return isl_bool_false;
523 /* The last local variable involved in the equality constraint
524 * at position "eq" in "bmap" is the local variable at position "div".
525 * It can therefore be used to extract an explicit representation
526 * for that variable.
527 * Do so unless the local variable already has an explicit representation or
528 * the explicit representation would involve any other local variables
529 * that in turn do not have an explicit representation.
530 * An equality constraint involving local variables without an explicit
531 * representation can be used in isl_basic_map_drop_redundant_divs
532 * to separate out an independent local variable. Introducing
533 * an explicit representation here would block this transformation,
534 * while the partial explicit representation in itself is not very useful.
535 * Set *progress if anything is changed.
537 * The equality constraint is of the form
539 * f(x) + n e >= 0
541 * with n a positive number. The explicit representation derived from
542 * this constraint is
544 * floor((-f(x))/n)
546 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
547 int div, int eq, int *progress)
549 isl_size total;
550 unsigned o_div;
551 isl_bool involves;
553 if (!bmap)
554 return NULL;
556 if (!isl_int_is_zero(bmap->div[div][0]))
557 return bmap;
559 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
560 if (involves < 0)
561 return isl_basic_map_free(bmap);
562 if (involves)
563 return bmap;
565 total = isl_basic_map_dim(bmap, isl_dim_all);
566 if (total < 0)
567 return isl_basic_map_free(bmap);
568 o_div = isl_basic_map_offset(bmap, isl_dim_div);
569 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
570 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
571 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
572 if (progress)
573 *progress = 1;
575 return bmap;
578 /* Perform fangcheng (Gaussian elimination) on the equality
579 * constraints of "bmap".
580 * That is, put them into row-echelon form, starting from the last column
581 * backward and use them to eliminate the corresponding coefficients
582 * from all constraints.
584 * If "progress" is not NULL, then it gets set if the elimination
585 * results in any changes.
586 * The elimination process may result in some equality constraints
587 * getting interchanged or removed.
588 * If "swap" or "drop" are not NULL, then they get called when
589 * two equality constraints get interchanged or
590 * when a number of final equality constraints get removed.
591 * As a special case, if the input turns out to be empty,
592 * then drop gets called with the number of removed equality
593 * constraints set to the total number of equality constraints.
594 * If "swap" or "drop" are not NULL, then the local variables (if any)
595 * are assumed to be in a valid order.
597 __isl_give isl_basic_map *isl_basic_map_gauss5(__isl_take isl_basic_map *bmap,
598 int *progress,
599 isl_stat (*swap)(unsigned a, unsigned b, void *user),
600 isl_stat (*drop)(unsigned n, void *user), void *user)
602 int k;
603 int done;
604 int last_var;
605 unsigned total_var;
606 isl_size total;
607 unsigned n_drop;
609 if (!swap && !drop)
610 bmap = isl_basic_map_order_divs(bmap);
612 total = isl_basic_map_dim(bmap, isl_dim_all);
613 if (total < 0)
614 return isl_basic_map_free(bmap);
616 total_var = total - bmap->n_div;
618 last_var = total - 1;
619 for (done = 0; done < bmap->n_eq; ++done) {
620 for (; last_var >= 0; --last_var) {
621 for (k = done; k < bmap->n_eq; ++k)
622 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
623 break;
624 if (k < bmap->n_eq)
625 break;
627 if (last_var < 0)
628 break;
629 if (k != done) {
630 swap_equality(bmap, k, done);
631 if (swap && swap(k, done, user) < 0)
632 return isl_basic_map_free(bmap);
634 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
635 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
637 bmap = eliminate_var_using_equality(bmap, last_var,
638 bmap->eq[done], 1, progress);
640 if (last_var >= total_var)
641 bmap = set_div_from_eq(bmap, last_var - total_var,
642 done, progress);
643 if (!bmap)
644 return NULL;
646 if (done == bmap->n_eq)
647 return bmap;
648 for (k = done; k < bmap->n_eq; ++k) {
649 if (isl_int_is_zero(bmap->eq[k][0]))
650 continue;
651 if (drop && drop(bmap->n_eq, user) < 0)
652 return isl_basic_map_free(bmap);
653 return isl_basic_map_set_to_empty(bmap);
655 n_drop = bmap->n_eq - done;
656 bmap = isl_basic_map_free_equality(bmap, n_drop);
657 if (drop && drop(n_drop, user) < 0)
658 return isl_basic_map_free(bmap);
659 return bmap;
662 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
663 int *progress)
665 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
668 __isl_give isl_basic_set *isl_basic_set_gauss(
669 __isl_take isl_basic_set *bset, int *progress)
671 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
672 progress));
676 static unsigned int round_up(unsigned int v)
678 int old_v = v;
680 while (v) {
681 old_v = v;
682 v ^= v & -v;
684 return old_v << 1;
687 /* Hash table of inequalities in a basic map.
688 * "index" is an array of addresses of inequalities in the basic map, some
689 * of which are NULL. The inequalities are hashed on the coefficients
690 * except the constant term.
691 * "size" is the number of elements in the array and is always a power of two
692 * "bits" is the number of bits need to represent an index into the array.
693 * "total" is the total dimension of the basic map.
695 struct isl_constraint_index {
696 unsigned int size;
697 int bits;
698 isl_int ***index;
699 isl_size total;
702 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
704 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
705 __isl_keep isl_basic_map *bmap)
707 isl_ctx *ctx;
709 ci->index = NULL;
710 if (!bmap)
711 return isl_stat_error;
712 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
713 if (ci->total < 0)
714 return isl_stat_error;
715 if (bmap->n_ineq == 0)
716 return isl_stat_ok;
717 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
718 ci->bits = ffs(ci->size) - 1;
719 ctx = isl_basic_map_get_ctx(bmap);
720 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
721 if (!ci->index)
722 return isl_stat_error;
724 return isl_stat_ok;
727 /* Free the memory allocated by create_constraint_index.
729 static void constraint_index_free(struct isl_constraint_index *ci)
731 free(ci->index);
734 /* Return the position in ci->index that contains the address of
735 * an inequality that is equal to *ineq up to the constant term,
736 * provided this address is not identical to "ineq".
737 * If there is no such inequality, then return the position where
738 * such an inequality should be inserted.
740 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
742 int h;
743 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
744 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
745 if (ineq != ci->index[h] &&
746 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
747 break;
748 return h;
751 /* Return the position in ci->index that contains the address of
752 * an inequality that is equal to the k'th inequality of "bmap"
753 * up to the constant term, provided it does not point to the very
754 * same inequality.
755 * If there is no such inequality, then return the position where
756 * such an inequality should be inserted.
758 static int hash_index(struct isl_constraint_index *ci,
759 __isl_keep isl_basic_map *bmap, int k)
761 return hash_index_ineq(ci, &bmap->ineq[k]);
764 static int set_hash_index(struct isl_constraint_index *ci,
765 __isl_keep isl_basic_set *bset, int k)
767 return hash_index(ci, bset, k);
770 /* Fill in the "ci" data structure with the inequalities of "bset".
772 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
773 __isl_keep isl_basic_set *bset)
775 int k, h;
777 if (create_constraint_index(ci, bset) < 0)
778 return isl_stat_error;
780 for (k = 0; k < bset->n_ineq; ++k) {
781 h = set_hash_index(ci, bset, k);
782 ci->index[h] = &bset->ineq[k];
785 return isl_stat_ok;
788 /* Is the inequality ineq (obviously) redundant with respect
789 * to the constraints in "ci"?
791 * Look for an inequality in "ci" with the same coefficients and then
792 * check if the contant term of "ineq" is greater than or equal
793 * to the constant term of that inequality. If so, "ineq" is clearly
794 * redundant.
796 * Note that hash_index_ineq ignores a stored constraint if it has
797 * the same address as the passed inequality. It is ok to pass
798 * the address of a local variable here since it will never be
799 * the same as the address of a constraint in "ci".
801 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
802 isl_int *ineq)
804 int h;
806 h = hash_index_ineq(ci, &ineq);
807 if (!ci->index[h])
808 return isl_bool_false;
809 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
812 /* If we can eliminate more than one div, then we need to make
813 * sure we do it from last div to first div, in order not to
814 * change the position of the other divs that still need to
815 * be removed.
817 static __isl_give isl_basic_map *remove_duplicate_divs(
818 __isl_take isl_basic_map *bmap, int *progress)
820 unsigned int size;
821 int *index;
822 int *elim_for;
823 int k, l, h;
824 int bits;
825 struct isl_blk eq;
826 isl_size v_div;
827 unsigned total;
828 struct isl_ctx *ctx;
830 bmap = isl_basic_map_order_divs(bmap);
831 if (!bmap || bmap->n_div <= 1)
832 return bmap;
834 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
835 if (v_div < 0)
836 return isl_basic_map_free(bmap);
837 total = v_div + bmap->n_div;
839 ctx = bmap->ctx;
840 for (k = bmap->n_div - 1; k >= 0; --k)
841 if (!isl_int_is_zero(bmap->div[k][0]))
842 break;
843 if (k <= 0)
844 return bmap;
846 size = round_up(4 * bmap->n_div / 3 - 1);
847 if (size == 0)
848 return bmap;
849 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
850 bits = ffs(size) - 1;
851 index = isl_calloc_array(ctx, int, size);
852 if (!elim_for || !index)
853 goto out;
854 eq = isl_blk_alloc(ctx, 1+total);
855 if (isl_blk_is_error(eq))
856 goto out;
858 isl_seq_clr(eq.data, 1+total);
859 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
860 for (--k; k >= 0; --k) {
861 uint32_t hash;
863 if (isl_int_is_zero(bmap->div[k][0]))
864 continue;
866 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
867 for (h = hash; index[h]; h = (h+1) % size)
868 if (isl_seq_eq(bmap->div[k],
869 bmap->div[index[h]-1], 2+total))
870 break;
871 if (index[h]) {
872 *progress = 1;
873 l = index[h] - 1;
874 elim_for[l] = k + 1;
876 index[h] = k+1;
878 for (l = bmap->n_div - 1; l >= 0; --l) {
879 if (!elim_for[l])
880 continue;
881 k = elim_for[l] - 1;
882 isl_int_set_si(eq.data[1 + v_div + k], -1);
883 isl_int_set_si(eq.data[1 + v_div + l], 1);
884 bmap = eliminate_div(bmap, eq.data, l, 1);
885 if (!bmap)
886 break;
887 isl_int_set_si(eq.data[1 + v_div + k], 0);
888 isl_int_set_si(eq.data[1 + v_div + l], 0);
891 isl_blk_free(ctx, eq);
892 out:
893 free(index);
894 free(elim_for);
895 return bmap;
898 static int n_pure_div_eq(__isl_keep isl_basic_map *bmap)
900 int i, j;
901 isl_size v_div;
903 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
904 if (v_div < 0)
905 return -1;
906 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
907 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
908 --j;
909 if (j < 0)
910 break;
911 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
912 return 0;
914 return i;
917 /* Normalize divs that appear in equalities.
919 * In particular, we assume that bmap contains some equalities
920 * of the form
922 * a x = m * e_i
924 * and we want to replace the set of e_i by a minimal set and
925 * such that the new e_i have a canonical representation in terms
926 * of the vector x.
927 * If any of the equalities involves more than one divs, then
928 * we currently simply bail out.
930 * Let us first additionally assume that all equalities involve
931 * a div. The equalities then express modulo constraints on the
932 * remaining variables and we can use "parameter compression"
933 * to find a minimal set of constraints. The result is a transformation
935 * x = T(x') = x_0 + G x'
937 * with G a lower-triangular matrix with all elements below the diagonal
938 * non-negative and smaller than the diagonal element on the same row.
939 * We first normalize x_0 by making the same property hold in the affine
940 * T matrix.
941 * The rows i of G with a 1 on the diagonal do not impose any modulo
942 * constraint and simply express x_i = x'_i.
943 * For each of the remaining rows i, we introduce a div and a corresponding
944 * equality. In particular
946 * g_ii e_j = x_i - g_i(x')
948 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
949 * corresponding div (if g_kk != 1).
951 * If there are any equalities not involving any div, then we
952 * first apply a variable compression on the variables x:
954 * x = C x'' x'' = C_2 x
956 * and perform the above parameter compression on A C instead of on A.
957 * The resulting compression is then of the form
959 * x'' = T(x') = x_0 + G x'
961 * and in constructing the new divs and the corresponding equalities,
962 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
963 * by the corresponding row from C_2.
965 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
966 int *progress)
968 int i, j, k;
969 isl_size v_div;
970 int div_eq;
971 struct isl_mat *B;
972 struct isl_vec *d;
973 struct isl_mat *T = NULL;
974 struct isl_mat *C = NULL;
975 struct isl_mat *C2 = NULL;
976 isl_int v;
977 int *pos = NULL;
978 int dropped, needed;
980 if (!bmap)
981 return NULL;
983 if (bmap->n_div == 0)
984 return bmap;
986 if (bmap->n_eq == 0)
987 return bmap;
989 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
990 return bmap;
992 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
993 div_eq = n_pure_div_eq(bmap);
994 if (v_div < 0 || div_eq < 0)
995 return isl_basic_map_free(bmap);
996 if (div_eq == 0)
997 return bmap;
999 if (div_eq < bmap->n_eq) {
1000 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1001 bmap->n_eq - div_eq, 0, 1 + v_div);
1002 C = isl_mat_variable_compression(B, &C2);
1003 if (!C || !C2)
1004 goto error;
1005 if (C->n_col == 0) {
1006 bmap = isl_basic_map_set_to_empty(bmap);
1007 isl_mat_free(C);
1008 isl_mat_free(C2);
1009 goto done;
1013 d = isl_vec_alloc(bmap->ctx, div_eq);
1014 if (!d)
1015 goto error;
1016 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1017 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1018 --j;
1019 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1021 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1023 if (C) {
1024 B = isl_mat_product(B, C);
1025 C = NULL;
1028 T = isl_mat_parameter_compression(B, d);
1029 if (!T)
1030 goto error;
1031 if (T->n_col == 0) {
1032 bmap = isl_basic_map_set_to_empty(bmap);
1033 isl_mat_free(C2);
1034 isl_mat_free(T);
1035 goto done;
1037 isl_int_init(v);
1038 for (i = 0; i < T->n_row - 1; ++i) {
1039 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1040 if (isl_int_is_zero(v))
1041 continue;
1042 isl_mat_col_submul(T, 0, v, 1 + i);
1044 isl_int_clear(v);
1045 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1046 if (!pos)
1047 goto error;
1048 /* We have to be careful because dropping equalities may reorder them */
1049 dropped = 0;
1050 for (j = bmap->n_div - 1; j >= 0; --j) {
1051 for (i = 0; i < bmap->n_eq; ++i)
1052 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1053 break;
1054 if (i < bmap->n_eq) {
1055 bmap = isl_basic_map_drop_div(bmap, j);
1056 if (isl_basic_map_drop_equality(bmap, i) < 0)
1057 goto error;
1058 ++dropped;
1061 pos[0] = 0;
1062 needed = 0;
1063 for (i = 1; i < T->n_row; ++i) {
1064 if (isl_int_is_one(T->row[i][i]))
1065 pos[i] = i;
1066 else
1067 needed++;
1069 if (needed > dropped) {
1070 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1071 if (!bmap)
1072 goto error;
1074 for (i = 1; i < T->n_row; ++i) {
1075 if (isl_int_is_one(T->row[i][i]))
1076 continue;
1077 k = isl_basic_map_alloc_div(bmap);
1078 pos[i] = 1 + v_div + k;
1079 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1080 isl_int_set(bmap->div[k][0], T->row[i][i]);
1081 if (C2)
1082 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1083 else
1084 isl_int_set_si(bmap->div[k][1 + i], 1);
1085 for (j = 0; j < i; ++j) {
1086 if (isl_int_is_zero(T->row[i][j]))
1087 continue;
1088 if (pos[j] < T->n_row && C2)
1089 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1090 C2->row[pos[j]], 1 + v_div);
1091 else
1092 isl_int_neg(bmap->div[k][1 + pos[j]],
1093 T->row[i][j]);
1095 j = isl_basic_map_alloc_equality(bmap);
1096 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1097 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1099 free(pos);
1100 isl_mat_free(C2);
1101 isl_mat_free(T);
1103 if (progress)
1104 *progress = 1;
1105 done:
1106 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1108 return bmap;
1109 error:
1110 free(pos);
1111 isl_mat_free(C);
1112 isl_mat_free(C2);
1113 isl_mat_free(T);
1114 isl_basic_map_free(bmap);
1115 return NULL;
1118 static __isl_give isl_basic_map *set_div_from_lower_bound(
1119 __isl_take isl_basic_map *bmap, int div, int ineq)
1121 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1123 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1124 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1125 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1126 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1127 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1129 return bmap;
1132 /* Check whether it is ok to define a div based on an inequality.
1133 * To avoid the introduction of circular definitions of divs, we
1134 * do not allow such a definition if the resulting expression would refer to
1135 * any other undefined divs or if any known div is defined in
1136 * terms of the unknown div.
1138 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1139 int div, int ineq)
1141 int j;
1142 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1144 /* Not defined in terms of unknown divs */
1145 for (j = 0; j < bmap->n_div; ++j) {
1146 if (div == j)
1147 continue;
1148 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1149 continue;
1150 if (isl_int_is_zero(bmap->div[j][0]))
1151 return isl_bool_false;
1154 /* No other div defined in terms of this one => avoid loops */
1155 for (j = 0; j < bmap->n_div; ++j) {
1156 if (div == j)
1157 continue;
1158 if (isl_int_is_zero(bmap->div[j][0]))
1159 continue;
1160 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1161 return isl_bool_false;
1164 return isl_bool_true;
1167 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1168 * be a better expression than the current one?
1170 * If we do not have any expression yet, then any expression would be better.
1171 * Otherwise we check if the last variable involved in the inequality
1172 * (disregarding the div that it would define) is in an earlier position
1173 * than the last variable involved in the current div expression.
1175 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1176 int div, int ineq)
1178 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1179 int last_div;
1180 int last_ineq;
1182 if (isl_int_is_zero(bmap->div[div][0]))
1183 return isl_bool_true;
1185 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1186 bmap->n_div - (div + 1)) >= 0)
1187 return isl_bool_false;
1189 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1190 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1191 total + bmap->n_div);
1193 return last_ineq < last_div;
1196 /* Given two constraints "k" and "l" that are opposite to each other,
1197 * except for the constant term, check if we can use them
1198 * to obtain an expression for one of the hitherto unknown divs or
1199 * a "better" expression for a div for which we already have an expression.
1200 * "sum" is the sum of the constant terms of the constraints.
1201 * If this sum is strictly smaller than the coefficient of one
1202 * of the divs, then this pair can be used to define the div.
1203 * To avoid the introduction of circular definitions of divs, we
1204 * do not use the pair if the resulting expression would refer to
1205 * any other undefined divs or if any known div is defined in
1206 * terms of the unknown div.
1208 static __isl_give isl_basic_map *check_for_div_constraints(
1209 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1210 int *progress)
1212 int i;
1213 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1215 for (i = 0; i < bmap->n_div; ++i) {
1216 isl_bool set_div;
1218 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1219 continue;
1220 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1221 continue;
1222 set_div = better_div_constraint(bmap, i, k);
1223 if (set_div >= 0 && set_div)
1224 set_div = ok_to_set_div_from_bound(bmap, i, k);
1225 if (set_div < 0)
1226 return isl_basic_map_free(bmap);
1227 if (!set_div)
1228 break;
1229 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1230 bmap = set_div_from_lower_bound(bmap, i, k);
1231 else
1232 bmap = set_div_from_lower_bound(bmap, i, l);
1233 if (progress)
1234 *progress = 1;
1235 break;
1237 return bmap;
1240 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1241 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1243 struct isl_constraint_index ci;
1244 int k, l, h;
1245 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1246 isl_int sum;
1248 if (total < 0 || bmap->n_ineq <= 1)
1249 return bmap;
1251 if (create_constraint_index(&ci, bmap) < 0)
1252 return bmap;
1254 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1255 ci.index[h] = &bmap->ineq[0];
1256 for (k = 1; k < bmap->n_ineq; ++k) {
1257 h = hash_index(&ci, bmap, k);
1258 if (!ci.index[h]) {
1259 ci.index[h] = &bmap->ineq[k];
1260 continue;
1262 if (progress)
1263 *progress = 1;
1264 l = ci.index[h] - &bmap->ineq[0];
1265 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1266 swap_inequality(bmap, k, l);
1267 isl_basic_map_drop_inequality(bmap, k);
1268 --k;
1270 isl_int_init(sum);
1271 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
1272 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1273 h = hash_index(&ci, bmap, k);
1274 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1275 if (!ci.index[h])
1276 continue;
1277 l = ci.index[h] - &bmap->ineq[0];
1278 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1279 if (isl_int_is_pos(sum)) {
1280 if (detect_divs)
1281 bmap = check_for_div_constraints(bmap, k, l,
1282 sum, progress);
1283 continue;
1285 if (isl_int_is_zero(sum)) {
1286 /* We need to break out of the loop after these
1287 * changes since the contents of the hash
1288 * will no longer be valid.
1289 * Plus, we probably we want to regauss first.
1291 if (progress)
1292 *progress = 1;
1293 isl_basic_map_drop_inequality(bmap, l);
1294 isl_basic_map_inequality_to_equality(bmap, k);
1295 } else
1296 bmap = isl_basic_map_set_to_empty(bmap);
1297 break;
1299 isl_int_clear(sum);
1301 constraint_index_free(&ci);
1302 return bmap;
1305 /* Detect all pairs of inequalities that form an equality.
1307 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1308 * Call it repeatedly while it is making progress.
1310 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1311 __isl_take isl_basic_map *bmap, int *progress)
1313 int duplicate;
1315 do {
1316 duplicate = 0;
1317 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1318 &duplicate, 0);
1319 if (progress && duplicate)
1320 *progress = 1;
1321 } while (duplicate);
1323 return bmap;
1326 /* Given a known integer division "div" that is not integral
1327 * (with denominator 1), eliminate it from the constraints in "bmap"
1328 * where it appears with a (positive or negative) unit coefficient.
1329 * If "progress" is not NULL, then it gets set if the elimination
1330 * results in any changes.
1332 * That is, replace
1334 * floor(e/m) + f >= 0
1336 * by
1338 * e + m f >= 0
1340 * and
1342 * -floor(e/m) + f >= 0
1344 * by
1346 * -e + m f + m - 1 >= 0
1348 * The first conversion is valid because floor(e/m) >= -f is equivalent
1349 * to e/m >= -f because -f is an integral expression.
1350 * The second conversion follows from the fact that
1352 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1355 * Note that one of the div constraints may have been eliminated
1356 * due to being redundant with respect to the constraint that is
1357 * being modified by this function. The modified constraint may
1358 * no longer imply this div constraint, so we add it back to make
1359 * sure we do not lose any information.
1361 static __isl_give isl_basic_map *eliminate_unit_div(
1362 __isl_take isl_basic_map *bmap, int div, int *progress)
1364 int j;
1365 isl_size v_div, dim;
1366 isl_ctx *ctx;
1368 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1369 dim = isl_basic_map_dim(bmap, isl_dim_all);
1370 if (v_div < 0 || dim < 0)
1371 return isl_basic_map_free(bmap);
1373 ctx = isl_basic_map_get_ctx(bmap);
1375 for (j = 0; j < bmap->n_ineq; ++j) {
1376 int s;
1378 if (!isl_int_is_one(bmap->ineq[j][1 + v_div + div]) &&
1379 !isl_int_is_negone(bmap->ineq[j][1 + v_div + div]))
1380 continue;
1382 if (progress)
1383 *progress = 1;
1385 s = isl_int_sgn(bmap->ineq[j][1 + v_div + div]);
1386 isl_int_set_si(bmap->ineq[j][1 + v_div + div], 0);
1387 if (s < 0)
1388 isl_seq_combine(bmap->ineq[j],
1389 ctx->negone, bmap->div[div] + 1,
1390 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1391 else
1392 isl_seq_combine(bmap->ineq[j],
1393 ctx->one, bmap->div[div] + 1,
1394 bmap->div[div][0], bmap->ineq[j], 1 + dim);
1395 if (s < 0) {
1396 isl_int_add(bmap->ineq[j][0],
1397 bmap->ineq[j][0], bmap->div[div][0]);
1398 isl_int_sub_ui(bmap->ineq[j][0],
1399 bmap->ineq[j][0], 1);
1402 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1403 bmap = isl_basic_map_add_div_constraint(bmap, div, s);
1404 if (!bmap)
1405 return NULL;
1408 return bmap;
1411 /* Eliminate selected known divs from constraints where they appear with
1412 * a (positive or negative) unit coefficient.
1413 * In particular, only handle those for which "select" returns isl_bool_true.
1414 * If "progress" is not NULL, then it gets set if the elimination
1415 * results in any changes.
1417 * We skip integral divs, i.e., those with denominator 1, as we would
1418 * risk eliminating the div from the div constraints. We do not need
1419 * to handle those divs here anyway since the div constraints will turn
1420 * out to form an equality and this equality can then be used to eliminate
1421 * the div from all constraints.
1423 static __isl_give isl_basic_map *eliminate_selected_unit_divs(
1424 __isl_take isl_basic_map *bmap,
1425 isl_bool (*select)(__isl_keep isl_basic_map *bmap, int div),
1426 int *progress)
1428 int i;
1430 if (!bmap)
1431 return NULL;
1433 for (i = 0; i < bmap->n_div; ++i) {
1434 isl_bool selected;
1436 if (isl_int_is_zero(bmap->div[i][0]))
1437 continue;
1438 if (isl_int_is_one(bmap->div[i][0]))
1439 continue;
1440 selected = select(bmap, i);
1441 if (selected < 0)
1442 return isl_basic_map_free(bmap);
1443 if (!selected)
1444 continue;
1445 bmap = eliminate_unit_div(bmap, i, progress);
1446 if (!bmap)
1447 return NULL;
1450 return bmap;
1453 /* eliminate_selected_unit_divs callback that selects every
1454 * integer division.
1456 static isl_bool is_any_div(__isl_keep isl_basic_map *bmap, int div)
1458 return isl_bool_true;
1461 /* Eliminate known divs from constraints where they appear with
1462 * a (positive or negative) unit coefficient.
1463 * If "progress" is not NULL, then it gets set if the elimination
1464 * results in any changes.
1466 static __isl_give isl_basic_map *eliminate_unit_divs(
1467 __isl_take isl_basic_map *bmap, int *progress)
1469 return eliminate_selected_unit_divs(bmap, &is_any_div, progress);
1472 /* eliminate_selected_unit_divs callback that selects
1473 * integer divisions that only appear with
1474 * a (positive or negative) unit coefficient
1475 * (outside their div constraints).
1477 static isl_bool is_pure_unit_div(__isl_keep isl_basic_map *bmap, int div)
1479 int i;
1480 isl_size v_div, n_ineq;
1482 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1483 n_ineq = isl_basic_map_n_inequality(bmap);
1484 if (v_div < 0 || n_ineq < 0)
1485 return isl_bool_error;
1487 for (i = 0; i < n_ineq; ++i) {
1488 isl_bool skip;
1490 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
1491 continue;
1492 skip = isl_basic_map_is_div_constraint(bmap,
1493 bmap->ineq[i], div);
1494 if (skip < 0)
1495 return isl_bool_error;
1496 if (skip)
1497 continue;
1498 if (!isl_int_is_one(bmap->ineq[i][1 + v_div + div]) &&
1499 !isl_int_is_negone(bmap->ineq[i][1 + v_div + div]))
1500 return isl_bool_false;
1503 return isl_bool_true;
1506 /* Eliminate known divs from constraints where they appear with
1507 * a (positive or negative) unit coefficient,
1508 * but only if they do not appear in any other constraints
1509 * (other than the div constraints).
1511 __isl_give isl_basic_map *isl_basic_map_eliminate_pure_unit_divs(
1512 __isl_take isl_basic_map *bmap)
1514 return eliminate_selected_unit_divs(bmap, &is_pure_unit_div, NULL);
1517 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1519 int progress = 1;
1520 if (!bmap)
1521 return NULL;
1522 while (progress) {
1523 isl_bool empty;
1525 progress = 0;
1526 empty = isl_basic_map_plain_is_empty(bmap);
1527 if (empty < 0)
1528 return isl_basic_map_free(bmap);
1529 if (empty)
1530 break;
1531 bmap = isl_basic_map_normalize_constraints(bmap);
1532 bmap = reduce_div_coefficients(bmap);
1533 bmap = normalize_div_expressions(bmap);
1534 bmap = remove_duplicate_divs(bmap, &progress);
1535 bmap = eliminate_unit_divs(bmap, &progress);
1536 bmap = eliminate_divs_eq(bmap, &progress);
1537 bmap = eliminate_divs_ineq(bmap, &progress);
1538 bmap = isl_basic_map_gauss(bmap, &progress);
1539 /* requires equalities in normal form */
1540 bmap = normalize_divs(bmap, &progress);
1541 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1542 &progress, 1);
1544 return bmap;
1547 __isl_give isl_basic_set *isl_basic_set_simplify(
1548 __isl_take isl_basic_set *bset)
1550 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1554 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1555 isl_int *constraint, unsigned div)
1557 unsigned pos;
1559 if (!bmap)
1560 return isl_bool_error;
1562 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1564 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1565 int neg;
1566 isl_int_sub(bmap->div[div][1],
1567 bmap->div[div][1], bmap->div[div][0]);
1568 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1569 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1570 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1571 isl_int_add(bmap->div[div][1],
1572 bmap->div[div][1], bmap->div[div][0]);
1573 if (!neg)
1574 return isl_bool_false;
1575 if (isl_seq_first_non_zero(constraint+pos+1,
1576 bmap->n_div-div-1) != -1)
1577 return isl_bool_false;
1578 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1579 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1580 return isl_bool_false;
1581 if (isl_seq_first_non_zero(constraint+pos+1,
1582 bmap->n_div-div-1) != -1)
1583 return isl_bool_false;
1584 } else
1585 return isl_bool_false;
1587 return isl_bool_true;
1590 /* If the only constraints a div d=floor(f/m)
1591 * appears in are its two defining constraints
1593 * f - m d >=0
1594 * -(f - (m - 1)) + m d >= 0
1596 * then it can safely be removed.
1598 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1600 int i;
1601 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1602 unsigned pos = 1 + v_div + div;
1604 if (v_div < 0)
1605 return isl_bool_error;
1607 for (i = 0; i < bmap->n_eq; ++i)
1608 if (!isl_int_is_zero(bmap->eq[i][pos]))
1609 return isl_bool_false;
1611 for (i = 0; i < bmap->n_ineq; ++i) {
1612 isl_bool red;
1614 if (isl_int_is_zero(bmap->ineq[i][pos]))
1615 continue;
1616 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1617 if (red < 0 || !red)
1618 return red;
1621 for (i = 0; i < bmap->n_div; ++i) {
1622 if (isl_int_is_zero(bmap->div[i][0]))
1623 continue;
1624 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1625 return isl_bool_false;
1628 return isl_bool_true;
1632 * Remove divs that don't occur in any of the constraints or other divs.
1633 * These can arise when dropping constraints from a basic map or
1634 * when the divs of a basic map have been temporarily aligned
1635 * with the divs of another basic map.
1637 static __isl_give isl_basic_map *remove_redundant_divs(
1638 __isl_take isl_basic_map *bmap)
1640 int i;
1641 isl_size v_div;
1643 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1644 if (v_div < 0)
1645 return isl_basic_map_free(bmap);
1647 for (i = bmap->n_div-1; i >= 0; --i) {
1648 isl_bool redundant;
1650 redundant = div_is_redundant(bmap, i);
1651 if (redundant < 0)
1652 return isl_basic_map_free(bmap);
1653 if (!redundant)
1654 continue;
1655 bmap = isl_basic_map_drop_constraints_involving(bmap,
1656 v_div + i, 1);
1657 bmap = isl_basic_map_drop_div(bmap, i);
1659 return bmap;
1662 /* Mark "bmap" as final, without checking for obviously redundant
1663 * integer divisions. This function should be used when "bmap"
1664 * is known not to involve any such integer divisions.
1666 __isl_give isl_basic_map *isl_basic_map_mark_final(
1667 __isl_take isl_basic_map *bmap)
1669 if (!bmap)
1670 return NULL;
1671 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1672 return bmap;
1675 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1677 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1679 bmap = remove_redundant_divs(bmap);
1680 bmap = isl_basic_map_mark_final(bmap);
1681 return bmap;
1684 __isl_give isl_basic_set *isl_basic_set_finalize(
1685 __isl_take isl_basic_set *bset)
1687 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1690 /* Remove definition of any div that is defined in terms of the given variable.
1691 * The div itself is not removed. Functions such as
1692 * eliminate_divs_ineq depend on the other divs remaining in place.
1694 static __isl_give isl_basic_map *remove_dependent_vars(
1695 __isl_take isl_basic_map *bmap, int pos)
1697 int i;
1699 if (!bmap)
1700 return NULL;
1702 for (i = 0; i < bmap->n_div; ++i) {
1703 if (isl_int_is_zero(bmap->div[i][0]))
1704 continue;
1705 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1706 continue;
1707 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1708 if (!bmap)
1709 return NULL;
1711 return bmap;
1714 /* Eliminate the specified variables from the constraints using
1715 * Fourier-Motzkin. The variables themselves are not removed.
1717 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1718 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1720 int d;
1721 int i, j, k;
1722 isl_size total;
1723 int need_gauss = 0;
1725 if (n == 0)
1726 return bmap;
1727 total = isl_basic_map_dim(bmap, isl_dim_all);
1728 if (total < 0)
1729 return isl_basic_map_free(bmap);
1731 bmap = isl_basic_map_cow(bmap);
1732 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1733 bmap = remove_dependent_vars(bmap, d);
1734 if (!bmap)
1735 return NULL;
1737 for (d = pos + n - 1;
1738 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1739 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1740 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1741 int n_lower, n_upper;
1742 if (!bmap)
1743 return NULL;
1744 for (i = 0; i < bmap->n_eq; ++i) {
1745 if (isl_int_is_zero(bmap->eq[i][1+d]))
1746 continue;
1747 bmap = eliminate_var_using_equality(bmap, d,
1748 bmap->eq[i], 0, NULL);
1749 if (isl_basic_map_drop_equality(bmap, i) < 0)
1750 return isl_basic_map_free(bmap);
1751 need_gauss = 1;
1752 break;
1754 if (i < bmap->n_eq)
1755 continue;
1756 n_lower = 0;
1757 n_upper = 0;
1758 for (i = 0; i < bmap->n_ineq; ++i) {
1759 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1760 n_lower++;
1761 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1762 n_upper++;
1764 bmap = isl_basic_map_extend_constraints(bmap,
1765 0, n_lower * n_upper);
1766 if (!bmap)
1767 goto error;
1768 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1769 int last;
1770 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1771 continue;
1772 last = -1;
1773 for (j = 0; j < i; ++j) {
1774 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1775 continue;
1776 last = j;
1777 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1778 isl_int_sgn(bmap->ineq[j][1+d]))
1779 continue;
1780 k = isl_basic_map_alloc_inequality(bmap);
1781 if (k < 0)
1782 goto error;
1783 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1784 1+total);
1785 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1786 1+d, 1+total, NULL);
1788 isl_basic_map_drop_inequality(bmap, i);
1789 i = last + 1;
1791 if (n_lower > 0 && n_upper > 0) {
1792 bmap = isl_basic_map_normalize_constraints(bmap);
1793 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1794 NULL, 0);
1795 bmap = isl_basic_map_gauss(bmap, NULL);
1796 bmap = isl_basic_map_remove_redundancies(bmap);
1797 need_gauss = 0;
1798 if (!bmap)
1799 goto error;
1800 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1801 break;
1804 if (need_gauss)
1805 bmap = isl_basic_map_gauss(bmap, NULL);
1806 return bmap;
1807 error:
1808 isl_basic_map_free(bmap);
1809 return NULL;
1812 __isl_give isl_basic_set *isl_basic_set_eliminate_vars(
1813 __isl_take isl_basic_set *bset, unsigned pos, unsigned n)
1815 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1816 pos, n));
1819 /* Eliminate the specified n dimensions starting at first from the
1820 * constraints, without removing the dimensions from the space.
1821 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1822 * Otherwise, they are projected out and the original space is restored.
1824 __isl_give isl_basic_map *isl_basic_map_eliminate(
1825 __isl_take isl_basic_map *bmap,
1826 enum isl_dim_type type, unsigned first, unsigned n)
1828 isl_space *space;
1830 if (!bmap)
1831 return NULL;
1832 if (n == 0)
1833 return bmap;
1835 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1836 return isl_basic_map_free(bmap);
1838 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1839 first += isl_basic_map_offset(bmap, type) - 1;
1840 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1841 return isl_basic_map_finalize(bmap);
1844 space = isl_basic_map_get_space(bmap);
1845 bmap = isl_basic_map_project_out(bmap, type, first, n);
1846 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1847 bmap = isl_basic_map_reset_space(bmap, space);
1848 return bmap;
1851 __isl_give isl_basic_set *isl_basic_set_eliminate(
1852 __isl_take isl_basic_set *bset,
1853 enum isl_dim_type type, unsigned first, unsigned n)
1855 return isl_basic_map_eliminate(bset, type, first, n);
1858 /* Remove all constraints from "bmap" that reference any unknown local
1859 * variables (directly or indirectly).
1861 * Dropping all constraints on a local variable will make it redundant,
1862 * so it will get removed implicitly by
1863 * isl_basic_map_drop_constraints_involving_dims. Some other local
1864 * variables may also end up becoming redundant if they only appear
1865 * in constraints together with the unknown local variable.
1866 * Therefore, start over after calling
1867 * isl_basic_map_drop_constraints_involving_dims.
1869 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_unknown_divs(
1870 __isl_take isl_basic_map *bmap)
1872 isl_bool known;
1873 isl_size n_div;
1874 int i, o_div;
1876 known = isl_basic_map_divs_known(bmap);
1877 if (known < 0)
1878 return isl_basic_map_free(bmap);
1879 if (known)
1880 return bmap;
1882 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1883 if (n_div < 0)
1884 return isl_basic_map_free(bmap);
1885 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1887 for (i = 0; i < n_div; ++i) {
1888 known = isl_basic_map_div_is_known(bmap, i);
1889 if (known < 0)
1890 return isl_basic_map_free(bmap);
1891 if (known)
1892 continue;
1893 bmap = remove_dependent_vars(bmap, o_div + i);
1894 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1895 isl_dim_div, i, 1);
1896 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1897 if (n_div < 0)
1898 return isl_basic_map_free(bmap);
1899 i = -1;
1902 return bmap;
1905 /* Remove all constraints from "bset" that reference any unknown local
1906 * variables (directly or indirectly).
1908 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_unknown_divs(
1909 __isl_take isl_basic_set *bset)
1911 isl_basic_map *bmap;
1913 bmap = bset_to_bmap(bset);
1914 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
1915 return bset_from_bmap(bmap);
1918 /* Remove all constraints from "map" that reference any unknown local
1919 * variables (directly or indirectly).
1921 * Since constraints may get dropped from the basic maps,
1922 * they may no longer be disjoint from each other.
1924 __isl_give isl_map *isl_map_drop_constraints_involving_unknown_divs(
1925 __isl_take isl_map *map)
1927 int i;
1928 isl_bool known;
1930 known = isl_map_divs_known(map);
1931 if (known < 0)
1932 return isl_map_free(map);
1933 if (known)
1934 return map;
1936 map = isl_map_cow(map);
1937 if (!map)
1938 return NULL;
1940 for (i = 0; i < map->n; ++i) {
1941 map->p[i] =
1942 isl_basic_map_drop_constraints_involving_unknown_divs(
1943 map->p[i]);
1944 if (!map->p[i])
1945 return isl_map_free(map);
1948 if (map->n > 1)
1949 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1951 return map;
1954 /* Don't assume equalities are in order, because align_divs
1955 * may have changed the order of the divs.
1957 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim,
1958 unsigned len)
1960 int d, i;
1962 for (d = 0; d < len; ++d)
1963 elim[d] = -1;
1964 for (i = 0; i < bmap->n_eq; ++i) {
1965 for (d = len - 1; d >= 0; --d) {
1966 if (isl_int_is_zero(bmap->eq[i][1+d]))
1967 continue;
1968 elim[d] = i;
1969 break;
1974 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1975 int *elim, unsigned len)
1977 compute_elimination_index(bset_to_bmap(bset), elim, len);
1980 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1981 __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
1983 int d;
1984 int copied = 0;
1986 for (d = total - 1; d >= 0; --d) {
1987 if (isl_int_is_zero(src[1+d]))
1988 continue;
1989 if (elim[d] == -1)
1990 continue;
1991 if (!copied) {
1992 isl_seq_cpy(dst, src, 1 + total);
1993 copied = 1;
1995 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1997 return copied;
2000 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2001 __isl_keep isl_basic_set *bset, int *elim, unsigned total)
2003 return reduced_using_equalities(dst, src,
2004 bset_to_bmap(bset), elim, total);
2007 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
2008 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2010 int i;
2011 int *elim;
2012 isl_size dim;
2014 if (!bset || !context)
2015 goto error;
2017 if (context->n_eq == 0) {
2018 isl_basic_set_free(context);
2019 return bset;
2022 bset = isl_basic_set_cow(bset);
2023 dim = isl_basic_set_dim(bset, isl_dim_set);
2024 if (dim < 0)
2025 goto error;
2027 elim = isl_alloc_array(bset->ctx, int, dim);
2028 if (!elim)
2029 goto error;
2030 set_compute_elimination_index(context, elim, dim);
2031 for (i = 0; i < bset->n_eq; ++i)
2032 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2033 context, elim, dim);
2034 for (i = 0; i < bset->n_ineq; ++i)
2035 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2036 context, elim, dim);
2037 isl_basic_set_free(context);
2038 free(elim);
2039 bset = isl_basic_set_simplify(bset);
2040 bset = isl_basic_set_finalize(bset);
2041 return bset;
2042 error:
2043 isl_basic_set_free(bset);
2044 isl_basic_set_free(context);
2045 return NULL;
2048 /* For each inequality in "ineq" that is a shifted (more relaxed)
2049 * copy of an inequality in "context", mark the corresponding entry
2050 * in "row" with -1.
2051 * If an inequality only has a non-negative constant term, then
2052 * mark it as well.
2054 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2055 __isl_keep isl_basic_set *context, int *row)
2057 struct isl_constraint_index ci;
2058 isl_size n_ineq, cols;
2059 unsigned total;
2060 int k;
2062 if (!ineq || !context)
2063 return isl_stat_error;
2064 if (context->n_ineq == 0)
2065 return isl_stat_ok;
2066 if (setup_constraint_index(&ci, context) < 0)
2067 return isl_stat_error;
2069 n_ineq = isl_mat_rows(ineq);
2070 cols = isl_mat_cols(ineq);
2071 if (n_ineq < 0 || cols < 0)
2072 return isl_stat_error;
2073 total = cols - 1;
2074 for (k = 0; k < n_ineq; ++k) {
2075 int l;
2076 isl_bool redundant;
2078 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2079 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2080 row[k] = -1;
2081 continue;
2083 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2084 if (redundant < 0)
2085 goto error;
2086 if (!redundant)
2087 continue;
2088 row[k] = -1;
2090 constraint_index_free(&ci);
2091 return isl_stat_ok;
2092 error:
2093 constraint_index_free(&ci);
2094 return isl_stat_error;
2097 static __isl_give isl_basic_set *remove_shifted_constraints(
2098 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
2100 struct isl_constraint_index ci;
2101 int k;
2103 if (!bset || !context)
2104 return bset;
2106 if (context->n_ineq == 0)
2107 return bset;
2108 if (setup_constraint_index(&ci, context) < 0)
2109 return bset;
2111 for (k = 0; k < bset->n_ineq; ++k) {
2112 isl_bool redundant;
2114 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2115 if (redundant < 0)
2116 goto error;
2117 if (!redundant)
2118 continue;
2119 bset = isl_basic_set_cow(bset);
2120 if (!bset)
2121 goto error;
2122 isl_basic_set_drop_inequality(bset, k);
2123 --k;
2125 constraint_index_free(&ci);
2126 return bset;
2127 error:
2128 constraint_index_free(&ci);
2129 return bset;
2132 /* Remove constraints from "bmap" that are identical to constraints
2133 * in "context" or that are more relaxed (greater constant term).
2135 * We perform the test for shifted copies on the pure constraints
2136 * in remove_shifted_constraints.
2138 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2139 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2141 isl_basic_set *bset, *bset_context;
2143 if (!bmap || !context)
2144 goto error;
2146 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2147 isl_basic_map_free(context);
2148 return bmap;
2151 bmap = isl_basic_map_order_divs(bmap);
2152 context = isl_basic_map_align_divs(context, bmap);
2153 bmap = isl_basic_map_align_divs(bmap, context);
2155 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2156 bset_context = isl_basic_map_underlying_set(context);
2157 bset = remove_shifted_constraints(bset, bset_context);
2158 isl_basic_set_free(bset_context);
2160 bmap = isl_basic_map_overlying_set(bset, bmap);
2162 return bmap;
2163 error:
2164 isl_basic_map_free(bmap);
2165 isl_basic_map_free(context);
2166 return NULL;
2169 /* Does the (linear part of a) constraint "c" involve any of the "len"
2170 * "relevant" dimensions?
2172 static int is_related(isl_int *c, int len, int *relevant)
2174 int i;
2176 for (i = 0; i < len; ++i) {
2177 if (!relevant[i])
2178 continue;
2179 if (!isl_int_is_zero(c[i]))
2180 return 1;
2183 return 0;
2186 /* Drop constraints from "bmap" that do not involve any of
2187 * the dimensions marked "relevant".
2189 static __isl_give isl_basic_map *drop_unrelated_constraints(
2190 __isl_take isl_basic_map *bmap, int *relevant)
2192 int i;
2193 isl_size dim;
2195 dim = isl_basic_map_dim(bmap, isl_dim_all);
2196 if (dim < 0)
2197 return isl_basic_map_free(bmap);
2198 for (i = 0; i < dim; ++i)
2199 if (!relevant[i])
2200 break;
2201 if (i >= dim)
2202 return bmap;
2204 for (i = bmap->n_eq - 1; i >= 0; --i)
2205 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2206 bmap = isl_basic_map_cow(bmap);
2207 if (isl_basic_map_drop_equality(bmap, i) < 0)
2208 return isl_basic_map_free(bmap);
2211 for (i = bmap->n_ineq - 1; i >= 0; --i)
2212 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2213 bmap = isl_basic_map_cow(bmap);
2214 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2215 return isl_basic_map_free(bmap);
2218 return bmap;
2221 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2223 * In particular, for any variable involved in the constraint,
2224 * find the actual group id from before and replace the group
2225 * of the corresponding variable by the minimal group of all
2226 * the variables involved in the constraint considered so far
2227 * (if this minimum is smaller) or replace the minimum by this group
2228 * (if the minimum is larger).
2230 * At the end, all the variables in "c" will (indirectly) point
2231 * to the minimal of the groups that they referred to originally.
2233 static void update_groups(int dim, int *group, isl_int *c)
2235 int j;
2236 int min = dim;
2238 for (j = 0; j < dim; ++j) {
2239 if (isl_int_is_zero(c[j]))
2240 continue;
2241 while (group[j] >= 0 && group[group[j]] != group[j])
2242 group[j] = group[group[j]];
2243 if (group[j] == min)
2244 continue;
2245 if (group[j] < min) {
2246 if (min >= 0 && min < dim)
2247 group[min] = group[j];
2248 min = group[j];
2249 } else
2250 group[group[j]] = min;
2254 /* Allocate an array of groups of variables, one for each variable
2255 * in "context", initialized to zero.
2257 static int *alloc_groups(__isl_keep isl_basic_set *context)
2259 isl_ctx *ctx;
2260 isl_size dim;
2262 dim = isl_basic_set_dim(context, isl_dim_set);
2263 if (dim < 0)
2264 return NULL;
2265 ctx = isl_basic_set_get_ctx(context);
2266 return isl_calloc_array(ctx, int, dim);
2269 /* Drop constraints from "bmap" that only involve variables that are
2270 * not related to any of the variables marked with a "-1" in "group".
2272 * We construct groups of variables that collect variables that
2273 * (indirectly) appear in some common constraint of "bmap".
2274 * Each group is identified by the first variable in the group,
2275 * except for the special group of variables that was already identified
2276 * in the input as -1 (or are related to those variables).
2277 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2278 * otherwise the group of i is the group of group[i].
2280 * We first initialize groups for the remaining variables.
2281 * Then we iterate over the constraints of "bmap" and update the
2282 * group of the variables in the constraint by the smallest group.
2283 * Finally, we resolve indirect references to groups by running over
2284 * the variables.
2286 * After computing the groups, we drop constraints that do not involve
2287 * any variables in the -1 group.
2289 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2290 __isl_take isl_basic_map *bmap, __isl_take int *group)
2292 isl_size dim;
2293 int i;
2294 int last;
2296 dim = isl_basic_map_dim(bmap, isl_dim_all);
2297 if (dim < 0)
2298 return isl_basic_map_free(bmap);
2300 last = -1;
2301 for (i = 0; i < dim; ++i)
2302 if (group[i] >= 0)
2303 last = group[i] = i;
2304 if (last < 0) {
2305 free(group);
2306 return bmap;
2309 for (i = 0; i < bmap->n_eq; ++i)
2310 update_groups(dim, group, bmap->eq[i] + 1);
2311 for (i = 0; i < bmap->n_ineq; ++i)
2312 update_groups(dim, group, bmap->ineq[i] + 1);
2314 for (i = 0; i < dim; ++i)
2315 if (group[i] >= 0)
2316 group[i] = group[group[i]];
2318 for (i = 0; i < dim; ++i)
2319 group[i] = group[i] == -1;
2321 bmap = drop_unrelated_constraints(bmap, group);
2323 free(group);
2324 return bmap;
2327 /* Drop constraints from "context" that are irrelevant for computing
2328 * the gist of "bset".
2330 * In particular, drop constraints in variables that are not related
2331 * to any of the variables involved in the constraints of "bset"
2332 * in the sense that there is no sequence of constraints that connects them.
2334 * We first mark all variables that appear in "bset" as belonging
2335 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2337 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2338 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2340 int *group;
2341 isl_size dim;
2342 int i, j;
2344 dim = isl_basic_set_dim(bset, isl_dim_set);
2345 if (!context || dim < 0)
2346 return isl_basic_set_free(context);
2348 group = alloc_groups(context);
2350 if (!group)
2351 return isl_basic_set_free(context);
2353 for (i = 0; i < dim; ++i) {
2354 for (j = 0; j < bset->n_eq; ++j)
2355 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2356 break;
2357 if (j < bset->n_eq) {
2358 group[i] = -1;
2359 continue;
2361 for (j = 0; j < bset->n_ineq; ++j)
2362 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2363 break;
2364 if (j < bset->n_ineq)
2365 group[i] = -1;
2368 return isl_basic_map_drop_unrelated_constraints(context, group);
2371 /* Drop constraints from "context" that are irrelevant for computing
2372 * the gist of the inequalities "ineq".
2373 * Inequalities in "ineq" for which the corresponding element of row
2374 * is set to -1 have already been marked for removal and should be ignored.
2376 * In particular, drop constraints in variables that are not related
2377 * to any of the variables involved in "ineq"
2378 * in the sense that there is no sequence of constraints that connects them.
2380 * We first mark all variables that appear in "bset" as belonging
2381 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2383 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2384 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2386 int *group;
2387 isl_size dim;
2388 int i, j;
2389 isl_size n;
2391 dim = isl_basic_set_dim(context, isl_dim_set);
2392 n = isl_mat_rows(ineq);
2393 if (dim < 0 || n < 0)
2394 return isl_basic_set_free(context);
2396 group = alloc_groups(context);
2398 if (!group)
2399 return isl_basic_set_free(context);
2401 for (i = 0; i < dim; ++i) {
2402 for (j = 0; j < n; ++j) {
2403 if (row[j] < 0)
2404 continue;
2405 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2406 break;
2408 if (j < n)
2409 group[i] = -1;
2412 return isl_basic_map_drop_unrelated_constraints(context, group);
2415 /* Do all "n" entries of "row" contain a negative value?
2417 static int all_neg(int *row, int n)
2419 int i;
2421 for (i = 0; i < n; ++i)
2422 if (row[i] >= 0)
2423 return 0;
2425 return 1;
2428 /* Update the inequalities in "bset" based on the information in "row"
2429 * and "tab".
2431 * In particular, the array "row" contains either -1, meaning that
2432 * the corresponding inequality of "bset" is redundant, or the index
2433 * of an inequality in "tab".
2435 * If the row entry is -1, then drop the inequality.
2436 * Otherwise, if the constraint is marked redundant in the tableau,
2437 * then drop the inequality. Similarly, if it is marked as an equality
2438 * in the tableau, then turn the inequality into an equality and
2439 * perform Gaussian elimination.
2441 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2442 __isl_keep int *row, struct isl_tab *tab)
2444 int i;
2445 unsigned n_ineq;
2446 unsigned n_eq;
2447 int found_equality = 0;
2449 if (!bset)
2450 return NULL;
2451 if (tab && tab->empty)
2452 return isl_basic_set_set_to_empty(bset);
2454 n_ineq = bset->n_ineq;
2455 for (i = n_ineq - 1; i >= 0; --i) {
2456 if (row[i] < 0) {
2457 if (isl_basic_set_drop_inequality(bset, i) < 0)
2458 return isl_basic_set_free(bset);
2459 continue;
2461 if (!tab)
2462 continue;
2463 n_eq = tab->n_eq;
2464 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2465 isl_basic_map_inequality_to_equality(bset, i);
2466 found_equality = 1;
2467 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2468 if (isl_basic_set_drop_inequality(bset, i) < 0)
2469 return isl_basic_set_free(bset);
2473 if (found_equality)
2474 bset = isl_basic_set_gauss(bset, NULL);
2475 bset = isl_basic_set_finalize(bset);
2476 return bset;
2479 /* Update the inequalities in "bset" based on the information in "row"
2480 * and "tab" and free all arguments (other than "bset").
2482 static __isl_give isl_basic_set *update_ineq_free(
2483 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2484 __isl_take isl_basic_set *context, __isl_take int *row,
2485 struct isl_tab *tab)
2487 isl_mat_free(ineq);
2488 isl_basic_set_free(context);
2490 bset = update_ineq(bset, row, tab);
2492 free(row);
2493 isl_tab_free(tab);
2494 return bset;
2497 /* Remove all information from bset that is redundant in the context
2498 * of context.
2499 * "ineq" contains the (possibly transformed) inequalities of "bset",
2500 * in the same order.
2501 * The (explicit) equalities of "bset" are assumed to have been taken
2502 * into account by the transformation such that only the inequalities
2503 * are relevant.
2504 * "context" is assumed not to be empty.
2506 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2507 * A value of -1 means that the inequality is obviously redundant and may
2508 * not even appear in "tab".
2510 * We first mark the inequalities of "bset"
2511 * that are obviously redundant with respect to some inequality in "context".
2512 * Then we remove those constraints from "context" that have become
2513 * irrelevant for computing the gist of "bset".
2514 * Note that this removal of constraints cannot be replaced by
2515 * a factorization because factors in "bset" may still be connected
2516 * to each other through constraints in "context".
2518 * If there are any inequalities left, we construct a tableau for
2519 * the context and then add the inequalities of "bset".
2520 * Before adding these inequalities, we freeze all constraints such that
2521 * they won't be considered redundant in terms of the constraints of "bset".
2522 * Then we detect all redundant constraints (among the
2523 * constraints that weren't frozen), first by checking for redundancy in the
2524 * the tableau and then by checking if replacing a constraint by its negation
2525 * would lead to an empty set. This last step is fairly expensive
2526 * and could be optimized by more reuse of the tableau.
2527 * Finally, we update bset according to the results.
2529 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2530 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2532 int i, r;
2533 int *row = NULL;
2534 isl_ctx *ctx;
2535 isl_basic_set *combined = NULL;
2536 struct isl_tab *tab = NULL;
2537 unsigned n_eq, context_ineq;
2539 if (!bset || !ineq || !context)
2540 goto error;
2542 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2543 isl_basic_set_free(context);
2544 isl_mat_free(ineq);
2545 return bset;
2548 ctx = isl_basic_set_get_ctx(context);
2549 row = isl_calloc_array(ctx, int, bset->n_ineq);
2550 if (!row)
2551 goto error;
2553 if (mark_shifted_constraints(ineq, context, row) < 0)
2554 goto error;
2555 if (all_neg(row, bset->n_ineq))
2556 return update_ineq_free(bset, ineq, context, row, NULL);
2558 context = drop_irrelevant_constraints_marked(context, ineq, row);
2559 if (!context)
2560 goto error;
2561 if (isl_basic_set_plain_is_universe(context))
2562 return update_ineq_free(bset, ineq, context, row, NULL);
2564 n_eq = context->n_eq;
2565 context_ineq = context->n_ineq;
2566 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2567 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2568 tab = isl_tab_from_basic_set(combined, 0);
2569 for (i = 0; i < context_ineq; ++i)
2570 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2571 goto error;
2572 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2573 goto error;
2574 r = context_ineq;
2575 for (i = 0; i < bset->n_ineq; ++i) {
2576 if (row[i] < 0)
2577 continue;
2578 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2579 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2580 goto error;
2581 row[i] = r++;
2583 if (isl_tab_detect_implicit_equalities(tab) < 0)
2584 goto error;
2585 if (isl_tab_detect_redundant(tab) < 0)
2586 goto error;
2587 for (i = bset->n_ineq - 1; i >= 0; --i) {
2588 isl_basic_set *test;
2589 int is_empty;
2591 if (row[i] < 0)
2592 continue;
2593 r = row[i];
2594 if (tab->con[n_eq + r].is_redundant)
2595 continue;
2596 test = isl_basic_set_dup(combined);
2597 test = isl_inequality_negate(test, r);
2598 test = isl_basic_set_update_from_tab(test, tab);
2599 is_empty = isl_basic_set_is_empty(test);
2600 isl_basic_set_free(test);
2601 if (is_empty < 0)
2602 goto error;
2603 if (is_empty)
2604 tab->con[n_eq + r].is_redundant = 1;
2606 bset = update_ineq_free(bset, ineq, context, row, tab);
2607 if (bset) {
2608 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2609 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2612 isl_basic_set_free(combined);
2613 return bset;
2614 error:
2615 free(row);
2616 isl_mat_free(ineq);
2617 isl_tab_free(tab);
2618 isl_basic_set_free(combined);
2619 isl_basic_set_free(context);
2620 isl_basic_set_free(bset);
2621 return NULL;
2624 /* Extract the inequalities of "bset" as an isl_mat.
2626 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2628 isl_size total;
2629 isl_ctx *ctx;
2630 isl_mat *ineq;
2632 total = isl_basic_set_dim(bset, isl_dim_all);
2633 if (total < 0)
2634 return NULL;
2636 ctx = isl_basic_set_get_ctx(bset);
2637 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2638 0, 1 + total);
2640 return ineq;
2643 /* Remove all information from "bset" that is redundant in the context
2644 * of "context", for the case where both "bset" and "context" are
2645 * full-dimensional.
2647 static __isl_give isl_basic_set *uset_gist_uncompressed(
2648 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2650 isl_mat *ineq;
2652 ineq = extract_ineq(bset);
2653 return uset_gist_full(bset, ineq, context);
2656 /* Replace "bset" by an empty basic set in the same space.
2658 static __isl_give isl_basic_set *replace_by_empty(
2659 __isl_take isl_basic_set *bset)
2661 isl_space *space;
2663 space = isl_basic_set_get_space(bset);
2664 isl_basic_set_free(bset);
2665 return isl_basic_set_empty(space);
2668 /* Remove all information from "bset" that is redundant in the context
2669 * of "context", for the case where the combined equalities of
2670 * "bset" and "context" allow for a compression that can be obtained
2671 * by preapplication of "T".
2672 * If the compression of "context" is empty, meaning that "bset" and
2673 * "context" do not intersect, then return the empty set.
2675 * "bset" itself is not transformed by "T". Instead, the inequalities
2676 * are extracted from "bset" and those are transformed by "T".
2677 * uset_gist_full then determines which of the transformed inequalities
2678 * are redundant with respect to the transformed "context" and removes
2679 * the corresponding inequalities from "bset".
2681 * After preapplying "T" to the inequalities, any common factor is
2682 * removed from the coefficients. If this results in a tightening
2683 * of the constant term, then the same tightening is applied to
2684 * the corresponding untransformed inequality in "bset".
2685 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2687 * g f'(x) + r >= 0
2689 * with 0 <= r < g, then it is equivalent to
2691 * f'(x) >= 0
2693 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2694 * subspace compressed by T since the latter would be transformed to
2696 * g f'(x) >= 0
2698 static __isl_give isl_basic_set *uset_gist_compressed(
2699 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2700 __isl_take isl_mat *T)
2702 isl_ctx *ctx;
2703 isl_mat *ineq;
2704 int i;
2705 isl_size n_row, n_col;
2706 isl_int rem;
2708 ineq = extract_ineq(bset);
2709 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2710 context = isl_basic_set_preimage(context, T);
2712 if (!ineq || !context)
2713 goto error;
2714 if (isl_basic_set_plain_is_empty(context)) {
2715 isl_mat_free(ineq);
2716 isl_basic_set_free(context);
2717 return replace_by_empty(bset);
2720 ctx = isl_mat_get_ctx(ineq);
2721 n_row = isl_mat_rows(ineq);
2722 n_col = isl_mat_cols(ineq);
2723 if (n_row < 0 || n_col < 0)
2724 goto error;
2725 isl_int_init(rem);
2726 for (i = 0; i < n_row; ++i) {
2727 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2728 if (isl_int_is_zero(ctx->normalize_gcd))
2729 continue;
2730 if (isl_int_is_one(ctx->normalize_gcd))
2731 continue;
2732 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2733 ctx->normalize_gcd, n_col - 1);
2734 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2735 isl_int_fdiv_q(ineq->row[i][0],
2736 ineq->row[i][0], ctx->normalize_gcd);
2737 if (isl_int_is_zero(rem))
2738 continue;
2739 bset = isl_basic_set_cow(bset);
2740 if (!bset)
2741 break;
2742 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2744 isl_int_clear(rem);
2746 return uset_gist_full(bset, ineq, context);
2747 error:
2748 isl_mat_free(ineq);
2749 isl_basic_set_free(context);
2750 isl_basic_set_free(bset);
2751 return NULL;
2754 /* Project "bset" onto the variables that are involved in "template".
2756 static __isl_give isl_basic_set *project_onto_involved(
2757 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2759 int i;
2760 isl_size n;
2762 n = isl_basic_set_dim(template, isl_dim_set);
2763 if (n < 0 || !template)
2764 return isl_basic_set_free(bset);
2766 for (i = 0; i < n; ++i) {
2767 isl_bool involved;
2769 involved = isl_basic_set_involves_dims(template,
2770 isl_dim_set, i, 1);
2771 if (involved < 0)
2772 return isl_basic_set_free(bset);
2773 if (involved)
2774 continue;
2775 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2778 return bset;
2781 /* Remove all information from bset that is redundant in the context
2782 * of context. In particular, equalities that are linear combinations
2783 * of those in context are removed. Then the inequalities that are
2784 * redundant in the context of the equalities and inequalities of
2785 * context are removed.
2787 * First of all, we drop those constraints from "context"
2788 * that are irrelevant for computing the gist of "bset".
2789 * Alternatively, we could factorize the intersection of "context" and "bset".
2791 * We first compute the intersection of the integer affine hulls
2792 * of "bset" and "context",
2793 * compute the gist inside this intersection and then reduce
2794 * the constraints with respect to the equalities of the context
2795 * that only involve variables already involved in the input.
2796 * If the intersection of the affine hulls turns out to be empty,
2797 * then return the empty set.
2799 * If two constraints are mutually redundant, then uset_gist_full
2800 * will remove the second of those constraints. We therefore first
2801 * sort the constraints so that constraints not involving existentially
2802 * quantified variables are given precedence over those that do.
2803 * We have to perform this sorting before the variable compression,
2804 * because that may effect the order of the variables.
2806 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2807 __isl_take isl_basic_set *context)
2809 isl_mat *eq;
2810 isl_mat *T;
2811 isl_basic_set *aff;
2812 isl_basic_set *aff_context;
2813 isl_size total;
2815 total = isl_basic_set_dim(bset, isl_dim_all);
2816 if (total < 0 || !context)
2817 goto error;
2819 context = drop_irrelevant_constraints(context, bset);
2821 bset = isl_basic_set_detect_equalities(bset);
2822 aff = isl_basic_set_copy(bset);
2823 aff = isl_basic_set_plain_affine_hull(aff);
2824 context = isl_basic_set_detect_equalities(context);
2825 aff_context = isl_basic_set_copy(context);
2826 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2827 aff = isl_basic_set_intersect(aff, aff_context);
2828 if (!aff)
2829 goto error;
2830 if (isl_basic_set_plain_is_empty(aff)) {
2831 isl_basic_set_free(bset);
2832 isl_basic_set_free(context);
2833 return aff;
2835 bset = isl_basic_set_sort_constraints(bset);
2836 if (aff->n_eq == 0) {
2837 isl_basic_set_free(aff);
2838 return uset_gist_uncompressed(bset, context);
2840 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2841 eq = isl_mat_cow(eq);
2842 T = isl_mat_variable_compression(eq, NULL);
2843 isl_basic_set_free(aff);
2844 if (T && T->n_col == 0) {
2845 isl_mat_free(T);
2846 isl_basic_set_free(context);
2847 return replace_by_empty(bset);
2850 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2851 aff_context = project_onto_involved(aff_context, bset);
2853 bset = uset_gist_compressed(bset, context, T);
2854 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2856 if (bset) {
2857 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2858 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2861 return bset;
2862 error:
2863 isl_basic_set_free(bset);
2864 isl_basic_set_free(context);
2865 return NULL;
2868 /* Return the number of equality constraints in "bmap" that involve
2869 * local variables. This function assumes that Gaussian elimination
2870 * has been applied to the equality constraints.
2872 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2874 int i;
2875 isl_size total, n_div;
2877 if (!bmap)
2878 return -1;
2880 if (bmap->n_eq == 0)
2881 return 0;
2883 total = isl_basic_map_dim(bmap, isl_dim_all);
2884 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2885 if (total < 0 || n_div < 0)
2886 return -1;
2887 total -= n_div;
2889 for (i = 0; i < bmap->n_eq; ++i)
2890 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2891 n_div) == -1)
2892 return i;
2894 return bmap->n_eq;
2897 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2898 * The constraints are assumed not to involve any local variables.
2900 static __isl_give isl_basic_map *basic_map_from_equalities(
2901 __isl_take isl_space *space, __isl_take isl_mat *eq)
2903 int i, k;
2904 isl_size total;
2905 isl_basic_map *bmap = NULL;
2907 total = isl_space_dim(space, isl_dim_all);
2908 if (total < 0 || !eq)
2909 goto error;
2911 if (1 + total != eq->n_col)
2912 isl_die(isl_space_get_ctx(space), isl_error_internal,
2913 "unexpected number of columns", goto error);
2915 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2916 0, eq->n_row, 0);
2917 for (i = 0; i < eq->n_row; ++i) {
2918 k = isl_basic_map_alloc_equality(bmap);
2919 if (k < 0)
2920 goto error;
2921 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2924 isl_space_free(space);
2925 isl_mat_free(eq);
2926 return bmap;
2927 error:
2928 isl_space_free(space);
2929 isl_mat_free(eq);
2930 isl_basic_map_free(bmap);
2931 return NULL;
2934 /* Construct and return a variable compression based on the equality
2935 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2936 * "n1" is the number of (initial) equality constraints in "bmap1"
2937 * that do involve local variables.
2938 * "n2" is the number of (initial) equality constraints in "bmap2"
2939 * that do involve local variables.
2940 * "total" is the total number of other variables.
2941 * This function assumes that Gaussian elimination
2942 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2943 * such that the equality constraints not involving local variables
2944 * are those that start at "n1" or "n2".
2946 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2947 * then simply compute the compression based on the equality constraints
2948 * in the other basic map.
2949 * Otherwise, combine the equality constraints from both into a new
2950 * basic map such that Gaussian elimination can be applied to this combination
2951 * and then construct a variable compression from the resulting
2952 * equality constraints.
2954 static __isl_give isl_mat *combined_variable_compression(
2955 __isl_keep isl_basic_map *bmap1, int n1,
2956 __isl_keep isl_basic_map *bmap2, int n2, int total)
2958 isl_ctx *ctx;
2959 isl_mat *E1, *E2, *V;
2960 isl_basic_map *bmap;
2962 ctx = isl_basic_map_get_ctx(bmap1);
2963 if (bmap1->n_eq == n1) {
2964 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2965 n2, bmap2->n_eq - n2, 0, 1 + total);
2966 return isl_mat_variable_compression(E2, NULL);
2968 if (bmap2->n_eq == n2) {
2969 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2970 n1, bmap1->n_eq - n1, 0, 1 + total);
2971 return isl_mat_variable_compression(E1, NULL);
2973 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2974 n1, bmap1->n_eq - n1, 0, 1 + total);
2975 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2976 n2, bmap2->n_eq - n2, 0, 1 + total);
2977 E1 = isl_mat_concat(E1, E2);
2978 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2979 bmap = isl_basic_map_gauss(bmap, NULL);
2980 if (!bmap)
2981 return NULL;
2982 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2983 V = isl_mat_variable_compression(E1, NULL);
2984 isl_basic_map_free(bmap);
2986 return V;
2989 /* Extract the stride constraints from "bmap", compressed
2990 * with respect to both the stride constraints in "context" and
2991 * the remaining equality constraints in both "bmap" and "context".
2992 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2993 * "context_n_eq" is the number of (initial) stride constraints in "context".
2995 * Let x be all variables in "bmap" (and "context") other than the local
2996 * variables. First compute a variable compression
2998 * x = V x'
3000 * based on the non-stride equality constraints in "bmap" and "context".
3001 * Consider the stride constraints of "context",
3003 * A(x) + B(y) = 0
3005 * with y the local variables and plug in the variable compression,
3006 * resulting in
3008 * A(V x') + B(y) = 0
3010 * Use these constraints to compute a parameter compression on x'
3012 * x' = T x''
3014 * Now consider the stride constraints of "bmap"
3016 * C(x) + D(y) = 0
3018 * and plug in x = V*T x''.
3019 * That is, return A = [C*V*T D].
3021 static __isl_give isl_mat *extract_compressed_stride_constraints(
3022 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3023 __isl_keep isl_basic_map *context, int context_n_eq)
3025 isl_size total, n_div;
3026 isl_ctx *ctx;
3027 isl_mat *A, *B, *T, *V;
3029 total = isl_basic_map_dim(context, isl_dim_all);
3030 n_div = isl_basic_map_dim(context, isl_dim_div);
3031 if (total < 0 || n_div < 0)
3032 return NULL;
3033 total -= n_div;
3035 ctx = isl_basic_map_get_ctx(bmap);
3037 V = combined_variable_compression(bmap, bmap_n_eq,
3038 context, context_n_eq, total);
3040 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3041 B = isl_mat_sub_alloc6(ctx, context->eq,
3042 0, context_n_eq, 1 + total, n_div);
3043 A = isl_mat_product(A, isl_mat_copy(V));
3044 T = isl_mat_parameter_compression_ext(A, B);
3045 T = isl_mat_product(V, T);
3047 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3048 if (n_div < 0)
3049 T = isl_mat_free(T);
3050 else
3051 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3053 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3054 0, bmap_n_eq, 0, 1 + total + n_div);
3055 A = isl_mat_product(A, T);
3057 return A;
3060 /* Remove the prime factors from *g that have an exponent that
3061 * is strictly smaller than the exponent in "c".
3062 * All exponents in *g are known to be smaller than or equal
3063 * to those in "c".
3065 * That is, if *g is equal to
3067 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3069 * and "c" is equal to
3071 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3073 * then update *g to
3075 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3076 * p_n^{e_n * (e_n = f_n)}
3078 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3079 * neither does the gcd of *g and c / *g.
3080 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3081 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3082 * Dividing *g by this gcd therefore strictly reduces the exponent
3083 * of the prime factors that need to be removed, while leaving the
3084 * other prime factors untouched.
3085 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3086 * removes all undesired factors, without removing any others.
3088 static void remove_incomplete_powers(isl_int *g, isl_int c)
3090 isl_int t;
3092 isl_int_init(t);
3093 for (;;) {
3094 isl_int_divexact(t, c, *g);
3095 isl_int_gcd(t, t, *g);
3096 if (isl_int_is_one(t))
3097 break;
3098 isl_int_divexact(*g, *g, t);
3100 isl_int_clear(t);
3103 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3104 * of the same stride constraints in a compressed space that exploits
3105 * all equalities in the context and the other equalities in "bmap".
3107 * If the stride constraints of "bmap" are of the form
3109 * C(x) + D(y) = 0
3111 * then A is of the form
3113 * B(x') + D(y) = 0
3115 * If any of these constraints involves only a single local variable y,
3116 * then the constraint appears as
3118 * f(x) + m y_i = 0
3120 * in "bmap" and as
3122 * h(x') + m y_i = 0
3124 * in "A".
3126 * Let g be the gcd of m and the coefficients of h.
3127 * Then, in particular, g is a divisor of the coefficients of h and
3129 * f(x) = h(x')
3131 * is known to be a multiple of g.
3132 * If some prime factor in m appears with the same exponent in g,
3133 * then it can be removed from m because f(x) is already known
3134 * to be a multiple of g and therefore in particular of this power
3135 * of the prime factors.
3136 * Prime factors that appear with a smaller exponent in g cannot
3137 * be removed from m.
3138 * Let g' be the divisor of g containing all prime factors that
3139 * appear with the same exponent in m and g, then
3141 * f(x) + m y_i = 0
3143 * can be replaced by
3145 * f(x) + m/g' y_i' = 0
3147 * Note that (if g' != 1) this changes the explicit representation
3148 * of y_i to that of y_i', so the integer division at position i
3149 * is marked unknown and later recomputed by a call to
3150 * isl_basic_map_gauss.
3152 static __isl_give isl_basic_map *reduce_stride_constraints(
3153 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3155 int i;
3156 isl_size total, n_div;
3157 int any = 0;
3158 isl_int gcd;
3160 total = isl_basic_map_dim(bmap, isl_dim_all);
3161 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3162 if (total < 0 || n_div < 0 || !A)
3163 return isl_basic_map_free(bmap);
3164 total -= n_div;
3166 isl_int_init(gcd);
3167 for (i = 0; i < n; ++i) {
3168 int div;
3170 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3171 if (div < 0)
3172 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3173 "equality constraints modified unexpectedly",
3174 goto error);
3175 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3176 n_div - div - 1) != -1)
3177 continue;
3178 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3179 goto error;
3180 if (isl_int_is_one(gcd))
3181 continue;
3182 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3183 if (isl_int_is_one(gcd))
3184 continue;
3185 isl_int_divexact(bmap->eq[i][1 + total + div],
3186 bmap->eq[i][1 + total + div], gcd);
3187 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3188 if (!bmap)
3189 goto error;
3190 any = 1;
3192 isl_int_clear(gcd);
3194 if (any)
3195 bmap = isl_basic_map_gauss(bmap, NULL);
3197 return bmap;
3198 error:
3199 isl_int_clear(gcd);
3200 isl_basic_map_free(bmap);
3201 return NULL;
3204 /* Simplify the stride constraints in "bmap" based on
3205 * the remaining equality constraints in "bmap" and all equality
3206 * constraints in "context".
3207 * Only do this if both "bmap" and "context" have stride constraints.
3209 * First extract a copy of the stride constraints in "bmap" in a compressed
3210 * space exploiting all the other equality constraints and then
3211 * use this compressed copy to simplify the original stride constraints.
3213 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3214 __isl_keep isl_basic_map *context)
3216 int bmap_n_eq, context_n_eq;
3217 isl_mat *A;
3219 if (!bmap || !context)
3220 return isl_basic_map_free(bmap);
3222 bmap_n_eq = n_div_eq(bmap);
3223 context_n_eq = n_div_eq(context);
3225 if (bmap_n_eq < 0 || context_n_eq < 0)
3226 return isl_basic_map_free(bmap);
3227 if (bmap_n_eq == 0 || context_n_eq == 0)
3228 return bmap;
3230 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3231 context, context_n_eq);
3232 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3234 isl_mat_free(A);
3236 return bmap;
3239 /* Return a basic map that has the same intersection with "context" as "bmap"
3240 * and that is as "simple" as possible.
3242 * The core computation is performed on the pure constraints.
3243 * When we add back the meaning of the integer divisions, we need
3244 * to (re)introduce the div constraints. If we happen to have
3245 * discovered that some of these integer divisions are equal to
3246 * some affine combination of other variables, then these div
3247 * constraints may end up getting simplified in terms of the equalities,
3248 * resulting in extra inequalities on the other variables that
3249 * may have been removed already or that may not even have been
3250 * part of the input. We try and remove those constraints of
3251 * this form that are most obviously redundant with respect to
3252 * the context. We also remove those div constraints that are
3253 * redundant with respect to the other constraints in the result.
3255 * The stride constraints among the equality constraints in "bmap" are
3256 * also simplified with respecting to the other equality constraints
3257 * in "bmap" and with respect to all equality constraints in "context".
3259 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3260 __isl_take isl_basic_map *context)
3262 isl_basic_set *bset, *eq;
3263 isl_basic_map *eq_bmap;
3264 isl_size total, n_div, n_div_bmap;
3265 unsigned extra, n_eq, n_ineq;
3267 if (!bmap || !context)
3268 goto error;
3270 if (isl_basic_map_plain_is_universe(bmap)) {
3271 isl_basic_map_free(context);
3272 return bmap;
3274 if (isl_basic_map_plain_is_empty(context)) {
3275 isl_space *space = isl_basic_map_get_space(bmap);
3276 isl_basic_map_free(bmap);
3277 isl_basic_map_free(context);
3278 return isl_basic_map_universe(space);
3280 if (isl_basic_map_plain_is_empty(bmap)) {
3281 isl_basic_map_free(context);
3282 return bmap;
3285 bmap = isl_basic_map_remove_redundancies(bmap);
3286 context = isl_basic_map_remove_redundancies(context);
3287 bmap = isl_basic_map_order_divs(bmap);
3288 context = isl_basic_map_align_divs(context, bmap);
3290 n_div = isl_basic_map_dim(context, isl_dim_div);
3291 total = isl_basic_map_dim(bmap, isl_dim_all);
3292 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3293 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3294 goto error;
3295 extra = n_div - n_div_bmap;
3297 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3298 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3299 bset = uset_gist(bset,
3300 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3301 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3303 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3304 isl_basic_set_plain_is_empty(bset)) {
3305 isl_basic_map_free(context);
3306 return isl_basic_map_overlying_set(bset, bmap);
3309 n_eq = bset->n_eq;
3310 n_ineq = bset->n_ineq;
3311 eq = isl_basic_set_copy(bset);
3312 eq = isl_basic_set_cow(eq);
3313 eq = isl_basic_set_free_inequality(eq, n_ineq);
3314 bset = isl_basic_set_free_equality(bset, n_eq);
3316 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3317 eq_bmap = gist_strides(eq_bmap, context);
3318 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3319 bmap = isl_basic_map_overlying_set(bset, bmap);
3320 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3321 bmap = isl_basic_map_remove_redundancies(bmap);
3323 return bmap;
3324 error:
3325 isl_basic_map_free(bmap);
3326 isl_basic_map_free(context);
3327 return NULL;
3331 * Assumes context has no implicit divs.
3333 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3334 __isl_take isl_basic_map *context)
3336 int i;
3338 if (!map || !context)
3339 goto error;
3341 if (isl_basic_map_plain_is_empty(context)) {
3342 isl_space *space = isl_map_get_space(map);
3343 isl_map_free(map);
3344 isl_basic_map_free(context);
3345 return isl_map_universe(space);
3348 context = isl_basic_map_remove_redundancies(context);
3349 map = isl_map_cow(map);
3350 if (isl_map_basic_map_check_equal_space(map, context) < 0)
3351 goto error;
3352 map = isl_map_compute_divs(map);
3353 if (!map)
3354 goto error;
3355 for (i = map->n - 1; i >= 0; --i) {
3356 map->p[i] = isl_basic_map_gist(map->p[i],
3357 isl_basic_map_copy(context));
3358 if (!map->p[i])
3359 goto error;
3360 if (isl_basic_map_plain_is_empty(map->p[i])) {
3361 isl_basic_map_free(map->p[i]);
3362 if (i != map->n - 1)
3363 map->p[i] = map->p[map->n - 1];
3364 map->n--;
3367 isl_basic_map_free(context);
3368 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3369 return map;
3370 error:
3371 isl_map_free(map);
3372 isl_basic_map_free(context);
3373 return NULL;
3376 /* Drop all inequalities from "bmap" that also appear in "context".
3377 * "context" is assumed to have only known local variables and
3378 * the initial local variables of "bmap" are assumed to be the same
3379 * as those of "context".
3380 * The constraints of both "bmap" and "context" are assumed
3381 * to have been sorted using isl_basic_map_sort_constraints.
3383 * Run through the inequality constraints of "bmap" and "context"
3384 * in sorted order.
3385 * If a constraint of "bmap" involves variables not in "context",
3386 * then it cannot appear in "context".
3387 * If a matching constraint is found, it is removed from "bmap".
3389 static __isl_give isl_basic_map *drop_inequalities(
3390 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3392 int i1, i2;
3393 isl_size total, bmap_total;
3394 unsigned extra;
3396 total = isl_basic_map_dim(context, isl_dim_all);
3397 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3398 if (total < 0 || bmap_total < 0)
3399 return isl_basic_map_free(bmap);
3401 extra = bmap_total - total;
3403 i1 = bmap->n_ineq - 1;
3404 i2 = context->n_ineq - 1;
3405 while (bmap && i1 >= 0 && i2 >= 0) {
3406 int cmp;
3408 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3409 extra) != -1) {
3410 --i1;
3411 continue;
3413 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3414 context->ineq[i2]);
3415 if (cmp < 0) {
3416 --i2;
3417 continue;
3419 if (cmp > 0) {
3420 --i1;
3421 continue;
3423 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3424 bmap = isl_basic_map_cow(bmap);
3425 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3426 bmap = isl_basic_map_free(bmap);
3428 --i1;
3429 --i2;
3432 return bmap;
3435 /* Drop all equalities from "bmap" that also appear in "context".
3436 * "context" is assumed to have only known local variables and
3437 * the initial local variables of "bmap" are assumed to be the same
3438 * as those of "context".
3440 * Run through the equality constraints of "bmap" and "context"
3441 * in sorted order.
3442 * If a constraint of "bmap" involves variables not in "context",
3443 * then it cannot appear in "context".
3444 * If a matching constraint is found, it is removed from "bmap".
3446 static __isl_give isl_basic_map *drop_equalities(
3447 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3449 int i1, i2;
3450 isl_size total, bmap_total;
3451 unsigned extra;
3453 total = isl_basic_map_dim(context, isl_dim_all);
3454 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3455 if (total < 0 || bmap_total < 0)
3456 return isl_basic_map_free(bmap);
3458 extra = bmap_total - total;
3460 i1 = bmap->n_eq - 1;
3461 i2 = context->n_eq - 1;
3463 while (bmap && i1 >= 0 && i2 >= 0) {
3464 int last1, last2;
3466 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3467 extra) != -1)
3468 break;
3469 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3470 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3471 if (last1 > last2) {
3472 --i2;
3473 continue;
3475 if (last1 < last2) {
3476 --i1;
3477 continue;
3479 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3480 bmap = isl_basic_map_cow(bmap);
3481 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3482 bmap = isl_basic_map_free(bmap);
3484 --i1;
3485 --i2;
3488 return bmap;
3491 /* Remove the constraints in "context" from "bmap".
3492 * "context" is assumed to have explicit representations
3493 * for all local variables.
3495 * First align the divs of "bmap" to those of "context" and
3496 * sort the constraints. Then drop all constraints from "bmap"
3497 * that appear in "context".
3499 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3500 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3502 isl_bool done, known;
3504 done = isl_basic_map_plain_is_universe(context);
3505 if (done == isl_bool_false)
3506 done = isl_basic_map_plain_is_universe(bmap);
3507 if (done == isl_bool_false)
3508 done = isl_basic_map_plain_is_empty(context);
3509 if (done == isl_bool_false)
3510 done = isl_basic_map_plain_is_empty(bmap);
3511 if (done < 0)
3512 goto error;
3513 if (done) {
3514 isl_basic_map_free(context);
3515 return bmap;
3517 known = isl_basic_map_divs_known(context);
3518 if (known < 0)
3519 goto error;
3520 if (!known)
3521 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3522 "context has unknown divs", goto error);
3524 context = isl_basic_map_order_divs(context);
3525 bmap = isl_basic_map_align_divs(bmap, context);
3526 bmap = isl_basic_map_gauss(bmap, NULL);
3527 bmap = isl_basic_map_sort_constraints(bmap);
3528 context = isl_basic_map_sort_constraints(context);
3530 bmap = drop_inequalities(bmap, context);
3531 bmap = drop_equalities(bmap, context);
3533 isl_basic_map_free(context);
3534 bmap = isl_basic_map_finalize(bmap);
3535 return bmap;
3536 error:
3537 isl_basic_map_free(bmap);
3538 isl_basic_map_free(context);
3539 return NULL;
3542 /* Replace "map" by the disjunct at position "pos" and free "context".
3544 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3545 int pos, __isl_take isl_basic_map *context)
3547 isl_basic_map *bmap;
3549 bmap = isl_basic_map_copy(map->p[pos]);
3550 isl_map_free(map);
3551 isl_basic_map_free(context);
3552 return isl_map_from_basic_map(bmap);
3555 /* Remove the constraints in "context" from "map".
3556 * If any of the disjuncts in the result turns out to be the universe,
3557 * then return this universe.
3558 * "context" is assumed to have explicit representations
3559 * for all local variables.
3561 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3562 __isl_take isl_basic_map *context)
3564 int i;
3565 isl_bool univ, known;
3567 univ = isl_basic_map_plain_is_universe(context);
3568 if (univ < 0)
3569 goto error;
3570 if (univ) {
3571 isl_basic_map_free(context);
3572 return map;
3574 known = isl_basic_map_divs_known(context);
3575 if (known < 0)
3576 goto error;
3577 if (!known)
3578 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3579 "context has unknown divs", goto error);
3581 map = isl_map_cow(map);
3582 if (!map)
3583 goto error;
3584 for (i = 0; i < map->n; ++i) {
3585 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3586 isl_basic_map_copy(context));
3587 univ = isl_basic_map_plain_is_universe(map->p[i]);
3588 if (univ < 0)
3589 goto error;
3590 if (univ && map->n > 1)
3591 return replace_by_disjunct(map, i, context);
3594 isl_basic_map_free(context);
3595 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3596 if (map->n > 1)
3597 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3598 return map;
3599 error:
3600 isl_map_free(map);
3601 isl_basic_map_free(context);
3602 return NULL;
3605 /* Remove the constraints in "context" from "set".
3606 * If any of the disjuncts in the result turns out to be the universe,
3607 * then return this universe.
3608 * "context" is assumed to have explicit representations
3609 * for all local variables.
3611 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3612 __isl_take isl_basic_set *context)
3614 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3615 bset_to_bmap(context)));
3618 /* Remove the constraints in "context" from "map".
3619 * If any of the disjuncts in the result turns out to be the universe,
3620 * then return this universe.
3621 * "context" is assumed to consist of a single disjunct and
3622 * to have explicit representations for all local variables.
3624 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3625 __isl_take isl_map *context)
3627 isl_basic_map *hull;
3629 hull = isl_map_unshifted_simple_hull(context);
3630 return isl_map_plain_gist_basic_map(map, hull);
3633 /* Replace "map" by a universe map in the same space and free "drop".
3635 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3636 __isl_take isl_map *drop)
3638 isl_map *res;
3640 res = isl_map_universe(isl_map_get_space(map));
3641 isl_map_free(map);
3642 isl_map_free(drop);
3643 return res;
3646 /* Return a map that has the same intersection with "context" as "map"
3647 * and that is as "simple" as possible.
3649 * If "map" is already the universe, then we cannot make it any simpler.
3650 * Similarly, if "context" is the universe, then we cannot exploit it
3651 * to simplify "map"
3652 * If "map" and "context" are identical to each other, then we can
3653 * return the corresponding universe.
3655 * If either "map" or "context" consists of multiple disjuncts,
3656 * then check if "context" happens to be a subset of "map",
3657 * in which case all constraints can be removed.
3658 * In case of multiple disjuncts, the standard procedure
3659 * may not be able to detect that all constraints can be removed.
3661 * If none of these cases apply, we have to work a bit harder.
3662 * During this computation, we make use of a single disjunct context,
3663 * so if the original context consists of more than one disjunct
3664 * then we need to approximate the context by a single disjunct set.
3665 * Simply taking the simple hull may drop constraints that are
3666 * only implicitly available in each disjunct. We therefore also
3667 * look for constraints among those defining "map" that are valid
3668 * for the context. These can then be used to simplify away
3669 * the corresponding constraints in "map".
3671 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3672 __isl_take isl_map *context)
3674 int equal;
3675 int is_universe;
3676 isl_size n_disjunct_map, n_disjunct_context;
3677 isl_bool subset;
3678 isl_basic_map *hull;
3680 is_universe = isl_map_plain_is_universe(map);
3681 if (is_universe >= 0 && !is_universe)
3682 is_universe = isl_map_plain_is_universe(context);
3683 if (is_universe < 0)
3684 goto error;
3685 if (is_universe) {
3686 isl_map_free(context);
3687 return map;
3690 isl_map_align_params_bin(&map, &context);
3691 equal = isl_map_plain_is_equal(map, context);
3692 if (equal < 0)
3693 goto error;
3694 if (equal)
3695 return replace_by_universe(map, context);
3697 n_disjunct_map = isl_map_n_basic_map(map);
3698 n_disjunct_context = isl_map_n_basic_map(context);
3699 if (n_disjunct_map < 0 || n_disjunct_context < 0)
3700 goto error;
3701 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
3702 subset = isl_map_is_subset(context, map);
3703 if (subset < 0)
3704 goto error;
3705 if (subset)
3706 return replace_by_universe(map, context);
3709 context = isl_map_compute_divs(context);
3710 if (!context)
3711 goto error;
3712 if (n_disjunct_context == 1) {
3713 hull = isl_map_simple_hull(context);
3714 } else {
3715 isl_ctx *ctx;
3716 isl_map_list *list;
3718 ctx = isl_map_get_ctx(map);
3719 list = isl_map_list_alloc(ctx, 2);
3720 list = isl_map_list_add(list, isl_map_copy(context));
3721 list = isl_map_list_add(list, isl_map_copy(map));
3722 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3723 list);
3725 return isl_map_gist_basic_map(map, hull);
3726 error:
3727 isl_map_free(map);
3728 isl_map_free(context);
3729 return NULL;
3732 __isl_give isl_basic_set *isl_basic_set_gist(__isl_take isl_basic_set *bset,
3733 __isl_take isl_basic_set *context)
3735 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3736 bset_to_bmap(context)));
3739 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3740 __isl_take isl_basic_set *context)
3742 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3743 bset_to_bmap(context)));
3746 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3747 __isl_take isl_basic_set *context)
3749 isl_space *space = isl_set_get_space(set);
3750 isl_basic_set *dom_context = isl_basic_set_universe(space);
3751 dom_context = isl_basic_set_intersect_params(dom_context, context);
3752 return isl_set_gist_basic_set(set, dom_context);
3755 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3756 __isl_take isl_set *context)
3758 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3761 /* Compute the gist of "bmap" with respect to the constraints "context"
3762 * on the domain.
3764 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3765 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3767 isl_space *space = isl_basic_map_get_space(bmap);
3768 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3770 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3771 return isl_basic_map_gist(bmap, bmap_context);
3774 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3775 __isl_take isl_set *context)
3777 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3778 map_context = isl_map_intersect_domain(map_context, context);
3779 return isl_map_gist(map, map_context);
3782 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3783 __isl_take isl_set *context)
3785 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3786 map_context = isl_map_intersect_range(map_context, context);
3787 return isl_map_gist(map, map_context);
3790 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3791 __isl_take isl_set *context)
3793 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3794 map_context = isl_map_intersect_params(map_context, context);
3795 return isl_map_gist(map, map_context);
3798 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3799 __isl_take isl_set *context)
3801 return isl_map_gist_params(set, context);
3804 /* Quick check to see if two basic maps are disjoint.
3805 * In particular, we reduce the equalities and inequalities of
3806 * one basic map in the context of the equalities of the other
3807 * basic map and check if we get a contradiction.
3809 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3810 __isl_keep isl_basic_map *bmap2)
3812 struct isl_vec *v = NULL;
3813 int *elim = NULL;
3814 isl_size total;
3815 int i;
3817 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3818 return isl_bool_error;
3819 if (bmap1->n_div || bmap2->n_div)
3820 return isl_bool_false;
3821 if (!bmap1->n_eq && !bmap2->n_eq)
3822 return isl_bool_false;
3824 total = isl_space_dim(bmap1->dim, isl_dim_all);
3825 if (total < 0)
3826 return isl_bool_error;
3827 if (total == 0)
3828 return isl_bool_false;
3829 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3830 if (!v)
3831 goto error;
3832 elim = isl_alloc_array(bmap1->ctx, int, total);
3833 if (!elim)
3834 goto error;
3835 compute_elimination_index(bmap1, elim, total);
3836 for (i = 0; i < bmap2->n_eq; ++i) {
3837 int reduced;
3838 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3839 bmap1, elim, total);
3840 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3841 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3842 goto disjoint;
3844 for (i = 0; i < bmap2->n_ineq; ++i) {
3845 int reduced;
3846 reduced = reduced_using_equalities(v->block.data,
3847 bmap2->ineq[i], bmap1, elim, total);
3848 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3849 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3850 goto disjoint;
3852 compute_elimination_index(bmap2, elim, total);
3853 for (i = 0; i < bmap1->n_ineq; ++i) {
3854 int reduced;
3855 reduced = reduced_using_equalities(v->block.data,
3856 bmap1->ineq[i], bmap2, elim, total);
3857 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3858 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3859 goto disjoint;
3861 isl_vec_free(v);
3862 free(elim);
3863 return isl_bool_false;
3864 disjoint:
3865 isl_vec_free(v);
3866 free(elim);
3867 return isl_bool_true;
3868 error:
3869 isl_vec_free(v);
3870 free(elim);
3871 return isl_bool_error;
3874 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3875 __isl_keep isl_basic_set *bset2)
3877 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3878 bset_to_bmap(bset2));
3881 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3883 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3884 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3885 __isl_keep isl_basic_map *bmap2))
3887 int i, j;
3889 if (!map1 || !map2)
3890 return isl_bool_error;
3892 for (i = 0; i < map1->n; ++i) {
3893 for (j = 0; j < map2->n; ++j) {
3894 isl_bool d = test(map1->p[i], map2->p[j]);
3895 if (d != isl_bool_true)
3896 return d;
3900 return isl_bool_true;
3903 /* Are "map1" and "map2" obviously disjoint, based on information
3904 * that can be derived without looking at the individual basic maps?
3906 * In particular, if one of them is empty or if they live in different spaces
3907 * (ignoring parameters), then they are clearly disjoint.
3909 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3910 __isl_keep isl_map *map2)
3912 isl_bool disjoint;
3913 isl_bool match;
3915 if (!map1 || !map2)
3916 return isl_bool_error;
3918 disjoint = isl_map_plain_is_empty(map1);
3919 if (disjoint < 0 || disjoint)
3920 return disjoint;
3922 disjoint = isl_map_plain_is_empty(map2);
3923 if (disjoint < 0 || disjoint)
3924 return disjoint;
3926 match = isl_map_tuple_is_equal(map1, isl_dim_in, map2, isl_dim_in);
3927 if (match < 0 || !match)
3928 return match < 0 ? isl_bool_error : isl_bool_true;
3930 match = isl_map_tuple_is_equal(map1, isl_dim_out, map2, isl_dim_out);
3931 if (match < 0 || !match)
3932 return match < 0 ? isl_bool_error : isl_bool_true;
3934 return isl_bool_false;
3937 /* Are "map1" and "map2" obviously disjoint?
3939 * If one of them is empty or if they live in different spaces (ignoring
3940 * parameters), then they are clearly disjoint.
3941 * This is checked by isl_map_plain_is_disjoint_global.
3943 * If they have different parameters, then we skip any further tests.
3945 * If they are obviously equal, but not obviously empty, then we will
3946 * not be able to detect if they are disjoint.
3948 * Otherwise we check if each basic map in "map1" is obviously disjoint
3949 * from each basic map in "map2".
3951 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3952 __isl_keep isl_map *map2)
3954 isl_bool disjoint;
3955 isl_bool intersect;
3956 isl_bool match;
3958 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3959 if (disjoint < 0 || disjoint)
3960 return disjoint;
3962 match = isl_map_has_equal_params(map1, map2);
3963 if (match < 0 || !match)
3964 return match < 0 ? isl_bool_error : isl_bool_false;
3966 intersect = isl_map_plain_is_equal(map1, map2);
3967 if (intersect < 0 || intersect)
3968 return intersect < 0 ? isl_bool_error : isl_bool_false;
3970 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3973 /* Are "map1" and "map2" disjoint?
3974 * The parameters are assumed to have been aligned.
3976 * In particular, check whether all pairs of basic maps are disjoint.
3978 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3979 __isl_keep isl_map *map2)
3981 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3984 /* Are "map1" and "map2" disjoint?
3986 * They are disjoint if they are "obviously disjoint" or if one of them
3987 * is empty. Otherwise, they are not disjoint if one of them is universal.
3988 * If the two inputs are (obviously) equal and not empty, then they are
3989 * not disjoint.
3990 * If none of these cases apply, then check if all pairs of basic maps
3991 * are disjoint after aligning the parameters.
3993 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3995 isl_bool disjoint;
3996 isl_bool intersect;
3998 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3999 if (disjoint < 0 || disjoint)
4000 return disjoint;
4002 disjoint = isl_map_is_empty(map1);
4003 if (disjoint < 0 || disjoint)
4004 return disjoint;
4006 disjoint = isl_map_is_empty(map2);
4007 if (disjoint < 0 || disjoint)
4008 return disjoint;
4010 intersect = isl_map_plain_is_universe(map1);
4011 if (intersect < 0 || intersect)
4012 return isl_bool_not(intersect);
4014 intersect = isl_map_plain_is_universe(map2);
4015 if (intersect < 0 || intersect)
4016 return isl_bool_not(intersect);
4018 intersect = isl_map_plain_is_equal(map1, map2);
4019 if (intersect < 0 || intersect)
4020 return isl_bool_not(intersect);
4022 return isl_map_align_params_map_map_and_test(map1, map2,
4023 &isl_map_is_disjoint_aligned);
4026 /* Are "bmap1" and "bmap2" disjoint?
4028 * They are disjoint if they are "obviously disjoint" or if one of them
4029 * is empty. Otherwise, they are not disjoint if one of them is universal.
4030 * If none of these cases apply, we compute the intersection and see if
4031 * the result is empty.
4033 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4034 __isl_keep isl_basic_map *bmap2)
4036 isl_bool disjoint;
4037 isl_bool intersect;
4038 isl_basic_map *test;
4040 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4041 if (disjoint < 0 || disjoint)
4042 return disjoint;
4044 disjoint = isl_basic_map_is_empty(bmap1);
4045 if (disjoint < 0 || disjoint)
4046 return disjoint;
4048 disjoint = isl_basic_map_is_empty(bmap2);
4049 if (disjoint < 0 || disjoint)
4050 return disjoint;
4052 intersect = isl_basic_map_plain_is_universe(bmap1);
4053 if (intersect < 0 || intersect)
4054 return isl_bool_not(intersect);
4056 intersect = isl_basic_map_plain_is_universe(bmap2);
4057 if (intersect < 0 || intersect)
4058 return isl_bool_not(intersect);
4060 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4061 isl_basic_map_copy(bmap2));
4062 disjoint = isl_basic_map_is_empty(test);
4063 isl_basic_map_free(test);
4065 return disjoint;
4068 /* Are "bset1" and "bset2" disjoint?
4070 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4071 __isl_keep isl_basic_set *bset2)
4073 return isl_basic_map_is_disjoint(bset1, bset2);
4076 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4077 __isl_keep isl_set *set2)
4079 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4082 /* Are "set1" and "set2" disjoint?
4084 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4086 return isl_map_is_disjoint(set1, set2);
4089 /* Is "v" equal to 0, 1 or -1?
4091 static int is_zero_or_one(isl_int v)
4093 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4096 /* Are the "n" coefficients starting at "first" of inequality constraints
4097 * "i" and "j" of "bmap" opposite to each other?
4099 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4100 int first, int n)
4102 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4105 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4106 * apart from the constant term?
4108 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4110 isl_size total;
4112 total = isl_basic_map_dim(bmap, isl_dim_all);
4113 if (total < 0)
4114 return isl_bool_error;
4115 return is_opposite_part(bmap, i, j, 1, total);
4118 /* Check if we can combine a given div with lower bound l and upper
4119 * bound u with some other div and if so return that other div.
4120 * Otherwise, return a position beyond the integer divisions.
4121 * Return -1 on error.
4123 * We first check that
4124 * - the bounds are opposites of each other (except for the constant
4125 * term)
4126 * - the bounds do not reference any other div
4127 * - no div is defined in terms of this div
4129 * Let m be the size of the range allowed on the div by the bounds.
4130 * That is, the bounds are of the form
4132 * e <= a <= e + m - 1
4134 * with e some expression in the other variables.
4135 * We look for another div b such that no third div is defined in terms
4136 * of this second div b and such that in any constraint that contains
4137 * a (except for the given lower and upper bound), also contains b
4138 * with a coefficient that is m times that of b.
4139 * That is, all constraints (except for the lower and upper bound)
4140 * are of the form
4142 * e + f (a + m b) >= 0
4144 * Furthermore, in the constraints that only contain b, the coefficient
4145 * of b should be equal to 1 or -1.
4146 * If so, we return b so that "a + m b" can be replaced by
4147 * a single div "c = a + m b".
4149 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
4150 unsigned div, unsigned l, unsigned u)
4152 int i, j;
4153 unsigned n_div;
4154 isl_size v_div;
4155 int coalesce;
4156 isl_bool opp;
4158 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4159 if (n_div <= 1)
4160 return n_div;
4161 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4162 if (v_div < 0)
4163 return -1;
4164 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4165 return n_div;
4166 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4167 n_div - div - 1) != -1)
4168 return n_div;
4169 opp = is_opposite(bmap, l, u);
4170 if (opp < 0 || !opp)
4171 return opp < 0 ? -1 : n_div;
4173 for (i = 0; i < n_div; ++i) {
4174 if (isl_int_is_zero(bmap->div[i][0]))
4175 continue;
4176 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4177 return n_div;
4180 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4181 if (isl_int_is_neg(bmap->ineq[l][0])) {
4182 isl_int_sub(bmap->ineq[l][0],
4183 bmap->ineq[l][0], bmap->ineq[u][0]);
4184 bmap = isl_basic_map_copy(bmap);
4185 bmap = isl_basic_map_set_to_empty(bmap);
4186 isl_basic_map_free(bmap);
4187 return n_div;
4189 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4190 coalesce = n_div;
4191 for (i = 0; i < n_div; ++i) {
4192 if (i == div)
4193 continue;
4194 if (!pairs[i])
4195 continue;
4196 for (j = 0; j < n_div; ++j) {
4197 if (isl_int_is_zero(bmap->div[j][0]))
4198 continue;
4199 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
4200 break;
4202 if (j < n_div)
4203 continue;
4204 for (j = 0; j < bmap->n_ineq; ++j) {
4205 int valid;
4206 if (j == l || j == u)
4207 continue;
4208 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4209 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4210 continue;
4211 break;
4213 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4214 break;
4215 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4216 bmap->ineq[j][1 + v_div + div],
4217 bmap->ineq[l][0]);
4218 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4219 bmap->ineq[j][1 + v_div + i]);
4220 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4221 bmap->ineq[j][1 + v_div + div],
4222 bmap->ineq[l][0]);
4223 if (!valid)
4224 break;
4226 if (j < bmap->n_ineq)
4227 continue;
4228 coalesce = i;
4229 break;
4231 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4232 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4233 return coalesce;
4236 /* Internal data structure used during the construction and/or evaluation of
4237 * an inequality that ensures that a pair of bounds always allows
4238 * for an integer value.
4240 * "tab" is the tableau in which the inequality is evaluated. It may
4241 * be NULL until it is actually needed.
4242 * "v" contains the inequality coefficients.
4243 * "g", "fl" and "fu" are temporary scalars used during the construction and
4244 * evaluation.
4246 struct test_ineq_data {
4247 struct isl_tab *tab;
4248 isl_vec *v;
4249 isl_int g;
4250 isl_int fl;
4251 isl_int fu;
4254 /* Free all the memory allocated by the fields of "data".
4256 static void test_ineq_data_clear(struct test_ineq_data *data)
4258 isl_tab_free(data->tab);
4259 isl_vec_free(data->v);
4260 isl_int_clear(data->g);
4261 isl_int_clear(data->fl);
4262 isl_int_clear(data->fu);
4265 /* Is the inequality stored in data->v satisfied by "bmap"?
4266 * That is, does it only attain non-negative values?
4267 * data->tab is a tableau corresponding to "bmap".
4269 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4270 struct test_ineq_data *data)
4272 isl_ctx *ctx;
4273 enum isl_lp_result res;
4275 ctx = isl_basic_map_get_ctx(bmap);
4276 if (!data->tab)
4277 data->tab = isl_tab_from_basic_map(bmap, 0);
4278 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4279 if (res == isl_lp_error)
4280 return isl_bool_error;
4281 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4284 /* Given a lower and an upper bound on div i, do they always allow
4285 * for an integer value of the given div?
4286 * Determine this property by constructing an inequality
4287 * such that the property is guaranteed when the inequality is nonnegative.
4288 * The lower bound is inequality l, while the upper bound is inequality u.
4289 * The constructed inequality is stored in data->v.
4291 * Let the upper bound be
4293 * -n_u a + e_u >= 0
4295 * and the lower bound
4297 * n_l a + e_l >= 0
4299 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4300 * We have
4302 * - f_u e_l <= f_u f_l g a <= f_l e_u
4304 * Since all variables are integer valued, this is equivalent to
4306 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4308 * If this interval is at least f_u f_l g, then it contains at least
4309 * one integer value for a.
4310 * That is, the test constraint is
4312 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4314 * or
4316 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4318 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4319 * then the constraint can be scaled down by a factor g',
4320 * with the constant term replaced by
4321 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4322 * Note that the result of applying Fourier-Motzkin to this pair
4323 * of constraints is
4325 * f_l e_u + f_u e_l >= 0
4327 * If the constant term of the scaled down version of this constraint,
4328 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4329 * term of the scaled down test constraint, then the test constraint
4330 * is known to hold and no explicit evaluation is required.
4331 * This is essentially the Omega test.
4333 * If the test constraint consists of only a constant term, then
4334 * it is sufficient to look at the sign of this constant term.
4336 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4337 int l, int u, struct test_ineq_data *data)
4339 unsigned offset;
4340 isl_size n_div;
4342 offset = isl_basic_map_offset(bmap, isl_dim_div);
4343 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4344 if (n_div < 0)
4345 return isl_bool_error;
4347 isl_int_gcd(data->g,
4348 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4349 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4350 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4351 isl_int_neg(data->fu, data->fu);
4352 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4353 data->fu, bmap->ineq[l], offset + n_div);
4354 isl_int_mul(data->g, data->g, data->fl);
4355 isl_int_mul(data->g, data->g, data->fu);
4356 isl_int_sub(data->g, data->g, data->fl);
4357 isl_int_sub(data->g, data->g, data->fu);
4358 isl_int_add_ui(data->g, data->g, 1);
4359 isl_int_sub(data->fl, data->v->el[0], data->g);
4361 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4362 if (isl_int_is_zero(data->g))
4363 return isl_int_is_nonneg(data->fl);
4364 if (isl_int_is_one(data->g)) {
4365 isl_int_set(data->v->el[0], data->fl);
4366 return test_ineq_is_satisfied(bmap, data);
4368 isl_int_fdiv_q(data->fl, data->fl, data->g);
4369 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4370 if (isl_int_eq(data->fl, data->v->el[0]))
4371 return isl_bool_true;
4372 isl_int_set(data->v->el[0], data->fl);
4373 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4374 offset - 1 + n_div);
4376 return test_ineq_is_satisfied(bmap, data);
4379 /* Remove more kinds of divs that are not strictly needed.
4380 * In particular, if all pairs of lower and upper bounds on a div
4381 * are such that they allow at least one integer value of the div,
4382 * then we can eliminate the div using Fourier-Motzkin without
4383 * introducing any spurious solutions.
4385 * If at least one of the two constraints has a unit coefficient for the div,
4386 * then the presence of such a value is guaranteed so there is no need to check.
4387 * In particular, the value attained by the bound with unit coefficient
4388 * can serve as this intermediate value.
4390 static __isl_give isl_basic_map *drop_more_redundant_divs(
4391 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4393 isl_ctx *ctx;
4394 struct test_ineq_data data = { NULL, NULL };
4395 unsigned off;
4396 isl_size n_div;
4397 int remove = -1;
4399 isl_int_init(data.g);
4400 isl_int_init(data.fl);
4401 isl_int_init(data.fu);
4403 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4404 if (n_div < 0)
4405 goto error;
4407 ctx = isl_basic_map_get_ctx(bmap);
4408 off = isl_basic_map_offset(bmap, isl_dim_div);
4409 data.v = isl_vec_alloc(ctx, off + n_div);
4410 if (!data.v)
4411 goto error;
4413 while (n > 0) {
4414 int i, l, u;
4415 int best = -1;
4416 isl_bool has_int;
4418 for (i = 0; i < n_div; ++i) {
4419 if (!pairs[i])
4420 continue;
4421 if (best >= 0 && pairs[best] <= pairs[i])
4422 continue;
4423 best = i;
4426 i = best;
4427 for (l = 0; l < bmap->n_ineq; ++l) {
4428 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4429 continue;
4430 if (isl_int_is_one(bmap->ineq[l][off + i]))
4431 continue;
4432 for (u = 0; u < bmap->n_ineq; ++u) {
4433 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4434 continue;
4435 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4436 continue;
4437 has_int = int_between_bounds(bmap, i, l, u,
4438 &data);
4439 if (has_int < 0)
4440 goto error;
4441 if (data.tab && data.tab->empty)
4442 break;
4443 if (!has_int)
4444 break;
4446 if (u < bmap->n_ineq)
4447 break;
4449 if (data.tab && data.tab->empty) {
4450 bmap = isl_basic_map_set_to_empty(bmap);
4451 break;
4453 if (l == bmap->n_ineq) {
4454 remove = i;
4455 break;
4457 pairs[i] = 0;
4458 --n;
4461 test_ineq_data_clear(&data);
4463 free(pairs);
4465 if (remove < 0)
4466 return bmap;
4468 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4469 return isl_basic_map_drop_redundant_divs(bmap);
4470 error:
4471 free(pairs);
4472 isl_basic_map_free(bmap);
4473 test_ineq_data_clear(&data);
4474 return NULL;
4477 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4478 * and the upper bound u, div1 always occurs together with div2 in the form
4479 * (div1 + m div2), where m is the constant range on the variable div1
4480 * allowed by l and u, replace the pair div1 and div2 by a single
4481 * div that is equal to div1 + m div2.
4483 * The new div will appear in the location that contains div2.
4484 * We need to modify all constraints that contain
4485 * div2 = (div - div1) / m
4486 * The coefficient of div2 is known to be equal to 1 or -1.
4487 * (If a constraint does not contain div2, it will also not contain div1.)
4488 * If the constraint also contains div1, then we know they appear
4489 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4490 * i.e., the coefficient of div is f.
4492 * Otherwise, we first need to introduce div1 into the constraint.
4493 * Let l be
4495 * div1 + f >=0
4497 * and u
4499 * -div1 + f' >= 0
4501 * A lower bound on div2
4503 * div2 + t >= 0
4505 * can be replaced by
4507 * m div2 + div1 + m t + f >= 0
4509 * An upper bound
4511 * -div2 + t >= 0
4513 * can be replaced by
4515 * -(m div2 + div1) + m t + f' >= 0
4517 * These constraint are those that we would obtain from eliminating
4518 * div1 using Fourier-Motzkin.
4520 * After all constraints have been modified, we drop the lower and upper
4521 * bound and then drop div1.
4522 * Since the new div is only placed in the same location that used
4523 * to store div2, but otherwise has a different meaning, any possible
4524 * explicit representation of the original div2 is removed.
4526 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4527 unsigned div1, unsigned div2, unsigned l, unsigned u)
4529 isl_ctx *ctx;
4530 isl_int m;
4531 isl_size v_div;
4532 unsigned total;
4533 int i;
4535 ctx = isl_basic_map_get_ctx(bmap);
4537 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4538 if (v_div < 0)
4539 return isl_basic_map_free(bmap);
4540 total = 1 + v_div + bmap->n_div;
4542 isl_int_init(m);
4543 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4544 isl_int_add_ui(m, m, 1);
4546 for (i = 0; i < bmap->n_ineq; ++i) {
4547 if (i == l || i == u)
4548 continue;
4549 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4550 continue;
4551 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4552 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4553 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4554 ctx->one, bmap->ineq[l], total);
4555 else
4556 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4557 ctx->one, bmap->ineq[u], total);
4559 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4560 bmap->ineq[i][1 + v_div + div1]);
4561 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4564 isl_int_clear(m);
4565 if (l > u) {
4566 isl_basic_map_drop_inequality(bmap, l);
4567 isl_basic_map_drop_inequality(bmap, u);
4568 } else {
4569 isl_basic_map_drop_inequality(bmap, u);
4570 isl_basic_map_drop_inequality(bmap, l);
4572 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4573 bmap = isl_basic_map_drop_div(bmap, div1);
4574 return bmap;
4577 /* First check if we can coalesce any pair of divs and
4578 * then continue with dropping more redundant divs.
4580 * We loop over all pairs of lower and upper bounds on a div
4581 * with coefficient 1 and -1, respectively, check if there
4582 * is any other div "c" with which we can coalesce the div
4583 * and if so, perform the coalescing.
4585 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4586 __isl_take isl_basic_map *bmap, int *pairs, int n)
4588 int i, l, u;
4589 isl_size v_div;
4590 isl_size n_div;
4592 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4593 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4594 if (v_div < 0 || n_div < 0)
4595 return isl_basic_map_free(bmap);
4597 for (i = 0; i < n_div; ++i) {
4598 if (!pairs[i])
4599 continue;
4600 for (l = 0; l < bmap->n_ineq; ++l) {
4601 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4602 continue;
4603 for (u = 0; u < bmap->n_ineq; ++u) {
4604 int c;
4606 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4607 continue;
4608 c = div_find_coalesce(bmap, pairs, i, l, u);
4609 if (c < 0)
4610 goto error;
4611 if (c >= n_div)
4612 continue;
4613 free(pairs);
4614 bmap = coalesce_divs(bmap, i, c, l, u);
4615 return isl_basic_map_drop_redundant_divs(bmap);
4620 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4621 free(pairs);
4622 return bmap;
4625 return drop_more_redundant_divs(bmap, pairs, n);
4626 error:
4627 free(pairs);
4628 isl_basic_map_free(bmap);
4629 return NULL;
4632 /* Are the "n" coefficients starting at "first" of inequality constraints
4633 * "i" and "j" of "bmap" equal to each other?
4635 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4636 int first, int n)
4638 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4641 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4642 * apart from the constant term and the coefficient at position "pos"?
4644 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4645 int pos)
4647 isl_size total;
4649 total = isl_basic_map_dim(bmap, isl_dim_all);
4650 if (total < 0)
4651 return isl_bool_error;
4652 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4653 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4656 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4657 * apart from the constant term and the coefficient at position "pos"?
4659 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4660 int pos)
4662 isl_size total;
4664 total = isl_basic_map_dim(bmap, isl_dim_all);
4665 if (total < 0)
4666 return isl_bool_error;
4667 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4668 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4671 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4672 * been modified, simplying it if "simplify" is set.
4673 * Free the temporary data structure "pairs" that was associated
4674 * to the old version of "bmap".
4676 static __isl_give isl_basic_map *drop_redundant_divs_again(
4677 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4679 if (simplify)
4680 bmap = isl_basic_map_simplify(bmap);
4681 free(pairs);
4682 return isl_basic_map_drop_redundant_divs(bmap);
4685 /* Is "div" the single unknown existentially quantified variable
4686 * in inequality constraint "ineq" of "bmap"?
4687 * "div" is known to have a non-zero coefficient in "ineq".
4689 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4690 int div)
4692 int i;
4693 isl_size n_div;
4694 unsigned o_div;
4695 isl_bool known;
4697 known = isl_basic_map_div_is_known(bmap, div);
4698 if (known < 0 || known)
4699 return isl_bool_not(known);
4700 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4701 if (n_div < 0)
4702 return isl_bool_error;
4703 if (n_div == 1)
4704 return isl_bool_true;
4705 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4706 for (i = 0; i < n_div; ++i) {
4707 isl_bool known;
4709 if (i == div)
4710 continue;
4711 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4712 continue;
4713 known = isl_basic_map_div_is_known(bmap, i);
4714 if (known < 0 || !known)
4715 return known;
4718 return isl_bool_true;
4721 /* Does integer division "div" have coefficient 1 in inequality constraint
4722 * "ineq" of "map"?
4724 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4726 unsigned o_div;
4728 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4729 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4730 return isl_bool_true;
4732 return isl_bool_false;
4735 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4736 * then try and drop redundant divs again,
4737 * freeing the temporary data structure "pairs" that was associated
4738 * to the old version of "bmap".
4740 static __isl_give isl_basic_map *set_eq_and_try_again(
4741 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4743 bmap = isl_basic_map_cow(bmap);
4744 isl_basic_map_inequality_to_equality(bmap, ineq);
4745 return drop_redundant_divs_again(bmap, pairs, 1);
4748 /* Drop the integer division at position "div", along with the two
4749 * inequality constraints "ineq1" and "ineq2" in which it appears
4750 * from "bmap" and then try and drop redundant divs again,
4751 * freeing the temporary data structure "pairs" that was associated
4752 * to the old version of "bmap".
4754 static __isl_give isl_basic_map *drop_div_and_try_again(
4755 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4756 __isl_take int *pairs)
4758 if (ineq1 > ineq2) {
4759 isl_basic_map_drop_inequality(bmap, ineq1);
4760 isl_basic_map_drop_inequality(bmap, ineq2);
4761 } else {
4762 isl_basic_map_drop_inequality(bmap, ineq2);
4763 isl_basic_map_drop_inequality(bmap, ineq1);
4765 bmap = isl_basic_map_drop_div(bmap, div);
4766 return drop_redundant_divs_again(bmap, pairs, 0);
4769 /* Given two inequality constraints
4771 * f(x) + n d + c >= 0, (ineq)
4773 * with d the variable at position "pos", and
4775 * f(x) + c0 >= 0, (lower)
4777 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4778 * determined by the first constraint.
4779 * That is, store
4781 * ceil((c0 - c)/n)
4783 * in *l.
4785 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4786 int ineq, int lower, int pos, isl_int *l)
4788 isl_int_neg(*l, bmap->ineq[ineq][0]);
4789 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4790 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4793 /* Given two inequality constraints
4795 * f(x) + n d + c >= 0, (ineq)
4797 * with d the variable at position "pos", and
4799 * -f(x) - c0 >= 0, (upper)
4801 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4802 * determined by the first constraint.
4803 * That is, store
4805 * ceil((-c1 - c)/n)
4807 * in *u.
4809 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4810 int ineq, int upper, int pos, isl_int *u)
4812 isl_int_neg(*u, bmap->ineq[ineq][0]);
4813 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4814 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4817 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4818 * does the corresponding lower bound have a fixed value in "bmap"?
4820 * In particular, "ineq" is of the form
4822 * f(x) + n d + c >= 0
4824 * with n > 0, c the constant term and
4825 * d the existentially quantified variable "div".
4826 * That is, the lower bound is
4828 * ceil((-f(x) - c)/n)
4830 * Look for a pair of constraints
4832 * f(x) + c0 >= 0
4833 * -f(x) + c1 >= 0
4835 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4836 * That is, check that
4838 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4840 * If so, return the index of inequality f(x) + c0 >= 0.
4841 * Otherwise, return bmap->n_ineq.
4842 * Return -1 on error.
4844 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4846 int i;
4847 int lower = -1, upper = -1;
4848 unsigned o_div;
4849 isl_int l, u;
4850 int equal;
4852 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4853 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4854 isl_bool par, opp;
4856 if (i == ineq)
4857 continue;
4858 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4859 continue;
4860 par = isl_bool_false;
4861 if (lower < 0)
4862 par = is_parallel_except(bmap, ineq, i, o_div + div);
4863 if (par < 0)
4864 return -1;
4865 if (par) {
4866 lower = i;
4867 continue;
4869 opp = isl_bool_false;
4870 if (upper < 0)
4871 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4872 if (opp < 0)
4873 return -1;
4874 if (opp)
4875 upper = i;
4878 if (lower < 0 || upper < 0)
4879 return bmap->n_ineq;
4881 isl_int_init(l);
4882 isl_int_init(u);
4884 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4885 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4887 equal = isl_int_eq(l, u);
4889 isl_int_clear(l);
4890 isl_int_clear(u);
4892 return equal ? lower : bmap->n_ineq;
4895 /* Given a lower bound constraint "ineq" on the existentially quantified
4896 * variable "div", such that the corresponding lower bound has
4897 * a fixed value in "bmap", assign this fixed value to the variable and
4898 * then try and drop redundant divs again,
4899 * freeing the temporary data structure "pairs" that was associated
4900 * to the old version of "bmap".
4901 * "lower" determines the constant value for the lower bound.
4903 * In particular, "ineq" is of the form
4905 * f(x) + n d + c >= 0,
4907 * while "lower" is of the form
4909 * f(x) + c0 >= 0
4911 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4912 * is ceil((c0 - c)/n).
4914 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4915 int div, int ineq, int lower, int *pairs)
4917 isl_int c;
4918 unsigned o_div;
4920 isl_int_init(c);
4922 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4923 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4924 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4925 free(pairs);
4927 isl_int_clear(c);
4929 return isl_basic_map_drop_redundant_divs(bmap);
4932 /* Do any of the integer divisions of "bmap" involve integer division "div"?
4934 * The integer division "div" could only ever appear in any later
4935 * integer division (with an explicit representation).
4937 static isl_bool any_div_involves_div(__isl_keep isl_basic_map *bmap, int div)
4939 int i;
4940 isl_size v_div, n_div;
4942 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4943 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4944 if (v_div < 0 || n_div < 0)
4945 return isl_bool_error;
4947 for (i = div + 1; i < n_div; ++i) {
4948 isl_bool unknown;
4950 unknown = isl_basic_map_div_is_marked_unknown(bmap, i);
4951 if (unknown < 0)
4952 return isl_bool_error;
4953 if (unknown)
4954 continue;
4955 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4956 return isl_bool_true;
4959 return isl_bool_false;
4962 /* Remove divs that are not strictly needed based on the inequality
4963 * constraints.
4964 * In particular, if a div only occurs positively (or negatively)
4965 * in constraints, then it can simply be dropped.
4966 * Also, if a div occurs in only two constraints and if moreover
4967 * those two constraints are opposite to each other, except for the constant
4968 * term and if the sum of the constant terms is such that for any value
4969 * of the other values, there is always at least one integer value of the
4970 * div, i.e., if one plus this sum is greater than or equal to
4971 * the (absolute value) of the coefficient of the div in the constraints,
4972 * then we can also simply drop the div.
4974 * If an existentially quantified variable does not have an explicit
4975 * representation, appears in only a single lower bound that does not
4976 * involve any other such existentially quantified variables and appears
4977 * in this lower bound with coefficient 1,
4978 * then fix the variable to the value of the lower bound. That is,
4979 * turn the inequality into an equality.
4980 * If for any value of the other variables, there is any value
4981 * for the existentially quantified variable satisfying the constraints,
4982 * then this lower bound also satisfies the constraints.
4983 * It is therefore safe to pick this lower bound.
4985 * The same reasoning holds even if the coefficient is not one.
4986 * However, fixing the variable to the value of the lower bound may
4987 * in general introduce an extra integer division, in which case
4988 * it may be better to pick another value.
4989 * If this integer division has a known constant value, then plugging
4990 * in this constant value removes the existentially quantified variable
4991 * completely. In particular, if the lower bound is of the form
4992 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4993 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4994 * then the existentially quantified variable can be assigned this
4995 * shared value.
4997 * We skip divs that appear in equalities or in the definition of other divs.
4998 * Divs that appear in the definition of other divs usually occur in at least
4999 * 4 constraints, but the constraints may have been simplified.
5001 * If any divs are left after these simple checks then we move on
5002 * to more complicated cases in drop_more_redundant_divs.
5004 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
5005 __isl_take isl_basic_map *bmap)
5007 int i, j;
5008 isl_size off;
5009 int *pairs = NULL;
5010 int n = 0;
5011 isl_size n_ineq;
5013 if (!bmap)
5014 goto error;
5015 if (bmap->n_div == 0)
5016 return bmap;
5018 off = isl_basic_map_var_offset(bmap, isl_dim_div);
5019 if (off < 0)
5020 return isl_basic_map_free(bmap);
5021 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
5022 if (!pairs)
5023 goto error;
5025 n_ineq = isl_basic_map_n_inequality(bmap);
5026 if (n_ineq < 0)
5027 goto error;
5028 for (i = 0; i < bmap->n_div; ++i) {
5029 int pos, neg;
5030 int last_pos, last_neg;
5031 int redundant;
5032 int defined;
5033 isl_bool involves, opp, set_div;
5035 defined = !isl_int_is_zero(bmap->div[i][0]);
5036 involves = any_div_involves_div(bmap, i);
5037 if (involves < 0)
5038 goto error;
5039 if (involves)
5040 continue;
5041 for (j = 0; j < bmap->n_eq; ++j)
5042 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5043 break;
5044 if (j < bmap->n_eq)
5045 continue;
5046 ++n;
5047 pos = neg = 0;
5048 for (j = 0; j < bmap->n_ineq; ++j) {
5049 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5050 last_pos = j;
5051 ++pos;
5053 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5054 last_neg = j;
5055 ++neg;
5058 pairs[i] = pos * neg;
5059 if (pairs[i] == 0) {
5060 for (j = bmap->n_ineq - 1; j >= 0; --j)
5061 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5062 isl_basic_map_drop_inequality(bmap, j);
5063 bmap = isl_basic_map_drop_div(bmap, i);
5064 return drop_redundant_divs_again(bmap, pairs, 0);
5066 if (pairs[i] != 1)
5067 opp = isl_bool_false;
5068 else
5069 opp = is_opposite(bmap, last_pos, last_neg);
5070 if (opp < 0)
5071 goto error;
5072 if (!opp) {
5073 int lower;
5074 isl_bool single, one;
5076 if (pos != 1)
5077 continue;
5078 single = single_unknown(bmap, last_pos, i);
5079 if (single < 0)
5080 goto error;
5081 if (!single)
5082 continue;
5083 one = has_coef_one(bmap, i, last_pos);
5084 if (one < 0)
5085 goto error;
5086 if (one)
5087 return set_eq_and_try_again(bmap, last_pos,
5088 pairs);
5089 lower = lower_bound_is_cst(bmap, i, last_pos);
5090 if (lower < 0)
5091 goto error;
5092 if (lower < n_ineq)
5093 return fix_cst_lower(bmap, i, last_pos, lower,
5094 pairs);
5095 continue;
5098 isl_int_add(bmap->ineq[last_pos][0],
5099 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5100 isl_int_add_ui(bmap->ineq[last_pos][0],
5101 bmap->ineq[last_pos][0], 1);
5102 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5103 bmap->ineq[last_pos][1+off+i]);
5104 isl_int_sub_ui(bmap->ineq[last_pos][0],
5105 bmap->ineq[last_pos][0], 1);
5106 isl_int_sub(bmap->ineq[last_pos][0],
5107 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5108 if (redundant)
5109 return drop_div_and_try_again(bmap, i,
5110 last_pos, last_neg, pairs);
5111 if (defined)
5112 set_div = isl_bool_false;
5113 else
5114 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
5115 if (set_div < 0)
5116 return isl_basic_map_free(bmap);
5117 if (set_div) {
5118 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5119 return drop_redundant_divs_again(bmap, pairs, 1);
5121 pairs[i] = 0;
5122 --n;
5125 if (n > 0)
5126 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5128 free(pairs);
5129 return bmap;
5130 error:
5131 free(pairs);
5132 isl_basic_map_free(bmap);
5133 return NULL;
5136 /* Consider the coefficients at "c" as a row vector and replace
5137 * them with their product with "T". "T" is assumed to be a square matrix.
5139 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5141 isl_size n;
5142 isl_ctx *ctx;
5143 isl_vec *v;
5145 n = isl_mat_rows(T);
5146 if (n < 0)
5147 return isl_stat_error;
5148 if (isl_seq_first_non_zero(c, n) == -1)
5149 return isl_stat_ok;
5150 ctx = isl_mat_get_ctx(T);
5151 v = isl_vec_alloc(ctx, n);
5152 if (!v)
5153 return isl_stat_error;
5154 isl_seq_swp_or_cpy(v->el, c, n);
5155 v = isl_vec_mat_product(v, isl_mat_copy(T));
5156 if (!v)
5157 return isl_stat_error;
5158 isl_seq_swp_or_cpy(c, v->el, n);
5159 isl_vec_free(v);
5161 return isl_stat_ok;
5164 /* Plug in T for the variables in "bmap" starting at "pos".
5165 * T is a linear unimodular matrix, i.e., without constant term.
5167 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5168 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5170 int i;
5171 isl_size n_row, n_col;
5173 bmap = isl_basic_map_cow(bmap);
5174 n_row = isl_mat_rows(T);
5175 n_col = isl_mat_cols(T);
5176 if (!bmap || n_row < 0 || n_col < 0)
5177 goto error;
5179 if (n_col != n_row)
5180 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5181 "expecting square matrix", goto error);
5183 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
5184 goto error;
5186 for (i = 0; i < bmap->n_eq; ++i)
5187 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5188 goto error;
5189 for (i = 0; i < bmap->n_ineq; ++i)
5190 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5191 goto error;
5192 for (i = 0; i < bmap->n_div; ++i) {
5193 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5194 continue;
5195 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5196 goto error;
5199 isl_mat_free(T);
5200 return bmap;
5201 error:
5202 isl_basic_map_free(bmap);
5203 isl_mat_free(T);
5204 return NULL;
5207 /* Remove divs that are not strictly needed.
5209 * First look for an equality constraint involving two or more
5210 * existentially quantified variables without an explicit
5211 * representation. Replace the combination that appears
5212 * in the equality constraint by a single existentially quantified
5213 * variable such that the equality can be used to derive
5214 * an explicit representation for the variable.
5215 * If there are no more such equality constraints, then continue
5216 * with isl_basic_map_drop_redundant_divs_ineq.
5218 * In particular, if the equality constraint is of the form
5220 * f(x) + \sum_i c_i a_i = 0
5222 * with a_i existentially quantified variable without explicit
5223 * representation, then apply a transformation on the existentially
5224 * quantified variables to turn the constraint into
5226 * f(x) + g a_1' = 0
5228 * with g the gcd of the c_i.
5229 * In order to easily identify which existentially quantified variables
5230 * have a complete explicit representation, i.e., without being defined
5231 * in terms of other existentially quantified variables without
5232 * an explicit representation, the existentially quantified variables
5233 * are first sorted.
5235 * The variable transformation is computed by extending the row
5236 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5238 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5239 * [a_2'] [ a_2 ]
5240 * ... = U ....
5241 * [a_n'] [ a_n ]
5243 * with [c_1/g ... c_n/g] representing the first row of U.
5244 * The inverse of U is then plugged into the original constraints.
5245 * The call to isl_basic_map_simplify makes sure the explicit
5246 * representation for a_1' is extracted from the equality constraint.
5248 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5249 __isl_take isl_basic_map *bmap)
5251 int first;
5252 int i;
5253 unsigned o_div;
5254 isl_size n_div;
5255 int l;
5256 isl_ctx *ctx;
5257 isl_mat *T;
5259 if (!bmap)
5260 return NULL;
5261 if (isl_basic_map_divs_known(bmap))
5262 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5263 if (bmap->n_eq == 0)
5264 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5265 bmap = isl_basic_map_sort_divs(bmap);
5266 if (!bmap)
5267 return NULL;
5269 first = isl_basic_map_first_unknown_div(bmap);
5270 if (first < 0)
5271 return isl_basic_map_free(bmap);
5273 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5274 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5275 if (n_div < 0)
5276 return isl_basic_map_free(bmap);
5278 for (i = 0; i < bmap->n_eq; ++i) {
5279 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5280 n_div - (first));
5281 if (l < 0)
5282 continue;
5283 l += first;
5284 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5285 n_div - (l + 1)) == -1)
5286 continue;
5287 break;
5289 if (i >= bmap->n_eq)
5290 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5292 ctx = isl_basic_map_get_ctx(bmap);
5293 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5294 if (!T)
5295 return isl_basic_map_free(bmap);
5296 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5297 T = isl_mat_normalize_row(T, 0);
5298 T = isl_mat_unimodular_complete(T, 1);
5299 T = isl_mat_right_inverse(T);
5301 for (i = l; i < n_div; ++i)
5302 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5303 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5304 bmap = isl_basic_map_simplify(bmap);
5306 return isl_basic_map_drop_redundant_divs(bmap);
5309 /* Does "bmap" satisfy any equality that involves more than 2 variables
5310 * and/or has coefficients different from -1 and 1?
5312 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5314 int i;
5315 isl_size total;
5317 total = isl_basic_map_dim(bmap, isl_dim_all);
5318 if (total < 0)
5319 return isl_bool_error;
5321 for (i = 0; i < bmap->n_eq; ++i) {
5322 int j, k;
5324 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5325 if (j < 0)
5326 continue;
5327 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5328 !isl_int_is_negone(bmap->eq[i][1 + j]))
5329 return isl_bool_true;
5331 j += 1;
5332 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5333 if (k < 0)
5334 continue;
5335 j += k;
5336 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5337 !isl_int_is_negone(bmap->eq[i][1 + j]))
5338 return isl_bool_true;
5340 j += 1;
5341 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5342 if (k >= 0)
5343 return isl_bool_true;
5346 return isl_bool_false;
5349 /* Remove any common factor g from the constraint coefficients in "v".
5350 * The constant term is stored in the first position and is replaced
5351 * by floor(c/g). If any common factor is removed and if this results
5352 * in a tightening of the constraint, then set *tightened.
5354 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5355 int *tightened)
5357 isl_ctx *ctx;
5359 if (!v)
5360 return NULL;
5361 ctx = isl_vec_get_ctx(v);
5362 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5363 if (isl_int_is_zero(ctx->normalize_gcd))
5364 return v;
5365 if (isl_int_is_one(ctx->normalize_gcd))
5366 return v;
5367 v = isl_vec_cow(v);
5368 if (!v)
5369 return NULL;
5370 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5371 *tightened = 1;
5372 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5373 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5374 v->size - 1);
5375 return v;
5378 /* Internal representation used by isl_basic_map_reduce_coefficients.
5380 * "total" is the total dimensionality of the original basic map.
5381 * "v" is a temporary vector of size 1 + total that can be used
5382 * to store constraint coefficients.
5383 * "T" is the variable compression.
5384 * "T2" is the inverse transformation.
5385 * "tightened" is set if any constant term got tightened
5386 * while reducing the coefficients.
5388 struct isl_reduce_coefficients_data {
5389 isl_size total;
5390 isl_vec *v;
5391 isl_mat *T;
5392 isl_mat *T2;
5393 int tightened;
5396 /* Free all memory allocated in "data".
5398 static void isl_reduce_coefficients_data_clear(
5399 struct isl_reduce_coefficients_data *data)
5401 data->T = isl_mat_free(data->T);
5402 data->T2 = isl_mat_free(data->T2);
5403 data->v = isl_vec_free(data->v);
5406 /* Initialize "data" for "bmap", freeing all allocated memory
5407 * if anything goes wrong.
5409 * In particular, construct a variable compression
5410 * from the equality constraints of "bmap" and
5411 * allocate a temporary vector.
5413 static isl_stat isl_reduce_coefficients_data_init(
5414 __isl_keep isl_basic_map *bmap,
5415 struct isl_reduce_coefficients_data *data)
5417 isl_ctx *ctx;
5418 isl_mat *eq;
5420 data->v = NULL;
5421 data->T = NULL;
5422 data->T2 = NULL;
5423 data->tightened = 0;
5425 data->total = isl_basic_map_dim(bmap, isl_dim_all);
5426 if (data->total < 0)
5427 return isl_stat_error;
5428 ctx = isl_basic_map_get_ctx(bmap);
5429 data->v = isl_vec_alloc(ctx, 1 + data->total);
5430 if (!data->v)
5431 return isl_stat_error;
5433 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq,
5434 0, 1 + data->total);
5435 data->T = isl_mat_variable_compression(eq, &data->T2);
5436 if (!data->T || !data->T2)
5437 goto error;
5439 return isl_stat_ok;
5440 error:
5441 isl_reduce_coefficients_data_clear(data);
5442 return isl_stat_error;
5445 /* Reduce the coefficients of "bmap" by applying the variable compression
5446 * in "data".
5447 * In particular, apply the variable compression to each constraint,
5448 * factor out any common factor in the non-constant coefficients and
5449 * then apply the inverse of the compression.
5451 * Only apply the reduction on a single copy of the basic map
5452 * since the reduction may leave the result in an inconsistent state.
5453 * In particular, the constraints may not be gaussed.
5455 static __isl_give isl_basic_map *reduce_coefficients(
5456 __isl_take isl_basic_map *bmap,
5457 struct isl_reduce_coefficients_data *data)
5459 int i;
5460 isl_size total;
5462 total = isl_basic_map_dim(bmap, isl_dim_all);
5463 if (total < 0)
5464 return isl_basic_map_free(bmap);
5465 if (total != data->total)
5466 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
5467 "total dimensionality changed unexpectedly",
5468 return isl_basic_map_free(bmap));
5470 bmap = isl_basic_map_cow(bmap);
5471 if (!bmap)
5472 return NULL;
5474 for (i = 0; i < bmap->n_ineq; ++i) {
5475 isl_seq_cpy(data->v->el, bmap->ineq[i], 1 + data->total);
5476 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T));
5477 data->v = normalize_constraint(data->v, &data->tightened);
5478 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T2));
5479 if (!data->v)
5480 return isl_basic_map_free(bmap);
5481 isl_seq_cpy(bmap->ineq[i], data->v->el, 1 + data->total);
5484 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5486 return bmap;
5489 /* If "bmap" is an integer set that satisfies any equality involving
5490 * more than 2 variables and/or has coefficients different from -1 and 1,
5491 * then use variable compression to reduce the coefficients by removing
5492 * any (hidden) common factor.
5493 * In particular, apply the variable compression to each constraint,
5494 * factor out any common factor in the non-constant coefficients and
5495 * then apply the inverse of the compression.
5496 * At the end, we mark the basic map as having reduced constants.
5497 * If this flag is still set on the next invocation of this function,
5498 * then we skip the computation.
5500 * Removing a common factor may result in a tightening of some of
5501 * the constraints. If this happens, then we may end up with two
5502 * opposite inequalities that can be replaced by an equality.
5503 * We therefore call isl_basic_map_detect_inequality_pairs,
5504 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5505 * and isl_basic_map_gauss if such a pair was found.
5507 * Tightening may also result in some other constraints becoming
5508 * (rationally) redundant with respect to the tightened constraint
5509 * (in combination with other constraints). The basic map may
5510 * therefore no longer be assumed to have no redundant constraints.
5512 * Note that this function may leave the result in an inconsistent state.
5513 * In particular, the constraints may not be gaussed.
5514 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5515 * for some of the test cases to pass successfully.
5516 * Any potential modification of the representation is therefore only
5517 * performed on a single copy of the basic map.
5519 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5520 __isl_take isl_basic_map *bmap)
5522 struct isl_reduce_coefficients_data data;
5523 isl_bool multi;
5525 if (!bmap)
5526 return NULL;
5527 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5528 return bmap;
5529 if (isl_basic_map_is_rational(bmap))
5530 return bmap;
5531 if (bmap->n_eq == 0)
5532 return bmap;
5533 multi = has_multiple_var_equality(bmap);
5534 if (multi < 0)
5535 return isl_basic_map_free(bmap);
5536 if (!multi)
5537 return bmap;
5539 if (isl_reduce_coefficients_data_init(bmap, &data) < 0)
5540 return isl_basic_map_free(bmap);
5542 if (data.T->n_col == 0) {
5543 isl_reduce_coefficients_data_clear(&data);
5544 return isl_basic_map_set_to_empty(bmap);
5547 bmap = reduce_coefficients(bmap, &data);
5548 if (!bmap)
5549 goto error;
5551 isl_reduce_coefficients_data_clear(&data);
5553 if (data.tightened) {
5554 int progress = 0;
5556 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
5557 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5558 if (progress) {
5559 bmap = isl_basic_map_gauss(bmap, NULL);
5560 bmap = eliminate_divs_eq(bmap, &progress);
5564 return bmap;
5565 error:
5566 isl_reduce_coefficients_data_clear(&data);
5567 return isl_basic_map_free(bmap);
5570 /* Shift the integer division at position "div" of "bmap"
5571 * by "shift" times the variable at position "pos".
5572 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5573 * corresponds to the constant term.
5575 * That is, if the integer division has the form
5577 * floor(f(x)/d)
5579 * then replace it by
5581 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5583 __isl_give isl_basic_map *isl_basic_map_shift_div(
5584 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5586 int i;
5587 isl_size total, n_div;
5589 if (isl_int_is_zero(shift))
5590 return bmap;
5591 total = isl_basic_map_dim(bmap, isl_dim_all);
5592 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5593 total -= n_div;
5594 if (total < 0 || n_div < 0)
5595 return isl_basic_map_free(bmap);
5597 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5599 for (i = 0; i < bmap->n_eq; ++i) {
5600 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5601 continue;
5602 isl_int_submul(bmap->eq[i][pos],
5603 shift, bmap->eq[i][1 + total + div]);
5605 for (i = 0; i < bmap->n_ineq; ++i) {
5606 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5607 continue;
5608 isl_int_submul(bmap->ineq[i][pos],
5609 shift, bmap->ineq[i][1 + total + div]);
5611 for (i = 0; i < bmap->n_div; ++i) {
5612 if (isl_int_is_zero(bmap->div[i][0]))
5613 continue;
5614 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5615 continue;
5616 isl_int_submul(bmap->div[i][1 + pos],
5617 shift, bmap->div[i][1 + 1 + total + div]);
5620 return bmap;