2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 #include "isl_map_private.h"
16 /* Add all constraints of bmap to tab. The equalities of bmap
17 * are added as a pair of inequalities.
19 static int tab_add_constraints(struct isl_tab *tab,
20 __isl_keep isl_basic_map *bmap)
28 total = isl_basic_map_total_dim(bmap);
30 if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
33 for (i = 0; i < bmap->n_eq; ++i) {
34 if (isl_tab_add_ineq(tab, bmap->eq[i]) < 0)
36 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + total);
37 if (isl_tab_add_ineq(tab, bmap->eq[i]) < 0)
39 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + total);
44 for (i = 0; i < bmap->n_ineq; ++i) {
45 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
54 /* Add a specific constraint of bmap (or its opposite) to tab.
55 * The position of the constraint is specified by "c", where
56 * the equalities of bmap are counted twice, once for the inequality
57 * that is equal to the equality, and once for its negation.
59 static int tab_add_constraint(struct isl_tab *tab,
60 __isl_keep isl_basic_map *bmap, int c, int oppose)
68 total = isl_basic_map_total_dim(bmap);
70 if (c < 2 * bmap->n_eq) {
71 if ((c % 2) != oppose)
72 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2], 1 + total);
74 isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
75 r = isl_tab_add_ineq(tab, bmap->eq[c/2]);
77 isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
78 if ((c % 2) != oppose)
79 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2], 1 + total);
83 isl_seq_neg(bmap->ineq[c], bmap->ineq[c], 1 + total);
84 isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
86 r = isl_tab_add_ineq(tab, bmap->ineq[c]);
88 isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
89 isl_seq_neg(bmap->ineq[c], bmap->ineq[c], 1 + total);
96 /* Freeze all constraints of tableau tab.
98 static int tab_freeze_constraints(struct isl_tab *tab)
102 for (i = 0; i < tab->n_con; ++i)
103 if (isl_tab_freeze_constraint(tab, i) < 0)
109 /* Check for redundant constraints starting at offset.
110 * Put the indices of the redundant constraints in index
111 * and return the number of redundant constraints.
113 static int n_non_redundant(struct isl_tab *tab, int offset, int **index)
116 int n_test = tab->n_con - offset;
118 if (isl_tab_detect_redundant(tab) < 0)
122 *index = isl_alloc_array(tab->mat->ctx, int, n_test);
126 for (n = 0, i = 0; i < n_test; ++i) {
128 r = isl_tab_is_redundant(tab, offset + i);
139 /* basic_map_collect_diff calls add on each of the pieces of
140 * the set difference between bmap and map until the add method
141 * return a negative value.
143 struct isl_diff_collector {
144 int (*add)(struct isl_diff_collector *dc,
145 __isl_take isl_basic_map *bmap);
148 /* Compute the set difference between bmap and map and call
149 * dc->add on each of the piece until this function returns
151 * Return 0 on success and -1 on error. dc->add returning
152 * a negative value is treated as an error, but the calling
153 * function can interpret the results based on the state of dc.
155 * Assumes that both bmap and map have known divs.
157 * The difference is computed by a backtracking algorithm.
158 * Each level corresponds to a basic map in "map".
159 * When a node in entered for the first time, we check
160 * if the corresonding basic map intersect the current piece
161 * of "bmap". If not, we move to the next level.
162 * Otherwise, we split the current piece into as many
163 * pieces as there are non-redundant constraints of the current
164 * basic map in the intersection. Each of these pieces is
165 * handled by a child of the current node.
166 * In particular, if there are n non-redundant constraints,
167 * then for each 0 <= i < n, a piece is cut off by adding
168 * constraints 0 <= j < i and adding the opposite of constrain i.
169 * If there are no non-redundant constraints, meaning that the current
170 * piece is a subset of the current basic map, then we simply backtrack.
172 * In the leaves, we check if the remaining piece has any integer points
173 * and if so, pass it along to dc->add. As a special case, if nothing
174 * has been removed when we end up in a leaf, we simply pass along
175 * the original basic map.
177 static int basic_map_collect_diff(__isl_take isl_basic_map *bmap,
178 __isl_take isl_map *map, struct isl_diff_collector *dc)
185 struct isl_tab *tab = NULL;
186 struct isl_tab_undo **snap = NULL;
191 empty = isl_basic_map_is_empty(bmap);
193 isl_basic_map_free(bmap);
195 return empty < 0 ? -1 : 0;
198 bmap = isl_basic_map_cow(bmap);
199 map = isl_map_cow(map);
204 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
205 k = isl_alloc_array(map->ctx, int, map->n);
206 n = isl_alloc_array(map->ctx, int, map->n);
207 index = isl_calloc_array(map->ctx, int *, map->n);
208 if (!snap || !k || !n || !index)
211 for (i = 0; i < map->n; ++i) {
212 bmap = isl_basic_map_align_divs(bmap, map->p[i]);
216 for (i = 0; i < map->n; ++i) {
217 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
222 tab = isl_tab_from_basic_map(bmap);
223 if (isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
231 if (level >= map->n) {
233 struct isl_basic_map *bm;
235 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
239 bm = isl_basic_map_copy(tab->bmap);
240 bm = isl_basic_map_cow(bm);
241 bm = isl_basic_map_update_from_tab(bm, tab);
242 bm = isl_basic_map_simplify(bm);
243 bm = isl_basic_map_finalize(bm);
244 empty = isl_basic_map_is_empty(bm);
246 isl_basic_map_free(bm);
247 else if (dc->add(dc, bm) < 0)
256 int offset = tab->n_con;
257 snap[level] = isl_tab_snap(tab);
258 if (tab_freeze_constraints(tab) < 0)
260 if (tab_add_constraints(tab, map->p[level]) < 0)
265 if (isl_tab_rollback(tab, snap[level]) < 0)
271 n[level] = n_non_redundant(tab, offset, &index[level]);
279 if (isl_tab_rollback(tab, snap[level]) < 0)
281 if (tab_add_constraint(tab, map->p[level],
282 index[level][0], 1) < 0)
287 if (k[level] + 1 >= n[level]) {
291 if (isl_tab_rollback(tab, snap[level]) < 0)
293 if (tab_add_constraint(tab, map->p[level],
294 index[level][k[level]], 0) < 0)
296 snap[level] = isl_tab_snap(tab);
298 if (tab_add_constraint(tab, map->p[level],
299 index[level][k[level]], 1) < 0)
311 for (i = 0; index && i < map->n; ++i)
315 isl_basic_map_free(bmap);
324 for (i = 0; index && i < map->n; ++i)
327 isl_basic_map_free(bmap);
332 /* A diff collector that actually collects all parts of the
333 * set difference in the field diff.
335 struct isl_subtract_diff_collector {
336 struct isl_diff_collector dc;
337 struct isl_map *diff;
340 /* isl_subtract_diff_collector callback.
342 static int basic_map_subtract_add(struct isl_diff_collector *dc,
343 __isl_take isl_basic_map *bmap)
345 struct isl_subtract_diff_collector *sdc;
346 sdc = (struct isl_subtract_diff_collector *)dc;
348 sdc->diff = isl_map_union_disjoint(sdc->diff,
349 isl_map_from_basic_map(bmap));
351 return sdc->diff ? 0 : -1;
354 /* Return the set difference between bmap and map.
356 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
357 __isl_take isl_map *map)
359 struct isl_subtract_diff_collector sdc;
360 sdc.dc.add = &basic_map_subtract_add;
361 sdc.diff = isl_map_empty_like_basic_map(bmap);
362 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
363 isl_map_free(sdc.diff);
369 /* Return the set difference between map1 and map2.
370 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
372 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
375 struct isl_map *diff;
380 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
382 if (isl_map_is_empty(map2)) {
387 map1 = isl_map_compute_divs(map1);
388 map2 = isl_map_compute_divs(map2);
392 map1 = isl_map_remove_empty_parts(map1);
393 map2 = isl_map_remove_empty_parts(map2);
395 diff = isl_map_empty_like(map1);
396 for (i = 0; i < map1->n; ++i) {
398 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
400 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
401 diff = isl_map_union_disjoint(diff, d);
403 diff = isl_map_union(diff, d);
416 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
418 return (struct isl_set *)
420 (struct isl_map *)set1, (struct isl_map *)set2);
423 /* A diff collector that aborts as soon as its add function is called,
424 * setting empty to 0.
426 struct isl_is_empty_diff_collector {
427 struct isl_diff_collector dc;
431 /* isl_is_empty_diff_collector callback.
433 static int basic_map_is_empty_add(struct isl_diff_collector *dc,
434 __isl_take isl_basic_map *bmap)
436 struct isl_is_empty_diff_collector *edc;
437 edc = (struct isl_is_empty_diff_collector *)dc;
441 isl_basic_map_free(bmap);
445 /* Check if bmap \ map is empty by computing this set difference
446 * and breaking off as soon as the difference is known to be non-empty.
448 static int basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
449 __isl_keep isl_map *map)
452 struct isl_is_empty_diff_collector edc;
454 r = isl_basic_map_fast_is_empty(bmap);
458 edc.dc.add = &basic_map_is_empty_add;
460 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
461 isl_map_copy(map), &edc.dc);
465 return r < 0 ? -1 : 1;
468 /* Check if map1 \ map2 is empty by checking if the set difference is empty
469 * for each of the basic maps in map1.
471 static int map_diff_is_empty(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
479 for (i = 0; i < map1->n; ++i) {
480 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
481 if (is_empty < 0 || !is_empty)
488 /* Return 1 if "bmap" contains a single element.
490 int isl_basic_map_is_singleton(__isl_keep isl_basic_map *bmap)
498 return bmap->n_eq == isl_basic_map_total_dim(bmap);
501 /* Return 1 if "map" contains a single element.
503 int isl_map_is_singleton(__isl_keep isl_map *map)
510 return isl_basic_map_is_singleton(map->p[0]);
513 /* Given a singleton basic map, extract the single element
516 static __isl_give isl_vec *singleton_extract_point(__isl_keep isl_basic_map *bmap)
520 struct isl_vec *point;
526 dim = isl_basic_map_total_dim(bmap);
527 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
528 point = isl_vec_alloc(bmap->ctx, 1 + dim);
534 isl_int_set_si(point->el[0], 1);
535 for (j = 0; j < bmap->n_eq; ++j) {
538 isl_assert(bmap->ctx,
539 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
541 isl_assert(bmap->ctx,
542 isl_int_is_one(bmap->eq[j][1 + i]) ||
543 isl_int_is_negone(bmap->eq[j][1 + i]),
545 isl_assert(bmap->ctx,
546 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
549 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
550 isl_int_divexact(m, bmap->eq[j][1 + i], m);
552 isl_seq_scale(point->el, point->el, m, 1 + i);
553 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
555 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
566 /* Return 1 if "bmap" contains the point "point".
567 * "bmap" is assumed to have known divs.
568 * The point is first extended with the divs and then passed
569 * to basic_map_contains.
571 static int basic_map_contains_point(__isl_keep isl_basic_map *bmap,
572 __isl_keep isl_vec *point)
581 if (bmap->n_div == 0)
582 return isl_basic_map_contains(bmap, point);
584 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
585 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
589 isl_seq_cpy(vec->el, point->el, point->size);
590 for (i = 0; i < bmap->n_div; ++i) {
591 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
592 1 + dim + i, &vec->el[1+dim+i]);
593 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
597 contains = isl_basic_map_contains(bmap, vec);
603 /* Return 1 is the singleton map "map1" is a subset of "map2",
604 * i.e., if the single element of "map1" is also an element of "map2".
606 static int map_is_singleton_subset(__isl_keep isl_map *map1,
607 __isl_keep isl_map *map2)
611 struct isl_vec *point;
618 point = singleton_extract_point(map1->p[0]);
622 for (i = 0; i < map2->n; ++i) {
623 is_subset = basic_map_contains_point(map2->p[i], point);
632 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
635 struct isl_map *diff;
640 if (isl_map_is_empty(map1))
643 if (isl_map_is_empty(map2))
646 if (isl_map_fast_is_universe(map2))
649 map1 = isl_map_compute_divs(isl_map_copy(map1));
650 map2 = isl_map_compute_divs(isl_map_copy(map2));
651 if (isl_map_is_singleton(map1)) {
652 is_subset = map_is_singleton_subset(map1, map2);
657 is_subset = map_diff_is_empty(map1, map2);
664 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
666 return isl_map_is_subset(
667 (struct isl_map *)set1, (struct isl_map *)set2);
670 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
673 struct isl_subtract_diff_collector sdc;
674 sdc.dc.add = &basic_map_subtract_add;
678 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
683 map = isl_map_compute_divs(map);
684 map = isl_map_remove_empty_parts(map);
686 if (!map || map->n <= 1)
689 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
691 for (i = 1; i < map->n; ++i) {
692 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
693 struct isl_map *copy = isl_map_copy(sdc.diff);
694 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
695 isl_map_free(sdc.diff);
706 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
708 return (struct isl_set *)isl_map_make_disjoint((struct isl_map *)set);