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