extract out isl_union_eval.c
[isl.git] / isl_map_simplify.c
blob255c50f592c485f01d7b69e55b644553ad56ab05
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include "isl_equalities.h"
18 #include <isl/map.h>
19 #include <isl_seq.h>
20 #include "isl_tab.h"
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
25 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
27 isl_int *t = bmap->eq[a];
28 bmap->eq[a] = bmap->eq[b];
29 bmap->eq[b] = t;
32 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
34 if (a != b) {
35 isl_int *t = bmap->ineq[a];
36 bmap->ineq[a] = bmap->ineq[b];
37 bmap->ineq[b] = t;
41 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
43 isl_seq_cpy(c, c + n, rem);
44 isl_seq_clr(c + rem, n);
47 /* Drop n dimensions starting at first.
49 * In principle, this frees up some extra variables as the number
50 * of columns remains constant, but we would have to extend
51 * the div array too as the number of rows in this array is assumed
52 * to be equal to extra.
54 struct isl_basic_set *isl_basic_set_drop_dims(
55 struct isl_basic_set *bset, unsigned first, unsigned n)
57 int i;
59 if (!bset)
60 goto error;
62 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
64 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
65 return bset;
67 bset = isl_basic_set_cow(bset);
68 if (!bset)
69 return NULL;
71 for (i = 0; i < bset->n_eq; ++i)
72 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
73 (bset->dim->n_out-first-n)+bset->extra);
75 for (i = 0; i < bset->n_ineq; ++i)
76 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
77 (bset->dim->n_out-first-n)+bset->extra);
79 for (i = 0; i < bset->n_div; ++i)
80 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
81 (bset->dim->n_out-first-n)+bset->extra);
83 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
84 if (!bset->dim)
85 goto error;
87 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
88 bset = isl_basic_set_simplify(bset);
89 return isl_basic_set_finalize(bset);
90 error:
91 isl_basic_set_free(bset);
92 return NULL;
95 struct isl_set *isl_set_drop_dims(
96 struct isl_set *set, unsigned first, unsigned n)
98 int i;
100 if (!set)
101 goto error;
103 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
105 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
106 return set;
107 set = isl_set_cow(set);
108 if (!set)
109 goto error;
110 set->dim = isl_space_drop_outputs(set->dim, first, n);
111 if (!set->dim)
112 goto error;
114 for (i = 0; i < set->n; ++i) {
115 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
116 if (!set->p[i])
117 goto error;
120 ISL_F_CLR(set, ISL_SET_NORMALIZED);
121 return set;
122 error:
123 isl_set_free(set);
124 return NULL;
127 /* Move "n" divs starting at "first" to the end of the list of divs.
129 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
130 unsigned first, unsigned n)
132 isl_int **div;
133 int i;
135 if (first + n == bmap->n_div)
136 return bmap;
138 div = isl_alloc_array(bmap->ctx, isl_int *, n);
139 if (!div)
140 goto error;
141 for (i = 0; i < n; ++i)
142 div[i] = bmap->div[first + i];
143 for (i = 0; i < bmap->n_div - first - n; ++i)
144 bmap->div[first + i] = bmap->div[first + n + i];
145 for (i = 0; i < n; ++i)
146 bmap->div[bmap->n_div - n + i] = div[i];
147 free(div);
148 return bmap;
149 error:
150 isl_basic_map_free(bmap);
151 return NULL;
154 /* Drop "n" dimensions of type "type" starting at "first".
156 * In principle, this frees up some extra variables as the number
157 * of columns remains constant, but we would have to extend
158 * the div array too as the number of rows in this array is assumed
159 * to be equal to extra.
161 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
162 enum isl_dim_type type, unsigned first, unsigned n)
164 int i;
165 unsigned dim;
166 unsigned offset;
167 unsigned left;
169 if (!bmap)
170 goto error;
172 dim = isl_basic_map_dim(bmap, type);
173 isl_assert(bmap->ctx, first + n <= dim, goto error);
175 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
176 return bmap;
178 bmap = isl_basic_map_cow(bmap);
179 if (!bmap)
180 return NULL;
182 offset = isl_basic_map_offset(bmap, type) + first;
183 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
184 for (i = 0; i < bmap->n_eq; ++i)
185 constraint_drop_vars(bmap->eq[i]+offset, n, left);
187 for (i = 0; i < bmap->n_ineq; ++i)
188 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
190 for (i = 0; i < bmap->n_div; ++i)
191 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
193 if (type == isl_dim_div) {
194 bmap = move_divs_last(bmap, first, n);
195 if (!bmap)
196 goto error;
197 isl_basic_map_free_div(bmap, n);
198 } else
199 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
200 if (!bmap->dim)
201 goto error;
203 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
204 bmap = isl_basic_map_simplify(bmap);
205 return isl_basic_map_finalize(bmap);
206 error:
207 isl_basic_map_free(bmap);
208 return NULL;
211 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
212 enum isl_dim_type type, unsigned first, unsigned n)
214 return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset,
215 type, first, n);
218 struct isl_basic_map *isl_basic_map_drop_inputs(
219 struct isl_basic_map *bmap, unsigned first, unsigned n)
221 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
224 struct isl_map *isl_map_drop(struct isl_map *map,
225 enum isl_dim_type type, unsigned first, unsigned n)
227 int i;
229 if (!map)
230 goto error;
232 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
234 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
235 return map;
236 map = isl_map_cow(map);
237 if (!map)
238 goto error;
239 map->dim = isl_space_drop_dims(map->dim, type, first, n);
240 if (!map->dim)
241 goto error;
243 for (i = 0; i < map->n; ++i) {
244 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
245 if (!map->p[i])
246 goto error;
248 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
250 return map;
251 error:
252 isl_map_free(map);
253 return NULL;
256 struct isl_set *isl_set_drop(struct isl_set *set,
257 enum isl_dim_type type, unsigned first, unsigned n)
259 return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
262 struct isl_map *isl_map_drop_inputs(
263 struct isl_map *map, unsigned first, unsigned n)
265 return isl_map_drop(map, isl_dim_in, first, n);
269 * We don't cow, as the div is assumed to be redundant.
271 static struct isl_basic_map *isl_basic_map_drop_div(
272 struct isl_basic_map *bmap, unsigned div)
274 int i;
275 unsigned pos;
277 if (!bmap)
278 goto error;
280 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
282 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
284 for (i = 0; i < bmap->n_eq; ++i)
285 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
287 for (i = 0; i < bmap->n_ineq; ++i) {
288 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
289 isl_basic_map_drop_inequality(bmap, i);
290 --i;
291 continue;
293 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
296 for (i = 0; i < bmap->n_div; ++i)
297 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
299 if (div != bmap->n_div - 1) {
300 int j;
301 isl_int *t = bmap->div[div];
303 for (j = div; j < bmap->n_div - 1; ++j)
304 bmap->div[j] = bmap->div[j+1];
306 bmap->div[bmap->n_div - 1] = t;
308 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
309 isl_basic_map_free_div(bmap, 1);
311 return bmap;
312 error:
313 isl_basic_map_free(bmap);
314 return NULL;
317 struct isl_basic_map *isl_basic_map_normalize_constraints(
318 struct isl_basic_map *bmap)
320 int i;
321 isl_int gcd;
322 unsigned total = isl_basic_map_total_dim(bmap);
324 if (!bmap)
325 return NULL;
327 isl_int_init(gcd);
328 for (i = bmap->n_eq - 1; i >= 0; --i) {
329 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
330 if (isl_int_is_zero(gcd)) {
331 if (!isl_int_is_zero(bmap->eq[i][0])) {
332 bmap = isl_basic_map_set_to_empty(bmap);
333 break;
335 isl_basic_map_drop_equality(bmap, i);
336 continue;
338 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
339 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
340 if (isl_int_is_one(gcd))
341 continue;
342 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
343 bmap = isl_basic_map_set_to_empty(bmap);
344 break;
346 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
349 for (i = bmap->n_ineq - 1; i >= 0; --i) {
350 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
351 if (isl_int_is_zero(gcd)) {
352 if (isl_int_is_neg(bmap->ineq[i][0])) {
353 bmap = isl_basic_map_set_to_empty(bmap);
354 break;
356 isl_basic_map_drop_inequality(bmap, i);
357 continue;
359 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
360 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
361 if (isl_int_is_one(gcd))
362 continue;
363 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
364 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
366 isl_int_clear(gcd);
368 return bmap;
371 struct isl_basic_set *isl_basic_set_normalize_constraints(
372 struct isl_basic_set *bset)
374 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
375 (struct isl_basic_map *)bset);
378 /* Assuming the variable at position "pos" has an integer coefficient
379 * in integer division "div", extract it from this integer division.
380 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
381 * corresponds to the constant term.
383 * That is, the integer division is of the form
385 * floor((... + c * d * x_pos + ...)/d)
387 * Replace it by
389 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
391 static __isl_give isl_basic_map *remove_var_from_div(
392 __isl_take isl_basic_map *bmap, int div, int pos)
394 isl_int shift;
396 isl_int_init(shift);
397 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
398 isl_int_neg(shift, shift);
399 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
400 isl_int_clear(shift);
402 return bmap;
405 /* Check if integer division "div" has any integral coefficient
406 * (or constant term). If so, extract them from the integer division.
408 static __isl_give isl_basic_map *remove_independent_vars_from_div(
409 __isl_take isl_basic_map *bmap, int div)
411 int i;
412 unsigned total = 1 + isl_basic_map_total_dim(bmap);
414 for (i = 0; i < total; ++i) {
415 if (isl_int_is_zero(bmap->div[div][1 + i]))
416 continue;
417 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
418 bmap->div[div][0]))
419 continue;
420 bmap = remove_var_from_div(bmap, div, i);
421 if (!bmap)
422 break;
425 return bmap;
428 /* Check if any known integer division has any integral coefficient
429 * (or constant term). If so, extract them from the integer division.
431 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
432 __isl_take isl_basic_map *bmap)
434 int i;
436 if (!bmap)
437 return NULL;
438 if (bmap->n_div == 0)
439 return bmap;
441 for (i = 0; i < bmap->n_div; ++i) {
442 if (isl_int_is_zero(bmap->div[i][0]))
443 continue;
444 bmap = remove_independent_vars_from_div(bmap, i);
445 if (!bmap)
446 break;
449 return bmap;
452 /* Remove any common factor in numerator and denominator of the div expression,
453 * not taking into account the constant term.
454 * That is, if the div is of the form
456 * floor((a + m f(x))/(m d))
458 * then replace it by
460 * floor((floor(a/m) + f(x))/d)
462 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
463 * and can therefore not influence the result of the floor.
465 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
467 unsigned total = isl_basic_map_total_dim(bmap);
468 isl_ctx *ctx = bmap->ctx;
470 if (isl_int_is_zero(bmap->div[div][0]))
471 return;
472 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
473 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
474 if (isl_int_is_one(ctx->normalize_gcd))
475 return;
476 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
477 ctx->normalize_gcd);
478 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
479 ctx->normalize_gcd);
480 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
481 ctx->normalize_gcd, total);
484 /* Remove any common factor in numerator and denominator of a div expression,
485 * not taking into account the constant term.
486 * That is, look for any div of the form
488 * floor((a + m f(x))/(m d))
490 * and replace it by
492 * floor((floor(a/m) + f(x))/d)
494 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
495 * and can therefore not influence the result of the floor.
497 static __isl_give isl_basic_map *normalize_div_expressions(
498 __isl_take isl_basic_map *bmap)
500 int i;
502 if (!bmap)
503 return NULL;
504 if (bmap->n_div == 0)
505 return bmap;
507 for (i = 0; i < bmap->n_div; ++i)
508 normalize_div_expression(bmap, i);
510 return bmap;
513 /* Assumes divs have been ordered if keep_divs is set.
515 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
516 unsigned pos, isl_int *eq, int keep_divs, int *progress)
518 unsigned total;
519 unsigned space_total;
520 int k;
521 int last_div;
523 total = isl_basic_map_total_dim(bmap);
524 space_total = isl_space_dim(bmap->dim, isl_dim_all);
525 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
526 for (k = 0; k < bmap->n_eq; ++k) {
527 if (bmap->eq[k] == eq)
528 continue;
529 if (isl_int_is_zero(bmap->eq[k][1+pos]))
530 continue;
531 if (progress)
532 *progress = 1;
533 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
534 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
537 for (k = 0; k < bmap->n_ineq; ++k) {
538 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
539 continue;
540 if (progress)
541 *progress = 1;
542 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
543 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
544 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
547 for (k = 0; k < bmap->n_div; ++k) {
548 if (isl_int_is_zero(bmap->div[k][0]))
549 continue;
550 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
551 continue;
552 if (progress)
553 *progress = 1;
554 /* We need to be careful about circular definitions,
555 * so for now we just remove the definition of div k
556 * if the equality contains any divs.
557 * If keep_divs is set, then the divs have been ordered
558 * and we can keep the definition as long as the result
559 * is still ordered.
561 if (last_div == -1 || (keep_divs && last_div < k)) {
562 isl_seq_elim(bmap->div[k]+1, eq,
563 1+pos, 1+total, &bmap->div[k][0]);
564 normalize_div_expression(bmap, k);
565 } else
566 isl_seq_clr(bmap->div[k], 1 + total);
567 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
571 /* Assumes divs have been ordered if keep_divs is set.
573 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
574 isl_int *eq, unsigned div, int keep_divs)
576 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
578 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
580 bmap = isl_basic_map_drop_div(bmap, div);
582 return bmap;
585 /* Check if elimination of div "div" using equality "eq" would not
586 * result in a div depending on a later div.
588 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
589 unsigned div)
591 int k;
592 int last_div;
593 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
594 unsigned pos = space_total + div;
596 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
597 if (last_div < 0 || last_div <= div)
598 return 1;
600 for (k = 0; k <= last_div; ++k) {
601 if (isl_int_is_zero(bmap->div[k][0]))
602 return 1;
603 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
604 return 0;
607 return 1;
610 /* Elimininate divs based on equalities
612 static struct isl_basic_map *eliminate_divs_eq(
613 struct isl_basic_map *bmap, int *progress)
615 int d;
616 int i;
617 int modified = 0;
618 unsigned off;
620 bmap = isl_basic_map_order_divs(bmap);
622 if (!bmap)
623 return NULL;
625 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
627 for (d = bmap->n_div - 1; d >= 0 ; --d) {
628 for (i = 0; i < bmap->n_eq; ++i) {
629 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
630 !isl_int_is_negone(bmap->eq[i][off + d]))
631 continue;
632 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
633 continue;
634 modified = 1;
635 *progress = 1;
636 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
637 if (isl_basic_map_drop_equality(bmap, i) < 0)
638 return isl_basic_map_free(bmap);
639 break;
642 if (modified)
643 return eliminate_divs_eq(bmap, progress);
644 return bmap;
647 /* Elimininate divs based on inequalities
649 static struct isl_basic_map *eliminate_divs_ineq(
650 struct isl_basic_map *bmap, int *progress)
652 int d;
653 int i;
654 unsigned off;
655 struct isl_ctx *ctx;
657 if (!bmap)
658 return NULL;
660 ctx = bmap->ctx;
661 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
663 for (d = bmap->n_div - 1; d >= 0 ; --d) {
664 for (i = 0; i < bmap->n_eq; ++i)
665 if (!isl_int_is_zero(bmap->eq[i][off + d]))
666 break;
667 if (i < bmap->n_eq)
668 continue;
669 for (i = 0; i < bmap->n_ineq; ++i)
670 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
671 break;
672 if (i < bmap->n_ineq)
673 continue;
674 *progress = 1;
675 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
676 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
677 break;
678 bmap = isl_basic_map_drop_div(bmap, d);
679 if (!bmap)
680 break;
682 return bmap;
685 struct isl_basic_map *isl_basic_map_gauss(
686 struct isl_basic_map *bmap, int *progress)
688 int k;
689 int done;
690 int last_var;
691 unsigned total_var;
692 unsigned total;
694 bmap = isl_basic_map_order_divs(bmap);
696 if (!bmap)
697 return NULL;
699 total = isl_basic_map_total_dim(bmap);
700 total_var = total - bmap->n_div;
702 last_var = total - 1;
703 for (done = 0; done < bmap->n_eq; ++done) {
704 for (; last_var >= 0; --last_var) {
705 for (k = done; k < bmap->n_eq; ++k)
706 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
707 break;
708 if (k < bmap->n_eq)
709 break;
711 if (last_var < 0)
712 break;
713 if (k != done)
714 swap_equality(bmap, k, done);
715 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
716 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
718 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
719 progress);
721 if (last_var >= total_var &&
722 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
723 unsigned div = last_var - total_var;
724 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
725 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
726 isl_int_set(bmap->div[div][0],
727 bmap->eq[done][1+last_var]);
728 if (progress)
729 *progress = 1;
730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
733 if (done == bmap->n_eq)
734 return bmap;
735 for (k = done; k < bmap->n_eq; ++k) {
736 if (isl_int_is_zero(bmap->eq[k][0]))
737 continue;
738 return isl_basic_map_set_to_empty(bmap);
740 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
741 return bmap;
744 struct isl_basic_set *isl_basic_set_gauss(
745 struct isl_basic_set *bset, int *progress)
747 return (struct isl_basic_set*)isl_basic_map_gauss(
748 (struct isl_basic_map *)bset, progress);
752 static unsigned int round_up(unsigned int v)
754 int old_v = v;
756 while (v) {
757 old_v = v;
758 v ^= v & -v;
760 return old_v << 1;
763 static int hash_index(isl_int ***index, unsigned int size, int bits,
764 struct isl_basic_map *bmap, int k)
766 int h;
767 unsigned total = isl_basic_map_total_dim(bmap);
768 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
769 for (h = hash; index[h]; h = (h+1) % size)
770 if (&bmap->ineq[k] != index[h] &&
771 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
772 break;
773 return h;
776 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
777 struct isl_basic_set *bset, int k)
779 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
782 /* If we can eliminate more than one div, then we need to make
783 * sure we do it from last div to first div, in order not to
784 * change the position of the other divs that still need to
785 * be removed.
787 static struct isl_basic_map *remove_duplicate_divs(
788 struct isl_basic_map *bmap, int *progress)
790 unsigned int size;
791 int *index;
792 int *elim_for;
793 int k, l, h;
794 int bits;
795 struct isl_blk eq;
796 unsigned total_var;
797 unsigned total;
798 struct isl_ctx *ctx;
800 bmap = isl_basic_map_order_divs(bmap);
801 if (!bmap || bmap->n_div <= 1)
802 return bmap;
804 total_var = isl_space_dim(bmap->dim, isl_dim_all);
805 total = total_var + bmap->n_div;
807 ctx = bmap->ctx;
808 for (k = bmap->n_div - 1; k >= 0; --k)
809 if (!isl_int_is_zero(bmap->div[k][0]))
810 break;
811 if (k <= 0)
812 return bmap;
814 size = round_up(4 * bmap->n_div / 3 - 1);
815 if (size == 0)
816 return bmap;
817 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
818 bits = ffs(size) - 1;
819 index = isl_calloc_array(ctx, int, size);
820 if (!elim_for || !index)
821 goto out;
822 eq = isl_blk_alloc(ctx, 1+total);
823 if (isl_blk_is_error(eq))
824 goto out;
826 isl_seq_clr(eq.data, 1+total);
827 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
828 for (--k; k >= 0; --k) {
829 uint32_t hash;
831 if (isl_int_is_zero(bmap->div[k][0]))
832 continue;
834 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
835 for (h = hash; index[h]; h = (h+1) % size)
836 if (isl_seq_eq(bmap->div[k],
837 bmap->div[index[h]-1], 2+total))
838 break;
839 if (index[h]) {
840 *progress = 1;
841 l = index[h] - 1;
842 elim_for[l] = k + 1;
844 index[h] = k+1;
846 for (l = bmap->n_div - 1; l >= 0; --l) {
847 if (!elim_for[l])
848 continue;
849 k = elim_for[l] - 1;
850 isl_int_set_si(eq.data[1+total_var+k], -1);
851 isl_int_set_si(eq.data[1+total_var+l], 1);
852 bmap = eliminate_div(bmap, eq.data, l, 1);
853 if (!bmap)
854 break;
855 isl_int_set_si(eq.data[1+total_var+k], 0);
856 isl_int_set_si(eq.data[1+total_var+l], 0);
859 isl_blk_free(ctx, eq);
860 out:
861 free(index);
862 free(elim_for);
863 return bmap;
866 static int n_pure_div_eq(struct isl_basic_map *bmap)
868 int i, j;
869 unsigned total;
871 total = isl_space_dim(bmap->dim, isl_dim_all);
872 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
873 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
874 --j;
875 if (j < 0)
876 break;
877 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
878 return 0;
880 return i;
883 /* Normalize divs that appear in equalities.
885 * In particular, we assume that bmap contains some equalities
886 * of the form
888 * a x = m * e_i
890 * and we want to replace the set of e_i by a minimal set and
891 * such that the new e_i have a canonical representation in terms
892 * of the vector x.
893 * If any of the equalities involves more than one divs, then
894 * we currently simply bail out.
896 * Let us first additionally assume that all equalities involve
897 * a div. The equalities then express modulo constraints on the
898 * remaining variables and we can use "parameter compression"
899 * to find a minimal set of constraints. The result is a transformation
901 * x = T(x') = x_0 + G x'
903 * with G a lower-triangular matrix with all elements below the diagonal
904 * non-negative and smaller than the diagonal element on the same row.
905 * We first normalize x_0 by making the same property hold in the affine
906 * T matrix.
907 * The rows i of G with a 1 on the diagonal do not impose any modulo
908 * constraint and simply express x_i = x'_i.
909 * For each of the remaining rows i, we introduce a div and a corresponding
910 * equality. In particular
912 * g_ii e_j = x_i - g_i(x')
914 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
915 * corresponding div (if g_kk != 1).
917 * If there are any equalities not involving any div, then we
918 * first apply a variable compression on the variables x:
920 * x = C x'' x'' = C_2 x
922 * and perform the above parameter compression on A C instead of on A.
923 * The resulting compression is then of the form
925 * x'' = T(x') = x_0 + G x'
927 * and in constructing the new divs and the corresponding equalities,
928 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
929 * by the corresponding row from C_2.
931 static struct isl_basic_map *normalize_divs(
932 struct isl_basic_map *bmap, int *progress)
934 int i, j, k;
935 int total;
936 int div_eq;
937 struct isl_mat *B;
938 struct isl_vec *d;
939 struct isl_mat *T = NULL;
940 struct isl_mat *C = NULL;
941 struct isl_mat *C2 = NULL;
942 isl_int v;
943 int *pos;
944 int dropped, needed;
946 if (!bmap)
947 return NULL;
949 if (bmap->n_div == 0)
950 return bmap;
952 if (bmap->n_eq == 0)
953 return bmap;
955 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
956 return bmap;
958 total = isl_space_dim(bmap->dim, isl_dim_all);
959 div_eq = n_pure_div_eq(bmap);
960 if (div_eq == 0)
961 return bmap;
963 if (div_eq < bmap->n_eq) {
964 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
965 bmap->n_eq - div_eq, 0, 1 + total);
966 C = isl_mat_variable_compression(B, &C2);
967 if (!C || !C2)
968 goto error;
969 if (C->n_col == 0) {
970 bmap = isl_basic_map_set_to_empty(bmap);
971 isl_mat_free(C);
972 isl_mat_free(C2);
973 goto done;
977 d = isl_vec_alloc(bmap->ctx, div_eq);
978 if (!d)
979 goto error;
980 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
981 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
982 --j;
983 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
985 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
987 if (C) {
988 B = isl_mat_product(B, C);
989 C = NULL;
992 T = isl_mat_parameter_compression(B, d);
993 if (!T)
994 goto error;
995 if (T->n_col == 0) {
996 bmap = isl_basic_map_set_to_empty(bmap);
997 isl_mat_free(C2);
998 isl_mat_free(T);
999 goto done;
1001 isl_int_init(v);
1002 for (i = 0; i < T->n_row - 1; ++i) {
1003 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1004 if (isl_int_is_zero(v))
1005 continue;
1006 isl_mat_col_submul(T, 0, v, 1 + i);
1008 isl_int_clear(v);
1009 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1010 if (!pos)
1011 goto error;
1012 /* We have to be careful because dropping equalities may reorder them */
1013 dropped = 0;
1014 for (j = bmap->n_div - 1; j >= 0; --j) {
1015 for (i = 0; i < bmap->n_eq; ++i)
1016 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1017 break;
1018 if (i < bmap->n_eq) {
1019 bmap = isl_basic_map_drop_div(bmap, j);
1020 isl_basic_map_drop_equality(bmap, i);
1021 ++dropped;
1024 pos[0] = 0;
1025 needed = 0;
1026 for (i = 1; i < T->n_row; ++i) {
1027 if (isl_int_is_one(T->row[i][i]))
1028 pos[i] = i;
1029 else
1030 needed++;
1032 if (needed > dropped) {
1033 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1034 needed, needed, 0);
1035 if (!bmap)
1036 goto error;
1038 for (i = 1; i < T->n_row; ++i) {
1039 if (isl_int_is_one(T->row[i][i]))
1040 continue;
1041 k = isl_basic_map_alloc_div(bmap);
1042 pos[i] = 1 + total + k;
1043 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1044 isl_int_set(bmap->div[k][0], T->row[i][i]);
1045 if (C2)
1046 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1047 else
1048 isl_int_set_si(bmap->div[k][1 + i], 1);
1049 for (j = 0; j < i; ++j) {
1050 if (isl_int_is_zero(T->row[i][j]))
1051 continue;
1052 if (pos[j] < T->n_row && C2)
1053 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1054 C2->row[pos[j]], 1 + total);
1055 else
1056 isl_int_neg(bmap->div[k][1 + pos[j]],
1057 T->row[i][j]);
1059 j = isl_basic_map_alloc_equality(bmap);
1060 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1061 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1063 free(pos);
1064 isl_mat_free(C2);
1065 isl_mat_free(T);
1067 if (progress)
1068 *progress = 1;
1069 done:
1070 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1072 return bmap;
1073 error:
1074 isl_mat_free(C);
1075 isl_mat_free(C2);
1076 isl_mat_free(T);
1077 return bmap;
1080 static struct isl_basic_map *set_div_from_lower_bound(
1081 struct isl_basic_map *bmap, int div, int ineq)
1083 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1085 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1086 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1087 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1088 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1089 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1091 return bmap;
1094 /* Check whether it is ok to define a div based on an inequality.
1095 * To avoid the introduction of circular definitions of divs, we
1096 * do not allow such a definition if the resulting expression would refer to
1097 * any other undefined divs or if any known div is defined in
1098 * terms of the unknown div.
1100 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1101 int div, int ineq)
1103 int j;
1104 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1106 /* Not defined in terms of unknown divs */
1107 for (j = 0; j < bmap->n_div; ++j) {
1108 if (div == j)
1109 continue;
1110 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1111 continue;
1112 if (isl_int_is_zero(bmap->div[j][0]))
1113 return 0;
1116 /* No other div defined in terms of this one => avoid loops */
1117 for (j = 0; j < bmap->n_div; ++j) {
1118 if (div == j)
1119 continue;
1120 if (isl_int_is_zero(bmap->div[j][0]))
1121 continue;
1122 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1123 return 0;
1126 return 1;
1129 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1130 * be a better expression than the current one?
1132 * If we do not have any expression yet, then any expression would be better.
1133 * Otherwise we check if the last variable involved in the inequality
1134 * (disregarding the div that it would define) is in an earlier position
1135 * than the last variable involved in the current div expression.
1137 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1138 int div, int ineq)
1140 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1141 int last_div;
1142 int last_ineq;
1144 if (isl_int_is_zero(bmap->div[div][0]))
1145 return 1;
1147 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1148 bmap->n_div - (div + 1)) >= 0)
1149 return 0;
1151 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1152 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1153 total + bmap->n_div);
1155 return last_ineq < last_div;
1158 /* Given two constraints "k" and "l" that are opposite to each other,
1159 * except for the constant term, check if we can use them
1160 * to obtain an expression for one of the hitherto unknown divs or
1161 * a "better" expression for a div for which we already have an expression.
1162 * "sum" is the sum of the constant terms of the constraints.
1163 * If this sum is strictly smaller than the coefficient of one
1164 * of the divs, then this pair can be used define the div.
1165 * To avoid the introduction of circular definitions of divs, we
1166 * do not use the pair if the resulting expression would refer to
1167 * any other undefined divs or if any known div is defined in
1168 * terms of the unknown div.
1170 static struct isl_basic_map *check_for_div_constraints(
1171 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1173 int i;
1174 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1176 for (i = 0; i < bmap->n_div; ++i) {
1177 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1178 continue;
1179 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1180 continue;
1181 if (!better_div_constraint(bmap, i, k))
1182 continue;
1183 if (!ok_to_set_div_from_bound(bmap, i, k))
1184 break;
1185 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1186 bmap = set_div_from_lower_bound(bmap, i, k);
1187 else
1188 bmap = set_div_from_lower_bound(bmap, i, l);
1189 if (progress)
1190 *progress = 1;
1191 break;
1193 return bmap;
1196 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1197 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1199 unsigned int size;
1200 isl_int ***index;
1201 int k, l, h;
1202 int bits;
1203 unsigned total = isl_basic_map_total_dim(bmap);
1204 isl_int sum;
1205 isl_ctx *ctx;
1207 if (!bmap || bmap->n_ineq <= 1)
1208 return bmap;
1210 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1211 if (size == 0)
1212 return bmap;
1213 bits = ffs(size) - 1;
1214 ctx = isl_basic_map_get_ctx(bmap);
1215 index = isl_calloc_array(ctx, isl_int **, size);
1216 if (!index)
1217 return bmap;
1219 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
1220 for (k = 1; k < bmap->n_ineq; ++k) {
1221 h = hash_index(index, size, bits, bmap, k);
1222 if (!index[h]) {
1223 index[h] = &bmap->ineq[k];
1224 continue;
1226 if (progress)
1227 *progress = 1;
1228 l = index[h] - &bmap->ineq[0];
1229 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1230 swap_inequality(bmap, k, l);
1231 isl_basic_map_drop_inequality(bmap, k);
1232 --k;
1234 isl_int_init(sum);
1235 for (k = 0; k < bmap->n_ineq-1; ++k) {
1236 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1237 h = hash_index(index, size, bits, bmap, k);
1238 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1239 if (!index[h])
1240 continue;
1241 l = index[h] - &bmap->ineq[0];
1242 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1243 if (isl_int_is_pos(sum)) {
1244 if (detect_divs)
1245 bmap = check_for_div_constraints(bmap, k, l,
1246 sum, progress);
1247 continue;
1249 if (isl_int_is_zero(sum)) {
1250 /* We need to break out of the loop after these
1251 * changes since the contents of the hash
1252 * will no longer be valid.
1253 * Plus, we probably we want to regauss first.
1255 if (progress)
1256 *progress = 1;
1257 isl_basic_map_drop_inequality(bmap, l);
1258 isl_basic_map_inequality_to_equality(bmap, k);
1259 } else
1260 bmap = isl_basic_map_set_to_empty(bmap);
1261 break;
1263 isl_int_clear(sum);
1265 free(index);
1266 return bmap;
1269 /* Detect all pairs of inequalities that form an equality.
1271 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1272 * Call it repeatedly while it is making progress.
1274 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1275 __isl_take isl_basic_map *bmap, int *progress)
1277 int duplicate;
1279 do {
1280 duplicate = 0;
1281 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1282 &duplicate, 0);
1283 if (progress && duplicate)
1284 *progress = 1;
1285 } while (duplicate);
1287 return bmap;
1290 /* Eliminate knowns divs from constraints where they appear with
1291 * a (positive or negative) unit coefficient.
1293 * That is, replace
1295 * floor(e/m) + f >= 0
1297 * by
1299 * e + m f >= 0
1301 * and
1303 * -floor(e/m) + f >= 0
1305 * by
1307 * -e + m f + m - 1 >= 0
1309 * The first conversion is valid because floor(e/m) >= -f is equivalent
1310 * to e/m >= -f because -f is an integral expression.
1311 * The second conversion follows from the fact that
1313 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1316 * Note that one of the div constraints may have been eliminated
1317 * due to being redundant with respect to the constraint that is
1318 * being modified by this function. The modified constraint may
1319 * no longer imply this div constraint, so we add it back to make
1320 * sure we do not lose any information.
1322 * We skip integral divs, i.e., those with denominator 1, as we would
1323 * risk eliminating the div from the div constraints. We do not need
1324 * to handle those divs here anyway since the div constraints will turn
1325 * out to form an equality and this equality can then be use to eliminate
1326 * the div from all constraints.
1328 static __isl_give isl_basic_map *eliminate_unit_divs(
1329 __isl_take isl_basic_map *bmap, int *progress)
1331 int i, j;
1332 isl_ctx *ctx;
1333 unsigned total;
1335 if (!bmap)
1336 return NULL;
1338 ctx = isl_basic_map_get_ctx(bmap);
1339 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1341 for (i = 0; i < bmap->n_div; ++i) {
1342 if (isl_int_is_zero(bmap->div[i][0]))
1343 continue;
1344 if (isl_int_is_one(bmap->div[i][0]))
1345 continue;
1346 for (j = 0; j < bmap->n_ineq; ++j) {
1347 int s;
1349 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1350 !isl_int_is_negone(bmap->ineq[j][total + i]))
1351 continue;
1353 *progress = 1;
1355 s = isl_int_sgn(bmap->ineq[j][total + i]);
1356 isl_int_set_si(bmap->ineq[j][total + i], 0);
1357 if (s < 0)
1358 isl_seq_combine(bmap->ineq[j],
1359 ctx->negone, bmap->div[i] + 1,
1360 bmap->div[i][0], bmap->ineq[j],
1361 total + bmap->n_div);
1362 else
1363 isl_seq_combine(bmap->ineq[j],
1364 ctx->one, bmap->div[i] + 1,
1365 bmap->div[i][0], bmap->ineq[j],
1366 total + bmap->n_div);
1367 if (s < 0) {
1368 isl_int_add(bmap->ineq[j][0],
1369 bmap->ineq[j][0], bmap->div[i][0]);
1370 isl_int_sub_ui(bmap->ineq[j][0],
1371 bmap->ineq[j][0], 1);
1374 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1375 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1376 return isl_basic_map_free(bmap);
1380 return bmap;
1383 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1385 int progress = 1;
1386 if (!bmap)
1387 return NULL;
1388 while (progress) {
1389 progress = 0;
1390 if (!bmap)
1391 break;
1392 if (isl_basic_map_plain_is_empty(bmap))
1393 break;
1394 bmap = isl_basic_map_normalize_constraints(bmap);
1395 bmap = remove_independent_vars_from_divs(bmap);
1396 bmap = normalize_div_expressions(bmap);
1397 bmap = remove_duplicate_divs(bmap, &progress);
1398 bmap = eliminate_unit_divs(bmap, &progress);
1399 bmap = eliminate_divs_eq(bmap, &progress);
1400 bmap = eliminate_divs_ineq(bmap, &progress);
1401 bmap = isl_basic_map_gauss(bmap, &progress);
1402 /* requires equalities in normal form */
1403 bmap = normalize_divs(bmap, &progress);
1404 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1405 &progress, 1);
1406 if (bmap && progress)
1407 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1409 return bmap;
1412 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1414 return (struct isl_basic_set *)
1415 isl_basic_map_simplify((struct isl_basic_map *)bset);
1419 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1420 isl_int *constraint, unsigned div)
1422 unsigned pos;
1424 if (!bmap)
1425 return -1;
1427 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1429 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1430 int neg;
1431 isl_int_sub(bmap->div[div][1],
1432 bmap->div[div][1], bmap->div[div][0]);
1433 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1434 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1435 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1436 isl_int_add(bmap->div[div][1],
1437 bmap->div[div][1], bmap->div[div][0]);
1438 if (!neg)
1439 return 0;
1440 if (isl_seq_first_non_zero(constraint+pos+1,
1441 bmap->n_div-div-1) != -1)
1442 return 0;
1443 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1444 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1445 return 0;
1446 if (isl_seq_first_non_zero(constraint+pos+1,
1447 bmap->n_div-div-1) != -1)
1448 return 0;
1449 } else
1450 return 0;
1452 return 1;
1455 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1456 isl_int *constraint, unsigned div)
1458 return isl_basic_map_is_div_constraint(bset, constraint, div);
1462 /* If the only constraints a div d=floor(f/m)
1463 * appears in are its two defining constraints
1465 * f - m d >=0
1466 * -(f - (m - 1)) + m d >= 0
1468 * then it can safely be removed.
1470 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1472 int i;
1473 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1475 for (i = 0; i < bmap->n_eq; ++i)
1476 if (!isl_int_is_zero(bmap->eq[i][pos]))
1477 return 0;
1479 for (i = 0; i < bmap->n_ineq; ++i) {
1480 if (isl_int_is_zero(bmap->ineq[i][pos]))
1481 continue;
1482 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1483 return 0;
1486 for (i = 0; i < bmap->n_div; ++i) {
1487 if (isl_int_is_zero(bmap->div[i][0]))
1488 continue;
1489 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1490 return 0;
1493 return 1;
1497 * Remove divs that don't occur in any of the constraints or other divs.
1498 * These can arise when dropping constraints from a basic map or
1499 * when the divs of a basic map have been temporarily aligned
1500 * with the divs of another basic map.
1502 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1504 int i;
1506 if (!bmap)
1507 return NULL;
1509 for (i = bmap->n_div-1; i >= 0; --i) {
1510 if (!div_is_redundant(bmap, i))
1511 continue;
1512 bmap = isl_basic_map_drop_div(bmap, i);
1514 return bmap;
1517 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1519 bmap = remove_redundant_divs(bmap);
1520 if (!bmap)
1521 return NULL;
1522 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1523 return bmap;
1526 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1528 return (struct isl_basic_set *)
1529 isl_basic_map_finalize((struct isl_basic_map *)bset);
1532 struct isl_set *isl_set_finalize(struct isl_set *set)
1534 int i;
1536 if (!set)
1537 return NULL;
1538 for (i = 0; i < set->n; ++i) {
1539 set->p[i] = isl_basic_set_finalize(set->p[i]);
1540 if (!set->p[i])
1541 goto error;
1543 return set;
1544 error:
1545 isl_set_free(set);
1546 return NULL;
1549 struct isl_map *isl_map_finalize(struct isl_map *map)
1551 int i;
1553 if (!map)
1554 return NULL;
1555 for (i = 0; i < map->n; ++i) {
1556 map->p[i] = isl_basic_map_finalize(map->p[i]);
1557 if (!map->p[i])
1558 goto error;
1560 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1561 return map;
1562 error:
1563 isl_map_free(map);
1564 return NULL;
1568 /* Remove definition of any div that is defined in terms of the given variable.
1569 * The div itself is not removed. Functions such as
1570 * eliminate_divs_ineq depend on the other divs remaining in place.
1572 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1573 int pos)
1575 int i;
1577 if (!bmap)
1578 return NULL;
1580 for (i = 0; i < bmap->n_div; ++i) {
1581 if (isl_int_is_zero(bmap->div[i][0]))
1582 continue;
1583 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1584 continue;
1585 isl_int_set_si(bmap->div[i][0], 0);
1587 return bmap;
1590 /* Eliminate the specified variables from the constraints using
1591 * Fourier-Motzkin. The variables themselves are not removed.
1593 struct isl_basic_map *isl_basic_map_eliminate_vars(
1594 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1596 int d;
1597 int i, j, k;
1598 unsigned total;
1599 int need_gauss = 0;
1601 if (n == 0)
1602 return bmap;
1603 if (!bmap)
1604 return NULL;
1605 total = isl_basic_map_total_dim(bmap);
1607 bmap = isl_basic_map_cow(bmap);
1608 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1609 bmap = remove_dependent_vars(bmap, d);
1610 if (!bmap)
1611 return NULL;
1613 for (d = pos + n - 1;
1614 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1615 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1616 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1617 int n_lower, n_upper;
1618 if (!bmap)
1619 return NULL;
1620 for (i = 0; i < bmap->n_eq; ++i) {
1621 if (isl_int_is_zero(bmap->eq[i][1+d]))
1622 continue;
1623 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1624 isl_basic_map_drop_equality(bmap, i);
1625 need_gauss = 1;
1626 break;
1628 if (i < bmap->n_eq)
1629 continue;
1630 n_lower = 0;
1631 n_upper = 0;
1632 for (i = 0; i < bmap->n_ineq; ++i) {
1633 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1634 n_lower++;
1635 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1636 n_upper++;
1638 bmap = isl_basic_map_extend_constraints(bmap,
1639 0, n_lower * n_upper);
1640 if (!bmap)
1641 goto error;
1642 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1643 int last;
1644 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1645 continue;
1646 last = -1;
1647 for (j = 0; j < i; ++j) {
1648 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1649 continue;
1650 last = j;
1651 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1652 isl_int_sgn(bmap->ineq[j][1+d]))
1653 continue;
1654 k = isl_basic_map_alloc_inequality(bmap);
1655 if (k < 0)
1656 goto error;
1657 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1658 1+total);
1659 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1660 1+d, 1+total, NULL);
1662 isl_basic_map_drop_inequality(bmap, i);
1663 i = last + 1;
1665 if (n_lower > 0 && n_upper > 0) {
1666 bmap = isl_basic_map_normalize_constraints(bmap);
1667 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1668 NULL, 0);
1669 bmap = isl_basic_map_gauss(bmap, NULL);
1670 bmap = isl_basic_map_remove_redundancies(bmap);
1671 need_gauss = 0;
1672 if (!bmap)
1673 goto error;
1674 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1675 break;
1678 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1679 if (need_gauss)
1680 bmap = isl_basic_map_gauss(bmap, NULL);
1681 return bmap;
1682 error:
1683 isl_basic_map_free(bmap);
1684 return NULL;
1687 struct isl_basic_set *isl_basic_set_eliminate_vars(
1688 struct isl_basic_set *bset, unsigned pos, unsigned n)
1690 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1691 (struct isl_basic_map *)bset, pos, n);
1694 /* Eliminate the specified n dimensions starting at first from the
1695 * constraints, without removing the dimensions from the space.
1696 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1697 * Otherwise, they are projected out and the original space is restored.
1699 __isl_give isl_basic_map *isl_basic_map_eliminate(
1700 __isl_take isl_basic_map *bmap,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1703 isl_space *space;
1705 if (!bmap)
1706 return NULL;
1707 if (n == 0)
1708 return bmap;
1710 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1711 isl_die(bmap->ctx, isl_error_invalid,
1712 "index out of bounds", goto error);
1714 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1715 first += isl_basic_map_offset(bmap, type) - 1;
1716 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1717 return isl_basic_map_finalize(bmap);
1720 space = isl_basic_map_get_space(bmap);
1721 bmap = isl_basic_map_project_out(bmap, type, first, n);
1722 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1723 bmap = isl_basic_map_reset_space(bmap, space);
1724 return bmap;
1725 error:
1726 isl_basic_map_free(bmap);
1727 return NULL;
1730 __isl_give isl_basic_set *isl_basic_set_eliminate(
1731 __isl_take isl_basic_set *bset,
1732 enum isl_dim_type type, unsigned first, unsigned n)
1734 return isl_basic_map_eliminate(bset, type, first, n);
1737 /* Don't assume equalities are in order, because align_divs
1738 * may have changed the order of the divs.
1740 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1742 int d, i;
1743 unsigned total;
1745 total = isl_space_dim(bmap->dim, isl_dim_all);
1746 for (d = 0; d < total; ++d)
1747 elim[d] = -1;
1748 for (i = 0; i < bmap->n_eq; ++i) {
1749 for (d = total - 1; d >= 0; --d) {
1750 if (isl_int_is_zero(bmap->eq[i][1+d]))
1751 continue;
1752 elim[d] = i;
1753 break;
1758 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1760 compute_elimination_index((struct isl_basic_map *)bset, elim);
1763 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1764 struct isl_basic_map *bmap, int *elim)
1766 int d;
1767 int copied = 0;
1768 unsigned total;
1770 total = isl_space_dim(bmap->dim, isl_dim_all);
1771 for (d = total - 1; d >= 0; --d) {
1772 if (isl_int_is_zero(src[1+d]))
1773 continue;
1774 if (elim[d] == -1)
1775 continue;
1776 if (!copied) {
1777 isl_seq_cpy(dst, src, 1 + total);
1778 copied = 1;
1780 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1782 return copied;
1785 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1786 struct isl_basic_set *bset, int *elim)
1788 return reduced_using_equalities(dst, src,
1789 (struct isl_basic_map *)bset, elim);
1792 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1793 struct isl_basic_set *bset, struct isl_basic_set *context)
1795 int i;
1796 int *elim;
1798 if (!bset || !context)
1799 goto error;
1801 if (context->n_eq == 0) {
1802 isl_basic_set_free(context);
1803 return bset;
1806 bset = isl_basic_set_cow(bset);
1807 if (!bset)
1808 goto error;
1810 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1811 if (!elim)
1812 goto error;
1813 set_compute_elimination_index(context, elim);
1814 for (i = 0; i < bset->n_eq; ++i)
1815 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1816 context, elim);
1817 for (i = 0; i < bset->n_ineq; ++i)
1818 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1819 context, elim);
1820 isl_basic_set_free(context);
1821 free(elim);
1822 bset = isl_basic_set_simplify(bset);
1823 bset = isl_basic_set_finalize(bset);
1824 return bset;
1825 error:
1826 isl_basic_set_free(bset);
1827 isl_basic_set_free(context);
1828 return NULL;
1831 static struct isl_basic_set *remove_shifted_constraints(
1832 struct isl_basic_set *bset, struct isl_basic_set *context)
1834 unsigned int size;
1835 isl_int ***index;
1836 int bits;
1837 int k, h, l;
1838 isl_ctx *ctx;
1840 if (!bset || !context)
1841 return bset;
1843 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1844 if (size == 0)
1845 return bset;
1846 bits = ffs(size) - 1;
1847 ctx = isl_basic_set_get_ctx(bset);
1848 index = isl_calloc_array(ctx, isl_int **, size);
1849 if (!index)
1850 return bset;
1852 for (k = 0; k < context->n_ineq; ++k) {
1853 h = set_hash_index(index, size, bits, context, k);
1854 index[h] = &context->ineq[k];
1856 for (k = 0; k < bset->n_ineq; ++k) {
1857 h = set_hash_index(index, size, bits, bset, k);
1858 if (!index[h])
1859 continue;
1860 l = index[h] - &context->ineq[0];
1861 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1862 continue;
1863 bset = isl_basic_set_cow(bset);
1864 if (!bset)
1865 goto error;
1866 isl_basic_set_drop_inequality(bset, k);
1867 --k;
1869 free(index);
1870 return bset;
1871 error:
1872 free(index);
1873 return bset;
1876 /* Remove constraints from "bmap" that are identical to constraints
1877 * in "context" or that are more relaxed (greater constant term).
1879 * We perform the test for shifted copies on the pure constraints
1880 * in remove_shifted_constraints.
1882 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1883 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1885 isl_basic_set *bset, *bset_context;
1887 if (!bmap || !context)
1888 goto error;
1890 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1891 isl_basic_map_free(context);
1892 return bmap;
1895 context = isl_basic_map_align_divs(context, bmap);
1896 bmap = isl_basic_map_align_divs(bmap, context);
1898 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1899 bset_context = isl_basic_map_underlying_set(context);
1900 bset = remove_shifted_constraints(bset, bset_context);
1901 isl_basic_set_free(bset_context);
1903 bmap = isl_basic_map_overlying_set(bset, bmap);
1905 return bmap;
1906 error:
1907 isl_basic_map_free(bmap);
1908 isl_basic_map_free(context);
1909 return NULL;
1912 /* Does the (linear part of a) constraint "c" involve any of the "len"
1913 * "relevant" dimensions?
1915 static int is_related(isl_int *c, int len, int *relevant)
1917 int i;
1919 for (i = 0; i < len; ++i) {
1920 if (!relevant[i])
1921 continue;
1922 if (!isl_int_is_zero(c[i]))
1923 return 1;
1926 return 0;
1929 /* Drop constraints from "bset" that do not involve any of
1930 * the dimensions marked "relevant".
1932 static __isl_give isl_basic_set *drop_unrelated_constraints(
1933 __isl_take isl_basic_set *bset, int *relevant)
1935 int i, dim;
1937 dim = isl_basic_set_dim(bset, isl_dim_set);
1938 for (i = 0; i < dim; ++i)
1939 if (!relevant[i])
1940 break;
1941 if (i >= dim)
1942 return bset;
1944 for (i = bset->n_eq - 1; i >= 0; --i)
1945 if (!is_related(bset->eq[i] + 1, dim, relevant))
1946 isl_basic_set_drop_equality(bset, i);
1948 for (i = bset->n_ineq - 1; i >= 0; --i)
1949 if (!is_related(bset->ineq[i] + 1, dim, relevant))
1950 isl_basic_set_drop_inequality(bset, i);
1952 return bset;
1955 /* Update the groups in "group" based on the (linear part of a) constraint "c".
1957 * In particular, for any variable involved in the constraint,
1958 * find the actual group id from before and replace the group
1959 * of the corresponding variable by the minimal group of all
1960 * the variables involved in the constraint considered so far
1961 * (if this minimum is smaller) or replace the minimum by this group
1962 * (if the minimum is larger).
1964 * At the end, all the variables in "c" will (indirectly) point
1965 * to the minimal of the groups that they referred to originally.
1967 static void update_groups(int dim, int *group, isl_int *c)
1969 int j;
1970 int min = dim;
1972 for (j = 0; j < dim; ++j) {
1973 if (isl_int_is_zero(c[j]))
1974 continue;
1975 while (group[j] >= 0 && group[group[j]] != group[j])
1976 group[j] = group[group[j]];
1977 if (group[j] == min)
1978 continue;
1979 if (group[j] < min) {
1980 if (min >= 0 && min < dim)
1981 group[min] = group[j];
1982 min = group[j];
1983 } else
1984 group[group[j]] = min;
1988 /* Drop constraints from "context" that are irrelevant for computing
1989 * the gist of "bset".
1991 * In particular, drop constraints in variables that are not related
1992 * to any of the variables involved in the constraints of "bset"
1993 * in the sense that there is no sequence of constraints that connects them.
1995 * We construct groups of variables that collect variables that
1996 * (indirectly) appear in some common constraint of "context".
1997 * Each group is identified by the first variable in the group,
1998 * except for the special group of variables that appear in "bset"
1999 * (or are related to those variables), which is identified by -1.
2000 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2001 * otherwise the group of i is the group of group[i].
2003 * We first initialize the -1 group with the variables that appear in "bset".
2004 * Then we initialize groups for the remaining variables.
2005 * Then we iterate over the constraints of "context" and update the
2006 * group of the variables in the constraint by the smallest group.
2007 * Finally, we resolve indirect references to groups by running over
2008 * the variables.
2010 * After computing the groups, we drop constraints that do not involve
2011 * any variables in the -1 group.
2013 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2014 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2016 isl_ctx *ctx;
2017 int *group;
2018 int dim;
2019 int i, j;
2020 int last;
2022 if (!context || !bset)
2023 return isl_basic_set_free(context);
2025 dim = isl_basic_set_dim(bset, isl_dim_set);
2026 ctx = isl_basic_set_get_ctx(bset);
2027 group = isl_calloc_array(ctx, int, dim);
2029 if (!group)
2030 goto error;
2032 for (i = 0; i < dim; ++i) {
2033 for (j = 0; j < bset->n_eq; ++j)
2034 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2035 break;
2036 if (j < bset->n_eq) {
2037 group[i] = -1;
2038 continue;
2040 for (j = 0; j < bset->n_ineq; ++j)
2041 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2042 break;
2043 if (j < bset->n_ineq)
2044 group[i] = -1;
2047 last = -1;
2048 for (i = 0; i < dim; ++i)
2049 if (group[i] >= 0)
2050 last = group[i] = i;
2051 if (last < 0) {
2052 free(group);
2053 return context;
2056 for (i = 0; i < context->n_eq; ++i)
2057 update_groups(dim, group, context->eq[i] + 1);
2058 for (i = 0; i < context->n_ineq; ++i)
2059 update_groups(dim, group, context->ineq[i] + 1);
2061 for (i = 0; i < dim; ++i)
2062 if (group[i] >= 0)
2063 group[i] = group[group[i]];
2065 for (i = 0; i < dim; ++i)
2066 group[i] = group[i] == -1;
2068 context = drop_unrelated_constraints(context, group);
2070 free(group);
2071 return context;
2072 error:
2073 free(group);
2074 return isl_basic_set_free(context);
2077 /* Remove all information from bset that is redundant in the context
2078 * of context. Both bset and context are assumed to be full-dimensional.
2080 * We first remove the inequalities from "bset"
2081 * that are obviously redundant with respect to some inequality in "context".
2082 * Then we remove those constraints from "context" that have become
2083 * irrelevant for computing the gist of "bset".
2084 * Note that this removal of constraints cannot be replaced by
2085 * a factorization because factors in "bset" may still be connected
2086 * to each other through constraints in "context".
2088 * If there are any inequalities left, we construct a tableau for
2089 * the context and then add the inequalities of "bset".
2090 * Before adding these inequalities, we freeze all constraints such that
2091 * they won't be considered redundant in terms of the constraints of "bset".
2092 * Then we detect all redundant constraints (among the
2093 * constraints that weren't frozen), first by checking for redundancy in the
2094 * the tableau and then by checking if replacing a constraint by its negation
2095 * would lead to an empty set. This last step is fairly expensive
2096 * and could be optimized by more reuse of the tableau.
2097 * Finally, we update bset according to the results.
2099 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2100 __isl_take isl_basic_set *context)
2102 int i, k;
2103 isl_basic_set *combined = NULL;
2104 struct isl_tab *tab = NULL;
2105 unsigned context_ineq;
2106 unsigned total;
2108 if (!bset || !context)
2109 goto error;
2111 if (isl_basic_set_is_universe(bset)) {
2112 isl_basic_set_free(context);
2113 return bset;
2116 if (isl_basic_set_is_universe(context)) {
2117 isl_basic_set_free(context);
2118 return bset;
2121 bset = remove_shifted_constraints(bset, context);
2122 if (!bset)
2123 goto error;
2124 if (bset->n_ineq == 0)
2125 goto done;
2127 context = drop_irrelevant_constraints(context, bset);
2128 if (!context)
2129 goto error;
2130 if (isl_basic_set_is_universe(context)) {
2131 isl_basic_set_free(context);
2132 return bset;
2135 context_ineq = context->n_ineq;
2136 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2137 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2138 tab = isl_tab_from_basic_set(combined, 0);
2139 for (i = 0; i < context_ineq; ++i)
2140 if (isl_tab_freeze_constraint(tab, i) < 0)
2141 goto error;
2142 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2143 goto error;
2144 for (i = 0; i < bset->n_ineq; ++i)
2145 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
2146 goto error;
2147 bset = isl_basic_set_add_constraints(combined, bset, 0);
2148 combined = NULL;
2149 if (!bset)
2150 goto error;
2151 if (isl_tab_detect_redundant(tab) < 0)
2152 goto error;
2153 total = isl_basic_set_total_dim(bset);
2154 for (i = context_ineq; i < bset->n_ineq; ++i) {
2155 int is_empty;
2156 if (tab->con[i].is_redundant)
2157 continue;
2158 tab->con[i].is_redundant = 1;
2159 combined = isl_basic_set_dup(bset);
2160 combined = isl_basic_set_update_from_tab(combined, tab);
2161 combined = isl_basic_set_extend_constraints(combined, 0, 1);
2162 k = isl_basic_set_alloc_inequality(combined);
2163 if (k < 0)
2164 goto error;
2165 isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total);
2166 isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1);
2167 is_empty = isl_basic_set_is_empty(combined);
2168 if (is_empty < 0)
2169 goto error;
2170 isl_basic_set_free(combined);
2171 combined = NULL;
2172 if (!is_empty)
2173 tab->con[i].is_redundant = 0;
2175 for (i = 0; i < context_ineq; ++i)
2176 tab->con[i].is_redundant = 1;
2177 bset = isl_basic_set_update_from_tab(bset, tab);
2178 if (bset) {
2179 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2180 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2183 isl_tab_free(tab);
2184 done:
2185 bset = isl_basic_set_simplify(bset);
2186 bset = isl_basic_set_finalize(bset);
2187 isl_basic_set_free(context);
2188 return bset;
2189 error:
2190 isl_tab_free(tab);
2191 isl_basic_set_free(combined);
2192 isl_basic_set_free(context);
2193 isl_basic_set_free(bset);
2194 return NULL;
2197 /* Remove all information from bset that is redundant in the context
2198 * of context. In particular, equalities that are linear combinations
2199 * of those in context are removed. Then the inequalities that are
2200 * redundant in the context of the equalities and inequalities of
2201 * context are removed.
2203 * First of all, we drop those constraints from "context"
2204 * that are irrelevant for computing the gist of "bset".
2205 * Alternatively, we could factorize the intersection of "context" and "bset".
2207 * We first compute the integer affine hull of the intersection,
2208 * compute the gist inside this affine hull and then add back
2209 * those equalities that are not implied by the context.
2211 * If two constraints are mutually redundant, then uset_gist_full
2212 * will remove the second of those constraints. We therefore first
2213 * sort the constraints so that constraints not involving existentially
2214 * quantified variables are given precedence over those that do.
2215 * We have to perform this sorting before the variable compression,
2216 * because that may effect the order of the variables.
2218 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2219 __isl_take isl_basic_set *context)
2221 isl_mat *eq;
2222 isl_mat *T, *T2;
2223 isl_basic_set *aff;
2224 isl_basic_set *aff_context;
2225 unsigned total;
2227 if (!bset || !context)
2228 goto error;
2230 context = drop_irrelevant_constraints(context, bset);
2232 aff = isl_basic_set_copy(bset);
2233 aff = isl_basic_set_intersect(aff, isl_basic_set_copy(context));
2234 aff = isl_basic_set_affine_hull(aff);
2235 if (!aff)
2236 goto error;
2237 if (isl_basic_set_plain_is_empty(aff)) {
2238 isl_basic_set_free(bset);
2239 isl_basic_set_free(context);
2240 return aff;
2242 bset = isl_basic_set_sort_constraints(bset);
2243 if (aff->n_eq == 0) {
2244 isl_basic_set_free(aff);
2245 return uset_gist_full(bset, context);
2247 total = isl_basic_set_total_dim(bset);
2248 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2249 eq = isl_mat_cow(eq);
2250 T = isl_mat_variable_compression(eq, &T2);
2251 if (T && T->n_col == 0) {
2252 isl_mat_free(T);
2253 isl_mat_free(T2);
2254 isl_basic_set_free(context);
2255 isl_basic_set_free(aff);
2256 return isl_basic_set_set_to_empty(bset);
2259 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2261 bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
2262 context = isl_basic_set_preimage(context, T);
2264 bset = uset_gist_full(bset, context);
2265 bset = isl_basic_set_preimage(bset, T2);
2266 bset = isl_basic_set_intersect(bset, aff);
2267 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2269 if (bset) {
2270 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2271 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2274 return bset;
2275 error:
2276 isl_basic_set_free(bset);
2277 isl_basic_set_free(context);
2278 return NULL;
2281 /* Normalize the divs in "bmap" in the context of the equalities in "context".
2282 * We simply add the equalities in context to bmap and then do a regular
2283 * div normalizations. Better results can be obtained by normalizing
2284 * only the divs in bmap than do not also appear in context.
2285 * We need to be careful to reduce the divs using the equalities
2286 * so that later calls to isl_basic_map_overlying_set wouldn't introduce
2287 * spurious constraints.
2289 static struct isl_basic_map *normalize_divs_in_context(
2290 struct isl_basic_map *bmap, struct isl_basic_map *context)
2292 int i;
2293 unsigned total_context;
2294 int div_eq;
2296 div_eq = n_pure_div_eq(bmap);
2297 if (div_eq == 0)
2298 return bmap;
2300 bmap = isl_basic_map_cow(bmap);
2301 if (context->n_div > 0)
2302 bmap = isl_basic_map_align_divs(bmap, context);
2304 total_context = isl_basic_map_total_dim(context);
2305 bmap = isl_basic_map_extend_constraints(bmap, context->n_eq, 0);
2306 for (i = 0; i < context->n_eq; ++i) {
2307 int k;
2308 k = isl_basic_map_alloc_equality(bmap);
2309 if (k < 0)
2310 return isl_basic_map_free(bmap);
2311 isl_seq_cpy(bmap->eq[k], context->eq[i], 1 + total_context);
2312 isl_seq_clr(bmap->eq[k] + 1 + total_context,
2313 isl_basic_map_total_dim(bmap) - total_context);
2315 bmap = isl_basic_map_gauss(bmap, NULL);
2316 bmap = normalize_divs(bmap, NULL);
2317 bmap = isl_basic_map_gauss(bmap, NULL);
2318 return bmap;
2321 /* Return a basic map that has the same intersection with "context" as "bmap"
2322 * and that is as "simple" as possible.
2324 * The core computation is performed on the pure constraints.
2325 * When we add back the meaning of the integer divisions, we need
2326 * to (re)introduce the div constraints. If we happen to have
2327 * discovered that some of these integer divisions are equal to
2328 * some affine combination of other variables, then these div
2329 * constraints may end up getting simplified in terms of the equalities,
2330 * resulting in extra inequalities on the other variables that
2331 * may have been removed already or that may not even have been
2332 * part of the input. We try and remove those constraints of
2333 * this form that are most obviously redundant with respect to
2334 * the context. We also remove those div constraints that are
2335 * redundant with respect to the other constraints in the result.
2337 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
2338 struct isl_basic_map *context)
2340 isl_basic_set *bset, *eq;
2341 isl_basic_map *eq_bmap;
2342 unsigned n_div, n_eq, n_ineq;
2344 if (!bmap || !context)
2345 goto error;
2347 if (isl_basic_map_is_universe(bmap)) {
2348 isl_basic_map_free(context);
2349 return bmap;
2351 if (isl_basic_map_plain_is_empty(context)) {
2352 isl_space *space = isl_basic_map_get_space(bmap);
2353 isl_basic_map_free(bmap);
2354 isl_basic_map_free(context);
2355 return isl_basic_map_universe(space);
2357 if (isl_basic_map_plain_is_empty(bmap)) {
2358 isl_basic_map_free(context);
2359 return bmap;
2362 bmap = isl_basic_map_remove_redundancies(bmap);
2363 context = isl_basic_map_remove_redundancies(context);
2364 if (!context)
2365 goto error;
2367 if (context->n_eq)
2368 bmap = normalize_divs_in_context(bmap, context);
2370 context = isl_basic_map_align_divs(context, bmap);
2371 bmap = isl_basic_map_align_divs(bmap, context);
2372 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2374 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
2375 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
2377 if (!bset || bset->n_eq == 0 || n_div == 0 ||
2378 isl_basic_set_plain_is_empty(bset)) {
2379 isl_basic_map_free(context);
2380 return isl_basic_map_overlying_set(bset, bmap);
2383 n_eq = bset->n_eq;
2384 n_ineq = bset->n_ineq;
2385 eq = isl_basic_set_copy(bset);
2386 eq = isl_basic_set_cow(eq);
2387 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
2388 eq = isl_basic_set_free(eq);
2389 if (isl_basic_set_free_equality(bset, n_eq) < 0)
2390 bset = isl_basic_set_free(bset);
2392 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
2393 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
2394 bmap = isl_basic_map_overlying_set(bset, bmap);
2395 bmap = isl_basic_map_intersect(bmap, eq_bmap);
2396 bmap = isl_basic_map_remove_redundancies(bmap);
2398 return bmap;
2399 error:
2400 isl_basic_map_free(bmap);
2401 isl_basic_map_free(context);
2402 return NULL;
2406 * Assumes context has no implicit divs.
2408 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
2409 __isl_take isl_basic_map *context)
2411 int i;
2413 if (!map || !context)
2414 goto error;
2416 if (isl_basic_map_plain_is_empty(context)) {
2417 isl_space *space = isl_map_get_space(map);
2418 isl_map_free(map);
2419 isl_basic_map_free(context);
2420 return isl_map_universe(space);
2423 context = isl_basic_map_remove_redundancies(context);
2424 map = isl_map_cow(map);
2425 if (!map || !context)
2426 goto error;
2427 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
2428 map = isl_map_compute_divs(map);
2429 if (!map)
2430 goto error;
2431 for (i = map->n - 1; i >= 0; --i) {
2432 map->p[i] = isl_basic_map_gist(map->p[i],
2433 isl_basic_map_copy(context));
2434 if (!map->p[i])
2435 goto error;
2436 if (isl_basic_map_plain_is_empty(map->p[i])) {
2437 isl_basic_map_free(map->p[i]);
2438 if (i != map->n - 1)
2439 map->p[i] = map->p[map->n - 1];
2440 map->n--;
2443 isl_basic_map_free(context);
2444 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2445 return map;
2446 error:
2447 isl_map_free(map);
2448 isl_basic_map_free(context);
2449 return NULL;
2452 /* Return a map that has the same intersection with "context" as "map"
2453 * and that is as "simple" as possible.
2455 * If "map" is already the universe, then we cannot make it any simpler.
2456 * Similarly, if "context" is the universe, then we cannot exploit it
2457 * to simplify "map"
2458 * If "map" and "context" are identical to each other, then we can
2459 * return the corresponding universe.
2461 * If none of these cases apply, we have to work a bit harder.
2462 * During this computation, we make use of a single disjunct context,
2463 * so if the original context consists of more than one disjunct
2464 * then we need to approximate the context by a single disjunct set.
2465 * Simply taking the simple hull may drop constraints that are
2466 * only implicitly available in each disjunct. We therefore also
2467 * look for constraints among those defining "map" that are valid
2468 * for the context. These can then be used to simplify away
2469 * the corresponding constraints in "map".
2471 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
2472 __isl_take isl_map *context)
2474 int equal;
2475 int is_universe;
2476 isl_basic_map *hull;
2478 is_universe = isl_map_plain_is_universe(map);
2479 if (is_universe >= 0 && !is_universe)
2480 is_universe = isl_map_plain_is_universe(context);
2481 if (is_universe < 0)
2482 goto error;
2483 if (is_universe) {
2484 isl_map_free(context);
2485 return map;
2488 equal = isl_map_plain_is_equal(map, context);
2489 if (equal < 0)
2490 goto error;
2491 if (equal) {
2492 isl_map *res = isl_map_universe(isl_map_get_space(map));
2493 isl_map_free(map);
2494 isl_map_free(context);
2495 return res;
2498 context = isl_map_compute_divs(context);
2499 if (!context)
2500 goto error;
2501 if (isl_map_n_basic_map(context) == 1) {
2502 hull = isl_map_simple_hull(context);
2503 } else {
2504 isl_ctx *ctx;
2505 isl_map_list *list;
2507 ctx = isl_map_get_ctx(map);
2508 list = isl_map_list_alloc(ctx, 2);
2509 list = isl_map_list_add(list, isl_map_copy(context));
2510 list = isl_map_list_add(list, isl_map_copy(map));
2511 hull = isl_map_unshifted_simple_hull_from_map_list(context,
2512 list);
2514 return isl_map_gist_basic_map(map, hull);
2515 error:
2516 isl_map_free(map);
2517 isl_map_free(context);
2518 return NULL;
2521 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
2522 __isl_take isl_map *context)
2524 return isl_map_align_params_map_map_and(map, context, &map_gist);
2527 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
2528 struct isl_basic_set *context)
2530 return (struct isl_basic_set *)isl_basic_map_gist(
2531 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
2534 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
2535 __isl_take isl_basic_set *context)
2537 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
2538 (struct isl_basic_map *)context);
2541 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
2542 __isl_take isl_basic_set *context)
2544 isl_space *space = isl_set_get_space(set);
2545 isl_basic_set *dom_context = isl_basic_set_universe(space);
2546 dom_context = isl_basic_set_intersect_params(dom_context, context);
2547 return isl_set_gist_basic_set(set, dom_context);
2550 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
2551 __isl_take isl_set *context)
2553 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
2554 (struct isl_map *)context);
2557 /* Compute the gist of "bmap" with respect to the constraints "context"
2558 * on the domain.
2560 __isl_give isl_basic_map *isl_basic_map_gist_domain(
2561 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
2563 isl_space *space = isl_basic_map_get_space(bmap);
2564 isl_basic_map *bmap_context = isl_basic_map_universe(space);
2566 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
2567 return isl_basic_map_gist(bmap, bmap_context);
2570 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
2571 __isl_take isl_set *context)
2573 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2574 map_context = isl_map_intersect_domain(map_context, context);
2575 return isl_map_gist(map, map_context);
2578 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
2579 __isl_take isl_set *context)
2581 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2582 map_context = isl_map_intersect_range(map_context, context);
2583 return isl_map_gist(map, map_context);
2586 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
2587 __isl_take isl_set *context)
2589 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2590 map_context = isl_map_intersect_params(map_context, context);
2591 return isl_map_gist(map, map_context);
2594 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
2595 __isl_take isl_set *context)
2597 return isl_map_gist_params(set, context);
2600 /* Quick check to see if two basic maps are disjoint.
2601 * In particular, we reduce the equalities and inequalities of
2602 * one basic map in the context of the equalities of the other
2603 * basic map and check if we get a contradiction.
2605 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
2606 __isl_keep isl_basic_map *bmap2)
2608 struct isl_vec *v = NULL;
2609 int *elim = NULL;
2610 unsigned total;
2611 int i;
2613 if (!bmap1 || !bmap2)
2614 return isl_bool_error;
2615 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
2616 return isl_bool_error);
2617 if (bmap1->n_div || bmap2->n_div)
2618 return isl_bool_false;
2619 if (!bmap1->n_eq && !bmap2->n_eq)
2620 return isl_bool_false;
2622 total = isl_space_dim(bmap1->dim, isl_dim_all);
2623 if (total == 0)
2624 return isl_bool_false;
2625 v = isl_vec_alloc(bmap1->ctx, 1 + total);
2626 if (!v)
2627 goto error;
2628 elim = isl_alloc_array(bmap1->ctx, int, total);
2629 if (!elim)
2630 goto error;
2631 compute_elimination_index(bmap1, elim);
2632 for (i = 0; i < bmap2->n_eq; ++i) {
2633 int reduced;
2634 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
2635 bmap1, elim);
2636 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
2637 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2638 goto disjoint;
2640 for (i = 0; i < bmap2->n_ineq; ++i) {
2641 int reduced;
2642 reduced = reduced_using_equalities(v->block.data,
2643 bmap2->ineq[i], bmap1, elim);
2644 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2645 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2646 goto disjoint;
2648 compute_elimination_index(bmap2, elim);
2649 for (i = 0; i < bmap1->n_ineq; ++i) {
2650 int reduced;
2651 reduced = reduced_using_equalities(v->block.data,
2652 bmap1->ineq[i], bmap2, elim);
2653 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2654 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2655 goto disjoint;
2657 isl_vec_free(v);
2658 free(elim);
2659 return isl_bool_false;
2660 disjoint:
2661 isl_vec_free(v);
2662 free(elim);
2663 return isl_bool_true;
2664 error:
2665 isl_vec_free(v);
2666 free(elim);
2667 return isl_bool_error;
2670 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
2671 __isl_keep isl_basic_set *bset2)
2673 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
2674 (struct isl_basic_map *)bset2);
2677 /* Are "map1" and "map2" obviously disjoint?
2679 * If one of them is empty or if they live in different spaces (ignoring
2680 * parameters), then they are clearly disjoint.
2682 * If they have different parameters, then we skip any further tests.
2684 * If they are obviously equal, but not obviously empty, then we will
2685 * not be able to detect if they are disjoint.
2687 * Otherwise we check if each basic map in "map1" is obviously disjoint
2688 * from each basic map in "map2".
2690 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
2691 __isl_keep isl_map *map2)
2693 int i, j;
2694 isl_bool disjoint;
2695 isl_bool intersect;
2696 isl_bool match;
2698 if (!map1 || !map2)
2699 return isl_bool_error;
2701 disjoint = isl_map_plain_is_empty(map1);
2702 if (disjoint < 0 || disjoint)
2703 return disjoint;
2705 disjoint = isl_map_plain_is_empty(map2);
2706 if (disjoint < 0 || disjoint)
2707 return disjoint;
2709 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
2710 map2->dim, isl_dim_in);
2711 if (match < 0 || !match)
2712 return match < 0 ? isl_bool_error : isl_bool_true;
2714 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
2715 map2->dim, isl_dim_out);
2716 if (match < 0 || !match)
2717 return match < 0 ? isl_bool_error : isl_bool_true;
2719 match = isl_space_match(map1->dim, isl_dim_param,
2720 map2->dim, isl_dim_param);
2721 if (match < 0 || !match)
2722 return match < 0 ? isl_bool_error : isl_bool_false;
2724 intersect = isl_map_plain_is_equal(map1, map2);
2725 if (intersect < 0 || intersect)
2726 return intersect < 0 ? isl_bool_error : isl_bool_false;
2728 for (i = 0; i < map1->n; ++i) {
2729 for (j = 0; j < map2->n; ++j) {
2730 isl_bool d = isl_basic_map_plain_is_disjoint(map1->p[i],
2731 map2->p[j]);
2732 if (d != isl_bool_true)
2733 return d;
2736 return isl_bool_true;
2739 /* Are "map1" and "map2" disjoint?
2741 * They are disjoint if they are "obviously disjoint" or if one of them
2742 * is empty. Otherwise, they are not disjoint if one of them is universal.
2743 * If none of these cases apply, we compute the intersection and see if
2744 * the result is empty.
2746 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
2748 isl_bool disjoint;
2749 isl_bool intersect;
2750 isl_map *test;
2752 disjoint = isl_map_plain_is_disjoint(map1, map2);
2753 if (disjoint < 0 || disjoint)
2754 return disjoint;
2756 disjoint = isl_map_is_empty(map1);
2757 if (disjoint < 0 || disjoint)
2758 return disjoint;
2760 disjoint = isl_map_is_empty(map2);
2761 if (disjoint < 0 || disjoint)
2762 return disjoint;
2764 intersect = isl_map_plain_is_universe(map1);
2765 if (intersect < 0 || intersect)
2766 return intersect < 0 ? isl_bool_error : isl_bool_false;
2768 intersect = isl_map_plain_is_universe(map2);
2769 if (intersect < 0 || intersect)
2770 return intersect < 0 ? isl_bool_error : isl_bool_false;
2772 test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
2773 disjoint = isl_map_is_empty(test);
2774 isl_map_free(test);
2776 return disjoint;
2779 /* Are "bmap1" and "bmap2" disjoint?
2781 * They are disjoint if they are "obviously disjoint" or if one of them
2782 * is empty. Otherwise, they are not disjoint if one of them is universal.
2783 * If none of these cases apply, we compute the intersection and see if
2784 * the result is empty.
2786 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
2787 __isl_keep isl_basic_map *bmap2)
2789 isl_bool disjoint;
2790 isl_bool intersect;
2791 isl_basic_map *test;
2793 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
2794 if (disjoint < 0 || disjoint)
2795 return disjoint;
2797 disjoint = isl_basic_map_is_empty(bmap1);
2798 if (disjoint < 0 || disjoint)
2799 return disjoint;
2801 disjoint = isl_basic_map_is_empty(bmap2);
2802 if (disjoint < 0 || disjoint)
2803 return disjoint;
2805 intersect = isl_basic_map_is_universe(bmap1);
2806 if (intersect < 0 || intersect)
2807 return intersect < 0 ? isl_bool_error : isl_bool_false;
2809 intersect = isl_basic_map_is_universe(bmap2);
2810 if (intersect < 0 || intersect)
2811 return intersect < 0 ? isl_bool_error : isl_bool_false;
2813 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
2814 isl_basic_map_copy(bmap2));
2815 disjoint = isl_basic_map_is_empty(test);
2816 isl_basic_map_free(test);
2818 return disjoint;
2821 /* Are "bset1" and "bset2" disjoint?
2823 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
2824 __isl_keep isl_basic_set *bset2)
2826 return isl_basic_map_is_disjoint(bset1, bset2);
2829 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
2830 __isl_keep isl_set *set2)
2832 return isl_map_plain_is_disjoint((struct isl_map *)set1,
2833 (struct isl_map *)set2);
2836 /* Are "set1" and "set2" disjoint?
2838 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
2840 return isl_map_is_disjoint(set1, set2);
2843 /* Check if we can combine a given div with lower bound l and upper
2844 * bound u with some other div and if so return that other div.
2845 * Otherwise return -1.
2847 * We first check that
2848 * - the bounds are opposites of each other (except for the constant
2849 * term)
2850 * - the bounds do not reference any other div
2851 * - no div is defined in terms of this div
2853 * Let m be the size of the range allowed on the div by the bounds.
2854 * That is, the bounds are of the form
2856 * e <= a <= e + m - 1
2858 * with e some expression in the other variables.
2859 * We look for another div b such that no third div is defined in terms
2860 * of this second div b and such that in any constraint that contains
2861 * a (except for the given lower and upper bound), also contains b
2862 * with a coefficient that is m times that of b.
2863 * That is, all constraints (execpt for the lower and upper bound)
2864 * are of the form
2866 * e + f (a + m b) >= 0
2868 * If so, we return b so that "a + m b" can be replaced by
2869 * a single div "c = a + m b".
2871 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
2872 unsigned div, unsigned l, unsigned u)
2874 int i, j;
2875 unsigned dim;
2876 int coalesce = -1;
2878 if (bmap->n_div <= 1)
2879 return -1;
2880 dim = isl_space_dim(bmap->dim, isl_dim_all);
2881 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
2882 return -1;
2883 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
2884 bmap->n_div - div - 1) != -1)
2885 return -1;
2886 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
2887 dim + bmap->n_div))
2888 return -1;
2890 for (i = 0; i < bmap->n_div; ++i) {
2891 if (isl_int_is_zero(bmap->div[i][0]))
2892 continue;
2893 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
2894 return -1;
2897 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2898 if (isl_int_is_neg(bmap->ineq[l][0])) {
2899 isl_int_sub(bmap->ineq[l][0],
2900 bmap->ineq[l][0], bmap->ineq[u][0]);
2901 bmap = isl_basic_map_copy(bmap);
2902 bmap = isl_basic_map_set_to_empty(bmap);
2903 isl_basic_map_free(bmap);
2904 return -1;
2906 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2907 for (i = 0; i < bmap->n_div; ++i) {
2908 if (i == div)
2909 continue;
2910 if (!pairs[i])
2911 continue;
2912 for (j = 0; j < bmap->n_div; ++j) {
2913 if (isl_int_is_zero(bmap->div[j][0]))
2914 continue;
2915 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
2916 break;
2918 if (j < bmap->n_div)
2919 continue;
2920 for (j = 0; j < bmap->n_ineq; ++j) {
2921 int valid;
2922 if (j == l || j == u)
2923 continue;
2924 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
2925 continue;
2926 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
2927 break;
2928 isl_int_mul(bmap->ineq[j][1 + dim + div],
2929 bmap->ineq[j][1 + dim + div],
2930 bmap->ineq[l][0]);
2931 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
2932 bmap->ineq[j][1 + dim + i]);
2933 isl_int_divexact(bmap->ineq[j][1 + dim + div],
2934 bmap->ineq[j][1 + dim + div],
2935 bmap->ineq[l][0]);
2936 if (!valid)
2937 break;
2939 if (j < bmap->n_ineq)
2940 continue;
2941 coalesce = i;
2942 break;
2944 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2945 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2946 return coalesce;
2949 /* Given a lower and an upper bound on div i, construct an inequality
2950 * that when nonnegative ensures that this pair of bounds always allows
2951 * for an integer value of the given div.
2952 * The lower bound is inequality l, while the upper bound is inequality u.
2953 * The constructed inequality is stored in ineq.
2954 * g, fl, fu are temporary scalars.
2956 * Let the upper bound be
2958 * -n_u a + e_u >= 0
2960 * and the lower bound
2962 * n_l a + e_l >= 0
2964 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2965 * We have
2967 * - f_u e_l <= f_u f_l g a <= f_l e_u
2969 * Since all variables are integer valued, this is equivalent to
2971 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2973 * If this interval is at least f_u f_l g, then it contains at least
2974 * one integer value for a.
2975 * That is, the test constraint is
2977 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2979 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
2980 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
2982 unsigned dim;
2983 dim = isl_space_dim(bmap->dim, isl_dim_all);
2985 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
2986 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
2987 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
2988 isl_int_neg(fu, fu);
2989 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
2990 1 + dim + bmap->n_div);
2991 isl_int_add(ineq[0], ineq[0], fl);
2992 isl_int_add(ineq[0], ineq[0], fu);
2993 isl_int_sub_ui(ineq[0], ineq[0], 1);
2994 isl_int_mul(g, g, fl);
2995 isl_int_mul(g, g, fu);
2996 isl_int_sub(ineq[0], ineq[0], g);
2999 /* Remove more kinds of divs that are not strictly needed.
3000 * In particular, if all pairs of lower and upper bounds on a div
3001 * are such that they allow at least one integer value of the div,
3002 * the we can eliminate the div using Fourier-Motzkin without
3003 * introducing any spurious solutions.
3005 static struct isl_basic_map *drop_more_redundant_divs(
3006 struct isl_basic_map *bmap, int *pairs, int n)
3008 struct isl_tab *tab = NULL;
3009 struct isl_vec *vec = NULL;
3010 unsigned dim;
3011 int remove = -1;
3012 isl_int g, fl, fu;
3014 isl_int_init(g);
3015 isl_int_init(fl);
3016 isl_int_init(fu);
3018 if (!bmap)
3019 goto error;
3021 dim = isl_space_dim(bmap->dim, isl_dim_all);
3022 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
3023 if (!vec)
3024 goto error;
3026 tab = isl_tab_from_basic_map(bmap, 0);
3028 while (n > 0) {
3029 int i, l, u;
3030 int best = -1;
3031 enum isl_lp_result res;
3033 for (i = 0; i < bmap->n_div; ++i) {
3034 if (!pairs[i])
3035 continue;
3036 if (best >= 0 && pairs[best] <= pairs[i])
3037 continue;
3038 best = i;
3041 i = best;
3042 for (l = 0; l < bmap->n_ineq; ++l) {
3043 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
3044 continue;
3045 for (u = 0; u < bmap->n_ineq; ++u) {
3046 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
3047 continue;
3048 construct_test_ineq(bmap, i, l, u,
3049 vec->el, g, fl, fu);
3050 res = isl_tab_min(tab, vec->el,
3051 bmap->ctx->one, &g, NULL, 0);
3052 if (res == isl_lp_error)
3053 goto error;
3054 if (res == isl_lp_empty) {
3055 bmap = isl_basic_map_set_to_empty(bmap);
3056 break;
3058 if (res != isl_lp_ok || isl_int_is_neg(g))
3059 break;
3061 if (u < bmap->n_ineq)
3062 break;
3064 if (l == bmap->n_ineq) {
3065 remove = i;
3066 break;
3068 pairs[i] = 0;
3069 --n;
3072 isl_tab_free(tab);
3073 isl_vec_free(vec);
3075 isl_int_clear(g);
3076 isl_int_clear(fl);
3077 isl_int_clear(fu);
3079 free(pairs);
3081 if (remove < 0)
3082 return bmap;
3084 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
3085 return isl_basic_map_drop_redundant_divs(bmap);
3086 error:
3087 free(pairs);
3088 isl_basic_map_free(bmap);
3089 isl_tab_free(tab);
3090 isl_vec_free(vec);
3091 isl_int_clear(g);
3092 isl_int_clear(fl);
3093 isl_int_clear(fu);
3094 return NULL;
3097 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
3098 * and the upper bound u, div1 always occurs together with div2 in the form
3099 * (div1 + m div2), where m is the constant range on the variable div1
3100 * allowed by l and u, replace the pair div1 and div2 by a single
3101 * div that is equal to div1 + m div2.
3103 * The new div will appear in the location that contains div2.
3104 * We need to modify all constraints that contain
3105 * div2 = (div - div1) / m
3106 * (If a constraint does not contain div2, it will also not contain div1.)
3107 * If the constraint also contains div1, then we know they appear
3108 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3109 * i.e., the coefficient of div is f.
3111 * Otherwise, we first need to introduce div1 into the constraint.
3112 * Let the l be
3114 * div1 + f >=0
3116 * and u
3118 * -div1 + f' >= 0
3120 * A lower bound on div2
3122 * n div2 + t >= 0
3124 * can be replaced by
3126 * (n * (m div 2 + div1) + m t + n f)/g >= 0
3128 * with g = gcd(m,n).
3129 * An upper bound
3131 * -n div2 + t >= 0
3133 * can be replaced by
3135 * (-n * (m div2 + div1) + m t + n f')/g >= 0
3137 * These constraint are those that we would obtain from eliminating
3138 * div1 using Fourier-Motzkin.
3140 * After all constraints have been modified, we drop the lower and upper
3141 * bound and then drop div1.
3143 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
3144 unsigned div1, unsigned div2, unsigned l, unsigned u)
3146 isl_int a;
3147 isl_int b;
3148 isl_int m;
3149 unsigned dim, total;
3150 int i;
3152 dim = isl_space_dim(bmap->dim, isl_dim_all);
3153 total = 1 + dim + bmap->n_div;
3155 isl_int_init(a);
3156 isl_int_init(b);
3157 isl_int_init(m);
3158 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
3159 isl_int_add_ui(m, m, 1);
3161 for (i = 0; i < bmap->n_ineq; ++i) {
3162 if (i == l || i == u)
3163 continue;
3164 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
3165 continue;
3166 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
3167 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
3168 isl_int_divexact(a, m, b);
3169 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
3170 if (isl_int_is_pos(b)) {
3171 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3172 b, bmap->ineq[l], total);
3173 } else {
3174 isl_int_neg(b, b);
3175 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3176 b, bmap->ineq[u], total);
3179 isl_int_set(bmap->ineq[i][1 + dim + div2],
3180 bmap->ineq[i][1 + dim + div1]);
3181 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
3184 isl_int_clear(a);
3185 isl_int_clear(b);
3186 isl_int_clear(m);
3187 if (l > u) {
3188 isl_basic_map_drop_inequality(bmap, l);
3189 isl_basic_map_drop_inequality(bmap, u);
3190 } else {
3191 isl_basic_map_drop_inequality(bmap, u);
3192 isl_basic_map_drop_inequality(bmap, l);
3194 bmap = isl_basic_map_drop_div(bmap, div1);
3195 return bmap;
3198 /* First check if we can coalesce any pair of divs and
3199 * then continue with dropping more redundant divs.
3201 * We loop over all pairs of lower and upper bounds on a div
3202 * with coefficient 1 and -1, respectively, check if there
3203 * is any other div "c" with which we can coalesce the div
3204 * and if so, perform the coalescing.
3206 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
3207 struct isl_basic_map *bmap, int *pairs, int n)
3209 int i, l, u;
3210 unsigned dim;
3212 dim = isl_space_dim(bmap->dim, isl_dim_all);
3214 for (i = 0; i < bmap->n_div; ++i) {
3215 if (!pairs[i])
3216 continue;
3217 for (l = 0; l < bmap->n_ineq; ++l) {
3218 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
3219 continue;
3220 for (u = 0; u < bmap->n_ineq; ++u) {
3221 int c;
3223 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
3224 continue;
3225 c = div_find_coalesce(bmap, pairs, i, l, u);
3226 if (c < 0)
3227 continue;
3228 free(pairs);
3229 bmap = coalesce_divs(bmap, i, c, l, u);
3230 return isl_basic_map_drop_redundant_divs(bmap);
3235 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3236 return bmap;
3238 return drop_more_redundant_divs(bmap, pairs, n);
3241 /* Remove divs that are not strictly needed.
3242 * In particular, if a div only occurs positively (or negatively)
3243 * in constraints, then it can simply be dropped.
3244 * Also, if a div occurs in only two constraints and if moreover
3245 * those two constraints are opposite to each other, except for the constant
3246 * term and if the sum of the constant terms is such that for any value
3247 * of the other values, there is always at least one integer value of the
3248 * div, i.e., if one plus this sum is greater than or equal to
3249 * the (absolute value) of the coefficent of the div in the constraints,
3250 * then we can also simply drop the div.
3252 * We skip divs that appear in equalities or in the definition of other divs.
3253 * Divs that appear in the definition of other divs usually occur in at least
3254 * 4 constraints, but the constraints may have been simplified.
3256 * If any divs are left after these simple checks then we move on
3257 * to more complicated cases in drop_more_redundant_divs.
3259 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
3260 struct isl_basic_map *bmap)
3262 int i, j;
3263 unsigned off;
3264 int *pairs = NULL;
3265 int n = 0;
3267 if (!bmap)
3268 goto error;
3269 if (bmap->n_div == 0)
3270 return bmap;
3272 off = isl_space_dim(bmap->dim, isl_dim_all);
3273 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
3274 if (!pairs)
3275 goto error;
3277 for (i = 0; i < bmap->n_div; ++i) {
3278 int pos, neg;
3279 int last_pos, last_neg;
3280 int redundant;
3281 int defined;
3283 defined = !isl_int_is_zero(bmap->div[i][0]);
3284 for (j = i; j < bmap->n_div; ++j)
3285 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
3286 break;
3287 if (j < bmap->n_div)
3288 continue;
3289 for (j = 0; j < bmap->n_eq; ++j)
3290 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
3291 break;
3292 if (j < bmap->n_eq)
3293 continue;
3294 ++n;
3295 pos = neg = 0;
3296 for (j = 0; j < bmap->n_ineq; ++j) {
3297 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
3298 last_pos = j;
3299 ++pos;
3301 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
3302 last_neg = j;
3303 ++neg;
3306 pairs[i] = pos * neg;
3307 if (pairs[i] == 0) {
3308 for (j = bmap->n_ineq - 1; j >= 0; --j)
3309 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
3310 isl_basic_map_drop_inequality(bmap, j);
3311 bmap = isl_basic_map_drop_div(bmap, i);
3312 free(pairs);
3313 return isl_basic_map_drop_redundant_divs(bmap);
3315 if (pairs[i] != 1)
3316 continue;
3317 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
3318 bmap->ineq[last_neg] + 1,
3319 off + bmap->n_div))
3320 continue;
3322 isl_int_add(bmap->ineq[last_pos][0],
3323 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3324 isl_int_add_ui(bmap->ineq[last_pos][0],
3325 bmap->ineq[last_pos][0], 1);
3326 redundant = isl_int_ge(bmap->ineq[last_pos][0],
3327 bmap->ineq[last_pos][1+off+i]);
3328 isl_int_sub_ui(bmap->ineq[last_pos][0],
3329 bmap->ineq[last_pos][0], 1);
3330 isl_int_sub(bmap->ineq[last_pos][0],
3331 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3332 if (!redundant) {
3333 if (defined ||
3334 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
3335 pairs[i] = 0;
3336 --n;
3337 continue;
3339 bmap = set_div_from_lower_bound(bmap, i, last_pos);
3340 bmap = isl_basic_map_simplify(bmap);
3341 free(pairs);
3342 return isl_basic_map_drop_redundant_divs(bmap);
3344 if (last_pos > last_neg) {
3345 isl_basic_map_drop_inequality(bmap, last_pos);
3346 isl_basic_map_drop_inequality(bmap, last_neg);
3347 } else {
3348 isl_basic_map_drop_inequality(bmap, last_neg);
3349 isl_basic_map_drop_inequality(bmap, last_pos);
3351 bmap = isl_basic_map_drop_div(bmap, i);
3352 free(pairs);
3353 return isl_basic_map_drop_redundant_divs(bmap);
3356 if (n > 0)
3357 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
3359 free(pairs);
3360 return bmap;
3361 error:
3362 free(pairs);
3363 isl_basic_map_free(bmap);
3364 return NULL;
3367 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
3368 struct isl_basic_set *bset)
3370 return (struct isl_basic_set *)
3371 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
3374 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
3376 int i;
3378 if (!map)
3379 return NULL;
3380 for (i = 0; i < map->n; ++i) {
3381 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
3382 if (!map->p[i])
3383 goto error;
3385 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3386 return map;
3387 error:
3388 isl_map_free(map);
3389 return NULL;
3392 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
3394 return (struct isl_set *)
3395 isl_map_drop_redundant_divs((struct isl_map *)set);
3398 /* Does "bmap" satisfy any equality that involves more than 2 variables
3399 * and/or has coefficients different from -1 and 1?
3401 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
3403 int i;
3404 unsigned total;
3406 total = isl_basic_map_dim(bmap, isl_dim_all);
3408 for (i = 0; i < bmap->n_eq; ++i) {
3409 int j, k;
3411 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
3412 if (j < 0)
3413 continue;
3414 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3415 !isl_int_is_negone(bmap->eq[i][1 + j]))
3416 return 1;
3418 j += 1;
3419 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3420 if (k < 0)
3421 continue;
3422 j += k;
3423 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3424 !isl_int_is_negone(bmap->eq[i][1 + j]))
3425 return 1;
3427 j += 1;
3428 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3429 if (k >= 0)
3430 return 1;
3433 return 0;
3436 /* Remove any common factor g from the constraint coefficients in "v".
3437 * The constant term is stored in the first position and is replaced
3438 * by floor(c/g). If any common factor is removed and if this results
3439 * in a tightening of the constraint, then set *tightened.
3441 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
3442 int *tightened)
3444 isl_ctx *ctx;
3446 if (!v)
3447 return NULL;
3448 ctx = isl_vec_get_ctx(v);
3449 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
3450 if (isl_int_is_zero(ctx->normalize_gcd))
3451 return v;
3452 if (isl_int_is_one(ctx->normalize_gcd))
3453 return v;
3454 v = isl_vec_cow(v);
3455 if (!v)
3456 return NULL;
3457 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
3458 *tightened = 1;
3459 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
3460 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
3461 v->size - 1);
3462 return v;
3465 /* If "bmap" is an integer set that satisfies any equality involving
3466 * more than 2 variables and/or has coefficients different from -1 and 1,
3467 * then use variable compression to reduce the coefficients by removing
3468 * any (hidden) common factor.
3469 * In particular, apply the variable compression to each constraint,
3470 * factor out any common factor in the non-constant coefficients and
3471 * then apply the inverse of the compression.
3472 * At the end, we mark the basic map as having reduced constants.
3473 * If this flag is still set on the next invocation of this function,
3474 * then we skip the computation.
3476 * Removing a common factor may result in a tightening of some of
3477 * the constraints. If this happens, then we may end up with two
3478 * opposite inequalities that can be replaced by an equality.
3479 * We therefore call isl_basic_map_detect_inequality_pairs,
3480 * which checks for such pairs of inequalities as well as eliminate_divs_eq
3481 * and isl_basic_map_gauss if such a pair was found.
3483 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
3484 __isl_take isl_basic_map *bmap)
3486 unsigned total;
3487 isl_ctx *ctx;
3488 isl_vec *v;
3489 isl_mat *eq, *T, *T2;
3490 int i;
3491 int tightened;
3493 if (!bmap)
3494 return NULL;
3495 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
3496 return bmap;
3497 if (isl_basic_map_is_rational(bmap))
3498 return bmap;
3499 if (bmap->n_eq == 0)
3500 return bmap;
3501 if (!has_multiple_var_equality(bmap))
3502 return bmap;
3504 total = isl_basic_map_dim(bmap, isl_dim_all);
3505 ctx = isl_basic_map_get_ctx(bmap);
3506 v = isl_vec_alloc(ctx, 1 + total);
3507 if (!v)
3508 return isl_basic_map_free(bmap);
3510 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3511 T = isl_mat_variable_compression(eq, &T2);
3512 if (!T || !T2)
3513 goto error;
3514 if (T->n_col == 0) {
3515 isl_mat_free(T);
3516 isl_mat_free(T2);
3517 isl_vec_free(v);
3518 return isl_basic_map_set_to_empty(bmap);
3521 tightened = 0;
3522 for (i = 0; i < bmap->n_ineq; ++i) {
3523 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
3524 v = isl_vec_mat_product(v, isl_mat_copy(T));
3525 v = normalize_constraint(v, &tightened);
3526 v = isl_vec_mat_product(v, isl_mat_copy(T2));
3527 if (!v)
3528 goto error;
3529 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
3532 isl_mat_free(T);
3533 isl_mat_free(T2);
3534 isl_vec_free(v);
3536 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
3538 if (tightened) {
3539 int progress = 0;
3541 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
3542 if (progress) {
3543 bmap = eliminate_divs_eq(bmap, &progress);
3544 bmap = isl_basic_map_gauss(bmap, NULL);
3548 return bmap;
3549 error:
3550 isl_mat_free(T);
3551 isl_mat_free(T2);
3552 isl_vec_free(v);
3553 return isl_basic_map_free(bmap);
3556 /* Shift the integer division at position "div" of "bmap"
3557 * by "shift" times the variable at position "pos".
3558 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
3559 * corresponds to the constant term.
3561 * That is, if the integer division has the form
3563 * floor(f(x)/d)
3565 * then replace it by
3567 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
3569 __isl_give isl_basic_map *isl_basic_map_shift_div(
3570 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
3572 int i;
3573 unsigned total;
3575 if (!bmap)
3576 return NULL;
3578 total = isl_basic_map_dim(bmap, isl_dim_all);
3579 total -= isl_basic_map_dim(bmap, isl_dim_div);
3581 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
3583 for (i = 0; i < bmap->n_eq; ++i) {
3584 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
3585 continue;
3586 isl_int_submul(bmap->eq[i][pos],
3587 shift, bmap->eq[i][1 + total + div]);
3589 for (i = 0; i < bmap->n_ineq; ++i) {
3590 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
3591 continue;
3592 isl_int_submul(bmap->ineq[i][pos],
3593 shift, bmap->ineq[i][1 + total + div]);
3595 for (i = 0; i < bmap->n_div; ++i) {
3596 if (isl_int_is_zero(bmap->div[i][0]))
3597 continue;
3598 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
3599 continue;
3600 isl_int_submul(bmap->div[i][1 + pos],
3601 shift, bmap->div[i][1 + 1 + total + div]);
3604 return bmap;