isl_tab_pip.c: extract out shared sol_init
[isl.git] / isl_map_simplify.c
blobc9e1311ac360802287c1995b21908d0648cbae4e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
49 isl_seq_cpy(c, c + n, rem);
50 isl_seq_clr(c + rem, n);
53 /* Drop n dimensions starting at first.
55 * In principle, this frees up some extra variables as the number
56 * of columns remains constant, but we would have to extend
57 * the div array too as the number of rows in this array is assumed
58 * to be equal to extra.
60 struct isl_basic_set *isl_basic_set_drop_dims(
61 struct isl_basic_set *bset, unsigned first, unsigned n)
63 int i;
65 if (!bset)
66 goto error;
68 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
70 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
71 return bset;
73 bset = isl_basic_set_cow(bset);
74 if (!bset)
75 return NULL;
77 for (i = 0; i < bset->n_eq; ++i)
78 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
79 (bset->dim->n_out-first-n)+bset->extra);
81 for (i = 0; i < bset->n_ineq; ++i)
82 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
83 (bset->dim->n_out-first-n)+bset->extra);
85 for (i = 0; i < bset->n_div; ++i)
86 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
87 (bset->dim->n_out-first-n)+bset->extra);
89 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
90 if (!bset->dim)
91 goto error;
93 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
94 bset = isl_basic_set_simplify(bset);
95 return isl_basic_set_finalize(bset);
96 error:
97 isl_basic_set_free(bset);
98 return NULL;
101 struct isl_set *isl_set_drop_dims(
102 struct isl_set *set, unsigned first, unsigned n)
104 int i;
106 if (!set)
107 goto error;
109 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
111 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
112 return set;
113 set = isl_set_cow(set);
114 if (!set)
115 goto error;
116 set->dim = isl_space_drop_outputs(set->dim, first, n);
117 if (!set->dim)
118 goto error;
120 for (i = 0; i < set->n; ++i) {
121 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
122 if (!set->p[i])
123 goto error;
126 ISL_F_CLR(set, ISL_SET_NORMALIZED);
127 return set;
128 error:
129 isl_set_free(set);
130 return NULL;
133 /* Move "n" divs starting at "first" to the end of the list of divs.
135 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
136 unsigned first, unsigned n)
138 isl_int **div;
139 int i;
141 if (first + n == bmap->n_div)
142 return bmap;
144 div = isl_alloc_array(bmap->ctx, isl_int *, n);
145 if (!div)
146 goto error;
147 for (i = 0; i < n; ++i)
148 div[i] = bmap->div[first + i];
149 for (i = 0; i < bmap->n_div - first - n; ++i)
150 bmap->div[first + i] = bmap->div[first + n + i];
151 for (i = 0; i < n; ++i)
152 bmap->div[bmap->n_div - n + i] = div[i];
153 free(div);
154 return bmap;
155 error:
156 isl_basic_map_free(bmap);
157 return NULL;
160 /* Drop "n" dimensions of type "type" starting at "first".
162 * In principle, this frees up some extra variables as the number
163 * of columns remains constant, but we would have to extend
164 * the div array too as the number of rows in this array is assumed
165 * to be equal to extra.
167 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
168 enum isl_dim_type type, unsigned first, unsigned n)
170 int i;
171 unsigned dim;
172 unsigned offset;
173 unsigned left;
175 if (!bmap)
176 goto error;
178 dim = isl_basic_map_dim(bmap, type);
179 isl_assert(bmap->ctx, first + n <= dim, goto error);
181 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
182 return bmap;
184 bmap = isl_basic_map_cow(bmap);
185 if (!bmap)
186 return NULL;
188 offset = isl_basic_map_offset(bmap, type) + first;
189 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
190 for (i = 0; i < bmap->n_eq; ++i)
191 constraint_drop_vars(bmap->eq[i]+offset, n, left);
193 for (i = 0; i < bmap->n_ineq; ++i)
194 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
196 for (i = 0; i < bmap->n_div; ++i)
197 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
199 if (type == isl_dim_div) {
200 bmap = move_divs_last(bmap, first, n);
201 if (!bmap)
202 goto error;
203 isl_basic_map_free_div(bmap, n);
204 } else
205 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
206 if (!bmap->dim)
207 goto error;
209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
210 bmap = isl_basic_map_simplify(bmap);
211 return isl_basic_map_finalize(bmap);
212 error:
213 isl_basic_map_free(bmap);
214 return NULL;
217 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
218 enum isl_dim_type type, unsigned first, unsigned n)
220 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
221 type, first, n));
224 struct isl_basic_map *isl_basic_map_drop_inputs(
225 struct isl_basic_map *bmap, unsigned first, unsigned n)
227 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
230 struct isl_map *isl_map_drop(struct isl_map *map,
231 enum isl_dim_type type, unsigned first, unsigned n)
233 int i;
235 if (!map)
236 goto error;
238 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
240 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
241 return map;
242 map = isl_map_cow(map);
243 if (!map)
244 goto error;
245 map->dim = isl_space_drop_dims(map->dim, type, first, n);
246 if (!map->dim)
247 goto error;
249 for (i = 0; i < map->n; ++i) {
250 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
251 if (!map->p[i])
252 goto error;
254 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
256 return map;
257 error:
258 isl_map_free(map);
259 return NULL;
262 struct isl_set *isl_set_drop(struct isl_set *set,
263 enum isl_dim_type type, unsigned first, unsigned n)
265 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
268 struct isl_map *isl_map_drop_inputs(
269 struct isl_map *map, unsigned first, unsigned n)
271 return isl_map_drop(map, isl_dim_in, first, n);
275 * We don't cow, as the div is assumed to be redundant.
277 __isl_give isl_basic_map *isl_basic_map_drop_div(
278 __isl_take isl_basic_map *bmap, unsigned div)
280 int i;
281 unsigned pos;
283 if (!bmap)
284 goto error;
286 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
288 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
290 for (i = 0; i < bmap->n_eq; ++i)
291 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
293 for (i = 0; i < bmap->n_ineq; ++i) {
294 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
295 isl_basic_map_drop_inequality(bmap, i);
296 --i;
297 continue;
299 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
302 for (i = 0; i < bmap->n_div; ++i)
303 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
305 if (div != bmap->n_div - 1) {
306 int j;
307 isl_int *t = bmap->div[div];
309 for (j = div; j < bmap->n_div - 1; ++j)
310 bmap->div[j] = bmap->div[j+1];
312 bmap->div[bmap->n_div - 1] = t;
314 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
315 isl_basic_map_free_div(bmap, 1);
317 return bmap;
318 error:
319 isl_basic_map_free(bmap);
320 return NULL;
323 struct isl_basic_map *isl_basic_map_normalize_constraints(
324 struct isl_basic_map *bmap)
326 int i;
327 isl_int gcd;
328 unsigned total = isl_basic_map_total_dim(bmap);
330 if (!bmap)
331 return NULL;
333 isl_int_init(gcd);
334 for (i = bmap->n_eq - 1; i >= 0; --i) {
335 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
336 if (isl_int_is_zero(gcd)) {
337 if (!isl_int_is_zero(bmap->eq[i][0])) {
338 bmap = isl_basic_map_set_to_empty(bmap);
339 break;
341 isl_basic_map_drop_equality(bmap, i);
342 continue;
344 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
345 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
346 if (isl_int_is_one(gcd))
347 continue;
348 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
349 bmap = isl_basic_map_set_to_empty(bmap);
350 break;
352 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
355 for (i = bmap->n_ineq - 1; i >= 0; --i) {
356 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
357 if (isl_int_is_zero(gcd)) {
358 if (isl_int_is_neg(bmap->ineq[i][0])) {
359 bmap = isl_basic_map_set_to_empty(bmap);
360 break;
362 isl_basic_map_drop_inequality(bmap, i);
363 continue;
365 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
366 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
367 if (isl_int_is_one(gcd))
368 continue;
369 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
370 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
372 isl_int_clear(gcd);
374 return bmap;
377 struct isl_basic_set *isl_basic_set_normalize_constraints(
378 struct isl_basic_set *bset)
380 isl_basic_map *bmap = bset_to_bmap(bset);
381 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
384 /* Reduce the coefficient of the variable at position "pos"
385 * in integer division "div", such that it lies in the half-open
386 * interval (1/2,1/2], extracting any excess value from this integer division.
387 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
388 * corresponds to the constant term.
390 * That is, the integer division is of the form
392 * floor((... + (c * d + r) * x_pos + ...)/d)
394 * with -d < 2 * r <= d.
395 * Replace it by
397 * floor((... + r * x_pos + ...)/d) + c * x_pos
399 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
400 * Otherwise, c = floor((c * d + r)/d) + 1.
402 * This is the same normalization that is performed by isl_aff_floor.
404 static __isl_give isl_basic_map *reduce_coefficient_in_div(
405 __isl_take isl_basic_map *bmap, int div, int pos)
407 isl_int shift;
408 int add_one;
410 isl_int_init(shift);
411 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
412 isl_int_mul_ui(shift, shift, 2);
413 add_one = isl_int_gt(shift, bmap->div[div][0]);
414 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
415 if (add_one)
416 isl_int_add_ui(shift, shift, 1);
417 isl_int_neg(shift, shift);
418 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
419 isl_int_clear(shift);
421 return bmap;
424 /* Does the coefficient of the variable at position "pos"
425 * in integer division "div" need to be reduced?
426 * That is, does it lie outside the half-open interval (1/2,1/2]?
427 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
428 * 2 * c != d.
430 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
431 int pos)
433 isl_bool r;
435 if (isl_int_is_zero(bmap->div[div][1 + pos]))
436 return isl_bool_false;
438 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
439 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
440 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
441 isl_int_divexact_ui(bmap->div[div][1 + pos],
442 bmap->div[div][1 + pos], 2);
444 return r;
447 /* Reduce the coefficients (including the constant term) of
448 * integer division "div", if needed.
449 * In particular, make sure all coefficients lie in
450 * the half-open interval (1/2,1/2].
452 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
453 __isl_take isl_basic_map *bmap, int div)
455 int i;
456 unsigned total = 1 + isl_basic_map_total_dim(bmap);
458 for (i = 0; i < total; ++i) {
459 isl_bool reduce;
461 reduce = needs_reduction(bmap, div, i);
462 if (reduce < 0)
463 return isl_basic_map_free(bmap);
464 if (!reduce)
465 continue;
466 bmap = reduce_coefficient_in_div(bmap, div, i);
467 if (!bmap)
468 break;
471 return bmap;
474 /* Reduce the coefficients (including the constant term) of
475 * the known integer divisions, if needed
476 * In particular, make sure all coefficients lie in
477 * the half-open interval (1/2,1/2].
479 static __isl_give isl_basic_map *reduce_div_coefficients(
480 __isl_take isl_basic_map *bmap)
482 int i;
484 if (!bmap)
485 return NULL;
486 if (bmap->n_div == 0)
487 return bmap;
489 for (i = 0; i < bmap->n_div; ++i) {
490 if (isl_int_is_zero(bmap->div[i][0]))
491 continue;
492 bmap = reduce_div_coefficients_of_div(bmap, i);
493 if (!bmap)
494 break;
497 return bmap;
500 /* Remove any common factor in numerator and denominator of the div expression,
501 * not taking into account the constant term.
502 * That is, if the div is of the form
504 * floor((a + m f(x))/(m d))
506 * then replace it by
508 * floor((floor(a/m) + f(x))/d)
510 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
511 * and can therefore not influence the result of the floor.
513 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
515 unsigned total = isl_basic_map_total_dim(bmap);
516 isl_ctx *ctx = bmap->ctx;
518 if (isl_int_is_zero(bmap->div[div][0]))
519 return;
520 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
521 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
522 if (isl_int_is_one(ctx->normalize_gcd))
523 return;
524 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
525 ctx->normalize_gcd);
526 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
527 ctx->normalize_gcd);
528 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
529 ctx->normalize_gcd, total);
532 /* Remove any common factor in numerator and denominator of a div expression,
533 * not taking into account the constant term.
534 * That is, look for any div of the form
536 * floor((a + m f(x))/(m d))
538 * and replace it by
540 * floor((floor(a/m) + f(x))/d)
542 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
543 * and can therefore not influence the result of the floor.
545 static __isl_give isl_basic_map *normalize_div_expressions(
546 __isl_take isl_basic_map *bmap)
548 int i;
550 if (!bmap)
551 return NULL;
552 if (bmap->n_div == 0)
553 return bmap;
555 for (i = 0; i < bmap->n_div; ++i)
556 normalize_div_expression(bmap, i);
558 return bmap;
561 /* Assumes divs have been ordered if keep_divs is set.
563 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
564 unsigned pos, isl_int *eq, int keep_divs, int *progress)
566 unsigned total;
567 unsigned space_total;
568 int k;
569 int last_div;
571 total = isl_basic_map_total_dim(bmap);
572 space_total = isl_space_dim(bmap->dim, isl_dim_all);
573 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
574 for (k = 0; k < bmap->n_eq; ++k) {
575 if (bmap->eq[k] == eq)
576 continue;
577 if (isl_int_is_zero(bmap->eq[k][1+pos]))
578 continue;
579 if (progress)
580 *progress = 1;
581 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
582 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
585 for (k = 0; k < bmap->n_ineq; ++k) {
586 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
587 continue;
588 if (progress)
589 *progress = 1;
590 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
591 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
595 for (k = 0; k < bmap->n_div; ++k) {
596 if (isl_int_is_zero(bmap->div[k][0]))
597 continue;
598 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
599 continue;
600 if (progress)
601 *progress = 1;
602 /* We need to be careful about circular definitions,
603 * so for now we just remove the definition of div k
604 * if the equality contains any divs.
605 * If keep_divs is set, then the divs have been ordered
606 * and we can keep the definition as long as the result
607 * is still ordered.
609 if (last_div == -1 || (keep_divs && last_div < k)) {
610 isl_seq_elim(bmap->div[k]+1, eq,
611 1+pos, 1+total, &bmap->div[k][0]);
612 normalize_div_expression(bmap, k);
613 } else
614 isl_seq_clr(bmap->div[k], 1 + total);
615 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
619 /* Assumes divs have been ordered if keep_divs is set.
621 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
622 isl_int *eq, unsigned div, int keep_divs)
624 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
626 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
628 bmap = isl_basic_map_drop_div(bmap, div);
630 return bmap;
633 /* Check if elimination of div "div" using equality "eq" would not
634 * result in a div depending on a later div.
636 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
637 unsigned div)
639 int k;
640 int last_div;
641 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
642 unsigned pos = space_total + div;
644 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
645 if (last_div < 0 || last_div <= div)
646 return 1;
648 for (k = 0; k <= last_div; ++k) {
649 if (isl_int_is_zero(bmap->div[k][0]))
650 return 1;
651 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
652 return 0;
655 return 1;
658 /* Elimininate divs based on equalities
660 static struct isl_basic_map *eliminate_divs_eq(
661 struct isl_basic_map *bmap, int *progress)
663 int d;
664 int i;
665 int modified = 0;
666 unsigned off;
668 bmap = isl_basic_map_order_divs(bmap);
670 if (!bmap)
671 return NULL;
673 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
675 for (d = bmap->n_div - 1; d >= 0 ; --d) {
676 for (i = 0; i < bmap->n_eq; ++i) {
677 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
678 !isl_int_is_negone(bmap->eq[i][off + d]))
679 continue;
680 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
681 continue;
682 modified = 1;
683 *progress = 1;
684 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
685 if (isl_basic_map_drop_equality(bmap, i) < 0)
686 return isl_basic_map_free(bmap);
687 break;
690 if (modified)
691 return eliminate_divs_eq(bmap, progress);
692 return bmap;
695 /* Elimininate divs based on inequalities
697 static struct isl_basic_map *eliminate_divs_ineq(
698 struct isl_basic_map *bmap, int *progress)
700 int d;
701 int i;
702 unsigned off;
703 struct isl_ctx *ctx;
705 if (!bmap)
706 return NULL;
708 ctx = bmap->ctx;
709 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
711 for (d = bmap->n_div - 1; d >= 0 ; --d) {
712 for (i = 0; i < bmap->n_eq; ++i)
713 if (!isl_int_is_zero(bmap->eq[i][off + d]))
714 break;
715 if (i < bmap->n_eq)
716 continue;
717 for (i = 0; i < bmap->n_ineq; ++i)
718 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
719 break;
720 if (i < bmap->n_ineq)
721 continue;
722 *progress = 1;
723 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
724 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
725 break;
726 bmap = isl_basic_map_drop_div(bmap, d);
727 if (!bmap)
728 break;
730 return bmap;
733 /* Does the equality constraint at position "eq" in "bmap" involve
734 * any local variables in the range [first, first + n)
735 * that are not marked as having an explicit representation?
737 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
738 int eq, unsigned first, unsigned n)
740 unsigned o_div;
741 int i;
743 if (!bmap)
744 return isl_bool_error;
746 o_div = isl_basic_map_offset(bmap, isl_dim_div);
747 for (i = 0; i < n; ++i) {
748 isl_bool unknown;
750 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
751 continue;
752 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
753 if (unknown < 0)
754 return isl_bool_error;
755 if (unknown)
756 return isl_bool_true;
759 return isl_bool_false;
762 /* The last local variable involved in the equality constraint
763 * at position "eq" in "bmap" is the local variable at position "div".
764 * It can therefore be used to extract an explicit representation
765 * for that variable.
766 * Do so unless the local variable already has an explicit representation or
767 * the explicit representation would involve any other local variables
768 * that in turn do not have an explicit representation.
769 * An equality constraint involving local variables without an explicit
770 * representation can be used in isl_basic_map_drop_redundant_divs
771 * to separate out an independent local variable. Introducing
772 * an explicit representation here would block this transformation,
773 * while the partial explicit representation in itself is not very useful.
774 * Set *progress if anything is changed.
776 * The equality constraint is of the form
778 * f(x) + n e >= 0
780 * with n a positive number. The explicit representation derived from
781 * this constraint is
783 * floor((-f(x))/n)
785 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
786 int div, int eq, int *progress)
788 unsigned total, o_div;
789 isl_bool involves;
791 if (!bmap)
792 return NULL;
794 if (!isl_int_is_zero(bmap->div[div][0]))
795 return bmap;
797 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
798 if (involves < 0)
799 return isl_basic_map_free(bmap);
800 if (involves)
801 return bmap;
803 total = isl_basic_map_dim(bmap, isl_dim_all);
804 o_div = isl_basic_map_offset(bmap, isl_dim_div);
805 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
806 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
807 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
808 if (progress)
809 *progress = 1;
810 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
812 return bmap;
815 struct isl_basic_map *isl_basic_map_gauss(
816 struct isl_basic_map *bmap, int *progress)
818 int k;
819 int done;
820 int last_var;
821 unsigned total_var;
822 unsigned total;
824 bmap = isl_basic_map_order_divs(bmap);
826 if (!bmap)
827 return NULL;
829 total = isl_basic_map_total_dim(bmap);
830 total_var = total - bmap->n_div;
832 last_var = total - 1;
833 for (done = 0; done < bmap->n_eq; ++done) {
834 for (; last_var >= 0; --last_var) {
835 for (k = done; k < bmap->n_eq; ++k)
836 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
837 break;
838 if (k < bmap->n_eq)
839 break;
841 if (last_var < 0)
842 break;
843 if (k != done)
844 swap_equality(bmap, k, done);
845 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
846 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
848 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
849 progress);
851 if (last_var >= total_var)
852 bmap = set_div_from_eq(bmap, last_var - total_var,
853 done, progress);
854 if (!bmap)
855 return NULL;
857 if (done == bmap->n_eq)
858 return bmap;
859 for (k = done; k < bmap->n_eq; ++k) {
860 if (isl_int_is_zero(bmap->eq[k][0]))
861 continue;
862 return isl_basic_map_set_to_empty(bmap);
864 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
865 return bmap;
868 struct isl_basic_set *isl_basic_set_gauss(
869 struct isl_basic_set *bset, int *progress)
871 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
872 progress));
876 static unsigned int round_up(unsigned int v)
878 int old_v = v;
880 while (v) {
881 old_v = v;
882 v ^= v & -v;
884 return old_v << 1;
887 /* Hash table of inequalities in a basic map.
888 * "index" is an array of addresses of inequalities in the basic map, some
889 * of which are NULL. The inequalities are hashed on the coefficients
890 * except the constant term.
891 * "size" is the number of elements in the array and is always a power of two
892 * "bits" is the number of bits need to represent an index into the array.
893 * "total" is the total dimension of the basic map.
895 struct isl_constraint_index {
896 unsigned int size;
897 int bits;
898 isl_int ***index;
899 unsigned total;
902 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
904 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
905 __isl_keep isl_basic_map *bmap)
907 isl_ctx *ctx;
909 ci->index = NULL;
910 if (!bmap)
911 return isl_stat_error;
912 ci->total = isl_basic_set_total_dim(bmap);
913 if (bmap->n_ineq == 0)
914 return isl_stat_ok;
915 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
916 ci->bits = ffs(ci->size) - 1;
917 ctx = isl_basic_map_get_ctx(bmap);
918 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
919 if (!ci->index)
920 return isl_stat_error;
922 return isl_stat_ok;
925 /* Free the memory allocated by create_constraint_index.
927 static void constraint_index_free(struct isl_constraint_index *ci)
929 free(ci->index);
932 /* Return the position in ci->index that contains the address of
933 * an inequality that is equal to *ineq up to the constant term,
934 * provided this address is not identical to "ineq".
935 * If there is no such inequality, then return the position where
936 * such an inequality should be inserted.
938 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
940 int h;
941 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
942 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
943 if (ineq != ci->index[h] &&
944 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
945 break;
946 return h;
949 /* Return the position in ci->index that contains the address of
950 * an inequality that is equal to the k'th inequality of "bmap"
951 * up to the constant term, provided it does not point to the very
952 * same inequality.
953 * If there is no such inequality, then return the position where
954 * such an inequality should be inserted.
956 static int hash_index(struct isl_constraint_index *ci,
957 __isl_keep isl_basic_map *bmap, int k)
959 return hash_index_ineq(ci, &bmap->ineq[k]);
962 static int set_hash_index(struct isl_constraint_index *ci,
963 struct isl_basic_set *bset, int k)
965 return hash_index(ci, bset, k);
968 /* Fill in the "ci" data structure with the inequalities of "bset".
970 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
971 __isl_keep isl_basic_set *bset)
973 int k, h;
975 if (create_constraint_index(ci, bset) < 0)
976 return isl_stat_error;
978 for (k = 0; k < bset->n_ineq; ++k) {
979 h = set_hash_index(ci, bset, k);
980 ci->index[h] = &bset->ineq[k];
983 return isl_stat_ok;
986 /* Is the inequality ineq (obviously) redundant with respect
987 * to the constraints in "ci"?
989 * Look for an inequality in "ci" with the same coefficients and then
990 * check if the contant term of "ineq" is greater than or equal
991 * to the constant term of that inequality. If so, "ineq" is clearly
992 * redundant.
994 * Note that hash_index_ineq ignores a stored constraint if it has
995 * the same address as the passed inequality. It is ok to pass
996 * the address of a local variable here since it will never be
997 * the same as the address of a constraint in "ci".
999 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
1000 isl_int *ineq)
1002 int h;
1004 h = hash_index_ineq(ci, &ineq);
1005 if (!ci->index[h])
1006 return isl_bool_false;
1007 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
1010 /* If we can eliminate more than one div, then we need to make
1011 * sure we do it from last div to first div, in order not to
1012 * change the position of the other divs that still need to
1013 * be removed.
1015 static struct isl_basic_map *remove_duplicate_divs(
1016 struct isl_basic_map *bmap, int *progress)
1018 unsigned int size;
1019 int *index;
1020 int *elim_for;
1021 int k, l, h;
1022 int bits;
1023 struct isl_blk eq;
1024 unsigned total_var;
1025 unsigned total;
1026 struct isl_ctx *ctx;
1028 bmap = isl_basic_map_order_divs(bmap);
1029 if (!bmap || bmap->n_div <= 1)
1030 return bmap;
1032 total_var = isl_space_dim(bmap->dim, isl_dim_all);
1033 total = total_var + bmap->n_div;
1035 ctx = bmap->ctx;
1036 for (k = bmap->n_div - 1; k >= 0; --k)
1037 if (!isl_int_is_zero(bmap->div[k][0]))
1038 break;
1039 if (k <= 0)
1040 return bmap;
1042 size = round_up(4 * bmap->n_div / 3 - 1);
1043 if (size == 0)
1044 return bmap;
1045 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
1046 bits = ffs(size) - 1;
1047 index = isl_calloc_array(ctx, int, size);
1048 if (!elim_for || !index)
1049 goto out;
1050 eq = isl_blk_alloc(ctx, 1+total);
1051 if (isl_blk_is_error(eq))
1052 goto out;
1054 isl_seq_clr(eq.data, 1+total);
1055 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
1056 for (--k; k >= 0; --k) {
1057 uint32_t hash;
1059 if (isl_int_is_zero(bmap->div[k][0]))
1060 continue;
1062 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
1063 for (h = hash; index[h]; h = (h+1) % size)
1064 if (isl_seq_eq(bmap->div[k],
1065 bmap->div[index[h]-1], 2+total))
1066 break;
1067 if (index[h]) {
1068 *progress = 1;
1069 l = index[h] - 1;
1070 elim_for[l] = k + 1;
1072 index[h] = k+1;
1074 for (l = bmap->n_div - 1; l >= 0; --l) {
1075 if (!elim_for[l])
1076 continue;
1077 k = elim_for[l] - 1;
1078 isl_int_set_si(eq.data[1+total_var+k], -1);
1079 isl_int_set_si(eq.data[1+total_var+l], 1);
1080 bmap = eliminate_div(bmap, eq.data, l, 1);
1081 if (!bmap)
1082 break;
1083 isl_int_set_si(eq.data[1+total_var+k], 0);
1084 isl_int_set_si(eq.data[1+total_var+l], 0);
1087 isl_blk_free(ctx, eq);
1088 out:
1089 free(index);
1090 free(elim_for);
1091 return bmap;
1094 static int n_pure_div_eq(struct isl_basic_map *bmap)
1096 int i, j;
1097 unsigned total;
1099 total = isl_space_dim(bmap->dim, isl_dim_all);
1100 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1101 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1102 --j;
1103 if (j < 0)
1104 break;
1105 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
1106 return 0;
1108 return i;
1111 /* Normalize divs that appear in equalities.
1113 * In particular, we assume that bmap contains some equalities
1114 * of the form
1116 * a x = m * e_i
1118 * and we want to replace the set of e_i by a minimal set and
1119 * such that the new e_i have a canonical representation in terms
1120 * of the vector x.
1121 * If any of the equalities involves more than one divs, then
1122 * we currently simply bail out.
1124 * Let us first additionally assume that all equalities involve
1125 * a div. The equalities then express modulo constraints on the
1126 * remaining variables and we can use "parameter compression"
1127 * to find a minimal set of constraints. The result is a transformation
1129 * x = T(x') = x_0 + G x'
1131 * with G a lower-triangular matrix with all elements below the diagonal
1132 * non-negative and smaller than the diagonal element on the same row.
1133 * We first normalize x_0 by making the same property hold in the affine
1134 * T matrix.
1135 * The rows i of G with a 1 on the diagonal do not impose any modulo
1136 * constraint and simply express x_i = x'_i.
1137 * For each of the remaining rows i, we introduce a div and a corresponding
1138 * equality. In particular
1140 * g_ii e_j = x_i - g_i(x')
1142 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1143 * corresponding div (if g_kk != 1).
1145 * If there are any equalities not involving any div, then we
1146 * first apply a variable compression on the variables x:
1148 * x = C x'' x'' = C_2 x
1150 * and perform the above parameter compression on A C instead of on A.
1151 * The resulting compression is then of the form
1153 * x'' = T(x') = x_0 + G x'
1155 * and in constructing the new divs and the corresponding equalities,
1156 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1157 * by the corresponding row from C_2.
1159 static struct isl_basic_map *normalize_divs(
1160 struct isl_basic_map *bmap, int *progress)
1162 int i, j, k;
1163 int total;
1164 int div_eq;
1165 struct isl_mat *B;
1166 struct isl_vec *d;
1167 struct isl_mat *T = NULL;
1168 struct isl_mat *C = NULL;
1169 struct isl_mat *C2 = NULL;
1170 isl_int v;
1171 int *pos;
1172 int dropped, needed;
1174 if (!bmap)
1175 return NULL;
1177 if (bmap->n_div == 0)
1178 return bmap;
1180 if (bmap->n_eq == 0)
1181 return bmap;
1183 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1184 return bmap;
1186 total = isl_space_dim(bmap->dim, isl_dim_all);
1187 div_eq = n_pure_div_eq(bmap);
1188 if (div_eq == 0)
1189 return bmap;
1191 if (div_eq < bmap->n_eq) {
1192 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1193 bmap->n_eq - div_eq, 0, 1 + total);
1194 C = isl_mat_variable_compression(B, &C2);
1195 if (!C || !C2)
1196 goto error;
1197 if (C->n_col == 0) {
1198 bmap = isl_basic_map_set_to_empty(bmap);
1199 isl_mat_free(C);
1200 isl_mat_free(C2);
1201 goto done;
1205 d = isl_vec_alloc(bmap->ctx, div_eq);
1206 if (!d)
1207 goto error;
1208 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1209 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1210 --j;
1211 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1213 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1215 if (C) {
1216 B = isl_mat_product(B, C);
1217 C = NULL;
1220 T = isl_mat_parameter_compression(B, d);
1221 if (!T)
1222 goto error;
1223 if (T->n_col == 0) {
1224 bmap = isl_basic_map_set_to_empty(bmap);
1225 isl_mat_free(C2);
1226 isl_mat_free(T);
1227 goto done;
1229 isl_int_init(v);
1230 for (i = 0; i < T->n_row - 1; ++i) {
1231 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1232 if (isl_int_is_zero(v))
1233 continue;
1234 isl_mat_col_submul(T, 0, v, 1 + i);
1236 isl_int_clear(v);
1237 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1238 if (!pos)
1239 goto error;
1240 /* We have to be careful because dropping equalities may reorder them */
1241 dropped = 0;
1242 for (j = bmap->n_div - 1; j >= 0; --j) {
1243 for (i = 0; i < bmap->n_eq; ++i)
1244 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1245 break;
1246 if (i < bmap->n_eq) {
1247 bmap = isl_basic_map_drop_div(bmap, j);
1248 isl_basic_map_drop_equality(bmap, i);
1249 ++dropped;
1252 pos[0] = 0;
1253 needed = 0;
1254 for (i = 1; i < T->n_row; ++i) {
1255 if (isl_int_is_one(T->row[i][i]))
1256 pos[i] = i;
1257 else
1258 needed++;
1260 if (needed > dropped) {
1261 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1262 needed, needed, 0);
1263 if (!bmap)
1264 goto error;
1266 for (i = 1; i < T->n_row; ++i) {
1267 if (isl_int_is_one(T->row[i][i]))
1268 continue;
1269 k = isl_basic_map_alloc_div(bmap);
1270 pos[i] = 1 + total + k;
1271 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1272 isl_int_set(bmap->div[k][0], T->row[i][i]);
1273 if (C2)
1274 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1275 else
1276 isl_int_set_si(bmap->div[k][1 + i], 1);
1277 for (j = 0; j < i; ++j) {
1278 if (isl_int_is_zero(T->row[i][j]))
1279 continue;
1280 if (pos[j] < T->n_row && C2)
1281 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1282 C2->row[pos[j]], 1 + total);
1283 else
1284 isl_int_neg(bmap->div[k][1 + pos[j]],
1285 T->row[i][j]);
1287 j = isl_basic_map_alloc_equality(bmap);
1288 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1289 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1291 free(pos);
1292 isl_mat_free(C2);
1293 isl_mat_free(T);
1295 if (progress)
1296 *progress = 1;
1297 done:
1298 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1300 return bmap;
1301 error:
1302 isl_mat_free(C);
1303 isl_mat_free(C2);
1304 isl_mat_free(T);
1305 return bmap;
1308 static struct isl_basic_map *set_div_from_lower_bound(
1309 struct isl_basic_map *bmap, int div, int ineq)
1311 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1313 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1314 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1315 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1316 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1317 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1319 return bmap;
1322 /* Check whether it is ok to define a div based on an inequality.
1323 * To avoid the introduction of circular definitions of divs, we
1324 * do not allow such a definition if the resulting expression would refer to
1325 * any other undefined divs or if any known div is defined in
1326 * terms of the unknown div.
1328 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1329 int div, int ineq)
1331 int j;
1332 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1334 /* Not defined in terms of unknown divs */
1335 for (j = 0; j < bmap->n_div; ++j) {
1336 if (div == j)
1337 continue;
1338 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1339 continue;
1340 if (isl_int_is_zero(bmap->div[j][0]))
1341 return 0;
1344 /* No other div defined in terms of this one => avoid loops */
1345 for (j = 0; j < bmap->n_div; ++j) {
1346 if (div == j)
1347 continue;
1348 if (isl_int_is_zero(bmap->div[j][0]))
1349 continue;
1350 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1351 return 0;
1354 return 1;
1357 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1358 * be a better expression than the current one?
1360 * If we do not have any expression yet, then any expression would be better.
1361 * Otherwise we check if the last variable involved in the inequality
1362 * (disregarding the div that it would define) is in an earlier position
1363 * than the last variable involved in the current div expression.
1365 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1366 int div, int ineq)
1368 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1369 int last_div;
1370 int last_ineq;
1372 if (isl_int_is_zero(bmap->div[div][0]))
1373 return 1;
1375 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1376 bmap->n_div - (div + 1)) >= 0)
1377 return 0;
1379 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1380 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1381 total + bmap->n_div);
1383 return last_ineq < last_div;
1386 /* Given two constraints "k" and "l" that are opposite to each other,
1387 * except for the constant term, check if we can use them
1388 * to obtain an expression for one of the hitherto unknown divs or
1389 * a "better" expression for a div for which we already have an expression.
1390 * "sum" is the sum of the constant terms of the constraints.
1391 * If this sum is strictly smaller than the coefficient of one
1392 * of the divs, then this pair can be used define the div.
1393 * To avoid the introduction of circular definitions of divs, we
1394 * do not use the pair if the resulting expression would refer to
1395 * any other undefined divs or if any known div is defined in
1396 * terms of the unknown div.
1398 static struct isl_basic_map *check_for_div_constraints(
1399 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1401 int i;
1402 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1404 for (i = 0; i < bmap->n_div; ++i) {
1405 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1406 continue;
1407 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1408 continue;
1409 if (!better_div_constraint(bmap, i, k))
1410 continue;
1411 if (!ok_to_set_div_from_bound(bmap, i, k))
1412 break;
1413 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1414 bmap = set_div_from_lower_bound(bmap, i, k);
1415 else
1416 bmap = set_div_from_lower_bound(bmap, i, l);
1417 if (progress)
1418 *progress = 1;
1419 break;
1421 return bmap;
1424 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1425 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1427 struct isl_constraint_index ci;
1428 int k, l, h;
1429 unsigned total = isl_basic_map_total_dim(bmap);
1430 isl_int sum;
1432 if (!bmap || bmap->n_ineq <= 1)
1433 return bmap;
1435 if (create_constraint_index(&ci, bmap) < 0)
1436 return bmap;
1438 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1439 ci.index[h] = &bmap->ineq[0];
1440 for (k = 1; k < bmap->n_ineq; ++k) {
1441 h = hash_index(&ci, bmap, k);
1442 if (!ci.index[h]) {
1443 ci.index[h] = &bmap->ineq[k];
1444 continue;
1446 if (progress)
1447 *progress = 1;
1448 l = ci.index[h] - &bmap->ineq[0];
1449 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1450 swap_inequality(bmap, k, l);
1451 isl_basic_map_drop_inequality(bmap, k);
1452 --k;
1454 isl_int_init(sum);
1455 for (k = 0; k < bmap->n_ineq-1; ++k) {
1456 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1457 h = hash_index(&ci, bmap, k);
1458 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1459 if (!ci.index[h])
1460 continue;
1461 l = ci.index[h] - &bmap->ineq[0];
1462 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1463 if (isl_int_is_pos(sum)) {
1464 if (detect_divs)
1465 bmap = check_for_div_constraints(bmap, k, l,
1466 sum, progress);
1467 continue;
1469 if (isl_int_is_zero(sum)) {
1470 /* We need to break out of the loop after these
1471 * changes since the contents of the hash
1472 * will no longer be valid.
1473 * Plus, we probably we want to regauss first.
1475 if (progress)
1476 *progress = 1;
1477 isl_basic_map_drop_inequality(bmap, l);
1478 isl_basic_map_inequality_to_equality(bmap, k);
1479 } else
1480 bmap = isl_basic_map_set_to_empty(bmap);
1481 break;
1483 isl_int_clear(sum);
1485 constraint_index_free(&ci);
1486 return bmap;
1489 /* Detect all pairs of inequalities that form an equality.
1491 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1492 * Call it repeatedly while it is making progress.
1494 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1495 __isl_take isl_basic_map *bmap, int *progress)
1497 int duplicate;
1499 do {
1500 duplicate = 0;
1501 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1502 &duplicate, 0);
1503 if (progress && duplicate)
1504 *progress = 1;
1505 } while (duplicate);
1507 return bmap;
1510 /* Eliminate knowns divs from constraints where they appear with
1511 * a (positive or negative) unit coefficient.
1513 * That is, replace
1515 * floor(e/m) + f >= 0
1517 * by
1519 * e + m f >= 0
1521 * and
1523 * -floor(e/m) + f >= 0
1525 * by
1527 * -e + m f + m - 1 >= 0
1529 * The first conversion is valid because floor(e/m) >= -f is equivalent
1530 * to e/m >= -f because -f is an integral expression.
1531 * The second conversion follows from the fact that
1533 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1536 * Note that one of the div constraints may have been eliminated
1537 * due to being redundant with respect to the constraint that is
1538 * being modified by this function. The modified constraint may
1539 * no longer imply this div constraint, so we add it back to make
1540 * sure we do not lose any information.
1542 * We skip integral divs, i.e., those with denominator 1, as we would
1543 * risk eliminating the div from the div constraints. We do not need
1544 * to handle those divs here anyway since the div constraints will turn
1545 * out to form an equality and this equality can then be used to eliminate
1546 * the div from all constraints.
1548 static __isl_give isl_basic_map *eliminate_unit_divs(
1549 __isl_take isl_basic_map *bmap, int *progress)
1551 int i, j;
1552 isl_ctx *ctx;
1553 unsigned total;
1555 if (!bmap)
1556 return NULL;
1558 ctx = isl_basic_map_get_ctx(bmap);
1559 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1561 for (i = 0; i < bmap->n_div; ++i) {
1562 if (isl_int_is_zero(bmap->div[i][0]))
1563 continue;
1564 if (isl_int_is_one(bmap->div[i][0]))
1565 continue;
1566 for (j = 0; j < bmap->n_ineq; ++j) {
1567 int s;
1569 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1570 !isl_int_is_negone(bmap->ineq[j][total + i]))
1571 continue;
1573 *progress = 1;
1575 s = isl_int_sgn(bmap->ineq[j][total + i]);
1576 isl_int_set_si(bmap->ineq[j][total + i], 0);
1577 if (s < 0)
1578 isl_seq_combine(bmap->ineq[j],
1579 ctx->negone, bmap->div[i] + 1,
1580 bmap->div[i][0], bmap->ineq[j],
1581 total + bmap->n_div);
1582 else
1583 isl_seq_combine(bmap->ineq[j],
1584 ctx->one, bmap->div[i] + 1,
1585 bmap->div[i][0], bmap->ineq[j],
1586 total + bmap->n_div);
1587 if (s < 0) {
1588 isl_int_add(bmap->ineq[j][0],
1589 bmap->ineq[j][0], bmap->div[i][0]);
1590 isl_int_sub_ui(bmap->ineq[j][0],
1591 bmap->ineq[j][0], 1);
1594 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1595 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1596 return isl_basic_map_free(bmap);
1600 return bmap;
1603 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1605 int progress = 1;
1606 if (!bmap)
1607 return NULL;
1608 while (progress) {
1609 progress = 0;
1610 if (!bmap)
1611 break;
1612 if (isl_basic_map_plain_is_empty(bmap))
1613 break;
1614 bmap = isl_basic_map_normalize_constraints(bmap);
1615 bmap = reduce_div_coefficients(bmap);
1616 bmap = normalize_div_expressions(bmap);
1617 bmap = remove_duplicate_divs(bmap, &progress);
1618 bmap = eliminate_unit_divs(bmap, &progress);
1619 bmap = eliminate_divs_eq(bmap, &progress);
1620 bmap = eliminate_divs_ineq(bmap, &progress);
1621 bmap = isl_basic_map_gauss(bmap, &progress);
1622 /* requires equalities in normal form */
1623 bmap = normalize_divs(bmap, &progress);
1624 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1625 &progress, 1);
1626 if (bmap && progress)
1627 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1629 return bmap;
1632 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1634 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1638 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1639 isl_int *constraint, unsigned div)
1641 unsigned pos;
1643 if (!bmap)
1644 return -1;
1646 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1648 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1649 int neg;
1650 isl_int_sub(bmap->div[div][1],
1651 bmap->div[div][1], bmap->div[div][0]);
1652 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1653 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1654 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1655 isl_int_add(bmap->div[div][1],
1656 bmap->div[div][1], bmap->div[div][0]);
1657 if (!neg)
1658 return 0;
1659 if (isl_seq_first_non_zero(constraint+pos+1,
1660 bmap->n_div-div-1) != -1)
1661 return 0;
1662 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1663 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1664 return 0;
1665 if (isl_seq_first_non_zero(constraint+pos+1,
1666 bmap->n_div-div-1) != -1)
1667 return 0;
1668 } else
1669 return 0;
1671 return 1;
1674 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1675 isl_int *constraint, unsigned div)
1677 return isl_basic_map_is_div_constraint(bset, constraint, div);
1681 /* If the only constraints a div d=floor(f/m)
1682 * appears in are its two defining constraints
1684 * f - m d >=0
1685 * -(f - (m - 1)) + m d >= 0
1687 * then it can safely be removed.
1689 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1691 int i;
1692 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1694 for (i = 0; i < bmap->n_eq; ++i)
1695 if (!isl_int_is_zero(bmap->eq[i][pos]))
1696 return 0;
1698 for (i = 0; i < bmap->n_ineq; ++i) {
1699 if (isl_int_is_zero(bmap->ineq[i][pos]))
1700 continue;
1701 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1702 return 0;
1705 for (i = 0; i < bmap->n_div; ++i) {
1706 if (isl_int_is_zero(bmap->div[i][0]))
1707 continue;
1708 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1709 return 0;
1712 return 1;
1716 * Remove divs that don't occur in any of the constraints or other divs.
1717 * These can arise when dropping constraints from a basic map or
1718 * when the divs of a basic map have been temporarily aligned
1719 * with the divs of another basic map.
1721 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1723 int i;
1725 if (!bmap)
1726 return NULL;
1728 for (i = bmap->n_div-1; i >= 0; --i) {
1729 if (!div_is_redundant(bmap, i))
1730 continue;
1731 bmap = isl_basic_map_drop_div(bmap, i);
1733 return bmap;
1736 /* Mark "bmap" as final, without checking for obviously redundant
1737 * integer divisions. This function should be used when "bmap"
1738 * is known not to involve any such integer divisions.
1740 __isl_give isl_basic_map *isl_basic_map_mark_final(
1741 __isl_take isl_basic_map *bmap)
1743 if (!bmap)
1744 return NULL;
1745 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1746 return bmap;
1749 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1751 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1753 bmap = remove_redundant_divs(bmap);
1754 bmap = isl_basic_map_mark_final(bmap);
1755 return bmap;
1758 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1760 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1763 struct isl_set *isl_set_finalize(struct isl_set *set)
1765 int i;
1767 if (!set)
1768 return NULL;
1769 for (i = 0; i < set->n; ++i) {
1770 set->p[i] = isl_basic_set_finalize(set->p[i]);
1771 if (!set->p[i])
1772 goto error;
1774 return set;
1775 error:
1776 isl_set_free(set);
1777 return NULL;
1780 struct isl_map *isl_map_finalize(struct isl_map *map)
1782 int i;
1784 if (!map)
1785 return NULL;
1786 for (i = 0; i < map->n; ++i) {
1787 map->p[i] = isl_basic_map_finalize(map->p[i]);
1788 if (!map->p[i])
1789 goto error;
1791 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1792 return map;
1793 error:
1794 isl_map_free(map);
1795 return NULL;
1799 /* Remove definition of any div that is defined in terms of the given variable.
1800 * The div itself is not removed. Functions such as
1801 * eliminate_divs_ineq depend on the other divs remaining in place.
1803 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1804 int pos)
1806 int i;
1808 if (!bmap)
1809 return NULL;
1811 for (i = 0; i < bmap->n_div; ++i) {
1812 if (isl_int_is_zero(bmap->div[i][0]))
1813 continue;
1814 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1815 continue;
1816 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1817 if (!bmap)
1818 return NULL;
1820 return bmap;
1823 /* Eliminate the specified variables from the constraints using
1824 * Fourier-Motzkin. The variables themselves are not removed.
1826 struct isl_basic_map *isl_basic_map_eliminate_vars(
1827 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1829 int d;
1830 int i, j, k;
1831 unsigned total;
1832 int need_gauss = 0;
1834 if (n == 0)
1835 return bmap;
1836 if (!bmap)
1837 return NULL;
1838 total = isl_basic_map_total_dim(bmap);
1840 bmap = isl_basic_map_cow(bmap);
1841 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1842 bmap = remove_dependent_vars(bmap, d);
1843 if (!bmap)
1844 return NULL;
1846 for (d = pos + n - 1;
1847 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1848 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1849 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1850 int n_lower, n_upper;
1851 if (!bmap)
1852 return NULL;
1853 for (i = 0; i < bmap->n_eq; ++i) {
1854 if (isl_int_is_zero(bmap->eq[i][1+d]))
1855 continue;
1856 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1857 isl_basic_map_drop_equality(bmap, i);
1858 need_gauss = 1;
1859 break;
1861 if (i < bmap->n_eq)
1862 continue;
1863 n_lower = 0;
1864 n_upper = 0;
1865 for (i = 0; i < bmap->n_ineq; ++i) {
1866 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1867 n_lower++;
1868 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1869 n_upper++;
1871 bmap = isl_basic_map_extend_constraints(bmap,
1872 0, n_lower * n_upper);
1873 if (!bmap)
1874 goto error;
1875 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1876 int last;
1877 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1878 continue;
1879 last = -1;
1880 for (j = 0; j < i; ++j) {
1881 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1882 continue;
1883 last = j;
1884 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1885 isl_int_sgn(bmap->ineq[j][1+d]))
1886 continue;
1887 k = isl_basic_map_alloc_inequality(bmap);
1888 if (k < 0)
1889 goto error;
1890 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1891 1+total);
1892 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1893 1+d, 1+total, NULL);
1895 isl_basic_map_drop_inequality(bmap, i);
1896 i = last + 1;
1898 if (n_lower > 0 && n_upper > 0) {
1899 bmap = isl_basic_map_normalize_constraints(bmap);
1900 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1901 NULL, 0);
1902 bmap = isl_basic_map_gauss(bmap, NULL);
1903 bmap = isl_basic_map_remove_redundancies(bmap);
1904 need_gauss = 0;
1905 if (!bmap)
1906 goto error;
1907 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1908 break;
1911 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1912 if (need_gauss)
1913 bmap = isl_basic_map_gauss(bmap, NULL);
1914 return bmap;
1915 error:
1916 isl_basic_map_free(bmap);
1917 return NULL;
1920 struct isl_basic_set *isl_basic_set_eliminate_vars(
1921 struct isl_basic_set *bset, unsigned pos, unsigned n)
1923 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1924 pos, n));
1927 /* Eliminate the specified n dimensions starting at first from the
1928 * constraints, without removing the dimensions from the space.
1929 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1930 * Otherwise, they are projected out and the original space is restored.
1932 __isl_give isl_basic_map *isl_basic_map_eliminate(
1933 __isl_take isl_basic_map *bmap,
1934 enum isl_dim_type type, unsigned first, unsigned n)
1936 isl_space *space;
1938 if (!bmap)
1939 return NULL;
1940 if (n == 0)
1941 return bmap;
1943 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1944 isl_die(bmap->ctx, isl_error_invalid,
1945 "index out of bounds", goto error);
1947 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1948 first += isl_basic_map_offset(bmap, type) - 1;
1949 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1950 return isl_basic_map_finalize(bmap);
1953 space = isl_basic_map_get_space(bmap);
1954 bmap = isl_basic_map_project_out(bmap, type, first, n);
1955 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1956 bmap = isl_basic_map_reset_space(bmap, space);
1957 return bmap;
1958 error:
1959 isl_basic_map_free(bmap);
1960 return NULL;
1963 __isl_give isl_basic_set *isl_basic_set_eliminate(
1964 __isl_take isl_basic_set *bset,
1965 enum isl_dim_type type, unsigned first, unsigned n)
1967 return isl_basic_map_eliminate(bset, type, first, n);
1970 /* Remove all constraints from "bmap" that reference any unknown local
1971 * variables (directly or indirectly).
1973 * Dropping all constraints on a local variable will make it redundant,
1974 * so it will get removed implicitly by
1975 * isl_basic_map_drop_constraints_involving_dims. Some other local
1976 * variables may also end up becoming redundant if they only appear
1977 * in constraints together with the unknown local variable.
1978 * Therefore, start over after calling
1979 * isl_basic_map_drop_constraints_involving_dims.
1981 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1982 __isl_take isl_basic_map *bmap)
1984 isl_bool known;
1985 int i, n_div, o_div;
1987 known = isl_basic_map_divs_known(bmap);
1988 if (known < 0)
1989 return isl_basic_map_free(bmap);
1990 if (known)
1991 return bmap;
1993 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1994 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1996 for (i = 0; i < n_div; ++i) {
1997 known = isl_basic_map_div_is_known(bmap, i);
1998 if (known < 0)
1999 return isl_basic_map_free(bmap);
2000 if (known)
2001 continue;
2002 bmap = remove_dependent_vars(bmap, o_div + i);
2003 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
2004 isl_dim_div, i, 1);
2005 if (!bmap)
2006 return NULL;
2007 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2008 i = -1;
2011 return bmap;
2014 /* Remove all constraints from "map" that reference any unknown local
2015 * variables (directly or indirectly).
2017 * Since constraints may get dropped from the basic maps,
2018 * they may no longer be disjoint from each other.
2020 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
2021 __isl_take isl_map *map)
2023 int i;
2024 isl_bool known;
2026 known = isl_map_divs_known(map);
2027 if (known < 0)
2028 return isl_map_free(map);
2029 if (known)
2030 return map;
2032 map = isl_map_cow(map);
2033 if (!map)
2034 return NULL;
2036 for (i = 0; i < map->n; ++i) {
2037 map->p[i] =
2038 isl_basic_map_drop_constraint_involving_unknown_divs(
2039 map->p[i]);
2040 if (!map->p[i])
2041 return isl_map_free(map);
2044 if (map->n > 1)
2045 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2047 return map;
2050 /* Don't assume equalities are in order, because align_divs
2051 * may have changed the order of the divs.
2053 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
2055 int d, i;
2056 unsigned total;
2058 total = isl_space_dim(bmap->dim, isl_dim_all);
2059 for (d = 0; d < total; ++d)
2060 elim[d] = -1;
2061 for (i = 0; i < bmap->n_eq; ++i) {
2062 for (d = total - 1; d >= 0; --d) {
2063 if (isl_int_is_zero(bmap->eq[i][1+d]))
2064 continue;
2065 elim[d] = i;
2066 break;
2071 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
2073 compute_elimination_index(bset_to_bmap(bset), elim);
2076 static int reduced_using_equalities(isl_int *dst, isl_int *src,
2077 struct isl_basic_map *bmap, int *elim)
2079 int d;
2080 int copied = 0;
2081 unsigned total;
2083 total = isl_space_dim(bmap->dim, isl_dim_all);
2084 for (d = total - 1; d >= 0; --d) {
2085 if (isl_int_is_zero(src[1+d]))
2086 continue;
2087 if (elim[d] == -1)
2088 continue;
2089 if (!copied) {
2090 isl_seq_cpy(dst, src, 1 + total);
2091 copied = 1;
2093 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2095 return copied;
2098 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2099 struct isl_basic_set *bset, int *elim)
2101 return reduced_using_equalities(dst, src,
2102 bset_to_bmap(bset), elim);
2105 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
2106 struct isl_basic_set *bset, struct isl_basic_set *context)
2108 int i;
2109 int *elim;
2111 if (!bset || !context)
2112 goto error;
2114 if (context->n_eq == 0) {
2115 isl_basic_set_free(context);
2116 return bset;
2119 bset = isl_basic_set_cow(bset);
2120 if (!bset)
2121 goto error;
2123 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2124 if (!elim)
2125 goto error;
2126 set_compute_elimination_index(context, elim);
2127 for (i = 0; i < bset->n_eq; ++i)
2128 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2129 context, elim);
2130 for (i = 0; i < bset->n_ineq; ++i)
2131 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2132 context, elim);
2133 isl_basic_set_free(context);
2134 free(elim);
2135 bset = isl_basic_set_simplify(bset);
2136 bset = isl_basic_set_finalize(bset);
2137 return bset;
2138 error:
2139 isl_basic_set_free(bset);
2140 isl_basic_set_free(context);
2141 return NULL;
2144 /* For each inequality in "ineq" that is a shifted (more relaxed)
2145 * copy of an inequality in "context", mark the corresponding entry
2146 * in "row" with -1.
2147 * If an inequality only has a non-negative constant term, then
2148 * mark it as well.
2150 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2151 __isl_keep isl_basic_set *context, int *row)
2153 struct isl_constraint_index ci;
2154 int n_ineq;
2155 unsigned total;
2156 int k;
2158 if (!ineq || !context)
2159 return isl_stat_error;
2160 if (context->n_ineq == 0)
2161 return isl_stat_ok;
2162 if (setup_constraint_index(&ci, context) < 0)
2163 return isl_stat_error;
2165 n_ineq = isl_mat_rows(ineq);
2166 total = isl_mat_cols(ineq) - 1;
2167 for (k = 0; k < n_ineq; ++k) {
2168 int l;
2169 isl_bool redundant;
2171 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2172 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2173 row[k] = -1;
2174 continue;
2176 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2177 if (redundant < 0)
2178 goto error;
2179 if (!redundant)
2180 continue;
2181 row[k] = -1;
2183 constraint_index_free(&ci);
2184 return isl_stat_ok;
2185 error:
2186 constraint_index_free(&ci);
2187 return isl_stat_error;
2190 static struct isl_basic_set *remove_shifted_constraints(
2191 struct isl_basic_set *bset, struct isl_basic_set *context)
2193 struct isl_constraint_index ci;
2194 int k;
2196 if (!bset || !context)
2197 return bset;
2199 if (context->n_ineq == 0)
2200 return bset;
2201 if (setup_constraint_index(&ci, context) < 0)
2202 return bset;
2204 for (k = 0; k < bset->n_ineq; ++k) {
2205 isl_bool redundant;
2207 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2208 if (redundant < 0)
2209 goto error;
2210 if (!redundant)
2211 continue;
2212 bset = isl_basic_set_cow(bset);
2213 if (!bset)
2214 goto error;
2215 isl_basic_set_drop_inequality(bset, k);
2216 --k;
2218 constraint_index_free(&ci);
2219 return bset;
2220 error:
2221 constraint_index_free(&ci);
2222 return bset;
2225 /* Remove constraints from "bmap" that are identical to constraints
2226 * in "context" or that are more relaxed (greater constant term).
2228 * We perform the test for shifted copies on the pure constraints
2229 * in remove_shifted_constraints.
2231 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2232 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2234 isl_basic_set *bset, *bset_context;
2236 if (!bmap || !context)
2237 goto error;
2239 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2240 isl_basic_map_free(context);
2241 return bmap;
2244 context = isl_basic_map_align_divs(context, bmap);
2245 bmap = isl_basic_map_align_divs(bmap, context);
2247 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2248 bset_context = isl_basic_map_underlying_set(context);
2249 bset = remove_shifted_constraints(bset, bset_context);
2250 isl_basic_set_free(bset_context);
2252 bmap = isl_basic_map_overlying_set(bset, bmap);
2254 return bmap;
2255 error:
2256 isl_basic_map_free(bmap);
2257 isl_basic_map_free(context);
2258 return NULL;
2261 /* Does the (linear part of a) constraint "c" involve any of the "len"
2262 * "relevant" dimensions?
2264 static int is_related(isl_int *c, int len, int *relevant)
2266 int i;
2268 for (i = 0; i < len; ++i) {
2269 if (!relevant[i])
2270 continue;
2271 if (!isl_int_is_zero(c[i]))
2272 return 1;
2275 return 0;
2278 /* Drop constraints from "bmap" that do not involve any of
2279 * the dimensions marked "relevant".
2281 static __isl_give isl_basic_map *drop_unrelated_constraints(
2282 __isl_take isl_basic_map *bmap, int *relevant)
2284 int i, dim;
2286 dim = isl_basic_map_dim(bmap, isl_dim_all);
2287 for (i = 0; i < dim; ++i)
2288 if (!relevant[i])
2289 break;
2290 if (i >= dim)
2291 return bmap;
2293 for (i = bmap->n_eq - 1; i >= 0; --i)
2294 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2295 bmap = isl_basic_map_cow(bmap);
2296 if (isl_basic_map_drop_equality(bmap, i) < 0)
2297 return isl_basic_map_free(bmap);
2300 for (i = bmap->n_ineq - 1; i >= 0; --i)
2301 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2302 bmap = isl_basic_map_cow(bmap);
2303 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2304 return isl_basic_map_free(bmap);
2307 return bmap;
2310 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2312 * In particular, for any variable involved in the constraint,
2313 * find the actual group id from before and replace the group
2314 * of the corresponding variable by the minimal group of all
2315 * the variables involved in the constraint considered so far
2316 * (if this minimum is smaller) or replace the minimum by this group
2317 * (if the minimum is larger).
2319 * At the end, all the variables in "c" will (indirectly) point
2320 * to the minimal of the groups that they referred to originally.
2322 static void update_groups(int dim, int *group, isl_int *c)
2324 int j;
2325 int min = dim;
2327 for (j = 0; j < dim; ++j) {
2328 if (isl_int_is_zero(c[j]))
2329 continue;
2330 while (group[j] >= 0 && group[group[j]] != group[j])
2331 group[j] = group[group[j]];
2332 if (group[j] == min)
2333 continue;
2334 if (group[j] < min) {
2335 if (min >= 0 && min < dim)
2336 group[min] = group[j];
2337 min = group[j];
2338 } else
2339 group[group[j]] = min;
2343 /* Allocate an array of groups of variables, one for each variable
2344 * in "context", initialized to zero.
2346 static int *alloc_groups(__isl_keep isl_basic_set *context)
2348 isl_ctx *ctx;
2349 int dim;
2351 dim = isl_basic_set_dim(context, isl_dim_set);
2352 ctx = isl_basic_set_get_ctx(context);
2353 return isl_calloc_array(ctx, int, dim);
2356 /* Drop constraints from "bmap" that only involve variables that are
2357 * not related to any of the variables marked with a "-1" in "group".
2359 * We construct groups of variables that collect variables that
2360 * (indirectly) appear in some common constraint of "bmap".
2361 * Each group is identified by the first variable in the group,
2362 * except for the special group of variables that was already identified
2363 * in the input as -1 (or are related to those variables).
2364 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2365 * otherwise the group of i is the group of group[i].
2367 * We first initialize groups for the remaining variables.
2368 * Then we iterate over the constraints of "bmap" and update the
2369 * group of the variables in the constraint by the smallest group.
2370 * Finally, we resolve indirect references to groups by running over
2371 * the variables.
2373 * After computing the groups, we drop constraints that do not involve
2374 * any variables in the -1 group.
2376 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2377 __isl_take isl_basic_map *bmap, __isl_take int *group)
2379 int dim;
2380 int i;
2381 int last;
2383 if (!bmap)
2384 return NULL;
2386 dim = isl_basic_map_dim(bmap, isl_dim_all);
2388 last = -1;
2389 for (i = 0; i < dim; ++i)
2390 if (group[i] >= 0)
2391 last = group[i] = i;
2392 if (last < 0) {
2393 free(group);
2394 return bmap;
2397 for (i = 0; i < bmap->n_eq; ++i)
2398 update_groups(dim, group, bmap->eq[i] + 1);
2399 for (i = 0; i < bmap->n_ineq; ++i)
2400 update_groups(dim, group, bmap->ineq[i] + 1);
2402 for (i = 0; i < dim; ++i)
2403 if (group[i] >= 0)
2404 group[i] = group[group[i]];
2406 for (i = 0; i < dim; ++i)
2407 group[i] = group[i] == -1;
2409 bmap = drop_unrelated_constraints(bmap, group);
2411 free(group);
2412 return bmap;
2415 /* Drop constraints from "context" that are irrelevant for computing
2416 * the gist of "bset".
2418 * In particular, drop constraints in variables that are not related
2419 * to any of the variables involved in the constraints of "bset"
2420 * in the sense that there is no sequence of constraints that connects them.
2422 * We first mark all variables that appear in "bset" as belonging
2423 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2425 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2426 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2428 int *group;
2429 int dim;
2430 int i, j;
2432 if (!context || !bset)
2433 return isl_basic_set_free(context);
2435 group = alloc_groups(context);
2437 if (!group)
2438 return isl_basic_set_free(context);
2440 dim = isl_basic_set_dim(bset, isl_dim_set);
2441 for (i = 0; i < dim; ++i) {
2442 for (j = 0; j < bset->n_eq; ++j)
2443 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2444 break;
2445 if (j < bset->n_eq) {
2446 group[i] = -1;
2447 continue;
2449 for (j = 0; j < bset->n_ineq; ++j)
2450 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2451 break;
2452 if (j < bset->n_ineq)
2453 group[i] = -1;
2456 return isl_basic_map_drop_unrelated_constraints(context, group);
2459 /* Drop constraints from "context" that are irrelevant for computing
2460 * the gist of the inequalities "ineq".
2461 * Inequalities in "ineq" for which the corresponding element of row
2462 * is set to -1 have already been marked for removal and should be ignored.
2464 * In particular, drop constraints in variables that are not related
2465 * to any of the variables involved in "ineq"
2466 * in the sense that there is no sequence of constraints that connects them.
2468 * We first mark all variables that appear in "bset" as belonging
2469 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2471 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2472 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2474 int *group;
2475 int dim;
2476 int i, j, n;
2478 if (!context || !ineq)
2479 return isl_basic_set_free(context);
2481 group = alloc_groups(context);
2483 if (!group)
2484 return isl_basic_set_free(context);
2486 dim = isl_basic_set_dim(context, isl_dim_set);
2487 n = isl_mat_rows(ineq);
2488 for (i = 0; i < dim; ++i) {
2489 for (j = 0; j < n; ++j) {
2490 if (row[j] < 0)
2491 continue;
2492 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2493 break;
2495 if (j < n)
2496 group[i] = -1;
2499 return isl_basic_map_drop_unrelated_constraints(context, group);
2502 /* Do all "n" entries of "row" contain a negative value?
2504 static int all_neg(int *row, int n)
2506 int i;
2508 for (i = 0; i < n; ++i)
2509 if (row[i] >= 0)
2510 return 0;
2512 return 1;
2515 /* Update the inequalities in "bset" based on the information in "row"
2516 * and "tab".
2518 * In particular, the array "row" contains either -1, meaning that
2519 * the corresponding inequality of "bset" is redundant, or the index
2520 * of an inequality in "tab".
2522 * If the row entry is -1, then drop the inequality.
2523 * Otherwise, if the constraint is marked redundant in the tableau,
2524 * then drop the inequality. Similarly, if it is marked as an equality
2525 * in the tableau, then turn the inequality into an equality and
2526 * perform Gaussian elimination.
2528 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2529 __isl_keep int *row, struct isl_tab *tab)
2531 int i;
2532 unsigned n_ineq;
2533 unsigned n_eq;
2534 int found_equality = 0;
2536 if (!bset)
2537 return NULL;
2538 if (tab && tab->empty)
2539 return isl_basic_set_set_to_empty(bset);
2541 n_ineq = bset->n_ineq;
2542 for (i = n_ineq - 1; i >= 0; --i) {
2543 if (row[i] < 0) {
2544 if (isl_basic_set_drop_inequality(bset, i) < 0)
2545 return isl_basic_set_free(bset);
2546 continue;
2548 if (!tab)
2549 continue;
2550 n_eq = tab->n_eq;
2551 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2552 isl_basic_map_inequality_to_equality(bset, i);
2553 found_equality = 1;
2554 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2555 if (isl_basic_set_drop_inequality(bset, i) < 0)
2556 return isl_basic_set_free(bset);
2560 if (found_equality)
2561 bset = isl_basic_set_gauss(bset, NULL);
2562 bset = isl_basic_set_finalize(bset);
2563 return bset;
2566 /* Update the inequalities in "bset" based on the information in "row"
2567 * and "tab" and free all arguments (other than "bset").
2569 static __isl_give isl_basic_set *update_ineq_free(
2570 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2571 __isl_take isl_basic_set *context, __isl_take int *row,
2572 struct isl_tab *tab)
2574 isl_mat_free(ineq);
2575 isl_basic_set_free(context);
2577 bset = update_ineq(bset, row, tab);
2579 free(row);
2580 isl_tab_free(tab);
2581 return bset;
2584 /* Remove all information from bset that is redundant in the context
2585 * of context.
2586 * "ineq" contains the (possibly transformed) inequalities of "bset",
2587 * in the same order.
2588 * The (explicit) equalities of "bset" are assumed to have been taken
2589 * into account by the transformation such that only the inequalities
2590 * are relevant.
2591 * "context" is assumed not to be empty.
2593 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2594 * A value of -1 means that the inequality is obviously redundant and may
2595 * not even appear in "tab".
2597 * We first mark the inequalities of "bset"
2598 * that are obviously redundant with respect to some inequality in "context".
2599 * Then we remove those constraints from "context" that have become
2600 * irrelevant for computing the gist of "bset".
2601 * Note that this removal of constraints cannot be replaced by
2602 * a factorization because factors in "bset" may still be connected
2603 * to each other through constraints in "context".
2605 * If there are any inequalities left, we construct a tableau for
2606 * the context and then add the inequalities of "bset".
2607 * Before adding these inequalities, we freeze all constraints such that
2608 * they won't be considered redundant in terms of the constraints of "bset".
2609 * Then we detect all redundant constraints (among the
2610 * constraints that weren't frozen), first by checking for redundancy in the
2611 * the tableau and then by checking if replacing a constraint by its negation
2612 * would lead to an empty set. This last step is fairly expensive
2613 * and could be optimized by more reuse of the tableau.
2614 * Finally, we update bset according to the results.
2616 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2617 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2619 int i, r;
2620 int *row = NULL;
2621 isl_ctx *ctx;
2622 isl_basic_set *combined = NULL;
2623 struct isl_tab *tab = NULL;
2624 unsigned n_eq, context_ineq;
2625 unsigned total;
2627 if (!bset || !ineq || !context)
2628 goto error;
2630 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2631 isl_basic_set_free(context);
2632 isl_mat_free(ineq);
2633 return bset;
2636 ctx = isl_basic_set_get_ctx(context);
2637 row = isl_calloc_array(ctx, int, bset->n_ineq);
2638 if (!row)
2639 goto error;
2641 if (mark_shifted_constraints(ineq, context, row) < 0)
2642 goto error;
2643 if (all_neg(row, bset->n_ineq))
2644 return update_ineq_free(bset, ineq, context, row, NULL);
2646 context = drop_irrelevant_constraints_marked(context, ineq, row);
2647 if (!context)
2648 goto error;
2649 if (isl_basic_set_plain_is_universe(context))
2650 return update_ineq_free(bset, ineq, context, row, NULL);
2652 n_eq = context->n_eq;
2653 context_ineq = context->n_ineq;
2654 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2655 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2656 tab = isl_tab_from_basic_set(combined, 0);
2657 for (i = 0; i < context_ineq; ++i)
2658 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2659 goto error;
2660 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2661 goto error;
2662 r = context_ineq;
2663 for (i = 0; i < bset->n_ineq; ++i) {
2664 if (row[i] < 0)
2665 continue;
2666 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2667 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2668 goto error;
2669 row[i] = r++;
2671 if (isl_tab_detect_implicit_equalities(tab) < 0)
2672 goto error;
2673 if (isl_tab_detect_redundant(tab) < 0)
2674 goto error;
2675 total = isl_basic_set_total_dim(bset);
2676 for (i = bset->n_ineq - 1; i >= 0; --i) {
2677 isl_basic_set *test;
2678 int is_empty;
2680 if (row[i] < 0)
2681 continue;
2682 r = row[i];
2683 if (tab->con[n_eq + r].is_redundant)
2684 continue;
2685 test = isl_basic_set_dup(combined);
2686 if (isl_inequality_negate(test, r) < 0)
2687 test = isl_basic_set_free(test);
2688 test = isl_basic_set_update_from_tab(test, tab);
2689 is_empty = isl_basic_set_is_empty(test);
2690 isl_basic_set_free(test);
2691 if (is_empty < 0)
2692 goto error;
2693 if (is_empty)
2694 tab->con[n_eq + r].is_redundant = 1;
2696 bset = update_ineq_free(bset, ineq, context, row, tab);
2697 if (bset) {
2698 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2699 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2702 isl_basic_set_free(combined);
2703 return bset;
2704 error:
2705 free(row);
2706 isl_mat_free(ineq);
2707 isl_tab_free(tab);
2708 isl_basic_set_free(combined);
2709 isl_basic_set_free(context);
2710 isl_basic_set_free(bset);
2711 return NULL;
2714 /* Extract the inequalities of "bset" as an isl_mat.
2716 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2718 unsigned total;
2719 isl_ctx *ctx;
2720 isl_mat *ineq;
2722 if (!bset)
2723 return NULL;
2725 ctx = isl_basic_set_get_ctx(bset);
2726 total = isl_basic_set_total_dim(bset);
2727 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2728 0, 1 + total);
2730 return ineq;
2733 /* Remove all information from "bset" that is redundant in the context
2734 * of "context", for the case where both "bset" and "context" are
2735 * full-dimensional.
2737 static __isl_give isl_basic_set *uset_gist_uncompressed(
2738 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2740 isl_mat *ineq;
2742 ineq = extract_ineq(bset);
2743 return uset_gist_full(bset, ineq, context);
2746 /* Remove all information from "bset" that is redundant in the context
2747 * of "context", for the case where the combined equalities of
2748 * "bset" and "context" allow for a compression that can be obtained
2749 * by preapplication of "T".
2751 * "bset" itself is not transformed by "T". Instead, the inequalities
2752 * are extracted from "bset" and those are transformed by "T".
2753 * uset_gist_full then determines which of the transformed inequalities
2754 * are redundant with respect to the transformed "context" and removes
2755 * the corresponding inequalities from "bset".
2757 * After preapplying "T" to the inequalities, any common factor is
2758 * removed from the coefficients. If this results in a tightening
2759 * of the constant term, then the same tightening is applied to
2760 * the corresponding untransformed inequality in "bset".
2761 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2763 * g f'(x) + r >= 0
2765 * with 0 <= r < g, then it is equivalent to
2767 * f'(x) >= 0
2769 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2770 * subspace compressed by T since the latter would be transformed to
2772 * g f'(x) >= 0
2774 static __isl_give isl_basic_set *uset_gist_compressed(
2775 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2776 __isl_take isl_mat *T)
2778 isl_ctx *ctx;
2779 isl_mat *ineq;
2780 int i, n_row, n_col;
2781 isl_int rem;
2783 ineq = extract_ineq(bset);
2784 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2785 context = isl_basic_set_preimage(context, T);
2787 if (!ineq || !context)
2788 goto error;
2789 if (isl_basic_set_plain_is_empty(context)) {
2790 isl_mat_free(ineq);
2791 isl_basic_set_free(context);
2792 return isl_basic_set_set_to_empty(bset);
2795 ctx = isl_mat_get_ctx(ineq);
2796 n_row = isl_mat_rows(ineq);
2797 n_col = isl_mat_cols(ineq);
2798 isl_int_init(rem);
2799 for (i = 0; i < n_row; ++i) {
2800 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2801 if (isl_int_is_zero(ctx->normalize_gcd))
2802 continue;
2803 if (isl_int_is_one(ctx->normalize_gcd))
2804 continue;
2805 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2806 ctx->normalize_gcd, n_col - 1);
2807 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2808 isl_int_fdiv_q(ineq->row[i][0],
2809 ineq->row[i][0], ctx->normalize_gcd);
2810 if (isl_int_is_zero(rem))
2811 continue;
2812 bset = isl_basic_set_cow(bset);
2813 if (!bset)
2814 break;
2815 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2817 isl_int_clear(rem);
2819 return uset_gist_full(bset, ineq, context);
2820 error:
2821 isl_mat_free(ineq);
2822 isl_basic_set_free(context);
2823 isl_basic_set_free(bset);
2824 return NULL;
2827 /* Project "bset" onto the variables that are involved in "template".
2829 static __isl_give isl_basic_set *project_onto_involved(
2830 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2832 int i, n;
2834 if (!bset || !template)
2835 return isl_basic_set_free(bset);
2837 n = isl_basic_set_dim(template, isl_dim_set);
2839 for (i = 0; i < n; ++i) {
2840 isl_bool involved;
2842 involved = isl_basic_set_involves_dims(template,
2843 isl_dim_set, i, 1);
2844 if (involved < 0)
2845 return isl_basic_set_free(bset);
2846 if (involved)
2847 continue;
2848 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2851 return bset;
2854 /* Remove all information from bset that is redundant in the context
2855 * of context. In particular, equalities that are linear combinations
2856 * of those in context are removed. Then the inequalities that are
2857 * redundant in the context of the equalities and inequalities of
2858 * context are removed.
2860 * First of all, we drop those constraints from "context"
2861 * that are irrelevant for computing the gist of "bset".
2862 * Alternatively, we could factorize the intersection of "context" and "bset".
2864 * We first compute the intersection of the integer affine hulls
2865 * of "bset" and "context",
2866 * compute the gist inside this intersection and then reduce
2867 * the constraints with respect to the equalities of the context
2868 * that only involve variables already involved in the input.
2870 * If two constraints are mutually redundant, then uset_gist_full
2871 * will remove the second of those constraints. We therefore first
2872 * sort the constraints so that constraints not involving existentially
2873 * quantified variables are given precedence over those that do.
2874 * We have to perform this sorting before the variable compression,
2875 * because that may effect the order of the variables.
2877 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2878 __isl_take isl_basic_set *context)
2880 isl_mat *eq;
2881 isl_mat *T;
2882 isl_basic_set *aff;
2883 isl_basic_set *aff_context;
2884 unsigned total;
2886 if (!bset || !context)
2887 goto error;
2889 context = drop_irrelevant_constraints(context, bset);
2891 bset = isl_basic_set_detect_equalities(bset);
2892 aff = isl_basic_set_copy(bset);
2893 aff = isl_basic_set_plain_affine_hull(aff);
2894 context = isl_basic_set_detect_equalities(context);
2895 aff_context = isl_basic_set_copy(context);
2896 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2897 aff = isl_basic_set_intersect(aff, aff_context);
2898 if (!aff)
2899 goto error;
2900 if (isl_basic_set_plain_is_empty(aff)) {
2901 isl_basic_set_free(bset);
2902 isl_basic_set_free(context);
2903 return aff;
2905 bset = isl_basic_set_sort_constraints(bset);
2906 if (aff->n_eq == 0) {
2907 isl_basic_set_free(aff);
2908 return uset_gist_uncompressed(bset, context);
2910 total = isl_basic_set_total_dim(bset);
2911 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2912 eq = isl_mat_cow(eq);
2913 T = isl_mat_variable_compression(eq, NULL);
2914 isl_basic_set_free(aff);
2915 if (T && T->n_col == 0) {
2916 isl_mat_free(T);
2917 isl_basic_set_free(context);
2918 return isl_basic_set_set_to_empty(bset);
2921 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2922 aff_context = project_onto_involved(aff_context, bset);
2924 bset = uset_gist_compressed(bset, context, T);
2925 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2927 if (bset) {
2928 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2929 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2932 return bset;
2933 error:
2934 isl_basic_set_free(bset);
2935 isl_basic_set_free(context);
2936 return NULL;
2939 /* Return the number of equality constraints in "bmap" that involve
2940 * local variables. This function assumes that Gaussian elimination
2941 * has been applied to the equality constraints.
2943 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2945 int i;
2946 int total, n_div;
2948 if (!bmap)
2949 return -1;
2951 if (bmap->n_eq == 0)
2952 return 0;
2954 total = isl_basic_map_dim(bmap, isl_dim_all);
2955 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2956 total -= n_div;
2958 for (i = 0; i < bmap->n_eq; ++i)
2959 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2960 n_div) == -1)
2961 return i;
2963 return bmap->n_eq;
2966 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2967 * The constraints are assumed not to involve any local variables.
2969 static __isl_give isl_basic_map *basic_map_from_equalities(
2970 __isl_take isl_space *space, __isl_take isl_mat *eq)
2972 int i, k;
2973 isl_basic_map *bmap = NULL;
2975 if (!space || !eq)
2976 goto error;
2978 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2979 isl_die(isl_space_get_ctx(space), isl_error_internal,
2980 "unexpected number of columns", goto error);
2982 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2983 0, eq->n_row, 0);
2984 for (i = 0; i < eq->n_row; ++i) {
2985 k = isl_basic_map_alloc_equality(bmap);
2986 if (k < 0)
2987 goto error;
2988 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2991 isl_space_free(space);
2992 isl_mat_free(eq);
2993 return bmap;
2994 error:
2995 isl_space_free(space);
2996 isl_mat_free(eq);
2997 isl_basic_map_free(bmap);
2998 return NULL;
3001 /* Construct and return a variable compression based on the equality
3002 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3003 * "n1" is the number of (initial) equality constraints in "bmap1"
3004 * that do involve local variables.
3005 * "n2" is the number of (initial) equality constraints in "bmap2"
3006 * that do involve local variables.
3007 * "total" is the total number of other variables.
3008 * This function assumes that Gaussian elimination
3009 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3010 * such that the equality constraints not involving local variables
3011 * are those that start at "n1" or "n2".
3013 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3014 * then simply compute the compression based on the equality constraints
3015 * in the other basic map.
3016 * Otherwise, combine the equality constraints from both into a new
3017 * basic map such that Gaussian elimination can be applied to this combination
3018 * and then construct a variable compression from the resulting
3019 * equality constraints.
3021 static __isl_give isl_mat *combined_variable_compression(
3022 __isl_keep isl_basic_map *bmap1, int n1,
3023 __isl_keep isl_basic_map *bmap2, int n2, int total)
3025 isl_ctx *ctx;
3026 isl_mat *E1, *E2, *V;
3027 isl_basic_map *bmap;
3029 ctx = isl_basic_map_get_ctx(bmap1);
3030 if (bmap1->n_eq == n1) {
3031 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3032 n2, bmap2->n_eq - n2, 0, 1 + total);
3033 return isl_mat_variable_compression(E2, NULL);
3035 if (bmap2->n_eq == n2) {
3036 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3037 n1, bmap1->n_eq - n1, 0, 1 + total);
3038 return isl_mat_variable_compression(E1, NULL);
3040 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3041 n1, bmap1->n_eq - n1, 0, 1 + total);
3042 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3043 n2, bmap2->n_eq - n2, 0, 1 + total);
3044 E1 = isl_mat_concat(E1, E2);
3045 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
3046 bmap = isl_basic_map_gauss(bmap, NULL);
3047 if (!bmap)
3048 return NULL;
3049 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3050 V = isl_mat_variable_compression(E1, NULL);
3051 isl_basic_map_free(bmap);
3053 return V;
3056 /* Extract the stride constraints from "bmap", compressed
3057 * with respect to both the stride constraints in "context" and
3058 * the remaining equality constraints in both "bmap" and "context".
3059 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3060 * "context_n_eq" is the number of (initial) stride constraints in "context".
3062 * Let x be all variables in "bmap" (and "context") other than the local
3063 * variables. First compute a variable compression
3065 * x = V x'
3067 * based on the non-stride equality constraints in "bmap" and "context".
3068 * Consider the stride constraints of "context",
3070 * A(x) + B(y) = 0
3072 * with y the local variables and plug in the variable compression,
3073 * resulting in
3075 * A(V x') + B(y) = 0
3077 * Use these constraints to compute a parameter compression on x'
3079 * x' = T x''
3081 * Now consider the stride constraints of "bmap"
3083 * C(x) + D(y) = 0
3085 * and plug in x = V*T x''.
3086 * That is, return A = [C*V*T D].
3088 static __isl_give isl_mat *extract_compressed_stride_constraints(
3089 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3090 __isl_keep isl_basic_map *context, int context_n_eq)
3092 int total, n_div;
3093 isl_ctx *ctx;
3094 isl_mat *A, *B, *T, *V;
3096 total = isl_basic_map_dim(context, isl_dim_all);
3097 n_div = isl_basic_map_dim(context, isl_dim_div);
3098 total -= n_div;
3100 ctx = isl_basic_map_get_ctx(bmap);
3102 V = combined_variable_compression(bmap, bmap_n_eq,
3103 context, context_n_eq, total);
3105 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3106 B = isl_mat_sub_alloc6(ctx, context->eq,
3107 0, context_n_eq, 1 + total, n_div);
3108 A = isl_mat_product(A, isl_mat_copy(V));
3109 T = isl_mat_parameter_compression_ext(A, B);
3110 T = isl_mat_product(V, T);
3112 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3113 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3115 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3116 0, bmap_n_eq, 0, 1 + total + n_div);
3117 A = isl_mat_product(A, T);
3119 return A;
3122 /* Remove the prime factors from *g that have an exponent that
3123 * is strictly smaller than the exponent in "c".
3124 * All exponents in *g are known to be smaller than or equal
3125 * to those in "c".
3127 * That is, if *g is equal to
3129 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3131 * and "c" is equal to
3133 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3135 * then update *g to
3137 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3138 * p_n^{e_n * (e_n = f_n)}
3140 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3141 * neither does the gcd of *g and c / *g.
3142 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3143 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3144 * Dividing *g by this gcd therefore strictly reduces the exponent
3145 * of the prime factors that need to be removed, while leaving the
3146 * other prime factors untouched.
3147 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3148 * removes all undesired factors, without removing any others.
3150 static void remove_incomplete_powers(isl_int *g, isl_int c)
3152 isl_int t;
3154 isl_int_init(t);
3155 for (;;) {
3156 isl_int_divexact(t, c, *g);
3157 isl_int_gcd(t, t, *g);
3158 if (isl_int_is_one(t))
3159 break;
3160 isl_int_divexact(*g, *g, t);
3162 isl_int_clear(t);
3165 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3166 * of the same stride constraints in a compressed space that exploits
3167 * all equalities in the context and the other equalities in "bmap".
3169 * If the stride constraints of "bmap" are of the form
3171 * C(x) + D(y) = 0
3173 * then A is of the form
3175 * B(x') + D(y) = 0
3177 * If any of these constraints involves only a single local variable y,
3178 * then the constraint appears as
3180 * f(x) + m y_i = 0
3182 * in "bmap" and as
3184 * h(x') + m y_i = 0
3186 * in "A".
3188 * Let g be the gcd of m and the coefficients of h.
3189 * Then, in particular, g is a divisor of the coefficients of h and
3191 * f(x) = h(x')
3193 * is known to be a multiple of g.
3194 * If some prime factor in m appears with the same exponent in g,
3195 * then it can be removed from m because f(x) is already known
3196 * to be a multiple of g and therefore in particular of this power
3197 * of the prime factors.
3198 * Prime factors that appear with a smaller exponent in g cannot
3199 * be removed from m.
3200 * Let g' be the divisor of g containing all prime factors that
3201 * appear with the same exponent in m and g, then
3203 * f(x) + m y_i = 0
3205 * can be replaced by
3207 * f(x) + m/g' y_i' = 0
3209 * Note that (if g' != 1) this changes the explicit representation
3210 * of y_i to that of y_i', so the integer division at position i
3211 * is marked unknown and later recomputed by a call to
3212 * isl_basic_map_gauss.
3214 static __isl_give isl_basic_map *reduce_stride_constraints(
3215 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3217 int i;
3218 int total, n_div;
3219 int any = 0;
3220 isl_int gcd;
3222 if (!bmap || !A)
3223 return isl_basic_map_free(bmap);
3225 total = isl_basic_map_dim(bmap, isl_dim_all);
3226 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3227 total -= n_div;
3229 isl_int_init(gcd);
3230 for (i = 0; i < n; ++i) {
3231 int div;
3233 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3234 if (div < 0)
3235 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3236 "equality constraints modified unexpectedly",
3237 goto error);
3238 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3239 n_div - div - 1) != -1)
3240 continue;
3241 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3242 goto error;
3243 if (isl_int_is_one(gcd))
3244 continue;
3245 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3246 if (isl_int_is_one(gcd))
3247 continue;
3248 isl_int_divexact(bmap->eq[i][1 + total + div],
3249 bmap->eq[i][1 + total + div], gcd);
3250 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3251 if (!bmap)
3252 goto error;
3253 any = 1;
3255 isl_int_clear(gcd);
3257 if (any)
3258 bmap = isl_basic_map_gauss(bmap, NULL);
3260 return bmap;
3261 error:
3262 isl_int_clear(gcd);
3263 isl_basic_map_free(bmap);
3264 return NULL;
3267 /* Simplify the stride constraints in "bmap" based on
3268 * the remaining equality constraints in "bmap" and all equality
3269 * constraints in "context".
3270 * Only do this if both "bmap" and "context" have stride constraints.
3272 * First extract a copy of the stride constraints in "bmap" in a compressed
3273 * space exploiting all the other equality constraints and then
3274 * use this compressed copy to simplify the original stride constraints.
3276 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3277 __isl_keep isl_basic_map *context)
3279 int bmap_n_eq, context_n_eq;
3280 isl_mat *A;
3282 if (!bmap || !context)
3283 return isl_basic_map_free(bmap);
3285 bmap_n_eq = n_div_eq(bmap);
3286 context_n_eq = n_div_eq(context);
3288 if (bmap_n_eq < 0 || context_n_eq < 0)
3289 return isl_basic_map_free(bmap);
3290 if (bmap_n_eq == 0 || context_n_eq == 0)
3291 return bmap;
3293 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3294 context, context_n_eq);
3295 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3297 isl_mat_free(A);
3299 return bmap;
3302 /* Return a basic map that has the same intersection with "context" as "bmap"
3303 * and that is as "simple" as possible.
3305 * The core computation is performed on the pure constraints.
3306 * When we add back the meaning of the integer divisions, we need
3307 * to (re)introduce the div constraints. If we happen to have
3308 * discovered that some of these integer divisions are equal to
3309 * some affine combination of other variables, then these div
3310 * constraints may end up getting simplified in terms of the equalities,
3311 * resulting in extra inequalities on the other variables that
3312 * may have been removed already or that may not even have been
3313 * part of the input. We try and remove those constraints of
3314 * this form that are most obviously redundant with respect to
3315 * the context. We also remove those div constraints that are
3316 * redundant with respect to the other constraints in the result.
3318 * The stride constraints among the equality constraints in "bmap" are
3319 * also simplified with respecting to the other equality constraints
3320 * in "bmap" and with respect to all equality constraints in "context".
3322 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3323 struct isl_basic_map *context)
3325 isl_basic_set *bset, *eq;
3326 isl_basic_map *eq_bmap;
3327 unsigned total, n_div, extra, n_eq, n_ineq;
3329 if (!bmap || !context)
3330 goto error;
3332 if (isl_basic_map_plain_is_universe(bmap)) {
3333 isl_basic_map_free(context);
3334 return bmap;
3336 if (isl_basic_map_plain_is_empty(context)) {
3337 isl_space *space = isl_basic_map_get_space(bmap);
3338 isl_basic_map_free(bmap);
3339 isl_basic_map_free(context);
3340 return isl_basic_map_universe(space);
3342 if (isl_basic_map_plain_is_empty(bmap)) {
3343 isl_basic_map_free(context);
3344 return bmap;
3347 bmap = isl_basic_map_remove_redundancies(bmap);
3348 context = isl_basic_map_remove_redundancies(context);
3349 if (!context)
3350 goto error;
3352 context = isl_basic_map_align_divs(context, bmap);
3353 n_div = isl_basic_map_dim(context, isl_dim_div);
3354 total = isl_basic_map_dim(bmap, isl_dim_all);
3355 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3357 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3358 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3359 bset = uset_gist(bset,
3360 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3361 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3363 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3364 isl_basic_set_plain_is_empty(bset)) {
3365 isl_basic_map_free(context);
3366 return isl_basic_map_overlying_set(bset, bmap);
3369 n_eq = bset->n_eq;
3370 n_ineq = bset->n_ineq;
3371 eq = isl_basic_set_copy(bset);
3372 eq = isl_basic_set_cow(eq);
3373 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3374 eq = isl_basic_set_free(eq);
3375 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3376 bset = isl_basic_set_free(bset);
3378 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3379 eq_bmap = gist_strides(eq_bmap, context);
3380 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3381 bmap = isl_basic_map_overlying_set(bset, bmap);
3382 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3383 bmap = isl_basic_map_remove_redundancies(bmap);
3385 return bmap;
3386 error:
3387 isl_basic_map_free(bmap);
3388 isl_basic_map_free(context);
3389 return NULL;
3393 * Assumes context has no implicit divs.
3395 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3396 __isl_take isl_basic_map *context)
3398 int i;
3400 if (!map || !context)
3401 goto error;
3403 if (isl_basic_map_plain_is_empty(context)) {
3404 isl_space *space = isl_map_get_space(map);
3405 isl_map_free(map);
3406 isl_basic_map_free(context);
3407 return isl_map_universe(space);
3410 context = isl_basic_map_remove_redundancies(context);
3411 map = isl_map_cow(map);
3412 if (!map || !context)
3413 goto error;
3414 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3415 map = isl_map_compute_divs(map);
3416 if (!map)
3417 goto error;
3418 for (i = map->n - 1; i >= 0; --i) {
3419 map->p[i] = isl_basic_map_gist(map->p[i],
3420 isl_basic_map_copy(context));
3421 if (!map->p[i])
3422 goto error;
3423 if (isl_basic_map_plain_is_empty(map->p[i])) {
3424 isl_basic_map_free(map->p[i]);
3425 if (i != map->n - 1)
3426 map->p[i] = map->p[map->n - 1];
3427 map->n--;
3430 isl_basic_map_free(context);
3431 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3432 return map;
3433 error:
3434 isl_map_free(map);
3435 isl_basic_map_free(context);
3436 return NULL;
3439 /* Drop all inequalities from "bmap" that also appear in "context".
3440 * "context" is assumed to have only known local variables and
3441 * the initial local variables of "bmap" are assumed to be the same
3442 * as those of "context".
3443 * The constraints of both "bmap" and "context" are assumed
3444 * to have been sorted using isl_basic_map_sort_constraints.
3446 * Run through the inequality constraints of "bmap" and "context"
3447 * in sorted order.
3448 * If a constraint of "bmap" involves variables not in "context",
3449 * then it cannot appear in "context".
3450 * If a matching constraint is found, it is removed from "bmap".
3452 static __isl_give isl_basic_map *drop_inequalities(
3453 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3455 int i1, i2;
3456 unsigned total, extra;
3458 if (!bmap || !context)
3459 return isl_basic_map_free(bmap);
3461 total = isl_basic_map_total_dim(context);
3462 extra = isl_basic_map_total_dim(bmap) - total;
3464 i1 = bmap->n_ineq - 1;
3465 i2 = context->n_ineq - 1;
3466 while (bmap && i1 >= 0 && i2 >= 0) {
3467 int cmp;
3469 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3470 extra) != -1) {
3471 --i1;
3472 continue;
3474 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3475 context->ineq[i2]);
3476 if (cmp < 0) {
3477 --i2;
3478 continue;
3480 if (cmp > 0) {
3481 --i1;
3482 continue;
3484 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3485 bmap = isl_basic_map_cow(bmap);
3486 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3487 bmap = isl_basic_map_free(bmap);
3489 --i1;
3490 --i2;
3493 return bmap;
3496 /* Drop all equalities from "bmap" that also appear in "context".
3497 * "context" is assumed to have only known local variables and
3498 * the initial local variables of "bmap" are assumed to be the same
3499 * as those of "context".
3501 * Run through the equality constraints of "bmap" and "context"
3502 * in sorted order.
3503 * If a constraint of "bmap" involves variables not in "context",
3504 * then it cannot appear in "context".
3505 * If a matching constraint is found, it is removed from "bmap".
3507 static __isl_give isl_basic_map *drop_equalities(
3508 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3510 int i1, i2;
3511 unsigned total, extra;
3513 if (!bmap || !context)
3514 return isl_basic_map_free(bmap);
3516 total = isl_basic_map_total_dim(context);
3517 extra = isl_basic_map_total_dim(bmap) - total;
3519 i1 = bmap->n_eq - 1;
3520 i2 = context->n_eq - 1;
3522 while (bmap && i1 >= 0 && i2 >= 0) {
3523 int last1, last2;
3525 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3526 extra) != -1)
3527 break;
3528 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3529 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3530 if (last1 > last2) {
3531 --i2;
3532 continue;
3534 if (last1 < last2) {
3535 --i1;
3536 continue;
3538 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3539 bmap = isl_basic_map_cow(bmap);
3540 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3541 bmap = isl_basic_map_free(bmap);
3543 --i1;
3544 --i2;
3547 return bmap;
3550 /* Remove the constraints in "context" from "bmap".
3551 * "context" is assumed to have explicit representations
3552 * for all local variables.
3554 * First align the divs of "bmap" to those of "context" and
3555 * sort the constraints. Then drop all constraints from "bmap"
3556 * that appear in "context".
3558 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3559 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3561 isl_bool done, known;
3563 done = isl_basic_map_plain_is_universe(context);
3564 if (done == isl_bool_false)
3565 done = isl_basic_map_plain_is_universe(bmap);
3566 if (done == isl_bool_false)
3567 done = isl_basic_map_plain_is_empty(context);
3568 if (done == isl_bool_false)
3569 done = isl_basic_map_plain_is_empty(bmap);
3570 if (done < 0)
3571 goto error;
3572 if (done) {
3573 isl_basic_map_free(context);
3574 return bmap;
3576 known = isl_basic_map_divs_known(context);
3577 if (known < 0)
3578 goto error;
3579 if (!known)
3580 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3581 "context has unknown divs", goto error);
3583 bmap = isl_basic_map_align_divs(bmap, context);
3584 bmap = isl_basic_map_gauss(bmap, NULL);
3585 bmap = isl_basic_map_sort_constraints(bmap);
3586 context = isl_basic_map_sort_constraints(context);
3588 bmap = drop_inequalities(bmap, context);
3589 bmap = drop_equalities(bmap, context);
3591 isl_basic_map_free(context);
3592 bmap = isl_basic_map_finalize(bmap);
3593 return bmap;
3594 error:
3595 isl_basic_map_free(bmap);
3596 isl_basic_map_free(context);
3597 return NULL;
3600 /* Replace "map" by the disjunct at position "pos" and free "context".
3602 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3603 int pos, __isl_take isl_basic_map *context)
3605 isl_basic_map *bmap;
3607 bmap = isl_basic_map_copy(map->p[pos]);
3608 isl_map_free(map);
3609 isl_basic_map_free(context);
3610 return isl_map_from_basic_map(bmap);
3613 /* Remove the constraints in "context" from "map".
3614 * If any of the disjuncts in the result turns out to be the universe,
3615 * then return this universe.
3616 * "context" is assumed to have explicit representations
3617 * for all local variables.
3619 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3620 __isl_take isl_basic_map *context)
3622 int i;
3623 isl_bool univ, known;
3625 univ = isl_basic_map_plain_is_universe(context);
3626 if (univ < 0)
3627 goto error;
3628 if (univ) {
3629 isl_basic_map_free(context);
3630 return map;
3632 known = isl_basic_map_divs_known(context);
3633 if (known < 0)
3634 goto error;
3635 if (!known)
3636 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3637 "context has unknown divs", goto error);
3639 map = isl_map_cow(map);
3640 if (!map)
3641 goto error;
3642 for (i = 0; i < map->n; ++i) {
3643 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3644 isl_basic_map_copy(context));
3645 univ = isl_basic_map_plain_is_universe(map->p[i]);
3646 if (univ < 0)
3647 goto error;
3648 if (univ && map->n > 1)
3649 return replace_by_disjunct(map, i, context);
3652 isl_basic_map_free(context);
3653 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3654 if (map->n > 1)
3655 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3656 return map;
3657 error:
3658 isl_map_free(map);
3659 isl_basic_map_free(context);
3660 return NULL;
3663 /* Replace "map" by a universe map in the same space and free "drop".
3665 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3666 __isl_take isl_map *drop)
3668 isl_map *res;
3670 res = isl_map_universe(isl_map_get_space(map));
3671 isl_map_free(map);
3672 isl_map_free(drop);
3673 return res;
3676 /* Return a map that has the same intersection with "context" as "map"
3677 * and that is as "simple" as possible.
3679 * If "map" is already the universe, then we cannot make it any simpler.
3680 * Similarly, if "context" is the universe, then we cannot exploit it
3681 * to simplify "map"
3682 * If "map" and "context" are identical to each other, then we can
3683 * return the corresponding universe.
3685 * If either "map" or "context" consists of multiple disjuncts,
3686 * then check if "context" happens to be a subset of "map",
3687 * in which case all constraints can be removed.
3688 * In case of multiple disjuncts, the standard procedure
3689 * may not be able to detect that all constraints can be removed.
3691 * If none of these cases apply, we have to work a bit harder.
3692 * During this computation, we make use of a single disjunct context,
3693 * so if the original context consists of more than one disjunct
3694 * then we need to approximate the context by a single disjunct set.
3695 * Simply taking the simple hull may drop constraints that are
3696 * only implicitly available in each disjunct. We therefore also
3697 * look for constraints among those defining "map" that are valid
3698 * for the context. These can then be used to simplify away
3699 * the corresponding constraints in "map".
3701 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3702 __isl_take isl_map *context)
3704 int equal;
3705 int is_universe;
3706 int single_disjunct_map, single_disjunct_context;
3707 isl_bool subset;
3708 isl_basic_map *hull;
3710 is_universe = isl_map_plain_is_universe(map);
3711 if (is_universe >= 0 && !is_universe)
3712 is_universe = isl_map_plain_is_universe(context);
3713 if (is_universe < 0)
3714 goto error;
3715 if (is_universe) {
3716 isl_map_free(context);
3717 return map;
3720 equal = isl_map_plain_is_equal(map, context);
3721 if (equal < 0)
3722 goto error;
3723 if (equal)
3724 return replace_by_universe(map, context);
3726 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3727 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3728 if (!single_disjunct_map || !single_disjunct_context) {
3729 subset = isl_map_is_subset(context, map);
3730 if (subset < 0)
3731 goto error;
3732 if (subset)
3733 return replace_by_universe(map, context);
3736 context = isl_map_compute_divs(context);
3737 if (!context)
3738 goto error;
3739 if (single_disjunct_context) {
3740 hull = isl_map_simple_hull(context);
3741 } else {
3742 isl_ctx *ctx;
3743 isl_map_list *list;
3745 ctx = isl_map_get_ctx(map);
3746 list = isl_map_list_alloc(ctx, 2);
3747 list = isl_map_list_add(list, isl_map_copy(context));
3748 list = isl_map_list_add(list, isl_map_copy(map));
3749 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3750 list);
3752 return isl_map_gist_basic_map(map, hull);
3753 error:
3754 isl_map_free(map);
3755 isl_map_free(context);
3756 return NULL;
3759 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3760 __isl_take isl_map *context)
3762 return isl_map_align_params_map_map_and(map, context, &map_gist);
3765 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3766 struct isl_basic_set *context)
3768 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3769 bset_to_bmap(context)));
3772 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3773 __isl_take isl_basic_set *context)
3775 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3776 bset_to_bmap(context)));
3779 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3780 __isl_take isl_basic_set *context)
3782 isl_space *space = isl_set_get_space(set);
3783 isl_basic_set *dom_context = isl_basic_set_universe(space);
3784 dom_context = isl_basic_set_intersect_params(dom_context, context);
3785 return isl_set_gist_basic_set(set, dom_context);
3788 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3789 __isl_take isl_set *context)
3791 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3794 /* Compute the gist of "bmap" with respect to the constraints "context"
3795 * on the domain.
3797 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3798 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3800 isl_space *space = isl_basic_map_get_space(bmap);
3801 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3803 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3804 return isl_basic_map_gist(bmap, bmap_context);
3807 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3808 __isl_take isl_set *context)
3810 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3811 map_context = isl_map_intersect_domain(map_context, context);
3812 return isl_map_gist(map, map_context);
3815 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3816 __isl_take isl_set *context)
3818 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3819 map_context = isl_map_intersect_range(map_context, context);
3820 return isl_map_gist(map, map_context);
3823 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3824 __isl_take isl_set *context)
3826 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3827 map_context = isl_map_intersect_params(map_context, context);
3828 return isl_map_gist(map, map_context);
3831 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3832 __isl_take isl_set *context)
3834 return isl_map_gist_params(set, context);
3837 /* Quick check to see if two basic maps are disjoint.
3838 * In particular, we reduce the equalities and inequalities of
3839 * one basic map in the context of the equalities of the other
3840 * basic map and check if we get a contradiction.
3842 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3843 __isl_keep isl_basic_map *bmap2)
3845 struct isl_vec *v = NULL;
3846 int *elim = NULL;
3847 unsigned total;
3848 int i;
3850 if (!bmap1 || !bmap2)
3851 return isl_bool_error;
3852 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3853 return isl_bool_error);
3854 if (bmap1->n_div || bmap2->n_div)
3855 return isl_bool_false;
3856 if (!bmap1->n_eq && !bmap2->n_eq)
3857 return isl_bool_false;
3859 total = isl_space_dim(bmap1->dim, isl_dim_all);
3860 if (total == 0)
3861 return isl_bool_false;
3862 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3863 if (!v)
3864 goto error;
3865 elim = isl_alloc_array(bmap1->ctx, int, total);
3866 if (!elim)
3867 goto error;
3868 compute_elimination_index(bmap1, elim);
3869 for (i = 0; i < bmap2->n_eq; ++i) {
3870 int reduced;
3871 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3872 bmap1, elim);
3873 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3874 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3875 goto disjoint;
3877 for (i = 0; i < bmap2->n_ineq; ++i) {
3878 int reduced;
3879 reduced = reduced_using_equalities(v->block.data,
3880 bmap2->ineq[i], bmap1, elim);
3881 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3882 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3883 goto disjoint;
3885 compute_elimination_index(bmap2, elim);
3886 for (i = 0; i < bmap1->n_ineq; ++i) {
3887 int reduced;
3888 reduced = reduced_using_equalities(v->block.data,
3889 bmap1->ineq[i], bmap2, elim);
3890 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3891 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3892 goto disjoint;
3894 isl_vec_free(v);
3895 free(elim);
3896 return isl_bool_false;
3897 disjoint:
3898 isl_vec_free(v);
3899 free(elim);
3900 return isl_bool_true;
3901 error:
3902 isl_vec_free(v);
3903 free(elim);
3904 return isl_bool_error;
3907 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3908 __isl_keep isl_basic_set *bset2)
3910 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3911 bset_to_bmap(bset2));
3914 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3916 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3917 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3918 __isl_keep isl_basic_map *bmap2))
3920 int i, j;
3922 if (!map1 || !map2)
3923 return isl_bool_error;
3925 for (i = 0; i < map1->n; ++i) {
3926 for (j = 0; j < map2->n; ++j) {
3927 isl_bool d = test(map1->p[i], map2->p[j]);
3928 if (d != isl_bool_true)
3929 return d;
3933 return isl_bool_true;
3936 /* Are "map1" and "map2" obviously disjoint, based on information
3937 * that can be derived without looking at the individual basic maps?
3939 * In particular, if one of them is empty or if they live in different spaces
3940 * (ignoring parameters), then they are clearly disjoint.
3942 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3943 __isl_keep isl_map *map2)
3945 isl_bool disjoint;
3946 isl_bool match;
3948 if (!map1 || !map2)
3949 return isl_bool_error;
3951 disjoint = isl_map_plain_is_empty(map1);
3952 if (disjoint < 0 || disjoint)
3953 return disjoint;
3955 disjoint = isl_map_plain_is_empty(map2);
3956 if (disjoint < 0 || disjoint)
3957 return disjoint;
3959 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3960 map2->dim, isl_dim_in);
3961 if (match < 0 || !match)
3962 return match < 0 ? isl_bool_error : isl_bool_true;
3964 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3965 map2->dim, isl_dim_out);
3966 if (match < 0 || !match)
3967 return match < 0 ? isl_bool_error : isl_bool_true;
3969 return isl_bool_false;
3972 /* Are "map1" and "map2" obviously disjoint?
3974 * If one of them is empty or if they live in different spaces (ignoring
3975 * parameters), then they are clearly disjoint.
3976 * This is checked by isl_map_plain_is_disjoint_global.
3978 * If they have different parameters, then we skip any further tests.
3980 * If they are obviously equal, but not obviously empty, then we will
3981 * not be able to detect if they are disjoint.
3983 * Otherwise we check if each basic map in "map1" is obviously disjoint
3984 * from each basic map in "map2".
3986 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3987 __isl_keep isl_map *map2)
3989 isl_bool disjoint;
3990 isl_bool intersect;
3991 isl_bool match;
3993 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3994 if (disjoint < 0 || disjoint)
3995 return disjoint;
3997 match = isl_space_match(map1->dim, isl_dim_param,
3998 map2->dim, isl_dim_param);
3999 if (match < 0 || !match)
4000 return match < 0 ? isl_bool_error : isl_bool_false;
4002 intersect = isl_map_plain_is_equal(map1, map2);
4003 if (intersect < 0 || intersect)
4004 return intersect < 0 ? isl_bool_error : isl_bool_false;
4006 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
4009 /* Are "map1" and "map2" disjoint?
4011 * They are disjoint if they are "obviously disjoint" or if one of them
4012 * is empty. Otherwise, they are not disjoint if one of them is universal.
4013 * If the two inputs are (obviously) equal and not empty, then they are
4014 * not disjoint.
4015 * If none of these cases apply, then check if all pairs of basic maps
4016 * are disjoint.
4018 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
4020 isl_bool disjoint;
4021 isl_bool intersect;
4023 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
4024 if (disjoint < 0 || disjoint)
4025 return disjoint;
4027 disjoint = isl_map_is_empty(map1);
4028 if (disjoint < 0 || disjoint)
4029 return disjoint;
4031 disjoint = isl_map_is_empty(map2);
4032 if (disjoint < 0 || disjoint)
4033 return disjoint;
4035 intersect = isl_map_plain_is_universe(map1);
4036 if (intersect < 0 || intersect)
4037 return intersect < 0 ? isl_bool_error : isl_bool_false;
4039 intersect = isl_map_plain_is_universe(map2);
4040 if (intersect < 0 || intersect)
4041 return intersect < 0 ? isl_bool_error : isl_bool_false;
4043 intersect = isl_map_plain_is_equal(map1, map2);
4044 if (intersect < 0 || intersect)
4045 return isl_bool_not(intersect);
4047 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
4050 /* Are "bmap1" and "bmap2" disjoint?
4052 * They are disjoint if they are "obviously disjoint" or if one of them
4053 * is empty. Otherwise, they are not disjoint if one of them is universal.
4054 * If none of these cases apply, we compute the intersection and see if
4055 * the result is empty.
4057 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
4058 __isl_keep isl_basic_map *bmap2)
4060 isl_bool disjoint;
4061 isl_bool intersect;
4062 isl_basic_map *test;
4064 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4065 if (disjoint < 0 || disjoint)
4066 return disjoint;
4068 disjoint = isl_basic_map_is_empty(bmap1);
4069 if (disjoint < 0 || disjoint)
4070 return disjoint;
4072 disjoint = isl_basic_map_is_empty(bmap2);
4073 if (disjoint < 0 || disjoint)
4074 return disjoint;
4076 intersect = isl_basic_map_plain_is_universe(bmap1);
4077 if (intersect < 0 || intersect)
4078 return intersect < 0 ? isl_bool_error : isl_bool_false;
4080 intersect = isl_basic_map_plain_is_universe(bmap2);
4081 if (intersect < 0 || intersect)
4082 return intersect < 0 ? isl_bool_error : isl_bool_false;
4084 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4085 isl_basic_map_copy(bmap2));
4086 disjoint = isl_basic_map_is_empty(test);
4087 isl_basic_map_free(test);
4089 return disjoint;
4092 /* Are "bset1" and "bset2" disjoint?
4094 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4095 __isl_keep isl_basic_set *bset2)
4097 return isl_basic_map_is_disjoint(bset1, bset2);
4100 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4101 __isl_keep isl_set *set2)
4103 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4106 /* Are "set1" and "set2" disjoint?
4108 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4110 return isl_map_is_disjoint(set1, set2);
4113 /* Is "v" equal to 0, 1 or -1?
4115 static int is_zero_or_one(isl_int v)
4117 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4120 /* Check if we can combine a given div with lower bound l and upper
4121 * bound u with some other div and if so return that other div.
4122 * Otherwise return -1.
4124 * We first check that
4125 * - the bounds are opposites of each other (except for the constant
4126 * term)
4127 * - the bounds do not reference any other div
4128 * - no div is defined in terms of this div
4130 * Let m be the size of the range allowed on the div by the bounds.
4131 * That is, the bounds are of the form
4133 * e <= a <= e + m - 1
4135 * with e some expression in the other variables.
4136 * We look for another div b such that no third div is defined in terms
4137 * of this second div b and such that in any constraint that contains
4138 * a (except for the given lower and upper bound), also contains b
4139 * with a coefficient that is m times that of b.
4140 * That is, all constraints (execpt for the lower and upper bound)
4141 * are of the form
4143 * e + f (a + m b) >= 0
4145 * Furthermore, in the constraints that only contain b, the coefficient
4146 * of b should be equal to 1 or -1.
4147 * If so, we return b so that "a + m b" can be replaced by
4148 * a single div "c = a + m b".
4150 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4151 unsigned div, unsigned l, unsigned u)
4153 int i, j;
4154 unsigned dim;
4155 int coalesce = -1;
4157 if (bmap->n_div <= 1)
4158 return -1;
4159 dim = isl_space_dim(bmap->dim, isl_dim_all);
4160 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4161 return -1;
4162 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4163 bmap->n_div - div - 1) != -1)
4164 return -1;
4165 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4166 dim + bmap->n_div))
4167 return -1;
4169 for (i = 0; i < bmap->n_div; ++i) {
4170 if (isl_int_is_zero(bmap->div[i][0]))
4171 continue;
4172 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4173 return -1;
4176 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4177 if (isl_int_is_neg(bmap->ineq[l][0])) {
4178 isl_int_sub(bmap->ineq[l][0],
4179 bmap->ineq[l][0], bmap->ineq[u][0]);
4180 bmap = isl_basic_map_copy(bmap);
4181 bmap = isl_basic_map_set_to_empty(bmap);
4182 isl_basic_map_free(bmap);
4183 return -1;
4185 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4186 for (i = 0; i < bmap->n_div; ++i) {
4187 if (i == div)
4188 continue;
4189 if (!pairs[i])
4190 continue;
4191 for (j = 0; j < bmap->n_div; ++j) {
4192 if (isl_int_is_zero(bmap->div[j][0]))
4193 continue;
4194 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4195 break;
4197 if (j < bmap->n_div)
4198 continue;
4199 for (j = 0; j < bmap->n_ineq; ++j) {
4200 int valid;
4201 if (j == l || j == u)
4202 continue;
4203 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4204 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4205 continue;
4206 break;
4208 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4209 break;
4210 isl_int_mul(bmap->ineq[j][1 + dim + div],
4211 bmap->ineq[j][1 + dim + div],
4212 bmap->ineq[l][0]);
4213 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4214 bmap->ineq[j][1 + dim + i]);
4215 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4216 bmap->ineq[j][1 + dim + div],
4217 bmap->ineq[l][0]);
4218 if (!valid)
4219 break;
4221 if (j < bmap->n_ineq)
4222 continue;
4223 coalesce = i;
4224 break;
4226 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4227 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4228 return coalesce;
4231 /* Internal data structure used during the construction and/or evaluation of
4232 * an inequality that ensures that a pair of bounds always allows
4233 * for an integer value.
4235 * "tab" is the tableau in which the inequality is evaluated. It may
4236 * be NULL until it is actually needed.
4237 * "v" contains the inequality coefficients.
4238 * "g", "fl" and "fu" are temporary scalars used during the construction and
4239 * evaluation.
4241 struct test_ineq_data {
4242 struct isl_tab *tab;
4243 isl_vec *v;
4244 isl_int g;
4245 isl_int fl;
4246 isl_int fu;
4249 /* Free all the memory allocated by the fields of "data".
4251 static void test_ineq_data_clear(struct test_ineq_data *data)
4253 isl_tab_free(data->tab);
4254 isl_vec_free(data->v);
4255 isl_int_clear(data->g);
4256 isl_int_clear(data->fl);
4257 isl_int_clear(data->fu);
4260 /* Is the inequality stored in data->v satisfied by "bmap"?
4261 * That is, does it only attain non-negative values?
4262 * data->tab is a tableau corresponding to "bmap".
4264 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4265 struct test_ineq_data *data)
4267 isl_ctx *ctx;
4268 enum isl_lp_result res;
4270 ctx = isl_basic_map_get_ctx(bmap);
4271 if (!data->tab)
4272 data->tab = isl_tab_from_basic_map(bmap, 0);
4273 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4274 if (res == isl_lp_error)
4275 return isl_bool_error;
4276 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4279 /* Given a lower and an upper bound on div i, do they always allow
4280 * for an integer value of the given div?
4281 * Determine this property by constructing an inequality
4282 * such that the property is guaranteed when the inequality is nonnegative.
4283 * The lower bound is inequality l, while the upper bound is inequality u.
4284 * The constructed inequality is stored in data->v.
4286 * Let the upper bound be
4288 * -n_u a + e_u >= 0
4290 * and the lower bound
4292 * n_l a + e_l >= 0
4294 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4295 * We have
4297 * - f_u e_l <= f_u f_l g a <= f_l e_u
4299 * Since all variables are integer valued, this is equivalent to
4301 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4303 * If this interval is at least f_u f_l g, then it contains at least
4304 * one integer value for a.
4305 * That is, the test constraint is
4307 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4309 * or
4311 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4313 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4314 * then the constraint can be scaled down by a factor g',
4315 * with the constant term replaced by
4316 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4317 * Note that the result of applying Fourier-Motzkin to this pair
4318 * of constraints is
4320 * f_l e_u + f_u e_l >= 0
4322 * If the constant term of the scaled down version of this constraint,
4323 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4324 * term of the scaled down test constraint, then the test constraint
4325 * is known to hold and no explicit evaluation is required.
4326 * This is essentially the Omega test.
4328 * If the test constraint consists of only a constant term, then
4329 * it is sufficient to look at the sign of this constant term.
4331 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4332 int l, int u, struct test_ineq_data *data)
4334 unsigned offset, n_div;
4335 offset = isl_basic_map_offset(bmap, isl_dim_div);
4336 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4338 isl_int_gcd(data->g,
4339 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4340 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4341 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4342 isl_int_neg(data->fu, data->fu);
4343 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4344 data->fu, bmap->ineq[l], offset + n_div);
4345 isl_int_mul(data->g, data->g, data->fl);
4346 isl_int_mul(data->g, data->g, data->fu);
4347 isl_int_sub(data->g, data->g, data->fl);
4348 isl_int_sub(data->g, data->g, data->fu);
4349 isl_int_add_ui(data->g, data->g, 1);
4350 isl_int_sub(data->fl, data->v->el[0], data->g);
4352 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4353 if (isl_int_is_zero(data->g))
4354 return isl_int_is_nonneg(data->fl);
4355 if (isl_int_is_one(data->g)) {
4356 isl_int_set(data->v->el[0], data->fl);
4357 return test_ineq_is_satisfied(bmap, data);
4359 isl_int_fdiv_q(data->fl, data->fl, data->g);
4360 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4361 if (isl_int_eq(data->fl, data->v->el[0]))
4362 return isl_bool_true;
4363 isl_int_set(data->v->el[0], data->fl);
4364 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4365 offset - 1 + n_div);
4367 return test_ineq_is_satisfied(bmap, data);
4370 /* Remove more kinds of divs that are not strictly needed.
4371 * In particular, if all pairs of lower and upper bounds on a div
4372 * are such that they allow at least one integer value of the div,
4373 * then we can eliminate the div using Fourier-Motzkin without
4374 * introducing any spurious solutions.
4376 * If at least one of the two constraints has a unit coefficient for the div,
4377 * then the presence of such a value is guaranteed so there is no need to check.
4378 * In particular, the value attained by the bound with unit coefficient
4379 * can serve as this intermediate value.
4381 static struct isl_basic_map *drop_more_redundant_divs(
4382 struct isl_basic_map *bmap, int *pairs, int n)
4384 isl_ctx *ctx;
4385 struct test_ineq_data data = { NULL, NULL };
4386 unsigned off, n_div;
4387 int remove = -1;
4389 isl_int_init(data.g);
4390 isl_int_init(data.fl);
4391 isl_int_init(data.fu);
4393 if (!bmap)
4394 goto error;
4396 ctx = isl_basic_map_get_ctx(bmap);
4397 off = isl_basic_map_offset(bmap, isl_dim_div);
4398 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4399 data.v = isl_vec_alloc(ctx, off + n_div);
4400 if (!data.v)
4401 goto error;
4403 while (n > 0) {
4404 int i, l, u;
4405 int best = -1;
4406 isl_bool has_int;
4408 for (i = 0; i < n_div; ++i) {
4409 if (!pairs[i])
4410 continue;
4411 if (best >= 0 && pairs[best] <= pairs[i])
4412 continue;
4413 best = i;
4416 i = best;
4417 for (l = 0; l < bmap->n_ineq; ++l) {
4418 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4419 continue;
4420 if (isl_int_is_one(bmap->ineq[l][off + i]))
4421 continue;
4422 for (u = 0; u < bmap->n_ineq; ++u) {
4423 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4424 continue;
4425 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4426 continue;
4427 has_int = int_between_bounds(bmap, i, l, u,
4428 &data);
4429 if (has_int < 0)
4430 goto error;
4431 if (data.tab && data.tab->empty)
4432 break;
4433 if (!has_int)
4434 break;
4436 if (u < bmap->n_ineq)
4437 break;
4439 if (data.tab && data.tab->empty) {
4440 bmap = isl_basic_map_set_to_empty(bmap);
4441 break;
4443 if (l == bmap->n_ineq) {
4444 remove = i;
4445 break;
4447 pairs[i] = 0;
4448 --n;
4451 test_ineq_data_clear(&data);
4453 free(pairs);
4455 if (remove < 0)
4456 return bmap;
4458 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4459 return isl_basic_map_drop_redundant_divs(bmap);
4460 error:
4461 free(pairs);
4462 isl_basic_map_free(bmap);
4463 test_ineq_data_clear(&data);
4464 return NULL;
4467 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4468 * and the upper bound u, div1 always occurs together with div2 in the form
4469 * (div1 + m div2), where m is the constant range on the variable div1
4470 * allowed by l and u, replace the pair div1 and div2 by a single
4471 * div that is equal to div1 + m div2.
4473 * The new div will appear in the location that contains div2.
4474 * We need to modify all constraints that contain
4475 * div2 = (div - div1) / m
4476 * The coefficient of div2 is known to be equal to 1 or -1.
4477 * (If a constraint does not contain div2, it will also not contain div1.)
4478 * If the constraint also contains div1, then we know they appear
4479 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4480 * i.e., the coefficient of div is f.
4482 * Otherwise, we first need to introduce div1 into the constraint.
4483 * Let the l be
4485 * div1 + f >=0
4487 * and u
4489 * -div1 + f' >= 0
4491 * A lower bound on div2
4493 * div2 + t >= 0
4495 * can be replaced by
4497 * m div2 + div1 + m t + f >= 0
4499 * An upper bound
4501 * -div2 + t >= 0
4503 * can be replaced by
4505 * -(m div2 + div1) + m t + f' >= 0
4507 * These constraint are those that we would obtain from eliminating
4508 * div1 using Fourier-Motzkin.
4510 * After all constraints have been modified, we drop the lower and upper
4511 * bound and then drop div1.
4513 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4514 unsigned div1, unsigned div2, unsigned l, unsigned u)
4516 isl_ctx *ctx;
4517 isl_int m;
4518 unsigned dim, total;
4519 int i;
4521 ctx = isl_basic_map_get_ctx(bmap);
4523 dim = isl_space_dim(bmap->dim, isl_dim_all);
4524 total = 1 + dim + bmap->n_div;
4526 isl_int_init(m);
4527 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4528 isl_int_add_ui(m, m, 1);
4530 for (i = 0; i < bmap->n_ineq; ++i) {
4531 if (i == l || i == u)
4532 continue;
4533 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4534 continue;
4535 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4536 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4537 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4538 ctx->one, bmap->ineq[l], total);
4539 else
4540 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4541 ctx->one, bmap->ineq[u], total);
4543 isl_int_set(bmap->ineq[i][1 + dim + div2],
4544 bmap->ineq[i][1 + dim + div1]);
4545 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4548 isl_int_clear(m);
4549 if (l > u) {
4550 isl_basic_map_drop_inequality(bmap, l);
4551 isl_basic_map_drop_inequality(bmap, u);
4552 } else {
4553 isl_basic_map_drop_inequality(bmap, u);
4554 isl_basic_map_drop_inequality(bmap, l);
4556 bmap = isl_basic_map_drop_div(bmap, div1);
4557 return bmap;
4560 /* First check if we can coalesce any pair of divs and
4561 * then continue with dropping more redundant divs.
4563 * We loop over all pairs of lower and upper bounds on a div
4564 * with coefficient 1 and -1, respectively, check if there
4565 * is any other div "c" with which we can coalesce the div
4566 * and if so, perform the coalescing.
4568 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4569 struct isl_basic_map *bmap, int *pairs, int n)
4571 int i, l, u;
4572 unsigned dim;
4574 dim = isl_space_dim(bmap->dim, isl_dim_all);
4576 for (i = 0; i < bmap->n_div; ++i) {
4577 if (!pairs[i])
4578 continue;
4579 for (l = 0; l < bmap->n_ineq; ++l) {
4580 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4581 continue;
4582 for (u = 0; u < bmap->n_ineq; ++u) {
4583 int c;
4585 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4586 continue;
4587 c = div_find_coalesce(bmap, pairs, i, l, u);
4588 if (c < 0)
4589 continue;
4590 free(pairs);
4591 bmap = coalesce_divs(bmap, i, c, l, u);
4592 return isl_basic_map_drop_redundant_divs(bmap);
4597 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4598 return bmap;
4600 return drop_more_redundant_divs(bmap, pairs, n);
4603 /* Are the "n" coefficients starting at "first" of inequality constraints
4604 * "i" and "j" of "bmap" equal to each other?
4606 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4607 int first, int n)
4609 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4612 /* Are the "n" coefficients starting at "first" of inequality constraints
4613 * "i" and "j" of "bmap" opposite to each other?
4615 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4616 int first, int n)
4618 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4621 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4622 * apart from the constant term?
4624 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4626 unsigned total;
4628 total = isl_basic_map_dim(bmap, isl_dim_all);
4629 return is_opposite_part(bmap, i, j, 1, total);
4632 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4633 * apart from the constant term and the coefficient at position "pos"?
4635 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4636 int pos)
4638 unsigned total;
4640 total = isl_basic_map_dim(bmap, isl_dim_all);
4641 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4642 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4645 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4646 * apart from the constant term and the coefficient at position "pos"?
4648 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4649 int pos)
4651 unsigned total;
4653 total = isl_basic_map_dim(bmap, isl_dim_all);
4654 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4655 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4658 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4659 * been modified, simplying it if "simplify" is set.
4660 * Free the temporary data structure "pairs" that was associated
4661 * to the old version of "bmap".
4663 static __isl_give isl_basic_map *drop_redundant_divs_again(
4664 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4666 if (simplify)
4667 bmap = isl_basic_map_simplify(bmap);
4668 free(pairs);
4669 return isl_basic_map_drop_redundant_divs(bmap);
4672 /* Is "div" the single unknown existentially quantified variable
4673 * in inequality constraint "ineq" of "bmap"?
4674 * "div" is known to have a non-zero coefficient in "ineq".
4676 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4678 int i;
4679 unsigned n_div, o_div;
4681 if (isl_basic_map_div_is_known(bmap, div))
4682 return 0;
4683 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4684 if (n_div == 1)
4685 return 1;
4686 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4687 for (i = 0; i < n_div; ++i) {
4688 if (i == div)
4689 continue;
4690 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4691 continue;
4692 if (!isl_basic_map_div_is_known(bmap, i))
4693 return 0;
4696 return 1;
4699 /* Does integer division "div" have coefficient 1 in inequality constraint
4700 * "ineq" of "map"?
4702 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4704 unsigned o_div;
4706 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4707 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4708 return 1;
4710 return 0;
4713 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4714 * then try and drop redundant divs again,
4715 * freeing the temporary data structure "pairs" that was associated
4716 * to the old version of "bmap".
4718 static __isl_give isl_basic_map *set_eq_and_try_again(
4719 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4721 bmap = isl_basic_map_cow(bmap);
4722 isl_basic_map_inequality_to_equality(bmap, ineq);
4723 return drop_redundant_divs_again(bmap, pairs, 1);
4726 /* Drop the integer division at position "div", along with the two
4727 * inequality constraints "ineq1" and "ineq2" in which it appears
4728 * from "bmap" and then try and drop redundant divs again,
4729 * freeing the temporary data structure "pairs" that was associated
4730 * to the old version of "bmap".
4732 static __isl_give isl_basic_map *drop_div_and_try_again(
4733 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4734 __isl_take int *pairs)
4736 if (ineq1 > ineq2) {
4737 isl_basic_map_drop_inequality(bmap, ineq1);
4738 isl_basic_map_drop_inequality(bmap, ineq2);
4739 } else {
4740 isl_basic_map_drop_inequality(bmap, ineq2);
4741 isl_basic_map_drop_inequality(bmap, ineq1);
4743 bmap = isl_basic_map_drop_div(bmap, div);
4744 return drop_redundant_divs_again(bmap, pairs, 0);
4747 /* Given two inequality constraints
4749 * f(x) + n d + c >= 0, (ineq)
4751 * with d the variable at position "pos", and
4753 * f(x) + c0 >= 0, (lower)
4755 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4756 * determined by the first constraint.
4757 * That is, store
4759 * ceil((c0 - c)/n)
4761 * in *l.
4763 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4764 int ineq, int lower, int pos, isl_int *l)
4766 isl_int_neg(*l, bmap->ineq[ineq][0]);
4767 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4768 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4771 /* Given two inequality constraints
4773 * f(x) + n d + c >= 0, (ineq)
4775 * with d the variable at position "pos", and
4777 * -f(x) - c0 >= 0, (upper)
4779 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4780 * determined by the first constraint.
4781 * That is, store
4783 * ceil((-c1 - c)/n)
4785 * in *u.
4787 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4788 int ineq, int upper, int pos, isl_int *u)
4790 isl_int_neg(*u, bmap->ineq[ineq][0]);
4791 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4792 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4795 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4796 * does the corresponding lower bound have a fixed value in "bmap"?
4798 * In particular, "ineq" is of the form
4800 * f(x) + n d + c >= 0
4802 * with n > 0, c the constant term and
4803 * d the existentially quantified variable "div".
4804 * That is, the lower bound is
4806 * ceil((-f(x) - c)/n)
4808 * Look for a pair of constraints
4810 * f(x) + c0 >= 0
4811 * -f(x) + c1 >= 0
4813 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4814 * That is, check that
4816 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4818 * If so, return the index of inequality f(x) + c0 >= 0.
4819 * Otherwise, return -1.
4821 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4823 int i;
4824 int lower = -1, upper = -1;
4825 unsigned o_div, n_div;
4826 isl_int l, u;
4827 int equal;
4829 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4830 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4831 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4832 if (i == ineq)
4833 continue;
4834 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4835 continue;
4836 if (lower < 0 &&
4837 is_parallel_except(bmap, ineq, i, o_div + div)) {
4838 lower = i;
4839 continue;
4841 if (upper < 0 &&
4842 is_opposite_except(bmap, ineq, i, o_div + div)) {
4843 upper = i;
4847 if (lower < 0 || upper < 0)
4848 return -1;
4850 isl_int_init(l);
4851 isl_int_init(u);
4853 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4854 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4856 equal = isl_int_eq(l, u);
4858 isl_int_clear(l);
4859 isl_int_clear(u);
4861 return equal ? lower : -1;
4864 /* Given a lower bound constraint "ineq" on the existentially quantified
4865 * variable "div", such that the corresponding lower bound has
4866 * a fixed value in "bmap", assign this fixed value to the variable and
4867 * then try and drop redundant divs again,
4868 * freeing the temporary data structure "pairs" that was associated
4869 * to the old version of "bmap".
4870 * "lower" determines the constant value for the lower bound.
4872 * In particular, "ineq" is of the form
4874 * f(x) + n d + c >= 0,
4876 * while "lower" is of the form
4878 * f(x) + c0 >= 0
4880 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4881 * is ceil((c0 - c)/n).
4883 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4884 int div, int ineq, int lower, int *pairs)
4886 isl_int c;
4887 unsigned o_div;
4889 isl_int_init(c);
4891 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4892 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4893 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4894 free(pairs);
4896 isl_int_clear(c);
4898 return isl_basic_map_drop_redundant_divs(bmap);
4901 /* Remove divs that are not strictly needed based on the inequality
4902 * constraints.
4903 * In particular, if a div only occurs positively (or negatively)
4904 * in constraints, then it can simply be dropped.
4905 * Also, if a div occurs in only two constraints and if moreover
4906 * those two constraints are opposite to each other, except for the constant
4907 * term and if the sum of the constant terms is such that for any value
4908 * of the other values, there is always at least one integer value of the
4909 * div, i.e., if one plus this sum is greater than or equal to
4910 * the (absolute value) of the coefficient of the div in the constraints,
4911 * then we can also simply drop the div.
4913 * If an existentially quantified variable does not have an explicit
4914 * representation, appears in only a single lower bound that does not
4915 * involve any other such existentially quantified variables and appears
4916 * in this lower bound with coefficient 1,
4917 * then fix the variable to the value of the lower bound. That is,
4918 * turn the inequality into an equality.
4919 * If for any value of the other variables, there is any value
4920 * for the existentially quantified variable satisfying the constraints,
4921 * then this lower bound also satisfies the constraints.
4922 * It is therefore safe to pick this lower bound.
4924 * The same reasoning holds even if the coefficient is not one.
4925 * However, fixing the variable to the value of the lower bound may
4926 * in general introduce an extra integer division, in which case
4927 * it may be better to pick another value.
4928 * If this integer division has a known constant value, then plugging
4929 * in this constant value removes the existentially quantified variable
4930 * completely. In particular, if the lower bound is of the form
4931 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4932 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4933 * then the existentially quantified variable can be assigned this
4934 * shared value.
4936 * We skip divs that appear in equalities or in the definition of other divs.
4937 * Divs that appear in the definition of other divs usually occur in at least
4938 * 4 constraints, but the constraints may have been simplified.
4940 * If any divs are left after these simple checks then we move on
4941 * to more complicated cases in drop_more_redundant_divs.
4943 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4944 __isl_take isl_basic_map *bmap)
4946 int i, j;
4947 unsigned off;
4948 int *pairs = NULL;
4949 int n = 0;
4951 if (!bmap)
4952 goto error;
4953 if (bmap->n_div == 0)
4954 return bmap;
4956 off = isl_space_dim(bmap->dim, isl_dim_all);
4957 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4958 if (!pairs)
4959 goto error;
4961 for (i = 0; i < bmap->n_div; ++i) {
4962 int pos, neg;
4963 int last_pos, last_neg;
4964 int redundant;
4965 int defined;
4967 defined = !isl_int_is_zero(bmap->div[i][0]);
4968 for (j = i; j < bmap->n_div; ++j)
4969 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4970 break;
4971 if (j < bmap->n_div)
4972 continue;
4973 for (j = 0; j < bmap->n_eq; ++j)
4974 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4975 break;
4976 if (j < bmap->n_eq)
4977 continue;
4978 ++n;
4979 pos = neg = 0;
4980 for (j = 0; j < bmap->n_ineq; ++j) {
4981 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4982 last_pos = j;
4983 ++pos;
4985 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4986 last_neg = j;
4987 ++neg;
4990 pairs[i] = pos * neg;
4991 if (pairs[i] == 0) {
4992 for (j = bmap->n_ineq - 1; j >= 0; --j)
4993 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4994 isl_basic_map_drop_inequality(bmap, j);
4995 bmap = isl_basic_map_drop_div(bmap, i);
4996 return drop_redundant_divs_again(bmap, pairs, 0);
4998 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4999 int single, lower;
5000 if (pos != 1)
5001 continue;
5002 single = single_unknown(bmap, last_pos, i);
5003 if (!single)
5004 continue;
5005 if (has_coef_one(bmap, i, last_pos))
5006 return set_eq_and_try_again(bmap, last_pos,
5007 pairs);
5008 lower = lower_bound_is_cst(bmap, i, last_pos);
5009 if (lower >= 0)
5010 return fix_cst_lower(bmap, i, last_pos, lower,
5011 pairs);
5012 continue;
5015 isl_int_add(bmap->ineq[last_pos][0],
5016 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5017 isl_int_add_ui(bmap->ineq[last_pos][0],
5018 bmap->ineq[last_pos][0], 1);
5019 redundant = isl_int_ge(bmap->ineq[last_pos][0],
5020 bmap->ineq[last_pos][1+off+i]);
5021 isl_int_sub_ui(bmap->ineq[last_pos][0],
5022 bmap->ineq[last_pos][0], 1);
5023 isl_int_sub(bmap->ineq[last_pos][0],
5024 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
5025 if (redundant)
5026 return drop_div_and_try_again(bmap, i,
5027 last_pos, last_neg, pairs);
5028 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
5029 bmap = set_div_from_lower_bound(bmap, i, last_pos);
5030 return drop_redundant_divs_again(bmap, pairs, 1);
5032 pairs[i] = 0;
5033 --n;
5036 if (n > 0)
5037 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
5039 free(pairs);
5040 return bmap;
5041 error:
5042 free(pairs);
5043 isl_basic_map_free(bmap);
5044 return NULL;
5047 /* Consider the coefficients at "c" as a row vector and replace
5048 * them with their product with "T". "T" is assumed to be a square matrix.
5050 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
5052 int n;
5053 isl_ctx *ctx;
5054 isl_vec *v;
5056 if (!T)
5057 return isl_stat_error;
5058 n = isl_mat_rows(T);
5059 if (isl_seq_first_non_zero(c, n) == -1)
5060 return isl_stat_ok;
5061 ctx = isl_mat_get_ctx(T);
5062 v = isl_vec_alloc(ctx, n);
5063 if (!v)
5064 return isl_stat_error;
5065 isl_seq_swp_or_cpy(v->el, c, n);
5066 v = isl_vec_mat_product(v, isl_mat_copy(T));
5067 if (!v)
5068 return isl_stat_error;
5069 isl_seq_swp_or_cpy(c, v->el, n);
5070 isl_vec_free(v);
5072 return isl_stat_ok;
5075 /* Plug in T for the variables in "bmap" starting at "pos".
5076 * T is a linear unimodular matrix, i.e., without constant term.
5078 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
5079 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
5081 int i;
5082 unsigned n, total;
5084 bmap = isl_basic_map_cow(bmap);
5085 if (!bmap || !T)
5086 goto error;
5088 n = isl_mat_cols(T);
5089 if (n != isl_mat_rows(T))
5090 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5091 "expecting square matrix", goto error);
5093 total = isl_basic_map_dim(bmap, isl_dim_all);
5094 if (pos + n > total || pos + n < pos)
5095 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5096 "invalid range", goto error);
5098 for (i = 0; i < bmap->n_eq; ++i)
5099 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5100 goto error;
5101 for (i = 0; i < bmap->n_ineq; ++i)
5102 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5103 goto error;
5104 for (i = 0; i < bmap->n_div; ++i) {
5105 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5106 continue;
5107 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5108 goto error;
5111 isl_mat_free(T);
5112 return bmap;
5113 error:
5114 isl_basic_map_free(bmap);
5115 isl_mat_free(T);
5116 return NULL;
5119 /* Remove divs that are not strictly needed.
5121 * First look for an equality constraint involving two or more
5122 * existentially quantified variables without an explicit
5123 * representation. Replace the combination that appears
5124 * in the equality constraint by a single existentially quantified
5125 * variable such that the equality can be used to derive
5126 * an explicit representation for the variable.
5127 * If there are no more such equality constraints, then continue
5128 * with isl_basic_map_drop_redundant_divs_ineq.
5130 * In particular, if the equality constraint is of the form
5132 * f(x) + \sum_i c_i a_i = 0
5134 * with a_i existentially quantified variable without explicit
5135 * representation, then apply a transformation on the existentially
5136 * quantified variables to turn the constraint into
5138 * f(x) + g a_1' = 0
5140 * with g the gcd of the c_i.
5141 * In order to easily identify which existentially quantified variables
5142 * have a complete explicit representation, i.e., without being defined
5143 * in terms of other existentially quantified variables without
5144 * an explicit representation, the existentially quantified variables
5145 * are first sorted.
5147 * The variable transformation is computed by extending the row
5148 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5150 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5151 * [a_2'] [ a_2 ]
5152 * ... = U ....
5153 * [a_n'] [ a_n ]
5155 * with [c_1/g ... c_n/g] representing the first row of U.
5156 * The inverse of U is then plugged into the original constraints.
5157 * The call to isl_basic_map_simplify makes sure the explicit
5158 * representation for a_1' is extracted from the equality constraint.
5160 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5161 __isl_take isl_basic_map *bmap)
5163 int first;
5164 int i;
5165 unsigned o_div, n_div;
5166 int l;
5167 isl_ctx *ctx;
5168 isl_mat *T;
5170 if (!bmap)
5171 return NULL;
5172 if (isl_basic_map_divs_known(bmap))
5173 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5174 if (bmap->n_eq == 0)
5175 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5176 bmap = isl_basic_map_sort_divs(bmap);
5177 if (!bmap)
5178 return NULL;
5180 first = isl_basic_map_first_unknown_div(bmap);
5181 if (first < 0)
5182 return isl_basic_map_free(bmap);
5184 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5185 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5187 for (i = 0; i < bmap->n_eq; ++i) {
5188 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5189 n_div - (first));
5190 if (l < 0)
5191 continue;
5192 l += first;
5193 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5194 n_div - (l + 1)) == -1)
5195 continue;
5196 break;
5198 if (i >= bmap->n_eq)
5199 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5201 ctx = isl_basic_map_get_ctx(bmap);
5202 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5203 if (!T)
5204 return isl_basic_map_free(bmap);
5205 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5206 T = isl_mat_normalize_row(T, 0);
5207 T = isl_mat_unimodular_complete(T, 1);
5208 T = isl_mat_right_inverse(T);
5210 for (i = l; i < n_div; ++i)
5211 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5212 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5213 bmap = isl_basic_map_simplify(bmap);
5215 return isl_basic_map_drop_redundant_divs(bmap);
5218 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5219 struct isl_basic_set *bset)
5221 isl_basic_map *bmap = bset_to_bmap(bset);
5222 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5225 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5227 int i;
5229 if (!map)
5230 return NULL;
5231 for (i = 0; i < map->n; ++i) {
5232 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5233 if (!map->p[i])
5234 goto error;
5236 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5237 return map;
5238 error:
5239 isl_map_free(map);
5240 return NULL;
5243 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5245 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5248 /* Does "bmap" satisfy any equality that involves more than 2 variables
5249 * and/or has coefficients different from -1 and 1?
5251 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5253 int i;
5254 unsigned total;
5256 total = isl_basic_map_dim(bmap, isl_dim_all);
5258 for (i = 0; i < bmap->n_eq; ++i) {
5259 int j, k;
5261 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5262 if (j < 0)
5263 continue;
5264 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5265 !isl_int_is_negone(bmap->eq[i][1 + j]))
5266 return 1;
5268 j += 1;
5269 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5270 if (k < 0)
5271 continue;
5272 j += k;
5273 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5274 !isl_int_is_negone(bmap->eq[i][1 + j]))
5275 return 1;
5277 j += 1;
5278 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5279 if (k >= 0)
5280 return 1;
5283 return 0;
5286 /* Remove any common factor g from the constraint coefficients in "v".
5287 * The constant term is stored in the first position and is replaced
5288 * by floor(c/g). If any common factor is removed and if this results
5289 * in a tightening of the constraint, then set *tightened.
5291 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5292 int *tightened)
5294 isl_ctx *ctx;
5296 if (!v)
5297 return NULL;
5298 ctx = isl_vec_get_ctx(v);
5299 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5300 if (isl_int_is_zero(ctx->normalize_gcd))
5301 return v;
5302 if (isl_int_is_one(ctx->normalize_gcd))
5303 return v;
5304 v = isl_vec_cow(v);
5305 if (!v)
5306 return NULL;
5307 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5308 *tightened = 1;
5309 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5310 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5311 v->size - 1);
5312 return v;
5315 /* If "bmap" is an integer set that satisfies any equality involving
5316 * more than 2 variables and/or has coefficients different from -1 and 1,
5317 * then use variable compression to reduce the coefficients by removing
5318 * any (hidden) common factor.
5319 * In particular, apply the variable compression to each constraint,
5320 * factor out any common factor in the non-constant coefficients and
5321 * then apply the inverse of the compression.
5322 * At the end, we mark the basic map as having reduced constants.
5323 * If this flag is still set on the next invocation of this function,
5324 * then we skip the computation.
5326 * Removing a common factor may result in a tightening of some of
5327 * the constraints. If this happens, then we may end up with two
5328 * opposite inequalities that can be replaced by an equality.
5329 * We therefore call isl_basic_map_detect_inequality_pairs,
5330 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5331 * and isl_basic_map_gauss if such a pair was found.
5333 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5334 __isl_take isl_basic_map *bmap)
5336 unsigned total;
5337 isl_ctx *ctx;
5338 isl_vec *v;
5339 isl_mat *eq, *T, *T2;
5340 int i;
5341 int tightened;
5343 if (!bmap)
5344 return NULL;
5345 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5346 return bmap;
5347 if (isl_basic_map_is_rational(bmap))
5348 return bmap;
5349 if (bmap->n_eq == 0)
5350 return bmap;
5351 if (!has_multiple_var_equality(bmap))
5352 return bmap;
5354 total = isl_basic_map_dim(bmap, isl_dim_all);
5355 ctx = isl_basic_map_get_ctx(bmap);
5356 v = isl_vec_alloc(ctx, 1 + total);
5357 if (!v)
5358 return isl_basic_map_free(bmap);
5360 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5361 T = isl_mat_variable_compression(eq, &T2);
5362 if (!T || !T2)
5363 goto error;
5364 if (T->n_col == 0) {
5365 isl_mat_free(T);
5366 isl_mat_free(T2);
5367 isl_vec_free(v);
5368 return isl_basic_map_set_to_empty(bmap);
5371 tightened = 0;
5372 for (i = 0; i < bmap->n_ineq; ++i) {
5373 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5374 v = isl_vec_mat_product(v, isl_mat_copy(T));
5375 v = normalize_constraint(v, &tightened);
5376 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5377 if (!v)
5378 goto error;
5379 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5382 isl_mat_free(T);
5383 isl_mat_free(T2);
5384 isl_vec_free(v);
5386 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5388 if (tightened) {
5389 int progress = 0;
5391 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5392 if (progress) {
5393 bmap = eliminate_divs_eq(bmap, &progress);
5394 bmap = isl_basic_map_gauss(bmap, NULL);
5398 return bmap;
5399 error:
5400 isl_mat_free(T);
5401 isl_mat_free(T2);
5402 isl_vec_free(v);
5403 return isl_basic_map_free(bmap);
5406 /* Shift the integer division at position "div" of "bmap"
5407 * by "shift" times the variable at position "pos".
5408 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5409 * corresponds to the constant term.
5411 * That is, if the integer division has the form
5413 * floor(f(x)/d)
5415 * then replace it by
5417 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5419 __isl_give isl_basic_map *isl_basic_map_shift_div(
5420 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5422 int i;
5423 unsigned total;
5425 if (isl_int_is_zero(shift))
5426 return bmap;
5427 if (!bmap)
5428 return NULL;
5430 total = isl_basic_map_dim(bmap, isl_dim_all);
5431 total -= isl_basic_map_dim(bmap, isl_dim_div);
5433 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5435 for (i = 0; i < bmap->n_eq; ++i) {
5436 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5437 continue;
5438 isl_int_submul(bmap->eq[i][pos],
5439 shift, bmap->eq[i][1 + total + div]);
5441 for (i = 0; i < bmap->n_ineq; ++i) {
5442 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5443 continue;
5444 isl_int_submul(bmap->ineq[i][pos],
5445 shift, bmap->ineq[i][1 + total + div]);
5447 for (i = 0; i < bmap->n_div; ++i) {
5448 if (isl_int_is_zero(bmap->div[i][0]))
5449 continue;
5450 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5451 continue;
5452 isl_int_submul(bmap->div[i][1 + pos],
5453 shift, bmap->div[i][1 + 1 + total + div]);
5456 return bmap;