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 static void expand_constraint(isl_vec
*v
, unsigned dim
,
17 isl_int
*c
, int *div_map
, unsigned n_div
)
21 isl_seq_cpy(v
->el
, c
, 1 + dim
);
22 isl_seq_clr(v
->el
+ 1 + dim
, v
->size
- (1 + dim
));
24 for (i
= 0; i
< n_div
; ++i
)
25 isl_int_set(v
->el
[1 + dim
+ div_map
[i
]], c
[1 + dim
+ i
]);
28 /* Add all constraints of bmap to tab. The equalities of bmap
29 * are added as a pair of inequalities.
31 static int tab_add_constraints(struct isl_tab
*tab
,
32 __isl_keep isl_basic_map
*bmap
, int *div_map
)
43 tab_total
= isl_basic_map_total_dim(tab
->bmap
);
44 bmap_total
= isl_basic_map_total_dim(bmap
);
45 dim
= isl_dim_total(tab
->bmap
->dim
);
47 if (isl_tab_extend_cons(tab
, 2 * bmap
->n_eq
+ bmap
->n_ineq
) < 0)
50 v
= isl_vec_alloc(bmap
->ctx
, 1 + tab_total
);
52 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
53 expand_constraint(v
, dim
, bmap
->eq
[i
], div_map
, bmap
->n_div
);
54 if (isl_tab_add_ineq(tab
, v
->el
) < 0)
56 isl_seq_neg(bmap
->eq
[i
], bmap
->eq
[i
], 1 + bmap_total
);
57 expand_constraint(v
, dim
, bmap
->eq
[i
], div_map
, bmap
->n_div
);
58 if (isl_tab_add_ineq(tab
, v
->el
) < 0)
60 isl_seq_neg(bmap
->eq
[i
], bmap
->eq
[i
], 1 + bmap_total
);
65 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
66 expand_constraint(v
, dim
, bmap
->ineq
[i
], div_map
, bmap
->n_div
);
67 if (isl_tab_add_ineq(tab
, v
->el
) < 0)
80 /* Add a specific constraint of bmap (or its opposite) to tab.
81 * The position of the constraint is specified by "c", where
82 * the equalities of bmap are counted twice, once for the inequality
83 * that is equal to the equality, and once for its negation.
85 static int tab_add_constraint(struct isl_tab
*tab
,
86 __isl_keep isl_basic_map
*bmap
, int *div_map
, int c
, int oppose
)
97 tab_total
= isl_basic_map_total_dim(tab
->bmap
);
98 bmap_total
= isl_basic_map_total_dim(bmap
);
99 dim
= isl_dim_total(tab
->bmap
->dim
);
101 v
= isl_vec_alloc(bmap
->ctx
, 1 + tab_total
);
105 if (c
< 2 * bmap
->n_eq
) {
106 if ((c
% 2) != oppose
)
107 isl_seq_neg(bmap
->eq
[c
/2], bmap
->eq
[c
/2],
110 isl_int_sub_ui(bmap
->eq
[c
/2][0], bmap
->eq
[c
/2][0], 1);
111 expand_constraint(v
, dim
, bmap
->eq
[c
/2], div_map
, bmap
->n_div
);
112 r
= isl_tab_add_ineq(tab
, v
->el
);
114 isl_int_add_ui(bmap
->eq
[c
/2][0], bmap
->eq
[c
/2][0], 1);
115 if ((c
% 2) != oppose
)
116 isl_seq_neg(bmap
->eq
[c
/2], bmap
->eq
[c
/2],
121 isl_seq_neg(bmap
->ineq
[c
], bmap
->ineq
[c
],
123 isl_int_sub_ui(bmap
->ineq
[c
][0], bmap
->ineq
[c
][0], 1);
125 expand_constraint(v
, dim
, bmap
->ineq
[c
], div_map
, bmap
->n_div
);
126 r
= isl_tab_add_ineq(tab
, v
->el
);
128 isl_int_add_ui(bmap
->ineq
[c
][0], bmap
->ineq
[c
][0], 1);
129 isl_seq_neg(bmap
->ineq
[c
], bmap
->ineq
[c
],
138 static int tab_add_divs(struct isl_tab
*tab
, __isl_keep isl_basic_map
*bmap
,
152 *div_map
= isl_alloc_array(bmap
->ctx
, int, bmap
->n_div
);
156 total
= isl_basic_map_total_dim(tab
->bmap
);
157 dim
= total
- tab
->bmap
->n_div
;
158 vec
= isl_vec_alloc(bmap
->ctx
, 2 + total
+ bmap
->n_div
);
162 for (i
= 0; i
< bmap
->n_div
; ++i
) {
163 isl_seq_cpy(vec
->el
, bmap
->div
[i
], 2 + dim
);
164 isl_seq_clr(vec
->el
+ 2 + dim
, tab
->bmap
->n_div
);
165 for (j
= 0; j
< i
; ++j
)
166 isl_int_set(vec
->el
[2 + dim
+ (*div_map
)[j
]],
167 bmap
->div
[i
][2 + dim
+ j
]);
168 for (j
= 0; j
< tab
->bmap
->n_div
; ++j
)
169 if (isl_seq_eq(tab
->bmap
->div
[j
],
170 vec
->el
, 2 + dim
+ tab
->bmap
->n_div
))
173 if (j
== tab
->bmap
->n_div
) {
174 vec
->size
= 2 + dim
+ tab
->bmap
->n_div
;
175 if (isl_tab_add_div(tab
, vec
, NULL
, NULL
) < 0)
189 /* Freeze all constraints of tableau tab.
191 static int tab_freeze_constraints(struct isl_tab
*tab
)
195 for (i
= 0; i
< tab
->n_con
; ++i
)
196 if (isl_tab_freeze_constraint(tab
, i
) < 0)
202 /* Check for redundant constraints starting at offset.
203 * Put the indices of the redundant constraints in index
204 * and return the number of redundant constraints.
206 static int n_non_redundant(struct isl_tab
*tab
, int offset
, int **index
)
209 int n_test
= tab
->n_con
- offset
;
211 if (isl_tab_detect_redundant(tab
) < 0)
215 *index
= isl_alloc_array(tab
->mat
->ctx
, int, n_test
);
219 for (n
= 0, i
= 0; i
< n_test
; ++i
) {
221 r
= isl_tab_is_redundant(tab
, offset
+ i
);
232 /* basic_map_collect_diff calls add on each of the pieces of
233 * the set difference between bmap and map until the add method
234 * return a negative value.
236 struct isl_diff_collector
{
237 int (*add
)(struct isl_diff_collector
*dc
,
238 __isl_take isl_basic_map
*bmap
);
241 /* Compute the set difference between bmap and map and call
242 * dc->add on each of the piece until this function returns
244 * Return 0 on success and -1 on error. dc->add returning
245 * a negative value is treated as an error, but the calling
246 * function can interpret the results based on the state of dc.
248 * Assumes that both bmap and map have known divs.
250 * The difference is computed by a backtracking algorithm.
251 * Each level corresponds to a basic map in "map".
252 * When a node in entered for the first time, we check
253 * if the corresonding basic map intersects the current piece
254 * of "bmap". If not, we move to the next level.
255 * Otherwise, we split the current piece into as many
256 * pieces as there are non-redundant constraints of the current
257 * basic map in the intersection. Each of these pieces is
258 * handled by a child of the current node.
259 * In particular, if there are n non-redundant constraints,
260 * then for each 0 <= i < n, a piece is cut off by adding
261 * constraints 0 <= j < i and adding the opposite of constraint i.
262 * If there are no non-redundant constraints, meaning that the current
263 * piece is a subset of the current basic map, then we simply backtrack.
265 * In the leaves, we check if the remaining piece has any integer points
266 * and if so, pass it along to dc->add. As a special case, if nothing
267 * has been removed when we end up in a leaf, we simply pass along
268 * the original basic map.
270 static int basic_map_collect_diff(__isl_take isl_basic_map
*bmap
,
271 __isl_take isl_map
*map
, struct isl_diff_collector
*dc
)
278 struct isl_tab
*tab
= NULL
;
279 struct isl_tab_undo
**snap
= NULL
;
283 int **div_map
= NULL
;
285 empty
= isl_basic_map_is_empty(bmap
);
287 isl_basic_map_free(bmap
);
289 return empty
< 0 ? -1 : 0;
292 bmap
= isl_basic_map_cow(bmap
);
293 map
= isl_map_cow(map
);
298 snap
= isl_alloc_array(map
->ctx
, struct isl_tab_undo
*, map
->n
);
299 k
= isl_alloc_array(map
->ctx
, int, map
->n
);
300 n
= isl_alloc_array(map
->ctx
, int, map
->n
);
301 index
= isl_calloc_array(map
->ctx
, int *, map
->n
);
302 div_map
= isl_calloc_array(map
->ctx
, int *, map
->n
);
303 if (!snap
|| !k
|| !n
|| !index
|| !div_map
)
306 bmap
= isl_basic_map_order_divs(bmap
);
307 map
= isl_map_order_divs(map
);
309 tab
= isl_tab_from_basic_map(bmap
);
310 if (isl_tab_track_bmap(tab
, isl_basic_map_copy(bmap
)) < 0)
318 if (level
>= map
->n
) {
320 struct isl_basic_map
*bm
;
322 if (dc
->add(dc
, isl_basic_map_copy(bmap
)) < 0)
326 bm
= isl_basic_map_copy(tab
->bmap
);
327 bm
= isl_basic_map_cow(bm
);
328 bm
= isl_basic_map_update_from_tab(bm
, tab
);
329 bm
= isl_basic_map_simplify(bm
);
330 bm
= isl_basic_map_finalize(bm
);
331 empty
= isl_basic_map_is_empty(bm
);
333 isl_basic_map_free(bm
);
334 else if (dc
->add(dc
, bm
) < 0)
344 struct isl_tab_undo
*snap2
;
345 snap2
= isl_tab_snap(tab
);
346 if (tab_add_divs(tab
, map
->p
[level
],
347 &div_map
[level
]) < 0)
350 snap
[level
] = isl_tab_snap(tab
);
351 if (tab_freeze_constraints(tab
) < 0)
353 if (tab_add_constraints(tab
, map
->p
[level
],
359 if (isl_tab_rollback(tab
, snap2
) < 0)
365 n
[level
] = n_non_redundant(tab
, offset
, &index
[level
]);
373 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
375 if (tab_add_constraint(tab
, map
->p
[level
],
376 div_map
[level
], index
[level
][0], 1) < 0)
381 if (k
[level
] + 1 >= n
[level
]) {
385 if (isl_tab_rollback(tab
, snap
[level
]) < 0)
387 if (tab_add_constraint(tab
, map
->p
[level
],
389 index
[level
][k
[level
]], 0) < 0)
391 snap
[level
] = isl_tab_snap(tab
);
393 if (tab_add_constraint(tab
, map
->p
[level
],
395 index
[level
][k
[level
]], 1) < 0)
407 for (i
= 0; index
&& i
< map
->n
; ++i
)
410 for (i
= 0; div_map
&& i
< map
->n
; ++i
)
414 isl_basic_map_free(bmap
);
423 for (i
= 0; index
&& i
< map
->n
; ++i
)
426 for (i
= 0; div_map
&& i
< map
->n
; ++i
)
429 isl_basic_map_free(bmap
);
434 /* A diff collector that actually collects all parts of the
435 * set difference in the field diff.
437 struct isl_subtract_diff_collector
{
438 struct isl_diff_collector dc
;
439 struct isl_map
*diff
;
442 /* isl_subtract_diff_collector callback.
444 static int basic_map_subtract_add(struct isl_diff_collector
*dc
,
445 __isl_take isl_basic_map
*bmap
)
447 struct isl_subtract_diff_collector
*sdc
;
448 sdc
= (struct isl_subtract_diff_collector
*)dc
;
450 sdc
->diff
= isl_map_union_disjoint(sdc
->diff
,
451 isl_map_from_basic_map(bmap
));
453 return sdc
->diff
? 0 : -1;
456 /* Return the set difference between bmap and map.
458 static __isl_give isl_map
*basic_map_subtract(__isl_take isl_basic_map
*bmap
,
459 __isl_take isl_map
*map
)
461 struct isl_subtract_diff_collector sdc
;
462 sdc
.dc
.add
= &basic_map_subtract_add
;
463 sdc
.diff
= isl_map_empty_like_basic_map(bmap
);
464 if (basic_map_collect_diff(bmap
, map
, &sdc
.dc
) < 0) {
465 isl_map_free(sdc
.diff
);
471 /* Return the set difference between map1 and map2.
472 * (U_i A_i) \ (U_j B_j) is computed as U_i (A_i \ (U_j B_j))
474 struct isl_map
*isl_map_subtract(struct isl_map
*map1
, struct isl_map
*map2
)
477 struct isl_map
*diff
;
482 isl_assert(map1
->ctx
, isl_dim_equal(map1
->dim
, map2
->dim
), goto error
);
484 if (isl_map_is_empty(map2
)) {
489 map1
= isl_map_compute_divs(map1
);
490 map2
= isl_map_compute_divs(map2
);
494 map1
= isl_map_remove_empty_parts(map1
);
495 map2
= isl_map_remove_empty_parts(map2
);
497 diff
= isl_map_empty_like(map1
);
498 for (i
= 0; i
< map1
->n
; ++i
) {
500 d
= basic_map_subtract(isl_basic_map_copy(map1
->p
[i
]),
502 if (ISL_F_ISSET(map1
, ISL_MAP_DISJOINT
))
503 diff
= isl_map_union_disjoint(diff
, d
);
505 diff
= isl_map_union(diff
, d
);
518 struct isl_set
*isl_set_subtract(struct isl_set
*set1
, struct isl_set
*set2
)
520 return (struct isl_set
*)
522 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
525 /* A diff collector that aborts as soon as its add function is called,
526 * setting empty to 0.
528 struct isl_is_empty_diff_collector
{
529 struct isl_diff_collector dc
;
533 /* isl_is_empty_diff_collector callback.
535 static int basic_map_is_empty_add(struct isl_diff_collector
*dc
,
536 __isl_take isl_basic_map
*bmap
)
538 struct isl_is_empty_diff_collector
*edc
;
539 edc
= (struct isl_is_empty_diff_collector
*)dc
;
543 isl_basic_map_free(bmap
);
547 /* Check if bmap \ map is empty by computing this set difference
548 * and breaking off as soon as the difference is known to be non-empty.
550 static int basic_map_diff_is_empty(__isl_keep isl_basic_map
*bmap
,
551 __isl_keep isl_map
*map
)
554 struct isl_is_empty_diff_collector edc
;
556 r
= isl_basic_map_fast_is_empty(bmap
);
560 edc
.dc
.add
= &basic_map_is_empty_add
;
562 r
= basic_map_collect_diff(isl_basic_map_copy(bmap
),
563 isl_map_copy(map
), &edc
.dc
);
567 return r
< 0 ? -1 : 1;
570 /* Check if map1 \ map2 is empty by checking if the set difference is empty
571 * for each of the basic maps in map1.
573 static int map_diff_is_empty(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
581 for (i
= 0; i
< map1
->n
; ++i
) {
582 is_empty
= basic_map_diff_is_empty(map1
->p
[i
], map2
);
583 if (is_empty
< 0 || !is_empty
)
590 /* Return 1 if "bmap" contains a single element.
592 int isl_basic_map_is_singleton(__isl_keep isl_basic_map
*bmap
)
600 return bmap
->n_eq
== isl_basic_map_total_dim(bmap
);
603 /* Return 1 if "map" contains a single element.
605 int isl_map_is_singleton(__isl_keep isl_map
*map
)
612 return isl_basic_map_is_singleton(map
->p
[0]);
615 /* Given a singleton basic map, extract the single element
618 static __isl_give isl_vec
*singleton_extract_point(__isl_keep isl_basic_map
*bmap
)
622 struct isl_vec
*point
;
628 dim
= isl_basic_map_total_dim(bmap
);
629 isl_assert(bmap
->ctx
, bmap
->n_eq
== dim
, return NULL
);
630 point
= isl_vec_alloc(bmap
->ctx
, 1 + dim
);
636 isl_int_set_si(point
->el
[0], 1);
637 for (j
= 0; j
< bmap
->n_eq
; ++j
) {
640 isl_assert(bmap
->ctx
,
641 isl_seq_first_non_zero(bmap
->eq
[j
] + 1, i
) == -1,
643 isl_assert(bmap
->ctx
,
644 isl_int_is_one(bmap
->eq
[j
][1 + i
]) ||
645 isl_int_is_negone(bmap
->eq
[j
][1 + i
]),
647 isl_assert(bmap
->ctx
,
648 isl_seq_first_non_zero(bmap
->eq
[j
]+1+i
+1, dim
-i
-1) == -1,
651 isl_int_gcd(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
652 isl_int_divexact(m
, bmap
->eq
[j
][1 + i
], m
);
654 isl_seq_scale(point
->el
, point
->el
, m
, 1 + i
);
655 isl_int_divexact(m
, point
->el
[0], bmap
->eq
[j
][1 + i
]);
657 isl_int_mul(point
->el
[1 + i
], m
, bmap
->eq
[j
][0]);
668 /* Return 1 if "bmap" contains the point "point".
669 * "bmap" is assumed to have known divs.
670 * The point is first extended with the divs and then passed
671 * to basic_map_contains.
673 static int basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
674 __isl_keep isl_vec
*point
)
683 if (bmap
->n_div
== 0)
684 return isl_basic_map_contains(bmap
, point
);
686 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
687 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
691 isl_seq_cpy(vec
->el
, point
->el
, point
->size
);
692 for (i
= 0; i
< bmap
->n_div
; ++i
) {
693 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
694 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
695 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
699 contains
= isl_basic_map_contains(bmap
, vec
);
705 /* Return 1 is the singleton map "map1" is a subset of "map2",
706 * i.e., if the single element of "map1" is also an element of "map2".
708 static int map_is_singleton_subset(__isl_keep isl_map
*map1
,
709 __isl_keep isl_map
*map2
)
713 struct isl_vec
*point
;
720 point
= singleton_extract_point(map1
->p
[0]);
724 for (i
= 0; i
< map2
->n
; ++i
) {
725 is_subset
= basic_map_contains_point(map2
->p
[i
], point
);
734 int isl_map_is_subset(struct isl_map
*map1
, struct isl_map
*map2
)
737 struct isl_map
*diff
;
742 if (isl_map_is_empty(map1
))
745 if (isl_map_is_empty(map2
))
748 if (isl_map_fast_is_universe(map2
))
751 map1
= isl_map_compute_divs(isl_map_copy(map1
));
752 map2
= isl_map_compute_divs(isl_map_copy(map2
));
753 if (isl_map_is_singleton(map1
)) {
754 is_subset
= map_is_singleton_subset(map1
, map2
);
759 is_subset
= map_diff_is_empty(map1
, map2
);
766 int isl_set_is_subset(struct isl_set
*set1
, struct isl_set
*set2
)
768 return isl_map_is_subset(
769 (struct isl_map
*)set1
, (struct isl_map
*)set2
);
772 __isl_give isl_map
*isl_map_make_disjoint(__isl_take isl_map
*map
)
775 struct isl_subtract_diff_collector sdc
;
776 sdc
.dc
.add
= &basic_map_subtract_add
;
780 if (ISL_F_ISSET(map
, ISL_MAP_DISJOINT
))
785 map
= isl_map_compute_divs(map
);
786 map
= isl_map_remove_empty_parts(map
);
788 if (!map
|| map
->n
<= 1)
791 sdc
.diff
= isl_map_from_basic_map(isl_basic_map_copy(map
->p
[0]));
793 for (i
= 1; i
< map
->n
; ++i
) {
794 struct isl_basic_map
*bmap
= isl_basic_map_copy(map
->p
[i
]);
795 struct isl_map
*copy
= isl_map_copy(sdc
.diff
);
796 if (basic_map_collect_diff(bmap
, copy
, &sdc
.dc
) < 0) {
797 isl_map_free(sdc
.diff
);
808 __isl_give isl_set
*isl_set_make_disjoint(__isl_take isl_set
*set
)
810 return (struct isl_set
*)isl_map_make_disjoint((struct isl_map
*)set
);
813 __isl_give isl_set
*isl_set_complement(__isl_take isl_set
*set
)
820 universe
= isl_set_universe(isl_set_get_dim(set
));
822 return isl_set_subtract(universe
, set
);