2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 #include <isl_map_private.h>
19 #include <isl_space_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl/union_set.h>
22 #include <isl/deprecated/union_map_int.h>
24 /* Return the number of parameters of "umap", where "type"
25 * is required to be set to isl_dim_param.
27 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
28 enum isl_dim_type type
)
33 if (type
!= isl_dim_param
)
34 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
35 "can only reference parameters", return 0);
37 return isl_space_dim(umap
->dim
, type
);
40 /* Return the number of parameters of "uset", where "type"
41 * is required to be set to isl_dim_param.
43 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
44 enum isl_dim_type type
)
46 return isl_union_map_dim(uset
, type
);
49 /* Return the id of the specified dimension.
51 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
52 enum isl_dim_type type
, unsigned pos
)
57 if (type
!= isl_dim_param
)
58 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
59 "can only reference parameters", return NULL
);
61 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
64 /* Is this union set a parameter domain?
66 int isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
73 if (uset
->table
.n
!= 1)
76 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
77 params
= isl_set_is_params(set
);
82 static __isl_give isl_union_map
*isl_union_map_alloc(
83 __isl_take isl_space
*space
, int size
)
87 space
= isl_space_params(space
);
91 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
93 isl_space_free(space
);
99 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
100 return isl_union_map_free(umap
);
105 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
107 return isl_union_map_alloc(dim
, 16);
110 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
112 return isl_union_map_empty(dim
);
115 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
117 return umap
? umap
->dim
->ctx
: NULL
;
120 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
122 return uset
? uset
->dim
->ctx
: NULL
;
125 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
129 return isl_space_copy(umap
->dim
);
132 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
134 return isl_union_map_get_space(uset
);
137 static int free_umap_entry(void **entry
, void *user
)
139 isl_map
*map
= *entry
;
144 static int add_map(__isl_take isl_map
*map
, void *user
)
146 isl_union_map
**umap
= (isl_union_map
**)user
;
148 *umap
= isl_union_map_add_map(*umap
, map
);
153 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
160 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
161 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
165 isl_union_map_free(dup
);
169 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
177 return isl_union_map_dup(umap
);
180 struct isl_union_align
{
185 static int align_entry(void **entry
, void *user
)
187 isl_map
*map
= *entry
;
189 struct isl_union_align
*data
= user
;
191 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
192 isl_map_get_space(map
));
194 data
->res
= isl_union_map_add_map(data
->res
,
195 isl_map_realign(isl_map_copy(map
), exp
));
200 /* Align the parameters of umap along those of model.
201 * The result has the parameters of model first, in the same order
202 * as they appear in model, followed by any remaining parameters of
203 * umap that do not appear in model.
205 __isl_give isl_union_map
*isl_union_map_align_params(
206 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
208 struct isl_union_align data
= { NULL
, NULL
};
213 if (isl_space_match(umap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
214 isl_space_free(model
);
218 model
= isl_space_params(model
);
219 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
223 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
225 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
226 &align_entry
, &data
) < 0)
229 isl_reordering_free(data
.exp
);
230 isl_union_map_free(umap
);
231 isl_space_free(model
);
234 isl_reordering_free(data
.exp
);
235 isl_union_map_free(umap
);
236 isl_union_map_free(data
.res
);
237 isl_space_free(model
);
241 __isl_give isl_union_set
*isl_union_set_align_params(
242 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
244 return isl_union_map_align_params(uset
, model
);
247 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
248 __isl_take isl_union_map
*umap2
)
250 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
251 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
253 umap1
= isl_union_map_cow(umap1
);
255 if (!umap1
|| !umap2
)
258 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
261 isl_union_map_free(umap2
);
265 isl_union_map_free(umap1
);
266 isl_union_map_free(umap2
);
270 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
271 __isl_take isl_union_set
*uset2
)
273 return isl_union_map_union(uset1
, uset2
);
276 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
285 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
287 return isl_union_map_copy(uset
);
290 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
298 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
299 &free_umap_entry
, NULL
);
300 isl_hash_table_clear(&umap
->table
);
301 isl_space_free(umap
->dim
);
306 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
308 return isl_union_map_free(uset
);
311 static int has_dim(const void *entry
, const void *val
)
313 isl_map
*map
= (isl_map
*)entry
;
314 isl_space
*dim
= (isl_space
*)val
;
316 return isl_space_is_equal(map
->dim
, dim
);
319 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
320 __isl_take isl_map
*map
)
323 struct isl_hash_table_entry
*entry
;
328 if (isl_map_plain_is_empty(map
)) {
333 if (!isl_space_match(map
->dim
, isl_dim_param
, umap
->dim
, isl_dim_param
)) {
334 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
335 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
338 umap
= isl_union_map_cow(umap
);
343 hash
= isl_space_get_hash(map
->dim
);
344 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
345 &has_dim
, map
->dim
, 1);
352 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
361 isl_union_map_free(umap
);
365 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
366 __isl_take isl_set
*set
)
368 return isl_union_map_add_map(uset
, (isl_map
*)set
);
371 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
379 dim
= isl_map_get_space(map
);
380 dim
= isl_space_params(dim
);
381 umap
= isl_union_map_empty(dim
);
382 umap
= isl_union_map_add_map(umap
, map
);
387 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
389 return isl_union_map_from_map((isl_map
*)set
);
392 __isl_give isl_union_map
*isl_union_map_from_basic_map(
393 __isl_take isl_basic_map
*bmap
)
395 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
398 __isl_give isl_union_set
*isl_union_set_from_basic_set(
399 __isl_take isl_basic_set
*bset
)
401 return isl_union_map_from_basic_map(bset
);
404 struct isl_union_map_foreach_data
406 int (*fn
)(__isl_take isl_map
*map
, void *user
);
410 static int call_on_copy(void **entry
, void *user
)
412 isl_map
*map
= *entry
;
413 struct isl_union_map_foreach_data
*data
;
414 data
= (struct isl_union_map_foreach_data
*)user
;
416 return data
->fn(isl_map_copy(map
), data
->user
);
419 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
421 return umap
? umap
->table
.n
: 0;
424 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
426 return uset
? uset
->table
.n
: 0;
429 int isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
430 int (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
432 struct isl_union_map_foreach_data data
= { fn
, user
};
437 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
438 &call_on_copy
, &data
);
441 static int copy_map(void **entry
, void *user
)
443 isl_map
*map
= *entry
;
444 isl_map
**map_p
= user
;
446 *map_p
= isl_map_copy(map
);
451 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
458 ctx
= isl_union_map_get_ctx(umap
);
459 if (umap
->table
.n
!= 1)
460 isl_die(ctx
, isl_error_invalid
,
461 "union map needs to contain elements in exactly "
462 "one space", goto error
);
464 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
466 isl_union_map_free(umap
);
470 isl_union_map_free(umap
);
474 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
476 return isl_map_from_union_map(uset
);
479 /* Extract the map in "umap" that lives in the given space (ignoring
482 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
483 __isl_take isl_space
*space
)
486 struct isl_hash_table_entry
*entry
;
488 space
= isl_space_drop_dims(space
, isl_dim_param
,
489 0, isl_space_dim(space
, isl_dim_param
));
490 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
494 hash
= isl_space_get_hash(space
);
495 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
498 return isl_map_empty(space
);
499 isl_space_free(space
);
500 return isl_map_copy(entry
->data
);
502 isl_space_free(space
);
506 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
507 __isl_take isl_space
*dim
)
509 return (isl_set
*)isl_union_map_extract_map(uset
, dim
);
512 /* Check if umap contains a map in the given space.
514 __isl_give
int isl_union_map_contains(__isl_keep isl_union_map
*umap
,
515 __isl_keep isl_space
*dim
)
518 struct isl_hash_table_entry
*entry
;
523 hash
= isl_space_get_hash(dim
);
524 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
529 __isl_give
int isl_union_set_contains(__isl_keep isl_union_set
*uset
,
530 __isl_keep isl_space
*dim
)
532 return isl_union_map_contains(uset
, dim
);
535 int isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
536 int (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
538 return isl_union_map_foreach_map(uset
,
539 (int(*)(__isl_take isl_map
*, void*))fn
, user
);
542 struct isl_union_set_foreach_point_data
{
543 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
547 static int foreach_point(__isl_take isl_set
*set
, void *user
)
549 struct isl_union_set_foreach_point_data
*data
= user
;
552 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
558 int isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
559 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
561 struct isl_union_set_foreach_point_data data
= { fn
, user
};
562 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
565 struct isl_union_map_gen_bin_data
{
566 isl_union_map
*umap2
;
570 static int subtract_entry(void **entry
, void *user
)
572 struct isl_union_map_gen_bin_data
*data
= user
;
574 struct isl_hash_table_entry
*entry2
;
575 isl_map
*map
= *entry
;
577 hash
= isl_space_get_hash(map
->dim
);
578 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
579 hash
, &has_dim
, map
->dim
, 0);
580 map
= isl_map_copy(map
);
583 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
585 empty
= isl_map_is_empty(map
);
595 data
->res
= isl_union_map_add_map(data
->res
, map
);
600 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
601 __isl_take isl_union_map
*umap2
, int (*fn
)(void **, void *))
603 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
605 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
606 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
608 if (!umap1
|| !umap2
)
612 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
614 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
618 isl_union_map_free(umap1
);
619 isl_union_map_free(umap2
);
622 isl_union_map_free(umap1
);
623 isl_union_map_free(umap2
);
624 isl_union_map_free(data
.res
);
628 __isl_give isl_union_map
*isl_union_map_subtract(
629 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
631 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
634 __isl_give isl_union_set
*isl_union_set_subtract(
635 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
637 return isl_union_map_subtract(uset1
, uset2
);
640 struct isl_union_map_gen_bin_set_data
{
645 static int intersect_params_entry(void **entry
, void *user
)
647 struct isl_union_map_gen_bin_set_data
*data
= user
;
648 isl_map
*map
= *entry
;
651 map
= isl_map_copy(map
);
652 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
654 empty
= isl_map_is_empty(map
);
660 data
->res
= isl_union_map_add_map(data
->res
, map
);
665 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
666 __isl_take isl_set
*set
, int (*fn
)(void **, void *))
668 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
670 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
671 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
677 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
679 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
683 isl_union_map_free(umap
);
687 isl_union_map_free(umap
);
689 isl_union_map_free(data
.res
);
693 __isl_give isl_union_map
*isl_union_map_intersect_params(
694 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
696 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
699 __isl_give isl_union_set
*isl_union_set_intersect_params(
700 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
702 return isl_union_map_intersect_params(uset
, set
);
705 static __isl_give isl_union_map
*union_map_intersect_params(
706 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
708 return isl_union_map_intersect_params(umap
,
709 isl_set_from_union_set(uset
));
712 static __isl_give isl_union_map
*union_map_gist_params(
713 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
715 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
718 struct isl_union_map_match_bin_data
{
719 isl_union_map
*umap2
;
721 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
724 static int match_bin_entry(void **entry
, void *user
)
726 struct isl_union_map_match_bin_data
*data
= user
;
728 struct isl_hash_table_entry
*entry2
;
729 isl_map
*map
= *entry
;
732 hash
= isl_space_get_hash(map
->dim
);
733 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
734 hash
, &has_dim
, map
->dim
, 0);
738 map
= isl_map_copy(map
);
739 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
741 empty
= isl_map_is_empty(map
);
751 data
->res
= isl_union_map_add_map(data
->res
, map
);
756 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
757 __isl_take isl_union_map
*umap2
,
758 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
760 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
762 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
763 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
765 if (!umap1
|| !umap2
)
769 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
771 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
772 &match_bin_entry
, &data
) < 0)
775 isl_union_map_free(umap1
);
776 isl_union_map_free(umap2
);
779 isl_union_map_free(umap1
);
780 isl_union_map_free(umap2
);
781 isl_union_map_free(data
.res
);
785 __isl_give isl_union_map
*isl_union_map_intersect(
786 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
788 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
791 /* Compute the intersection of the two union_sets.
792 * As a special case, if exactly one of the two union_sets
793 * is a parameter domain, then intersect the parameter domain
794 * of the other one with this set.
796 __isl_give isl_union_set
*isl_union_set_intersect(
797 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
801 p1
= isl_union_set_is_params(uset1
);
802 p2
= isl_union_set_is_params(uset2
);
803 if (p1
< 0 || p2
< 0)
806 return union_map_intersect_params(uset1
, uset2
);
808 return union_map_intersect_params(uset2
, uset1
);
809 return isl_union_map_intersect(uset1
, uset2
);
811 isl_union_set_free(uset1
);
812 isl_union_set_free(uset2
);
816 static int gist_params_entry(void **entry
, void *user
)
818 struct isl_union_map_gen_bin_set_data
*data
= user
;
819 isl_map
*map
= *entry
;
822 map
= isl_map_copy(map
);
823 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
825 empty
= isl_map_is_empty(map
);
831 data
->res
= isl_union_map_add_map(data
->res
, map
);
836 __isl_give isl_union_map
*isl_union_map_gist_params(
837 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
839 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
842 __isl_give isl_union_set
*isl_union_set_gist_params(
843 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
845 return isl_union_map_gist_params(uset
, set
);
848 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
849 __isl_take isl_union_map
*context
)
851 return match_bin_op(umap
, context
, &isl_map_gist
);
854 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
855 __isl_take isl_union_set
*context
)
857 if (isl_union_set_is_params(context
))
858 return union_map_gist_params(uset
, context
);
859 return isl_union_map_gist(uset
, context
);
862 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
863 __isl_take isl_map
*set2
)
865 return isl_set_lex_le_set((isl_set
*)set1
, (isl_set
*)set2
);
868 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
869 __isl_take isl_map
*set2
)
871 return isl_set_lex_lt_set((isl_set
*)set1
, (isl_set
*)set2
);
874 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
875 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
877 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
880 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
881 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
883 return match_bin_op(uset1
, uset2
, &lex_le_set
);
886 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
887 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
889 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
892 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
893 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
895 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
898 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
899 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
901 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
904 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
905 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
907 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
910 static int intersect_domain_entry(void **entry
, void *user
)
912 struct isl_union_map_gen_bin_data
*data
= user
;
914 struct isl_hash_table_entry
*entry2
;
916 isl_map
*map
= *entry
;
919 dim
= isl_map_get_space(map
);
920 dim
= isl_space_domain(dim
);
921 hash
= isl_space_get_hash(dim
);
922 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
923 hash
, &has_dim
, dim
, 0);
928 map
= isl_map_copy(map
);
929 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
931 empty
= isl_map_is_empty(map
);
941 data
->res
= isl_union_map_add_map(data
->res
, map
);
946 /* Intersect the domain of "umap" with "uset".
947 * If "uset" is a parameters domain, then intersect the parameter
948 * domain of "umap" with this set.
950 __isl_give isl_union_map
*isl_union_map_intersect_domain(
951 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
953 if (isl_union_set_is_params(uset
))
954 return union_map_intersect_params(umap
, uset
);
955 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
958 /* Remove the elements of data->umap2 from the domain of *entry
959 * and add the result to data->res.
961 static int subtract_domain_entry(void **entry
, void *user
)
963 struct isl_union_map_gen_bin_data
*data
= user
;
965 struct isl_hash_table_entry
*entry2
;
967 isl_map
*map
= *entry
;
970 dim
= isl_map_get_space(map
);
971 dim
= isl_space_domain(dim
);
972 hash
= isl_space_get_hash(dim
);
973 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
974 hash
, &has_dim
, dim
, 0);
977 map
= isl_map_copy(map
);
980 data
->res
= isl_union_map_add_map(data
->res
, map
);
984 map
= isl_map_subtract_domain(map
, isl_set_copy(entry2
->data
));
986 empty
= isl_map_is_empty(map
);
996 data
->res
= isl_union_map_add_map(data
->res
, map
);
1001 /* Remove the elements of "uset" from the domain of "umap".
1003 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1004 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1006 return gen_bin_op(umap
, dom
, &subtract_domain_entry
);
1009 /* Remove the elements of data->umap2 from the range of *entry
1010 * and add the result to data->res.
1012 static int subtract_range_entry(void **entry
, void *user
)
1014 struct isl_union_map_gen_bin_data
*data
= user
;
1016 struct isl_hash_table_entry
*entry2
;
1018 isl_map
*map
= *entry
;
1021 space
= isl_map_get_space(map
);
1022 space
= isl_space_range(space
);
1023 hash
= isl_space_get_hash(space
);
1024 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1025 hash
, &has_dim
, space
, 0);
1026 isl_space_free(space
);
1028 map
= isl_map_copy(map
);
1031 data
->res
= isl_union_map_add_map(data
->res
, map
);
1035 map
= isl_map_subtract_range(map
, isl_set_copy(entry2
->data
));
1037 empty
= isl_map_is_empty(map
);
1047 data
->res
= isl_union_map_add_map(data
->res
, map
);
1052 /* Remove the elements of "uset" from the range of "umap".
1054 __isl_give isl_union_map
*isl_union_map_subtract_range(
1055 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1057 return gen_bin_op(umap
, dom
, &subtract_range_entry
);
1060 static int gist_domain_entry(void **entry
, void *user
)
1062 struct isl_union_map_gen_bin_data
*data
= user
;
1064 struct isl_hash_table_entry
*entry2
;
1066 isl_map
*map
= *entry
;
1069 dim
= isl_map_get_space(map
);
1070 dim
= isl_space_domain(dim
);
1071 hash
= isl_space_get_hash(dim
);
1072 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1073 hash
, &has_dim
, dim
, 0);
1074 isl_space_free(dim
);
1078 map
= isl_map_copy(map
);
1079 map
= isl_map_gist_domain(map
, isl_set_copy(entry2
->data
));
1081 empty
= isl_map_is_empty(map
);
1087 data
->res
= isl_union_map_add_map(data
->res
, map
);
1092 /* Compute the gist of "umap" with respect to the domain "uset".
1093 * If "uset" is a parameters domain, then compute the gist
1094 * with respect to this parameter domain.
1096 __isl_give isl_union_map
*isl_union_map_gist_domain(
1097 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1099 if (isl_union_set_is_params(uset
))
1100 return union_map_gist_params(umap
, uset
);
1101 return gen_bin_op(umap
, uset
, &gist_domain_entry
);
1104 static int gist_range_entry(void **entry
, void *user
)
1106 struct isl_union_map_gen_bin_data
*data
= user
;
1108 struct isl_hash_table_entry
*entry2
;
1110 isl_map
*map
= *entry
;
1113 space
= isl_map_get_space(map
);
1114 space
= isl_space_range(space
);
1115 hash
= isl_space_get_hash(space
);
1116 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1117 hash
, &has_dim
, space
, 0);
1118 isl_space_free(space
);
1122 map
= isl_map_copy(map
);
1123 map
= isl_map_gist_range(map
, isl_set_copy(entry2
->data
));
1125 empty
= isl_map_is_empty(map
);
1131 data
->res
= isl_union_map_add_map(data
->res
, map
);
1136 /* Compute the gist of "umap" with respect to the range "uset".
1138 __isl_give isl_union_map
*isl_union_map_gist_range(
1139 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1141 return gen_bin_op(umap
, uset
, &gist_range_entry
);
1144 static int intersect_range_entry(void **entry
, void *user
)
1146 struct isl_union_map_gen_bin_data
*data
= user
;
1148 struct isl_hash_table_entry
*entry2
;
1150 isl_map
*map
= *entry
;
1153 dim
= isl_map_get_space(map
);
1154 dim
= isl_space_range(dim
);
1155 hash
= isl_space_get_hash(dim
);
1156 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1157 hash
, &has_dim
, dim
, 0);
1158 isl_space_free(dim
);
1162 map
= isl_map_copy(map
);
1163 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
1165 empty
= isl_map_is_empty(map
);
1175 data
->res
= isl_union_map_add_map(data
->res
, map
);
1180 __isl_give isl_union_map
*isl_union_map_intersect_range(
1181 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1183 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
1186 struct isl_union_map_bin_data
{
1187 isl_union_map
*umap2
;
1190 int (*fn
)(void **entry
, void *user
);
1193 static int apply_range_entry(void **entry
, void *user
)
1195 struct isl_union_map_bin_data
*data
= user
;
1196 isl_map
*map2
= *entry
;
1199 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1200 map2
->dim
, isl_dim_in
))
1203 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1205 empty
= isl_map_is_empty(map2
);
1215 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1220 static int bin_entry(void **entry
, void *user
)
1222 struct isl_union_map_bin_data
*data
= user
;
1223 isl_map
*map
= *entry
;
1226 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1227 data
->fn
, data
) < 0)
1233 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1234 __isl_take isl_union_map
*umap2
, int (*fn
)(void **entry
, void *user
))
1236 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1238 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1239 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1241 if (!umap1
|| !umap2
)
1245 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1247 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1248 &bin_entry
, &data
) < 0)
1251 isl_union_map_free(umap1
);
1252 isl_union_map_free(umap2
);
1255 isl_union_map_free(umap1
);
1256 isl_union_map_free(umap2
);
1257 isl_union_map_free(data
.res
);
1261 __isl_give isl_union_map
*isl_union_map_apply_range(
1262 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1264 return bin_op(umap1
, umap2
, &apply_range_entry
);
1267 __isl_give isl_union_map
*isl_union_map_apply_domain(
1268 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1270 umap1
= isl_union_map_reverse(umap1
);
1271 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1272 return isl_union_map_reverse(umap1
);
1275 __isl_give isl_union_set
*isl_union_set_apply(
1276 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1278 return isl_union_map_apply_range(uset
, umap
);
1281 static int map_lex_lt_entry(void **entry
, void *user
)
1283 struct isl_union_map_bin_data
*data
= user
;
1284 isl_map
*map2
= *entry
;
1286 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1287 map2
->dim
, isl_dim_out
))
1290 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1292 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1297 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1298 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1300 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1303 static int map_lex_le_entry(void **entry
, void *user
)
1305 struct isl_union_map_bin_data
*data
= user
;
1306 isl_map
*map2
= *entry
;
1308 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1309 map2
->dim
, isl_dim_out
))
1312 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1314 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1319 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1320 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1322 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1325 static int product_entry(void **entry
, void *user
)
1327 struct isl_union_map_bin_data
*data
= user
;
1328 isl_map
*map2
= *entry
;
1330 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1332 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1337 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1338 __isl_take isl_union_map
*umap2
)
1340 return bin_op(umap1
, umap2
, &product_entry
);
1343 static int set_product_entry(void **entry
, void *user
)
1345 struct isl_union_map_bin_data
*data
= user
;
1346 isl_set
*set2
= *entry
;
1348 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1350 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1355 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1356 __isl_take isl_union_set
*uset2
)
1358 return bin_op(uset1
, uset2
, &set_product_entry
);
1361 static int domain_product_entry(void **entry
, void *user
)
1363 struct isl_union_map_bin_data
*data
= user
;
1364 isl_map
*map2
= *entry
;
1366 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1367 map2
->dim
, isl_dim_out
))
1370 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1371 isl_map_copy(map2
));
1373 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1378 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1380 __isl_give isl_union_map
*isl_union_map_domain_product(
1381 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1383 return bin_op(umap1
, umap2
, &domain_product_entry
);
1386 static int range_product_entry(void **entry
, void *user
)
1388 struct isl_union_map_bin_data
*data
= user
;
1389 isl_map
*map2
= *entry
;
1391 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1392 map2
->dim
, isl_dim_in
))
1395 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1396 isl_map_copy(map2
));
1398 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1403 __isl_give isl_union_map
*isl_union_map_range_product(
1404 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1406 return bin_op(umap1
, umap2
, &range_product_entry
);
1409 static int flat_range_product_entry(void **entry
, void *user
)
1411 struct isl_union_map_bin_data
*data
= user
;
1412 isl_map
*map2
= *entry
;
1414 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1415 map2
->dim
, isl_dim_in
))
1418 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1419 isl_map_copy(map2
));
1421 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1426 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1427 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1429 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1432 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1433 int (*fn
)(void **, void *))
1440 res
= isl_union_map_alloc(isl_space_copy(umap
->dim
), umap
->table
.n
);
1441 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1444 isl_union_map_free(umap
);
1447 isl_union_map_free(umap
);
1448 isl_union_set_free(res
);
1452 static int from_range_entry(void **entry
, void *user
)
1454 isl_map
*set
= *entry
;
1455 isl_union_set
**res
= user
;
1457 *res
= isl_union_map_add_map(*res
,
1458 isl_map_from_range(isl_set_copy(set
)));
1463 __isl_give isl_union_map
*isl_union_map_from_range(
1464 __isl_take isl_union_set
*uset
)
1466 return cond_un_op(uset
, &from_range_entry
);
1469 __isl_give isl_union_map
*isl_union_map_from_domain(
1470 __isl_take isl_union_set
*uset
)
1472 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1475 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1476 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1478 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1479 isl_union_map_from_range(range
));
1482 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1483 int (*fn
)(void **, void *))
1485 umap
= isl_union_map_cow(umap
);
1489 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1494 isl_union_map_free(umap
);
1498 static int affine_entry(void **entry
, void *user
)
1500 isl_map
**map
= (isl_map
**)entry
;
1502 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1504 return *map
? 0 : -1;
1507 __isl_give isl_union_map
*isl_union_map_affine_hull(
1508 __isl_take isl_union_map
*umap
)
1510 return un_op(umap
, &affine_entry
);
1513 __isl_give isl_union_set
*isl_union_set_affine_hull(
1514 __isl_take isl_union_set
*uset
)
1516 return isl_union_map_affine_hull(uset
);
1519 static int polyhedral_entry(void **entry
, void *user
)
1521 isl_map
**map
= (isl_map
**)entry
;
1523 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1525 return *map
? 0 : -1;
1528 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1529 __isl_take isl_union_map
*umap
)
1531 return un_op(umap
, &polyhedral_entry
);
1534 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1535 __isl_take isl_union_set
*uset
)
1537 return isl_union_map_polyhedral_hull(uset
);
1540 static int simple_entry(void **entry
, void *user
)
1542 isl_map
**map
= (isl_map
**)entry
;
1544 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1546 return *map
? 0 : -1;
1549 __isl_give isl_union_map
*isl_union_map_simple_hull(
1550 __isl_take isl_union_map
*umap
)
1552 return un_op(umap
, &simple_entry
);
1555 __isl_give isl_union_set
*isl_union_set_simple_hull(
1556 __isl_take isl_union_set
*uset
)
1558 return isl_union_map_simple_hull(uset
);
1561 static int inplace_entry(void **entry
, void *user
)
1563 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1564 isl_map
**map
= (isl_map
**)entry
;
1567 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1568 copy
= fn(isl_map_copy(*map
));
1578 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1579 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1584 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1585 &inplace_entry
, &fn
) < 0)
1590 isl_union_map_free(umap
);
1594 __isl_give isl_union_map
*isl_union_map_coalesce(
1595 __isl_take isl_union_map
*umap
)
1597 return inplace(umap
, &isl_map_coalesce
);
1600 __isl_give isl_union_set
*isl_union_set_coalesce(
1601 __isl_take isl_union_set
*uset
)
1603 return isl_union_map_coalesce(uset
);
1606 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1607 __isl_take isl_union_map
*umap
)
1609 return inplace(umap
, &isl_map_detect_equalities
);
1612 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1613 __isl_take isl_union_set
*uset
)
1615 return isl_union_map_detect_equalities(uset
);
1618 __isl_give isl_union_map
*isl_union_map_compute_divs(
1619 __isl_take isl_union_map
*umap
)
1621 return inplace(umap
, &isl_map_compute_divs
);
1624 __isl_give isl_union_set
*isl_union_set_compute_divs(
1625 __isl_take isl_union_set
*uset
)
1627 return isl_union_map_compute_divs(uset
);
1630 static int lexmin_entry(void **entry
, void *user
)
1632 isl_map
**map
= (isl_map
**)entry
;
1634 *map
= isl_map_lexmin(*map
);
1636 return *map
? 0 : -1;
1639 __isl_give isl_union_map
*isl_union_map_lexmin(
1640 __isl_take isl_union_map
*umap
)
1642 return un_op(umap
, &lexmin_entry
);
1645 __isl_give isl_union_set
*isl_union_set_lexmin(
1646 __isl_take isl_union_set
*uset
)
1648 return isl_union_map_lexmin(uset
);
1651 static int lexmax_entry(void **entry
, void *user
)
1653 isl_map
**map
= (isl_map
**)entry
;
1655 *map
= isl_map_lexmax(*map
);
1657 return *map
? 0 : -1;
1660 __isl_give isl_union_map
*isl_union_map_lexmax(
1661 __isl_take isl_union_map
*umap
)
1663 return un_op(umap
, &lexmax_entry
);
1666 __isl_give isl_union_set
*isl_union_set_lexmax(
1667 __isl_take isl_union_set
*uset
)
1669 return isl_union_map_lexmax(uset
);
1672 static int universe_entry(void **entry
, void *user
)
1674 isl_map
*map
= *entry
;
1675 isl_union_map
**res
= user
;
1677 map
= isl_map_universe(isl_map_get_space(map
));
1678 *res
= isl_union_map_add_map(*res
, map
);
1683 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1685 return cond_un_op(umap
, &universe_entry
);
1688 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1690 return isl_union_map_universe(uset
);
1693 static int reverse_entry(void **entry
, void *user
)
1695 isl_map
*map
= *entry
;
1696 isl_union_map
**res
= user
;
1698 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1703 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1705 return cond_un_op(umap
, &reverse_entry
);
1708 static int params_entry(void **entry
, void *user
)
1710 isl_map
*map
= *entry
;
1711 isl_union_set
**res
= user
;
1713 *res
= isl_union_set_add_set(*res
, isl_map_params(isl_map_copy(map
)));
1718 /* Compute the parameter domain of the given union map.
1720 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1724 empty
= isl_union_map_is_empty(umap
);
1729 space
= isl_union_map_get_space(umap
);
1730 isl_union_map_free(umap
);
1731 return isl_set_empty(space
);
1733 return isl_set_from_union_set(cond_un_op(umap
, ¶ms_entry
));
1735 isl_union_map_free(umap
);
1739 /* Compute the parameter domain of the given union set.
1741 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1743 return isl_union_map_params(uset
);
1746 static int domain_entry(void **entry
, void *user
)
1748 isl_map
*map
= *entry
;
1749 isl_union_set
**res
= user
;
1751 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1756 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1758 return cond_un_op(umap
, &domain_entry
);
1761 static int range_entry(void **entry
, void *user
)
1763 isl_map
*map
= *entry
;
1764 isl_union_set
**res
= user
;
1766 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1771 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1773 return cond_un_op(umap
, &range_entry
);
1776 static int domain_map_entry(void **entry
, void *user
)
1778 isl_map
*map
= *entry
;
1779 isl_union_set
**res
= user
;
1781 *res
= isl_union_map_add_map(*res
,
1782 isl_map_domain_map(isl_map_copy(map
)));
1787 __isl_give isl_union_map
*isl_union_map_domain_map(
1788 __isl_take isl_union_map
*umap
)
1790 return cond_un_op(umap
, &domain_map_entry
);
1793 static int range_map_entry(void **entry
, void *user
)
1795 isl_map
*map
= *entry
;
1796 isl_union_set
**res
= user
;
1798 *res
= isl_union_map_add_map(*res
,
1799 isl_map_range_map(isl_map_copy(map
)));
1804 __isl_give isl_union_map
*isl_union_map_range_map(
1805 __isl_take isl_union_map
*umap
)
1807 return cond_un_op(umap
, &range_map_entry
);
1810 static int deltas_entry(void **entry
, void *user
)
1812 isl_map
*map
= *entry
;
1813 isl_union_set
**res
= user
;
1815 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1816 map
->dim
, isl_dim_out
))
1819 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1824 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1826 return cond_un_op(umap
, &deltas_entry
);
1829 static int deltas_map_entry(void **entry
, void *user
)
1831 isl_map
*map
= *entry
;
1832 isl_union_map
**res
= user
;
1834 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1835 map
->dim
, isl_dim_out
))
1838 *res
= isl_union_map_add_map(*res
,
1839 isl_map_deltas_map(isl_map_copy(map
)));
1844 __isl_give isl_union_map
*isl_union_map_deltas_map(
1845 __isl_take isl_union_map
*umap
)
1847 return cond_un_op(umap
, &deltas_map_entry
);
1850 static int identity_entry(void **entry
, void *user
)
1852 isl_set
*set
= *entry
;
1853 isl_union_map
**res
= user
;
1855 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
1860 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1862 return cond_un_op(uset
, &identity_entry
);
1865 static int unwrap_entry(void **entry
, void *user
)
1867 isl_set
*set
= *entry
;
1868 isl_union_set
**res
= user
;
1870 if (!isl_set_is_wrapping(set
))
1873 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
1878 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
1880 return cond_un_op(uset
, &unwrap_entry
);
1883 static int wrap_entry(void **entry
, void *user
)
1885 isl_map
*map
= *entry
;
1886 isl_union_set
**res
= user
;
1888 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
1893 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
1895 return cond_un_op(umap
, &wrap_entry
);
1898 struct isl_union_map_is_subset_data
{
1899 isl_union_map
*umap2
;
1903 static int is_subset_entry(void **entry
, void *user
)
1905 struct isl_union_map_is_subset_data
*data
= user
;
1907 struct isl_hash_table_entry
*entry2
;
1908 isl_map
*map
= *entry
;
1910 hash
= isl_space_get_hash(map
->dim
);
1911 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1912 hash
, &has_dim
, map
->dim
, 0);
1914 int empty
= isl_map_is_empty(map
);
1919 data
->is_subset
= 0;
1923 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
1924 if (data
->is_subset
< 0 || !data
->is_subset
)
1930 int isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
1931 __isl_keep isl_union_map
*umap2
)
1933 struct isl_union_map_is_subset_data data
= { NULL
, 1 };
1935 umap1
= isl_union_map_copy(umap1
);
1936 umap2
= isl_union_map_copy(umap2
);
1937 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1938 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1940 if (!umap1
|| !umap2
)
1944 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1945 &is_subset_entry
, &data
) < 0 &&
1949 isl_union_map_free(umap1
);
1950 isl_union_map_free(umap2
);
1952 return data
.is_subset
;
1954 isl_union_map_free(umap1
);
1955 isl_union_map_free(umap2
);
1959 int isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
1960 __isl_keep isl_union_set
*uset2
)
1962 return isl_union_map_is_subset(uset1
, uset2
);
1965 int isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
1966 __isl_keep isl_union_map
*umap2
)
1970 if (!umap1
|| !umap2
)
1972 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1975 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1979 int isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
1980 __isl_keep isl_union_set
*uset2
)
1982 return isl_union_map_is_equal(uset1
, uset2
);
1985 int isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
1986 __isl_keep isl_union_map
*umap2
)
1990 if (!umap1
|| !umap2
)
1992 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
1995 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
1996 if (is_subset
== -1)
2001 int isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2002 __isl_keep isl_union_set
*uset2
)
2004 return isl_union_map_is_strict_subset(uset1
, uset2
);
2007 static int sample_entry(void **entry
, void *user
)
2009 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2010 isl_map
*map
= *entry
;
2012 *sample
= isl_map_sample(isl_map_copy(map
));
2015 if (!isl_basic_map_plain_is_empty(*sample
))
2020 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2022 isl_basic_map
*sample
= NULL
;
2027 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2028 &sample_entry
, &sample
) < 0 &&
2033 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2035 isl_union_map_free(umap
);
2039 isl_union_map_free(umap
);
2043 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2045 return (isl_basic_set
*)isl_union_map_sample(uset
);
2048 struct isl_forall_data
{
2050 int (*fn
)(__isl_keep isl_map
*map
);
2053 static int forall_entry(void **entry
, void *user
)
2055 struct isl_forall_data
*data
= user
;
2056 isl_map
*map
= *entry
;
2058 data
->res
= data
->fn(map
);
2068 static int union_map_forall(__isl_keep isl_union_map
*umap
,
2069 int (*fn
)(__isl_keep isl_map
*map
))
2071 struct isl_forall_data data
= { 1, fn
};
2076 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2077 &forall_entry
, &data
) < 0 && data
.res
)
2083 struct isl_forall_user_data
{
2085 int (*fn
)(__isl_keep isl_map
*map
, void *user
);
2089 static int forall_user_entry(void **entry
, void *user
)
2091 struct isl_forall_user_data
*data
= user
;
2092 isl_map
*map
= *entry
;
2094 data
->res
= data
->fn(map
, data
->user
);
2104 /* Check if fn(map, user) returns true for all maps "map" in umap.
2106 static int union_map_forall_user(__isl_keep isl_union_map
*umap
,
2107 int (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2109 struct isl_forall_user_data data
= { 1, fn
, user
};
2114 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2115 &forall_user_entry
, &data
) < 0 && data
.res
)
2121 int isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2123 return union_map_forall(umap
, &isl_map_is_empty
);
2126 int isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2128 return isl_union_map_is_empty(uset
);
2131 static int is_subset_of_identity(__isl_keep isl_map
*map
)
2140 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2141 map
->dim
, isl_dim_out
))
2144 dim
= isl_map_get_space(map
);
2145 id
= isl_map_identity(dim
);
2147 is_subset
= isl_map_is_subset(map
, id
);
2154 /* Given an isl_union_map that consists of a single map, check
2155 * if it is single-valued.
2157 static int single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2162 umap
= isl_union_map_copy(umap
);
2163 map
= isl_map_from_union_map(umap
);
2164 sv
= isl_map_is_single_valued(map
);
2170 /* Internal data structure for single_valued_on_domain.
2172 * "umap" is the union map to be tested.
2173 * "sv" is set to 1 as long as "umap" may still be single-valued.
2175 struct isl_union_map_is_sv_data
{
2176 isl_union_map
*umap
;
2180 /* Check if the data->umap is single-valued on "set".
2182 * If data->umap consists of a single map on "set", then test it
2185 * Otherwise, compute
2189 * check if the result is a subset of the identity mapping and
2190 * store the result in data->sv.
2192 * Terminate as soon as data->umap has been determined not to
2195 static int single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2197 struct isl_union_map_is_sv_data
*data
= user
;
2198 isl_union_map
*umap
, *test
;
2200 umap
= isl_union_map_copy(data
->umap
);
2201 umap
= isl_union_map_intersect_domain(umap
,
2202 isl_union_set_from_set(set
));
2204 if (isl_union_map_n_map(umap
) == 1) {
2205 data
->sv
= single_map_is_single_valued(umap
);
2206 isl_union_map_free(umap
);
2208 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2209 test
= isl_union_map_apply_range(test
, umap
);
2211 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2213 isl_union_map_free(test
);
2216 if (data
->sv
< 0 || !data
->sv
)
2221 /* Check if the given map is single-valued.
2223 * If the union map consists of a single map, then test it as an isl_map.
2224 * Otherwise, check if the union map is single-valued on each of its
2227 int isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2229 isl_union_map
*universe
;
2230 isl_union_set
*domain
;
2231 struct isl_union_map_is_sv_data data
;
2233 if (isl_union_map_n_map(umap
) == 1)
2234 return single_map_is_single_valued(umap
);
2236 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2237 domain
= isl_union_map_domain(universe
);
2241 if (isl_union_set_foreach_set(domain
,
2242 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2244 isl_union_set_free(domain
);
2249 int isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2253 umap
= isl_union_map_copy(umap
);
2254 umap
= isl_union_map_reverse(umap
);
2255 in
= isl_union_map_is_single_valued(umap
);
2256 isl_union_map_free(umap
);
2261 /* Represents a map that has a fixed value (v) for one of its
2263 * The map in this structure is not reference counted, so it
2264 * is only valid while the isl_union_map from which it was
2265 * obtained is still alive.
2267 struct isl_fixed_map
{
2272 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2276 struct isl_fixed_map
*v
;
2278 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2281 for (i
= 0; i
< n
; ++i
)
2282 isl_int_init(v
[i
].v
);
2286 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2292 for (i
= 0; i
< n
; ++i
)
2293 isl_int_clear(v
[i
].v
);
2297 /* Compare the "v" field of two isl_fixed_map structs.
2299 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2301 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2302 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2304 return isl_int_cmp(e1
->v
, e2
->v
);
2307 /* Internal data structure used while checking whether all maps
2308 * in a union_map have a fixed value for a given output dimension.
2309 * v is the list of maps, with the fixed value for the dimension
2310 * n is the number of maps considered so far
2311 * pos is the output dimension under investigation
2313 struct isl_fixed_dim_data
{
2314 struct isl_fixed_map
*v
;
2319 static int fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2321 struct isl_fixed_dim_data
*data
= user
;
2323 data
->v
[data
->n
].map
= map
;
2324 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2325 &data
->v
[data
->n
++].v
);
2328 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
2329 int first
, int n_range
);
2331 /* Given a list of the maps, with their fixed values at output dimension "pos",
2332 * check whether the ranges of the maps form an obvious partition.
2334 * We first sort the maps according to their fixed values.
2335 * If all maps have a different value, then we know the ranges form
2337 * Otherwise, we collect the maps with the same fixed value and
2338 * check whether each such collection is obviously injective
2339 * based on later dimensions.
2341 static int separates(struct isl_fixed_map
*v
, int n
,
2342 __isl_take isl_space
*dim
, int pos
, int n_range
)
2349 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2351 for (i
= 0; i
+ 1 < n
; ++i
) {
2353 isl_union_map
*part
;
2356 for (j
= i
+ 1; j
< n
; ++j
)
2357 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2363 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2364 for (k
= i
; k
< j
; ++k
)
2365 part
= isl_union_map_add_map(part
,
2366 isl_map_copy(v
[k
].map
));
2368 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2377 isl_space_free(dim
);
2378 free_isl_fixed_map_array(v
, n
);
2381 isl_space_free(dim
);
2382 free_isl_fixed_map_array(v
, n
);
2386 /* Check whether the maps in umap have obviously distinct ranges.
2387 * In particular, check for an output dimension in the range
2388 * [first,n_range) for which all maps have a fixed value
2389 * and then check if these values, possibly along with fixed values
2390 * at later dimensions, entail distinct ranges.
2392 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
2393 int first
, int n_range
)
2397 struct isl_fixed_dim_data data
= { NULL
};
2399 ctx
= isl_union_map_get_ctx(umap
);
2401 n
= isl_union_map_n_map(umap
);
2406 isl_union_map_free(umap
);
2410 if (first
>= n_range
) {
2411 isl_union_map_free(umap
);
2415 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2419 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2425 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2430 dim
= isl_union_map_get_space(umap
);
2431 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2432 isl_union_map_free(umap
);
2436 free_isl_fixed_map_array(data
.v
, n
);
2437 isl_union_map_free(umap
);
2441 free_isl_fixed_map_array(data
.v
, n
);
2442 isl_union_map_free(umap
);
2446 /* Check whether the maps in umap that map to subsets of "ran"
2447 * have obviously distinct ranges.
2449 static int plain_injective_on_range_wrap(__isl_keep isl_set
*ran
, void *user
)
2451 isl_union_map
*umap
= user
;
2453 umap
= isl_union_map_copy(umap
);
2454 umap
= isl_union_map_intersect_range(umap
,
2455 isl_union_set_from_set(isl_set_copy(ran
)));
2456 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2459 /* Check if the given union_map is obviously injective.
2461 * In particular, we first check if all individual maps are obviously
2462 * injective and then check if all the ranges of these maps are
2463 * obviously disjoint.
2465 int isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2468 isl_union_map
*univ
;
2471 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2477 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2478 ran
= isl_union_map_range(univ
);
2480 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2482 isl_union_set_free(ran
);
2487 int isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2491 sv
= isl_union_map_is_single_valued(umap
);
2495 return isl_union_map_is_injective(umap
);
2498 static int zip_entry(void **entry
, void *user
)
2500 isl_map
*map
= *entry
;
2501 isl_union_map
**res
= user
;
2503 if (!isl_map_can_zip(map
))
2506 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
2511 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2513 return cond_un_op(umap
, &zip_entry
);
2516 static int uncurry_entry(void **entry
, void *user
)
2518 isl_map
*map
= *entry
;
2519 isl_union_map
**res
= user
;
2521 if (!isl_map_can_uncurry(map
))
2524 *res
= isl_union_map_add_map(*res
, isl_map_uncurry(isl_map_copy(map
)));
2529 /* Given a union map, take the maps of the form A -> (B -> C) and
2530 * return the union of the corresponding maps (A -> B) -> C.
2532 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
2534 return cond_un_op(umap
, &uncurry_entry
);
2537 static int curry_entry(void **entry
, void *user
)
2539 isl_map
*map
= *entry
;
2540 isl_union_map
**res
= user
;
2542 if (!isl_map_can_curry(map
))
2545 *res
= isl_union_map_add_map(*res
, isl_map_curry(isl_map_copy(map
)));
2550 /* Given a union map, take the maps of the form (A -> B) -> C and
2551 * return the union of the corresponding maps A -> (B -> C).
2553 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
2555 return cond_un_op(umap
, &curry_entry
);
2558 static int lift_entry(void **entry
, void *user
)
2560 isl_set
*set
= *entry
;
2561 isl_union_set
**res
= user
;
2563 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
2568 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
2570 return cond_un_op(uset
, &lift_entry
);
2573 static int coefficients_entry(void **entry
, void *user
)
2575 isl_set
*set
= *entry
;
2576 isl_union_set
**res
= user
;
2578 set
= isl_set_copy(set
);
2579 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
2580 *res
= isl_union_set_add_set(*res
, set
);
2585 __isl_give isl_union_set
*isl_union_set_coefficients(
2586 __isl_take isl_union_set
*uset
)
2595 ctx
= isl_union_set_get_ctx(uset
);
2596 dim
= isl_space_set_alloc(ctx
, 0, 0);
2597 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2598 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2599 &coefficients_entry
, &res
) < 0)
2602 isl_union_set_free(uset
);
2605 isl_union_set_free(uset
);
2606 isl_union_set_free(res
);
2610 static int solutions_entry(void **entry
, void *user
)
2612 isl_set
*set
= *entry
;
2613 isl_union_set
**res
= user
;
2615 set
= isl_set_copy(set
);
2616 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2618 *res
= isl_union_set_from_set(set
);
2620 *res
= isl_union_set_add_set(*res
, set
);
2628 __isl_give isl_union_set
*isl_union_set_solutions(
2629 __isl_take isl_union_set
*uset
)
2631 isl_union_set
*res
= NULL
;
2636 if (uset
->table
.n
== 0) {
2637 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2638 isl_union_set_free(uset
);
2642 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2643 &solutions_entry
, &res
) < 0)
2646 isl_union_set_free(uset
);
2649 isl_union_set_free(uset
);
2650 isl_union_set_free(res
);
2654 /* Is the domain space of "map" equal to "space"?
2656 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2658 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2659 space
, isl_dim_out
);
2662 /* Is the range space of "map" equal to "space"?
2664 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2666 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
2667 space
, isl_dim_out
);
2670 /* Is the set space of "map" equal to "space"?
2672 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2674 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
2675 space
, isl_dim_out
);
2678 /* Internal data structure for preimage_pw_multi_aff.
2680 * "pma" is the function under which the preimage should be taken.
2681 * "space" is the space of "pma".
2682 * "res" collects the results.
2683 * "fn" computes the preimage for a given map.
2684 * "match" returns true if "fn" can be called.
2686 struct isl_union_map_preimage_data
{
2688 isl_pw_multi_aff
*pma
;
2690 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
2691 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
2692 __isl_take isl_pw_multi_aff
*pma
);
2695 /* Call data->fn to compute the preimage of the domain or range of *entry
2696 * under the function represented by data->pma, provided the domain/range
2697 * space of *entry matches the target space of data->pma
2698 * (as given by data->match), and add the result to data->res.
2700 static int preimage_entry(void **entry
, void *user
)
2703 isl_map
*map
= *entry
;
2704 struct isl_union_map_preimage_data
*data
= user
;
2707 m
= data
->match(map
, data
->space
);
2713 map
= isl_map_copy(map
);
2714 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
2716 empty
= isl_map_is_empty(map
);
2717 if (empty
< 0 || empty
) {
2719 return empty
< 0 ? -1 : 0;
2722 data
->res
= isl_union_map_add_map(data
->res
, map
);
2727 /* Compute the preimage of the domain or range of "umap" under the function
2728 * represented by "pma".
2729 * In other words, plug in "pma" in the domain or range of "umap".
2730 * The function "fn" performs the actual preimage computation on a map,
2731 * while "match" determines to which maps the function should be applied.
2733 static __isl_give isl_union_map
*preimage_pw_multi_aff(
2734 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
2735 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
2736 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
2737 __isl_take isl_pw_multi_aff
*pma
))
2741 struct isl_union_map_preimage_data data
;
2743 umap
= isl_union_map_align_params(umap
,
2744 isl_pw_multi_aff_get_space(pma
));
2745 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
2750 ctx
= isl_union_map_get_ctx(umap
);
2751 space
= isl_union_map_get_space(umap
);
2752 data
.space
= isl_pw_multi_aff_get_space(pma
);
2754 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
2757 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
2759 data
.res
= isl_union_map_free(data
.res
);
2761 isl_space_free(data
.space
);
2762 isl_union_map_free(umap
);
2763 isl_pw_multi_aff_free(pma
);
2766 isl_union_map_free(umap
);
2767 isl_pw_multi_aff_free(pma
);
2771 /* Compute the preimage of the domain of "umap" under the function
2772 * represented by "pma".
2773 * In other words, plug in "pma" in the domain of "umap".
2774 * The result contains maps that live in the same spaces as the maps of "umap"
2775 * with domain space equal to the target space of "pma",
2776 * except that the domain has been replaced by the domain space of "pma".
2778 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
2779 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
2781 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
2782 &isl_map_preimage_domain_pw_multi_aff
);
2785 /* Compute the preimage of the range of "umap" under the function
2786 * represented by "pma".
2787 * In other words, plug in "pma" in the range of "umap".
2788 * The result contains maps that live in the same spaces as the maps of "umap"
2789 * with range space equal to the target space of "pma",
2790 * except that the range has been replaced by the domain space of "pma".
2792 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
2793 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
2795 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
2796 &isl_map_preimage_range_pw_multi_aff
);
2799 /* Compute the preimage of "uset" under the function represented by "pma".
2800 * In other words, plug in "pma" in "uset".
2801 * The result contains sets that live in the same spaces as the sets of "uset"
2802 * with space equal to the target space of "pma",
2803 * except that the space has been replaced by the domain space of "pma".
2805 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
2806 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
2808 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
2809 &isl_set_preimage_pw_multi_aff
);
2812 /* Compute the preimage of the domain of "umap" under the function
2813 * represented by "ma".
2814 * In other words, plug in "ma" in the domain of "umap".
2815 * The result contains maps that live in the same spaces as the maps of "umap"
2816 * with domain space equal to the target space of "ma",
2817 * except that the domain has been replaced by the domain space of "ma".
2819 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
2820 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
2822 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
2823 isl_pw_multi_aff_from_multi_aff(ma
));
2826 /* Compute the preimage of the range of "umap" under the function
2827 * represented by "ma".
2828 * In other words, plug in "ma" in the range of "umap".
2829 * The result contains maps that live in the same spaces as the maps of "umap"
2830 * with range space equal to the target space of "ma",
2831 * except that the range has been replaced by the domain space of "ma".
2833 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
2834 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
2836 return isl_union_map_preimage_range_pw_multi_aff(umap
,
2837 isl_pw_multi_aff_from_multi_aff(ma
));
2840 /* Compute the preimage of "uset" under the function represented by "ma".
2841 * In other words, plug in "ma" in "uset".
2842 * The result contains sets that live in the same spaces as the sets of "uset"
2843 * with space equal to the target space of "ma",
2844 * except that the space has been replaced by the domain space of "ma".
2846 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
2847 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
2849 return isl_union_set_preimage_pw_multi_aff(uset
,
2850 isl_pw_multi_aff_from_multi_aff(ma
));
2853 /* Internal data structure for preimage_multi_pw_aff.
2855 * "mpa" is the function under which the preimage should be taken.
2856 * "space" is the space of "mpa".
2857 * "res" collects the results.
2858 * "fn" computes the preimage for a given map.
2859 * "match" returns true if "fn" can be called.
2861 struct isl_union_map_preimage_mpa_data
{
2863 isl_multi_pw_aff
*mpa
;
2865 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
2866 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
2867 __isl_take isl_multi_pw_aff
*mpa
);
2870 /* Call data->fn to compute the preimage of the domain or range of *entry
2871 * under the function represented by data->mpa, provided the domain/range
2872 * space of *entry matches the target space of data->mpa
2873 * (as given by data->match), and add the result to data->res.
2875 static int preimage_mpa_entry(void **entry
, void *user
)
2878 isl_map
*map
= *entry
;
2879 struct isl_union_map_preimage_mpa_data
*data
= user
;
2882 m
= data
->match(map
, data
->space
);
2888 map
= isl_map_copy(map
);
2889 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
2891 empty
= isl_map_is_empty(map
);
2892 if (empty
< 0 || empty
) {
2894 return empty
< 0 ? -1 : 0;
2897 data
->res
= isl_union_map_add_map(data
->res
, map
);
2902 /* Compute the preimage of the domain or range of "umap" under the function
2903 * represented by "mpa".
2904 * In other words, plug in "mpa" in the domain or range of "umap".
2905 * The function "fn" performs the actual preimage computation on a map,
2906 * while "match" determines to which maps the function should be applied.
2908 static __isl_give isl_union_map
*preimage_multi_pw_aff(
2909 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
2910 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
2911 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
2912 __isl_take isl_multi_pw_aff
*mpa
))
2916 struct isl_union_map_preimage_mpa_data data
;
2918 umap
= isl_union_map_align_params(umap
,
2919 isl_multi_pw_aff_get_space(mpa
));
2920 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
2925 ctx
= isl_union_map_get_ctx(umap
);
2926 space
= isl_union_map_get_space(umap
);
2927 data
.space
= isl_multi_pw_aff_get_space(mpa
);
2929 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
2932 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
2934 data
.res
= isl_union_map_free(data
.res
);
2936 isl_space_free(data
.space
);
2937 isl_union_map_free(umap
);
2938 isl_multi_pw_aff_free(mpa
);
2941 isl_union_map_free(umap
);
2942 isl_multi_pw_aff_free(mpa
);
2946 /* Compute the preimage of the domain of "umap" under the function
2947 * represented by "mpa".
2948 * In other words, plug in "mpa" in the domain of "umap".
2949 * The result contains maps that live in the same spaces as the maps of "umap"
2950 * with domain space equal to the target space of "mpa",
2951 * except that the domain has been replaced by the domain space of "mpa".
2953 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
2954 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
2956 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
2957 &isl_map_preimage_domain_multi_pw_aff
);
2960 /* Internal data structure for preimage_upma.
2962 * "umap" is the map of which the preimage should be computed.
2963 * "res" collects the results.
2964 * "fn" computes the preimage for a given piecewise multi-affine function.
2966 struct isl_union_map_preimage_upma_data
{
2967 isl_union_map
*umap
;
2969 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
2970 __isl_take isl_pw_multi_aff
*pma
);
2973 /* Call data->fn to compute the preimage of the domain or range of data->umap
2974 * under the function represented by pma and add the result to data->res.
2976 static int preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
2978 struct isl_union_map_preimage_upma_data
*data
= user
;
2979 isl_union_map
*umap
;
2981 umap
= isl_union_map_copy(data
->umap
);
2982 umap
= data
->fn(umap
, pma
);
2983 data
->res
= isl_union_map_union(data
->res
, umap
);
2985 return data
->res
? 0 : -1;
2988 /* Compute the preimage of the domain or range of "umap" under the function
2989 * represented by "upma".
2990 * In other words, plug in "upma" in the domain or range of "umap".
2991 * The function "fn" performs the actual preimage computation
2992 * on a piecewise multi-affine function.
2994 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
2995 __isl_take isl_union_map
*umap
,
2996 __isl_take isl_union_pw_multi_aff
*upma
,
2997 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
2998 __isl_take isl_pw_multi_aff
*pma
))
3000 struct isl_union_map_preimage_upma_data data
;
3003 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3005 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3006 &preimage_upma
, &data
) < 0)
3007 data
.res
= isl_union_map_free(data
.res
);
3009 isl_union_map_free(umap
);
3010 isl_union_pw_multi_aff_free(upma
);
3015 /* Compute the preimage of the domain of "umap" under the function
3016 * represented by "upma".
3017 * In other words, plug in "upma" in the domain of "umap".
3018 * The result contains maps that live in the same spaces as the maps of "umap"
3019 * with domain space equal to one of the target spaces of "upma",
3020 * except that the domain has been replaced by one of the the domain spaces that
3021 * corresponds to that target space of "upma".
3023 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3024 __isl_take isl_union_map
*umap
,
3025 __isl_take isl_union_pw_multi_aff
*upma
)
3027 return preimage_union_pw_multi_aff(umap
, upma
,
3028 &isl_union_map_preimage_domain_pw_multi_aff
);
3031 /* Compute the preimage of the range of "umap" under the function
3032 * represented by "upma".
3033 * In other words, plug in "upma" in the range of "umap".
3034 * The result contains maps that live in the same spaces as the maps of "umap"
3035 * with range space equal to one of the target spaces of "upma",
3036 * except that the range has been replaced by one of the the domain spaces that
3037 * corresponds to that target space of "upma".
3039 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3040 __isl_take isl_union_map
*umap
,
3041 __isl_take isl_union_pw_multi_aff
*upma
)
3043 return preimage_union_pw_multi_aff(umap
, upma
,
3044 &isl_union_map_preimage_range_pw_multi_aff
);
3047 /* Compute the preimage of "uset" under the function represented by "upma".
3048 * In other words, plug in "upma" in the range of "uset".
3049 * The result contains sets that live in the same spaces as the sets of "uset"
3050 * with space equal to one of the target spaces of "upma",
3051 * except that the space has been replaced by one of the the domain spaces that
3052 * corresponds to that target space of "upma".
3054 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3055 __isl_take isl_union_set
*uset
,
3056 __isl_take isl_union_pw_multi_aff
*upma
)
3058 return preimage_union_pw_multi_aff(uset
, upma
,
3059 &isl_union_set_preimage_pw_multi_aff
);
3062 /* Reset the user pointer on all identifiers of parameters and tuples
3063 * of the space of *entry.
3065 static int reset_user(void **entry
, void *user
)
3067 isl_map
**map
= (isl_map
**)entry
;
3069 *map
= isl_map_reset_user(*map
);
3071 return *map
? 0 : -1;
3074 /* Reset the user pointer on all identifiers of parameters and tuples
3075 * of the spaces of "umap".
3077 __isl_give isl_union_map
*isl_union_map_reset_user(
3078 __isl_take isl_union_map
*umap
)
3080 umap
= isl_union_map_cow(umap
);
3083 umap
->dim
= isl_space_reset_user(umap
->dim
);
3085 return isl_union_map_free(umap
);
3086 umap
= un_op(umap
, &reset_user
);
3091 /* Reset the user pointer on all identifiers of parameters and tuples
3092 * of the spaces of "uset".
3094 __isl_give isl_union_set
*isl_union_set_reset_user(
3095 __isl_take isl_union_set
*uset
)
3097 return isl_union_map_reset_user(uset
);
3100 /* Internal data structure for isl_union_map_project_out.
3101 * "type", "first" and "n" are the arguments for the isl_map_project_out
3103 * "res" collects the results.
3105 struct isl_union_map_project_out_data
{
3106 enum isl_dim_type type
;
3113 /* Turn the data->n dimensions of type data->type, starting at data->first
3114 * into existentially quantified variables and add the result to data->res.
3116 static int project_out(__isl_take isl_map
*map
, void *user
)
3118 struct isl_union_map_project_out_data
*data
= user
;
3120 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3121 data
->res
= isl_union_map_add_map(data
->res
, map
);
3126 /* Turn the "n" dimensions of type "type", starting at "first"
3127 * into existentially quantified variables.
3128 * Since the space of an isl_union_map only contains parameters,
3129 * type is required to be equal to isl_dim_param.
3131 __isl_give isl_union_map
*isl_union_map_project_out(
3132 __isl_take isl_union_map
*umap
,
3133 enum isl_dim_type type
, unsigned first
, unsigned n
)
3136 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3141 if (type
!= isl_dim_param
)
3142 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3143 "can only project out parameters",
3144 return isl_union_map_free(umap
));
3146 space
= isl_union_map_get_space(umap
);
3147 space
= isl_space_drop_dims(space
, type
, first
, n
);
3148 data
.res
= isl_union_map_empty(space
);
3149 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3150 data
.res
= isl_union_map_free(data
.res
);
3152 isl_union_map_free(umap
);