2 * Copyright 2010-2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_map_private.h>
17 #include <isl_space_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl/union_set.h>
21 /* Is this union set a parameter domain?
23 int isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
30 if (uset
->table
.n
!= 1)
33 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
34 params
= isl_set_is_params(set
);
39 static __isl_give isl_union_map
*isl_union_map_alloc(__isl_take isl_space
*dim
,
47 umap
= isl_calloc_type(dim
->ctx
, isl_union_map
);
53 if (isl_hash_table_init(dim
->ctx
, &umap
->table
, size
) < 0)
59 isl_union_map_free(umap
);
63 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
65 return isl_union_map_alloc(dim
, 16);
68 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
70 return isl_union_map_empty(dim
);
73 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
75 return umap
? umap
->dim
->ctx
: NULL
;
78 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
80 return uset
? uset
->dim
->ctx
: NULL
;
83 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
87 return isl_space_copy(umap
->dim
);
90 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
92 return isl_union_map_get_space(uset
);
95 static int free_umap_entry(void **entry
, void *user
)
97 isl_map
*map
= *entry
;
102 static int add_map(__isl_take isl_map
*map
, void *user
)
104 isl_union_map
**umap
= (isl_union_map
**)user
;
106 *umap
= isl_union_map_add_map(*umap
, map
);
111 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
118 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
119 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
123 isl_union_map_free(dup
);
127 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
135 return isl_union_map_dup(umap
);
138 struct isl_union_align
{
143 static int align_entry(void **entry
, void *user
)
145 isl_map
*map
= *entry
;
147 struct isl_union_align
*data
= user
;
149 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
150 isl_map_get_space(map
));
152 data
->res
= isl_union_map_add_map(data
->res
,
153 isl_map_realign(isl_map_copy(map
), exp
));
158 /* Align the parameters of umap along those of model.
159 * The result has the parameters of model first, in the same order
160 * as they appear in model, followed by any remaining parameters of
161 * umap that do not appear in model.
163 __isl_give isl_union_map
*isl_union_map_align_params(
164 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
166 struct isl_union_align data
= { NULL
, NULL
};
171 if (isl_space_match(umap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
172 isl_space_free(model
);
176 model
= isl_space_params(model
);
177 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
181 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
183 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
184 &align_entry
, &data
) < 0)
187 isl_reordering_free(data
.exp
);
188 isl_union_map_free(umap
);
189 isl_space_free(model
);
192 isl_reordering_free(data
.exp
);
193 isl_union_map_free(umap
);
194 isl_union_map_free(data
.res
);
195 isl_space_free(model
);
199 __isl_give isl_union_set
*isl_union_set_align_params(
200 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
202 return isl_union_map_align_params(uset
, model
);
205 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
206 __isl_take isl_union_map
*umap2
)
208 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
209 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
211 umap1
= isl_union_map_cow(umap1
);
213 if (!umap1
|| !umap2
)
216 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
219 isl_union_map_free(umap2
);
223 isl_union_map_free(umap1
);
224 isl_union_map_free(umap2
);
228 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
229 __isl_take isl_union_set
*uset2
)
231 return isl_union_map_union(uset1
, uset2
);
234 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
243 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
245 return isl_union_map_copy(uset
);
248 void *isl_union_map_free(__isl_take isl_union_map
*umap
)
256 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
257 &free_umap_entry
, NULL
);
258 isl_hash_table_clear(&umap
->table
);
259 isl_space_free(umap
->dim
);
264 void *isl_union_set_free(__isl_take isl_union_set
*uset
)
266 return isl_union_map_free(uset
);
269 static int has_dim(const void *entry
, const void *val
)
271 isl_map
*map
= (isl_map
*)entry
;
272 isl_space
*dim
= (isl_space
*)val
;
274 return isl_space_is_equal(map
->dim
, dim
);
277 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
278 __isl_take isl_map
*map
)
281 struct isl_hash_table_entry
*entry
;
286 if (isl_map_plain_is_empty(map
)) {
291 if (!isl_space_match(map
->dim
, isl_dim_param
, umap
->dim
, isl_dim_param
)) {
292 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
293 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
296 umap
= isl_union_map_cow(umap
);
301 hash
= isl_space_get_hash(map
->dim
);
302 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
303 &has_dim
, map
->dim
, 1);
310 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
319 isl_union_map_free(umap
);
323 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
324 __isl_take isl_set
*set
)
326 return isl_union_map_add_map(uset
, (isl_map
*)set
);
329 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
337 dim
= isl_map_get_space(map
);
338 dim
= isl_space_params(dim
);
339 umap
= isl_union_map_empty(dim
);
340 umap
= isl_union_map_add_map(umap
, map
);
345 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
347 return isl_union_map_from_map((isl_map
*)set
);
350 __isl_give isl_union_map
*isl_union_map_from_basic_map(
351 __isl_take isl_basic_map
*bmap
)
353 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
356 __isl_give isl_union_set
*isl_union_set_from_basic_set(
357 __isl_take isl_basic_set
*bset
)
359 return isl_union_map_from_basic_map(bset
);
362 struct isl_union_map_foreach_data
364 int (*fn
)(__isl_take isl_map
*map
, void *user
);
368 static int call_on_copy(void **entry
, void *user
)
370 isl_map
*map
= *entry
;
371 struct isl_union_map_foreach_data
*data
;
372 data
= (struct isl_union_map_foreach_data
*)user
;
374 return data
->fn(isl_map_copy(map
), data
->user
);
377 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
379 return umap
? umap
->table
.n
: 0;
382 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
384 return uset
? uset
->table
.n
: 0;
387 int isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
388 int (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
390 struct isl_union_map_foreach_data data
= { fn
, user
};
395 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
396 &call_on_copy
, &data
);
399 static int copy_map(void **entry
, void *user
)
401 isl_map
*map
= *entry
;
402 isl_map
**map_p
= user
;
404 *map_p
= isl_map_copy(map
);
409 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
416 ctx
= isl_union_map_get_ctx(umap
);
417 if (umap
->table
.n
!= 1)
418 isl_die(ctx
, isl_error_invalid
,
419 "union map needs to contain elements in exactly "
420 "one space", return isl_union_map_free(umap
));
422 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
424 isl_union_map_free(umap
);
429 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
431 return isl_map_from_union_map(uset
);
434 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
435 __isl_take isl_space
*dim
)
438 struct isl_hash_table_entry
*entry
;
443 hash
= isl_space_get_hash(dim
);
444 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
447 return isl_map_empty(dim
);
449 return isl_map_copy(entry
->data
);
455 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
456 __isl_take isl_space
*dim
)
458 return (isl_set
*)isl_union_map_extract_map(uset
, dim
);
461 /* Check if umap contains a map in the given space.
463 __isl_give
int isl_union_map_contains(__isl_keep isl_union_map
*umap
,
464 __isl_keep isl_space
*dim
)
467 struct isl_hash_table_entry
*entry
;
472 hash
= isl_space_get_hash(dim
);
473 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
478 __isl_give
int isl_union_set_contains(__isl_keep isl_union_set
*uset
,
479 __isl_keep isl_space
*dim
)
481 return isl_union_map_contains(uset
, dim
);
484 int isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
485 int (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
487 return isl_union_map_foreach_map(uset
,
488 (int(*)(__isl_take isl_map
*, void*))fn
, user
);
491 struct isl_union_set_foreach_point_data
{
492 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
496 static int foreach_point(__isl_take isl_set
*set
, void *user
)
498 struct isl_union_set_foreach_point_data
*data
= user
;
501 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
507 int isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
508 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
510 struct isl_union_set_foreach_point_data data
= { fn
, user
};
511 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
514 struct isl_union_map_gen_bin_data
{
515 isl_union_map
*umap2
;
519 static int subtract_entry(void **entry
, void *user
)
521 struct isl_union_map_gen_bin_data
*data
= user
;
523 struct isl_hash_table_entry
*entry2
;
524 isl_map
*map
= *entry
;
526 hash
= isl_space_get_hash(map
->dim
);
527 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
528 hash
, &has_dim
, map
->dim
, 0);
529 map
= isl_map_copy(map
);
532 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
534 empty
= isl_map_is_empty(map
);
544 data
->res
= isl_union_map_add_map(data
->res
, map
);
549 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
550 __isl_take isl_union_map
*umap2
, int (*fn
)(void **, void *))
552 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
554 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
555 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
557 if (!umap1
|| !umap2
)
561 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
563 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
567 isl_union_map_free(umap1
);
568 isl_union_map_free(umap2
);
571 isl_union_map_free(umap1
);
572 isl_union_map_free(umap2
);
573 isl_union_map_free(data
.res
);
577 __isl_give isl_union_map
*isl_union_map_subtract(
578 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
580 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
583 __isl_give isl_union_set
*isl_union_set_subtract(
584 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
586 return isl_union_map_subtract(uset1
, uset2
);
589 struct isl_union_map_gen_bin_set_data
{
594 static int intersect_params_entry(void **entry
, void *user
)
596 struct isl_union_map_gen_bin_set_data
*data
= user
;
597 isl_map
*map
= *entry
;
600 map
= isl_map_copy(map
);
601 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
603 empty
= isl_map_is_empty(map
);
609 data
->res
= isl_union_map_add_map(data
->res
, map
);
614 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
615 __isl_take isl_set
*set
, int (*fn
)(void **, void *))
617 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
619 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
620 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
626 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
628 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
632 isl_union_map_free(umap
);
636 isl_union_map_free(umap
);
638 isl_union_map_free(data
.res
);
642 __isl_give isl_union_map
*isl_union_map_intersect_params(
643 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
645 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
648 __isl_give isl_union_set
*isl_union_set_intersect_params(
649 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
651 return isl_union_map_intersect_params(uset
, set
);
654 static __isl_give isl_union_map
*union_map_intersect_params(
655 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
657 return isl_union_map_intersect_params(umap
,
658 isl_set_from_union_set(uset
));
661 static __isl_give isl_union_map
*union_map_gist_params(
662 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
664 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
667 struct isl_union_map_match_bin_data
{
668 isl_union_map
*umap2
;
670 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
673 static int match_bin_entry(void **entry
, void *user
)
675 struct isl_union_map_match_bin_data
*data
= user
;
677 struct isl_hash_table_entry
*entry2
;
678 isl_map
*map
= *entry
;
681 hash
= isl_space_get_hash(map
->dim
);
682 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
683 hash
, &has_dim
, map
->dim
, 0);
687 map
= isl_map_copy(map
);
688 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
690 empty
= isl_map_is_empty(map
);
700 data
->res
= isl_union_map_add_map(data
->res
, map
);
705 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
706 __isl_take isl_union_map
*umap2
,
707 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
709 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
711 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
712 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
714 if (!umap1
|| !umap2
)
718 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
720 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
721 &match_bin_entry
, &data
) < 0)
724 isl_union_map_free(umap1
);
725 isl_union_map_free(umap2
);
728 isl_union_map_free(umap1
);
729 isl_union_map_free(umap2
);
730 isl_union_map_free(data
.res
);
734 __isl_give isl_union_map
*isl_union_map_intersect(
735 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
737 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
740 /* Compute the intersection of the two union_sets.
741 * As a special case, if exactly one of the two union_sets
742 * is a parameter domain, then intersect the parameter domain
743 * of the other one with this set.
745 __isl_give isl_union_set
*isl_union_set_intersect(
746 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
750 p1
= isl_union_set_is_params(uset1
);
751 p2
= isl_union_set_is_params(uset2
);
752 if (p1
< 0 || p2
< 0)
755 return union_map_intersect_params(uset1
, uset2
);
757 return union_map_intersect_params(uset2
, uset1
);
758 return isl_union_map_intersect(uset1
, uset2
);
760 isl_union_set_free(uset1
);
761 isl_union_set_free(uset2
);
765 static int gist_params_entry(void **entry
, void *user
)
767 struct isl_union_map_gen_bin_set_data
*data
= user
;
768 isl_map
*map
= *entry
;
771 map
= isl_map_copy(map
);
772 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
774 empty
= isl_map_is_empty(map
);
780 data
->res
= isl_union_map_add_map(data
->res
, map
);
785 __isl_give isl_union_map
*isl_union_map_gist_params(
786 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
788 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
791 __isl_give isl_union_set
*isl_union_set_gist_params(
792 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
794 return isl_union_map_gist_params(uset
, set
);
797 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
798 __isl_take isl_union_map
*context
)
800 return match_bin_op(umap
, context
, &isl_map_gist
);
803 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
804 __isl_take isl_union_set
*context
)
806 if (isl_union_set_is_params(context
))
807 return union_map_gist_params(uset
, context
);
808 return isl_union_map_gist(uset
, context
);
811 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
812 __isl_take isl_map
*set2
)
814 return isl_set_lex_le_set((isl_set
*)set1
, (isl_set
*)set2
);
817 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
818 __isl_take isl_map
*set2
)
820 return isl_set_lex_lt_set((isl_set
*)set1
, (isl_set
*)set2
);
823 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
824 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
826 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
829 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
830 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
832 return match_bin_op(uset1
, uset2
, &lex_le_set
);
835 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
836 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
838 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
841 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
842 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
844 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
847 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
848 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
850 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
853 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
854 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
856 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
859 static int intersect_domain_entry(void **entry
, void *user
)
861 struct isl_union_map_gen_bin_data
*data
= user
;
863 struct isl_hash_table_entry
*entry2
;
865 isl_map
*map
= *entry
;
868 dim
= isl_map_get_space(map
);
869 dim
= isl_space_domain(dim
);
870 hash
= isl_space_get_hash(dim
);
871 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
872 hash
, &has_dim
, dim
, 0);
877 map
= isl_map_copy(map
);
878 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
880 empty
= isl_map_is_empty(map
);
890 data
->res
= isl_union_map_add_map(data
->res
, map
);
895 /* Intersect the domain of "umap" with "uset".
896 * If "uset" is a parameters domain, then intersect the parameter
897 * domain of "umap" with this set.
899 __isl_give isl_union_map
*isl_union_map_intersect_domain(
900 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
902 if (isl_union_set_is_params(uset
))
903 return union_map_intersect_params(umap
, uset
);
904 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
907 static int gist_domain_entry(void **entry
, void *user
)
909 struct isl_union_map_gen_bin_data
*data
= user
;
911 struct isl_hash_table_entry
*entry2
;
913 isl_map
*map
= *entry
;
916 dim
= isl_map_get_space(map
);
917 dim
= isl_space_domain(dim
);
918 hash
= isl_space_get_hash(dim
);
919 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
920 hash
, &has_dim
, dim
, 0);
925 map
= isl_map_copy(map
);
926 map
= isl_map_gist_domain(map
, isl_set_copy(entry2
->data
));
928 empty
= isl_map_is_empty(map
);
934 data
->res
= isl_union_map_add_map(data
->res
, map
);
939 /* Compute the gist of "umap" with respect to the domain "uset".
940 * If "uset" is a parameters domain, then compute the gist
941 * with respect to this parameter domain.
943 __isl_give isl_union_map
*isl_union_map_gist_domain(
944 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
946 if (isl_union_set_is_params(uset
))
947 return union_map_gist_params(umap
, uset
);
948 return gen_bin_op(umap
, uset
, &gist_domain_entry
);
951 static int gist_range_entry(void **entry
, void *user
)
953 struct isl_union_map_gen_bin_data
*data
= user
;
955 struct isl_hash_table_entry
*entry2
;
957 isl_map
*map
= *entry
;
960 space
= isl_map_get_space(map
);
961 space
= isl_space_range(space
);
962 hash
= isl_space_get_hash(space
);
963 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
964 hash
, &has_dim
, space
, 0);
965 isl_space_free(space
);
969 map
= isl_map_copy(map
);
970 map
= isl_map_gist_range(map
, isl_set_copy(entry2
->data
));
972 empty
= isl_map_is_empty(map
);
978 data
->res
= isl_union_map_add_map(data
->res
, map
);
983 /* Compute the gist of "umap" with respect to the range "uset".
985 __isl_give isl_union_map
*isl_union_map_gist_range(
986 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
988 return gen_bin_op(umap
, uset
, &gist_range_entry
);
991 static int intersect_range_entry(void **entry
, void *user
)
993 struct isl_union_map_gen_bin_data
*data
= user
;
995 struct isl_hash_table_entry
*entry2
;
997 isl_map
*map
= *entry
;
1000 dim
= isl_map_get_space(map
);
1001 dim
= isl_space_range(dim
);
1002 hash
= isl_space_get_hash(dim
);
1003 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1004 hash
, &has_dim
, dim
, 0);
1005 isl_space_free(dim
);
1009 map
= isl_map_copy(map
);
1010 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
1012 empty
= isl_map_is_empty(map
);
1022 data
->res
= isl_union_map_add_map(data
->res
, map
);
1027 __isl_give isl_union_map
*isl_union_map_intersect_range(
1028 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1030 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
1033 struct isl_union_map_bin_data
{
1034 isl_union_map
*umap2
;
1037 int (*fn
)(void **entry
, void *user
);
1040 static int apply_range_entry(void **entry
, void *user
)
1042 struct isl_union_map_bin_data
*data
= user
;
1043 isl_map
*map2
= *entry
;
1046 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
1047 map2
->dim
, isl_dim_in
))
1050 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1052 empty
= isl_map_is_empty(map2
);
1062 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1067 static int bin_entry(void **entry
, void *user
)
1069 struct isl_union_map_bin_data
*data
= user
;
1070 isl_map
*map
= *entry
;
1073 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1074 data
->fn
, data
) < 0)
1080 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1081 __isl_take isl_union_map
*umap2
, int (*fn
)(void **entry
, void *user
))
1083 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1085 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1086 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1088 if (!umap1
|| !umap2
)
1092 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1094 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1095 &bin_entry
, &data
) < 0)
1098 isl_union_map_free(umap1
);
1099 isl_union_map_free(umap2
);
1102 isl_union_map_free(umap1
);
1103 isl_union_map_free(umap2
);
1104 isl_union_map_free(data
.res
);
1108 __isl_give isl_union_map
*isl_union_map_apply_range(
1109 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1111 return bin_op(umap1
, umap2
, &apply_range_entry
);
1114 __isl_give isl_union_map
*isl_union_map_apply_domain(
1115 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1117 umap1
= isl_union_map_reverse(umap1
);
1118 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1119 return isl_union_map_reverse(umap1
);
1122 __isl_give isl_union_set
*isl_union_set_apply(
1123 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1125 return isl_union_map_apply_range(uset
, umap
);
1128 static int map_lex_lt_entry(void **entry
, void *user
)
1130 struct isl_union_map_bin_data
*data
= user
;
1131 isl_map
*map2
= *entry
;
1133 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
1134 map2
->dim
, isl_dim_out
))
1137 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1139 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1144 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1145 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1147 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1150 static int map_lex_le_entry(void **entry
, void *user
)
1152 struct isl_union_map_bin_data
*data
= user
;
1153 isl_map
*map2
= *entry
;
1155 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
1156 map2
->dim
, isl_dim_out
))
1159 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1161 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1166 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1167 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1169 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1172 static int product_entry(void **entry
, void *user
)
1174 struct isl_union_map_bin_data
*data
= user
;
1175 isl_map
*map2
= *entry
;
1177 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1179 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1184 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1185 __isl_take isl_union_map
*umap2
)
1187 return bin_op(umap1
, umap2
, &product_entry
);
1190 static int set_product_entry(void **entry
, void *user
)
1192 struct isl_union_map_bin_data
*data
= user
;
1193 isl_set
*set2
= *entry
;
1195 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1197 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1202 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1203 __isl_take isl_union_set
*uset2
)
1205 return bin_op(uset1
, uset2
, &set_product_entry
);
1208 static int domain_product_entry(void **entry
, void *user
)
1210 struct isl_union_map_bin_data
*data
= user
;
1211 isl_map
*map2
= *entry
;
1213 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_out
,
1214 map2
->dim
, isl_dim_out
))
1217 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1218 isl_map_copy(map2
));
1220 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1225 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1227 __isl_give isl_union_map
*isl_union_map_domain_product(
1228 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1230 return bin_op(umap1
, umap2
, &domain_product_entry
);
1233 static int range_product_entry(void **entry
, void *user
)
1235 struct isl_union_map_bin_data
*data
= user
;
1236 isl_map
*map2
= *entry
;
1238 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_in
,
1239 map2
->dim
, isl_dim_in
))
1242 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1243 isl_map_copy(map2
));
1245 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1250 __isl_give isl_union_map
*isl_union_map_range_product(
1251 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1253 return bin_op(umap1
, umap2
, &range_product_entry
);
1256 static int flat_range_product_entry(void **entry
, void *user
)
1258 struct isl_union_map_bin_data
*data
= user
;
1259 isl_map
*map2
= *entry
;
1261 if (!isl_space_tuple_match(data
->map
->dim
, isl_dim_in
,
1262 map2
->dim
, isl_dim_in
))
1265 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1266 isl_map_copy(map2
));
1268 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1273 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1274 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1276 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1279 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1280 int (*fn
)(void **, void *))
1287 res
= isl_union_map_alloc(isl_space_copy(umap
->dim
), umap
->table
.n
);
1288 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1291 isl_union_map_free(umap
);
1294 isl_union_map_free(umap
);
1295 isl_union_set_free(res
);
1299 static int from_range_entry(void **entry
, void *user
)
1301 isl_map
*set
= *entry
;
1302 isl_union_set
**res
= user
;
1304 *res
= isl_union_map_add_map(*res
,
1305 isl_map_from_range(isl_set_copy(set
)));
1310 __isl_give isl_union_map
*isl_union_map_from_range(
1311 __isl_take isl_union_set
*uset
)
1313 return cond_un_op(uset
, &from_range_entry
);
1316 __isl_give isl_union_map
*isl_union_map_from_domain(
1317 __isl_take isl_union_set
*uset
)
1319 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1322 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1323 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1325 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1326 isl_union_map_from_range(range
));
1329 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1330 int (*fn
)(void **, void *))
1332 umap
= isl_union_map_cow(umap
);
1336 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1341 isl_union_map_free(umap
);
1345 static int affine_entry(void **entry
, void *user
)
1347 isl_map
**map
= (isl_map
**)entry
;
1349 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1351 return *map
? 0 : -1;
1354 __isl_give isl_union_map
*isl_union_map_affine_hull(
1355 __isl_take isl_union_map
*umap
)
1357 return un_op(umap
, &affine_entry
);
1360 __isl_give isl_union_set
*isl_union_set_affine_hull(
1361 __isl_take isl_union_set
*uset
)
1363 return isl_union_map_affine_hull(uset
);
1366 static int polyhedral_entry(void **entry
, void *user
)
1368 isl_map
**map
= (isl_map
**)entry
;
1370 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1372 return *map
? 0 : -1;
1375 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1376 __isl_take isl_union_map
*umap
)
1378 return un_op(umap
, &polyhedral_entry
);
1381 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1382 __isl_take isl_union_set
*uset
)
1384 return isl_union_map_polyhedral_hull(uset
);
1387 static int simple_entry(void **entry
, void *user
)
1389 isl_map
**map
= (isl_map
**)entry
;
1391 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1393 return *map
? 0 : -1;
1396 __isl_give isl_union_map
*isl_union_map_simple_hull(
1397 __isl_take isl_union_map
*umap
)
1399 return un_op(umap
, &simple_entry
);
1402 __isl_give isl_union_set
*isl_union_set_simple_hull(
1403 __isl_take isl_union_set
*uset
)
1405 return isl_union_map_simple_hull(uset
);
1408 static int inplace_entry(void **entry
, void *user
)
1410 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1411 isl_map
**map
= (isl_map
**)entry
;
1414 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1415 copy
= fn(isl_map_copy(*map
));
1425 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1426 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1431 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1432 &inplace_entry
, &fn
) < 0)
1437 isl_union_map_free(umap
);
1441 __isl_give isl_union_map
*isl_union_map_coalesce(
1442 __isl_take isl_union_map
*umap
)
1444 return inplace(umap
, &isl_map_coalesce
);
1447 __isl_give isl_union_set
*isl_union_set_coalesce(
1448 __isl_take isl_union_set
*uset
)
1450 return isl_union_map_coalesce(uset
);
1453 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1454 __isl_take isl_union_map
*umap
)
1456 return inplace(umap
, &isl_map_detect_equalities
);
1459 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1460 __isl_take isl_union_set
*uset
)
1462 return isl_union_map_detect_equalities(uset
);
1465 __isl_give isl_union_map
*isl_union_map_compute_divs(
1466 __isl_take isl_union_map
*umap
)
1468 return inplace(umap
, &isl_map_compute_divs
);
1471 __isl_give isl_union_set
*isl_union_set_compute_divs(
1472 __isl_take isl_union_set
*uset
)
1474 return isl_union_map_compute_divs(uset
);
1477 static int lexmin_entry(void **entry
, void *user
)
1479 isl_map
**map
= (isl_map
**)entry
;
1481 *map
= isl_map_lexmin(*map
);
1483 return *map
? 0 : -1;
1486 __isl_give isl_union_map
*isl_union_map_lexmin(
1487 __isl_take isl_union_map
*umap
)
1489 return un_op(umap
, &lexmin_entry
);
1492 __isl_give isl_union_set
*isl_union_set_lexmin(
1493 __isl_take isl_union_set
*uset
)
1495 return isl_union_map_lexmin(uset
);
1498 static int lexmax_entry(void **entry
, void *user
)
1500 isl_map
**map
= (isl_map
**)entry
;
1502 *map
= isl_map_lexmax(*map
);
1504 return *map
? 0 : -1;
1507 __isl_give isl_union_map
*isl_union_map_lexmax(
1508 __isl_take isl_union_map
*umap
)
1510 return un_op(umap
, &lexmax_entry
);
1513 __isl_give isl_union_set
*isl_union_set_lexmax(
1514 __isl_take isl_union_set
*uset
)
1516 return isl_union_map_lexmax(uset
);
1519 static int universe_entry(void **entry
, void *user
)
1521 isl_map
*map
= *entry
;
1522 isl_union_map
**res
= user
;
1524 map
= isl_map_universe(isl_map_get_space(map
));
1525 *res
= isl_union_map_add_map(*res
, map
);
1530 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1532 return cond_un_op(umap
, &universe_entry
);
1535 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1537 return isl_union_map_universe(uset
);
1540 static int reverse_entry(void **entry
, void *user
)
1542 isl_map
*map
= *entry
;
1543 isl_union_map
**res
= user
;
1545 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1550 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1552 return cond_un_op(umap
, &reverse_entry
);
1555 static int params_entry(void **entry
, void *user
)
1557 isl_map
*map
= *entry
;
1558 isl_union_set
**res
= user
;
1560 *res
= isl_union_set_add_set(*res
, isl_map_params(isl_map_copy(map
)));
1565 /* Compute the parameter domain of the given union map.
1567 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1571 empty
= isl_union_map_is_empty(umap
);
1573 return isl_union_map_free(umap
);
1575 return isl_set_empty(isl_union_map_get_space(umap
));
1576 return isl_set_from_union_set(cond_un_op(umap
, ¶ms_entry
));
1579 /* Compute the parameter domain of the given union set.
1581 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1583 return isl_union_map_params(uset
);
1586 static int domain_entry(void **entry
, void *user
)
1588 isl_map
*map
= *entry
;
1589 isl_union_set
**res
= user
;
1591 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1596 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1598 return cond_un_op(umap
, &domain_entry
);
1601 static int range_entry(void **entry
, void *user
)
1603 isl_map
*map
= *entry
;
1604 isl_union_set
**res
= user
;
1606 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1611 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1613 return cond_un_op(umap
, &range_entry
);
1616 static int domain_map_entry(void **entry
, void *user
)
1618 isl_map
*map
= *entry
;
1619 isl_union_set
**res
= user
;
1621 *res
= isl_union_map_add_map(*res
,
1622 isl_map_domain_map(isl_map_copy(map
)));
1627 __isl_give isl_union_map
*isl_union_map_domain_map(
1628 __isl_take isl_union_map
*umap
)
1630 return cond_un_op(umap
, &domain_map_entry
);
1633 static int range_map_entry(void **entry
, void *user
)
1635 isl_map
*map
= *entry
;
1636 isl_union_set
**res
= user
;
1638 *res
= isl_union_map_add_map(*res
,
1639 isl_map_range_map(isl_map_copy(map
)));
1644 __isl_give isl_union_map
*isl_union_map_range_map(
1645 __isl_take isl_union_map
*umap
)
1647 return cond_un_op(umap
, &range_map_entry
);
1650 static int deltas_entry(void **entry
, void *user
)
1652 isl_map
*map
= *entry
;
1653 isl_union_set
**res
= user
;
1655 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1658 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1663 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1665 return cond_un_op(umap
, &deltas_entry
);
1668 static int deltas_map_entry(void **entry
, void *user
)
1670 isl_map
*map
= *entry
;
1671 isl_union_map
**res
= user
;
1673 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1676 *res
= isl_union_map_add_map(*res
,
1677 isl_map_deltas_map(isl_map_copy(map
)));
1682 __isl_give isl_union_map
*isl_union_map_deltas_map(
1683 __isl_take isl_union_map
*umap
)
1685 return cond_un_op(umap
, &deltas_map_entry
);
1688 static int identity_entry(void **entry
, void *user
)
1690 isl_set
*set
= *entry
;
1691 isl_union_map
**res
= user
;
1693 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
1698 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1700 return cond_un_op(uset
, &identity_entry
);
1703 static int unwrap_entry(void **entry
, void *user
)
1705 isl_set
*set
= *entry
;
1706 isl_union_set
**res
= user
;
1708 if (!isl_set_is_wrapping(set
))
1711 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
1716 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
1718 return cond_un_op(uset
, &unwrap_entry
);
1721 static int wrap_entry(void **entry
, void *user
)
1723 isl_map
*map
= *entry
;
1724 isl_union_set
**res
= user
;
1726 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
1731 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
1733 return cond_un_op(umap
, &wrap_entry
);
1736 struct isl_union_map_is_subset_data
{
1737 isl_union_map
*umap2
;
1741 static int is_subset_entry(void **entry
, void *user
)
1743 struct isl_union_map_is_subset_data
*data
= user
;
1745 struct isl_hash_table_entry
*entry2
;
1746 isl_map
*map
= *entry
;
1748 hash
= isl_space_get_hash(map
->dim
);
1749 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1750 hash
, &has_dim
, map
->dim
, 0);
1752 int empty
= isl_map_is_empty(map
);
1757 data
->is_subset
= 0;
1761 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
1762 if (data
->is_subset
< 0 || !data
->is_subset
)
1768 int isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
1769 __isl_keep isl_union_map
*umap2
)
1771 struct isl_union_map_is_subset_data data
= { NULL
, 1 };
1773 umap1
= isl_union_map_copy(umap1
);
1774 umap2
= isl_union_map_copy(umap2
);
1775 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1776 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1778 if (!umap1
|| !umap2
)
1782 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1783 &is_subset_entry
, &data
) < 0 &&
1787 isl_union_map_free(umap1
);
1788 isl_union_map_free(umap2
);
1790 return data
.is_subset
;
1792 isl_union_map_free(umap1
);
1793 isl_union_map_free(umap2
);
1797 int isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
1798 __isl_keep isl_union_set
*uset2
)
1800 return isl_union_map_is_subset(uset1
, uset2
);
1803 int isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
1804 __isl_keep isl_union_map
*umap2
)
1808 if (!umap1
|| !umap2
)
1810 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1813 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1817 int isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
1818 __isl_keep isl_union_set
*uset2
)
1820 return isl_union_map_is_equal(uset1
, uset2
);
1823 int isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
1824 __isl_keep isl_union_map
*umap2
)
1828 if (!umap1
|| !umap2
)
1830 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1833 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1834 if (is_subset
== -1)
1839 int isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
1840 __isl_keep isl_union_set
*uset2
)
1842 return isl_union_map_is_strict_subset(uset1
, uset2
);
1845 static int sample_entry(void **entry
, void *user
)
1847 isl_basic_map
**sample
= (isl_basic_map
**)user
;
1848 isl_map
*map
= *entry
;
1850 *sample
= isl_map_sample(isl_map_copy(map
));
1853 if (!isl_basic_map_plain_is_empty(*sample
))
1858 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
1860 isl_basic_map
*sample
= NULL
;
1865 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1866 &sample_entry
, &sample
) < 0 &&
1871 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
1873 isl_union_map_free(umap
);
1877 isl_union_map_free(umap
);
1881 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
1883 return (isl_basic_set
*)isl_union_map_sample(uset
);
1886 struct isl_forall_data
{
1888 int (*fn
)(__isl_keep isl_map
*map
);
1891 static int forall_entry(void **entry
, void *user
)
1893 struct isl_forall_data
*data
= user
;
1894 isl_map
*map
= *entry
;
1896 data
->res
= data
->fn(map
);
1906 static int union_map_forall(__isl_keep isl_union_map
*umap
,
1907 int (*fn
)(__isl_keep isl_map
*map
))
1909 struct isl_forall_data data
= { 1, fn
};
1914 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1915 &forall_entry
, &data
) < 0 && data
.res
)
1921 struct isl_forall_user_data
{
1923 int (*fn
)(__isl_keep isl_map
*map
, void *user
);
1927 static int forall_user_entry(void **entry
, void *user
)
1929 struct isl_forall_user_data
*data
= user
;
1930 isl_map
*map
= *entry
;
1932 data
->res
= data
->fn(map
, data
->user
);
1942 /* Check if fn(map, user) returns true for all maps "map" in umap.
1944 static int union_map_forall_user(__isl_keep isl_union_map
*umap
,
1945 int (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
1947 struct isl_forall_user_data data
= { 1, fn
, user
};
1952 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1953 &forall_user_entry
, &data
) < 0 && data
.res
)
1959 int isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
1961 return union_map_forall(umap
, &isl_map_is_empty
);
1964 int isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
1966 return isl_union_map_is_empty(uset
);
1969 static int is_subset_of_identity(__isl_keep isl_map
*map
)
1978 if (!isl_space_tuple_match(map
->dim
, isl_dim_in
, map
->dim
, isl_dim_out
))
1981 dim
= isl_map_get_space(map
);
1982 id
= isl_map_identity(dim
);
1984 is_subset
= isl_map_is_subset(map
, id
);
1991 /* Check if the given map is single-valued.
1996 * and check if the result is a subset of the identity mapping.
1998 int isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2000 isl_union_map
*test
;
2003 if (isl_union_map_n_map(umap
) == 1) {
2005 umap
= isl_union_map_copy(umap
);
2006 map
= isl_map_from_union_map(umap
);
2007 sv
= isl_map_is_single_valued(map
);
2012 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2013 test
= isl_union_map_apply_range(test
, isl_union_map_copy(umap
));
2015 sv
= union_map_forall(test
, &is_subset_of_identity
);
2017 isl_union_map_free(test
);
2022 int isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2026 umap
= isl_union_map_copy(umap
);
2027 umap
= isl_union_map_reverse(umap
);
2028 in
= isl_union_map_is_single_valued(umap
);
2029 isl_union_map_free(umap
);
2034 /* Represents a map that has a fixed value (v) for one of its
2036 * The map in this structure is not reference counted, so it
2037 * is only valid while the isl_union_map from which it was
2038 * obtained is still alive.
2040 struct isl_fixed_map
{
2045 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2049 struct isl_fixed_map
*v
;
2051 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2054 for (i
= 0; i
< n
; ++i
)
2055 isl_int_init(v
[i
].v
);
2059 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2065 for (i
= 0; i
< n
; ++i
)
2066 isl_int_clear(v
[i
].v
);
2070 /* Compare the "v" field of two isl_fixed_map structs.
2072 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2074 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2075 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2077 return isl_int_cmp(e1
->v
, e2
->v
);
2080 /* Internal data structure used while checking whether all maps
2081 * in a union_map have a fixed value for a given output dimension.
2082 * v is the list of maps, with the fixed value for the dimension
2083 * n is the number of maps considered so far
2084 * pos is the output dimension under investigation
2086 struct isl_fixed_dim_data
{
2087 struct isl_fixed_map
*v
;
2092 static int fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2094 struct isl_fixed_dim_data
*data
= user
;
2096 data
->v
[data
->n
].map
= map
;
2097 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2098 &data
->v
[data
->n
++].v
);
2101 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
2102 int first
, int n_range
);
2104 /* Given a list of the maps, with their fixed values at output dimension "pos",
2105 * check whether the ranges of the maps form an obvious partition.
2107 * We first sort the maps according to their fixed values.
2108 * If all maps have a different value, then we know the ranges form
2110 * Otherwise, we collect the maps with the same fixed value and
2111 * check whether each such collection is obviously injective
2112 * based on later dimensions.
2114 static int separates(struct isl_fixed_map
*v
, int n
,
2115 __isl_take isl_space
*dim
, int pos
, int n_range
)
2122 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2124 for (i
= 0; i
+ 1 < n
; ++i
) {
2126 isl_union_map
*part
;
2129 for (j
= i
+ 1; j
< n
; ++j
)
2130 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2136 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2137 for (k
= i
; k
< j
; ++k
)
2138 part
= isl_union_map_add_map(part
,
2139 isl_map_copy(v
[k
].map
));
2141 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2150 isl_space_free(dim
);
2151 free_isl_fixed_map_array(v
, n
);
2154 isl_space_free(dim
);
2155 free_isl_fixed_map_array(v
, n
);
2159 /* Check whether the maps in umap have obviously distinct ranges.
2160 * In particular, check for an output dimension in the range
2161 * [first,n_range) for which all maps have a fixed value
2162 * and then check if these values, possibly along with fixed values
2163 * at later dimensions, entail distinct ranges.
2165 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
2166 int first
, int n_range
)
2170 struct isl_fixed_dim_data data
= { NULL
};
2172 ctx
= isl_union_map_get_ctx(umap
);
2177 n
= isl_union_map_n_map(umap
);
2179 isl_union_map_free(umap
);
2183 if (first
>= n_range
) {
2184 isl_union_map_free(umap
);
2188 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2192 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2198 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2203 dim
= isl_union_map_get_space(umap
);
2204 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2205 isl_union_map_free(umap
);
2209 free_isl_fixed_map_array(data
.v
, n
);
2210 isl_union_map_free(umap
);
2214 free_isl_fixed_map_array(data
.v
, n
);
2215 isl_union_map_free(umap
);
2219 /* Check whether the maps in umap that map to subsets of "ran"
2220 * have obviously distinct ranges.
2222 static int plain_injective_on_range_wrap(__isl_keep isl_set
*ran
, void *user
)
2224 isl_union_map
*umap
= user
;
2226 umap
= isl_union_map_copy(umap
);
2227 umap
= isl_union_map_intersect_range(umap
,
2228 isl_union_set_from_set(isl_set_copy(ran
)));
2229 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2232 /* Check if the given union_map is obviously injective.
2234 * In particular, we first check if all individual maps are obviously
2235 * injective and then check if all the ranges of these maps are
2236 * obviously disjoint.
2238 int isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2241 isl_union_map
*univ
;
2244 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2250 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2251 ran
= isl_union_map_range(univ
);
2253 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2255 isl_union_set_free(ran
);
2260 int isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2264 sv
= isl_union_map_is_single_valued(umap
);
2268 return isl_union_map_is_injective(umap
);
2271 static int zip_entry(void **entry
, void *user
)
2273 isl_map
*map
= *entry
;
2274 isl_union_map
**res
= user
;
2276 if (!isl_map_can_zip(map
))
2279 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
2284 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2286 return cond_un_op(umap
, &zip_entry
);
2289 static int curry_entry(void **entry
, void *user
)
2291 isl_map
*map
= *entry
;
2292 isl_union_map
**res
= user
;
2294 if (!isl_map_can_curry(map
))
2297 *res
= isl_union_map_add_map(*res
, isl_map_curry(isl_map_copy(map
)));
2302 /* Given a union map, take the maps of the form (A -> B) -> C and
2303 * return the union of the corresponding maps A -> (B -> C).
2305 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
2307 return cond_un_op(umap
, &curry_entry
);
2310 static int lift_entry(void **entry
, void *user
)
2312 isl_set
*set
= *entry
;
2313 isl_union_set
**res
= user
;
2315 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
2320 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
2322 return cond_un_op(uset
, &lift_entry
);
2325 static int coefficients_entry(void **entry
, void *user
)
2327 isl_set
*set
= *entry
;
2328 isl_union_set
**res
= user
;
2330 set
= isl_set_copy(set
);
2331 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
2332 *res
= isl_union_set_add_set(*res
, set
);
2337 __isl_give isl_union_set
*isl_union_set_coefficients(
2338 __isl_take isl_union_set
*uset
)
2347 ctx
= isl_union_set_get_ctx(uset
);
2348 dim
= isl_space_set_alloc(ctx
, 0, 0);
2349 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2350 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2351 &coefficients_entry
, &res
) < 0)
2354 isl_union_set_free(uset
);
2357 isl_union_set_free(uset
);
2358 isl_union_set_free(res
);
2362 static int solutions_entry(void **entry
, void *user
)
2364 isl_set
*set
= *entry
;
2365 isl_union_set
**res
= user
;
2367 set
= isl_set_copy(set
);
2368 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2370 *res
= isl_union_set_from_set(set
);
2372 *res
= isl_union_set_add_set(*res
, set
);
2380 __isl_give isl_union_set
*isl_union_set_solutions(
2381 __isl_take isl_union_set
*uset
)
2383 isl_union_set
*res
= NULL
;
2388 if (uset
->table
.n
== 0) {
2389 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2390 isl_union_set_free(uset
);
2394 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2395 &solutions_entry
, &res
) < 0)
2398 isl_union_set_free(uset
);
2401 isl_union_set_free(uset
);
2402 isl_union_set_free(res
);