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);
337 if (level
>= map
->n
) {
339 struct isl_basic_map
*bm
;
341 if (dc
->add(dc
, isl_basic_map_copy(bmap
)) < 0)
345 bm
= isl_basic_map_copy(tab
->bmap
);
346 bm
= isl_basic_map_cow(bm
);
347 bm
= isl_basic_map_update_from_tab(bm
, tab
);
348 bm
= isl_basic_map_simplify(bm
);
349 bm
= isl_basic_map_finalize(bm
);
350 empty
= isl_basic_map_is_empty(bm
);
352 isl_basic_map_free(bm
);
353 else if (dc
->add(dc
, bm
) < 0)
363 struct isl_tab_undo
*snap2
;
364 snap2
= isl_tab_snap(tab
);
365 if (tab_add_divs(tab
, map
->p
[level
],
366 &div_map
[level
]) < 0)
369 snap
[level
] = isl_tab_snap(tab
);
370 if (tab_freeze_constraints(tab
) < 0)
372 if (tab_add_constraints(tab
, map
->p
[level
],
378 if (isl_tab_rollback(tab
, snap2
) < 0)
384 n
[level
] = n_non_redundant(ctx
, tab
, offset
,
393 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
395 if (tab_add_constraint(tab
, map
->p
[level
],
396 div_map
[level
], index
[level
][0], 1) < 0)
401 if (k
[level
] + 1 >= n
[level
]) {
405 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
407 if (tab_add_constraint(tab
, map
->p
[level
],
409 index
[level
][k
[level
]], 0) < 0)
411 snap
[level
] = isl_tab_snap(tab
);
413 if (tab_add_constraint(tab
, map
->p
[level
],
415 index
[level
][k
[level
]], 1) < 0)
427 for (i
= 0; index
&& i
< map
->n
; ++i
)
430 for (i
= 0; div_map
&& i
< map
->n
; ++i
)
434 isl_basic_map_free(bmap
);
443 for (i
= 0; index
&& i
< map
->n
; ++i
)
446 for (i
= 0; div_map
&& i
< map
->n
; ++i
)
449 isl_basic_map_free(bmap
);
454 /* A diff collector that actually collects all parts of the
455 * set difference in the field diff.
457 struct isl_subtract_diff_collector
{
458 struct isl_diff_collector dc
;
459 struct isl_map
*diff
;
462 /* isl_subtract_diff_collector callback.
464 static int basic_map_subtract_add(struct isl_diff_collector
*dc
,
465 __isl_take isl_basic_map
*bmap
)
467 struct isl_subtract_diff_collector
*sdc
;
468 sdc
= (struct isl_subtract_diff_collector
*)dc
;
470 sdc
->diff
= isl_map_union_disjoint(sdc
->diff
,
471 isl_map_from_basic_map(bmap
));
473 return sdc
->diff
? 0 : -1;
476 /* Return the set difference between bmap and map.
478 static __isl_give isl_map
*basic_map_subtract(__isl_take isl_basic_map
*bmap
,
479 __isl_take isl_map
*map
)
481 struct isl_subtract_diff_collector sdc
;
482 sdc
.dc
.add
= &basic_map_subtract_add
;
483 sdc
.diff
= isl_map_empty_like_basic_map(bmap
);
484 if (basic_map_collect_diff(bmap
, map
, &sdc
.dc
) < 0) {
485 isl_map_free(sdc
.diff
);
491 /* Return the set difference between map1 and map2.
492 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
494 static __isl_give isl_map
*map_subtract( __isl_take isl_map
*map1
,
495 __isl_take isl_map
*map2
)
498 struct isl_map
*diff
;
503 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
505 if (isl_map_is_empty(map2
)) {
510 map1
= isl_map_compute_divs(map1
);
511 map2
= isl_map_compute_divs(map2
);
515 map1
= isl_map_remove_empty_parts(map1
);
516 map2
= isl_map_remove_empty_parts(map2
);
518 diff
= isl_map_empty_like(map1
);
519 for (i
= 0; i
< map1
->n
; ++i
) {
521 d
= basic_map_subtract(isl_basic_map_copy(map1
->p
[i
]),
523 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
))
524 diff
= isl_map_union_disjoint(diff
, d
);
526 diff
= isl_map_union(diff
, d
);
539 __isl_give isl_map
*isl_map_subtract( __isl_take isl_map
*map1
,
540 __isl_take isl_map
*map2
)
542 return isl_map_align_params_map_map_and(map1
, map2
, &map_subtract
);
545 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
547 return (struct isl_set
*)
549 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
552 /* Remove the elements of "dom" from the domain of "map".
554 static __isl_give isl_map
*map_subtract_domain(__isl_take isl_map
*map
,
555 __isl_take isl_set
*dom
)
559 if (!isl_map_compatible_domain(map
, dom
))
560 isl_die(isl_set_get_ctx(dom
), isl_error_invalid
,
561 "incompatible spaces", goto error
);
563 ext_dom
= isl_map_universe(isl_map_get_space(map
));
564 ext_dom
= isl_map_intersect_domain(ext_dom
, dom
);
565 return isl_map_subtract(map
, ext_dom
);
572 __isl_give isl_map
*isl_map_subtract_domain(__isl_take isl_map
*map
,
573 __isl_take isl_set
*dom
)
575 return isl_map_align_params_map_map_and(map
, dom
, &map_subtract_domain
);
578 /* Remove the elements of "dom" from the range of "map".
580 static __isl_give isl_map
*map_subtract_range(__isl_take isl_map
*map
,
581 __isl_take isl_set
*dom
)
585 if (!isl_map_compatible_range(map
, dom
))
586 isl_die(isl_set_get_ctx(dom
), isl_error_invalid
,
587 "incompatible spaces", goto error
);
589 ext_dom
= isl_map_universe(isl_map_get_space(map
));
590 ext_dom
= isl_map_intersect_range(ext_dom
, dom
);
591 return isl_map_subtract(map
, ext_dom
);
598 __isl_give isl_map
*isl_map_subtract_range(__isl_take isl_map
*map
,
599 __isl_take isl_set
*dom
)
601 return isl_map_align_params_map_map_and(map
, dom
, &map_subtract_range
);
604 /* A diff collector that aborts as soon as its add function is called,
605 * setting empty to 0.
607 struct isl_is_empty_diff_collector
{
608 struct isl_diff_collector dc
;
612 /* isl_is_empty_diff_collector callback.
614 static int basic_map_is_empty_add(struct isl_diff_collector
*dc
,
615 __isl_take isl_basic_map
*bmap
)
617 struct isl_is_empty_diff_collector
*edc
;
618 edc
= (struct isl_is_empty_diff_collector
*)dc
;
622 isl_basic_map_free(bmap
);
626 /* Check if bmap \ map is empty by computing this set difference
627 * and breaking off as soon as the difference is known to be non-empty.
629 static int basic_map_diff_is_empty(__isl_keep isl_basic_map
*bmap
,
630 __isl_keep isl_map
*map
)
633 struct isl_is_empty_diff_collector edc
;
635 r
= isl_basic_map_plain_is_empty(bmap
);
639 edc
.dc
.add
= &basic_map_is_empty_add
;
641 r
= basic_map_collect_diff(isl_basic_map_copy(bmap
),
642 isl_map_copy(map
), &edc
.dc
);
646 return r
< 0 ? -1 : 1;
649 /* Check if map1 \ map2 is empty by checking if the set difference is empty
650 * for each of the basic maps in map1.
652 static int map_diff_is_empty(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
660 for (i
= 0; i
< map1
->n
; ++i
) {
661 is_empty
= basic_map_diff_is_empty(map1
->p
[i
], map2
);
662 if (is_empty
< 0 || !is_empty
)
669 /* Return 1 if "bmap" contains a single element.
671 int isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map
*bmap
)
679 return bmap
->n_eq
== isl_basic_map_total_dim(bmap
);
682 /* Return 1 if "map" contains a single element.
684 int isl_map_plain_is_singleton(__isl_keep isl_map
*map
)
691 return isl_basic_map_plain_is_singleton(map
->p
[0]);
694 /* Given a singleton basic map, extract the single element
697 static __isl_give isl_point
*singleton_extract_point(
698 __isl_keep isl_basic_map
*bmap
)
702 struct isl_vec
*point
;
708 dim
= isl_basic_map_total_dim(bmap
);
709 isl_assert(bmap
->ctx
, bmap
->n_eq
== dim
, return NULL
);
710 point
= isl_vec_alloc(bmap
->ctx
, 1 + dim
);
716 isl_int_set_si(point
->el
[0], 1);
717 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
719 isl_assert(bmap
->ctx
,
720 isl_seq_first_non_zero(bmap
->eq
[j
] + 1, i
) == -1,
722 isl_assert(bmap
->ctx
,
723 isl_int_is_one(bmap
->eq
[j
][1 + i
]) ||
724 isl_int_is_negone(bmap
->eq
[j
][1 + i
]),
726 isl_assert(bmap
->ctx
,
727 isl_seq_first_non_zero(bmap
->eq
[j
]+1+i
+1, dim
-i
-1) == -1,
730 isl_int_gcd(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
731 isl_int_divexact(m
, bmap
->eq
[j
][1 + i
], m
);
733 isl_seq_scale(point
->el
, point
->el
, m
, 1 + i
);
734 isl_int_divexact(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
736 isl_int_mul(point
->el
[1 + i
], m
, bmap
->eq
[j
][0]);
740 return isl_point_alloc(isl_basic_map_get_space(bmap
), point
);
747 /* Return 1 is the singleton map "map1" is a subset of "map2",
748 * i.e., if the single element of "map1" is also an element of "map2".
749 * Assumes "map2" has known divs.
751 static int map_is_singleton_subset(__isl_keep isl_map
*map1
,
752 __isl_keep isl_map
*map2
)
756 struct isl_point
*point
;
763 point
= singleton_extract_point(map1
->p
[0]);
767 for (i
= 0; i
< map2
->n
; ++i
) {
768 is_subset
= isl_basic_map_contains_point(map2
->p
[i
], point
);
773 isl_point_free(point
);
777 static int map_is_subset(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
786 if (!isl_map_has_equal_space(map1
, map2
))
789 empty
= isl_map_is_empty(map1
);
795 empty
= isl_map_is_empty(map2
);
801 rat1
= isl_map_has_rational(map1
);
802 rat2
= isl_map_has_rational(map2
);
803 if (rat1
< 0 || rat2
< 0)
808 if (isl_map_plain_is_universe(map2
))
811 map2
= isl_map_compute_divs(isl_map_copy(map2
));
812 if (isl_map_plain_is_singleton(map1
)) {
813 is_subset
= map_is_singleton_subset(map1
, map2
);
817 is_subset
= map_diff_is_empty(map1
, map2
);
823 int isl_map_is_subset(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
825 return isl_map_align_params_map_map_and_test(map1
, map2
,
829 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
831 return isl_map_is_subset(
832 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
835 __isl_give isl_map
*isl_map_make_disjoint(__isl_take isl_map
*map
)
838 struct isl_subtract_diff_collector sdc
;
839 sdc
.dc
.add
= &basic_map_subtract_add
;
843 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
848 map
= isl_map_compute_divs(map
);
849 map
= isl_map_remove_empty_parts(map
);
851 if (!map
|| map
->n
<= 1)
854 sdc
.diff
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[0]));
856 for (i
= 1; i
< map
->n
; ++i
) {
857 struct isl_basic_map
*bmap
= isl_basic_map_copy(map
->p
[i
]);
858 struct isl_map
*copy
= isl_map_copy(sdc
.diff
);
859 if (basic_map_collect_diff(bmap
, copy
, &sdc
.dc
) < 0) {
860 isl_map_free(sdc
.diff
);
871 __isl_give isl_set
*isl_set_make_disjoint(__isl_take isl_set
*set
)
873 return (struct isl_set
*)isl_map_make_disjoint((struct isl_map
*)set
);
876 __isl_give isl_map
*isl_map_complement(__isl_take isl_map
*map
)
883 universe
= isl_map_universe(isl_map_get_space(map
));
885 return isl_map_subtract(universe
, map
);
888 __isl_give isl_set
*isl_set_complement(__isl_take isl_set
*set
)
890 return isl_map_complement(set
);