isl_map_simplify.c: uset_gist_full: take constraint matrix as input
[isl.git] / isl_map_simplify.c
blobef9a110b1191c07881eef81e685f088e8ef6b363
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include "isl_equalities.h"
18 #include <isl/map.h>
19 #include <isl_seq.h>
20 #include "isl_tab.h"
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
25 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
27 isl_int *t = bmap->eq[a];
28 bmap->eq[a] = bmap->eq[b];
29 bmap->eq[b] = t;
32 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
34 if (a != b) {
35 isl_int *t = bmap->ineq[a];
36 bmap->ineq[a] = bmap->ineq[b];
37 bmap->ineq[b] = t;
41 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
43 isl_seq_cpy(c, c + n, rem);
44 isl_seq_clr(c + rem, n);
47 /* Drop n dimensions starting at first.
49 * In principle, this frees up some extra variables as the number
50 * of columns remains constant, but we would have to extend
51 * the div array too as the number of rows in this array is assumed
52 * to be equal to extra.
54 struct isl_basic_set *isl_basic_set_drop_dims(
55 struct isl_basic_set *bset, unsigned first, unsigned n)
57 int i;
59 if (!bset)
60 goto error;
62 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
64 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
65 return bset;
67 bset = isl_basic_set_cow(bset);
68 if (!bset)
69 return NULL;
71 for (i = 0; i < bset->n_eq; ++i)
72 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
73 (bset->dim->n_out-first-n)+bset->extra);
75 for (i = 0; i < bset->n_ineq; ++i)
76 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
77 (bset->dim->n_out-first-n)+bset->extra);
79 for (i = 0; i < bset->n_div; ++i)
80 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
81 (bset->dim->n_out-first-n)+bset->extra);
83 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
84 if (!bset->dim)
85 goto error;
87 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
88 bset = isl_basic_set_simplify(bset);
89 return isl_basic_set_finalize(bset);
90 error:
91 isl_basic_set_free(bset);
92 return NULL;
95 struct isl_set *isl_set_drop_dims(
96 struct isl_set *set, unsigned first, unsigned n)
98 int i;
100 if (!set)
101 goto error;
103 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
105 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
106 return set;
107 set = isl_set_cow(set);
108 if (!set)
109 goto error;
110 set->dim = isl_space_drop_outputs(set->dim, first, n);
111 if (!set->dim)
112 goto error;
114 for (i = 0; i < set->n; ++i) {
115 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
116 if (!set->p[i])
117 goto error;
120 ISL_F_CLR(set, ISL_SET_NORMALIZED);
121 return set;
122 error:
123 isl_set_free(set);
124 return NULL;
127 /* Move "n" divs starting at "first" to the end of the list of divs.
129 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
130 unsigned first, unsigned n)
132 isl_int **div;
133 int i;
135 if (first + n == bmap->n_div)
136 return bmap;
138 div = isl_alloc_array(bmap->ctx, isl_int *, n);
139 if (!div)
140 goto error;
141 for (i = 0; i < n; ++i)
142 div[i] = bmap->div[first + i];
143 for (i = 0; i < bmap->n_div - first - n; ++i)
144 bmap->div[first + i] = bmap->div[first + n + i];
145 for (i = 0; i < n; ++i)
146 bmap->div[bmap->n_div - n + i] = div[i];
147 free(div);
148 return bmap;
149 error:
150 isl_basic_map_free(bmap);
151 return NULL;
154 /* Drop "n" dimensions of type "type" starting at "first".
156 * In principle, this frees up some extra variables as the number
157 * of columns remains constant, but we would have to extend
158 * the div array too as the number of rows in this array is assumed
159 * to be equal to extra.
161 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
162 enum isl_dim_type type, unsigned first, unsigned n)
164 int i;
165 unsigned dim;
166 unsigned offset;
167 unsigned left;
169 if (!bmap)
170 goto error;
172 dim = isl_basic_map_dim(bmap, type);
173 isl_assert(bmap->ctx, first + n <= dim, goto error);
175 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
176 return bmap;
178 bmap = isl_basic_map_cow(bmap);
179 if (!bmap)
180 return NULL;
182 offset = isl_basic_map_offset(bmap, type) + first;
183 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
184 for (i = 0; i < bmap->n_eq; ++i)
185 constraint_drop_vars(bmap->eq[i]+offset, n, left);
187 for (i = 0; i < bmap->n_ineq; ++i)
188 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
190 for (i = 0; i < bmap->n_div; ++i)
191 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
193 if (type == isl_dim_div) {
194 bmap = move_divs_last(bmap, first, n);
195 if (!bmap)
196 goto error;
197 isl_basic_map_free_div(bmap, n);
198 } else
199 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
200 if (!bmap->dim)
201 goto error;
203 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
204 bmap = isl_basic_map_simplify(bmap);
205 return isl_basic_map_finalize(bmap);
206 error:
207 isl_basic_map_free(bmap);
208 return NULL;
211 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
212 enum isl_dim_type type, unsigned first, unsigned n)
214 return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset,
215 type, first, n);
218 struct isl_basic_map *isl_basic_map_drop_inputs(
219 struct isl_basic_map *bmap, unsigned first, unsigned n)
221 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
224 struct isl_map *isl_map_drop(struct isl_map *map,
225 enum isl_dim_type type, unsigned first, unsigned n)
227 int i;
229 if (!map)
230 goto error;
232 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
234 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
235 return map;
236 map = isl_map_cow(map);
237 if (!map)
238 goto error;
239 map->dim = isl_space_drop_dims(map->dim, type, first, n);
240 if (!map->dim)
241 goto error;
243 for (i = 0; i < map->n; ++i) {
244 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
245 if (!map->p[i])
246 goto error;
248 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
250 return map;
251 error:
252 isl_map_free(map);
253 return NULL;
256 struct isl_set *isl_set_drop(struct isl_set *set,
257 enum isl_dim_type type, unsigned first, unsigned n)
259 return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
262 struct isl_map *isl_map_drop_inputs(
263 struct isl_map *map, unsigned first, unsigned n)
265 return isl_map_drop(map, isl_dim_in, first, n);
269 * We don't cow, as the div is assumed to be redundant.
271 static struct isl_basic_map *isl_basic_map_drop_div(
272 struct isl_basic_map *bmap, unsigned div)
274 int i;
275 unsigned pos;
277 if (!bmap)
278 goto error;
280 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
282 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
284 for (i = 0; i < bmap->n_eq; ++i)
285 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
287 for (i = 0; i < bmap->n_ineq; ++i) {
288 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
289 isl_basic_map_drop_inequality(bmap, i);
290 --i;
291 continue;
293 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
296 for (i = 0; i < bmap->n_div; ++i)
297 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
299 if (div != bmap->n_div - 1) {
300 int j;
301 isl_int *t = bmap->div[div];
303 for (j = div; j < bmap->n_div - 1; ++j)
304 bmap->div[j] = bmap->div[j+1];
306 bmap->div[bmap->n_div - 1] = t;
308 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
309 isl_basic_map_free_div(bmap, 1);
311 return bmap;
312 error:
313 isl_basic_map_free(bmap);
314 return NULL;
317 struct isl_basic_map *isl_basic_map_normalize_constraints(
318 struct isl_basic_map *bmap)
320 int i;
321 isl_int gcd;
322 unsigned total = isl_basic_map_total_dim(bmap);
324 if (!bmap)
325 return NULL;
327 isl_int_init(gcd);
328 for (i = bmap->n_eq - 1; i >= 0; --i) {
329 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
330 if (isl_int_is_zero(gcd)) {
331 if (!isl_int_is_zero(bmap->eq[i][0])) {
332 bmap = isl_basic_map_set_to_empty(bmap);
333 break;
335 isl_basic_map_drop_equality(bmap, i);
336 continue;
338 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
339 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
340 if (isl_int_is_one(gcd))
341 continue;
342 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
343 bmap = isl_basic_map_set_to_empty(bmap);
344 break;
346 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
349 for (i = bmap->n_ineq - 1; i >= 0; --i) {
350 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
351 if (isl_int_is_zero(gcd)) {
352 if (isl_int_is_neg(bmap->ineq[i][0])) {
353 bmap = isl_basic_map_set_to_empty(bmap);
354 break;
356 isl_basic_map_drop_inequality(bmap, i);
357 continue;
359 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
360 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
361 if (isl_int_is_one(gcd))
362 continue;
363 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
364 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
366 isl_int_clear(gcd);
368 return bmap;
371 struct isl_basic_set *isl_basic_set_normalize_constraints(
372 struct isl_basic_set *bset)
374 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
375 (struct isl_basic_map *)bset);
378 /* Assuming the variable at position "pos" has an integer coefficient
379 * in integer division "div", extract it from this integer division.
380 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
381 * corresponds to the constant term.
383 * That is, the integer division is of the form
385 * floor((... + c * d * x_pos + ...)/d)
387 * Replace it by
389 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
391 static __isl_give isl_basic_map *remove_var_from_div(
392 __isl_take isl_basic_map *bmap, int div, int pos)
394 isl_int shift;
396 isl_int_init(shift);
397 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
398 isl_int_neg(shift, shift);
399 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
400 isl_int_clear(shift);
402 return bmap;
405 /* Check if integer division "div" has any integral coefficient
406 * (or constant term). If so, extract them from the integer division.
408 static __isl_give isl_basic_map *remove_independent_vars_from_div(
409 __isl_take isl_basic_map *bmap, int div)
411 int i;
412 unsigned total = 1 + isl_basic_map_total_dim(bmap);
414 for (i = 0; i < total; ++i) {
415 if (isl_int_is_zero(bmap->div[div][1 + i]))
416 continue;
417 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
418 bmap->div[div][0]))
419 continue;
420 bmap = remove_var_from_div(bmap, div, i);
421 if (!bmap)
422 break;
425 return bmap;
428 /* Check if any known integer division has any integral coefficient
429 * (or constant term). If so, extract them from the integer division.
431 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
432 __isl_take isl_basic_map *bmap)
434 int i;
436 if (!bmap)
437 return NULL;
438 if (bmap->n_div == 0)
439 return bmap;
441 for (i = 0; i < bmap->n_div; ++i) {
442 if (isl_int_is_zero(bmap->div[i][0]))
443 continue;
444 bmap = remove_independent_vars_from_div(bmap, i);
445 if (!bmap)
446 break;
449 return bmap;
452 /* Remove any common factor in numerator and denominator of the div expression,
453 * not taking into account the constant term.
454 * That is, if the div is of the form
456 * floor((a + m f(x))/(m d))
458 * then replace it by
460 * floor((floor(a/m) + f(x))/d)
462 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
463 * and can therefore not influence the result of the floor.
465 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
467 unsigned total = isl_basic_map_total_dim(bmap);
468 isl_ctx *ctx = bmap->ctx;
470 if (isl_int_is_zero(bmap->div[div][0]))
471 return;
472 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
473 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
474 if (isl_int_is_one(ctx->normalize_gcd))
475 return;
476 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
477 ctx->normalize_gcd);
478 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
479 ctx->normalize_gcd);
480 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
481 ctx->normalize_gcd, total);
484 /* Remove any common factor in numerator and denominator of a div expression,
485 * not taking into account the constant term.
486 * That is, look for any div of the form
488 * floor((a + m f(x))/(m d))
490 * and replace it by
492 * floor((floor(a/m) + f(x))/d)
494 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
495 * and can therefore not influence the result of the floor.
497 static __isl_give isl_basic_map *normalize_div_expressions(
498 __isl_take isl_basic_map *bmap)
500 int i;
502 if (!bmap)
503 return NULL;
504 if (bmap->n_div == 0)
505 return bmap;
507 for (i = 0; i < bmap->n_div; ++i)
508 normalize_div_expression(bmap, i);
510 return bmap;
513 /* Assumes divs have been ordered if keep_divs is set.
515 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
516 unsigned pos, isl_int *eq, int keep_divs, int *progress)
518 unsigned total;
519 unsigned space_total;
520 int k;
521 int last_div;
523 total = isl_basic_map_total_dim(bmap);
524 space_total = isl_space_dim(bmap->dim, isl_dim_all);
525 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
526 for (k = 0; k < bmap->n_eq; ++k) {
527 if (bmap->eq[k] == eq)
528 continue;
529 if (isl_int_is_zero(bmap->eq[k][1+pos]))
530 continue;
531 if (progress)
532 *progress = 1;
533 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
534 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
537 for (k = 0; k < bmap->n_ineq; ++k) {
538 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
539 continue;
540 if (progress)
541 *progress = 1;
542 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
543 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
544 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
547 for (k = 0; k < bmap->n_div; ++k) {
548 if (isl_int_is_zero(bmap->div[k][0]))
549 continue;
550 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
551 continue;
552 if (progress)
553 *progress = 1;
554 /* We need to be careful about circular definitions,
555 * so for now we just remove the definition of div k
556 * if the equality contains any divs.
557 * If keep_divs is set, then the divs have been ordered
558 * and we can keep the definition as long as the result
559 * is still ordered.
561 if (last_div == -1 || (keep_divs && last_div < k)) {
562 isl_seq_elim(bmap->div[k]+1, eq,
563 1+pos, 1+total, &bmap->div[k][0]);
564 normalize_div_expression(bmap, k);
565 } else
566 isl_seq_clr(bmap->div[k], 1 + total);
567 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
571 /* Assumes divs have been ordered if keep_divs is set.
573 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
574 isl_int *eq, unsigned div, int keep_divs)
576 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
578 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
580 bmap = isl_basic_map_drop_div(bmap, div);
582 return bmap;
585 /* Check if elimination of div "div" using equality "eq" would not
586 * result in a div depending on a later div.
588 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
589 unsigned div)
591 int k;
592 int last_div;
593 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
594 unsigned pos = space_total + div;
596 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
597 if (last_div < 0 || last_div <= div)
598 return 1;
600 for (k = 0; k <= last_div; ++k) {
601 if (isl_int_is_zero(bmap->div[k][0]))
602 return 1;
603 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
604 return 0;
607 return 1;
610 /* Elimininate divs based on equalities
612 static struct isl_basic_map *eliminate_divs_eq(
613 struct isl_basic_map *bmap, int *progress)
615 int d;
616 int i;
617 int modified = 0;
618 unsigned off;
620 bmap = isl_basic_map_order_divs(bmap);
622 if (!bmap)
623 return NULL;
625 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
627 for (d = bmap->n_div - 1; d >= 0 ; --d) {
628 for (i = 0; i < bmap->n_eq; ++i) {
629 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
630 !isl_int_is_negone(bmap->eq[i][off + d]))
631 continue;
632 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
633 continue;
634 modified = 1;
635 *progress = 1;
636 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
637 if (isl_basic_map_drop_equality(bmap, i) < 0)
638 return isl_basic_map_free(bmap);
639 break;
642 if (modified)
643 return eliminate_divs_eq(bmap, progress);
644 return bmap;
647 /* Elimininate divs based on inequalities
649 static struct isl_basic_map *eliminate_divs_ineq(
650 struct isl_basic_map *bmap, int *progress)
652 int d;
653 int i;
654 unsigned off;
655 struct isl_ctx *ctx;
657 if (!bmap)
658 return NULL;
660 ctx = bmap->ctx;
661 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
663 for (d = bmap->n_div - 1; d >= 0 ; --d) {
664 for (i = 0; i < bmap->n_eq; ++i)
665 if (!isl_int_is_zero(bmap->eq[i][off + d]))
666 break;
667 if (i < bmap->n_eq)
668 continue;
669 for (i = 0; i < bmap->n_ineq; ++i)
670 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
671 break;
672 if (i < bmap->n_ineq)
673 continue;
674 *progress = 1;
675 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
676 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
677 break;
678 bmap = isl_basic_map_drop_div(bmap, d);
679 if (!bmap)
680 break;
682 return bmap;
685 struct isl_basic_map *isl_basic_map_gauss(
686 struct isl_basic_map *bmap, int *progress)
688 int k;
689 int done;
690 int last_var;
691 unsigned total_var;
692 unsigned total;
694 bmap = isl_basic_map_order_divs(bmap);
696 if (!bmap)
697 return NULL;
699 total = isl_basic_map_total_dim(bmap);
700 total_var = total - bmap->n_div;
702 last_var = total - 1;
703 for (done = 0; done < bmap->n_eq; ++done) {
704 for (; last_var >= 0; --last_var) {
705 for (k = done; k < bmap->n_eq; ++k)
706 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
707 break;
708 if (k < bmap->n_eq)
709 break;
711 if (last_var < 0)
712 break;
713 if (k != done)
714 swap_equality(bmap, k, done);
715 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
716 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
718 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
719 progress);
721 if (last_var >= total_var &&
722 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
723 unsigned div = last_var - total_var;
724 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
725 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
726 isl_int_set(bmap->div[div][0],
727 bmap->eq[done][1+last_var]);
728 if (progress)
729 *progress = 1;
730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
733 if (done == bmap->n_eq)
734 return bmap;
735 for (k = done; k < bmap->n_eq; ++k) {
736 if (isl_int_is_zero(bmap->eq[k][0]))
737 continue;
738 return isl_basic_map_set_to_empty(bmap);
740 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
741 return bmap;
744 struct isl_basic_set *isl_basic_set_gauss(
745 struct isl_basic_set *bset, int *progress)
747 return (struct isl_basic_set*)isl_basic_map_gauss(
748 (struct isl_basic_map *)bset, progress);
752 static unsigned int round_up(unsigned int v)
754 int old_v = v;
756 while (v) {
757 old_v = v;
758 v ^= v & -v;
760 return old_v << 1;
763 /* Hash table of inequalities in a basic map.
764 * "index" is an array of addresses of inequalities in the basic map, some
765 * of which are NULL. The inequalities are hashed on the coefficients
766 * except the constant term.
767 * "size" is the number of elements in the array and is always a power of two
768 * "bits" is the number of bits need to represent an index into the array.
769 * "total" is the total dimension of the basic map.
771 struct isl_constraint_index {
772 unsigned int size;
773 int bits;
774 isl_int ***index;
775 unsigned total;
778 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
780 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
781 __isl_keep isl_basic_map *bmap)
783 isl_ctx *ctx;
785 ci->index = NULL;
786 if (!bmap)
787 return isl_stat_error;
788 ci->total = isl_basic_set_total_dim(bmap);
789 if (bmap->n_ineq == 0)
790 return isl_stat_ok;
791 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
792 ci->bits = ffs(ci->size) - 1;
793 ctx = isl_basic_map_get_ctx(bmap);
794 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
795 if (!ci->index)
796 return isl_stat_error;
798 return isl_stat_ok;
801 /* Free the memory allocated by create_constraint_index.
803 static void constraint_index_free(struct isl_constraint_index *ci)
805 free(ci->index);
808 /* Return the position in ci->index that contains the address of
809 * an inequality that is equal to *ineq up to the constant term,
810 * provided this address is not identical to "ineq".
811 * If there is no such inequality, then return the position where
812 * such an inequality should be inserted.
814 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
816 int h;
817 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
818 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
819 if (ineq != ci->index[h] &&
820 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
821 break;
822 return h;
825 /* Return the position in ci->index that contains the address of
826 * an inequality that is equal to the k'th inequality of "bmap"
827 * up to the constant term, provided it does not point to the very
828 * same inequality.
829 * If there is no such inequality, then return the position where
830 * such an inequality should be inserted.
832 static int hash_index(struct isl_constraint_index *ci,
833 __isl_keep isl_basic_map *bmap, int k)
835 return hash_index_ineq(ci, &bmap->ineq[k]);
838 static int set_hash_index(struct isl_constraint_index *ci,
839 struct isl_basic_set *bset, int k)
841 return hash_index(ci, bset, k);
844 /* Fill in the "ci" data structure with the inequalities of "bset".
846 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
847 __isl_keep isl_basic_set *bset)
849 int k, h;
851 if (create_constraint_index(ci, bset) < 0)
852 return isl_stat_error;
854 for (k = 0; k < bset->n_ineq; ++k) {
855 h = set_hash_index(ci, bset, k);
856 ci->index[h] = &bset->ineq[k];
859 return isl_stat_ok;
862 /* Is the inequality ineq (obviously) redundant with respect
863 * to the constraints in "ci"?
865 * Look for an inequality in "ci" with the same coefficients and then
866 * check if the contant term of "ineq" is greater than or equal
867 * to the constant term of that inequality. If so, "ineq" is clearly
868 * redundant.
870 * Note that hash_index_ineq ignores a stored constraint if it has
871 * the same address as the passed inequality. It is ok to pass
872 * the address of a local variable here since it will never be
873 * the same as the address of a constraint in "ci".
875 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
876 isl_int *ineq)
878 int h;
880 h = hash_index_ineq(ci, &ineq);
881 if (!ci->index[h])
882 return isl_bool_false;
883 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
886 /* If we can eliminate more than one div, then we need to make
887 * sure we do it from last div to first div, in order not to
888 * change the position of the other divs that still need to
889 * be removed.
891 static struct isl_basic_map *remove_duplicate_divs(
892 struct isl_basic_map *bmap, int *progress)
894 unsigned int size;
895 int *index;
896 int *elim_for;
897 int k, l, h;
898 int bits;
899 struct isl_blk eq;
900 unsigned total_var;
901 unsigned total;
902 struct isl_ctx *ctx;
904 bmap = isl_basic_map_order_divs(bmap);
905 if (!bmap || bmap->n_div <= 1)
906 return bmap;
908 total_var = isl_space_dim(bmap->dim, isl_dim_all);
909 total = total_var + bmap->n_div;
911 ctx = bmap->ctx;
912 for (k = bmap->n_div - 1; k >= 0; --k)
913 if (!isl_int_is_zero(bmap->div[k][0]))
914 break;
915 if (k <= 0)
916 return bmap;
918 size = round_up(4 * bmap->n_div / 3 - 1);
919 if (size == 0)
920 return bmap;
921 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
922 bits = ffs(size) - 1;
923 index = isl_calloc_array(ctx, int, size);
924 if (!elim_for || !index)
925 goto out;
926 eq = isl_blk_alloc(ctx, 1+total);
927 if (isl_blk_is_error(eq))
928 goto out;
930 isl_seq_clr(eq.data, 1+total);
931 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
932 for (--k; k >= 0; --k) {
933 uint32_t hash;
935 if (isl_int_is_zero(bmap->div[k][0]))
936 continue;
938 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
939 for (h = hash; index[h]; h = (h+1) % size)
940 if (isl_seq_eq(bmap->div[k],
941 bmap->div[index[h]-1], 2+total))
942 break;
943 if (index[h]) {
944 *progress = 1;
945 l = index[h] - 1;
946 elim_for[l] = k + 1;
948 index[h] = k+1;
950 for (l = bmap->n_div - 1; l >= 0; --l) {
951 if (!elim_for[l])
952 continue;
953 k = elim_for[l] - 1;
954 isl_int_set_si(eq.data[1+total_var+k], -1);
955 isl_int_set_si(eq.data[1+total_var+l], 1);
956 bmap = eliminate_div(bmap, eq.data, l, 1);
957 if (!bmap)
958 break;
959 isl_int_set_si(eq.data[1+total_var+k], 0);
960 isl_int_set_si(eq.data[1+total_var+l], 0);
963 isl_blk_free(ctx, eq);
964 out:
965 free(index);
966 free(elim_for);
967 return bmap;
970 static int n_pure_div_eq(struct isl_basic_map *bmap)
972 int i, j;
973 unsigned total;
975 total = isl_space_dim(bmap->dim, isl_dim_all);
976 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
977 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
978 --j;
979 if (j < 0)
980 break;
981 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
982 return 0;
984 return i;
987 /* Normalize divs that appear in equalities.
989 * In particular, we assume that bmap contains some equalities
990 * of the form
992 * a x = m * e_i
994 * and we want to replace the set of e_i by a minimal set and
995 * such that the new e_i have a canonical representation in terms
996 * of the vector x.
997 * If any of the equalities involves more than one divs, then
998 * we currently simply bail out.
1000 * Let us first additionally assume that all equalities involve
1001 * a div. The equalities then express modulo constraints on the
1002 * remaining variables and we can use "parameter compression"
1003 * to find a minimal set of constraints. The result is a transformation
1005 * x = T(x') = x_0 + G x'
1007 * with G a lower-triangular matrix with all elements below the diagonal
1008 * non-negative and smaller than the diagonal element on the same row.
1009 * We first normalize x_0 by making the same property hold in the affine
1010 * T matrix.
1011 * The rows i of G with a 1 on the diagonal do not impose any modulo
1012 * constraint and simply express x_i = x'_i.
1013 * For each of the remaining rows i, we introduce a div and a corresponding
1014 * equality. In particular
1016 * g_ii e_j = x_i - g_i(x')
1018 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1019 * corresponding div (if g_kk != 1).
1021 * If there are any equalities not involving any div, then we
1022 * first apply a variable compression on the variables x:
1024 * x = C x'' x'' = C_2 x
1026 * and perform the above parameter compression on A C instead of on A.
1027 * The resulting compression is then of the form
1029 * x'' = T(x') = x_0 + G x'
1031 * and in constructing the new divs and the corresponding equalities,
1032 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1033 * by the corresponding row from C_2.
1035 static struct isl_basic_map *normalize_divs(
1036 struct isl_basic_map *bmap, int *progress)
1038 int i, j, k;
1039 int total;
1040 int div_eq;
1041 struct isl_mat *B;
1042 struct isl_vec *d;
1043 struct isl_mat *T = NULL;
1044 struct isl_mat *C = NULL;
1045 struct isl_mat *C2 = NULL;
1046 isl_int v;
1047 int *pos;
1048 int dropped, needed;
1050 if (!bmap)
1051 return NULL;
1053 if (bmap->n_div == 0)
1054 return bmap;
1056 if (bmap->n_eq == 0)
1057 return bmap;
1059 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1060 return bmap;
1062 total = isl_space_dim(bmap->dim, isl_dim_all);
1063 div_eq = n_pure_div_eq(bmap);
1064 if (div_eq == 0)
1065 return bmap;
1067 if (div_eq < bmap->n_eq) {
1068 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1069 bmap->n_eq - div_eq, 0, 1 + total);
1070 C = isl_mat_variable_compression(B, &C2);
1071 if (!C || !C2)
1072 goto error;
1073 if (C->n_col == 0) {
1074 bmap = isl_basic_map_set_to_empty(bmap);
1075 isl_mat_free(C);
1076 isl_mat_free(C2);
1077 goto done;
1081 d = isl_vec_alloc(bmap->ctx, div_eq);
1082 if (!d)
1083 goto error;
1084 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1085 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1086 --j;
1087 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1089 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1091 if (C) {
1092 B = isl_mat_product(B, C);
1093 C = NULL;
1096 T = isl_mat_parameter_compression(B, d);
1097 if (!T)
1098 goto error;
1099 if (T->n_col == 0) {
1100 bmap = isl_basic_map_set_to_empty(bmap);
1101 isl_mat_free(C2);
1102 isl_mat_free(T);
1103 goto done;
1105 isl_int_init(v);
1106 for (i = 0; i < T->n_row - 1; ++i) {
1107 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1108 if (isl_int_is_zero(v))
1109 continue;
1110 isl_mat_col_submul(T, 0, v, 1 + i);
1112 isl_int_clear(v);
1113 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1114 if (!pos)
1115 goto error;
1116 /* We have to be careful because dropping equalities may reorder them */
1117 dropped = 0;
1118 for (j = bmap->n_div - 1; j >= 0; --j) {
1119 for (i = 0; i < bmap->n_eq; ++i)
1120 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1121 break;
1122 if (i < bmap->n_eq) {
1123 bmap = isl_basic_map_drop_div(bmap, j);
1124 isl_basic_map_drop_equality(bmap, i);
1125 ++dropped;
1128 pos[0] = 0;
1129 needed = 0;
1130 for (i = 1; i < T->n_row; ++i) {
1131 if (isl_int_is_one(T->row[i][i]))
1132 pos[i] = i;
1133 else
1134 needed++;
1136 if (needed > dropped) {
1137 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1138 needed, needed, 0);
1139 if (!bmap)
1140 goto error;
1142 for (i = 1; i < T->n_row; ++i) {
1143 if (isl_int_is_one(T->row[i][i]))
1144 continue;
1145 k = isl_basic_map_alloc_div(bmap);
1146 pos[i] = 1 + total + k;
1147 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1148 isl_int_set(bmap->div[k][0], T->row[i][i]);
1149 if (C2)
1150 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1151 else
1152 isl_int_set_si(bmap->div[k][1 + i], 1);
1153 for (j = 0; j < i; ++j) {
1154 if (isl_int_is_zero(T->row[i][j]))
1155 continue;
1156 if (pos[j] < T->n_row && C2)
1157 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1158 C2->row[pos[j]], 1 + total);
1159 else
1160 isl_int_neg(bmap->div[k][1 + pos[j]],
1161 T->row[i][j]);
1163 j = isl_basic_map_alloc_equality(bmap);
1164 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1165 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1167 free(pos);
1168 isl_mat_free(C2);
1169 isl_mat_free(T);
1171 if (progress)
1172 *progress = 1;
1173 done:
1174 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1176 return bmap;
1177 error:
1178 isl_mat_free(C);
1179 isl_mat_free(C2);
1180 isl_mat_free(T);
1181 return bmap;
1184 static struct isl_basic_map *set_div_from_lower_bound(
1185 struct isl_basic_map *bmap, int div, int ineq)
1187 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1189 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1190 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1191 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1192 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1193 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1195 return bmap;
1198 /* Check whether it is ok to define a div based on an inequality.
1199 * To avoid the introduction of circular definitions of divs, we
1200 * do not allow such a definition if the resulting expression would refer to
1201 * any other undefined divs or if any known div is defined in
1202 * terms of the unknown div.
1204 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1205 int div, int ineq)
1207 int j;
1208 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1210 /* Not defined in terms of unknown divs */
1211 for (j = 0; j < bmap->n_div; ++j) {
1212 if (div == j)
1213 continue;
1214 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1215 continue;
1216 if (isl_int_is_zero(bmap->div[j][0]))
1217 return 0;
1220 /* No other div defined in terms of this one => avoid loops */
1221 for (j = 0; j < bmap->n_div; ++j) {
1222 if (div == j)
1223 continue;
1224 if (isl_int_is_zero(bmap->div[j][0]))
1225 continue;
1226 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1227 return 0;
1230 return 1;
1233 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1234 * be a better expression than the current one?
1236 * If we do not have any expression yet, then any expression would be better.
1237 * Otherwise we check if the last variable involved in the inequality
1238 * (disregarding the div that it would define) is in an earlier position
1239 * than the last variable involved in the current div expression.
1241 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1242 int div, int ineq)
1244 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1245 int last_div;
1246 int last_ineq;
1248 if (isl_int_is_zero(bmap->div[div][0]))
1249 return 1;
1251 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1252 bmap->n_div - (div + 1)) >= 0)
1253 return 0;
1255 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1256 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1257 total + bmap->n_div);
1259 return last_ineq < last_div;
1262 /* Given two constraints "k" and "l" that are opposite to each other,
1263 * except for the constant term, check if we can use them
1264 * to obtain an expression for one of the hitherto unknown divs or
1265 * a "better" expression for a div for which we already have an expression.
1266 * "sum" is the sum of the constant terms of the constraints.
1267 * If this sum is strictly smaller than the coefficient of one
1268 * of the divs, then this pair can be used define the div.
1269 * To avoid the introduction of circular definitions of divs, we
1270 * do not use the pair if the resulting expression would refer to
1271 * any other undefined divs or if any known div is defined in
1272 * terms of the unknown div.
1274 static struct isl_basic_map *check_for_div_constraints(
1275 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1277 int i;
1278 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1280 for (i = 0; i < bmap->n_div; ++i) {
1281 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1282 continue;
1283 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1284 continue;
1285 if (!better_div_constraint(bmap, i, k))
1286 continue;
1287 if (!ok_to_set_div_from_bound(bmap, i, k))
1288 break;
1289 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1290 bmap = set_div_from_lower_bound(bmap, i, k);
1291 else
1292 bmap = set_div_from_lower_bound(bmap, i, l);
1293 if (progress)
1294 *progress = 1;
1295 break;
1297 return bmap;
1300 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1301 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1303 struct isl_constraint_index ci;
1304 int k, l, h;
1305 unsigned total = isl_basic_map_total_dim(bmap);
1306 isl_int sum;
1308 if (!bmap || bmap->n_ineq <= 1)
1309 return bmap;
1311 if (create_constraint_index(&ci, bmap) < 0)
1312 return bmap;
1314 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1315 ci.index[h] = &bmap->ineq[0];
1316 for (k = 1; k < bmap->n_ineq; ++k) {
1317 h = hash_index(&ci, bmap, k);
1318 if (!ci.index[h]) {
1319 ci.index[h] = &bmap->ineq[k];
1320 continue;
1322 if (progress)
1323 *progress = 1;
1324 l = ci.index[h] - &bmap->ineq[0];
1325 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1326 swap_inequality(bmap, k, l);
1327 isl_basic_map_drop_inequality(bmap, k);
1328 --k;
1330 isl_int_init(sum);
1331 for (k = 0; k < bmap->n_ineq-1; ++k) {
1332 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1333 h = hash_index(&ci, bmap, k);
1334 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1335 if (!ci.index[h])
1336 continue;
1337 l = ci.index[h] - &bmap->ineq[0];
1338 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1339 if (isl_int_is_pos(sum)) {
1340 if (detect_divs)
1341 bmap = check_for_div_constraints(bmap, k, l,
1342 sum, progress);
1343 continue;
1345 if (isl_int_is_zero(sum)) {
1346 /* We need to break out of the loop after these
1347 * changes since the contents of the hash
1348 * will no longer be valid.
1349 * Plus, we probably we want to regauss first.
1351 if (progress)
1352 *progress = 1;
1353 isl_basic_map_drop_inequality(bmap, l);
1354 isl_basic_map_inequality_to_equality(bmap, k);
1355 } else
1356 bmap = isl_basic_map_set_to_empty(bmap);
1357 break;
1359 isl_int_clear(sum);
1361 constraint_index_free(&ci);
1362 return bmap;
1365 /* Detect all pairs of inequalities that form an equality.
1367 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1368 * Call it repeatedly while it is making progress.
1370 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1371 __isl_take isl_basic_map *bmap, int *progress)
1373 int duplicate;
1375 do {
1376 duplicate = 0;
1377 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1378 &duplicate, 0);
1379 if (progress && duplicate)
1380 *progress = 1;
1381 } while (duplicate);
1383 return bmap;
1386 /* Eliminate knowns divs from constraints where they appear with
1387 * a (positive or negative) unit coefficient.
1389 * That is, replace
1391 * floor(e/m) + f >= 0
1393 * by
1395 * e + m f >= 0
1397 * and
1399 * -floor(e/m) + f >= 0
1401 * by
1403 * -e + m f + m - 1 >= 0
1405 * The first conversion is valid because floor(e/m) >= -f is equivalent
1406 * to e/m >= -f because -f is an integral expression.
1407 * The second conversion follows from the fact that
1409 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1412 * Note that one of the div constraints may have been eliminated
1413 * due to being redundant with respect to the constraint that is
1414 * being modified by this function. The modified constraint may
1415 * no longer imply this div constraint, so we add it back to make
1416 * sure we do not lose any information.
1418 * We skip integral divs, i.e., those with denominator 1, as we would
1419 * risk eliminating the div from the div constraints. We do not need
1420 * to handle those divs here anyway since the div constraints will turn
1421 * out to form an equality and this equality can then be use to eliminate
1422 * the div from all constraints.
1424 static __isl_give isl_basic_map *eliminate_unit_divs(
1425 __isl_take isl_basic_map *bmap, int *progress)
1427 int i, j;
1428 isl_ctx *ctx;
1429 unsigned total;
1431 if (!bmap)
1432 return NULL;
1434 ctx = isl_basic_map_get_ctx(bmap);
1435 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1437 for (i = 0; i < bmap->n_div; ++i) {
1438 if (isl_int_is_zero(bmap->div[i][0]))
1439 continue;
1440 if (isl_int_is_one(bmap->div[i][0]))
1441 continue;
1442 for (j = 0; j < bmap->n_ineq; ++j) {
1443 int s;
1445 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1446 !isl_int_is_negone(bmap->ineq[j][total + i]))
1447 continue;
1449 *progress = 1;
1451 s = isl_int_sgn(bmap->ineq[j][total + i]);
1452 isl_int_set_si(bmap->ineq[j][total + i], 0);
1453 if (s < 0)
1454 isl_seq_combine(bmap->ineq[j],
1455 ctx->negone, bmap->div[i] + 1,
1456 bmap->div[i][0], bmap->ineq[j],
1457 total + bmap->n_div);
1458 else
1459 isl_seq_combine(bmap->ineq[j],
1460 ctx->one, bmap->div[i] + 1,
1461 bmap->div[i][0], bmap->ineq[j],
1462 total + bmap->n_div);
1463 if (s < 0) {
1464 isl_int_add(bmap->ineq[j][0],
1465 bmap->ineq[j][0], bmap->div[i][0]);
1466 isl_int_sub_ui(bmap->ineq[j][0],
1467 bmap->ineq[j][0], 1);
1470 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1471 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1472 return isl_basic_map_free(bmap);
1476 return bmap;
1479 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1481 int progress = 1;
1482 if (!bmap)
1483 return NULL;
1484 while (progress) {
1485 progress = 0;
1486 if (!bmap)
1487 break;
1488 if (isl_basic_map_plain_is_empty(bmap))
1489 break;
1490 bmap = isl_basic_map_normalize_constraints(bmap);
1491 bmap = remove_independent_vars_from_divs(bmap);
1492 bmap = normalize_div_expressions(bmap);
1493 bmap = remove_duplicate_divs(bmap, &progress);
1494 bmap = eliminate_unit_divs(bmap, &progress);
1495 bmap = eliminate_divs_eq(bmap, &progress);
1496 bmap = eliminate_divs_ineq(bmap, &progress);
1497 bmap = isl_basic_map_gauss(bmap, &progress);
1498 /* requires equalities in normal form */
1499 bmap = normalize_divs(bmap, &progress);
1500 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1501 &progress, 1);
1502 if (bmap && progress)
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1505 return bmap;
1508 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1510 return (struct isl_basic_set *)
1511 isl_basic_map_simplify((struct isl_basic_map *)bset);
1515 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1516 isl_int *constraint, unsigned div)
1518 unsigned pos;
1520 if (!bmap)
1521 return -1;
1523 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1525 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1526 int neg;
1527 isl_int_sub(bmap->div[div][1],
1528 bmap->div[div][1], bmap->div[div][0]);
1529 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1530 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1531 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1532 isl_int_add(bmap->div[div][1],
1533 bmap->div[div][1], bmap->div[div][0]);
1534 if (!neg)
1535 return 0;
1536 if (isl_seq_first_non_zero(constraint+pos+1,
1537 bmap->n_div-div-1) != -1)
1538 return 0;
1539 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1540 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1541 return 0;
1542 if (isl_seq_first_non_zero(constraint+pos+1,
1543 bmap->n_div-div-1) != -1)
1544 return 0;
1545 } else
1546 return 0;
1548 return 1;
1551 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1552 isl_int *constraint, unsigned div)
1554 return isl_basic_map_is_div_constraint(bset, constraint, div);
1558 /* If the only constraints a div d=floor(f/m)
1559 * appears in are its two defining constraints
1561 * f - m d >=0
1562 * -(f - (m - 1)) + m d >= 0
1564 * then it can safely be removed.
1566 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1568 int i;
1569 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1571 for (i = 0; i < bmap->n_eq; ++i)
1572 if (!isl_int_is_zero(bmap->eq[i][pos]))
1573 return 0;
1575 for (i = 0; i < bmap->n_ineq; ++i) {
1576 if (isl_int_is_zero(bmap->ineq[i][pos]))
1577 continue;
1578 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1579 return 0;
1582 for (i = 0; i < bmap->n_div; ++i) {
1583 if (isl_int_is_zero(bmap->div[i][0]))
1584 continue;
1585 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1586 return 0;
1589 return 1;
1593 * Remove divs that don't occur in any of the constraints or other divs.
1594 * These can arise when dropping constraints from a basic map or
1595 * when the divs of a basic map have been temporarily aligned
1596 * with the divs of another basic map.
1598 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1600 int i;
1602 if (!bmap)
1603 return NULL;
1605 for (i = bmap->n_div-1; i >= 0; --i) {
1606 if (!div_is_redundant(bmap, i))
1607 continue;
1608 bmap = isl_basic_map_drop_div(bmap, i);
1610 return bmap;
1613 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1615 bmap = remove_redundant_divs(bmap);
1616 if (!bmap)
1617 return NULL;
1618 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1619 return bmap;
1622 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1624 return (struct isl_basic_set *)
1625 isl_basic_map_finalize((struct isl_basic_map *)bset);
1628 struct isl_set *isl_set_finalize(struct isl_set *set)
1630 int i;
1632 if (!set)
1633 return NULL;
1634 for (i = 0; i < set->n; ++i) {
1635 set->p[i] = isl_basic_set_finalize(set->p[i]);
1636 if (!set->p[i])
1637 goto error;
1639 return set;
1640 error:
1641 isl_set_free(set);
1642 return NULL;
1645 struct isl_map *isl_map_finalize(struct isl_map *map)
1647 int i;
1649 if (!map)
1650 return NULL;
1651 for (i = 0; i < map->n; ++i) {
1652 map->p[i] = isl_basic_map_finalize(map->p[i]);
1653 if (!map->p[i])
1654 goto error;
1656 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1657 return map;
1658 error:
1659 isl_map_free(map);
1660 return NULL;
1664 /* Remove definition of any div that is defined in terms of the given variable.
1665 * The div itself is not removed. Functions such as
1666 * eliminate_divs_ineq depend on the other divs remaining in place.
1668 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1669 int pos)
1671 int i;
1673 if (!bmap)
1674 return NULL;
1676 for (i = 0; i < bmap->n_div; ++i) {
1677 if (isl_int_is_zero(bmap->div[i][0]))
1678 continue;
1679 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1680 continue;
1681 isl_int_set_si(bmap->div[i][0], 0);
1683 return bmap;
1686 /* Eliminate the specified variables from the constraints using
1687 * Fourier-Motzkin. The variables themselves are not removed.
1689 struct isl_basic_map *isl_basic_map_eliminate_vars(
1690 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1692 int d;
1693 int i, j, k;
1694 unsigned total;
1695 int need_gauss = 0;
1697 if (n == 0)
1698 return bmap;
1699 if (!bmap)
1700 return NULL;
1701 total = isl_basic_map_total_dim(bmap);
1703 bmap = isl_basic_map_cow(bmap);
1704 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1705 bmap = remove_dependent_vars(bmap, d);
1706 if (!bmap)
1707 return NULL;
1709 for (d = pos + n - 1;
1710 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1711 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1712 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1713 int n_lower, n_upper;
1714 if (!bmap)
1715 return NULL;
1716 for (i = 0; i < bmap->n_eq; ++i) {
1717 if (isl_int_is_zero(bmap->eq[i][1+d]))
1718 continue;
1719 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1720 isl_basic_map_drop_equality(bmap, i);
1721 need_gauss = 1;
1722 break;
1724 if (i < bmap->n_eq)
1725 continue;
1726 n_lower = 0;
1727 n_upper = 0;
1728 for (i = 0; i < bmap->n_ineq; ++i) {
1729 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1730 n_lower++;
1731 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1732 n_upper++;
1734 bmap = isl_basic_map_extend_constraints(bmap,
1735 0, n_lower * n_upper);
1736 if (!bmap)
1737 goto error;
1738 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1739 int last;
1740 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1741 continue;
1742 last = -1;
1743 for (j = 0; j < i; ++j) {
1744 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1745 continue;
1746 last = j;
1747 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1748 isl_int_sgn(bmap->ineq[j][1+d]))
1749 continue;
1750 k = isl_basic_map_alloc_inequality(bmap);
1751 if (k < 0)
1752 goto error;
1753 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1754 1+total);
1755 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1756 1+d, 1+total, NULL);
1758 isl_basic_map_drop_inequality(bmap, i);
1759 i = last + 1;
1761 if (n_lower > 0 && n_upper > 0) {
1762 bmap = isl_basic_map_normalize_constraints(bmap);
1763 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1764 NULL, 0);
1765 bmap = isl_basic_map_gauss(bmap, NULL);
1766 bmap = isl_basic_map_remove_redundancies(bmap);
1767 need_gauss = 0;
1768 if (!bmap)
1769 goto error;
1770 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1771 break;
1774 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1775 if (need_gauss)
1776 bmap = isl_basic_map_gauss(bmap, NULL);
1777 return bmap;
1778 error:
1779 isl_basic_map_free(bmap);
1780 return NULL;
1783 struct isl_basic_set *isl_basic_set_eliminate_vars(
1784 struct isl_basic_set *bset, unsigned pos, unsigned n)
1786 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1787 (struct isl_basic_map *)bset, pos, n);
1790 /* Eliminate the specified n dimensions starting at first from the
1791 * constraints, without removing the dimensions from the space.
1792 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1793 * Otherwise, they are projected out and the original space is restored.
1795 __isl_give isl_basic_map *isl_basic_map_eliminate(
1796 __isl_take isl_basic_map *bmap,
1797 enum isl_dim_type type, unsigned first, unsigned n)
1799 isl_space *space;
1801 if (!bmap)
1802 return NULL;
1803 if (n == 0)
1804 return bmap;
1806 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1807 isl_die(bmap->ctx, isl_error_invalid,
1808 "index out of bounds", goto error);
1810 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1811 first += isl_basic_map_offset(bmap, type) - 1;
1812 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1813 return isl_basic_map_finalize(bmap);
1816 space = isl_basic_map_get_space(bmap);
1817 bmap = isl_basic_map_project_out(bmap, type, first, n);
1818 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1819 bmap = isl_basic_map_reset_space(bmap, space);
1820 return bmap;
1821 error:
1822 isl_basic_map_free(bmap);
1823 return NULL;
1826 __isl_give isl_basic_set *isl_basic_set_eliminate(
1827 __isl_take isl_basic_set *bset,
1828 enum isl_dim_type type, unsigned first, unsigned n)
1830 return isl_basic_map_eliminate(bset, type, first, n);
1833 /* Don't assume equalities are in order, because align_divs
1834 * may have changed the order of the divs.
1836 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1838 int d, i;
1839 unsigned total;
1841 total = isl_space_dim(bmap->dim, isl_dim_all);
1842 for (d = 0; d < total; ++d)
1843 elim[d] = -1;
1844 for (i = 0; i < bmap->n_eq; ++i) {
1845 for (d = total - 1; d >= 0; --d) {
1846 if (isl_int_is_zero(bmap->eq[i][1+d]))
1847 continue;
1848 elim[d] = i;
1849 break;
1854 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1856 compute_elimination_index((struct isl_basic_map *)bset, elim);
1859 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1860 struct isl_basic_map *bmap, int *elim)
1862 int d;
1863 int copied = 0;
1864 unsigned total;
1866 total = isl_space_dim(bmap->dim, isl_dim_all);
1867 for (d = total - 1; d >= 0; --d) {
1868 if (isl_int_is_zero(src[1+d]))
1869 continue;
1870 if (elim[d] == -1)
1871 continue;
1872 if (!copied) {
1873 isl_seq_cpy(dst, src, 1 + total);
1874 copied = 1;
1876 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1878 return copied;
1881 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1882 struct isl_basic_set *bset, int *elim)
1884 return reduced_using_equalities(dst, src,
1885 (struct isl_basic_map *)bset, elim);
1888 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1889 struct isl_basic_set *bset, struct isl_basic_set *context)
1891 int i;
1892 int *elim;
1894 if (!bset || !context)
1895 goto error;
1897 if (context->n_eq == 0) {
1898 isl_basic_set_free(context);
1899 return bset;
1902 bset = isl_basic_set_cow(bset);
1903 if (!bset)
1904 goto error;
1906 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1907 if (!elim)
1908 goto error;
1909 set_compute_elimination_index(context, elim);
1910 for (i = 0; i < bset->n_eq; ++i)
1911 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1912 context, elim);
1913 for (i = 0; i < bset->n_ineq; ++i)
1914 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1915 context, elim);
1916 isl_basic_set_free(context);
1917 free(elim);
1918 bset = isl_basic_set_simplify(bset);
1919 bset = isl_basic_set_finalize(bset);
1920 return bset;
1921 error:
1922 isl_basic_set_free(bset);
1923 isl_basic_set_free(context);
1924 return NULL;
1927 /* For each inequality in "ineq" that is a shifted (more relaxed)
1928 * copy of an inequality in "context", mark the corresponding entry
1929 * in "row" with -1.
1931 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1932 __isl_keep isl_basic_set *context, int *row)
1934 struct isl_constraint_index ci;
1935 int n_ineq;
1936 unsigned total;
1937 int k;
1939 if (!ineq || !context)
1940 return isl_stat_error;
1941 if (context->n_ineq == 0)
1942 return isl_stat_ok;
1943 if (setup_constraint_index(&ci, context) < 0)
1944 return isl_stat_error;
1946 n_ineq = isl_mat_rows(ineq);
1947 total = isl_mat_cols(ineq) - 1;
1948 for (k = 0; k < n_ineq; ++k) {
1949 isl_bool redundant;
1951 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1952 if (redundant < 0)
1953 goto error;
1954 if (!redundant)
1955 continue;
1956 row[k] = -1;
1958 constraint_index_free(&ci);
1959 return isl_stat_ok;
1960 error:
1961 constraint_index_free(&ci);
1962 return isl_stat_error;
1965 static struct isl_basic_set *remove_shifted_constraints(
1966 struct isl_basic_set *bset, struct isl_basic_set *context)
1968 struct isl_constraint_index ci;
1969 int k;
1971 if (!bset || !context)
1972 return bset;
1974 if (context->n_ineq == 0)
1975 return bset;
1976 if (setup_constraint_index(&ci, context) < 0)
1977 return bset;
1979 for (k = 0; k < bset->n_ineq; ++k) {
1980 isl_bool redundant;
1982 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1983 if (redundant < 0)
1984 goto error;
1985 if (!redundant)
1986 continue;
1987 bset = isl_basic_set_cow(bset);
1988 if (!bset)
1989 goto error;
1990 isl_basic_set_drop_inequality(bset, k);
1991 --k;
1993 constraint_index_free(&ci);
1994 return bset;
1995 error:
1996 constraint_index_free(&ci);
1997 return bset;
2000 /* Remove constraints from "bmap" that are identical to constraints
2001 * in "context" or that are more relaxed (greater constant term).
2003 * We perform the test for shifted copies on the pure constraints
2004 * in remove_shifted_constraints.
2006 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2007 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2009 isl_basic_set *bset, *bset_context;
2011 if (!bmap || !context)
2012 goto error;
2014 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2015 isl_basic_map_free(context);
2016 return bmap;
2019 context = isl_basic_map_align_divs(context, bmap);
2020 bmap = isl_basic_map_align_divs(bmap, context);
2022 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2023 bset_context = isl_basic_map_underlying_set(context);
2024 bset = remove_shifted_constraints(bset, bset_context);
2025 isl_basic_set_free(bset_context);
2027 bmap = isl_basic_map_overlying_set(bset, bmap);
2029 return bmap;
2030 error:
2031 isl_basic_map_free(bmap);
2032 isl_basic_map_free(context);
2033 return NULL;
2036 /* Does the (linear part of a) constraint "c" involve any of the "len"
2037 * "relevant" dimensions?
2039 static int is_related(isl_int *c, int len, int *relevant)
2041 int i;
2043 for (i = 0; i < len; ++i) {
2044 if (!relevant[i])
2045 continue;
2046 if (!isl_int_is_zero(c[i]))
2047 return 1;
2050 return 0;
2053 /* Drop constraints from "bset" that do not involve any of
2054 * the dimensions marked "relevant".
2056 static __isl_give isl_basic_set *drop_unrelated_constraints(
2057 __isl_take isl_basic_set *bset, int *relevant)
2059 int i, dim;
2061 dim = isl_basic_set_dim(bset, isl_dim_set);
2062 for (i = 0; i < dim; ++i)
2063 if (!relevant[i])
2064 break;
2065 if (i >= dim)
2066 return bset;
2068 for (i = bset->n_eq - 1; i >= 0; --i)
2069 if (!is_related(bset->eq[i] + 1, dim, relevant))
2070 isl_basic_set_drop_equality(bset, i);
2072 for (i = bset->n_ineq - 1; i >= 0; --i)
2073 if (!is_related(bset->ineq[i] + 1, dim, relevant))
2074 isl_basic_set_drop_inequality(bset, i);
2076 return bset;
2079 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2081 * In particular, for any variable involved in the constraint,
2082 * find the actual group id from before and replace the group
2083 * of the corresponding variable by the minimal group of all
2084 * the variables involved in the constraint considered so far
2085 * (if this minimum is smaller) or replace the minimum by this group
2086 * (if the minimum is larger).
2088 * At the end, all the variables in "c" will (indirectly) point
2089 * to the minimal of the groups that they referred to originally.
2091 static void update_groups(int dim, int *group, isl_int *c)
2093 int j;
2094 int min = dim;
2096 for (j = 0; j < dim; ++j) {
2097 if (isl_int_is_zero(c[j]))
2098 continue;
2099 while (group[j] >= 0 && group[group[j]] != group[j])
2100 group[j] = group[group[j]];
2101 if (group[j] == min)
2102 continue;
2103 if (group[j] < min) {
2104 if (min >= 0 && min < dim)
2105 group[min] = group[j];
2106 min = group[j];
2107 } else
2108 group[group[j]] = min;
2112 /* Allocate an array of groups of variables, one for each variable
2113 * in "context", initialized to zero.
2115 static int *alloc_groups(__isl_keep isl_basic_set *context)
2117 isl_ctx *ctx;
2118 int dim;
2120 dim = isl_basic_set_dim(context, isl_dim_set);
2121 ctx = isl_basic_set_get_ctx(context);
2122 return isl_calloc_array(ctx, int, dim);
2125 /* Drop constraints from "context" that only involve variables that are
2126 * not related to any of the variables marked with a "-1" in "group".
2128 * We construct groups of variables that collect variables that
2129 * (indirectly) appear in some common constraint of "context".
2130 * Each group is identified by the first variable in the group,
2131 * except for the special group of variables that was already identified
2132 * in the input as -1 (or are related to those variables).
2133 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2134 * otherwise the group of i is the group of group[i].
2136 * We first initialize groups for the remaining variables.
2137 * Then we iterate over the constraints of "context" and update the
2138 * group of the variables in the constraint by the smallest group.
2139 * Finally, we resolve indirect references to groups by running over
2140 * the variables.
2142 * After computing the groups, we drop constraints that do not involve
2143 * any variables in the -1 group.
2145 static __isl_give isl_basic_set *group_and_drop_irrelevant_constraints(
2146 __isl_take isl_basic_set *context, __isl_take int *group)
2148 int dim;
2149 int i;
2150 int last;
2152 dim = isl_basic_set_dim(context, isl_dim_set);
2154 last = -1;
2155 for (i = 0; i < dim; ++i)
2156 if (group[i] >= 0)
2157 last = group[i] = i;
2158 if (last < 0) {
2159 free(group);
2160 return context;
2163 for (i = 0; i < context->n_eq; ++i)
2164 update_groups(dim, group, context->eq[i] + 1);
2165 for (i = 0; i < context->n_ineq; ++i)
2166 update_groups(dim, group, context->ineq[i] + 1);
2168 for (i = 0; i < dim; ++i)
2169 if (group[i] >= 0)
2170 group[i] = group[group[i]];
2172 for (i = 0; i < dim; ++i)
2173 group[i] = group[i] == -1;
2175 context = drop_unrelated_constraints(context, group);
2177 free(group);
2178 return context;
2181 /* Drop constraints from "context" that are irrelevant for computing
2182 * the gist of "bset".
2184 * In particular, drop constraints in variables that are not related
2185 * to any of the variables involved in the constraints of "bset"
2186 * in the sense that there is no sequence of constraints that connects them.
2188 * We first mark all variables that appear in "bset" as belonging
2189 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2191 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2192 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2194 int *group;
2195 int dim;
2196 int i, j;
2198 if (!context || !bset)
2199 return isl_basic_set_free(context);
2201 group = alloc_groups(context);
2203 if (!group)
2204 return isl_basic_set_free(context);
2206 dim = isl_basic_set_dim(bset, isl_dim_set);
2207 for (i = 0; i < dim; ++i) {
2208 for (j = 0; j < bset->n_eq; ++j)
2209 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2210 break;
2211 if (j < bset->n_eq) {
2212 group[i] = -1;
2213 continue;
2215 for (j = 0; j < bset->n_ineq; ++j)
2216 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2217 break;
2218 if (j < bset->n_ineq)
2219 group[i] = -1;
2222 return group_and_drop_irrelevant_constraints(context, group);
2225 /* Drop constraints from "context" that are irrelevant for computing
2226 * the gist of the inequalities "ineq".
2227 * Inequalities in "ineq" for which the corresponding element of row
2228 * is set to -1 have already been marked for removal and should be ignored.
2230 * In particular, drop constraints in variables that are not related
2231 * to any of the variables involved in "ineq"
2232 * in the sense that there is no sequence of constraints that connects them.
2234 * We first mark all variables that appear in "bset" as belonging
2235 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2237 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2238 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2240 int *group;
2241 int dim;
2242 int i, j, n;
2244 if (!context || !ineq)
2245 return isl_basic_set_free(context);
2247 group = alloc_groups(context);
2249 if (!group)
2250 return isl_basic_set_free(context);
2252 dim = isl_basic_set_dim(context, isl_dim_set);
2253 n = isl_mat_rows(ineq);
2254 for (i = 0; i < dim; ++i) {
2255 for (j = 0; j < n; ++j) {
2256 if (row[j] < 0)
2257 continue;
2258 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2259 break;
2261 if (j < n)
2262 group[i] = -1;
2265 return group_and_drop_irrelevant_constraints(context, group);
2268 /* Do all "n" entries of "row" contain a negative value?
2270 static int all_neg(int *row, int n)
2272 int i;
2274 for (i = 0; i < n; ++i)
2275 if (row[i] >= 0)
2276 return 0;
2278 return 1;
2281 /* Update the inequalities in "bset" based on the information in "row"
2282 * and "tab".
2284 * In particular, the array "row" contains either -1, meaning that
2285 * the corresponding inequality of "bset" is redundant, or the index
2286 * of an inequality in "tab".
2288 * If the row entry is -1, then drop the inequality.
2289 * Otherwise, if the constraint is marked redundant in the tableau,
2290 * then drop the inequality.
2292 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2293 __isl_keep int *row, struct isl_tab *tab)
2295 int i;
2296 unsigned n_ineq;
2297 unsigned n_eq;
2299 if (!bset)
2300 return NULL;
2302 n_ineq = bset->n_ineq;
2303 for (i = n_ineq - 1; i >= 0; --i) {
2304 if (row[i] < 0) {
2305 if (isl_basic_set_drop_inequality(bset, i) < 0)
2306 return isl_basic_set_free(bset);
2307 continue;
2309 if (!tab)
2310 continue;
2311 n_eq = tab->n_eq;
2312 if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2313 if (isl_basic_set_drop_inequality(bset, i) < 0)
2314 return isl_basic_set_free(bset);
2318 bset = isl_basic_set_finalize(bset);
2319 return bset;
2322 /* Update the inequalities in "bset" based on the information in "row"
2323 * and "tab" and free all arguments (other than "bset").
2325 static __isl_give isl_basic_set *update_ineq_free(
2326 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2327 __isl_take isl_basic_set *context, __isl_take int *row,
2328 struct isl_tab *tab)
2330 isl_mat_free(ineq);
2331 isl_basic_set_free(context);
2333 bset = update_ineq(bset, row, tab);
2335 free(row);
2336 isl_tab_free(tab);
2337 return bset;
2340 /* Remove all information from bset that is redundant in the context
2341 * of context. Both bset and context are assumed to be full-dimensional.
2342 * "ineq" contains the inequalities of "bset".
2344 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2345 * A value of -1 means that the inequality is obviously redundant and may
2346 * not even appear in "tab".
2348 * We first mark the inequalities of "bset"
2349 * that are obviously redundant with respect to some inequality in "context".
2350 * Then we remove those constraints from "context" that have become
2351 * irrelevant for computing the gist of "bset".
2352 * Note that this removal of constraints cannot be replaced by
2353 * a factorization because factors in "bset" may still be connected
2354 * to each other through constraints in "context".
2356 * If there are any inequalities left, we construct a tableau for
2357 * the context and then add the inequalities of "bset".
2358 * Before adding these inequalities, we freeze all constraints such that
2359 * they won't be considered redundant in terms of the constraints of "bset".
2360 * Then we detect all redundant constraints (among the
2361 * constraints that weren't frozen), first by checking for redundancy in the
2362 * the tableau and then by checking if replacing a constraint by its negation
2363 * would lead to an empty set. This last step is fairly expensive
2364 * and could be optimized by more reuse of the tableau.
2365 * Finally, we update bset according to the results.
2367 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2368 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2370 int i, r;
2371 int *row = NULL;
2372 isl_ctx *ctx;
2373 isl_basic_set *combined = NULL;
2374 struct isl_tab *tab = NULL;
2375 unsigned context_ineq;
2376 unsigned total;
2378 if (!bset || !ineq || !context)
2379 goto error;
2381 if (isl_basic_set_is_universe(bset) ||
2382 isl_basic_set_is_universe(context)) {
2383 isl_basic_set_free(context);
2384 isl_mat_free(ineq);
2385 return bset;
2388 ctx = isl_basic_set_get_ctx(context);
2389 row = isl_calloc_array(ctx, int, bset->n_ineq);
2390 if (!row)
2391 goto error;
2393 if (mark_shifted_constraints(ineq, context, row) < 0)
2394 goto error;
2395 if (all_neg(row, bset->n_ineq))
2396 return update_ineq_free(bset, ineq, context, row, NULL);
2398 context = drop_irrelevant_constraints_marked(context, ineq, row);
2399 if (!context)
2400 goto error;
2401 if (isl_basic_set_is_universe(context))
2402 return update_ineq_free(bset, ineq, context, row, NULL);
2404 context_ineq = context->n_ineq;
2405 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2406 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2407 tab = isl_tab_from_basic_set(combined, 0);
2408 for (i = 0; i < context_ineq; ++i)
2409 if (isl_tab_freeze_constraint(tab, i) < 0)
2410 goto error;
2411 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2412 goto error;
2413 r = context_ineq;
2414 for (i = 0; i < bset->n_ineq; ++i) {
2415 if (row[i] < 0)
2416 continue;
2417 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2418 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2419 goto error;
2420 row[i] = r++;
2422 if (isl_tab_detect_redundant(tab) < 0)
2423 goto error;
2424 total = isl_basic_set_total_dim(bset);
2425 for (i = bset->n_ineq - 1; i >= 0; --i) {
2426 isl_basic_set *test;
2427 int is_empty;
2429 if (row[i] < 0)
2430 continue;
2431 r = row[i];
2432 if (tab->con[r].is_redundant)
2433 continue;
2434 test = isl_basic_set_dup(combined);
2435 if (isl_inequality_negate(test, r) < 0)
2436 test = isl_basic_set_free(test);
2437 test = isl_basic_set_update_from_tab(test, tab);
2438 is_empty = isl_basic_set_is_empty(test);
2439 isl_basic_set_free(test);
2440 if (is_empty < 0)
2441 goto error;
2442 if (is_empty)
2443 tab->con[r].is_redundant = 1;
2445 bset = update_ineq_free(bset, ineq, context, row, tab);
2446 if (bset) {
2447 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2448 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2451 isl_basic_set_free(combined);
2452 return bset;
2453 error:
2454 free(row);
2455 isl_mat_free(ineq);
2456 isl_tab_free(tab);
2457 isl_basic_set_free(combined);
2458 isl_basic_set_free(context);
2459 isl_basic_set_free(bset);
2460 return NULL;
2463 /* Extract the inequalities of "bset" as an isl_mat.
2465 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2467 unsigned total;
2468 isl_ctx *ctx;
2469 isl_mat *ineq;
2471 if (!bset)
2472 return NULL;
2474 ctx = isl_basic_set_get_ctx(bset);
2475 total = isl_basic_set_total_dim(bset);
2476 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2477 0, 1 + total);
2479 return ineq;
2482 /* Remove all information from "bset" that is redundant in the context
2483 * of "context", for the case where both "bset" and "context" are
2484 * full-dimensional.
2486 static __isl_give isl_basic_set *uset_gist_uncompressed(
2487 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2489 isl_mat *ineq;
2491 ineq = extract_ineq(bset);
2492 return uset_gist_full(bset, ineq, context);
2495 /* Remove all information from "bset" that is redundant in the context
2496 * of "context", for the case where the combined equalities of
2497 * "bset" and "context" allow for a compression that can be obtained
2498 * by preapplication of "T".
2499 * Preapplication of "T2" moves back to the original space.
2501 static __isl_give isl_basic_set *uset_gist_compressed(
2502 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2503 __isl_take isl_mat *T, __isl_take isl_mat *T2)
2505 isl_mat *ineq;
2507 bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
2508 context = isl_basic_set_preimage(context, T);
2510 ineq = extract_ineq(bset);
2511 bset = uset_gist_full(bset, ineq, context);
2512 return isl_basic_set_preimage(bset, T2);
2515 /* Remove all information from bset that is redundant in the context
2516 * of context. In particular, equalities that are linear combinations
2517 * of those in context are removed. Then the inequalities that are
2518 * redundant in the context of the equalities and inequalities of
2519 * context are removed.
2521 * First of all, we drop those constraints from "context"
2522 * that are irrelevant for computing the gist of "bset".
2523 * Alternatively, we could factorize the intersection of "context" and "bset".
2525 * We first compute the integer affine hull of the intersection,
2526 * compute the gist inside this affine hull and then add back
2527 * those equalities that are not implied by the context.
2529 * If two constraints are mutually redundant, then uset_gist_full
2530 * will remove the second of those constraints. We therefore first
2531 * sort the constraints so that constraints not involving existentially
2532 * quantified variables are given precedence over those that do.
2533 * We have to perform this sorting before the variable compression,
2534 * because that may effect the order of the variables.
2536 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2537 __isl_take isl_basic_set *context)
2539 isl_mat *eq;
2540 isl_mat *T, *T2;
2541 isl_basic_set *aff;
2542 isl_basic_set *aff_context;
2543 unsigned total;
2545 if (!bset || !context)
2546 goto error;
2548 context = drop_irrelevant_constraints(context, bset);
2550 aff = isl_basic_set_copy(bset);
2551 aff = isl_basic_set_intersect(aff, isl_basic_set_copy(context));
2552 aff = isl_basic_set_affine_hull(aff);
2553 if (!aff)
2554 goto error;
2555 if (isl_basic_set_plain_is_empty(aff)) {
2556 isl_basic_set_free(bset);
2557 isl_basic_set_free(context);
2558 return aff;
2560 bset = isl_basic_set_sort_constraints(bset);
2561 if (aff->n_eq == 0) {
2562 isl_basic_set_free(aff);
2563 return uset_gist_uncompressed(bset, context);
2565 total = isl_basic_set_total_dim(bset);
2566 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2567 eq = isl_mat_cow(eq);
2568 T = isl_mat_variable_compression(eq, &T2);
2569 if (T && T->n_col == 0) {
2570 isl_mat_free(T);
2571 isl_mat_free(T2);
2572 isl_basic_set_free(context);
2573 isl_basic_set_free(aff);
2574 return isl_basic_set_set_to_empty(bset);
2577 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2579 bset = uset_gist_compressed(bset, context, T, T2);
2580 bset = isl_basic_set_intersect(bset, aff);
2581 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2583 if (bset) {
2584 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2585 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2588 return bset;
2589 error:
2590 isl_basic_set_free(bset);
2591 isl_basic_set_free(context);
2592 return NULL;
2595 /* Return a basic map that has the same intersection with "context" as "bmap"
2596 * and that is as "simple" as possible.
2598 * The core computation is performed on the pure constraints.
2599 * When we add back the meaning of the integer divisions, we need
2600 * to (re)introduce the div constraints. If we happen to have
2601 * discovered that some of these integer divisions are equal to
2602 * some affine combination of other variables, then these div
2603 * constraints may end up getting simplified in terms of the equalities,
2604 * resulting in extra inequalities on the other variables that
2605 * may have been removed already or that may not even have been
2606 * part of the input. We try and remove those constraints of
2607 * this form that are most obviously redundant with respect to
2608 * the context. We also remove those div constraints that are
2609 * redundant with respect to the other constraints in the result.
2611 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
2612 struct isl_basic_map *context)
2614 isl_basic_set *bset, *eq;
2615 isl_basic_map *eq_bmap;
2616 unsigned n_div, n_eq, n_ineq;
2618 if (!bmap || !context)
2619 goto error;
2621 if (isl_basic_map_is_universe(bmap)) {
2622 isl_basic_map_free(context);
2623 return bmap;
2625 if (isl_basic_map_plain_is_empty(context)) {
2626 isl_space *space = isl_basic_map_get_space(bmap);
2627 isl_basic_map_free(bmap);
2628 isl_basic_map_free(context);
2629 return isl_basic_map_universe(space);
2631 if (isl_basic_map_plain_is_empty(bmap)) {
2632 isl_basic_map_free(context);
2633 return bmap;
2636 bmap = isl_basic_map_remove_redundancies(bmap);
2637 context = isl_basic_map_remove_redundancies(context);
2638 if (!context)
2639 goto error;
2641 context = isl_basic_map_align_divs(context, bmap);
2642 bmap = isl_basic_map_align_divs(bmap, context);
2643 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2645 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
2646 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
2648 if (!bset || bset->n_eq == 0 || n_div == 0 ||
2649 isl_basic_set_plain_is_empty(bset)) {
2650 isl_basic_map_free(context);
2651 return isl_basic_map_overlying_set(bset, bmap);
2654 n_eq = bset->n_eq;
2655 n_ineq = bset->n_ineq;
2656 eq = isl_basic_set_copy(bset);
2657 eq = isl_basic_set_cow(eq);
2658 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
2659 eq = isl_basic_set_free(eq);
2660 if (isl_basic_set_free_equality(bset, n_eq) < 0)
2661 bset = isl_basic_set_free(bset);
2663 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
2664 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
2665 bmap = isl_basic_map_overlying_set(bset, bmap);
2666 bmap = isl_basic_map_intersect(bmap, eq_bmap);
2667 bmap = isl_basic_map_remove_redundancies(bmap);
2669 return bmap;
2670 error:
2671 isl_basic_map_free(bmap);
2672 isl_basic_map_free(context);
2673 return NULL;
2677 * Assumes context has no implicit divs.
2679 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
2680 __isl_take isl_basic_map *context)
2682 int i;
2684 if (!map || !context)
2685 goto error;
2687 if (isl_basic_map_plain_is_empty(context)) {
2688 isl_space *space = isl_map_get_space(map);
2689 isl_map_free(map);
2690 isl_basic_map_free(context);
2691 return isl_map_universe(space);
2694 context = isl_basic_map_remove_redundancies(context);
2695 map = isl_map_cow(map);
2696 if (!map || !context)
2697 goto error;
2698 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
2699 map = isl_map_compute_divs(map);
2700 if (!map)
2701 goto error;
2702 for (i = map->n - 1; i >= 0; --i) {
2703 map->p[i] = isl_basic_map_gist(map->p[i],
2704 isl_basic_map_copy(context));
2705 if (!map->p[i])
2706 goto error;
2707 if (isl_basic_map_plain_is_empty(map->p[i])) {
2708 isl_basic_map_free(map->p[i]);
2709 if (i != map->n - 1)
2710 map->p[i] = map->p[map->n - 1];
2711 map->n--;
2714 isl_basic_map_free(context);
2715 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2716 return map;
2717 error:
2718 isl_map_free(map);
2719 isl_basic_map_free(context);
2720 return NULL;
2723 /* Return a map that has the same intersection with "context" as "map"
2724 * and that is as "simple" as possible.
2726 * If "map" is already the universe, then we cannot make it any simpler.
2727 * Similarly, if "context" is the universe, then we cannot exploit it
2728 * to simplify "map"
2729 * If "map" and "context" are identical to each other, then we can
2730 * return the corresponding universe.
2732 * If none of these cases apply, we have to work a bit harder.
2733 * During this computation, we make use of a single disjunct context,
2734 * so if the original context consists of more than one disjunct
2735 * then we need to approximate the context by a single disjunct set.
2736 * Simply taking the simple hull may drop constraints that are
2737 * only implicitly available in each disjunct. We therefore also
2738 * look for constraints among those defining "map" that are valid
2739 * for the context. These can then be used to simplify away
2740 * the corresponding constraints in "map".
2742 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
2743 __isl_take isl_map *context)
2745 int equal;
2746 int is_universe;
2747 isl_basic_map *hull;
2749 is_universe = isl_map_plain_is_universe(map);
2750 if (is_universe >= 0 && !is_universe)
2751 is_universe = isl_map_plain_is_universe(context);
2752 if (is_universe < 0)
2753 goto error;
2754 if (is_universe) {
2755 isl_map_free(context);
2756 return map;
2759 equal = isl_map_plain_is_equal(map, context);
2760 if (equal < 0)
2761 goto error;
2762 if (equal) {
2763 isl_map *res = isl_map_universe(isl_map_get_space(map));
2764 isl_map_free(map);
2765 isl_map_free(context);
2766 return res;
2769 context = isl_map_compute_divs(context);
2770 if (!context)
2771 goto error;
2772 if (isl_map_n_basic_map(context) == 1) {
2773 hull = isl_map_simple_hull(context);
2774 } else {
2775 isl_ctx *ctx;
2776 isl_map_list *list;
2778 ctx = isl_map_get_ctx(map);
2779 list = isl_map_list_alloc(ctx, 2);
2780 list = isl_map_list_add(list, isl_map_copy(context));
2781 list = isl_map_list_add(list, isl_map_copy(map));
2782 hull = isl_map_unshifted_simple_hull_from_map_list(context,
2783 list);
2785 return isl_map_gist_basic_map(map, hull);
2786 error:
2787 isl_map_free(map);
2788 isl_map_free(context);
2789 return NULL;
2792 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
2793 __isl_take isl_map *context)
2795 return isl_map_align_params_map_map_and(map, context, &map_gist);
2798 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
2799 struct isl_basic_set *context)
2801 return (struct isl_basic_set *)isl_basic_map_gist(
2802 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
2805 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
2806 __isl_take isl_basic_set *context)
2808 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
2809 (struct isl_basic_map *)context);
2812 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
2813 __isl_take isl_basic_set *context)
2815 isl_space *space = isl_set_get_space(set);
2816 isl_basic_set *dom_context = isl_basic_set_universe(space);
2817 dom_context = isl_basic_set_intersect_params(dom_context, context);
2818 return isl_set_gist_basic_set(set, dom_context);
2821 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
2822 __isl_take isl_set *context)
2824 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
2825 (struct isl_map *)context);
2828 /* Compute the gist of "bmap" with respect to the constraints "context"
2829 * on the domain.
2831 __isl_give isl_basic_map *isl_basic_map_gist_domain(
2832 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
2834 isl_space *space = isl_basic_map_get_space(bmap);
2835 isl_basic_map *bmap_context = isl_basic_map_universe(space);
2837 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
2838 return isl_basic_map_gist(bmap, bmap_context);
2841 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
2842 __isl_take isl_set *context)
2844 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2845 map_context = isl_map_intersect_domain(map_context, context);
2846 return isl_map_gist(map, map_context);
2849 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
2850 __isl_take isl_set *context)
2852 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2853 map_context = isl_map_intersect_range(map_context, context);
2854 return isl_map_gist(map, map_context);
2857 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
2858 __isl_take isl_set *context)
2860 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2861 map_context = isl_map_intersect_params(map_context, context);
2862 return isl_map_gist(map, map_context);
2865 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
2866 __isl_take isl_set *context)
2868 return isl_map_gist_params(set, context);
2871 /* Quick check to see if two basic maps are disjoint.
2872 * In particular, we reduce the equalities and inequalities of
2873 * one basic map in the context of the equalities of the other
2874 * basic map and check if we get a contradiction.
2876 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
2877 __isl_keep isl_basic_map *bmap2)
2879 struct isl_vec *v = NULL;
2880 int *elim = NULL;
2881 unsigned total;
2882 int i;
2884 if (!bmap1 || !bmap2)
2885 return isl_bool_error;
2886 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
2887 return isl_bool_error);
2888 if (bmap1->n_div || bmap2->n_div)
2889 return isl_bool_false;
2890 if (!bmap1->n_eq && !bmap2->n_eq)
2891 return isl_bool_false;
2893 total = isl_space_dim(bmap1->dim, isl_dim_all);
2894 if (total == 0)
2895 return isl_bool_false;
2896 v = isl_vec_alloc(bmap1->ctx, 1 + total);
2897 if (!v)
2898 goto error;
2899 elim = isl_alloc_array(bmap1->ctx, int, total);
2900 if (!elim)
2901 goto error;
2902 compute_elimination_index(bmap1, elim);
2903 for (i = 0; i < bmap2->n_eq; ++i) {
2904 int reduced;
2905 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
2906 bmap1, elim);
2907 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
2908 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2909 goto disjoint;
2911 for (i = 0; i < bmap2->n_ineq; ++i) {
2912 int reduced;
2913 reduced = reduced_using_equalities(v->block.data,
2914 bmap2->ineq[i], bmap1, elim);
2915 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2916 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2917 goto disjoint;
2919 compute_elimination_index(bmap2, elim);
2920 for (i = 0; i < bmap1->n_ineq; ++i) {
2921 int reduced;
2922 reduced = reduced_using_equalities(v->block.data,
2923 bmap1->ineq[i], bmap2, elim);
2924 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2925 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2926 goto disjoint;
2928 isl_vec_free(v);
2929 free(elim);
2930 return isl_bool_false;
2931 disjoint:
2932 isl_vec_free(v);
2933 free(elim);
2934 return isl_bool_true;
2935 error:
2936 isl_vec_free(v);
2937 free(elim);
2938 return isl_bool_error;
2941 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
2942 __isl_keep isl_basic_set *bset2)
2944 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
2945 (struct isl_basic_map *)bset2);
2948 /* Are "map1" and "map2" obviously disjoint?
2950 * If one of them is empty or if they live in different spaces (ignoring
2951 * parameters), then they are clearly disjoint.
2953 * If they have different parameters, then we skip any further tests.
2955 * If they are obviously equal, but not obviously empty, then we will
2956 * not be able to detect if they are disjoint.
2958 * Otherwise we check if each basic map in "map1" is obviously disjoint
2959 * from each basic map in "map2".
2961 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
2962 __isl_keep isl_map *map2)
2964 int i, j;
2965 isl_bool disjoint;
2966 isl_bool intersect;
2967 isl_bool match;
2969 if (!map1 || !map2)
2970 return isl_bool_error;
2972 disjoint = isl_map_plain_is_empty(map1);
2973 if (disjoint < 0 || disjoint)
2974 return disjoint;
2976 disjoint = isl_map_plain_is_empty(map2);
2977 if (disjoint < 0 || disjoint)
2978 return disjoint;
2980 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
2981 map2->dim, isl_dim_in);
2982 if (match < 0 || !match)
2983 return match < 0 ? isl_bool_error : isl_bool_true;
2985 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
2986 map2->dim, isl_dim_out);
2987 if (match < 0 || !match)
2988 return match < 0 ? isl_bool_error : isl_bool_true;
2990 match = isl_space_match(map1->dim, isl_dim_param,
2991 map2->dim, isl_dim_param);
2992 if (match < 0 || !match)
2993 return match < 0 ? isl_bool_error : isl_bool_false;
2995 intersect = isl_map_plain_is_equal(map1, map2);
2996 if (intersect < 0 || intersect)
2997 return intersect < 0 ? isl_bool_error : isl_bool_false;
2999 for (i = 0; i < map1->n; ++i) {
3000 for (j = 0; j < map2->n; ++j) {
3001 isl_bool d = isl_basic_map_plain_is_disjoint(map1->p[i],
3002 map2->p[j]);
3003 if (d != isl_bool_true)
3004 return d;
3007 return isl_bool_true;
3010 /* Are "map1" and "map2" disjoint?
3012 * They are disjoint if they are "obviously disjoint" or if one of them
3013 * is empty. Otherwise, they are not disjoint if one of them is universal.
3014 * If none of these cases apply, we compute the intersection and see if
3015 * the result is empty.
3017 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3019 isl_bool disjoint;
3020 isl_bool intersect;
3021 isl_map *test;
3023 disjoint = isl_map_plain_is_disjoint(map1, map2);
3024 if (disjoint < 0 || disjoint)
3025 return disjoint;
3027 disjoint = isl_map_is_empty(map1);
3028 if (disjoint < 0 || disjoint)
3029 return disjoint;
3031 disjoint = isl_map_is_empty(map2);
3032 if (disjoint < 0 || disjoint)
3033 return disjoint;
3035 intersect = isl_map_plain_is_universe(map1);
3036 if (intersect < 0 || intersect)
3037 return intersect < 0 ? isl_bool_error : isl_bool_false;
3039 intersect = isl_map_plain_is_universe(map2);
3040 if (intersect < 0 || intersect)
3041 return intersect < 0 ? isl_bool_error : isl_bool_false;
3043 test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
3044 disjoint = isl_map_is_empty(test);
3045 isl_map_free(test);
3047 return disjoint;
3050 /* Are "bmap1" and "bmap2" disjoint?
3052 * They are disjoint if they are "obviously disjoint" or if one of them
3053 * is empty. Otherwise, they are not disjoint if one of them is universal.
3054 * If none of these cases apply, we compute the intersection and see if
3055 * the result is empty.
3057 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3058 __isl_keep isl_basic_map *bmap2)
3060 isl_bool disjoint;
3061 isl_bool intersect;
3062 isl_basic_map *test;
3064 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3065 if (disjoint < 0 || disjoint)
3066 return disjoint;
3068 disjoint = isl_basic_map_is_empty(bmap1);
3069 if (disjoint < 0 || disjoint)
3070 return disjoint;
3072 disjoint = isl_basic_map_is_empty(bmap2);
3073 if (disjoint < 0 || disjoint)
3074 return disjoint;
3076 intersect = isl_basic_map_is_universe(bmap1);
3077 if (intersect < 0 || intersect)
3078 return intersect < 0 ? isl_bool_error : isl_bool_false;
3080 intersect = isl_basic_map_is_universe(bmap2);
3081 if (intersect < 0 || intersect)
3082 return intersect < 0 ? isl_bool_error : isl_bool_false;
3084 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3085 isl_basic_map_copy(bmap2));
3086 disjoint = isl_basic_map_is_empty(test);
3087 isl_basic_map_free(test);
3089 return disjoint;
3092 /* Are "bset1" and "bset2" disjoint?
3094 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3095 __isl_keep isl_basic_set *bset2)
3097 return isl_basic_map_is_disjoint(bset1, bset2);
3100 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3101 __isl_keep isl_set *set2)
3103 return isl_map_plain_is_disjoint((struct isl_map *)set1,
3104 (struct isl_map *)set2);
3107 /* Are "set1" and "set2" disjoint?
3109 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3111 return isl_map_is_disjoint(set1, set2);
3114 /* Check if we can combine a given div with lower bound l and upper
3115 * bound u with some other div and if so return that other div.
3116 * Otherwise return -1.
3118 * We first check that
3119 * - the bounds are opposites of each other (except for the constant
3120 * term)
3121 * - the bounds do not reference any other div
3122 * - no div is defined in terms of this div
3124 * Let m be the size of the range allowed on the div by the bounds.
3125 * That is, the bounds are of the form
3127 * e <= a <= e + m - 1
3129 * with e some expression in the other variables.
3130 * We look for another div b such that no third div is defined in terms
3131 * of this second div b and such that in any constraint that contains
3132 * a (except for the given lower and upper bound), also contains b
3133 * with a coefficient that is m times that of b.
3134 * That is, all constraints (execpt for the lower and upper bound)
3135 * are of the form
3137 * e + f (a + m b) >= 0
3139 * If so, we return b so that "a + m b" can be replaced by
3140 * a single div "c = a + m b".
3142 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
3143 unsigned div, unsigned l, unsigned u)
3145 int i, j;
3146 unsigned dim;
3147 int coalesce = -1;
3149 if (bmap->n_div <= 1)
3150 return -1;
3151 dim = isl_space_dim(bmap->dim, isl_dim_all);
3152 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3153 return -1;
3154 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3155 bmap->n_div - div - 1) != -1)
3156 return -1;
3157 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3158 dim + bmap->n_div))
3159 return -1;
3161 for (i = 0; i < bmap->n_div; ++i) {
3162 if (isl_int_is_zero(bmap->div[i][0]))
3163 continue;
3164 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3165 return -1;
3168 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3169 if (isl_int_is_neg(bmap->ineq[l][0])) {
3170 isl_int_sub(bmap->ineq[l][0],
3171 bmap->ineq[l][0], bmap->ineq[u][0]);
3172 bmap = isl_basic_map_copy(bmap);
3173 bmap = isl_basic_map_set_to_empty(bmap);
3174 isl_basic_map_free(bmap);
3175 return -1;
3177 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3178 for (i = 0; i < bmap->n_div; ++i) {
3179 if (i == div)
3180 continue;
3181 if (!pairs[i])
3182 continue;
3183 for (j = 0; j < bmap->n_div; ++j) {
3184 if (isl_int_is_zero(bmap->div[j][0]))
3185 continue;
3186 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3187 break;
3189 if (j < bmap->n_div)
3190 continue;
3191 for (j = 0; j < bmap->n_ineq; ++j) {
3192 int valid;
3193 if (j == l || j == u)
3194 continue;
3195 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
3196 continue;
3197 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3198 break;
3199 isl_int_mul(bmap->ineq[j][1 + dim + div],
3200 bmap->ineq[j][1 + dim + div],
3201 bmap->ineq[l][0]);
3202 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3203 bmap->ineq[j][1 + dim + i]);
3204 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3205 bmap->ineq[j][1 + dim + div],
3206 bmap->ineq[l][0]);
3207 if (!valid)
3208 break;
3210 if (j < bmap->n_ineq)
3211 continue;
3212 coalesce = i;
3213 break;
3215 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3216 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3217 return coalesce;
3220 /* Given a lower and an upper bound on div i, construct an inequality
3221 * that when nonnegative ensures that this pair of bounds always allows
3222 * for an integer value of the given div.
3223 * The lower bound is inequality l, while the upper bound is inequality u.
3224 * The constructed inequality is stored in ineq.
3225 * g, fl, fu are temporary scalars.
3227 * Let the upper bound be
3229 * -n_u a + e_u >= 0
3231 * and the lower bound
3233 * n_l a + e_l >= 0
3235 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
3236 * We have
3238 * - f_u e_l <= f_u f_l g a <= f_l e_u
3240 * Since all variables are integer valued, this is equivalent to
3242 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
3244 * If this interval is at least f_u f_l g, then it contains at least
3245 * one integer value for a.
3246 * That is, the test constraint is
3248 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
3250 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
3251 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
3253 unsigned dim;
3254 dim = isl_space_dim(bmap->dim, isl_dim_all);
3256 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
3257 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
3258 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
3259 isl_int_neg(fu, fu);
3260 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
3261 1 + dim + bmap->n_div);
3262 isl_int_add(ineq[0], ineq[0], fl);
3263 isl_int_add(ineq[0], ineq[0], fu);
3264 isl_int_sub_ui(ineq[0], ineq[0], 1);
3265 isl_int_mul(g, g, fl);
3266 isl_int_mul(g, g, fu);
3267 isl_int_sub(ineq[0], ineq[0], g);
3270 /* Remove more kinds of divs that are not strictly needed.
3271 * In particular, if all pairs of lower and upper bounds on a div
3272 * are such that they allow at least one integer value of the div,
3273 * the we can eliminate the div using Fourier-Motzkin without
3274 * introducing any spurious solutions.
3276 static struct isl_basic_map *drop_more_redundant_divs(
3277 struct isl_basic_map *bmap, int *pairs, int n)
3279 struct isl_tab *tab = NULL;
3280 struct isl_vec *vec = NULL;
3281 unsigned dim;
3282 int remove = -1;
3283 isl_int g, fl, fu;
3285 isl_int_init(g);
3286 isl_int_init(fl);
3287 isl_int_init(fu);
3289 if (!bmap)
3290 goto error;
3292 dim = isl_space_dim(bmap->dim, isl_dim_all);
3293 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
3294 if (!vec)
3295 goto error;
3297 tab = isl_tab_from_basic_map(bmap, 0);
3299 while (n > 0) {
3300 int i, l, u;
3301 int best = -1;
3302 enum isl_lp_result res;
3304 for (i = 0; i < bmap->n_div; ++i) {
3305 if (!pairs[i])
3306 continue;
3307 if (best >= 0 && pairs[best] <= pairs[i])
3308 continue;
3309 best = i;
3312 i = best;
3313 for (l = 0; l < bmap->n_ineq; ++l) {
3314 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
3315 continue;
3316 for (u = 0; u < bmap->n_ineq; ++u) {
3317 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
3318 continue;
3319 construct_test_ineq(bmap, i, l, u,
3320 vec->el, g, fl, fu);
3321 res = isl_tab_min(tab, vec->el,
3322 bmap->ctx->one, &g, NULL, 0);
3323 if (res == isl_lp_error)
3324 goto error;
3325 if (res == isl_lp_empty) {
3326 bmap = isl_basic_map_set_to_empty(bmap);
3327 break;
3329 if (res != isl_lp_ok || isl_int_is_neg(g))
3330 break;
3332 if (u < bmap->n_ineq)
3333 break;
3335 if (l == bmap->n_ineq) {
3336 remove = i;
3337 break;
3339 pairs[i] = 0;
3340 --n;
3343 isl_tab_free(tab);
3344 isl_vec_free(vec);
3346 isl_int_clear(g);
3347 isl_int_clear(fl);
3348 isl_int_clear(fu);
3350 free(pairs);
3352 if (remove < 0)
3353 return bmap;
3355 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
3356 return isl_basic_map_drop_redundant_divs(bmap);
3357 error:
3358 free(pairs);
3359 isl_basic_map_free(bmap);
3360 isl_tab_free(tab);
3361 isl_vec_free(vec);
3362 isl_int_clear(g);
3363 isl_int_clear(fl);
3364 isl_int_clear(fu);
3365 return NULL;
3368 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
3369 * and the upper bound u, div1 always occurs together with div2 in the form
3370 * (div1 + m div2), where m is the constant range on the variable div1
3371 * allowed by l and u, replace the pair div1 and div2 by a single
3372 * div that is equal to div1 + m div2.
3374 * The new div will appear in the location that contains div2.
3375 * We need to modify all constraints that contain
3376 * div2 = (div - div1) / m
3377 * (If a constraint does not contain div2, it will also not contain div1.)
3378 * If the constraint also contains div1, then we know they appear
3379 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3380 * i.e., the coefficient of div is f.
3382 * Otherwise, we first need to introduce div1 into the constraint.
3383 * Let the l be
3385 * div1 + f >=0
3387 * and u
3389 * -div1 + f' >= 0
3391 * A lower bound on div2
3393 * n div2 + t >= 0
3395 * can be replaced by
3397 * (n * (m div 2 + div1) + m t + n f)/g >= 0
3399 * with g = gcd(m,n).
3400 * An upper bound
3402 * -n div2 + t >= 0
3404 * can be replaced by
3406 * (-n * (m div2 + div1) + m t + n f')/g >= 0
3408 * These constraint are those that we would obtain from eliminating
3409 * div1 using Fourier-Motzkin.
3411 * After all constraints have been modified, we drop the lower and upper
3412 * bound and then drop div1.
3414 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
3415 unsigned div1, unsigned div2, unsigned l, unsigned u)
3417 isl_int a;
3418 isl_int b;
3419 isl_int m;
3420 unsigned dim, total;
3421 int i;
3423 dim = isl_space_dim(bmap->dim, isl_dim_all);
3424 total = 1 + dim + bmap->n_div;
3426 isl_int_init(a);
3427 isl_int_init(b);
3428 isl_int_init(m);
3429 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
3430 isl_int_add_ui(m, m, 1);
3432 for (i = 0; i < bmap->n_ineq; ++i) {
3433 if (i == l || i == u)
3434 continue;
3435 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
3436 continue;
3437 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
3438 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
3439 isl_int_divexact(a, m, b);
3440 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
3441 if (isl_int_is_pos(b)) {
3442 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3443 b, bmap->ineq[l], total);
3444 } else {
3445 isl_int_neg(b, b);
3446 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3447 b, bmap->ineq[u], total);
3450 isl_int_set(bmap->ineq[i][1 + dim + div2],
3451 bmap->ineq[i][1 + dim + div1]);
3452 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
3455 isl_int_clear(a);
3456 isl_int_clear(b);
3457 isl_int_clear(m);
3458 if (l > u) {
3459 isl_basic_map_drop_inequality(bmap, l);
3460 isl_basic_map_drop_inequality(bmap, u);
3461 } else {
3462 isl_basic_map_drop_inequality(bmap, u);
3463 isl_basic_map_drop_inequality(bmap, l);
3465 bmap = isl_basic_map_drop_div(bmap, div1);
3466 return bmap;
3469 /* First check if we can coalesce any pair of divs and
3470 * then continue with dropping more redundant divs.
3472 * We loop over all pairs of lower and upper bounds on a div
3473 * with coefficient 1 and -1, respectively, check if there
3474 * is any other div "c" with which we can coalesce the div
3475 * and if so, perform the coalescing.
3477 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
3478 struct isl_basic_map *bmap, int *pairs, int n)
3480 int i, l, u;
3481 unsigned dim;
3483 dim = isl_space_dim(bmap->dim, isl_dim_all);
3485 for (i = 0; i < bmap->n_div; ++i) {
3486 if (!pairs[i])
3487 continue;
3488 for (l = 0; l < bmap->n_ineq; ++l) {
3489 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
3490 continue;
3491 for (u = 0; u < bmap->n_ineq; ++u) {
3492 int c;
3494 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
3495 continue;
3496 c = div_find_coalesce(bmap, pairs, i, l, u);
3497 if (c < 0)
3498 continue;
3499 free(pairs);
3500 bmap = coalesce_divs(bmap, i, c, l, u);
3501 return isl_basic_map_drop_redundant_divs(bmap);
3506 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3507 return bmap;
3509 return drop_more_redundant_divs(bmap, pairs, n);
3512 /* Remove divs that are not strictly needed.
3513 * In particular, if a div only occurs positively (or negatively)
3514 * in constraints, then it can simply be dropped.
3515 * Also, if a div occurs in only two constraints and if moreover
3516 * those two constraints are opposite to each other, except for the constant
3517 * term and if the sum of the constant terms is such that for any value
3518 * of the other values, there is always at least one integer value of the
3519 * div, i.e., if one plus this sum is greater than or equal to
3520 * the (absolute value) of the coefficent of the div in the constraints,
3521 * then we can also simply drop the div.
3523 * We skip divs that appear in equalities or in the definition of other divs.
3524 * Divs that appear in the definition of other divs usually occur in at least
3525 * 4 constraints, but the constraints may have been simplified.
3527 * If any divs are left after these simple checks then we move on
3528 * to more complicated cases in drop_more_redundant_divs.
3530 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
3531 struct isl_basic_map *bmap)
3533 int i, j;
3534 unsigned off;
3535 int *pairs = NULL;
3536 int n = 0;
3538 if (!bmap)
3539 goto error;
3540 if (bmap->n_div == 0)
3541 return bmap;
3543 off = isl_space_dim(bmap->dim, isl_dim_all);
3544 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
3545 if (!pairs)
3546 goto error;
3548 for (i = 0; i < bmap->n_div; ++i) {
3549 int pos, neg;
3550 int last_pos, last_neg;
3551 int redundant;
3552 int defined;
3554 defined = !isl_int_is_zero(bmap->div[i][0]);
3555 for (j = i; j < bmap->n_div; ++j)
3556 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
3557 break;
3558 if (j < bmap->n_div)
3559 continue;
3560 for (j = 0; j < bmap->n_eq; ++j)
3561 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
3562 break;
3563 if (j < bmap->n_eq)
3564 continue;
3565 ++n;
3566 pos = neg = 0;
3567 for (j = 0; j < bmap->n_ineq; ++j) {
3568 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
3569 last_pos = j;
3570 ++pos;
3572 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
3573 last_neg = j;
3574 ++neg;
3577 pairs[i] = pos * neg;
3578 if (pairs[i] == 0) {
3579 for (j = bmap->n_ineq - 1; j >= 0; --j)
3580 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
3581 isl_basic_map_drop_inequality(bmap, j);
3582 bmap = isl_basic_map_drop_div(bmap, i);
3583 free(pairs);
3584 return isl_basic_map_drop_redundant_divs(bmap);
3586 if (pairs[i] != 1)
3587 continue;
3588 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
3589 bmap->ineq[last_neg] + 1,
3590 off + bmap->n_div))
3591 continue;
3593 isl_int_add(bmap->ineq[last_pos][0],
3594 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3595 isl_int_add_ui(bmap->ineq[last_pos][0],
3596 bmap->ineq[last_pos][0], 1);
3597 redundant = isl_int_ge(bmap->ineq[last_pos][0],
3598 bmap->ineq[last_pos][1+off+i]);
3599 isl_int_sub_ui(bmap->ineq[last_pos][0],
3600 bmap->ineq[last_pos][0], 1);
3601 isl_int_sub(bmap->ineq[last_pos][0],
3602 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3603 if (!redundant) {
3604 if (defined ||
3605 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
3606 pairs[i] = 0;
3607 --n;
3608 continue;
3610 bmap = set_div_from_lower_bound(bmap, i, last_pos);
3611 bmap = isl_basic_map_simplify(bmap);
3612 free(pairs);
3613 return isl_basic_map_drop_redundant_divs(bmap);
3615 if (last_pos > last_neg) {
3616 isl_basic_map_drop_inequality(bmap, last_pos);
3617 isl_basic_map_drop_inequality(bmap, last_neg);
3618 } else {
3619 isl_basic_map_drop_inequality(bmap, last_neg);
3620 isl_basic_map_drop_inequality(bmap, last_pos);
3622 bmap = isl_basic_map_drop_div(bmap, i);
3623 free(pairs);
3624 return isl_basic_map_drop_redundant_divs(bmap);
3627 if (n > 0)
3628 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
3630 free(pairs);
3631 return bmap;
3632 error:
3633 free(pairs);
3634 isl_basic_map_free(bmap);
3635 return NULL;
3638 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
3639 struct isl_basic_set *bset)
3641 return (struct isl_basic_set *)
3642 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
3645 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
3647 int i;
3649 if (!map)
3650 return NULL;
3651 for (i = 0; i < map->n; ++i) {
3652 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
3653 if (!map->p[i])
3654 goto error;
3656 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3657 return map;
3658 error:
3659 isl_map_free(map);
3660 return NULL;
3663 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
3665 return (struct isl_set *)
3666 isl_map_drop_redundant_divs((struct isl_map *)set);
3669 /* Does "bmap" satisfy any equality that involves more than 2 variables
3670 * and/or has coefficients different from -1 and 1?
3672 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
3674 int i;
3675 unsigned total;
3677 total = isl_basic_map_dim(bmap, isl_dim_all);
3679 for (i = 0; i < bmap->n_eq; ++i) {
3680 int j, k;
3682 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
3683 if (j < 0)
3684 continue;
3685 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3686 !isl_int_is_negone(bmap->eq[i][1 + j]))
3687 return 1;
3689 j += 1;
3690 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3691 if (k < 0)
3692 continue;
3693 j += k;
3694 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3695 !isl_int_is_negone(bmap->eq[i][1 + j]))
3696 return 1;
3698 j += 1;
3699 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3700 if (k >= 0)
3701 return 1;
3704 return 0;
3707 /* Remove any common factor g from the constraint coefficients in "v".
3708 * The constant term is stored in the first position and is replaced
3709 * by floor(c/g). If any common factor is removed and if this results
3710 * in a tightening of the constraint, then set *tightened.
3712 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
3713 int *tightened)
3715 isl_ctx *ctx;
3717 if (!v)
3718 return NULL;
3719 ctx = isl_vec_get_ctx(v);
3720 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
3721 if (isl_int_is_zero(ctx->normalize_gcd))
3722 return v;
3723 if (isl_int_is_one(ctx->normalize_gcd))
3724 return v;
3725 v = isl_vec_cow(v);
3726 if (!v)
3727 return NULL;
3728 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
3729 *tightened = 1;
3730 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
3731 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
3732 v->size - 1);
3733 return v;
3736 /* If "bmap" is an integer set that satisfies any equality involving
3737 * more than 2 variables and/or has coefficients different from -1 and 1,
3738 * then use variable compression to reduce the coefficients by removing
3739 * any (hidden) common factor.
3740 * In particular, apply the variable compression to each constraint,
3741 * factor out any common factor in the non-constant coefficients and
3742 * then apply the inverse of the compression.
3743 * At the end, we mark the basic map as having reduced constants.
3744 * If this flag is still set on the next invocation of this function,
3745 * then we skip the computation.
3747 * Removing a common factor may result in a tightening of some of
3748 * the constraints. If this happens, then we may end up with two
3749 * opposite inequalities that can be replaced by an equality.
3750 * We therefore call isl_basic_map_detect_inequality_pairs,
3751 * which checks for such pairs of inequalities as well as eliminate_divs_eq
3752 * and isl_basic_map_gauss if such a pair was found.
3754 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
3755 __isl_take isl_basic_map *bmap)
3757 unsigned total;
3758 isl_ctx *ctx;
3759 isl_vec *v;
3760 isl_mat *eq, *T, *T2;
3761 int i;
3762 int tightened;
3764 if (!bmap)
3765 return NULL;
3766 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
3767 return bmap;
3768 if (isl_basic_map_is_rational(bmap))
3769 return bmap;
3770 if (bmap->n_eq == 0)
3771 return bmap;
3772 if (!has_multiple_var_equality(bmap))
3773 return bmap;
3775 total = isl_basic_map_dim(bmap, isl_dim_all);
3776 ctx = isl_basic_map_get_ctx(bmap);
3777 v = isl_vec_alloc(ctx, 1 + total);
3778 if (!v)
3779 return isl_basic_map_free(bmap);
3781 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3782 T = isl_mat_variable_compression(eq, &T2);
3783 if (!T || !T2)
3784 goto error;
3785 if (T->n_col == 0) {
3786 isl_mat_free(T);
3787 isl_mat_free(T2);
3788 isl_vec_free(v);
3789 return isl_basic_map_set_to_empty(bmap);
3792 tightened = 0;
3793 for (i = 0; i < bmap->n_ineq; ++i) {
3794 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
3795 v = isl_vec_mat_product(v, isl_mat_copy(T));
3796 v = normalize_constraint(v, &tightened);
3797 v = isl_vec_mat_product(v, isl_mat_copy(T2));
3798 if (!v)
3799 goto error;
3800 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
3803 isl_mat_free(T);
3804 isl_mat_free(T2);
3805 isl_vec_free(v);
3807 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
3809 if (tightened) {
3810 int progress = 0;
3812 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
3813 if (progress) {
3814 bmap = eliminate_divs_eq(bmap, &progress);
3815 bmap = isl_basic_map_gauss(bmap, NULL);
3819 return bmap;
3820 error:
3821 isl_mat_free(T);
3822 isl_mat_free(T2);
3823 isl_vec_free(v);
3824 return isl_basic_map_free(bmap);
3827 /* Shift the integer division at position "div" of "bmap"
3828 * by "shift" times the variable at position "pos".
3829 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
3830 * corresponds to the constant term.
3832 * That is, if the integer division has the form
3834 * floor(f(x)/d)
3836 * then replace it by
3838 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
3840 __isl_give isl_basic_map *isl_basic_map_shift_div(
3841 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
3843 int i;
3844 unsigned total;
3846 if (!bmap)
3847 return NULL;
3849 total = isl_basic_map_dim(bmap, isl_dim_all);
3850 total -= isl_basic_map_dim(bmap, isl_dim_div);
3852 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
3854 for (i = 0; i < bmap->n_eq; ++i) {
3855 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
3856 continue;
3857 isl_int_submul(bmap->eq[i][pos],
3858 shift, bmap->eq[i][1 + total + div]);
3860 for (i = 0; i < bmap->n_ineq; ++i) {
3861 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
3862 continue;
3863 isl_int_submul(bmap->ineq[i][pos],
3864 shift, bmap->ineq[i][1 + total + div]);
3866 for (i = 0; i < bmap->n_div; ++i) {
3867 if (isl_int_is_zero(bmap->div[i][0]))
3868 continue;
3869 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
3870 continue;
3871 isl_int_submul(bmap->div[i][1 + pos],
3872 shift, bmap->div[i][1 + 1 + total + div]);
3875 return bmap;