isl_basic_map_gauss: extract out set_div_from_eq
[isl.git] / isl_map_simplify.c
blob6056c1a949461f36224a4e02a744a392747f22af
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 /* The last local variable involved in the equality constraint
692 * at position "eq" in "bmap" is the local variable at position "div".
693 * It can therefore be used to extract an explicit representation
694 * for that variable.
695 * Do so unless the local variable already has an explicit representation.
696 * Set *progress if anything is changed.
698 * The equality constraint is of the form
700 * f(x) + n e >= 0
702 * with n a positive number. The explicit representation derived from
703 * this constraint is
705 * floor((-f(x))/n)
707 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
708 int div, int eq, int *progress)
710 unsigned total, o_div;
712 if (!bmap)
713 return NULL;
715 if (!isl_int_is_zero(bmap->div[div][0]))
716 return bmap;
718 total = isl_basic_map_dim(bmap, isl_dim_all);
719 o_div = isl_basic_map_offset(bmap, isl_dim_div);
720 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
721 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
722 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
723 if (progress)
724 *progress = 1;
725 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
727 return bmap;
730 struct isl_basic_map *isl_basic_map_gauss(
731 struct isl_basic_map *bmap, int *progress)
733 int k;
734 int done;
735 int last_var;
736 unsigned total_var;
737 unsigned total;
739 bmap = isl_basic_map_order_divs(bmap);
741 if (!bmap)
742 return NULL;
744 total = isl_basic_map_total_dim(bmap);
745 total_var = total - bmap->n_div;
747 last_var = total - 1;
748 for (done = 0; done < bmap->n_eq; ++done) {
749 for (; last_var >= 0; --last_var) {
750 for (k = done; k < bmap->n_eq; ++k)
751 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
752 break;
753 if (k < bmap->n_eq)
754 break;
756 if (last_var < 0)
757 break;
758 if (k != done)
759 swap_equality(bmap, k, done);
760 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
761 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
763 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
764 progress);
766 if (last_var >= total_var)
767 bmap = set_div_from_eq(bmap, last_var - total_var,
768 done, progress);
769 if (!bmap)
770 return NULL;
772 if (done == bmap->n_eq)
773 return bmap;
774 for (k = done; k < bmap->n_eq; ++k) {
775 if (isl_int_is_zero(bmap->eq[k][0]))
776 continue;
777 return isl_basic_map_set_to_empty(bmap);
779 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
780 return bmap;
783 struct isl_basic_set *isl_basic_set_gauss(
784 struct isl_basic_set *bset, int *progress)
786 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
787 progress));
791 static unsigned int round_up(unsigned int v)
793 int old_v = v;
795 while (v) {
796 old_v = v;
797 v ^= v & -v;
799 return old_v << 1;
802 /* Hash table of inequalities in a basic map.
803 * "index" is an array of addresses of inequalities in the basic map, some
804 * of which are NULL. The inequalities are hashed on the coefficients
805 * except the constant term.
806 * "size" is the number of elements in the array and is always a power of two
807 * "bits" is the number of bits need to represent an index into the array.
808 * "total" is the total dimension of the basic map.
810 struct isl_constraint_index {
811 unsigned int size;
812 int bits;
813 isl_int ***index;
814 unsigned total;
817 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
819 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
820 __isl_keep isl_basic_map *bmap)
822 isl_ctx *ctx;
824 ci->index = NULL;
825 if (!bmap)
826 return isl_stat_error;
827 ci->total = isl_basic_set_total_dim(bmap);
828 if (bmap->n_ineq == 0)
829 return isl_stat_ok;
830 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
831 ci->bits = ffs(ci->size) - 1;
832 ctx = isl_basic_map_get_ctx(bmap);
833 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
834 if (!ci->index)
835 return isl_stat_error;
837 return isl_stat_ok;
840 /* Free the memory allocated by create_constraint_index.
842 static void constraint_index_free(struct isl_constraint_index *ci)
844 free(ci->index);
847 /* Return the position in ci->index that contains the address of
848 * an inequality that is equal to *ineq up to the constant term,
849 * provided this address is not identical to "ineq".
850 * If there is no such inequality, then return the position where
851 * such an inequality should be inserted.
853 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
855 int h;
856 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
857 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
858 if (ineq != ci->index[h] &&
859 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
860 break;
861 return h;
864 /* Return the position in ci->index that contains the address of
865 * an inequality that is equal to the k'th inequality of "bmap"
866 * up to the constant term, provided it does not point to the very
867 * same inequality.
868 * If there is no such inequality, then return the position where
869 * such an inequality should be inserted.
871 static int hash_index(struct isl_constraint_index *ci,
872 __isl_keep isl_basic_map *bmap, int k)
874 return hash_index_ineq(ci, &bmap->ineq[k]);
877 static int set_hash_index(struct isl_constraint_index *ci,
878 struct isl_basic_set *bset, int k)
880 return hash_index(ci, bset, k);
883 /* Fill in the "ci" data structure with the inequalities of "bset".
885 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
886 __isl_keep isl_basic_set *bset)
888 int k, h;
890 if (create_constraint_index(ci, bset) < 0)
891 return isl_stat_error;
893 for (k = 0; k < bset->n_ineq; ++k) {
894 h = set_hash_index(ci, bset, k);
895 ci->index[h] = &bset->ineq[k];
898 return isl_stat_ok;
901 /* Is the inequality ineq (obviously) redundant with respect
902 * to the constraints in "ci"?
904 * Look for an inequality in "ci" with the same coefficients and then
905 * check if the contant term of "ineq" is greater than or equal
906 * to the constant term of that inequality. If so, "ineq" is clearly
907 * redundant.
909 * Note that hash_index_ineq ignores a stored constraint if it has
910 * the same address as the passed inequality. It is ok to pass
911 * the address of a local variable here since it will never be
912 * the same as the address of a constraint in "ci".
914 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
915 isl_int *ineq)
917 int h;
919 h = hash_index_ineq(ci, &ineq);
920 if (!ci->index[h])
921 return isl_bool_false;
922 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
925 /* If we can eliminate more than one div, then we need to make
926 * sure we do it from last div to first div, in order not to
927 * change the position of the other divs that still need to
928 * be removed.
930 static struct isl_basic_map *remove_duplicate_divs(
931 struct isl_basic_map *bmap, int *progress)
933 unsigned int size;
934 int *index;
935 int *elim_for;
936 int k, l, h;
937 int bits;
938 struct isl_blk eq;
939 unsigned total_var;
940 unsigned total;
941 struct isl_ctx *ctx;
943 bmap = isl_basic_map_order_divs(bmap);
944 if (!bmap || bmap->n_div <= 1)
945 return bmap;
947 total_var = isl_space_dim(bmap->dim, isl_dim_all);
948 total = total_var + bmap->n_div;
950 ctx = bmap->ctx;
951 for (k = bmap->n_div - 1; k >= 0; --k)
952 if (!isl_int_is_zero(bmap->div[k][0]))
953 break;
954 if (k <= 0)
955 return bmap;
957 size = round_up(4 * bmap->n_div / 3 - 1);
958 if (size == 0)
959 return bmap;
960 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
961 bits = ffs(size) - 1;
962 index = isl_calloc_array(ctx, int, size);
963 if (!elim_for || !index)
964 goto out;
965 eq = isl_blk_alloc(ctx, 1+total);
966 if (isl_blk_is_error(eq))
967 goto out;
969 isl_seq_clr(eq.data, 1+total);
970 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
971 for (--k; k >= 0; --k) {
972 uint32_t hash;
974 if (isl_int_is_zero(bmap->div[k][0]))
975 continue;
977 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
978 for (h = hash; index[h]; h = (h+1) % size)
979 if (isl_seq_eq(bmap->div[k],
980 bmap->div[index[h]-1], 2+total))
981 break;
982 if (index[h]) {
983 *progress = 1;
984 l = index[h] - 1;
985 elim_for[l] = k + 1;
987 index[h] = k+1;
989 for (l = bmap->n_div - 1; l >= 0; --l) {
990 if (!elim_for[l])
991 continue;
992 k = elim_for[l] - 1;
993 isl_int_set_si(eq.data[1+total_var+k], -1);
994 isl_int_set_si(eq.data[1+total_var+l], 1);
995 bmap = eliminate_div(bmap, eq.data, l, 1);
996 if (!bmap)
997 break;
998 isl_int_set_si(eq.data[1+total_var+k], 0);
999 isl_int_set_si(eq.data[1+total_var+l], 0);
1002 isl_blk_free(ctx, eq);
1003 out:
1004 free(index);
1005 free(elim_for);
1006 return bmap;
1009 static int n_pure_div_eq(struct isl_basic_map *bmap)
1011 int i, j;
1012 unsigned total;
1014 total = isl_space_dim(bmap->dim, isl_dim_all);
1015 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1016 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1017 --j;
1018 if (j < 0)
1019 break;
1020 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
1021 return 0;
1023 return i;
1026 /* Normalize divs that appear in equalities.
1028 * In particular, we assume that bmap contains some equalities
1029 * of the form
1031 * a x = m * e_i
1033 * and we want to replace the set of e_i by a minimal set and
1034 * such that the new e_i have a canonical representation in terms
1035 * of the vector x.
1036 * If any of the equalities involves more than one divs, then
1037 * we currently simply bail out.
1039 * Let us first additionally assume that all equalities involve
1040 * a div. The equalities then express modulo constraints on the
1041 * remaining variables and we can use "parameter compression"
1042 * to find a minimal set of constraints. The result is a transformation
1044 * x = T(x') = x_0 + G x'
1046 * with G a lower-triangular matrix with all elements below the diagonal
1047 * non-negative and smaller than the diagonal element on the same row.
1048 * We first normalize x_0 by making the same property hold in the affine
1049 * T matrix.
1050 * The rows i of G with a 1 on the diagonal do not impose any modulo
1051 * constraint and simply express x_i = x'_i.
1052 * For each of the remaining rows i, we introduce a div and a corresponding
1053 * equality. In particular
1055 * g_ii e_j = x_i - g_i(x')
1057 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1058 * corresponding div (if g_kk != 1).
1060 * If there are any equalities not involving any div, then we
1061 * first apply a variable compression on the variables x:
1063 * x = C x'' x'' = C_2 x
1065 * and perform the above parameter compression on A C instead of on A.
1066 * The resulting compression is then of the form
1068 * x'' = T(x') = x_0 + G x'
1070 * and in constructing the new divs and the corresponding equalities,
1071 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1072 * by the corresponding row from C_2.
1074 static struct isl_basic_map *normalize_divs(
1075 struct isl_basic_map *bmap, int *progress)
1077 int i, j, k;
1078 int total;
1079 int div_eq;
1080 struct isl_mat *B;
1081 struct isl_vec *d;
1082 struct isl_mat *T = NULL;
1083 struct isl_mat *C = NULL;
1084 struct isl_mat *C2 = NULL;
1085 isl_int v;
1086 int *pos;
1087 int dropped, needed;
1089 if (!bmap)
1090 return NULL;
1092 if (bmap->n_div == 0)
1093 return bmap;
1095 if (bmap->n_eq == 0)
1096 return bmap;
1098 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
1099 return bmap;
1101 total = isl_space_dim(bmap->dim, isl_dim_all);
1102 div_eq = n_pure_div_eq(bmap);
1103 if (div_eq == 0)
1104 return bmap;
1106 if (div_eq < bmap->n_eq) {
1107 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1108 bmap->n_eq - div_eq, 0, 1 + total);
1109 C = isl_mat_variable_compression(B, &C2);
1110 if (!C || !C2)
1111 goto error;
1112 if (C->n_col == 0) {
1113 bmap = isl_basic_map_set_to_empty(bmap);
1114 isl_mat_free(C);
1115 isl_mat_free(C2);
1116 goto done;
1120 d = isl_vec_alloc(bmap->ctx, div_eq);
1121 if (!d)
1122 goto error;
1123 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1124 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
1125 --j;
1126 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
1128 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
1130 if (C) {
1131 B = isl_mat_product(B, C);
1132 C = NULL;
1135 T = isl_mat_parameter_compression(B, d);
1136 if (!T)
1137 goto error;
1138 if (T->n_col == 0) {
1139 bmap = isl_basic_map_set_to_empty(bmap);
1140 isl_mat_free(C2);
1141 isl_mat_free(T);
1142 goto done;
1144 isl_int_init(v);
1145 for (i = 0; i < T->n_row - 1; ++i) {
1146 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1147 if (isl_int_is_zero(v))
1148 continue;
1149 isl_mat_col_submul(T, 0, v, 1 + i);
1151 isl_int_clear(v);
1152 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1153 if (!pos)
1154 goto error;
1155 /* We have to be careful because dropping equalities may reorder them */
1156 dropped = 0;
1157 for (j = bmap->n_div - 1; j >= 0; --j) {
1158 for (i = 0; i < bmap->n_eq; ++i)
1159 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
1160 break;
1161 if (i < bmap->n_eq) {
1162 bmap = isl_basic_map_drop_div(bmap, j);
1163 isl_basic_map_drop_equality(bmap, i);
1164 ++dropped;
1167 pos[0] = 0;
1168 needed = 0;
1169 for (i = 1; i < T->n_row; ++i) {
1170 if (isl_int_is_one(T->row[i][i]))
1171 pos[i] = i;
1172 else
1173 needed++;
1175 if (needed > dropped) {
1176 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1177 needed, needed, 0);
1178 if (!bmap)
1179 goto error;
1181 for (i = 1; i < T->n_row; ++i) {
1182 if (isl_int_is_one(T->row[i][i]))
1183 continue;
1184 k = isl_basic_map_alloc_div(bmap);
1185 pos[i] = 1 + total + k;
1186 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1187 isl_int_set(bmap->div[k][0], T->row[i][i]);
1188 if (C2)
1189 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1190 else
1191 isl_int_set_si(bmap->div[k][1 + i], 1);
1192 for (j = 0; j < i; ++j) {
1193 if (isl_int_is_zero(T->row[i][j]))
1194 continue;
1195 if (pos[j] < T->n_row && C2)
1196 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1197 C2->row[pos[j]], 1 + total);
1198 else
1199 isl_int_neg(bmap->div[k][1 + pos[j]],
1200 T->row[i][j]);
1202 j = isl_basic_map_alloc_equality(bmap);
1203 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1204 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1206 free(pos);
1207 isl_mat_free(C2);
1208 isl_mat_free(T);
1210 if (progress)
1211 *progress = 1;
1212 done:
1213 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1215 return bmap;
1216 error:
1217 isl_mat_free(C);
1218 isl_mat_free(C2);
1219 isl_mat_free(T);
1220 return bmap;
1223 static struct isl_basic_map *set_div_from_lower_bound(
1224 struct isl_basic_map *bmap, int div, int ineq)
1226 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1228 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1229 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1230 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1231 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1232 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1234 return bmap;
1237 /* Check whether it is ok to define a div based on an inequality.
1238 * To avoid the introduction of circular definitions of divs, we
1239 * do not allow such a definition if the resulting expression would refer to
1240 * any other undefined divs or if any known div is defined in
1241 * terms of the unknown div.
1243 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
1244 int div, int ineq)
1246 int j;
1247 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1249 /* Not defined in terms of unknown divs */
1250 for (j = 0; j < bmap->n_div; ++j) {
1251 if (div == j)
1252 continue;
1253 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1254 continue;
1255 if (isl_int_is_zero(bmap->div[j][0]))
1256 return 0;
1259 /* No other div defined in terms of this one => avoid loops */
1260 for (j = 0; j < bmap->n_div; ++j) {
1261 if (div == j)
1262 continue;
1263 if (isl_int_is_zero(bmap->div[j][0]))
1264 continue;
1265 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1266 return 0;
1269 return 1;
1272 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1273 * be a better expression than the current one?
1275 * If we do not have any expression yet, then any expression would be better.
1276 * Otherwise we check if the last variable involved in the inequality
1277 * (disregarding the div that it would define) is in an earlier position
1278 * than the last variable involved in the current div expression.
1280 static int better_div_constraint(__isl_keep isl_basic_map *bmap,
1281 int div, int ineq)
1283 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1284 int last_div;
1285 int last_ineq;
1287 if (isl_int_is_zero(bmap->div[div][0]))
1288 return 1;
1290 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1291 bmap->n_div - (div + 1)) >= 0)
1292 return 0;
1294 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1295 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1296 total + bmap->n_div);
1298 return last_ineq < last_div;
1301 /* Given two constraints "k" and "l" that are opposite to each other,
1302 * except for the constant term, check if we can use them
1303 * to obtain an expression for one of the hitherto unknown divs or
1304 * a "better" expression for a div for which we already have an expression.
1305 * "sum" is the sum of the constant terms of the constraints.
1306 * If this sum is strictly smaller than the coefficient of one
1307 * of the divs, then this pair can be used define the div.
1308 * To avoid the introduction of circular definitions of divs, we
1309 * do not use the pair if the resulting expression would refer to
1310 * any other undefined divs or if any known div is defined in
1311 * terms of the unknown div.
1313 static struct isl_basic_map *check_for_div_constraints(
1314 struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
1316 int i;
1317 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1319 for (i = 0; i < bmap->n_div; ++i) {
1320 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1321 continue;
1322 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1323 continue;
1324 if (!better_div_constraint(bmap, i, k))
1325 continue;
1326 if (!ok_to_set_div_from_bound(bmap, i, k))
1327 break;
1328 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1329 bmap = set_div_from_lower_bound(bmap, i, k);
1330 else
1331 bmap = set_div_from_lower_bound(bmap, i, l);
1332 if (progress)
1333 *progress = 1;
1334 break;
1336 return bmap;
1339 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1340 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1342 struct isl_constraint_index ci;
1343 int k, l, h;
1344 unsigned total = isl_basic_map_total_dim(bmap);
1345 isl_int sum;
1347 if (!bmap || bmap->n_ineq <= 1)
1348 return bmap;
1350 if (create_constraint_index(&ci, bmap) < 0)
1351 return bmap;
1353 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1354 ci.index[h] = &bmap->ineq[0];
1355 for (k = 1; k < bmap->n_ineq; ++k) {
1356 h = hash_index(&ci, bmap, k);
1357 if (!ci.index[h]) {
1358 ci.index[h] = &bmap->ineq[k];
1359 continue;
1361 if (progress)
1362 *progress = 1;
1363 l = ci.index[h] - &bmap->ineq[0];
1364 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1365 swap_inequality(bmap, k, l);
1366 isl_basic_map_drop_inequality(bmap, k);
1367 --k;
1369 isl_int_init(sum);
1370 for (k = 0; k < bmap->n_ineq-1; ++k) {
1371 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1372 h = hash_index(&ci, bmap, k);
1373 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1374 if (!ci.index[h])
1375 continue;
1376 l = ci.index[h] - &bmap->ineq[0];
1377 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1378 if (isl_int_is_pos(sum)) {
1379 if (detect_divs)
1380 bmap = check_for_div_constraints(bmap, k, l,
1381 sum, progress);
1382 continue;
1384 if (isl_int_is_zero(sum)) {
1385 /* We need to break out of the loop after these
1386 * changes since the contents of the hash
1387 * will no longer be valid.
1388 * Plus, we probably we want to regauss first.
1390 if (progress)
1391 *progress = 1;
1392 isl_basic_map_drop_inequality(bmap, l);
1393 isl_basic_map_inequality_to_equality(bmap, k);
1394 } else
1395 bmap = isl_basic_map_set_to_empty(bmap);
1396 break;
1398 isl_int_clear(sum);
1400 constraint_index_free(&ci);
1401 return bmap;
1404 /* Detect all pairs of inequalities that form an equality.
1406 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1407 * Call it repeatedly while it is making progress.
1409 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1410 __isl_take isl_basic_map *bmap, int *progress)
1412 int duplicate;
1414 do {
1415 duplicate = 0;
1416 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1417 &duplicate, 0);
1418 if (progress && duplicate)
1419 *progress = 1;
1420 } while (duplicate);
1422 return bmap;
1425 /* Eliminate knowns divs from constraints where they appear with
1426 * a (positive or negative) unit coefficient.
1428 * That is, replace
1430 * floor(e/m) + f >= 0
1432 * by
1434 * e + m f >= 0
1436 * and
1438 * -floor(e/m) + f >= 0
1440 * by
1442 * -e + m f + m - 1 >= 0
1444 * The first conversion is valid because floor(e/m) >= -f is equivalent
1445 * to e/m >= -f because -f is an integral expression.
1446 * The second conversion follows from the fact that
1448 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1451 * Note that one of the div constraints may have been eliminated
1452 * due to being redundant with respect to the constraint that is
1453 * being modified by this function. The modified constraint may
1454 * no longer imply this div constraint, so we add it back to make
1455 * sure we do not lose any information.
1457 * We skip integral divs, i.e., those with denominator 1, as we would
1458 * risk eliminating the div from the div constraints. We do not need
1459 * to handle those divs here anyway since the div constraints will turn
1460 * out to form an equality and this equality can then be used to eliminate
1461 * the div from all constraints.
1463 static __isl_give isl_basic_map *eliminate_unit_divs(
1464 __isl_take isl_basic_map *bmap, int *progress)
1466 int i, j;
1467 isl_ctx *ctx;
1468 unsigned total;
1470 if (!bmap)
1471 return NULL;
1473 ctx = isl_basic_map_get_ctx(bmap);
1474 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1476 for (i = 0; i < bmap->n_div; ++i) {
1477 if (isl_int_is_zero(bmap->div[i][0]))
1478 continue;
1479 if (isl_int_is_one(bmap->div[i][0]))
1480 continue;
1481 for (j = 0; j < bmap->n_ineq; ++j) {
1482 int s;
1484 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1485 !isl_int_is_negone(bmap->ineq[j][total + i]))
1486 continue;
1488 *progress = 1;
1490 s = isl_int_sgn(bmap->ineq[j][total + i]);
1491 isl_int_set_si(bmap->ineq[j][total + i], 0);
1492 if (s < 0)
1493 isl_seq_combine(bmap->ineq[j],
1494 ctx->negone, bmap->div[i] + 1,
1495 bmap->div[i][0], bmap->ineq[j],
1496 total + bmap->n_div);
1497 else
1498 isl_seq_combine(bmap->ineq[j],
1499 ctx->one, bmap->div[i] + 1,
1500 bmap->div[i][0], bmap->ineq[j],
1501 total + bmap->n_div);
1502 if (s < 0) {
1503 isl_int_add(bmap->ineq[j][0],
1504 bmap->ineq[j][0], bmap->div[i][0]);
1505 isl_int_sub_ui(bmap->ineq[j][0],
1506 bmap->ineq[j][0], 1);
1509 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1510 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1511 return isl_basic_map_free(bmap);
1515 return bmap;
1518 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1520 int progress = 1;
1521 if (!bmap)
1522 return NULL;
1523 while (progress) {
1524 progress = 0;
1525 if (!bmap)
1526 break;
1527 if (isl_basic_map_plain_is_empty(bmap))
1528 break;
1529 bmap = isl_basic_map_normalize_constraints(bmap);
1530 bmap = remove_independent_vars_from_divs(bmap);
1531 bmap = normalize_div_expressions(bmap);
1532 bmap = remove_duplicate_divs(bmap, &progress);
1533 bmap = eliminate_unit_divs(bmap, &progress);
1534 bmap = eliminate_divs_eq(bmap, &progress);
1535 bmap = eliminate_divs_ineq(bmap, &progress);
1536 bmap = isl_basic_map_gauss(bmap, &progress);
1537 /* requires equalities in normal form */
1538 bmap = normalize_divs(bmap, &progress);
1539 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1540 &progress, 1);
1541 if (bmap && progress)
1542 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1544 return bmap;
1547 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1549 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1553 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1554 isl_int *constraint, unsigned div)
1556 unsigned pos;
1558 if (!bmap)
1559 return -1;
1561 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1563 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1564 int neg;
1565 isl_int_sub(bmap->div[div][1],
1566 bmap->div[div][1], bmap->div[div][0]);
1567 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1568 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1569 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1570 isl_int_add(bmap->div[div][1],
1571 bmap->div[div][1], bmap->div[div][0]);
1572 if (!neg)
1573 return 0;
1574 if (isl_seq_first_non_zero(constraint+pos+1,
1575 bmap->n_div-div-1) != -1)
1576 return 0;
1577 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1578 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1579 return 0;
1580 if (isl_seq_first_non_zero(constraint+pos+1,
1581 bmap->n_div-div-1) != -1)
1582 return 0;
1583 } else
1584 return 0;
1586 return 1;
1589 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1590 isl_int *constraint, unsigned div)
1592 return isl_basic_map_is_div_constraint(bset, constraint, div);
1596 /* If the only constraints a div d=floor(f/m)
1597 * appears in are its two defining constraints
1599 * f - m d >=0
1600 * -(f - (m - 1)) + m d >= 0
1602 * then it can safely be removed.
1604 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1606 int i;
1607 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1609 for (i = 0; i < bmap->n_eq; ++i)
1610 if (!isl_int_is_zero(bmap->eq[i][pos]))
1611 return 0;
1613 for (i = 0; i < bmap->n_ineq; ++i) {
1614 if (isl_int_is_zero(bmap->ineq[i][pos]))
1615 continue;
1616 if (!isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div))
1617 return 0;
1620 for (i = 0; i < bmap->n_div; ++i) {
1621 if (isl_int_is_zero(bmap->div[i][0]))
1622 continue;
1623 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1624 return 0;
1627 return 1;
1631 * Remove divs that don't occur in any of the constraints or other divs.
1632 * These can arise when dropping constraints from a basic map or
1633 * when the divs of a basic map have been temporarily aligned
1634 * with the divs of another basic map.
1636 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1638 int i;
1640 if (!bmap)
1641 return NULL;
1643 for (i = bmap->n_div-1; i >= 0; --i) {
1644 if (!div_is_redundant(bmap, i))
1645 continue;
1646 bmap = isl_basic_map_drop_div(bmap, i);
1648 return bmap;
1651 /* Mark "bmap" as final, without checking for obviously redundant
1652 * integer divisions. This function should be used when "bmap"
1653 * is known not to involve any such integer divisions.
1655 __isl_give isl_basic_map *isl_basic_map_mark_final(
1656 __isl_take isl_basic_map *bmap)
1658 if (!bmap)
1659 return NULL;
1660 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1661 return bmap;
1664 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1666 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1668 bmap = remove_redundant_divs(bmap);
1669 bmap = isl_basic_map_mark_final(bmap);
1670 return bmap;
1673 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1675 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1678 struct isl_set *isl_set_finalize(struct isl_set *set)
1680 int i;
1682 if (!set)
1683 return NULL;
1684 for (i = 0; i < set->n; ++i) {
1685 set->p[i] = isl_basic_set_finalize(set->p[i]);
1686 if (!set->p[i])
1687 goto error;
1689 return set;
1690 error:
1691 isl_set_free(set);
1692 return NULL;
1695 struct isl_map *isl_map_finalize(struct isl_map *map)
1697 int i;
1699 if (!map)
1700 return NULL;
1701 for (i = 0; i < map->n; ++i) {
1702 map->p[i] = isl_basic_map_finalize(map->p[i]);
1703 if (!map->p[i])
1704 goto error;
1706 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1707 return map;
1708 error:
1709 isl_map_free(map);
1710 return NULL;
1714 /* Remove definition of any div that is defined in terms of the given variable.
1715 * The div itself is not removed. Functions such as
1716 * eliminate_divs_ineq depend on the other divs remaining in place.
1718 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1719 int pos)
1721 int i;
1723 if (!bmap)
1724 return NULL;
1726 for (i = 0; i < bmap->n_div; ++i) {
1727 if (isl_int_is_zero(bmap->div[i][0]))
1728 continue;
1729 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1730 continue;
1731 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1732 if (!bmap)
1733 return NULL;
1735 return bmap;
1738 /* Eliminate the specified variables from the constraints using
1739 * Fourier-Motzkin. The variables themselves are not removed.
1741 struct isl_basic_map *isl_basic_map_eliminate_vars(
1742 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1744 int d;
1745 int i, j, k;
1746 unsigned total;
1747 int need_gauss = 0;
1749 if (n == 0)
1750 return bmap;
1751 if (!bmap)
1752 return NULL;
1753 total = isl_basic_map_total_dim(bmap);
1755 bmap = isl_basic_map_cow(bmap);
1756 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1757 bmap = remove_dependent_vars(bmap, d);
1758 if (!bmap)
1759 return NULL;
1761 for (d = pos + n - 1;
1762 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1763 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1764 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1765 int n_lower, n_upper;
1766 if (!bmap)
1767 return NULL;
1768 for (i = 0; i < bmap->n_eq; ++i) {
1769 if (isl_int_is_zero(bmap->eq[i][1+d]))
1770 continue;
1771 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1772 isl_basic_map_drop_equality(bmap, i);
1773 need_gauss = 1;
1774 break;
1776 if (i < bmap->n_eq)
1777 continue;
1778 n_lower = 0;
1779 n_upper = 0;
1780 for (i = 0; i < bmap->n_ineq; ++i) {
1781 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1782 n_lower++;
1783 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1784 n_upper++;
1786 bmap = isl_basic_map_extend_constraints(bmap,
1787 0, n_lower * n_upper);
1788 if (!bmap)
1789 goto error;
1790 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1791 int last;
1792 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1793 continue;
1794 last = -1;
1795 for (j = 0; j < i; ++j) {
1796 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1797 continue;
1798 last = j;
1799 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1800 isl_int_sgn(bmap->ineq[j][1+d]))
1801 continue;
1802 k = isl_basic_map_alloc_inequality(bmap);
1803 if (k < 0)
1804 goto error;
1805 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1806 1+total);
1807 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1808 1+d, 1+total, NULL);
1810 isl_basic_map_drop_inequality(bmap, i);
1811 i = last + 1;
1813 if (n_lower > 0 && n_upper > 0) {
1814 bmap = isl_basic_map_normalize_constraints(bmap);
1815 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1816 NULL, 0);
1817 bmap = isl_basic_map_gauss(bmap, NULL);
1818 bmap = isl_basic_map_remove_redundancies(bmap);
1819 need_gauss = 0;
1820 if (!bmap)
1821 goto error;
1822 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1823 break;
1826 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1827 if (need_gauss)
1828 bmap = isl_basic_map_gauss(bmap, NULL);
1829 return bmap;
1830 error:
1831 isl_basic_map_free(bmap);
1832 return NULL;
1835 struct isl_basic_set *isl_basic_set_eliminate_vars(
1836 struct isl_basic_set *bset, unsigned pos, unsigned n)
1838 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1839 pos, n));
1842 /* Eliminate the specified n dimensions starting at first from the
1843 * constraints, without removing the dimensions from the space.
1844 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1845 * Otherwise, they are projected out and the original space is restored.
1847 __isl_give isl_basic_map *isl_basic_map_eliminate(
1848 __isl_take isl_basic_map *bmap,
1849 enum isl_dim_type type, unsigned first, unsigned n)
1851 isl_space *space;
1853 if (!bmap)
1854 return NULL;
1855 if (n == 0)
1856 return bmap;
1858 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1859 isl_die(bmap->ctx, isl_error_invalid,
1860 "index out of bounds", goto error);
1862 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1863 first += isl_basic_map_offset(bmap, type) - 1;
1864 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1865 return isl_basic_map_finalize(bmap);
1868 space = isl_basic_map_get_space(bmap);
1869 bmap = isl_basic_map_project_out(bmap, type, first, n);
1870 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1871 bmap = isl_basic_map_reset_space(bmap, space);
1872 return bmap;
1873 error:
1874 isl_basic_map_free(bmap);
1875 return NULL;
1878 __isl_give isl_basic_set *isl_basic_set_eliminate(
1879 __isl_take isl_basic_set *bset,
1880 enum isl_dim_type type, unsigned first, unsigned n)
1882 return isl_basic_map_eliminate(bset, type, first, n);
1885 /* Remove all constraints from "bmap" that reference any unknown local
1886 * variables (directly or indirectly).
1888 * Dropping all constraints on a local variable will make it redundant,
1889 * so it will get removed implicitly by
1890 * isl_basic_map_drop_constraints_involving_dims. Some other local
1891 * variables may also end up becoming redundant if they only appear
1892 * in constraints together with the unknown local variable.
1893 * Therefore, start over after calling
1894 * isl_basic_map_drop_constraints_involving_dims.
1896 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1897 __isl_take isl_basic_map *bmap)
1899 isl_bool known;
1900 int i, n_div, o_div;
1902 known = isl_basic_map_divs_known(bmap);
1903 if (known < 0)
1904 return isl_basic_map_free(bmap);
1905 if (known)
1906 return bmap;
1908 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1909 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1911 for (i = 0; i < n_div; ++i) {
1912 known = isl_basic_map_div_is_known(bmap, i);
1913 if (known < 0)
1914 return isl_basic_map_free(bmap);
1915 if (known)
1916 continue;
1917 bmap = remove_dependent_vars(bmap, o_div + i);
1918 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1919 isl_dim_div, i, 1);
1920 if (!bmap)
1921 return NULL;
1922 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1923 i = -1;
1926 return bmap;
1929 /* Remove all constraints from "map" that reference any unknown local
1930 * variables (directly or indirectly).
1932 * Since constraints may get dropped from the basic maps,
1933 * they may no longer be disjoint from each other.
1935 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1936 __isl_take isl_map *map)
1938 int i;
1939 isl_bool known;
1941 known = isl_map_divs_known(map);
1942 if (known < 0)
1943 return isl_map_free(map);
1944 if (known)
1945 return map;
1947 map = isl_map_cow(map);
1948 if (!map)
1949 return NULL;
1951 for (i = 0; i < map->n; ++i) {
1952 map->p[i] =
1953 isl_basic_map_drop_constraint_involving_unknown_divs(
1954 map->p[i]);
1955 if (!map->p[i])
1956 return isl_map_free(map);
1959 if (map->n > 1)
1960 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1962 return map;
1965 /* Don't assume equalities are in order, because align_divs
1966 * may have changed the order of the divs.
1968 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1970 int d, i;
1971 unsigned total;
1973 total = isl_space_dim(bmap->dim, isl_dim_all);
1974 for (d = 0; d < total; ++d)
1975 elim[d] = -1;
1976 for (i = 0; i < bmap->n_eq; ++i) {
1977 for (d = total - 1; d >= 0; --d) {
1978 if (isl_int_is_zero(bmap->eq[i][1+d]))
1979 continue;
1980 elim[d] = i;
1981 break;
1986 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1988 compute_elimination_index(bset_to_bmap(bset), elim);
1991 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1992 struct isl_basic_map *bmap, int *elim)
1994 int d;
1995 int copied = 0;
1996 unsigned total;
1998 total = isl_space_dim(bmap->dim, isl_dim_all);
1999 for (d = total - 1; d >= 0; --d) {
2000 if (isl_int_is_zero(src[1+d]))
2001 continue;
2002 if (elim[d] == -1)
2003 continue;
2004 if (!copied) {
2005 isl_seq_cpy(dst, src, 1 + total);
2006 copied = 1;
2008 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2010 return copied;
2013 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
2014 struct isl_basic_set *bset, int *elim)
2016 return reduced_using_equalities(dst, src,
2017 bset_to_bmap(bset), elim);
2020 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
2021 struct isl_basic_set *bset, struct isl_basic_set *context)
2023 int i;
2024 int *elim;
2026 if (!bset || !context)
2027 goto error;
2029 if (context->n_eq == 0) {
2030 isl_basic_set_free(context);
2031 return bset;
2034 bset = isl_basic_set_cow(bset);
2035 if (!bset)
2036 goto error;
2038 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
2039 if (!elim)
2040 goto error;
2041 set_compute_elimination_index(context, elim);
2042 for (i = 0; i < bset->n_eq; ++i)
2043 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2044 context, elim);
2045 for (i = 0; i < bset->n_ineq; ++i)
2046 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2047 context, elim);
2048 isl_basic_set_free(context);
2049 free(elim);
2050 bset = isl_basic_set_simplify(bset);
2051 bset = isl_basic_set_finalize(bset);
2052 return bset;
2053 error:
2054 isl_basic_set_free(bset);
2055 isl_basic_set_free(context);
2056 return NULL;
2059 /* For each inequality in "ineq" that is a shifted (more relaxed)
2060 * copy of an inequality in "context", mark the corresponding entry
2061 * in "row" with -1.
2062 * If an inequality only has a non-negative constant term, then
2063 * mark it as well.
2065 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
2066 __isl_keep isl_basic_set *context, int *row)
2068 struct isl_constraint_index ci;
2069 int n_ineq;
2070 unsigned total;
2071 int k;
2073 if (!ineq || !context)
2074 return isl_stat_error;
2075 if (context->n_ineq == 0)
2076 return isl_stat_ok;
2077 if (setup_constraint_index(&ci, context) < 0)
2078 return isl_stat_error;
2080 n_ineq = isl_mat_rows(ineq);
2081 total = isl_mat_cols(ineq) - 1;
2082 for (k = 0; k < n_ineq; ++k) {
2083 int l;
2084 isl_bool redundant;
2086 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2087 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2088 row[k] = -1;
2089 continue;
2091 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2092 if (redundant < 0)
2093 goto error;
2094 if (!redundant)
2095 continue;
2096 row[k] = -1;
2098 constraint_index_free(&ci);
2099 return isl_stat_ok;
2100 error:
2101 constraint_index_free(&ci);
2102 return isl_stat_error;
2105 static struct isl_basic_set *remove_shifted_constraints(
2106 struct isl_basic_set *bset, struct isl_basic_set *context)
2108 struct isl_constraint_index ci;
2109 int k;
2111 if (!bset || !context)
2112 return bset;
2114 if (context->n_ineq == 0)
2115 return bset;
2116 if (setup_constraint_index(&ci, context) < 0)
2117 return bset;
2119 for (k = 0; k < bset->n_ineq; ++k) {
2120 isl_bool redundant;
2122 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
2123 if (redundant < 0)
2124 goto error;
2125 if (!redundant)
2126 continue;
2127 bset = isl_basic_set_cow(bset);
2128 if (!bset)
2129 goto error;
2130 isl_basic_set_drop_inequality(bset, k);
2131 --k;
2133 constraint_index_free(&ci);
2134 return bset;
2135 error:
2136 constraint_index_free(&ci);
2137 return bset;
2140 /* Remove constraints from "bmap" that are identical to constraints
2141 * in "context" or that are more relaxed (greater constant term).
2143 * We perform the test for shifted copies on the pure constraints
2144 * in remove_shifted_constraints.
2146 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
2147 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
2149 isl_basic_set *bset, *bset_context;
2151 if (!bmap || !context)
2152 goto error;
2154 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
2155 isl_basic_map_free(context);
2156 return bmap;
2159 context = isl_basic_map_align_divs(context, bmap);
2160 bmap = isl_basic_map_align_divs(bmap, context);
2162 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2163 bset_context = isl_basic_map_underlying_set(context);
2164 bset = remove_shifted_constraints(bset, bset_context);
2165 isl_basic_set_free(bset_context);
2167 bmap = isl_basic_map_overlying_set(bset, bmap);
2169 return bmap;
2170 error:
2171 isl_basic_map_free(bmap);
2172 isl_basic_map_free(context);
2173 return NULL;
2176 /* Does the (linear part of a) constraint "c" involve any of the "len"
2177 * "relevant" dimensions?
2179 static int is_related(isl_int *c, int len, int *relevant)
2181 int i;
2183 for (i = 0; i < len; ++i) {
2184 if (!relevant[i])
2185 continue;
2186 if (!isl_int_is_zero(c[i]))
2187 return 1;
2190 return 0;
2193 /* Drop constraints from "bmap" that do not involve any of
2194 * the dimensions marked "relevant".
2196 static __isl_give isl_basic_map *drop_unrelated_constraints(
2197 __isl_take isl_basic_map *bmap, int *relevant)
2199 int i, dim;
2201 dim = isl_basic_map_dim(bmap, isl_dim_all);
2202 for (i = 0; i < dim; ++i)
2203 if (!relevant[i])
2204 break;
2205 if (i >= dim)
2206 return bmap;
2208 for (i = bmap->n_eq - 1; i >= 0; --i)
2209 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2210 bmap = isl_basic_map_cow(bmap);
2211 if (isl_basic_map_drop_equality(bmap, i) < 0)
2212 return isl_basic_map_free(bmap);
2215 for (i = bmap->n_ineq - 1; i >= 0; --i)
2216 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2217 bmap = isl_basic_map_cow(bmap);
2218 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2219 return isl_basic_map_free(bmap);
2222 return bmap;
2225 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2227 * In particular, for any variable involved in the constraint,
2228 * find the actual group id from before and replace the group
2229 * of the corresponding variable by the minimal group of all
2230 * the variables involved in the constraint considered so far
2231 * (if this minimum is smaller) or replace the minimum by this group
2232 * (if the minimum is larger).
2234 * At the end, all the variables in "c" will (indirectly) point
2235 * to the minimal of the groups that they referred to originally.
2237 static void update_groups(int dim, int *group, isl_int *c)
2239 int j;
2240 int min = dim;
2242 for (j = 0; j < dim; ++j) {
2243 if (isl_int_is_zero(c[j]))
2244 continue;
2245 while (group[j] >= 0 && group[group[j]] != group[j])
2246 group[j] = group[group[j]];
2247 if (group[j] == min)
2248 continue;
2249 if (group[j] < min) {
2250 if (min >= 0 && min < dim)
2251 group[min] = group[j];
2252 min = group[j];
2253 } else
2254 group[group[j]] = min;
2258 /* Allocate an array of groups of variables, one for each variable
2259 * in "context", initialized to zero.
2261 static int *alloc_groups(__isl_keep isl_basic_set *context)
2263 isl_ctx *ctx;
2264 int dim;
2266 dim = isl_basic_set_dim(context, isl_dim_set);
2267 ctx = isl_basic_set_get_ctx(context);
2268 return isl_calloc_array(ctx, int, dim);
2271 /* Drop constraints from "bmap" that only involve variables that are
2272 * not related to any of the variables marked with a "-1" in "group".
2274 * We construct groups of variables that collect variables that
2275 * (indirectly) appear in some common constraint of "bmap".
2276 * Each group is identified by the first variable in the group,
2277 * except for the special group of variables that was already identified
2278 * in the input as -1 (or are related to those variables).
2279 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2280 * otherwise the group of i is the group of group[i].
2282 * We first initialize groups for the remaining variables.
2283 * Then we iterate over the constraints of "bmap" and update the
2284 * group of the variables in the constraint by the smallest group.
2285 * Finally, we resolve indirect references to groups by running over
2286 * the variables.
2288 * After computing the groups, we drop constraints that do not involve
2289 * any variables in the -1 group.
2291 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2292 __isl_take isl_basic_map *bmap, __isl_take int *group)
2294 int dim;
2295 int i;
2296 int last;
2298 if (!bmap)
2299 return NULL;
2301 dim = isl_basic_map_dim(bmap, isl_dim_all);
2303 last = -1;
2304 for (i = 0; i < dim; ++i)
2305 if (group[i] >= 0)
2306 last = group[i] = i;
2307 if (last < 0) {
2308 free(group);
2309 return bmap;
2312 for (i = 0; i < bmap->n_eq; ++i)
2313 update_groups(dim, group, bmap->eq[i] + 1);
2314 for (i = 0; i < bmap->n_ineq; ++i)
2315 update_groups(dim, group, bmap->ineq[i] + 1);
2317 for (i = 0; i < dim; ++i)
2318 if (group[i] >= 0)
2319 group[i] = group[group[i]];
2321 for (i = 0; i < dim; ++i)
2322 group[i] = group[i] == -1;
2324 bmap = drop_unrelated_constraints(bmap, group);
2326 free(group);
2327 return bmap;
2330 /* Drop constraints from "context" that are irrelevant for computing
2331 * the gist of "bset".
2333 * In particular, drop constraints in variables that are not related
2334 * to any of the variables involved in the constraints of "bset"
2335 * in the sense that there is no sequence of constraints that connects them.
2337 * We first mark all variables that appear in "bset" as belonging
2338 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2340 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2341 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2343 int *group;
2344 int dim;
2345 int i, j;
2347 if (!context || !bset)
2348 return isl_basic_set_free(context);
2350 group = alloc_groups(context);
2352 if (!group)
2353 return isl_basic_set_free(context);
2355 dim = isl_basic_set_dim(bset, isl_dim_set);
2356 for (i = 0; i < dim; ++i) {
2357 for (j = 0; j < bset->n_eq; ++j)
2358 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2359 break;
2360 if (j < bset->n_eq) {
2361 group[i] = -1;
2362 continue;
2364 for (j = 0; j < bset->n_ineq; ++j)
2365 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2366 break;
2367 if (j < bset->n_ineq)
2368 group[i] = -1;
2371 return isl_basic_map_drop_unrelated_constraints(context, group);
2374 /* Drop constraints from "context" that are irrelevant for computing
2375 * the gist of the inequalities "ineq".
2376 * Inequalities in "ineq" for which the corresponding element of row
2377 * is set to -1 have already been marked for removal and should be ignored.
2379 * In particular, drop constraints in variables that are not related
2380 * to any of the variables involved in "ineq"
2381 * in the sense that there is no sequence of constraints that connects them.
2383 * We first mark all variables that appear in "bset" as belonging
2384 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2386 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2387 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2389 int *group;
2390 int dim;
2391 int i, j, n;
2393 if (!context || !ineq)
2394 return isl_basic_set_free(context);
2396 group = alloc_groups(context);
2398 if (!group)
2399 return isl_basic_set_free(context);
2401 dim = isl_basic_set_dim(context, isl_dim_set);
2402 n = isl_mat_rows(ineq);
2403 for (i = 0; i < dim; ++i) {
2404 for (j = 0; j < n; ++j) {
2405 if (row[j] < 0)
2406 continue;
2407 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2408 break;
2410 if (j < n)
2411 group[i] = -1;
2414 return isl_basic_map_drop_unrelated_constraints(context, group);
2417 /* Do all "n" entries of "row" contain a negative value?
2419 static int all_neg(int *row, int n)
2421 int i;
2423 for (i = 0; i < n; ++i)
2424 if (row[i] >= 0)
2425 return 0;
2427 return 1;
2430 /* Update the inequalities in "bset" based on the information in "row"
2431 * and "tab".
2433 * In particular, the array "row" contains either -1, meaning that
2434 * the corresponding inequality of "bset" is redundant, or the index
2435 * of an inequality in "tab".
2437 * If the row entry is -1, then drop the inequality.
2438 * Otherwise, if the constraint is marked redundant in the tableau,
2439 * then drop the inequality. Similarly, if it is marked as an equality
2440 * in the tableau, then turn the inequality into an equality and
2441 * perform Gaussian elimination.
2443 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2444 __isl_keep int *row, struct isl_tab *tab)
2446 int i;
2447 unsigned n_ineq;
2448 unsigned n_eq;
2449 int found_equality = 0;
2451 if (!bset)
2452 return NULL;
2453 if (tab && tab->empty)
2454 return isl_basic_set_set_to_empty(bset);
2456 n_ineq = bset->n_ineq;
2457 for (i = n_ineq - 1; i >= 0; --i) {
2458 if (row[i] < 0) {
2459 if (isl_basic_set_drop_inequality(bset, i) < 0)
2460 return isl_basic_set_free(bset);
2461 continue;
2463 if (!tab)
2464 continue;
2465 n_eq = tab->n_eq;
2466 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2467 isl_basic_map_inequality_to_equality(bset, i);
2468 found_equality = 1;
2469 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2470 if (isl_basic_set_drop_inequality(bset, i) < 0)
2471 return isl_basic_set_free(bset);
2475 if (found_equality)
2476 bset = isl_basic_set_gauss(bset, NULL);
2477 bset = isl_basic_set_finalize(bset);
2478 return bset;
2481 /* Update the inequalities in "bset" based on the information in "row"
2482 * and "tab" and free all arguments (other than "bset").
2484 static __isl_give isl_basic_set *update_ineq_free(
2485 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2486 __isl_take isl_basic_set *context, __isl_take int *row,
2487 struct isl_tab *tab)
2489 isl_mat_free(ineq);
2490 isl_basic_set_free(context);
2492 bset = update_ineq(bset, row, tab);
2494 free(row);
2495 isl_tab_free(tab);
2496 return bset;
2499 /* Remove all information from bset that is redundant in the context
2500 * of context.
2501 * "ineq" contains the (possibly transformed) inequalities of "bset",
2502 * in the same order.
2503 * The (explicit) equalities of "bset" are assumed to have been taken
2504 * into account by the transformation such that only the inequalities
2505 * are relevant.
2506 * "context" is assumed not to be empty.
2508 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2509 * A value of -1 means that the inequality is obviously redundant and may
2510 * not even appear in "tab".
2512 * We first mark the inequalities of "bset"
2513 * that are obviously redundant with respect to some inequality in "context".
2514 * Then we remove those constraints from "context" that have become
2515 * irrelevant for computing the gist of "bset".
2516 * Note that this removal of constraints cannot be replaced by
2517 * a factorization because factors in "bset" may still be connected
2518 * to each other through constraints in "context".
2520 * If there are any inequalities left, we construct a tableau for
2521 * the context and then add the inequalities of "bset".
2522 * Before adding these inequalities, we freeze all constraints such that
2523 * they won't be considered redundant in terms of the constraints of "bset".
2524 * Then we detect all redundant constraints (among the
2525 * constraints that weren't frozen), first by checking for redundancy in the
2526 * the tableau and then by checking if replacing a constraint by its negation
2527 * would lead to an empty set. This last step is fairly expensive
2528 * and could be optimized by more reuse of the tableau.
2529 * Finally, we update bset according to the results.
2531 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2532 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2534 int i, r;
2535 int *row = NULL;
2536 isl_ctx *ctx;
2537 isl_basic_set *combined = NULL;
2538 struct isl_tab *tab = NULL;
2539 unsigned n_eq, context_ineq;
2540 unsigned total;
2542 if (!bset || !ineq || !context)
2543 goto error;
2545 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2546 isl_basic_set_free(context);
2547 isl_mat_free(ineq);
2548 return bset;
2551 ctx = isl_basic_set_get_ctx(context);
2552 row = isl_calloc_array(ctx, int, bset->n_ineq);
2553 if (!row)
2554 goto error;
2556 if (mark_shifted_constraints(ineq, context, row) < 0)
2557 goto error;
2558 if (all_neg(row, bset->n_ineq))
2559 return update_ineq_free(bset, ineq, context, row, NULL);
2561 context = drop_irrelevant_constraints_marked(context, ineq, row);
2562 if (!context)
2563 goto error;
2564 if (isl_basic_set_plain_is_universe(context))
2565 return update_ineq_free(bset, ineq, context, row, NULL);
2567 n_eq = context->n_eq;
2568 context_ineq = context->n_ineq;
2569 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2570 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2571 tab = isl_tab_from_basic_set(combined, 0);
2572 for (i = 0; i < context_ineq; ++i)
2573 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2574 goto error;
2575 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2576 goto error;
2577 r = context_ineq;
2578 for (i = 0; i < bset->n_ineq; ++i) {
2579 if (row[i] < 0)
2580 continue;
2581 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2582 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2583 goto error;
2584 row[i] = r++;
2586 if (isl_tab_detect_implicit_equalities(tab) < 0)
2587 goto error;
2588 if (isl_tab_detect_redundant(tab) < 0)
2589 goto error;
2590 total = isl_basic_set_total_dim(bset);
2591 for (i = bset->n_ineq - 1; i >= 0; --i) {
2592 isl_basic_set *test;
2593 int is_empty;
2595 if (row[i] < 0)
2596 continue;
2597 r = row[i];
2598 if (tab->con[n_eq + r].is_redundant)
2599 continue;
2600 test = isl_basic_set_dup(combined);
2601 if (isl_inequality_negate(test, r) < 0)
2602 test = isl_basic_set_free(test);
2603 test = isl_basic_set_update_from_tab(test, tab);
2604 is_empty = isl_basic_set_is_empty(test);
2605 isl_basic_set_free(test);
2606 if (is_empty < 0)
2607 goto error;
2608 if (is_empty)
2609 tab->con[n_eq + r].is_redundant = 1;
2611 bset = update_ineq_free(bset, ineq, context, row, tab);
2612 if (bset) {
2613 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2614 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2617 isl_basic_set_free(combined);
2618 return bset;
2619 error:
2620 free(row);
2621 isl_mat_free(ineq);
2622 isl_tab_free(tab);
2623 isl_basic_set_free(combined);
2624 isl_basic_set_free(context);
2625 isl_basic_set_free(bset);
2626 return NULL;
2629 /* Extract the inequalities of "bset" as an isl_mat.
2631 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2633 unsigned total;
2634 isl_ctx *ctx;
2635 isl_mat *ineq;
2637 if (!bset)
2638 return NULL;
2640 ctx = isl_basic_set_get_ctx(bset);
2641 total = isl_basic_set_total_dim(bset);
2642 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2643 0, 1 + total);
2645 return ineq;
2648 /* Remove all information from "bset" that is redundant in the context
2649 * of "context", for the case where both "bset" and "context" are
2650 * full-dimensional.
2652 static __isl_give isl_basic_set *uset_gist_uncompressed(
2653 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2655 isl_mat *ineq;
2657 ineq = extract_ineq(bset);
2658 return uset_gist_full(bset, ineq, context);
2661 /* Remove all information from "bset" that is redundant in the context
2662 * of "context", for the case where the combined equalities of
2663 * "bset" and "context" allow for a compression that can be obtained
2664 * by preapplication of "T".
2666 * "bset" itself is not transformed by "T". Instead, the inequalities
2667 * are extracted from "bset" and those are transformed by "T".
2668 * uset_gist_full then determines which of the transformed inequalities
2669 * are redundant with respect to the transformed "context" and removes
2670 * the corresponding inequalities from "bset".
2672 * After preapplying "T" to the inequalities, any common factor is
2673 * removed from the coefficients. If this results in a tightening
2674 * of the constant term, then the same tightening is applied to
2675 * the corresponding untransformed inequality in "bset".
2676 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2678 * g f'(x) + r >= 0
2680 * with 0 <= r < g, then it is equivalent to
2682 * f'(x) >= 0
2684 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2685 * subspace compressed by T since the latter would be transformed to
2687 * g f'(x) >= 0
2689 static __isl_give isl_basic_set *uset_gist_compressed(
2690 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2691 __isl_take isl_mat *T)
2693 isl_ctx *ctx;
2694 isl_mat *ineq;
2695 int i, n_row, n_col;
2696 isl_int rem;
2698 ineq = extract_ineq(bset);
2699 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2700 context = isl_basic_set_preimage(context, T);
2702 if (!ineq || !context)
2703 goto error;
2704 if (isl_basic_set_plain_is_empty(context)) {
2705 isl_mat_free(ineq);
2706 isl_basic_set_free(context);
2707 return isl_basic_set_set_to_empty(bset);
2710 ctx = isl_mat_get_ctx(ineq);
2711 n_row = isl_mat_rows(ineq);
2712 n_col = isl_mat_cols(ineq);
2713 isl_int_init(rem);
2714 for (i = 0; i < n_row; ++i) {
2715 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2716 if (isl_int_is_zero(ctx->normalize_gcd))
2717 continue;
2718 if (isl_int_is_one(ctx->normalize_gcd))
2719 continue;
2720 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2721 ctx->normalize_gcd, n_col - 1);
2722 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2723 isl_int_fdiv_q(ineq->row[i][0],
2724 ineq->row[i][0], ctx->normalize_gcd);
2725 if (isl_int_is_zero(rem))
2726 continue;
2727 bset = isl_basic_set_cow(bset);
2728 if (!bset)
2729 break;
2730 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2732 isl_int_clear(rem);
2734 return uset_gist_full(bset, ineq, context);
2735 error:
2736 isl_mat_free(ineq);
2737 isl_basic_set_free(context);
2738 isl_basic_set_free(bset);
2739 return NULL;
2742 /* Project "bset" onto the variables that are involved in "template".
2744 static __isl_give isl_basic_set *project_onto_involved(
2745 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2747 int i, n;
2749 if (!bset || !template)
2750 return isl_basic_set_free(bset);
2752 n = isl_basic_set_dim(template, isl_dim_set);
2754 for (i = 0; i < n; ++i) {
2755 isl_bool involved;
2757 involved = isl_basic_set_involves_dims(template,
2758 isl_dim_set, i, 1);
2759 if (involved < 0)
2760 return isl_basic_set_free(bset);
2761 if (involved)
2762 continue;
2763 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2766 return bset;
2769 /* Remove all information from bset that is redundant in the context
2770 * of context. In particular, equalities that are linear combinations
2771 * of those in context are removed. Then the inequalities that are
2772 * redundant in the context of the equalities and inequalities of
2773 * context are removed.
2775 * First of all, we drop those constraints from "context"
2776 * that are irrelevant for computing the gist of "bset".
2777 * Alternatively, we could factorize the intersection of "context" and "bset".
2779 * We first compute the intersection of the integer affine hulls
2780 * of "bset" and "context",
2781 * compute the gist inside this intersection and then reduce
2782 * the constraints with respect to the equalities of the context
2783 * that only involve variables already involved in the input.
2785 * If two constraints are mutually redundant, then uset_gist_full
2786 * will remove the second of those constraints. We therefore first
2787 * sort the constraints so that constraints not involving existentially
2788 * quantified variables are given precedence over those that do.
2789 * We have to perform this sorting before the variable compression,
2790 * because that may effect the order of the variables.
2792 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2793 __isl_take isl_basic_set *context)
2795 isl_mat *eq;
2796 isl_mat *T;
2797 isl_basic_set *aff;
2798 isl_basic_set *aff_context;
2799 unsigned total;
2801 if (!bset || !context)
2802 goto error;
2804 context = drop_irrelevant_constraints(context, bset);
2806 bset = isl_basic_set_detect_equalities(bset);
2807 aff = isl_basic_set_copy(bset);
2808 aff = isl_basic_set_plain_affine_hull(aff);
2809 context = isl_basic_set_detect_equalities(context);
2810 aff_context = isl_basic_set_copy(context);
2811 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2812 aff = isl_basic_set_intersect(aff, aff_context);
2813 if (!aff)
2814 goto error;
2815 if (isl_basic_set_plain_is_empty(aff)) {
2816 isl_basic_set_free(bset);
2817 isl_basic_set_free(context);
2818 return aff;
2820 bset = isl_basic_set_sort_constraints(bset);
2821 if (aff->n_eq == 0) {
2822 isl_basic_set_free(aff);
2823 return uset_gist_uncompressed(bset, context);
2825 total = isl_basic_set_total_dim(bset);
2826 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2827 eq = isl_mat_cow(eq);
2828 T = isl_mat_variable_compression(eq, NULL);
2829 isl_basic_set_free(aff);
2830 if (T && T->n_col == 0) {
2831 isl_mat_free(T);
2832 isl_basic_set_free(context);
2833 return isl_basic_set_set_to_empty(bset);
2836 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2837 aff_context = project_onto_involved(aff_context, bset);
2839 bset = uset_gist_compressed(bset, context, T);
2840 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2842 if (bset) {
2843 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2844 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2847 return bset;
2848 error:
2849 isl_basic_set_free(bset);
2850 isl_basic_set_free(context);
2851 return NULL;
2854 /* Return the number of equality constraints in "bmap" that involve
2855 * local variables. This function assumes that Gaussian elimination
2856 * has been applied to the equality constraints.
2858 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2860 int i;
2861 int total, n_div;
2863 if (!bmap)
2864 return -1;
2866 if (bmap->n_eq == 0)
2867 return 0;
2869 total = isl_basic_map_dim(bmap, isl_dim_all);
2870 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2871 total -= n_div;
2873 for (i = 0; i < bmap->n_eq; ++i)
2874 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2875 n_div) == -1)
2876 return i;
2878 return bmap->n_eq;
2881 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2882 * The constraints are assumed not to involve any local variables.
2884 static __isl_give isl_basic_map *basic_map_from_equalities(
2885 __isl_take isl_space *space, __isl_take isl_mat *eq)
2887 int i, k;
2888 isl_basic_map *bmap = NULL;
2890 if (!space || !eq)
2891 goto error;
2893 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2894 isl_die(isl_space_get_ctx(space), isl_error_internal,
2895 "unexpected number of columns", goto error);
2897 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2898 0, eq->n_row, 0);
2899 for (i = 0; i < eq->n_row; ++i) {
2900 k = isl_basic_map_alloc_equality(bmap);
2901 if (k < 0)
2902 goto error;
2903 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2906 isl_space_free(space);
2907 isl_mat_free(eq);
2908 return bmap;
2909 error:
2910 isl_space_free(space);
2911 isl_mat_free(eq);
2912 isl_basic_map_free(bmap);
2913 return NULL;
2916 /* Construct and return a variable compression based on the equality
2917 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2918 * "n1" is the number of (initial) equality constraints in "bmap1"
2919 * that do involve local variables.
2920 * "n2" is the number of (initial) equality constraints in "bmap2"
2921 * that do involve local variables.
2922 * "total" is the total number of other variables.
2923 * This function assumes that Gaussian elimination
2924 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2925 * such that the equality constraints not involving local variables
2926 * are those that start at "n1" or "n2".
2928 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2929 * then simply compute the compression based on the equality constraints
2930 * in the other basic map.
2931 * Otherwise, combine the equality constraints from both into a new
2932 * basic map such that Gaussian elimination can be applied to this combination
2933 * and then construct a variable compression from the resulting
2934 * equality constraints.
2936 static __isl_give isl_mat *combined_variable_compression(
2937 __isl_keep isl_basic_map *bmap1, int n1,
2938 __isl_keep isl_basic_map *bmap2, int n2, int total)
2940 isl_ctx *ctx;
2941 isl_mat *E1, *E2, *V;
2942 isl_basic_map *bmap;
2944 ctx = isl_basic_map_get_ctx(bmap1);
2945 if (bmap1->n_eq == n1) {
2946 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2947 n2, bmap2->n_eq - n2, 0, 1 + total);
2948 return isl_mat_variable_compression(E2, NULL);
2950 if (bmap2->n_eq == n2) {
2951 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2952 n1, bmap1->n_eq - n1, 0, 1 + total);
2953 return isl_mat_variable_compression(E1, NULL);
2955 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2956 n1, bmap1->n_eq - n1, 0, 1 + total);
2957 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2958 n2, bmap2->n_eq - n2, 0, 1 + total);
2959 E1 = isl_mat_concat(E1, E2);
2960 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2961 bmap = isl_basic_map_gauss(bmap, NULL);
2962 if (!bmap)
2963 return NULL;
2964 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2965 V = isl_mat_variable_compression(E1, NULL);
2966 isl_basic_map_free(bmap);
2968 return V;
2971 /* Extract the stride constraints from "bmap", compressed
2972 * with respect to both the stride constraints in "context" and
2973 * the remaining equality constraints in both "bmap" and "context".
2974 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2975 * "context_n_eq" is the number of (initial) stride constraints in "context".
2977 * Let x be all variables in "bmap" (and "context") other than the local
2978 * variables. First compute a variable compression
2980 * x = V x'
2982 * based on the non-stride equality constraints in "bmap" and "context".
2983 * Consider the stride constraints of "context",
2985 * A(x) + B(y) = 0
2987 * with y the local variables and plug in the variable compression,
2988 * resulting in
2990 * A(V x') + B(y) = 0
2992 * Use these constraints to compute a parameter compression on x'
2994 * x' = T x''
2996 * Now consider the stride constraints of "bmap"
2998 * C(x) + D(y) = 0
3000 * and plug in x = V*T x''.
3001 * That is, return A = [C*V*T D].
3003 static __isl_give isl_mat *extract_compressed_stride_constraints(
3004 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3005 __isl_keep isl_basic_map *context, int context_n_eq)
3007 int total, n_div;
3008 isl_ctx *ctx;
3009 isl_mat *A, *B, *T, *V;
3011 total = isl_basic_map_dim(context, isl_dim_all);
3012 n_div = isl_basic_map_dim(context, isl_dim_div);
3013 total -= n_div;
3015 ctx = isl_basic_map_get_ctx(bmap);
3017 V = combined_variable_compression(bmap, bmap_n_eq,
3018 context, context_n_eq, total);
3020 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3021 B = isl_mat_sub_alloc6(ctx, context->eq,
3022 0, context_n_eq, 1 + total, n_div);
3023 A = isl_mat_product(A, isl_mat_copy(V));
3024 T = isl_mat_parameter_compression_ext(A, B);
3025 T = isl_mat_product(V, T);
3027 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3028 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
3030 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3031 0, bmap_n_eq, 0, 1 + total + n_div);
3032 A = isl_mat_product(A, T);
3034 return A;
3037 /* Remove the prime factors from *g that have an exponent that
3038 * is strictly smaller than the exponent in "c".
3039 * All exponents in *g are known to be smaller than or equal
3040 * to those in "c".
3042 * That is, if *g is equal to
3044 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3046 * and "c" is equal to
3048 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3050 * then update *g to
3052 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3053 * p_n^{e_n * (e_n = f_n)}
3055 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3056 * neither does the gcd of *g and c / *g.
3057 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3058 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3059 * Dividing *g by this gcd therefore strictly reduces the exponent
3060 * of the prime factors that need to be removed, while leaving the
3061 * other prime factors untouched.
3062 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3063 * removes all undesired factors, without removing any others.
3065 static void remove_incomplete_powers(isl_int *g, isl_int c)
3067 isl_int t;
3069 isl_int_init(t);
3070 for (;;) {
3071 isl_int_divexact(t, c, *g);
3072 isl_int_gcd(t, t, *g);
3073 if (isl_int_is_one(t))
3074 break;
3075 isl_int_divexact(*g, *g, t);
3077 isl_int_clear(t);
3080 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3081 * of the same stride constraints in a compressed space that exploits
3082 * all equalities in the context and the other equalities in "bmap".
3084 * If the stride constraints of "bmap" are of the form
3086 * C(x) + D(y) = 0
3088 * then A is of the form
3090 * B(x') + D(y) = 0
3092 * If any of these constraints involves only a single local variable y,
3093 * then the constraint appears as
3095 * f(x) + m y_i = 0
3097 * in "bmap" and as
3099 * h(x') + m y_i = 0
3101 * in "A".
3103 * Let g be the gcd of m and the coefficients of h.
3104 * Then, in particular, g is a divisor of the coefficients of h and
3106 * f(x) = h(x')
3108 * is known to be a multiple of g.
3109 * If some prime factor in m appears with the same exponent in g,
3110 * then it can be removed from m because f(x) is already known
3111 * to be a multiple of g and therefore in particular of this power
3112 * of the prime factors.
3113 * Prime factors that appear with a smaller exponent in g cannot
3114 * be removed from m.
3115 * Let g' be the divisor of g containing all prime factors that
3116 * appear with the same exponent in m and g, then
3118 * f(x) + m y_i = 0
3120 * can be replaced by
3122 * f(x) + m/g' y_i' = 0
3124 * Note that (if g' != 1) this changes the explicit representation
3125 * of y_i to that of y_i', so the integer division at position i
3126 * is marked unknown and later recomputed by a call to
3127 * isl_basic_map_gauss.
3129 static __isl_give isl_basic_map *reduce_stride_constraints(
3130 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
3132 int i;
3133 int total, n_div;
3134 int any = 0;
3135 isl_int gcd;
3137 if (!bmap || !A)
3138 return isl_basic_map_free(bmap);
3140 total = isl_basic_map_dim(bmap, isl_dim_all);
3141 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3142 total -= n_div;
3144 isl_int_init(gcd);
3145 for (i = 0; i < n; ++i) {
3146 int div;
3148 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
3149 if (div < 0)
3150 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3151 "equality constraints modified unexpectedly",
3152 goto error);
3153 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3154 n_div - div - 1) != -1)
3155 continue;
3156 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3157 goto error;
3158 if (isl_int_is_one(gcd))
3159 continue;
3160 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3161 if (isl_int_is_one(gcd))
3162 continue;
3163 isl_int_divexact(bmap->eq[i][1 + total + div],
3164 bmap->eq[i][1 + total + div], gcd);
3165 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3166 if (!bmap)
3167 goto error;
3168 any = 1;
3170 isl_int_clear(gcd);
3172 if (any)
3173 bmap = isl_basic_map_gauss(bmap, NULL);
3175 return bmap;
3176 error:
3177 isl_int_clear(gcd);
3178 isl_basic_map_free(bmap);
3179 return NULL;
3182 /* Simplify the stride constraints in "bmap" based on
3183 * the remaining equality constraints in "bmap" and all equality
3184 * constraints in "context".
3185 * Only do this if both "bmap" and "context" have stride constraints.
3187 * First extract a copy of the stride constraints in "bmap" in a compressed
3188 * space exploiting all the other equality constraints and then
3189 * use this compressed copy to simplify the original stride constraints.
3191 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3192 __isl_keep isl_basic_map *context)
3194 int bmap_n_eq, context_n_eq;
3195 isl_mat *A;
3197 if (!bmap || !context)
3198 return isl_basic_map_free(bmap);
3200 bmap_n_eq = n_div_eq(bmap);
3201 context_n_eq = n_div_eq(context);
3203 if (bmap_n_eq < 0 || context_n_eq < 0)
3204 return isl_basic_map_free(bmap);
3205 if (bmap_n_eq == 0 || context_n_eq == 0)
3206 return bmap;
3208 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3209 context, context_n_eq);
3210 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3212 isl_mat_free(A);
3214 return bmap;
3217 /* Return a basic map that has the same intersection with "context" as "bmap"
3218 * and that is as "simple" as possible.
3220 * The core computation is performed on the pure constraints.
3221 * When we add back the meaning of the integer divisions, we need
3222 * to (re)introduce the div constraints. If we happen to have
3223 * discovered that some of these integer divisions are equal to
3224 * some affine combination of other variables, then these div
3225 * constraints may end up getting simplified in terms of the equalities,
3226 * resulting in extra inequalities on the other variables that
3227 * may have been removed already or that may not even have been
3228 * part of the input. We try and remove those constraints of
3229 * this form that are most obviously redundant with respect to
3230 * the context. We also remove those div constraints that are
3231 * redundant with respect to the other constraints in the result.
3233 * The stride constraints among the equality constraints in "bmap" are
3234 * also simplified with respecting to the other equality constraints
3235 * in "bmap" and with respect to all equality constraints in "context".
3237 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3238 struct isl_basic_map *context)
3240 isl_basic_set *bset, *eq;
3241 isl_basic_map *eq_bmap;
3242 unsigned total, n_div, extra, n_eq, n_ineq;
3244 if (!bmap || !context)
3245 goto error;
3247 if (isl_basic_map_plain_is_universe(bmap)) {
3248 isl_basic_map_free(context);
3249 return bmap;
3251 if (isl_basic_map_plain_is_empty(context)) {
3252 isl_space *space = isl_basic_map_get_space(bmap);
3253 isl_basic_map_free(bmap);
3254 isl_basic_map_free(context);
3255 return isl_basic_map_universe(space);
3257 if (isl_basic_map_plain_is_empty(bmap)) {
3258 isl_basic_map_free(context);
3259 return bmap;
3262 bmap = isl_basic_map_remove_redundancies(bmap);
3263 context = isl_basic_map_remove_redundancies(context);
3264 if (!context)
3265 goto error;
3267 context = isl_basic_map_align_divs(context, bmap);
3268 n_div = isl_basic_map_dim(context, isl_dim_div);
3269 total = isl_basic_map_dim(bmap, isl_dim_all);
3270 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3272 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3273 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3274 bset = uset_gist(bset,
3275 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3276 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3278 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3279 isl_basic_set_plain_is_empty(bset)) {
3280 isl_basic_map_free(context);
3281 return isl_basic_map_overlying_set(bset, bmap);
3284 n_eq = bset->n_eq;
3285 n_ineq = bset->n_ineq;
3286 eq = isl_basic_set_copy(bset);
3287 eq = isl_basic_set_cow(eq);
3288 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3289 eq = isl_basic_set_free(eq);
3290 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3291 bset = isl_basic_set_free(bset);
3293 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3294 eq_bmap = gist_strides(eq_bmap, context);
3295 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3296 bmap = isl_basic_map_overlying_set(bset, bmap);
3297 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3298 bmap = isl_basic_map_remove_redundancies(bmap);
3300 return bmap;
3301 error:
3302 isl_basic_map_free(bmap);
3303 isl_basic_map_free(context);
3304 return NULL;
3308 * Assumes context has no implicit divs.
3310 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3311 __isl_take isl_basic_map *context)
3313 int i;
3315 if (!map || !context)
3316 goto error;
3318 if (isl_basic_map_plain_is_empty(context)) {
3319 isl_space *space = isl_map_get_space(map);
3320 isl_map_free(map);
3321 isl_basic_map_free(context);
3322 return isl_map_universe(space);
3325 context = isl_basic_map_remove_redundancies(context);
3326 map = isl_map_cow(map);
3327 if (!map || !context)
3328 goto error;
3329 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3330 map = isl_map_compute_divs(map);
3331 if (!map)
3332 goto error;
3333 for (i = map->n - 1; i >= 0; --i) {
3334 map->p[i] = isl_basic_map_gist(map->p[i],
3335 isl_basic_map_copy(context));
3336 if (!map->p[i])
3337 goto error;
3338 if (isl_basic_map_plain_is_empty(map->p[i])) {
3339 isl_basic_map_free(map->p[i]);
3340 if (i != map->n - 1)
3341 map->p[i] = map->p[map->n - 1];
3342 map->n--;
3345 isl_basic_map_free(context);
3346 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3347 return map;
3348 error:
3349 isl_map_free(map);
3350 isl_basic_map_free(context);
3351 return NULL;
3354 /* Drop all inequalities from "bmap" that also appear in "context".
3355 * "context" is assumed to have only known local variables and
3356 * the initial local variables of "bmap" are assumed to be the same
3357 * as those of "context".
3358 * The constraints of both "bmap" and "context" are assumed
3359 * to have been sorted using isl_basic_map_sort_constraints.
3361 * Run through the inequality constraints of "bmap" and "context"
3362 * in sorted order.
3363 * If a constraint of "bmap" involves variables not in "context",
3364 * then it cannot appear in "context".
3365 * If a matching constraint is found, it is removed from "bmap".
3367 static __isl_give isl_basic_map *drop_inequalities(
3368 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3370 int i1, i2;
3371 unsigned total, extra;
3373 if (!bmap || !context)
3374 return isl_basic_map_free(bmap);
3376 total = isl_basic_map_total_dim(context);
3377 extra = isl_basic_map_total_dim(bmap) - total;
3379 i1 = bmap->n_ineq - 1;
3380 i2 = context->n_ineq - 1;
3381 while (bmap && i1 >= 0 && i2 >= 0) {
3382 int cmp;
3384 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3385 extra) != -1) {
3386 --i1;
3387 continue;
3389 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3390 context->ineq[i2]);
3391 if (cmp < 0) {
3392 --i2;
3393 continue;
3395 if (cmp > 0) {
3396 --i1;
3397 continue;
3399 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3400 bmap = isl_basic_map_cow(bmap);
3401 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3402 bmap = isl_basic_map_free(bmap);
3404 --i1;
3405 --i2;
3408 return bmap;
3411 /* Drop all equalities from "bmap" that also appear in "context".
3412 * "context" is assumed to have only known local variables and
3413 * the initial local variables of "bmap" are assumed to be the same
3414 * as those of "context".
3416 * Run through the equality constraints of "bmap" and "context"
3417 * in sorted order.
3418 * If a constraint of "bmap" involves variables not in "context",
3419 * then it cannot appear in "context".
3420 * If a matching constraint is found, it is removed from "bmap".
3422 static __isl_give isl_basic_map *drop_equalities(
3423 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3425 int i1, i2;
3426 unsigned total, extra;
3428 if (!bmap || !context)
3429 return isl_basic_map_free(bmap);
3431 total = isl_basic_map_total_dim(context);
3432 extra = isl_basic_map_total_dim(bmap) - total;
3434 i1 = bmap->n_eq - 1;
3435 i2 = context->n_eq - 1;
3437 while (bmap && i1 >= 0 && i2 >= 0) {
3438 int last1, last2;
3440 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3441 extra) != -1)
3442 break;
3443 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3444 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3445 if (last1 > last2) {
3446 --i2;
3447 continue;
3449 if (last1 < last2) {
3450 --i1;
3451 continue;
3453 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3454 bmap = isl_basic_map_cow(bmap);
3455 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3456 bmap = isl_basic_map_free(bmap);
3458 --i1;
3459 --i2;
3462 return bmap;
3465 /* Remove the constraints in "context" from "bmap".
3466 * "context" is assumed to have explicit representations
3467 * for all local variables.
3469 * First align the divs of "bmap" to those of "context" and
3470 * sort the constraints. Then drop all constraints from "bmap"
3471 * that appear in "context".
3473 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3474 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3476 isl_bool done, known;
3478 done = isl_basic_map_plain_is_universe(context);
3479 if (done == isl_bool_false)
3480 done = isl_basic_map_plain_is_universe(bmap);
3481 if (done == isl_bool_false)
3482 done = isl_basic_map_plain_is_empty(context);
3483 if (done == isl_bool_false)
3484 done = isl_basic_map_plain_is_empty(bmap);
3485 if (done < 0)
3486 goto error;
3487 if (done) {
3488 isl_basic_map_free(context);
3489 return bmap;
3491 known = isl_basic_map_divs_known(context);
3492 if (known < 0)
3493 goto error;
3494 if (!known)
3495 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3496 "context has unknown divs", goto error);
3498 bmap = isl_basic_map_align_divs(bmap, context);
3499 bmap = isl_basic_map_gauss(bmap, NULL);
3500 bmap = isl_basic_map_sort_constraints(bmap);
3501 context = isl_basic_map_sort_constraints(context);
3503 bmap = drop_inequalities(bmap, context);
3504 bmap = drop_equalities(bmap, context);
3506 isl_basic_map_free(context);
3507 bmap = isl_basic_map_finalize(bmap);
3508 return bmap;
3509 error:
3510 isl_basic_map_free(bmap);
3511 isl_basic_map_free(context);
3512 return NULL;
3515 /* Replace "map" by the disjunct at position "pos" and free "context".
3517 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3518 int pos, __isl_take isl_basic_map *context)
3520 isl_basic_map *bmap;
3522 bmap = isl_basic_map_copy(map->p[pos]);
3523 isl_map_free(map);
3524 isl_basic_map_free(context);
3525 return isl_map_from_basic_map(bmap);
3528 /* Remove the constraints in "context" from "map".
3529 * If any of the disjuncts in the result turns out to be the universe,
3530 * then return this universe.
3531 * "context" is assumed to have explicit representations
3532 * for all local variables.
3534 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3535 __isl_take isl_basic_map *context)
3537 int i;
3538 isl_bool univ, known;
3540 univ = isl_basic_map_plain_is_universe(context);
3541 if (univ < 0)
3542 goto error;
3543 if (univ) {
3544 isl_basic_map_free(context);
3545 return map;
3547 known = isl_basic_map_divs_known(context);
3548 if (known < 0)
3549 goto error;
3550 if (!known)
3551 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3552 "context has unknown divs", goto error);
3554 map = isl_map_cow(map);
3555 if (!map)
3556 goto error;
3557 for (i = 0; i < map->n; ++i) {
3558 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3559 isl_basic_map_copy(context));
3560 univ = isl_basic_map_plain_is_universe(map->p[i]);
3561 if (univ < 0)
3562 goto error;
3563 if (univ && map->n > 1)
3564 return replace_by_disjunct(map, i, context);
3567 isl_basic_map_free(context);
3568 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3569 if (map->n > 1)
3570 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3571 return map;
3572 error:
3573 isl_map_free(map);
3574 isl_basic_map_free(context);
3575 return NULL;
3578 /* Replace "map" by a universe map in the same space and free "drop".
3580 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3581 __isl_take isl_map *drop)
3583 isl_map *res;
3585 res = isl_map_universe(isl_map_get_space(map));
3586 isl_map_free(map);
3587 isl_map_free(drop);
3588 return res;
3591 /* Return a map that has the same intersection with "context" as "map"
3592 * and that is as "simple" as possible.
3594 * If "map" is already the universe, then we cannot make it any simpler.
3595 * Similarly, if "context" is the universe, then we cannot exploit it
3596 * to simplify "map"
3597 * If "map" and "context" are identical to each other, then we can
3598 * return the corresponding universe.
3600 * If either "map" or "context" consists of multiple disjuncts,
3601 * then check if "context" happens to be a subset of "map",
3602 * in which case all constraints can be removed.
3603 * In case of multiple disjuncts, the standard procedure
3604 * may not be able to detect that all constraints can be removed.
3606 * If none of these cases apply, we have to work a bit harder.
3607 * During this computation, we make use of a single disjunct context,
3608 * so if the original context consists of more than one disjunct
3609 * then we need to approximate the context by a single disjunct set.
3610 * Simply taking the simple hull may drop constraints that are
3611 * only implicitly available in each disjunct. We therefore also
3612 * look for constraints among those defining "map" that are valid
3613 * for the context. These can then be used to simplify away
3614 * the corresponding constraints in "map".
3616 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3617 __isl_take isl_map *context)
3619 int equal;
3620 int is_universe;
3621 int single_disjunct_map, single_disjunct_context;
3622 isl_bool subset;
3623 isl_basic_map *hull;
3625 is_universe = isl_map_plain_is_universe(map);
3626 if (is_universe >= 0 && !is_universe)
3627 is_universe = isl_map_plain_is_universe(context);
3628 if (is_universe < 0)
3629 goto error;
3630 if (is_universe) {
3631 isl_map_free(context);
3632 return map;
3635 equal = isl_map_plain_is_equal(map, context);
3636 if (equal < 0)
3637 goto error;
3638 if (equal)
3639 return replace_by_universe(map, context);
3641 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3642 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3643 if (!single_disjunct_map || !single_disjunct_context) {
3644 subset = isl_map_is_subset(context, map);
3645 if (subset < 0)
3646 goto error;
3647 if (subset)
3648 return replace_by_universe(map, context);
3651 context = isl_map_compute_divs(context);
3652 if (!context)
3653 goto error;
3654 if (single_disjunct_context) {
3655 hull = isl_map_simple_hull(context);
3656 } else {
3657 isl_ctx *ctx;
3658 isl_map_list *list;
3660 ctx = isl_map_get_ctx(map);
3661 list = isl_map_list_alloc(ctx, 2);
3662 list = isl_map_list_add(list, isl_map_copy(context));
3663 list = isl_map_list_add(list, isl_map_copy(map));
3664 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3665 list);
3667 return isl_map_gist_basic_map(map, hull);
3668 error:
3669 isl_map_free(map);
3670 isl_map_free(context);
3671 return NULL;
3674 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3675 __isl_take isl_map *context)
3677 return isl_map_align_params_map_map_and(map, context, &map_gist);
3680 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3681 struct isl_basic_set *context)
3683 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3684 bset_to_bmap(context)));
3687 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3688 __isl_take isl_basic_set *context)
3690 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3691 bset_to_bmap(context)));
3694 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3695 __isl_take isl_basic_set *context)
3697 isl_space *space = isl_set_get_space(set);
3698 isl_basic_set *dom_context = isl_basic_set_universe(space);
3699 dom_context = isl_basic_set_intersect_params(dom_context, context);
3700 return isl_set_gist_basic_set(set, dom_context);
3703 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3704 __isl_take isl_set *context)
3706 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3709 /* Compute the gist of "bmap" with respect to the constraints "context"
3710 * on the domain.
3712 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3713 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3715 isl_space *space = isl_basic_map_get_space(bmap);
3716 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3718 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3719 return isl_basic_map_gist(bmap, bmap_context);
3722 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3723 __isl_take isl_set *context)
3725 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3726 map_context = isl_map_intersect_domain(map_context, context);
3727 return isl_map_gist(map, map_context);
3730 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3731 __isl_take isl_set *context)
3733 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3734 map_context = isl_map_intersect_range(map_context, context);
3735 return isl_map_gist(map, map_context);
3738 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3739 __isl_take isl_set *context)
3741 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3742 map_context = isl_map_intersect_params(map_context, context);
3743 return isl_map_gist(map, map_context);
3746 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3747 __isl_take isl_set *context)
3749 return isl_map_gist_params(set, context);
3752 /* Quick check to see if two basic maps are disjoint.
3753 * In particular, we reduce the equalities and inequalities of
3754 * one basic map in the context of the equalities of the other
3755 * basic map and check if we get a contradiction.
3757 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3758 __isl_keep isl_basic_map *bmap2)
3760 struct isl_vec *v = NULL;
3761 int *elim = NULL;
3762 unsigned total;
3763 int i;
3765 if (!bmap1 || !bmap2)
3766 return isl_bool_error;
3767 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3768 return isl_bool_error);
3769 if (bmap1->n_div || bmap2->n_div)
3770 return isl_bool_false;
3771 if (!bmap1->n_eq && !bmap2->n_eq)
3772 return isl_bool_false;
3774 total = isl_space_dim(bmap1->dim, isl_dim_all);
3775 if (total == 0)
3776 return isl_bool_false;
3777 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3778 if (!v)
3779 goto error;
3780 elim = isl_alloc_array(bmap1->ctx, int, total);
3781 if (!elim)
3782 goto error;
3783 compute_elimination_index(bmap1, elim);
3784 for (i = 0; i < bmap2->n_eq; ++i) {
3785 int reduced;
3786 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3787 bmap1, elim);
3788 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3789 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3790 goto disjoint;
3792 for (i = 0; i < bmap2->n_ineq; ++i) {
3793 int reduced;
3794 reduced = reduced_using_equalities(v->block.data,
3795 bmap2->ineq[i], bmap1, elim);
3796 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3797 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3798 goto disjoint;
3800 compute_elimination_index(bmap2, elim);
3801 for (i = 0; i < bmap1->n_ineq; ++i) {
3802 int reduced;
3803 reduced = reduced_using_equalities(v->block.data,
3804 bmap1->ineq[i], bmap2, elim);
3805 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3806 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3807 goto disjoint;
3809 isl_vec_free(v);
3810 free(elim);
3811 return isl_bool_false;
3812 disjoint:
3813 isl_vec_free(v);
3814 free(elim);
3815 return isl_bool_true;
3816 error:
3817 isl_vec_free(v);
3818 free(elim);
3819 return isl_bool_error;
3822 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3823 __isl_keep isl_basic_set *bset2)
3825 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3826 bset_to_bmap(bset2));
3829 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3831 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3832 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3833 __isl_keep isl_basic_map *bmap2))
3835 int i, j;
3837 if (!map1 || !map2)
3838 return isl_bool_error;
3840 for (i = 0; i < map1->n; ++i) {
3841 for (j = 0; j < map2->n; ++j) {
3842 isl_bool d = test(map1->p[i], map2->p[j]);
3843 if (d != isl_bool_true)
3844 return d;
3848 return isl_bool_true;
3851 /* Are "map1" and "map2" obviously disjoint, based on information
3852 * that can be derived without looking at the individual basic maps?
3854 * In particular, if one of them is empty or if they live in different spaces
3855 * (ignoring parameters), then they are clearly disjoint.
3857 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3858 __isl_keep isl_map *map2)
3860 isl_bool disjoint;
3861 isl_bool match;
3863 if (!map1 || !map2)
3864 return isl_bool_error;
3866 disjoint = isl_map_plain_is_empty(map1);
3867 if (disjoint < 0 || disjoint)
3868 return disjoint;
3870 disjoint = isl_map_plain_is_empty(map2);
3871 if (disjoint < 0 || disjoint)
3872 return disjoint;
3874 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3875 map2->dim, isl_dim_in);
3876 if (match < 0 || !match)
3877 return match < 0 ? isl_bool_error : isl_bool_true;
3879 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3880 map2->dim, isl_dim_out);
3881 if (match < 0 || !match)
3882 return match < 0 ? isl_bool_error : isl_bool_true;
3884 return isl_bool_false;
3887 /* Are "map1" and "map2" obviously disjoint?
3889 * If one of them is empty or if they live in different spaces (ignoring
3890 * parameters), then they are clearly disjoint.
3891 * This is checked by isl_map_plain_is_disjoint_global.
3893 * If they have different parameters, then we skip any further tests.
3895 * If they are obviously equal, but not obviously empty, then we will
3896 * not be able to detect if they are disjoint.
3898 * Otherwise we check if each basic map in "map1" is obviously disjoint
3899 * from each basic map in "map2".
3901 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3902 __isl_keep isl_map *map2)
3904 isl_bool disjoint;
3905 isl_bool intersect;
3906 isl_bool match;
3908 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3909 if (disjoint < 0 || disjoint)
3910 return disjoint;
3912 match = isl_space_match(map1->dim, isl_dim_param,
3913 map2->dim, isl_dim_param);
3914 if (match < 0 || !match)
3915 return match < 0 ? isl_bool_error : isl_bool_false;
3917 intersect = isl_map_plain_is_equal(map1, map2);
3918 if (intersect < 0 || intersect)
3919 return intersect < 0 ? isl_bool_error : isl_bool_false;
3921 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3924 /* Are "map1" and "map2" disjoint?
3926 * They are disjoint if they are "obviously disjoint" or if one of them
3927 * is empty. Otherwise, they are not disjoint if one of them is universal.
3928 * If the two inputs are (obviously) equal and not empty, then they are
3929 * not disjoint.
3930 * If none of these cases apply, then check if all pairs of basic maps
3931 * are disjoint.
3933 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3935 isl_bool disjoint;
3936 isl_bool intersect;
3938 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3939 if (disjoint < 0 || disjoint)
3940 return disjoint;
3942 disjoint = isl_map_is_empty(map1);
3943 if (disjoint < 0 || disjoint)
3944 return disjoint;
3946 disjoint = isl_map_is_empty(map2);
3947 if (disjoint < 0 || disjoint)
3948 return disjoint;
3950 intersect = isl_map_plain_is_universe(map1);
3951 if (intersect < 0 || intersect)
3952 return intersect < 0 ? isl_bool_error : isl_bool_false;
3954 intersect = isl_map_plain_is_universe(map2);
3955 if (intersect < 0 || intersect)
3956 return intersect < 0 ? isl_bool_error : isl_bool_false;
3958 intersect = isl_map_plain_is_equal(map1, map2);
3959 if (intersect < 0 || intersect)
3960 return isl_bool_not(intersect);
3962 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3965 /* Are "bmap1" and "bmap2" disjoint?
3967 * They are disjoint if they are "obviously disjoint" or if one of them
3968 * is empty. Otherwise, they are not disjoint if one of them is universal.
3969 * If none of these cases apply, we compute the intersection and see if
3970 * the result is empty.
3972 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3973 __isl_keep isl_basic_map *bmap2)
3975 isl_bool disjoint;
3976 isl_bool intersect;
3977 isl_basic_map *test;
3979 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3980 if (disjoint < 0 || disjoint)
3981 return disjoint;
3983 disjoint = isl_basic_map_is_empty(bmap1);
3984 if (disjoint < 0 || disjoint)
3985 return disjoint;
3987 disjoint = isl_basic_map_is_empty(bmap2);
3988 if (disjoint < 0 || disjoint)
3989 return disjoint;
3991 intersect = isl_basic_map_plain_is_universe(bmap1);
3992 if (intersect < 0 || intersect)
3993 return intersect < 0 ? isl_bool_error : isl_bool_false;
3995 intersect = isl_basic_map_plain_is_universe(bmap2);
3996 if (intersect < 0 || intersect)
3997 return intersect < 0 ? isl_bool_error : isl_bool_false;
3999 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
4000 isl_basic_map_copy(bmap2));
4001 disjoint = isl_basic_map_is_empty(test);
4002 isl_basic_map_free(test);
4004 return disjoint;
4007 /* Are "bset1" and "bset2" disjoint?
4009 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
4010 __isl_keep isl_basic_set *bset2)
4012 return isl_basic_map_is_disjoint(bset1, bset2);
4015 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
4016 __isl_keep isl_set *set2)
4018 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
4021 /* Are "set1" and "set2" disjoint?
4023 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4025 return isl_map_is_disjoint(set1, set2);
4028 /* Is "v" equal to 0, 1 or -1?
4030 static int is_zero_or_one(isl_int v)
4032 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
4035 /* Check if we can combine a given div with lower bound l and upper
4036 * bound u with some other div and if so return that other div.
4037 * Otherwise return -1.
4039 * We first check that
4040 * - the bounds are opposites of each other (except for the constant
4041 * term)
4042 * - the bounds do not reference any other div
4043 * - no div is defined in terms of this div
4045 * Let m be the size of the range allowed on the div by the bounds.
4046 * That is, the bounds are of the form
4048 * e <= a <= e + m - 1
4050 * with e some expression in the other variables.
4051 * We look for another div b such that no third div is defined in terms
4052 * of this second div b and such that in any constraint that contains
4053 * a (except for the given lower and upper bound), also contains b
4054 * with a coefficient that is m times that of b.
4055 * That is, all constraints (execpt for the lower and upper bound)
4056 * are of the form
4058 * e + f (a + m b) >= 0
4060 * Furthermore, in the constraints that only contain b, the coefficient
4061 * of b should be equal to 1 or -1.
4062 * If so, we return b so that "a + m b" can be replaced by
4063 * a single div "c = a + m b".
4065 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
4066 unsigned div, unsigned l, unsigned u)
4068 int i, j;
4069 unsigned dim;
4070 int coalesce = -1;
4072 if (bmap->n_div <= 1)
4073 return -1;
4074 dim = isl_space_dim(bmap->dim, isl_dim_all);
4075 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
4076 return -1;
4077 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
4078 bmap->n_div - div - 1) != -1)
4079 return -1;
4080 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
4081 dim + bmap->n_div))
4082 return -1;
4084 for (i = 0; i < bmap->n_div; ++i) {
4085 if (isl_int_is_zero(bmap->div[i][0]))
4086 continue;
4087 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
4088 return -1;
4091 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4092 if (isl_int_is_neg(bmap->ineq[l][0])) {
4093 isl_int_sub(bmap->ineq[l][0],
4094 bmap->ineq[l][0], bmap->ineq[u][0]);
4095 bmap = isl_basic_map_copy(bmap);
4096 bmap = isl_basic_map_set_to_empty(bmap);
4097 isl_basic_map_free(bmap);
4098 return -1;
4100 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4101 for (i = 0; i < bmap->n_div; ++i) {
4102 if (i == div)
4103 continue;
4104 if (!pairs[i])
4105 continue;
4106 for (j = 0; j < bmap->n_div; ++j) {
4107 if (isl_int_is_zero(bmap->div[j][0]))
4108 continue;
4109 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
4110 break;
4112 if (j < bmap->n_div)
4113 continue;
4114 for (j = 0; j < bmap->n_ineq; ++j) {
4115 int valid;
4116 if (j == l || j == u)
4117 continue;
4118 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
4119 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
4120 continue;
4121 break;
4123 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
4124 break;
4125 isl_int_mul(bmap->ineq[j][1 + dim + div],
4126 bmap->ineq[j][1 + dim + div],
4127 bmap->ineq[l][0]);
4128 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
4129 bmap->ineq[j][1 + dim + i]);
4130 isl_int_divexact(bmap->ineq[j][1 + dim + div],
4131 bmap->ineq[j][1 + dim + div],
4132 bmap->ineq[l][0]);
4133 if (!valid)
4134 break;
4136 if (j < bmap->n_ineq)
4137 continue;
4138 coalesce = i;
4139 break;
4141 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4142 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4143 return coalesce;
4146 /* Internal data structure used during the construction and/or evaluation of
4147 * an inequality that ensures that a pair of bounds always allows
4148 * for an integer value.
4150 * "tab" is the tableau in which the inequality is evaluated. It may
4151 * be NULL until it is actually needed.
4152 * "v" contains the inequality coefficients.
4153 * "g", "fl" and "fu" are temporary scalars used during the construction and
4154 * evaluation.
4156 struct test_ineq_data {
4157 struct isl_tab *tab;
4158 isl_vec *v;
4159 isl_int g;
4160 isl_int fl;
4161 isl_int fu;
4164 /* Free all the memory allocated by the fields of "data".
4166 static void test_ineq_data_clear(struct test_ineq_data *data)
4168 isl_tab_free(data->tab);
4169 isl_vec_free(data->v);
4170 isl_int_clear(data->g);
4171 isl_int_clear(data->fl);
4172 isl_int_clear(data->fu);
4175 /* Is the inequality stored in data->v satisfied by "bmap"?
4176 * That is, does it only attain non-negative values?
4177 * data->tab is a tableau corresponding to "bmap".
4179 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4180 struct test_ineq_data *data)
4182 isl_ctx *ctx;
4183 enum isl_lp_result res;
4185 ctx = isl_basic_map_get_ctx(bmap);
4186 if (!data->tab)
4187 data->tab = isl_tab_from_basic_map(bmap, 0);
4188 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4189 if (res == isl_lp_error)
4190 return isl_bool_error;
4191 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4194 /* Given a lower and an upper bound on div i, do they always allow
4195 * for an integer value of the given div?
4196 * Determine this property by constructing an inequality
4197 * such that the property is guaranteed when the inequality is nonnegative.
4198 * The lower bound is inequality l, while the upper bound is inequality u.
4199 * The constructed inequality is stored in data->v.
4201 * Let the upper bound be
4203 * -n_u a + e_u >= 0
4205 * and the lower bound
4207 * n_l a + e_l >= 0
4209 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4210 * We have
4212 * - f_u e_l <= f_u f_l g a <= f_l e_u
4214 * Since all variables are integer valued, this is equivalent to
4216 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4218 * If this interval is at least f_u f_l g, then it contains at least
4219 * one integer value for a.
4220 * That is, the test constraint is
4222 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4224 * or
4226 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4228 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4229 * then the constraint can be scaled down by a factor g',
4230 * with the constant term replaced by
4231 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4232 * Note that the result of applying Fourier-Motzkin to this pair
4233 * of constraints is
4235 * f_l e_u + f_u e_l >= 0
4237 * If the constant term of the scaled down version of this constraint,
4238 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4239 * term of the scaled down test constraint, then the test constraint
4240 * is known to hold and no explicit evaluation is required.
4241 * This is essentially the Omega test.
4243 * If the test constraint consists of only a constant term, then
4244 * it is sufficient to look at the sign of this constant term.
4246 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4247 int l, int u, struct test_ineq_data *data)
4249 unsigned offset, n_div;
4250 offset = isl_basic_map_offset(bmap, isl_dim_div);
4251 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4253 isl_int_gcd(data->g,
4254 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4255 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4256 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4257 isl_int_neg(data->fu, data->fu);
4258 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4259 data->fu, bmap->ineq[l], offset + n_div);
4260 isl_int_mul(data->g, data->g, data->fl);
4261 isl_int_mul(data->g, data->g, data->fu);
4262 isl_int_sub(data->g, data->g, data->fl);
4263 isl_int_sub(data->g, data->g, data->fu);
4264 isl_int_add_ui(data->g, data->g, 1);
4265 isl_int_sub(data->fl, data->v->el[0], data->g);
4267 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4268 if (isl_int_is_zero(data->g))
4269 return isl_int_is_nonneg(data->fl);
4270 if (isl_int_is_one(data->g)) {
4271 isl_int_set(data->v->el[0], data->fl);
4272 return test_ineq_is_satisfied(bmap, data);
4274 isl_int_fdiv_q(data->fl, data->fl, data->g);
4275 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4276 if (isl_int_eq(data->fl, data->v->el[0]))
4277 return isl_bool_true;
4278 isl_int_set(data->v->el[0], data->fl);
4279 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4280 offset - 1 + n_div);
4282 return test_ineq_is_satisfied(bmap, data);
4285 /* Remove more kinds of divs that are not strictly needed.
4286 * In particular, if all pairs of lower and upper bounds on a div
4287 * are such that they allow at least one integer value of the div,
4288 * then we can eliminate the div using Fourier-Motzkin without
4289 * introducing any spurious solutions.
4291 * If at least one of the two constraints has a unit coefficient for the div,
4292 * then the presence of such a value is guaranteed so there is no need to check.
4293 * In particular, the value attained by the bound with unit coefficient
4294 * can serve as this intermediate value.
4296 static struct isl_basic_map *drop_more_redundant_divs(
4297 struct isl_basic_map *bmap, int *pairs, int n)
4299 isl_ctx *ctx;
4300 struct test_ineq_data data = { NULL, NULL };
4301 unsigned off, n_div;
4302 int remove = -1;
4304 isl_int_init(data.g);
4305 isl_int_init(data.fl);
4306 isl_int_init(data.fu);
4308 if (!bmap)
4309 goto error;
4311 ctx = isl_basic_map_get_ctx(bmap);
4312 off = isl_basic_map_offset(bmap, isl_dim_div);
4313 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4314 data.v = isl_vec_alloc(ctx, off + n_div);
4315 if (!data.v)
4316 goto error;
4318 while (n > 0) {
4319 int i, l, u;
4320 int best = -1;
4321 isl_bool has_int;
4323 for (i = 0; i < n_div; ++i) {
4324 if (!pairs[i])
4325 continue;
4326 if (best >= 0 && pairs[best] <= pairs[i])
4327 continue;
4328 best = i;
4331 i = best;
4332 for (l = 0; l < bmap->n_ineq; ++l) {
4333 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4334 continue;
4335 if (isl_int_is_one(bmap->ineq[l][off + i]))
4336 continue;
4337 for (u = 0; u < bmap->n_ineq; ++u) {
4338 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4339 continue;
4340 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4341 continue;
4342 has_int = int_between_bounds(bmap, i, l, u,
4343 &data);
4344 if (has_int < 0)
4345 goto error;
4346 if (data.tab && data.tab->empty)
4347 break;
4348 if (!has_int)
4349 break;
4351 if (u < bmap->n_ineq)
4352 break;
4354 if (data.tab && data.tab->empty) {
4355 bmap = isl_basic_map_set_to_empty(bmap);
4356 break;
4358 if (l == bmap->n_ineq) {
4359 remove = i;
4360 break;
4362 pairs[i] = 0;
4363 --n;
4366 test_ineq_data_clear(&data);
4368 free(pairs);
4370 if (remove < 0)
4371 return bmap;
4373 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4374 return isl_basic_map_drop_redundant_divs(bmap);
4375 error:
4376 free(pairs);
4377 isl_basic_map_free(bmap);
4378 test_ineq_data_clear(&data);
4379 return NULL;
4382 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4383 * and the upper bound u, div1 always occurs together with div2 in the form
4384 * (div1 + m div2), where m is the constant range on the variable div1
4385 * allowed by l and u, replace the pair div1 and div2 by a single
4386 * div that is equal to div1 + m div2.
4388 * The new div will appear in the location that contains div2.
4389 * We need to modify all constraints that contain
4390 * div2 = (div - div1) / m
4391 * The coefficient of div2 is known to be equal to 1 or -1.
4392 * (If a constraint does not contain div2, it will also not contain div1.)
4393 * If the constraint also contains div1, then we know they appear
4394 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4395 * i.e., the coefficient of div is f.
4397 * Otherwise, we first need to introduce div1 into the constraint.
4398 * Let the l be
4400 * div1 + f >=0
4402 * and u
4404 * -div1 + f' >= 0
4406 * A lower bound on div2
4408 * div2 + t >= 0
4410 * can be replaced by
4412 * m div2 + div1 + m t + f >= 0
4414 * An upper bound
4416 * -div2 + t >= 0
4418 * can be replaced by
4420 * -(m div2 + div1) + m t + f' >= 0
4422 * These constraint are those that we would obtain from eliminating
4423 * div1 using Fourier-Motzkin.
4425 * After all constraints have been modified, we drop the lower and upper
4426 * bound and then drop div1.
4428 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4429 unsigned div1, unsigned div2, unsigned l, unsigned u)
4431 isl_ctx *ctx;
4432 isl_int m;
4433 unsigned dim, total;
4434 int i;
4436 ctx = isl_basic_map_get_ctx(bmap);
4438 dim = isl_space_dim(bmap->dim, isl_dim_all);
4439 total = 1 + dim + bmap->n_div;
4441 isl_int_init(m);
4442 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4443 isl_int_add_ui(m, m, 1);
4445 for (i = 0; i < bmap->n_ineq; ++i) {
4446 if (i == l || i == u)
4447 continue;
4448 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4449 continue;
4450 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4451 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4452 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4453 ctx->one, bmap->ineq[l], total);
4454 else
4455 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4456 ctx->one, bmap->ineq[u], total);
4458 isl_int_set(bmap->ineq[i][1 + dim + div2],
4459 bmap->ineq[i][1 + dim + div1]);
4460 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4463 isl_int_clear(m);
4464 if (l > u) {
4465 isl_basic_map_drop_inequality(bmap, l);
4466 isl_basic_map_drop_inequality(bmap, u);
4467 } else {
4468 isl_basic_map_drop_inequality(bmap, u);
4469 isl_basic_map_drop_inequality(bmap, l);
4471 bmap = isl_basic_map_drop_div(bmap, div1);
4472 return bmap;
4475 /* First check if we can coalesce any pair of divs and
4476 * then continue with dropping more redundant divs.
4478 * We loop over all pairs of lower and upper bounds on a div
4479 * with coefficient 1 and -1, respectively, check if there
4480 * is any other div "c" with which we can coalesce the div
4481 * and if so, perform the coalescing.
4483 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4484 struct isl_basic_map *bmap, int *pairs, int n)
4486 int i, l, u;
4487 unsigned dim;
4489 dim = isl_space_dim(bmap->dim, isl_dim_all);
4491 for (i = 0; i < bmap->n_div; ++i) {
4492 if (!pairs[i])
4493 continue;
4494 for (l = 0; l < bmap->n_ineq; ++l) {
4495 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4496 continue;
4497 for (u = 0; u < bmap->n_ineq; ++u) {
4498 int c;
4500 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4501 continue;
4502 c = div_find_coalesce(bmap, pairs, i, l, u);
4503 if (c < 0)
4504 continue;
4505 free(pairs);
4506 bmap = coalesce_divs(bmap, i, c, l, u);
4507 return isl_basic_map_drop_redundant_divs(bmap);
4512 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4513 return bmap;
4515 return drop_more_redundant_divs(bmap, pairs, n);
4518 /* Are the "n" coefficients starting at "first" of inequality constraints
4519 * "i" and "j" of "bmap" equal to each other?
4521 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4522 int first, int n)
4524 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4527 /* Are the "n" coefficients starting at "first" of inequality constraints
4528 * "i" and "j" of "bmap" opposite to each other?
4530 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4531 int first, int n)
4533 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4536 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4537 * apart from the constant term?
4539 static int is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4541 unsigned total;
4543 total = isl_basic_map_dim(bmap, isl_dim_all);
4544 return is_opposite_part(bmap, i, j, 1, total);
4547 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4548 * apart from the constant term and the coefficient at position "pos"?
4550 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4551 int pos)
4553 unsigned total;
4555 total = isl_basic_map_dim(bmap, isl_dim_all);
4556 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4557 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4560 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4561 * apart from the constant term and the coefficient at position "pos"?
4563 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4564 int pos)
4566 unsigned total;
4568 total = isl_basic_map_dim(bmap, isl_dim_all);
4569 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4570 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4573 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4574 * been modified, simplying it if "simplify" is set.
4575 * Free the temporary data structure "pairs" that was associated
4576 * to the old version of "bmap".
4578 static __isl_give isl_basic_map *drop_redundant_divs_again(
4579 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4581 if (simplify)
4582 bmap = isl_basic_map_simplify(bmap);
4583 free(pairs);
4584 return isl_basic_map_drop_redundant_divs(bmap);
4587 /* Is "div" the single unknown existentially quantified variable
4588 * in inequality constraint "ineq" of "bmap"?
4589 * "div" is known to have a non-zero coefficient in "ineq".
4591 static int single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
4593 int i;
4594 unsigned n_div, o_div;
4596 if (isl_basic_map_div_is_known(bmap, div))
4597 return 0;
4598 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4599 if (n_div == 1)
4600 return 1;
4601 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4602 for (i = 0; i < n_div; ++i) {
4603 if (i == div)
4604 continue;
4605 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4606 continue;
4607 if (!isl_basic_map_div_is_known(bmap, i))
4608 return 0;
4611 return 1;
4614 /* Does integer division "div" have coefficient 1 in inequality constraint
4615 * "ineq" of "map"?
4617 static int has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4619 unsigned o_div;
4621 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4622 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4623 return 1;
4625 return 0;
4628 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4629 * then try and drop redundant divs again,
4630 * freeing the temporary data structure "pairs" that was associated
4631 * to the old version of "bmap".
4633 static __isl_give isl_basic_map *set_eq_and_try_again(
4634 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4636 bmap = isl_basic_map_cow(bmap);
4637 isl_basic_map_inequality_to_equality(bmap, ineq);
4638 return drop_redundant_divs_again(bmap, pairs, 1);
4641 /* Drop the integer division at position "div", along with the two
4642 * inequality constraints "ineq1" and "ineq2" in which it appears
4643 * from "bmap" and then try and drop redundant divs again,
4644 * freeing the temporary data structure "pairs" that was associated
4645 * to the old version of "bmap".
4647 static __isl_give isl_basic_map *drop_div_and_try_again(
4648 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4649 __isl_take int *pairs)
4651 if (ineq1 > ineq2) {
4652 isl_basic_map_drop_inequality(bmap, ineq1);
4653 isl_basic_map_drop_inequality(bmap, ineq2);
4654 } else {
4655 isl_basic_map_drop_inequality(bmap, ineq2);
4656 isl_basic_map_drop_inequality(bmap, ineq1);
4658 bmap = isl_basic_map_drop_div(bmap, div);
4659 return drop_redundant_divs_again(bmap, pairs, 0);
4662 /* Given two inequality constraints
4664 * f(x) + n d + c >= 0, (ineq)
4666 * with d the variable at position "pos", and
4668 * f(x) + c0 >= 0, (lower)
4670 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4671 * determined by the first constraint.
4672 * That is, store
4674 * ceil((c0 - c)/n)
4676 * in *l.
4678 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4679 int ineq, int lower, int pos, isl_int *l)
4681 isl_int_neg(*l, bmap->ineq[ineq][0]);
4682 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4683 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4686 /* Given two inequality constraints
4688 * f(x) + n d + c >= 0, (ineq)
4690 * with d the variable at position "pos", and
4692 * -f(x) - c0 >= 0, (upper)
4694 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4695 * determined by the first constraint.
4696 * That is, store
4698 * ceil((-c1 - c)/n)
4700 * in *u.
4702 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4703 int ineq, int upper, int pos, isl_int *u)
4705 isl_int_neg(*u, bmap->ineq[ineq][0]);
4706 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4707 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4710 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4711 * does the corresponding lower bound have a fixed value in "bmap"?
4713 * In particular, "ineq" is of the form
4715 * f(x) + n d + c >= 0
4717 * with n > 0, c the constant term and
4718 * d the existentially quantified variable "div".
4719 * That is, the lower bound is
4721 * ceil((-f(x) - c)/n)
4723 * Look for a pair of constraints
4725 * f(x) + c0 >= 0
4726 * -f(x) + c1 >= 0
4728 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4729 * That is, check that
4731 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4733 * If so, return the index of inequality f(x) + c0 >= 0.
4734 * Otherwise, return -1.
4736 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4738 int i;
4739 int lower = -1, upper = -1;
4740 unsigned o_div, n_div;
4741 isl_int l, u;
4742 int equal;
4744 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4745 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4746 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4747 if (i == ineq)
4748 continue;
4749 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4750 continue;
4751 if (lower < 0 &&
4752 is_parallel_except(bmap, ineq, i, o_div + div)) {
4753 lower = i;
4754 continue;
4756 if (upper < 0 &&
4757 is_opposite_except(bmap, ineq, i, o_div + div)) {
4758 upper = i;
4762 if (lower < 0 || upper < 0)
4763 return -1;
4765 isl_int_init(l);
4766 isl_int_init(u);
4768 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4769 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4771 equal = isl_int_eq(l, u);
4773 isl_int_clear(l);
4774 isl_int_clear(u);
4776 return equal ? lower : -1;
4779 /* Given a lower bound constraint "ineq" on the existentially quantified
4780 * variable "div", such that the corresponding lower bound has
4781 * a fixed value in "bmap", assign this fixed value to the variable and
4782 * then try and drop redundant divs again,
4783 * freeing the temporary data structure "pairs" that was associated
4784 * to the old version of "bmap".
4785 * "lower" determines the constant value for the lower bound.
4787 * In particular, "ineq" is of the form
4789 * f(x) + n d + c >= 0,
4791 * while "lower" is of the form
4793 * f(x) + c0 >= 0
4795 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4796 * is ceil((c0 - c)/n).
4798 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4799 int div, int ineq, int lower, int *pairs)
4801 isl_int c;
4802 unsigned o_div;
4804 isl_int_init(c);
4806 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4807 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4808 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4809 free(pairs);
4811 isl_int_clear(c);
4813 return isl_basic_map_drop_redundant_divs(bmap);
4816 /* Remove divs that are not strictly needed based on the inequality
4817 * constraints.
4818 * In particular, if a div only occurs positively (or negatively)
4819 * in constraints, then it can simply be dropped.
4820 * Also, if a div occurs in only two constraints and if moreover
4821 * those two constraints are opposite to each other, except for the constant
4822 * term and if the sum of the constant terms is such that for any value
4823 * of the other values, there is always at least one integer value of the
4824 * div, i.e., if one plus this sum is greater than or equal to
4825 * the (absolute value) of the coefficient of the div in the constraints,
4826 * then we can also simply drop the div.
4828 * If an existentially quantified variable does not have an explicit
4829 * representation, appears in only a single lower bound that does not
4830 * involve any other such existentially quantified variables and appears
4831 * in this lower bound with coefficient 1,
4832 * then fix the variable to the value of the lower bound. That is,
4833 * turn the inequality into an equality.
4834 * If for any value of the other variables, there is any value
4835 * for the existentially quantified variable satisfying the constraints,
4836 * then this lower bound also satisfies the constraints.
4837 * It is therefore safe to pick this lower bound.
4839 * The same reasoning holds even if the coefficient is not one.
4840 * However, fixing the variable to the value of the lower bound may
4841 * in general introduce an extra integer division, in which case
4842 * it may be better to pick another value.
4843 * If this integer division has a known constant value, then plugging
4844 * in this constant value removes the existentially quantified variable
4845 * completely. In particular, if the lower bound is of the form
4846 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4847 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4848 * then the existentially quantified variable can be assigned this
4849 * shared value.
4851 * We skip divs that appear in equalities or in the definition of other divs.
4852 * Divs that appear in the definition of other divs usually occur in at least
4853 * 4 constraints, but the constraints may have been simplified.
4855 * If any divs are left after these simple checks then we move on
4856 * to more complicated cases in drop_more_redundant_divs.
4858 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4859 __isl_take isl_basic_map *bmap)
4861 int i, j;
4862 unsigned off;
4863 int *pairs = NULL;
4864 int n = 0;
4866 if (!bmap)
4867 goto error;
4868 if (bmap->n_div == 0)
4869 return bmap;
4871 off = isl_space_dim(bmap->dim, isl_dim_all);
4872 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4873 if (!pairs)
4874 goto error;
4876 for (i = 0; i < bmap->n_div; ++i) {
4877 int pos, neg;
4878 int last_pos, last_neg;
4879 int redundant;
4880 int defined;
4882 defined = !isl_int_is_zero(bmap->div[i][0]);
4883 for (j = i; j < bmap->n_div; ++j)
4884 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4885 break;
4886 if (j < bmap->n_div)
4887 continue;
4888 for (j = 0; j < bmap->n_eq; ++j)
4889 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4890 break;
4891 if (j < bmap->n_eq)
4892 continue;
4893 ++n;
4894 pos = neg = 0;
4895 for (j = 0; j < bmap->n_ineq; ++j) {
4896 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4897 last_pos = j;
4898 ++pos;
4900 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4901 last_neg = j;
4902 ++neg;
4905 pairs[i] = pos * neg;
4906 if (pairs[i] == 0) {
4907 for (j = bmap->n_ineq - 1; j >= 0; --j)
4908 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4909 isl_basic_map_drop_inequality(bmap, j);
4910 bmap = isl_basic_map_drop_div(bmap, i);
4911 return drop_redundant_divs_again(bmap, pairs, 0);
4913 if (pairs[i] != 1 || !is_opposite(bmap, last_pos, last_neg)) {
4914 int single, lower;
4915 if (pos != 1)
4916 continue;
4917 single = single_unknown(bmap, last_pos, i);
4918 if (!single)
4919 continue;
4920 if (has_coef_one(bmap, i, last_pos))
4921 return set_eq_and_try_again(bmap, last_pos,
4922 pairs);
4923 lower = lower_bound_is_cst(bmap, i, last_pos);
4924 if (lower >= 0)
4925 return fix_cst_lower(bmap, i, last_pos, lower,
4926 pairs);
4927 continue;
4930 isl_int_add(bmap->ineq[last_pos][0],
4931 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4932 isl_int_add_ui(bmap->ineq[last_pos][0],
4933 bmap->ineq[last_pos][0], 1);
4934 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4935 bmap->ineq[last_pos][1+off+i]);
4936 isl_int_sub_ui(bmap->ineq[last_pos][0],
4937 bmap->ineq[last_pos][0], 1);
4938 isl_int_sub(bmap->ineq[last_pos][0],
4939 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4940 if (redundant)
4941 return drop_div_and_try_again(bmap, i,
4942 last_pos, last_neg, pairs);
4943 if (!defined && ok_to_set_div_from_bound(bmap, i, last_pos)) {
4944 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4945 return drop_redundant_divs_again(bmap, pairs, 1);
4947 pairs[i] = 0;
4948 --n;
4951 if (n > 0)
4952 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4954 free(pairs);
4955 return bmap;
4956 error:
4957 free(pairs);
4958 isl_basic_map_free(bmap);
4959 return NULL;
4962 /* Consider the coefficients at "c" as a row vector and replace
4963 * them with their product with "T". "T" is assumed to be a square matrix.
4965 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4967 int n;
4968 isl_ctx *ctx;
4969 isl_vec *v;
4971 if (!T)
4972 return isl_stat_error;
4973 n = isl_mat_rows(T);
4974 if (isl_seq_first_non_zero(c, n) == -1)
4975 return isl_stat_ok;
4976 ctx = isl_mat_get_ctx(T);
4977 v = isl_vec_alloc(ctx, n);
4978 if (!v)
4979 return isl_stat_error;
4980 isl_seq_swp_or_cpy(v->el, c, n);
4981 v = isl_vec_mat_product(v, isl_mat_copy(T));
4982 if (!v)
4983 return isl_stat_error;
4984 isl_seq_swp_or_cpy(c, v->el, n);
4985 isl_vec_free(v);
4987 return isl_stat_ok;
4990 /* Plug in T for the variables in "bmap" starting at "pos".
4991 * T is a linear unimodular matrix, i.e., without constant term.
4993 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4994 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4996 int i;
4997 unsigned n, total;
4999 bmap = isl_basic_map_cow(bmap);
5000 if (!bmap || !T)
5001 goto error;
5003 n = isl_mat_cols(T);
5004 if (n != isl_mat_rows(T))
5005 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5006 "expecting square matrix", goto error);
5008 total = isl_basic_map_dim(bmap, isl_dim_all);
5009 if (pos + n > total || pos + n < pos)
5010 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
5011 "invalid range", goto error);
5013 for (i = 0; i < bmap->n_eq; ++i)
5014 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
5015 goto error;
5016 for (i = 0; i < bmap->n_ineq; ++i)
5017 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
5018 goto error;
5019 for (i = 0; i < bmap->n_div; ++i) {
5020 if (isl_basic_map_div_is_marked_unknown(bmap, i))
5021 continue;
5022 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
5023 goto error;
5026 isl_mat_free(T);
5027 return bmap;
5028 error:
5029 isl_basic_map_free(bmap);
5030 isl_mat_free(T);
5031 return NULL;
5034 /* Remove divs that are not strictly needed.
5036 * First look for an equality constraint involving two or more
5037 * existentially quantified variables without an explicit
5038 * representation. Replace the combination that appears
5039 * in the equality constraint by a single existentially quantified
5040 * variable such that the equality can be used to derive
5041 * an explicit representation for the variable.
5042 * If there are no more such equality constraints, then continue
5043 * with isl_basic_map_drop_redundant_divs_ineq.
5045 * In particular, if the equality constraint is of the form
5047 * f(x) + \sum_i c_i a_i = 0
5049 * with a_i existentially quantified variable without explicit
5050 * representation, then apply a transformation on the existentially
5051 * quantified variables to turn the constraint into
5053 * f(x) + g a_1' = 0
5055 * with g the gcd of the c_i.
5056 * In order to easily identify which existentially quantified variables
5057 * have a complete explicit representation, i.e., without being defined
5058 * in terms of other existentially quantified variables without
5059 * an explicit representation, the existentially quantified variables
5060 * are first sorted.
5062 * The variable transformation is computed by extending the row
5063 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5065 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5066 * [a_2'] [ a_2 ]
5067 * ... = U ....
5068 * [a_n'] [ a_n ]
5070 * with [c_1/g ... c_n/g] representing the first row of U.
5071 * The inverse of U is then plugged into the original constraints.
5072 * The call to isl_basic_map_simplify makes sure the explicit
5073 * representation for a_1' is extracted from the equality constraint.
5075 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5076 __isl_take isl_basic_map *bmap)
5078 int first;
5079 int i;
5080 unsigned o_div, n_div;
5081 int l;
5082 isl_ctx *ctx;
5083 isl_mat *T;
5085 if (!bmap)
5086 return NULL;
5087 if (isl_basic_map_divs_known(bmap))
5088 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5089 if (bmap->n_eq == 0)
5090 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5091 bmap = isl_basic_map_sort_divs(bmap);
5092 if (!bmap)
5093 return NULL;
5095 first = isl_basic_map_first_unknown_div(bmap);
5096 if (first < 0)
5097 return isl_basic_map_free(bmap);
5099 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5100 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5102 for (i = 0; i < bmap->n_eq; ++i) {
5103 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5104 n_div - (first));
5105 if (l < 0)
5106 continue;
5107 l += first;
5108 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5109 n_div - (l + 1)) == -1)
5110 continue;
5111 break;
5113 if (i >= bmap->n_eq)
5114 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5116 ctx = isl_basic_map_get_ctx(bmap);
5117 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5118 if (!T)
5119 return isl_basic_map_free(bmap);
5120 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5121 T = isl_mat_normalize_row(T, 0);
5122 T = isl_mat_unimodular_complete(T, 1);
5123 T = isl_mat_right_inverse(T);
5125 for (i = l; i < n_div; ++i)
5126 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5127 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5128 bmap = isl_basic_map_simplify(bmap);
5130 return isl_basic_map_drop_redundant_divs(bmap);
5133 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
5134 struct isl_basic_set *bset)
5136 isl_basic_map *bmap = bset_to_bmap(bset);
5137 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap));
5140 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
5142 int i;
5144 if (!map)
5145 return NULL;
5146 for (i = 0; i < map->n; ++i) {
5147 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
5148 if (!map->p[i])
5149 goto error;
5151 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5152 return map;
5153 error:
5154 isl_map_free(map);
5155 return NULL;
5158 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
5160 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set)));
5163 /* Does "bmap" satisfy any equality that involves more than 2 variables
5164 * and/or has coefficients different from -1 and 1?
5166 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5168 int i;
5169 unsigned total;
5171 total = isl_basic_map_dim(bmap, isl_dim_all);
5173 for (i = 0; i < bmap->n_eq; ++i) {
5174 int j, k;
5176 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5177 if (j < 0)
5178 continue;
5179 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5180 !isl_int_is_negone(bmap->eq[i][1 + j]))
5181 return 1;
5183 j += 1;
5184 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5185 if (k < 0)
5186 continue;
5187 j += k;
5188 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5189 !isl_int_is_negone(bmap->eq[i][1 + j]))
5190 return 1;
5192 j += 1;
5193 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5194 if (k >= 0)
5195 return 1;
5198 return 0;
5201 /* Remove any common factor g from the constraint coefficients in "v".
5202 * The constant term is stored in the first position and is replaced
5203 * by floor(c/g). If any common factor is removed and if this results
5204 * in a tightening of the constraint, then set *tightened.
5206 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5207 int *tightened)
5209 isl_ctx *ctx;
5211 if (!v)
5212 return NULL;
5213 ctx = isl_vec_get_ctx(v);
5214 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5215 if (isl_int_is_zero(ctx->normalize_gcd))
5216 return v;
5217 if (isl_int_is_one(ctx->normalize_gcd))
5218 return v;
5219 v = isl_vec_cow(v);
5220 if (!v)
5221 return NULL;
5222 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5223 *tightened = 1;
5224 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5225 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5226 v->size - 1);
5227 return v;
5230 /* If "bmap" is an integer set that satisfies any equality involving
5231 * more than 2 variables and/or has coefficients different from -1 and 1,
5232 * then use variable compression to reduce the coefficients by removing
5233 * any (hidden) common factor.
5234 * In particular, apply the variable compression to each constraint,
5235 * factor out any common factor in the non-constant coefficients and
5236 * then apply the inverse of the compression.
5237 * At the end, we mark the basic map as having reduced constants.
5238 * If this flag is still set on the next invocation of this function,
5239 * then we skip the computation.
5241 * Removing a common factor may result in a tightening of some of
5242 * the constraints. If this happens, then we may end up with two
5243 * opposite inequalities that can be replaced by an equality.
5244 * We therefore call isl_basic_map_detect_inequality_pairs,
5245 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5246 * and isl_basic_map_gauss if such a pair was found.
5248 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5249 __isl_take isl_basic_map *bmap)
5251 unsigned total;
5252 isl_ctx *ctx;
5253 isl_vec *v;
5254 isl_mat *eq, *T, *T2;
5255 int i;
5256 int tightened;
5258 if (!bmap)
5259 return NULL;
5260 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5261 return bmap;
5262 if (isl_basic_map_is_rational(bmap))
5263 return bmap;
5264 if (bmap->n_eq == 0)
5265 return bmap;
5266 if (!has_multiple_var_equality(bmap))
5267 return bmap;
5269 total = isl_basic_map_dim(bmap, isl_dim_all);
5270 ctx = isl_basic_map_get_ctx(bmap);
5271 v = isl_vec_alloc(ctx, 1 + total);
5272 if (!v)
5273 return isl_basic_map_free(bmap);
5275 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5276 T = isl_mat_variable_compression(eq, &T2);
5277 if (!T || !T2)
5278 goto error;
5279 if (T->n_col == 0) {
5280 isl_mat_free(T);
5281 isl_mat_free(T2);
5282 isl_vec_free(v);
5283 return isl_basic_map_set_to_empty(bmap);
5286 tightened = 0;
5287 for (i = 0; i < bmap->n_ineq; ++i) {
5288 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5289 v = isl_vec_mat_product(v, isl_mat_copy(T));
5290 v = normalize_constraint(v, &tightened);
5291 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5292 if (!v)
5293 goto error;
5294 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5297 isl_mat_free(T);
5298 isl_mat_free(T2);
5299 isl_vec_free(v);
5301 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5303 if (tightened) {
5304 int progress = 0;
5306 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5307 if (progress) {
5308 bmap = eliminate_divs_eq(bmap, &progress);
5309 bmap = isl_basic_map_gauss(bmap, NULL);
5313 return bmap;
5314 error:
5315 isl_mat_free(T);
5316 isl_mat_free(T2);
5317 isl_vec_free(v);
5318 return isl_basic_map_free(bmap);
5321 /* Shift the integer division at position "div" of "bmap"
5322 * by "shift" times the variable at position "pos".
5323 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5324 * corresponds to the constant term.
5326 * That is, if the integer division has the form
5328 * floor(f(x)/d)
5330 * then replace it by
5332 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5334 __isl_give isl_basic_map *isl_basic_map_shift_div(
5335 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5337 int i;
5338 unsigned total;
5340 if (isl_int_is_zero(shift))
5341 return bmap;
5342 if (!bmap)
5343 return NULL;
5345 total = isl_basic_map_dim(bmap, isl_dim_all);
5346 total -= isl_basic_map_dim(bmap, isl_dim_div);
5348 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5350 for (i = 0; i < bmap->n_eq; ++i) {
5351 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5352 continue;
5353 isl_int_submul(bmap->eq[i][pos],
5354 shift, bmap->eq[i][1 + total + div]);
5356 for (i = 0; i < bmap->n_ineq; ++i) {
5357 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5358 continue;
5359 isl_int_submul(bmap->ineq[i][pos],
5360 shift, bmap->ineq[i][1 + total + div]);
5362 for (i = 0; i < bmap->n_div; ++i) {
5363 if (isl_int_is_zero(bmap->div[i][0]))
5364 continue;
5365 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5366 continue;
5367 isl_int_submul(bmap->div[i][1 + pos],
5368 shift, bmap->div[i][1 + 1 + total + div]);
5371 return bmap;