2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl_maybe_map.h>
26 #include <isl_id_private.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
31 #include <uset_to_umap.c>
32 #include <uset_from_umap.c>
33 #include <set_list_from_map_list_inl.c>
35 /* Return the number of parameters of "umap", where "type"
36 * is required to be set to isl_dim_param.
38 isl_size
isl_union_map_dim(__isl_keep isl_union_map
*umap
,
39 enum isl_dim_type type
)
42 return isl_size_error
;
44 if (type
!= isl_dim_param
)
45 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
46 "can only reference parameters", return isl_size_error
);
48 return isl_space_dim(umap
->dim
, type
);
51 /* Return the number of parameters of "uset", where "type"
52 * is required to be set to isl_dim_param.
54 isl_size
isl_union_set_dim(__isl_keep isl_union_set
*uset
,
55 enum isl_dim_type type
)
57 return isl_union_map_dim(uset
, type
);
60 /* Return the id of the specified dimension.
62 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
63 enum isl_dim_type type
, unsigned pos
)
68 if (type
!= isl_dim_param
)
69 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
70 "can only reference parameters", return NULL
);
72 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
75 /* Is this union set a parameter domain?
77 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
83 return isl_bool_error
;
84 if (uset
->table
.n
!= 1)
85 return isl_bool_false
;
87 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
88 params
= isl_set_is_params(set
);
93 /* Is this union map actually a parameter domain?
94 * Users should never call this function. Outside of isl,
95 * a union map can never be a parameter domain.
97 isl_bool
isl_union_map_is_params(__isl_keep isl_union_map
*umap
)
99 return isl_union_set_is_params(uset_from_umap(umap
));
102 static __isl_give isl_union_map
*isl_union_map_alloc(
103 __isl_take isl_space
*space
, int size
)
107 space
= isl_space_params(space
);
111 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
113 isl_space_free(space
);
119 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
120 return isl_union_map_free(umap
);
125 /* Create an empty union map without specifying any parameters.
127 __isl_give isl_union_map
*isl_union_map_empty_ctx(isl_ctx
*ctx
)
129 return isl_union_map_empty_space(isl_space_unit(ctx
));
132 __isl_give isl_union_map
*isl_union_map_empty_space(__isl_take isl_space
*space
)
134 return isl_union_map_alloc(space
, 16);
137 /* This is an alternative name for the function above.
139 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*space
)
141 return isl_union_map_empty_space(space
);
144 /* Create an empty union set without specifying any parameters.
146 __isl_give isl_union_set
*isl_union_set_empty_ctx(isl_ctx
*ctx
)
148 return uset_from_umap(isl_union_map_empty_ctx(ctx
));
151 __isl_give isl_union_set
*isl_union_set_empty_space(__isl_take isl_space
*space
)
153 return uset_from_umap(isl_union_map_empty_space(space
));
156 /* This is an alternative name for the function above.
158 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*space
)
160 return isl_union_set_empty_space(space
);
163 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
165 return umap
? umap
->dim
->ctx
: NULL
;
168 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
170 return uset
? uset
->dim
->ctx
: NULL
;
173 /* Return the space of "umap".
175 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
177 return umap
? umap
->dim
: NULL
;
180 /* Return the space of "uset".
182 __isl_keep isl_space
*isl_union_set_peek_space(__isl_keep isl_union_set
*uset
)
184 return isl_union_map_peek_space(uset_to_umap(uset
));
187 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
189 return isl_space_copy(isl_union_map_peek_space(umap
));
192 /* Return the position of the parameter with the given name
194 * Return -1 if no such dimension can be found.
196 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
197 enum isl_dim_type type
, const char *name
)
201 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
204 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
206 return isl_union_map_get_space(uset
);
209 static isl_stat
free_umap_entry(void **entry
, void *user
)
211 isl_map
*map
= *entry
;
216 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
218 isl_union_map
**umap
= (isl_union_map
**)user
;
220 *umap
= isl_union_map_add_map(*umap
, map
);
225 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
232 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
233 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
237 isl_union_map_free(dup
);
241 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
249 return isl_union_map_dup(umap
);
252 struct isl_union_align
{
257 static isl_stat
align_entry(void **entry
, void *user
)
259 isl_map
*map
= *entry
;
261 struct isl_union_align
*data
= user
;
263 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
264 isl_map_get_space(map
));
266 data
->res
= isl_union_map_add_map(data
->res
,
267 isl_map_realign(isl_map_copy(map
), exp
));
272 /* Align the parameters of umap along those of model.
273 * The result has the parameters of model first, in the same order
274 * as they appear in model, followed by any remaining parameters of
275 * umap that do not appear in model.
277 __isl_give isl_union_map
*isl_union_map_align_params(
278 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
280 struct isl_union_align data
= { NULL
, NULL
};
281 isl_bool equal_params
;
286 equal_params
= isl_space_has_equal_params(umap
->dim
, model
);
287 if (equal_params
< 0)
290 isl_space_free(model
);
294 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
298 data
.res
= isl_union_map_alloc(isl_reordering_get_space(data
.exp
),
300 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
301 &align_entry
, &data
) < 0)
304 isl_reordering_free(data
.exp
);
305 isl_union_map_free(umap
);
306 isl_space_free(model
);
309 isl_reordering_free(data
.exp
);
310 isl_union_map_free(umap
);
311 isl_union_map_free(data
.res
);
312 isl_space_free(model
);
316 __isl_give isl_union_set
*isl_union_set_align_params(
317 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
319 return isl_union_map_align_params(uset
, model
);
322 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
323 __isl_take isl_union_map
*umap2
)
325 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
326 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
328 umap1
= isl_union_map_cow(umap1
);
330 if (!umap1
|| !umap2
)
333 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
336 isl_union_map_free(umap2
);
340 isl_union_map_free(umap1
);
341 isl_union_map_free(umap2
);
345 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
346 __isl_take isl_union_set
*uset2
)
348 return isl_union_map_union(uset1
, uset2
);
351 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
360 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
362 return isl_union_map_copy(uset
);
365 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
373 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
374 &free_umap_entry
, NULL
);
375 isl_hash_table_clear(&umap
->table
);
376 isl_space_free(umap
->dim
);
381 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
383 return isl_union_map_free(uset
);
386 /* Do "umap" and "space" have the same parameters?
388 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
389 __isl_keep isl_space
*space
)
391 isl_space
*umap_space
;
393 umap_space
= isl_union_map_peek_space(umap
);
394 return isl_space_has_equal_params(umap_space
, space
);
397 /* Do "uset" and "space" have the same parameters?
399 isl_bool
isl_union_set_space_has_equal_params(__isl_keep isl_union_set
*uset
,
400 __isl_keep isl_space
*space
)
402 return isl_union_map_space_has_equal_params(uset_to_umap(uset
), space
);
405 static isl_bool
has_space(const void *entry
, const void *val
)
407 isl_map
*map
= (isl_map
*)entry
;
408 isl_space
*space
= (isl_space
*) val
;
410 return isl_space_is_equal(map
->dim
, space
);
413 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
414 __isl_take isl_map
*map
)
417 struct isl_hash_table_entry
*entry
;
423 if (isl_map_plain_is_empty(map
)) {
428 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
432 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
433 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
436 umap
= isl_union_map_cow(umap
);
441 hash
= isl_space_get_hash(map
->dim
);
442 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
443 &has_space
, map
->dim
, 1);
450 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
459 isl_union_map_free(umap
);
463 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
464 __isl_take isl_set
*set
)
466 return isl_union_map_add_map(uset
, set_to_map(set
));
469 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
477 dim
= isl_map_get_space(map
);
478 dim
= isl_space_params(dim
);
479 umap
= isl_union_map_empty(dim
);
480 umap
= isl_union_map_add_map(umap
, map
);
485 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
487 return isl_union_map_from_map(set_to_map(set
));
490 __isl_give isl_union_map
*isl_union_map_from_basic_map(
491 __isl_take isl_basic_map
*bmap
)
493 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
496 __isl_give isl_union_set
*isl_union_set_from_basic_set(
497 __isl_take isl_basic_set
*bset
)
499 return isl_union_map_from_basic_map(bset
);
502 struct isl_union_map_foreach_data
504 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
508 static isl_stat
call_on_copy(void **entry
, void *user
)
510 isl_map
*map
= *entry
;
511 struct isl_union_map_foreach_data
*data
;
512 data
= (struct isl_union_map_foreach_data
*)user
;
514 return data
->fn(isl_map_copy(map
), data
->user
);
517 isl_size
isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
519 return umap
? umap
->table
.n
: isl_size_error
;
522 isl_size
isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
524 return uset
? uset
->table
.n
: isl_size_error
;
527 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
528 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
530 struct isl_union_map_foreach_data data
= { fn
, user
};
533 return isl_stat_error
;
535 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
536 &call_on_copy
, &data
);
539 /* Internal data structure for isl_union_map_every_map.
541 * "test" is the user-specified callback function.
542 * "user" is the user-specified callback function argument.
544 * "failed" is initialized to 0 and set to 1 if "test" fails
547 struct isl_union_map_every_data
{
548 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
);
553 /* Call data->test on "map".
554 * If this fails, then set data->failed and abort.
556 static isl_stat
call_every(void **entry
, void *user
)
558 isl_map
*map
= *entry
;
559 struct isl_union_map_every_data
*data
= user
;
562 r
= data
->test(map
, data
->user
);
564 return isl_stat_error
;
568 return isl_stat_error
;
571 /* Does "test" succeed on every map in "umap"?
573 isl_bool
isl_union_map_every_map(__isl_keep isl_union_map
*umap
,
574 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
), void *user
)
576 struct isl_union_map_every_data data
= { test
, user
, 0 };
580 return isl_bool_error
;
582 r
= isl_hash_table_foreach(isl_union_map_get_ctx(umap
), &umap
->table
,
585 return isl_bool_true
;
587 return isl_bool_false
;
588 return isl_bool_error
;
591 /* Add "map" to "list".
593 static isl_stat
add_list_map(__isl_take isl_map
*map
, void *user
)
595 isl_map_list
**list
= user
;
597 *list
= isl_map_list_add(*list
, map
);
600 return isl_stat_error
;
604 /* Return the maps in "umap" as a list.
606 * First construct a list of the appropriate size and then add all the
609 __isl_give isl_map_list
*isl_union_map_get_map_list(
610 __isl_keep isl_union_map
*umap
)
616 n_maps
= isl_union_map_n_map(umap
);
619 ctx
= isl_union_map_get_ctx(umap
);
620 list
= isl_map_list_alloc(ctx
, n_maps
);
622 if (isl_union_map_foreach_map(umap
, &add_list_map
, &list
) < 0)
623 list
= isl_map_list_free(list
);
628 /* Return the sets in "uset" as a list.
630 __isl_give isl_set_list
*isl_union_set_get_set_list(
631 __isl_keep isl_union_set
*uset
)
633 return set_list_from_map_list(
634 isl_union_map_get_map_list(uset_to_umap(uset
)));
637 /* Can "umap" be converted to an isl_map?
638 * That is, does it contain elements in exactly one space?
640 isl_bool
isl_union_map_isa_map(__isl_keep isl_union_map
*umap
)
644 n
= isl_union_map_n_map(umap
);
646 return isl_bool_error
;
647 return isl_bool_ok(n
== 1);
650 /* Can "uset" be converted to an isl_set?
651 * That is, does it contain elements in exactly one space?
653 isl_bool
isl_union_set_isa_set(__isl_keep isl_union_set
*uset
)
655 return isl_union_map_isa_map(uset_to_umap(uset
));
658 static isl_stat
copy_map(void **entry
, void *user
)
660 isl_map
*map
= *entry
;
661 isl_map
**map_p
= user
;
663 *map_p
= isl_map_copy(map
);
665 return isl_stat_error
;
668 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
674 is_map
= isl_union_map_isa_map(umap
);
677 ctx
= isl_union_map_get_ctx(umap
);
679 isl_die(ctx
, isl_error_invalid
,
680 "union map needs to contain elements in exactly "
681 "one space", goto error
);
683 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
685 isl_union_map_free(umap
);
689 isl_union_map_free(umap
);
693 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
695 return isl_map_from_union_map(uset
);
698 /* Extract the map in "umap" that lives in the given space (ignoring
701 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
702 __isl_take isl_space
*space
)
705 struct isl_hash_table_entry
*entry
;
707 space
= isl_space_drop_all_params(space
);
708 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
712 hash
= isl_space_get_hash(space
);
713 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
714 &has_space
, space
, 0);
717 if (entry
== isl_hash_table_entry_none
)
718 return isl_map_empty(space
);
719 isl_space_free(space
);
720 return isl_map_copy(entry
->data
);
722 isl_space_free(space
);
726 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
727 __isl_take isl_space
*space
)
729 return set_from_map(isl_union_map_extract_map(uset
, space
));
732 /* Check if umap contains a map in the given space.
734 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
735 __isl_keep isl_space
*space
)
738 struct isl_hash_table_entry
*entry
;
741 return isl_bool_error
;
743 hash
= isl_space_get_hash(space
);
744 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
745 &has_space
, space
, 0);
747 return isl_bool_error
;
748 return isl_bool_ok(entry
!= isl_hash_table_entry_none
);
751 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
752 __isl_keep isl_space
*space
)
754 return isl_union_map_contains(uset
, space
);
757 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
758 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
760 return isl_union_map_foreach_map(uset
,
761 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
764 /* Internal data structure for isl_union_set_every_set.
766 * "test" is the user-specified callback function.
767 * "user" is the user-specified callback function argument.
769 struct isl_test_set_from_map_data
{
770 isl_bool (*test
)(__isl_keep isl_set
*set
, void *user
);
774 /* Call data->test on "map", which is part of an isl_union_set and
775 * therefore known to be an isl_set.
777 static isl_bool
test_set_from_map(__isl_keep isl_map
*map
, void *user
)
779 struct isl_test_set_from_map_data
*data
= user
;
781 return data
->test(set_from_map(map
), data
->user
);
784 /* Does "test" succeed on every set in "uset"?
786 isl_bool
isl_union_set_every_set(__isl_keep isl_union_set
*uset
,
787 isl_bool (*test
)(__isl_keep isl_set
*set
, void *user
), void *user
)
789 struct isl_test_set_from_map_data data
= { test
, user
};
791 return isl_union_map_every_map(uset_to_umap(uset
),
792 &test_set_from_map
, &data
);
795 struct isl_union_set_foreach_point_data
{
796 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
800 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
802 struct isl_union_set_foreach_point_data
*data
= user
;
805 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
811 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
812 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
814 struct isl_union_set_foreach_point_data data
= { fn
, user
};
815 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
818 /* Data structure that specifies how gen_bin_op should
819 * construct results from the inputs.
821 * If "subtract" is set, then a map in the first input is copied to the result
822 * if there is no corresponding map in the second input.
823 * Otherwise, a map in the first input with no corresponding map
824 * in the second input is ignored.
825 * If "filter" is not NULL, then it specifies which maps in the first
826 * input may have a matching map in the second input.
827 * In particular, it makes sure that "match_space" can be called
828 * on the space of the map.
829 * "match_space" specifies how to transform the space of a map
830 * in the first input to the space of the corresponding map
831 * in the second input.
832 * "fn_map" specifies how the matching maps, one from each input,
833 * should be combined to form a map in the result.
835 struct isl_bin_op_control
{
837 isl_bool (*filter
)(__isl_keep isl_map
*map
);
838 __isl_give isl_space
*(*match_space
)(__isl_take isl_space
*space
);
839 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map1
,
840 __isl_take isl_map
*map2
);
843 /* Internal data structure for gen_bin_op.
844 * "control" specifies how the maps in the result should be constructed.
845 * "umap2" is a pointer to the second argument.
846 * "res" collects the results.
848 struct isl_union_map_gen_bin_data
{
849 struct isl_bin_op_control
*control
;
850 isl_union_map
*umap2
;
854 /* Add a copy of "map" to "res" and return the result.
856 static __isl_give isl_union_map
*bin_add_map(__isl_take isl_union_map
*res
,
857 __isl_keep isl_map
*map
)
859 return isl_union_map_add_map(res
, isl_map_copy(map
));
862 /* Combine "map1" and "map2", add the result to "res" and return the result.
863 * Check whether the result is empty before adding it to "res".
865 static __isl_give isl_union_map
*bin_add_pair(__isl_take isl_union_map
*res
,
866 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
867 struct isl_union_map_gen_bin_data
*data
)
872 map
= data
->control
->fn_map(isl_map_copy(map1
), isl_map_copy(map2
));
873 empty
= isl_map_is_empty(map
);
874 if (empty
< 0 || empty
) {
877 return isl_union_map_free(res
);
880 return isl_union_map_add_map(res
, map
);
883 /* Dummy match_space function that simply returns the input space.
885 static __isl_give isl_space
*identity(__isl_take isl_space
*space
)
890 /* Look for the map in data->umap2 that corresponds to "map", if any.
891 * Return (isl_bool_true, matching map) if there is one,
892 * (isl_bool_false, NULL) if there is no matching map and
893 * (isl_bool_error, NULL) on error.
895 * If not NULL, then data->control->filter specifies whether "map"
896 * can have any matching map. If so,
897 * data->control->match_space specifies which map in data->umap2
898 * corresponds to "map".
900 static __isl_keep isl_maybe_isl_map
bin_try_get_match(
901 struct isl_union_map_gen_bin_data
*data
, __isl_keep isl_map
*map
)
904 struct isl_hash_table_entry
*entry2
;
906 isl_maybe_isl_map res
= { isl_bool_error
, NULL
};
908 if (data
->control
->filter
) {
909 res
.valid
= data
->control
->filter(map
);
910 if (res
.valid
< 0 || !res
.valid
)
912 res
.valid
= isl_bool_error
;
915 space
= isl_map_get_space(map
);
916 if (data
->control
->match_space
!= &identity
)
917 space
= data
->control
->match_space(space
);
920 hash
= isl_space_get_hash(space
);
921 entry2
= isl_hash_table_find(isl_union_map_get_ctx(data
->umap2
),
922 &data
->umap2
->table
, hash
,
923 &has_space
, space
, 0);
924 isl_space_free(space
);
926 res
.valid
= isl_bool_ok(entry2
!= isl_hash_table_entry_none
);
927 if (res
.valid
>= 0 && res
.valid
)
928 res
.value
= entry2
->data
;
933 /* isl_hash_table_foreach callback for gen_bin_op.
934 * Look for the map in data->umap2 that corresponds
935 * to the map that "entry" points to, apply the binary operation and
936 * add the result to data->res.
938 * If no corresponding map can be found, then the effect depends
939 * on data->control->subtract. If it is set, then the current map
940 * is added directly to the result. Otherwise, it is ignored.
942 static isl_stat
gen_bin_entry(void **entry
, void *user
)
944 struct isl_union_map_gen_bin_data
*data
= user
;
945 isl_map
*map
= *entry
;
948 m
= bin_try_get_match(data
, map
);
950 return isl_stat_error
;
951 if (!m
.valid
&& !data
->control
->subtract
)
955 data
->res
= bin_add_map(data
->res
, map
);
957 data
->res
= bin_add_pair(data
->res
, map
, m
.value
, data
);
959 return isl_stat_error
;
964 /* Apply a binary operation to "umap1" and "umap2" based on "control".
965 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
968 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
969 __isl_take isl_union_map
*umap2
, struct isl_bin_op_control
*control
)
971 struct isl_union_map_gen_bin_data data
= { control
, NULL
, NULL
};
973 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
974 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
976 if (!umap1
|| !umap2
)
980 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
982 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
983 &gen_bin_entry
, &data
) < 0)
986 isl_union_map_free(umap1
);
987 isl_union_map_free(umap2
);
990 isl_union_map_free(umap1
);
991 isl_union_map_free(umap2
);
992 isl_union_map_free(data
.res
);
996 __isl_give isl_union_map
*isl_union_map_subtract(
997 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
999 struct isl_bin_op_control control
= {
1001 .match_space
= &identity
,
1002 .fn_map
= &isl_map_subtract
,
1005 return gen_bin_op(umap1
, umap2
, &control
);
1008 __isl_give isl_union_set
*isl_union_set_subtract(
1009 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1011 return isl_union_map_subtract(uset1
, uset2
);
1014 struct isl_union_map_gen_bin_set_data
{
1019 static isl_stat
intersect_params_entry(void **entry
, void *user
)
1021 struct isl_union_map_gen_bin_set_data
*data
= user
;
1022 isl_map
*map
= *entry
;
1025 map
= isl_map_copy(map
);
1026 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
1028 empty
= isl_map_is_empty(map
);
1031 return isl_stat_error
;
1034 data
->res
= isl_union_map_add_map(data
->res
, map
);
1039 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
1040 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
1042 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
1044 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
1045 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
1051 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
1053 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1057 isl_union_map_free(umap
);
1061 isl_union_map_free(umap
);
1063 isl_union_map_free(data
.res
);
1067 /* Intersect "umap" with the parameter domain "set".
1069 * If "set" does not have any constraints, then we can return immediately.
1071 __isl_give isl_union_map
*isl_union_map_intersect_params(
1072 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1076 is_universe
= isl_set_plain_is_universe(set
);
1077 if (is_universe
< 0)
1084 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
1086 isl_union_map_free(umap
);
1091 __isl_give isl_union_set
*isl_union_set_intersect_params(
1092 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1094 return isl_union_map_intersect_params(uset
, set
);
1097 static __isl_give isl_union_map
*union_map_intersect_params(
1098 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1100 return isl_union_map_intersect_params(umap
,
1101 isl_set_from_union_set(uset
));
1104 static __isl_give isl_union_map
*union_map_gist_params(
1105 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1107 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
1110 struct isl_union_map_match_bin_data
{
1111 isl_union_map
*umap2
;
1113 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
1116 static isl_stat
match_bin_entry(void **entry
, void *user
)
1118 struct isl_union_map_match_bin_data
*data
= user
;
1120 struct isl_hash_table_entry
*entry2
;
1121 isl_map
*map
= *entry
;
1124 hash
= isl_space_get_hash(map
->dim
);
1125 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1126 hash
, &has_space
, map
->dim
, 0);
1128 return isl_stat_error
;
1129 if (entry2
== isl_hash_table_entry_none
)
1132 map
= isl_map_copy(map
);
1133 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
1135 empty
= isl_map_is_empty(map
);
1138 return isl_stat_error
;
1145 data
->res
= isl_union_map_add_map(data
->res
, map
);
1150 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
1151 __isl_take isl_union_map
*umap2
,
1152 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
1154 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
1156 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1157 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1159 if (!umap1
|| !umap2
)
1163 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1165 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1166 &match_bin_entry
, &data
) < 0)
1169 isl_union_map_free(umap1
);
1170 isl_union_map_free(umap2
);
1173 isl_union_map_free(umap1
);
1174 isl_union_map_free(umap2
);
1175 isl_union_map_free(data
.res
);
1179 __isl_give isl_union_map
*isl_union_map_intersect(
1180 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1182 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
1185 /* Compute the intersection of the two union_sets.
1186 * As a special case, if exactly one of the two union_sets
1187 * is a parameter domain, then intersect the parameter domain
1188 * of the other one with this set.
1190 __isl_give isl_union_set
*isl_union_set_intersect(
1191 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1195 p1
= isl_union_set_is_params(uset1
);
1196 p2
= isl_union_set_is_params(uset2
);
1197 if (p1
< 0 || p2
< 0)
1200 return union_map_intersect_params(uset1
, uset2
);
1202 return union_map_intersect_params(uset2
, uset1
);
1203 return isl_union_map_intersect(uset1
, uset2
);
1205 isl_union_set_free(uset1
);
1206 isl_union_set_free(uset2
);
1210 static isl_stat
gist_params_entry(void **entry
, void *user
)
1212 struct isl_union_map_gen_bin_set_data
*data
= user
;
1213 isl_map
*map
= *entry
;
1216 map
= isl_map_copy(map
);
1217 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
1219 empty
= isl_map_is_empty(map
);
1222 return isl_stat_error
;
1225 data
->res
= isl_union_map_add_map(data
->res
, map
);
1230 __isl_give isl_union_map
*isl_union_map_gist_params(
1231 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1233 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
1236 __isl_give isl_union_set
*isl_union_set_gist_params(
1237 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1239 return isl_union_map_gist_params(uset
, set
);
1242 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
1243 __isl_take isl_union_map
*context
)
1245 return match_bin_op(umap
, context
, &isl_map_gist
);
1248 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
1249 __isl_take isl_union_set
*context
)
1251 if (isl_union_set_is_params(context
))
1252 return union_map_gist_params(uset
, context
);
1253 return isl_union_map_gist(uset
, context
);
1256 /* For each map in "umap", remove the constraints in the corresponding map
1258 * Each map in "context" is assumed to consist of a single disjunct and
1259 * to have explicit representations for all local variables.
1261 __isl_give isl_union_map
*isl_union_map_plain_gist(
1262 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*context
)
1264 return match_bin_op(umap
, context
, &isl_map_plain_gist
);
1267 /* For each set in "uset", remove the constraints in the corresponding set
1269 * Each set in "context" is assumed to consist of a single disjunct and
1270 * to have explicit representations for all local variables.
1272 __isl_give isl_union_set
*isl_union_set_plain_gist(
1273 __isl_take isl_union_set
*uset
, __isl_take isl_union_set
*context
)
1275 return isl_union_map_plain_gist(uset
, context
);
1278 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
1279 __isl_take isl_map
*set2
)
1281 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
1284 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
1285 __isl_take isl_map
*set2
)
1287 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
1290 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
1291 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1293 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
1296 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
1297 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1299 return match_bin_op(uset1
, uset2
, &lex_le_set
);
1302 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
1303 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1305 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
1308 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
1309 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1311 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
1314 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
1315 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1317 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
1320 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
1321 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1323 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
1326 /* Intersect the domain of "umap" with "uset".
1328 static __isl_give isl_union_map
*union_map_intersect_domain(
1329 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1331 struct isl_bin_op_control control
= {
1332 .match_space
= &isl_space_domain
,
1333 .fn_map
= &isl_map_intersect_domain
,
1336 return gen_bin_op(umap
, uset
, &control
);
1339 /* Intersect the domain of "umap" with "uset".
1340 * If "uset" is a parameters domain, then intersect the parameter
1341 * domain of "umap" with this set.
1343 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1344 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1346 if (isl_union_set_is_params(uset
))
1347 return union_map_intersect_params(umap
, uset
);
1349 return union_map_intersect_domain(umap
, uset
);
1352 /* Remove the elements of "uset" from the domain of "umap".
1354 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1355 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1357 struct isl_bin_op_control control
= {
1359 .match_space
= &isl_space_domain
,
1360 .fn_map
= &isl_map_subtract_domain
,
1363 return gen_bin_op(umap
, dom
, &control
);
1366 /* Remove the elements of "uset" from the range of "umap".
1368 __isl_give isl_union_map
*isl_union_map_subtract_range(
1369 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1371 struct isl_bin_op_control control
= {
1373 .match_space
= &isl_space_range
,
1374 .fn_map
= &isl_map_subtract_range
,
1377 return gen_bin_op(umap
, dom
, &control
);
1380 /* Compute the gist of "umap" with respect to the domain "uset".
1382 static __isl_give isl_union_map
*union_map_gist_domain(
1383 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1385 struct isl_bin_op_control control
= {
1386 .match_space
= &isl_space_domain
,
1387 .fn_map
= &isl_map_gist_domain
,
1390 return gen_bin_op(umap
, uset
, &control
);
1393 /* Compute the gist of "umap" with respect to the domain "uset".
1394 * If "uset" is a parameters domain, then compute the gist
1395 * with respect to this parameter domain.
1397 __isl_give isl_union_map
*isl_union_map_gist_domain(
1398 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1400 if (isl_union_set_is_params(uset
))
1401 return union_map_gist_params(umap
, uset
);
1403 return union_map_gist_domain(umap
, uset
);
1406 /* Compute the gist of "umap" with respect to the range "uset".
1408 __isl_give isl_union_map
*isl_union_map_gist_range(
1409 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1411 struct isl_bin_op_control control
= {
1412 .match_space
= &isl_space_range
,
1413 .fn_map
= &isl_map_gist_range
,
1416 return gen_bin_op(umap
, uset
, &control
);
1419 __isl_give isl_union_map
*isl_union_map_intersect_range(
1420 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1422 struct isl_bin_op_control control
= {
1423 .match_space
= &isl_space_range
,
1424 .fn_map
= &isl_map_intersect_range
,
1427 return gen_bin_op(umap
, uset
, &control
);
1430 /* Intersect each map in "umap" in a space [A -> B] -> C
1431 * with the corresponding map in "factor" in the space B -> C and
1432 * collect the results.
1434 __isl_give isl_union_map
*isl_union_map_intersect_domain_factor_range(
1435 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1437 struct isl_bin_op_control control
= {
1438 .filter
= &isl_map_domain_is_wrapping
,
1439 .match_space
= &isl_space_domain_factor_range
,
1440 .fn_map
= &isl_map_intersect_domain_factor_range
,
1443 return gen_bin_op(umap
, factor
, &control
);
1446 /* Intersect each map in "umap" in a space A -> [B -> C]
1447 * with the corresponding map in "factor" in the space A -> B and
1448 * collect the results.
1450 __isl_give isl_union_map
*isl_union_map_intersect_range_factor_domain(
1451 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1453 struct isl_bin_op_control control
= {
1454 .filter
= &isl_map_range_is_wrapping
,
1455 .match_space
= &isl_space_range_factor_domain
,
1456 .fn_map
= &isl_map_intersect_range_factor_domain
,
1459 return gen_bin_op(umap
, factor
, &control
);
1462 /* Intersect each map in "umap" in a space A -> [B -> C]
1463 * with the corresponding map in "factor" in the space A -> C and
1464 * collect the results.
1466 __isl_give isl_union_map
*isl_union_map_intersect_range_factor_range(
1467 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1469 struct isl_bin_op_control control
= {
1470 .filter
= &isl_map_range_is_wrapping
,
1471 .match_space
= &isl_space_range_factor_range
,
1472 .fn_map
= &isl_map_intersect_range_factor_range
,
1475 return gen_bin_op(umap
, factor
, &control
);
1478 struct isl_union_map_bin_data
{
1479 isl_union_map
*umap2
;
1482 isl_stat (*fn
)(void **entry
, void *user
);
1485 static isl_stat
apply_range_entry(void **entry
, void *user
)
1487 struct isl_union_map_bin_data
*data
= user
;
1488 isl_map
*map2
= *entry
;
1491 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1492 map2
->dim
, isl_dim_in
))
1495 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1497 empty
= isl_map_is_empty(map2
);
1500 return isl_stat_error
;
1507 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1512 static isl_stat
bin_entry(void **entry
, void *user
)
1514 struct isl_union_map_bin_data
*data
= user
;
1515 isl_map
*map
= *entry
;
1518 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1519 data
->fn
, data
) < 0)
1520 return isl_stat_error
;
1525 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1526 __isl_take isl_union_map
*umap2
,
1527 isl_stat (*fn
)(void **entry
, void *user
))
1529 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1531 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1532 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1534 if (!umap1
|| !umap2
)
1538 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1540 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1541 &bin_entry
, &data
) < 0)
1544 isl_union_map_free(umap1
);
1545 isl_union_map_free(umap2
);
1548 isl_union_map_free(umap1
);
1549 isl_union_map_free(umap2
);
1550 isl_union_map_free(data
.res
);
1554 __isl_give isl_union_map
*isl_union_map_apply_range(
1555 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1557 return bin_op(umap1
, umap2
, &apply_range_entry
);
1560 __isl_give isl_union_map
*isl_union_map_apply_domain(
1561 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1563 umap1
= isl_union_map_reverse(umap1
);
1564 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1565 return isl_union_map_reverse(umap1
);
1568 __isl_give isl_union_set
*isl_union_set_apply(
1569 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1571 return isl_union_map_apply_range(uset
, umap
);
1574 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1576 struct isl_union_map_bin_data
*data
= user
;
1577 isl_map
*map2
= *entry
;
1579 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1580 map2
->dim
, isl_dim_out
))
1583 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1585 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1590 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1591 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1593 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1596 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1598 struct isl_union_map_bin_data
*data
= user
;
1599 isl_map
*map2
= *entry
;
1601 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1602 map2
->dim
, isl_dim_out
))
1605 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1607 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1612 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1613 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1615 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1618 static isl_stat
product_entry(void **entry
, void *user
)
1620 struct isl_union_map_bin_data
*data
= user
;
1621 isl_map
*map2
= *entry
;
1623 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1625 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1630 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1631 __isl_take isl_union_map
*umap2
)
1633 return bin_op(umap1
, umap2
, &product_entry
);
1636 static isl_stat
set_product_entry(void **entry
, void *user
)
1638 struct isl_union_map_bin_data
*data
= user
;
1639 isl_set
*set2
= *entry
;
1641 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1643 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1648 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1649 __isl_take isl_union_set
*uset2
)
1651 return bin_op(uset1
, uset2
, &set_product_entry
);
1654 static isl_stat
domain_product_entry(void **entry
, void *user
)
1656 struct isl_union_map_bin_data
*data
= user
;
1657 isl_map
*map2
= *entry
;
1659 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1660 map2
->dim
, isl_dim_out
))
1663 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1664 isl_map_copy(map2
));
1666 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1671 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1673 __isl_give isl_union_map
*isl_union_map_domain_product(
1674 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1676 return bin_op(umap1
, umap2
, &domain_product_entry
);
1679 static isl_stat
range_product_entry(void **entry
, void *user
)
1681 struct isl_union_map_bin_data
*data
= user
;
1682 isl_map
*map2
= *entry
;
1684 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1685 map2
->dim
, isl_dim_in
))
1688 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1689 isl_map_copy(map2
));
1691 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1696 __isl_give isl_union_map
*isl_union_map_range_product(
1697 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1699 return bin_op(umap1
, umap2
, &range_product_entry
);
1702 /* If data->map A -> B and "map2" C -> D have the same range space,
1703 * then add (A, C) -> (B * D) to data->res.
1705 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1707 struct isl_union_map_bin_data
*data
= user
;
1708 isl_map
*map2
= *entry
;
1710 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1711 map2
->dim
, isl_dim_out
))
1714 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1715 isl_map_copy(map2
));
1717 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1722 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1724 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1725 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1727 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1730 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1732 struct isl_union_map_bin_data
*data
= user
;
1733 isl_map
*map2
= *entry
;
1735 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1736 map2
->dim
, isl_dim_in
))
1739 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1740 isl_map_copy(map2
));
1742 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1747 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1748 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1750 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1753 /* Data structure that specifies how un_op should modify
1754 * the maps in the union map.
1756 * If "inplace" is set, then the maps in the input union map
1757 * are modified in place. This means that "fn_map" should not
1758 * change the meaning of the map or that the union map only
1759 * has a single reference.
1760 * If "total" is set, then all maps need to be modified and
1761 * the results need to live in the same space.
1762 * Otherwise, a new union map is constructed to store the results.
1763 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1764 * are taken into account. "filter_user" is passed as the second argument
1765 * to "filter". No filter can be set if "inplace" or
1767 * Exactly one of "fn_map" or "fn_map2" should be set, specifying
1768 * how the maps (selected by "filter") should be transformed.
1769 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1771 struct isl_un_op_control
{
1774 isl_bool (*filter
)(__isl_keep isl_map
*map
, void *user
);
1776 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map
);
1777 __isl_give isl_map
*(*fn_map2
)(__isl_take isl_map
*map
, void *user
);
1781 /* Data structure for wrapping the data for un_op_filter_drop_user.
1782 * "filter" is the function that is being wrapped.
1784 struct isl_un_op_drop_user_data
{
1785 isl_bool (*filter
)(__isl_keep isl_map
*map
);
1788 /* Wrapper for isl_un_op_control filters that do not require
1789 * a second argument.
1790 * Simply call data->filter without the second argument.
1792 static isl_bool
un_op_filter_drop_user(__isl_keep isl_map
*map
, void *user
)
1794 struct isl_un_op_drop_user_data
*data
= user
;
1795 return data
->filter(map
);
1798 /* Internal data structure for "un_op".
1799 * "control" specifies how the maps in the union map should be modified.
1800 * "res" collects the results.
1802 struct isl_union_map_un_data
{
1803 struct isl_un_op_control
*control
;
1807 /* isl_hash_table_foreach callback for un_op.
1808 * Handle the map that "entry" points to.
1810 * If control->filter is set, then check if this map satisfies the filter.
1811 * If so (or if control->filter is not set), modify the map
1812 * by calling control->fn_map or control->fn_map2 and
1813 * either add the result to data->res or
1814 * replace the original entry by the result (if control->inplace is set).
1816 static isl_stat
un_entry(void **entry
, void *user
)
1818 struct isl_union_map_un_data
*data
= user
;
1819 struct isl_un_op_control
*control
= data
->control
;
1820 isl_map
*map
= *entry
;
1822 if (control
->filter
) {
1825 ok
= control
->filter(map
, control
->filter_user
);
1827 return isl_stat_error
;
1832 map
= isl_map_copy(map
);
1833 if (control
->fn_map2
!= NULL
)
1834 map
= control
->fn_map2(map
, control
->fn_map2_user
);
1836 map
= control
->fn_map(map
);
1838 return isl_stat_error
;
1839 if (control
->inplace
) {
1840 isl_map_free(*entry
);
1843 data
->res
= isl_union_map_add_map(data
->res
, map
);
1845 return isl_stat_error
;
1851 /* Modify the maps in "umap" based on "control".
1852 * If control->inplace is set, then modify the maps in "umap" in-place.
1853 * Otherwise, create a new union map to hold the results.
1854 * If control->total is set, then perform an inplace computation
1855 * if "umap" is only referenced once. Otherwise, create a new union map
1856 * to store the results.
1858 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1859 struct isl_un_op_control
*control
)
1861 struct isl_union_map_un_data data
= { control
};
1865 if (!!control
->fn_map
== !!control
->fn_map2
)
1866 isl_die(isl_union_map_get_ctx(umap
), isl_error_internal
,
1867 "exactly one mapping function should be specified",
1868 return isl_union_map_free(umap
));
1869 if ((control
->inplace
|| control
->total
) && control
->filter
)
1870 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
1871 "inplace/total modification cannot be filtered",
1872 return isl_union_map_free(umap
));
1874 if (control
->total
&& umap
->ref
== 1)
1875 control
->inplace
= 1;
1876 if (control
->inplace
) {
1881 space
= isl_union_map_get_space(umap
);
1882 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
1884 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1885 &umap
->table
, &un_entry
, &data
) < 0)
1886 data
.res
= isl_union_map_free(data
.res
);
1888 if (control
->inplace
)
1890 isl_union_map_free(umap
);
1894 __isl_give isl_union_map
*isl_union_map_from_range(
1895 __isl_take isl_union_set
*uset
)
1897 struct isl_un_op_control control
= {
1898 .fn_map
= &isl_map_from_range
,
1900 return un_op(uset
, &control
);
1903 __isl_give isl_union_map
*isl_union_map_from_domain(
1904 __isl_take isl_union_set
*uset
)
1906 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1909 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1910 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1912 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1913 isl_union_map_from_range(range
));
1916 /* Modify the maps in "umap" by applying "fn" on them.
1917 * "fn" should apply to all maps in "umap" and should not modify the space.
1919 static __isl_give isl_union_map
*total(__isl_take isl_union_map
*umap
,
1920 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1922 struct isl_un_op_control control
= {
1927 return un_op(umap
, &control
);
1930 /* Compute the affine hull of "map" and return the result as an isl_map.
1932 static __isl_give isl_map
*isl_map_affine_hull_map(__isl_take isl_map
*map
)
1934 return isl_map_from_basic_map(isl_map_affine_hull(map
));
1937 __isl_give isl_union_map
*isl_union_map_affine_hull(
1938 __isl_take isl_union_map
*umap
)
1940 return total(umap
, &isl_map_affine_hull_map
);
1943 __isl_give isl_union_set
*isl_union_set_affine_hull(
1944 __isl_take isl_union_set
*uset
)
1946 return isl_union_map_affine_hull(uset
);
1949 /* Wrapper around isl_set_combined_lineality_space
1950 * that returns the combined lineality space in the form of an isl_set
1951 * instead of an isl_basic_set.
1953 static __isl_give isl_set
*combined_lineality_space(__isl_take isl_set
*set
)
1955 return isl_set_from_basic_set(isl_set_combined_lineality_space(set
));
1958 /* For each set in "uset", compute the (linear) hull
1959 * of the lineality spaces of its basic sets and
1960 * collect and return the results.
1962 __isl_give isl_union_set
*isl_union_set_combined_lineality_space(
1963 __isl_take isl_union_set
*uset
)
1965 struct isl_un_op_control control
= {
1966 .fn_map
= &combined_lineality_space
,
1968 return un_op(uset
, &control
);
1971 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1973 static __isl_give isl_map
*isl_map_polyhedral_hull_map(__isl_take isl_map
*map
)
1975 return isl_map_from_basic_map(isl_map_polyhedral_hull(map
));
1978 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1979 __isl_take isl_union_map
*umap
)
1981 return total(umap
, &isl_map_polyhedral_hull_map
);
1984 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1985 __isl_take isl_union_set
*uset
)
1987 return isl_union_map_polyhedral_hull(uset
);
1990 /* Compute a superset of the convex hull of "map" that is described
1991 * by only translates of the constraints in the constituents of "map" and
1992 * return the result as an isl_map.
1994 static __isl_give isl_map
*isl_map_simple_hull_map(__isl_take isl_map
*map
)
1996 return isl_map_from_basic_map(isl_map_simple_hull(map
));
1999 __isl_give isl_union_map
*isl_union_map_simple_hull(
2000 __isl_take isl_union_map
*umap
)
2002 return total(umap
, &isl_map_simple_hull_map
);
2005 __isl_give isl_union_set
*isl_union_set_simple_hull(
2006 __isl_take isl_union_set
*uset
)
2008 return isl_union_map_simple_hull(uset
);
2011 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
2012 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
2014 struct isl_un_op_control control
= {
2019 return un_op(umap
, &control
);
2022 /* Remove redundant constraints in each of the basic maps of "umap".
2023 * Since removing redundant constraints does not change the meaning
2024 * or the space, the operation can be performed in-place.
2026 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
2027 __isl_take isl_union_map
*umap
)
2029 return inplace(umap
, &isl_map_remove_redundancies
);
2032 /* Remove redundant constraints in each of the basic sets of "uset".
2034 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
2035 __isl_take isl_union_set
*uset
)
2037 return isl_union_map_remove_redundancies(uset
);
2040 __isl_give isl_union_map
*isl_union_map_coalesce(
2041 __isl_take isl_union_map
*umap
)
2043 return inplace(umap
, &isl_map_coalesce
);
2046 __isl_give isl_union_set
*isl_union_set_coalesce(
2047 __isl_take isl_union_set
*uset
)
2049 return isl_union_map_coalesce(uset
);
2052 __isl_give isl_union_map
*isl_union_map_detect_equalities(
2053 __isl_take isl_union_map
*umap
)
2055 return inplace(umap
, &isl_map_detect_equalities
);
2058 __isl_give isl_union_set
*isl_union_set_detect_equalities(
2059 __isl_take isl_union_set
*uset
)
2061 return isl_union_map_detect_equalities(uset
);
2064 __isl_give isl_union_map
*isl_union_map_compute_divs(
2065 __isl_take isl_union_map
*umap
)
2067 return inplace(umap
, &isl_map_compute_divs
);
2070 __isl_give isl_union_set
*isl_union_set_compute_divs(
2071 __isl_take isl_union_set
*uset
)
2073 return isl_union_map_compute_divs(uset
);
2076 __isl_give isl_union_map
*isl_union_map_lexmin(
2077 __isl_take isl_union_map
*umap
)
2079 return total(umap
, &isl_map_lexmin
);
2082 __isl_give isl_union_set
*isl_union_set_lexmin(
2083 __isl_take isl_union_set
*uset
)
2085 return isl_union_map_lexmin(uset
);
2088 __isl_give isl_union_map
*isl_union_map_lexmax(
2089 __isl_take isl_union_map
*umap
)
2091 return total(umap
, &isl_map_lexmax
);
2094 __isl_give isl_union_set
*isl_union_set_lexmax(
2095 __isl_take isl_union_set
*uset
)
2097 return isl_union_map_lexmax(uset
);
2100 /* Return the universe in the space of "map".
2102 static __isl_give isl_map
*universe(__isl_take isl_map
*map
)
2106 space
= isl_map_get_space(map
);
2108 return isl_map_universe(space
);
2111 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
2113 struct isl_un_op_control control
= {
2114 .fn_map
= &universe
,
2116 return un_op(umap
, &control
);
2119 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
2121 return isl_union_map_universe(uset
);
2124 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
2126 struct isl_un_op_control control
= {
2127 .fn_map
= &isl_map_reverse
,
2129 return un_op(umap
, &control
);
2132 /* Given a union map, take the maps of the form A -> (B -> C) and
2133 * return the union of the corresponding maps A -> (C -> B).
2135 __isl_give isl_union_map
*isl_union_map_range_reverse(
2136 __isl_take isl_union_map
*umap
)
2138 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2139 struct isl_un_op_control control
= {
2140 .filter
= &un_op_filter_drop_user
,
2141 .filter_user
= &data
,
2142 .fn_map
= &isl_map_range_reverse
,
2144 return un_op(umap
, &control
);
2147 /* Compute the parameter domain of the given union map.
2149 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
2151 struct isl_un_op_control control
= {
2152 .fn_map
= &isl_map_params
,
2156 empty
= isl_union_map_is_empty(umap
);
2161 space
= isl_union_map_get_space(umap
);
2162 isl_union_map_free(umap
);
2163 return isl_set_empty(space
);
2165 return isl_set_from_union_set(un_op(umap
, &control
));
2167 isl_union_map_free(umap
);
2171 /* Compute the parameter domain of the given union set.
2173 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
2175 return isl_union_map_params(uset
);
2178 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
2180 struct isl_un_op_control control
= {
2181 .fn_map
= &isl_map_domain
,
2183 return un_op(umap
, &control
);
2186 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
2188 struct isl_un_op_control control
= {
2189 .fn_map
= &isl_map_range
,
2191 return un_op(umap
, &control
);
2194 __isl_give isl_union_map
*isl_union_map_domain_map(
2195 __isl_take isl_union_map
*umap
)
2197 struct isl_un_op_control control
= {
2198 .fn_map
= &isl_map_domain_map
,
2200 return un_op(umap
, &control
);
2203 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2204 * add the result to "res".
2206 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
2208 isl_union_pw_multi_aff
**res
= user
;
2210 isl_pw_multi_aff
*pma
;
2212 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
2213 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
2214 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2216 return *res
? isl_stat_ok
: isl_stat_error
;
2220 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2223 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
2224 __isl_take isl_union_map
*umap
)
2226 isl_union_pw_multi_aff
*res
;
2228 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
2229 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
2230 res
= isl_union_pw_multi_aff_free(res
);
2232 isl_union_map_free(umap
);
2236 __isl_give isl_union_map
*isl_union_map_range_map(
2237 __isl_take isl_union_map
*umap
)
2239 struct isl_un_op_control control
= {
2240 .fn_map
= &isl_map_range_map
,
2242 return un_op(umap
, &control
);
2245 /* Given a collection of wrapped maps of the form A[B -> C],
2246 * return the collection of maps A[B -> C] -> B.
2248 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
2249 __isl_take isl_union_set
*uset
)
2251 struct isl_un_op_drop_user_data data
= { &isl_set_is_wrapping
};
2252 struct isl_un_op_control control
= {
2253 .filter
= &un_op_filter_drop_user
,
2254 .filter_user
= &data
,
2255 .fn_map
= &isl_set_wrapped_domain_map
,
2257 return un_op(uset
, &control
);
2260 /* Does "map" relate elements from the same space?
2262 static isl_bool
equal_tuples(__isl_keep isl_map
*map
, void *user
)
2264 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2265 map
->dim
, isl_dim_out
);
2268 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
2270 struct isl_un_op_control control
= {
2271 .filter
= &equal_tuples
,
2272 .fn_map
= &isl_map_deltas
,
2274 return un_op(umap
, &control
);
2277 __isl_give isl_union_map
*isl_union_map_deltas_map(
2278 __isl_take isl_union_map
*umap
)
2280 struct isl_un_op_control control
= {
2281 .filter
= &equal_tuples
,
2282 .fn_map
= &isl_map_deltas_map
,
2284 return un_op(umap
, &control
);
2287 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
2289 struct isl_un_op_control control
= {
2290 .fn_map
= &isl_set_identity
,
2292 return un_op(uset
, &control
);
2295 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2297 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
2299 isl_union_pw_multi_aff
**res
= user
;
2301 isl_pw_multi_aff
*pma
;
2303 space
= isl_space_map_from_set(isl_set_get_space(set
));
2304 pma
= isl_pw_multi_aff_identity(space
);
2305 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
2306 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2308 return *res
? isl_stat_ok
: isl_stat_error
;
2311 /* Return an identity function on "uset" in the form
2312 * of an isl_union_pw_multi_aff.
2314 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
2315 __isl_take isl_union_set
*uset
)
2317 isl_union_pw_multi_aff
*res
;
2319 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
2320 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
2321 res
= isl_union_pw_multi_aff_free(res
);
2323 isl_union_set_free(uset
);
2327 /* For each map in "umap" of the form [A -> B] -> C,
2328 * construct the map A -> C and collect the results.
2330 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2331 __isl_take isl_union_map
*umap
)
2333 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2334 struct isl_un_op_control control
= {
2335 .filter
= &un_op_filter_drop_user
,
2336 .filter_user
= &data
,
2337 .fn_map
= &isl_map_domain_factor_domain
,
2339 return un_op(umap
, &control
);
2342 /* For each map in "umap" of the form [A -> B] -> C,
2343 * construct the map B -> C and collect the results.
2345 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2346 __isl_take isl_union_map
*umap
)
2348 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2349 struct isl_un_op_control control
= {
2350 .filter
= &un_op_filter_drop_user
,
2351 .filter_user
= &data
,
2352 .fn_map
= &isl_map_domain_factor_range
,
2354 return un_op(umap
, &control
);
2357 /* For each map in "umap" of the form A -> [B -> C],
2358 * construct the map A -> B and collect the results.
2360 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2361 __isl_take isl_union_map
*umap
)
2363 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2364 struct isl_un_op_control control
= {
2365 .filter
= &un_op_filter_drop_user
,
2366 .filter_user
= &data
,
2367 .fn_map
= &isl_map_range_factor_domain
,
2369 return un_op(umap
, &control
);
2372 /* For each map in "umap" of the form A -> [B -> C],
2373 * construct the map A -> C and collect the results.
2375 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2376 __isl_take isl_union_map
*umap
)
2378 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2379 struct isl_un_op_control control
= {
2380 .filter
= &un_op_filter_drop_user
,
2381 .filter_user
= &data
,
2382 .fn_map
= &isl_map_range_factor_range
,
2384 return un_op(umap
, &control
);
2387 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2388 * construct the map A -> C and collect the results.
2390 __isl_give isl_union_map
*isl_union_map_factor_domain(
2391 __isl_take isl_union_map
*umap
)
2393 struct isl_un_op_drop_user_data data
= { &isl_map_is_product
};
2394 struct isl_un_op_control control
= {
2395 .filter
= &un_op_filter_drop_user
,
2396 .filter_user
= &data
,
2397 .fn_map
= &isl_map_factor_domain
,
2399 return un_op(umap
, &control
);
2402 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2403 * construct the map B -> D and collect the results.
2405 __isl_give isl_union_map
*isl_union_map_factor_range(
2406 __isl_take isl_union_map
*umap
)
2408 struct isl_un_op_drop_user_data data
= { &isl_map_is_product
};
2409 struct isl_un_op_control control
= {
2410 .filter
= &un_op_filter_drop_user
,
2411 .filter_user
= &data
,
2412 .fn_map
= &isl_map_factor_range
,
2414 return un_op(umap
, &control
);
2417 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2419 struct isl_un_op_drop_user_data data
= { &isl_set_is_wrapping
};
2420 struct isl_un_op_control control
= {
2421 .filter
= &un_op_filter_drop_user
,
2422 .filter_user
= &data
,
2423 .fn_map
= &isl_set_unwrap
,
2425 return un_op(uset
, &control
);
2428 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2430 struct isl_un_op_control control
= {
2431 .fn_map
= &isl_map_wrap
,
2433 return un_op(umap
, &control
);
2436 struct isl_union_map_is_subset_data
{
2437 isl_union_map
*umap2
;
2441 static isl_stat
is_subset_entry(void **entry
, void *user
)
2443 struct isl_union_map_is_subset_data
*data
= user
;
2445 struct isl_hash_table_entry
*entry2
;
2446 isl_map
*map
= *entry
;
2448 hash
= isl_space_get_hash(map
->dim
);
2449 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2450 hash
, &has_space
, map
->dim
, 0);
2452 return isl_stat_error
;
2453 if (entry2
== isl_hash_table_entry_none
) {
2454 int empty
= isl_map_is_empty(map
);
2456 return isl_stat_error
;
2459 data
->is_subset
= isl_bool_false
;
2460 return isl_stat_error
;
2463 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2464 if (data
->is_subset
< 0 || !data
->is_subset
)
2465 return isl_stat_error
;
2470 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2471 __isl_keep isl_union_map
*umap2
)
2473 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2475 umap1
= isl_union_map_copy(umap1
);
2476 umap2
= isl_union_map_copy(umap2
);
2477 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2478 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2480 if (!umap1
|| !umap2
)
2484 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2485 &is_subset_entry
, &data
) < 0 &&
2489 isl_union_map_free(umap1
);
2490 isl_union_map_free(umap2
);
2492 return data
.is_subset
;
2494 isl_union_map_free(umap1
);
2495 isl_union_map_free(umap2
);
2496 return isl_bool_error
;
2499 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2500 __isl_keep isl_union_set
*uset2
)
2502 return isl_union_map_is_subset(uset1
, uset2
);
2505 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2506 __isl_keep isl_union_map
*umap2
)
2510 if (!umap1
|| !umap2
)
2511 return isl_bool_error
;
2512 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2513 if (is_subset
!= isl_bool_true
)
2515 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2519 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2520 __isl_keep isl_union_set
*uset2
)
2522 return isl_union_map_is_equal(uset1
, uset2
);
2525 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2526 __isl_keep isl_union_map
*umap2
)
2530 if (!umap1
|| !umap2
)
2531 return isl_bool_error
;
2532 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2533 if (is_subset
!= isl_bool_true
)
2535 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2536 return isl_bool_not(is_subset
);
2539 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2540 __isl_keep isl_union_set
*uset2
)
2542 return isl_union_map_is_strict_subset(uset1
, uset2
);
2545 /* Internal data structure for isl_union_map_is_disjoint.
2546 * umap2 is the union map with which we are comparing.
2547 * is_disjoint is initialized to 1 and is set to 0 as soon
2548 * as the union maps turn out not to be disjoint.
2550 struct isl_union_map_is_disjoint_data
{
2551 isl_union_map
*umap2
;
2552 isl_bool is_disjoint
;
2555 /* Check if "map" is disjoint from data->umap2 and abort
2556 * the search if it is not.
2558 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2560 struct isl_union_map_is_disjoint_data
*data
= user
;
2562 struct isl_hash_table_entry
*entry2
;
2563 isl_map
*map
= *entry
;
2565 hash
= isl_space_get_hash(map
->dim
);
2566 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2567 hash
, &has_space
, map
->dim
, 0);
2569 return isl_stat_error
;
2570 if (entry2
== isl_hash_table_entry_none
)
2573 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2574 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2575 return isl_stat_error
;
2580 /* Are "umap1" and "umap2" disjoint?
2582 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2583 __isl_keep isl_union_map
*umap2
)
2585 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2587 umap1
= isl_union_map_copy(umap1
);
2588 umap2
= isl_union_map_copy(umap2
);
2589 umap1
= isl_union_map_align_params(umap1
,
2590 isl_union_map_get_space(umap2
));
2591 umap2
= isl_union_map_align_params(umap2
,
2592 isl_union_map_get_space(umap1
));
2594 if (!umap1
|| !umap2
)
2598 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2599 &is_disjoint_entry
, &data
) < 0 &&
2603 isl_union_map_free(umap1
);
2604 isl_union_map_free(umap2
);
2606 return data
.is_disjoint
;
2608 isl_union_map_free(umap1
);
2609 isl_union_map_free(umap2
);
2610 return isl_bool_error
;
2613 /* Are "uset1" and "uset2" disjoint?
2615 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2616 __isl_keep isl_union_set
*uset2
)
2618 return isl_union_map_is_disjoint(uset1
, uset2
);
2621 static isl_stat
sample_entry(void **entry
, void *user
)
2623 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2624 isl_map
*map
= *entry
;
2626 *sample
= isl_map_sample(isl_map_copy(map
));
2628 return isl_stat_error
;
2629 if (!isl_basic_map_plain_is_empty(*sample
))
2630 return isl_stat_error
;
2634 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2636 isl_basic_map
*sample
= NULL
;
2641 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2642 &sample_entry
, &sample
) < 0 &&
2647 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2649 isl_union_map_free(umap
);
2653 isl_union_map_free(umap
);
2657 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2659 return bset_from_bmap(isl_union_map_sample(uset
));
2662 /* Return an element in "uset" in the form of an isl_point.
2663 * Return a void isl_point if "uset" is empty.
2665 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2667 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2670 struct isl_forall_data
{
2672 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2675 static isl_stat
forall_entry(void **entry
, void *user
)
2677 struct isl_forall_data
*data
= user
;
2678 isl_map
*map
= *entry
;
2680 data
->res
= data
->fn(map
);
2682 return isl_stat_error
;
2685 return isl_stat_error
;
2690 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2691 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2693 struct isl_forall_data data
= { isl_bool_true
, fn
};
2696 return isl_bool_error
;
2698 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2699 &forall_entry
, &data
) < 0 && data
.res
)
2700 return isl_bool_error
;
2705 struct isl_forall_user_data
{
2707 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2711 static isl_stat
forall_user_entry(void **entry
, void *user
)
2713 struct isl_forall_user_data
*data
= user
;
2714 isl_map
*map
= *entry
;
2716 data
->res
= data
->fn(map
, data
->user
);
2718 return isl_stat_error
;
2721 return isl_stat_error
;
2726 /* Check if fn(map, user) returns true for all maps "map" in umap.
2728 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2729 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2731 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2734 return isl_bool_error
;
2736 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2737 &forall_user_entry
, &data
) < 0 && data
.res
)
2738 return isl_bool_error
;
2743 /* Is "umap" obviously empty?
2745 isl_bool
isl_union_map_plain_is_empty(__isl_keep isl_union_map
*umap
)
2749 n
= isl_union_map_n_map(umap
);
2751 return isl_bool_error
;
2755 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2757 return union_map_forall(umap
, &isl_map_is_empty
);
2760 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2762 return isl_union_map_is_empty(uset
);
2765 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2772 return isl_bool_error
;
2774 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2775 map
->dim
, isl_dim_out
))
2776 return isl_bool_false
;
2778 dim
= isl_map_get_space(map
);
2779 id
= isl_map_identity(dim
);
2781 is_subset
= isl_map_is_subset(map
, id
);
2788 /* Given an isl_union_map that consists of a single map, check
2789 * if it is single-valued.
2791 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2796 umap
= isl_union_map_copy(umap
);
2797 map
= isl_map_from_union_map(umap
);
2798 sv
= isl_map_is_single_valued(map
);
2804 /* Internal data structure for single_valued_on_domain.
2806 * "umap" is the union map to be tested.
2807 * "sv" is set to 1 as long as "umap" may still be single-valued.
2809 struct isl_union_map_is_sv_data
{
2810 isl_union_map
*umap
;
2814 /* Check if the data->umap is single-valued on "set".
2816 * If data->umap consists of a single map on "set", then test it
2819 * Otherwise, compute
2823 * check if the result is a subset of the identity mapping and
2824 * store the result in data->sv.
2826 * Terminate as soon as data->umap has been determined not to
2829 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2831 struct isl_union_map_is_sv_data
*data
= user
;
2832 isl_union_map
*umap
, *test
;
2835 umap
= isl_union_map_copy(data
->umap
);
2836 umap
= isl_union_map_intersect_domain(umap
,
2837 isl_union_set_from_set(set
));
2839 n
= isl_union_map_n_map(umap
);
2841 data
->sv
= isl_bool_error
;
2842 } else if (n
== 1) {
2843 data
->sv
= single_map_is_single_valued(umap
);
2844 isl_union_map_free(umap
);
2846 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2847 test
= isl_union_map_apply_range(test
, umap
);
2849 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2851 isl_union_map_free(test
);
2854 if (data
->sv
< 0 || !data
->sv
)
2855 return isl_stat_error
;
2859 /* Check if the given map is single-valued.
2861 * If the union map consists of a single map, then test it as an isl_map.
2862 * Otherwise, check if the union map is single-valued on each of its
2865 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2867 isl_union_map
*universe
;
2868 isl_union_set
*domain
;
2869 struct isl_union_map_is_sv_data data
;
2872 n
= isl_union_map_n_map(umap
);
2874 return isl_bool_error
;
2876 return single_map_is_single_valued(umap
);
2878 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2879 domain
= isl_union_map_domain(universe
);
2881 data
.sv
= isl_bool_true
;
2883 if (isl_union_set_foreach_set(domain
,
2884 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2885 data
.sv
= isl_bool_error
;
2886 isl_union_set_free(domain
);
2891 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2895 umap
= isl_union_map_copy(umap
);
2896 umap
= isl_union_map_reverse(umap
);
2897 in
= isl_union_map_is_single_valued(umap
);
2898 isl_union_map_free(umap
);
2903 /* Is "map" obviously not an identity relation because
2904 * it maps elements from one space to another space?
2905 * Update *non_identity accordingly.
2907 * In particular, if the domain and range spaces are the same,
2908 * then the map is not considered to obviously not be an identity relation.
2909 * Otherwise, the map is considered to obviously not be an identity relation
2910 * if it is is non-empty.
2912 * If "map" is determined to obviously not be an identity relation,
2913 * then the search is aborted.
2915 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2917 isl_bool
*non_identity
= user
;
2921 space
= isl_map_get_space(map
);
2922 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2923 if (equal
>= 0 && !equal
)
2924 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2926 *non_identity
= isl_bool_not(equal
);
2927 isl_space_free(space
);
2930 if (*non_identity
< 0 || *non_identity
)
2931 return isl_stat_error
;
2936 /* Is "umap" obviously not an identity relation because
2937 * it maps elements from one space to another space?
2939 * As soon as a map has been found that maps elements to a different space,
2940 * non_identity is changed and the search is aborted.
2942 static isl_bool
isl_union_map_plain_is_not_identity(
2943 __isl_keep isl_union_map
*umap
)
2945 isl_bool non_identity
;
2947 non_identity
= isl_bool_false
;
2948 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2949 &non_identity
) < 0 &&
2950 non_identity
== isl_bool_false
)
2951 return isl_bool_error
;
2953 return non_identity
;
2956 /* Does "map" only map elements to themselves?
2957 * Update *identity accordingly.
2959 * If "map" is determined not to be an identity relation,
2960 * then the search is aborted.
2962 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2964 isl_bool
*identity
= user
;
2966 *identity
= isl_map_is_identity(map
);
2969 if (*identity
< 0 || !*identity
)
2970 return isl_stat_error
;
2975 /* Does "umap" only map elements to themselves?
2977 * First check if there are any maps that map elements to different spaces.
2978 * If not, then check that all the maps (between identical spaces)
2979 * are identity relations.
2981 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2983 isl_bool non_identity
;
2986 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2987 if (non_identity
< 0 || non_identity
)
2988 return isl_bool_not(non_identity
);
2990 identity
= isl_bool_true
;
2991 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2992 identity
== isl_bool_true
)
2993 return isl_bool_error
;
2998 /* Represents a map that has a fixed value (v) for one of its
3000 * The map in this structure is not reference counted, so it
3001 * is only valid while the isl_union_map from which it was
3002 * obtained is still alive.
3004 struct isl_fixed_map
{
3009 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
3013 struct isl_fixed_map
*v
;
3015 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
3018 for (i
= 0; i
< n
; ++i
)
3019 isl_int_init(v
[i
].v
);
3023 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
3029 for (i
= 0; i
< n
; ++i
)
3030 isl_int_clear(v
[i
].v
);
3034 /* Compare the "v" field of two isl_fixed_map structs.
3036 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
3038 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
3039 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
3041 return isl_int_cmp(e1
->v
, e2
->v
);
3044 /* Internal data structure used while checking whether all maps
3045 * in a union_map have a fixed value for a given output dimension.
3046 * v is the list of maps, with the fixed value for the dimension
3047 * n is the number of maps considered so far
3048 * pos is the output dimension under investigation
3050 struct isl_fixed_dim_data
{
3051 struct isl_fixed_map
*v
;
3056 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
3058 struct isl_fixed_dim_data
*data
= user
;
3060 data
->v
[data
->n
].map
= map
;
3061 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
3062 &data
->v
[data
->n
++].v
);
3065 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
3066 int first
, int n_range
);
3068 /* Given a list of the maps, with their fixed values at output dimension "pos",
3069 * check whether the ranges of the maps form an obvious partition.
3071 * We first sort the maps according to their fixed values.
3072 * If all maps have a different value, then we know the ranges form
3074 * Otherwise, we collect the maps with the same fixed value and
3075 * check whether each such collection is obviously injective
3076 * based on later dimensions.
3078 static int separates(struct isl_fixed_map
*v
, int n
,
3079 __isl_take isl_space
*dim
, int pos
, int n_range
)
3086 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
3088 for (i
= 0; i
+ 1 < n
; ++i
) {
3090 isl_union_map
*part
;
3093 for (j
= i
+ 1; j
< n
; ++j
)
3094 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
3100 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
3101 for (k
= i
; k
< j
; ++k
)
3102 part
= isl_union_map_add_map(part
,
3103 isl_map_copy(v
[k
].map
));
3105 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
3114 isl_space_free(dim
);
3115 free_isl_fixed_map_array(v
, n
);
3118 isl_space_free(dim
);
3119 free_isl_fixed_map_array(v
, n
);
3123 /* Check whether the maps in umap have obviously distinct ranges.
3124 * In particular, check for an output dimension in the range
3125 * [first,n_range) for which all maps have a fixed value
3126 * and then check if these values, possibly along with fixed values
3127 * at later dimensions, entail distinct ranges.
3129 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
3130 int first
, int n_range
)
3134 struct isl_fixed_dim_data data
= { NULL
};
3136 ctx
= isl_union_map_get_ctx(umap
);
3138 n
= isl_union_map_n_map(umap
);
3143 isl_union_map_free(umap
);
3144 return isl_bool_true
;
3147 if (first
>= n_range
) {
3148 isl_union_map_free(umap
);
3149 return isl_bool_false
;
3152 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
3156 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
3162 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
3167 dim
= isl_union_map_get_space(umap
);
3168 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
3169 isl_union_map_free(umap
);
3173 free_isl_fixed_map_array(data
.v
, n
);
3174 isl_union_map_free(umap
);
3176 return isl_bool_false
;
3178 free_isl_fixed_map_array(data
.v
, n
);
3179 isl_union_map_free(umap
);
3180 return isl_bool_error
;
3183 /* Check whether the maps in umap that map to subsets of "ran"
3184 * have obviously distinct ranges.
3186 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
3190 isl_union_map
*umap
= user
;
3192 dim
= isl_set_dim(ran
, isl_dim_set
);
3194 return isl_bool_error
;
3196 umap
= isl_union_map_copy(umap
);
3197 umap
= isl_union_map_intersect_range(umap
,
3198 isl_union_set_from_set(isl_set_copy(ran
)));
3199 return plain_injective_on_range(umap
, 0, dim
);
3202 /* Check if the given union_map is obviously injective.
3204 * In particular, we first check if all individual maps are obviously
3205 * injective and then check if all the ranges of these maps are
3206 * obviously disjoint.
3208 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
3211 isl_union_map
*univ
;
3214 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
3216 return isl_bool_error
;
3218 return isl_bool_false
;
3220 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
3221 ran
= isl_union_map_range(univ
);
3223 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
3225 isl_union_set_free(ran
);
3230 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
3234 sv
= isl_union_map_is_single_valued(umap
);
3238 return isl_union_map_is_injective(umap
);
3241 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
3243 struct isl_un_op_drop_user_data data
= { &isl_map_can_zip
};
3244 struct isl_un_op_control control
= {
3245 .filter
= &un_op_filter_drop_user
,
3246 .filter_user
= &data
,
3247 .fn_map
= &isl_map_zip
,
3249 return un_op(umap
, &control
);
3252 /* Given a union map, take the maps of the form A -> (B -> C) and
3253 * return the union of the corresponding maps (A -> B) -> C.
3255 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
3257 struct isl_un_op_drop_user_data data
= { &isl_map_can_uncurry
};
3258 struct isl_un_op_control control
= {
3259 .filter
= &un_op_filter_drop_user
,
3260 .filter_user
= &data
,
3261 .fn_map
= &isl_map_uncurry
,
3263 return un_op(umap
, &control
);
3266 /* Given a union map, take the maps of the form (A -> B) -> C and
3267 * return the union of the corresponding maps A -> (B -> C).
3269 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
3271 struct isl_un_op_drop_user_data data
= { &isl_map_can_curry
};
3272 struct isl_un_op_control control
= {
3273 .filter
= &un_op_filter_drop_user
,
3274 .filter_user
= &data
,
3275 .fn_map
= &isl_map_curry
,
3277 return un_op(umap
, &control
);
3280 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3281 * return the union of the corresponding maps A -> (B -> (C -> D)).
3283 __isl_give isl_union_map
*isl_union_map_range_curry(
3284 __isl_take isl_union_map
*umap
)
3286 struct isl_un_op_drop_user_data data
= { &isl_map_can_range_curry
};
3287 struct isl_un_op_control control
= {
3288 .filter
= &un_op_filter_drop_user
,
3289 .filter_user
= &data
,
3290 .fn_map
= &isl_map_range_curry
,
3292 return un_op(umap
, &control
);
3295 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
3297 struct isl_un_op_control control
= {
3298 .fn_map
= &isl_set_lift
,
3300 return un_op(uset
, &control
);
3303 static isl_stat
coefficients_entry(void **entry
, void *user
)
3305 isl_set
*set
= *entry
;
3306 isl_union_set
**res
= user
;
3308 set
= isl_set_copy(set
);
3309 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
3310 *res
= isl_union_set_add_set(*res
, set
);
3315 __isl_give isl_union_set
*isl_union_set_coefficients(
3316 __isl_take isl_union_set
*uset
)
3325 ctx
= isl_union_set_get_ctx(uset
);
3326 dim
= isl_space_set_alloc(ctx
, 0, 0);
3327 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
3328 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3329 &coefficients_entry
, &res
) < 0)
3332 isl_union_set_free(uset
);
3335 isl_union_set_free(uset
);
3336 isl_union_set_free(res
);
3340 static isl_stat
solutions_entry(void **entry
, void *user
)
3342 isl_set
*set
= *entry
;
3343 isl_union_set
**res
= user
;
3345 set
= isl_set_copy(set
);
3346 set
= isl_set_from_basic_set(isl_set_solutions(set
));
3348 *res
= isl_union_set_from_set(set
);
3350 *res
= isl_union_set_add_set(*res
, set
);
3353 return isl_stat_error
;
3358 __isl_give isl_union_set
*isl_union_set_solutions(
3359 __isl_take isl_union_set
*uset
)
3361 isl_union_set
*res
= NULL
;
3366 if (uset
->table
.n
== 0) {
3367 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
3368 isl_union_set_free(uset
);
3372 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3373 &solutions_entry
, &res
) < 0)
3376 isl_union_set_free(uset
);
3379 isl_union_set_free(uset
);
3380 isl_union_set_free(res
);
3384 /* Is the domain space of "map" equal to "space"?
3386 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3388 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
3389 space
, isl_dim_out
);
3392 /* Is the range space of "map" equal to "space"?
3394 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3396 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3397 space
, isl_dim_out
);
3400 /* Is the set space of "map" equal to "space"?
3402 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3404 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3405 space
, isl_dim_out
);
3408 /* Internal data structure for preimage_pw_multi_aff.
3410 * "pma" is the function under which the preimage should be taken.
3411 * "space" is the space of "pma".
3412 * "res" collects the results.
3413 * "fn" computes the preimage for a given map.
3414 * "match" returns true if "fn" can be called.
3416 struct isl_union_map_preimage_data
{
3418 isl_pw_multi_aff
*pma
;
3420 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3421 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3422 __isl_take isl_pw_multi_aff
*pma
);
3425 /* Call data->fn to compute the preimage of the domain or range of *entry
3426 * under the function represented by data->pma, provided the domain/range
3427 * space of *entry matches the target space of data->pma
3428 * (as given by data->match), and add the result to data->res.
3430 static isl_stat
preimage_entry(void **entry
, void *user
)
3433 isl_map
*map
= *entry
;
3434 struct isl_union_map_preimage_data
*data
= user
;
3437 m
= data
->match(map
, data
->space
);
3439 return isl_stat_error
;
3443 map
= isl_map_copy(map
);
3444 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3446 empty
= isl_map_is_empty(map
);
3447 if (empty
< 0 || empty
) {
3449 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3452 data
->res
= isl_union_map_add_map(data
->res
, map
);
3457 /* Compute the preimage of the domain or range of "umap" under the function
3458 * represented by "pma".
3459 * In other words, plug in "pma" in the domain or range of "umap".
3460 * The function "fn" performs the actual preimage computation on a map,
3461 * while "match" determines to which maps the function should be applied.
3463 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3464 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3465 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3466 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3467 __isl_take isl_pw_multi_aff
*pma
))
3471 struct isl_union_map_preimage_data data
;
3473 umap
= isl_union_map_align_params(umap
,
3474 isl_pw_multi_aff_get_space(pma
));
3475 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3480 ctx
= isl_union_map_get_ctx(umap
);
3481 space
= isl_union_map_get_space(umap
);
3482 data
.space
= isl_pw_multi_aff_get_space(pma
);
3484 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3487 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3489 data
.res
= isl_union_map_free(data
.res
);
3491 isl_space_free(data
.space
);
3492 isl_union_map_free(umap
);
3493 isl_pw_multi_aff_free(pma
);
3496 isl_union_map_free(umap
);
3497 isl_pw_multi_aff_free(pma
);
3501 /* Compute the preimage of the domain of "umap" under the function
3502 * represented by "pma".
3503 * In other words, plug in "pma" in the domain of "umap".
3504 * The result contains maps that live in the same spaces as the maps of "umap"
3505 * with domain space equal to the target space of "pma",
3506 * except that the domain has been replaced by the domain space of "pma".
3508 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3509 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3511 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3512 &isl_map_preimage_domain_pw_multi_aff
);
3515 /* Compute the preimage of the range of "umap" under the function
3516 * represented by "pma".
3517 * In other words, plug in "pma" in the range of "umap".
3518 * The result contains maps that live in the same spaces as the maps of "umap"
3519 * with range space equal to the target space of "pma",
3520 * except that the range has been replaced by the domain space of "pma".
3522 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3523 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3525 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3526 &isl_map_preimage_range_pw_multi_aff
);
3529 /* Compute the preimage of "uset" under the function represented by "pma".
3530 * In other words, plug in "pma" in "uset".
3531 * The result contains sets that live in the same spaces as the sets of "uset"
3532 * with space equal to the target space of "pma",
3533 * except that the space has been replaced by the domain space of "pma".
3535 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3536 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3538 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3539 &isl_set_preimage_pw_multi_aff
);
3542 /* Compute the preimage of the domain of "umap" under the function
3543 * represented by "ma".
3544 * In other words, plug in "ma" in the domain of "umap".
3545 * The result contains maps that live in the same spaces as the maps of "umap"
3546 * with domain space equal to the target space of "ma",
3547 * except that the domain has been replaced by the domain space of "ma".
3549 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3550 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3552 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3553 isl_pw_multi_aff_from_multi_aff(ma
));
3556 /* Compute the preimage of the range of "umap" under the function
3557 * represented by "ma".
3558 * In other words, plug in "ma" in the range of "umap".
3559 * The result contains maps that live in the same spaces as the maps of "umap"
3560 * with range space equal to the target space of "ma",
3561 * except that the range has been replaced by the domain space of "ma".
3563 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3564 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3566 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3567 isl_pw_multi_aff_from_multi_aff(ma
));
3570 /* Compute the preimage of "uset" under the function represented by "ma".
3571 * In other words, plug in "ma" in "uset".
3572 * The result contains sets that live in the same spaces as the sets of "uset"
3573 * with space equal to the target space of "ma",
3574 * except that the space has been replaced by the domain space of "ma".
3576 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3577 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3579 return isl_union_set_preimage_pw_multi_aff(uset
,
3580 isl_pw_multi_aff_from_multi_aff(ma
));
3583 /* Internal data structure for preimage_multi_pw_aff.
3585 * "mpa" is the function under which the preimage should be taken.
3586 * "space" is the space of "mpa".
3587 * "res" collects the results.
3588 * "fn" computes the preimage for a given map.
3589 * "match" returns true if "fn" can be called.
3591 struct isl_union_map_preimage_mpa_data
{
3593 isl_multi_pw_aff
*mpa
;
3595 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3596 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3597 __isl_take isl_multi_pw_aff
*mpa
);
3600 /* Call data->fn to compute the preimage of the domain or range of *entry
3601 * under the function represented by data->mpa, provided the domain/range
3602 * space of *entry matches the target space of data->mpa
3603 * (as given by data->match), and add the result to data->res.
3605 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3608 isl_map
*map
= *entry
;
3609 struct isl_union_map_preimage_mpa_data
*data
= user
;
3612 m
= data
->match(map
, data
->space
);
3614 return isl_stat_error
;
3618 map
= isl_map_copy(map
);
3619 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3621 empty
= isl_map_is_empty(map
);
3622 if (empty
< 0 || empty
) {
3624 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3627 data
->res
= isl_union_map_add_map(data
->res
, map
);
3632 /* Compute the preimage of the domain or range of "umap" under the function
3633 * represented by "mpa".
3634 * In other words, plug in "mpa" in the domain or range of "umap".
3635 * The function "fn" performs the actual preimage computation on a map,
3636 * while "match" determines to which maps the function should be applied.
3638 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3639 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3640 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3641 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3642 __isl_take isl_multi_pw_aff
*mpa
))
3646 struct isl_union_map_preimage_mpa_data data
;
3648 umap
= isl_union_map_align_params(umap
,
3649 isl_multi_pw_aff_get_space(mpa
));
3650 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3655 ctx
= isl_union_map_get_ctx(umap
);
3656 space
= isl_union_map_get_space(umap
);
3657 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3659 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3662 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3664 data
.res
= isl_union_map_free(data
.res
);
3666 isl_space_free(data
.space
);
3667 isl_union_map_free(umap
);
3668 isl_multi_pw_aff_free(mpa
);
3671 isl_union_map_free(umap
);
3672 isl_multi_pw_aff_free(mpa
);
3676 /* Compute the preimage of the domain of "umap" under the function
3677 * represented by "mpa".
3678 * In other words, plug in "mpa" in the domain of "umap".
3679 * The result contains maps that live in the same spaces as the maps of "umap"
3680 * with domain space equal to the target space of "mpa",
3681 * except that the domain has been replaced by the domain space of "mpa".
3683 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3684 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3686 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3687 &isl_map_preimage_domain_multi_pw_aff
);
3690 /* Internal data structure for preimage_upma.
3692 * "umap" is the map of which the preimage should be computed.
3693 * "res" collects the results.
3694 * "fn" computes the preimage for a given piecewise multi-affine function.
3696 struct isl_union_map_preimage_upma_data
{
3697 isl_union_map
*umap
;
3699 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3700 __isl_take isl_pw_multi_aff
*pma
);
3703 /* Call data->fn to compute the preimage of the domain or range of data->umap
3704 * under the function represented by pma and add the result to data->res.
3706 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3708 struct isl_union_map_preimage_upma_data
*data
= user
;
3709 isl_union_map
*umap
;
3711 umap
= isl_union_map_copy(data
->umap
);
3712 umap
= data
->fn(umap
, pma
);
3713 data
->res
= isl_union_map_union(data
->res
, umap
);
3715 return data
->res
? isl_stat_ok
: isl_stat_error
;
3718 /* Compute the preimage of the domain or range of "umap" under the function
3719 * represented by "upma".
3720 * In other words, plug in "upma" in the domain or range of "umap".
3721 * The function "fn" performs the actual preimage computation
3722 * on a piecewise multi-affine function.
3724 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3725 __isl_take isl_union_map
*umap
,
3726 __isl_take isl_union_pw_multi_aff
*upma
,
3727 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3728 __isl_take isl_pw_multi_aff
*pma
))
3730 struct isl_union_map_preimage_upma_data data
;
3733 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3735 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3736 &preimage_upma
, &data
) < 0)
3737 data
.res
= isl_union_map_free(data
.res
);
3739 isl_union_map_free(umap
);
3740 isl_union_pw_multi_aff_free(upma
);
3745 /* Compute the preimage of the domain of "umap" under the function
3746 * represented by "upma".
3747 * In other words, plug in "upma" in the domain of "umap".
3748 * The result contains maps that live in the same spaces as the maps of "umap"
3749 * with domain space equal to one of the target spaces of "upma",
3750 * except that the domain has been replaced by one of the domain spaces that
3751 * correspond to that target space of "upma".
3753 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3754 __isl_take isl_union_map
*umap
,
3755 __isl_take isl_union_pw_multi_aff
*upma
)
3757 return preimage_union_pw_multi_aff(umap
, upma
,
3758 &isl_union_map_preimage_domain_pw_multi_aff
);
3761 /* Compute the preimage of the range of "umap" under the function
3762 * represented by "upma".
3763 * In other words, plug in "upma" in the range of "umap".
3764 * The result contains maps that live in the same spaces as the maps of "umap"
3765 * with range space equal to one of the target spaces of "upma",
3766 * except that the range has been replaced by one of the domain spaces that
3767 * correspond to that target space of "upma".
3769 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3770 __isl_take isl_union_map
*umap
,
3771 __isl_take isl_union_pw_multi_aff
*upma
)
3773 return preimage_union_pw_multi_aff(umap
, upma
,
3774 &isl_union_map_preimage_range_pw_multi_aff
);
3777 /* Compute the preimage of "uset" under the function represented by "upma".
3778 * In other words, plug in "upma" in the range of "uset".
3779 * The result contains sets that live in the same spaces as the sets of "uset"
3780 * with space equal to one of the target spaces of "upma",
3781 * except that the space has been replaced by one of the domain spaces that
3782 * correspond to that target space of "upma".
3784 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3785 __isl_take isl_union_set
*uset
,
3786 __isl_take isl_union_pw_multi_aff
*upma
)
3788 return preimage_union_pw_multi_aff(uset
, upma
,
3789 &isl_union_set_preimage_pw_multi_aff
);
3792 /* Reset the user pointer on all identifiers of parameters and tuples
3793 * of the spaces of "umap".
3795 __isl_give isl_union_map
*isl_union_map_reset_user(
3796 __isl_take isl_union_map
*umap
)
3798 umap
= isl_union_map_cow(umap
);
3801 umap
->dim
= isl_space_reset_user(umap
->dim
);
3803 return isl_union_map_free(umap
);
3804 return total(umap
, &isl_map_reset_user
);
3807 /* Reset the user pointer on all identifiers of parameters and tuples
3808 * of the spaces of "uset".
3810 __isl_give isl_union_set
*isl_union_set_reset_user(
3811 __isl_take isl_union_set
*uset
)
3813 return isl_union_map_reset_user(uset
);
3816 /* Remove all existentially quantified variables and integer divisions
3817 * from "umap" using Fourier-Motzkin elimination.
3819 __isl_give isl_union_map
*isl_union_map_remove_divs(
3820 __isl_take isl_union_map
*umap
)
3822 return total(umap
, &isl_map_remove_divs
);
3825 /* Remove all existentially quantified variables and integer divisions
3826 * from "uset" using Fourier-Motzkin elimination.
3828 __isl_give isl_union_set
*isl_union_set_remove_divs(
3829 __isl_take isl_union_set
*uset
)
3831 return isl_union_map_remove_divs(uset
);
3834 /* Internal data structure for isl_union_map_project_out.
3835 * "type", "first" and "n" are the arguments for the isl_map_project_out
3837 * "res" collects the results.
3839 struct isl_union_map_project_out_data
{
3840 enum isl_dim_type type
;
3847 /* Turn the data->n dimensions of type data->type, starting at data->first
3848 * into existentially quantified variables and add the result to data->res.
3850 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3852 struct isl_union_map_project_out_data
*data
= user
;
3854 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3855 data
->res
= isl_union_map_add_map(data
->res
, map
);
3860 /* Turn the "n" dimensions of type "type", starting at "first"
3861 * into existentially quantified variables.
3862 * Since the space of an isl_union_map only contains parameters,
3863 * type is required to be equal to isl_dim_param.
3865 __isl_give isl_union_map
*isl_union_map_project_out(
3866 __isl_take isl_union_map
*umap
,
3867 enum isl_dim_type type
, unsigned first
, unsigned n
)
3870 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3875 if (type
!= isl_dim_param
)
3876 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3877 "can only project out parameters",
3878 return isl_union_map_free(umap
));
3880 space
= isl_union_map_get_space(umap
);
3881 space
= isl_space_drop_dims(space
, type
, first
, n
);
3882 data
.res
= isl_union_map_empty(space
);
3883 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3884 data
.res
= isl_union_map_free(data
.res
);
3886 isl_union_map_free(umap
);
3892 #define TYPE isl_union_map
3893 #include "isl_project_out_all_params_templ.c"
3895 /* Turn the "n" dimensions of type "type", starting at "first"
3896 * into existentially quantified variables.
3897 * Since the space of an isl_union_set only contains parameters,
3898 * "type" is required to be equal to isl_dim_param.
3900 __isl_give isl_union_set
*isl_union_set_project_out(
3901 __isl_take isl_union_set
*uset
,
3902 enum isl_dim_type type
, unsigned first
, unsigned n
)
3904 return isl_union_map_project_out(uset
, type
, first
, n
);
3907 /* Project out all parameters from "uset" by existentially quantifying
3910 __isl_give isl_union_set
*isl_union_set_project_out_all_params(
3911 __isl_take isl_union_set
*uset
)
3913 return uset_from_umap(
3914 isl_union_map_project_out_all_params(uset_to_umap(uset
)));
3917 /* Internal data structure for isl_union_map_involves_dims.
3918 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3920 struct isl_union_map_involves_dims_data
{
3925 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3927 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3929 struct isl_union_map_involves_dims_data
*data
= user
;
3932 involves
= isl_map_involves_dims(map
,
3933 isl_dim_param
, data
->first
, data
->n
);
3934 return isl_bool_not(involves
);
3937 /* Does "umap" involve any of the n parameters starting at first?
3938 * "type" is required to be set to isl_dim_param.
3940 * "umap" involves any of those parameters if any of its maps
3941 * involve the parameters. In other words, "umap" does not
3942 * involve any of the parameters if all its maps to not
3943 * involve the parameters.
3945 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3946 enum isl_dim_type type
, unsigned first
, unsigned n
)
3948 struct isl_union_map_involves_dims_data data
= { first
, n
};
3951 if (type
!= isl_dim_param
)
3952 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3953 "can only reference parameters", return isl_bool_error
);
3955 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3957 return isl_bool_not(excludes
);
3960 /* Internal data structure for isl_union_map_reset_range_space.
3961 * "range" is the space from which to set the range space.
3962 * "res" collects the results.
3964 struct isl_union_map_reset_range_space_data
{
3969 /* Replace the range space of "map" by the range space of data->range and
3970 * add the result to data->res.
3972 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3974 struct isl_union_map_reset_range_space_data
*data
= user
;
3977 space
= isl_map_get_space(map
);
3978 space
= isl_space_domain(space
);
3979 space
= isl_space_extend_domain_with_range(space
,
3980 isl_space_copy(data
->range
));
3981 map
= isl_map_reset_space(map
, space
);
3982 data
->res
= isl_union_map_add_map(data
->res
, map
);
3984 return data
->res
? isl_stat_ok
: isl_stat_error
;
3987 /* Replace the range space of all the maps in "umap" by
3988 * the range space of "space".
3990 * This assumes that all maps have the same output dimension.
3991 * This function should therefore not be made publicly available.
3993 * Since the spaces of the maps change, so do their hash value.
3994 * We therefore need to create a new isl_union_map.
3996 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3997 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3999 struct isl_union_map_reset_range_space_data data
= { space
};
4001 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
4002 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
4003 data
.res
= isl_union_map_free(data
.res
);
4005 isl_space_free(space
);
4006 isl_union_map_free(umap
);
4010 /* Check that "umap" and "space" have the same number of parameters.
4012 static isl_stat
check_union_map_space_equal_dim(__isl_keep isl_union_map
*umap
,
4013 __isl_keep isl_space
*space
)
4015 isl_size dim1
, dim2
;
4017 dim1
= isl_union_map_dim(umap
, isl_dim_param
);
4018 dim2
= isl_space_dim(space
, isl_dim_param
);
4019 if (dim1
< 0 || dim2
< 0)
4020 return isl_stat_error
;
4023 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
4024 "number of parameters does not match", return isl_stat_error
);
4027 /* Internal data structure for isl_union_map_reset_equal_dim_space.
4028 * "space" is the target space.
4029 * "res" collects the results.
4031 struct isl_union_map_reset_params_data
{
4036 /* Replace the parameters of "map" by those of data->space and
4037 * add the result to data->res.
4039 static isl_stat
reset_params(__isl_take isl_map
*map
, void *user
)
4041 struct isl_union_map_reset_params_data
*data
= user
;
4044 space
= isl_map_get_space(map
);
4045 space
= isl_space_replace_params(space
, data
->space
);
4046 map
= isl_map_reset_equal_dim_space(map
, space
);
4047 data
->res
= isl_union_map_add_map(data
->res
, map
);
4049 return data
->res
? isl_stat_ok
: isl_stat_error
;
4052 /* Replace the space of "umap" by "space", without modifying
4053 * the dimension of "umap", i.e., the number of parameters of "umap".
4055 * Since the hash values of the maps in the union map depend
4056 * on the parameters, a new union map needs to be constructed.
4058 __isl_give isl_union_map
*isl_union_map_reset_equal_dim_space(
4059 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
4061 struct isl_union_map_reset_params_data data
= { space
};
4063 isl_space
*umap_space
;
4065 umap_space
= isl_union_map_peek_space(umap
);
4066 equal
= isl_space_is_equal(umap_space
, space
);
4070 isl_space_free(space
);
4073 if (check_union_map_space_equal_dim(umap
, space
) < 0)
4076 data
.res
= isl_union_map_empty(isl_space_copy(space
));
4077 if (isl_union_map_foreach_map(umap
, &reset_params
, &data
) < 0)
4078 data
.res
= isl_union_map_free(data
.res
);
4080 isl_space_free(space
);
4081 isl_union_map_free(umap
);
4084 isl_union_map_free(umap
);
4085 isl_space_free(space
);
4089 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4090 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4091 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4092 * to the domain and the range of each map.
4093 * "res" collects the results.
4095 struct isl_union_order_at_data
{
4096 isl_multi_union_pw_aff
*mupa
;
4097 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
4098 __isl_take isl_multi_pw_aff
*mpa2
);
4102 /* Intersect "map" with the result of applying data->order to
4103 * the functions in data->mupa that apply to the domain and the range
4104 * of "map" and add the result to data->res.
4106 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
4108 struct isl_union_order_at_data
*data
= user
;
4110 isl_multi_pw_aff
*mpa1
, *mpa2
;
4113 space
= isl_space_domain(isl_map_get_space(map
));
4114 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
4115 space
= isl_space_range(isl_map_get_space(map
));
4116 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
4117 order
= data
->order(mpa1
, mpa2
);
4118 map
= isl_map_intersect(map
, order
);
4119 data
->res
= isl_union_map_add_map(data
->res
, map
);
4121 return data
->res
? isl_stat_ok
: isl_stat_error
;
4124 /* If "mupa" has a non-trivial explicit domain, then intersect
4125 * domain and range of "umap" with this explicit domain.
4126 * If the explicit domain only describes constraints on the parameters,
4127 * then the intersection only needs to be performed once.
4129 static __isl_give isl_union_map
*intersect_explicit_domain(
4130 __isl_take isl_union_map
*umap
, __isl_keep isl_multi_union_pw_aff
*mupa
)
4132 isl_bool non_trivial
, is_params
;
4135 non_trivial
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa
);
4136 if (non_trivial
< 0)
4137 return isl_union_map_free(umap
);
4140 mupa
= isl_multi_union_pw_aff_copy(mupa
);
4141 dom
= isl_multi_union_pw_aff_domain(mupa
);
4142 is_params
= isl_union_set_is_params(dom
);
4143 if (is_params
< 0) {
4144 isl_union_set_free(dom
);
4145 return isl_union_map_free(umap
);
4150 set
= isl_union_set_params(dom
);
4151 umap
= isl_union_map_intersect_params(umap
, set
);
4154 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(dom
));
4155 umap
= isl_union_map_intersect_range(umap
, dom
);
4159 /* Intersect each map in "umap" with the result of calling "order"
4160 * on the functions is "mupa" that apply to the domain and the range
4163 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
4164 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
4165 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
4166 __isl_take isl_multi_pw_aff
*mpa2
))
4168 struct isl_union_order_at_data data
;
4170 umap
= isl_union_map_align_params(umap
,
4171 isl_multi_union_pw_aff_get_space(mupa
));
4172 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
4173 isl_union_map_get_space(umap
));
4174 umap
= intersect_explicit_domain(umap
, mupa
);
4177 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
4178 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
4179 data
.res
= isl_union_map_free(data
.res
);
4181 isl_multi_union_pw_aff_free(mupa
);
4182 isl_union_map_free(umap
);
4186 /* Return the subset of "umap" where the domain and the range
4187 * have equal "mupa" values.
4189 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
4190 __isl_take isl_union_map
*umap
,
4191 __isl_take isl_multi_union_pw_aff
*mupa
)
4193 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4194 &isl_multi_pw_aff_eq_map
);
4197 /* Return the subset of "umap" where the domain has a lexicographically
4198 * smaller "mupa" value than the range.
4200 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
4201 __isl_take isl_union_map
*umap
,
4202 __isl_take isl_multi_union_pw_aff
*mupa
)
4204 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4205 &isl_multi_pw_aff_lex_lt_map
);
4208 /* Return the subset of "umap" where the domain has a lexicographically
4209 * greater "mupa" value than the range.
4211 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
4212 __isl_take isl_union_map
*umap
,
4213 __isl_take isl_multi_union_pw_aff
*mupa
)
4215 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4216 &isl_multi_pw_aff_lex_gt_map
);
4219 /* Return the union of the elements in the list "list".
4221 __isl_give isl_union_set
*isl_union_set_list_union(
4222 __isl_take isl_union_set_list
*list
)
4230 n
= isl_union_set_list_n_union_set(list
);
4234 ctx
= isl_union_set_list_get_ctx(list
);
4235 space
= isl_space_params_alloc(ctx
, 0);
4236 res
= isl_union_set_empty(space
);
4238 for (i
= 0; i
< n
; ++i
) {
4239 isl_union_set
*uset_i
;
4241 uset_i
= isl_union_set_list_get_union_set(list
, i
);
4242 res
= isl_union_set_union(res
, uset_i
);
4245 isl_union_set_list_free(list
);
4248 isl_union_set_list_free(list
);
4252 /* Update *hash with the hash value of "map".
4254 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
4256 uint32_t *hash
= user
;
4259 map_hash
= isl_map_get_hash(map
);
4260 isl_hash_hash(*hash
, map_hash
);
4266 /* Return a hash value that digests "umap".
4268 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
4275 hash
= isl_hash_init();
4276 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
4282 /* Return a hash value that digests "uset".
4284 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
4286 return isl_union_map_get_hash(uset
);
4289 /* Add the number of basic sets in "set" to "n".
4291 static isl_stat
add_n(__isl_take isl_set
*set
, void *user
)
4296 set_n
= isl_set_n_basic_set(set
);
4300 return set_n
< 0 ? isl_stat_error
: isl_stat_ok
;
4303 /* Return the total number of basic sets in "uset".
4305 int isl_union_set_n_basic_set(__isl_keep isl_union_set
*uset
)
4309 if (isl_union_set_foreach_set(uset
, &add_n
, &n
) < 0)
4315 /* Add the basic sets in "set" to "list".
4317 static isl_stat
add_list(__isl_take isl_set
*set
, void *user
)
4319 isl_basic_set_list
**list
= user
;
4320 isl_basic_set_list
*list_i
;
4322 list_i
= isl_set_get_basic_set_list(set
);
4323 *list
= isl_basic_set_list_concat(*list
, list_i
);
4327 return isl_stat_error
;
4331 /* Return a list containing all the basic sets in "uset".
4333 * First construct a list of the appropriate size and
4334 * then add all the elements.
4336 __isl_give isl_basic_set_list
*isl_union_set_get_basic_set_list(
4337 __isl_keep isl_union_set
*uset
)
4341 isl_basic_set_list
*list
;
4345 ctx
= isl_union_set_get_ctx(uset
);
4346 n
= isl_union_set_n_basic_set(uset
);
4349 list
= isl_basic_set_list_alloc(ctx
, n
);
4350 if (isl_union_set_foreach_set(uset
, &add_list
, &list
) < 0)
4351 list
= isl_basic_set_list_free(list
);
4356 /* Internal data structure for isl_union_map_remove_map_if.
4357 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4359 struct isl_union_map_remove_map_if_data
{
4360 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
4364 /* isl_un_op_control filter that negates the result of data->fn
4367 static isl_bool
not(__isl_keep isl_map
*map
, void *user
)
4369 struct isl_union_map_remove_map_if_data
*data
= user
;
4371 return isl_bool_not(data
->fn(map
, data
->user
));
4374 /* Dummy isl_un_op_control transformation callback that
4375 * simply returns the input.
4377 static __isl_give isl_map
*map_id(__isl_take isl_map
*map
)
4382 /* Call "fn" on every map in "umap" and remove those maps
4383 * for which the callback returns true.
4385 * Use un_op to keep only those maps that are not filtered out,
4386 * applying an identity transformation on them.
4388 __isl_give isl_union_map
*isl_union_map_remove_map_if(
4389 __isl_take isl_union_map
*umap
,
4390 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
4392 struct isl_union_map_remove_map_if_data data
= { fn
, user
};
4393 struct isl_un_op_control control
= {
4395 .filter_user
= &data
,
4398 return un_op(umap
, &control
);
4401 /* Does "map" have "space" as range (ignoring parameters)?
4403 static isl_bool
has_range_space(__isl_keep isl_map
*map
, void *user
)
4405 isl_space
*space
= user
;
4407 return isl_space_has_range_tuples(space
, isl_map_peek_space(map
));
4410 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4412 static __isl_give isl_map
*bind_range(__isl_take isl_map
*map
, void *user
)
4414 isl_multi_id
*tuple
= user
;
4416 return isl_map_bind_range(map
, isl_multi_id_copy(tuple
));
4419 /* Bind the output dimensions of "umap" to parameters with identifiers
4420 * specified by "tuple", living in the range space of "umap",
4421 * for those maps that have a matching range space.
4423 __isl_give isl_union_set
*isl_union_map_bind_range(
4424 __isl_take isl_union_map
*umap
, __isl_take isl_multi_id
*tuple
)
4426 struct isl_un_op_control control
= {
4427 .filter
= &has_range_space
,
4428 .filter_user
= isl_multi_id_peek_space(tuple
),
4429 .fn_map2
= &bind_range
,
4430 .fn_map2_user
= tuple
,
4432 isl_union_set
*bound
;
4434 bound
= uset_from_umap(un_op(umap
, &control
));
4435 isl_multi_id_free(tuple
);