isl_map_simplify.c: hash_index: extract out hash_index_ineq
[isl.git] / isl_map_simplify.c
blob1a5c2c8e936dd94a3bc6d9277cd68cb65e43223c
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.
769 * "total" is the total dimension of the basic map.
771 struct isl_constraint_index {
772 unsigned int size;
773 int bits;
774 isl_int ***index;
775 unsigned total;
778 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
780 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
781 __isl_keep isl_basic_map *bmap)
783 isl_ctx *ctx;
785 ci->index = NULL;
786 if (!bmap)
787 return isl_stat_error;
788 ci->total = isl_basic_set_total_dim(bmap);
789 if (bmap->n_ineq == 0)
790 return isl_stat_ok;
791 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
792 ci->bits = ffs(ci->size) - 1;
793 ctx = isl_basic_map_get_ctx(bmap);
794 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
795 if (!ci->index)
796 return isl_stat_error;
798 return isl_stat_ok;
801 /* Free the memory allocated by create_constraint_index.
803 static void constraint_index_free(struct isl_constraint_index *ci)
805 free(ci->index);
808 /* Return the position in ci->index that contains the address of
809 * an inequality that is equal to *ineq up to the constant term,
810 * provided this address is not identical to "ineq".
811 * If there is no such inequality, then return the position where
812 * such an inequality should be inserted.
814 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
816 int h;
817 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
818 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
819 if (ineq != ci->index[h] &&
820 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
821 break;
822 return h;
825 /* Return the position in ci->index that contains the address of
826 * an inequality that is equal to the k'th inequality of "bmap"
827 * up to the constant term, provided it does not point to the very
828 * same inequality.
829 * If there is no such inequality, then return the position where
830 * such an inequality should be inserted.
832 static int hash_index(struct isl_constraint_index *ci,
833 __isl_keep isl_basic_map *bmap, int k)
835 return hash_index_ineq(ci, &bmap->ineq[k]);
838 static int set_hash_index(struct isl_constraint_index *ci,
839 struct isl_basic_set *bset, int k)
841 return hash_index(ci, bset, k);
844 /* Fill in the "ci" data structure with the inequalities of "bset".
846 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
847 __isl_keep isl_basic_set *bset)
849 int k, h;
851 if (create_constraint_index(ci, bset) < 0)
852 return isl_stat_error;
854 for (k = 0; k < bset->n_ineq; ++k) {
855 h = set_hash_index(ci, bset, k);
856 ci->index[h] = &bset->ineq[k];
859 return isl_stat_ok;
862 /* If we can eliminate more than one div, then we need to make
863 * sure we do it from last div to first div, in order not to
864 * change the position of the other divs that still need to
865 * be removed.
867 static struct isl_basic_map *remove_duplicate_divs(
868 struct isl_basic_map *bmap, int *progress)
870 unsigned int size;
871 int *index;
872 int *elim_for;
873 int k, l, h;
874 int bits;
875 struct isl_blk eq;
876 unsigned total_var;
877 unsigned total;
878 struct isl_ctx *ctx;
880 bmap = isl_basic_map_order_divs(bmap);
881 if (!bmap || bmap->n_div <= 1)
882 return bmap;
884 total_var = isl_space_dim(bmap->dim, isl_dim_all);
885 total = total_var + bmap->n_div;
887 ctx = bmap->ctx;
888 for (k = bmap->n_div - 1; k >= 0; --k)
889 if (!isl_int_is_zero(bmap->div[k][0]))
890 break;
891 if (k <= 0)
892 return bmap;
894 size = round_up(4 * bmap->n_div / 3 - 1);
895 if (size == 0)
896 return bmap;
897 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
898 bits = ffs(size) - 1;
899 index = isl_calloc_array(ctx, int, size);
900 if (!elim_for || !index)
901 goto out;
902 eq = isl_blk_alloc(ctx, 1+total);
903 if (isl_blk_is_error(eq))
904 goto out;
906 isl_seq_clr(eq.data, 1+total);
907 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
908 for (--k; k >= 0; --k) {
909 uint32_t hash;
911 if (isl_int_is_zero(bmap->div[k][0]))
912 continue;
914 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
915 for (h = hash; index[h]; h = (h+1) % size)
916 if (isl_seq_eq(bmap->div[k],
917 bmap->div[index[h]-1], 2+total))
918 break;
919 if (index[h]) {
920 *progress = 1;
921 l = index[h] - 1;
922 elim_for[l] = k + 1;
924 index[h] = k+1;
926 for (l = bmap->n_div - 1; l >= 0; --l) {
927 if (!elim_for[l])
928 continue;
929 k = elim_for[l] - 1;
930 isl_int_set_si(eq.data[1+total_var+k], -1);
931 isl_int_set_si(eq.data[1+total_var+l], 1);
932 bmap = eliminate_div(bmap, eq.data, l, 1);
933 if (!bmap)
934 break;
935 isl_int_set_si(eq.data[1+total_var+k], 0);
936 isl_int_set_si(eq.data[1+total_var+l], 0);
939 isl_blk_free(ctx, eq);
940 out:
941 free(index);
942 free(elim_for);
943 return bmap;
946 static int n_pure_div_eq(struct isl_basic_map *bmap)
948 int i, j;
949 unsigned total;
951 total = isl_space_dim(bmap->dim, isl_dim_all);
952 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
953 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
954 --j;
955 if (j < 0)
956 break;
957 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
958 return 0;
960 return i;
963 /* Normalize divs that appear in equalities.
965 * In particular, we assume that bmap contains some equalities
966 * of the form
968 * a x = m * e_i
970 * and we want to replace the set of e_i by a minimal set and
971 * such that the new e_i have a canonical representation in terms
972 * of the vector x.
973 * If any of the equalities involves more than one divs, then
974 * we currently simply bail out.
976 * Let us first additionally assume that all equalities involve
977 * a div. The equalities then express modulo constraints on the
978 * remaining variables and we can use "parameter compression"
979 * to find a minimal set of constraints. The result is a transformation
981 * x = T(x') = x_0 + G x'
983 * with G a lower-triangular matrix with all elements below the diagonal
984 * non-negative and smaller than the diagonal element on the same row.
985 * We first normalize x_0 by making the same property hold in the affine
986 * T matrix.
987 * The rows i of G with a 1 on the diagonal do not impose any modulo
988 * constraint and simply express x_i = x'_i.
989 * For each of the remaining rows i, we introduce a div and a corresponding
990 * equality. In particular
992 * g_ii e_j = x_i - g_i(x')
994 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
995 * corresponding div (if g_kk != 1).
997 * If there are any equalities not involving any div, then we
998 * first apply a variable compression on the variables x:
1000 * x = C x'' x'' = C_2 x
1002 * and perform the above parameter compression on A C instead of on A.
1003 * The resulting compression is then of the form
1005 * x'' = T(x') = x_0 + G x'
1007 * and in constructing the new divs and the corresponding equalities,
1008 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1009 * by the corresponding row from C_2.
1011 static struct isl_basic_map *normalize_divs(
1012 struct isl_basic_map *bmap, int *progress)
1014 int i, j, k;
1015 int total;
1016 int div_eq;
1017 struct isl_mat *B;
1018 struct isl_vec *d;
1019 struct isl_mat *T = NULL;
1020 struct isl_mat *C = NULL;
1021 struct isl_mat *C2 = NULL;
1022 isl_int v;
1023 int *pos;
1024 int dropped, needed;
1026 if (!bmap)
1027 return NULL;
1029 if (bmap->n_div == 0)
1030 return bmap;
1032 if (bmap->n_eq == 0)
1033 return bmap;
1035 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1036 return bmap;
1038 total = isl_space_dim(bmap->dim, isl_dim_all);
1039 div_eq = n_pure_div_eq(bmap);
1040 if (div_eq == 0)
1041 return bmap;
1043 if (div_eq < bmap->n_eq) {
1044 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1045 bmap->n_eq - div_eq, 0, 1 + total);
1046 C = isl_mat_variable_compression(B, &C2);
1047 if (!C || !C2)
1048 goto error;
1049 if (C->n_col == 0) {
1050 bmap = isl_basic_map_set_to_empty(bmap);
1051 isl_mat_free(C);
1052 isl_mat_free(C2);
1053 goto done;
1057 d = isl_vec_alloc(bmap->ctx, div_eq);
1058 if (!d)
1059 goto error;
1060 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1061 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1062 --j;
1063 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1065 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1067 if (C) {
1068 B = isl_mat_product(B, C);
1069 C = NULL;
1072 T = isl_mat_parameter_compression(B, d);
1073 if (!T)
1074 goto error;
1075 if (T->n_col == 0) {
1076 bmap = isl_basic_map_set_to_empty(bmap);
1077 isl_mat_free(C2);
1078 isl_mat_free(T);
1079 goto done;
1081 isl_int_init(v);
1082 for (i = 0; i < T->n_row - 1; ++i) {
1083 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1084 if (isl_int_is_zero(v))
1085 continue;
1086 isl_mat_col_submul(T, 0, v, 1 + i);
1088 isl_int_clear(v);
1089 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1090 if (!pos)
1091 goto error;
1092 /* We have to be careful because dropping equalities may reorder them */
1093 dropped = 0;
1094 for (j = bmap->n_div - 1; j >= 0; --j) {
1095 for (i = 0; i < bmap->n_eq; ++i)
1096 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1097 break;
1098 if (i < bmap->n_eq) {
1099 bmap = isl_basic_map_drop_div(bmap, j);
1100 isl_basic_map_drop_equality(bmap, i);
1101 ++dropped;
1104 pos[0] = 0;
1105 needed = 0;
1106 for (i = 1; i < T->n_row; ++i) {
1107 if (isl_int_is_one(T->row[i][i]))
1108 pos[i] = i;
1109 else
1110 needed++;
1112 if (needed > dropped) {
1113 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1114 needed, needed, 0);
1115 if (!bmap)
1116 goto error;
1118 for (i = 1; i < T->n_row; ++i) {
1119 if (isl_int_is_one(T->row[i][i]))
1120 continue;
1121 k = isl_basic_map_alloc_div(bmap);
1122 pos[i] = 1 + total + k;
1123 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1124 isl_int_set(bmap->div[k][0], T->row[i][i]);
1125 if (C2)
1126 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1127 else
1128 isl_int_set_si(bmap->div[k][1 + i], 1);
1129 for (j = 0; j < i; ++j) {
1130 if (isl_int_is_zero(T->row[i][j]))
1131 continue;
1132 if (pos[j] < T->n_row && C2)
1133 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1134 C2->row[pos[j]], 1 + total);
1135 else
1136 isl_int_neg(bmap->div[k][1 + pos[j]],
1137 T->row[i][j]);
1139 j = isl_basic_map_alloc_equality(bmap);
1140 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1141 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1143 free(pos);
1144 isl_mat_free(C2);
1145 isl_mat_free(T);
1147 if (progress)
1148 *progress = 1;
1149 done:
1150 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1152 return bmap;
1153 error:
1154 isl_mat_free(C);
1155 isl_mat_free(C2);
1156 isl_mat_free(T);
1157 return bmap;
1160 static struct isl_basic_map *set_div_from_lower_bound(
1161 struct isl_basic_map *bmap, int div, int ineq)
1163 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1165 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1166 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1167 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1168 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1169 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1171 return bmap;
1174 /* Check whether it is ok to define a div based on an inequality.
1175 * To avoid the introduction of circular definitions of divs, we
1176 * do not allow such a definition if the resulting expression would refer to
1177 * any other undefined divs or if any known div is defined in
1178 * terms of the unknown div.
1180 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1181 int div, int ineq)
1183 int j;
1184 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1186 /* Not defined in terms of unknown divs */
1187 for (j = 0; j < bmap->n_div; ++j) {
1188 if (div == j)
1189 continue;
1190 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1191 continue;
1192 if (isl_int_is_zero(bmap->div[j][0]))
1193 return 0;
1196 /* No other div defined in terms of this one => avoid loops */
1197 for (j = 0; j < bmap->n_div; ++j) {
1198 if (div == j)
1199 continue;
1200 if (isl_int_is_zero(bmap->div[j][0]))
1201 continue;
1202 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1203 return 0;
1206 return 1;
1209 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1210 * be a better expression than the current one?
1212 * If we do not have any expression yet, then any expression would be better.
1213 * Otherwise we check if the last variable involved in the inequality
1214 * (disregarding the div that it would define) is in an earlier position
1215 * than the last variable involved in the current div expression.
1217 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1218 int div, int ineq)
1220 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1221 int last_div;
1222 int last_ineq;
1224 if (isl_int_is_zero(bmap->div[div][0]))
1225 return 1;
1227 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1228 bmap->n_div - (div + 1)) >= 0)
1229 return 0;
1231 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1232 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1233 total + bmap->n_div);
1235 return last_ineq < last_div;
1238 /* Given two constraints "k" and "l" that are opposite to each other,
1239 * except for the constant term, check if we can use them
1240 * to obtain an expression for one of the hitherto unknown divs or
1241 * a "better" expression for a div for which we already have an expression.
1242 * "sum" is the sum of the constant terms of the constraints.
1243 * If this sum is strictly smaller than the coefficient of one
1244 * of the divs, then this pair can be used define the div.
1245 * To avoid the introduction of circular definitions of divs, we
1246 * do not use the pair if the resulting expression would refer to
1247 * any other undefined divs or if any known div is defined in
1248 * terms of the unknown div.
1250 static struct isl_basic_map *check_for_div_constraints(
1251 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1253 int i;
1254 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1256 for (i = 0; i < bmap->n_div; ++i) {
1257 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1258 continue;
1259 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1260 continue;
1261 if (!better_div_constraint(bmap, i, k))
1262 continue;
1263 if (!ok_to_set_div_from_bound(bmap, i, k))
1264 break;
1265 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1266 bmap = set_div_from_lower_bound(bmap, i, k);
1267 else
1268 bmap = set_div_from_lower_bound(bmap, i, l);
1269 if (progress)
1270 *progress = 1;
1271 break;
1273 return bmap;
1276 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1277 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1279 struct isl_constraint_index ci;
1280 int k, l, h;
1281 unsigned total = isl_basic_map_total_dim(bmap);
1282 isl_int sum;
1284 if (!bmap || bmap->n_ineq <= 1)
1285 return bmap;
1287 if (create_constraint_index(&ci, bmap) < 0)
1288 return bmap;
1290 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1291 ci.index[h] = &bmap->ineq[0];
1292 for (k = 1; k < bmap->n_ineq; ++k) {
1293 h = hash_index(&ci, bmap, k);
1294 if (!ci.index[h]) {
1295 ci.index[h] = &bmap->ineq[k];
1296 continue;
1298 if (progress)
1299 *progress = 1;
1300 l = ci.index[h] - &bmap->ineq[0];
1301 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1302 swap_inequality(bmap, k, l);
1303 isl_basic_map_drop_inequality(bmap, k);
1304 --k;
1306 isl_int_init(sum);
1307 for (k = 0; k < bmap->n_ineq-1; ++k) {
1308 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1309 h = hash_index(&ci, bmap, k);
1310 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1311 if (!ci.index[h])
1312 continue;
1313 l = ci.index[h] - &bmap->ineq[0];
1314 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1315 if (isl_int_is_pos(sum)) {
1316 if (detect_divs)
1317 bmap = check_for_div_constraints(bmap, k, l,
1318 sum, progress);
1319 continue;
1321 if (isl_int_is_zero(sum)) {
1322 /* We need to break out of the loop after these
1323 * changes since the contents of the hash
1324 * will no longer be valid.
1325 * Plus, we probably we want to regauss first.
1327 if (progress)
1328 *progress = 1;
1329 isl_basic_map_drop_inequality(bmap, l);
1330 isl_basic_map_inequality_to_equality(bmap, k);
1331 } else
1332 bmap = isl_basic_map_set_to_empty(bmap);
1333 break;
1335 isl_int_clear(sum);
1337 constraint_index_free(&ci);
1338 return bmap;
1341 /* Detect all pairs of inequalities that form an equality.
1343 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1344 * Call it repeatedly while it is making progress.
1346 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1347 __isl_take isl_basic_map *bmap, int *progress)
1349 int duplicate;
1351 do {
1352 duplicate = 0;
1353 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1354 &duplicate, 0);
1355 if (progress && duplicate)
1356 *progress = 1;
1357 } while (duplicate);
1359 return bmap;
1362 /* Eliminate knowns divs from constraints where they appear with
1363 * a (positive or negative) unit coefficient.
1365 * That is, replace
1367 * floor(e/m) + f >= 0
1369 * by
1371 * e + m f >= 0
1373 * and
1375 * -floor(e/m) + f >= 0
1377 * by
1379 * -e + m f + m - 1 >= 0
1381 * The first conversion is valid because floor(e/m) >= -f is equivalent
1382 * to e/m >= -f because -f is an integral expression.
1383 * The second conversion follows from the fact that
1385 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1388 * Note that one of the div constraints may have been eliminated
1389 * due to being redundant with respect to the constraint that is
1390 * being modified by this function. The modified constraint may
1391 * no longer imply this div constraint, so we add it back to make
1392 * sure we do not lose any information.
1394 * We skip integral divs, i.e., those with denominator 1, as we would
1395 * risk eliminating the div from the div constraints. We do not need
1396 * to handle those divs here anyway since the div constraints will turn
1397 * out to form an equality and this equality can then be use to eliminate
1398 * the div from all constraints.
1400 static __isl_give isl_basic_map *eliminate_unit_divs(
1401 __isl_take isl_basic_map *bmap, int *progress)
1403 int i, j;
1404 isl_ctx *ctx;
1405 unsigned total;
1407 if (!bmap)
1408 return NULL;
1410 ctx = isl_basic_map_get_ctx(bmap);
1411 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1413 for (i = 0; i < bmap->n_div; ++i) {
1414 if (isl_int_is_zero(bmap->div[i][0]))
1415 continue;
1416 if (isl_int_is_one(bmap->div[i][0]))
1417 continue;
1418 for (j = 0; j < bmap->n_ineq; ++j) {
1419 int s;
1421 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1422 !isl_int_is_negone(bmap->ineq[j][total + i]))
1423 continue;
1425 *progress = 1;
1427 s = isl_int_sgn(bmap->ineq[j][total + i]);
1428 isl_int_set_si(bmap->ineq[j][total + i], 0);
1429 if (s < 0)
1430 isl_seq_combine(bmap->ineq[j],
1431 ctx->negone, bmap->div[i] + 1,
1432 bmap->div[i][0], bmap->ineq[j],
1433 total + bmap->n_div);
1434 else
1435 isl_seq_combine(bmap->ineq[j],
1436 ctx->one, bmap->div[i] + 1,
1437 bmap->div[i][0], bmap->ineq[j],
1438 total + bmap->n_div);
1439 if (s < 0) {
1440 isl_int_add(bmap->ineq[j][0],
1441 bmap->ineq[j][0], bmap->div[i][0]);
1442 isl_int_sub_ui(bmap->ineq[j][0],
1443 bmap->ineq[j][0], 1);
1446 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1447 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1448 return isl_basic_map_free(bmap);
1452 return bmap;
1455 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1457 int progress = 1;
1458 if (!bmap)
1459 return NULL;
1460 while (progress) {
1461 progress = 0;
1462 if (!bmap)
1463 break;
1464 if (isl_basic_map_plain_is_empty(bmap))
1465 break;
1466 bmap = isl_basic_map_normalize_constraints(bmap);
1467 bmap = remove_independent_vars_from_divs(bmap);
1468 bmap = normalize_div_expressions(bmap);
1469 bmap = remove_duplicate_divs(bmap, &progress);
1470 bmap = eliminate_unit_divs(bmap, &progress);
1471 bmap = eliminate_divs_eq(bmap, &progress);
1472 bmap = eliminate_divs_ineq(bmap, &progress);
1473 bmap = isl_basic_map_gauss(bmap, &progress);
1474 /* requires equalities in normal form */
1475 bmap = normalize_divs(bmap, &progress);
1476 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1477 &progress, 1);
1478 if (bmap && progress)
1479 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1481 return bmap;
1484 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1486 return (struct isl_basic_set *)
1487 isl_basic_map_simplify((struct isl_basic_map *)bset);
1491 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1492 isl_int *constraint, unsigned div)
1494 unsigned pos;
1496 if (!bmap)
1497 return -1;
1499 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1501 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1502 int neg;
1503 isl_int_sub(bmap->div[div][1],
1504 bmap->div[div][1], bmap->div[div][0]);
1505 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1506 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1507 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1508 isl_int_add(bmap->div[div][1],
1509 bmap->div[div][1], bmap->div[div][0]);
1510 if (!neg)
1511 return 0;
1512 if (isl_seq_first_non_zero(constraint+pos+1,
1513 bmap->n_div-div-1) != -1)
1514 return 0;
1515 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1516 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1517 return 0;
1518 if (isl_seq_first_non_zero(constraint+pos+1,
1519 bmap->n_div-div-1) != -1)
1520 return 0;
1521 } else
1522 return 0;
1524 return 1;
1527 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1528 isl_int *constraint, unsigned div)
1530 return isl_basic_map_is_div_constraint(bset, constraint, div);
1534 /* If the only constraints a div d=floor(f/m)
1535 * appears in are its two defining constraints
1537 * f - m d >=0
1538 * -(f - (m - 1)) + m d >= 0
1540 * then it can safely be removed.
1542 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1544 int i;
1545 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1547 for (i = 0; i < bmap->n_eq; ++i)
1548 if (!isl_int_is_zero(bmap->eq[i][pos]))
1549 return 0;
1551 for (i = 0; i < bmap->n_ineq; ++i) {
1552 if (isl_int_is_zero(bmap->ineq[i][pos]))
1553 continue;
1554 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1555 return 0;
1558 for (i = 0; i < bmap->n_div; ++i) {
1559 if (isl_int_is_zero(bmap->div[i][0]))
1560 continue;
1561 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1562 return 0;
1565 return 1;
1569 * Remove divs that don't occur in any of the constraints or other divs.
1570 * These can arise when dropping constraints from a basic map or
1571 * when the divs of a basic map have been temporarily aligned
1572 * with the divs of another basic map.
1574 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1576 int i;
1578 if (!bmap)
1579 return NULL;
1581 for (i = bmap->n_div-1; i >= 0; --i) {
1582 if (!div_is_redundant(bmap, i))
1583 continue;
1584 bmap = isl_basic_map_drop_div(bmap, i);
1586 return bmap;
1589 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1591 bmap = remove_redundant_divs(bmap);
1592 if (!bmap)
1593 return NULL;
1594 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1595 return bmap;
1598 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1600 return (struct isl_basic_set *)
1601 isl_basic_map_finalize((struct isl_basic_map *)bset);
1604 struct isl_set *isl_set_finalize(struct isl_set *set)
1606 int i;
1608 if (!set)
1609 return NULL;
1610 for (i = 0; i < set->n; ++i) {
1611 set->p[i] = isl_basic_set_finalize(set->p[i]);
1612 if (!set->p[i])
1613 goto error;
1615 return set;
1616 error:
1617 isl_set_free(set);
1618 return NULL;
1621 struct isl_map *isl_map_finalize(struct isl_map *map)
1623 int i;
1625 if (!map)
1626 return NULL;
1627 for (i = 0; i < map->n; ++i) {
1628 map->p[i] = isl_basic_map_finalize(map->p[i]);
1629 if (!map->p[i])
1630 goto error;
1632 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1633 return map;
1634 error:
1635 isl_map_free(map);
1636 return NULL;
1640 /* Remove definition of any div that is defined in terms of the given variable.
1641 * The div itself is not removed. Functions such as
1642 * eliminate_divs_ineq depend on the other divs remaining in place.
1644 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1645 int pos)
1647 int i;
1649 if (!bmap)
1650 return NULL;
1652 for (i = 0; i < bmap->n_div; ++i) {
1653 if (isl_int_is_zero(bmap->div[i][0]))
1654 continue;
1655 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1656 continue;
1657 isl_int_set_si(bmap->div[i][0], 0);
1659 return bmap;
1662 /* Eliminate the specified variables from the constraints using
1663 * Fourier-Motzkin. The variables themselves are not removed.
1665 struct isl_basic_map *isl_basic_map_eliminate_vars(
1666 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1668 int d;
1669 int i, j, k;
1670 unsigned total;
1671 int need_gauss = 0;
1673 if (n == 0)
1674 return bmap;
1675 if (!bmap)
1676 return NULL;
1677 total = isl_basic_map_total_dim(bmap);
1679 bmap = isl_basic_map_cow(bmap);
1680 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1681 bmap = remove_dependent_vars(bmap, d);
1682 if (!bmap)
1683 return NULL;
1685 for (d = pos + n - 1;
1686 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1687 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1688 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1689 int n_lower, n_upper;
1690 if (!bmap)
1691 return NULL;
1692 for (i = 0; i < bmap->n_eq; ++i) {
1693 if (isl_int_is_zero(bmap->eq[i][1+d]))
1694 continue;
1695 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1696 isl_basic_map_drop_equality(bmap, i);
1697 need_gauss = 1;
1698 break;
1700 if (i < bmap->n_eq)
1701 continue;
1702 n_lower = 0;
1703 n_upper = 0;
1704 for (i = 0; i < bmap->n_ineq; ++i) {
1705 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1706 n_lower++;
1707 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1708 n_upper++;
1710 bmap = isl_basic_map_extend_constraints(bmap,
1711 0, n_lower * n_upper);
1712 if (!bmap)
1713 goto error;
1714 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1715 int last;
1716 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1717 continue;
1718 last = -1;
1719 for (j = 0; j < i; ++j) {
1720 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1721 continue;
1722 last = j;
1723 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1724 isl_int_sgn(bmap->ineq[j][1+d]))
1725 continue;
1726 k = isl_basic_map_alloc_inequality(bmap);
1727 if (k < 0)
1728 goto error;
1729 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1730 1+total);
1731 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1732 1+d, 1+total, NULL);
1734 isl_basic_map_drop_inequality(bmap, i);
1735 i = last + 1;
1737 if (n_lower > 0 && n_upper > 0) {
1738 bmap = isl_basic_map_normalize_constraints(bmap);
1739 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1740 NULL, 0);
1741 bmap = isl_basic_map_gauss(bmap, NULL);
1742 bmap = isl_basic_map_remove_redundancies(bmap);
1743 need_gauss = 0;
1744 if (!bmap)
1745 goto error;
1746 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1747 break;
1750 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1751 if (need_gauss)
1752 bmap = isl_basic_map_gauss(bmap, NULL);
1753 return bmap;
1754 error:
1755 isl_basic_map_free(bmap);
1756 return NULL;
1759 struct isl_basic_set *isl_basic_set_eliminate_vars(
1760 struct isl_basic_set *bset, unsigned pos, unsigned n)
1762 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1763 (struct isl_basic_map *)bset, pos, n);
1766 /* Eliminate the specified n dimensions starting at first from the
1767 * constraints, without removing the dimensions from the space.
1768 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1769 * Otherwise, they are projected out and the original space is restored.
1771 __isl_give isl_basic_map *isl_basic_map_eliminate(
1772 __isl_take isl_basic_map *bmap,
1773 enum isl_dim_type type, unsigned first, unsigned n)
1775 isl_space *space;
1777 if (!bmap)
1778 return NULL;
1779 if (n == 0)
1780 return bmap;
1782 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1783 isl_die(bmap->ctx, isl_error_invalid,
1784 "index out of bounds", goto error);
1786 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1787 first += isl_basic_map_offset(bmap, type) - 1;
1788 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1789 return isl_basic_map_finalize(bmap);
1792 space = isl_basic_map_get_space(bmap);
1793 bmap = isl_basic_map_project_out(bmap, type, first, n);
1794 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1795 bmap = isl_basic_map_reset_space(bmap, space);
1796 return bmap;
1797 error:
1798 isl_basic_map_free(bmap);
1799 return NULL;
1802 __isl_give isl_basic_set *isl_basic_set_eliminate(
1803 __isl_take isl_basic_set *bset,
1804 enum isl_dim_type type, unsigned first, unsigned n)
1806 return isl_basic_map_eliminate(bset, type, first, n);
1809 /* Don't assume equalities are in order, because align_divs
1810 * may have changed the order of the divs.
1812 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1814 int d, i;
1815 unsigned total;
1817 total = isl_space_dim(bmap->dim, isl_dim_all);
1818 for (d = 0; d < total; ++d)
1819 elim[d] = -1;
1820 for (i = 0; i < bmap->n_eq; ++i) {
1821 for (d = total - 1; d >= 0; --d) {
1822 if (isl_int_is_zero(bmap->eq[i][1+d]))
1823 continue;
1824 elim[d] = i;
1825 break;
1830 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1832 compute_elimination_index((struct isl_basic_map *)bset, elim);
1835 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1836 struct isl_basic_map *bmap, int *elim)
1838 int d;
1839 int copied = 0;
1840 unsigned total;
1842 total = isl_space_dim(bmap->dim, isl_dim_all);
1843 for (d = total - 1; d >= 0; --d) {
1844 if (isl_int_is_zero(src[1+d]))
1845 continue;
1846 if (elim[d] == -1)
1847 continue;
1848 if (!copied) {
1849 isl_seq_cpy(dst, src, 1 + total);
1850 copied = 1;
1852 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1854 return copied;
1857 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1858 struct isl_basic_set *bset, int *elim)
1860 return reduced_using_equalities(dst, src,
1861 (struct isl_basic_map *)bset, elim);
1864 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1865 struct isl_basic_set *bset, struct isl_basic_set *context)
1867 int i;
1868 int *elim;
1870 if (!bset || !context)
1871 goto error;
1873 if (context->n_eq == 0) {
1874 isl_basic_set_free(context);
1875 return bset;
1878 bset = isl_basic_set_cow(bset);
1879 if (!bset)
1880 goto error;
1882 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1883 if (!elim)
1884 goto error;
1885 set_compute_elimination_index(context, elim);
1886 for (i = 0; i < bset->n_eq; ++i)
1887 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1888 context, elim);
1889 for (i = 0; i < bset->n_ineq; ++i)
1890 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1891 context, elim);
1892 isl_basic_set_free(context);
1893 free(elim);
1894 bset = isl_basic_set_simplify(bset);
1895 bset = isl_basic_set_finalize(bset);
1896 return bset;
1897 error:
1898 isl_basic_set_free(bset);
1899 isl_basic_set_free(context);
1900 return NULL;
1903 static struct isl_basic_set *remove_shifted_constraints(
1904 struct isl_basic_set *bset, struct isl_basic_set *context)
1906 struct isl_constraint_index ci;
1907 int k, h, l;
1909 if (!bset || !context)
1910 return bset;
1912 if (context->n_ineq == 0)
1913 return bset;
1914 if (setup_constraint_index(&ci, context) < 0)
1915 return bset;
1917 for (k = 0; k < bset->n_ineq; ++k) {
1918 h = set_hash_index(&ci, bset, k);
1919 if (!ci.index[h])
1920 continue;
1921 l = ci.index[h] - &context->ineq[0];
1922 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1923 continue;
1924 bset = isl_basic_set_cow(bset);
1925 if (!bset)
1926 goto error;
1927 isl_basic_set_drop_inequality(bset, k);
1928 --k;
1930 constraint_index_free(&ci);
1931 return bset;
1932 error:
1933 constraint_index_free(&ci);
1934 return bset;
1937 /* Remove constraints from "bmap" that are identical to constraints
1938 * in "context" or that are more relaxed (greater constant term).
1940 * We perform the test for shifted copies on the pure constraints
1941 * in remove_shifted_constraints.
1943 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1944 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1946 isl_basic_set *bset, *bset_context;
1948 if (!bmap || !context)
1949 goto error;
1951 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1952 isl_basic_map_free(context);
1953 return bmap;
1956 context = isl_basic_map_align_divs(context, bmap);
1957 bmap = isl_basic_map_align_divs(bmap, context);
1959 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1960 bset_context = isl_basic_map_underlying_set(context);
1961 bset = remove_shifted_constraints(bset, bset_context);
1962 isl_basic_set_free(bset_context);
1964 bmap = isl_basic_map_overlying_set(bset, bmap);
1966 return bmap;
1967 error:
1968 isl_basic_map_free(bmap);
1969 isl_basic_map_free(context);
1970 return NULL;
1973 /* Does the (linear part of a) constraint "c" involve any of the "len"
1974 * "relevant" dimensions?
1976 static int is_related(isl_int *c, int len, int *relevant)
1978 int i;
1980 for (i = 0; i < len; ++i) {
1981 if (!relevant[i])
1982 continue;
1983 if (!isl_int_is_zero(c[i]))
1984 return 1;
1987 return 0;
1990 /* Drop constraints from "bset" that do not involve any of
1991 * the dimensions marked "relevant".
1993 static __isl_give isl_basic_set *drop_unrelated_constraints(
1994 __isl_take isl_basic_set *bset, int *relevant)
1996 int i, dim;
1998 dim = isl_basic_set_dim(bset, isl_dim_set);
1999 for (i = 0; i < dim; ++i)
2000 if (!relevant[i])
2001 break;
2002 if (i >= dim)
2003 return bset;
2005 for (i = bset->n_eq - 1; i >= 0; --i)
2006 if (!is_related(bset->eq[i] + 1, dim, relevant))
2007 isl_basic_set_drop_equality(bset, i);
2009 for (i = bset->n_ineq - 1; i >= 0; --i)
2010 if (!is_related(bset->ineq[i] + 1, dim, relevant))
2011 isl_basic_set_drop_inequality(bset, i);
2013 return bset;
2016 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2018 * In particular, for any variable involved in the constraint,
2019 * find the actual group id from before and replace the group
2020 * of the corresponding variable by the minimal group of all
2021 * the variables involved in the constraint considered so far
2022 * (if this minimum is smaller) or replace the minimum by this group
2023 * (if the minimum is larger).
2025 * At the end, all the variables in "c" will (indirectly) point
2026 * to the minimal of the groups that they referred to originally.
2028 static void update_groups(int dim, int *group, isl_int *c)
2030 int j;
2031 int min = dim;
2033 for (j = 0; j < dim; ++j) {
2034 if (isl_int_is_zero(c[j]))
2035 continue;
2036 while (group[j] >= 0 && group[group[j]] != group[j])
2037 group[j] = group[group[j]];
2038 if (group[j] == min)
2039 continue;
2040 if (group[j] < min) {
2041 if (min >= 0 && min < dim)
2042 group[min] = group[j];
2043 min = group[j];
2044 } else
2045 group[group[j]] = min;
2049 /* Drop constraints from "context" that are irrelevant for computing
2050 * the gist of "bset".
2052 * In particular, drop constraints in variables that are not related
2053 * to any of the variables involved in the constraints of "bset"
2054 * in the sense that there is no sequence of constraints that connects them.
2056 * We construct groups of variables that collect variables that
2057 * (indirectly) appear in some common constraint of "context".
2058 * Each group is identified by the first variable in the group,
2059 * except for the special group of variables that appear in "bset"
2060 * (or are related to those variables), which is identified by -1.
2061 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2062 * otherwise the group of i is the group of group[i].
2064 * We first initialize the -1 group with the variables that appear in "bset".
2065 * Then we initialize groups for the remaining variables.
2066 * Then we iterate over the constraints of "context" and update the
2067 * group of the variables in the constraint by the smallest group.
2068 * Finally, we resolve indirect references to groups by running over
2069 * the variables.
2071 * After computing the groups, we drop constraints that do not involve
2072 * any variables in the -1 group.
2074 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2075 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2077 isl_ctx *ctx;
2078 int *group;
2079 int dim;
2080 int i, j;
2081 int last;
2083 if (!context || !bset)
2084 return isl_basic_set_free(context);
2086 dim = isl_basic_set_dim(bset, isl_dim_set);
2087 ctx = isl_basic_set_get_ctx(bset);
2088 group = isl_calloc_array(ctx, int, dim);
2090 if (!group)
2091 goto error;
2093 for (i = 0; i < dim; ++i) {
2094 for (j = 0; j < bset->n_eq; ++j)
2095 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2096 break;
2097 if (j < bset->n_eq) {
2098 group[i] = -1;
2099 continue;
2101 for (j = 0; j < bset->n_ineq; ++j)
2102 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2103 break;
2104 if (j < bset->n_ineq)
2105 group[i] = -1;
2108 last = -1;
2109 for (i = 0; i < dim; ++i)
2110 if (group[i] >= 0)
2111 last = group[i] = i;
2112 if (last < 0) {
2113 free(group);
2114 return context;
2117 for (i = 0; i < context->n_eq; ++i)
2118 update_groups(dim, group, context->eq[i] + 1);
2119 for (i = 0; i < context->n_ineq; ++i)
2120 update_groups(dim, group, context->ineq[i] + 1);
2122 for (i = 0; i < dim; ++i)
2123 if (group[i] >= 0)
2124 group[i] = group[group[i]];
2126 for (i = 0; i < dim; ++i)
2127 group[i] = group[i] == -1;
2129 context = drop_unrelated_constraints(context, group);
2131 free(group);
2132 return context;
2133 error:
2134 free(group);
2135 return isl_basic_set_free(context);
2138 /* Remove all information from bset that is redundant in the context
2139 * of context. Both bset and context are assumed to be full-dimensional.
2141 * We first remove the inequalities from "bset"
2142 * that are obviously redundant with respect to some inequality in "context".
2143 * Then we remove those constraints from "context" that have become
2144 * irrelevant for computing the gist of "bset".
2145 * Note that this removal of constraints cannot be replaced by
2146 * a factorization because factors in "bset" may still be connected
2147 * to each other through constraints in "context".
2149 * If there are any inequalities left, we construct a tableau for
2150 * the context and then add the inequalities of "bset".
2151 * Before adding these inequalities, we freeze all constraints such that
2152 * they won't be considered redundant in terms of the constraints of "bset".
2153 * Then we detect all redundant constraints (among the
2154 * constraints that weren't frozen), first by checking for redundancy in the
2155 * the tableau and then by checking if replacing a constraint by its negation
2156 * would lead to an empty set. This last step is fairly expensive
2157 * and could be optimized by more reuse of the tableau.
2158 * Finally, we update bset according to the results.
2160 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2161 __isl_take isl_basic_set *context)
2163 int i, k;
2164 isl_basic_set *combined = NULL;
2165 struct isl_tab *tab = NULL;
2166 unsigned context_ineq;
2167 unsigned total;
2169 if (!bset || !context)
2170 goto error;
2172 if (isl_basic_set_is_universe(bset)) {
2173 isl_basic_set_free(context);
2174 return bset;
2177 if (isl_basic_set_is_universe(context)) {
2178 isl_basic_set_free(context);
2179 return bset;
2182 bset = remove_shifted_constraints(bset, context);
2183 if (!bset)
2184 goto error;
2185 if (bset->n_ineq == 0)
2186 goto done;
2188 context = drop_irrelevant_constraints(context, bset);
2189 if (!context)
2190 goto error;
2191 if (isl_basic_set_is_universe(context)) {
2192 isl_basic_set_free(context);
2193 return bset;
2196 context_ineq = context->n_ineq;
2197 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2198 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2199 tab = isl_tab_from_basic_set(combined, 0);
2200 for (i = 0; i < context_ineq; ++i)
2201 if (isl_tab_freeze_constraint(tab, i) < 0)
2202 goto error;
2203 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2204 goto error;
2205 for (i = 0; i < bset->n_ineq; ++i)
2206 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
2207 goto error;
2208 bset = isl_basic_set_add_constraints(combined, bset, 0);
2209 combined = NULL;
2210 if (!bset)
2211 goto error;
2212 if (isl_tab_detect_redundant(tab) < 0)
2213 goto error;
2214 total = isl_basic_set_total_dim(bset);
2215 for (i = context_ineq; i < bset->n_ineq; ++i) {
2216 int is_empty;
2217 if (tab->con[i].is_redundant)
2218 continue;
2219 tab->con[i].is_redundant = 1;
2220 combined = isl_basic_set_dup(bset);
2221 combined = isl_basic_set_update_from_tab(combined, tab);
2222 combined = isl_basic_set_extend_constraints(combined, 0, 1);
2223 k = isl_basic_set_alloc_inequality(combined);
2224 if (k < 0)
2225 goto error;
2226 isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total);
2227 isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1);
2228 is_empty = isl_basic_set_is_empty(combined);
2229 if (is_empty < 0)
2230 goto error;
2231 isl_basic_set_free(combined);
2232 combined = NULL;
2233 if (!is_empty)
2234 tab->con[i].is_redundant = 0;
2236 for (i = 0; i < context_ineq; ++i)
2237 tab->con[i].is_redundant = 1;
2238 bset = isl_basic_set_update_from_tab(bset, tab);
2239 if (bset) {
2240 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2241 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2244 isl_tab_free(tab);
2245 done:
2246 bset = isl_basic_set_finalize(bset);
2247 isl_basic_set_free(context);
2248 return bset;
2249 error:
2250 isl_tab_free(tab);
2251 isl_basic_set_free(combined);
2252 isl_basic_set_free(context);
2253 isl_basic_set_free(bset);
2254 return NULL;
2257 /* Remove all information from bset that is redundant in the context
2258 * of context. In particular, equalities that are linear combinations
2259 * of those in context are removed. Then the inequalities that are
2260 * redundant in the context of the equalities and inequalities of
2261 * context are removed.
2263 * First of all, we drop those constraints from "context"
2264 * that are irrelevant for computing the gist of "bset".
2265 * Alternatively, we could factorize the intersection of "context" and "bset".
2267 * We first compute the integer affine hull of the intersection,
2268 * compute the gist inside this affine hull and then add back
2269 * those equalities that are not implied by the context.
2271 * If two constraints are mutually redundant, then uset_gist_full
2272 * will remove the second of those constraints. We therefore first
2273 * sort the constraints so that constraints not involving existentially
2274 * quantified variables are given precedence over those that do.
2275 * We have to perform this sorting before the variable compression,
2276 * because that may effect the order of the variables.
2278 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2279 __isl_take isl_basic_set *context)
2281 isl_mat *eq;
2282 isl_mat *T, *T2;
2283 isl_basic_set *aff;
2284 isl_basic_set *aff_context;
2285 unsigned total;
2287 if (!bset || !context)
2288 goto error;
2290 context = drop_irrelevant_constraints(context, bset);
2292 aff = isl_basic_set_copy(bset);
2293 aff = isl_basic_set_intersect(aff, isl_basic_set_copy(context));
2294 aff = isl_basic_set_affine_hull(aff);
2295 if (!aff)
2296 goto error;
2297 if (isl_basic_set_plain_is_empty(aff)) {
2298 isl_basic_set_free(bset);
2299 isl_basic_set_free(context);
2300 return aff;
2302 bset = isl_basic_set_sort_constraints(bset);
2303 if (aff->n_eq == 0) {
2304 isl_basic_set_free(aff);
2305 return uset_gist_full(bset, context);
2307 total = isl_basic_set_total_dim(bset);
2308 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2309 eq = isl_mat_cow(eq);
2310 T = isl_mat_variable_compression(eq, &T2);
2311 if (T && T->n_col == 0) {
2312 isl_mat_free(T);
2313 isl_mat_free(T2);
2314 isl_basic_set_free(context);
2315 isl_basic_set_free(aff);
2316 return isl_basic_set_set_to_empty(bset);
2319 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2321 bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
2322 context = isl_basic_set_preimage(context, T);
2324 bset = uset_gist_full(bset, context);
2325 bset = isl_basic_set_preimage(bset, T2);
2326 bset = isl_basic_set_intersect(bset, aff);
2327 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2329 if (bset) {
2330 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2331 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2334 return bset;
2335 error:
2336 isl_basic_set_free(bset);
2337 isl_basic_set_free(context);
2338 return NULL;
2341 /* Return a basic map that has the same intersection with "context" as "bmap"
2342 * and that is as "simple" as possible.
2344 * The core computation is performed on the pure constraints.
2345 * When we add back the meaning of the integer divisions, we need
2346 * to (re)introduce the div constraints. If we happen to have
2347 * discovered that some of these integer divisions are equal to
2348 * some affine combination of other variables, then these div
2349 * constraints may end up getting simplified in terms of the equalities,
2350 * resulting in extra inequalities on the other variables that
2351 * may have been removed already or that may not even have been
2352 * part of the input. We try and remove those constraints of
2353 * this form that are most obviously redundant with respect to
2354 * the context. We also remove those div constraints that are
2355 * redundant with respect to the other constraints in the result.
2357 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
2358 struct isl_basic_map *context)
2360 isl_basic_set *bset, *eq;
2361 isl_basic_map *eq_bmap;
2362 unsigned n_div, n_eq, n_ineq;
2364 if (!bmap || !context)
2365 goto error;
2367 if (isl_basic_map_is_universe(bmap)) {
2368 isl_basic_map_free(context);
2369 return bmap;
2371 if (isl_basic_map_plain_is_empty(context)) {
2372 isl_space *space = isl_basic_map_get_space(bmap);
2373 isl_basic_map_free(bmap);
2374 isl_basic_map_free(context);
2375 return isl_basic_map_universe(space);
2377 if (isl_basic_map_plain_is_empty(bmap)) {
2378 isl_basic_map_free(context);
2379 return bmap;
2382 bmap = isl_basic_map_remove_redundancies(bmap);
2383 context = isl_basic_map_remove_redundancies(context);
2384 if (!context)
2385 goto error;
2387 context = isl_basic_map_align_divs(context, bmap);
2388 bmap = isl_basic_map_align_divs(bmap, context);
2389 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2391 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
2392 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
2394 if (!bset || bset->n_eq == 0 || n_div == 0 ||
2395 isl_basic_set_plain_is_empty(bset)) {
2396 isl_basic_map_free(context);
2397 return isl_basic_map_overlying_set(bset, bmap);
2400 n_eq = bset->n_eq;
2401 n_ineq = bset->n_ineq;
2402 eq = isl_basic_set_copy(bset);
2403 eq = isl_basic_set_cow(eq);
2404 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
2405 eq = isl_basic_set_free(eq);
2406 if (isl_basic_set_free_equality(bset, n_eq) < 0)
2407 bset = isl_basic_set_free(bset);
2409 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
2410 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
2411 bmap = isl_basic_map_overlying_set(bset, bmap);
2412 bmap = isl_basic_map_intersect(bmap, eq_bmap);
2413 bmap = isl_basic_map_remove_redundancies(bmap);
2415 return bmap;
2416 error:
2417 isl_basic_map_free(bmap);
2418 isl_basic_map_free(context);
2419 return NULL;
2423 * Assumes context has no implicit divs.
2425 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
2426 __isl_take isl_basic_map *context)
2428 int i;
2430 if (!map || !context)
2431 goto error;
2433 if (isl_basic_map_plain_is_empty(context)) {
2434 isl_space *space = isl_map_get_space(map);
2435 isl_map_free(map);
2436 isl_basic_map_free(context);
2437 return isl_map_universe(space);
2440 context = isl_basic_map_remove_redundancies(context);
2441 map = isl_map_cow(map);
2442 if (!map || !context)
2443 goto error;
2444 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
2445 map = isl_map_compute_divs(map);
2446 if (!map)
2447 goto error;
2448 for (i = map->n - 1; i >= 0; --i) {
2449 map->p[i] = isl_basic_map_gist(map->p[i],
2450 isl_basic_map_copy(context));
2451 if (!map->p[i])
2452 goto error;
2453 if (isl_basic_map_plain_is_empty(map->p[i])) {
2454 isl_basic_map_free(map->p[i]);
2455 if (i != map->n - 1)
2456 map->p[i] = map->p[map->n - 1];
2457 map->n--;
2460 isl_basic_map_free(context);
2461 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2462 return map;
2463 error:
2464 isl_map_free(map);
2465 isl_basic_map_free(context);
2466 return NULL;
2469 /* Return a map that has the same intersection with "context" as "map"
2470 * and that is as "simple" as possible.
2472 * If "map" is already the universe, then we cannot make it any simpler.
2473 * Similarly, if "context" is the universe, then we cannot exploit it
2474 * to simplify "map"
2475 * If "map" and "context" are identical to each other, then we can
2476 * return the corresponding universe.
2478 * If none of these cases apply, we have to work a bit harder.
2479 * During this computation, we make use of a single disjunct context,
2480 * so if the original context consists of more than one disjunct
2481 * then we need to approximate the context by a single disjunct set.
2482 * Simply taking the simple hull may drop constraints that are
2483 * only implicitly available in each disjunct. We therefore also
2484 * look for constraints among those defining "map" that are valid
2485 * for the context. These can then be used to simplify away
2486 * the corresponding constraints in "map".
2488 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
2489 __isl_take isl_map *context)
2491 int equal;
2492 int is_universe;
2493 isl_basic_map *hull;
2495 is_universe = isl_map_plain_is_universe(map);
2496 if (is_universe >= 0 && !is_universe)
2497 is_universe = isl_map_plain_is_universe(context);
2498 if (is_universe < 0)
2499 goto error;
2500 if (is_universe) {
2501 isl_map_free(context);
2502 return map;
2505 equal = isl_map_plain_is_equal(map, context);
2506 if (equal < 0)
2507 goto error;
2508 if (equal) {
2509 isl_map *res = isl_map_universe(isl_map_get_space(map));
2510 isl_map_free(map);
2511 isl_map_free(context);
2512 return res;
2515 context = isl_map_compute_divs(context);
2516 if (!context)
2517 goto error;
2518 if (isl_map_n_basic_map(context) == 1) {
2519 hull = isl_map_simple_hull(context);
2520 } else {
2521 isl_ctx *ctx;
2522 isl_map_list *list;
2524 ctx = isl_map_get_ctx(map);
2525 list = isl_map_list_alloc(ctx, 2);
2526 list = isl_map_list_add(list, isl_map_copy(context));
2527 list = isl_map_list_add(list, isl_map_copy(map));
2528 hull = isl_map_unshifted_simple_hull_from_map_list(context,
2529 list);
2531 return isl_map_gist_basic_map(map, hull);
2532 error:
2533 isl_map_free(map);
2534 isl_map_free(context);
2535 return NULL;
2538 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
2539 __isl_take isl_map *context)
2541 return isl_map_align_params_map_map_and(map, context, &map_gist);
2544 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
2545 struct isl_basic_set *context)
2547 return (struct isl_basic_set *)isl_basic_map_gist(
2548 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
2551 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
2552 __isl_take isl_basic_set *context)
2554 return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
2555 (struct isl_basic_map *)context);
2558 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
2559 __isl_take isl_basic_set *context)
2561 isl_space *space = isl_set_get_space(set);
2562 isl_basic_set *dom_context = isl_basic_set_universe(space);
2563 dom_context = isl_basic_set_intersect_params(dom_context, context);
2564 return isl_set_gist_basic_set(set, dom_context);
2567 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
2568 __isl_take isl_set *context)
2570 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
2571 (struct isl_map *)context);
2574 /* Compute the gist of "bmap" with respect to the constraints "context"
2575 * on the domain.
2577 __isl_give isl_basic_map *isl_basic_map_gist_domain(
2578 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
2580 isl_space *space = isl_basic_map_get_space(bmap);
2581 isl_basic_map *bmap_context = isl_basic_map_universe(space);
2583 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
2584 return isl_basic_map_gist(bmap, bmap_context);
2587 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
2588 __isl_take isl_set *context)
2590 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2591 map_context = isl_map_intersect_domain(map_context, context);
2592 return isl_map_gist(map, map_context);
2595 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
2596 __isl_take isl_set *context)
2598 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2599 map_context = isl_map_intersect_range(map_context, context);
2600 return isl_map_gist(map, map_context);
2603 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
2604 __isl_take isl_set *context)
2606 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
2607 map_context = isl_map_intersect_params(map_context, context);
2608 return isl_map_gist(map, map_context);
2611 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
2612 __isl_take isl_set *context)
2614 return isl_map_gist_params(set, context);
2617 /* Quick check to see if two basic maps are disjoint.
2618 * In particular, we reduce the equalities and inequalities of
2619 * one basic map in the context of the equalities of the other
2620 * basic map and check if we get a contradiction.
2622 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
2623 __isl_keep isl_basic_map *bmap2)
2625 struct isl_vec *v = NULL;
2626 int *elim = NULL;
2627 unsigned total;
2628 int i;
2630 if (!bmap1 || !bmap2)
2631 return isl_bool_error;
2632 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
2633 return isl_bool_error);
2634 if (bmap1->n_div || bmap2->n_div)
2635 return isl_bool_false;
2636 if (!bmap1->n_eq && !bmap2->n_eq)
2637 return isl_bool_false;
2639 total = isl_space_dim(bmap1->dim, isl_dim_all);
2640 if (total == 0)
2641 return isl_bool_false;
2642 v = isl_vec_alloc(bmap1->ctx, 1 + total);
2643 if (!v)
2644 goto error;
2645 elim = isl_alloc_array(bmap1->ctx, int, total);
2646 if (!elim)
2647 goto error;
2648 compute_elimination_index(bmap1, elim);
2649 for (i = 0; i < bmap2->n_eq; ++i) {
2650 int reduced;
2651 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
2652 bmap1, elim);
2653 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
2654 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2655 goto disjoint;
2657 for (i = 0; i < bmap2->n_ineq; ++i) {
2658 int reduced;
2659 reduced = reduced_using_equalities(v->block.data,
2660 bmap2->ineq[i], bmap1, elim);
2661 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2662 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2663 goto disjoint;
2665 compute_elimination_index(bmap2, elim);
2666 for (i = 0; i < bmap1->n_ineq; ++i) {
2667 int reduced;
2668 reduced = reduced_using_equalities(v->block.data,
2669 bmap1->ineq[i], bmap2, elim);
2670 if (reduced && isl_int_is_neg(v->block.data[0]) &&
2671 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
2672 goto disjoint;
2674 isl_vec_free(v);
2675 free(elim);
2676 return isl_bool_false;
2677 disjoint:
2678 isl_vec_free(v);
2679 free(elim);
2680 return isl_bool_true;
2681 error:
2682 isl_vec_free(v);
2683 free(elim);
2684 return isl_bool_error;
2687 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
2688 __isl_keep isl_basic_set *bset2)
2690 return isl_basic_map_plain_is_disjoint((struct isl_basic_map *)bset1,
2691 (struct isl_basic_map *)bset2);
2694 /* Are "map1" and "map2" obviously disjoint?
2696 * If one of them is empty or if they live in different spaces (ignoring
2697 * parameters), then they are clearly disjoint.
2699 * If they have different parameters, then we skip any further tests.
2701 * If they are obviously equal, but not obviously empty, then we will
2702 * not be able to detect if they are disjoint.
2704 * Otherwise we check if each basic map in "map1" is obviously disjoint
2705 * from each basic map in "map2".
2707 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
2708 __isl_keep isl_map *map2)
2710 int i, j;
2711 isl_bool disjoint;
2712 isl_bool intersect;
2713 isl_bool match;
2715 if (!map1 || !map2)
2716 return isl_bool_error;
2718 disjoint = isl_map_plain_is_empty(map1);
2719 if (disjoint < 0 || disjoint)
2720 return disjoint;
2722 disjoint = isl_map_plain_is_empty(map2);
2723 if (disjoint < 0 || disjoint)
2724 return disjoint;
2726 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
2727 map2->dim, isl_dim_in);
2728 if (match < 0 || !match)
2729 return match < 0 ? isl_bool_error : isl_bool_true;
2731 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
2732 map2->dim, isl_dim_out);
2733 if (match < 0 || !match)
2734 return match < 0 ? isl_bool_error : isl_bool_true;
2736 match = isl_space_match(map1->dim, isl_dim_param,
2737 map2->dim, isl_dim_param);
2738 if (match < 0 || !match)
2739 return match < 0 ? isl_bool_error : isl_bool_false;
2741 intersect = isl_map_plain_is_equal(map1, map2);
2742 if (intersect < 0 || intersect)
2743 return intersect < 0 ? isl_bool_error : isl_bool_false;
2745 for (i = 0; i < map1->n; ++i) {
2746 for (j = 0; j < map2->n; ++j) {
2747 isl_bool d = isl_basic_map_plain_is_disjoint(map1->p[i],
2748 map2->p[j]);
2749 if (d != isl_bool_true)
2750 return d;
2753 return isl_bool_true;
2756 /* Are "map1" and "map2" disjoint?
2758 * They are disjoint if they are "obviously disjoint" or if one of them
2759 * is empty. Otherwise, they are not disjoint if one of them is universal.
2760 * If none of these cases apply, we compute the intersection and see if
2761 * the result is empty.
2763 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
2765 isl_bool disjoint;
2766 isl_bool intersect;
2767 isl_map *test;
2769 disjoint = isl_map_plain_is_disjoint(map1, map2);
2770 if (disjoint < 0 || disjoint)
2771 return disjoint;
2773 disjoint = isl_map_is_empty(map1);
2774 if (disjoint < 0 || disjoint)
2775 return disjoint;
2777 disjoint = isl_map_is_empty(map2);
2778 if (disjoint < 0 || disjoint)
2779 return disjoint;
2781 intersect = isl_map_plain_is_universe(map1);
2782 if (intersect < 0 || intersect)
2783 return intersect < 0 ? isl_bool_error : isl_bool_false;
2785 intersect = isl_map_plain_is_universe(map2);
2786 if (intersect < 0 || intersect)
2787 return intersect < 0 ? isl_bool_error : isl_bool_false;
2789 test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
2790 disjoint = isl_map_is_empty(test);
2791 isl_map_free(test);
2793 return disjoint;
2796 /* Are "bmap1" and "bmap2" disjoint?
2798 * They are disjoint if they are "obviously disjoint" or if one of them
2799 * is empty. Otherwise, they are not disjoint if one of them is universal.
2800 * If none of these cases apply, we compute the intersection and see if
2801 * the result is empty.
2803 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
2804 __isl_keep isl_basic_map *bmap2)
2806 isl_bool disjoint;
2807 isl_bool intersect;
2808 isl_basic_map *test;
2810 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
2811 if (disjoint < 0 || disjoint)
2812 return disjoint;
2814 disjoint = isl_basic_map_is_empty(bmap1);
2815 if (disjoint < 0 || disjoint)
2816 return disjoint;
2818 disjoint = isl_basic_map_is_empty(bmap2);
2819 if (disjoint < 0 || disjoint)
2820 return disjoint;
2822 intersect = isl_basic_map_is_universe(bmap1);
2823 if (intersect < 0 || intersect)
2824 return intersect < 0 ? isl_bool_error : isl_bool_false;
2826 intersect = isl_basic_map_is_universe(bmap2);
2827 if (intersect < 0 || intersect)
2828 return intersect < 0 ? isl_bool_error : isl_bool_false;
2830 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
2831 isl_basic_map_copy(bmap2));
2832 disjoint = isl_basic_map_is_empty(test);
2833 isl_basic_map_free(test);
2835 return disjoint;
2838 /* Are "bset1" and "bset2" disjoint?
2840 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
2841 __isl_keep isl_basic_set *bset2)
2843 return isl_basic_map_is_disjoint(bset1, bset2);
2846 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
2847 __isl_keep isl_set *set2)
2849 return isl_map_plain_is_disjoint((struct isl_map *)set1,
2850 (struct isl_map *)set2);
2853 /* Are "set1" and "set2" disjoint?
2855 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
2857 return isl_map_is_disjoint(set1, set2);
2860 /* Check if we can combine a given div with lower bound l and upper
2861 * bound u with some other div and if so return that other div.
2862 * Otherwise return -1.
2864 * We first check that
2865 * - the bounds are opposites of each other (except for the constant
2866 * term)
2867 * - the bounds do not reference any other div
2868 * - no div is defined in terms of this div
2870 * Let m be the size of the range allowed on the div by the bounds.
2871 * That is, the bounds are of the form
2873 * e <= a <= e + m - 1
2875 * with e some expression in the other variables.
2876 * We look for another div b such that no third div is defined in terms
2877 * of this second div b and such that in any constraint that contains
2878 * a (except for the given lower and upper bound), also contains b
2879 * with a coefficient that is m times that of b.
2880 * That is, all constraints (execpt for the lower and upper bound)
2881 * are of the form
2883 * e + f (a + m b) >= 0
2885 * If so, we return b so that "a + m b" can be replaced by
2886 * a single div "c = a + m b".
2888 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
2889 unsigned div, unsigned l, unsigned u)
2891 int i, j;
2892 unsigned dim;
2893 int coalesce = -1;
2895 if (bmap->n_div <= 1)
2896 return -1;
2897 dim = isl_space_dim(bmap->dim, isl_dim_all);
2898 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
2899 return -1;
2900 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
2901 bmap->n_div - div - 1) != -1)
2902 return -1;
2903 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
2904 dim + bmap->n_div))
2905 return -1;
2907 for (i = 0; i < bmap->n_div; ++i) {
2908 if (isl_int_is_zero(bmap->div[i][0]))
2909 continue;
2910 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
2911 return -1;
2914 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2915 if (isl_int_is_neg(bmap->ineq[l][0])) {
2916 isl_int_sub(bmap->ineq[l][0],
2917 bmap->ineq[l][0], bmap->ineq[u][0]);
2918 bmap = isl_basic_map_copy(bmap);
2919 bmap = isl_basic_map_set_to_empty(bmap);
2920 isl_basic_map_free(bmap);
2921 return -1;
2923 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2924 for (i = 0; i < bmap->n_div; ++i) {
2925 if (i == div)
2926 continue;
2927 if (!pairs[i])
2928 continue;
2929 for (j = 0; j < bmap->n_div; ++j) {
2930 if (isl_int_is_zero(bmap->div[j][0]))
2931 continue;
2932 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
2933 break;
2935 if (j < bmap->n_div)
2936 continue;
2937 for (j = 0; j < bmap->n_ineq; ++j) {
2938 int valid;
2939 if (j == l || j == u)
2940 continue;
2941 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
2942 continue;
2943 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
2944 break;
2945 isl_int_mul(bmap->ineq[j][1 + dim + div],
2946 bmap->ineq[j][1 + dim + div],
2947 bmap->ineq[l][0]);
2948 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
2949 bmap->ineq[j][1 + dim + i]);
2950 isl_int_divexact(bmap->ineq[j][1 + dim + div],
2951 bmap->ineq[j][1 + dim + div],
2952 bmap->ineq[l][0]);
2953 if (!valid)
2954 break;
2956 if (j < bmap->n_ineq)
2957 continue;
2958 coalesce = i;
2959 break;
2961 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2962 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2963 return coalesce;
2966 /* Given a lower and an upper bound on div i, construct an inequality
2967 * that when nonnegative ensures that this pair of bounds always allows
2968 * for an integer value of the given div.
2969 * The lower bound is inequality l, while the upper bound is inequality u.
2970 * The constructed inequality is stored in ineq.
2971 * g, fl, fu are temporary scalars.
2973 * Let the upper bound be
2975 * -n_u a + e_u >= 0
2977 * and the lower bound
2979 * n_l a + e_l >= 0
2981 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2982 * We have
2984 * - f_u e_l <= f_u f_l g a <= f_l e_u
2986 * Since all variables are integer valued, this is equivalent to
2988 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2990 * If this interval is at least f_u f_l g, then it contains at least
2991 * one integer value for a.
2992 * That is, the test constraint is
2994 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2996 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
2997 int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
2999 unsigned dim;
3000 dim = isl_space_dim(bmap->dim, isl_dim_all);
3002 isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
3003 isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
3004 isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
3005 isl_int_neg(fu, fu);
3006 isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
3007 1 + dim + bmap->n_div);
3008 isl_int_add(ineq[0], ineq[0], fl);
3009 isl_int_add(ineq[0], ineq[0], fu);
3010 isl_int_sub_ui(ineq[0], ineq[0], 1);
3011 isl_int_mul(g, g, fl);
3012 isl_int_mul(g, g, fu);
3013 isl_int_sub(ineq[0], ineq[0], g);
3016 /* Remove more kinds of divs that are not strictly needed.
3017 * In particular, if all pairs of lower and upper bounds on a div
3018 * are such that they allow at least one integer value of the div,
3019 * the we can eliminate the div using Fourier-Motzkin without
3020 * introducing any spurious solutions.
3022 static struct isl_basic_map *drop_more_redundant_divs(
3023 struct isl_basic_map *bmap, int *pairs, int n)
3025 struct isl_tab *tab = NULL;
3026 struct isl_vec *vec = NULL;
3027 unsigned dim;
3028 int remove = -1;
3029 isl_int g, fl, fu;
3031 isl_int_init(g);
3032 isl_int_init(fl);
3033 isl_int_init(fu);
3035 if (!bmap)
3036 goto error;
3038 dim = isl_space_dim(bmap->dim, isl_dim_all);
3039 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
3040 if (!vec)
3041 goto error;
3043 tab = isl_tab_from_basic_map(bmap, 0);
3045 while (n > 0) {
3046 int i, l, u;
3047 int best = -1;
3048 enum isl_lp_result res;
3050 for (i = 0; i < bmap->n_div; ++i) {
3051 if (!pairs[i])
3052 continue;
3053 if (best >= 0 && pairs[best] <= pairs[i])
3054 continue;
3055 best = i;
3058 i = best;
3059 for (l = 0; l < bmap->n_ineq; ++l) {
3060 if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
3061 continue;
3062 for (u = 0; u < bmap->n_ineq; ++u) {
3063 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
3064 continue;
3065 construct_test_ineq(bmap, i, l, u,
3066 vec->el, g, fl, fu);
3067 res = isl_tab_min(tab, vec->el,
3068 bmap->ctx->one, &g, NULL, 0);
3069 if (res == isl_lp_error)
3070 goto error;
3071 if (res == isl_lp_empty) {
3072 bmap = isl_basic_map_set_to_empty(bmap);
3073 break;
3075 if (res != isl_lp_ok || isl_int_is_neg(g))
3076 break;
3078 if (u < bmap->n_ineq)
3079 break;
3081 if (l == bmap->n_ineq) {
3082 remove = i;
3083 break;
3085 pairs[i] = 0;
3086 --n;
3089 isl_tab_free(tab);
3090 isl_vec_free(vec);
3092 isl_int_clear(g);
3093 isl_int_clear(fl);
3094 isl_int_clear(fu);
3096 free(pairs);
3098 if (remove < 0)
3099 return bmap;
3101 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
3102 return isl_basic_map_drop_redundant_divs(bmap);
3103 error:
3104 free(pairs);
3105 isl_basic_map_free(bmap);
3106 isl_tab_free(tab);
3107 isl_vec_free(vec);
3108 isl_int_clear(g);
3109 isl_int_clear(fl);
3110 isl_int_clear(fu);
3111 return NULL;
3114 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
3115 * and the upper bound u, div1 always occurs together with div2 in the form
3116 * (div1 + m div2), where m is the constant range on the variable div1
3117 * allowed by l and u, replace the pair div1 and div2 by a single
3118 * div that is equal to div1 + m div2.
3120 * The new div will appear in the location that contains div2.
3121 * We need to modify all constraints that contain
3122 * div2 = (div - div1) / m
3123 * (If a constraint does not contain div2, it will also not contain div1.)
3124 * If the constraint also contains div1, then we know they appear
3125 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
3126 * i.e., the coefficient of div is f.
3128 * Otherwise, we first need to introduce div1 into the constraint.
3129 * Let the l be
3131 * div1 + f >=0
3133 * and u
3135 * -div1 + f' >= 0
3137 * A lower bound on div2
3139 * n div2 + t >= 0
3141 * can be replaced by
3143 * (n * (m div 2 + div1) + m t + n f)/g >= 0
3145 * with g = gcd(m,n).
3146 * An upper bound
3148 * -n div2 + t >= 0
3150 * can be replaced by
3152 * (-n * (m div2 + div1) + m t + n f')/g >= 0
3154 * These constraint are those that we would obtain from eliminating
3155 * div1 using Fourier-Motzkin.
3157 * After all constraints have been modified, we drop the lower and upper
3158 * bound and then drop div1.
3160 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
3161 unsigned div1, unsigned div2, unsigned l, unsigned u)
3163 isl_int a;
3164 isl_int b;
3165 isl_int m;
3166 unsigned dim, total;
3167 int i;
3169 dim = isl_space_dim(bmap->dim, isl_dim_all);
3170 total = 1 + dim + bmap->n_div;
3172 isl_int_init(a);
3173 isl_int_init(b);
3174 isl_int_init(m);
3175 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
3176 isl_int_add_ui(m, m, 1);
3178 for (i = 0; i < bmap->n_ineq; ++i) {
3179 if (i == l || i == u)
3180 continue;
3181 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
3182 continue;
3183 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
3184 isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
3185 isl_int_divexact(a, m, b);
3186 isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
3187 if (isl_int_is_pos(b)) {
3188 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3189 b, bmap->ineq[l], total);
3190 } else {
3191 isl_int_neg(b, b);
3192 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
3193 b, bmap->ineq[u], total);
3196 isl_int_set(bmap->ineq[i][1 + dim + div2],
3197 bmap->ineq[i][1 + dim + div1]);
3198 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
3201 isl_int_clear(a);
3202 isl_int_clear(b);
3203 isl_int_clear(m);
3204 if (l > u) {
3205 isl_basic_map_drop_inequality(bmap, l);
3206 isl_basic_map_drop_inequality(bmap, u);
3207 } else {
3208 isl_basic_map_drop_inequality(bmap, u);
3209 isl_basic_map_drop_inequality(bmap, l);
3211 bmap = isl_basic_map_drop_div(bmap, div1);
3212 return bmap;
3215 /* First check if we can coalesce any pair of divs and
3216 * then continue with dropping more redundant divs.
3218 * We loop over all pairs of lower and upper bounds on a div
3219 * with coefficient 1 and -1, respectively, check if there
3220 * is any other div "c" with which we can coalesce the div
3221 * and if so, perform the coalescing.
3223 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
3224 struct isl_basic_map *bmap, int *pairs, int n)
3226 int i, l, u;
3227 unsigned dim;
3229 dim = isl_space_dim(bmap->dim, isl_dim_all);
3231 for (i = 0; i < bmap->n_div; ++i) {
3232 if (!pairs[i])
3233 continue;
3234 for (l = 0; l < bmap->n_ineq; ++l) {
3235 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
3236 continue;
3237 for (u = 0; u < bmap->n_ineq; ++u) {
3238 int c;
3240 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
3241 continue;
3242 c = div_find_coalesce(bmap, pairs, i, l, u);
3243 if (c < 0)
3244 continue;
3245 free(pairs);
3246 bmap = coalesce_divs(bmap, i, c, l, u);
3247 return isl_basic_map_drop_redundant_divs(bmap);
3252 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3253 return bmap;
3255 return drop_more_redundant_divs(bmap, pairs, n);
3258 /* Remove divs that are not strictly needed.
3259 * In particular, if a div only occurs positively (or negatively)
3260 * in constraints, then it can simply be dropped.
3261 * Also, if a div occurs in only two constraints and if moreover
3262 * those two constraints are opposite to each other, except for the constant
3263 * term and if the sum of the constant terms is such that for any value
3264 * of the other values, there is always at least one integer value of the
3265 * div, i.e., if one plus this sum is greater than or equal to
3266 * the (absolute value) of the coefficent of the div in the constraints,
3267 * then we can also simply drop the div.
3269 * We skip divs that appear in equalities or in the definition of other divs.
3270 * Divs that appear in the definition of other divs usually occur in at least
3271 * 4 constraints, but the constraints may have been simplified.
3273 * If any divs are left after these simple checks then we move on
3274 * to more complicated cases in drop_more_redundant_divs.
3276 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
3277 struct isl_basic_map *bmap)
3279 int i, j;
3280 unsigned off;
3281 int *pairs = NULL;
3282 int n = 0;
3284 if (!bmap)
3285 goto error;
3286 if (bmap->n_div == 0)
3287 return bmap;
3289 off = isl_space_dim(bmap->dim, isl_dim_all);
3290 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
3291 if (!pairs)
3292 goto error;
3294 for (i = 0; i < bmap->n_div; ++i) {
3295 int pos, neg;
3296 int last_pos, last_neg;
3297 int redundant;
3298 int defined;
3300 defined = !isl_int_is_zero(bmap->div[i][0]);
3301 for (j = i; j < bmap->n_div; ++j)
3302 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
3303 break;
3304 if (j < bmap->n_div)
3305 continue;
3306 for (j = 0; j < bmap->n_eq; ++j)
3307 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
3308 break;
3309 if (j < bmap->n_eq)
3310 continue;
3311 ++n;
3312 pos = neg = 0;
3313 for (j = 0; j < bmap->n_ineq; ++j) {
3314 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
3315 last_pos = j;
3316 ++pos;
3318 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
3319 last_neg = j;
3320 ++neg;
3323 pairs[i] = pos * neg;
3324 if (pairs[i] == 0) {
3325 for (j = bmap->n_ineq - 1; j >= 0; --j)
3326 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
3327 isl_basic_map_drop_inequality(bmap, j);
3328 bmap = isl_basic_map_drop_div(bmap, i);
3329 free(pairs);
3330 return isl_basic_map_drop_redundant_divs(bmap);
3332 if (pairs[i] != 1)
3333 continue;
3334 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
3335 bmap->ineq[last_neg] + 1,
3336 off + bmap->n_div))
3337 continue;
3339 isl_int_add(bmap->ineq[last_pos][0],
3340 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3341 isl_int_add_ui(bmap->ineq[last_pos][0],
3342 bmap->ineq[last_pos][0], 1);
3343 redundant = isl_int_ge(bmap->ineq[last_pos][0],
3344 bmap->ineq[last_pos][1+off+i]);
3345 isl_int_sub_ui(bmap->ineq[last_pos][0],
3346 bmap->ineq[last_pos][0], 1);
3347 isl_int_sub(bmap->ineq[last_pos][0],
3348 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
3349 if (!redundant) {
3350 if (defined ||
3351 !ok_to_set_div_from_bound(bmap, i, last_pos)) {
3352 pairs[i] = 0;
3353 --n;
3354 continue;
3356 bmap = set_div_from_lower_bound(bmap, i, last_pos);
3357 bmap = isl_basic_map_simplify(bmap);
3358 free(pairs);
3359 return isl_basic_map_drop_redundant_divs(bmap);
3361 if (last_pos > last_neg) {
3362 isl_basic_map_drop_inequality(bmap, last_pos);
3363 isl_basic_map_drop_inequality(bmap, last_neg);
3364 } else {
3365 isl_basic_map_drop_inequality(bmap, last_neg);
3366 isl_basic_map_drop_inequality(bmap, last_pos);
3368 bmap = isl_basic_map_drop_div(bmap, i);
3369 free(pairs);
3370 return isl_basic_map_drop_redundant_divs(bmap);
3373 if (n > 0)
3374 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
3376 free(pairs);
3377 return bmap;
3378 error:
3379 free(pairs);
3380 isl_basic_map_free(bmap);
3381 return NULL;
3384 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
3385 struct isl_basic_set *bset)
3387 return (struct isl_basic_set *)
3388 isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
3391 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
3393 int i;
3395 if (!map)
3396 return NULL;
3397 for (i = 0; i < map->n; ++i) {
3398 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
3399 if (!map->p[i])
3400 goto error;
3402 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3403 return map;
3404 error:
3405 isl_map_free(map);
3406 return NULL;
3409 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
3411 return (struct isl_set *)
3412 isl_map_drop_redundant_divs((struct isl_map *)set);
3415 /* Does "bmap" satisfy any equality that involves more than 2 variables
3416 * and/or has coefficients different from -1 and 1?
3418 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
3420 int i;
3421 unsigned total;
3423 total = isl_basic_map_dim(bmap, isl_dim_all);
3425 for (i = 0; i < bmap->n_eq; ++i) {
3426 int j, k;
3428 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
3429 if (j < 0)
3430 continue;
3431 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3432 !isl_int_is_negone(bmap->eq[i][1 + j]))
3433 return 1;
3435 j += 1;
3436 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3437 if (k < 0)
3438 continue;
3439 j += k;
3440 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
3441 !isl_int_is_negone(bmap->eq[i][1 + j]))
3442 return 1;
3444 j += 1;
3445 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
3446 if (k >= 0)
3447 return 1;
3450 return 0;
3453 /* Remove any common factor g from the constraint coefficients in "v".
3454 * The constant term is stored in the first position and is replaced
3455 * by floor(c/g). If any common factor is removed and if this results
3456 * in a tightening of the constraint, then set *tightened.
3458 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
3459 int *tightened)
3461 isl_ctx *ctx;
3463 if (!v)
3464 return NULL;
3465 ctx = isl_vec_get_ctx(v);
3466 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
3467 if (isl_int_is_zero(ctx->normalize_gcd))
3468 return v;
3469 if (isl_int_is_one(ctx->normalize_gcd))
3470 return v;
3471 v = isl_vec_cow(v);
3472 if (!v)
3473 return NULL;
3474 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
3475 *tightened = 1;
3476 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
3477 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
3478 v->size - 1);
3479 return v;
3482 /* If "bmap" is an integer set that satisfies any equality involving
3483 * more than 2 variables and/or has coefficients different from -1 and 1,
3484 * then use variable compression to reduce the coefficients by removing
3485 * any (hidden) common factor.
3486 * In particular, apply the variable compression to each constraint,
3487 * factor out any common factor in the non-constant coefficients and
3488 * then apply the inverse of the compression.
3489 * At the end, we mark the basic map as having reduced constants.
3490 * If this flag is still set on the next invocation of this function,
3491 * then we skip the computation.
3493 * Removing a common factor may result in a tightening of some of
3494 * the constraints. If this happens, then we may end up with two
3495 * opposite inequalities that can be replaced by an equality.
3496 * We therefore call isl_basic_map_detect_inequality_pairs,
3497 * which checks for such pairs of inequalities as well as eliminate_divs_eq
3498 * and isl_basic_map_gauss if such a pair was found.
3500 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
3501 __isl_take isl_basic_map *bmap)
3503 unsigned total;
3504 isl_ctx *ctx;
3505 isl_vec *v;
3506 isl_mat *eq, *T, *T2;
3507 int i;
3508 int tightened;
3510 if (!bmap)
3511 return NULL;
3512 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
3513 return bmap;
3514 if (isl_basic_map_is_rational(bmap))
3515 return bmap;
3516 if (bmap->n_eq == 0)
3517 return bmap;
3518 if (!has_multiple_var_equality(bmap))
3519 return bmap;
3521 total = isl_basic_map_dim(bmap, isl_dim_all);
3522 ctx = isl_basic_map_get_ctx(bmap);
3523 v = isl_vec_alloc(ctx, 1 + total);
3524 if (!v)
3525 return isl_basic_map_free(bmap);
3527 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3528 T = isl_mat_variable_compression(eq, &T2);
3529 if (!T || !T2)
3530 goto error;
3531 if (T->n_col == 0) {
3532 isl_mat_free(T);
3533 isl_mat_free(T2);
3534 isl_vec_free(v);
3535 return isl_basic_map_set_to_empty(bmap);
3538 tightened = 0;
3539 for (i = 0; i < bmap->n_ineq; ++i) {
3540 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
3541 v = isl_vec_mat_product(v, isl_mat_copy(T));
3542 v = normalize_constraint(v, &tightened);
3543 v = isl_vec_mat_product(v, isl_mat_copy(T2));
3544 if (!v)
3545 goto error;
3546 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
3549 isl_mat_free(T);
3550 isl_mat_free(T2);
3551 isl_vec_free(v);
3553 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
3555 if (tightened) {
3556 int progress = 0;
3558 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
3559 if (progress) {
3560 bmap = eliminate_divs_eq(bmap, &progress);
3561 bmap = isl_basic_map_gauss(bmap, NULL);
3565 return bmap;
3566 error:
3567 isl_mat_free(T);
3568 isl_mat_free(T2);
3569 isl_vec_free(v);
3570 return isl_basic_map_free(bmap);
3573 /* Shift the integer division at position "div" of "bmap"
3574 * by "shift" times the variable at position "pos".
3575 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
3576 * corresponds to the constant term.
3578 * That is, if the integer division has the form
3580 * floor(f(x)/d)
3582 * then replace it by
3584 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
3586 __isl_give isl_basic_map *isl_basic_map_shift_div(
3587 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
3589 int i;
3590 unsigned total;
3592 if (!bmap)
3593 return NULL;
3595 total = isl_basic_map_dim(bmap, isl_dim_all);
3596 total -= isl_basic_map_dim(bmap, isl_dim_div);
3598 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
3600 for (i = 0; i < bmap->n_eq; ++i) {
3601 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
3602 continue;
3603 isl_int_submul(bmap->eq[i][pos],
3604 shift, bmap->eq[i][1 + total + div]);
3606 for (i = 0; i < bmap->n_ineq; ++i) {
3607 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
3608 continue;
3609 isl_int_submul(bmap->ineq[i][pos],
3610 shift, bmap->ineq[i][1 + total + div]);
3612 for (i = 0; i < bmap->n_div; ++i) {
3613 if (isl_int_is_zero(bmap->div[i][0]))
3614 continue;
3615 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
3616 continue;
3617 isl_int_submul(bmap->div[i][1 + pos],
3618 shift, bmap->div[i][1 + 1 + total + div]);
3621 return bmap;