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>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
30 #include <uset_to_umap.c>
31 #include <uset_from_umap.c>
32 #include <set_list_from_map_list_inl.c>
34 /* Return the number of parameters of "umap", where "type"
35 * is required to be set to isl_dim_param.
37 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
38 enum isl_dim_type type
)
43 if (type
!= isl_dim_param
)
44 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
45 "can only reference parameters", return 0);
47 return isl_space_dim(umap
->dim
, type
);
50 /* Return the number of parameters of "uset", where "type"
51 * is required to be set to isl_dim_param.
53 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
54 enum isl_dim_type type
)
56 return isl_union_map_dim(uset
, type
);
59 /* Return the id of the specified dimension.
61 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
62 enum isl_dim_type type
, unsigned pos
)
67 if (type
!= isl_dim_param
)
68 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
69 "can only reference parameters", return NULL
);
71 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
74 /* Is this union set a parameter domain?
76 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
82 return isl_bool_error
;
83 if (uset
->table
.n
!= 1)
84 return isl_bool_false
;
86 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
87 params
= isl_set_is_params(set
);
92 /* Is this union map actually a parameter domain?
93 * Users should never call this function. Outside of isl,
94 * a union map can never be a parameter domain.
96 isl_bool
isl_union_map_is_params(__isl_keep isl_union_map
*umap
)
98 return isl_union_set_is_params(uset_from_umap(umap
));
101 static __isl_give isl_union_map
*isl_union_map_alloc(
102 __isl_take isl_space
*space
, int size
)
106 space
= isl_space_params(space
);
110 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
112 isl_space_free(space
);
118 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
119 return isl_union_map_free(umap
);
124 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*space
)
126 return isl_union_map_alloc(space
, 16);
129 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*space
)
131 return isl_union_map_empty(space
);
134 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
136 return umap
? umap
->dim
->ctx
: NULL
;
139 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
141 return uset
? uset
->dim
->ctx
: NULL
;
144 /* Return the space of "umap".
146 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
148 return umap
? umap
->dim
: NULL
;
151 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
153 return isl_space_copy(isl_union_map_peek_space(umap
));
156 /* Return the position of the parameter with the given name
158 * Return -1 if no such dimension can be found.
160 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
161 enum isl_dim_type type
, const char *name
)
165 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
168 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
170 return isl_union_map_get_space(uset
);
173 static isl_stat
free_umap_entry(void **entry
, void *user
)
175 isl_map
*map
= *entry
;
180 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
182 isl_union_map
**umap
= (isl_union_map
**)user
;
184 *umap
= isl_union_map_add_map(*umap
, map
);
189 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
196 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
197 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
201 isl_union_map_free(dup
);
205 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
213 return isl_union_map_dup(umap
);
216 struct isl_union_align
{
221 static isl_stat
align_entry(void **entry
, void *user
)
223 isl_map
*map
= *entry
;
225 struct isl_union_align
*data
= user
;
227 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
228 isl_map_get_space(map
));
230 data
->res
= isl_union_map_add_map(data
->res
,
231 isl_map_realign(isl_map_copy(map
), exp
));
236 /* Align the parameters of umap along those of model.
237 * The result has the parameters of model first, in the same order
238 * as they appear in model, followed by any remaining parameters of
239 * umap that do not appear in model.
241 __isl_give isl_union_map
*isl_union_map_align_params(
242 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
244 struct isl_union_align data
= { NULL
, NULL
};
245 isl_bool equal_params
;
250 equal_params
= isl_space_has_equal_params(umap
->dim
, model
);
251 if (equal_params
< 0)
254 isl_space_free(model
);
258 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
262 data
.res
= isl_union_map_alloc(isl_reordering_get_space(data
.exp
),
264 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
265 &align_entry
, &data
) < 0)
268 isl_reordering_free(data
.exp
);
269 isl_union_map_free(umap
);
270 isl_space_free(model
);
273 isl_reordering_free(data
.exp
);
274 isl_union_map_free(umap
);
275 isl_union_map_free(data
.res
);
276 isl_space_free(model
);
280 __isl_give isl_union_set
*isl_union_set_align_params(
281 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
283 return isl_union_map_align_params(uset
, model
);
286 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
287 __isl_take isl_union_map
*umap2
)
289 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
290 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
292 umap1
= isl_union_map_cow(umap1
);
294 if (!umap1
|| !umap2
)
297 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
300 isl_union_map_free(umap2
);
304 isl_union_map_free(umap1
);
305 isl_union_map_free(umap2
);
309 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
310 __isl_take isl_union_set
*uset2
)
312 return isl_union_map_union(uset1
, uset2
);
315 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
324 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
326 return isl_union_map_copy(uset
);
329 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
337 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
338 &free_umap_entry
, NULL
);
339 isl_hash_table_clear(&umap
->table
);
340 isl_space_free(umap
->dim
);
345 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
347 return isl_union_map_free(uset
);
350 /* Do "umap" and "space" have the same parameters?
352 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
353 __isl_keep isl_space
*space
)
355 isl_space
*umap_space
;
357 umap_space
= isl_union_map_peek_space(umap
);
358 return isl_space_has_equal_params(umap_space
, space
);
361 /* Do "uset" and "space" have the same parameters?
363 isl_bool
isl_union_set_space_has_equal_params(__isl_keep isl_union_set
*uset
,
364 __isl_keep isl_space
*space
)
366 return isl_union_map_space_has_equal_params(uset_to_umap(uset
), space
);
369 static int has_space(const void *entry
, const void *val
)
371 isl_map
*map
= (isl_map
*)entry
;
372 isl_space
*space
= (isl_space
*) val
;
374 return isl_space_is_equal(map
->dim
, space
);
377 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
378 __isl_take isl_map
*map
)
381 struct isl_hash_table_entry
*entry
;
387 if (isl_map_plain_is_empty(map
)) {
392 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
396 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
397 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
400 umap
= isl_union_map_cow(umap
);
405 hash
= isl_space_get_hash(map
->dim
);
406 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
407 &has_space
, map
->dim
, 1);
414 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
423 isl_union_map_free(umap
);
427 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
428 __isl_take isl_set
*set
)
430 return isl_union_map_add_map(uset
, set_to_map(set
));
433 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
441 dim
= isl_map_get_space(map
);
442 dim
= isl_space_params(dim
);
443 umap
= isl_union_map_empty(dim
);
444 umap
= isl_union_map_add_map(umap
, map
);
449 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
451 return isl_union_map_from_map(set_to_map(set
));
454 __isl_give isl_union_map
*isl_union_map_from_basic_map(
455 __isl_take isl_basic_map
*bmap
)
457 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
460 __isl_give isl_union_set
*isl_union_set_from_basic_set(
461 __isl_take isl_basic_set
*bset
)
463 return isl_union_map_from_basic_map(bset
);
466 struct isl_union_map_foreach_data
468 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
472 static isl_stat
call_on_copy(void **entry
, void *user
)
474 isl_map
*map
= *entry
;
475 struct isl_union_map_foreach_data
*data
;
476 data
= (struct isl_union_map_foreach_data
*)user
;
478 return data
->fn(isl_map_copy(map
), data
->user
);
481 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
483 return umap
? umap
->table
.n
: 0;
486 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
488 return uset
? uset
->table
.n
: 0;
491 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
492 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
494 struct isl_union_map_foreach_data data
= { fn
, user
};
497 return isl_stat_error
;
499 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
500 &call_on_copy
, &data
);
503 /* Internal data structure for isl_union_map_every_map.
505 * "test" is the user-specified callback function.
506 * "user" is the user-specified callback function argument.
508 * "failed" is initialized to 0 and set to 1 if "test" fails
511 struct isl_union_map_every_data
{
512 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
);
517 /* Call data->test on "map".
518 * If this fails, then set data->failed and abort.
520 static isl_stat
call_every(void **entry
, void *user
)
522 isl_map
*map
= *entry
;
523 struct isl_union_map_every_data
*data
= user
;
526 r
= data
->test(map
, data
->user
);
528 return isl_stat_error
;
532 return isl_stat_error
;
535 /* Does "test" succeed on every map in "umap"?
537 isl_bool
isl_union_map_every_map(__isl_keep isl_union_map
*umap
,
538 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
), void *user
)
540 struct isl_union_map_every_data data
= { test
, user
, 0 };
544 return isl_bool_error
;
546 r
= isl_hash_table_foreach(isl_union_map_get_ctx(umap
), &umap
->table
,
549 return isl_bool_true
;
551 return isl_bool_false
;
552 return isl_bool_error
;
555 /* Add "map" to "list".
557 static isl_stat
add_list_map(__isl_take isl_map
*map
, void *user
)
559 isl_map_list
**list
= user
;
561 *list
= isl_map_list_add(*list
, map
);
564 return isl_stat_error
;
568 /* Return the maps in "umap" as a list.
570 * First construct a list of the appropriate size and then add all the
573 __isl_give isl_map_list
*isl_union_map_get_map_list(
574 __isl_keep isl_union_map
*umap
)
582 ctx
= isl_union_map_get_ctx(umap
);
583 n_maps
= isl_union_map_n_map(umap
);
584 list
= isl_map_list_alloc(ctx
, n_maps
);
586 if (isl_union_map_foreach_map(umap
, &add_list_map
, &list
) < 0)
587 list
= isl_map_list_free(list
);
592 /* Return the sets in "uset" as a list.
594 __isl_give isl_set_list
*isl_union_set_get_set_list(
595 __isl_keep isl_union_set
*uset
)
597 return set_list_from_map_list(
598 isl_union_map_get_map_list(uset_to_umap(uset
)));
601 static isl_stat
copy_map(void **entry
, void *user
)
603 isl_map
*map
= *entry
;
604 isl_map
**map_p
= user
;
606 *map_p
= isl_map_copy(map
);
608 return isl_stat_error
;
611 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
618 ctx
= isl_union_map_get_ctx(umap
);
619 if (umap
->table
.n
!= 1)
620 isl_die(ctx
, isl_error_invalid
,
621 "union map needs to contain elements in exactly "
622 "one space", goto error
);
624 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
626 isl_union_map_free(umap
);
630 isl_union_map_free(umap
);
634 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
636 return isl_map_from_union_map(uset
);
639 /* Extract the map in "umap" that lives in the given space (ignoring
642 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
643 __isl_take isl_space
*space
)
646 struct isl_hash_table_entry
*entry
;
648 space
= isl_space_drop_dims(space
, isl_dim_param
,
649 0, isl_space_dim(space
, isl_dim_param
));
650 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
654 hash
= isl_space_get_hash(space
);
655 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
656 &has_space
, space
, 0);
658 return isl_map_empty(space
);
659 isl_space_free(space
);
660 return isl_map_copy(entry
->data
);
662 isl_space_free(space
);
666 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
667 __isl_take isl_space
*dim
)
669 return set_from_map(isl_union_map_extract_map(uset
, dim
));
672 /* Check if umap contains a map in the given space.
674 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
675 __isl_keep isl_space
*space
)
678 struct isl_hash_table_entry
*entry
;
681 return isl_bool_error
;
683 hash
= isl_space_get_hash(space
);
684 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
685 &has_space
, space
, 0);
689 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
690 __isl_keep isl_space
*space
)
692 return isl_union_map_contains(uset
, space
);
695 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
696 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
698 return isl_union_map_foreach_map(uset
,
699 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
702 struct isl_union_set_foreach_point_data
{
703 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
707 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
709 struct isl_union_set_foreach_point_data
*data
= user
;
712 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
718 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
719 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
721 struct isl_union_set_foreach_point_data data
= { fn
, user
};
722 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
725 /* Data structure that specifies how gen_bin_op should
726 * construct results from the inputs.
728 * If "subtract" is set, then a map in the first input is copied to the result
729 * if there is no corresponding map in the second input.
730 * Otherwise, a map in the first input with no corresponding map
731 * in the second input is ignored.
732 * If "filter" is not NULL, then it specifies which maps in the first
733 * input may have a matching map in the second input.
734 * In particular, it makes sure that "match_space" can be called
735 * on the space of the map.
736 * "match_space" specifies how to transform the space of a map
737 * in the first input to the space of the corresponding map
738 * in the second input.
739 * "fn_map" specifies how the matching maps, one from each input,
740 * should be combined to form a map in the result.
742 struct isl_bin_op_control
{
744 isl_bool (*filter
)(__isl_keep isl_map
*map
);
745 __isl_give isl_space
*(*match_space
)(__isl_take isl_space
*space
);
746 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map1
,
747 __isl_take isl_map
*map2
);
750 /* Internal data structure for gen_bin_op.
751 * "control" specifies how the maps in the result should be constructed.
752 * "umap2" is a pointer to the second argument.
753 * "res" collects the results.
755 struct isl_union_map_gen_bin_data
{
756 struct isl_bin_op_control
*control
;
757 isl_union_map
*umap2
;
761 /* Add a copy of "map" to "res" and return the result.
763 static __isl_give isl_union_map
*bin_add_map(__isl_take isl_union_map
*res
,
764 __isl_keep isl_map
*map
)
766 return isl_union_map_add_map(res
, isl_map_copy(map
));
769 /* Combine "map1" and "map2", add the result to "res" and return the result.
770 * Check whether the result is empty before adding it to "res".
772 static __isl_give isl_union_map
*bin_add_pair(__isl_take isl_union_map
*res
,
773 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
774 struct isl_union_map_gen_bin_data
*data
)
779 map
= data
->control
->fn_map(isl_map_copy(map1
), isl_map_copy(map2
));
780 empty
= isl_map_is_empty(map
);
781 if (empty
< 0 || empty
) {
784 return isl_union_map_free(res
);
787 return isl_union_map_add_map(res
, map
);
790 /* Dummy match_space function that simply returns the input space.
792 static __isl_give isl_space
*identity(__isl_take isl_space
*space
)
797 /* Look for the map in data->umap2 that corresponds to "map", if any.
798 * Return (isl_bool_true, matching map) if there is one,
799 * (isl_bool_false, NULL) if there is no matching map and
800 * (isl_bool_error, NULL) on error.
802 * If not NULL, then data->control->filter specifies whether "map"
803 * can have any matching map. If so,
804 * data->control->match_space specifies which map in data->umap2
805 * corresponds to "map".
807 static __isl_keep isl_maybe_isl_map
bin_try_get_match(
808 struct isl_union_map_gen_bin_data
*data
, __isl_keep isl_map
*map
)
811 struct isl_hash_table_entry
*entry2
;
813 isl_maybe_isl_map res
= { isl_bool_error
, NULL
};
815 if (data
->control
->filter
) {
816 res
.valid
= data
->control
->filter(map
);
817 if (res
.valid
< 0 || !res
.valid
)
819 res
.valid
= isl_bool_error
;
822 space
= isl_map_get_space(map
);
823 if (data
->control
->match_space
!= &identity
)
824 space
= data
->control
->match_space(space
);
827 hash
= isl_space_get_hash(space
);
828 entry2
= isl_hash_table_find(isl_union_map_get_ctx(data
->umap2
),
829 &data
->umap2
->table
, hash
,
830 &has_space
, space
, 0);
831 isl_space_free(space
);
832 res
.valid
= entry2
!= NULL
;
834 res
.value
= entry2
->data
;
839 /* isl_hash_table_foreach callback for gen_bin_op.
840 * Look for the map in data->umap2 that corresponds
841 * to the map that "entry" points to, apply the binary operation and
842 * add the result to data->res.
844 * If no corresponding map can be found, then the effect depends
845 * on data->control->subtract. If it is set, then the current map
846 * is added directly to the result. Otherwise, it is ignored.
848 static isl_stat
gen_bin_entry(void **entry
, void *user
)
850 struct isl_union_map_gen_bin_data
*data
= user
;
851 isl_map
*map
= *entry
;
854 m
= bin_try_get_match(data
, map
);
856 return isl_stat_error
;
857 if (!m
.valid
&& !data
->control
->subtract
)
861 data
->res
= bin_add_map(data
->res
, map
);
863 data
->res
= bin_add_pair(data
->res
, map
, m
.value
, data
);
865 return isl_stat_error
;
870 /* Apply a binary operation to "umap1" and "umap2" based on "control".
871 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
874 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
875 __isl_take isl_union_map
*umap2
, struct isl_bin_op_control
*control
)
877 struct isl_union_map_gen_bin_data data
= { control
, NULL
, NULL
};
879 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
880 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
882 if (!umap1
|| !umap2
)
886 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
888 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
889 &gen_bin_entry
, &data
) < 0)
892 isl_union_map_free(umap1
);
893 isl_union_map_free(umap2
);
896 isl_union_map_free(umap1
);
897 isl_union_map_free(umap2
);
898 isl_union_map_free(data
.res
);
902 __isl_give isl_union_map
*isl_union_map_subtract(
903 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
905 struct isl_bin_op_control control
= {
907 .match_space
= &identity
,
908 .fn_map
= &isl_map_subtract
,
911 return gen_bin_op(umap1
, umap2
, &control
);
914 __isl_give isl_union_set
*isl_union_set_subtract(
915 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
917 return isl_union_map_subtract(uset1
, uset2
);
920 struct isl_union_map_gen_bin_set_data
{
925 static isl_stat
intersect_params_entry(void **entry
, void *user
)
927 struct isl_union_map_gen_bin_set_data
*data
= user
;
928 isl_map
*map
= *entry
;
931 map
= isl_map_copy(map
);
932 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
934 empty
= isl_map_is_empty(map
);
937 return isl_stat_error
;
940 data
->res
= isl_union_map_add_map(data
->res
, map
);
945 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
946 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
948 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
950 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
951 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
957 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
959 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
963 isl_union_map_free(umap
);
967 isl_union_map_free(umap
);
969 isl_union_map_free(data
.res
);
973 /* Intersect "umap" with the parameter domain "set".
975 * If "set" does not have any constraints, then we can return immediately.
977 __isl_give isl_union_map
*isl_union_map_intersect_params(
978 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
982 is_universe
= isl_set_plain_is_universe(set
);
990 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
992 isl_union_map_free(umap
);
997 __isl_give isl_union_set
*isl_union_set_intersect_params(
998 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1000 return isl_union_map_intersect_params(uset
, set
);
1003 static __isl_give isl_union_map
*union_map_intersect_params(
1004 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1006 return isl_union_map_intersect_params(umap
,
1007 isl_set_from_union_set(uset
));
1010 static __isl_give isl_union_map
*union_map_gist_params(
1011 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1013 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
1016 struct isl_union_map_match_bin_data
{
1017 isl_union_map
*umap2
;
1019 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
1022 static isl_stat
match_bin_entry(void **entry
, void *user
)
1024 struct isl_union_map_match_bin_data
*data
= user
;
1026 struct isl_hash_table_entry
*entry2
;
1027 isl_map
*map
= *entry
;
1030 hash
= isl_space_get_hash(map
->dim
);
1031 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1032 hash
, &has_space
, map
->dim
, 0);
1036 map
= isl_map_copy(map
);
1037 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
1039 empty
= isl_map_is_empty(map
);
1042 return isl_stat_error
;
1049 data
->res
= isl_union_map_add_map(data
->res
, map
);
1054 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
1055 __isl_take isl_union_map
*umap2
,
1056 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
1058 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
1060 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1061 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1063 if (!umap1
|| !umap2
)
1067 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1069 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1070 &match_bin_entry
, &data
) < 0)
1073 isl_union_map_free(umap1
);
1074 isl_union_map_free(umap2
);
1077 isl_union_map_free(umap1
);
1078 isl_union_map_free(umap2
);
1079 isl_union_map_free(data
.res
);
1083 __isl_give isl_union_map
*isl_union_map_intersect(
1084 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1086 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
1089 /* Compute the intersection of the two union_sets.
1090 * As a special case, if exactly one of the two union_sets
1091 * is a parameter domain, then intersect the parameter domain
1092 * of the other one with this set.
1094 __isl_give isl_union_set
*isl_union_set_intersect(
1095 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1099 p1
= isl_union_set_is_params(uset1
);
1100 p2
= isl_union_set_is_params(uset2
);
1101 if (p1
< 0 || p2
< 0)
1104 return union_map_intersect_params(uset1
, uset2
);
1106 return union_map_intersect_params(uset2
, uset1
);
1107 return isl_union_map_intersect(uset1
, uset2
);
1109 isl_union_set_free(uset1
);
1110 isl_union_set_free(uset2
);
1114 static isl_stat
gist_params_entry(void **entry
, void *user
)
1116 struct isl_union_map_gen_bin_set_data
*data
= user
;
1117 isl_map
*map
= *entry
;
1120 map
= isl_map_copy(map
);
1121 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
1123 empty
= isl_map_is_empty(map
);
1126 return isl_stat_error
;
1129 data
->res
= isl_union_map_add_map(data
->res
, map
);
1134 __isl_give isl_union_map
*isl_union_map_gist_params(
1135 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1137 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
1140 __isl_give isl_union_set
*isl_union_set_gist_params(
1141 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1143 return isl_union_map_gist_params(uset
, set
);
1146 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
1147 __isl_take isl_union_map
*context
)
1149 return match_bin_op(umap
, context
, &isl_map_gist
);
1152 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
1153 __isl_take isl_union_set
*context
)
1155 if (isl_union_set_is_params(context
))
1156 return union_map_gist_params(uset
, context
);
1157 return isl_union_map_gist(uset
, context
);
1160 /* For each map in "umap", remove the constraints in the corresponding map
1162 * Each map in "context" is assumed to consist of a single disjunct and
1163 * to have explicit representations for all local variables.
1165 __isl_give isl_union_map
*isl_union_map_plain_gist(
1166 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*context
)
1168 return match_bin_op(umap
, context
, &isl_map_plain_gist
);
1171 /* For each set in "uset", remove the constraints in the corresponding set
1173 * Each set in "context" is assumed to consist of a single disjunct and
1174 * to have explicit representations for all local variables.
1176 __isl_give isl_union_set
*isl_union_set_plain_gist(
1177 __isl_take isl_union_set
*uset
, __isl_take isl_union_set
*context
)
1179 return isl_union_map_plain_gist(uset
, context
);
1182 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
1183 __isl_take isl_map
*set2
)
1185 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
1188 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
1189 __isl_take isl_map
*set2
)
1191 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
1194 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
1195 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1197 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
1200 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
1201 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1203 return match_bin_op(uset1
, uset2
, &lex_le_set
);
1206 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
1207 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1209 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
1212 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
1213 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1215 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
1218 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
1219 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1221 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
1224 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
1225 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1227 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
1230 /* Intersect the domain of "umap" with "uset".
1232 static __isl_give isl_union_map
*union_map_intersect_domain(
1233 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1235 struct isl_bin_op_control control
= {
1236 .match_space
= &isl_space_domain
,
1237 .fn_map
= &isl_map_intersect_domain
,
1240 return gen_bin_op(umap
, uset
, &control
);
1243 /* Intersect the domain of "umap" with "uset".
1244 * If "uset" is a parameters domain, then intersect the parameter
1245 * domain of "umap" with this set.
1247 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1248 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1250 if (isl_union_set_is_params(uset
))
1251 return union_map_intersect_params(umap
, uset
);
1253 return union_map_intersect_domain(umap
, uset
);
1256 /* Remove the elements of "uset" from the domain of "umap".
1258 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1259 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1261 struct isl_bin_op_control control
= {
1263 .match_space
= &isl_space_domain
,
1264 .fn_map
= &isl_map_subtract_domain
,
1267 return gen_bin_op(umap
, dom
, &control
);
1270 /* Remove the elements of "uset" from the range of "umap".
1272 __isl_give isl_union_map
*isl_union_map_subtract_range(
1273 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1275 struct isl_bin_op_control control
= {
1277 .match_space
= &isl_space_range
,
1278 .fn_map
= &isl_map_subtract_range
,
1281 return gen_bin_op(umap
, dom
, &control
);
1284 /* Compute the gist of "umap" with respect to the domain "uset".
1286 static __isl_give isl_union_map
*union_map_gist_domain(
1287 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1289 struct isl_bin_op_control control
= {
1290 .match_space
= &isl_space_domain
,
1291 .fn_map
= &isl_map_gist_domain
,
1294 return gen_bin_op(umap
, uset
, &control
);
1297 /* Compute the gist of "umap" with respect to the domain "uset".
1298 * If "uset" is a parameters domain, then compute the gist
1299 * with respect to this parameter domain.
1301 __isl_give isl_union_map
*isl_union_map_gist_domain(
1302 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1304 if (isl_union_set_is_params(uset
))
1305 return union_map_gist_params(umap
, uset
);
1307 return union_map_gist_domain(umap
, uset
);
1310 /* Compute the gist of "umap" with respect to the range "uset".
1312 __isl_give isl_union_map
*isl_union_map_gist_range(
1313 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1315 struct isl_bin_op_control control
= {
1316 .match_space
= &isl_space_range
,
1317 .fn_map
= &isl_map_gist_range
,
1320 return gen_bin_op(umap
, uset
, &control
);
1323 __isl_give isl_union_map
*isl_union_map_intersect_range(
1324 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1326 struct isl_bin_op_control control
= {
1327 .match_space
= &isl_space_range
,
1328 .fn_map
= &isl_map_intersect_range
,
1331 return gen_bin_op(umap
, uset
, &control
);
1334 /* Intersect each map in "umap" in a space A -> [B -> C]
1335 * with the corresponding map in "factor" in the space A -> C and
1336 * collect the results.
1338 __isl_give isl_union_map
*isl_union_map_intersect_range_factor_range(
1339 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1341 struct isl_bin_op_control control
= {
1342 .filter
= &isl_map_range_is_wrapping
,
1343 .match_space
= &isl_space_range_factor_range
,
1344 .fn_map
= &isl_map_intersect_range_factor_range
,
1347 return gen_bin_op(umap
, factor
, &control
);
1350 struct isl_union_map_bin_data
{
1351 isl_union_map
*umap2
;
1354 isl_stat (*fn
)(void **entry
, void *user
);
1357 static isl_stat
apply_range_entry(void **entry
, void *user
)
1359 struct isl_union_map_bin_data
*data
= user
;
1360 isl_map
*map2
= *entry
;
1363 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1364 map2
->dim
, isl_dim_in
))
1367 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1369 empty
= isl_map_is_empty(map2
);
1372 return isl_stat_error
;
1379 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1384 static isl_stat
bin_entry(void **entry
, void *user
)
1386 struct isl_union_map_bin_data
*data
= user
;
1387 isl_map
*map
= *entry
;
1390 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1391 data
->fn
, data
) < 0)
1392 return isl_stat_error
;
1397 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1398 __isl_take isl_union_map
*umap2
,
1399 isl_stat (*fn
)(void **entry
, void *user
))
1401 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1403 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1404 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1406 if (!umap1
|| !umap2
)
1410 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1412 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1413 &bin_entry
, &data
) < 0)
1416 isl_union_map_free(umap1
);
1417 isl_union_map_free(umap2
);
1420 isl_union_map_free(umap1
);
1421 isl_union_map_free(umap2
);
1422 isl_union_map_free(data
.res
);
1426 __isl_give isl_union_map
*isl_union_map_apply_range(
1427 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1429 return bin_op(umap1
, umap2
, &apply_range_entry
);
1432 __isl_give isl_union_map
*isl_union_map_apply_domain(
1433 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1435 umap1
= isl_union_map_reverse(umap1
);
1436 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1437 return isl_union_map_reverse(umap1
);
1440 __isl_give isl_union_set
*isl_union_set_apply(
1441 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1443 return isl_union_map_apply_range(uset
, umap
);
1446 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1448 struct isl_union_map_bin_data
*data
= user
;
1449 isl_map
*map2
= *entry
;
1451 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1452 map2
->dim
, isl_dim_out
))
1455 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1457 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1462 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1463 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1465 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1468 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1470 struct isl_union_map_bin_data
*data
= user
;
1471 isl_map
*map2
= *entry
;
1473 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1474 map2
->dim
, isl_dim_out
))
1477 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1479 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1484 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1485 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1487 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1490 static isl_stat
product_entry(void **entry
, void *user
)
1492 struct isl_union_map_bin_data
*data
= user
;
1493 isl_map
*map2
= *entry
;
1495 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1497 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1502 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1503 __isl_take isl_union_map
*umap2
)
1505 return bin_op(umap1
, umap2
, &product_entry
);
1508 static isl_stat
set_product_entry(void **entry
, void *user
)
1510 struct isl_union_map_bin_data
*data
= user
;
1511 isl_set
*set2
= *entry
;
1513 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1515 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1520 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1521 __isl_take isl_union_set
*uset2
)
1523 return bin_op(uset1
, uset2
, &set_product_entry
);
1526 static isl_stat
domain_product_entry(void **entry
, void *user
)
1528 struct isl_union_map_bin_data
*data
= user
;
1529 isl_map
*map2
= *entry
;
1531 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1532 map2
->dim
, isl_dim_out
))
1535 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1536 isl_map_copy(map2
));
1538 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1543 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1545 __isl_give isl_union_map
*isl_union_map_domain_product(
1546 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1548 return bin_op(umap1
, umap2
, &domain_product_entry
);
1551 static isl_stat
range_product_entry(void **entry
, void *user
)
1553 struct isl_union_map_bin_data
*data
= user
;
1554 isl_map
*map2
= *entry
;
1556 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1557 map2
->dim
, isl_dim_in
))
1560 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1561 isl_map_copy(map2
));
1563 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1568 __isl_give isl_union_map
*isl_union_map_range_product(
1569 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1571 return bin_op(umap1
, umap2
, &range_product_entry
);
1574 /* If data->map A -> B and "map2" C -> D have the same range space,
1575 * then add (A, C) -> (B * D) to data->res.
1577 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1579 struct isl_union_map_bin_data
*data
= user
;
1580 isl_map
*map2
= *entry
;
1582 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1583 map2
->dim
, isl_dim_out
))
1586 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1587 isl_map_copy(map2
));
1589 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1594 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1596 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1597 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1599 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1602 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1604 struct isl_union_map_bin_data
*data
= user
;
1605 isl_map
*map2
= *entry
;
1607 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1608 map2
->dim
, isl_dim_in
))
1611 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1612 isl_map_copy(map2
));
1614 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1619 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1620 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1622 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1625 /* Data structure that specifies how un_op should modify
1626 * the maps in the union map.
1628 * If "inplace" is set, then the maps in the input union map
1629 * are modified in place. This means that "fn_map" should not
1630 * change the meaning of the map or that the union map only
1631 * has a single reference.
1632 * If "total" is set, then all maps need to be modified and
1633 * the results need to live in the same space.
1634 * Otherwise, a new union map is constructed to store the results.
1635 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1636 * are taken into account. "filter_user" is passed as the second argument
1637 * to "filter". No filter can be set if "inplace" or
1639 * "fn_map" specifies how the maps (selected by "filter")
1640 * should be transformed.
1642 struct isl_un_op_control
{
1645 isl_bool (*filter
)(__isl_keep isl_map
*map
, void *user
);
1647 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map
);
1650 /* Data structure for wrapping the data for un_op_filter_drop_user.
1651 * "filter" is the function that is being wrapped.
1653 struct isl_un_op_drop_user_data
{
1654 isl_bool (*filter
)(__isl_keep isl_map
*map
);
1657 /* Wrapper for isl_un_op_control filters that do not require
1658 * a second argument.
1659 * Simply call data->filter without the second argument.
1661 static isl_bool
un_op_filter_drop_user(__isl_keep isl_map
*map
, void *user
)
1663 struct isl_un_op_drop_user_data
*data
= user
;
1664 return data
->filter(map
);
1667 /* Internal data structure for "un_op".
1668 * "control" specifies how the maps in the union map should be modified.
1669 * "res" collects the results.
1671 struct isl_union_map_un_data
{
1672 struct isl_un_op_control
*control
;
1676 /* isl_hash_table_foreach callback for un_op.
1677 * Handle the map that "entry" points to.
1679 * If control->filter is set, then check if this map satisfies the filter.
1680 * If so (or if control->filter is not set), modify the map
1681 * by calling control->fn_map and either add the result to data->res or
1682 * replace the original entry by the result (if control->inplace is set).
1684 static isl_stat
un_entry(void **entry
, void *user
)
1686 struct isl_union_map_un_data
*data
= user
;
1687 isl_map
*map
= *entry
;
1689 if (data
->control
->filter
) {
1692 ok
= data
->control
->filter(map
, data
->control
->filter_user
);
1694 return isl_stat_error
;
1699 map
= data
->control
->fn_map(isl_map_copy(map
));
1701 return isl_stat_error
;
1702 if (data
->control
->inplace
) {
1703 isl_map_free(*entry
);
1706 data
->res
= isl_union_map_add_map(data
->res
, map
);
1708 return isl_stat_error
;
1714 /* Modify the maps in "umap" based on "control".
1715 * If control->inplace is set, then modify the maps in "umap" in-place.
1716 * Otherwise, create a new union map to hold the results.
1717 * If control->total is set, then perform an inplace computation
1718 * if "umap" is only referenced once. Otherwise, create a new union map
1719 * to store the results.
1721 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1722 struct isl_un_op_control
*control
)
1724 struct isl_union_map_un_data data
= { control
};
1728 if ((control
->inplace
|| control
->total
) && control
->filter
)
1729 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
1730 "inplace/total modification cannot be filtered",
1731 return isl_union_map_free(umap
));
1733 if (control
->total
&& umap
->ref
== 1)
1734 control
->inplace
= 1;
1735 if (control
->inplace
) {
1740 space
= isl_union_map_get_space(umap
);
1741 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
1743 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1744 &umap
->table
, &un_entry
, &data
) < 0)
1745 data
.res
= isl_union_map_free(data
.res
);
1747 if (control
->inplace
)
1749 isl_union_map_free(umap
);
1753 __isl_give isl_union_map
*isl_union_map_from_range(
1754 __isl_take isl_union_set
*uset
)
1756 struct isl_un_op_control control
= {
1757 .fn_map
= &isl_map_from_range
,
1759 return un_op(uset
, &control
);
1762 __isl_give isl_union_map
*isl_union_map_from_domain(
1763 __isl_take isl_union_set
*uset
)
1765 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1768 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1769 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1771 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1772 isl_union_map_from_range(range
));
1775 /* Modify the maps in "umap" by applying "fn" on them.
1776 * "fn" should apply to all maps in "umap" and should not modify the space.
1778 static __isl_give isl_union_map
*total(__isl_take isl_union_map
*umap
,
1779 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1781 struct isl_un_op_control control
= {
1786 return un_op(umap
, &control
);
1789 /* Compute the affine hull of "map" and return the result as an isl_map.
1791 static __isl_give isl_map
*isl_map_affine_hull_map(__isl_take isl_map
*map
)
1793 return isl_map_from_basic_map(isl_map_affine_hull(map
));
1796 __isl_give isl_union_map
*isl_union_map_affine_hull(
1797 __isl_take isl_union_map
*umap
)
1799 return total(umap
, &isl_map_affine_hull_map
);
1802 __isl_give isl_union_set
*isl_union_set_affine_hull(
1803 __isl_take isl_union_set
*uset
)
1805 return isl_union_map_affine_hull(uset
);
1808 /* Wrapper around isl_set_combined_lineality_space
1809 * that returns the combined lineality space in the form of an isl_set
1810 * instead of an isl_basic_set.
1812 static __isl_give isl_set
*combined_lineality_space(__isl_take isl_set
*set
)
1814 return isl_set_from_basic_set(isl_set_combined_lineality_space(set
));
1817 /* For each set in "uset", compute the (linear) hull
1818 * of the lineality spaces of its basic sets and
1819 * collect and return the results.
1821 __isl_give isl_union_set
*isl_union_set_combined_lineality_space(
1822 __isl_take isl_union_set
*uset
)
1824 struct isl_un_op_control control
= {
1825 .fn_map
= &combined_lineality_space
,
1827 return un_op(uset
, &control
);
1830 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1832 static __isl_give isl_map
*isl_map_polyhedral_hull_map(__isl_take isl_map
*map
)
1834 return isl_map_from_basic_map(isl_map_polyhedral_hull(map
));
1837 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1838 __isl_take isl_union_map
*umap
)
1840 return total(umap
, &isl_map_polyhedral_hull_map
);
1843 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1844 __isl_take isl_union_set
*uset
)
1846 return isl_union_map_polyhedral_hull(uset
);
1849 /* Compute a superset of the convex hull of "map" that is described
1850 * by only translates of the constraints in the constituents of "map" and
1851 * return the result as an isl_map.
1853 static __isl_give isl_map
*isl_map_simple_hull_map(__isl_take isl_map
*map
)
1855 return isl_map_from_basic_map(isl_map_simple_hull(map
));
1858 __isl_give isl_union_map
*isl_union_map_simple_hull(
1859 __isl_take isl_union_map
*umap
)
1861 return total(umap
, &isl_map_simple_hull_map
);
1864 __isl_give isl_union_set
*isl_union_set_simple_hull(
1865 __isl_take isl_union_set
*uset
)
1867 return isl_union_map_simple_hull(uset
);
1870 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1871 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1873 struct isl_un_op_control control
= {
1878 return un_op(umap
, &control
);
1881 /* Remove redundant constraints in each of the basic maps of "umap".
1882 * Since removing redundant constraints does not change the meaning
1883 * or the space, the operation can be performed in-place.
1885 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1886 __isl_take isl_union_map
*umap
)
1888 return inplace(umap
, &isl_map_remove_redundancies
);
1891 /* Remove redundant constraints in each of the basic sets of "uset".
1893 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1894 __isl_take isl_union_set
*uset
)
1896 return isl_union_map_remove_redundancies(uset
);
1899 __isl_give isl_union_map
*isl_union_map_coalesce(
1900 __isl_take isl_union_map
*umap
)
1902 return inplace(umap
, &isl_map_coalesce
);
1905 __isl_give isl_union_set
*isl_union_set_coalesce(
1906 __isl_take isl_union_set
*uset
)
1908 return isl_union_map_coalesce(uset
);
1911 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1912 __isl_take isl_union_map
*umap
)
1914 return inplace(umap
, &isl_map_detect_equalities
);
1917 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1918 __isl_take isl_union_set
*uset
)
1920 return isl_union_map_detect_equalities(uset
);
1923 __isl_give isl_union_map
*isl_union_map_compute_divs(
1924 __isl_take isl_union_map
*umap
)
1926 return inplace(umap
, &isl_map_compute_divs
);
1929 __isl_give isl_union_set
*isl_union_set_compute_divs(
1930 __isl_take isl_union_set
*uset
)
1932 return isl_union_map_compute_divs(uset
);
1935 __isl_give isl_union_map
*isl_union_map_lexmin(
1936 __isl_take isl_union_map
*umap
)
1938 return total(umap
, &isl_map_lexmin
);
1941 __isl_give isl_union_set
*isl_union_set_lexmin(
1942 __isl_take isl_union_set
*uset
)
1944 return isl_union_map_lexmin(uset
);
1947 __isl_give isl_union_map
*isl_union_map_lexmax(
1948 __isl_take isl_union_map
*umap
)
1950 return total(umap
, &isl_map_lexmax
);
1953 __isl_give isl_union_set
*isl_union_set_lexmax(
1954 __isl_take isl_union_set
*uset
)
1956 return isl_union_map_lexmax(uset
);
1959 /* Return the universe in the space of "map".
1961 static __isl_give isl_map
*universe(__isl_take isl_map
*map
)
1965 space
= isl_map_get_space(map
);
1967 return isl_map_universe(space
);
1970 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1972 struct isl_un_op_control control
= {
1973 .fn_map
= &universe
,
1975 return un_op(umap
, &control
);
1978 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1980 return isl_union_map_universe(uset
);
1983 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1985 struct isl_un_op_control control
= {
1986 .fn_map
= &isl_map_reverse
,
1988 return un_op(umap
, &control
);
1991 /* Compute the parameter domain of the given union map.
1993 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1995 struct isl_un_op_control control
= {
1996 .fn_map
= &isl_map_params
,
2000 empty
= isl_union_map_is_empty(umap
);
2005 space
= isl_union_map_get_space(umap
);
2006 isl_union_map_free(umap
);
2007 return isl_set_empty(space
);
2009 return isl_set_from_union_set(un_op(umap
, &control
));
2011 isl_union_map_free(umap
);
2015 /* Compute the parameter domain of the given union set.
2017 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
2019 return isl_union_map_params(uset
);
2022 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
2024 struct isl_un_op_control control
= {
2025 .fn_map
= &isl_map_domain
,
2027 return un_op(umap
, &control
);
2030 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
2032 struct isl_un_op_control control
= {
2033 .fn_map
= &isl_map_range
,
2035 return un_op(umap
, &control
);
2038 __isl_give isl_union_map
*isl_union_map_domain_map(
2039 __isl_take isl_union_map
*umap
)
2041 struct isl_un_op_control control
= {
2042 .fn_map
= &isl_map_domain_map
,
2044 return un_op(umap
, &control
);
2047 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2048 * add the result to "res".
2050 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
2052 isl_union_pw_multi_aff
**res
= user
;
2054 isl_pw_multi_aff
*pma
;
2056 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
2057 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
2058 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2060 return *res
? isl_stat_ok
: isl_stat_error
;
2064 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2067 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
2068 __isl_take isl_union_map
*umap
)
2070 isl_union_pw_multi_aff
*res
;
2072 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
2073 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
2074 res
= isl_union_pw_multi_aff_free(res
);
2076 isl_union_map_free(umap
);
2080 __isl_give isl_union_map
*isl_union_map_range_map(
2081 __isl_take isl_union_map
*umap
)
2083 struct isl_un_op_control control
= {
2084 .fn_map
= &isl_map_range_map
,
2086 return un_op(umap
, &control
);
2089 /* Given a collection of wrapped maps of the form A[B -> C],
2090 * return the collection of maps A[B -> C] -> B.
2092 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
2093 __isl_take isl_union_set
*uset
)
2095 struct isl_un_op_drop_user_data data
= { &isl_set_is_wrapping
};
2096 struct isl_un_op_control control
= {
2097 .filter
= &un_op_filter_drop_user
,
2098 .filter_user
= &data
,
2099 .fn_map
= &isl_set_wrapped_domain_map
,
2101 return un_op(uset
, &control
);
2104 /* Does "map" relate elements from the same space?
2106 static isl_bool
equal_tuples(__isl_keep isl_map
*map
, void *user
)
2108 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2109 map
->dim
, isl_dim_out
);
2112 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
2114 struct isl_un_op_control control
= {
2115 .filter
= &equal_tuples
,
2116 .fn_map
= &isl_map_deltas
,
2118 return un_op(umap
, &control
);
2121 __isl_give isl_union_map
*isl_union_map_deltas_map(
2122 __isl_take isl_union_map
*umap
)
2124 struct isl_un_op_control control
= {
2125 .filter
= &equal_tuples
,
2126 .fn_map
= &isl_map_deltas_map
,
2128 return un_op(umap
, &control
);
2131 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
2133 struct isl_un_op_control control
= {
2134 .fn_map
= &isl_set_identity
,
2136 return un_op(uset
, &control
);
2139 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2141 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
2143 isl_union_pw_multi_aff
**res
= user
;
2145 isl_pw_multi_aff
*pma
;
2147 space
= isl_space_map_from_set(isl_set_get_space(set
));
2148 pma
= isl_pw_multi_aff_identity(space
);
2149 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
2150 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2152 return *res
? isl_stat_ok
: isl_stat_error
;
2155 /* Return an identity function on "uset" in the form
2156 * of an isl_union_pw_multi_aff.
2158 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
2159 __isl_take isl_union_set
*uset
)
2161 isl_union_pw_multi_aff
*res
;
2163 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
2164 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
2165 res
= isl_union_pw_multi_aff_free(res
);
2167 isl_union_set_free(uset
);
2171 /* For each map in "umap" of the form [A -> B] -> C,
2172 * construct the map A -> C and collect the results.
2174 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2175 __isl_take isl_union_map
*umap
)
2177 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2178 struct isl_un_op_control control
= {
2179 .filter
= &un_op_filter_drop_user
,
2180 .filter_user
= &data
,
2181 .fn_map
= &isl_map_domain_factor_domain
,
2183 return un_op(umap
, &control
);
2186 /* For each map in "umap" of the form [A -> B] -> C,
2187 * construct the map B -> C and collect the results.
2189 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2190 __isl_take isl_union_map
*umap
)
2192 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2193 struct isl_un_op_control control
= {
2194 .filter
= &un_op_filter_drop_user
,
2195 .filter_user
= &data
,
2196 .fn_map
= &isl_map_domain_factor_range
,
2198 return un_op(umap
, &control
);
2201 /* For each map in "umap" of the form A -> [B -> C],
2202 * construct the map A -> B and collect the results.
2204 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2205 __isl_take isl_union_map
*umap
)
2207 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2208 struct isl_un_op_control control
= {
2209 .filter
= &un_op_filter_drop_user
,
2210 .filter_user
= &data
,
2211 .fn_map
= &isl_map_range_factor_domain
,
2213 return un_op(umap
, &control
);
2216 /* For each map in "umap" of the form A -> [B -> C],
2217 * construct the map A -> C and collect the results.
2219 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2220 __isl_take isl_union_map
*umap
)
2222 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2223 struct isl_un_op_control control
= {
2224 .filter
= &un_op_filter_drop_user
,
2225 .filter_user
= &data
,
2226 .fn_map
= &isl_map_range_factor_range
,
2228 return un_op(umap
, &control
);
2231 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2232 * construct the map A -> C and collect the results.
2234 __isl_give isl_union_map
*isl_union_map_factor_domain(
2235 __isl_take isl_union_map
*umap
)
2237 struct isl_un_op_drop_user_data data
= { &isl_map_is_product
};
2238 struct isl_un_op_control control
= {
2239 .filter
= &un_op_filter_drop_user
,
2240 .filter_user
= &data
,
2241 .fn_map
= &isl_map_factor_domain
,
2243 return un_op(umap
, &control
);
2246 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2247 * construct the map B -> D and collect the results.
2249 __isl_give isl_union_map
*isl_union_map_factor_range(
2250 __isl_take isl_union_map
*umap
)
2252 struct isl_un_op_drop_user_data data
= { &isl_map_is_product
};
2253 struct isl_un_op_control control
= {
2254 .filter
= &un_op_filter_drop_user
,
2255 .filter_user
= &data
,
2256 .fn_map
= &isl_map_factor_range
,
2258 return un_op(umap
, &control
);
2261 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2263 struct isl_un_op_drop_user_data data
= { &isl_set_is_wrapping
};
2264 struct isl_un_op_control control
= {
2265 .filter
= &un_op_filter_drop_user
,
2266 .filter_user
= &data
,
2267 .fn_map
= &isl_set_unwrap
,
2269 return un_op(uset
, &control
);
2272 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2274 struct isl_un_op_control control
= {
2275 .fn_map
= &isl_map_wrap
,
2277 return un_op(umap
, &control
);
2280 struct isl_union_map_is_subset_data
{
2281 isl_union_map
*umap2
;
2285 static isl_stat
is_subset_entry(void **entry
, void *user
)
2287 struct isl_union_map_is_subset_data
*data
= user
;
2289 struct isl_hash_table_entry
*entry2
;
2290 isl_map
*map
= *entry
;
2292 hash
= isl_space_get_hash(map
->dim
);
2293 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2294 hash
, &has_space
, map
->dim
, 0);
2296 int empty
= isl_map_is_empty(map
);
2298 return isl_stat_error
;
2301 data
->is_subset
= isl_bool_false
;
2302 return isl_stat_error
;
2305 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2306 if (data
->is_subset
< 0 || !data
->is_subset
)
2307 return isl_stat_error
;
2312 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2313 __isl_keep isl_union_map
*umap2
)
2315 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2317 umap1
= isl_union_map_copy(umap1
);
2318 umap2
= isl_union_map_copy(umap2
);
2319 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2320 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2322 if (!umap1
|| !umap2
)
2326 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2327 &is_subset_entry
, &data
) < 0 &&
2331 isl_union_map_free(umap1
);
2332 isl_union_map_free(umap2
);
2334 return data
.is_subset
;
2336 isl_union_map_free(umap1
);
2337 isl_union_map_free(umap2
);
2338 return isl_bool_error
;
2341 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2342 __isl_keep isl_union_set
*uset2
)
2344 return isl_union_map_is_subset(uset1
, uset2
);
2347 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2348 __isl_keep isl_union_map
*umap2
)
2352 if (!umap1
|| !umap2
)
2353 return isl_bool_error
;
2354 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2355 if (is_subset
!= isl_bool_true
)
2357 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2361 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2362 __isl_keep isl_union_set
*uset2
)
2364 return isl_union_map_is_equal(uset1
, uset2
);
2367 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2368 __isl_keep isl_union_map
*umap2
)
2372 if (!umap1
|| !umap2
)
2373 return isl_bool_error
;
2374 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2375 if (is_subset
!= isl_bool_true
)
2377 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2378 return isl_bool_not(is_subset
);
2381 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2382 __isl_keep isl_union_set
*uset2
)
2384 return isl_union_map_is_strict_subset(uset1
, uset2
);
2387 /* Internal data structure for isl_union_map_is_disjoint.
2388 * umap2 is the union map with which we are comparing.
2389 * is_disjoint is initialized to 1 and is set to 0 as soon
2390 * as the union maps turn out not to be disjoint.
2392 struct isl_union_map_is_disjoint_data
{
2393 isl_union_map
*umap2
;
2394 isl_bool is_disjoint
;
2397 /* Check if "map" is disjoint from data->umap2 and abort
2398 * the search if it is not.
2400 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2402 struct isl_union_map_is_disjoint_data
*data
= user
;
2404 struct isl_hash_table_entry
*entry2
;
2405 isl_map
*map
= *entry
;
2407 hash
= isl_space_get_hash(map
->dim
);
2408 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2409 hash
, &has_space
, map
->dim
, 0);
2413 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2414 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2415 return isl_stat_error
;
2420 /* Are "umap1" and "umap2" disjoint?
2422 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2423 __isl_keep isl_union_map
*umap2
)
2425 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2427 umap1
= isl_union_map_copy(umap1
);
2428 umap2
= isl_union_map_copy(umap2
);
2429 umap1
= isl_union_map_align_params(umap1
,
2430 isl_union_map_get_space(umap2
));
2431 umap2
= isl_union_map_align_params(umap2
,
2432 isl_union_map_get_space(umap1
));
2434 if (!umap1
|| !umap2
)
2438 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2439 &is_disjoint_entry
, &data
) < 0 &&
2443 isl_union_map_free(umap1
);
2444 isl_union_map_free(umap2
);
2446 return data
.is_disjoint
;
2448 isl_union_map_free(umap1
);
2449 isl_union_map_free(umap2
);
2450 return isl_bool_error
;
2453 /* Are "uset1" and "uset2" disjoint?
2455 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2456 __isl_keep isl_union_set
*uset2
)
2458 return isl_union_map_is_disjoint(uset1
, uset2
);
2461 static isl_stat
sample_entry(void **entry
, void *user
)
2463 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2464 isl_map
*map
= *entry
;
2466 *sample
= isl_map_sample(isl_map_copy(map
));
2468 return isl_stat_error
;
2469 if (!isl_basic_map_plain_is_empty(*sample
))
2470 return isl_stat_error
;
2474 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2476 isl_basic_map
*sample
= NULL
;
2481 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2482 &sample_entry
, &sample
) < 0 &&
2487 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2489 isl_union_map_free(umap
);
2493 isl_union_map_free(umap
);
2497 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2499 return bset_from_bmap(isl_union_map_sample(uset
));
2502 /* Return an element in "uset" in the form of an isl_point.
2503 * Return a void isl_point if "uset" is empty.
2505 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2507 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2510 struct isl_forall_data
{
2512 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2515 static isl_stat
forall_entry(void **entry
, void *user
)
2517 struct isl_forall_data
*data
= user
;
2518 isl_map
*map
= *entry
;
2520 data
->res
= data
->fn(map
);
2522 return isl_stat_error
;
2525 return isl_stat_error
;
2530 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2531 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2533 struct isl_forall_data data
= { isl_bool_true
, fn
};
2536 return isl_bool_error
;
2538 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2539 &forall_entry
, &data
) < 0 && data
.res
)
2540 return isl_bool_error
;
2545 struct isl_forall_user_data
{
2547 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2551 static isl_stat
forall_user_entry(void **entry
, void *user
)
2553 struct isl_forall_user_data
*data
= user
;
2554 isl_map
*map
= *entry
;
2556 data
->res
= data
->fn(map
, data
->user
);
2558 return isl_stat_error
;
2561 return isl_stat_error
;
2566 /* Check if fn(map, user) returns true for all maps "map" in umap.
2568 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2569 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2571 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2574 return isl_bool_error
;
2576 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2577 &forall_user_entry
, &data
) < 0 && data
.res
)
2578 return isl_bool_error
;
2583 /* Is "umap" obviously empty?
2585 isl_bool
isl_union_map_plain_is_empty(__isl_keep isl_union_map
*umap
)
2588 return isl_bool_error
;
2589 return isl_union_map_n_map(umap
) == 0;
2592 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2594 return union_map_forall(umap
, &isl_map_is_empty
);
2597 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2599 return isl_union_map_is_empty(uset
);
2602 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2609 return isl_bool_error
;
2611 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2612 map
->dim
, isl_dim_out
))
2613 return isl_bool_false
;
2615 dim
= isl_map_get_space(map
);
2616 id
= isl_map_identity(dim
);
2618 is_subset
= isl_map_is_subset(map
, id
);
2625 /* Given an isl_union_map that consists of a single map, check
2626 * if it is single-valued.
2628 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2633 umap
= isl_union_map_copy(umap
);
2634 map
= isl_map_from_union_map(umap
);
2635 sv
= isl_map_is_single_valued(map
);
2641 /* Internal data structure for single_valued_on_domain.
2643 * "umap" is the union map to be tested.
2644 * "sv" is set to 1 as long as "umap" may still be single-valued.
2646 struct isl_union_map_is_sv_data
{
2647 isl_union_map
*umap
;
2651 /* Check if the data->umap is single-valued on "set".
2653 * If data->umap consists of a single map on "set", then test it
2656 * Otherwise, compute
2660 * check if the result is a subset of the identity mapping and
2661 * store the result in data->sv.
2663 * Terminate as soon as data->umap has been determined not to
2666 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2668 struct isl_union_map_is_sv_data
*data
= user
;
2669 isl_union_map
*umap
, *test
;
2671 umap
= isl_union_map_copy(data
->umap
);
2672 umap
= isl_union_map_intersect_domain(umap
,
2673 isl_union_set_from_set(set
));
2675 if (isl_union_map_n_map(umap
) == 1) {
2676 data
->sv
= single_map_is_single_valued(umap
);
2677 isl_union_map_free(umap
);
2679 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2680 test
= isl_union_map_apply_range(test
, umap
);
2682 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2684 isl_union_map_free(test
);
2687 if (data
->sv
< 0 || !data
->sv
)
2688 return isl_stat_error
;
2692 /* Check if the given map is single-valued.
2694 * If the union map consists of a single map, then test it as an isl_map.
2695 * Otherwise, check if the union map is single-valued on each of its
2698 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2700 isl_union_map
*universe
;
2701 isl_union_set
*domain
;
2702 struct isl_union_map_is_sv_data data
;
2704 if (isl_union_map_n_map(umap
) == 1)
2705 return single_map_is_single_valued(umap
);
2707 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2708 domain
= isl_union_map_domain(universe
);
2710 data
.sv
= isl_bool_true
;
2712 if (isl_union_set_foreach_set(domain
,
2713 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2714 data
.sv
= isl_bool_error
;
2715 isl_union_set_free(domain
);
2720 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2724 umap
= isl_union_map_copy(umap
);
2725 umap
= isl_union_map_reverse(umap
);
2726 in
= isl_union_map_is_single_valued(umap
);
2727 isl_union_map_free(umap
);
2732 /* Is "map" obviously not an identity relation because
2733 * it maps elements from one space to another space?
2734 * Update *non_identity accordingly.
2736 * In particular, if the domain and range spaces are the same,
2737 * then the map is not considered to obviously not be an identity relation.
2738 * Otherwise, the map is considered to obviously not be an identity relation
2739 * if it is is non-empty.
2741 * If "map" is determined to obviously not be an identity relation,
2742 * then the search is aborted.
2744 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2746 isl_bool
*non_identity
= user
;
2750 space
= isl_map_get_space(map
);
2751 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2752 if (equal
>= 0 && !equal
)
2753 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2755 *non_identity
= isl_bool_not(equal
);
2756 isl_space_free(space
);
2759 if (*non_identity
< 0 || *non_identity
)
2760 return isl_stat_error
;
2765 /* Is "umap" obviously not an identity relation because
2766 * it maps elements from one space to another space?
2768 * As soon as a map has been found that maps elements to a different space,
2769 * non_identity is changed and the search is aborted.
2771 static isl_bool
isl_union_map_plain_is_not_identity(
2772 __isl_keep isl_union_map
*umap
)
2774 isl_bool non_identity
;
2776 non_identity
= isl_bool_false
;
2777 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2778 &non_identity
) < 0 &&
2779 non_identity
== isl_bool_false
)
2780 return isl_bool_error
;
2782 return non_identity
;
2785 /* Does "map" only map elements to themselves?
2786 * Update *identity accordingly.
2788 * If "map" is determined not to be an identity relation,
2789 * then the search is aborted.
2791 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2793 isl_bool
*identity
= user
;
2795 *identity
= isl_map_is_identity(map
);
2798 if (*identity
< 0 || !*identity
)
2799 return isl_stat_error
;
2804 /* Does "umap" only map elements to themselves?
2806 * First check if there are any maps that map elements to different spaces.
2807 * If not, then check that all the maps (between identical spaces)
2808 * are identity relations.
2810 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2812 isl_bool non_identity
;
2815 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2816 if (non_identity
< 0 || non_identity
)
2817 return isl_bool_not(non_identity
);
2819 identity
= isl_bool_true
;
2820 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2821 identity
== isl_bool_true
)
2822 return isl_bool_error
;
2827 /* Represents a map that has a fixed value (v) for one of its
2829 * The map in this structure is not reference counted, so it
2830 * is only valid while the isl_union_map from which it was
2831 * obtained is still alive.
2833 struct isl_fixed_map
{
2838 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2842 struct isl_fixed_map
*v
;
2844 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2847 for (i
= 0; i
< n
; ++i
)
2848 isl_int_init(v
[i
].v
);
2852 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2858 for (i
= 0; i
< n
; ++i
)
2859 isl_int_clear(v
[i
].v
);
2863 /* Compare the "v" field of two isl_fixed_map structs.
2865 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2867 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2868 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2870 return isl_int_cmp(e1
->v
, e2
->v
);
2873 /* Internal data structure used while checking whether all maps
2874 * in a union_map have a fixed value for a given output dimension.
2875 * v is the list of maps, with the fixed value for the dimension
2876 * n is the number of maps considered so far
2877 * pos is the output dimension under investigation
2879 struct isl_fixed_dim_data
{
2880 struct isl_fixed_map
*v
;
2885 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2887 struct isl_fixed_dim_data
*data
= user
;
2889 data
->v
[data
->n
].map
= map
;
2890 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2891 &data
->v
[data
->n
++].v
);
2894 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2895 int first
, int n_range
);
2897 /* Given a list of the maps, with their fixed values at output dimension "pos",
2898 * check whether the ranges of the maps form an obvious partition.
2900 * We first sort the maps according to their fixed values.
2901 * If all maps have a different value, then we know the ranges form
2903 * Otherwise, we collect the maps with the same fixed value and
2904 * check whether each such collection is obviously injective
2905 * based on later dimensions.
2907 static int separates(struct isl_fixed_map
*v
, int n
,
2908 __isl_take isl_space
*dim
, int pos
, int n_range
)
2915 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2917 for (i
= 0; i
+ 1 < n
; ++i
) {
2919 isl_union_map
*part
;
2922 for (j
= i
+ 1; j
< n
; ++j
)
2923 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2929 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2930 for (k
= i
; k
< j
; ++k
)
2931 part
= isl_union_map_add_map(part
,
2932 isl_map_copy(v
[k
].map
));
2934 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2943 isl_space_free(dim
);
2944 free_isl_fixed_map_array(v
, n
);
2947 isl_space_free(dim
);
2948 free_isl_fixed_map_array(v
, n
);
2952 /* Check whether the maps in umap have obviously distinct ranges.
2953 * In particular, check for an output dimension in the range
2954 * [first,n_range) for which all maps have a fixed value
2955 * and then check if these values, possibly along with fixed values
2956 * at later dimensions, entail distinct ranges.
2958 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2959 int first
, int n_range
)
2963 struct isl_fixed_dim_data data
= { NULL
};
2965 ctx
= isl_union_map_get_ctx(umap
);
2967 n
= isl_union_map_n_map(umap
);
2972 isl_union_map_free(umap
);
2973 return isl_bool_true
;
2976 if (first
>= n_range
) {
2977 isl_union_map_free(umap
);
2978 return isl_bool_false
;
2981 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2985 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2991 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2996 dim
= isl_union_map_get_space(umap
);
2997 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2998 isl_union_map_free(umap
);
3002 free_isl_fixed_map_array(data
.v
, n
);
3003 isl_union_map_free(umap
);
3005 return isl_bool_false
;
3007 free_isl_fixed_map_array(data
.v
, n
);
3008 isl_union_map_free(umap
);
3009 return isl_bool_error
;
3012 /* Check whether the maps in umap that map to subsets of "ran"
3013 * have obviously distinct ranges.
3015 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
3018 isl_union_map
*umap
= user
;
3020 umap
= isl_union_map_copy(umap
);
3021 umap
= isl_union_map_intersect_range(umap
,
3022 isl_union_set_from_set(isl_set_copy(ran
)));
3023 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
3026 /* Check if the given union_map is obviously injective.
3028 * In particular, we first check if all individual maps are obviously
3029 * injective and then check if all the ranges of these maps are
3030 * obviously disjoint.
3032 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
3035 isl_union_map
*univ
;
3038 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
3040 return isl_bool_error
;
3042 return isl_bool_false
;
3044 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
3045 ran
= isl_union_map_range(univ
);
3047 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
3049 isl_union_set_free(ran
);
3054 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
3058 sv
= isl_union_map_is_single_valued(umap
);
3062 return isl_union_map_is_injective(umap
);
3065 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
3067 struct isl_un_op_drop_user_data data
= { &isl_map_can_zip
};
3068 struct isl_un_op_control control
= {
3069 .filter
= &un_op_filter_drop_user
,
3070 .filter_user
= &data
,
3071 .fn_map
= &isl_map_zip
,
3073 return un_op(umap
, &control
);
3076 /* Given a union map, take the maps of the form A -> (B -> C) and
3077 * return the union of the corresponding maps (A -> B) -> C.
3079 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
3081 struct isl_un_op_drop_user_data data
= { &isl_map_can_uncurry
};
3082 struct isl_un_op_control control
= {
3083 .filter
= &un_op_filter_drop_user
,
3084 .filter_user
= &data
,
3085 .fn_map
= &isl_map_uncurry
,
3087 return un_op(umap
, &control
);
3090 /* Given a union map, take the maps of the form (A -> B) -> C and
3091 * return the union of the corresponding maps A -> (B -> C).
3093 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
3095 struct isl_un_op_drop_user_data data
= { &isl_map_can_curry
};
3096 struct isl_un_op_control control
= {
3097 .filter
= &un_op_filter_drop_user
,
3098 .filter_user
= &data
,
3099 .fn_map
= &isl_map_curry
,
3101 return un_op(umap
, &control
);
3104 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3105 * return the union of the corresponding maps A -> (B -> (C -> D)).
3107 __isl_give isl_union_map
*isl_union_map_range_curry(
3108 __isl_take isl_union_map
*umap
)
3110 struct isl_un_op_drop_user_data data
= { &isl_map_can_range_curry
};
3111 struct isl_un_op_control control
= {
3112 .filter
= &un_op_filter_drop_user
,
3113 .filter_user
= &data
,
3114 .fn_map
= &isl_map_range_curry
,
3116 return un_op(umap
, &control
);
3119 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
3121 struct isl_un_op_control control
= {
3122 .fn_map
= &isl_set_lift
,
3124 return un_op(uset
, &control
);
3127 static isl_stat
coefficients_entry(void **entry
, void *user
)
3129 isl_set
*set
= *entry
;
3130 isl_union_set
**res
= user
;
3132 set
= isl_set_copy(set
);
3133 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
3134 *res
= isl_union_set_add_set(*res
, set
);
3139 __isl_give isl_union_set
*isl_union_set_coefficients(
3140 __isl_take isl_union_set
*uset
)
3149 ctx
= isl_union_set_get_ctx(uset
);
3150 dim
= isl_space_set_alloc(ctx
, 0, 0);
3151 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
3152 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3153 &coefficients_entry
, &res
) < 0)
3156 isl_union_set_free(uset
);
3159 isl_union_set_free(uset
);
3160 isl_union_set_free(res
);
3164 static isl_stat
solutions_entry(void **entry
, void *user
)
3166 isl_set
*set
= *entry
;
3167 isl_union_set
**res
= user
;
3169 set
= isl_set_copy(set
);
3170 set
= isl_set_from_basic_set(isl_set_solutions(set
));
3172 *res
= isl_union_set_from_set(set
);
3174 *res
= isl_union_set_add_set(*res
, set
);
3177 return isl_stat_error
;
3182 __isl_give isl_union_set
*isl_union_set_solutions(
3183 __isl_take isl_union_set
*uset
)
3185 isl_union_set
*res
= NULL
;
3190 if (uset
->table
.n
== 0) {
3191 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
3192 isl_union_set_free(uset
);
3196 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3197 &solutions_entry
, &res
) < 0)
3200 isl_union_set_free(uset
);
3203 isl_union_set_free(uset
);
3204 isl_union_set_free(res
);
3208 /* Is the domain space of "map" equal to "space"?
3210 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3212 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
3213 space
, isl_dim_out
);
3216 /* Is the range space of "map" equal to "space"?
3218 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3220 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3221 space
, isl_dim_out
);
3224 /* Is the set space of "map" equal to "space"?
3226 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3228 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3229 space
, isl_dim_out
);
3232 /* Internal data structure for preimage_pw_multi_aff.
3234 * "pma" is the function under which the preimage should be taken.
3235 * "space" is the space of "pma".
3236 * "res" collects the results.
3237 * "fn" computes the preimage for a given map.
3238 * "match" returns true if "fn" can be called.
3240 struct isl_union_map_preimage_data
{
3242 isl_pw_multi_aff
*pma
;
3244 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3245 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3246 __isl_take isl_pw_multi_aff
*pma
);
3249 /* Call data->fn to compute the preimage of the domain or range of *entry
3250 * under the function represented by data->pma, provided the domain/range
3251 * space of *entry matches the target space of data->pma
3252 * (as given by data->match), and add the result to data->res.
3254 static isl_stat
preimage_entry(void **entry
, void *user
)
3257 isl_map
*map
= *entry
;
3258 struct isl_union_map_preimage_data
*data
= user
;
3261 m
= data
->match(map
, data
->space
);
3263 return isl_stat_error
;
3267 map
= isl_map_copy(map
);
3268 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3270 empty
= isl_map_is_empty(map
);
3271 if (empty
< 0 || empty
) {
3273 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3276 data
->res
= isl_union_map_add_map(data
->res
, map
);
3281 /* Compute the preimage of the domain or range of "umap" under the function
3282 * represented by "pma".
3283 * In other words, plug in "pma" in the domain or range of "umap".
3284 * The function "fn" performs the actual preimage computation on a map,
3285 * while "match" determines to which maps the function should be applied.
3287 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3288 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3289 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3290 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3291 __isl_take isl_pw_multi_aff
*pma
))
3295 struct isl_union_map_preimage_data data
;
3297 umap
= isl_union_map_align_params(umap
,
3298 isl_pw_multi_aff_get_space(pma
));
3299 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3304 ctx
= isl_union_map_get_ctx(umap
);
3305 space
= isl_union_map_get_space(umap
);
3306 data
.space
= isl_pw_multi_aff_get_space(pma
);
3308 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3311 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3313 data
.res
= isl_union_map_free(data
.res
);
3315 isl_space_free(data
.space
);
3316 isl_union_map_free(umap
);
3317 isl_pw_multi_aff_free(pma
);
3320 isl_union_map_free(umap
);
3321 isl_pw_multi_aff_free(pma
);
3325 /* Compute the preimage of the domain of "umap" under the function
3326 * represented by "pma".
3327 * In other words, plug in "pma" in the domain of "umap".
3328 * The result contains maps that live in the same spaces as the maps of "umap"
3329 * with domain space equal to the target space of "pma",
3330 * except that the domain has been replaced by the domain space of "pma".
3332 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3333 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3335 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3336 &isl_map_preimage_domain_pw_multi_aff
);
3339 /* Compute the preimage of the range of "umap" under the function
3340 * represented by "pma".
3341 * In other words, plug in "pma" in the range of "umap".
3342 * The result contains maps that live in the same spaces as the maps of "umap"
3343 * with range space equal to the target space of "pma",
3344 * except that the range has been replaced by the domain space of "pma".
3346 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3347 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3349 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3350 &isl_map_preimage_range_pw_multi_aff
);
3353 /* Compute the preimage of "uset" under the function represented by "pma".
3354 * In other words, plug in "pma" in "uset".
3355 * The result contains sets that live in the same spaces as the sets of "uset"
3356 * with space equal to the target space of "pma",
3357 * except that the space has been replaced by the domain space of "pma".
3359 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3360 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3362 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3363 &isl_set_preimage_pw_multi_aff
);
3366 /* Compute the preimage of the domain of "umap" under the function
3367 * represented by "ma".
3368 * In other words, plug in "ma" in the domain of "umap".
3369 * The result contains maps that live in the same spaces as the maps of "umap"
3370 * with domain space equal to the target space of "ma",
3371 * except that the domain has been replaced by the domain space of "ma".
3373 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3374 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3376 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3377 isl_pw_multi_aff_from_multi_aff(ma
));
3380 /* Compute the preimage of the range of "umap" under the function
3381 * represented by "ma".
3382 * In other words, plug in "ma" in the range of "umap".
3383 * The result contains maps that live in the same spaces as the maps of "umap"
3384 * with range space equal to the target space of "ma",
3385 * except that the range has been replaced by the domain space of "ma".
3387 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3388 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3390 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3391 isl_pw_multi_aff_from_multi_aff(ma
));
3394 /* Compute the preimage of "uset" under the function represented by "ma".
3395 * In other words, plug in "ma" in "uset".
3396 * The result contains sets that live in the same spaces as the sets of "uset"
3397 * with space equal to the target space of "ma",
3398 * except that the space has been replaced by the domain space of "ma".
3400 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3401 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3403 return isl_union_set_preimage_pw_multi_aff(uset
,
3404 isl_pw_multi_aff_from_multi_aff(ma
));
3407 /* Internal data structure for preimage_multi_pw_aff.
3409 * "mpa" is the function under which the preimage should be taken.
3410 * "space" is the space of "mpa".
3411 * "res" collects the results.
3412 * "fn" computes the preimage for a given map.
3413 * "match" returns true if "fn" can be called.
3415 struct isl_union_map_preimage_mpa_data
{
3417 isl_multi_pw_aff
*mpa
;
3419 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3420 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3421 __isl_take isl_multi_pw_aff
*mpa
);
3424 /* Call data->fn to compute the preimage of the domain or range of *entry
3425 * under the function represented by data->mpa, provided the domain/range
3426 * space of *entry matches the target space of data->mpa
3427 * (as given by data->match), and add the result to data->res.
3429 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3432 isl_map
*map
= *entry
;
3433 struct isl_union_map_preimage_mpa_data
*data
= user
;
3436 m
= data
->match(map
, data
->space
);
3438 return isl_stat_error
;
3442 map
= isl_map_copy(map
);
3443 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3445 empty
= isl_map_is_empty(map
);
3446 if (empty
< 0 || empty
) {
3448 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3451 data
->res
= isl_union_map_add_map(data
->res
, map
);
3456 /* Compute the preimage of the domain or range of "umap" under the function
3457 * represented by "mpa".
3458 * In other words, plug in "mpa" in the domain or range of "umap".
3459 * The function "fn" performs the actual preimage computation on a map,
3460 * while "match" determines to which maps the function should be applied.
3462 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3463 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3464 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3465 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3466 __isl_take isl_multi_pw_aff
*mpa
))
3470 struct isl_union_map_preimage_mpa_data data
;
3472 umap
= isl_union_map_align_params(umap
,
3473 isl_multi_pw_aff_get_space(mpa
));
3474 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3479 ctx
= isl_union_map_get_ctx(umap
);
3480 space
= isl_union_map_get_space(umap
);
3481 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3483 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3486 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3488 data
.res
= isl_union_map_free(data
.res
);
3490 isl_space_free(data
.space
);
3491 isl_union_map_free(umap
);
3492 isl_multi_pw_aff_free(mpa
);
3495 isl_union_map_free(umap
);
3496 isl_multi_pw_aff_free(mpa
);
3500 /* Compute the preimage of the domain of "umap" under the function
3501 * represented by "mpa".
3502 * In other words, plug in "mpa" in the domain of "umap".
3503 * The result contains maps that live in the same spaces as the maps of "umap"
3504 * with domain space equal to the target space of "mpa",
3505 * except that the domain has been replaced by the domain space of "mpa".
3507 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3508 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3510 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3511 &isl_map_preimage_domain_multi_pw_aff
);
3514 /* Internal data structure for preimage_upma.
3516 * "umap" is the map of which the preimage should be computed.
3517 * "res" collects the results.
3518 * "fn" computes the preimage for a given piecewise multi-affine function.
3520 struct isl_union_map_preimage_upma_data
{
3521 isl_union_map
*umap
;
3523 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3524 __isl_take isl_pw_multi_aff
*pma
);
3527 /* Call data->fn to compute the preimage of the domain or range of data->umap
3528 * under the function represented by pma and add the result to data->res.
3530 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3532 struct isl_union_map_preimage_upma_data
*data
= user
;
3533 isl_union_map
*umap
;
3535 umap
= isl_union_map_copy(data
->umap
);
3536 umap
= data
->fn(umap
, pma
);
3537 data
->res
= isl_union_map_union(data
->res
, umap
);
3539 return data
->res
? isl_stat_ok
: isl_stat_error
;
3542 /* Compute the preimage of the domain or range of "umap" under the function
3543 * represented by "upma".
3544 * In other words, plug in "upma" in the domain or range of "umap".
3545 * The function "fn" performs the actual preimage computation
3546 * on a piecewise multi-affine function.
3548 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3549 __isl_take isl_union_map
*umap
,
3550 __isl_take isl_union_pw_multi_aff
*upma
,
3551 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3552 __isl_take isl_pw_multi_aff
*pma
))
3554 struct isl_union_map_preimage_upma_data data
;
3557 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3559 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3560 &preimage_upma
, &data
) < 0)
3561 data
.res
= isl_union_map_free(data
.res
);
3563 isl_union_map_free(umap
);
3564 isl_union_pw_multi_aff_free(upma
);
3569 /* Compute the preimage of the domain of "umap" under the function
3570 * represented by "upma".
3571 * In other words, plug in "upma" in the domain of "umap".
3572 * The result contains maps that live in the same spaces as the maps of "umap"
3573 * with domain space equal to one of the target spaces of "upma",
3574 * except that the domain has been replaced by one of the domain spaces that
3575 * correspond to that target space of "upma".
3577 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3578 __isl_take isl_union_map
*umap
,
3579 __isl_take isl_union_pw_multi_aff
*upma
)
3581 return preimage_union_pw_multi_aff(umap
, upma
,
3582 &isl_union_map_preimage_domain_pw_multi_aff
);
3585 /* Compute the preimage of the range of "umap" under the function
3586 * represented by "upma".
3587 * In other words, plug in "upma" in the range of "umap".
3588 * The result contains maps that live in the same spaces as the maps of "umap"
3589 * with range space equal to one of the target spaces of "upma",
3590 * except that the range has been replaced by one of the domain spaces that
3591 * correspond to that target space of "upma".
3593 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3594 __isl_take isl_union_map
*umap
,
3595 __isl_take isl_union_pw_multi_aff
*upma
)
3597 return preimage_union_pw_multi_aff(umap
, upma
,
3598 &isl_union_map_preimage_range_pw_multi_aff
);
3601 /* Compute the preimage of "uset" under the function represented by "upma".
3602 * In other words, plug in "upma" in the range of "uset".
3603 * The result contains sets that live in the same spaces as the sets of "uset"
3604 * with space equal to one of the target spaces of "upma",
3605 * except that the space has been replaced by one of the domain spaces that
3606 * correspond to that target space of "upma".
3608 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3609 __isl_take isl_union_set
*uset
,
3610 __isl_take isl_union_pw_multi_aff
*upma
)
3612 return preimage_union_pw_multi_aff(uset
, upma
,
3613 &isl_union_set_preimage_pw_multi_aff
);
3616 /* Reset the user pointer on all identifiers of parameters and tuples
3617 * of the spaces of "umap".
3619 __isl_give isl_union_map
*isl_union_map_reset_user(
3620 __isl_take isl_union_map
*umap
)
3622 umap
= isl_union_map_cow(umap
);
3625 umap
->dim
= isl_space_reset_user(umap
->dim
);
3627 return isl_union_map_free(umap
);
3628 return total(umap
, &isl_map_reset_user
);
3631 /* Reset the user pointer on all identifiers of parameters and tuples
3632 * of the spaces of "uset".
3634 __isl_give isl_union_set
*isl_union_set_reset_user(
3635 __isl_take isl_union_set
*uset
)
3637 return isl_union_map_reset_user(uset
);
3640 /* Remove all existentially quantified variables and integer divisions
3641 * from "umap" using Fourier-Motzkin elimination.
3643 __isl_give isl_union_map
*isl_union_map_remove_divs(
3644 __isl_take isl_union_map
*umap
)
3646 return total(umap
, &isl_map_remove_divs
);
3649 /* Remove all existentially quantified variables and integer divisions
3650 * from "uset" using Fourier-Motzkin elimination.
3652 __isl_give isl_union_set
*isl_union_set_remove_divs(
3653 __isl_take isl_union_set
*uset
)
3655 return isl_union_map_remove_divs(uset
);
3658 /* Internal data structure for isl_union_map_project_out.
3659 * "type", "first" and "n" are the arguments for the isl_map_project_out
3661 * "res" collects the results.
3663 struct isl_union_map_project_out_data
{
3664 enum isl_dim_type type
;
3671 /* Turn the data->n dimensions of type data->type, starting at data->first
3672 * into existentially quantified variables and add the result to data->res.
3674 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3676 struct isl_union_map_project_out_data
*data
= user
;
3678 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3679 data
->res
= isl_union_map_add_map(data
->res
, map
);
3684 /* Turn the "n" dimensions of type "type", starting at "first"
3685 * into existentially quantified variables.
3686 * Since the space of an isl_union_map only contains parameters,
3687 * type is required to be equal to isl_dim_param.
3689 __isl_give isl_union_map
*isl_union_map_project_out(
3690 __isl_take isl_union_map
*umap
,
3691 enum isl_dim_type type
, unsigned first
, unsigned n
)
3694 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3699 if (type
!= isl_dim_param
)
3700 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3701 "can only project out parameters",
3702 return isl_union_map_free(umap
));
3704 space
= isl_union_map_get_space(umap
);
3705 space
= isl_space_drop_dims(space
, type
, first
, n
);
3706 data
.res
= isl_union_map_empty(space
);
3707 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3708 data
.res
= isl_union_map_free(data
.res
);
3710 isl_union_map_free(umap
);
3715 /* Project out all parameters from "umap" by existentially quantifying
3718 __isl_give isl_union_map
*isl_union_map_project_out_all_params(
3719 __isl_take isl_union_map
*umap
)
3725 n
= isl_union_map_dim(umap
, isl_dim_param
);
3726 return isl_union_map_project_out(umap
, isl_dim_param
, 0, n
);
3729 /* Turn the "n" dimensions of type "type", starting at "first"
3730 * into existentially quantified variables.
3731 * Since the space of an isl_union_set only contains parameters,
3732 * "type" is required to be equal to isl_dim_param.
3734 __isl_give isl_union_set
*isl_union_set_project_out(
3735 __isl_take isl_union_set
*uset
,
3736 enum isl_dim_type type
, unsigned first
, unsigned n
)
3738 return isl_union_map_project_out(uset
, type
, first
, n
);
3741 /* Internal data structure for isl_union_map_involves_dims.
3742 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3744 struct isl_union_map_involves_dims_data
{
3749 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3751 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3753 struct isl_union_map_involves_dims_data
*data
= user
;
3756 involves
= isl_map_involves_dims(map
,
3757 isl_dim_param
, data
->first
, data
->n
);
3758 return isl_bool_not(involves
);
3761 /* Does "umap" involve any of the n parameters starting at first?
3762 * "type" is required to be set to isl_dim_param.
3764 * "umap" involves any of those parameters if any of its maps
3765 * involve the parameters. In other words, "umap" does not
3766 * involve any of the parameters if all its maps to not
3767 * involve the parameters.
3769 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3770 enum isl_dim_type type
, unsigned first
, unsigned n
)
3772 struct isl_union_map_involves_dims_data data
= { first
, n
};
3775 if (type
!= isl_dim_param
)
3776 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3777 "can only reference parameters", return isl_bool_error
);
3779 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3781 return isl_bool_not(excludes
);
3784 /* Internal data structure for isl_union_map_reset_range_space.
3785 * "range" is the space from which to set the range space.
3786 * "res" collects the results.
3788 struct isl_union_map_reset_range_space_data
{
3793 /* Replace the range space of "map" by the range space of data->range and
3794 * add the result to data->res.
3796 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3798 struct isl_union_map_reset_range_space_data
*data
= user
;
3801 space
= isl_map_get_space(map
);
3802 space
= isl_space_domain(space
);
3803 space
= isl_space_extend_domain_with_range(space
,
3804 isl_space_copy(data
->range
));
3805 map
= isl_map_reset_space(map
, space
);
3806 data
->res
= isl_union_map_add_map(data
->res
, map
);
3808 return data
->res
? isl_stat_ok
: isl_stat_error
;
3811 /* Replace the range space of all the maps in "umap" by
3812 * the range space of "space".
3814 * This assumes that all maps have the same output dimension.
3815 * This function should therefore not be made publicly available.
3817 * Since the spaces of the maps change, so do their hash value.
3818 * We therefore need to create a new isl_union_map.
3820 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3821 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3823 struct isl_union_map_reset_range_space_data data
= { space
};
3825 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3826 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
3827 data
.res
= isl_union_map_free(data
.res
);
3829 isl_space_free(space
);
3830 isl_union_map_free(umap
);
3834 /* Check that "umap" and "space" have the same number of parameters.
3836 static isl_stat
check_union_map_space_equal_dim(__isl_keep isl_union_map
*umap
,
3837 __isl_keep isl_space
*space
)
3839 unsigned dim1
, dim2
;
3841 if (!umap
|| !space
)
3842 return isl_stat_error
;
3843 dim1
= isl_union_map_dim(umap
, isl_dim_param
);
3844 dim2
= isl_space_dim(space
, isl_dim_param
);
3847 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3848 "number of parameters does not match", return isl_stat_error
);
3851 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3852 * "space" is the target space.
3853 * "res" collects the results.
3855 struct isl_union_map_reset_params_data
{
3860 /* Replace the parameters of "map" by those of data->space and
3861 * add the result to data->res.
3863 static isl_stat
reset_params(__isl_take isl_map
*map
, void *user
)
3865 struct isl_union_map_reset_params_data
*data
= user
;
3868 space
= isl_map_get_space(map
);
3869 space
= isl_space_replace_params(space
, data
->space
);
3870 map
= isl_map_reset_equal_dim_space(map
, space
);
3871 data
->res
= isl_union_map_add_map(data
->res
, map
);
3873 return data
->res
? isl_stat_ok
: isl_stat_error
;
3876 /* Replace the space of "umap" by "space", without modifying
3877 * the dimension of "umap", i.e., the number of parameters of "umap".
3879 * Since the hash values of the maps in the union map depend
3880 * on the parameters, a new union map needs to be constructed.
3882 __isl_give isl_union_map
*isl_union_map_reset_equal_dim_space(
3883 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3885 struct isl_union_map_reset_params_data data
= { space
};
3887 isl_space
*umap_space
;
3889 umap_space
= isl_union_map_peek_space(umap
);
3890 equal
= isl_space_is_equal(umap_space
, space
);
3894 isl_space_free(space
);
3897 if (check_union_map_space_equal_dim(umap
, space
) < 0)
3900 data
.res
= isl_union_map_empty(isl_space_copy(space
));
3901 if (isl_union_map_foreach_map(umap
, &reset_params
, &data
) < 0)
3902 data
.res
= isl_union_map_free(data
.res
);
3904 isl_space_free(space
);
3905 isl_union_map_free(umap
);
3908 isl_union_map_free(umap
);
3909 isl_space_free(space
);
3913 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3914 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3915 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3916 * to the domain and the range of each map.
3917 * "res" collects the results.
3919 struct isl_union_order_at_data
{
3920 isl_multi_union_pw_aff
*mupa
;
3921 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3922 __isl_take isl_multi_pw_aff
*mpa2
);
3926 /* Intersect "map" with the result of applying data->order to
3927 * the functions in data->mupa that apply to the domain and the range
3928 * of "map" and add the result to data->res.
3930 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
3932 struct isl_union_order_at_data
*data
= user
;
3934 isl_multi_pw_aff
*mpa1
, *mpa2
;
3937 space
= isl_space_domain(isl_map_get_space(map
));
3938 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3939 space
= isl_space_range(isl_map_get_space(map
));
3940 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3941 order
= data
->order(mpa1
, mpa2
);
3942 map
= isl_map_intersect(map
, order
);
3943 data
->res
= isl_union_map_add_map(data
->res
, map
);
3945 return data
->res
? isl_stat_ok
: isl_stat_error
;
3948 /* If "mupa" has a non-trivial explicit domain, then intersect
3949 * domain and range of "umap" with this explicit domain.
3950 * If the explicit domain only describes constraints on the parameters,
3951 * then the intersection only needs to be performed once.
3953 static __isl_give isl_union_map
*intersect_explicit_domain(
3954 __isl_take isl_union_map
*umap
, __isl_keep isl_multi_union_pw_aff
*mupa
)
3956 isl_bool non_trivial
, is_params
;
3959 non_trivial
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa
);
3960 if (non_trivial
< 0)
3961 return isl_union_map_free(umap
);
3964 mupa
= isl_multi_union_pw_aff_copy(mupa
);
3965 dom
= isl_multi_union_pw_aff_domain(mupa
);
3966 is_params
= isl_union_set_is_params(dom
);
3967 if (is_params
< 0) {
3968 isl_union_set_free(dom
);
3969 return isl_union_map_free(umap
);
3974 set
= isl_union_set_params(dom
);
3975 umap
= isl_union_map_intersect_params(umap
, set
);
3978 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(dom
));
3979 umap
= isl_union_map_intersect_range(umap
, dom
);
3983 /* Intersect each map in "umap" with the result of calling "order"
3984 * on the functions is "mupa" that apply to the domain and the range
3987 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
3988 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
3989 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3990 __isl_take isl_multi_pw_aff
*mpa2
))
3992 struct isl_union_order_at_data data
;
3994 umap
= isl_union_map_align_params(umap
,
3995 isl_multi_union_pw_aff_get_space(mupa
));
3996 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
3997 isl_union_map_get_space(umap
));
3998 umap
= intersect_explicit_domain(umap
, mupa
);
4001 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
4002 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
4003 data
.res
= isl_union_map_free(data
.res
);
4005 isl_multi_union_pw_aff_free(mupa
);
4006 isl_union_map_free(umap
);
4010 /* Return the subset of "umap" where the domain and the range
4011 * have equal "mupa" values.
4013 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
4014 __isl_take isl_union_map
*umap
,
4015 __isl_take isl_multi_union_pw_aff
*mupa
)
4017 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4018 &isl_multi_pw_aff_eq_map
);
4021 /* Return the subset of "umap" where the domain has a lexicographically
4022 * smaller "mupa" value than the range.
4024 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
4025 __isl_take isl_union_map
*umap
,
4026 __isl_take isl_multi_union_pw_aff
*mupa
)
4028 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4029 &isl_multi_pw_aff_lex_lt_map
);
4032 /* Return the subset of "umap" where the domain has a lexicographically
4033 * greater "mupa" value than the range.
4035 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
4036 __isl_take isl_union_map
*umap
,
4037 __isl_take isl_multi_union_pw_aff
*mupa
)
4039 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4040 &isl_multi_pw_aff_lex_gt_map
);
4043 /* Return the union of the elements in the list "list".
4045 __isl_give isl_union_set
*isl_union_set_list_union(
4046 __isl_take isl_union_set_list
*list
)
4056 ctx
= isl_union_set_list_get_ctx(list
);
4057 space
= isl_space_params_alloc(ctx
, 0);
4058 res
= isl_union_set_empty(space
);
4060 n
= isl_union_set_list_n_union_set(list
);
4061 for (i
= 0; i
< n
; ++i
) {
4062 isl_union_set
*uset_i
;
4064 uset_i
= isl_union_set_list_get_union_set(list
, i
);
4065 res
= isl_union_set_union(res
, uset_i
);
4068 isl_union_set_list_free(list
);
4072 /* Update *hash with the hash value of "map".
4074 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
4076 uint32_t *hash
= user
;
4079 map_hash
= isl_map_get_hash(map
);
4080 isl_hash_hash(*hash
, map_hash
);
4086 /* Return a hash value that digests "umap".
4088 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
4095 hash
= isl_hash_init();
4096 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
4102 /* Return a hash value that digests "uset".
4104 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
4106 return isl_union_map_get_hash(uset
);
4109 /* Add the number of basic sets in "set" to "n".
4111 static isl_stat
add_n(__isl_take isl_set
*set
, void *user
)
4115 *n
+= isl_set_n_basic_set(set
);
4121 /* Return the total number of basic sets in "uset".
4123 int isl_union_set_n_basic_set(__isl_keep isl_union_set
*uset
)
4127 if (isl_union_set_foreach_set(uset
, &add_n
, &n
) < 0)
4133 /* Add the basic sets in "set" to "list".
4135 static isl_stat
add_list(__isl_take isl_set
*set
, void *user
)
4137 isl_basic_set_list
**list
= user
;
4138 isl_basic_set_list
*list_i
;
4140 list_i
= isl_set_get_basic_set_list(set
);
4141 *list
= isl_basic_set_list_concat(*list
, list_i
);
4145 return isl_stat_error
;
4149 /* Return a list containing all the basic sets in "uset".
4151 * First construct a list of the appropriate size and
4152 * then add all the elements.
4154 __isl_give isl_basic_set_list
*isl_union_set_get_basic_set_list(
4155 __isl_keep isl_union_set
*uset
)
4159 isl_basic_set_list
*list
;
4163 ctx
= isl_union_set_get_ctx(uset
);
4164 n
= isl_union_set_n_basic_set(uset
);
4167 list
= isl_basic_set_list_alloc(ctx
, n
);
4168 if (isl_union_set_foreach_set(uset
, &add_list
, &list
) < 0)
4169 list
= isl_basic_set_list_free(list
);
4174 /* Internal data structure for isl_union_map_remove_map_if.
4175 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4177 struct isl_union_map_remove_map_if_data
{
4178 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
4182 /* isl_un_op_control filter that negates the result of data->fn
4185 static isl_bool
not(__isl_keep isl_map
*map
, void *user
)
4187 struct isl_union_map_remove_map_if_data
*data
= user
;
4189 return isl_bool_not(data
->fn(map
, data
->user
));
4192 /* Dummy isl_un_op_control transformation callback that
4193 * simply returns the input.
4195 static __isl_give isl_map
*map_id(__isl_take isl_map
*map
)
4200 /* Call "fn" on every map in "umap" and remove those maps
4201 * for which the callback returns true.
4203 * Use un_op to keep only those maps that are not filtered out,
4204 * applying an identity transformation on them.
4206 __isl_give isl_union_map
*isl_union_map_remove_map_if(
4207 __isl_take isl_union_map
*umap
,
4208 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
4210 struct isl_union_map_remove_map_if_data data
= { fn
, user
};
4211 struct isl_un_op_control control
= {
4213 .filter_user
= &data
,
4216 return un_op(umap
, &control
);