2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_map_private.h>
15 #include <isl_point_private.h>
17 /* Expand the constraint "c" into "v". The initial "dim" dimensions
18 * are the same, but "v" may have more divs than "c" and the divs of "c"
19 * may appear in different positions in "v".
20 * The number of divs in "c" is given by "n_div" and the mapping
21 * of divs in "c" to divs in "v" is given by "div_map".
23 * Although it shouldn't happen in practice, it is theoretically
24 * possible that two or more divs in "c" are mapped to the same div in "v".
25 * These divs are then necessarily the same, so we simply add their
28 static void expand_constraint(isl_vec *v, unsigned dim,
29 isl_int *c, int *div_map, unsigned n_div)
33 isl_seq_cpy(v->el, c, 1 + dim);
34 isl_seq_clr(v->el + 1 + dim, v->size - (1 + dim));
36 for (i = 0; i < n_div; ++i) {
37 int pos = 1 + dim + div_map[i];
38 isl_int_add(v->el[pos], v->el[pos], c[1 + dim + i]);
42 /* Add all constraints of bmap to tab. The equalities of bmap
43 * are added as a pair of inequalities.
45 static int tab_add_constraints(struct isl_tab *tab,
46 __isl_keep isl_basic_map *bmap, int *div_map)
57 tab_total = isl_basic_map_total_dim(tab->bmap);
58 bmap_total = isl_basic_map_total_dim(bmap);
59 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
61 if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
64 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
68 for (i = 0; i < bmap->n_eq; ++i) {
69 expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
70 if (isl_tab_add_ineq(tab, v->el) < 0)
72 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
73 expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
74 if (isl_tab_add_ineq(tab, v->el) < 0)
76 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
81 for (i = 0; i < bmap->n_ineq; ++i) {
82 expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
83 if (isl_tab_add_ineq(tab, v->el) < 0)
96 /* Add a specific constraint of bmap (or its opposite) to tab.
97 * The position of the constraint is specified by "c", where
98 * the equalities of bmap are counted twice, once for the inequality
99 * that is equal to the equality, and once for its negation.
101 static int tab_add_constraint(struct isl_tab *tab,
102 __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
113 tab_total = isl_basic_map_total_dim(tab->bmap);
114 bmap_total = isl_basic_map_total_dim(bmap);
115 dim = isl_space_dim(tab->bmap->dim, isl_dim_all);
117 v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
121 if (c < 2 * bmap->n_eq) {
122 if ((c % 2) != oppose)
123 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
126 isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
127 expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
128 r = isl_tab_add_ineq(tab, v->el);
130 isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
131 if ((c % 2) != oppose)
132 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
137 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
139 isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
141 expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
142 r = isl_tab_add_ineq(tab, v->el);
144 isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
145 isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
154 static int tab_add_divs(struct isl_tab *tab, __isl_keep isl_basic_map *bmap,
168 *div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
172 total = isl_basic_map_total_dim(tab->bmap);
173 dim = total - tab->bmap->n_div;
174 vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
178 for (i = 0; i < bmap->n_div; ++i) {
179 isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
180 isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
181 for (j = 0; j < i; ++j)
182 isl_int_set(vec->el[2 + dim + (*div_map)[j]],
183 bmap->div[i][2 + dim + j]);
184 for (j = 0; j < tab->bmap->n_div; ++j)
185 if (isl_seq_eq(tab->bmap->div[j],
186 vec->el, 2 + dim + tab->bmap->n_div))
189 if (j == tab->bmap->n_div) {
190 vec->size = 2 + dim + tab->bmap->n_div;
191 if (isl_tab_add_div(tab, vec, NULL, NULL) < 0)
205 /* Freeze all constraints of tableau tab.
207 static int tab_freeze_constraints(struct isl_tab *tab)
211 for (i = 0; i < tab->n_con; ++i)
212 if (isl_tab_freeze_constraint(tab, i) < 0)
218 /* Check for redundant constraints starting at offset.
219 * Put the indices of the redundant constraints in index
220 * and return the number of redundant constraints.
222 static int n_non_redundant(isl_ctx *ctx, struct isl_tab *tab,
223 int offset, int **index)
226 int n_test = tab->n_con - offset;
228 if (isl_tab_detect_redundant(tab) < 0)
232 *index = isl_alloc_array(ctx, int, n_test);
236 for (n = 0, i = 0; i < n_test; ++i) {
238 r = isl_tab_is_redundant(tab, offset + i);
249 /* basic_map_collect_diff calls add on each of the pieces of
250 * the set difference between bmap and map until the add method
251 * return a negative value.
253 struct isl_diff_collector {
254 int (*add)(struct isl_diff_collector *dc,
255 __isl_take isl_basic_map *bmap);
258 /* Compute the set difference between bmap and map and call
259 * dc->add on each of the piece until this function returns
261 * Return 0 on success and -1 on error. dc->add returning
262 * a negative value is treated as an error, but the calling
263 * function can interpret the results based on the state of dc.
265 * Assumes that map has known divs.
267 * The difference is computed by a backtracking algorithm.
268 * Each level corresponds to a basic map in "map".
269 * When a node in entered for the first time, we check
270 * if the corresonding basic map intersects the current piece
271 * of "bmap". If not, we move to the next level.
272 * Otherwise, we split the current piece into as many
273 * pieces as there are non-redundant constraints of the current
274 * basic map in the intersection. Each of these pieces is
275 * handled by a child of the current node.
276 * In particular, if there are n non-redundant constraints,
277 * then for each 0 <= i < n, a piece is cut off by adding
278 * constraints 0 <= j < i and adding the opposite of constraint i.
279 * If there are no non-redundant constraints, meaning that the current
280 * piece is a subset of the current basic map, then we simply backtrack.
282 * In the leaves, we check if the remaining piece has any integer points
283 * and if so, pass it along to dc->add. As a special case, if nothing
284 * has been removed when we end up in a leaf, we simply pass along
285 * the original basic map.
287 static int basic_map_collect_diff(__isl_take isl_basic_map *bmap,
288 __isl_take isl_map *map, struct isl_diff_collector *dc)
296 struct isl_tab *tab = NULL;
297 struct isl_tab_undo **snap = NULL;
301 int **div_map = NULL;
303 empty = isl_basic_map_is_empty(bmap);
305 isl_basic_map_free(bmap);
307 return empty < 0 ? -1 : 0;
310 bmap = isl_basic_map_cow(bmap);
311 map = isl_map_cow(map);
317 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
318 k = isl_alloc_array(map->ctx, int, map->n);
319 n = isl_alloc_array(map->ctx, int, map->n);
320 index = isl_calloc_array(map->ctx, int *, map->n);
321 div_map = isl_calloc_array(map->ctx, int *, map->n);
322 if (!snap || !k || !n || !index || !div_map)
325 bmap = isl_basic_map_order_divs(bmap);
326 map = isl_map_order_divs(map);
328 tab = isl_tab_from_basic_map(bmap, 1);
335 if (level >= map->n) {
337 struct isl_basic_map *bm;
339 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
343 bm = isl_basic_map_copy(tab->bmap);
344 bm = isl_basic_map_cow(bm);
345 bm = isl_basic_map_update_from_tab(bm, tab);
346 bm = isl_basic_map_simplify(bm);
347 bm = isl_basic_map_finalize(bm);
348 empty = isl_basic_map_is_empty(bm);
350 isl_basic_map_free(bm);
351 else if (dc->add(dc, bm) < 0)
361 struct isl_tab_undo *snap2;
362 snap2 = isl_tab_snap(tab);
363 if (tab_add_divs(tab, map->p[level],
364 &div_map[level]) < 0)
367 snap[level] = isl_tab_snap(tab);
368 if (tab_freeze_constraints(tab) < 0)
370 if (tab_add_constraints(tab, map->p[level],
376 if (isl_tab_rollback(tab, snap2) < 0)
382 n[level] = n_non_redundant(ctx, tab, offset,
391 if (isl_tab_rollback(tab, snap[level]) < 0)
393 if (tab_add_constraint(tab, map->p[level],
394 div_map[level], index[level][0], 1) < 0)
399 if (k[level] + 1 >= n[level]) {
403 if (isl_tab_rollback(tab, snap[level]) < 0)
405 if (tab_add_constraint(tab, map->p[level],
407 index[level][k[level]], 0) < 0)
409 snap[level] = isl_tab_snap(tab);
411 if (tab_add_constraint(tab, map->p[level],
413 index[level][k[level]], 1) < 0)
425 for (i = 0; index && i < map->n; ++i)
428 for (i = 0; div_map && i < map->n; ++i)
432 isl_basic_map_free(bmap);
441 for (i = 0; index && i < map->n; ++i)
444 for (i = 0; div_map && i < map->n; ++i)
447 isl_basic_map_free(bmap);
452 /* A diff collector that actually collects all parts of the
453 * set difference in the field diff.
455 struct isl_subtract_diff_collector {
456 struct isl_diff_collector dc;
457 struct isl_map *diff;
460 /* isl_subtract_diff_collector callback.
462 static int basic_map_subtract_add(struct isl_diff_collector *dc,
463 __isl_take isl_basic_map *bmap)
465 struct isl_subtract_diff_collector *sdc;
466 sdc = (struct isl_subtract_diff_collector *)dc;
468 sdc->diff = isl_map_union_disjoint(sdc->diff,
469 isl_map_from_basic_map(bmap));
471 return sdc->diff ? 0 : -1;
474 /* Return the set difference between bmap and map.
476 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
477 __isl_take isl_map *map)
479 struct isl_subtract_diff_collector sdc;
480 sdc.dc.add = &basic_map_subtract_add;
481 sdc.diff = isl_map_empty_like_basic_map(bmap);
482 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
483 isl_map_free(sdc.diff);
489 /* Return the set difference between map1 and map2.
490 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
492 static __isl_give isl_map *map_subtract( __isl_take isl_map *map1,
493 __isl_take isl_map *map2)
496 struct isl_map *diff;
501 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
503 if (isl_map_is_empty(map2)) {
508 map1 = isl_map_compute_divs(map1);
509 map2 = isl_map_compute_divs(map2);
513 map1 = isl_map_remove_empty_parts(map1);
514 map2 = isl_map_remove_empty_parts(map2);
516 diff = isl_map_empty_like(map1);
517 for (i = 0; i < map1->n; ++i) {
519 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
521 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
522 diff = isl_map_union_disjoint(diff, d);
524 diff = isl_map_union(diff, d);
537 __isl_give isl_map *isl_map_subtract( __isl_take isl_map *map1,
538 __isl_take isl_map *map2)
540 return isl_map_align_params_map_map_and(map1, map2, &map_subtract);
543 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
545 return (struct isl_set *)
547 (struct isl_map *)set1, (struct isl_map *)set2);
550 /* Remove the elements of "dom" from the domain of "map".
552 static __isl_give isl_map *map_subtract_domain(__isl_take isl_map *map,
553 __isl_take isl_set *dom)
557 if (!isl_map_compatible_domain(map, dom))
558 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
559 "incompatible spaces", goto error);
561 ext_dom = isl_map_universe(isl_map_get_space(map));
562 ext_dom = isl_map_intersect_domain(ext_dom, dom);
563 return isl_map_subtract(map, ext_dom);
570 __isl_give isl_map *isl_map_subtract_domain(__isl_take isl_map *map,
571 __isl_take isl_set *dom)
573 return isl_map_align_params_map_map_and(map, dom, &map_subtract_domain);
576 /* Remove the elements of "dom" from the range of "map".
578 static __isl_give isl_map *map_subtract_range(__isl_take isl_map *map,
579 __isl_take isl_set *dom)
583 if (!isl_map_compatible_range(map, dom))
584 isl_die(isl_set_get_ctx(dom), isl_error_invalid,
585 "incompatible spaces", goto error);
587 ext_dom = isl_map_universe(isl_map_get_space(map));
588 ext_dom = isl_map_intersect_range(ext_dom, dom);
589 return isl_map_subtract(map, ext_dom);
596 __isl_give isl_map *isl_map_subtract_range(__isl_take isl_map *map,
597 __isl_take isl_set *dom)
599 return isl_map_align_params_map_map_and(map, dom, &map_subtract_range);
602 /* A diff collector that aborts as soon as its add function is called,
603 * setting empty to 0.
605 struct isl_is_empty_diff_collector {
606 struct isl_diff_collector dc;
610 /* isl_is_empty_diff_collector callback.
612 static int basic_map_is_empty_add(struct isl_diff_collector *dc,
613 __isl_take isl_basic_map *bmap)
615 struct isl_is_empty_diff_collector *edc;
616 edc = (struct isl_is_empty_diff_collector *)dc;
620 isl_basic_map_free(bmap);
624 /* Check if bmap \ map is empty by computing this set difference
625 * and breaking off as soon as the difference is known to be non-empty.
627 static int basic_map_diff_is_empty(__isl_keep isl_basic_map *bmap,
628 __isl_keep isl_map *map)
631 struct isl_is_empty_diff_collector edc;
633 r = isl_basic_map_plain_is_empty(bmap);
637 edc.dc.add = &basic_map_is_empty_add;
639 r = basic_map_collect_diff(isl_basic_map_copy(bmap),
640 isl_map_copy(map), &edc.dc);
644 return r < 0 ? -1 : 1;
647 /* Check if map1 \ map2 is empty by checking if the set difference is empty
648 * for each of the basic maps in map1.
650 static int map_diff_is_empty(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
658 for (i = 0; i < map1->n; ++i) {
659 is_empty = basic_map_diff_is_empty(map1->p[i], map2);
660 if (is_empty < 0 || !is_empty)
667 /* Return 1 if "bmap" contains a single element.
669 int isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map *bmap)
677 return bmap->n_eq == isl_basic_map_total_dim(bmap);
680 /* Return 1 if "map" contains a single element.
682 int isl_map_plain_is_singleton(__isl_keep isl_map *map)
689 return isl_basic_map_plain_is_singleton(map->p[0]);
692 /* Given a singleton basic map, extract the single element
695 static __isl_give isl_point *singleton_extract_point(
696 __isl_keep isl_basic_map *bmap)
700 struct isl_vec *point;
706 dim = isl_basic_map_total_dim(bmap);
707 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
708 point = isl_vec_alloc(bmap->ctx, 1 + dim);
714 isl_int_set_si(point->el[0], 1);
715 for (j = 0; j < bmap->n_eq; ++j) {
717 isl_assert(bmap->ctx,
718 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
720 isl_assert(bmap->ctx,
721 isl_int_is_one(bmap->eq[j][1 + i]) ||
722 isl_int_is_negone(bmap->eq[j][1 + i]),
724 isl_assert(bmap->ctx,
725 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
728 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
729 isl_int_divexact(m, bmap->eq[j][1 + i], m);
731 isl_seq_scale(point->el, point->el, m, 1 + i);
732 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
734 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
738 return isl_point_alloc(isl_basic_map_get_space(bmap), point);
745 /* Return 1 is the singleton map "map1" is a subset of "map2",
746 * i.e., if the single element of "map1" is also an element of "map2".
747 * Assumes "map2" has known divs.
749 static int map_is_singleton_subset(__isl_keep isl_map *map1,
750 __isl_keep isl_map *map2)
754 struct isl_point *point;
761 point = singleton_extract_point(map1->p[0]);
765 for (i = 0; i < map2->n; ++i) {
766 is_subset = isl_basic_map_contains_point(map2->p[i], point);
771 isl_point_free(point);
775 static int map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
782 if (!isl_map_has_equal_space(map1, map2))
785 if (isl_map_is_empty(map1))
788 if (isl_map_is_empty(map2))
791 if (isl_map_plain_is_universe(map2))
794 map2 = isl_map_compute_divs(isl_map_copy(map2));
795 if (isl_map_plain_is_singleton(map1)) {
796 is_subset = map_is_singleton_subset(map1, map2);
800 is_subset = map_diff_is_empty(map1, map2);
806 int isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
808 return isl_map_align_params_map_map_and_test(map1, map2,
812 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
814 return isl_map_is_subset(
815 (struct isl_map *)set1, (struct isl_map *)set2);
818 __isl_give isl_map *isl_map_make_disjoint(__isl_take isl_map *map)
821 struct isl_subtract_diff_collector sdc;
822 sdc.dc.add = &basic_map_subtract_add;
826 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
831 map = isl_map_compute_divs(map);
832 map = isl_map_remove_empty_parts(map);
834 if (!map || map->n <= 1)
837 sdc.diff = isl_map_from_basic_map(isl_basic_map_copy(map->p[0]));
839 for (i = 1; i < map->n; ++i) {
840 struct isl_basic_map *bmap = isl_basic_map_copy(map->p[i]);
841 struct isl_map *copy = isl_map_copy(sdc.diff);
842 if (basic_map_collect_diff(bmap, copy, &sdc.dc) < 0) {
843 isl_map_free(sdc.diff);
854 __isl_give isl_set *isl_set_make_disjoint(__isl_take isl_set *set)
856 return (struct isl_set *)isl_map_make_disjoint((struct isl_map *)set);
859 __isl_give isl_map *isl_map_complement(__isl_take isl_map *map)
866 universe = isl_map_universe(isl_map_get_space(map));
868 return isl_map_subtract(universe, map);
871 __isl_give isl_set *isl_set_complement(__isl_take isl_set *set)
873 return isl_map_complement(set);