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>
16 #include <isl_vec_private.h>
18 /* Expand the constraint "c" into "v". The initial "dim" dimensions
19 * are the same, but "v" may have more divs than "c" and the divs of "c"
20 * may appear in different positions in "v".
21 * The number of divs in "c" is given by "n_div" and the mapping
22 * of divs in "c" to divs in "v" is given by "div_map".
24 * Although it shouldn't happen in practice, it is theoretically
25 * possible that two or more divs in "c" are mapped to the same div in "v".
26 * These divs are then necessarily the same, so we simply add their
29 static void expand_constraint(isl_vec
*v
, unsigned dim
,
30 isl_int
*c
, int *div_map
, unsigned n_div
)
34 isl_seq_cpy(v
->el
, c
, 1 + dim
);
35 isl_seq_clr(v
->el
+ 1 + dim
, v
->size
- (1 + dim
));
37 for (i
= 0; i
< n_div
; ++i
) {
38 int pos
= 1 + dim
+ div_map
[i
];
39 isl_int_add(v
->el
[pos
], v
->el
[pos
], c
[1 + dim
+ i
]);
43 /* Add all constraints of bmap to tab. The equalities of bmap
44 * are added as a pair of inequalities.
46 static int tab_add_constraints(struct isl_tab
*tab
,
47 __isl_keep isl_basic_map
*bmap
, int *div_map
)
58 tab_total
= isl_basic_map_total_dim(tab
->bmap
);
59 bmap_total
= isl_basic_map_total_dim(bmap
);
60 dim
= isl_space_dim(tab
->bmap
->dim
, isl_dim_all
);
62 if (isl_tab_extend_cons(tab
, 2 * bmap
->n_eq
+ bmap
->n_ineq
) < 0)
65 v
= isl_vec_alloc(bmap
->ctx
, 1 + tab_total
);
69 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
70 expand_constraint(v
, dim
, bmap
->eq
[i
], div_map
, bmap
->n_div
);
71 if (isl_tab_add_ineq(tab
, v
->el
) < 0)
73 isl_seq_neg(bmap
->eq
[i
], bmap
->eq
[i
], 1 + bmap_total
);
74 expand_constraint(v
, dim
, bmap
->eq
[i
], div_map
, bmap
->n_div
);
75 if (isl_tab_add_ineq(tab
, v
->el
) < 0)
77 isl_seq_neg(bmap
->eq
[i
], bmap
->eq
[i
], 1 + bmap_total
);
82 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
83 expand_constraint(v
, dim
, bmap
->ineq
[i
], div_map
, bmap
->n_div
);
84 if (isl_tab_add_ineq(tab
, v
->el
) < 0)
97 /* Add a specific constraint of bmap (or its opposite) to tab.
98 * The position of the constraint is specified by "c", where
99 * the equalities of bmap are counted twice, once for the inequality
100 * that is equal to the equality, and once for its negation.
102 static int tab_add_constraint(struct isl_tab
*tab
,
103 __isl_keep isl_basic_map
*bmap
, int *div_map
, int c
, int oppose
)
114 tab_total
= isl_basic_map_total_dim(tab
->bmap
);
115 bmap_total
= isl_basic_map_total_dim(bmap
);
116 dim
= isl_space_dim(tab
->bmap
->dim
, isl_dim_all
);
118 v
= isl_vec_alloc(bmap
->ctx
, 1 + tab_total
);
122 if (c
< 2 * bmap
->n_eq
) {
123 if ((c
% 2) != oppose
)
124 isl_seq_neg(bmap
->eq
[c
/2], bmap
->eq
[c
/2],
127 isl_int_sub_ui(bmap
->eq
[c
/2][0], bmap
->eq
[c
/2][0], 1);
128 expand_constraint(v
, dim
, bmap
->eq
[c
/2], div_map
, bmap
->n_div
);
129 r
= isl_tab_add_ineq(tab
, v
->el
);
131 isl_int_add_ui(bmap
->eq
[c
/2][0], bmap
->eq
[c
/2][0], 1);
132 if ((c
% 2) != oppose
)
133 isl_seq_neg(bmap
->eq
[c
/2], bmap
->eq
[c
/2],
138 isl_seq_neg(bmap
->ineq
[c
], bmap
->ineq
[c
],
140 isl_int_sub_ui(bmap
->ineq
[c
][0], bmap
->ineq
[c
][0], 1);
142 expand_constraint(v
, dim
, bmap
->ineq
[c
], div_map
, bmap
->n_div
);
143 r
= isl_tab_add_ineq(tab
, v
->el
);
145 isl_int_add_ui(bmap
->ineq
[c
][0], bmap
->ineq
[c
][0], 1);
146 isl_seq_neg(bmap
->ineq
[c
], bmap
->ineq
[c
],
155 static int tab_add_divs(struct isl_tab
*tab
, __isl_keep isl_basic_map
*bmap
,
169 *div_map
= isl_alloc_array(bmap
->ctx
, int, bmap
->n_div
);
173 total
= isl_basic_map_total_dim(tab
->bmap
);
174 dim
= total
- tab
->bmap
->n_div
;
175 vec
= isl_vec_alloc(bmap
->ctx
, 2 + total
+ bmap
->n_div
);
179 for (i
= 0; i
< bmap
->n_div
; ++i
) {
180 isl_seq_cpy(vec
->el
, bmap
->div
[i
], 2 + dim
);
181 isl_seq_clr(vec
->el
+ 2 + dim
, tab
->bmap
->n_div
);
182 for (j
= 0; j
< i
; ++j
)
183 isl_int_set(vec
->el
[2 + dim
+ (*div_map
)[j
]],
184 bmap
->div
[i
][2 + dim
+ j
]);
185 for (j
= 0; j
< tab
->bmap
->n_div
; ++j
)
186 if (isl_seq_eq(tab
->bmap
->div
[j
],
187 vec
->el
, 2 + dim
+ tab
->bmap
->n_div
))
190 if (j
== tab
->bmap
->n_div
) {
191 vec
->size
= 2 + dim
+ tab
->bmap
->n_div
;
192 if (isl_tab_add_div(tab
, vec
, NULL
, NULL
) < 0)
206 /* Freeze all constraints of tableau tab.
208 static int tab_freeze_constraints(struct isl_tab
*tab
)
212 for (i
= 0; i
< tab
->n_con
; ++i
)
213 if (isl_tab_freeze_constraint(tab
, i
) < 0)
219 /* Check for redundant constraints starting at offset.
220 * Put the indices of the redundant constraints in index
221 * and return the number of redundant constraints.
223 static int n_non_redundant(isl_ctx
*ctx
, struct isl_tab
*tab
,
224 int offset
, int **index
)
227 int n_test
= tab
->n_con
- offset
;
229 if (isl_tab_detect_redundant(tab
) < 0)
235 *index
= isl_alloc_array(ctx
, int, n_test
);
239 for (n
= 0, i
= 0; i
< n_test
; ++i
) {
241 r
= isl_tab_is_redundant(tab
, offset
+ i
);
252 /* basic_map_collect_diff calls add on each of the pieces of
253 * the set difference between bmap and map until the add method
254 * return a negative value.
256 struct isl_diff_collector
{
257 int (*add
)(struct isl_diff_collector
*dc
,
258 __isl_take isl_basic_map
*bmap
);
261 /* Compute the set difference between bmap and map and call
262 * dc->add on each of the piece until this function returns
264 * Return 0 on success and -1 on error. dc->add returning
265 * a negative value is treated as an error, but the calling
266 * function can interpret the results based on the state of dc.
268 * Assumes that map has known divs.
270 * The difference is computed by a backtracking algorithm.
271 * Each level corresponds to a basic map in "map".
272 * When a node in entered for the first time, we check
273 * if the corresonding basic map intersects the current piece
274 * of "bmap". If not, we move to the next level.
275 * Otherwise, we split the current piece into as many
276 * pieces as there are non-redundant constraints of the current
277 * basic map in the intersection. Each of these pieces is
278 * handled by a child of the current node.
279 * In particular, if there are n non-redundant constraints,
280 * then for each 0 <= i < n, a piece is cut off by adding
281 * constraints 0 <= j < i and adding the opposite of constraint i.
282 * If there are no non-redundant constraints, meaning that the current
283 * piece is a subset of the current basic map, then we simply backtrack.
285 * In the leaves, we check if the remaining piece has any integer points
286 * and if so, pass it along to dc->add. As a special case, if nothing
287 * has been removed when we end up in a leaf, we simply pass along
288 * the original basic map.
290 static int basic_map_collect_diff(__isl_take isl_basic_map
*bmap
,
291 __isl_take isl_map
*map
, struct isl_diff_collector
*dc
)
299 struct isl_tab
*tab
= NULL
;
300 struct isl_tab_undo
**snap
= NULL
;
304 int **div_map
= NULL
;
306 empty
= isl_basic_map_is_empty(bmap
);
308 isl_basic_map_free(bmap
);
310 return empty
< 0 ? -1 : 0;
313 bmap
= isl_basic_map_cow(bmap
);
314 map
= isl_map_cow(map
);
320 snap
= isl_alloc_array(map
->ctx
, struct isl_tab_undo
*, map
->n
);
321 k
= isl_alloc_array(map
->ctx
, int, map
->n
);
322 n
= isl_alloc_array(map
->ctx
, int, map
->n
);
323 index
= isl_calloc_array(map
->ctx
, int *, map
->n
);
324 div_map
= isl_calloc_array(map
->ctx
, int *, map
->n
);
325 if (!snap
|| !k
|| !n
|| !index
|| !div_map
)
328 bmap
= isl_basic_map_order_divs(bmap
);
329 map
= isl_map_order_divs(map
);
331 tab
= isl_tab_from_basic_map(bmap
, 1);
340 if (level
>= map
->n
) {
342 struct isl_basic_map
*bm
;
344 if (dc
->add(dc
, isl_basic_map_copy(bmap
)) < 0)
348 bm
= isl_basic_map_copy(tab
->bmap
);
349 bm
= isl_basic_map_cow(bm
);
350 bm
= isl_basic_map_update_from_tab(bm
, tab
);
351 bm
= isl_basic_map_simplify(bm
);
352 bm
= isl_basic_map_finalize(bm
);
353 empty
= isl_basic_map_is_empty(bm
);
355 isl_basic_map_free(bm
);
356 else if (dc
->add(dc
, bm
) < 0)
366 struct isl_tab_undo
*snap2
;
367 snap2
= isl_tab_snap(tab
);
368 if (tab_add_divs(tab
, map
->p
[level
],
369 &div_map
[level
]) < 0)
372 snap
[level
] = isl_tab_snap(tab
);
373 if (tab_freeze_constraints(tab
) < 0)
375 if (tab_add_constraints(tab
, map
->p
[level
],
381 if (isl_tab_rollback(tab
, snap2
) < 0)
387 n
[level
] = n_non_redundant(ctx
, tab
, offset
,
396 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
398 if (tab_add_constraint(tab
, map
->p
[level
],
399 div_map
[level
], index
[level
][0], 1) < 0)
404 if (k
[level
] + 1 >= n
[level
]) {
408 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
410 if (tab_add_constraint(tab
, map
->p
[level
],
412 index
[level
][k
[level
]], 0) < 0)
414 snap
[level
] = isl_tab_snap(tab
);
416 if (tab_add_constraint(tab
, map
->p
[level
],
418 index
[level
][k
[level
]], 1) < 0)
430 for (i
= 0; index
&& i
< map
->n
; ++i
)
433 for (i
= 0; div_map
&& i
< map
->n
; ++i
)
437 isl_basic_map_free(bmap
);
446 for (i
= 0; index
&& i
< map
->n
; ++i
)
449 for (i
= 0; div_map
&& i
< map
->n
; ++i
)
452 isl_basic_map_free(bmap
);
457 /* A diff collector that actually collects all parts of the
458 * set difference in the field diff.
460 struct isl_subtract_diff_collector
{
461 struct isl_diff_collector dc
;
462 struct isl_map
*diff
;
465 /* isl_subtract_diff_collector callback.
467 static int basic_map_subtract_add(struct isl_diff_collector
*dc
,
468 __isl_take isl_basic_map
*bmap
)
470 struct isl_subtract_diff_collector
*sdc
;
471 sdc
= (struct isl_subtract_diff_collector
*)dc
;
473 sdc
->diff
= isl_map_union_disjoint(sdc
->diff
,
474 isl_map_from_basic_map(bmap
));
476 return sdc
->diff
? 0 : -1;
479 /* Return the set difference between bmap and map.
481 static __isl_give isl_map
*basic_map_subtract(__isl_take isl_basic_map
*bmap
,
482 __isl_take isl_map
*map
)
484 struct isl_subtract_diff_collector sdc
;
485 sdc
.dc
.add
= &basic_map_subtract_add
;
486 sdc
.diff
= isl_map_empty(isl_basic_map_get_space(bmap
));
487 if (basic_map_collect_diff(bmap
, map
, &sdc
.dc
) < 0) {
488 isl_map_free(sdc
.diff
);
494 /* Return an empty map living in the same space as "map1" and "map2".
496 static __isl_give isl_map
*replace_pair_by_empty( __isl_take isl_map
*map1
,
497 __isl_take isl_map
*map2
)
501 space
= isl_map_get_space(map1
);
504 return isl_map_empty(space
);
507 /* Return the set difference between map1 and map2.
508 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
510 * If "map1" and "map2" are obviously equal to each other,
511 * then return an empty map in the same space.
513 * If "map1" and "map2" are disjoint, then simply return "map1".
515 static __isl_give isl_map
*map_subtract( __isl_take isl_map
*map1
,
516 __isl_take isl_map
*map2
)
520 struct isl_map
*diff
;
525 isl_assert(map1
->ctx
, isl_space_is_equal(map1
->dim
, map2
->dim
), goto error
);
527 equal
= isl_map_plain_is_equal(map1
, map2
);
531 return replace_pair_by_empty(map1
, map2
);
533 disjoint
= isl_map_is_disjoint(map1
, map2
);
541 map1
= isl_map_compute_divs(map1
);
542 map2
= isl_map_compute_divs(map2
);
546 map1
= isl_map_remove_empty_parts(map1
);
547 map2
= isl_map_remove_empty_parts(map2
);
549 diff
= isl_map_empty(isl_map_get_space(map1
));
550 for (i
= 0; i
< map1
->n
; ++i
) {
552 d
= basic_map_subtract(isl_basic_map_copy(map1
->p
[i
]),
554 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
))
555 diff
= isl_map_union_disjoint(diff
, d
);
557 diff
= isl_map_union(diff
, d
);
570 __isl_give isl_map
*isl_map_subtract( __isl_take isl_map
*map1
,
571 __isl_take isl_map
*map2
)
573 return isl_map_align_params_map_map_and(map1
, map2
, &map_subtract
);
576 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
578 return (struct isl_set
*)
580 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
583 /* Remove the elements of "dom" from the domain of "map".
585 static __isl_give isl_map
*map_subtract_domain(__isl_take isl_map
*map
,
586 __isl_take isl_set
*dom
)
590 if (!isl_map_compatible_domain(map
, dom
))
591 isl_die(isl_set_get_ctx(dom
), isl_error_invalid
,
592 "incompatible spaces", goto error
);
594 ext_dom
= isl_map_universe(isl_map_get_space(map
));
595 ext_dom
= isl_map_intersect_domain(ext_dom
, dom
);
596 return isl_map_subtract(map
, ext_dom
);
603 __isl_give isl_map
*isl_map_subtract_domain(__isl_take isl_map
*map
,
604 __isl_take isl_set
*dom
)
606 return isl_map_align_params_map_map_and(map
, dom
, &map_subtract_domain
);
609 /* Remove the elements of "dom" from the range of "map".
611 static __isl_give isl_map
*map_subtract_range(__isl_take isl_map
*map
,
612 __isl_take isl_set
*dom
)
616 if (!isl_map_compatible_range(map
, dom
))
617 isl_die(isl_set_get_ctx(dom
), isl_error_invalid
,
618 "incompatible spaces", goto error
);
620 ext_dom
= isl_map_universe(isl_map_get_space(map
));
621 ext_dom
= isl_map_intersect_range(ext_dom
, dom
);
622 return isl_map_subtract(map
, ext_dom
);
629 __isl_give isl_map
*isl_map_subtract_range(__isl_take isl_map
*map
,
630 __isl_take isl_set
*dom
)
632 return isl_map_align_params_map_map_and(map
, dom
, &map_subtract_range
);
635 /* A diff collector that aborts as soon as its add function is called,
636 * setting empty to 0.
638 struct isl_is_empty_diff_collector
{
639 struct isl_diff_collector dc
;
643 /* isl_is_empty_diff_collector callback.
645 static int basic_map_is_empty_add(struct isl_diff_collector
*dc
,
646 __isl_take isl_basic_map
*bmap
)
648 struct isl_is_empty_diff_collector
*edc
;
649 edc
= (struct isl_is_empty_diff_collector
*)dc
;
653 isl_basic_map_free(bmap
);
657 /* Check if bmap \ map is empty by computing this set difference
658 * and breaking off as soon as the difference is known to be non-empty.
660 static int basic_map_diff_is_empty(__isl_keep isl_basic_map
*bmap
,
661 __isl_keep isl_map
*map
)
664 struct isl_is_empty_diff_collector edc
;
666 r
= isl_basic_map_plain_is_empty(bmap
);
670 edc
.dc
.add
= &basic_map_is_empty_add
;
672 r
= basic_map_collect_diff(isl_basic_map_copy(bmap
),
673 isl_map_copy(map
), &edc
.dc
);
677 return r
< 0 ? -1 : 1;
680 /* Check if map1 \ map2 is empty by checking if the set difference is empty
681 * for each of the basic maps in map1.
683 static int map_diff_is_empty(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
691 for (i
= 0; i
< map1
->n
; ++i
) {
692 is_empty
= basic_map_diff_is_empty(map1
->p
[i
], map2
);
693 if (is_empty
< 0 || !is_empty
)
700 /* Return 1 if "bmap" contains a single element.
702 int isl_basic_map_plain_is_singleton(__isl_keep isl_basic_map
*bmap
)
710 return bmap
->n_eq
== isl_basic_map_total_dim(bmap
);
713 /* Return 1 if "map" contains a single element.
715 int isl_map_plain_is_singleton(__isl_keep isl_map
*map
)
722 return isl_basic_map_plain_is_singleton(map
->p
[0]);
725 /* Given a singleton basic map, extract the single element
728 static __isl_give isl_point
*singleton_extract_point(
729 __isl_keep isl_basic_map
*bmap
)
733 struct isl_vec
*point
;
739 dim
= isl_basic_map_total_dim(bmap
);
740 isl_assert(bmap
->ctx
, bmap
->n_eq
== dim
, return NULL
);
741 point
= isl_vec_alloc(bmap
->ctx
, 1 + dim
);
747 isl_int_set_si(point
->el
[0], 1);
748 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
750 isl_assert(bmap
->ctx
,
751 isl_seq_first_non_zero(bmap
->eq
[j
] + 1, i
) == -1,
753 isl_assert(bmap
->ctx
,
754 isl_int_is_one(bmap
->eq
[j
][1 + i
]) ||
755 isl_int_is_negone(bmap
->eq
[j
][1 + i
]),
757 isl_assert(bmap
->ctx
,
758 isl_seq_first_non_zero(bmap
->eq
[j
]+1+i
+1, dim
-i
-1) == -1,
761 isl_int_gcd(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
762 isl_int_divexact(m
, bmap
->eq
[j
][1 + i
], m
);
764 isl_seq_scale(point
->el
, point
->el
, m
, 1 + i
);
765 isl_int_divexact(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
767 isl_int_mul(point
->el
[1 + i
], m
, bmap
->eq
[j
][0]);
771 return isl_point_alloc(isl_basic_map_get_space(bmap
), point
);
778 /* Return 1 if the singleton map "map1" is a subset of "map2",
779 * i.e., if the single element of "map1" is also an element of "map2".
780 * Assumes "map2" has known divs.
782 static int map_is_singleton_subset(__isl_keep isl_map
*map1
,
783 __isl_keep isl_map
*map2
)
787 struct isl_point
*point
;
792 isl_die(isl_map_get_ctx(map1
), isl_error_internal
,
793 "expecting single-disjunct input", return -1);
795 point
= singleton_extract_point(map1
->p
[0]);
799 for (i
= 0; i
< map2
->n
; ++i
) {
800 is_subset
= isl_basic_map_contains_point(map2
->p
[i
], point
);
805 isl_point_free(point
);
809 static int map_is_subset(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
818 if (!isl_map_has_equal_space(map1
, map2
))
821 empty
= isl_map_is_empty(map1
);
827 empty
= isl_map_is_empty(map2
);
833 rat1
= isl_map_has_rational(map1
);
834 rat2
= isl_map_has_rational(map2
);
835 if (rat1
< 0 || rat2
< 0)
840 if (isl_map_plain_is_universe(map2
))
843 map2
= isl_map_compute_divs(isl_map_copy(map2
));
844 if (isl_map_plain_is_singleton(map1
)) {
845 is_subset
= map_is_singleton_subset(map1
, map2
);
849 is_subset
= map_diff_is_empty(map1
, map2
);
855 int isl_map_is_subset(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
857 return isl_map_align_params_map_map_and_test(map1
, map2
,
861 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
863 return isl_map_is_subset(
864 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
867 __isl_give isl_map
*isl_map_make_disjoint(__isl_take isl_map
*map
)
870 struct isl_subtract_diff_collector sdc
;
871 sdc
.dc
.add
= &basic_map_subtract_add
;
875 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
880 map
= isl_map_compute_divs(map
);
881 map
= isl_map_remove_empty_parts(map
);
883 if (!map
|| map
->n
<= 1)
886 sdc
.diff
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[0]));
888 for (i
= 1; i
< map
->n
; ++i
) {
889 struct isl_basic_map
*bmap
= isl_basic_map_copy(map
->p
[i
]);
890 struct isl_map
*copy
= isl_map_copy(sdc
.diff
);
891 if (basic_map_collect_diff(bmap
, copy
, &sdc
.dc
) < 0) {
892 isl_map_free(sdc
.diff
);
903 __isl_give isl_set
*isl_set_make_disjoint(__isl_take isl_set
*set
)
905 return (struct isl_set
*)isl_map_make_disjoint((struct isl_map
*)set
);
908 __isl_give isl_map
*isl_map_complement(__isl_take isl_map
*map
)
915 universe
= isl_map_universe(isl_map_get_space(map
));
917 return isl_map_subtract(universe
, map
);
920 __isl_give isl_set
*isl_set_complement(__isl_take isl_set
*set
)
922 return isl_map_complement(set
);