isl_map_simplify.c: extract out setup_constraint_index
[isl.git] / isl_map_simplify.c
blob76de0649bd47a3de03508c369c49101da5cc1c40
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include "isl_equalities.h"
18 #include <isl/map.h>
19 #include <isl_seq.h>
20 #include "isl_tab.h"
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
25 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
27 isl_int *t = bmap->eq[a];
28 bmap->eq[a] = bmap->eq[b];
29 bmap->eq[b] = t;
32 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
34 if (a != b) {
35 isl_int *t = bmap->ineq[a];
36 bmap->ineq[a] = bmap->ineq[b];
37 bmap->ineq[b] = t;
41 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
43 isl_seq_cpy(c, c + n, rem);
44 isl_seq_clr(c + rem, n);
47 /* Drop n dimensions starting at first.
49 * In principle, this frees up some extra variables as the number
50 * of columns remains constant, but we would have to extend
51 * the div array too as the number of rows in this array is assumed
52 * to be equal to extra.
54 struct isl_basic_set *isl_basic_set_drop_dims(
55 struct isl_basic_set *bset, unsigned first, unsigned n)
57 int i;
59 if (!bset)
60 goto error;
62 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
64 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
65 return bset;
67 bset = isl_basic_set_cow(bset);
68 if (!bset)
69 return NULL;
71 for (i = 0; i < bset->n_eq; ++i)
72 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
73 (bset->dim->n_out-first-n)+bset->extra);
75 for (i = 0; i < bset->n_ineq; ++i)
76 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
77 (bset->dim->n_out-first-n)+bset->extra);
79 for (i = 0; i < bset->n_div; ++i)
80 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
81 (bset->dim->n_out-first-n)+bset->extra);
83 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
84 if (!bset->dim)
85 goto error;
87 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
88 bset = isl_basic_set_simplify(bset);
89 return isl_basic_set_finalize(bset);
90 error:
91 isl_basic_set_free(bset);
92 return NULL;
95 struct isl_set *isl_set_drop_dims(
96 struct isl_set *set, unsigned first, unsigned n)
98 int i;
100 if (!set)
101 goto error;
103 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
105 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
106 return set;
107 set = isl_set_cow(set);
108 if (!set)
109 goto error;
110 set->dim = isl_space_drop_outputs(set->dim, first, n);
111 if (!set->dim)
112 goto error;
114 for (i = 0; i < set->n; ++i) {
115 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
116 if (!set->p[i])
117 goto error;
120 ISL_F_CLR(set, ISL_SET_NORMALIZED);
121 return set;
122 error:
123 isl_set_free(set);
124 return NULL;
127 /* Move "n" divs starting at "first" to the end of the list of divs.
129 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
130 unsigned first, unsigned n)
132 isl_int **div;
133 int i;
135 if (first + n == bmap->n_div)
136 return bmap;
138 div = isl_alloc_array(bmap->ctx, isl_int *, n);
139 if (!div)
140 goto error;
141 for (i = 0; i < n; ++i)
142 div[i] = bmap->div[first + i];
143 for (i = 0; i < bmap->n_div - first - n; ++i)
144 bmap->div[first + i] = bmap->div[first + n + i];
145 for (i = 0; i < n; ++i)
146 bmap->div[bmap->n_div - n + i] = div[i];
147 free(div);
148 return bmap;
149 error:
150 isl_basic_map_free(bmap);
151 return NULL;
154 /* Drop "n" dimensions of type "type" starting at "first".
156 * In principle, this frees up some extra variables as the number
157 * of columns remains constant, but we would have to extend
158 * the div array too as the number of rows in this array is assumed
159 * to be equal to extra.
161 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
162 enum isl_dim_type type, unsigned first, unsigned n)
164 int i;
165 unsigned dim;
166 unsigned offset;
167 unsigned left;
169 if (!bmap)
170 goto error;
172 dim = isl_basic_map_dim(bmap, type);
173 isl_assert(bmap->ctx, first + n <= dim, goto error);
175 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
176 return bmap;
178 bmap = isl_basic_map_cow(bmap);
179 if (!bmap)
180 return NULL;
182 offset = isl_basic_map_offset(bmap, type) + first;
183 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
184 for (i = 0; i < bmap->n_eq; ++i)
185 constraint_drop_vars(bmap->eq[i]+offset, n, left);
187 for (i = 0; i < bmap->n_ineq; ++i)
188 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
190 for (i = 0; i < bmap->n_div; ++i)
191 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
193 if (type == isl_dim_div) {
194 bmap = move_divs_last(bmap, first, n);
195 if (!bmap)
196 goto error;
197 isl_basic_map_free_div(bmap, n);
198 } else
199 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
200 if (!bmap->dim)
201 goto error;
203 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
204 bmap = isl_basic_map_simplify(bmap);
205 return isl_basic_map_finalize(bmap);
206 error:
207 isl_basic_map_free(bmap);
208 return NULL;
211 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
212 enum isl_dim_type type, unsigned first, unsigned n)
214 return (isl_basic_set *)isl_basic_map_drop((isl_basic_map *)bset,
215 type, first, n);
218 struct isl_basic_map *isl_basic_map_drop_inputs(
219 struct isl_basic_map *bmap, unsigned first, unsigned n)
221 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
224 struct isl_map *isl_map_drop(struct isl_map *map,
225 enum isl_dim_type type, unsigned first, unsigned n)
227 int i;
229 if (!map)
230 goto error;
232 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
234 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
235 return map;
236 map = isl_map_cow(map);
237 if (!map)
238 goto error;
239 map->dim = isl_space_drop_dims(map->dim, type, first, n);
240 if (!map->dim)
241 goto error;
243 for (i = 0; i < map->n; ++i) {
244 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
245 if (!map->p[i])
246 goto error;
248 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
250 return map;
251 error:
252 isl_map_free(map);
253 return NULL;
256 struct isl_set *isl_set_drop(struct isl_set *set,
257 enum isl_dim_type type, unsigned first, unsigned n)
259 return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
262 struct isl_map *isl_map_drop_inputs(
263 struct isl_map *map, unsigned first, unsigned n)
265 return isl_map_drop(map, isl_dim_in, first, n);
269 * We don't cow, as the div is assumed to be redundant.
271 static struct isl_basic_map *isl_basic_map_drop_div(
272 struct isl_basic_map *bmap, unsigned div)
274 int i;
275 unsigned pos;
277 if (!bmap)
278 goto error;
280 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
282 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
284 for (i = 0; i < bmap->n_eq; ++i)
285 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
287 for (i = 0; i < bmap->n_ineq; ++i) {
288 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
289 isl_basic_map_drop_inequality(bmap, i);
290 --i;
291 continue;
293 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
296 for (i = 0; i < bmap->n_div; ++i)
297 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
299 if (div != bmap->n_div - 1) {
300 int j;
301 isl_int *t = bmap->div[div];
303 for (j = div; j < bmap->n_div - 1; ++j)
304 bmap->div[j] = bmap->div[j+1];
306 bmap->div[bmap->n_div - 1] = t;
308 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
309 isl_basic_map_free_div(bmap, 1);
311 return bmap;
312 error:
313 isl_basic_map_free(bmap);
314 return NULL;
317 struct isl_basic_map *isl_basic_map_normalize_constraints(
318 struct isl_basic_map *bmap)
320 int i;
321 isl_int gcd;
322 unsigned total = isl_basic_map_total_dim(bmap);
324 if (!bmap)
325 return NULL;
327 isl_int_init(gcd);
328 for (i = bmap->n_eq - 1; i >= 0; --i) {
329 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
330 if (isl_int_is_zero(gcd)) {
331 if (!isl_int_is_zero(bmap->eq[i][0])) {
332 bmap = isl_basic_map_set_to_empty(bmap);
333 break;
335 isl_basic_map_drop_equality(bmap, i);
336 continue;
338 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
339 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
340 if (isl_int_is_one(gcd))
341 continue;
342 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
343 bmap = isl_basic_map_set_to_empty(bmap);
344 break;
346 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
349 for (i = bmap->n_ineq - 1; i >= 0; --i) {
350 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
351 if (isl_int_is_zero(gcd)) {
352 if (isl_int_is_neg(bmap->ineq[i][0])) {
353 bmap = isl_basic_map_set_to_empty(bmap);
354 break;
356 isl_basic_map_drop_inequality(bmap, i);
357 continue;
359 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
360 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
361 if (isl_int_is_one(gcd))
362 continue;
363 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
364 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
366 isl_int_clear(gcd);
368 return bmap;
371 struct isl_basic_set *isl_basic_set_normalize_constraints(
372 struct isl_basic_set *bset)
374 return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
375 (struct isl_basic_map *)bset);
378 /* Assuming the variable at position "pos" has an integer coefficient
379 * in integer division "div", extract it from this integer division.
380 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
381 * corresponds to the constant term.
383 * That is, the integer division is of the form
385 * floor((... + c * d * x_pos + ...)/d)
387 * Replace it by
389 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
391 static __isl_give isl_basic_map *remove_var_from_div(
392 __isl_take isl_basic_map *bmap, int div, int pos)
394 isl_int shift;
396 isl_int_init(shift);
397 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
398 isl_int_neg(shift, shift);
399 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
400 isl_int_clear(shift);
402 return bmap;
405 /* Check if integer division "div" has any integral coefficient
406 * (or constant term). If so, extract them from the integer division.
408 static __isl_give isl_basic_map *remove_independent_vars_from_div(
409 __isl_take isl_basic_map *bmap, int div)
411 int i;
412 unsigned total = 1 + isl_basic_map_total_dim(bmap);
414 for (i = 0; i < total; ++i) {
415 if (isl_int_is_zero(bmap->div[div][1 + i]))
416 continue;
417 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
418 bmap->div[div][0]))
419 continue;
420 bmap = remove_var_from_div(bmap, div, i);
421 if (!bmap)
422 break;
425 return bmap;
428 /* Check if any known integer division has any integral coefficient
429 * (or constant term). If so, extract them from the integer division.
431 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
432 __isl_take isl_basic_map *bmap)
434 int i;
436 if (!bmap)
437 return NULL;
438 if (bmap->n_div == 0)
439 return bmap;
441 for (i = 0; i < bmap->n_div; ++i) {
442 if (isl_int_is_zero(bmap->div[i][0]))
443 continue;
444 bmap = remove_independent_vars_from_div(bmap, i);
445 if (!bmap)
446 break;
449 return bmap;
452 /* Remove any common factor in numerator and denominator of the div expression,
453 * not taking into account the constant term.
454 * That is, if the div is of the form
456 * floor((a + m f(x))/(m d))
458 * then replace it by
460 * floor((floor(a/m) + f(x))/d)
462 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
463 * and can therefore not influence the result of the floor.
465 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
467 unsigned total = isl_basic_map_total_dim(bmap);
468 isl_ctx *ctx = bmap->ctx;
470 if (isl_int_is_zero(bmap->div[div][0]))
471 return;
472 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
473 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
474 if (isl_int_is_one(ctx->normalize_gcd))
475 return;
476 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
477 ctx->normalize_gcd);
478 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
479 ctx->normalize_gcd);
480 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
481 ctx->normalize_gcd, total);
484 /* Remove any common factor in numerator and denominator of a div expression,
485 * not taking into account the constant term.
486 * That is, look for any div of the form
488 * floor((a + m f(x))/(m d))
490 * and replace it by
492 * floor((floor(a/m) + f(x))/d)
494 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
495 * and can therefore not influence the result of the floor.
497 static __isl_give isl_basic_map *normalize_div_expressions(
498 __isl_take isl_basic_map *bmap)
500 int i;
502 if (!bmap)
503 return NULL;
504 if (bmap->n_div == 0)
505 return bmap;
507 for (i = 0; i < bmap->n_div; ++i)
508 normalize_div_expression(bmap, i);
510 return bmap;
513 /* Assumes divs have been ordered if keep_divs is set.
515 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
516 unsigned pos, isl_int *eq, int keep_divs, int *progress)
518 unsigned total;
519 unsigned space_total;
520 int k;
521 int last_div;
523 total = isl_basic_map_total_dim(bmap);
524 space_total = isl_space_dim(bmap->dim, isl_dim_all);
525 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
526 for (k = 0; k < bmap->n_eq; ++k) {
527 if (bmap->eq[k] == eq)
528 continue;
529 if (isl_int_is_zero(bmap->eq[k][1+pos]))
530 continue;
531 if (progress)
532 *progress = 1;
533 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
534 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
537 for (k = 0; k < bmap->n_ineq; ++k) {
538 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
539 continue;
540 if (progress)
541 *progress = 1;
542 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
543 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
544 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
547 for (k = 0; k < bmap->n_div; ++k) {
548 if (isl_int_is_zero(bmap->div[k][0]))
549 continue;
550 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
551 continue;
552 if (progress)
553 *progress = 1;
554 /* We need to be careful about circular definitions,
555 * so for now we just remove the definition of div k
556 * if the equality contains any divs.
557 * If keep_divs is set, then the divs have been ordered
558 * and we can keep the definition as long as the result
559 * is still ordered.
561 if (last_div == -1 || (keep_divs && last_div < k)) {
562 isl_seq_elim(bmap->div[k]+1, eq,
563 1+pos, 1+total, &bmap->div[k][0]);
564 normalize_div_expression(bmap, k);
565 } else
566 isl_seq_clr(bmap->div[k], 1 + total);
567 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
571 /* Assumes divs have been ordered if keep_divs is set.
573 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
574 isl_int *eq, unsigned div, int keep_divs)
576 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
578 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
580 bmap = isl_basic_map_drop_div(bmap, div);
582 return bmap;
585 /* Check if elimination of div "div" using equality "eq" would not
586 * result in a div depending on a later div.
588 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
589 unsigned div)
591 int k;
592 int last_div;
593 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
594 unsigned pos = space_total + div;
596 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
597 if (last_div < 0 || last_div <= div)
598 return 1;
600 for (k = 0; k <= last_div; ++k) {
601 if (isl_int_is_zero(bmap->div[k][0]))
602 return 1;
603 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
604 return 0;
607 return 1;
610 /* Elimininate divs based on equalities
612 static struct isl_basic_map *eliminate_divs_eq(
613 struct isl_basic_map *bmap, int *progress)
615 int d;
616 int i;
617 int modified = 0;
618 unsigned off;
620 bmap = isl_basic_map_order_divs(bmap);
622 if (!bmap)
623 return NULL;
625 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
627 for (d = bmap->n_div - 1; d >= 0 ; --d) {
628 for (i = 0; i < bmap->n_eq; ++i) {
629 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
630 !isl_int_is_negone(bmap->eq[i][off + d]))
631 continue;
632 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
633 continue;
634 modified = 1;
635 *progress = 1;
636 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
637 if (isl_basic_map_drop_equality(bmap, i) < 0)
638 return isl_basic_map_free(bmap);
639 break;
642 if (modified)
643 return eliminate_divs_eq(bmap, progress);
644 return bmap;
647 /* Elimininate divs based on inequalities
649 static struct isl_basic_map *eliminate_divs_ineq(
650 struct isl_basic_map *bmap, int *progress)
652 int d;
653 int i;
654 unsigned off;
655 struct isl_ctx *ctx;
657 if (!bmap)
658 return NULL;
660 ctx = bmap->ctx;
661 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
663 for (d = bmap->n_div - 1; d >= 0 ; --d) {
664 for (i = 0; i < bmap->n_eq; ++i)
665 if (!isl_int_is_zero(bmap->eq[i][off + d]))
666 break;
667 if (i < bmap->n_eq)
668 continue;
669 for (i = 0; i < bmap->n_ineq; ++i)
670 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
671 break;
672 if (i < bmap->n_ineq)
673 continue;
674 *progress = 1;
675 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
676 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
677 break;
678 bmap = isl_basic_map_drop_div(bmap, d);
679 if (!bmap)
680 break;
682 return bmap;
685 struct isl_basic_map *isl_basic_map_gauss(
686 struct isl_basic_map *bmap, int *progress)
688 int k;
689 int done;
690 int last_var;
691 unsigned total_var;
692 unsigned total;
694 bmap = isl_basic_map_order_divs(bmap);
696 if (!bmap)
697 return NULL;
699 total = isl_basic_map_total_dim(bmap);
700 total_var = total - bmap->n_div;
702 last_var = total - 1;
703 for (done = 0; done < bmap->n_eq; ++done) {
704 for (; last_var >= 0; --last_var) {
705 for (k = done; k < bmap->n_eq; ++k)
706 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
707 break;
708 if (k < bmap->n_eq)
709 break;
711 if (last_var < 0)
712 break;
713 if (k != done)
714 swap_equality(bmap, k, done);
715 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
716 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
718 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
719 progress);
721 if (last_var >= total_var &&
722 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
723 unsigned div = last_var - total_var;
724 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
725 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
726 isl_int_set(bmap->div[div][0],
727 bmap->eq[done][1+last_var]);
728 if (progress)
729 *progress = 1;
730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
733 if (done == bmap->n_eq)
734 return bmap;
735 for (k = done; k < bmap->n_eq; ++k) {
736 if (isl_int_is_zero(bmap->eq[k][0]))
737 continue;
738 return isl_basic_map_set_to_empty(bmap);
740 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
741 return bmap;
744 struct isl_basic_set *isl_basic_set_gauss(
745 struct isl_basic_set *bset, int *progress)
747 return (struct isl_basic_set*)isl_basic_map_gauss(
748 (struct isl_basic_map *)bset, progress);
752 static unsigned int round_up(unsigned int v)
754 int old_v = v;
756 while (v) {
757 old_v = v;
758 v ^= v & -v;
760 return old_v << 1;
763 /* 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.
770 struct isl_constraint_index {
771 unsigned int size;
772 int bits;
773 isl_int ***index;
776 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
778 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
779 __isl_keep isl_basic_map *bmap)
781 isl_ctx *ctx;
783 ci->index = NULL;
784 if (!bmap)
785 return isl_stat_error;
786 if (bmap->n_ineq == 0)
787 return isl_stat_ok;
788 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
789 ci->bits = ffs(ci->size) - 1;
790 ctx = isl_basic_map_get_ctx(bmap);
791 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
792 if (!ci->index)
793 return isl_stat_error;
795 return isl_stat_ok;
798 /* Free the memory allocated by create_constraint_index.
800 static void constraint_index_free(struct isl_constraint_index *ci)
802 free(ci->index);
805 static int hash_index(struct isl_constraint_index *ci,
806 struct isl_basic_map *bmap, int k)
808 int h;
809 unsigned total = isl_basic_map_total_dim(bmap);
810 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, ci->bits);
811 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
812 if (&bmap->ineq[k] != ci->index[h] &&
813 isl_seq_eq(bmap->ineq[k]+1, ci->index[h][0]+1, total))
814 break;
815 return h;
818 static int set_hash_index(struct isl_constraint_index *ci,
819 struct isl_basic_set *bset, int k)
821 return hash_index(ci, bset, k);
824 /* Fill in the "ci" data structure with the inequalities of "bset".
826 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
827 __isl_keep isl_basic_set *bset)
829 int k, h;
831 if (create_constraint_index(ci, bset) < 0)
832 return isl_stat_error;
834 for (k = 0; k < bset->n_ineq; ++k) {
835 h = set_hash_index(ci, bset, k);
836 ci->index[h] = &bset->ineq[k];
839 return isl_stat_ok;
842 /* If we can eliminate more than one div, then we need to make
843 * sure we do it from last div to first div, in order not to
844 * change the position of the other divs that still need to
845 * be removed.
847 static struct isl_basic_map *remove_duplicate_divs(
848 struct isl_basic_map *bmap, int *progress)
850 unsigned int size;
851 int *index;
852 int *elim_for;
853 int k, l, h;
854 int bits;
855 struct isl_blk eq;
856 unsigned total_var;
857 unsigned total;
858 struct isl_ctx *ctx;
860 bmap = isl_basic_map_order_divs(bmap);
861 if (!bmap || bmap->n_div <= 1)
862 return bmap;
864 total_var = isl_space_dim(bmap->dim, isl_dim_all);
865 total = total_var + bmap->n_div;
867 ctx = bmap->ctx;
868 for (k = bmap->n_div - 1; k >= 0; --k)
869 if (!isl_int_is_zero(bmap->div[k][0]))
870 break;
871 if (k <= 0)
872 return bmap;
874 size = round_up(4 * bmap->n_div / 3 - 1);
875 if (size == 0)
876 return bmap;
877 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
878 bits = ffs(size) - 1;
879 index = isl_calloc_array(ctx, int, size);
880 if (!elim_for || !index)
881 goto out;
882 eq = isl_blk_alloc(ctx, 1+total);
883 if (isl_blk_is_error(eq))
884 goto out;
886 isl_seq_clr(eq.data, 1+total);
887 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
888 for (--k; k >= 0; --k) {
889 uint32_t hash;
891 if (isl_int_is_zero(bmap->div[k][0]))
892 continue;
894 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
895 for (h = hash; index[h]; h = (h+1) % size)
896 if (isl_seq_eq(bmap->div[k],
897 bmap->div[index[h]-1], 2+total))
898 break;
899 if (index[h]) {
900 *progress = 1;
901 l = index[h] - 1;
902 elim_for[l] = k + 1;
904 index[h] = k+1;
906 for (l = bmap->n_div - 1; l >= 0; --l) {
907 if (!elim_for[l])
908 continue;
909 k = elim_for[l] - 1;
910 isl_int_set_si(eq.data[1+total_var+k], -1);
911 isl_int_set_si(eq.data[1+total_var+l], 1);
912 bmap = eliminate_div(bmap, eq.data, l, 1);
913 if (!bmap)
914 break;
915 isl_int_set_si(eq.data[1+total_var+k], 0);
916 isl_int_set_si(eq.data[1+total_var+l], 0);
919 isl_blk_free(ctx, eq);
920 out:
921 free(index);
922 free(elim_for);
923 return bmap;
926 static int n_pure_div_eq(struct isl_basic_map *bmap)
928 int i, j;
929 unsigned total;
931 total = isl_space_dim(bmap->dim, isl_dim_all);
932 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
933 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
934 --j;
935 if (j < 0)
936 break;
937 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
938 return 0;
940 return i;
943 /* Normalize divs that appear in equalities.
945 * In particular, we assume that bmap contains some equalities
946 * of the form
948 * a x = m * e_i
950 * and we want to replace the set of e_i by a minimal set and
951 * such that the new e_i have a canonical representation in terms
952 * of the vector x.
953 * If any of the equalities involves more than one divs, then
954 * we currently simply bail out.
956 * Let us first additionally assume that all equalities involve
957 * a div. The equalities then express modulo constraints on the
958 * remaining variables and we can use "parameter compression"
959 * to find a minimal set of constraints. The result is a transformation
961 * x = T(x') = x_0 + G x'
963 * with G a lower-triangular matrix with all elements below the diagonal
964 * non-negative and smaller than the diagonal element on the same row.
965 * We first normalize x_0 by making the same property hold in the affine
966 * T matrix.
967 * The rows i of G with a 1 on the diagonal do not impose any modulo
968 * constraint and simply express x_i = x'_i.
969 * For each of the remaining rows i, we introduce a div and a corresponding
970 * equality. In particular
972 * g_ii e_j = x_i - g_i(x')
974 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
975 * corresponding div (if g_kk != 1).
977 * If there are any equalities not involving any div, then we
978 * first apply a variable compression on the variables x:
980 * x = C x'' x'' = C_2 x
982 * and perform the above parameter compression on A C instead of on A.
983 * The resulting compression is then of the form
985 * x'' = T(x') = x_0 + G x'
987 * and in constructing the new divs and the corresponding equalities,
988 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
989 * by the corresponding row from C_2.
991 static struct isl_basic_map *normalize_divs(
992 struct isl_basic_map *bmap, int *progress)
994 int i, j, k;
995 int total;
996 int div_eq;
997 struct isl_mat *B;
998 struct isl_vec *d;
999 struct isl_mat *T = NULL;
1000 struct isl_mat *C = NULL;
1001 struct isl_mat *C2 = NULL;
1002 isl_int v;
1003 int *pos;
1004 int dropped, needed;
1006 if (!bmap)
1007 return NULL;
1009 if (bmap->n_div == 0)
1010 return bmap;
1012 if (bmap->n_eq == 0)
1013 return bmap;
1015 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1016 return bmap;
1018 total = isl_space_dim(bmap->dim, isl_dim_all);
1019 div_eq = n_pure_div_eq(bmap);
1020 if (div_eq == 0)
1021 return bmap;
1023 if (div_eq < bmap->n_eq) {
1024 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1025 bmap->n_eq - div_eq, 0, 1 + total);
1026 C = isl_mat_variable_compression(B, &C2);
1027 if (!C || !C2)
1028 goto error;
1029 if (C->n_col == 0) {
1030 bmap = isl_basic_map_set_to_empty(bmap);
1031 isl_mat_free(C);
1032 isl_mat_free(C2);
1033 goto done;
1037 d = isl_vec_alloc(bmap->ctx, div_eq);
1038 if (!d)
1039 goto error;
1040 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1041 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1042 --j;
1043 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1045 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1047 if (C) {
1048 B = isl_mat_product(B, C);
1049 C = NULL;
1052 T = isl_mat_parameter_compression(B, d);
1053 if (!T)
1054 goto error;
1055 if (T->n_col == 0) {
1056 bmap = isl_basic_map_set_to_empty(bmap);
1057 isl_mat_free(C2);
1058 isl_mat_free(T);
1059 goto done;
1061 isl_int_init(v);
1062 for (i = 0; i < T->n_row - 1; ++i) {
1063 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1064 if (isl_int_is_zero(v))
1065 continue;
1066 isl_mat_col_submul(T, 0, v, 1 + i);
1068 isl_int_clear(v);
1069 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1070 if (!pos)
1071 goto error;
1072 /* We have to be careful because dropping equalities may reorder them */
1073 dropped = 0;
1074 for (j = bmap->n_div - 1; j >= 0; --j) {
1075 for (i = 0; i < bmap->n_eq; ++i)
1076 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1077 break;
1078 if (i < bmap->n_eq) {
1079 bmap = isl_basic_map_drop_div(bmap, j);
1080 isl_basic_map_drop_equality(bmap, i);
1081 ++dropped;
1084 pos[0] = 0;
1085 needed = 0;
1086 for (i = 1; i < T->n_row; ++i) {
1087 if (isl_int_is_one(T->row[i][i]))
1088 pos[i] = i;
1089 else
1090 needed++;
1092 if (needed > dropped) {
1093 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1094 needed, needed, 0);
1095 if (!bmap)
1096 goto error;
1098 for (i = 1; i < T->n_row; ++i) {
1099 if (isl_int_is_one(T->row[i][i]))
1100 continue;
1101 k = isl_basic_map_alloc_div(bmap);
1102 pos[i] = 1 + total + k;
1103 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1104 isl_int_set(bmap->div[k][0], T->row[i][i]);
1105 if (C2)
1106 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1107 else
1108 isl_int_set_si(bmap->div[k][1 + i], 1);
1109 for (j = 0; j < i; ++j) {
1110 if (isl_int_is_zero(T->row[i][j]))
1111 continue;
1112 if (pos[j] < T->n_row && C2)
1113 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1114 C2->row[pos[j]], 1 + total);
1115 else
1116 isl_int_neg(bmap->div[k][1 + pos[j]],
1117 T->row[i][j]);
1119 j = isl_basic_map_alloc_equality(bmap);
1120 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1121 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1123 free(pos);
1124 isl_mat_free(C2);
1125 isl_mat_free(T);
1127 if (progress)
1128 *progress = 1;
1129 done:
1130 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1132 return bmap;
1133 error:
1134 isl_mat_free(C);
1135 isl_mat_free(C2);
1136 isl_mat_free(T);
1137 return bmap;
1140 static struct isl_basic_map *set_div_from_lower_bound(
1141 struct isl_basic_map *bmap, int div, int ineq)
1143 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1145 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1146 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1147 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1148 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1149 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1151 return bmap;
1154 /* Check whether it is ok to define a div based on an inequality.
1155 * To avoid the introduction of circular definitions of divs, we
1156 * do not allow such a definition if the resulting expression would refer to
1157 * any other undefined divs or if any known div is defined in
1158 * terms of the unknown div.
1160 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1161 int div, int ineq)
1163 int j;
1164 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1166 /* Not defined in terms of unknown divs */
1167 for (j = 0; j < bmap->n_div; ++j) {
1168 if (div == j)
1169 continue;
1170 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1171 continue;
1172 if (isl_int_is_zero(bmap->div[j][0]))
1173 return 0;
1176 /* No other div defined in terms of this one => avoid loops */
1177 for (j = 0; j < bmap->n_div; ++j) {
1178 if (div == j)
1179 continue;
1180 if (isl_int_is_zero(bmap->div[j][0]))
1181 continue;
1182 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1183 return 0;
1186 return 1;
1189 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1190 * be a better expression than the current one?
1192 * If we do not have any expression yet, then any expression would be better.
1193 * Otherwise we check if the last variable involved in the inequality
1194 * (disregarding the div that it would define) is in an earlier position
1195 * than the last variable involved in the current div expression.
1197 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1198 int div, int ineq)
1200 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1201 int last_div;
1202 int last_ineq;
1204 if (isl_int_is_zero(bmap->div[div][0]))
1205 return 1;
1207 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1208 bmap->n_div - (div + 1)) >= 0)
1209 return 0;
1211 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1212 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1213 total + bmap->n_div);
1215 return last_ineq < last_div;
1218 /* Given two constraints "k" and "l" that are opposite to each other,
1219 * except for the constant term, check if we can use them
1220 * to obtain an expression for one of the hitherto unknown divs or
1221 * a "better" expression for a div for which we already have an expression.
1222 * "sum" is the sum of the constant terms of the constraints.
1223 * If this sum is strictly smaller than the coefficient of one
1224 * of the divs, then this pair can be used define the div.
1225 * To avoid the introduction of circular definitions of divs, we
1226 * do not use the pair if the resulting expression would refer to
1227 * any other undefined divs or if any known div is defined in
1228 * terms of the unknown div.
1230 static struct isl_basic_map *check_for_div_constraints(
1231 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1233 int i;
1234 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1236 for (i = 0; i < bmap->n_div; ++i) {
1237 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1238 continue;
1239 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1240 continue;
1241 if (!better_div_constraint(bmap, i, k))
1242 continue;
1243 if (!ok_to_set_div_from_bound(bmap, i, k))
1244 break;
1245 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1246 bmap = set_div_from_lower_bound(bmap, i, k);
1247 else
1248 bmap = set_div_from_lower_bound(bmap, i, l);
1249 if (progress)
1250 *progress = 1;
1251 break;
1253 return bmap;
1256 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1257 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1259 struct isl_constraint_index ci;
1260 int k, l, h;
1261 unsigned total = isl_basic_map_total_dim(bmap);
1262 isl_int sum;
1264 if (!bmap || bmap->n_ineq <= 1)
1265 return bmap;
1267 if (create_constraint_index(&ci, bmap) < 0)
1268 return bmap;
1270 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1271 ci.index[h] = &bmap->ineq[0];
1272 for (k = 1; k < bmap->n_ineq; ++k) {
1273 h = hash_index(&ci, bmap, k);
1274 if (!ci.index[h]) {
1275 ci.index[h] = &bmap->ineq[k];
1276 continue;
1278 if (progress)
1279 *progress = 1;
1280 l = ci.index[h] - &bmap->ineq[0];
1281 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1282 swap_inequality(bmap, k, l);
1283 isl_basic_map_drop_inequality(bmap, k);
1284 --k;
1286 isl_int_init(sum);
1287 for (k = 0; k < bmap->n_ineq-1; ++k) {
1288 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1289 h = hash_index(&ci, bmap, k);
1290 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1291 if (!ci.index[h])
1292 continue;
1293 l = ci.index[h] - &bmap->ineq[0];
1294 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1295 if (isl_int_is_pos(sum)) {
1296 if (detect_divs)
1297 bmap = check_for_div_constraints(bmap, k, l,
1298 sum, progress);
1299 continue;
1301 if (isl_int_is_zero(sum)) {
1302 /* We need to break out of the loop after these
1303 * changes since the contents of the hash
1304 * will no longer be valid.
1305 * Plus, we probably we want to regauss first.
1307 if (progress)
1308 *progress = 1;
1309 isl_basic_map_drop_inequality(bmap, l);
1310 isl_basic_map_inequality_to_equality(bmap, k);
1311 } else
1312 bmap = isl_basic_map_set_to_empty(bmap);
1313 break;
1315 isl_int_clear(sum);
1317 constraint_index_free(&ci);
1318 return bmap;
1321 /* Detect all pairs of inequalities that form an equality.
1323 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1324 * Call it repeatedly while it is making progress.
1326 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1327 __isl_take isl_basic_map *bmap, int *progress)
1329 int duplicate;
1331 do {
1332 duplicate = 0;
1333 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1334 &duplicate, 0);
1335 if (progress && duplicate)
1336 *progress = 1;
1337 } while (duplicate);
1339 return bmap;
1342 /* Eliminate knowns divs from constraints where they appear with
1343 * a (positive or negative) unit coefficient.
1345 * That is, replace
1347 * floor(e/m) + f >= 0
1349 * by
1351 * e + m f >= 0
1353 * and
1355 * -floor(e/m) + f >= 0
1357 * by
1359 * -e + m f + m - 1 >= 0
1361 * The first conversion is valid because floor(e/m) >= -f is equivalent
1362 * to e/m >= -f because -f is an integral expression.
1363 * The second conversion follows from the fact that
1365 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1368 * Note that one of the div constraints may have been eliminated
1369 * due to being redundant with respect to the constraint that is
1370 * being modified by this function. The modified constraint may
1371 * no longer imply this div constraint, so we add it back to make
1372 * sure we do not lose any information.
1374 * We skip integral divs, i.e., those with denominator 1, as we would
1375 * risk eliminating the div from the div constraints. We do not need
1376 * to handle those divs here anyway since the div constraints will turn
1377 * out to form an equality and this equality can then be use to eliminate
1378 * the div from all constraints.
1380 static __isl_give isl_basic_map *eliminate_unit_divs(
1381 __isl_take isl_basic_map *bmap, int *progress)
1383 int i, j;
1384 isl_ctx *ctx;
1385 unsigned total;
1387 if (!bmap)
1388 return NULL;
1390 ctx = isl_basic_map_get_ctx(bmap);
1391 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1393 for (i = 0; i < bmap->n_div; ++i) {
1394 if (isl_int_is_zero(bmap->div[i][0]))
1395 continue;
1396 if (isl_int_is_one(bmap->div[i][0]))
1397 continue;
1398 for (j = 0; j < bmap->n_ineq; ++j) {
1399 int s;
1401 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1402 !isl_int_is_negone(bmap->ineq[j][total + i]))
1403 continue;
1405 *progress = 1;
1407 s = isl_int_sgn(bmap->ineq[j][total + i]);
1408 isl_int_set_si(bmap->ineq[j][total + i], 0);
1409 if (s < 0)
1410 isl_seq_combine(bmap->ineq[j],
1411 ctx->negone, bmap->div[i] + 1,
1412 bmap->div[i][0], bmap->ineq[j],
1413 total + bmap->n_div);
1414 else
1415 isl_seq_combine(bmap->ineq[j],
1416 ctx->one, bmap->div[i] + 1,
1417 bmap->div[i][0], bmap->ineq[j],
1418 total + bmap->n_div);
1419 if (s < 0) {
1420 isl_int_add(bmap->ineq[j][0],
1421 bmap->ineq[j][0], bmap->div[i][0]);
1422 isl_int_sub_ui(bmap->ineq[j][0],
1423 bmap->ineq[j][0], 1);
1426 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1427 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1428 return isl_basic_map_free(bmap);
1432 return bmap;
1435 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1437 int progress = 1;
1438 if (!bmap)
1439 return NULL;
1440 while (progress) {
1441 progress = 0;
1442 if (!bmap)
1443 break;
1444 if (isl_basic_map_plain_is_empty(bmap))
1445 break;
1446 bmap = isl_basic_map_normalize_constraints(bmap);
1447 bmap = remove_independent_vars_from_divs(bmap);
1448 bmap = normalize_div_expressions(bmap);
1449 bmap = remove_duplicate_divs(bmap, &progress);
1450 bmap = eliminate_unit_divs(bmap, &progress);
1451 bmap = eliminate_divs_eq(bmap, &progress);
1452 bmap = eliminate_divs_ineq(bmap, &progress);
1453 bmap = isl_basic_map_gauss(bmap, &progress);
1454 /* requires equalities in normal form */
1455 bmap = normalize_divs(bmap, &progress);
1456 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1457 &progress, 1);
1458 if (bmap && progress)
1459 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1461 return bmap;
1464 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1466 return (struct isl_basic_set *)
1467 isl_basic_map_simplify((struct isl_basic_map *)bset);
1471 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1472 isl_int *constraint, unsigned div)
1474 unsigned pos;
1476 if (!bmap)
1477 return -1;
1479 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1481 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1482 int neg;
1483 isl_int_sub(bmap->div[div][1],
1484 bmap->div[div][1], bmap->div[div][0]);
1485 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1486 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1487 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1488 isl_int_add(bmap->div[div][1],
1489 bmap->div[div][1], bmap->div[div][0]);
1490 if (!neg)
1491 return 0;
1492 if (isl_seq_first_non_zero(constraint+pos+1,
1493 bmap->n_div-div-1) != -1)
1494 return 0;
1495 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1496 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1497 return 0;
1498 if (isl_seq_first_non_zero(constraint+pos+1,
1499 bmap->n_div-div-1) != -1)
1500 return 0;
1501 } else
1502 return 0;
1504 return 1;
1507 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1508 isl_int *constraint, unsigned div)
1510 return isl_basic_map_is_div_constraint(bset, constraint, div);
1514 /* If the only constraints a div d=floor(f/m)
1515 * appears in are its two defining constraints
1517 * f - m d >=0
1518 * -(f - (m - 1)) + m d >= 0
1520 * then it can safely be removed.
1522 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1524 int i;
1525 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1527 for (i = 0; i < bmap->n_eq; ++i)
1528 if (!isl_int_is_zero(bmap->eq[i][pos]))
1529 return 0;
1531 for (i = 0; i < bmap->n_ineq; ++i) {
1532 if (isl_int_is_zero(bmap->ineq[i][pos]))
1533 continue;
1534 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1535 return 0;
1538 for (i = 0; i < bmap->n_div; ++i) {
1539 if (isl_int_is_zero(bmap->div[i][0]))
1540 continue;
1541 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1542 return 0;
1545 return 1;
1549 * Remove divs that don't occur in any of the constraints or other divs.
1550 * These can arise when dropping constraints from a basic map or
1551 * when the divs of a basic map have been temporarily aligned
1552 * with the divs of another basic map.
1554 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1556 int i;
1558 if (!bmap)
1559 return NULL;
1561 for (i = bmap->n_div-1; i >= 0; --i) {
1562 if (!div_is_redundant(bmap, i))
1563 continue;
1564 bmap = isl_basic_map_drop_div(bmap, i);
1566 return bmap;
1569 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1571 bmap = remove_redundant_divs(bmap);
1572 if (!bmap)
1573 return NULL;
1574 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1575 return bmap;
1578 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1580 return (struct isl_basic_set *)
1581 isl_basic_map_finalize((struct isl_basic_map *)bset);
1584 struct isl_set *isl_set_finalize(struct isl_set *set)
1586 int i;
1588 if (!set)
1589 return NULL;
1590 for (i = 0; i < set->n; ++i) {
1591 set->p[i] = isl_basic_set_finalize(set->p[i]);
1592 if (!set->p[i])
1593 goto error;
1595 return set;
1596 error:
1597 isl_set_free(set);
1598 return NULL;
1601 struct isl_map *isl_map_finalize(struct isl_map *map)
1603 int i;
1605 if (!map)
1606 return NULL;
1607 for (i = 0; i < map->n; ++i) {
1608 map->p[i] = isl_basic_map_finalize(map->p[i]);
1609 if (!map->p[i])
1610 goto error;
1612 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1613 return map;
1614 error:
1615 isl_map_free(map);
1616 return NULL;
1620 /* Remove definition of any div that is defined in terms of the given variable.
1621 * The div itself is not removed. Functions such as
1622 * eliminate_divs_ineq depend on the other divs remaining in place.
1624 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1625 int pos)
1627 int i;
1629 if (!bmap)
1630 return NULL;
1632 for (i = 0; i < bmap->n_div; ++i) {
1633 if (isl_int_is_zero(bmap->div[i][0]))
1634 continue;
1635 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1636 continue;
1637 isl_int_set_si(bmap->div[i][0], 0);
1639 return bmap;
1642 /* Eliminate the specified variables from the constraints using
1643 * Fourier-Motzkin. The variables themselves are not removed.
1645 struct isl_basic_map *isl_basic_map_eliminate_vars(
1646 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1648 int d;
1649 int i, j, k;
1650 unsigned total;
1651 int need_gauss = 0;
1653 if (n == 0)
1654 return bmap;
1655 if (!bmap)
1656 return NULL;
1657 total = isl_basic_map_total_dim(bmap);
1659 bmap = isl_basic_map_cow(bmap);
1660 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1661 bmap = remove_dependent_vars(bmap, d);
1662 if (!bmap)
1663 return NULL;
1665 for (d = pos + n - 1;
1666 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1667 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1668 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1669 int n_lower, n_upper;
1670 if (!bmap)
1671 return NULL;
1672 for (i = 0; i < bmap->n_eq; ++i) {
1673 if (isl_int_is_zero(bmap->eq[i][1+d]))
1674 continue;
1675 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1676 isl_basic_map_drop_equality(bmap, i);
1677 need_gauss = 1;
1678 break;
1680 if (i < bmap->n_eq)
1681 continue;
1682 n_lower = 0;
1683 n_upper = 0;
1684 for (i = 0; i < bmap->n_ineq; ++i) {
1685 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1686 n_lower++;
1687 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1688 n_upper++;
1690 bmap = isl_basic_map_extend_constraints(bmap,
1691 0, n_lower * n_upper);
1692 if (!bmap)
1693 goto error;
1694 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1695 int last;
1696 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1697 continue;
1698 last = -1;
1699 for (j = 0; j < i; ++j) {
1700 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1701 continue;
1702 last = j;
1703 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1704 isl_int_sgn(bmap->ineq[j][1+d]))
1705 continue;
1706 k = isl_basic_map_alloc_inequality(bmap);
1707 if (k < 0)
1708 goto error;
1709 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1710 1+total);
1711 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1712 1+d, 1+total, NULL);
1714 isl_basic_map_drop_inequality(bmap, i);
1715 i = last + 1;
1717 if (n_lower > 0 && n_upper > 0) {
1718 bmap = isl_basic_map_normalize_constraints(bmap);
1719 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1720 NULL, 0);
1721 bmap = isl_basic_map_gauss(bmap, NULL);
1722 bmap = isl_basic_map_remove_redundancies(bmap);
1723 need_gauss = 0;
1724 if (!bmap)
1725 goto error;
1726 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1727 break;
1730 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1731 if (need_gauss)
1732 bmap = isl_basic_map_gauss(bmap, NULL);
1733 return bmap;
1734 error:
1735 isl_basic_map_free(bmap);
1736 return NULL;
1739 struct isl_basic_set *isl_basic_set_eliminate_vars(
1740 struct isl_basic_set *bset, unsigned pos, unsigned n)
1742 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1743 (struct isl_basic_map *)bset, pos, n);
1746 /* Eliminate the specified n dimensions starting at first from the
1747 * constraints, without removing the dimensions from the space.
1748 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1749 * Otherwise, they are projected out and the original space is restored.
1751 __isl_give isl_basic_map *isl_basic_map_eliminate(
1752 __isl_take isl_basic_map *bmap,
1753 enum isl_dim_type type, unsigned first, unsigned n)
1755 isl_space *space;
1757 if (!bmap)
1758 return NULL;
1759 if (n == 0)
1760 return bmap;
1762 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1763 isl_die(bmap->ctx, isl_error_invalid,
1764 "index out of bounds", goto error);
1766 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1767 first += isl_basic_map_offset(bmap, type) - 1;
1768 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1769 return isl_basic_map_finalize(bmap);
1772 space = isl_basic_map_get_space(bmap);
1773 bmap = isl_basic_map_project_out(bmap, type, first, n);
1774 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1775 bmap = isl_basic_map_reset_space(bmap, space);
1776 return bmap;
1777 error:
1778 isl_basic_map_free(bmap);
1779 return NULL;
1782 __isl_give isl_basic_set *isl_basic_set_eliminate(
1783 __isl_take isl_basic_set *bset,
1784 enum isl_dim_type type, unsigned first, unsigned n)
1786 return isl_basic_map_eliminate(bset, type, first, n);
1789 /* Don't assume equalities are in order, because align_divs
1790 * may have changed the order of the divs.
1792 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1794 int d, i;
1795 unsigned total;
1797 total = isl_space_dim(bmap->dim, isl_dim_all);
1798 for (d = 0; d < total; ++d)
1799 elim[d] = -1;
1800 for (i = 0; i < bmap->n_eq; ++i) {
1801 for (d = total - 1; d >= 0; --d) {
1802 if (isl_int_is_zero(bmap->eq[i][1+d]))
1803 continue;
1804 elim[d] = i;
1805 break;
1810 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1812 compute_elimination_index((struct isl_basic_map *)bset, elim);
1815 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1816 struct isl_basic_map *bmap, int *elim)
1818 int d;
1819 int copied = 0;
1820 unsigned total;
1822 total = isl_space_dim(bmap->dim, isl_dim_all);
1823 for (d = total - 1; d >= 0; --d) {
1824 if (isl_int_is_zero(src[1+d]))
1825 continue;
1826 if (elim[d] == -1)
1827 continue;
1828 if (!copied) {
1829 isl_seq_cpy(dst, src, 1 + total);
1830 copied = 1;
1832 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1834 return copied;
1837 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1838 struct isl_basic_set *bset, int *elim)
1840 return reduced_using_equalities(dst, src,
1841 (struct isl_basic_map *)bset, elim);
1844 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1845 struct isl_basic_set *bset, struct isl_basic_set *context)
1847 int i;
1848 int *elim;
1850 if (!bset || !context)
1851 goto error;
1853 if (context->n_eq == 0) {
1854 isl_basic_set_free(context);
1855 return bset;
1858 bset = isl_basic_set_cow(bset);
1859 if (!bset)
1860 goto error;
1862 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1863 if (!elim)
1864 goto error;
1865 set_compute_elimination_index(context, elim);
1866 for (i = 0; i < bset->n_eq; ++i)
1867 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1868 context, elim);
1869 for (i = 0; i < bset->n_ineq; ++i)
1870 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1871 context, elim);
1872 isl_basic_set_free(context);
1873 free(elim);
1874 bset = isl_basic_set_simplify(bset);
1875 bset = isl_basic_set_finalize(bset);
1876 return bset;
1877 error:
1878 isl_basic_set_free(bset);
1879 isl_basic_set_free(context);
1880 return NULL;
1883 static struct isl_basic_set *remove_shifted_constraints(
1884 struct isl_basic_set *bset, struct isl_basic_set *context)
1886 struct isl_constraint_index ci;
1887 int k, h, l;
1889 if (!bset || !context)
1890 return bset;
1892 if (context->n_ineq == 0)
1893 return bset;
1894 if (setup_constraint_index(&ci, context) < 0)
1895 return bset;
1897 for (k = 0; k < bset->n_ineq; ++k) {
1898 h = set_hash_index(&ci, bset, k);
1899 if (!ci.index[h])
1900 continue;
1901 l = ci.index[h] - &context->ineq[0];
1902 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1903 continue;
1904 bset = isl_basic_set_cow(bset);
1905 if (!bset)
1906 goto error;
1907 isl_basic_set_drop_inequality(bset, k);
1908 --k;
1910 constraint_index_free(&ci);
1911 return bset;
1912 error:
1913 constraint_index_free(&ci);
1914 return bset;
1917 /* Remove constraints from "bmap" that are identical to constraints
1918 * in "context" or that are more relaxed (greater constant term).
1920 * We perform the test for shifted copies on the pure constraints
1921 * in remove_shifted_constraints.
1923 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1924 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1926 isl_basic_set *bset, *bset_context;
1928 if (!bmap || !context)
1929 goto error;
1931 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1932 isl_basic_map_free(context);
1933 return bmap;
1936 context = isl_basic_map_align_divs(context, bmap);
1937 bmap = isl_basic_map_align_divs(bmap, context);
1939 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1940 bset_context = isl_basic_map_underlying_set(context);
1941 bset = remove_shifted_constraints(bset, bset_context);
1942 isl_basic_set_free(bset_context);
1944 bmap = isl_basic_map_overlying_set(bset, bmap);
1946 return bmap;
1947 error:
1948 isl_basic_map_free(bmap);
1949 isl_basic_map_free(context);
1950 return NULL;
1953 /* Does the (linear part of a) constraint "c" involve any of the "len"
1954 * "relevant" dimensions?
1956 static int is_related(isl_int *c, int len, int *relevant)
1958 int i;
1960 for (i = 0; i < len; ++i) {
1961 if (!relevant[i])
1962 continue;
1963 if (!isl_int_is_zero(c[i]))
1964 return 1;
1967 return 0;
1970 /* Drop constraints from "bset" that do not involve any of
1971 * the dimensions marked "relevant".
1973 static __isl_give isl_basic_set *drop_unrelated_constraints(
1974 __isl_take isl_basic_set *bset, int *relevant)
1976 int i, dim;
1978 dim = isl_basic_set_dim(bset, isl_dim_set);
1979 for (i = 0; i < dim; ++i)
1980 if (!relevant[i])
1981 break;
1982 if (i >= dim)
1983 return bset;
1985 for (i = bset->n_eq - 1; i >= 0; --i)
1986 if (!is_related(bset->eq[i] + 1, dim, relevant))
1987 isl_basic_set_drop_equality(bset, i);
1989 for (i = bset->n_ineq - 1; i >= 0; --i)
1990 if (!is_related(bset->ineq[i] + 1, dim, relevant))
1991 isl_basic_set_drop_inequality(bset, i);
1993 return bset;
1996 /* Update the groups in "group" based on the (linear part of a) constraint "c".
1998 * In particular, for any variable involved in the constraint,
1999 * find the actual group id from before and replace the group
2000 * of the corresponding variable by the minimal group of all
2001 * the variables involved in the constraint considered so far
2002 * (if this minimum is smaller) or replace the minimum by this group
2003 * (if the minimum is larger).
2005 * At the end, all the variables in "c" will (indirectly) point
2006 * to the minimal of the groups that they referred to originally.
2008 static void update_groups(int dim, int *group, isl_int *c)
2010 int j;
2011 int min = dim;
2013 for (j = 0; j < dim; ++j) {
2014 if (isl_int_is_zero(c[j]))
2015 continue;
2016 while (group[j] >= 0 && group[group[j]] != group[j])
2017 group[j] = group[group[j]];
2018 if (group[j] == min)
2019 continue;
2020 if (group[j] < min) {
2021 if (min >= 0 && min < dim)
2022 group[min] = group[j];
2023 min = group[j];
2024 } else
2025 group[group[j]] = min;
2029 /* Drop constraints from "context" that are irrelevant for computing
2030 * the gist of "bset".
2032 * In particular, drop constraints in variables that are not related
2033 * to any of the variables involved in the constraints of "bset"
2034 * in the sense that there is no sequence of constraints that connects them.
2036 * We construct groups of variables that collect variables that
2037 * (indirectly) appear in some common constraint of "context".
2038 * Each group is identified by the first variable in the group,
2039 * except for the special group of variables that appear in "bset"
2040 * (or are related to those variables), which is identified by -1.
2041 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2042 * otherwise the group of i is the group of group[i].
2044 * We first initialize the -1 group with the variables that appear in "bset".
2045 * Then we initialize groups for the remaining variables.
2046 * Then we iterate over the constraints of "context" and update the
2047 * group of the variables in the constraint by the smallest group.
2048 * Finally, we resolve indirect references to groups by running over
2049 * the variables.
2051 * After computing the groups, we drop constraints that do not involve
2052 * any variables in the -1 group.
2054 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2055 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2057 isl_ctx *ctx;
2058 int *group;
2059 int dim;
2060 int i, j;
2061 int last;
2063 if (!context || !bset)
2064 return isl_basic_set_free(context);
2066 dim = isl_basic_set_dim(bset, isl_dim_set);
2067 ctx = isl_basic_set_get_ctx(bset);
2068 group = isl_calloc_array(ctx, int, dim);
2070 if (!group)
2071 goto error;
2073 for (i = 0; i < dim; ++i) {
2074 for (j = 0; j < bset->n_eq; ++j)
2075 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2076 break;
2077 if (j < bset->n_eq) {
2078 group[i] = -1;
2079 continue;
2081 for (j = 0; j < bset->n_ineq; ++j)
2082 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2083 break;
2084 if (j < bset->n_ineq)
2085 group[i] = -1;
2088 last = -1;
2089 for (i = 0; i < dim; ++i)
2090 if (group[i] >= 0)
2091 last = group[i] = i;
2092 if (last < 0) {
2093 free(group);
2094 return context;
2097 for (i = 0; i < context->n_eq; ++i)
2098 update_groups(dim, group, context->eq[i] + 1);
2099 for (i = 0; i < context->n_ineq; ++i)
2100 update_groups(dim, group, context->ineq[i] + 1);
2102 for (i = 0; i < dim; ++i)
2103 if (group[i] >= 0)
2104 group[i] = group[group[i]];
2106 for (i = 0; i < dim; ++i)
2107 group[i] = group[i] == -1;
2109 context = drop_unrelated_constraints(context, group);
2111 free(group);
2112 return context;
2113 error:
2114 free(group);
2115 return isl_basic_set_free(context);
2118 /* Remove all information from bset that is redundant in the context
2119 * of context. Both bset and context are assumed to be full-dimensional.
2121 * We first remove the inequalities from "bset"
2122 * that are obviously redundant with respect to some inequality in "context".
2123 * Then we remove those constraints from "context" that have become
2124 * irrelevant for computing the gist of "bset".
2125 * Note that this removal of constraints cannot be replaced by
2126 * a factorization because factors in "bset" may still be connected
2127 * to each other through constraints in "context".
2129 * If there are any inequalities left, we construct a tableau for
2130 * the context and then add the inequalities of "bset".
2131 * Before adding these inequalities, we freeze all constraints such that
2132 * they won't be considered redundant in terms of the constraints of "bset".
2133 * Then we detect all redundant constraints (among the
2134 * constraints that weren't frozen), first by checking for redundancy in the
2135 * the tableau and then by checking if replacing a constraint by its negation
2136 * would lead to an empty set. This last step is fairly expensive
2137 * and could be optimized by more reuse of the tableau.
2138 * Finally, we update bset according to the results.
2140 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2141 __isl_take isl_basic_set *context)
2143 int i, k;
2144 isl_basic_set *combined = NULL;
2145 struct isl_tab *tab = NULL;
2146 unsigned context_ineq;
2147 unsigned total;
2149 if (!bset || !context)
2150 goto error;
2152 if (isl_basic_set_is_universe(bset)) {
2153 isl_basic_set_free(context);
2154 return bset;
2157 if (isl_basic_set_is_universe(context)) {
2158 isl_basic_set_free(context);
2159 return bset;
2162 bset = remove_shifted_constraints(bset, context);
2163 if (!bset)
2164 goto error;
2165 if (bset->n_ineq == 0)
2166 goto done;
2168 context = drop_irrelevant_constraints(context, bset);
2169 if (!context)
2170 goto error;
2171 if (isl_basic_set_is_universe(context)) {
2172 isl_basic_set_free(context);
2173 return bset;
2176 context_ineq = context->n_ineq;
2177 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2178 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2179 tab = isl_tab_from_basic_set(combined, 0);
2180 for (i = 0; i < context_ineq; ++i)
2181 if (isl_tab_freeze_constraint(tab, i) < 0)
2182 goto error;
2183 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2184 goto error;
2185 for (i = 0; i < bset->n_ineq; ++i)
2186 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
2187 goto error;
2188 bset = isl_basic_set_add_constraints(combined, bset, 0);
2189 combined = NULL;
2190 if (!bset)
2191 goto error;
2192 if (isl_tab_detect_redundant(tab) < 0)
2193 goto error;
2194 total = isl_basic_set_total_dim(bset);
2195 for (i = context_ineq; i < bset->n_ineq; ++i) {
2196 int is_empty;
2197 if (tab->con[i].is_redundant)
2198 continue;
2199 tab->con[i].is_redundant = 1;
2200 combined = isl_basic_set_dup(bset);
2201 combined = isl_basic_set_update_from_tab(combined, tab);
2202 combined = isl_basic_set_extend_constraints(combined, 0, 1);
2203 k = isl_basic_set_alloc_inequality(combined);
2204 if (k < 0)
2205 goto error;
2206 isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total);
2207 isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1);
2208 is_empty = isl_basic_set_is_empty(combined);
2209 if (is_empty < 0)
2210 goto error;
2211 isl_basic_set_free(combined);
2212 combined = NULL;
2213 if (!is_empty)
2214 tab->con[i].is_redundant = 0;
2216 for (i = 0; i < context_ineq; ++i)
2217 tab->con[i].is_redundant = 1;
2218 bset = isl_basic_set_update_from_tab(bset, tab);
2219 if (bset) {
2220 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2221 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2224 isl_tab_free(tab);
2225 done:
2226 bset = isl_basic_set_finalize(bset);
2227 isl_basic_set_free(context);
2228 return bset;
2229 error:
2230 isl_tab_free(tab);
2231 isl_basic_set_free(combined);
2232 isl_basic_set_free(context);
2233 isl_basic_set_free(bset);
2234 return NULL;
2237 /* Remove all information from bset that is redundant in the context
2238 * of context. In particular, equalities that are linear combinations
2239 * of those in context are removed. Then the inequalities that are
2240 * redundant in the context of the equalities and inequalities of
2241 * context are removed.
2243 * First of all, we drop those constraints from "context"
2244 * that are irrelevant for computing the gist of "bset".
2245 * Alternatively, we could factorize the intersection of "context" and "bset".
2247 * We first compute the integer affine hull of the intersection,
2248 * compute the gist inside this affine hull and then add back
2249 * those equalities that are not implied by the context.
2251 * If two constraints are mutually redundant, then uset_gist_full
2252 * will remove the second of those constraints. We therefore first
2253 * sort the constraints so that constraints not involving existentially
2254 * quantified variables are given precedence over those that do.
2255 * We have to perform this sorting before the variable compression,
2256 * because that may effect the order of the variables.
2258 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2259 __isl_take isl_basic_set *context)
2261 isl_mat *eq;
2262 isl_mat *T, *T2;
2263 isl_basic_set *aff;
2264 isl_basic_set *aff_context;
2265 unsigned total;
2267 if (!bset || !context)
2268 goto error;
2270 context = drop_irrelevant_constraints(context, bset);
2272 aff = isl_basic_set_copy(bset);
2273 aff = isl_basic_set_intersect(aff, isl_basic_set_copy(context));
2274 aff = isl_basic_set_affine_hull(aff);
2275 if (!aff)
2276 goto error;
2277 if (isl_basic_set_plain_is_empty(aff)) {
2278 isl_basic_set_free(bset);
2279 isl_basic_set_free(context);
2280 return aff;
2282 bset = isl_basic_set_sort_constraints(bset);
2283 if (aff->n_eq == 0) {
2284 isl_basic_set_free(aff);
2285 return uset_gist_full(bset, context);
2287 total = isl_basic_set_total_dim(bset);
2288 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2289 eq = isl_mat_cow(eq);
2290 T = isl_mat_variable_compression(eq, &T2);
2291 if (T && T->n_col == 0) {
2292 isl_mat_free(T);
2293 isl_mat_free(T2);
2294 isl_basic_set_free(context);
2295 isl_basic_set_free(aff);
2296 return isl_basic_set_set_to_empty(bset);
2299 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2301 bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
2302 context = isl_basic_set_preimage(context, T);
2304 bset = uset_gist_full(bset, context);
2305 bset = isl_basic_set_preimage(bset, T2);
2306 bset = isl_basic_set_intersect(bset, aff);
2307 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2309 if (bset) {
2310 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2311 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2314 return bset;
2315 error:
2316 isl_basic_set_free(bset);
2317 isl_basic_set_free(context);
2318 return NULL;
2321 /* Return a basic map that has the same intersection with "context" as "bmap"
2322 * and that is as "simple" as possible.
2324 * The core computation is performed on the pure constraints.
2325 * When we add back the meaning of the integer divisions, we need
2326 * to (re)introduce the div constraints. If we happen to have
2327 * discovered that some of these integer divisions are equal to
2328 * some affine combination of other variables, then these div
2329 * constraints may end up getting simplified in terms of the equalities,
2330 * resulting in extra inequalities on the other variables that
2331 * may have been removed already or that may not even have been
2332 * part of the input. We try and remove those constraints of
2333 * this form that are most obviously redundant with respect to
2334 * the context. We also remove those div constraints that are
2335 * redundant with respect to the other constraints in the result.
2337 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
2338 struct isl_basic_map *context)
2340 isl_basic_set *bset, *eq;
2341 isl_basic_map *eq_bmap;
2342 unsigned n_div, n_eq, n_ineq;
2344 if (!bmap || !context)
2345 goto error;
2347 if (isl_basic_map_is_universe(bmap)) {
2348 isl_basic_map_free(context);
2349 return bmap;
2351 if (isl_basic_map_plain_is_empty(context)) {
2352 isl_space *space = isl_basic_map_get_space(bmap);
2353 isl_basic_map_free(bmap);
2354 isl_basic_map_free(context);
2355 return isl_basic_map_universe(space);
2357 if (isl_basic_map_plain_is_empty(bmap)) {
2358 isl_basic_map_free(context);
2359 return bmap;
2362 bmap = isl_basic_map_remove_redundancies(bmap);
2363 context = isl_basic_map_remove_redundancies(context);
2364 if (!context)
2365 goto error;
2367 context = isl_basic_map_align_divs(context, bmap);
2368 bmap = isl_basic_map_align_divs(bmap, context);
2369 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2371 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
2372 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
2374 if (!bset || bset->n_eq == 0 || n_div == 0 ||
2375 isl_basic_set_plain_is_empty(bset)) {
2376 isl_basic_map_free(context);
2377 return isl_basic_map_overlying_set(bset, bmap);
2380 n_eq = bset->n_eq;
2381 n_ineq = bset->n_ineq;
2382 eq = isl_basic_set_copy(bset);
2383 eq = isl_basic_set_cow(eq);
2384 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
2385 eq = isl_basic_set_free(eq);
2386 if (isl_basic_set_free_equality(bset, n_eq) < 0)
2387 bset = isl_basic_set_free(bset);
2389 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
2390 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
2391 bmap = isl_basic_map_overlying_set(bset, bmap);
2392 bmap = isl_basic_map_intersect(bmap, eq_bmap);
2393 bmap = isl_basic_map_remove_redundancies(bmap);
2395 return bmap;
2396 error:
2397 isl_basic_map_free(bmap);
2398 isl_basic_map_free(context);
2399 return NULL;
2403 * Assumes context has no implicit divs.
2405 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
2406 __isl_take isl_basic_map *context)
2408 int i;
2410 if (!map || !context)
2411 goto error;
2413 if (isl_basic_map_plain_is_empty(context)) {
2414 isl_space *space = isl_map_get_space(map);
2415 isl_map_free(map);
2416 isl_basic_map_free(context);
2417 return isl_map_universe(space);
2420 context = isl_basic_map_remove_redundancies(context);
2421 map = isl_map_cow(map);
2422 if (!map || !context)
2423 goto error;
2424 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
2425 map = isl_map_compute_divs(map);
2426 if (!map)
2427 goto error;
2428 for (i = map->n - 1; i >= 0; --i) {
2429 map->p[i] = isl_basic_map_gist(map->p[i],
2430 isl_basic_map_copy(context));
2431 if (!map->p[i])
2432 goto error;
2433 if (isl_basic_map_plain_is_empty(map->p[i])) {
2434 isl_basic_map_free(map->p[i]);
2435 if (i != map->n - 1)
2436 map->p[i] = map->p[map->n - 1];
2437 map->n--;
2440 isl_basic_map_free(context);
2441 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2442 return map;
2443 error:
2444 isl_map_free(map);
2445 isl_basic_map_free(context);
2446 return NULL;
2449 /* Return a map that has the same intersection with "context" as "map"
2450 * and that is as "simple" as possible.
2452 * If "map" is already the universe, then we cannot make it any simpler.
2453 * Similarly, if "context" is the universe, then we cannot exploit it
2454 * to simplify "map"
2455 * If "map" and "context" are identical to each other, then we can
2456 * return the corresponding universe.
2458 * If none of these cases apply, we have to work a bit harder.
2459 * During this computation, we make use of a single disjunct context,
2460 * so if the original context consists of more than one disjunct
2461 * then we need to approximate the context by a single disjunct set.
2462 * Simply taking the simple hull may drop constraints that are
2463 * only implicitly available in each disjunct. We therefore also
2464 * look for constraints among those defining "map" that are valid
2465 * for the context. These can then be used to simplify away
2466 * the corresponding constraints in "map".
2468 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
2469 __isl_take isl_map *context)
2471 int equal;
2472 int is_universe;
2473 isl_basic_map *hull;
2475 is_universe = isl_map_plain_is_universe(map);
2476 if (is_universe >= 0 && !is_universe)
2477 is_universe = isl_map_plain_is_universe(context);
2478 if (is_universe < 0)
2479 goto error;
2480 if (is_universe) {
2481 isl_map_free(context);
2482 return map;
2485 equal = isl_map_plain_is_equal(map, context);
2486 if (equal < 0)
2487 goto error;
2488 if (equal) {
2489 isl_map *res = isl_map_universe(isl_map_get_space(map));
2490 isl_map_free(map);
2491 isl_map_free(context);
2492 return res;
2495 context = isl_map_compute_divs(context);
2496 if (!context)
2497 goto error;
2498 if (isl_map_n_basic_map(context) == 1) {
2499 hull = isl_map_simple_hull(context);
2500 } else {
2501 isl_ctx *ctx;
2502 isl_map_list *list;
2504 ctx = isl_map_get_ctx(map);
2505 list = isl_map_list_alloc(ctx, 2);
2506 list = isl_map_list_add(list, isl_map_copy(context));
2507 list = isl_map_list_add(list, isl_map_copy(map));
2508 hull = isl_map_unshifted_simple_hull_from_map_list(context,
2509 list);
2511 return isl_map_gist_basic_map(map, hull);
2512 error:
2513 isl_map_free(map);
2514 isl_map_free(context);
2515 return NULL;
2518 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
2519 __isl_take isl_map *context)
2521 return isl_map_align_params_map_map_and(map, context, &map_gist);
2524 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
2525 struct isl_basic_set *context)
2527 return (struct isl_basic_set *)isl_basic_map_gist(
2528 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
2531 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
2532 __isl_take isl_basic_set *context)
2534 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
2535 (struct isl_basic_map *)context);
2538 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
2539 __isl_take isl_basic_set *context)
2541 isl_space *space = isl_set_get_space(set);
2542 isl_basic_set *dom_context = isl_basic_set_universe(space);
2543 dom_context = isl_basic_set_intersect_params(dom_context, context);
2544 return isl_set_gist_basic_set(set, dom_context);
2547 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
2548 __isl_take isl_set *context)
2550 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
2551 (struct isl_map *)context);
2554 /* Compute the gist of "bmap" with respect to the constraints "context"
2555 * on the domain.
2557 __isl_give isl_basic_map *isl_basic_map_gist_domain(
2558 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
2560 isl_space *space = isl_basic_map_get_space(bmap);
2561 isl_basic_map *bmap_context = isl_basic_map_universe(space);
2563 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
2564 return isl_basic_map_gist(bmap, bmap_context);
2567 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
2568 __isl_take isl_set *context)
2570 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2571 map_context = isl_map_intersect_domain(map_context, context);
2572 return isl_map_gist(map, map_context);
2575 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
2576 __isl_take isl_set *context)
2578 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2579 map_context = isl_map_intersect_range(map_context, context);
2580 return isl_map_gist(map, map_context);
2583 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
2584 __isl_take isl_set *context)
2586 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2587 map_context = isl_map_intersect_params(map_context, context);
2588 return isl_map_gist(map, map_context);
2591 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
2592 __isl_take isl_set *context)
2594 return isl_map_gist_params(set, context);
2597 /* Quick check to see if two basic maps are disjoint.
2598 * In particular, we reduce the equalities and inequalities of
2599 * one basic map in the context of the equalities of the other
2600 * basic map and check if we get a contradiction.
2602 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
2603 __isl_keep isl_basic_map *bmap2)
2605 struct isl_vec *v = NULL;
2606 int *elim = NULL;
2607 unsigned total;
2608 int i;
2610 if (!bmap1 || !bmap2)
2611 return isl_bool_error;
2612 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
2613 return isl_bool_error);
2614 if (bmap1->n_div || bmap2->n_div)
2615 return isl_bool_false;
2616 if (!bmap1->n_eq && !bmap2->n_eq)
2617 return isl_bool_false;
2619 total = isl_space_dim(bmap1->dim, isl_dim_all);
2620 if (total == 0)
2621 return isl_bool_false;
2622 v = isl_vec_alloc(bmap1->ctx, 1 + total);
2623 if (!v)
2624 goto error;
2625 elim = isl_alloc_array(bmap1->ctx, int, total);
2626 if (!elim)
2627 goto error;
2628 compute_elimination_index(bmap1, elim);
2629 for (i = 0; i < bmap2->n_eq; ++i) {
2630 int reduced;
2631 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
2632 bmap1, elim);
2633 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
2634 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2635 goto disjoint;
2637 for (i = 0; i < bmap2->n_ineq; ++i) {
2638 int reduced;
2639 reduced = reduced_using_equalities(v->block.data,
2640 bmap2->ineq[i], bmap1, elim);
2641 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2642 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2643 goto disjoint;
2645 compute_elimination_index(bmap2, elim);
2646 for (i = 0; i < bmap1->n_ineq; ++i) {
2647 int reduced;
2648 reduced = reduced_using_equalities(v->block.data,
2649 bmap1->ineq[i], bmap2, elim);
2650 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2651 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2652 goto disjoint;
2654 isl_vec_free(v);
2655 free(elim);
2656 return isl_bool_false;
2657 disjoint:
2658 isl_vec_free(v);
2659 free(elim);
2660 return isl_bool_true;
2661 error:
2662 isl_vec_free(v);
2663 free(elim);
2664 return isl_bool_error;
2667 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
2668 __isl_keep isl_basic_set *bset2)
2670 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
2671 (struct isl_basic_map *)bset2);
2674 /* Are "map1" and "map2" obviously disjoint?
2676 * If one of them is empty or if they live in different spaces (ignoring
2677 * parameters), then they are clearly disjoint.
2679 * If they have different parameters, then we skip any further tests.
2681 * If they are obviously equal, but not obviously empty, then we will
2682 * not be able to detect if they are disjoint.
2684 * Otherwise we check if each basic map in "map1" is obviously disjoint
2685 * from each basic map in "map2".
2687 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
2688 __isl_keep isl_map *map2)
2690 int i, j;
2691 isl_bool disjoint;
2692 isl_bool intersect;
2693 isl_bool match;
2695 if (!map1 || !map2)
2696 return isl_bool_error;
2698 disjoint = isl_map_plain_is_empty(map1);
2699 if (disjoint < 0 || disjoint)
2700 return disjoint;
2702 disjoint = isl_map_plain_is_empty(map2);
2703 if (disjoint < 0 || disjoint)
2704 return disjoint;
2706 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
2707 map2->dim, isl_dim_in);
2708 if (match < 0 || !match)
2709 return match < 0 ? isl_bool_error : isl_bool_true;
2711 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
2712 map2->dim, isl_dim_out);
2713 if (match < 0 || !match)
2714 return match < 0 ? isl_bool_error : isl_bool_true;
2716 match = isl_space_match(map1->dim, isl_dim_param,
2717 map2->dim, isl_dim_param);
2718 if (match < 0 || !match)
2719 return match < 0 ? isl_bool_error : isl_bool_false;
2721 intersect = isl_map_plain_is_equal(map1, map2);
2722 if (intersect < 0 || intersect)
2723 return intersect < 0 ? isl_bool_error : isl_bool_false;
2725 for (i = 0; i < map1->n; ++i) {
2726 for (j = 0; j < map2->n; ++j) {
2727 isl_bool d = isl_basic_map_plain_is_disjoint(map1->p[i],
2728 map2->p[j]);
2729 if (d != isl_bool_true)
2730 return d;
2733 return isl_bool_true;
2736 /* Are "map1" and "map2" disjoint?
2738 * They are disjoint if they are "obviously disjoint" or if one of them
2739 * is empty. Otherwise, they are not disjoint if one of them is universal.
2740 * If none of these cases apply, we compute the intersection and see if
2741 * the result is empty.
2743 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
2745 isl_bool disjoint;
2746 isl_bool intersect;
2747 isl_map *test;
2749 disjoint = isl_map_plain_is_disjoint(map1, map2);
2750 if (disjoint < 0 || disjoint)
2751 return disjoint;
2753 disjoint = isl_map_is_empty(map1);
2754 if (disjoint < 0 || disjoint)
2755 return disjoint;
2757 disjoint = isl_map_is_empty(map2);
2758 if (disjoint < 0 || disjoint)
2759 return disjoint;
2761 intersect = isl_map_plain_is_universe(map1);
2762 if (intersect < 0 || intersect)
2763 return intersect < 0 ? isl_bool_error : isl_bool_false;
2765 intersect = isl_map_plain_is_universe(map2);
2766 if (intersect < 0 || intersect)
2767 return intersect < 0 ? isl_bool_error : isl_bool_false;
2769 test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
2770 disjoint = isl_map_is_empty(test);
2771 isl_map_free(test);
2773 return disjoint;
2776 /* Are "bmap1" and "bmap2" disjoint?
2778 * They are disjoint if they are "obviously disjoint" or if one of them
2779 * is empty. Otherwise, they are not disjoint if one of them is universal.
2780 * If none of these cases apply, we compute the intersection and see if
2781 * the result is empty.
2783 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
2784 __isl_keep isl_basic_map *bmap2)
2786 isl_bool disjoint;
2787 isl_bool intersect;
2788 isl_basic_map *test;
2790 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
2791 if (disjoint < 0 || disjoint)
2792 return disjoint;
2794 disjoint = isl_basic_map_is_empty(bmap1);
2795 if (disjoint < 0 || disjoint)
2796 return disjoint;
2798 disjoint = isl_basic_map_is_empty(bmap2);
2799 if (disjoint < 0 || disjoint)
2800 return disjoint;
2802 intersect = isl_basic_map_is_universe(bmap1);
2803 if (intersect < 0 || intersect)
2804 return intersect < 0 ? isl_bool_error : isl_bool_false;
2806 intersect = isl_basic_map_is_universe(bmap2);
2807 if (intersect < 0 || intersect)
2808 return intersect < 0 ? isl_bool_error : isl_bool_false;
2810 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
2811 isl_basic_map_copy(bmap2));
2812 disjoint = isl_basic_map_is_empty(test);
2813 isl_basic_map_free(test);
2815 return disjoint;
2818 /* Are "bset1" and "bset2" disjoint?
2820 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
2821 __isl_keep isl_basic_set *bset2)
2823 return isl_basic_map_is_disjoint(bset1, bset2);
2826 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
2827 __isl_keep isl_set *set2)
2829 return isl_map_plain_is_disjoint((struct isl_map *)set1,
2830 (struct isl_map *)set2);
2833 /* Are "set1" and "set2" disjoint?
2835 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
2837 return isl_map_is_disjoint(set1, set2);
2840 /* Check if we can combine a given div with lower bound l and upper
2841 * bound u with some other div and if so return that other div.
2842 * Otherwise return -1.
2844 * We first check that
2845 * - the bounds are opposites of each other (except for the constant
2846 * term)
2847 * - the bounds do not reference any other div
2848 * - no div is defined in terms of this div
2850 * Let m be the size of the range allowed on the div by the bounds.
2851 * That is, the bounds are of the form
2853 * e <= a <= e + m - 1
2855 * with e some expression in the other variables.
2856 * We look for another div b such that no third div is defined in terms
2857 * of this second div b and such that in any constraint that contains
2858 * a (except for the given lower and upper bound), also contains b
2859 * with a coefficient that is m times that of b.
2860 * That is, all constraints (execpt for the lower and upper bound)
2861 * are of the form
2863 * e + f (a + m b) >= 0
2865 * If so, we return b so that "a + m b" can be replaced by
2866 * a single div "c = a + m b".
2868 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
2869 unsigned div, unsigned l, unsigned u)
2871 int i, j;
2872 unsigned dim;
2873 int coalesce = -1;
2875 if (bmap->n_div <= 1)
2876 return -1;
2877 dim = isl_space_dim(bmap->dim, isl_dim_all);
2878 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
2879 return -1;
2880 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
2881 bmap->n_div - div - 1) != -1)
2882 return -1;
2883 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
2884 dim + bmap->n_div))
2885 return -1;
2887 for (i = 0; i < bmap->n_div; ++i) {
2888 if (isl_int_is_zero(bmap->div[i][0]))
2889 continue;
2890 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
2891 return -1;
2894 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2895 if (isl_int_is_neg(bmap->ineq[l][0])) {
2896 isl_int_sub(bmap->ineq[l][0],
2897 bmap->ineq[l][0], bmap->ineq[u][0]);
2898 bmap = isl_basic_map_copy(bmap);
2899 bmap = isl_basic_map_set_to_empty(bmap);
2900 isl_basic_map_free(bmap);
2901 return -1;
2903 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2904 for (i = 0; i < bmap->n_div; ++i) {
2905 if (i == div)
2906 continue;
2907 if (!pairs[i])
2908 continue;
2909 for (j = 0; j < bmap->n_div; ++j) {
2910 if (isl_int_is_zero(bmap->div[j][0]))
2911 continue;
2912 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
2913 break;
2915 if (j < bmap->n_div)
2916 continue;
2917 for (j = 0; j < bmap->n_ineq; ++j) {
2918 int valid;
2919 if (j == l || j == u)
2920 continue;
2921 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
2922 continue;
2923 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
2924 break;
2925 isl_int_mul(bmap->ineq[j][1 + dim + div],
2926 bmap->ineq[j][1 + dim + div],
2927 bmap->ineq[l][0]);
2928 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
2929 bmap->ineq[j][1 + dim + i]);
2930 isl_int_divexact(bmap->ineq[j][1 + dim + div],
2931 bmap->ineq[j][1 + dim + div],
2932 bmap->ineq[l][0]);
2933 if (!valid)
2934 break;
2936 if (j < bmap->n_ineq)
2937 continue;
2938 coalesce = i;
2939 break;
2941 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2942 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2943 return coalesce;
2946 /* Given a lower and an upper bound on div i, construct an inequality
2947 * that when nonnegative ensures that this pair of bounds always allows
2948 * for an integer value of the given div.
2949 * The lower bound is inequality l, while the upper bound is inequality u.
2950 * The constructed inequality is stored in ineq.
2951 * g, fl, fu are temporary scalars.
2953 * Let the upper bound be
2955 * -n_u a + e_u >= 0
2957 * and the lower bound
2959 * n_l a + e_l >= 0
2961 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2962 * We have
2964 * - f_u e_l <= f_u f_l g a <= f_l e_u
2966 * Since all variables are integer valued, this is equivalent to
2968 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2970 * If this interval is at least f_u f_l g, then it contains at least
2971 * one integer value for a.
2972 * That is, the test constraint is
2974 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2976 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
2977 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
2979 unsigned dim;
2980 dim = isl_space_dim(bmap->dim, isl_dim_all);
2982 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
2983 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
2984 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
2985 isl_int_neg(fu, fu);
2986 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
2987 1 + dim + bmap->n_div);
2988 isl_int_add(ineq[0], ineq[0], fl);
2989 isl_int_add(ineq[0], ineq[0], fu);
2990 isl_int_sub_ui(ineq[0], ineq[0], 1);
2991 isl_int_mul(g, g, fl);
2992 isl_int_mul(g, g, fu);
2993 isl_int_sub(ineq[0], ineq[0], g);
2996 /* Remove more kinds of divs that are not strictly needed.
2997 * In particular, if all pairs of lower and upper bounds on a div
2998 * are such that they allow at least one integer value of the div,
2999 * the we can eliminate the div using Fourier-Motzkin without
3000 * introducing any spurious solutions.
3002 static struct isl_basic_map *drop_more_redundant_divs(
3003 struct isl_basic_map *bmap, int *pairs, int n)
3005 struct isl_tab *tab = NULL;
3006 struct isl_vec *vec = NULL;
3007 unsigned dim;
3008 int remove = -1;
3009 isl_int g, fl, fu;
3011 isl_int_init(g);
3012 isl_int_init(fl);
3013 isl_int_init(fu);
3015 if (!bmap)
3016 goto error;
3018 dim = isl_space_dim(bmap->dim, isl_dim_all);
3019 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
3020 if (!vec)
3021 goto error;
3023 tab = isl_tab_from_basic_map(bmap, 0);
3025 while (n > 0) {
3026 int i, l, u;
3027 int best = -1;
3028 enum isl_lp_result res;
3030 for (i = 0; i < bmap->n_div; ++i) {
3031 if (!pairs[i])
3032 continue;
3033 if (best >= 0 && pairs[best] <= pairs[i])
3034 continue;
3035 best = i;
3038 i = best;
3039 for (l = 0; l < bmap->n_ineq; ++l) {
3040 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
3041 continue;
3042 for (u = 0; u < bmap->n_ineq; ++u) {
3043 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
3044 continue;
3045 construct_test_ineq(bmap, i, l, u,
3046 vec->el, g, fl, fu);
3047 res = isl_tab_min(tab, vec->el,
3048 bmap->ctx->one, &g, NULL, 0);
3049 if (res == isl_lp_error)
3050 goto error;
3051 if (res == isl_lp_empty) {
3052 bmap = isl_basic_map_set_to_empty(bmap);
3053 break;
3055 if (res != isl_lp_ok || isl_int_is_neg(g))
3056 break;
3058 if (u < bmap->n_ineq)
3059 break;
3061 if (l == bmap->n_ineq) {
3062 remove = i;
3063 break;
3065 pairs[i] = 0;
3066 --n;
3069 isl_tab_free(tab);
3070 isl_vec_free(vec);
3072 isl_int_clear(g);
3073 isl_int_clear(fl);
3074 isl_int_clear(fu);
3076 free(pairs);
3078 if (remove < 0)
3079 return bmap;
3081 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
3082 return isl_basic_map_drop_redundant_divs(bmap);
3083 error:
3084 free(pairs);
3085 isl_basic_map_free(bmap);
3086 isl_tab_free(tab);
3087 isl_vec_free(vec);
3088 isl_int_clear(g);
3089 isl_int_clear(fl);
3090 isl_int_clear(fu);
3091 return NULL;
3094 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
3095 * and the upper bound u, div1 always occurs together with div2 in the form
3096 * (div1 + m div2), where m is the constant range on the variable div1
3097 * allowed by l and u, replace the pair div1 and div2 by a single
3098 * div that is equal to div1 + m div2.
3100 * The new div will appear in the location that contains div2.
3101 * We need to modify all constraints that contain
3102 * div2 = (div - div1) / m
3103 * (If a constraint does not contain div2, it will also not contain div1.)
3104 * If the constraint also contains div1, then we know they appear
3105 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3106 * i.e., the coefficient of div is f.
3108 * Otherwise, we first need to introduce div1 into the constraint.
3109 * Let the l be
3111 * div1 + f >=0
3113 * and u
3115 * -div1 + f' >= 0
3117 * A lower bound on div2
3119 * n div2 + t >= 0
3121 * can be replaced by
3123 * (n * (m div 2 + div1) + m t + n f)/g >= 0
3125 * with g = gcd(m,n).
3126 * An upper bound
3128 * -n div2 + t >= 0
3130 * can be replaced by
3132 * (-n * (m div2 + div1) + m t + n f')/g >= 0
3134 * These constraint are those that we would obtain from eliminating
3135 * div1 using Fourier-Motzkin.
3137 * After all constraints have been modified, we drop the lower and upper
3138 * bound and then drop div1.
3140 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
3141 unsigned div1, unsigned div2, unsigned l, unsigned u)
3143 isl_int a;
3144 isl_int b;
3145 isl_int m;
3146 unsigned dim, total;
3147 int i;
3149 dim = isl_space_dim(bmap->dim, isl_dim_all);
3150 total = 1 + dim + bmap->n_div;
3152 isl_int_init(a);
3153 isl_int_init(b);
3154 isl_int_init(m);
3155 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
3156 isl_int_add_ui(m, m, 1);
3158 for (i = 0; i < bmap->n_ineq; ++i) {
3159 if (i == l || i == u)
3160 continue;
3161 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
3162 continue;
3163 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
3164 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
3165 isl_int_divexact(a, m, b);
3166 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
3167 if (isl_int_is_pos(b)) {
3168 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3169 b, bmap->ineq[l], total);
3170 } else {
3171 isl_int_neg(b, b);
3172 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3173 b, bmap->ineq[u], total);
3176 isl_int_set(bmap->ineq[i][1 + dim + div2],
3177 bmap->ineq[i][1 + dim + div1]);
3178 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
3181 isl_int_clear(a);
3182 isl_int_clear(b);
3183 isl_int_clear(m);
3184 if (l > u) {
3185 isl_basic_map_drop_inequality(bmap, l);
3186 isl_basic_map_drop_inequality(bmap, u);
3187 } else {
3188 isl_basic_map_drop_inequality(bmap, u);
3189 isl_basic_map_drop_inequality(bmap, l);
3191 bmap = isl_basic_map_drop_div(bmap, div1);
3192 return bmap;
3195 /* First check if we can coalesce any pair of divs and
3196 * then continue with dropping more redundant divs.
3198 * We loop over all pairs of lower and upper bounds on a div
3199 * with coefficient 1 and -1, respectively, check if there
3200 * is any other div "c" with which we can coalesce the div
3201 * and if so, perform the coalescing.
3203 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
3204 struct isl_basic_map *bmap, int *pairs, int n)
3206 int i, l, u;
3207 unsigned dim;
3209 dim = isl_space_dim(bmap->dim, isl_dim_all);
3211 for (i = 0; i < bmap->n_div; ++i) {
3212 if (!pairs[i])
3213 continue;
3214 for (l = 0; l < bmap->n_ineq; ++l) {
3215 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
3216 continue;
3217 for (u = 0; u < bmap->n_ineq; ++u) {
3218 int c;
3220 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
3221 continue;
3222 c = div_find_coalesce(bmap, pairs, i, l, u);
3223 if (c < 0)
3224 continue;
3225 free(pairs);
3226 bmap = coalesce_divs(bmap, i, c, l, u);
3227 return isl_basic_map_drop_redundant_divs(bmap);
3232 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3233 return bmap;
3235 return drop_more_redundant_divs(bmap, pairs, n);
3238 /* Remove divs that are not strictly needed.
3239 * In particular, if a div only occurs positively (or negatively)
3240 * in constraints, then it can simply be dropped.
3241 * Also, if a div occurs in only two constraints and if moreover
3242 * those two constraints are opposite to each other, except for the constant
3243 * term and if the sum of the constant terms is such that for any value
3244 * of the other values, there is always at least one integer value of the
3245 * div, i.e., if one plus this sum is greater than or equal to
3246 * the (absolute value) of the coefficent of the div in the constraints,
3247 * then we can also simply drop the div.
3249 * We skip divs that appear in equalities or in the definition of other divs.
3250 * Divs that appear in the definition of other divs usually occur in at least
3251 * 4 constraints, but the constraints may have been simplified.
3253 * If any divs are left after these simple checks then we move on
3254 * to more complicated cases in drop_more_redundant_divs.
3256 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
3257 struct isl_basic_map *bmap)
3259 int i, j;
3260 unsigned off;
3261 int *pairs = NULL;
3262 int n = 0;
3264 if (!bmap)
3265 goto error;
3266 if (bmap->n_div == 0)
3267 return bmap;
3269 off = isl_space_dim(bmap->dim, isl_dim_all);
3270 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
3271 if (!pairs)
3272 goto error;
3274 for (i = 0; i < bmap->n_div; ++i) {
3275 int pos, neg;
3276 int last_pos, last_neg;
3277 int redundant;
3278 int defined;
3280 defined = !isl_int_is_zero(bmap->div[i][0]);
3281 for (j = i; j < bmap->n_div; ++j)
3282 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
3283 break;
3284 if (j < bmap->n_div)
3285 continue;
3286 for (j = 0; j < bmap->n_eq; ++j)
3287 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
3288 break;
3289 if (j < bmap->n_eq)
3290 continue;
3291 ++n;
3292 pos = neg = 0;
3293 for (j = 0; j < bmap->n_ineq; ++j) {
3294 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
3295 last_pos = j;
3296 ++pos;
3298 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
3299 last_neg = j;
3300 ++neg;
3303 pairs[i] = pos * neg;
3304 if (pairs[i] == 0) {
3305 for (j = bmap->n_ineq - 1; j >= 0; --j)
3306 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
3307 isl_basic_map_drop_inequality(bmap, j);
3308 bmap = isl_basic_map_drop_div(bmap, i);
3309 free(pairs);
3310 return isl_basic_map_drop_redundant_divs(bmap);
3312 if (pairs[i] != 1)
3313 continue;
3314 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
3315 bmap->ineq[last_neg] + 1,
3316 off + bmap->n_div))
3317 continue;
3319 isl_int_add(bmap->ineq[last_pos][0],
3320 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3321 isl_int_add_ui(bmap->ineq[last_pos][0],
3322 bmap->ineq[last_pos][0], 1);
3323 redundant = isl_int_ge(bmap->ineq[last_pos][0],
3324 bmap->ineq[last_pos][1+off+i]);
3325 isl_int_sub_ui(bmap->ineq[last_pos][0],
3326 bmap->ineq[last_pos][0], 1);
3327 isl_int_sub(bmap->ineq[last_pos][0],
3328 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3329 if (!redundant) {
3330 if (defined ||
3331 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
3332 pairs[i] = 0;
3333 --n;
3334 continue;
3336 bmap = set_div_from_lower_bound(bmap, i, last_pos);
3337 bmap = isl_basic_map_simplify(bmap);
3338 free(pairs);
3339 return isl_basic_map_drop_redundant_divs(bmap);
3341 if (last_pos > last_neg) {
3342 isl_basic_map_drop_inequality(bmap, last_pos);
3343 isl_basic_map_drop_inequality(bmap, last_neg);
3344 } else {
3345 isl_basic_map_drop_inequality(bmap, last_neg);
3346 isl_basic_map_drop_inequality(bmap, last_pos);
3348 bmap = isl_basic_map_drop_div(bmap, i);
3349 free(pairs);
3350 return isl_basic_map_drop_redundant_divs(bmap);
3353 if (n > 0)
3354 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
3356 free(pairs);
3357 return bmap;
3358 error:
3359 free(pairs);
3360 isl_basic_map_free(bmap);
3361 return NULL;
3364 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
3365 struct isl_basic_set *bset)
3367 return (struct isl_basic_set *)
3368 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
3371 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
3373 int i;
3375 if (!map)
3376 return NULL;
3377 for (i = 0; i < map->n; ++i) {
3378 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
3379 if (!map->p[i])
3380 goto error;
3382 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3383 return map;
3384 error:
3385 isl_map_free(map);
3386 return NULL;
3389 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
3391 return (struct isl_set *)
3392 isl_map_drop_redundant_divs((struct isl_map *)set);
3395 /* Does "bmap" satisfy any equality that involves more than 2 variables
3396 * and/or has coefficients different from -1 and 1?
3398 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
3400 int i;
3401 unsigned total;
3403 total = isl_basic_map_dim(bmap, isl_dim_all);
3405 for (i = 0; i < bmap->n_eq; ++i) {
3406 int j, k;
3408 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
3409 if (j < 0)
3410 continue;
3411 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3412 !isl_int_is_negone(bmap->eq[i][1 + j]))
3413 return 1;
3415 j += 1;
3416 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3417 if (k < 0)
3418 continue;
3419 j += k;
3420 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3421 !isl_int_is_negone(bmap->eq[i][1 + j]))
3422 return 1;
3424 j += 1;
3425 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3426 if (k >= 0)
3427 return 1;
3430 return 0;
3433 /* Remove any common factor g from the constraint coefficients in "v".
3434 * The constant term is stored in the first position and is replaced
3435 * by floor(c/g). If any common factor is removed and if this results
3436 * in a tightening of the constraint, then set *tightened.
3438 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
3439 int *tightened)
3441 isl_ctx *ctx;
3443 if (!v)
3444 return NULL;
3445 ctx = isl_vec_get_ctx(v);
3446 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
3447 if (isl_int_is_zero(ctx->normalize_gcd))
3448 return v;
3449 if (isl_int_is_one(ctx->normalize_gcd))
3450 return v;
3451 v = isl_vec_cow(v);
3452 if (!v)
3453 return NULL;
3454 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
3455 *tightened = 1;
3456 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
3457 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
3458 v->size - 1);
3459 return v;
3462 /* If "bmap" is an integer set that satisfies any equality involving
3463 * more than 2 variables and/or has coefficients different from -1 and 1,
3464 * then use variable compression to reduce the coefficients by removing
3465 * any (hidden) common factor.
3466 * In particular, apply the variable compression to each constraint,
3467 * factor out any common factor in the non-constant coefficients and
3468 * then apply the inverse of the compression.
3469 * At the end, we mark the basic map as having reduced constants.
3470 * If this flag is still set on the next invocation of this function,
3471 * then we skip the computation.
3473 * Removing a common factor may result in a tightening of some of
3474 * the constraints. If this happens, then we may end up with two
3475 * opposite inequalities that can be replaced by an equality.
3476 * We therefore call isl_basic_map_detect_inequality_pairs,
3477 * which checks for such pairs of inequalities as well as eliminate_divs_eq
3478 * and isl_basic_map_gauss if such a pair was found.
3480 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
3481 __isl_take isl_basic_map *bmap)
3483 unsigned total;
3484 isl_ctx *ctx;
3485 isl_vec *v;
3486 isl_mat *eq, *T, *T2;
3487 int i;
3488 int tightened;
3490 if (!bmap)
3491 return NULL;
3492 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
3493 return bmap;
3494 if (isl_basic_map_is_rational(bmap))
3495 return bmap;
3496 if (bmap->n_eq == 0)
3497 return bmap;
3498 if (!has_multiple_var_equality(bmap))
3499 return bmap;
3501 total = isl_basic_map_dim(bmap, isl_dim_all);
3502 ctx = isl_basic_map_get_ctx(bmap);
3503 v = isl_vec_alloc(ctx, 1 + total);
3504 if (!v)
3505 return isl_basic_map_free(bmap);
3507 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3508 T = isl_mat_variable_compression(eq, &T2);
3509 if (!T || !T2)
3510 goto error;
3511 if (T->n_col == 0) {
3512 isl_mat_free(T);
3513 isl_mat_free(T2);
3514 isl_vec_free(v);
3515 return isl_basic_map_set_to_empty(bmap);
3518 tightened = 0;
3519 for (i = 0; i < bmap->n_ineq; ++i) {
3520 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
3521 v = isl_vec_mat_product(v, isl_mat_copy(T));
3522 v = normalize_constraint(v, &tightened);
3523 v = isl_vec_mat_product(v, isl_mat_copy(T2));
3524 if (!v)
3525 goto error;
3526 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
3529 isl_mat_free(T);
3530 isl_mat_free(T2);
3531 isl_vec_free(v);
3533 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
3535 if (tightened) {
3536 int progress = 0;
3538 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
3539 if (progress) {
3540 bmap = eliminate_divs_eq(bmap, &progress);
3541 bmap = isl_basic_map_gauss(bmap, NULL);
3545 return bmap;
3546 error:
3547 isl_mat_free(T);
3548 isl_mat_free(T2);
3549 isl_vec_free(v);
3550 return isl_basic_map_free(bmap);
3553 /* Shift the integer division at position "div" of "bmap"
3554 * by "shift" times the variable at position "pos".
3555 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
3556 * corresponds to the constant term.
3558 * That is, if the integer division has the form
3560 * floor(f(x)/d)
3562 * then replace it by
3564 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
3566 __isl_give isl_basic_map *isl_basic_map_shift_div(
3567 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
3569 int i;
3570 unsigned total;
3572 if (!bmap)
3573 return NULL;
3575 total = isl_basic_map_dim(bmap, isl_dim_all);
3576 total -= isl_basic_map_dim(bmap, isl_dim_div);
3578 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
3580 for (i = 0; i < bmap->n_eq; ++i) {
3581 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
3582 continue;
3583 isl_int_submul(bmap->eq[i][pos],
3584 shift, bmap->eq[i][1 + total + div]);
3586 for (i = 0; i < bmap->n_ineq; ++i) {
3587 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
3588 continue;
3589 isl_int_submul(bmap->ineq[i][pos],
3590 shift, bmap->ineq[i][1 + total + div]);
3592 for (i = 0; i < bmap->n_div; ++i) {
3593 if (isl_int_is_zero(bmap->div[i][0]))
3594 continue;
3595 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
3596 continue;
3597 isl_int_submul(bmap->div[i][1 + pos],
3598 shift, bmap->div[i][1 + 1 + total + div]);
3601 return bmap;