isl_union_pw_multi_aff_from_multi_union_pw_aff: plug memory leak on error path
[isl.git] / isl_map_simplify.c
blobf4660c7420b467bc81a6ac155d1e5f6e1952cf9e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 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 /* Hash table of inequalities in a basic map.
764 * "index" is an array of addresses of inequalities in the basic map, some
765 * of which are NULL. The inequalities are hashed on the coefficients
766 * except the constant term.
767 * "size" is the number of elements in the array and is always a power of two
768 * "bits" is the number of bits need to represent an index into the array.
769 * "total" is the total dimension of the basic map.
771 struct isl_constraint_index {
772 unsigned int size;
773 int bits;
774 isl_int ***index;
775 unsigned total;
778 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
780 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
781 __isl_keep isl_basic_map *bmap)
783 isl_ctx *ctx;
785 ci->index = NULL;
786 if (!bmap)
787 return isl_stat_error;
788 ci->total = isl_basic_set_total_dim(bmap);
789 if (bmap->n_ineq == 0)
790 return isl_stat_ok;
791 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
792 ci->bits = ffs(ci->size) - 1;
793 ctx = isl_basic_map_get_ctx(bmap);
794 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
795 if (!ci->index)
796 return isl_stat_error;
798 return isl_stat_ok;
801 /* Free the memory allocated by create_constraint_index.
803 static void constraint_index_free(struct isl_constraint_index *ci)
805 free(ci->index);
808 /* Return the position in ci->index that contains the address of
809 * an inequality that is equal to *ineq up to the constant term,
810 * provided this address is not identical to "ineq".
811 * If there is no such inequality, then return the position where
812 * such an inequality should be inserted.
814 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
816 int h;
817 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
818 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
819 if (ineq != ci->index[h] &&
820 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
821 break;
822 return h;
825 /* Return the position in ci->index that contains the address of
826 * an inequality that is equal to the k'th inequality of "bmap"
827 * up to the constant term, provided it does not point to the very
828 * same inequality.
829 * If there is no such inequality, then return the position where
830 * such an inequality should be inserted.
832 static int hash_index(struct isl_constraint_index *ci,
833 __isl_keep isl_basic_map *bmap, int k)
835 return hash_index_ineq(ci, &bmap->ineq[k]);
838 static int set_hash_index(struct isl_constraint_index *ci,
839 struct isl_basic_set *bset, int k)
841 return hash_index(ci, bset, k);
844 /* Fill in the "ci" data structure with the inequalities of "bset".
846 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
847 __isl_keep isl_basic_set *bset)
849 int k, h;
851 if (create_constraint_index(ci, bset) < 0)
852 return isl_stat_error;
854 for (k = 0; k < bset->n_ineq; ++k) {
855 h = set_hash_index(ci, bset, k);
856 ci->index[h] = &bset->ineq[k];
859 return isl_stat_ok;
862 /* Is the inequality ineq (obviously) redundant with respect
863 * to the constraints in "ci"?
865 * Look for an inequality in "ci" with the same coefficients and then
866 * check if the contant term of "ineq" is greater than or equal
867 * to the constant term of that inequality. If so, "ineq" is clearly
868 * redundant.
870 * Note that hash_index_ineq ignores a stored constraint if it has
871 * the same address as the passed inequality. It is ok to pass
872 * the address of a local variable here since it will never be
873 * the same as the address of a constraint in "ci".
875 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
876 isl_int *ineq)
878 int h;
880 h = hash_index_ineq(ci, &ineq);
881 if (!ci->index[h])
882 return isl_bool_false;
883 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
886 /* If we can eliminate more than one div, then we need to make
887 * sure we do it from last div to first div, in order not to
888 * change the position of the other divs that still need to
889 * be removed.
891 static struct isl_basic_map *remove_duplicate_divs(
892 struct isl_basic_map *bmap, int *progress)
894 unsigned int size;
895 int *index;
896 int *elim_for;
897 int k, l, h;
898 int bits;
899 struct isl_blk eq;
900 unsigned total_var;
901 unsigned total;
902 struct isl_ctx *ctx;
904 bmap = isl_basic_map_order_divs(bmap);
905 if (!bmap || bmap->n_div <= 1)
906 return bmap;
908 total_var = isl_space_dim(bmap->dim, isl_dim_all);
909 total = total_var + bmap->n_div;
911 ctx = bmap->ctx;
912 for (k = bmap->n_div - 1; k >= 0; --k)
913 if (!isl_int_is_zero(bmap->div[k][0]))
914 break;
915 if (k <= 0)
916 return bmap;
918 size = round_up(4 * bmap->n_div / 3 - 1);
919 if (size == 0)
920 return bmap;
921 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
922 bits = ffs(size) - 1;
923 index = isl_calloc_array(ctx, int, size);
924 if (!elim_for || !index)
925 goto out;
926 eq = isl_blk_alloc(ctx, 1+total);
927 if (isl_blk_is_error(eq))
928 goto out;
930 isl_seq_clr(eq.data, 1+total);
931 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
932 for (--k; k >= 0; --k) {
933 uint32_t hash;
935 if (isl_int_is_zero(bmap->div[k][0]))
936 continue;
938 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
939 for (h = hash; index[h]; h = (h+1) % size)
940 if (isl_seq_eq(bmap->div[k],
941 bmap->div[index[h]-1], 2+total))
942 break;
943 if (index[h]) {
944 *progress = 1;
945 l = index[h] - 1;
946 elim_for[l] = k + 1;
948 index[h] = k+1;
950 for (l = bmap->n_div - 1; l >= 0; --l) {
951 if (!elim_for[l])
952 continue;
953 k = elim_for[l] - 1;
954 isl_int_set_si(eq.data[1+total_var+k], -1);
955 isl_int_set_si(eq.data[1+total_var+l], 1);
956 bmap = eliminate_div(bmap, eq.data, l, 1);
957 if (!bmap)
958 break;
959 isl_int_set_si(eq.data[1+total_var+k], 0);
960 isl_int_set_si(eq.data[1+total_var+l], 0);
963 isl_blk_free(ctx, eq);
964 out:
965 free(index);
966 free(elim_for);
967 return bmap;
970 static int n_pure_div_eq(struct isl_basic_map *bmap)
972 int i, j;
973 unsigned total;
975 total = isl_space_dim(bmap->dim, isl_dim_all);
976 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
977 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
978 --j;
979 if (j < 0)
980 break;
981 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
982 return 0;
984 return i;
987 /* Normalize divs that appear in equalities.
989 * In particular, we assume that bmap contains some equalities
990 * of the form
992 * a x = m * e_i
994 * and we want to replace the set of e_i by a minimal set and
995 * such that the new e_i have a canonical representation in terms
996 * of the vector x.
997 * If any of the equalities involves more than one divs, then
998 * we currently simply bail out.
1000 * Let us first additionally assume that all equalities involve
1001 * a div. The equalities then express modulo constraints on the
1002 * remaining variables and we can use "parameter compression"
1003 * to find a minimal set of constraints. The result is a transformation
1005 * x = T(x') = x_0 + G x'
1007 * with G a lower-triangular matrix with all elements below the diagonal
1008 * non-negative and smaller than the diagonal element on the same row.
1009 * We first normalize x_0 by making the same property hold in the affine
1010 * T matrix.
1011 * The rows i of G with a 1 on the diagonal do not impose any modulo
1012 * constraint and simply express x_i = x'_i.
1013 * For each of the remaining rows i, we introduce a div and a corresponding
1014 * equality. In particular
1016 * g_ii e_j = x_i - g_i(x')
1018 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1019 * corresponding div (if g_kk != 1).
1021 * If there are any equalities not involving any div, then we
1022 * first apply a variable compression on the variables x:
1024 * x = C x'' x'' = C_2 x
1026 * and perform the above parameter compression on A C instead of on A.
1027 * The resulting compression is then of the form
1029 * x'' = T(x') = x_0 + G x'
1031 * and in constructing the new divs and the corresponding equalities,
1032 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1033 * by the corresponding row from C_2.
1035 static struct isl_basic_map *normalize_divs(
1036 struct isl_basic_map *bmap, int *progress)
1038 int i, j, k;
1039 int total;
1040 int div_eq;
1041 struct isl_mat *B;
1042 struct isl_vec *d;
1043 struct isl_mat *T = NULL;
1044 struct isl_mat *C = NULL;
1045 struct isl_mat *C2 = NULL;
1046 isl_int v;
1047 int *pos;
1048 int dropped, needed;
1050 if (!bmap)
1051 return NULL;
1053 if (bmap->n_div == 0)
1054 return bmap;
1056 if (bmap->n_eq == 0)
1057 return bmap;
1059 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1060 return bmap;
1062 total = isl_space_dim(bmap->dim, isl_dim_all);
1063 div_eq = n_pure_div_eq(bmap);
1064 if (div_eq == 0)
1065 return bmap;
1067 if (div_eq < bmap->n_eq) {
1068 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1069 bmap->n_eq - div_eq, 0, 1 + total);
1070 C = isl_mat_variable_compression(B, &C2);
1071 if (!C || !C2)
1072 goto error;
1073 if (C->n_col == 0) {
1074 bmap = isl_basic_map_set_to_empty(bmap);
1075 isl_mat_free(C);
1076 isl_mat_free(C2);
1077 goto done;
1081 d = isl_vec_alloc(bmap->ctx, div_eq);
1082 if (!d)
1083 goto error;
1084 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1085 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1086 --j;
1087 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1089 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1091 if (C) {
1092 B = isl_mat_product(B, C);
1093 C = NULL;
1096 T = isl_mat_parameter_compression(B, d);
1097 if (!T)
1098 goto error;
1099 if (T->n_col == 0) {
1100 bmap = isl_basic_map_set_to_empty(bmap);
1101 isl_mat_free(C2);
1102 isl_mat_free(T);
1103 goto done;
1105 isl_int_init(v);
1106 for (i = 0; i < T->n_row - 1; ++i) {
1107 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1108 if (isl_int_is_zero(v))
1109 continue;
1110 isl_mat_col_submul(T, 0, v, 1 + i);
1112 isl_int_clear(v);
1113 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1114 if (!pos)
1115 goto error;
1116 /* We have to be careful because dropping equalities may reorder them */
1117 dropped = 0;
1118 for (j = bmap->n_div - 1; j >= 0; --j) {
1119 for (i = 0; i < bmap->n_eq; ++i)
1120 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1121 break;
1122 if (i < bmap->n_eq) {
1123 bmap = isl_basic_map_drop_div(bmap, j);
1124 isl_basic_map_drop_equality(bmap, i);
1125 ++dropped;
1128 pos[0] = 0;
1129 needed = 0;
1130 for (i = 1; i < T->n_row; ++i) {
1131 if (isl_int_is_one(T->row[i][i]))
1132 pos[i] = i;
1133 else
1134 needed++;
1136 if (needed > dropped) {
1137 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1138 needed, needed, 0);
1139 if (!bmap)
1140 goto error;
1142 for (i = 1; i < T->n_row; ++i) {
1143 if (isl_int_is_one(T->row[i][i]))
1144 continue;
1145 k = isl_basic_map_alloc_div(bmap);
1146 pos[i] = 1 + total + k;
1147 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1148 isl_int_set(bmap->div[k][0], T->row[i][i]);
1149 if (C2)
1150 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1151 else
1152 isl_int_set_si(bmap->div[k][1 + i], 1);
1153 for (j = 0; j < i; ++j) {
1154 if (isl_int_is_zero(T->row[i][j]))
1155 continue;
1156 if (pos[j] < T->n_row && C2)
1157 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1158 C2->row[pos[j]], 1 + total);
1159 else
1160 isl_int_neg(bmap->div[k][1 + pos[j]],
1161 T->row[i][j]);
1163 j = isl_basic_map_alloc_equality(bmap);
1164 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1165 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1167 free(pos);
1168 isl_mat_free(C2);
1169 isl_mat_free(T);
1171 if (progress)
1172 *progress = 1;
1173 done:
1174 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1176 return bmap;
1177 error:
1178 isl_mat_free(C);
1179 isl_mat_free(C2);
1180 isl_mat_free(T);
1181 return bmap;
1184 static struct isl_basic_map *set_div_from_lower_bound(
1185 struct isl_basic_map *bmap, int div, int ineq)
1187 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1189 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1190 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1191 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1192 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1193 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1195 return bmap;
1198 /* Check whether it is ok to define a div based on an inequality.
1199 * To avoid the introduction of circular definitions of divs, we
1200 * do not allow such a definition if the resulting expression would refer to
1201 * any other undefined divs or if any known div is defined in
1202 * terms of the unknown div.
1204 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1205 int div, int ineq)
1207 int j;
1208 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1210 /* Not defined in terms of unknown divs */
1211 for (j = 0; j < bmap->n_div; ++j) {
1212 if (div == j)
1213 continue;
1214 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1215 continue;
1216 if (isl_int_is_zero(bmap->div[j][0]))
1217 return 0;
1220 /* No other div defined in terms of this one => avoid loops */
1221 for (j = 0; j < bmap->n_div; ++j) {
1222 if (div == j)
1223 continue;
1224 if (isl_int_is_zero(bmap->div[j][0]))
1225 continue;
1226 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1227 return 0;
1230 return 1;
1233 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1234 * be a better expression than the current one?
1236 * If we do not have any expression yet, then any expression would be better.
1237 * Otherwise we check if the last variable involved in the inequality
1238 * (disregarding the div that it would define) is in an earlier position
1239 * than the last variable involved in the current div expression.
1241 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1242 int div, int ineq)
1244 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1245 int last_div;
1246 int last_ineq;
1248 if (isl_int_is_zero(bmap->div[div][0]))
1249 return 1;
1251 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1252 bmap->n_div - (div + 1)) >= 0)
1253 return 0;
1255 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1256 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1257 total + bmap->n_div);
1259 return last_ineq < last_div;
1262 /* Given two constraints "k" and "l" that are opposite to each other,
1263 * except for the constant term, check if we can use them
1264 * to obtain an expression for one of the hitherto unknown divs or
1265 * a "better" expression for a div for which we already have an expression.
1266 * "sum" is the sum of the constant terms of the constraints.
1267 * If this sum is strictly smaller than the coefficient of one
1268 * of the divs, then this pair can be used define the div.
1269 * To avoid the introduction of circular definitions of divs, we
1270 * do not use the pair if the resulting expression would refer to
1271 * any other undefined divs or if any known div is defined in
1272 * terms of the unknown div.
1274 static struct isl_basic_map *check_for_div_constraints(
1275 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1277 int i;
1278 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1280 for (i = 0; i < bmap->n_div; ++i) {
1281 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1282 continue;
1283 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1284 continue;
1285 if (!better_div_constraint(bmap, i, k))
1286 continue;
1287 if (!ok_to_set_div_from_bound(bmap, i, k))
1288 break;
1289 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1290 bmap = set_div_from_lower_bound(bmap, i, k);
1291 else
1292 bmap = set_div_from_lower_bound(bmap, i, l);
1293 if (progress)
1294 *progress = 1;
1295 break;
1297 return bmap;
1300 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1301 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1303 struct isl_constraint_index ci;
1304 int k, l, h;
1305 unsigned total = isl_basic_map_total_dim(bmap);
1306 isl_int sum;
1308 if (!bmap || bmap->n_ineq <= 1)
1309 return bmap;
1311 if (create_constraint_index(&ci, bmap) < 0)
1312 return bmap;
1314 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1315 ci.index[h] = &bmap->ineq[0];
1316 for (k = 1; k < bmap->n_ineq; ++k) {
1317 h = hash_index(&ci, bmap, k);
1318 if (!ci.index[h]) {
1319 ci.index[h] = &bmap->ineq[k];
1320 continue;
1322 if (progress)
1323 *progress = 1;
1324 l = ci.index[h] - &bmap->ineq[0];
1325 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1326 swap_inequality(bmap, k, l);
1327 isl_basic_map_drop_inequality(bmap, k);
1328 --k;
1330 isl_int_init(sum);
1331 for (k = 0; k < bmap->n_ineq-1; ++k) {
1332 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1333 h = hash_index(&ci, bmap, k);
1334 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1335 if (!ci.index[h])
1336 continue;
1337 l = ci.index[h] - &bmap->ineq[0];
1338 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1339 if (isl_int_is_pos(sum)) {
1340 if (detect_divs)
1341 bmap = check_for_div_constraints(bmap, k, l,
1342 sum, progress);
1343 continue;
1345 if (isl_int_is_zero(sum)) {
1346 /* We need to break out of the loop after these
1347 * changes since the contents of the hash
1348 * will no longer be valid.
1349 * Plus, we probably we want to regauss first.
1351 if (progress)
1352 *progress = 1;
1353 isl_basic_map_drop_inequality(bmap, l);
1354 isl_basic_map_inequality_to_equality(bmap, k);
1355 } else
1356 bmap = isl_basic_map_set_to_empty(bmap);
1357 break;
1359 isl_int_clear(sum);
1361 constraint_index_free(&ci);
1362 return bmap;
1365 /* Detect all pairs of inequalities that form an equality.
1367 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1368 * Call it repeatedly while it is making progress.
1370 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1371 __isl_take isl_basic_map *bmap, int *progress)
1373 int duplicate;
1375 do {
1376 duplicate = 0;
1377 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1378 &duplicate, 0);
1379 if (progress && duplicate)
1380 *progress = 1;
1381 } while (duplicate);
1383 return bmap;
1386 /* Eliminate knowns divs from constraints where they appear with
1387 * a (positive or negative) unit coefficient.
1389 * That is, replace
1391 * floor(e/m) + f >= 0
1393 * by
1395 * e + m f >= 0
1397 * and
1399 * -floor(e/m) + f >= 0
1401 * by
1403 * -e + m f + m - 1 >= 0
1405 * The first conversion is valid because floor(e/m) >= -f is equivalent
1406 * to e/m >= -f because -f is an integral expression.
1407 * The second conversion follows from the fact that
1409 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1412 * Note that one of the div constraints may have been eliminated
1413 * due to being redundant with respect to the constraint that is
1414 * being modified by this function. The modified constraint may
1415 * no longer imply this div constraint, so we add it back to make
1416 * sure we do not lose any information.
1418 * We skip integral divs, i.e., those with denominator 1, as we would
1419 * risk eliminating the div from the div constraints. We do not need
1420 * to handle those divs here anyway since the div constraints will turn
1421 * out to form an equality and this equality can then be use to eliminate
1422 * the div from all constraints.
1424 static __isl_give isl_basic_map *eliminate_unit_divs(
1425 __isl_take isl_basic_map *bmap, int *progress)
1427 int i, j;
1428 isl_ctx *ctx;
1429 unsigned total;
1431 if (!bmap)
1432 return NULL;
1434 ctx = isl_basic_map_get_ctx(bmap);
1435 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1437 for (i = 0; i < bmap->n_div; ++i) {
1438 if (isl_int_is_zero(bmap->div[i][0]))
1439 continue;
1440 if (isl_int_is_one(bmap->div[i][0]))
1441 continue;
1442 for (j = 0; j < bmap->n_ineq; ++j) {
1443 int s;
1445 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1446 !isl_int_is_negone(bmap->ineq[j][total + i]))
1447 continue;
1449 *progress = 1;
1451 s = isl_int_sgn(bmap->ineq[j][total + i]);
1452 isl_int_set_si(bmap->ineq[j][total + i], 0);
1453 if (s < 0)
1454 isl_seq_combine(bmap->ineq[j],
1455 ctx->negone, bmap->div[i] + 1,
1456 bmap->div[i][0], bmap->ineq[j],
1457 total + bmap->n_div);
1458 else
1459 isl_seq_combine(bmap->ineq[j],
1460 ctx->one, bmap->div[i] + 1,
1461 bmap->div[i][0], bmap->ineq[j],
1462 total + bmap->n_div);
1463 if (s < 0) {
1464 isl_int_add(bmap->ineq[j][0],
1465 bmap->ineq[j][0], bmap->div[i][0]);
1466 isl_int_sub_ui(bmap->ineq[j][0],
1467 bmap->ineq[j][0], 1);
1470 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1471 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1472 return isl_basic_map_free(bmap);
1476 return bmap;
1479 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1481 int progress = 1;
1482 if (!bmap)
1483 return NULL;
1484 while (progress) {
1485 progress = 0;
1486 if (!bmap)
1487 break;
1488 if (isl_basic_map_plain_is_empty(bmap))
1489 break;
1490 bmap = isl_basic_map_normalize_constraints(bmap);
1491 bmap = remove_independent_vars_from_divs(bmap);
1492 bmap = normalize_div_expressions(bmap);
1493 bmap = remove_duplicate_divs(bmap, &progress);
1494 bmap = eliminate_unit_divs(bmap, &progress);
1495 bmap = eliminate_divs_eq(bmap, &progress);
1496 bmap = eliminate_divs_ineq(bmap, &progress);
1497 bmap = isl_basic_map_gauss(bmap, &progress);
1498 /* requires equalities in normal form */
1499 bmap = normalize_divs(bmap, &progress);
1500 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1501 &progress, 1);
1502 if (bmap && progress)
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1505 return bmap;
1508 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1510 return (struct isl_basic_set *)
1511 isl_basic_map_simplify((struct isl_basic_map *)bset);
1515 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1516 isl_int *constraint, unsigned div)
1518 unsigned pos;
1520 if (!bmap)
1521 return -1;
1523 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1525 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1526 int neg;
1527 isl_int_sub(bmap->div[div][1],
1528 bmap->div[div][1], bmap->div[div][0]);
1529 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1530 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1531 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1532 isl_int_add(bmap->div[div][1],
1533 bmap->div[div][1], bmap->div[div][0]);
1534 if (!neg)
1535 return 0;
1536 if (isl_seq_first_non_zero(constraint+pos+1,
1537 bmap->n_div-div-1) != -1)
1538 return 0;
1539 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1540 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1541 return 0;
1542 if (isl_seq_first_non_zero(constraint+pos+1,
1543 bmap->n_div-div-1) != -1)
1544 return 0;
1545 } else
1546 return 0;
1548 return 1;
1551 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1552 isl_int *constraint, unsigned div)
1554 return isl_basic_map_is_div_constraint(bset, constraint, div);
1558 /* If the only constraints a div d=floor(f/m)
1559 * appears in are its two defining constraints
1561 * f - m d >=0
1562 * -(f - (m - 1)) + m d >= 0
1564 * then it can safely be removed.
1566 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1568 int i;
1569 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1571 for (i = 0; i < bmap->n_eq; ++i)
1572 if (!isl_int_is_zero(bmap->eq[i][pos]))
1573 return 0;
1575 for (i = 0; i < bmap->n_ineq; ++i) {
1576 if (isl_int_is_zero(bmap->ineq[i][pos]))
1577 continue;
1578 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1579 return 0;
1582 for (i = 0; i < bmap->n_div; ++i) {
1583 if (isl_int_is_zero(bmap->div[i][0]))
1584 continue;
1585 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1586 return 0;
1589 return 1;
1593 * Remove divs that don't occur in any of the constraints or other divs.
1594 * These can arise when dropping constraints from a basic map or
1595 * when the divs of a basic map have been temporarily aligned
1596 * with the divs of another basic map.
1598 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1600 int i;
1602 if (!bmap)
1603 return NULL;
1605 for (i = bmap->n_div-1; i >= 0; --i) {
1606 if (!div_is_redundant(bmap, i))
1607 continue;
1608 bmap = isl_basic_map_drop_div(bmap, i);
1610 return bmap;
1613 /* Mark "bmap" as final, without checking for obviously redundant
1614 * integer divisions. This function should be used when "bmap"
1615 * is known not to involve any such integer divisions.
1617 __isl_give isl_basic_map *isl_basic_map_mark_final(
1618 __isl_take isl_basic_map *bmap)
1620 if (!bmap)
1621 return NULL;
1622 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1623 return bmap;
1626 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1628 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1630 bmap = remove_redundant_divs(bmap);
1631 bmap = isl_basic_map_mark_final(bmap);
1632 return bmap;
1635 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1637 return (struct isl_basic_set *)
1638 isl_basic_map_finalize((struct isl_basic_map *)bset);
1641 struct isl_set *isl_set_finalize(struct isl_set *set)
1643 int i;
1645 if (!set)
1646 return NULL;
1647 for (i = 0; i < set->n; ++i) {
1648 set->p[i] = isl_basic_set_finalize(set->p[i]);
1649 if (!set->p[i])
1650 goto error;
1652 return set;
1653 error:
1654 isl_set_free(set);
1655 return NULL;
1658 struct isl_map *isl_map_finalize(struct isl_map *map)
1660 int i;
1662 if (!map)
1663 return NULL;
1664 for (i = 0; i < map->n; ++i) {
1665 map->p[i] = isl_basic_map_finalize(map->p[i]);
1666 if (!map->p[i])
1667 goto error;
1669 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1670 return map;
1671 error:
1672 isl_map_free(map);
1673 return NULL;
1677 /* Remove definition of any div that is defined in terms of the given variable.
1678 * The div itself is not removed. Functions such as
1679 * eliminate_divs_ineq depend on the other divs remaining in place.
1681 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1682 int pos)
1684 int i;
1686 if (!bmap)
1687 return NULL;
1689 for (i = 0; i < bmap->n_div; ++i) {
1690 if (isl_int_is_zero(bmap->div[i][0]))
1691 continue;
1692 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1693 continue;
1694 isl_int_set_si(bmap->div[i][0], 0);
1696 return bmap;
1699 /* Eliminate the specified variables from the constraints using
1700 * Fourier-Motzkin. The variables themselves are not removed.
1702 struct isl_basic_map *isl_basic_map_eliminate_vars(
1703 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1705 int d;
1706 int i, j, k;
1707 unsigned total;
1708 int need_gauss = 0;
1710 if (n == 0)
1711 return bmap;
1712 if (!bmap)
1713 return NULL;
1714 total = isl_basic_map_total_dim(bmap);
1716 bmap = isl_basic_map_cow(bmap);
1717 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1718 bmap = remove_dependent_vars(bmap, d);
1719 if (!bmap)
1720 return NULL;
1722 for (d = pos + n - 1;
1723 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1724 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1725 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1726 int n_lower, n_upper;
1727 if (!bmap)
1728 return NULL;
1729 for (i = 0; i < bmap->n_eq; ++i) {
1730 if (isl_int_is_zero(bmap->eq[i][1+d]))
1731 continue;
1732 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1733 isl_basic_map_drop_equality(bmap, i);
1734 need_gauss = 1;
1735 break;
1737 if (i < bmap->n_eq)
1738 continue;
1739 n_lower = 0;
1740 n_upper = 0;
1741 for (i = 0; i < bmap->n_ineq; ++i) {
1742 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1743 n_lower++;
1744 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1745 n_upper++;
1747 bmap = isl_basic_map_extend_constraints(bmap,
1748 0, n_lower * n_upper);
1749 if (!bmap)
1750 goto error;
1751 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1752 int last;
1753 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1754 continue;
1755 last = -1;
1756 for (j = 0; j < i; ++j) {
1757 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1758 continue;
1759 last = j;
1760 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1761 isl_int_sgn(bmap->ineq[j][1+d]))
1762 continue;
1763 k = isl_basic_map_alloc_inequality(bmap);
1764 if (k < 0)
1765 goto error;
1766 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1767 1+total);
1768 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1769 1+d, 1+total, NULL);
1771 isl_basic_map_drop_inequality(bmap, i);
1772 i = last + 1;
1774 if (n_lower > 0 && n_upper > 0) {
1775 bmap = isl_basic_map_normalize_constraints(bmap);
1776 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1777 NULL, 0);
1778 bmap = isl_basic_map_gauss(bmap, NULL);
1779 bmap = isl_basic_map_remove_redundancies(bmap);
1780 need_gauss = 0;
1781 if (!bmap)
1782 goto error;
1783 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1784 break;
1787 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1788 if (need_gauss)
1789 bmap = isl_basic_map_gauss(bmap, NULL);
1790 return bmap;
1791 error:
1792 isl_basic_map_free(bmap);
1793 return NULL;
1796 struct isl_basic_set *isl_basic_set_eliminate_vars(
1797 struct isl_basic_set *bset, unsigned pos, unsigned n)
1799 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1800 (struct isl_basic_map *)bset, pos, n);
1803 /* Eliminate the specified n dimensions starting at first from the
1804 * constraints, without removing the dimensions from the space.
1805 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1806 * Otherwise, they are projected out and the original space is restored.
1808 __isl_give isl_basic_map *isl_basic_map_eliminate(
1809 __isl_take isl_basic_map *bmap,
1810 enum isl_dim_type type, unsigned first, unsigned n)
1812 isl_space *space;
1814 if (!bmap)
1815 return NULL;
1816 if (n == 0)
1817 return bmap;
1819 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1820 isl_die(bmap->ctx, isl_error_invalid,
1821 "index out of bounds", goto error);
1823 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1824 first += isl_basic_map_offset(bmap, type) - 1;
1825 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1826 return isl_basic_map_finalize(bmap);
1829 space = isl_basic_map_get_space(bmap);
1830 bmap = isl_basic_map_project_out(bmap, type, first, n);
1831 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1832 bmap = isl_basic_map_reset_space(bmap, space);
1833 return bmap;
1834 error:
1835 isl_basic_map_free(bmap);
1836 return NULL;
1839 __isl_give isl_basic_set *isl_basic_set_eliminate(
1840 __isl_take isl_basic_set *bset,
1841 enum isl_dim_type type, unsigned first, unsigned n)
1843 return isl_basic_map_eliminate(bset, type, first, n);
1846 /* Remove all constraints from "bmap" that reference any unknown local
1847 * variables (directly or indirectly).
1849 * Dropping all constraints on a local variable will make it redundant,
1850 * so it will get removed implicitly by
1851 * isl_basic_map_drop_constraints_involving_dims. Some other local
1852 * variables may also end up becoming redundant if they only appear
1853 * in constraints together with the unknown local variable.
1854 * Therefore, start over after calling
1855 * isl_basic_map_drop_constraints_involving_dims.
1857 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1858 __isl_take isl_basic_map *bmap)
1860 isl_bool known;
1861 int i, n_div, o_div;
1863 known = isl_basic_map_divs_known(bmap);
1864 if (known < 0)
1865 return isl_basic_map_free(bmap);
1866 if (known)
1867 return bmap;
1869 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1870 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1872 for (i = 0; i < n_div; ++i) {
1873 known = isl_basic_map_div_is_known(bmap, i);
1874 if (known < 0)
1875 return isl_basic_map_free(bmap);
1876 if (known)
1877 continue;
1878 bmap = remove_dependent_vars(bmap, o_div + i);
1879 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1880 isl_dim_div, i, 1);
1881 if (!bmap)
1882 return NULL;
1883 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1884 i = -1;
1887 return bmap;
1890 /* Remove all constraints from "map" that reference any unknown local
1891 * variables (directly or indirectly).
1893 * Since constraints may get dropped from the basic maps,
1894 * they may no longer be disjoint from each other.
1896 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1897 __isl_take isl_map *map)
1899 int i;
1900 isl_bool known;
1902 known = isl_map_divs_known(map);
1903 if (known < 0)
1904 return isl_map_free(map);
1905 if (known)
1906 return map;
1908 map = isl_map_cow(map);
1909 if (!map)
1910 return NULL;
1912 for (i = 0; i < map->n; ++i) {
1913 map->p[i] =
1914 isl_basic_map_drop_constraint_involving_unknown_divs(
1915 map->p[i]);
1916 if (!map->p[i])
1917 return isl_map_free(map);
1920 if (map->n > 1)
1921 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1923 return map;
1926 /* Don't assume equalities are in order, because align_divs
1927 * may have changed the order of the divs.
1929 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1931 int d, i;
1932 unsigned total;
1934 total = isl_space_dim(bmap->dim, isl_dim_all);
1935 for (d = 0; d < total; ++d)
1936 elim[d] = -1;
1937 for (i = 0; i < bmap->n_eq; ++i) {
1938 for (d = total - 1; d >= 0; --d) {
1939 if (isl_int_is_zero(bmap->eq[i][1+d]))
1940 continue;
1941 elim[d] = i;
1942 break;
1947 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1949 compute_elimination_index((struct isl_basic_map *)bset, elim);
1952 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1953 struct isl_basic_map *bmap, int *elim)
1955 int d;
1956 int copied = 0;
1957 unsigned total;
1959 total = isl_space_dim(bmap->dim, isl_dim_all);
1960 for (d = total - 1; d >= 0; --d) {
1961 if (isl_int_is_zero(src[1+d]))
1962 continue;
1963 if (elim[d] == -1)
1964 continue;
1965 if (!copied) {
1966 isl_seq_cpy(dst, src, 1 + total);
1967 copied = 1;
1969 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1971 return copied;
1974 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1975 struct isl_basic_set *bset, int *elim)
1977 return reduced_using_equalities(dst, src,
1978 (struct isl_basic_map *)bset, elim);
1981 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1982 struct isl_basic_set *bset, struct isl_basic_set *context)
1984 int i;
1985 int *elim;
1987 if (!bset || !context)
1988 goto error;
1990 if (context->n_eq == 0) {
1991 isl_basic_set_free(context);
1992 return bset;
1995 bset = isl_basic_set_cow(bset);
1996 if (!bset)
1997 goto error;
1999 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2000 if (!elim)
2001 goto error;
2002 set_compute_elimination_index(context, elim);
2003 for (i = 0; i < bset->n_eq; ++i)
2004 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2005 context, elim);
2006 for (i = 0; i < bset->n_ineq; ++i)
2007 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2008 context, elim);
2009 isl_basic_set_free(context);
2010 free(elim);
2011 bset = isl_basic_set_simplify(bset);
2012 bset = isl_basic_set_finalize(bset);
2013 return bset;
2014 error:
2015 isl_basic_set_free(bset);
2016 isl_basic_set_free(context);
2017 return NULL;
2020 /* For each inequality in "ineq" that is a shifted (more relaxed)
2021 * copy of an inequality in "context", mark the corresponding entry
2022 * in "row" with -1.
2023 * If an inequality only has a non-negative constant term, then
2024 * mark it as well.
2026 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2027 __isl_keep isl_basic_set *context, int *row)
2029 struct isl_constraint_index ci;
2030 int n_ineq;
2031 unsigned total;
2032 int k;
2034 if (!ineq || !context)
2035 return isl_stat_error;
2036 if (context->n_ineq == 0)
2037 return isl_stat_ok;
2038 if (setup_constraint_index(&ci, context) < 0)
2039 return isl_stat_error;
2041 n_ineq = isl_mat_rows(ineq);
2042 total = isl_mat_cols(ineq) - 1;
2043 for (k = 0; k < n_ineq; ++k) {
2044 int l;
2045 isl_bool redundant;
2047 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2048 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2049 row[k] = -1;
2050 continue;
2052 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2053 if (redundant < 0)
2054 goto error;
2055 if (!redundant)
2056 continue;
2057 row[k] = -1;
2059 constraint_index_free(&ci);
2060 return isl_stat_ok;
2061 error:
2062 constraint_index_free(&ci);
2063 return isl_stat_error;
2066 static struct isl_basic_set *remove_shifted_constraints(
2067 struct isl_basic_set *bset, struct isl_basic_set *context)
2069 struct isl_constraint_index ci;
2070 int k;
2072 if (!bset || !context)
2073 return bset;
2075 if (context->n_ineq == 0)
2076 return bset;
2077 if (setup_constraint_index(&ci, context) < 0)
2078 return bset;
2080 for (k = 0; k < bset->n_ineq; ++k) {
2081 isl_bool redundant;
2083 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2084 if (redundant < 0)
2085 goto error;
2086 if (!redundant)
2087 continue;
2088 bset = isl_basic_set_cow(bset);
2089 if (!bset)
2090 goto error;
2091 isl_basic_set_drop_inequality(bset, k);
2092 --k;
2094 constraint_index_free(&ci);
2095 return bset;
2096 error:
2097 constraint_index_free(&ci);
2098 return bset;
2101 /* Remove constraints from "bmap" that are identical to constraints
2102 * in "context" or that are more relaxed (greater constant term).
2104 * We perform the test for shifted copies on the pure constraints
2105 * in remove_shifted_constraints.
2107 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2108 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2110 isl_basic_set *bset, *bset_context;
2112 if (!bmap || !context)
2113 goto error;
2115 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2116 isl_basic_map_free(context);
2117 return bmap;
2120 context = isl_basic_map_align_divs(context, bmap);
2121 bmap = isl_basic_map_align_divs(bmap, context);
2123 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2124 bset_context = isl_basic_map_underlying_set(context);
2125 bset = remove_shifted_constraints(bset, bset_context);
2126 isl_basic_set_free(bset_context);
2128 bmap = isl_basic_map_overlying_set(bset, bmap);
2130 return bmap;
2131 error:
2132 isl_basic_map_free(bmap);
2133 isl_basic_map_free(context);
2134 return NULL;
2137 /* Does the (linear part of a) constraint "c" involve any of the "len"
2138 * "relevant" dimensions?
2140 static int is_related(isl_int *c, int len, int *relevant)
2142 int i;
2144 for (i = 0; i < len; ++i) {
2145 if (!relevant[i])
2146 continue;
2147 if (!isl_int_is_zero(c[i]))
2148 return 1;
2151 return 0;
2154 /* Drop constraints from "bset" that do not involve any of
2155 * the dimensions marked "relevant".
2157 static __isl_give isl_basic_set *drop_unrelated_constraints(
2158 __isl_take isl_basic_set *bset, int *relevant)
2160 int i, dim;
2162 dim = isl_basic_set_dim(bset, isl_dim_set);
2163 for (i = 0; i < dim; ++i)
2164 if (!relevant[i])
2165 break;
2166 if (i >= dim)
2167 return bset;
2169 for (i = bset->n_eq - 1; i >= 0; --i)
2170 if (!is_related(bset->eq[i] + 1, dim, relevant)) {
2171 bset = isl_basic_set_cow(bset);
2172 if (isl_basic_set_drop_equality(bset, i) < 0)
2173 return isl_basic_set_free(bset);
2176 for (i = bset->n_ineq - 1; i >= 0; --i)
2177 if (!is_related(bset->ineq[i] + 1, dim, relevant)) {
2178 bset = isl_basic_set_cow(bset);
2179 if (isl_basic_set_drop_inequality(bset, i) < 0)
2180 return isl_basic_set_free(bset);
2183 return bset;
2186 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2188 * In particular, for any variable involved in the constraint,
2189 * find the actual group id from before and replace the group
2190 * of the corresponding variable by the minimal group of all
2191 * the variables involved in the constraint considered so far
2192 * (if this minimum is smaller) or replace the minimum by this group
2193 * (if the minimum is larger).
2195 * At the end, all the variables in "c" will (indirectly) point
2196 * to the minimal of the groups that they referred to originally.
2198 static void update_groups(int dim, int *group, isl_int *c)
2200 int j;
2201 int min = dim;
2203 for (j = 0; j < dim; ++j) {
2204 if (isl_int_is_zero(c[j]))
2205 continue;
2206 while (group[j] >= 0 && group[group[j]] != group[j])
2207 group[j] = group[group[j]];
2208 if (group[j] == min)
2209 continue;
2210 if (group[j] < min) {
2211 if (min >= 0 && min < dim)
2212 group[min] = group[j];
2213 min = group[j];
2214 } else
2215 group[group[j]] = min;
2219 /* Allocate an array of groups of variables, one for each variable
2220 * in "context", initialized to zero.
2222 static int *alloc_groups(__isl_keep isl_basic_set *context)
2224 isl_ctx *ctx;
2225 int dim;
2227 dim = isl_basic_set_dim(context, isl_dim_set);
2228 ctx = isl_basic_set_get_ctx(context);
2229 return isl_calloc_array(ctx, int, dim);
2232 /* Drop constraints from "context" that only involve variables that are
2233 * not related to any of the variables marked with a "-1" in "group".
2235 * We construct groups of variables that collect variables that
2236 * (indirectly) appear in some common constraint of "context".
2237 * Each group is identified by the first variable in the group,
2238 * except for the special group of variables that was already identified
2239 * in the input as -1 (or are related to those variables).
2240 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2241 * otherwise the group of i is the group of group[i].
2243 * We first initialize groups for the remaining variables.
2244 * Then we iterate over the constraints of "context" and update the
2245 * group of the variables in the constraint by the smallest group.
2246 * Finally, we resolve indirect references to groups by running over
2247 * the variables.
2249 * After computing the groups, we drop constraints that do not involve
2250 * any variables in the -1 group.
2252 static __isl_give isl_basic_set *group_and_drop_irrelevant_constraints(
2253 __isl_take isl_basic_set *context, __isl_take int *group)
2255 int dim;
2256 int i;
2257 int last;
2259 dim = isl_basic_set_dim(context, isl_dim_set);
2261 last = -1;
2262 for (i = 0; i < dim; ++i)
2263 if (group[i] >= 0)
2264 last = group[i] = i;
2265 if (last < 0) {
2266 free(group);
2267 return context;
2270 for (i = 0; i < context->n_eq; ++i)
2271 update_groups(dim, group, context->eq[i] + 1);
2272 for (i = 0; i < context->n_ineq; ++i)
2273 update_groups(dim, group, context->ineq[i] + 1);
2275 for (i = 0; i < dim; ++i)
2276 if (group[i] >= 0)
2277 group[i] = group[group[i]];
2279 for (i = 0; i < dim; ++i)
2280 group[i] = group[i] == -1;
2282 context = drop_unrelated_constraints(context, group);
2284 free(group);
2285 return context;
2288 /* Drop constraints from "context" that are irrelevant for computing
2289 * the gist of "bset".
2291 * In particular, drop constraints in variables that are not related
2292 * to any of the variables involved in the constraints of "bset"
2293 * in the sense that there is no sequence of constraints that connects them.
2295 * We first mark all variables that appear in "bset" as belonging
2296 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2298 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2299 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2301 int *group;
2302 int dim;
2303 int i, j;
2305 if (!context || !bset)
2306 return isl_basic_set_free(context);
2308 group = alloc_groups(context);
2310 if (!group)
2311 return isl_basic_set_free(context);
2313 dim = isl_basic_set_dim(bset, isl_dim_set);
2314 for (i = 0; i < dim; ++i) {
2315 for (j = 0; j < bset->n_eq; ++j)
2316 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2317 break;
2318 if (j < bset->n_eq) {
2319 group[i] = -1;
2320 continue;
2322 for (j = 0; j < bset->n_ineq; ++j)
2323 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2324 break;
2325 if (j < bset->n_ineq)
2326 group[i] = -1;
2329 return group_and_drop_irrelevant_constraints(context, group);
2332 /* Drop constraints from "context" that are irrelevant for computing
2333 * the gist of the inequalities "ineq".
2334 * Inequalities in "ineq" for which the corresponding element of row
2335 * is set to -1 have already been marked for removal and should be ignored.
2337 * In particular, drop constraints in variables that are not related
2338 * to any of the variables involved in "ineq"
2339 * in the sense that there is no sequence of constraints that connects them.
2341 * We first mark all variables that appear in "bset" as belonging
2342 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2344 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2345 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2347 int *group;
2348 int dim;
2349 int i, j, n;
2351 if (!context || !ineq)
2352 return isl_basic_set_free(context);
2354 group = alloc_groups(context);
2356 if (!group)
2357 return isl_basic_set_free(context);
2359 dim = isl_basic_set_dim(context, isl_dim_set);
2360 n = isl_mat_rows(ineq);
2361 for (i = 0; i < dim; ++i) {
2362 for (j = 0; j < n; ++j) {
2363 if (row[j] < 0)
2364 continue;
2365 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2366 break;
2368 if (j < n)
2369 group[i] = -1;
2372 return group_and_drop_irrelevant_constraints(context, group);
2375 /* Do all "n" entries of "row" contain a negative value?
2377 static int all_neg(int *row, int n)
2379 int i;
2381 for (i = 0; i < n; ++i)
2382 if (row[i] >= 0)
2383 return 0;
2385 return 1;
2388 /* Update the inequalities in "bset" based on the information in "row"
2389 * and "tab".
2391 * In particular, the array "row" contains either -1, meaning that
2392 * the corresponding inequality of "bset" is redundant, or the index
2393 * of an inequality in "tab".
2395 * If the row entry is -1, then drop the inequality.
2396 * Otherwise, if the constraint is marked redundant in the tableau,
2397 * then drop the inequality. Similarly, if it is marked as an equality
2398 * in the tableau, then turn the inequality into an equality and
2399 * perform Gaussian elimination.
2401 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2402 __isl_keep int *row, struct isl_tab *tab)
2404 int i;
2405 unsigned n_ineq;
2406 unsigned n_eq;
2407 int found_equality = 0;
2409 if (!bset)
2410 return NULL;
2411 if (tab && tab->empty)
2412 return isl_basic_set_set_to_empty(bset);
2414 n_ineq = bset->n_ineq;
2415 for (i = n_ineq - 1; i >= 0; --i) {
2416 if (row[i] < 0) {
2417 if (isl_basic_set_drop_inequality(bset, i) < 0)
2418 return isl_basic_set_free(bset);
2419 continue;
2421 if (!tab)
2422 continue;
2423 n_eq = tab->n_eq;
2424 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2425 isl_basic_map_inequality_to_equality(bset, i);
2426 found_equality = 1;
2427 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2428 if (isl_basic_set_drop_inequality(bset, i) < 0)
2429 return isl_basic_set_free(bset);
2433 if (found_equality)
2434 bset = isl_basic_set_gauss(bset, NULL);
2435 bset = isl_basic_set_finalize(bset);
2436 return bset;
2439 /* Update the inequalities in "bset" based on the information in "row"
2440 * and "tab" and free all arguments (other than "bset").
2442 static __isl_give isl_basic_set *update_ineq_free(
2443 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2444 __isl_take isl_basic_set *context, __isl_take int *row,
2445 struct isl_tab *tab)
2447 isl_mat_free(ineq);
2448 isl_basic_set_free(context);
2450 bset = update_ineq(bset, row, tab);
2452 free(row);
2453 isl_tab_free(tab);
2454 return bset;
2457 /* Remove all information from bset that is redundant in the context
2458 * of context.
2459 * "ineq" contains the (possibly transformed) inequalities of "bset",
2460 * in the same order.
2461 * The (explicit) equalities of "bset" are assumed to have been taken
2462 * into account by the transformation such that only the inequalities
2463 * are relevant.
2464 * "context" is assumed not to be empty.
2466 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2467 * A value of -1 means that the inequality is obviously redundant and may
2468 * not even appear in "tab".
2470 * We first mark the inequalities of "bset"
2471 * that are obviously redundant with respect to some inequality in "context".
2472 * Then we remove those constraints from "context" that have become
2473 * irrelevant for computing the gist of "bset".
2474 * Note that this removal of constraints cannot be replaced by
2475 * a factorization because factors in "bset" may still be connected
2476 * to each other through constraints in "context".
2478 * If there are any inequalities left, we construct a tableau for
2479 * the context and then add the inequalities of "bset".
2480 * Before adding these inequalities, we freeze all constraints such that
2481 * they won't be considered redundant in terms of the constraints of "bset".
2482 * Then we detect all redundant constraints (among the
2483 * constraints that weren't frozen), first by checking for redundancy in the
2484 * the tableau and then by checking if replacing a constraint by its negation
2485 * would lead to an empty set. This last step is fairly expensive
2486 * and could be optimized by more reuse of the tableau.
2487 * Finally, we update bset according to the results.
2489 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2490 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2492 int i, r;
2493 int *row = NULL;
2494 isl_ctx *ctx;
2495 isl_basic_set *combined = NULL;
2496 struct isl_tab *tab = NULL;
2497 unsigned n_eq, context_ineq;
2498 unsigned total;
2500 if (!bset || !ineq || !context)
2501 goto error;
2503 if (bset->n_ineq == 0 || isl_basic_set_is_universe(context)) {
2504 isl_basic_set_free(context);
2505 isl_mat_free(ineq);
2506 return bset;
2509 ctx = isl_basic_set_get_ctx(context);
2510 row = isl_calloc_array(ctx, int, bset->n_ineq);
2511 if (!row)
2512 goto error;
2514 if (mark_shifted_constraints(ineq, context, row) < 0)
2515 goto error;
2516 if (all_neg(row, bset->n_ineq))
2517 return update_ineq_free(bset, ineq, context, row, NULL);
2519 context = drop_irrelevant_constraints_marked(context, ineq, row);
2520 if (!context)
2521 goto error;
2522 if (isl_basic_set_is_universe(context))
2523 return update_ineq_free(bset, ineq, context, row, NULL);
2525 n_eq = context->n_eq;
2526 context_ineq = context->n_ineq;
2527 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2528 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2529 tab = isl_tab_from_basic_set(combined, 0);
2530 for (i = 0; i < context_ineq; ++i)
2531 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2532 goto error;
2533 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2534 goto error;
2535 r = context_ineq;
2536 for (i = 0; i < bset->n_ineq; ++i) {
2537 if (row[i] < 0)
2538 continue;
2539 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2540 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2541 goto error;
2542 row[i] = r++;
2544 if (isl_tab_detect_implicit_equalities(tab) < 0)
2545 goto error;
2546 if (isl_tab_detect_redundant(tab) < 0)
2547 goto error;
2548 total = isl_basic_set_total_dim(bset);
2549 for (i = bset->n_ineq - 1; i >= 0; --i) {
2550 isl_basic_set *test;
2551 int is_empty;
2553 if (row[i] < 0)
2554 continue;
2555 r = row[i];
2556 if (tab->con[n_eq + r].is_redundant)
2557 continue;
2558 test = isl_basic_set_dup(combined);
2559 if (isl_inequality_negate(test, r) < 0)
2560 test = isl_basic_set_free(test);
2561 test = isl_basic_set_update_from_tab(test, tab);
2562 is_empty = isl_basic_set_is_empty(test);
2563 isl_basic_set_free(test);
2564 if (is_empty < 0)
2565 goto error;
2566 if (is_empty)
2567 tab->con[n_eq + r].is_redundant = 1;
2569 bset = update_ineq_free(bset, ineq, context, row, tab);
2570 if (bset) {
2571 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2572 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2575 isl_basic_set_free(combined);
2576 return bset;
2577 error:
2578 free(row);
2579 isl_mat_free(ineq);
2580 isl_tab_free(tab);
2581 isl_basic_set_free(combined);
2582 isl_basic_set_free(context);
2583 isl_basic_set_free(bset);
2584 return NULL;
2587 /* Extract the inequalities of "bset" as an isl_mat.
2589 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2591 unsigned total;
2592 isl_ctx *ctx;
2593 isl_mat *ineq;
2595 if (!bset)
2596 return NULL;
2598 ctx = isl_basic_set_get_ctx(bset);
2599 total = isl_basic_set_total_dim(bset);
2600 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2601 0, 1 + total);
2603 return ineq;
2606 /* Remove all information from "bset" that is redundant in the context
2607 * of "context", for the case where both "bset" and "context" are
2608 * full-dimensional.
2610 static __isl_give isl_basic_set *uset_gist_uncompressed(
2611 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2613 isl_mat *ineq;
2615 ineq = extract_ineq(bset);
2616 return uset_gist_full(bset, ineq, context);
2619 /* Remove all information from "bset" that is redundant in the context
2620 * of "context", for the case where the combined equalities of
2621 * "bset" and "context" allow for a compression that can be obtained
2622 * by preapplication of "T".
2624 * "bset" itself is not transformed by "T". Instead, the inequalities
2625 * are extracted from "bset" and those are transformed by "T".
2626 * uset_gist_full then determines which of the transformed inequalities
2627 * are redundant with respect to the transformed "context" and removes
2628 * the corresponding inequalities from "bset".
2630 * After preapplying "T" to the inequalities, any common factor is
2631 * removed from the coefficients. If this results in a tightening
2632 * of the constant term, then the same tightening is applied to
2633 * the corresponding untransformed inequality in "bset".
2634 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2636 * g f'(x) + r >= 0
2638 * with 0 <= r < g, then it is equivalent to
2640 * f'(x) >= 0
2642 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2643 * subspace compressed by T since the latter would be transformed to
2645 * g f'(x) >= 0
2647 static __isl_give isl_basic_set *uset_gist_compressed(
2648 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2649 __isl_take isl_mat *T)
2651 isl_ctx *ctx;
2652 isl_mat *ineq;
2653 int i, n_row, n_col;
2654 isl_int rem;
2656 ineq = extract_ineq(bset);
2657 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2658 context = isl_basic_set_preimage(context, T);
2660 if (!ineq || !context)
2661 goto error;
2662 if (isl_basic_set_plain_is_empty(context)) {
2663 isl_mat_free(ineq);
2664 isl_basic_set_free(context);
2665 return isl_basic_set_set_to_empty(bset);
2668 ctx = isl_mat_get_ctx(ineq);
2669 n_row = isl_mat_rows(ineq);
2670 n_col = isl_mat_cols(ineq);
2671 isl_int_init(rem);
2672 for (i = 0; i < n_row; ++i) {
2673 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2674 if (isl_int_is_zero(ctx->normalize_gcd))
2675 continue;
2676 if (isl_int_is_one(ctx->normalize_gcd))
2677 continue;
2678 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2679 ctx->normalize_gcd, n_col - 1);
2680 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2681 isl_int_fdiv_q(ineq->row[i][0],
2682 ineq->row[i][0], ctx->normalize_gcd);
2683 if (isl_int_is_zero(rem))
2684 continue;
2685 bset = isl_basic_set_cow(bset);
2686 if (!bset)
2687 break;
2688 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2690 isl_int_clear(rem);
2692 return uset_gist_full(bset, ineq, context);
2693 error:
2694 isl_mat_free(ineq);
2695 isl_basic_set_free(context);
2696 isl_basic_set_free(bset);
2697 return NULL;
2700 /* Project "bset" onto the variables that are involved in "template".
2702 static __isl_give isl_basic_set *project_onto_involved(
2703 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2705 int i, n;
2707 if (!bset || !template)
2708 return isl_basic_set_free(bset);
2710 n = isl_basic_set_dim(template, isl_dim_set);
2712 for (i = 0; i < n; ++i) {
2713 isl_bool involved;
2715 involved = isl_basic_set_involves_dims(template,
2716 isl_dim_set, i, 1);
2717 if (involved < 0)
2718 return isl_basic_set_free(bset);
2719 if (involved)
2720 continue;
2721 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2724 return bset;
2727 /* Remove all information from bset that is redundant in the context
2728 * of context. In particular, equalities that are linear combinations
2729 * of those in context are removed. Then the inequalities that are
2730 * redundant in the context of the equalities and inequalities of
2731 * context are removed.
2733 * First of all, we drop those constraints from "context"
2734 * that are irrelevant for computing the gist of "bset".
2735 * Alternatively, we could factorize the intersection of "context" and "bset".
2737 * We first compute the intersection of the integer affine hulls
2738 * of "bset" and "context",
2739 * compute the gist inside this intersection and then reduce
2740 * the constraints with respect to the equalities of the context
2741 * that only involve variables already involved in the input.
2743 * If two constraints are mutually redundant, then uset_gist_full
2744 * will remove the second of those constraints. We therefore first
2745 * sort the constraints so that constraints not involving existentially
2746 * quantified variables are given precedence over those that do.
2747 * We have to perform this sorting before the variable compression,
2748 * because that may effect the order of the variables.
2750 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2751 __isl_take isl_basic_set *context)
2753 isl_mat *eq;
2754 isl_mat *T;
2755 isl_basic_set *aff;
2756 isl_basic_set *aff_context;
2757 unsigned total;
2759 if (!bset || !context)
2760 goto error;
2762 context = drop_irrelevant_constraints(context, bset);
2764 bset = isl_basic_set_detect_equalities(bset);
2765 aff = isl_basic_set_copy(bset);
2766 aff = isl_basic_set_plain_affine_hull(aff);
2767 context = isl_basic_set_detect_equalities(context);
2768 aff_context = isl_basic_set_copy(context);
2769 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2770 aff = isl_basic_set_intersect(aff, aff_context);
2771 if (!aff)
2772 goto error;
2773 if (isl_basic_set_plain_is_empty(aff)) {
2774 isl_basic_set_free(bset);
2775 isl_basic_set_free(context);
2776 return aff;
2778 bset = isl_basic_set_sort_constraints(bset);
2779 if (aff->n_eq == 0) {
2780 isl_basic_set_free(aff);
2781 return uset_gist_uncompressed(bset, context);
2783 total = isl_basic_set_total_dim(bset);
2784 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2785 eq = isl_mat_cow(eq);
2786 T = isl_mat_variable_compression(eq, NULL);
2787 isl_basic_set_free(aff);
2788 if (T && T->n_col == 0) {
2789 isl_mat_free(T);
2790 isl_basic_set_free(context);
2791 return isl_basic_set_set_to_empty(bset);
2794 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2795 aff_context = project_onto_involved(aff_context, bset);
2797 bset = uset_gist_compressed(bset, context, T);
2798 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2800 if (bset) {
2801 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2802 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2805 return bset;
2806 error:
2807 isl_basic_set_free(bset);
2808 isl_basic_set_free(context);
2809 return NULL;
2812 /* Return a basic map that has the same intersection with "context" as "bmap"
2813 * and that is as "simple" as possible.
2815 * The core computation is performed on the pure constraints.
2816 * When we add back the meaning of the integer divisions, we need
2817 * to (re)introduce the div constraints. If we happen to have
2818 * discovered that some of these integer divisions are equal to
2819 * some affine combination of other variables, then these div
2820 * constraints may end up getting simplified in terms of the equalities,
2821 * resulting in extra inequalities on the other variables that
2822 * may have been removed already or that may not even have been
2823 * part of the input. We try and remove those constraints of
2824 * this form that are most obviously redundant with respect to
2825 * the context. We also remove those div constraints that are
2826 * redundant with respect to the other constraints in the result.
2828 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
2829 struct isl_basic_map *context)
2831 isl_basic_set *bset, *eq;
2832 isl_basic_map *eq_bmap;
2833 unsigned total, n_div, extra, n_eq, n_ineq;
2835 if (!bmap || !context)
2836 goto error;
2838 if (isl_basic_map_is_universe(bmap)) {
2839 isl_basic_map_free(context);
2840 return bmap;
2842 if (isl_basic_map_plain_is_empty(context)) {
2843 isl_space *space = isl_basic_map_get_space(bmap);
2844 isl_basic_map_free(bmap);
2845 isl_basic_map_free(context);
2846 return isl_basic_map_universe(space);
2848 if (isl_basic_map_plain_is_empty(bmap)) {
2849 isl_basic_map_free(context);
2850 return bmap;
2853 bmap = isl_basic_map_remove_redundancies(bmap);
2854 context = isl_basic_map_remove_redundancies(context);
2855 if (!context)
2856 goto error;
2858 context = isl_basic_map_align_divs(context, bmap);
2859 n_div = isl_basic_map_dim(context, isl_dim_div);
2860 total = isl_basic_map_dim(bmap, isl_dim_all);
2861 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
2863 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2864 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
2865 bset = uset_gist(bset,
2866 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
2867 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
2869 if (!bset || bset->n_eq == 0 || n_div == 0 ||
2870 isl_basic_set_plain_is_empty(bset)) {
2871 isl_basic_map_free(context);
2872 return isl_basic_map_overlying_set(bset, bmap);
2875 n_eq = bset->n_eq;
2876 n_ineq = bset->n_ineq;
2877 eq = isl_basic_set_copy(bset);
2878 eq = isl_basic_set_cow(eq);
2879 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
2880 eq = isl_basic_set_free(eq);
2881 if (isl_basic_set_free_equality(bset, n_eq) < 0)
2882 bset = isl_basic_set_free(bset);
2884 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
2885 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
2886 bmap = isl_basic_map_overlying_set(bset, bmap);
2887 bmap = isl_basic_map_intersect(bmap, eq_bmap);
2888 bmap = isl_basic_map_remove_redundancies(bmap);
2890 return bmap;
2891 error:
2892 isl_basic_map_free(bmap);
2893 isl_basic_map_free(context);
2894 return NULL;
2898 * Assumes context has no implicit divs.
2900 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
2901 __isl_take isl_basic_map *context)
2903 int i;
2905 if (!map || !context)
2906 goto error;
2908 if (isl_basic_map_plain_is_empty(context)) {
2909 isl_space *space = isl_map_get_space(map);
2910 isl_map_free(map);
2911 isl_basic_map_free(context);
2912 return isl_map_universe(space);
2915 context = isl_basic_map_remove_redundancies(context);
2916 map = isl_map_cow(map);
2917 if (!map || !context)
2918 goto error;
2919 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
2920 map = isl_map_compute_divs(map);
2921 if (!map)
2922 goto error;
2923 for (i = map->n - 1; i >= 0; --i) {
2924 map->p[i] = isl_basic_map_gist(map->p[i],
2925 isl_basic_map_copy(context));
2926 if (!map->p[i])
2927 goto error;
2928 if (isl_basic_map_plain_is_empty(map->p[i])) {
2929 isl_basic_map_free(map->p[i]);
2930 if (i != map->n - 1)
2931 map->p[i] = map->p[map->n - 1];
2932 map->n--;
2935 isl_basic_map_free(context);
2936 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2937 return map;
2938 error:
2939 isl_map_free(map);
2940 isl_basic_map_free(context);
2941 return NULL;
2944 /* Drop all inequalities from "bmap" that also appear in "context".
2945 * "context" is assumed to have only known local variables and
2946 * the initial local variables of "bmap" are assumed to be the same
2947 * as those of "context".
2948 * The constraints of both "bmap" and "context" are assumed
2949 * to have been sorted using isl_basic_map_sort_constraints.
2951 * Run through the inequality constraints of "bmap" and "context"
2952 * in sorted order.
2953 * If a constraint of "bmap" involves variables not in "context",
2954 * then it cannot appear in "context".
2955 * If a matching constraint is found, it is removed from "bmap".
2957 static __isl_give isl_basic_map *drop_inequalities(
2958 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
2960 int i1, i2;
2961 unsigned total, extra;
2963 if (!bmap || !context)
2964 return isl_basic_map_free(bmap);
2966 total = isl_basic_map_total_dim(context);
2967 extra = isl_basic_map_total_dim(bmap) - total;
2969 i1 = bmap->n_ineq - 1;
2970 i2 = context->n_ineq - 1;
2971 while (bmap && i1 >= 0 && i2 >= 0) {
2972 int cmp;
2974 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
2975 extra) != -1) {
2976 --i1;
2977 continue;
2979 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
2980 context->ineq[i2]);
2981 if (cmp < 0) {
2982 --i2;
2983 continue;
2985 if (cmp > 0) {
2986 --i1;
2987 continue;
2989 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
2990 bmap = isl_basic_map_cow(bmap);
2991 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
2992 bmap = isl_basic_map_free(bmap);
2994 --i1;
2995 --i2;
2998 return bmap;
3001 /* Drop all equalities from "bmap" that also appear in "context".
3002 * "context" is assumed to have only known local variables and
3003 * the initial local variables of "bmap" are assumed to be the same
3004 * as those of "context".
3006 * Run through the equality constraints of "bmap" and "context"
3007 * in sorted order.
3008 * If a constraint of "bmap" involves variables not in "context",
3009 * then it cannot appear in "context".
3010 * If a matching constraint is found, it is removed from "bmap".
3012 static __isl_give isl_basic_map *drop_equalities(
3013 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3015 int i1, i2;
3016 unsigned total, extra;
3018 if (!bmap || !context)
3019 return isl_basic_map_free(bmap);
3021 total = isl_basic_map_total_dim(context);
3022 extra = isl_basic_map_total_dim(bmap) - total;
3024 i1 = bmap->n_eq - 1;
3025 i2 = context->n_eq - 1;
3027 while (bmap && i1 >= 0 && i2 >= 0) {
3028 int last1, last2;
3030 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3031 extra) != -1)
3032 break;
3033 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3034 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3035 if (last1 > last2) {
3036 --i2;
3037 continue;
3039 if (last1 < last2) {
3040 --i1;
3041 continue;
3043 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3044 bmap = isl_basic_map_cow(bmap);
3045 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3046 bmap = isl_basic_map_free(bmap);
3048 --i1;
3049 --i2;
3052 return bmap;
3055 /* Remove the constraints in "context" from "bmap".
3056 * "context" is assumed to have explicit representations
3057 * for all local variables.
3059 * First align the divs of "bmap" to those of "context" and
3060 * sort the constraints. Then drop all constraints from "bmap"
3061 * that appear in "context".
3063 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3064 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3066 isl_bool done, known;
3068 done = isl_basic_map_is_universe(context);
3069 if (done == isl_bool_false)
3070 done = isl_basic_map_is_universe(bmap);
3071 if (done == isl_bool_false)
3072 done = isl_basic_map_plain_is_empty(context);
3073 if (done == isl_bool_false)
3074 done = isl_basic_map_plain_is_empty(bmap);
3075 if (done < 0)
3076 goto error;
3077 if (done) {
3078 isl_basic_map_free(context);
3079 return bmap;
3081 known = isl_basic_map_divs_known(context);
3082 if (known < 0)
3083 goto error;
3084 if (!known)
3085 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3086 "context has unknown divs", goto error);
3088 bmap = isl_basic_map_align_divs(bmap, context);
3089 bmap = isl_basic_map_gauss(bmap, NULL);
3090 bmap = isl_basic_map_sort_constraints(bmap);
3091 context = isl_basic_map_sort_constraints(context);
3093 bmap = drop_inequalities(bmap, context);
3094 bmap = drop_equalities(bmap, context);
3096 isl_basic_map_free(context);
3097 bmap = isl_basic_map_finalize(bmap);
3098 return bmap;
3099 error:
3100 isl_basic_map_free(bmap);
3101 isl_basic_map_free(context);
3102 return NULL;
3105 /* Replace "map" by the disjunct at position "pos" and free "context".
3107 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3108 int pos, __isl_take isl_basic_map *context)
3110 isl_basic_map *bmap;
3112 bmap = isl_basic_map_copy(map->p[pos]);
3113 isl_map_free(map);
3114 isl_basic_map_free(context);
3115 return isl_map_from_basic_map(bmap);
3118 /* Remove the constraints in "context" from "map".
3119 * If any of the disjuncts in the result turns out to be the universe,
3120 * the return this universe.
3121 * "context" is assumed to have explicit representations
3122 * for all local variables.
3124 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3125 __isl_take isl_basic_map *context)
3127 int i;
3128 isl_bool univ, known;
3130 univ = isl_basic_map_is_universe(context);
3131 if (univ < 0)
3132 goto error;
3133 if (univ) {
3134 isl_basic_map_free(context);
3135 return map;
3137 known = isl_basic_map_divs_known(context);
3138 if (known < 0)
3139 goto error;
3140 if (!known)
3141 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3142 "context has unknown divs", goto error);
3144 map = isl_map_cow(map);
3145 if (!map)
3146 goto error;
3147 for (i = 0; i < map->n; ++i) {
3148 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3149 isl_basic_map_copy(context));
3150 univ = isl_basic_map_is_universe(map->p[i]);
3151 if (univ < 0)
3152 goto error;
3153 if (univ && map->n > 1)
3154 return replace_by_disjunct(map, i, context);
3157 isl_basic_map_free(context);
3158 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3159 if (map->n > 1)
3160 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3161 return map;
3162 error:
3163 isl_map_free(map);
3164 isl_basic_map_free(context);
3165 return NULL;
3168 /* Return a map that has the same intersection with "context" as "map"
3169 * and that is as "simple" as possible.
3171 * If "map" is already the universe, then we cannot make it any simpler.
3172 * Similarly, if "context" is the universe, then we cannot exploit it
3173 * to simplify "map"
3174 * If "map" and "context" are identical to each other, then we can
3175 * return the corresponding universe.
3177 * If none of these cases apply, we have to work a bit harder.
3178 * During this computation, we make use of a single disjunct context,
3179 * so if the original context consists of more than one disjunct
3180 * then we need to approximate the context by a single disjunct set.
3181 * Simply taking the simple hull may drop constraints that are
3182 * only implicitly available in each disjunct. We therefore also
3183 * look for constraints among those defining "map" that are valid
3184 * for the context. These can then be used to simplify away
3185 * the corresponding constraints in "map".
3187 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3188 __isl_take isl_map *context)
3190 int equal;
3191 int is_universe;
3192 isl_basic_map *hull;
3194 is_universe = isl_map_plain_is_universe(map);
3195 if (is_universe >= 0 && !is_universe)
3196 is_universe = isl_map_plain_is_universe(context);
3197 if (is_universe < 0)
3198 goto error;
3199 if (is_universe) {
3200 isl_map_free(context);
3201 return map;
3204 equal = isl_map_plain_is_equal(map, context);
3205 if (equal < 0)
3206 goto error;
3207 if (equal) {
3208 isl_map *res = isl_map_universe(isl_map_get_space(map));
3209 isl_map_free(map);
3210 isl_map_free(context);
3211 return res;
3214 context = isl_map_compute_divs(context);
3215 if (!context)
3216 goto error;
3217 if (isl_map_n_basic_map(context) == 1) {
3218 hull = isl_map_simple_hull(context);
3219 } else {
3220 isl_ctx *ctx;
3221 isl_map_list *list;
3223 ctx = isl_map_get_ctx(map);
3224 list = isl_map_list_alloc(ctx, 2);
3225 list = isl_map_list_add(list, isl_map_copy(context));
3226 list = isl_map_list_add(list, isl_map_copy(map));
3227 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3228 list);
3230 return isl_map_gist_basic_map(map, hull);
3231 error:
3232 isl_map_free(map);
3233 isl_map_free(context);
3234 return NULL;
3237 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3238 __isl_take isl_map *context)
3240 return isl_map_align_params_map_map_and(map, context, &map_gist);
3243 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3244 struct isl_basic_set *context)
3246 return (struct isl_basic_set *)isl_basic_map_gist(
3247 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
3250 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3251 __isl_take isl_basic_set *context)
3253 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
3254 (struct isl_basic_map *)context);
3257 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3258 __isl_take isl_basic_set *context)
3260 isl_space *space = isl_set_get_space(set);
3261 isl_basic_set *dom_context = isl_basic_set_universe(space);
3262 dom_context = isl_basic_set_intersect_params(dom_context, context);
3263 return isl_set_gist_basic_set(set, dom_context);
3266 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3267 __isl_take isl_set *context)
3269 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
3270 (struct isl_map *)context);
3273 /* Compute the gist of "bmap" with respect to the constraints "context"
3274 * on the domain.
3276 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3277 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3279 isl_space *space = isl_basic_map_get_space(bmap);
3280 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3282 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3283 return isl_basic_map_gist(bmap, bmap_context);
3286 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3287 __isl_take isl_set *context)
3289 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3290 map_context = isl_map_intersect_domain(map_context, context);
3291 return isl_map_gist(map, map_context);
3294 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3295 __isl_take isl_set *context)
3297 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3298 map_context = isl_map_intersect_range(map_context, context);
3299 return isl_map_gist(map, map_context);
3302 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3303 __isl_take isl_set *context)
3305 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3306 map_context = isl_map_intersect_params(map_context, context);
3307 return isl_map_gist(map, map_context);
3310 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3311 __isl_take isl_set *context)
3313 return isl_map_gist_params(set, context);
3316 /* Quick check to see if two basic maps are disjoint.
3317 * In particular, we reduce the equalities and inequalities of
3318 * one basic map in the context of the equalities of the other
3319 * basic map and check if we get a contradiction.
3321 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3322 __isl_keep isl_basic_map *bmap2)
3324 struct isl_vec *v = NULL;
3325 int *elim = NULL;
3326 unsigned total;
3327 int i;
3329 if (!bmap1 || !bmap2)
3330 return isl_bool_error;
3331 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3332 return isl_bool_error);
3333 if (bmap1->n_div || bmap2->n_div)
3334 return isl_bool_false;
3335 if (!bmap1->n_eq && !bmap2->n_eq)
3336 return isl_bool_false;
3338 total = isl_space_dim(bmap1->dim, isl_dim_all);
3339 if (total == 0)
3340 return isl_bool_false;
3341 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3342 if (!v)
3343 goto error;
3344 elim = isl_alloc_array(bmap1->ctx, int, total);
3345 if (!elim)
3346 goto error;
3347 compute_elimination_index(bmap1, elim);
3348 for (i = 0; i < bmap2->n_eq; ++i) {
3349 int reduced;
3350 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3351 bmap1, elim);
3352 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3353 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3354 goto disjoint;
3356 for (i = 0; i < bmap2->n_ineq; ++i) {
3357 int reduced;
3358 reduced = reduced_using_equalities(v->block.data,
3359 bmap2->ineq[i], bmap1, elim);
3360 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3361 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3362 goto disjoint;
3364 compute_elimination_index(bmap2, elim);
3365 for (i = 0; i < bmap1->n_ineq; ++i) {
3366 int reduced;
3367 reduced = reduced_using_equalities(v->block.data,
3368 bmap1->ineq[i], bmap2, elim);
3369 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3370 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3371 goto disjoint;
3373 isl_vec_free(v);
3374 free(elim);
3375 return isl_bool_false;
3376 disjoint:
3377 isl_vec_free(v);
3378 free(elim);
3379 return isl_bool_true;
3380 error:
3381 isl_vec_free(v);
3382 free(elim);
3383 return isl_bool_error;
3386 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3387 __isl_keep isl_basic_set *bset2)
3389 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
3390 (struct isl_basic_map *)bset2);
3393 /* Are "map1" and "map2" obviously disjoint?
3395 * If one of them is empty or if they live in different spaces (ignoring
3396 * parameters), then they are clearly disjoint.
3398 * If they have different parameters, then we skip any further tests.
3400 * If they are obviously equal, but not obviously empty, then we will
3401 * not be able to detect if they are disjoint.
3403 * Otherwise we check if each basic map in "map1" is obviously disjoint
3404 * from each basic map in "map2".
3406 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3407 __isl_keep isl_map *map2)
3409 int i, j;
3410 isl_bool disjoint;
3411 isl_bool intersect;
3412 isl_bool match;
3414 if (!map1 || !map2)
3415 return isl_bool_error;
3417 disjoint = isl_map_plain_is_empty(map1);
3418 if (disjoint < 0 || disjoint)
3419 return disjoint;
3421 disjoint = isl_map_plain_is_empty(map2);
3422 if (disjoint < 0 || disjoint)
3423 return disjoint;
3425 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3426 map2->dim, isl_dim_in);
3427 if (match < 0 || !match)
3428 return match < 0 ? isl_bool_error : isl_bool_true;
3430 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3431 map2->dim, isl_dim_out);
3432 if (match < 0 || !match)
3433 return match < 0 ? isl_bool_error : isl_bool_true;
3435 match = isl_space_match(map1->dim, isl_dim_param,
3436 map2->dim, isl_dim_param);
3437 if (match < 0 || !match)
3438 return match < 0 ? isl_bool_error : isl_bool_false;
3440 intersect = isl_map_plain_is_equal(map1, map2);
3441 if (intersect < 0 || intersect)
3442 return intersect < 0 ? isl_bool_error : isl_bool_false;
3444 for (i = 0; i < map1->n; ++i) {
3445 for (j = 0; j < map2->n; ++j) {
3446 isl_bool d = isl_basic_map_plain_is_disjoint(map1->p[i],
3447 map2->p[j]);
3448 if (d != isl_bool_true)
3449 return d;
3452 return isl_bool_true;
3455 /* Are "map1" and "map2" disjoint?
3457 * They are disjoint if they are "obviously disjoint" or if one of them
3458 * is empty. Otherwise, they are not disjoint if one of them is universal.
3459 * If none of these cases apply, we compute the intersection and see if
3460 * the result is empty.
3462 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3464 isl_bool disjoint;
3465 isl_bool intersect;
3466 isl_map *test;
3468 disjoint = isl_map_plain_is_disjoint(map1, map2);
3469 if (disjoint < 0 || disjoint)
3470 return disjoint;
3472 disjoint = isl_map_is_empty(map1);
3473 if (disjoint < 0 || disjoint)
3474 return disjoint;
3476 disjoint = isl_map_is_empty(map2);
3477 if (disjoint < 0 || disjoint)
3478 return disjoint;
3480 intersect = isl_map_plain_is_universe(map1);
3481 if (intersect < 0 || intersect)
3482 return intersect < 0 ? isl_bool_error : isl_bool_false;
3484 intersect = isl_map_plain_is_universe(map2);
3485 if (intersect < 0 || intersect)
3486 return intersect < 0 ? isl_bool_error : isl_bool_false;
3488 test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
3489 disjoint = isl_map_is_empty(test);
3490 isl_map_free(test);
3492 return disjoint;
3495 /* Are "bmap1" and "bmap2" disjoint?
3497 * They are disjoint if they are "obviously disjoint" or if one of them
3498 * is empty. Otherwise, they are not disjoint if one of them is universal.
3499 * If none of these cases apply, we compute the intersection and see if
3500 * the result is empty.
3502 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3503 __isl_keep isl_basic_map *bmap2)
3505 isl_bool disjoint;
3506 isl_bool intersect;
3507 isl_basic_map *test;
3509 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3510 if (disjoint < 0 || disjoint)
3511 return disjoint;
3513 disjoint = isl_basic_map_is_empty(bmap1);
3514 if (disjoint < 0 || disjoint)
3515 return disjoint;
3517 disjoint = isl_basic_map_is_empty(bmap2);
3518 if (disjoint < 0 || disjoint)
3519 return disjoint;
3521 intersect = isl_basic_map_is_universe(bmap1);
3522 if (intersect < 0 || intersect)
3523 return intersect < 0 ? isl_bool_error : isl_bool_false;
3525 intersect = isl_basic_map_is_universe(bmap2);
3526 if (intersect < 0 || intersect)
3527 return intersect < 0 ? isl_bool_error : isl_bool_false;
3529 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3530 isl_basic_map_copy(bmap2));
3531 disjoint = isl_basic_map_is_empty(test);
3532 isl_basic_map_free(test);
3534 return disjoint;
3537 /* Are "bset1" and "bset2" disjoint?
3539 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3540 __isl_keep isl_basic_set *bset2)
3542 return isl_basic_map_is_disjoint(bset1, bset2);
3545 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3546 __isl_keep isl_set *set2)
3548 return isl_map_plain_is_disjoint((struct isl_map *)set1,
3549 (struct isl_map *)set2);
3552 /* Are "set1" and "set2" disjoint?
3554 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3556 return isl_map_is_disjoint(set1, set2);
3559 /* Is "v" equal to 0, 1 or -1?
3561 static int is_zero_or_one(isl_int v)
3563 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3566 /* Check if we can combine a given div with lower bound l and upper
3567 * bound u with some other div and if so return that other div.
3568 * Otherwise return -1.
3570 * We first check that
3571 * - the bounds are opposites of each other (except for the constant
3572 * term)
3573 * - the bounds do not reference any other div
3574 * - no div is defined in terms of this div
3576 * Let m be the size of the range allowed on the div by the bounds.
3577 * That is, the bounds are of the form
3579 * e <= a <= e + m - 1
3581 * with e some expression in the other variables.
3582 * We look for another div b such that no third div is defined in terms
3583 * of this second div b and such that in any constraint that contains
3584 * a (except for the given lower and upper bound), also contains b
3585 * with a coefficient that is m times that of b.
3586 * That is, all constraints (execpt for the lower and upper bound)
3587 * are of the form
3589 * e + f (a + m b) >= 0
3591 * Furthermore, in the constraints that only contain b, the coefficient
3592 * of b should be equal to 1 or -1.
3593 * If so, we return b so that "a + m b" can be replaced by
3594 * a single div "c = a + m b".
3596 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
3597 unsigned div, unsigned l, unsigned u)
3599 int i, j;
3600 unsigned dim;
3601 int coalesce = -1;
3603 if (bmap->n_div <= 1)
3604 return -1;
3605 dim = isl_space_dim(bmap->dim, isl_dim_all);
3606 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3607 return -1;
3608 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3609 bmap->n_div - div - 1) != -1)
3610 return -1;
3611 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3612 dim + bmap->n_div))
3613 return -1;
3615 for (i = 0; i < bmap->n_div; ++i) {
3616 if (isl_int_is_zero(bmap->div[i][0]))
3617 continue;
3618 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3619 return -1;
3622 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3623 if (isl_int_is_neg(bmap->ineq[l][0])) {
3624 isl_int_sub(bmap->ineq[l][0],
3625 bmap->ineq[l][0], bmap->ineq[u][0]);
3626 bmap = isl_basic_map_copy(bmap);
3627 bmap = isl_basic_map_set_to_empty(bmap);
3628 isl_basic_map_free(bmap);
3629 return -1;
3631 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3632 for (i = 0; i < bmap->n_div; ++i) {
3633 if (i == div)
3634 continue;
3635 if (!pairs[i])
3636 continue;
3637 for (j = 0; j < bmap->n_div; ++j) {
3638 if (isl_int_is_zero(bmap->div[j][0]))
3639 continue;
3640 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3641 break;
3643 if (j < bmap->n_div)
3644 continue;
3645 for (j = 0; j < bmap->n_ineq; ++j) {
3646 int valid;
3647 if (j == l || j == u)
3648 continue;
3649 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
3650 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3651 continue;
3652 break;
3654 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3655 break;
3656 isl_int_mul(bmap->ineq[j][1 + dim + div],
3657 bmap->ineq[j][1 + dim + div],
3658 bmap->ineq[l][0]);
3659 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3660 bmap->ineq[j][1 + dim + i]);
3661 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3662 bmap->ineq[j][1 + dim + div],
3663 bmap->ineq[l][0]);
3664 if (!valid)
3665 break;
3667 if (j < bmap->n_ineq)
3668 continue;
3669 coalesce = i;
3670 break;
3672 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3673 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3674 return coalesce;
3677 /* Given a lower and an upper bound on div i, construct an inequality
3678 * that when nonnegative ensures that this pair of bounds always allows
3679 * for an integer value of the given div.
3680 * The lower bound is inequality l, while the upper bound is inequality u.
3681 * The constructed inequality is stored in ineq.
3682 * g, fl, fu are temporary scalars.
3684 * Let the upper bound be
3686 * -n_u a + e_u >= 0
3688 * and the lower bound
3690 * n_l a + e_l >= 0
3692 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
3693 * We have
3695 * - f_u e_l <= f_u f_l g a <= f_l e_u
3697 * Since all variables are integer valued, this is equivalent to
3699 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
3701 * If this interval is at least f_u f_l g, then it contains at least
3702 * one integer value for a.
3703 * That is, the test constraint is
3705 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
3707 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
3708 int l, int u, isl_int *ineq, isl_int *g, isl_int *fl, isl_int *fu)
3710 unsigned dim;
3711 dim = isl_space_dim(bmap->dim, isl_dim_all);
3713 isl_int_gcd(*g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
3714 isl_int_divexact(*fl, bmap->ineq[l][1 + dim + i], *g);
3715 isl_int_divexact(*fu, bmap->ineq[u][1 + dim + i], *g);
3716 isl_int_neg(*fu, *fu);
3717 isl_seq_combine(ineq, *fl, bmap->ineq[u], *fu, bmap->ineq[l],
3718 1 + dim + bmap->n_div);
3719 isl_int_add(ineq[0], ineq[0], *fl);
3720 isl_int_add(ineq[0], ineq[0], *fu);
3721 isl_int_sub_ui(ineq[0], ineq[0], 1);
3722 isl_int_mul(*g, *g, *fl);
3723 isl_int_mul(*g, *g, *fu);
3724 isl_int_sub(ineq[0], ineq[0], *g);
3727 /* Remove more kinds of divs that are not strictly needed.
3728 * In particular, if all pairs of lower and upper bounds on a div
3729 * are such that they allow at least one integer value of the div,
3730 * the we can eliminate the div using Fourier-Motzkin without
3731 * introducing any spurious solutions.
3733 static struct isl_basic_map *drop_more_redundant_divs(
3734 struct isl_basic_map *bmap, int *pairs, int n)
3736 struct isl_tab *tab = NULL;
3737 struct isl_vec *vec = NULL;
3738 unsigned dim;
3739 int remove = -1;
3740 isl_int g, fl, fu;
3742 isl_int_init(g);
3743 isl_int_init(fl);
3744 isl_int_init(fu);
3746 if (!bmap)
3747 goto error;
3749 dim = isl_space_dim(bmap->dim, isl_dim_all);
3750 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
3751 if (!vec)
3752 goto error;
3754 tab = isl_tab_from_basic_map(bmap, 0);
3756 while (n > 0) {
3757 int i, l, u;
3758 int best = -1;
3759 enum isl_lp_result res;
3761 for (i = 0; i < bmap->n_div; ++i) {
3762 if (!pairs[i])
3763 continue;
3764 if (best >= 0 && pairs[best] <= pairs[i])
3765 continue;
3766 best = i;
3769 i = best;
3770 for (l = 0; l < bmap->n_ineq; ++l) {
3771 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
3772 continue;
3773 for (u = 0; u < bmap->n_ineq; ++u) {
3774 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
3775 continue;
3776 construct_test_ineq(bmap, i, l, u,
3777 vec->el, &g, &fl, &fu);
3778 res = isl_tab_min(tab, vec->el,
3779 bmap->ctx->one, &g, NULL, 0);
3780 if (res == isl_lp_error)
3781 goto error;
3782 if (res == isl_lp_empty) {
3783 bmap = isl_basic_map_set_to_empty(bmap);
3784 break;
3786 if (res != isl_lp_ok || isl_int_is_neg(g))
3787 break;
3789 if (u < bmap->n_ineq)
3790 break;
3792 if (l == bmap->n_ineq) {
3793 remove = i;
3794 break;
3796 pairs[i] = 0;
3797 --n;
3800 isl_tab_free(tab);
3801 isl_vec_free(vec);
3803 isl_int_clear(g);
3804 isl_int_clear(fl);
3805 isl_int_clear(fu);
3807 free(pairs);
3809 if (remove < 0)
3810 return bmap;
3812 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
3813 return isl_basic_map_drop_redundant_divs(bmap);
3814 error:
3815 free(pairs);
3816 isl_basic_map_free(bmap);
3817 isl_tab_free(tab);
3818 isl_vec_free(vec);
3819 isl_int_clear(g);
3820 isl_int_clear(fl);
3821 isl_int_clear(fu);
3822 return NULL;
3825 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
3826 * and the upper bound u, div1 always occurs together with div2 in the form
3827 * (div1 + m div2), where m is the constant range on the variable div1
3828 * allowed by l and u, replace the pair div1 and div2 by a single
3829 * div that is equal to div1 + m div2.
3831 * The new div will appear in the location that contains div2.
3832 * We need to modify all constraints that contain
3833 * div2 = (div - div1) / m
3834 * The coefficient of div2 is known to be equal to 1 or -1.
3835 * (If a constraint does not contain div2, it will also not contain div1.)
3836 * If the constraint also contains div1, then we know they appear
3837 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3838 * i.e., the coefficient of div is f.
3840 * Otherwise, we first need to introduce div1 into the constraint.
3841 * Let the l be
3843 * div1 + f >=0
3845 * and u
3847 * -div1 + f' >= 0
3849 * A lower bound on div2
3851 * div2 + t >= 0
3853 * can be replaced by
3855 * m div2 + div1 + m t + f >= 0
3857 * An upper bound
3859 * -div2 + t >= 0
3861 * can be replaced by
3863 * -(m div2 + div1) + m t + f' >= 0
3865 * These constraint are those that we would obtain from eliminating
3866 * div1 using Fourier-Motzkin.
3868 * After all constraints have been modified, we drop the lower and upper
3869 * bound and then drop div1.
3871 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
3872 unsigned div1, unsigned div2, unsigned l, unsigned u)
3874 isl_ctx *ctx;
3875 isl_int m;
3876 unsigned dim, total;
3877 int i;
3879 ctx = isl_basic_map_get_ctx(bmap);
3881 dim = isl_space_dim(bmap->dim, isl_dim_all);
3882 total = 1 + dim + bmap->n_div;
3884 isl_int_init(m);
3885 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
3886 isl_int_add_ui(m, m, 1);
3888 for (i = 0; i < bmap->n_ineq; ++i) {
3889 if (i == l || i == u)
3890 continue;
3891 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
3892 continue;
3893 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1]))
3894 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
3895 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
3896 ctx->one, bmap->ineq[l], total);
3897 else
3898 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
3899 ctx->one, bmap->ineq[u], total);
3900 isl_int_set(bmap->ineq[i][1 + dim + div2],
3901 bmap->ineq[i][1 + dim + div1]);
3902 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
3905 isl_int_clear(m);
3906 if (l > u) {
3907 isl_basic_map_drop_inequality(bmap, l);
3908 isl_basic_map_drop_inequality(bmap, u);
3909 } else {
3910 isl_basic_map_drop_inequality(bmap, u);
3911 isl_basic_map_drop_inequality(bmap, l);
3913 bmap = isl_basic_map_drop_div(bmap, div1);
3914 return bmap;
3917 /* First check if we can coalesce any pair of divs and
3918 * then continue with dropping more redundant divs.
3920 * We loop over all pairs of lower and upper bounds on a div
3921 * with coefficient 1 and -1, respectively, check if there
3922 * is any other div "c" with which we can coalesce the div
3923 * and if so, perform the coalescing.
3925 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
3926 struct isl_basic_map *bmap, int *pairs, int n)
3928 int i, l, u;
3929 unsigned dim;
3931 dim = isl_space_dim(bmap->dim, isl_dim_all);
3933 for (i = 0; i < bmap->n_div; ++i) {
3934 if (!pairs[i])
3935 continue;
3936 for (l = 0; l < bmap->n_ineq; ++l) {
3937 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
3938 continue;
3939 for (u = 0; u < bmap->n_ineq; ++u) {
3940 int c;
3942 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
3943 continue;
3944 c = div_find_coalesce(bmap, pairs, i, l, u);
3945 if (c < 0)
3946 continue;
3947 free(pairs);
3948 bmap = coalesce_divs(bmap, i, c, l, u);
3949 return isl_basic_map_drop_redundant_divs(bmap);
3954 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3955 return bmap;
3957 return drop_more_redundant_divs(bmap, pairs, n);
3960 /* Remove divs that are not strictly needed.
3961 * In particular, if a div only occurs positively (or negatively)
3962 * in constraints, then it can simply be dropped.
3963 * Also, if a div occurs in only two constraints and if moreover
3964 * those two constraints are opposite to each other, except for the constant
3965 * term and if the sum of the constant terms is such that for any value
3966 * of the other values, there is always at least one integer value of the
3967 * div, i.e., if one plus this sum is greater than or equal to
3968 * the (absolute value) of the coefficent of the div in the constraints,
3969 * then we can also simply drop the div.
3971 * We skip divs that appear in equalities or in the definition of other divs.
3972 * Divs that appear in the definition of other divs usually occur in at least
3973 * 4 constraints, but the constraints may have been simplified.
3975 * If any divs are left after these simple checks then we move on
3976 * to more complicated cases in drop_more_redundant_divs.
3978 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
3979 struct isl_basic_map *bmap)
3981 int i, j;
3982 unsigned off;
3983 int *pairs = NULL;
3984 int n = 0;
3986 if (!bmap)
3987 goto error;
3988 if (bmap->n_div == 0)
3989 return bmap;
3991 off = isl_space_dim(bmap->dim, isl_dim_all);
3992 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
3993 if (!pairs)
3994 goto error;
3996 for (i = 0; i < bmap->n_div; ++i) {
3997 int pos, neg;
3998 int last_pos, last_neg;
3999 int redundant;
4000 int defined;
4002 defined = !isl_int_is_zero(bmap->div[i][0]);
4003 for (j = i; j < bmap->n_div; ++j)
4004 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4005 break;
4006 if (j < bmap->n_div)
4007 continue;
4008 for (j = 0; j < bmap->n_eq; ++j)
4009 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4010 break;
4011 if (j < bmap->n_eq)
4012 continue;
4013 ++n;
4014 pos = neg = 0;
4015 for (j = 0; j < bmap->n_ineq; ++j) {
4016 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4017 last_pos = j;
4018 ++pos;
4020 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4021 last_neg = j;
4022 ++neg;
4025 pairs[i] = pos * neg;
4026 if (pairs[i] == 0) {
4027 for (j = bmap->n_ineq - 1; j >= 0; --j)
4028 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4029 isl_basic_map_drop_inequality(bmap, j);
4030 bmap = isl_basic_map_drop_div(bmap, i);
4031 free(pairs);
4032 return isl_basic_map_drop_redundant_divs(bmap);
4034 if (pairs[i] != 1)
4035 continue;
4036 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
4037 bmap->ineq[last_neg] + 1,
4038 off + bmap->n_div))
4039 continue;
4041 isl_int_add(bmap->ineq[last_pos][0],
4042 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4043 isl_int_add_ui(bmap->ineq[last_pos][0],
4044 bmap->ineq[last_pos][0], 1);
4045 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4046 bmap->ineq[last_pos][1+off+i]);
4047 isl_int_sub_ui(bmap->ineq[last_pos][0],
4048 bmap->ineq[last_pos][0], 1);
4049 isl_int_sub(bmap->ineq[last_pos][0],
4050 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4051 if (!redundant) {
4052 if (defined ||
4053 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
4054 pairs[i] = 0;
4055 --n;
4056 continue;
4058 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4059 bmap = isl_basic_map_simplify(bmap);
4060 free(pairs);
4061 return isl_basic_map_drop_redundant_divs(bmap);
4063 if (last_pos > last_neg) {
4064 isl_basic_map_drop_inequality(bmap, last_pos);
4065 isl_basic_map_drop_inequality(bmap, last_neg);
4066 } else {
4067 isl_basic_map_drop_inequality(bmap, last_neg);
4068 isl_basic_map_drop_inequality(bmap, last_pos);
4070 bmap = isl_basic_map_drop_div(bmap, i);
4071 free(pairs);
4072 return isl_basic_map_drop_redundant_divs(bmap);
4075 if (n > 0)
4076 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4078 free(pairs);
4079 return bmap;
4080 error:
4081 free(pairs);
4082 isl_basic_map_free(bmap);
4083 return NULL;
4086 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
4087 struct isl_basic_set *bset)
4089 return (struct isl_basic_set *)
4090 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
4093 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
4095 int i;
4097 if (!map)
4098 return NULL;
4099 for (i = 0; i < map->n; ++i) {
4100 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
4101 if (!map->p[i])
4102 goto error;
4104 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4105 return map;
4106 error:
4107 isl_map_free(map);
4108 return NULL;
4111 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
4113 return (struct isl_set *)
4114 isl_map_drop_redundant_divs((struct isl_map *)set);
4117 /* Does "bmap" satisfy any equality that involves more than 2 variables
4118 * and/or has coefficients different from -1 and 1?
4120 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
4122 int i;
4123 unsigned total;
4125 total = isl_basic_map_dim(bmap, isl_dim_all);
4127 for (i = 0; i < bmap->n_eq; ++i) {
4128 int j, k;
4130 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
4131 if (j < 0)
4132 continue;
4133 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
4134 !isl_int_is_negone(bmap->eq[i][1 + j]))
4135 return 1;
4137 j += 1;
4138 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
4139 if (k < 0)
4140 continue;
4141 j += k;
4142 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
4143 !isl_int_is_negone(bmap->eq[i][1 + j]))
4144 return 1;
4146 j += 1;
4147 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
4148 if (k >= 0)
4149 return 1;
4152 return 0;
4155 /* Remove any common factor g from the constraint coefficients in "v".
4156 * The constant term is stored in the first position and is replaced
4157 * by floor(c/g). If any common factor is removed and if this results
4158 * in a tightening of the constraint, then set *tightened.
4160 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
4161 int *tightened)
4163 isl_ctx *ctx;
4165 if (!v)
4166 return NULL;
4167 ctx = isl_vec_get_ctx(v);
4168 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
4169 if (isl_int_is_zero(ctx->normalize_gcd))
4170 return v;
4171 if (isl_int_is_one(ctx->normalize_gcd))
4172 return v;
4173 v = isl_vec_cow(v);
4174 if (!v)
4175 return NULL;
4176 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
4177 *tightened = 1;
4178 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
4179 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
4180 v->size - 1);
4181 return v;
4184 /* If "bmap" is an integer set that satisfies any equality involving
4185 * more than 2 variables and/or has coefficients different from -1 and 1,
4186 * then use variable compression to reduce the coefficients by removing
4187 * any (hidden) common factor.
4188 * In particular, apply the variable compression to each constraint,
4189 * factor out any common factor in the non-constant coefficients and
4190 * then apply the inverse of the compression.
4191 * At the end, we mark the basic map as having reduced constants.
4192 * If this flag is still set on the next invocation of this function,
4193 * then we skip the computation.
4195 * Removing a common factor may result in a tightening of some of
4196 * the constraints. If this happens, then we may end up with two
4197 * opposite inequalities that can be replaced by an equality.
4198 * We therefore call isl_basic_map_detect_inequality_pairs,
4199 * which checks for such pairs of inequalities as well as eliminate_divs_eq
4200 * and isl_basic_map_gauss if such a pair was found.
4202 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
4203 __isl_take isl_basic_map *bmap)
4205 unsigned total;
4206 isl_ctx *ctx;
4207 isl_vec *v;
4208 isl_mat *eq, *T, *T2;
4209 int i;
4210 int tightened;
4212 if (!bmap)
4213 return NULL;
4214 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
4215 return bmap;
4216 if (isl_basic_map_is_rational(bmap))
4217 return bmap;
4218 if (bmap->n_eq == 0)
4219 return bmap;
4220 if (!has_multiple_var_equality(bmap))
4221 return bmap;
4223 total = isl_basic_map_dim(bmap, isl_dim_all);
4224 ctx = isl_basic_map_get_ctx(bmap);
4225 v = isl_vec_alloc(ctx, 1 + total);
4226 if (!v)
4227 return isl_basic_map_free(bmap);
4229 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
4230 T = isl_mat_variable_compression(eq, &T2);
4231 if (!T || !T2)
4232 goto error;
4233 if (T->n_col == 0) {
4234 isl_mat_free(T);
4235 isl_mat_free(T2);
4236 isl_vec_free(v);
4237 return isl_basic_map_set_to_empty(bmap);
4240 tightened = 0;
4241 for (i = 0; i < bmap->n_ineq; ++i) {
4242 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
4243 v = isl_vec_mat_product(v, isl_mat_copy(T));
4244 v = normalize_constraint(v, &tightened);
4245 v = isl_vec_mat_product(v, isl_mat_copy(T2));
4246 if (!v)
4247 goto error;
4248 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
4251 isl_mat_free(T);
4252 isl_mat_free(T2);
4253 isl_vec_free(v);
4255 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
4257 if (tightened) {
4258 int progress = 0;
4260 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
4261 if (progress) {
4262 bmap = eliminate_divs_eq(bmap, &progress);
4263 bmap = isl_basic_map_gauss(bmap, NULL);
4267 return bmap;
4268 error:
4269 isl_mat_free(T);
4270 isl_mat_free(T2);
4271 isl_vec_free(v);
4272 return isl_basic_map_free(bmap);
4275 /* Shift the integer division at position "div" of "bmap"
4276 * by "shift" times the variable at position "pos".
4277 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
4278 * corresponds to the constant term.
4280 * That is, if the integer division has the form
4282 * floor(f(x)/d)
4284 * then replace it by
4286 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
4288 __isl_give isl_basic_map *isl_basic_map_shift_div(
4289 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
4291 int i;
4292 unsigned total;
4294 if (!bmap)
4295 return NULL;
4297 total = isl_basic_map_dim(bmap, isl_dim_all);
4298 total -= isl_basic_map_dim(bmap, isl_dim_div);
4300 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
4302 for (i = 0; i < bmap->n_eq; ++i) {
4303 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
4304 continue;
4305 isl_int_submul(bmap->eq[i][pos],
4306 shift, bmap->eq[i][1 + total + div]);
4308 for (i = 0; i < bmap->n_ineq; ++i) {
4309 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
4310 continue;
4311 isl_int_submul(bmap->ineq[i][pos],
4312 shift, bmap->ineq[i][1 + total + div]);
4314 for (i = 0; i < bmap->n_div; ++i) {
4315 if (isl_int_is_zero(bmap->div[i][0]))
4316 continue;
4317 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
4318 continue;
4319 isl_int_submul(bmap->div[i][1 + pos],
4320 shift, bmap->div[i][1 + 1 + total + div]);
4323 return bmap;