interface/python.cc: document order of superclasses
[isl.git] / isl_map_simplify.c
blob7acd2a7a76d055b3f03a3aef3fd62f108df03bd8
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
49 isl_seq_cpy(c, c + n, rem);
50 isl_seq_clr(c + rem, n);
53 /* Drop n dimensions starting at first.
55 * In principle, this frees up some extra variables as the number
56 * of columns remains constant, but we would have to extend
57 * the div array too as the number of rows in this array is assumed
58 * to be equal to extra.
60 struct isl_basic_set *isl_basic_set_drop_dims(
61 struct isl_basic_set *bset, unsigned first, unsigned n)
63 int i;
65 if (!bset)
66 goto error;
68 isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
70 if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
71 return bset;
73 bset = isl_basic_set_cow(bset);
74 if (!bset)
75 return NULL;
77 for (i = 0; i < bset->n_eq; ++i)
78 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
79 (bset->dim->n_out-first-n)+bset->extra);
81 for (i = 0; i < bset->n_ineq; ++i)
82 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
83 (bset->dim->n_out-first-n)+bset->extra);
85 for (i = 0; i < bset->n_div; ++i)
86 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
87 (bset->dim->n_out-first-n)+bset->extra);
89 bset->dim = isl_space_drop_outputs(bset->dim, first, n);
90 if (!bset->dim)
91 goto error;
93 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
94 bset = isl_basic_set_simplify(bset);
95 return isl_basic_set_finalize(bset);
96 error:
97 isl_basic_set_free(bset);
98 return NULL;
101 struct isl_set *isl_set_drop_dims(
102 struct isl_set *set, unsigned first, unsigned n)
104 int i;
106 if (!set)
107 goto error;
109 isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
111 if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
112 return set;
113 set = isl_set_cow(set);
114 if (!set)
115 goto error;
116 set->dim = isl_space_drop_outputs(set->dim, first, n);
117 if (!set->dim)
118 goto error;
120 for (i = 0; i < set->n; ++i) {
121 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
122 if (!set->p[i])
123 goto error;
126 ISL_F_CLR(set, ISL_SET_NORMALIZED);
127 return set;
128 error:
129 isl_set_free(set);
130 return NULL;
133 /* Move "n" divs starting at "first" to the end of the list of divs.
135 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
136 unsigned first, unsigned n)
138 isl_int **div;
139 int i;
141 if (first + n == bmap->n_div)
142 return bmap;
144 div = isl_alloc_array(bmap->ctx, isl_int *, n);
145 if (!div)
146 goto error;
147 for (i = 0; i < n; ++i)
148 div[i] = bmap->div[first + i];
149 for (i = 0; i < bmap->n_div - first - n; ++i)
150 bmap->div[first + i] = bmap->div[first + n + i];
151 for (i = 0; i < n; ++i)
152 bmap->div[bmap->n_div - n + i] = div[i];
153 free(div);
154 return bmap;
155 error:
156 isl_basic_map_free(bmap);
157 return NULL;
160 /* Drop "n" dimensions of type "type" starting at "first".
162 * In principle, this frees up some extra variables as the number
163 * of columns remains constant, but we would have to extend
164 * the div array too as the number of rows in this array is assumed
165 * to be equal to extra.
167 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
168 enum isl_dim_type type, unsigned first, unsigned n)
170 int i;
171 unsigned dim;
172 unsigned offset;
173 unsigned left;
175 if (!bmap)
176 goto error;
178 dim = isl_basic_map_dim(bmap, type);
179 isl_assert(bmap->ctx, first + n <= dim, goto error);
181 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
182 return bmap;
184 bmap = isl_basic_map_cow(bmap);
185 if (!bmap)
186 return NULL;
188 offset = isl_basic_map_offset(bmap, type) + first;
189 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
190 for (i = 0; i < bmap->n_eq; ++i)
191 constraint_drop_vars(bmap->eq[i]+offset, n, left);
193 for (i = 0; i < bmap->n_ineq; ++i)
194 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
196 for (i = 0; i < bmap->n_div; ++i)
197 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
199 if (type == isl_dim_div) {
200 bmap = move_divs_last(bmap, first, n);
201 if (!bmap)
202 goto error;
203 isl_basic_map_free_div(bmap, n);
204 } else
205 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
206 if (!bmap->dim)
207 goto error;
209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
210 bmap = isl_basic_map_simplify(bmap);
211 return isl_basic_map_finalize(bmap);
212 error:
213 isl_basic_map_free(bmap);
214 return NULL;
217 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
218 enum isl_dim_type type, unsigned first, unsigned n)
220 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
221 type, first, n));
224 struct isl_basic_map *isl_basic_map_drop_inputs(
225 struct isl_basic_map *bmap, unsigned first, unsigned n)
227 return isl_basic_map_drop(bmap, isl_dim_in, first, n);
230 struct isl_map *isl_map_drop(struct isl_map *map,
231 enum isl_dim_type type, unsigned first, unsigned n)
233 int i;
235 if (!map)
236 goto error;
238 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
240 if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
241 return map;
242 map = isl_map_cow(map);
243 if (!map)
244 goto error;
245 map->dim = isl_space_drop_dims(map->dim, type, first, n);
246 if (!map->dim)
247 goto error;
249 for (i = 0; i < map->n; ++i) {
250 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
251 if (!map->p[i])
252 goto error;
254 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
256 return map;
257 error:
258 isl_map_free(map);
259 return NULL;
262 struct isl_set *isl_set_drop(struct isl_set *set,
263 enum isl_dim_type type, unsigned first, unsigned n)
265 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
268 struct isl_map *isl_map_drop_inputs(
269 struct isl_map *map, unsigned first, unsigned n)
271 return isl_map_drop(map, isl_dim_in, first, n);
275 * We don't cow, as the div is assumed to be redundant.
277 __isl_give isl_basic_map *isl_basic_map_drop_div(
278 __isl_take isl_basic_map *bmap, unsigned div)
280 int i;
281 unsigned pos;
283 if (!bmap)
284 goto error;
286 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
288 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
290 for (i = 0; i < bmap->n_eq; ++i)
291 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
293 for (i = 0; i < bmap->n_ineq; ++i) {
294 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
295 isl_basic_map_drop_inequality(bmap, i);
296 --i;
297 continue;
299 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
302 for (i = 0; i < bmap->n_div; ++i)
303 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
305 if (div != bmap->n_div - 1) {
306 int j;
307 isl_int *t = bmap->div[div];
309 for (j = div; j < bmap->n_div - 1; ++j)
310 bmap->div[j] = bmap->div[j+1];
312 bmap->div[bmap->n_div - 1] = t;
314 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
315 isl_basic_map_free_div(bmap, 1);
317 return bmap;
318 error:
319 isl_basic_map_free(bmap);
320 return NULL;
323 struct isl_basic_map *isl_basic_map_normalize_constraints(
324 struct isl_basic_map *bmap)
326 int i;
327 isl_int gcd;
328 unsigned total = isl_basic_map_total_dim(bmap);
330 if (!bmap)
331 return NULL;
333 isl_int_init(gcd);
334 for (i = bmap->n_eq - 1; i >= 0; --i) {
335 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
336 if (isl_int_is_zero(gcd)) {
337 if (!isl_int_is_zero(bmap->eq[i][0])) {
338 bmap = isl_basic_map_set_to_empty(bmap);
339 break;
341 isl_basic_map_drop_equality(bmap, i);
342 continue;
344 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
345 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
346 if (isl_int_is_one(gcd))
347 continue;
348 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
349 bmap = isl_basic_map_set_to_empty(bmap);
350 break;
352 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
355 for (i = bmap->n_ineq - 1; i >= 0; --i) {
356 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
357 if (isl_int_is_zero(gcd)) {
358 if (isl_int_is_neg(bmap->ineq[i][0])) {
359 bmap = isl_basic_map_set_to_empty(bmap);
360 break;
362 isl_basic_map_drop_inequality(bmap, i);
363 continue;
365 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
366 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
367 if (isl_int_is_one(gcd))
368 continue;
369 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
370 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
372 isl_int_clear(gcd);
374 return bmap;
377 struct isl_basic_set *isl_basic_set_normalize_constraints(
378 struct isl_basic_set *bset)
380 isl_basic_map *bmap = bset_to_bmap(bset);
381 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
384 /* Assuming the variable at position "pos" has an integer coefficient
385 * in integer division "div", extract it from this integer division.
386 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
387 * corresponds to the constant term.
389 * That is, the integer division is of the form
391 * floor((... + c * d * x_pos + ...)/d)
393 * Replace it by
395 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
397 static __isl_give isl_basic_map *remove_var_from_div(
398 __isl_take isl_basic_map *bmap, int div, int pos)
400 isl_int shift;
402 isl_int_init(shift);
403 isl_int_divexact(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
404 isl_int_neg(shift, shift);
405 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
406 isl_int_clear(shift);
408 return bmap;
411 /* Check if integer division "div" has any integral coefficient
412 * (or constant term). If so, extract them from the integer division.
414 static __isl_give isl_basic_map *remove_independent_vars_from_div(
415 __isl_take isl_basic_map *bmap, int div)
417 int i;
418 unsigned total = 1 + isl_basic_map_total_dim(bmap);
420 for (i = 0; i < total; ++i) {
421 if (isl_int_is_zero(bmap->div[div][1 + i]))
422 continue;
423 if (!isl_int_is_divisible_by(bmap->div[div][1 + i],
424 bmap->div[div][0]))
425 continue;
426 bmap = remove_var_from_div(bmap, div, i);
427 if (!bmap)
428 break;
431 return bmap;
434 /* Check if any known integer division has any integral coefficient
435 * (or constant term). If so, extract them from the integer division.
437 static __isl_give isl_basic_map *remove_independent_vars_from_divs(
438 __isl_take isl_basic_map *bmap)
440 int i;
442 if (!bmap)
443 return NULL;
444 if (bmap->n_div == 0)
445 return bmap;
447 for (i = 0; i < bmap->n_div; ++i) {
448 if (isl_int_is_zero(bmap->div[i][0]))
449 continue;
450 bmap = remove_independent_vars_from_div(bmap, i);
451 if (!bmap)
452 break;
455 return bmap;
458 /* Remove any common factor in numerator and denominator of the div expression,
459 * not taking into account the constant term.
460 * That is, if the div is of the form
462 * floor((a + m f(x))/(m d))
464 * then replace it by
466 * floor((floor(a/m) + f(x))/d)
468 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
469 * and can therefore not influence the result of the floor.
471 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
473 unsigned total = isl_basic_map_total_dim(bmap);
474 isl_ctx *ctx = bmap->ctx;
476 if (isl_int_is_zero(bmap->div[div][0]))
477 return;
478 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
479 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
480 if (isl_int_is_one(ctx->normalize_gcd))
481 return;
482 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
483 ctx->normalize_gcd);
484 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
485 ctx->normalize_gcd);
486 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
487 ctx->normalize_gcd, total);
490 /* Remove any common factor in numerator and denominator of a div expression,
491 * not taking into account the constant term.
492 * That is, look for any div of the form
494 * floor((a + m f(x))/(m d))
496 * and replace it by
498 * floor((floor(a/m) + f(x))/d)
500 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
501 * and can therefore not influence the result of the floor.
503 static __isl_give isl_basic_map *normalize_div_expressions(
504 __isl_take isl_basic_map *bmap)
506 int i;
508 if (!bmap)
509 return NULL;
510 if (bmap->n_div == 0)
511 return bmap;
513 for (i = 0; i < bmap->n_div; ++i)
514 normalize_div_expression(bmap, i);
516 return bmap;
519 /* Assumes divs have been ordered if keep_divs is set.
521 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
522 unsigned pos, isl_int *eq, int keep_divs, int *progress)
524 unsigned total;
525 unsigned space_total;
526 int k;
527 int last_div;
529 total = isl_basic_map_total_dim(bmap);
530 space_total = isl_space_dim(bmap->dim, isl_dim_all);
531 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
532 for (k = 0; k < bmap->n_eq; ++k) {
533 if (bmap->eq[k] == eq)
534 continue;
535 if (isl_int_is_zero(bmap->eq[k][1+pos]))
536 continue;
537 if (progress)
538 *progress = 1;
539 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
540 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
543 for (k = 0; k < bmap->n_ineq; ++k) {
544 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
545 continue;
546 if (progress)
547 *progress = 1;
548 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
549 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
550 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
553 for (k = 0; k < bmap->n_div; ++k) {
554 if (isl_int_is_zero(bmap->div[k][0]))
555 continue;
556 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
557 continue;
558 if (progress)
559 *progress = 1;
560 /* We need to be careful about circular definitions,
561 * so for now we just remove the definition of div k
562 * if the equality contains any divs.
563 * If keep_divs is set, then the divs have been ordered
564 * and we can keep the definition as long as the result
565 * is still ordered.
567 if (last_div == -1 || (keep_divs && last_div < k)) {
568 isl_seq_elim(bmap->div[k]+1, eq,
569 1+pos, 1+total, &bmap->div[k][0]);
570 normalize_div_expression(bmap, k);
571 } else
572 isl_seq_clr(bmap->div[k], 1 + total);
573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
577 /* Assumes divs have been ordered if keep_divs is set.
579 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
580 isl_int *eq, unsigned div, int keep_divs)
582 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
584 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
586 bmap = isl_basic_map_drop_div(bmap, div);
588 return bmap;
591 /* Check if elimination of div "div" using equality "eq" would not
592 * result in a div depending on a later div.
594 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
595 unsigned div)
597 int k;
598 int last_div;
599 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
600 unsigned pos = space_total + div;
602 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
603 if (last_div < 0 || last_div <= div)
604 return 1;
606 for (k = 0; k <= last_div; ++k) {
607 if (isl_int_is_zero(bmap->div[k][0]))
608 return 1;
609 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
610 return 0;
613 return 1;
616 /* Elimininate divs based on equalities
618 static struct isl_basic_map *eliminate_divs_eq(
619 struct isl_basic_map *bmap, int *progress)
621 int d;
622 int i;
623 int modified = 0;
624 unsigned off;
626 bmap = isl_basic_map_order_divs(bmap);
628 if (!bmap)
629 return NULL;
631 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
633 for (d = bmap->n_div - 1; d >= 0 ; --d) {
634 for (i = 0; i < bmap->n_eq; ++i) {
635 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
636 !isl_int_is_negone(bmap->eq[i][off + d]))
637 continue;
638 if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
639 continue;
640 modified = 1;
641 *progress = 1;
642 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
643 if (isl_basic_map_drop_equality(bmap, i) < 0)
644 return isl_basic_map_free(bmap);
645 break;
648 if (modified)
649 return eliminate_divs_eq(bmap, progress);
650 return bmap;
653 /* Elimininate divs based on inequalities
655 static struct isl_basic_map *eliminate_divs_ineq(
656 struct isl_basic_map *bmap, int *progress)
658 int d;
659 int i;
660 unsigned off;
661 struct isl_ctx *ctx;
663 if (!bmap)
664 return NULL;
666 ctx = bmap->ctx;
667 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
669 for (d = bmap->n_div - 1; d >= 0 ; --d) {
670 for (i = 0; i < bmap->n_eq; ++i)
671 if (!isl_int_is_zero(bmap->eq[i][off + d]))
672 break;
673 if (i < bmap->n_eq)
674 continue;
675 for (i = 0; i < bmap->n_ineq; ++i)
676 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
677 break;
678 if (i < bmap->n_ineq)
679 continue;
680 *progress = 1;
681 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
682 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
683 break;
684 bmap = isl_basic_map_drop_div(bmap, d);
685 if (!bmap)
686 break;
688 return bmap;
691 struct isl_basic_map *isl_basic_map_gauss(
692 struct isl_basic_map *bmap, int *progress)
694 int k;
695 int done;
696 int last_var;
697 unsigned total_var;
698 unsigned total;
700 bmap = isl_basic_map_order_divs(bmap);
702 if (!bmap)
703 return NULL;
705 total = isl_basic_map_total_dim(bmap);
706 total_var = total - bmap->n_div;
708 last_var = total - 1;
709 for (done = 0; done < bmap->n_eq; ++done) {
710 for (; last_var >= 0; --last_var) {
711 for (k = done; k < bmap->n_eq; ++k)
712 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
713 break;
714 if (k < bmap->n_eq)
715 break;
717 if (last_var < 0)
718 break;
719 if (k != done)
720 swap_equality(bmap, k, done);
721 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
722 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
724 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
725 progress);
727 if (last_var >= total_var &&
728 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
729 unsigned div = last_var - total_var;
730 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
731 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
732 isl_int_set(bmap->div[div][0],
733 bmap->eq[done][1+last_var]);
734 if (progress)
735 *progress = 1;
736 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
739 if (done == bmap->n_eq)
740 return bmap;
741 for (k = done; k < bmap->n_eq; ++k) {
742 if (isl_int_is_zero(bmap->eq[k][0]))
743 continue;
744 return isl_basic_map_set_to_empty(bmap);
746 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
747 return bmap;
750 struct isl_basic_set *isl_basic_set_gauss(
751 struct isl_basic_set *bset, int *progress)
753 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
754 progress));
758 static unsigned int round_up(unsigned int v)
760 int old_v = v;
762 while (v) {
763 old_v = v;
764 v ^= v & -v;
766 return old_v << 1;
769 /* Hash table of inequalities in a basic map.
770 * "index" is an array of addresses of inequalities in the basic map, some
771 * of which are NULL. The inequalities are hashed on the coefficients
772 * except the constant term.
773 * "size" is the number of elements in the array and is always a power of two
774 * "bits" is the number of bits need to represent an index into the array.
775 * "total" is the total dimension of the basic map.
777 struct isl_constraint_index {
778 unsigned int size;
779 int bits;
780 isl_int ***index;
781 unsigned total;
784 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
786 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
787 __isl_keep isl_basic_map *bmap)
789 isl_ctx *ctx;
791 ci->index = NULL;
792 if (!bmap)
793 return isl_stat_error;
794 ci->total = isl_basic_set_total_dim(bmap);
795 if (bmap->n_ineq == 0)
796 return isl_stat_ok;
797 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
798 ci->bits = ffs(ci->size) - 1;
799 ctx = isl_basic_map_get_ctx(bmap);
800 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
801 if (!ci->index)
802 return isl_stat_error;
804 return isl_stat_ok;
807 /* Free the memory allocated by create_constraint_index.
809 static void constraint_index_free(struct isl_constraint_index *ci)
811 free(ci->index);
814 /* Return the position in ci->index that contains the address of
815 * an inequality that is equal to *ineq up to the constant term,
816 * provided this address is not identical to "ineq".
817 * If there is no such inequality, then return the position where
818 * such an inequality should be inserted.
820 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
822 int h;
823 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
824 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
825 if (ineq != ci->index[h] &&
826 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
827 break;
828 return h;
831 /* Return the position in ci->index that contains the address of
832 * an inequality that is equal to the k'th inequality of "bmap"
833 * up to the constant term, provided it does not point to the very
834 * same inequality.
835 * If there is no such inequality, then return the position where
836 * such an inequality should be inserted.
838 static int hash_index(struct isl_constraint_index *ci,
839 __isl_keep isl_basic_map *bmap, int k)
841 return hash_index_ineq(ci, &bmap->ineq[k]);
844 static int set_hash_index(struct isl_constraint_index *ci,
845 struct isl_basic_set *bset, int k)
847 return hash_index(ci, bset, k);
850 /* Fill in the "ci" data structure with the inequalities of "bset".
852 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
853 __isl_keep isl_basic_set *bset)
855 int k, h;
857 if (create_constraint_index(ci, bset) < 0)
858 return isl_stat_error;
860 for (k = 0; k < bset->n_ineq; ++k) {
861 h = set_hash_index(ci, bset, k);
862 ci->index[h] = &bset->ineq[k];
865 return isl_stat_ok;
868 /* Is the inequality ineq (obviously) redundant with respect
869 * to the constraints in "ci"?
871 * Look for an inequality in "ci" with the same coefficients and then
872 * check if the contant term of "ineq" is greater than or equal
873 * to the constant term of that inequality. If so, "ineq" is clearly
874 * redundant.
876 * Note that hash_index_ineq ignores a stored constraint if it has
877 * the same address as the passed inequality. It is ok to pass
878 * the address of a local variable here since it will never be
879 * the same as the address of a constraint in "ci".
881 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
882 isl_int *ineq)
884 int h;
886 h = hash_index_ineq(ci, &ineq);
887 if (!ci->index[h])
888 return isl_bool_false;
889 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
892 /* If we can eliminate more than one div, then we need to make
893 * sure we do it from last div to first div, in order not to
894 * change the position of the other divs that still need to
895 * be removed.
897 static struct isl_basic_map *remove_duplicate_divs(
898 struct isl_basic_map *bmap, int *progress)
900 unsigned int size;
901 int *index;
902 int *elim_for;
903 int k, l, h;
904 int bits;
905 struct isl_blk eq;
906 unsigned total_var;
907 unsigned total;
908 struct isl_ctx *ctx;
910 bmap = isl_basic_map_order_divs(bmap);
911 if (!bmap || bmap->n_div <= 1)
912 return bmap;
914 total_var = isl_space_dim(bmap->dim, isl_dim_all);
915 total = total_var + bmap->n_div;
917 ctx = bmap->ctx;
918 for (k = bmap->n_div - 1; k >= 0; --k)
919 if (!isl_int_is_zero(bmap->div[k][0]))
920 break;
921 if (k <= 0)
922 return bmap;
924 size = round_up(4 * bmap->n_div / 3 - 1);
925 if (size == 0)
926 return bmap;
927 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
928 bits = ffs(size) - 1;
929 index = isl_calloc_array(ctx, int, size);
930 if (!elim_for || !index)
931 goto out;
932 eq = isl_blk_alloc(ctx, 1+total);
933 if (isl_blk_is_error(eq))
934 goto out;
936 isl_seq_clr(eq.data, 1+total);
937 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
938 for (--k; k >= 0; --k) {
939 uint32_t hash;
941 if (isl_int_is_zero(bmap->div[k][0]))
942 continue;
944 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
945 for (h = hash; index[h]; h = (h+1) % size)
946 if (isl_seq_eq(bmap->div[k],
947 bmap->div[index[h]-1], 2+total))
948 break;
949 if (index[h]) {
950 *progress = 1;
951 l = index[h] - 1;
952 elim_for[l] = k + 1;
954 index[h] = k+1;
956 for (l = bmap->n_div - 1; l >= 0; --l) {
957 if (!elim_for[l])
958 continue;
959 k = elim_for[l] - 1;
960 isl_int_set_si(eq.data[1+total_var+k], -1);
961 isl_int_set_si(eq.data[1+total_var+l], 1);
962 bmap = eliminate_div(bmap, eq.data, l, 1);
963 if (!bmap)
964 break;
965 isl_int_set_si(eq.data[1+total_var+k], 0);
966 isl_int_set_si(eq.data[1+total_var+l], 0);
969 isl_blk_free(ctx, eq);
970 out:
971 free(index);
972 free(elim_for);
973 return bmap;
976 static int n_pure_div_eq(struct isl_basic_map *bmap)
978 int i, j;
979 unsigned total;
981 total = isl_space_dim(bmap->dim, isl_dim_all);
982 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
983 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
984 --j;
985 if (j < 0)
986 break;
987 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
988 return 0;
990 return i;
993 /* Normalize divs that appear in equalities.
995 * In particular, we assume that bmap contains some equalities
996 * of the form
998 * a x = m * e_i
1000 * and we want to replace the set of e_i by a minimal set and
1001 * such that the new e_i have a canonical representation in terms
1002 * of the vector x.
1003 * If any of the equalities involves more than one divs, then
1004 * we currently simply bail out.
1006 * Let us first additionally assume that all equalities involve
1007 * a div. The equalities then express modulo constraints on the
1008 * remaining variables and we can use "parameter compression"
1009 * to find a minimal set of constraints. The result is a transformation
1011 * x = T(x') = x_0 + G x'
1013 * with G a lower-triangular matrix with all elements below the diagonal
1014 * non-negative and smaller than the diagonal element on the same row.
1015 * We first normalize x_0 by making the same property hold in the affine
1016 * T matrix.
1017 * The rows i of G with a 1 on the diagonal do not impose any modulo
1018 * constraint and simply express x_i = x'_i.
1019 * For each of the remaining rows i, we introduce a div and a corresponding
1020 * equality. In particular
1022 * g_ii e_j = x_i - g_i(x')
1024 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1025 * corresponding div (if g_kk != 1).
1027 * If there are any equalities not involving any div, then we
1028 * first apply a variable compression on the variables x:
1030 * x = C x'' x'' = C_2 x
1032 * and perform the above parameter compression on A C instead of on A.
1033 * The resulting compression is then of the form
1035 * x'' = T(x') = x_0 + G x'
1037 * and in constructing the new divs and the corresponding equalities,
1038 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1039 * by the corresponding row from C_2.
1041 static struct isl_basic_map *normalize_divs(
1042 struct isl_basic_map *bmap, int *progress)
1044 int i, j, k;
1045 int total;
1046 int div_eq;
1047 struct isl_mat *B;
1048 struct isl_vec *d;
1049 struct isl_mat *T = NULL;
1050 struct isl_mat *C = NULL;
1051 struct isl_mat *C2 = NULL;
1052 isl_int v;
1053 int *pos;
1054 int dropped, needed;
1056 if (!bmap)
1057 return NULL;
1059 if (bmap->n_div == 0)
1060 return bmap;
1062 if (bmap->n_eq == 0)
1063 return bmap;
1065 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1066 return bmap;
1068 total = isl_space_dim(bmap->dim, isl_dim_all);
1069 div_eq = n_pure_div_eq(bmap);
1070 if (div_eq == 0)
1071 return bmap;
1073 if (div_eq < bmap->n_eq) {
1074 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1075 bmap->n_eq - div_eq, 0, 1 + total);
1076 C = isl_mat_variable_compression(B, &C2);
1077 if (!C || !C2)
1078 goto error;
1079 if (C->n_col == 0) {
1080 bmap = isl_basic_map_set_to_empty(bmap);
1081 isl_mat_free(C);
1082 isl_mat_free(C2);
1083 goto done;
1087 d = isl_vec_alloc(bmap->ctx, div_eq);
1088 if (!d)
1089 goto error;
1090 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1091 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1092 --j;
1093 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1095 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1097 if (C) {
1098 B = isl_mat_product(B, C);
1099 C = NULL;
1102 T = isl_mat_parameter_compression(B, d);
1103 if (!T)
1104 goto error;
1105 if (T->n_col == 0) {
1106 bmap = isl_basic_map_set_to_empty(bmap);
1107 isl_mat_free(C2);
1108 isl_mat_free(T);
1109 goto done;
1111 isl_int_init(v);
1112 for (i = 0; i < T->n_row - 1; ++i) {
1113 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1114 if (isl_int_is_zero(v))
1115 continue;
1116 isl_mat_col_submul(T, 0, v, 1 + i);
1118 isl_int_clear(v);
1119 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1120 if (!pos)
1121 goto error;
1122 /* We have to be careful because dropping equalities may reorder them */
1123 dropped = 0;
1124 for (j = bmap->n_div - 1; j >= 0; --j) {
1125 for (i = 0; i < bmap->n_eq; ++i)
1126 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1127 break;
1128 if (i < bmap->n_eq) {
1129 bmap = isl_basic_map_drop_div(bmap, j);
1130 isl_basic_map_drop_equality(bmap, i);
1131 ++dropped;
1134 pos[0] = 0;
1135 needed = 0;
1136 for (i = 1; i < T->n_row; ++i) {
1137 if (isl_int_is_one(T->row[i][i]))
1138 pos[i] = i;
1139 else
1140 needed++;
1142 if (needed > dropped) {
1143 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1144 needed, needed, 0);
1145 if (!bmap)
1146 goto error;
1148 for (i = 1; i < T->n_row; ++i) {
1149 if (isl_int_is_one(T->row[i][i]))
1150 continue;
1151 k = isl_basic_map_alloc_div(bmap);
1152 pos[i] = 1 + total + k;
1153 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1154 isl_int_set(bmap->div[k][0], T->row[i][i]);
1155 if (C2)
1156 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1157 else
1158 isl_int_set_si(bmap->div[k][1 + i], 1);
1159 for (j = 0; j < i; ++j) {
1160 if (isl_int_is_zero(T->row[i][j]))
1161 continue;
1162 if (pos[j] < T->n_row && C2)
1163 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1164 C2->row[pos[j]], 1 + total);
1165 else
1166 isl_int_neg(bmap->div[k][1 + pos[j]],
1167 T->row[i][j]);
1169 j = isl_basic_map_alloc_equality(bmap);
1170 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1171 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1173 free(pos);
1174 isl_mat_free(C2);
1175 isl_mat_free(T);
1177 if (progress)
1178 *progress = 1;
1179 done:
1180 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1182 return bmap;
1183 error:
1184 isl_mat_free(C);
1185 isl_mat_free(C2);
1186 isl_mat_free(T);
1187 return bmap;
1190 static struct isl_basic_map *set_div_from_lower_bound(
1191 struct isl_basic_map *bmap, int div, int ineq)
1193 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1195 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1196 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1197 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1198 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1199 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1201 return bmap;
1204 /* Check whether it is ok to define a div based on an inequality.
1205 * To avoid the introduction of circular definitions of divs, we
1206 * do not allow such a definition if the resulting expression would refer to
1207 * any other undefined divs or if any known div is defined in
1208 * terms of the unknown div.
1210 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1211 int div, int ineq)
1213 int j;
1214 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1216 /* Not defined in terms of unknown divs */
1217 for (j = 0; j < bmap->n_div; ++j) {
1218 if (div == j)
1219 continue;
1220 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1221 continue;
1222 if (isl_int_is_zero(bmap->div[j][0]))
1223 return 0;
1226 /* No other div defined in terms of this one => avoid loops */
1227 for (j = 0; j < bmap->n_div; ++j) {
1228 if (div == j)
1229 continue;
1230 if (isl_int_is_zero(bmap->div[j][0]))
1231 continue;
1232 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1233 return 0;
1236 return 1;
1239 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1240 * be a better expression than the current one?
1242 * If we do not have any expression yet, then any expression would be better.
1243 * Otherwise we check if the last variable involved in the inequality
1244 * (disregarding the div that it would define) is in an earlier position
1245 * than the last variable involved in the current div expression.
1247 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1248 int div, int ineq)
1250 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1251 int last_div;
1252 int last_ineq;
1254 if (isl_int_is_zero(bmap->div[div][0]))
1255 return 1;
1257 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1258 bmap->n_div - (div + 1)) >= 0)
1259 return 0;
1261 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1262 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1263 total + bmap->n_div);
1265 return last_ineq < last_div;
1268 /* Given two constraints "k" and "l" that are opposite to each other,
1269 * except for the constant term, check if we can use them
1270 * to obtain an expression for one of the hitherto unknown divs or
1271 * a "better" expression for a div for which we already have an expression.
1272 * "sum" is the sum of the constant terms of the constraints.
1273 * If this sum is strictly smaller than the coefficient of one
1274 * of the divs, then this pair can be used define the div.
1275 * To avoid the introduction of circular definitions of divs, we
1276 * do not use the pair if the resulting expression would refer to
1277 * any other undefined divs or if any known div is defined in
1278 * terms of the unknown div.
1280 static struct isl_basic_map *check_for_div_constraints(
1281 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1283 int i;
1284 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1286 for (i = 0; i < bmap->n_div; ++i) {
1287 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1288 continue;
1289 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1290 continue;
1291 if (!better_div_constraint(bmap, i, k))
1292 continue;
1293 if (!ok_to_set_div_from_bound(bmap, i, k))
1294 break;
1295 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1296 bmap = set_div_from_lower_bound(bmap, i, k);
1297 else
1298 bmap = set_div_from_lower_bound(bmap, i, l);
1299 if (progress)
1300 *progress = 1;
1301 break;
1303 return bmap;
1306 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1307 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1309 struct isl_constraint_index ci;
1310 int k, l, h;
1311 unsigned total = isl_basic_map_total_dim(bmap);
1312 isl_int sum;
1314 if (!bmap || bmap->n_ineq <= 1)
1315 return bmap;
1317 if (create_constraint_index(&ci, bmap) < 0)
1318 return bmap;
1320 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1321 ci.index[h] = &bmap->ineq[0];
1322 for (k = 1; k < bmap->n_ineq; ++k) {
1323 h = hash_index(&ci, bmap, k);
1324 if (!ci.index[h]) {
1325 ci.index[h] = &bmap->ineq[k];
1326 continue;
1328 if (progress)
1329 *progress = 1;
1330 l = ci.index[h] - &bmap->ineq[0];
1331 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1332 swap_inequality(bmap, k, l);
1333 isl_basic_map_drop_inequality(bmap, k);
1334 --k;
1336 isl_int_init(sum);
1337 for (k = 0; k < bmap->n_ineq-1; ++k) {
1338 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1339 h = hash_index(&ci, bmap, k);
1340 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1341 if (!ci.index[h])
1342 continue;
1343 l = ci.index[h] - &bmap->ineq[0];
1344 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1345 if (isl_int_is_pos(sum)) {
1346 if (detect_divs)
1347 bmap = check_for_div_constraints(bmap, k, l,
1348 sum, progress);
1349 continue;
1351 if (isl_int_is_zero(sum)) {
1352 /* We need to break out of the loop after these
1353 * changes since the contents of the hash
1354 * will no longer be valid.
1355 * Plus, we probably we want to regauss first.
1357 if (progress)
1358 *progress = 1;
1359 isl_basic_map_drop_inequality(bmap, l);
1360 isl_basic_map_inequality_to_equality(bmap, k);
1361 } else
1362 bmap = isl_basic_map_set_to_empty(bmap);
1363 break;
1365 isl_int_clear(sum);
1367 constraint_index_free(&ci);
1368 return bmap;
1371 /* Detect all pairs of inequalities that form an equality.
1373 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1374 * Call it repeatedly while it is making progress.
1376 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1377 __isl_take isl_basic_map *bmap, int *progress)
1379 int duplicate;
1381 do {
1382 duplicate = 0;
1383 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1384 &duplicate, 0);
1385 if (progress && duplicate)
1386 *progress = 1;
1387 } while (duplicate);
1389 return bmap;
1392 /* Eliminate knowns divs from constraints where they appear with
1393 * a (positive or negative) unit coefficient.
1395 * That is, replace
1397 * floor(e/m) + f >= 0
1399 * by
1401 * e + m f >= 0
1403 * and
1405 * -floor(e/m) + f >= 0
1407 * by
1409 * -e + m f + m - 1 >= 0
1411 * The first conversion is valid because floor(e/m) >= -f is equivalent
1412 * to e/m >= -f because -f is an integral expression.
1413 * The second conversion follows from the fact that
1415 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1418 * Note that one of the div constraints may have been eliminated
1419 * due to being redundant with respect to the constraint that is
1420 * being modified by this function. The modified constraint may
1421 * no longer imply this div constraint, so we add it back to make
1422 * sure we do not lose any information.
1424 * We skip integral divs, i.e., those with denominator 1, as we would
1425 * risk eliminating the div from the div constraints. We do not need
1426 * to handle those divs here anyway since the div constraints will turn
1427 * out to form an equality and this equality can then be used to eliminate
1428 * the div from all constraints.
1430 static __isl_give isl_basic_map *eliminate_unit_divs(
1431 __isl_take isl_basic_map *bmap, int *progress)
1433 int i, j;
1434 isl_ctx *ctx;
1435 unsigned total;
1437 if (!bmap)
1438 return NULL;
1440 ctx = isl_basic_map_get_ctx(bmap);
1441 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1443 for (i = 0; i < bmap->n_div; ++i) {
1444 if (isl_int_is_zero(bmap->div[i][0]))
1445 continue;
1446 if (isl_int_is_one(bmap->div[i][0]))
1447 continue;
1448 for (j = 0; j < bmap->n_ineq; ++j) {
1449 int s;
1451 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1452 !isl_int_is_negone(bmap->ineq[j][total + i]))
1453 continue;
1455 *progress = 1;
1457 s = isl_int_sgn(bmap->ineq[j][total + i]);
1458 isl_int_set_si(bmap->ineq[j][total + i], 0);
1459 if (s < 0)
1460 isl_seq_combine(bmap->ineq[j],
1461 ctx->negone, bmap->div[i] + 1,
1462 bmap->div[i][0], bmap->ineq[j],
1463 total + bmap->n_div);
1464 else
1465 isl_seq_combine(bmap->ineq[j],
1466 ctx->one, bmap->div[i] + 1,
1467 bmap->div[i][0], bmap->ineq[j],
1468 total + bmap->n_div);
1469 if (s < 0) {
1470 isl_int_add(bmap->ineq[j][0],
1471 bmap->ineq[j][0], bmap->div[i][0]);
1472 isl_int_sub_ui(bmap->ineq[j][0],
1473 bmap->ineq[j][0], 1);
1476 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1477 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1478 return isl_basic_map_free(bmap);
1482 return bmap;
1485 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1487 int progress = 1;
1488 if (!bmap)
1489 return NULL;
1490 while (progress) {
1491 progress = 0;
1492 if (!bmap)
1493 break;
1494 if (isl_basic_map_plain_is_empty(bmap))
1495 break;
1496 bmap = isl_basic_map_normalize_constraints(bmap);
1497 bmap = remove_independent_vars_from_divs(bmap);
1498 bmap = normalize_div_expressions(bmap);
1499 bmap = remove_duplicate_divs(bmap, &progress);
1500 bmap = eliminate_unit_divs(bmap, &progress);
1501 bmap = eliminate_divs_eq(bmap, &progress);
1502 bmap = eliminate_divs_ineq(bmap, &progress);
1503 bmap = isl_basic_map_gauss(bmap, &progress);
1504 /* requires equalities in normal form */
1505 bmap = normalize_divs(bmap, &progress);
1506 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1507 &progress, 1);
1508 if (bmap && progress)
1509 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1511 return bmap;
1514 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1516 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1520 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1521 isl_int *constraint, unsigned div)
1523 unsigned pos;
1525 if (!bmap)
1526 return -1;
1528 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1530 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1531 int neg;
1532 isl_int_sub(bmap->div[div][1],
1533 bmap->div[div][1], bmap->div[div][0]);
1534 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1535 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1536 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1537 isl_int_add(bmap->div[div][1],
1538 bmap->div[div][1], bmap->div[div][0]);
1539 if (!neg)
1540 return 0;
1541 if (isl_seq_first_non_zero(constraint+pos+1,
1542 bmap->n_div-div-1) != -1)
1543 return 0;
1544 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1545 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1546 return 0;
1547 if (isl_seq_first_non_zero(constraint+pos+1,
1548 bmap->n_div-div-1) != -1)
1549 return 0;
1550 } else
1551 return 0;
1553 return 1;
1556 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1557 isl_int *constraint, unsigned div)
1559 return isl_basic_map_is_div_constraint(bset, constraint, div);
1563 /* If the only constraints a div d=floor(f/m)
1564 * appears in are its two defining constraints
1566 * f - m d >=0
1567 * -(f - (m - 1)) + m d >= 0
1569 * then it can safely be removed.
1571 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1573 int i;
1574 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1576 for (i = 0; i < bmap->n_eq; ++i)
1577 if (!isl_int_is_zero(bmap->eq[i][pos]))
1578 return 0;
1580 for (i = 0; i < bmap->n_ineq; ++i) {
1581 if (isl_int_is_zero(bmap->ineq[i][pos]))
1582 continue;
1583 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1584 return 0;
1587 for (i = 0; i < bmap->n_div; ++i) {
1588 if (isl_int_is_zero(bmap->div[i][0]))
1589 continue;
1590 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1591 return 0;
1594 return 1;
1598 * Remove divs that don't occur in any of the constraints or other divs.
1599 * These can arise when dropping constraints from a basic map or
1600 * when the divs of a basic map have been temporarily aligned
1601 * with the divs of another basic map.
1603 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1605 int i;
1607 if (!bmap)
1608 return NULL;
1610 for (i = bmap->n_div-1; i >= 0; --i) {
1611 if (!div_is_redundant(bmap, i))
1612 continue;
1613 bmap = isl_basic_map_drop_div(bmap, i);
1615 return bmap;
1618 /* Mark "bmap" as final, without checking for obviously redundant
1619 * integer divisions. This function should be used when "bmap"
1620 * is known not to involve any such integer divisions.
1622 __isl_give isl_basic_map *isl_basic_map_mark_final(
1623 __isl_take isl_basic_map *bmap)
1625 if (!bmap)
1626 return NULL;
1627 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1628 return bmap;
1631 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1633 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1635 bmap = remove_redundant_divs(bmap);
1636 bmap = isl_basic_map_mark_final(bmap);
1637 return bmap;
1640 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1642 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1645 struct isl_set *isl_set_finalize(struct isl_set *set)
1647 int i;
1649 if (!set)
1650 return NULL;
1651 for (i = 0; i < set->n; ++i) {
1652 set->p[i] = isl_basic_set_finalize(set->p[i]);
1653 if (!set->p[i])
1654 goto error;
1656 return set;
1657 error:
1658 isl_set_free(set);
1659 return NULL;
1662 struct isl_map *isl_map_finalize(struct isl_map *map)
1664 int i;
1666 if (!map)
1667 return NULL;
1668 for (i = 0; i < map->n; ++i) {
1669 map->p[i] = isl_basic_map_finalize(map->p[i]);
1670 if (!map->p[i])
1671 goto error;
1673 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1674 return map;
1675 error:
1676 isl_map_free(map);
1677 return NULL;
1681 /* Remove definition of any div that is defined in terms of the given variable.
1682 * The div itself is not removed. Functions such as
1683 * eliminate_divs_ineq depend on the other divs remaining in place.
1685 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1686 int pos)
1688 int i;
1690 if (!bmap)
1691 return NULL;
1693 for (i = 0; i < bmap->n_div; ++i) {
1694 if (isl_int_is_zero(bmap->div[i][0]))
1695 continue;
1696 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1697 continue;
1698 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1699 if (!bmap)
1700 return NULL;
1702 return bmap;
1705 /* Eliminate the specified variables from the constraints using
1706 * Fourier-Motzkin. The variables themselves are not removed.
1708 struct isl_basic_map *isl_basic_map_eliminate_vars(
1709 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1711 int d;
1712 int i, j, k;
1713 unsigned total;
1714 int need_gauss = 0;
1716 if (n == 0)
1717 return bmap;
1718 if (!bmap)
1719 return NULL;
1720 total = isl_basic_map_total_dim(bmap);
1722 bmap = isl_basic_map_cow(bmap);
1723 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1724 bmap = remove_dependent_vars(bmap, d);
1725 if (!bmap)
1726 return NULL;
1728 for (d = pos + n - 1;
1729 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1730 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1731 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1732 int n_lower, n_upper;
1733 if (!bmap)
1734 return NULL;
1735 for (i = 0; i < bmap->n_eq; ++i) {
1736 if (isl_int_is_zero(bmap->eq[i][1+d]))
1737 continue;
1738 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1739 isl_basic_map_drop_equality(bmap, i);
1740 need_gauss = 1;
1741 break;
1743 if (i < bmap->n_eq)
1744 continue;
1745 n_lower = 0;
1746 n_upper = 0;
1747 for (i = 0; i < bmap->n_ineq; ++i) {
1748 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1749 n_lower++;
1750 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1751 n_upper++;
1753 bmap = isl_basic_map_extend_constraints(bmap,
1754 0, n_lower * n_upper);
1755 if (!bmap)
1756 goto error;
1757 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1758 int last;
1759 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1760 continue;
1761 last = -1;
1762 for (j = 0; j < i; ++j) {
1763 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1764 continue;
1765 last = j;
1766 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1767 isl_int_sgn(bmap->ineq[j][1+d]))
1768 continue;
1769 k = isl_basic_map_alloc_inequality(bmap);
1770 if (k < 0)
1771 goto error;
1772 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1773 1+total);
1774 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1775 1+d, 1+total, NULL);
1777 isl_basic_map_drop_inequality(bmap, i);
1778 i = last + 1;
1780 if (n_lower > 0 && n_upper > 0) {
1781 bmap = isl_basic_map_normalize_constraints(bmap);
1782 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1783 NULL, 0);
1784 bmap = isl_basic_map_gauss(bmap, NULL);
1785 bmap = isl_basic_map_remove_redundancies(bmap);
1786 need_gauss = 0;
1787 if (!bmap)
1788 goto error;
1789 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1790 break;
1793 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1794 if (need_gauss)
1795 bmap = isl_basic_map_gauss(bmap, NULL);
1796 return bmap;
1797 error:
1798 isl_basic_map_free(bmap);
1799 return NULL;
1802 struct isl_basic_set *isl_basic_set_eliminate_vars(
1803 struct isl_basic_set *bset, unsigned pos, unsigned n)
1805 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1806 pos, n));
1809 /* Eliminate the specified n dimensions starting at first from the
1810 * constraints, without removing the dimensions from the space.
1811 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1812 * Otherwise, they are projected out and the original space is restored.
1814 __isl_give isl_basic_map *isl_basic_map_eliminate(
1815 __isl_take isl_basic_map *bmap,
1816 enum isl_dim_type type, unsigned first, unsigned n)
1818 isl_space *space;
1820 if (!bmap)
1821 return NULL;
1822 if (n == 0)
1823 return bmap;
1825 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1826 isl_die(bmap->ctx, isl_error_invalid,
1827 "index out of bounds", goto error);
1829 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1830 first += isl_basic_map_offset(bmap, type) - 1;
1831 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1832 return isl_basic_map_finalize(bmap);
1835 space = isl_basic_map_get_space(bmap);
1836 bmap = isl_basic_map_project_out(bmap, type, first, n);
1837 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1838 bmap = isl_basic_map_reset_space(bmap, space);
1839 return bmap;
1840 error:
1841 isl_basic_map_free(bmap);
1842 return NULL;
1845 __isl_give isl_basic_set *isl_basic_set_eliminate(
1846 __isl_take isl_basic_set *bset,
1847 enum isl_dim_type type, unsigned first, unsigned n)
1849 return isl_basic_map_eliminate(bset, type, first, n);
1852 /* Remove all constraints from "bmap" that reference any unknown local
1853 * variables (directly or indirectly).
1855 * Dropping all constraints on a local variable will make it redundant,
1856 * so it will get removed implicitly by
1857 * isl_basic_map_drop_constraints_involving_dims. Some other local
1858 * variables may also end up becoming redundant if they only appear
1859 * in constraints together with the unknown local variable.
1860 * Therefore, start over after calling
1861 * isl_basic_map_drop_constraints_involving_dims.
1863 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1864 __isl_take isl_basic_map *bmap)
1866 isl_bool known;
1867 int i, n_div, o_div;
1869 known = isl_basic_map_divs_known(bmap);
1870 if (known < 0)
1871 return isl_basic_map_free(bmap);
1872 if (known)
1873 return bmap;
1875 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1876 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1878 for (i = 0; i < n_div; ++i) {
1879 known = isl_basic_map_div_is_known(bmap, i);
1880 if (known < 0)
1881 return isl_basic_map_free(bmap);
1882 if (known)
1883 continue;
1884 bmap = remove_dependent_vars(bmap, o_div + i);
1885 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1886 isl_dim_div, i, 1);
1887 if (!bmap)
1888 return NULL;
1889 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1890 i = -1;
1893 return bmap;
1896 /* Remove all constraints from "map" that reference any unknown local
1897 * variables (directly or indirectly).
1899 * Since constraints may get dropped from the basic maps,
1900 * they may no longer be disjoint from each other.
1902 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1903 __isl_take isl_map *map)
1905 int i;
1906 isl_bool known;
1908 known = isl_map_divs_known(map);
1909 if (known < 0)
1910 return isl_map_free(map);
1911 if (known)
1912 return map;
1914 map = isl_map_cow(map);
1915 if (!map)
1916 return NULL;
1918 for (i = 0; i < map->n; ++i) {
1919 map->p[i] =
1920 isl_basic_map_drop_constraint_involving_unknown_divs(
1921 map->p[i]);
1922 if (!map->p[i])
1923 return isl_map_free(map);
1926 if (map->n > 1)
1927 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1929 return map;
1932 /* Don't assume equalities are in order, because align_divs
1933 * may have changed the order of the divs.
1935 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1937 int d, i;
1938 unsigned total;
1940 total = isl_space_dim(bmap->dim, isl_dim_all);
1941 for (d = 0; d < total; ++d)
1942 elim[d] = -1;
1943 for (i = 0; i < bmap->n_eq; ++i) {
1944 for (d = total - 1; d >= 0; --d) {
1945 if (isl_int_is_zero(bmap->eq[i][1+d]))
1946 continue;
1947 elim[d] = i;
1948 break;
1953 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1955 compute_elimination_index(bset_to_bmap(bset), elim);
1958 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1959 struct isl_basic_map *bmap, int *elim)
1961 int d;
1962 int copied = 0;
1963 unsigned total;
1965 total = isl_space_dim(bmap->dim, isl_dim_all);
1966 for (d = total - 1; d >= 0; --d) {
1967 if (isl_int_is_zero(src[1+d]))
1968 continue;
1969 if (elim[d] == -1)
1970 continue;
1971 if (!copied) {
1972 isl_seq_cpy(dst, src, 1 + total);
1973 copied = 1;
1975 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1977 return copied;
1980 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1981 struct isl_basic_set *bset, int *elim)
1983 return reduced_using_equalities(dst, src,
1984 bset_to_bmap(bset), elim);
1987 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1988 struct isl_basic_set *bset, struct isl_basic_set *context)
1990 int i;
1991 int *elim;
1993 if (!bset || !context)
1994 goto error;
1996 if (context->n_eq == 0) {
1997 isl_basic_set_free(context);
1998 return bset;
2001 bset = isl_basic_set_cow(bset);
2002 if (!bset)
2003 goto error;
2005 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2006 if (!elim)
2007 goto error;
2008 set_compute_elimination_index(context, elim);
2009 for (i = 0; i < bset->n_eq; ++i)
2010 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2011 context, elim);
2012 for (i = 0; i < bset->n_ineq; ++i)
2013 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2014 context, elim);
2015 isl_basic_set_free(context);
2016 free(elim);
2017 bset = isl_basic_set_simplify(bset);
2018 bset = isl_basic_set_finalize(bset);
2019 return bset;
2020 error:
2021 isl_basic_set_free(bset);
2022 isl_basic_set_free(context);
2023 return NULL;
2026 /* For each inequality in "ineq" that is a shifted (more relaxed)
2027 * copy of an inequality in "context", mark the corresponding entry
2028 * in "row" with -1.
2029 * If an inequality only has a non-negative constant term, then
2030 * mark it as well.
2032 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2033 __isl_keep isl_basic_set *context, int *row)
2035 struct isl_constraint_index ci;
2036 int n_ineq;
2037 unsigned total;
2038 int k;
2040 if (!ineq || !context)
2041 return isl_stat_error;
2042 if (context->n_ineq == 0)
2043 return isl_stat_ok;
2044 if (setup_constraint_index(&ci, context) < 0)
2045 return isl_stat_error;
2047 n_ineq = isl_mat_rows(ineq);
2048 total = isl_mat_cols(ineq) - 1;
2049 for (k = 0; k < n_ineq; ++k) {
2050 int l;
2051 isl_bool redundant;
2053 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2054 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2055 row[k] = -1;
2056 continue;
2058 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2059 if (redundant < 0)
2060 goto error;
2061 if (!redundant)
2062 continue;
2063 row[k] = -1;
2065 constraint_index_free(&ci);
2066 return isl_stat_ok;
2067 error:
2068 constraint_index_free(&ci);
2069 return isl_stat_error;
2072 static struct isl_basic_set *remove_shifted_constraints(
2073 struct isl_basic_set *bset, struct isl_basic_set *context)
2075 struct isl_constraint_index ci;
2076 int k;
2078 if (!bset || !context)
2079 return bset;
2081 if (context->n_ineq == 0)
2082 return bset;
2083 if (setup_constraint_index(&ci, context) < 0)
2084 return bset;
2086 for (k = 0; k < bset->n_ineq; ++k) {
2087 isl_bool redundant;
2089 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2090 if (redundant < 0)
2091 goto error;
2092 if (!redundant)
2093 continue;
2094 bset = isl_basic_set_cow(bset);
2095 if (!bset)
2096 goto error;
2097 isl_basic_set_drop_inequality(bset, k);
2098 --k;
2100 constraint_index_free(&ci);
2101 return bset;
2102 error:
2103 constraint_index_free(&ci);
2104 return bset;
2107 /* Remove constraints from "bmap" that are identical to constraints
2108 * in "context" or that are more relaxed (greater constant term).
2110 * We perform the test for shifted copies on the pure constraints
2111 * in remove_shifted_constraints.
2113 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2114 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2116 isl_basic_set *bset, *bset_context;
2118 if (!bmap || !context)
2119 goto error;
2121 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2122 isl_basic_map_free(context);
2123 return bmap;
2126 context = isl_basic_map_align_divs(context, bmap);
2127 bmap = isl_basic_map_align_divs(bmap, context);
2129 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2130 bset_context = isl_basic_map_underlying_set(context);
2131 bset = remove_shifted_constraints(bset, bset_context);
2132 isl_basic_set_free(bset_context);
2134 bmap = isl_basic_map_overlying_set(bset, bmap);
2136 return bmap;
2137 error:
2138 isl_basic_map_free(bmap);
2139 isl_basic_map_free(context);
2140 return NULL;
2143 /* Does the (linear part of a) constraint "c" involve any of the "len"
2144 * "relevant" dimensions?
2146 static int is_related(isl_int *c, int len, int *relevant)
2148 int i;
2150 for (i = 0; i < len; ++i) {
2151 if (!relevant[i])
2152 continue;
2153 if (!isl_int_is_zero(c[i]))
2154 return 1;
2157 return 0;
2160 /* Drop constraints from "bmap" that do not involve any of
2161 * the dimensions marked "relevant".
2163 static __isl_give isl_basic_map *drop_unrelated_constraints(
2164 __isl_take isl_basic_map *bmap, int *relevant)
2166 int i, dim;
2168 dim = isl_basic_map_dim(bmap, isl_dim_all);
2169 for (i = 0; i < dim; ++i)
2170 if (!relevant[i])
2171 break;
2172 if (i >= dim)
2173 return bmap;
2175 for (i = bmap->n_eq - 1; i >= 0; --i)
2176 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2177 bmap = isl_basic_map_cow(bmap);
2178 if (isl_basic_map_drop_equality(bmap, i) < 0)
2179 return isl_basic_map_free(bmap);
2182 for (i = bmap->n_ineq - 1; i >= 0; --i)
2183 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2184 bmap = isl_basic_map_cow(bmap);
2185 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2186 return isl_basic_map_free(bmap);
2189 return bmap;
2192 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2194 * In particular, for any variable involved in the constraint,
2195 * find the actual group id from before and replace the group
2196 * of the corresponding variable by the minimal group of all
2197 * the variables involved in the constraint considered so far
2198 * (if this minimum is smaller) or replace the minimum by this group
2199 * (if the minimum is larger).
2201 * At the end, all the variables in "c" will (indirectly) point
2202 * to the minimal of the groups that they referred to originally.
2204 static void update_groups(int dim, int *group, isl_int *c)
2206 int j;
2207 int min = dim;
2209 for (j = 0; j < dim; ++j) {
2210 if (isl_int_is_zero(c[j]))
2211 continue;
2212 while (group[j] >= 0 && group[group[j]] != group[j])
2213 group[j] = group[group[j]];
2214 if (group[j] == min)
2215 continue;
2216 if (group[j] < min) {
2217 if (min >= 0 && min < dim)
2218 group[min] = group[j];
2219 min = group[j];
2220 } else
2221 group[group[j]] = min;
2225 /* Allocate an array of groups of variables, one for each variable
2226 * in "context", initialized to zero.
2228 static int *alloc_groups(__isl_keep isl_basic_set *context)
2230 isl_ctx *ctx;
2231 int dim;
2233 dim = isl_basic_set_dim(context, isl_dim_set);
2234 ctx = isl_basic_set_get_ctx(context);
2235 return isl_calloc_array(ctx, int, dim);
2238 /* Drop constraints from "bmap" that only involve variables that are
2239 * not related to any of the variables marked with a "-1" in "group".
2241 * We construct groups of variables that collect variables that
2242 * (indirectly) appear in some common constraint of "bmap".
2243 * Each group is identified by the first variable in the group,
2244 * except for the special group of variables that was already identified
2245 * in the input as -1 (or are related to those variables).
2246 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2247 * otherwise the group of i is the group of group[i].
2249 * We first initialize groups for the remaining variables.
2250 * Then we iterate over the constraints of "bmap" and update the
2251 * group of the variables in the constraint by the smallest group.
2252 * Finally, we resolve indirect references to groups by running over
2253 * the variables.
2255 * After computing the groups, we drop constraints that do not involve
2256 * any variables in the -1 group.
2258 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2259 __isl_take isl_basic_map *bmap, __isl_take int *group)
2261 int dim;
2262 int i;
2263 int last;
2265 if (!bmap)
2266 return NULL;
2268 dim = isl_basic_map_dim(bmap, isl_dim_all);
2270 last = -1;
2271 for (i = 0; i < dim; ++i)
2272 if (group[i] >= 0)
2273 last = group[i] = i;
2274 if (last < 0) {
2275 free(group);
2276 return bmap;
2279 for (i = 0; i < bmap->n_eq; ++i)
2280 update_groups(dim, group, bmap->eq[i] + 1);
2281 for (i = 0; i < bmap->n_ineq; ++i)
2282 update_groups(dim, group, bmap->ineq[i] + 1);
2284 for (i = 0; i < dim; ++i)
2285 if (group[i] >= 0)
2286 group[i] = group[group[i]];
2288 for (i = 0; i < dim; ++i)
2289 group[i] = group[i] == -1;
2291 bmap = drop_unrelated_constraints(bmap, group);
2293 free(group);
2294 return bmap;
2297 /* Drop constraints from "context" that are irrelevant for computing
2298 * the gist of "bset".
2300 * In particular, drop constraints in variables that are not related
2301 * to any of the variables involved in the constraints of "bset"
2302 * in the sense that there is no sequence of constraints that connects them.
2304 * We first mark all variables that appear in "bset" as belonging
2305 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2307 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2308 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2310 int *group;
2311 int dim;
2312 int i, j;
2314 if (!context || !bset)
2315 return isl_basic_set_free(context);
2317 group = alloc_groups(context);
2319 if (!group)
2320 return isl_basic_set_free(context);
2322 dim = isl_basic_set_dim(bset, isl_dim_set);
2323 for (i = 0; i < dim; ++i) {
2324 for (j = 0; j < bset->n_eq; ++j)
2325 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2326 break;
2327 if (j < bset->n_eq) {
2328 group[i] = -1;
2329 continue;
2331 for (j = 0; j < bset->n_ineq; ++j)
2332 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2333 break;
2334 if (j < bset->n_ineq)
2335 group[i] = -1;
2338 return isl_basic_map_drop_unrelated_constraints(context, group);
2341 /* Drop constraints from "context" that are irrelevant for computing
2342 * the gist of the inequalities "ineq".
2343 * Inequalities in "ineq" for which the corresponding element of row
2344 * is set to -1 have already been marked for removal and should be ignored.
2346 * In particular, drop constraints in variables that are not related
2347 * to any of the variables involved in "ineq"
2348 * in the sense that there is no sequence of constraints that connects them.
2350 * We first mark all variables that appear in "bset" as belonging
2351 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2353 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2354 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2356 int *group;
2357 int dim;
2358 int i, j, n;
2360 if (!context || !ineq)
2361 return isl_basic_set_free(context);
2363 group = alloc_groups(context);
2365 if (!group)
2366 return isl_basic_set_free(context);
2368 dim = isl_basic_set_dim(context, isl_dim_set);
2369 n = isl_mat_rows(ineq);
2370 for (i = 0; i < dim; ++i) {
2371 for (j = 0; j < n; ++j) {
2372 if (row[j] < 0)
2373 continue;
2374 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2375 break;
2377 if (j < n)
2378 group[i] = -1;
2381 return isl_basic_map_drop_unrelated_constraints(context, group);
2384 /* Do all "n" entries of "row" contain a negative value?
2386 static int all_neg(int *row, int n)
2388 int i;
2390 for (i = 0; i < n; ++i)
2391 if (row[i] >= 0)
2392 return 0;
2394 return 1;
2397 /* Update the inequalities in "bset" based on the information in "row"
2398 * and "tab".
2400 * In particular, the array "row" contains either -1, meaning that
2401 * the corresponding inequality of "bset" is redundant, or the index
2402 * of an inequality in "tab".
2404 * If the row entry is -1, then drop the inequality.
2405 * Otherwise, if the constraint is marked redundant in the tableau,
2406 * then drop the inequality. Similarly, if it is marked as an equality
2407 * in the tableau, then turn the inequality into an equality and
2408 * perform Gaussian elimination.
2410 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2411 __isl_keep int *row, struct isl_tab *tab)
2413 int i;
2414 unsigned n_ineq;
2415 unsigned n_eq;
2416 int found_equality = 0;
2418 if (!bset)
2419 return NULL;
2420 if (tab && tab->empty)
2421 return isl_basic_set_set_to_empty(bset);
2423 n_ineq = bset->n_ineq;
2424 for (i = n_ineq - 1; i >= 0; --i) {
2425 if (row[i] < 0) {
2426 if (isl_basic_set_drop_inequality(bset, i) < 0)
2427 return isl_basic_set_free(bset);
2428 continue;
2430 if (!tab)
2431 continue;
2432 n_eq = tab->n_eq;
2433 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2434 isl_basic_map_inequality_to_equality(bset, i);
2435 found_equality = 1;
2436 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2437 if (isl_basic_set_drop_inequality(bset, i) < 0)
2438 return isl_basic_set_free(bset);
2442 if (found_equality)
2443 bset = isl_basic_set_gauss(bset, NULL);
2444 bset = isl_basic_set_finalize(bset);
2445 return bset;
2448 /* Update the inequalities in "bset" based on the information in "row"
2449 * and "tab" and free all arguments (other than "bset").
2451 static __isl_give isl_basic_set *update_ineq_free(
2452 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2453 __isl_take isl_basic_set *context, __isl_take int *row,
2454 struct isl_tab *tab)
2456 isl_mat_free(ineq);
2457 isl_basic_set_free(context);
2459 bset = update_ineq(bset, row, tab);
2461 free(row);
2462 isl_tab_free(tab);
2463 return bset;
2466 /* Remove all information from bset that is redundant in the context
2467 * of context.
2468 * "ineq" contains the (possibly transformed) inequalities of "bset",
2469 * in the same order.
2470 * The (explicit) equalities of "bset" are assumed to have been taken
2471 * into account by the transformation such that only the inequalities
2472 * are relevant.
2473 * "context" is assumed not to be empty.
2475 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2476 * A value of -1 means that the inequality is obviously redundant and may
2477 * not even appear in "tab".
2479 * We first mark the inequalities of "bset"
2480 * that are obviously redundant with respect to some inequality in "context".
2481 * Then we remove those constraints from "context" that have become
2482 * irrelevant for computing the gist of "bset".
2483 * Note that this removal of constraints cannot be replaced by
2484 * a factorization because factors in "bset" may still be connected
2485 * to each other through constraints in "context".
2487 * If there are any inequalities left, we construct a tableau for
2488 * the context and then add the inequalities of "bset".
2489 * Before adding these inequalities, we freeze all constraints such that
2490 * they won't be considered redundant in terms of the constraints of "bset".
2491 * Then we detect all redundant constraints (among the
2492 * constraints that weren't frozen), first by checking for redundancy in the
2493 * the tableau and then by checking if replacing a constraint by its negation
2494 * would lead to an empty set. This last step is fairly expensive
2495 * and could be optimized by more reuse of the tableau.
2496 * Finally, we update bset according to the results.
2498 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2499 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2501 int i, r;
2502 int *row = NULL;
2503 isl_ctx *ctx;
2504 isl_basic_set *combined = NULL;
2505 struct isl_tab *tab = NULL;
2506 unsigned n_eq, context_ineq;
2507 unsigned total;
2509 if (!bset || !ineq || !context)
2510 goto error;
2512 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2513 isl_basic_set_free(context);
2514 isl_mat_free(ineq);
2515 return bset;
2518 ctx = isl_basic_set_get_ctx(context);
2519 row = isl_calloc_array(ctx, int, bset->n_ineq);
2520 if (!row)
2521 goto error;
2523 if (mark_shifted_constraints(ineq, context, row) < 0)
2524 goto error;
2525 if (all_neg(row, bset->n_ineq))
2526 return update_ineq_free(bset, ineq, context, row, NULL);
2528 context = drop_irrelevant_constraints_marked(context, ineq, row);
2529 if (!context)
2530 goto error;
2531 if (isl_basic_set_plain_is_universe(context))
2532 return update_ineq_free(bset, ineq, context, row, NULL);
2534 n_eq = context->n_eq;
2535 context_ineq = context->n_ineq;
2536 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2537 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2538 tab = isl_tab_from_basic_set(combined, 0);
2539 for (i = 0; i < context_ineq; ++i)
2540 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2541 goto error;
2542 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2543 goto error;
2544 r = context_ineq;
2545 for (i = 0; i < bset->n_ineq; ++i) {
2546 if (row[i] < 0)
2547 continue;
2548 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2549 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2550 goto error;
2551 row[i] = r++;
2553 if (isl_tab_detect_implicit_equalities(tab) < 0)
2554 goto error;
2555 if (isl_tab_detect_redundant(tab) < 0)
2556 goto error;
2557 total = isl_basic_set_total_dim(bset);
2558 for (i = bset->n_ineq - 1; i >= 0; --i) {
2559 isl_basic_set *test;
2560 int is_empty;
2562 if (row[i] < 0)
2563 continue;
2564 r = row[i];
2565 if (tab->con[n_eq + r].is_redundant)
2566 continue;
2567 test = isl_basic_set_dup(combined);
2568 if (isl_inequality_negate(test, r) < 0)
2569 test = isl_basic_set_free(test);
2570 test = isl_basic_set_update_from_tab(test, tab);
2571 is_empty = isl_basic_set_is_empty(test);
2572 isl_basic_set_free(test);
2573 if (is_empty < 0)
2574 goto error;
2575 if (is_empty)
2576 tab->con[n_eq + r].is_redundant = 1;
2578 bset = update_ineq_free(bset, ineq, context, row, tab);
2579 if (bset) {
2580 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2581 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2584 isl_basic_set_free(combined);
2585 return bset;
2586 error:
2587 free(row);
2588 isl_mat_free(ineq);
2589 isl_tab_free(tab);
2590 isl_basic_set_free(combined);
2591 isl_basic_set_free(context);
2592 isl_basic_set_free(bset);
2593 return NULL;
2596 /* Extract the inequalities of "bset" as an isl_mat.
2598 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2600 unsigned total;
2601 isl_ctx *ctx;
2602 isl_mat *ineq;
2604 if (!bset)
2605 return NULL;
2607 ctx = isl_basic_set_get_ctx(bset);
2608 total = isl_basic_set_total_dim(bset);
2609 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2610 0, 1 + total);
2612 return ineq;
2615 /* Remove all information from "bset" that is redundant in the context
2616 * of "context", for the case where both "bset" and "context" are
2617 * full-dimensional.
2619 static __isl_give isl_basic_set *uset_gist_uncompressed(
2620 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2622 isl_mat *ineq;
2624 ineq = extract_ineq(bset);
2625 return uset_gist_full(bset, ineq, context);
2628 /* Remove all information from "bset" that is redundant in the context
2629 * of "context", for the case where the combined equalities of
2630 * "bset" and "context" allow for a compression that can be obtained
2631 * by preapplication of "T".
2633 * "bset" itself is not transformed by "T". Instead, the inequalities
2634 * are extracted from "bset" and those are transformed by "T".
2635 * uset_gist_full then determines which of the transformed inequalities
2636 * are redundant with respect to the transformed "context" and removes
2637 * the corresponding inequalities from "bset".
2639 * After preapplying "T" to the inequalities, any common factor is
2640 * removed from the coefficients. If this results in a tightening
2641 * of the constant term, then the same tightening is applied to
2642 * the corresponding untransformed inequality in "bset".
2643 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2645 * g f'(x) + r >= 0
2647 * with 0 <= r < g, then it is equivalent to
2649 * f'(x) >= 0
2651 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2652 * subspace compressed by T since the latter would be transformed to
2654 * g f'(x) >= 0
2656 static __isl_give isl_basic_set *uset_gist_compressed(
2657 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2658 __isl_take isl_mat *T)
2660 isl_ctx *ctx;
2661 isl_mat *ineq;
2662 int i, n_row, n_col;
2663 isl_int rem;
2665 ineq = extract_ineq(bset);
2666 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2667 context = isl_basic_set_preimage(context, T);
2669 if (!ineq || !context)
2670 goto error;
2671 if (isl_basic_set_plain_is_empty(context)) {
2672 isl_mat_free(ineq);
2673 isl_basic_set_free(context);
2674 return isl_basic_set_set_to_empty(bset);
2677 ctx = isl_mat_get_ctx(ineq);
2678 n_row = isl_mat_rows(ineq);
2679 n_col = isl_mat_cols(ineq);
2680 isl_int_init(rem);
2681 for (i = 0; i < n_row; ++i) {
2682 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2683 if (isl_int_is_zero(ctx->normalize_gcd))
2684 continue;
2685 if (isl_int_is_one(ctx->normalize_gcd))
2686 continue;
2687 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2688 ctx->normalize_gcd, n_col - 1);
2689 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2690 isl_int_fdiv_q(ineq->row[i][0],
2691 ineq->row[i][0], ctx->normalize_gcd);
2692 if (isl_int_is_zero(rem))
2693 continue;
2694 bset = isl_basic_set_cow(bset);
2695 if (!bset)
2696 break;
2697 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2699 isl_int_clear(rem);
2701 return uset_gist_full(bset, ineq, context);
2702 error:
2703 isl_mat_free(ineq);
2704 isl_basic_set_free(context);
2705 isl_basic_set_free(bset);
2706 return NULL;
2709 /* Project "bset" onto the variables that are involved in "template".
2711 static __isl_give isl_basic_set *project_onto_involved(
2712 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2714 int i, n;
2716 if (!bset || !template)
2717 return isl_basic_set_free(bset);
2719 n = isl_basic_set_dim(template, isl_dim_set);
2721 for (i = 0; i < n; ++i) {
2722 isl_bool involved;
2724 involved = isl_basic_set_involves_dims(template,
2725 isl_dim_set, i, 1);
2726 if (involved < 0)
2727 return isl_basic_set_free(bset);
2728 if (involved)
2729 continue;
2730 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2733 return bset;
2736 /* Remove all information from bset that is redundant in the context
2737 * of context. In particular, equalities that are linear combinations
2738 * of those in context are removed. Then the inequalities that are
2739 * redundant in the context of the equalities and inequalities of
2740 * context are removed.
2742 * First of all, we drop those constraints from "context"
2743 * that are irrelevant for computing the gist of "bset".
2744 * Alternatively, we could factorize the intersection of "context" and "bset".
2746 * We first compute the intersection of the integer affine hulls
2747 * of "bset" and "context",
2748 * compute the gist inside this intersection and then reduce
2749 * the constraints with respect to the equalities of the context
2750 * that only involve variables already involved in the input.
2752 * If two constraints are mutually redundant, then uset_gist_full
2753 * will remove the second of those constraints. We therefore first
2754 * sort the constraints so that constraints not involving existentially
2755 * quantified variables are given precedence over those that do.
2756 * We have to perform this sorting before the variable compression,
2757 * because that may effect the order of the variables.
2759 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2760 __isl_take isl_basic_set *context)
2762 isl_mat *eq;
2763 isl_mat *T;
2764 isl_basic_set *aff;
2765 isl_basic_set *aff_context;
2766 unsigned total;
2768 if (!bset || !context)
2769 goto error;
2771 context = drop_irrelevant_constraints(context, bset);
2773 bset = isl_basic_set_detect_equalities(bset);
2774 aff = isl_basic_set_copy(bset);
2775 aff = isl_basic_set_plain_affine_hull(aff);
2776 context = isl_basic_set_detect_equalities(context);
2777 aff_context = isl_basic_set_copy(context);
2778 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2779 aff = isl_basic_set_intersect(aff, aff_context);
2780 if (!aff)
2781 goto error;
2782 if (isl_basic_set_plain_is_empty(aff)) {
2783 isl_basic_set_free(bset);
2784 isl_basic_set_free(context);
2785 return aff;
2787 bset = isl_basic_set_sort_constraints(bset);
2788 if (aff->n_eq == 0) {
2789 isl_basic_set_free(aff);
2790 return uset_gist_uncompressed(bset, context);
2792 total = isl_basic_set_total_dim(bset);
2793 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2794 eq = isl_mat_cow(eq);
2795 T = isl_mat_variable_compression(eq, NULL);
2796 isl_basic_set_free(aff);
2797 if (T && T->n_col == 0) {
2798 isl_mat_free(T);
2799 isl_basic_set_free(context);
2800 return isl_basic_set_set_to_empty(bset);
2803 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2804 aff_context = project_onto_involved(aff_context, bset);
2806 bset = uset_gist_compressed(bset, context, T);
2807 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2809 if (bset) {
2810 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2811 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2814 return bset;
2815 error:
2816 isl_basic_set_free(bset);
2817 isl_basic_set_free(context);
2818 return NULL;
2821 /* Return the number of equality constraints in "bmap" that involve
2822 * local variables. This function assumes that Gaussian elimination
2823 * has been applied to the equality constraints.
2825 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2827 int i;
2828 int total, n_div;
2830 if (!bmap)
2831 return -1;
2833 if (bmap->n_eq == 0)
2834 return 0;
2836 total = isl_basic_map_dim(bmap, isl_dim_all);
2837 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2838 total -= n_div;
2840 for (i = 0; i < bmap->n_eq; ++i)
2841 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2842 n_div) == -1)
2843 return i;
2845 return bmap->n_eq;
2848 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2849 * The constraints are assumed not to involve any local variables.
2851 static __isl_give isl_basic_map *basic_map_from_equalities(
2852 __isl_take isl_space *space, __isl_take isl_mat *eq)
2854 int i, k;
2855 isl_basic_map *bmap = NULL;
2857 if (!space || !eq)
2858 goto error;
2860 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2861 isl_die(isl_space_get_ctx(space), isl_error_internal,
2862 "unexpected number of columns", goto error);
2864 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2865 0, eq->n_row, 0);
2866 for (i = 0; i < eq->n_row; ++i) {
2867 k = isl_basic_map_alloc_equality(bmap);
2868 if (k < 0)
2869 goto error;
2870 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2873 isl_space_free(space);
2874 isl_mat_free(eq);
2875 return bmap;
2876 error:
2877 isl_space_free(space);
2878 isl_mat_free(eq);
2879 isl_basic_map_free(bmap);
2880 return NULL;
2883 /* Construct and return a variable compression based on the equality
2884 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2885 * "n1" is the number of (initial) equality constraints in "bmap1"
2886 * that do involve local variables.
2887 * "n2" is the number of (initial) equality constraints in "bmap2"
2888 * that do involve local variables.
2889 * "total" is the total number of other variables.
2890 * This function assumes that Gaussian elimination
2891 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2892 * such that the equality constraints not involving local variables
2893 * are those that start at "n1" or "n2".
2895 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2896 * then simply compute the compression based on the equality constraints
2897 * in the other basic map.
2898 * Otherwise, combine the equality constraints from both into a new
2899 * basic map such that Gaussian elimination can be applied to this combination
2900 * and then construct a variable compression from the resulting
2901 * equality constraints.
2903 static __isl_give isl_mat *combined_variable_compression(
2904 __isl_keep isl_basic_map *bmap1, int n1,
2905 __isl_keep isl_basic_map *bmap2, int n2, int total)
2907 isl_ctx *ctx;
2908 isl_mat *E1, *E2, *V;
2909 isl_basic_map *bmap;
2911 ctx = isl_basic_map_get_ctx(bmap1);
2912 if (bmap1->n_eq == n1) {
2913 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2914 n2, bmap2->n_eq - n2, 0, 1 + total);
2915 return isl_mat_variable_compression(E2, NULL);
2917 if (bmap2->n_eq == n2) {
2918 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2919 n1, bmap1->n_eq - n1, 0, 1 + total);
2920 return isl_mat_variable_compression(E1, NULL);
2922 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2923 n1, bmap1->n_eq - n1, 0, 1 + total);
2924 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2925 n2, bmap2->n_eq - n2, 0, 1 + total);
2926 E1 = isl_mat_concat(E1, E2);
2927 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2928 bmap = isl_basic_map_gauss(bmap, NULL);
2929 if (!bmap)
2930 return NULL;
2931 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2932 V = isl_mat_variable_compression(E1, NULL);
2933 isl_basic_map_free(bmap);
2935 return V;
2938 /* Extract the stride constraints from "bmap", compressed
2939 * with respect to both the stride constraints in "context" and
2940 * the remaining equality constraints in both "bmap" and "context".
2941 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2942 * "context_n_eq" is the number of (initial) stride constraints in "context".
2944 * Let x be all variables in "bmap" (and "context") other than the local
2945 * variables. First compute a variable compression
2947 * x = V x'
2949 * based on the non-stride equality constraints in "bmap" and "context".
2950 * Consider the stride constraints of "context",
2952 * A(x) + B(y) = 0
2954 * with y the local variables and plug in the variable compression,
2955 * resulting in
2957 * A(V x') + B(y) = 0
2959 * Use these constraints to compute a parameter compression on x'
2961 * x' = T x''
2963 * Now consider the stride constraints of "bmap"
2965 * C(x) + D(y) = 0
2967 * and plug in x = V*T x''.
2968 * That is, return A = [C*V*T D].
2970 static __isl_give isl_mat *extract_compressed_stride_constraints(
2971 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2972 __isl_keep isl_basic_map *context, int context_n_eq)
2974 int total, n_div;
2975 isl_ctx *ctx;
2976 isl_mat *A, *B, *T, *V;
2978 total = isl_basic_map_dim(context, isl_dim_all);
2979 n_div = isl_basic_map_dim(context, isl_dim_div);
2980 total -= n_div;
2982 ctx = isl_basic_map_get_ctx(bmap);
2984 V = combined_variable_compression(bmap, bmap_n_eq,
2985 context, context_n_eq, total);
2987 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2988 B = isl_mat_sub_alloc6(ctx, context->eq,
2989 0, context_n_eq, 1 + total, n_div);
2990 A = isl_mat_product(A, isl_mat_copy(V));
2991 T = isl_mat_parameter_compression_ext(A, B);
2992 T = isl_mat_product(V, T);
2994 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2995 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2997 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2998 0, bmap_n_eq, 0, 1 + total + n_div);
2999 A = isl_mat_product(A, T);
3001 return A;
3004 /* Remove the prime factors from *g that have an exponent that
3005 * is strictly smaller than the exponent in "c".
3006 * All exponents in *g are known to be smaller than or equal
3007 * to those in "c".
3009 * That is, if *g is equal to
3011 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3013 * and "c" is equal to
3015 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3017 * then update *g to
3019 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3020 * p_n^{e_n * (e_n = f_n)}
3022 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3023 * neither does the gcd of *g and c / *g.
3024 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3025 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3026 * Dividing *g by this gcd therefore strictly reduces the exponent
3027 * of the prime factors that need to be removed, while leaving the
3028 * other prime factors untouched.
3029 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3030 * removes all undesired factors, without removing any others.
3032 static void remove_incomplete_powers(isl_int *g, isl_int c)
3034 isl_int t;
3036 isl_int_init(t);
3037 for (;;) {
3038 isl_int_divexact(t, c, *g);
3039 isl_int_gcd(t, t, *g);
3040 if (isl_int_is_one(t))
3041 break;
3042 isl_int_divexact(*g, *g, t);
3044 isl_int_clear(t);
3047 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3048 * of the same stride constraints in a compressed space that exploits
3049 * all equalities in the context and the other equalities in "bmap".
3051 * If the stride constraints of "bmap" are of the form
3053 * C(x) + D(y) = 0
3055 * then A is of the form
3057 * B(x') + D(y) = 0
3059 * If any of these constraints involves only a single local variable y,
3060 * then the constraint appears as
3062 * f(x) + m y_i = 0
3064 * in "bmap" and as
3066 * h(x') + m y_i = 0
3068 * in "A".
3070 * Let g be the gcd of m and the coefficients of h.
3071 * Then, in particular, g is a divisor of the coefficients of h and
3073 * f(x) = h(x')
3075 * is known to be a multiple of g.
3076 * If some prime factor in m appears with the same exponent in g,
3077 * then it can be removed from m because f(x) is already known
3078 * to be a multiple of g and therefore in particular of this power
3079 * of the prime factors.
3080 * Prime factors that appear with a smaller exponent in g cannot
3081 * be removed from m.
3082 * Let g' be the divisor of g containing all prime factors that
3083 * appear with the same exponent in m and g, then
3085 * f(x) + m y_i = 0
3087 * can be replaced by
3089 * f(x) + m/g' y_i' = 0
3091 * Note that (if g' != 1) this changes the explicit representation
3092 * of y_i to that of y_i', so the integer division at position i
3093 * is marked unknown and later recomputed by a call to
3094 * isl_basic_map_gauss.
3096 static __isl_give isl_basic_map *reduce_stride_constraints(
3097 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3099 int i;
3100 int total, n_div;
3101 int any = 0;
3102 isl_int gcd;
3104 if (!bmap || !A)
3105 return isl_basic_map_free(bmap);
3107 total = isl_basic_map_dim(bmap, isl_dim_all);
3108 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3109 total -= n_div;
3111 isl_int_init(gcd);
3112 for (i = 0; i < n; ++i) {
3113 int div;
3115 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3116 if (div < 0)
3117 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3118 "equality constraints modified unexpectedly",
3119 goto error);
3120 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3121 n_div - div - 1) != -1)
3122 continue;
3123 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3124 goto error;
3125 if (isl_int_is_one(gcd))
3126 continue;
3127 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3128 if (isl_int_is_one(gcd))
3129 continue;
3130 isl_int_divexact(bmap->eq[i][1 + total + div],
3131 bmap->eq[i][1 + total + div], gcd);
3132 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3133 if (!bmap)
3134 goto error;
3135 any = 1;
3137 isl_int_clear(gcd);
3139 if (any)
3140 bmap = isl_basic_map_gauss(bmap, NULL);
3142 return bmap;
3143 error:
3144 isl_int_clear(gcd);
3145 isl_basic_map_free(bmap);
3146 return NULL;
3149 /* Simplify the stride constraints in "bmap" based on
3150 * the remaining equality constraints in "bmap" and all equality
3151 * constraints in "context".
3152 * Only do this if both "bmap" and "context" have stride constraints.
3154 * First extract a copy of the stride constraints in "bmap" in a compressed
3155 * space exploiting all the other equality constraints and then
3156 * use this compressed copy to simplify the original stride constraints.
3158 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3159 __isl_keep isl_basic_map *context)
3161 int bmap_n_eq, context_n_eq;
3162 isl_mat *A;
3164 if (!bmap || !context)
3165 return isl_basic_map_free(bmap);
3167 bmap_n_eq = n_div_eq(bmap);
3168 context_n_eq = n_div_eq(context);
3170 if (bmap_n_eq < 0 || context_n_eq < 0)
3171 return isl_basic_map_free(bmap);
3172 if (bmap_n_eq == 0 || context_n_eq == 0)
3173 return bmap;
3175 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3176 context, context_n_eq);
3177 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3179 isl_mat_free(A);
3181 return bmap;
3184 /* Return a basic map that has the same intersection with "context" as "bmap"
3185 * and that is as "simple" as possible.
3187 * The core computation is performed on the pure constraints.
3188 * When we add back the meaning of the integer divisions, we need
3189 * to (re)introduce the div constraints. If we happen to have
3190 * discovered that some of these integer divisions are equal to
3191 * some affine combination of other variables, then these div
3192 * constraints may end up getting simplified in terms of the equalities,
3193 * resulting in extra inequalities on the other variables that
3194 * may have been removed already or that may not even have been
3195 * part of the input. We try and remove those constraints of
3196 * this form that are most obviously redundant with respect to
3197 * the context. We also remove those div constraints that are
3198 * redundant with respect to the other constraints in the result.
3200 * The stride constraints among the equality constraints in "bmap" are
3201 * also simplified with respecting to the other equality constraints
3202 * in "bmap" and with respect to all equality constraints in "context".
3204 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3205 struct isl_basic_map *context)
3207 isl_basic_set *bset, *eq;
3208 isl_basic_map *eq_bmap;
3209 unsigned total, n_div, extra, n_eq, n_ineq;
3211 if (!bmap || !context)
3212 goto error;
3214 if (isl_basic_map_plain_is_universe(bmap)) {
3215 isl_basic_map_free(context);
3216 return bmap;
3218 if (isl_basic_map_plain_is_empty(context)) {
3219 isl_space *space = isl_basic_map_get_space(bmap);
3220 isl_basic_map_free(bmap);
3221 isl_basic_map_free(context);
3222 return isl_basic_map_universe(space);
3224 if (isl_basic_map_plain_is_empty(bmap)) {
3225 isl_basic_map_free(context);
3226 return bmap;
3229 bmap = isl_basic_map_remove_redundancies(bmap);
3230 context = isl_basic_map_remove_redundancies(context);
3231 if (!context)
3232 goto error;
3234 context = isl_basic_map_align_divs(context, bmap);
3235 n_div = isl_basic_map_dim(context, isl_dim_div);
3236 total = isl_basic_map_dim(bmap, isl_dim_all);
3237 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3239 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3240 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3241 bset = uset_gist(bset,
3242 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3243 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3245 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3246 isl_basic_set_plain_is_empty(bset)) {
3247 isl_basic_map_free(context);
3248 return isl_basic_map_overlying_set(bset, bmap);
3251 n_eq = bset->n_eq;
3252 n_ineq = bset->n_ineq;
3253 eq = isl_basic_set_copy(bset);
3254 eq = isl_basic_set_cow(eq);
3255 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3256 eq = isl_basic_set_free(eq);
3257 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3258 bset = isl_basic_set_free(bset);
3260 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3261 eq_bmap = gist_strides(eq_bmap, context);
3262 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3263 bmap = isl_basic_map_overlying_set(bset, bmap);
3264 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3265 bmap = isl_basic_map_remove_redundancies(bmap);
3267 return bmap;
3268 error:
3269 isl_basic_map_free(bmap);
3270 isl_basic_map_free(context);
3271 return NULL;
3275 * Assumes context has no implicit divs.
3277 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3278 __isl_take isl_basic_map *context)
3280 int i;
3282 if (!map || !context)
3283 goto error;
3285 if (isl_basic_map_plain_is_empty(context)) {
3286 isl_space *space = isl_map_get_space(map);
3287 isl_map_free(map);
3288 isl_basic_map_free(context);
3289 return isl_map_universe(space);
3292 context = isl_basic_map_remove_redundancies(context);
3293 map = isl_map_cow(map);
3294 if (!map || !context)
3295 goto error;
3296 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3297 map = isl_map_compute_divs(map);
3298 if (!map)
3299 goto error;
3300 for (i = map->n - 1; i >= 0; --i) {
3301 map->p[i] = isl_basic_map_gist(map->p[i],
3302 isl_basic_map_copy(context));
3303 if (!map->p[i])
3304 goto error;
3305 if (isl_basic_map_plain_is_empty(map->p[i])) {
3306 isl_basic_map_free(map->p[i]);
3307 if (i != map->n - 1)
3308 map->p[i] = map->p[map->n - 1];
3309 map->n--;
3312 isl_basic_map_free(context);
3313 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3314 return map;
3315 error:
3316 isl_map_free(map);
3317 isl_basic_map_free(context);
3318 return NULL;
3321 /* Drop all inequalities from "bmap" that also appear in "context".
3322 * "context" is assumed to have only known local variables and
3323 * the initial local variables of "bmap" are assumed to be the same
3324 * as those of "context".
3325 * The constraints of both "bmap" and "context" are assumed
3326 * to have been sorted using isl_basic_map_sort_constraints.
3328 * Run through the inequality constraints of "bmap" and "context"
3329 * in sorted order.
3330 * If a constraint of "bmap" involves variables not in "context",
3331 * then it cannot appear in "context".
3332 * If a matching constraint is found, it is removed from "bmap".
3334 static __isl_give isl_basic_map *drop_inequalities(
3335 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3337 int i1, i2;
3338 unsigned total, extra;
3340 if (!bmap || !context)
3341 return isl_basic_map_free(bmap);
3343 total = isl_basic_map_total_dim(context);
3344 extra = isl_basic_map_total_dim(bmap) - total;
3346 i1 = bmap->n_ineq - 1;
3347 i2 = context->n_ineq - 1;
3348 while (bmap && i1 >= 0 && i2 >= 0) {
3349 int cmp;
3351 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3352 extra) != -1) {
3353 --i1;
3354 continue;
3356 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3357 context->ineq[i2]);
3358 if (cmp < 0) {
3359 --i2;
3360 continue;
3362 if (cmp > 0) {
3363 --i1;
3364 continue;
3366 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3367 bmap = isl_basic_map_cow(bmap);
3368 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3369 bmap = isl_basic_map_free(bmap);
3371 --i1;
3372 --i2;
3375 return bmap;
3378 /* Drop all equalities from "bmap" that also appear in "context".
3379 * "context" is assumed to have only known local variables and
3380 * the initial local variables of "bmap" are assumed to be the same
3381 * as those of "context".
3383 * Run through the equality constraints of "bmap" and "context"
3384 * in sorted order.
3385 * If a constraint of "bmap" involves variables not in "context",
3386 * then it cannot appear in "context".
3387 * If a matching constraint is found, it is removed from "bmap".
3389 static __isl_give isl_basic_map *drop_equalities(
3390 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3392 int i1, i2;
3393 unsigned total, extra;
3395 if (!bmap || !context)
3396 return isl_basic_map_free(bmap);
3398 total = isl_basic_map_total_dim(context);
3399 extra = isl_basic_map_total_dim(bmap) - total;
3401 i1 = bmap->n_eq - 1;
3402 i2 = context->n_eq - 1;
3404 while (bmap && i1 >= 0 && i2 >= 0) {
3405 int last1, last2;
3407 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3408 extra) != -1)
3409 break;
3410 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3411 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3412 if (last1 > last2) {
3413 --i2;
3414 continue;
3416 if (last1 < last2) {
3417 --i1;
3418 continue;
3420 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3421 bmap = isl_basic_map_cow(bmap);
3422 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3423 bmap = isl_basic_map_free(bmap);
3425 --i1;
3426 --i2;
3429 return bmap;
3432 /* Remove the constraints in "context" from "bmap".
3433 * "context" is assumed to have explicit representations
3434 * for all local variables.
3436 * First align the divs of "bmap" to those of "context" and
3437 * sort the constraints. Then drop all constraints from "bmap"
3438 * that appear in "context".
3440 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3441 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3443 isl_bool done, known;
3445 done = isl_basic_map_plain_is_universe(context);
3446 if (done == isl_bool_false)
3447 done = isl_basic_map_plain_is_universe(bmap);
3448 if (done == isl_bool_false)
3449 done = isl_basic_map_plain_is_empty(context);
3450 if (done == isl_bool_false)
3451 done = isl_basic_map_plain_is_empty(bmap);
3452 if (done < 0)
3453 goto error;
3454 if (done) {
3455 isl_basic_map_free(context);
3456 return bmap;
3458 known = isl_basic_map_divs_known(context);
3459 if (known < 0)
3460 goto error;
3461 if (!known)
3462 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3463 "context has unknown divs", goto error);
3465 bmap = isl_basic_map_align_divs(bmap, context);
3466 bmap = isl_basic_map_gauss(bmap, NULL);
3467 bmap = isl_basic_map_sort_constraints(bmap);
3468 context = isl_basic_map_sort_constraints(context);
3470 bmap = drop_inequalities(bmap, context);
3471 bmap = drop_equalities(bmap, context);
3473 isl_basic_map_free(context);
3474 bmap = isl_basic_map_finalize(bmap);
3475 return bmap;
3476 error:
3477 isl_basic_map_free(bmap);
3478 isl_basic_map_free(context);
3479 return NULL;
3482 /* Replace "map" by the disjunct at position "pos" and free "context".
3484 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3485 int pos, __isl_take isl_basic_map *context)
3487 isl_basic_map *bmap;
3489 bmap = isl_basic_map_copy(map->p[pos]);
3490 isl_map_free(map);
3491 isl_basic_map_free(context);
3492 return isl_map_from_basic_map(bmap);
3495 /* Remove the constraints in "context" from "map".
3496 * If any of the disjuncts in the result turns out to be the universe,
3497 * then return this universe.
3498 * "context" is assumed to have explicit representations
3499 * for all local variables.
3501 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3502 __isl_take isl_basic_map *context)
3504 int i;
3505 isl_bool univ, known;
3507 univ = isl_basic_map_plain_is_universe(context);
3508 if (univ < 0)
3509 goto error;
3510 if (univ) {
3511 isl_basic_map_free(context);
3512 return map;
3514 known = isl_basic_map_divs_known(context);
3515 if (known < 0)
3516 goto error;
3517 if (!known)
3518 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3519 "context has unknown divs", goto error);
3521 map = isl_map_cow(map);
3522 if (!map)
3523 goto error;
3524 for (i = 0; i < map->n; ++i) {
3525 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3526 isl_basic_map_copy(context));
3527 univ = isl_basic_map_plain_is_universe(map->p[i]);
3528 if (univ < 0)
3529 goto error;
3530 if (univ && map->n > 1)
3531 return replace_by_disjunct(map, i, context);
3534 isl_basic_map_free(context);
3535 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3536 if (map->n > 1)
3537 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3538 return map;
3539 error:
3540 isl_map_free(map);
3541 isl_basic_map_free(context);
3542 return NULL;
3545 /* Replace "map" by a universe map in the same space and free "drop".
3547 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3548 __isl_take isl_map *drop)
3550 isl_map *res;
3552 res = isl_map_universe(isl_map_get_space(map));
3553 isl_map_free(map);
3554 isl_map_free(drop);
3555 return res;
3558 /* Return a map that has the same intersection with "context" as "map"
3559 * and that is as "simple" as possible.
3561 * If "map" is already the universe, then we cannot make it any simpler.
3562 * Similarly, if "context" is the universe, then we cannot exploit it
3563 * to simplify "map"
3564 * If "map" and "context" are identical to each other, then we can
3565 * return the corresponding universe.
3567 * If either "map" or "context" consists of multiple disjuncts,
3568 * then check if "context" happens to be a subset of "map",
3569 * in which case all constraints can be removed.
3570 * In case of multiple disjuncts, the standard procedure
3571 * may not be able to detect that all constraints can be removed.
3573 * If none of these cases apply, we have to work a bit harder.
3574 * During this computation, we make use of a single disjunct context,
3575 * so if the original context consists of more than one disjunct
3576 * then we need to approximate the context by a single disjunct set.
3577 * Simply taking the simple hull may drop constraints that are
3578 * only implicitly available in each disjunct. We therefore also
3579 * look for constraints among those defining "map" that are valid
3580 * for the context. These can then be used to simplify away
3581 * the corresponding constraints in "map".
3583 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3584 __isl_take isl_map *context)
3586 int equal;
3587 int is_universe;
3588 int single_disjunct_map, single_disjunct_context;
3589 isl_bool subset;
3590 isl_basic_map *hull;
3592 is_universe = isl_map_plain_is_universe(map);
3593 if (is_universe >= 0 && !is_universe)
3594 is_universe = isl_map_plain_is_universe(context);
3595 if (is_universe < 0)
3596 goto error;
3597 if (is_universe) {
3598 isl_map_free(context);
3599 return map;
3602 equal = isl_map_plain_is_equal(map, context);
3603 if (equal < 0)
3604 goto error;
3605 if (equal)
3606 return replace_by_universe(map, context);
3608 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3609 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3610 if (!single_disjunct_map || !single_disjunct_context) {
3611 subset = isl_map_is_subset(context, map);
3612 if (subset < 0)
3613 goto error;
3614 if (subset)
3615 return replace_by_universe(map, context);
3618 context = isl_map_compute_divs(context);
3619 if (!context)
3620 goto error;
3621 if (single_disjunct_context) {
3622 hull = isl_map_simple_hull(context);
3623 } else {
3624 isl_ctx *ctx;
3625 isl_map_list *list;
3627 ctx = isl_map_get_ctx(map);
3628 list = isl_map_list_alloc(ctx, 2);
3629 list = isl_map_list_add(list, isl_map_copy(context));
3630 list = isl_map_list_add(list, isl_map_copy(map));
3631 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3632 list);
3634 return isl_map_gist_basic_map(map, hull);
3635 error:
3636 isl_map_free(map);
3637 isl_map_free(context);
3638 return NULL;
3641 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3642 __isl_take isl_map *context)
3644 return isl_map_align_params_map_map_and(map, context, &map_gist);
3647 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3648 struct isl_basic_set *context)
3650 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3651 bset_to_bmap(context)));
3654 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3655 __isl_take isl_basic_set *context)
3657 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3658 bset_to_bmap(context)));
3661 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3662 __isl_take isl_basic_set *context)
3664 isl_space *space = isl_set_get_space(set);
3665 isl_basic_set *dom_context = isl_basic_set_universe(space);
3666 dom_context = isl_basic_set_intersect_params(dom_context, context);
3667 return isl_set_gist_basic_set(set, dom_context);
3670 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3671 __isl_take isl_set *context)
3673 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3676 /* Compute the gist of "bmap" with respect to the constraints "context"
3677 * on the domain.
3679 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3680 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3682 isl_space *space = isl_basic_map_get_space(bmap);
3683 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3685 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3686 return isl_basic_map_gist(bmap, bmap_context);
3689 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3690 __isl_take isl_set *context)
3692 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3693 map_context = isl_map_intersect_domain(map_context, context);
3694 return isl_map_gist(map, map_context);
3697 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3698 __isl_take isl_set *context)
3700 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3701 map_context = isl_map_intersect_range(map_context, context);
3702 return isl_map_gist(map, map_context);
3705 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3706 __isl_take isl_set *context)
3708 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3709 map_context = isl_map_intersect_params(map_context, context);
3710 return isl_map_gist(map, map_context);
3713 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3714 __isl_take isl_set *context)
3716 return isl_map_gist_params(set, context);
3719 /* Quick check to see if two basic maps are disjoint.
3720 * In particular, we reduce the equalities and inequalities of
3721 * one basic map in the context of the equalities of the other
3722 * basic map and check if we get a contradiction.
3724 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3725 __isl_keep isl_basic_map *bmap2)
3727 struct isl_vec *v = NULL;
3728 int *elim = NULL;
3729 unsigned total;
3730 int i;
3732 if (!bmap1 || !bmap2)
3733 return isl_bool_error;
3734 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3735 return isl_bool_error);
3736 if (bmap1->n_div || bmap2->n_div)
3737 return isl_bool_false;
3738 if (!bmap1->n_eq && !bmap2->n_eq)
3739 return isl_bool_false;
3741 total = isl_space_dim(bmap1->dim, isl_dim_all);
3742 if (total == 0)
3743 return isl_bool_false;
3744 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3745 if (!v)
3746 goto error;
3747 elim = isl_alloc_array(bmap1->ctx, int, total);
3748 if (!elim)
3749 goto error;
3750 compute_elimination_index(bmap1, elim);
3751 for (i = 0; i < bmap2->n_eq; ++i) {
3752 int reduced;
3753 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3754 bmap1, elim);
3755 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3756 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3757 goto disjoint;
3759 for (i = 0; i < bmap2->n_ineq; ++i) {
3760 int reduced;
3761 reduced = reduced_using_equalities(v->block.data,
3762 bmap2->ineq[i], bmap1, elim);
3763 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3764 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3765 goto disjoint;
3767 compute_elimination_index(bmap2, elim);
3768 for (i = 0; i < bmap1->n_ineq; ++i) {
3769 int reduced;
3770 reduced = reduced_using_equalities(v->block.data,
3771 bmap1->ineq[i], bmap2, elim);
3772 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3773 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3774 goto disjoint;
3776 isl_vec_free(v);
3777 free(elim);
3778 return isl_bool_false;
3779 disjoint:
3780 isl_vec_free(v);
3781 free(elim);
3782 return isl_bool_true;
3783 error:
3784 isl_vec_free(v);
3785 free(elim);
3786 return isl_bool_error;
3789 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3790 __isl_keep isl_basic_set *bset2)
3792 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3793 bset_to_bmap(bset2));
3796 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3798 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3799 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3800 __isl_keep isl_basic_map *bmap2))
3802 int i, j;
3804 if (!map1 || !map2)
3805 return isl_bool_error;
3807 for (i = 0; i < map1->n; ++i) {
3808 for (j = 0; j < map2->n; ++j) {
3809 isl_bool d = test(map1->p[i], map2->p[j]);
3810 if (d != isl_bool_true)
3811 return d;
3815 return isl_bool_true;
3818 /* Are "map1" and "map2" obviously disjoint, based on information
3819 * that can be derived without looking at the individual basic maps?
3821 * In particular, if one of them is empty or if they live in different spaces
3822 * (ignoring parameters), then they are clearly disjoint.
3824 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3825 __isl_keep isl_map *map2)
3827 isl_bool disjoint;
3828 isl_bool match;
3830 if (!map1 || !map2)
3831 return isl_bool_error;
3833 disjoint = isl_map_plain_is_empty(map1);
3834 if (disjoint < 0 || disjoint)
3835 return disjoint;
3837 disjoint = isl_map_plain_is_empty(map2);
3838 if (disjoint < 0 || disjoint)
3839 return disjoint;
3841 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3842 map2->dim, isl_dim_in);
3843 if (match < 0 || !match)
3844 return match < 0 ? isl_bool_error : isl_bool_true;
3846 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3847 map2->dim, isl_dim_out);
3848 if (match < 0 || !match)
3849 return match < 0 ? isl_bool_error : isl_bool_true;
3851 return isl_bool_false;
3854 /* Are "map1" and "map2" obviously disjoint?
3856 * If one of them is empty or if they live in different spaces (ignoring
3857 * parameters), then they are clearly disjoint.
3858 * This is checked by isl_map_plain_is_disjoint_global.
3860 * If they have different parameters, then we skip any further tests.
3862 * If they are obviously equal, but not obviously empty, then we will
3863 * not be able to detect if they are disjoint.
3865 * Otherwise we check if each basic map in "map1" is obviously disjoint
3866 * from each basic map in "map2".
3868 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3869 __isl_keep isl_map *map2)
3871 isl_bool disjoint;
3872 isl_bool intersect;
3873 isl_bool match;
3875 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3876 if (disjoint < 0 || disjoint)
3877 return disjoint;
3879 match = isl_space_match(map1->dim, isl_dim_param,
3880 map2->dim, isl_dim_param);
3881 if (match < 0 || !match)
3882 return match < 0 ? isl_bool_error : isl_bool_false;
3884 intersect = isl_map_plain_is_equal(map1, map2);
3885 if (intersect < 0 || intersect)
3886 return intersect < 0 ? isl_bool_error : isl_bool_false;
3888 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3891 /* Are "map1" and "map2" disjoint?
3893 * They are disjoint if they are "obviously disjoint" or if one of them
3894 * is empty. Otherwise, they are not disjoint if one of them is universal.
3895 * If the two inputs are (obviously) equal and not empty, then they are
3896 * not disjoint.
3897 * If none of these cases apply, then check if all pairs of basic maps
3898 * are disjoint.
3900 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3902 isl_bool disjoint;
3903 isl_bool intersect;
3905 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3906 if (disjoint < 0 || disjoint)
3907 return disjoint;
3909 disjoint = isl_map_is_empty(map1);
3910 if (disjoint < 0 || disjoint)
3911 return disjoint;
3913 disjoint = isl_map_is_empty(map2);
3914 if (disjoint < 0 || disjoint)
3915 return disjoint;
3917 intersect = isl_map_plain_is_universe(map1);
3918 if (intersect < 0 || intersect)
3919 return intersect < 0 ? isl_bool_error : isl_bool_false;
3921 intersect = isl_map_plain_is_universe(map2);
3922 if (intersect < 0 || intersect)
3923 return intersect < 0 ? isl_bool_error : isl_bool_false;
3925 intersect = isl_map_plain_is_equal(map1, map2);
3926 if (intersect < 0 || intersect)
3927 return isl_bool_not(intersect);
3929 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3932 /* Are "bmap1" and "bmap2" disjoint?
3934 * They are disjoint if they are "obviously disjoint" or if one of them
3935 * is empty. Otherwise, they are not disjoint if one of them is universal.
3936 * If none of these cases apply, we compute the intersection and see if
3937 * the result is empty.
3939 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3940 __isl_keep isl_basic_map *bmap2)
3942 isl_bool disjoint;
3943 isl_bool intersect;
3944 isl_basic_map *test;
3946 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3947 if (disjoint < 0 || disjoint)
3948 return disjoint;
3950 disjoint = isl_basic_map_is_empty(bmap1);
3951 if (disjoint < 0 || disjoint)
3952 return disjoint;
3954 disjoint = isl_basic_map_is_empty(bmap2);
3955 if (disjoint < 0 || disjoint)
3956 return disjoint;
3958 intersect = isl_basic_map_plain_is_universe(bmap1);
3959 if (intersect < 0 || intersect)
3960 return intersect < 0 ? isl_bool_error : isl_bool_false;
3962 intersect = isl_basic_map_plain_is_universe(bmap2);
3963 if (intersect < 0 || intersect)
3964 return intersect < 0 ? isl_bool_error : isl_bool_false;
3966 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3967 isl_basic_map_copy(bmap2));
3968 disjoint = isl_basic_map_is_empty(test);
3969 isl_basic_map_free(test);
3971 return disjoint;
3974 /* Are "bset1" and "bset2" disjoint?
3976 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3977 __isl_keep isl_basic_set *bset2)
3979 return isl_basic_map_is_disjoint(bset1, bset2);
3982 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3983 __isl_keep isl_set *set2)
3985 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3988 /* Are "set1" and "set2" disjoint?
3990 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3992 return isl_map_is_disjoint(set1, set2);
3995 /* Is "v" equal to 0, 1 or -1?
3997 static int is_zero_or_one(isl_int v)
3999 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4002 /* Check if we can combine a given div with lower bound l and upper
4003 * bound u with some other div and if so return that other div.
4004 * Otherwise return -1.
4006 * We first check that
4007 * - the bounds are opposites of each other (except for the constant
4008 * term)
4009 * - the bounds do not reference any other div
4010 * - no div is defined in terms of this div
4012 * Let m be the size of the range allowed on the div by the bounds.
4013 * That is, the bounds are of the form
4015 * e <= a <= e + m - 1
4017 * with e some expression in the other variables.
4018 * We look for another div b such that no third div is defined in terms
4019 * of this second div b and such that in any constraint that contains
4020 * a (except for the given lower and upper bound), also contains b
4021 * with a coefficient that is m times that of b.
4022 * That is, all constraints (execpt for the lower and upper bound)
4023 * are of the form
4025 * e + f (a + m b) >= 0
4027 * Furthermore, in the constraints that only contain b, the coefficient
4028 * of b should be equal to 1 or -1.
4029 * If so, we return b so that "a + m b" can be replaced by
4030 * a single div "c = a + m b".
4032 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4033 unsigned div, unsigned l, unsigned u)
4035 int i, j;
4036 unsigned dim;
4037 int coalesce = -1;
4039 if (bmap->n_div <= 1)
4040 return -1;
4041 dim = isl_space_dim(bmap->dim, isl_dim_all);
4042 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4043 return -1;
4044 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4045 bmap->n_div - div - 1) != -1)
4046 return -1;
4047 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4048 dim + bmap->n_div))
4049 return -1;
4051 for (i = 0; i < bmap->n_div; ++i) {
4052 if (isl_int_is_zero(bmap->div[i][0]))
4053 continue;
4054 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4055 return -1;
4058 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4059 if (isl_int_is_neg(bmap->ineq[l][0])) {
4060 isl_int_sub(bmap->ineq[l][0],
4061 bmap->ineq[l][0], bmap->ineq[u][0]);
4062 bmap = isl_basic_map_copy(bmap);
4063 bmap = isl_basic_map_set_to_empty(bmap);
4064 isl_basic_map_free(bmap);
4065 return -1;
4067 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4068 for (i = 0; i < bmap->n_div; ++i) {
4069 if (i == div)
4070 continue;
4071 if (!pairs[i])
4072 continue;
4073 for (j = 0; j < bmap->n_div; ++j) {
4074 if (isl_int_is_zero(bmap->div[j][0]))
4075 continue;
4076 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4077 break;
4079 if (j < bmap->n_div)
4080 continue;
4081 for (j = 0; j < bmap->n_ineq; ++j) {
4082 int valid;
4083 if (j == l || j == u)
4084 continue;
4085 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4086 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4087 continue;
4088 break;
4090 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4091 break;
4092 isl_int_mul(bmap->ineq[j][1 + dim + div],
4093 bmap->ineq[j][1 + dim + div],
4094 bmap->ineq[l][0]);
4095 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4096 bmap->ineq[j][1 + dim + i]);
4097 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4098 bmap->ineq[j][1 + dim + div],
4099 bmap->ineq[l][0]);
4100 if (!valid)
4101 break;
4103 if (j < bmap->n_ineq)
4104 continue;
4105 coalesce = i;
4106 break;
4108 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4109 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4110 return coalesce;
4113 /* Internal data structure used during the construction and/or evaluation of
4114 * an inequality that ensures that a pair of bounds always allows
4115 * for an integer value.
4117 * "tab" is the tableau in which the inequality is evaluated. It may
4118 * be NULL until it is actually needed.
4119 * "v" contains the inequality coefficients.
4120 * "g", "fl" and "fu" are temporary scalars used during the construction and
4121 * evaluation.
4123 struct test_ineq_data {
4124 struct isl_tab *tab;
4125 isl_vec *v;
4126 isl_int g;
4127 isl_int fl;
4128 isl_int fu;
4131 /* Free all the memory allocated by the fields of "data".
4133 static void test_ineq_data_clear(struct test_ineq_data *data)
4135 isl_tab_free(data->tab);
4136 isl_vec_free(data->v);
4137 isl_int_clear(data->g);
4138 isl_int_clear(data->fl);
4139 isl_int_clear(data->fu);
4142 /* Is the inequality stored in data->v satisfied by "bmap"?
4143 * That is, does it only attain non-negative values?
4144 * data->tab is a tableau corresponding to "bmap".
4146 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4147 struct test_ineq_data *data)
4149 isl_ctx *ctx;
4150 enum isl_lp_result res;
4152 ctx = isl_basic_map_get_ctx(bmap);
4153 if (!data->tab)
4154 data->tab = isl_tab_from_basic_map(bmap, 0);
4155 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4156 if (res == isl_lp_error)
4157 return isl_bool_error;
4158 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4161 /* Given a lower and an upper bound on div i, do they always allow
4162 * for an integer value of the given div?
4163 * Determine this property by constructing an inequality
4164 * such that the property is guaranteed when the inequality is nonnegative.
4165 * The lower bound is inequality l, while the upper bound is inequality u.
4166 * The constructed inequality is stored in data->v.
4168 * Let the upper bound be
4170 * -n_u a + e_u >= 0
4172 * and the lower bound
4174 * n_l a + e_l >= 0
4176 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4177 * We have
4179 * - f_u e_l <= f_u f_l g a <= f_l e_u
4181 * Since all variables are integer valued, this is equivalent to
4183 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4185 * If this interval is at least f_u f_l g, then it contains at least
4186 * one integer value for a.
4187 * That is, the test constraint is
4189 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4191 * or
4193 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4195 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4196 * then the constraint can be scaled down by a factor g',
4197 * with the constant term replaced by
4198 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4199 * Note that the result of applying Fourier-Motzkin to this pair
4200 * of constraints is
4202 * f_l e_u + f_u e_l >= 0
4204 * If the constant term of the scaled down version of this constraint,
4205 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4206 * term of the scaled down test constraint, then the test constraint
4207 * is known to hold and no explicit evaluation is required.
4208 * This is essentially the Omega test.
4210 * If the test constraint consists of only a constant term, then
4211 * it is sufficient to look at the sign of this constant term.
4213 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4214 int l, int u, struct test_ineq_data *data)
4216 unsigned offset, n_div;
4217 offset = isl_basic_map_offset(bmap, isl_dim_div);
4218 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4220 isl_int_gcd(data->g,
4221 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4222 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4223 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4224 isl_int_neg(data->fu, data->fu);
4225 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4226 data->fu, bmap->ineq[l], offset + n_div);
4227 isl_int_mul(data->g, data->g, data->fl);
4228 isl_int_mul(data->g, data->g, data->fu);
4229 isl_int_sub(data->g, data->g, data->fl);
4230 isl_int_sub(data->g, data->g, data->fu);
4231 isl_int_add_ui(data->g, data->g, 1);
4232 isl_int_sub(data->fl, data->v->el[0], data->g);
4234 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4235 if (isl_int_is_zero(data->g))
4236 return isl_int_is_nonneg(data->fl);
4237 if (isl_int_is_one(data->g)) {
4238 isl_int_set(data->v->el[0], data->fl);
4239 return test_ineq_is_satisfied(bmap, data);
4241 isl_int_fdiv_q(data->fl, data->fl, data->g);
4242 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4243 if (isl_int_eq(data->fl, data->v->el[0]))
4244 return isl_bool_true;
4245 isl_int_set(data->v->el[0], data->fl);
4246 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4247 offset - 1 + n_div);
4249 return test_ineq_is_satisfied(bmap, data);
4252 /* Remove more kinds of divs that are not strictly needed.
4253 * In particular, if all pairs of lower and upper bounds on a div
4254 * are such that they allow at least one integer value of the div,
4255 * then we can eliminate the div using Fourier-Motzkin without
4256 * introducing any spurious solutions.
4258 * If at least one of the two constraints has a unit coefficient for the div,
4259 * then the presence of such a value is guaranteed so there is no need to check.
4260 * In particular, the value attained by the bound with unit coefficient
4261 * can serve as this intermediate value.
4263 static struct isl_basic_map *drop_more_redundant_divs(
4264 struct isl_basic_map *bmap, int *pairs, int n)
4266 isl_ctx *ctx;
4267 struct test_ineq_data data = { NULL, NULL };
4268 unsigned off, n_div;
4269 int remove = -1;
4271 isl_int_init(data.g);
4272 isl_int_init(data.fl);
4273 isl_int_init(data.fu);
4275 if (!bmap)
4276 goto error;
4278 ctx = isl_basic_map_get_ctx(bmap);
4279 off = isl_basic_map_offset(bmap, isl_dim_div);
4280 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4281 data.v = isl_vec_alloc(ctx, off + n_div);
4282 if (!data.v)
4283 goto error;
4285 while (n > 0) {
4286 int i, l, u;
4287 int best = -1;
4288 isl_bool has_int;
4290 for (i = 0; i < n_div; ++i) {
4291 if (!pairs[i])
4292 continue;
4293 if (best >= 0 && pairs[best] <= pairs[i])
4294 continue;
4295 best = i;
4298 i = best;
4299 for (l = 0; l < bmap->n_ineq; ++l) {
4300 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4301 continue;
4302 if (isl_int_is_one(bmap->ineq[l][off + i]))
4303 continue;
4304 for (u = 0; u < bmap->n_ineq; ++u) {
4305 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4306 continue;
4307 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4308 continue;
4309 has_int = int_between_bounds(bmap, i, l, u,
4310 &data);
4311 if (has_int < 0)
4312 goto error;
4313 if (data.tab && data.tab->empty)
4314 break;
4315 if (!has_int)
4316 break;
4318 if (u < bmap->n_ineq)
4319 break;
4321 if (data.tab && data.tab->empty) {
4322 bmap = isl_basic_map_set_to_empty(bmap);
4323 break;
4325 if (l == bmap->n_ineq) {
4326 remove = i;
4327 break;
4329 pairs[i] = 0;
4330 --n;
4333 test_ineq_data_clear(&data);
4335 free(pairs);
4337 if (remove < 0)
4338 return bmap;
4340 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4341 return isl_basic_map_drop_redundant_divs(bmap);
4342 error:
4343 free(pairs);
4344 isl_basic_map_free(bmap);
4345 test_ineq_data_clear(&data);
4346 return NULL;
4349 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4350 * and the upper bound u, div1 always occurs together with div2 in the form
4351 * (div1 + m div2), where m is the constant range on the variable div1
4352 * allowed by l and u, replace the pair div1 and div2 by a single
4353 * div that is equal to div1 + m div2.
4355 * The new div will appear in the location that contains div2.
4356 * We need to modify all constraints that contain
4357 * div2 = (div - div1) / m
4358 * The coefficient of div2 is known to be equal to 1 or -1.
4359 * (If a constraint does not contain div2, it will also not contain div1.)
4360 * If the constraint also contains div1, then we know they appear
4361 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4362 * i.e., the coefficient of div is f.
4364 * Otherwise, we first need to introduce div1 into the constraint.
4365 * Let the l be
4367 * div1 + f >=0
4369 * and u
4371 * -div1 + f' >= 0
4373 * A lower bound on div2
4375 * div2 + t >= 0
4377 * can be replaced by
4379 * m div2 + div1 + m t + f >= 0
4381 * An upper bound
4383 * -div2 + t >= 0
4385 * can be replaced by
4387 * -(m div2 + div1) + m t + f' >= 0
4389 * These constraint are those that we would obtain from eliminating
4390 * div1 using Fourier-Motzkin.
4392 * After all constraints have been modified, we drop the lower and upper
4393 * bound and then drop div1.
4395 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4396 unsigned div1, unsigned div2, unsigned l, unsigned u)
4398 isl_ctx *ctx;
4399 isl_int m;
4400 unsigned dim, total;
4401 int i;
4403 ctx = isl_basic_map_get_ctx(bmap);
4405 dim = isl_space_dim(bmap->dim, isl_dim_all);
4406 total = 1 + dim + bmap->n_div;
4408 isl_int_init(m);
4409 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4410 isl_int_add_ui(m, m, 1);
4412 for (i = 0; i < bmap->n_ineq; ++i) {
4413 if (i == l || i == u)
4414 continue;
4415 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4416 continue;
4417 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4418 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4419 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4420 ctx->one, bmap->ineq[l], total);
4421 else
4422 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4423 ctx->one, bmap->ineq[u], total);
4425 isl_int_set(bmap->ineq[i][1 + dim + div2],
4426 bmap->ineq[i][1 + dim + div1]);
4427 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4430 isl_int_clear(m);
4431 if (l > u) {
4432 isl_basic_map_drop_inequality(bmap, l);
4433 isl_basic_map_drop_inequality(bmap, u);
4434 } else {
4435 isl_basic_map_drop_inequality(bmap, u);
4436 isl_basic_map_drop_inequality(bmap, l);
4438 bmap = isl_basic_map_drop_div(bmap, div1);
4439 return bmap;
4442 /* First check if we can coalesce any pair of divs and
4443 * then continue with dropping more redundant divs.
4445 * We loop over all pairs of lower and upper bounds on a div
4446 * with coefficient 1 and -1, respectively, check if there
4447 * is any other div "c" with which we can coalesce the div
4448 * and if so, perform the coalescing.
4450 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4451 struct isl_basic_map *bmap, int *pairs, int n)
4453 int i, l, u;
4454 unsigned dim;
4456 dim = isl_space_dim(bmap->dim, isl_dim_all);
4458 for (i = 0; i < bmap->n_div; ++i) {
4459 if (!pairs[i])
4460 continue;
4461 for (l = 0; l < bmap->n_ineq; ++l) {
4462 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4463 continue;
4464 for (u = 0; u < bmap->n_ineq; ++u) {
4465 int c;
4467 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4468 continue;
4469 c = div_find_coalesce(bmap, pairs, i, l, u);
4470 if (c < 0)
4471 continue;
4472 free(pairs);
4473 bmap = coalesce_divs(bmap, i, c, l, u);
4474 return isl_basic_map_drop_redundant_divs(bmap);
4479 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4480 return bmap;
4482 return drop_more_redundant_divs(bmap, pairs, n);
4485 /* Are the "n" coefficients starting at "first" of inequality constraints
4486 * "i" and "j" of "bmap" equal to each other?
4488 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4489 int first, int n)
4491 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4494 /* Are the "n" coefficients starting at "first" of inequality constraints
4495 * "i" and "j" of "bmap" opposite to each other?
4497 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4498 int first, int n)
4500 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4503 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4504 * apart from the constant term?
4506 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4508 unsigned total;
4510 total = isl_basic_map_dim(bmap, isl_dim_all);
4511 return is_opposite_part(bmap, i, j, 1, total);
4514 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4515 * apart from the constant term and the coefficient at position "pos"?
4517 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4518 int pos)
4520 unsigned total;
4522 total = isl_basic_map_dim(bmap, isl_dim_all);
4523 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4524 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4527 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4528 * apart from the constant term and the coefficient at position "pos"?
4530 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4531 int pos)
4533 unsigned total;
4535 total = isl_basic_map_dim(bmap, isl_dim_all);
4536 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4537 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4540 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4541 * been modified, simplying it if "simplify" is set.
4542 * Free the temporary data structure "pairs" that was associated
4543 * to the old version of "bmap".
4545 static __isl_give isl_basic_map *drop_redundant_divs_again(
4546 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4548 if (simplify)
4549 bmap = isl_basic_map_simplify(bmap);
4550 free(pairs);
4551 return isl_basic_map_drop_redundant_divs(bmap);
4554 /* Is "div" the single unknown existentially quantified variable
4555 * in inequality constraint "ineq" of "bmap"?
4556 * "div" is known to have a non-zero coefficient in "ineq".
4558 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4560 int i;
4561 unsigned n_div, o_div;
4563 if (isl_basic_map_div_is_known(bmap, div))
4564 return 0;
4565 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4566 if (n_div == 1)
4567 return 1;
4568 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4569 for (i = 0; i < n_div; ++i) {
4570 if (i == div)
4571 continue;
4572 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4573 continue;
4574 if (!isl_basic_map_div_is_known(bmap, i))
4575 return 0;
4578 return 1;
4581 /* Does integer division "div" have coefficient 1 in inequality constraint
4582 * "ineq" of "map"?
4584 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4586 unsigned o_div;
4588 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4589 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4590 return 1;
4592 return 0;
4595 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4596 * then try and drop redundant divs again,
4597 * freeing the temporary data structure "pairs" that was associated
4598 * to the old version of "bmap".
4600 static __isl_give isl_basic_map *set_eq_and_try_again(
4601 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4603 bmap = isl_basic_map_cow(bmap);
4604 isl_basic_map_inequality_to_equality(bmap, ineq);
4605 return drop_redundant_divs_again(bmap, pairs, 1);
4608 /* Drop the integer division at position "div", along with the two
4609 * inequality constraints "ineq1" and "ineq2" in which it appears
4610 * from "bmap" and then try and drop redundant divs again,
4611 * freeing the temporary data structure "pairs" that was associated
4612 * to the old version of "bmap".
4614 static __isl_give isl_basic_map *drop_div_and_try_again(
4615 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4616 __isl_take int *pairs)
4618 if (ineq1 > ineq2) {
4619 isl_basic_map_drop_inequality(bmap, ineq1);
4620 isl_basic_map_drop_inequality(bmap, ineq2);
4621 } else {
4622 isl_basic_map_drop_inequality(bmap, ineq2);
4623 isl_basic_map_drop_inequality(bmap, ineq1);
4625 bmap = isl_basic_map_drop_div(bmap, div);
4626 return drop_redundant_divs_again(bmap, pairs, 0);
4629 /* Given two inequality constraints
4631 * f(x) + n d + c >= 0, (ineq)
4633 * with d the variable at position "pos", and
4635 * f(x) + c0 >= 0, (lower)
4637 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4638 * determined by the first constraint.
4639 * That is, store
4641 * ceil((c0 - c)/n)
4643 * in *l.
4645 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4646 int ineq, int lower, int pos, isl_int *l)
4648 isl_int_neg(*l, bmap->ineq[ineq][0]);
4649 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4650 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4653 /* Given two inequality constraints
4655 * f(x) + n d + c >= 0, (ineq)
4657 * with d the variable at position "pos", and
4659 * -f(x) - c0 >= 0, (upper)
4661 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4662 * determined by the first constraint.
4663 * That is, store
4665 * ceil((-c1 - c)/n)
4667 * in *u.
4669 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4670 int ineq, int upper, int pos, isl_int *u)
4672 isl_int_neg(*u, bmap->ineq[ineq][0]);
4673 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4674 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4677 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4678 * does the corresponding lower bound have a fixed value in "bmap"?
4680 * In particular, "ineq" is of the form
4682 * f(x) + n d + c >= 0
4684 * with n > 0, c the constant term and
4685 * d the existentially quantified variable "div".
4686 * That is, the lower bound is
4688 * ceil((-f(x) - c)/n)
4690 * Look for a pair of constraints
4692 * f(x) + c0 >= 0
4693 * -f(x) + c1 >= 0
4695 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4696 * That is, check that
4698 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4700 * If so, return the index of inequality f(x) + c0 >= 0.
4701 * Otherwise, return -1.
4703 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4705 int i;
4706 int lower = -1, upper = -1;
4707 unsigned o_div, n_div;
4708 isl_int l, u;
4709 int equal;
4711 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4712 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4713 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4714 if (i == ineq)
4715 continue;
4716 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4717 continue;
4718 if (lower < 0 &&
4719 is_parallel_except(bmap, ineq, i, o_div + div)) {
4720 lower = i;
4721 continue;
4723 if (upper < 0 &&
4724 is_opposite_except(bmap, ineq, i, o_div + div)) {
4725 upper = i;
4729 if (lower < 0 || upper < 0)
4730 return -1;
4732 isl_int_init(l);
4733 isl_int_init(u);
4735 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4736 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4738 equal = isl_int_eq(l, u);
4740 isl_int_clear(l);
4741 isl_int_clear(u);
4743 return equal ? lower : -1;
4746 /* Given a lower bound constraint "ineq" on the existentially quantified
4747 * variable "div", such that the corresponding lower bound has
4748 * a fixed value in "bmap", assign this fixed value to the variable and
4749 * then try and drop redundant divs again,
4750 * freeing the temporary data structure "pairs" that was associated
4751 * to the old version of "bmap".
4752 * "lower" determines the constant value for the lower bound.
4754 * In particular, "ineq" is of the form
4756 * f(x) + n d + c >= 0,
4758 * while "lower" is of the form
4760 * f(x) + c0 >= 0
4762 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4763 * is ceil((c0 - c)/n).
4765 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4766 int div, int ineq, int lower, int *pairs)
4768 isl_int c;
4769 unsigned o_div;
4771 isl_int_init(c);
4773 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4774 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4775 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4776 free(pairs);
4778 isl_int_clear(c);
4780 return isl_basic_map_drop_redundant_divs(bmap);
4783 /* Remove divs that are not strictly needed based on the inequality
4784 * constraints.
4785 * In particular, if a div only occurs positively (or negatively)
4786 * in constraints, then it can simply be dropped.
4787 * Also, if a div occurs in only two constraints and if moreover
4788 * those two constraints are opposite to each other, except for the constant
4789 * term and if the sum of the constant terms is such that for any value
4790 * of the other values, there is always at least one integer value of the
4791 * div, i.e., if one plus this sum is greater than or equal to
4792 * the (absolute value) of the coefficient of the div in the constraints,
4793 * then we can also simply drop the div.
4795 * If an existentially quantified variable does not have an explicit
4796 * representation, appears in only a single lower bound that does not
4797 * involve any other such existentially quantified variables and appears
4798 * in this lower bound with coefficient 1,
4799 * then fix the variable to the value of the lower bound. That is,
4800 * turn the inequality into an equality.
4801 * If for any value of the other variables, there is any value
4802 * for the existentially quantified variable satisfying the constraints,
4803 * then this lower bound also satisfies the constraints.
4804 * It is therefore safe to pick this lower bound.
4806 * The same reasoning holds even if the coefficient is not one.
4807 * However, fixing the variable to the value of the lower bound may
4808 * in general introduce an extra integer division, in which case
4809 * it may be better to pick another value.
4810 * If this integer division has a known constant value, then plugging
4811 * in this constant value removes the existentially quantified variable
4812 * completely. In particular, if the lower bound is of the form
4813 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4814 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4815 * then the existentially quantified variable can be assigned this
4816 * shared value.
4818 * We skip divs that appear in equalities or in the definition of other divs.
4819 * Divs that appear in the definition of other divs usually occur in at least
4820 * 4 constraints, but the constraints may have been simplified.
4822 * If any divs are left after these simple checks then we move on
4823 * to more complicated cases in drop_more_redundant_divs.
4825 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4826 __isl_take isl_basic_map *bmap)
4828 int i, j;
4829 unsigned off;
4830 int *pairs = NULL;
4831 int n = 0;
4833 if (!bmap)
4834 goto error;
4835 if (bmap->n_div == 0)
4836 return bmap;
4838 off = isl_space_dim(bmap->dim, isl_dim_all);
4839 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4840 if (!pairs)
4841 goto error;
4843 for (i = 0; i < bmap->n_div; ++i) {
4844 int pos, neg;
4845 int last_pos, last_neg;
4846 int redundant;
4847 int defined;
4849 defined = !isl_int_is_zero(bmap->div[i][0]);
4850 for (j = i; j < bmap->n_div; ++j)
4851 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4852 break;
4853 if (j < bmap->n_div)
4854 continue;
4855 for (j = 0; j < bmap->n_eq; ++j)
4856 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4857 break;
4858 if (j < bmap->n_eq)
4859 continue;
4860 ++n;
4861 pos = neg = 0;
4862 for (j = 0; j < bmap->n_ineq; ++j) {
4863 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4864 last_pos = j;
4865 ++pos;
4867 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4868 last_neg = j;
4869 ++neg;
4872 pairs[i] = pos * neg;
4873 if (pairs[i] == 0) {
4874 for (j = bmap->n_ineq - 1; j >= 0; --j)
4875 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4876 isl_basic_map_drop_inequality(bmap, j);
4877 bmap = isl_basic_map_drop_div(bmap, i);
4878 return drop_redundant_divs_again(bmap, pairs, 0);
4880 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4881 int single, lower;
4882 if (pos != 1)
4883 continue;
4884 single = single_unknown(bmap, last_pos, i);
4885 if (!single)
4886 continue;
4887 if (has_coef_one(bmap, i, last_pos))
4888 return set_eq_and_try_again(bmap, last_pos,
4889 pairs);
4890 lower = lower_bound_is_cst(bmap, i, last_pos);
4891 if (lower >= 0)
4892 return fix_cst_lower(bmap, i, last_pos, lower,
4893 pairs);
4894 continue;
4897 isl_int_add(bmap->ineq[last_pos][0],
4898 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4899 isl_int_add_ui(bmap->ineq[last_pos][0],
4900 bmap->ineq[last_pos][0], 1);
4901 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4902 bmap->ineq[last_pos][1+off+i]);
4903 isl_int_sub_ui(bmap->ineq[last_pos][0],
4904 bmap->ineq[last_pos][0], 1);
4905 isl_int_sub(bmap->ineq[last_pos][0],
4906 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4907 if (redundant)
4908 return drop_div_and_try_again(bmap, i,
4909 last_pos, last_neg, pairs);
4910 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
4911 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4912 return drop_redundant_divs_again(bmap, pairs, 1);
4914 pairs[i] = 0;
4915 --n;
4918 if (n > 0)
4919 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4921 free(pairs);
4922 return bmap;
4923 error:
4924 free(pairs);
4925 isl_basic_map_free(bmap);
4926 return NULL;
4929 /* Consider the coefficients at "c" as a row vector and replace
4930 * them with their product with "T". "T" is assumed to be a square matrix.
4932 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4934 int n;
4935 isl_ctx *ctx;
4936 isl_vec *v;
4938 if (!T)
4939 return isl_stat_error;
4940 n = isl_mat_rows(T);
4941 if (isl_seq_first_non_zero(c, n) == -1)
4942 return isl_stat_ok;
4943 ctx = isl_mat_get_ctx(T);
4944 v = isl_vec_alloc(ctx, n);
4945 if (!v)
4946 return isl_stat_error;
4947 isl_seq_swp_or_cpy(v->el, c, n);
4948 v = isl_vec_mat_product(v, isl_mat_copy(T));
4949 if (!v)
4950 return isl_stat_error;
4951 isl_seq_swp_or_cpy(c, v->el, n);
4952 isl_vec_free(v);
4954 return isl_stat_ok;
4957 /* Plug in T for the variables in "bmap" starting at "pos".
4958 * T is a linear unimodular matrix, i.e., without constant term.
4960 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4961 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4963 int i;
4964 unsigned n, total;
4966 bmap = isl_basic_map_cow(bmap);
4967 if (!bmap || !T)
4968 goto error;
4970 n = isl_mat_cols(T);
4971 if (n != isl_mat_rows(T))
4972 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4973 "expecting square matrix", goto error);
4975 total = isl_basic_map_dim(bmap, isl_dim_all);
4976 if (pos + n > total || pos + n < pos)
4977 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4978 "invalid range", goto error);
4980 for (i = 0; i < bmap->n_eq; ++i)
4981 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4982 goto error;
4983 for (i = 0; i < bmap->n_ineq; ++i)
4984 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4985 goto error;
4986 for (i = 0; i < bmap->n_div; ++i) {
4987 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4988 continue;
4989 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4990 goto error;
4993 isl_mat_free(T);
4994 return bmap;
4995 error:
4996 isl_basic_map_free(bmap);
4997 isl_mat_free(T);
4998 return NULL;
5001 /* Remove divs that are not strictly needed.
5003 * First look for an equality constraint involving two or more
5004 * existentially quantified variables without an explicit
5005 * representation. Replace the combination that appears
5006 * in the equality constraint by a single existentially quantified
5007 * variable such that the equality can be used to derive
5008 * an explicit representation for the variable.
5009 * If there are no more such equality constraints, then continue
5010 * with isl_basic_map_drop_redundant_divs_ineq.
5012 * In particular, if the equality constraint is of the form
5014 * f(x) + \sum_i c_i a_i = 0
5016 * with a_i existentially quantified variable without explicit
5017 * representation, then apply a transformation on the existentially
5018 * quantified variables to turn the constraint into
5020 * f(x) + g a_1' = 0
5022 * with g the gcd of the c_i.
5023 * In order to easily identify which existentially quantified variables
5024 * have a complete explicit representation, i.e., without being defined
5025 * in terms of other existentially quantified variables without
5026 * an explicit representation, the existentially quantified variables
5027 * are first sorted.
5029 * The variable transformation is computed by extending the row
5030 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5032 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5033 * [a_2'] [ a_2 ]
5034 * ... = U ....
5035 * [a_n'] [ a_n ]
5037 * with [c_1/g ... c_n/g] representing the first row of U.
5038 * The inverse of U is then plugged into the original constraints.
5039 * The call to isl_basic_map_simplify makes sure the explicit
5040 * representation for a_1' is extracted from the equality constraint.
5042 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5043 __isl_take isl_basic_map *bmap)
5045 int first;
5046 int i;
5047 unsigned o_div, n_div;
5048 int l;
5049 isl_ctx *ctx;
5050 isl_mat *T;
5052 if (!bmap)
5053 return NULL;
5054 if (isl_basic_map_divs_known(bmap))
5055 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5056 if (bmap->n_eq == 0)
5057 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5058 bmap = isl_basic_map_sort_divs(bmap);
5059 if (!bmap)
5060 return NULL;
5062 first = isl_basic_map_first_unknown_div(bmap);
5063 if (first < 0)
5064 return isl_basic_map_free(bmap);
5066 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5067 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5069 for (i = 0; i < bmap->n_eq; ++i) {
5070 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5071 n_div - (first));
5072 if (l < 0)
5073 continue;
5074 l += first;
5075 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5076 n_div - (l + 1)) == -1)
5077 continue;
5078 break;
5080 if (i >= bmap->n_eq)
5081 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5083 ctx = isl_basic_map_get_ctx(bmap);
5084 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5085 if (!T)
5086 return isl_basic_map_free(bmap);
5087 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5088 T = isl_mat_normalize_row(T, 0);
5089 T = isl_mat_unimodular_complete(T, 1);
5090 T = isl_mat_right_inverse(T);
5092 for (i = l; i < n_div; ++i)
5093 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5094 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5095 bmap = isl_basic_map_simplify(bmap);
5097 return isl_basic_map_drop_redundant_divs(bmap);
5100 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5101 struct isl_basic_set *bset)
5103 isl_basic_map *bmap = bset_to_bmap(bset);
5104 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5107 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5109 int i;
5111 if (!map)
5112 return NULL;
5113 for (i = 0; i < map->n; ++i) {
5114 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5115 if (!map->p[i])
5116 goto error;
5118 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5119 return map;
5120 error:
5121 isl_map_free(map);
5122 return NULL;
5125 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5127 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5130 /* Does "bmap" satisfy any equality that involves more than 2 variables
5131 * and/or has coefficients different from -1 and 1?
5133 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5135 int i;
5136 unsigned total;
5138 total = isl_basic_map_dim(bmap, isl_dim_all);
5140 for (i = 0; i < bmap->n_eq; ++i) {
5141 int j, k;
5143 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5144 if (j < 0)
5145 continue;
5146 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5147 !isl_int_is_negone(bmap->eq[i][1 + j]))
5148 return 1;
5150 j += 1;
5151 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5152 if (k < 0)
5153 continue;
5154 j += k;
5155 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5156 !isl_int_is_negone(bmap->eq[i][1 + j]))
5157 return 1;
5159 j += 1;
5160 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5161 if (k >= 0)
5162 return 1;
5165 return 0;
5168 /* Remove any common factor g from the constraint coefficients in "v".
5169 * The constant term is stored in the first position and is replaced
5170 * by floor(c/g). If any common factor is removed and if this results
5171 * in a tightening of the constraint, then set *tightened.
5173 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5174 int *tightened)
5176 isl_ctx *ctx;
5178 if (!v)
5179 return NULL;
5180 ctx = isl_vec_get_ctx(v);
5181 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5182 if (isl_int_is_zero(ctx->normalize_gcd))
5183 return v;
5184 if (isl_int_is_one(ctx->normalize_gcd))
5185 return v;
5186 v = isl_vec_cow(v);
5187 if (!v)
5188 return NULL;
5189 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5190 *tightened = 1;
5191 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5192 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5193 v->size - 1);
5194 return v;
5197 /* If "bmap" is an integer set that satisfies any equality involving
5198 * more than 2 variables and/or has coefficients different from -1 and 1,
5199 * then use variable compression to reduce the coefficients by removing
5200 * any (hidden) common factor.
5201 * In particular, apply the variable compression to each constraint,
5202 * factor out any common factor in the non-constant coefficients and
5203 * then apply the inverse of the compression.
5204 * At the end, we mark the basic map as having reduced constants.
5205 * If this flag is still set on the next invocation of this function,
5206 * then we skip the computation.
5208 * Removing a common factor may result in a tightening of some of
5209 * the constraints. If this happens, then we may end up with two
5210 * opposite inequalities that can be replaced by an equality.
5211 * We therefore call isl_basic_map_detect_inequality_pairs,
5212 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5213 * and isl_basic_map_gauss if such a pair was found.
5215 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5216 __isl_take isl_basic_map *bmap)
5218 unsigned total;
5219 isl_ctx *ctx;
5220 isl_vec *v;
5221 isl_mat *eq, *T, *T2;
5222 int i;
5223 int tightened;
5225 if (!bmap)
5226 return NULL;
5227 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5228 return bmap;
5229 if (isl_basic_map_is_rational(bmap))
5230 return bmap;
5231 if (bmap->n_eq == 0)
5232 return bmap;
5233 if (!has_multiple_var_equality(bmap))
5234 return bmap;
5236 total = isl_basic_map_dim(bmap, isl_dim_all);
5237 ctx = isl_basic_map_get_ctx(bmap);
5238 v = isl_vec_alloc(ctx, 1 + total);
5239 if (!v)
5240 return isl_basic_map_free(bmap);
5242 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5243 T = isl_mat_variable_compression(eq, &T2);
5244 if (!T || !T2)
5245 goto error;
5246 if (T->n_col == 0) {
5247 isl_mat_free(T);
5248 isl_mat_free(T2);
5249 isl_vec_free(v);
5250 return isl_basic_map_set_to_empty(bmap);
5253 tightened = 0;
5254 for (i = 0; i < bmap->n_ineq; ++i) {
5255 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5256 v = isl_vec_mat_product(v, isl_mat_copy(T));
5257 v = normalize_constraint(v, &tightened);
5258 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5259 if (!v)
5260 goto error;
5261 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5264 isl_mat_free(T);
5265 isl_mat_free(T2);
5266 isl_vec_free(v);
5268 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5270 if (tightened) {
5271 int progress = 0;
5273 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5274 if (progress) {
5275 bmap = eliminate_divs_eq(bmap, &progress);
5276 bmap = isl_basic_map_gauss(bmap, NULL);
5280 return bmap;
5281 error:
5282 isl_mat_free(T);
5283 isl_mat_free(T2);
5284 isl_vec_free(v);
5285 return isl_basic_map_free(bmap);
5288 /* Shift the integer division at position "div" of "bmap"
5289 * by "shift" times the variable at position "pos".
5290 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5291 * corresponds to the constant term.
5293 * That is, if the integer division has the form
5295 * floor(f(x)/d)
5297 * then replace it by
5299 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5301 __isl_give isl_basic_map *isl_basic_map_shift_div(
5302 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5304 int i;
5305 unsigned total;
5307 if (!bmap)
5308 return NULL;
5310 total = isl_basic_map_dim(bmap, isl_dim_all);
5311 total -= isl_basic_map_dim(bmap, isl_dim_div);
5313 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5315 for (i = 0; i < bmap->n_eq; ++i) {
5316 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5317 continue;
5318 isl_int_submul(bmap->eq[i][pos],
5319 shift, bmap->eq[i][1 + total + div]);
5321 for (i = 0; i < bmap->n_ineq; ++i) {
5322 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5323 continue;
5324 isl_int_submul(bmap->ineq[i][pos],
5325 shift, bmap->ineq[i][1 + total + div]);
5327 for (i = 0; i < bmap->n_div; ++i) {
5328 if (isl_int_is_zero(bmap->div[i][0]))
5329 continue;
5330 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5331 continue;
5332 isl_int_submul(bmap->div[i][1 + pos],
5333 shift, bmap->div[i][1 + 1 + total + div]);
5336 return bmap;