4 #include "isl_map_private.h"
7 /* Add all constraints of bmap to tab. The equalities of bmap
8 * are added as a pair of inequalities.
10 static int tab_add_constraints(struct isl_tab *tab,
11 __isl_keep isl_basic_map *bmap)
19 total = isl_basic_map_total_dim(bmap);
21 if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
24 for (i = 0; i < bmap->n_eq; ++i) {
25 if (isl_tab_add_ineq(tab, bmap->eq[i]) < 0)
27 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + total);
28 if (isl_tab_add_ineq(tab, bmap->eq[i]) < 0)
30 isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + total);
35 for (i = 0; i < bmap->n_ineq; ++i) {
36 if (isl_tab_add_ineq(tab, bmap->ineq[i]) < 0)
45 /* Add a specific constraint of bmap (or its opposite) to tab.
46 * The position of the constraint is specified by "c", where
47 * the equalities of bmap are counted twice, once for the inequality
48 * that is equal to the equality, and once for its negation.
50 static int tab_add_constraint(struct isl_tab *tab,
51 __isl_keep isl_basic_map *bmap, int c, int oppose)
59 total = isl_basic_map_total_dim(bmap);
61 if (c < 2 * bmap->n_eq) {
62 if ((c % 2) != oppose)
63 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2], 1 + total);
65 isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
66 r = isl_tab_add_ineq(tab, bmap->eq[c/2]);
68 isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
69 if ((c % 2) != oppose)
70 isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2], 1 + total);
74 isl_seq_neg(bmap->ineq[c], bmap->ineq[c], 1 + total);
75 isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
77 r = isl_tab_add_ineq(tab, bmap->ineq[c]);
79 isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
80 isl_seq_neg(bmap->ineq[c], bmap->ineq[c], 1 + total);
87 /* Freeze all constraints of tableau tab.
89 static int tab_freeze_constraints(struct isl_tab *tab)
93 for (i = 0; i < tab->n_con; ++i)
94 if (isl_tab_freeze_constraint(tab, i) < 0)
100 /* Check for redundant constraints starting at offset.
101 * Put the indices of the redundant constraints in index
102 * and return the number of redundant constraints.
104 static int n_non_redundant(struct isl_tab *tab, int offset, int **index)
107 int n_test = tab->n_con - offset;
109 if (isl_tab_detect_redundant(tab) < 0)
113 *index = isl_alloc_array(tab->mat->ctx, int, n_test);
117 for (n = 0, i = 0; i < n_test; ++i) {
119 r = isl_tab_is_redundant(tab, offset + i);
130 /* basic_map_collect_diff calls add on each of the pieces of
131 * the set difference between bmap and map until the add method
132 * return a negative value.
134 struct isl_diff_collector {
135 int (*add)(struct isl_diff_collector *dc,
136 __isl_take isl_basic_map *bmap);
139 /* Compute the set difference between bmap and map and call
140 * dc->add on each of the piece until this function returns
142 * Return 0 on success and -1 on error. dc->add returning
143 * a negative value is treated as an error, but the calling
144 * function can interpret the results based on the state of dc.
146 * Assumes that both bmap and map have known divs.
148 * The difference is computed by a backtracking algorithm.
149 * Each level corresponds to a basic map in "map".
150 * When a node in entered for the first time, we check
151 * if the corresonding basic map intersect the current piece
152 * of "bmap". If not, we move to the next level.
153 * Otherwise, we split the current piece into as many
154 * pieces as there are non-redundant constraints of the current
155 * basic map in the intersection. Each of these pieces is
156 * handled by a child of the current node.
157 * In particular, if there are n non-redundant constraints,
158 * then for each 0 <= i < n, a piece is cut off by adding
159 * constraints 0 <= j < i and adding the opposite of constrain i.
160 * If there are no non-redundant constraints, meaning that the current
161 * piece is a subset of the current basic map, then we simply backtrack.
163 * In the leaves, we check if the remaining piece has any integer points
164 * and if so, pass it along to dc->add. As a special case, if nothing
165 * has been removed when we end up in a leaf, we simply pass along
166 * the original basic map.
168 static int basic_map_collect_diff(__isl_take isl_basic_map *bmap,
169 __isl_take isl_map *map, struct isl_diff_collector *dc)
176 struct isl_tab *tab = NULL;
177 struct isl_tab_undo **snap = NULL;
182 empty = isl_basic_map_is_empty(bmap);
184 isl_basic_map_free(bmap);
186 return empty < 0 ? -1 : 0;
189 bmap = isl_basic_map_cow(bmap);
190 map = isl_map_cow(map);
195 snap = isl_alloc_array(map->ctx, struct isl_tab_undo *, map->n);
196 k = isl_alloc_array(map->ctx, int, map->n);
197 n = isl_alloc_array(map->ctx, int, map->n);
198 index = isl_calloc_array(map->ctx, int *, map->n);
199 if (!snap || !k || !n || !index)
202 for (i = 0; i < map->n; ++i) {
203 bmap = isl_basic_map_align_divs(bmap, map->p[i]);
207 for (i = 0; i < map->n; ++i) {
208 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
213 tab = isl_tab_from_basic_map(bmap);
214 if (isl_tab_track_bmap(tab, isl_basic_map_copy(bmap)) < 0)
222 if (level >= map->n) {
224 struct isl_basic_map *bm;
226 if (dc->add(dc, isl_basic_map_copy(bmap)) < 0)
230 bm = isl_basic_map_copy(tab->bmap);
231 bm = isl_basic_map_cow(bm);
232 bm = isl_basic_map_update_from_tab(bm, tab);
233 bm = isl_basic_map_simplify(bm);
234 bm = isl_basic_map_finalize(bm);
235 empty = isl_basic_map_is_empty(bm);
237 isl_basic_map_free(bm);
238 else if (dc->add(dc, bm) < 0)
247 int offset = tab->n_con;
248 snap[level] = isl_tab_snap(tab);
249 if (tab_freeze_constraints(tab) < 0)
251 if (tab_add_constraints(tab, map->p[level]) < 0)
256 if (isl_tab_rollback(tab, snap[level]) < 0)
262 n[level] = n_non_redundant(tab, offset, &index[level]);
270 if (isl_tab_rollback(tab, snap[level]) < 0)
272 if (tab_add_constraint(tab, map->p[level],
273 index[level][0], 1) < 0)
278 if (k[level] + 1 >= n[level]) {
282 if (isl_tab_rollback(tab, snap[level]) < 0)
284 if (tab_add_constraint(tab, map->p[level],
285 index[level][k[level]], 0) < 0)
287 snap[level] = isl_tab_snap(tab);
289 if (tab_add_constraint(tab, map->p[level],
290 index[level][k[level]], 1) < 0)
302 for (i = 0; index && i < map->n; ++i)
306 isl_basic_map_free(bmap);
315 for (i = 0; index && i < map->n; ++i)
318 isl_basic_map_free(bmap);
323 /* A diff collector that actually collects all parts of the
324 * set difference in the field diff.
326 struct isl_subtract_diff_collector {
327 struct isl_diff_collector dc;
328 struct isl_map *diff;
331 /* isl_subtract_diff_collector callback.
333 int basic_map_subtract_add(struct isl_diff_collector *dc,
334 __isl_take isl_basic_map *bmap)
336 struct isl_subtract_diff_collector *sdc;
337 sdc = (struct isl_subtract_diff_collector *)dc;
339 sdc->diff = isl_map_union_disjoint(sdc->diff,
340 isl_map_from_basic_map(bmap));
342 return sdc->diff ? 0 : -1;
345 /* Return the set difference between bmap and map.
347 static __isl_give isl_map *basic_map_subtract(__isl_take isl_basic_map *bmap,
348 __isl_take isl_map *map)
350 struct isl_subtract_diff_collector sdc;
351 sdc.dc.add = &basic_map_subtract_add;
352 sdc.diff = isl_map_empty_like_basic_map(bmap);
353 if (basic_map_collect_diff(bmap, map, &sdc.dc) < 0) {
354 isl_map_free(sdc.diff);
360 /* Return the set difference between map1 and map2.
361 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
363 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
366 struct isl_map *diff;
371 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
373 if (isl_map_is_empty(map2)) {
378 map1 = isl_map_compute_divs(map1);
379 map2 = isl_map_compute_divs(map2);
383 map1 = isl_map_remove_empty_parts(map1);
384 map2 = isl_map_remove_empty_parts(map2);
386 diff = isl_map_empty_like(map1);
387 for (i = 0; i < map1->n; ++i) {
389 d = basic_map_subtract(isl_basic_map_copy(map1->p[i]),
391 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT))
392 diff = isl_map_union_disjoint(diff, d);
394 diff = isl_map_union(diff, d);
407 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
409 return (struct isl_set *)
411 (struct isl_map *)set1, (struct isl_map *)set2);
414 /* Return 1 if "bmap" contains a single element.
416 int isl_basic_map_is_singleton(__isl_keep isl_basic_map *bmap)
424 return bmap->n_eq == isl_basic_map_total_dim(bmap);
427 /* Return 1 if "map" contains a single element.
429 int isl_map_is_singleton(__isl_keep isl_map *map)
436 return isl_basic_map_is_singleton(map->p[0]);
439 /* Given a singleton basic map, extract the single element
442 static __isl_give isl_vec *singleton_extract_point(__isl_keep isl_basic_map *bmap)
446 struct isl_vec *point;
452 dim = isl_basic_map_total_dim(bmap);
453 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
454 point = isl_vec_alloc(bmap->ctx, 1 + dim);
460 isl_int_set_si(point->el[0], 1);
461 for (j = 0; j < bmap->n_eq; ++j) {
464 isl_assert(bmap->ctx,
465 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
467 isl_assert(bmap->ctx,
468 isl_int_is_one(bmap->eq[j][1 + i]) ||
469 isl_int_is_negone(bmap->eq[j][1 + i]),
471 isl_assert(bmap->ctx,
472 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
475 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
476 isl_int_divexact(m, bmap->eq[j][1 + i], m);
478 isl_seq_scale(point->el, point->el, m, 1 + i);
479 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
481 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
492 /* Return 1 if "bmap" contains the point "point".
493 * "bmap" is assumed to have known divs.
494 * The point is first extended with the divs and then passed
495 * to basic_map_contains.
497 static int basic_map_contains_point(__isl_keep isl_basic_map *bmap,
498 __isl_keep isl_vec *point)
507 if (bmap->n_div == 0)
508 return isl_basic_map_contains(bmap, point);
510 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
511 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
515 isl_seq_cpy(vec->el, point->el, point->size);
516 for (i = 0; i < bmap->n_div; ++i) {
517 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
518 1 + dim + i, &vec->el[1+dim+i]);
519 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
523 contains = isl_basic_map_contains(bmap, vec);
529 /* Return 1 is the singleton map "map1" is a subset of "map2",
530 * i.e., if the single element of "map1" is also an element of "map2".
532 static int map_is_singleton_subset(__isl_keep isl_map *map1,
533 __isl_keep isl_map *map2)
537 struct isl_vec *point;
544 point = singleton_extract_point(map1->p[0]);
548 for (i = 0; i < map2->n; ++i) {
549 is_subset = basic_map_contains_point(map2->p[i], point);
558 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
561 struct isl_map *diff;
566 if (isl_map_is_empty(map1))
569 if (isl_map_is_empty(map2))
572 if (isl_map_fast_is_universe(map2))
575 map2 = isl_map_compute_divs(isl_map_copy(map2));
576 if (isl_map_is_singleton(map1)) {
577 is_subset = map_is_singleton_subset(map1, map2);
581 diff = isl_map_subtract(isl_map_copy(map1), map2);
585 is_subset = isl_map_is_empty(diff);
591 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
593 return isl_map_is_subset(
594 (struct isl_map *)set1, (struct isl_map *)set2);