2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016 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
17 #include <isl_map_private.h>
18 #include <isl_union_map_private.h>
24 #include <isl_space_private.h>
25 #include <isl/union_set.h>
26 #include <isl/deprecated/union_map_int.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
32 /* Return the number of parameters of "umap", where "type"
33 * is required to be set to isl_dim_param.
35 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
36 enum isl_dim_type type
)
41 if (type
!= isl_dim_param
)
42 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
43 "can only reference parameters", return 0);
45 return isl_space_dim(umap
->dim
, type
);
48 /* Return the number of parameters of "uset", where "type"
49 * is required to be set to isl_dim_param.
51 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
52 enum isl_dim_type type
)
54 return isl_union_map_dim(uset
, type
);
57 /* Return the id of the specified dimension.
59 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
60 enum isl_dim_type type
, unsigned pos
)
65 if (type
!= isl_dim_param
)
66 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
67 "can only reference parameters", return NULL
);
69 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
72 /* Is this union set a parameter domain?
74 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
80 return isl_bool_error
;
81 if (uset
->table
.n
!= 1)
82 return isl_bool_false
;
84 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
85 params
= isl_set_is_params(set
);
90 static __isl_give isl_union_map
*isl_union_map_alloc(
91 __isl_take isl_space
*space
, int size
)
95 space
= isl_space_params(space
);
99 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
101 isl_space_free(space
);
107 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
108 return isl_union_map_free(umap
);
113 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
115 return isl_union_map_alloc(dim
, 16);
118 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
120 return isl_union_map_empty(dim
);
123 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
125 return umap
? umap
->dim
->ctx
: NULL
;
128 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
130 return uset
? uset
->dim
->ctx
: NULL
;
133 /* Return the space of "umap".
135 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
137 return umap
? umap
->dim
: NULL
;
140 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
142 return isl_space_copy(isl_union_map_peek_space(umap
));
145 /* Return the position of the parameter with the given name
147 * Return -1 if no such dimension can be found.
149 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
150 enum isl_dim_type type
, const char *name
)
154 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
157 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
159 return isl_union_map_get_space(uset
);
162 static isl_stat
free_umap_entry(void **entry
, void *user
)
164 isl_map
*map
= *entry
;
169 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
171 isl_union_map
**umap
= (isl_union_map
**)user
;
173 *umap
= isl_union_map_add_map(*umap
, map
);
178 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
185 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
186 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
190 isl_union_map_free(dup
);
194 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
202 return isl_union_map_dup(umap
);
205 struct isl_union_align
{
210 static isl_stat
align_entry(void **entry
, void *user
)
212 isl_map
*map
= *entry
;
214 struct isl_union_align
*data
= user
;
216 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
217 isl_map_get_space(map
));
219 data
->res
= isl_union_map_add_map(data
->res
,
220 isl_map_realign(isl_map_copy(map
), exp
));
225 /* Align the parameters of umap along those of model.
226 * The result has the parameters of model first, in the same order
227 * as they appear in model, followed by any remaining parameters of
228 * umap that do not appear in model.
230 __isl_give isl_union_map
*isl_union_map_align_params(
231 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
233 struct isl_union_align data
= { NULL
, NULL
};
238 if (isl_space_match(umap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
239 isl_space_free(model
);
243 model
= isl_space_params(model
);
244 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
248 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
250 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
251 &align_entry
, &data
) < 0)
254 isl_reordering_free(data
.exp
);
255 isl_union_map_free(umap
);
256 isl_space_free(model
);
259 isl_reordering_free(data
.exp
);
260 isl_union_map_free(umap
);
261 isl_union_map_free(data
.res
);
262 isl_space_free(model
);
266 __isl_give isl_union_set
*isl_union_set_align_params(
267 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
269 return isl_union_map_align_params(uset
, model
);
272 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
273 __isl_take isl_union_map
*umap2
)
275 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
276 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
278 umap1
= isl_union_map_cow(umap1
);
280 if (!umap1
|| !umap2
)
283 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
286 isl_union_map_free(umap2
);
290 isl_union_map_free(umap1
);
291 isl_union_map_free(umap2
);
295 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
296 __isl_take isl_union_set
*uset2
)
298 return isl_union_map_union(uset1
, uset2
);
301 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
310 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
312 return isl_union_map_copy(uset
);
315 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
323 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
324 &free_umap_entry
, NULL
);
325 isl_hash_table_clear(&umap
->table
);
326 isl_space_free(umap
->dim
);
331 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
333 return isl_union_map_free(uset
);
336 static int has_dim(const void *entry
, const void *val
)
338 isl_map
*map
= (isl_map
*)entry
;
339 isl_space
*dim
= (isl_space
*)val
;
341 return isl_space_is_equal(map
->dim
, dim
);
344 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
345 __isl_take isl_map
*map
)
348 struct isl_hash_table_entry
*entry
;
353 if (isl_map_plain_is_empty(map
)) {
358 if (!isl_space_match(map
->dim
, isl_dim_param
, umap
->dim
, isl_dim_param
)) {
359 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
360 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
363 umap
= isl_union_map_cow(umap
);
368 hash
= isl_space_get_hash(map
->dim
);
369 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
370 &has_dim
, map
->dim
, 1);
377 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
386 isl_union_map_free(umap
);
390 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
391 __isl_take isl_set
*set
)
393 return isl_union_map_add_map(uset
, set_to_map(set
));
396 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
404 dim
= isl_map_get_space(map
);
405 dim
= isl_space_params(dim
);
406 umap
= isl_union_map_empty(dim
);
407 umap
= isl_union_map_add_map(umap
, map
);
412 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
414 return isl_union_map_from_map(set_to_map(set
));
417 __isl_give isl_union_map
*isl_union_map_from_basic_map(
418 __isl_take isl_basic_map
*bmap
)
420 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
423 __isl_give isl_union_set
*isl_union_set_from_basic_set(
424 __isl_take isl_basic_set
*bset
)
426 return isl_union_map_from_basic_map(bset
);
429 struct isl_union_map_foreach_data
431 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
435 static isl_stat
call_on_copy(void **entry
, void *user
)
437 isl_map
*map
= *entry
;
438 struct isl_union_map_foreach_data
*data
;
439 data
= (struct isl_union_map_foreach_data
*)user
;
441 return data
->fn(isl_map_copy(map
), data
->user
);
444 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
446 return umap
? umap
->table
.n
: 0;
449 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
451 return uset
? uset
->table
.n
: 0;
454 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
455 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
457 struct isl_union_map_foreach_data data
= { fn
, user
};
460 return isl_stat_error
;
462 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
463 &call_on_copy
, &data
);
466 static isl_stat
copy_map(void **entry
, void *user
)
468 isl_map
*map
= *entry
;
469 isl_map
**map_p
= user
;
471 *map_p
= isl_map_copy(map
);
473 return isl_stat_error
;
476 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
483 ctx
= isl_union_map_get_ctx(umap
);
484 if (umap
->table
.n
!= 1)
485 isl_die(ctx
, isl_error_invalid
,
486 "union map needs to contain elements in exactly "
487 "one space", goto error
);
489 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
491 isl_union_map_free(umap
);
495 isl_union_map_free(umap
);
499 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
501 return isl_map_from_union_map(uset
);
504 /* Extract the map in "umap" that lives in the given space (ignoring
507 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
508 __isl_take isl_space
*space
)
511 struct isl_hash_table_entry
*entry
;
513 space
= isl_space_drop_dims(space
, isl_dim_param
,
514 0, isl_space_dim(space
, isl_dim_param
));
515 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
519 hash
= isl_space_get_hash(space
);
520 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
523 return isl_map_empty(space
);
524 isl_space_free(space
);
525 return isl_map_copy(entry
->data
);
527 isl_space_free(space
);
531 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
532 __isl_take isl_space
*dim
)
534 return set_from_map(isl_union_map_extract_map(uset
, dim
));
537 /* Check if umap contains a map in the given space.
539 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
540 __isl_keep isl_space
*space
)
543 struct isl_hash_table_entry
*entry
;
546 return isl_bool_error
;
548 hash
= isl_space_get_hash(space
);
549 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
554 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
555 __isl_keep isl_space
*space
)
557 return isl_union_map_contains(uset
, space
);
560 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
561 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
563 return isl_union_map_foreach_map(uset
,
564 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
567 struct isl_union_set_foreach_point_data
{
568 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
572 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
574 struct isl_union_set_foreach_point_data
*data
= user
;
577 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
583 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
584 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
586 struct isl_union_set_foreach_point_data data
= { fn
, user
};
587 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
590 struct isl_union_map_gen_bin_data
{
591 isl_union_map
*umap2
;
595 static isl_stat
subtract_entry(void **entry
, void *user
)
597 struct isl_union_map_gen_bin_data
*data
= user
;
599 struct isl_hash_table_entry
*entry2
;
600 isl_map
*map
= *entry
;
602 hash
= isl_space_get_hash(map
->dim
);
603 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
604 hash
, &has_dim
, map
->dim
, 0);
605 map
= isl_map_copy(map
);
608 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
610 empty
= isl_map_is_empty(map
);
613 return isl_stat_error
;
620 data
->res
= isl_union_map_add_map(data
->res
, map
);
625 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
626 __isl_take isl_union_map
*umap2
, isl_stat (*fn
)(void **, void *))
628 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
630 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
631 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
633 if (!umap1
|| !umap2
)
637 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
639 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
643 isl_union_map_free(umap1
);
644 isl_union_map_free(umap2
);
647 isl_union_map_free(umap1
);
648 isl_union_map_free(umap2
);
649 isl_union_map_free(data
.res
);
653 __isl_give isl_union_map
*isl_union_map_subtract(
654 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
656 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
659 __isl_give isl_union_set
*isl_union_set_subtract(
660 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
662 return isl_union_map_subtract(uset1
, uset2
);
665 struct isl_union_map_gen_bin_set_data
{
670 static isl_stat
intersect_params_entry(void **entry
, void *user
)
672 struct isl_union_map_gen_bin_set_data
*data
= user
;
673 isl_map
*map
= *entry
;
676 map
= isl_map_copy(map
);
677 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
679 empty
= isl_map_is_empty(map
);
682 return isl_stat_error
;
685 data
->res
= isl_union_map_add_map(data
->res
, map
);
690 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
691 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
693 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
695 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
696 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
702 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
704 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
708 isl_union_map_free(umap
);
712 isl_union_map_free(umap
);
714 isl_union_map_free(data
.res
);
718 /* Intersect "umap" with the parameter domain "set".
720 * If "set" does not have any constraints, then we can return immediately.
722 __isl_give isl_union_map
*isl_union_map_intersect_params(
723 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
727 is_universe
= isl_set_plain_is_universe(set
);
735 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
737 isl_union_map_free(umap
);
742 __isl_give isl_union_set
*isl_union_set_intersect_params(
743 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
745 return isl_union_map_intersect_params(uset
, set
);
748 static __isl_give isl_union_map
*union_map_intersect_params(
749 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
751 return isl_union_map_intersect_params(umap
,
752 isl_set_from_union_set(uset
));
755 static __isl_give isl_union_map
*union_map_gist_params(
756 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
758 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
761 struct isl_union_map_match_bin_data
{
762 isl_union_map
*umap2
;
764 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
767 static isl_stat
match_bin_entry(void **entry
, void *user
)
769 struct isl_union_map_match_bin_data
*data
= user
;
771 struct isl_hash_table_entry
*entry2
;
772 isl_map
*map
= *entry
;
775 hash
= isl_space_get_hash(map
->dim
);
776 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
777 hash
, &has_dim
, map
->dim
, 0);
781 map
= isl_map_copy(map
);
782 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
784 empty
= isl_map_is_empty(map
);
787 return isl_stat_error
;
794 data
->res
= isl_union_map_add_map(data
->res
, map
);
799 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
800 __isl_take isl_union_map
*umap2
,
801 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
803 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
805 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
806 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
808 if (!umap1
|| !umap2
)
812 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
814 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
815 &match_bin_entry
, &data
) < 0)
818 isl_union_map_free(umap1
);
819 isl_union_map_free(umap2
);
822 isl_union_map_free(umap1
);
823 isl_union_map_free(umap2
);
824 isl_union_map_free(data
.res
);
828 __isl_give isl_union_map
*isl_union_map_intersect(
829 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
831 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
834 /* Compute the intersection of the two union_sets.
835 * As a special case, if exactly one of the two union_sets
836 * is a parameter domain, then intersect the parameter domain
837 * of the other one with this set.
839 __isl_give isl_union_set
*isl_union_set_intersect(
840 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
844 p1
= isl_union_set_is_params(uset1
);
845 p2
= isl_union_set_is_params(uset2
);
846 if (p1
< 0 || p2
< 0)
849 return union_map_intersect_params(uset1
, uset2
);
851 return union_map_intersect_params(uset2
, uset1
);
852 return isl_union_map_intersect(uset1
, uset2
);
854 isl_union_set_free(uset1
);
855 isl_union_set_free(uset2
);
859 static isl_stat
gist_params_entry(void **entry
, void *user
)
861 struct isl_union_map_gen_bin_set_data
*data
= user
;
862 isl_map
*map
= *entry
;
865 map
= isl_map_copy(map
);
866 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
868 empty
= isl_map_is_empty(map
);
871 return isl_stat_error
;
874 data
->res
= isl_union_map_add_map(data
->res
, map
);
879 __isl_give isl_union_map
*isl_union_map_gist_params(
880 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
882 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
885 __isl_give isl_union_set
*isl_union_set_gist_params(
886 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
888 return isl_union_map_gist_params(uset
, set
);
891 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
892 __isl_take isl_union_map
*context
)
894 return match_bin_op(umap
, context
, &isl_map_gist
);
897 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
898 __isl_take isl_union_set
*context
)
900 if (isl_union_set_is_params(context
))
901 return union_map_gist_params(uset
, context
);
902 return isl_union_map_gist(uset
, context
);
905 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
906 __isl_take isl_map
*set2
)
908 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
911 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
912 __isl_take isl_map
*set2
)
914 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
917 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
918 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
920 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
923 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
924 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
926 return match_bin_op(uset1
, uset2
, &lex_le_set
);
929 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
930 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
932 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
935 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
936 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
938 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
941 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
942 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
944 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
947 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
948 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
950 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
953 static isl_stat
intersect_domain_entry(void **entry
, void *user
)
955 struct isl_union_map_gen_bin_data
*data
= user
;
957 struct isl_hash_table_entry
*entry2
;
959 isl_map
*map
= *entry
;
962 dim
= isl_map_get_space(map
);
963 dim
= isl_space_domain(dim
);
964 hash
= isl_space_get_hash(dim
);
965 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
966 hash
, &has_dim
, dim
, 0);
971 map
= isl_map_copy(map
);
972 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
974 empty
= isl_map_is_empty(map
);
977 return isl_stat_error
;
984 data
->res
= isl_union_map_add_map(data
->res
, map
);
989 /* Intersect the domain of "umap" with "uset".
990 * If "uset" is a parameters domain, then intersect the parameter
991 * domain of "umap" with this set.
993 __isl_give isl_union_map
*isl_union_map_intersect_domain(
994 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
996 if (isl_union_set_is_params(uset
))
997 return union_map_intersect_params(umap
, uset
);
998 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
1001 /* Remove the elements of data->umap2 from the domain of *entry
1002 * and add the result to data->res.
1004 static isl_stat
subtract_domain_entry(void **entry
, void *user
)
1006 struct isl_union_map_gen_bin_data
*data
= user
;
1008 struct isl_hash_table_entry
*entry2
;
1010 isl_map
*map
= *entry
;
1013 dim
= isl_map_get_space(map
);
1014 dim
= isl_space_domain(dim
);
1015 hash
= isl_space_get_hash(dim
);
1016 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1017 hash
, &has_dim
, dim
, 0);
1018 isl_space_free(dim
);
1020 map
= isl_map_copy(map
);
1023 data
->res
= isl_union_map_add_map(data
->res
, map
);
1027 map
= isl_map_subtract_domain(map
, isl_set_copy(entry2
->data
));
1029 empty
= isl_map_is_empty(map
);
1032 return isl_stat_error
;
1039 data
->res
= isl_union_map_add_map(data
->res
, map
);
1044 /* Remove the elements of "uset" from the domain of "umap".
1046 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1047 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1049 return gen_bin_op(umap
, dom
, &subtract_domain_entry
);
1052 /* Remove the elements of data->umap2 from the range of *entry
1053 * and add the result to data->res.
1055 static isl_stat
subtract_range_entry(void **entry
, void *user
)
1057 struct isl_union_map_gen_bin_data
*data
= user
;
1059 struct isl_hash_table_entry
*entry2
;
1061 isl_map
*map
= *entry
;
1064 space
= isl_map_get_space(map
);
1065 space
= isl_space_range(space
);
1066 hash
= isl_space_get_hash(space
);
1067 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1068 hash
, &has_dim
, space
, 0);
1069 isl_space_free(space
);
1071 map
= isl_map_copy(map
);
1074 data
->res
= isl_union_map_add_map(data
->res
, map
);
1078 map
= isl_map_subtract_range(map
, isl_set_copy(entry2
->data
));
1080 empty
= isl_map_is_empty(map
);
1083 return isl_stat_error
;
1090 data
->res
= isl_union_map_add_map(data
->res
, map
);
1095 /* Remove the elements of "uset" from the range of "umap".
1097 __isl_give isl_union_map
*isl_union_map_subtract_range(
1098 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1100 return gen_bin_op(umap
, dom
, &subtract_range_entry
);
1103 static isl_stat
gist_domain_entry(void **entry
, void *user
)
1105 struct isl_union_map_gen_bin_data
*data
= user
;
1107 struct isl_hash_table_entry
*entry2
;
1109 isl_map
*map
= *entry
;
1112 dim
= isl_map_get_space(map
);
1113 dim
= isl_space_domain(dim
);
1114 hash
= isl_space_get_hash(dim
);
1115 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1116 hash
, &has_dim
, dim
, 0);
1117 isl_space_free(dim
);
1121 map
= isl_map_copy(map
);
1122 map
= isl_map_gist_domain(map
, isl_set_copy(entry2
->data
));
1124 empty
= isl_map_is_empty(map
);
1127 return isl_stat_error
;
1130 data
->res
= isl_union_map_add_map(data
->res
, map
);
1135 /* Compute the gist of "umap" with respect to the domain "uset".
1136 * If "uset" is a parameters domain, then compute the gist
1137 * with respect to this parameter domain.
1139 __isl_give isl_union_map
*isl_union_map_gist_domain(
1140 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1142 if (isl_union_set_is_params(uset
))
1143 return union_map_gist_params(umap
, uset
);
1144 return gen_bin_op(umap
, uset
, &gist_domain_entry
);
1147 static isl_stat
gist_range_entry(void **entry
, void *user
)
1149 struct isl_union_map_gen_bin_data
*data
= user
;
1151 struct isl_hash_table_entry
*entry2
;
1153 isl_map
*map
= *entry
;
1156 space
= isl_map_get_space(map
);
1157 space
= isl_space_range(space
);
1158 hash
= isl_space_get_hash(space
);
1159 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1160 hash
, &has_dim
, space
, 0);
1161 isl_space_free(space
);
1165 map
= isl_map_copy(map
);
1166 map
= isl_map_gist_range(map
, isl_set_copy(entry2
->data
));
1168 empty
= isl_map_is_empty(map
);
1171 return isl_stat_error
;
1174 data
->res
= isl_union_map_add_map(data
->res
, map
);
1179 /* Compute the gist of "umap" with respect to the range "uset".
1181 __isl_give isl_union_map
*isl_union_map_gist_range(
1182 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1184 return gen_bin_op(umap
, uset
, &gist_range_entry
);
1187 static isl_stat
intersect_range_entry(void **entry
, void *user
)
1189 struct isl_union_map_gen_bin_data
*data
= user
;
1191 struct isl_hash_table_entry
*entry2
;
1193 isl_map
*map
= *entry
;
1196 dim
= isl_map_get_space(map
);
1197 dim
= isl_space_range(dim
);
1198 hash
= isl_space_get_hash(dim
);
1199 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1200 hash
, &has_dim
, dim
, 0);
1201 isl_space_free(dim
);
1205 map
= isl_map_copy(map
);
1206 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
1208 empty
= isl_map_is_empty(map
);
1211 return isl_stat_error
;
1218 data
->res
= isl_union_map_add_map(data
->res
, map
);
1223 __isl_give isl_union_map
*isl_union_map_intersect_range(
1224 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1226 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
1229 struct isl_union_map_bin_data
{
1230 isl_union_map
*umap2
;
1233 isl_stat (*fn
)(void **entry
, void *user
);
1236 static isl_stat
apply_range_entry(void **entry
, void *user
)
1238 struct isl_union_map_bin_data
*data
= user
;
1239 isl_map
*map2
= *entry
;
1242 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1243 map2
->dim
, isl_dim_in
))
1246 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1248 empty
= isl_map_is_empty(map2
);
1251 return isl_stat_error
;
1258 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1263 static isl_stat
bin_entry(void **entry
, void *user
)
1265 struct isl_union_map_bin_data
*data
= user
;
1266 isl_map
*map
= *entry
;
1269 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1270 data
->fn
, data
) < 0)
1271 return isl_stat_error
;
1276 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1277 __isl_take isl_union_map
*umap2
,
1278 isl_stat (*fn
)(void **entry
, void *user
))
1280 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1282 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1283 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1285 if (!umap1
|| !umap2
)
1289 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1291 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1292 &bin_entry
, &data
) < 0)
1295 isl_union_map_free(umap1
);
1296 isl_union_map_free(umap2
);
1299 isl_union_map_free(umap1
);
1300 isl_union_map_free(umap2
);
1301 isl_union_map_free(data
.res
);
1305 __isl_give isl_union_map
*isl_union_map_apply_range(
1306 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1308 return bin_op(umap1
, umap2
, &apply_range_entry
);
1311 __isl_give isl_union_map
*isl_union_map_apply_domain(
1312 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1314 umap1
= isl_union_map_reverse(umap1
);
1315 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1316 return isl_union_map_reverse(umap1
);
1319 __isl_give isl_union_set
*isl_union_set_apply(
1320 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1322 return isl_union_map_apply_range(uset
, umap
);
1325 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1327 struct isl_union_map_bin_data
*data
= user
;
1328 isl_map
*map2
= *entry
;
1330 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1331 map2
->dim
, isl_dim_out
))
1334 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1336 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1341 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1342 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1344 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1347 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1349 struct isl_union_map_bin_data
*data
= user
;
1350 isl_map
*map2
= *entry
;
1352 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1353 map2
->dim
, isl_dim_out
))
1356 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1358 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1363 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1364 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1366 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1369 static isl_stat
product_entry(void **entry
, void *user
)
1371 struct isl_union_map_bin_data
*data
= user
;
1372 isl_map
*map2
= *entry
;
1374 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1376 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1381 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1382 __isl_take isl_union_map
*umap2
)
1384 return bin_op(umap1
, umap2
, &product_entry
);
1387 static isl_stat
set_product_entry(void **entry
, void *user
)
1389 struct isl_union_map_bin_data
*data
= user
;
1390 isl_set
*set2
= *entry
;
1392 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1394 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1399 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1400 __isl_take isl_union_set
*uset2
)
1402 return bin_op(uset1
, uset2
, &set_product_entry
);
1405 static isl_stat
domain_product_entry(void **entry
, void *user
)
1407 struct isl_union_map_bin_data
*data
= user
;
1408 isl_map
*map2
= *entry
;
1410 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1411 map2
->dim
, isl_dim_out
))
1414 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1415 isl_map_copy(map2
));
1417 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1422 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1424 __isl_give isl_union_map
*isl_union_map_domain_product(
1425 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1427 return bin_op(umap1
, umap2
, &domain_product_entry
);
1430 static isl_stat
range_product_entry(void **entry
, void *user
)
1432 struct isl_union_map_bin_data
*data
= user
;
1433 isl_map
*map2
= *entry
;
1435 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1436 map2
->dim
, isl_dim_in
))
1439 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1440 isl_map_copy(map2
));
1442 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1447 __isl_give isl_union_map
*isl_union_map_range_product(
1448 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1450 return bin_op(umap1
, umap2
, &range_product_entry
);
1453 /* If data->map A -> B and "map2" C -> D have the same range space,
1454 * then add (A, C) -> (B * D) to data->res.
1456 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1458 struct isl_union_map_bin_data
*data
= user
;
1459 isl_map
*map2
= *entry
;
1461 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1462 map2
->dim
, isl_dim_out
))
1465 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1466 isl_map_copy(map2
));
1468 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1473 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1475 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1476 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1478 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1481 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1483 struct isl_union_map_bin_data
*data
= user
;
1484 isl_map
*map2
= *entry
;
1486 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1487 map2
->dim
, isl_dim_in
))
1490 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1491 isl_map_copy(map2
));
1493 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1498 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1499 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1501 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1504 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1505 isl_stat (*fn
)(void **, void *))
1512 res
= isl_union_map_alloc(isl_space_copy(umap
->dim
), umap
->table
.n
);
1513 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1516 isl_union_map_free(umap
);
1519 isl_union_map_free(umap
);
1520 isl_union_set_free(res
);
1524 static isl_stat
from_range_entry(void **entry
, void *user
)
1526 isl_map
*set
= *entry
;
1527 isl_union_set
**res
= user
;
1529 *res
= isl_union_map_add_map(*res
,
1530 isl_map_from_range(isl_set_copy(set
)));
1535 __isl_give isl_union_map
*isl_union_map_from_range(
1536 __isl_take isl_union_set
*uset
)
1538 return cond_un_op(uset
, &from_range_entry
);
1541 __isl_give isl_union_map
*isl_union_map_from_domain(
1542 __isl_take isl_union_set
*uset
)
1544 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1547 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1548 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1550 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1551 isl_union_map_from_range(range
));
1554 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1555 isl_stat (*fn
)(void **, void *))
1557 umap
= isl_union_map_cow(umap
);
1561 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1566 isl_union_map_free(umap
);
1570 static isl_stat
affine_entry(void **entry
, void *user
)
1572 isl_map
**map
= (isl_map
**)entry
;
1574 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1576 return *map
? isl_stat_ok
: isl_stat_error
;
1579 __isl_give isl_union_map
*isl_union_map_affine_hull(
1580 __isl_take isl_union_map
*umap
)
1582 return un_op(umap
, &affine_entry
);
1585 __isl_give isl_union_set
*isl_union_set_affine_hull(
1586 __isl_take isl_union_set
*uset
)
1588 return isl_union_map_affine_hull(uset
);
1591 static isl_stat
polyhedral_entry(void **entry
, void *user
)
1593 isl_map
**map
= (isl_map
**)entry
;
1595 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1597 return *map
? isl_stat_ok
: isl_stat_error
;
1600 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1601 __isl_take isl_union_map
*umap
)
1603 return un_op(umap
, &polyhedral_entry
);
1606 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1607 __isl_take isl_union_set
*uset
)
1609 return isl_union_map_polyhedral_hull(uset
);
1612 static isl_stat
simple_entry(void **entry
, void *user
)
1614 isl_map
**map
= (isl_map
**)entry
;
1616 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1618 return *map
? isl_stat_ok
: isl_stat_error
;
1621 __isl_give isl_union_map
*isl_union_map_simple_hull(
1622 __isl_take isl_union_map
*umap
)
1624 return un_op(umap
, &simple_entry
);
1627 __isl_give isl_union_set
*isl_union_set_simple_hull(
1628 __isl_take isl_union_set
*uset
)
1630 return isl_union_map_simple_hull(uset
);
1633 static isl_stat
inplace_entry(void **entry
, void *user
)
1635 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1636 isl_map
**map
= (isl_map
**)entry
;
1639 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1640 copy
= fn(isl_map_copy(*map
));
1642 return isl_stat_error
;
1650 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1651 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1656 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1657 &inplace_entry
, &fn
) < 0)
1662 isl_union_map_free(umap
);
1666 /* Remove redundant constraints in each of the basic maps of "umap".
1667 * Since removing redundant constraints does not change the meaning
1668 * or the space, the operation can be performed in-place.
1670 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1671 __isl_take isl_union_map
*umap
)
1673 return inplace(umap
, &isl_map_remove_redundancies
);
1676 /* Remove redundant constraints in each of the basic sets of "uset".
1678 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1679 __isl_take isl_union_set
*uset
)
1681 return isl_union_map_remove_redundancies(uset
);
1684 __isl_give isl_union_map
*isl_union_map_coalesce(
1685 __isl_take isl_union_map
*umap
)
1687 return inplace(umap
, &isl_map_coalesce
);
1690 __isl_give isl_union_set
*isl_union_set_coalesce(
1691 __isl_take isl_union_set
*uset
)
1693 return isl_union_map_coalesce(uset
);
1696 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1697 __isl_take isl_union_map
*umap
)
1699 return inplace(umap
, &isl_map_detect_equalities
);
1702 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1703 __isl_take isl_union_set
*uset
)
1705 return isl_union_map_detect_equalities(uset
);
1708 __isl_give isl_union_map
*isl_union_map_compute_divs(
1709 __isl_take isl_union_map
*umap
)
1711 return inplace(umap
, &isl_map_compute_divs
);
1714 __isl_give isl_union_set
*isl_union_set_compute_divs(
1715 __isl_take isl_union_set
*uset
)
1717 return isl_union_map_compute_divs(uset
);
1720 static isl_stat
lexmin_entry(void **entry
, void *user
)
1722 isl_map
**map
= (isl_map
**)entry
;
1724 *map
= isl_map_lexmin(*map
);
1726 return *map
? isl_stat_ok
: isl_stat_error
;
1729 __isl_give isl_union_map
*isl_union_map_lexmin(
1730 __isl_take isl_union_map
*umap
)
1732 return un_op(umap
, &lexmin_entry
);
1735 __isl_give isl_union_set
*isl_union_set_lexmin(
1736 __isl_take isl_union_set
*uset
)
1738 return isl_union_map_lexmin(uset
);
1741 static isl_stat
lexmax_entry(void **entry
, void *user
)
1743 isl_map
**map
= (isl_map
**)entry
;
1745 *map
= isl_map_lexmax(*map
);
1747 return *map
? isl_stat_ok
: isl_stat_error
;
1750 __isl_give isl_union_map
*isl_union_map_lexmax(
1751 __isl_take isl_union_map
*umap
)
1753 return un_op(umap
, &lexmax_entry
);
1756 __isl_give isl_union_set
*isl_union_set_lexmax(
1757 __isl_take isl_union_set
*uset
)
1759 return isl_union_map_lexmax(uset
);
1762 static isl_stat
universe_entry(void **entry
, void *user
)
1764 isl_map
*map
= *entry
;
1765 isl_union_map
**res
= user
;
1767 map
= isl_map_universe(isl_map_get_space(map
));
1768 *res
= isl_union_map_add_map(*res
, map
);
1773 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1775 return cond_un_op(umap
, &universe_entry
);
1778 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1780 return isl_union_map_universe(uset
);
1783 static isl_stat
reverse_entry(void **entry
, void *user
)
1785 isl_map
*map
= *entry
;
1786 isl_union_map
**res
= user
;
1788 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1793 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1795 return cond_un_op(umap
, &reverse_entry
);
1798 static isl_stat
params_entry(void **entry
, void *user
)
1800 isl_map
*map
= *entry
;
1801 isl_union_set
**res
= user
;
1803 *res
= isl_union_set_add_set(*res
, isl_map_params(isl_map_copy(map
)));
1808 /* Compute the parameter domain of the given union map.
1810 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1814 empty
= isl_union_map_is_empty(umap
);
1819 space
= isl_union_map_get_space(umap
);
1820 isl_union_map_free(umap
);
1821 return isl_set_empty(space
);
1823 return isl_set_from_union_set(cond_un_op(umap
, ¶ms_entry
));
1825 isl_union_map_free(umap
);
1829 /* Compute the parameter domain of the given union set.
1831 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1833 return isl_union_map_params(uset
);
1836 static isl_stat
domain_entry(void **entry
, void *user
)
1838 isl_map
*map
= *entry
;
1839 isl_union_set
**res
= user
;
1841 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1846 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1848 return cond_un_op(umap
, &domain_entry
);
1851 static isl_stat
range_entry(void **entry
, void *user
)
1853 isl_map
*map
= *entry
;
1854 isl_union_set
**res
= user
;
1856 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1861 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1863 return cond_un_op(umap
, &range_entry
);
1866 static isl_stat
domain_map_entry(void **entry
, void *user
)
1868 isl_map
*map
= *entry
;
1869 isl_union_set
**res
= user
;
1871 *res
= isl_union_map_add_map(*res
,
1872 isl_map_domain_map(isl_map_copy(map
)));
1877 __isl_give isl_union_map
*isl_union_map_domain_map(
1878 __isl_take isl_union_map
*umap
)
1880 return cond_un_op(umap
, &domain_map_entry
);
1883 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1884 * add the result to "res".
1886 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
1888 isl_union_pw_multi_aff
**res
= user
;
1890 isl_pw_multi_aff
*pma
;
1892 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
1893 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
1894 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1896 return *res
? isl_stat_ok
: isl_stat_error
;
1900 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1903 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
1904 __isl_take isl_union_map
*umap
)
1906 isl_union_pw_multi_aff
*res
;
1908 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
1909 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
1910 res
= isl_union_pw_multi_aff_free(res
);
1912 isl_union_map_free(umap
);
1916 static isl_stat
range_map_entry(void **entry
, void *user
)
1918 isl_map
*map
= *entry
;
1919 isl_union_set
**res
= user
;
1921 *res
= isl_union_map_add_map(*res
,
1922 isl_map_range_map(isl_map_copy(map
)));
1927 __isl_give isl_union_map
*isl_union_map_range_map(
1928 __isl_take isl_union_map
*umap
)
1930 return cond_un_op(umap
, &range_map_entry
);
1933 /* Check if "set" is of the form A[B -> C].
1934 * If so, add A[B -> C] -> B to "res".
1936 static isl_stat
wrapped_domain_map_entry(void **entry
, void *user
)
1938 isl_set
*set
= *entry
;
1939 isl_union_set
**res
= user
;
1942 wrapping
= isl_set_is_wrapping(set
);
1944 return isl_stat_error
;
1948 *res
= isl_union_map_add_map(*res
,
1949 isl_set_wrapped_domain_map(isl_set_copy(set
)));
1954 /* Given a collection of wrapped maps of the form A[B -> C],
1955 * return the collection of maps A[B -> C] -> B.
1957 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
1958 __isl_take isl_union_set
*uset
)
1960 return cond_un_op(uset
, &wrapped_domain_map_entry
);
1963 static isl_stat
deltas_entry(void **entry
, void *user
)
1965 isl_map
*map
= *entry
;
1966 isl_union_set
**res
= user
;
1968 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1969 map
->dim
, isl_dim_out
))
1972 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1977 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1979 return cond_un_op(umap
, &deltas_entry
);
1982 static isl_stat
deltas_map_entry(void **entry
, void *user
)
1984 isl_map
*map
= *entry
;
1985 isl_union_map
**res
= user
;
1987 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1988 map
->dim
, isl_dim_out
))
1991 *res
= isl_union_map_add_map(*res
,
1992 isl_map_deltas_map(isl_map_copy(map
)));
1997 __isl_give isl_union_map
*isl_union_map_deltas_map(
1998 __isl_take isl_union_map
*umap
)
2000 return cond_un_op(umap
, &deltas_map_entry
);
2003 static isl_stat
identity_entry(void **entry
, void *user
)
2005 isl_set
*set
= *entry
;
2006 isl_union_map
**res
= user
;
2008 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
2013 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
2015 return cond_un_op(uset
, &identity_entry
);
2018 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2020 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
2022 isl_union_pw_multi_aff
**res
= user
;
2024 isl_pw_multi_aff
*pma
;
2026 space
= isl_space_map_from_set(isl_set_get_space(set
));
2027 pma
= isl_pw_multi_aff_identity(space
);
2028 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
2029 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2031 return *res
? isl_stat_ok
: isl_stat_error
;
2034 /* Return an identity function on "uset" in the form
2035 * of an isl_union_pw_multi_aff.
2037 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
2038 __isl_take isl_union_set
*uset
)
2040 isl_union_pw_multi_aff
*res
;
2042 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
2043 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
2044 res
= isl_union_pw_multi_aff_free(res
);
2046 isl_union_set_free(uset
);
2050 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
2052 static isl_stat
domain_factor_domain_entry(void **entry
, void *user
)
2054 isl_map
*map
= *entry
;
2055 isl_union_map
**res
= user
;
2057 if (!isl_map_domain_is_wrapping(map
))
2060 *res
= isl_union_map_add_map(*res
,
2061 isl_map_domain_factor_domain(isl_map_copy(map
)));
2063 return *res
? isl_stat_ok
: isl_stat_error
;
2066 /* For each map in "umap" of the form [A -> B] -> C,
2067 * construct the map A -> C and collect the results.
2069 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2070 __isl_take isl_union_map
*umap
)
2072 return cond_un_op(umap
, &domain_factor_domain_entry
);
2075 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2077 static isl_stat
domain_factor_range_entry(void **entry
, void *user
)
2079 isl_map
*map
= *entry
;
2080 isl_union_map
**res
= user
;
2082 if (!isl_map_domain_is_wrapping(map
))
2085 *res
= isl_union_map_add_map(*res
,
2086 isl_map_domain_factor_range(isl_map_copy(map
)));
2088 return *res
? isl_stat_ok
: isl_stat_error
;
2091 /* For each map in "umap" of the form [A -> B] -> C,
2092 * construct the map B -> C and collect the results.
2094 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2095 __isl_take isl_union_map
*umap
)
2097 return cond_un_op(umap
, &domain_factor_range_entry
);
2100 /* If "map" is of the form A -> [B -> C], then add A -> B to "res".
2102 static isl_stat
range_factor_domain_entry(void **entry
, void *user
)
2104 isl_map
*map
= *entry
;
2105 isl_union_map
**res
= user
;
2107 if (!isl_map_range_is_wrapping(map
))
2110 *res
= isl_union_map_add_map(*res
,
2111 isl_map_range_factor_domain(isl_map_copy(map
)));
2113 return *res
? isl_stat_ok
: isl_stat_error
;
2116 /* For each map in "umap" of the form A -> [B -> C],
2117 * construct the map A -> B and collect the results.
2119 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2120 __isl_take isl_union_map
*umap
)
2122 return cond_un_op(umap
, &range_factor_domain_entry
);
2125 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2127 static isl_stat
range_factor_range_entry(void **entry
, void *user
)
2129 isl_map
*map
= *entry
;
2130 isl_union_map
**res
= user
;
2132 if (!isl_map_range_is_wrapping(map
))
2135 *res
= isl_union_map_add_map(*res
,
2136 isl_map_range_factor_range(isl_map_copy(map
)));
2138 return *res
? isl_stat_ok
: isl_stat_error
;
2141 /* For each map in "umap" of the form A -> [B -> C],
2142 * construct the map A -> C and collect the results.
2144 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2145 __isl_take isl_union_map
*umap
)
2147 return cond_un_op(umap
, &range_factor_range_entry
);
2150 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2152 static isl_stat
factor_domain_entry(void **entry
, void *user
)
2154 isl_map
*map
= *entry
;
2155 isl_union_map
**res
= user
;
2157 if (!isl_map_domain_is_wrapping(map
) || !isl_map_range_is_wrapping(map
))
2160 *res
= isl_union_map_add_map(*res
,
2161 isl_map_factor_domain(isl_map_copy(map
)));
2163 return *res
? isl_stat_ok
: isl_stat_error
;
2166 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2167 * construct the map A -> C and collect the results.
2169 __isl_give isl_union_map
*isl_union_map_factor_domain(
2170 __isl_take isl_union_map
*umap
)
2172 return cond_un_op(umap
, &factor_domain_entry
);
2175 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2177 static isl_stat
factor_range_entry(void **entry
, void *user
)
2179 isl_map
*map
= *entry
;
2180 isl_union_map
**res
= user
;
2182 if (!isl_map_domain_is_wrapping(map
) || !isl_map_range_is_wrapping(map
))
2185 *res
= isl_union_map_add_map(*res
,
2186 isl_map_factor_range(isl_map_copy(map
)));
2188 return *res
? isl_stat_ok
: isl_stat_error
;
2191 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2192 * construct the map B -> D and collect the results.
2194 __isl_give isl_union_map
*isl_union_map_factor_range(
2195 __isl_take isl_union_map
*umap
)
2197 return cond_un_op(umap
, &factor_range_entry
);
2200 static isl_stat
unwrap_entry(void **entry
, void *user
)
2202 isl_set
*set
= *entry
;
2203 isl_union_set
**res
= user
;
2205 if (!isl_set_is_wrapping(set
))
2208 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
2213 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2215 return cond_un_op(uset
, &unwrap_entry
);
2218 static isl_stat
wrap_entry(void **entry
, void *user
)
2220 isl_map
*map
= *entry
;
2221 isl_union_set
**res
= user
;
2223 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
2228 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2230 return cond_un_op(umap
, &wrap_entry
);
2233 struct isl_union_map_is_subset_data
{
2234 isl_union_map
*umap2
;
2238 static isl_stat
is_subset_entry(void **entry
, void *user
)
2240 struct isl_union_map_is_subset_data
*data
= user
;
2242 struct isl_hash_table_entry
*entry2
;
2243 isl_map
*map
= *entry
;
2245 hash
= isl_space_get_hash(map
->dim
);
2246 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2247 hash
, &has_dim
, map
->dim
, 0);
2249 int empty
= isl_map_is_empty(map
);
2251 return isl_stat_error
;
2254 data
->is_subset
= 0;
2255 return isl_stat_error
;
2258 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2259 if (data
->is_subset
< 0 || !data
->is_subset
)
2260 return isl_stat_error
;
2265 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2266 __isl_keep isl_union_map
*umap2
)
2268 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2270 umap1
= isl_union_map_copy(umap1
);
2271 umap2
= isl_union_map_copy(umap2
);
2272 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2273 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2275 if (!umap1
|| !umap2
)
2279 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2280 &is_subset_entry
, &data
) < 0 &&
2284 isl_union_map_free(umap1
);
2285 isl_union_map_free(umap2
);
2287 return data
.is_subset
;
2289 isl_union_map_free(umap1
);
2290 isl_union_map_free(umap2
);
2291 return isl_bool_error
;
2294 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2295 __isl_keep isl_union_set
*uset2
)
2297 return isl_union_map_is_subset(uset1
, uset2
);
2300 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2301 __isl_keep isl_union_map
*umap2
)
2305 if (!umap1
|| !umap2
)
2306 return isl_bool_error
;
2307 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2308 if (is_subset
!= isl_bool_true
)
2310 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2314 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2315 __isl_keep isl_union_set
*uset2
)
2317 return isl_union_map_is_equal(uset1
, uset2
);
2320 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2321 __isl_keep isl_union_map
*umap2
)
2325 if (!umap1
|| !umap2
)
2326 return isl_bool_error
;
2327 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2328 if (is_subset
!= isl_bool_true
)
2330 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2331 if (is_subset
== isl_bool_error
)
2336 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2337 __isl_keep isl_union_set
*uset2
)
2339 return isl_union_map_is_strict_subset(uset1
, uset2
);
2342 /* Internal data structure for isl_union_map_is_disjoint.
2343 * umap2 is the union map with which we are comparing.
2344 * is_disjoint is initialized to 1 and is set to 0 as soon
2345 * as the union maps turn out not to be disjoint.
2347 struct isl_union_map_is_disjoint_data
{
2348 isl_union_map
*umap2
;
2349 isl_bool is_disjoint
;
2352 /* Check if "map" is disjoint from data->umap2 and abort
2353 * the search if it is not.
2355 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2357 struct isl_union_map_is_disjoint_data
*data
= user
;
2359 struct isl_hash_table_entry
*entry2
;
2360 isl_map
*map
= *entry
;
2362 hash
= isl_space_get_hash(map
->dim
);
2363 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2364 hash
, &has_dim
, map
->dim
, 0);
2368 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2369 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2370 return isl_stat_error
;
2375 /* Are "umap1" and "umap2" disjoint?
2377 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2378 __isl_keep isl_union_map
*umap2
)
2380 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2382 umap1
= isl_union_map_copy(umap1
);
2383 umap2
= isl_union_map_copy(umap2
);
2384 umap1
= isl_union_map_align_params(umap1
,
2385 isl_union_map_get_space(umap2
));
2386 umap2
= isl_union_map_align_params(umap2
,
2387 isl_union_map_get_space(umap1
));
2389 if (!umap1
|| !umap2
)
2393 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2394 &is_disjoint_entry
, &data
) < 0 &&
2398 isl_union_map_free(umap1
);
2399 isl_union_map_free(umap2
);
2401 return data
.is_disjoint
;
2403 isl_union_map_free(umap1
);
2404 isl_union_map_free(umap2
);
2405 return isl_bool_error
;
2408 /* Are "uset1" and "uset2" disjoint?
2410 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2411 __isl_keep isl_union_set
*uset2
)
2413 return isl_union_map_is_disjoint(uset1
, uset2
);
2416 static isl_stat
sample_entry(void **entry
, void *user
)
2418 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2419 isl_map
*map
= *entry
;
2421 *sample
= isl_map_sample(isl_map_copy(map
));
2423 return isl_stat_error
;
2424 if (!isl_basic_map_plain_is_empty(*sample
))
2425 return isl_stat_error
;
2429 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2431 isl_basic_map
*sample
= NULL
;
2436 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2437 &sample_entry
, &sample
) < 0 &&
2442 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2444 isl_union_map_free(umap
);
2448 isl_union_map_free(umap
);
2452 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2454 return bset_from_bmap(isl_union_map_sample(uset
));
2457 /* Return an element in "uset" in the form of an isl_point.
2458 * Return a void isl_point if "uset" is empty.
2460 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2462 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2465 struct isl_forall_data
{
2467 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2470 static isl_stat
forall_entry(void **entry
, void *user
)
2472 struct isl_forall_data
*data
= user
;
2473 isl_map
*map
= *entry
;
2475 data
->res
= data
->fn(map
);
2477 return isl_stat_error
;
2480 return isl_stat_error
;
2485 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2486 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2488 struct isl_forall_data data
= { isl_bool_true
, fn
};
2491 return isl_bool_error
;
2493 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2494 &forall_entry
, &data
) < 0 && data
.res
)
2495 return isl_bool_error
;
2500 struct isl_forall_user_data
{
2502 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2506 static isl_stat
forall_user_entry(void **entry
, void *user
)
2508 struct isl_forall_user_data
*data
= user
;
2509 isl_map
*map
= *entry
;
2511 data
->res
= data
->fn(map
, data
->user
);
2513 return isl_stat_error
;
2516 return isl_stat_error
;
2521 /* Check if fn(map, user) returns true for all maps "map" in umap.
2523 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2524 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2526 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2529 return isl_bool_error
;
2531 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2532 &forall_user_entry
, &data
) < 0 && data
.res
)
2533 return isl_bool_error
;
2538 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2540 return union_map_forall(umap
, &isl_map_is_empty
);
2543 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2545 return isl_union_map_is_empty(uset
);
2548 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2555 return isl_bool_error
;
2557 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2558 map
->dim
, isl_dim_out
))
2559 return isl_bool_false
;
2561 dim
= isl_map_get_space(map
);
2562 id
= isl_map_identity(dim
);
2564 is_subset
= isl_map_is_subset(map
, id
);
2571 /* Given an isl_union_map that consists of a single map, check
2572 * if it is single-valued.
2574 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2579 umap
= isl_union_map_copy(umap
);
2580 map
= isl_map_from_union_map(umap
);
2581 sv
= isl_map_is_single_valued(map
);
2587 /* Internal data structure for single_valued_on_domain.
2589 * "umap" is the union map to be tested.
2590 * "sv" is set to 1 as long as "umap" may still be single-valued.
2592 struct isl_union_map_is_sv_data
{
2593 isl_union_map
*umap
;
2597 /* Check if the data->umap is single-valued on "set".
2599 * If data->umap consists of a single map on "set", then test it
2602 * Otherwise, compute
2606 * check if the result is a subset of the identity mapping and
2607 * store the result in data->sv.
2609 * Terminate as soon as data->umap has been determined not to
2612 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2614 struct isl_union_map_is_sv_data
*data
= user
;
2615 isl_union_map
*umap
, *test
;
2617 umap
= isl_union_map_copy(data
->umap
);
2618 umap
= isl_union_map_intersect_domain(umap
,
2619 isl_union_set_from_set(set
));
2621 if (isl_union_map_n_map(umap
) == 1) {
2622 data
->sv
= single_map_is_single_valued(umap
);
2623 isl_union_map_free(umap
);
2625 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2626 test
= isl_union_map_apply_range(test
, umap
);
2628 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2630 isl_union_map_free(test
);
2633 if (data
->sv
< 0 || !data
->sv
)
2634 return isl_stat_error
;
2638 /* Check if the given map is single-valued.
2640 * If the union map consists of a single map, then test it as an isl_map.
2641 * Otherwise, check if the union map is single-valued on each of its
2644 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2646 isl_union_map
*universe
;
2647 isl_union_set
*domain
;
2648 struct isl_union_map_is_sv_data data
;
2650 if (isl_union_map_n_map(umap
) == 1)
2651 return single_map_is_single_valued(umap
);
2653 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2654 domain
= isl_union_map_domain(universe
);
2656 data
.sv
= isl_bool_true
;
2658 if (isl_union_set_foreach_set(domain
,
2659 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2660 data
.sv
= isl_bool_error
;
2661 isl_union_set_free(domain
);
2666 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2670 umap
= isl_union_map_copy(umap
);
2671 umap
= isl_union_map_reverse(umap
);
2672 in
= isl_union_map_is_single_valued(umap
);
2673 isl_union_map_free(umap
);
2678 /* Is "map" obviously not an identity relation because
2679 * it maps elements from one space to another space?
2680 * Update *non_identity accordingly.
2682 * In particular, if the domain and range spaces are the same,
2683 * then the map is not considered to obviously not be an identity relation.
2684 * Otherwise, the map is considered to obviously not be an identity relation
2685 * if it is is non-empty.
2687 * If "map" is determined to obviously not be an identity relation,
2688 * then the search is aborted.
2690 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2692 isl_bool
*non_identity
= user
;
2696 space
= isl_map_get_space(map
);
2697 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2698 if (equal
>= 0 && !equal
)
2699 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2701 *non_identity
= isl_bool_not(equal
);
2702 isl_space_free(space
);
2705 if (*non_identity
< 0 || *non_identity
)
2706 return isl_stat_error
;
2711 /* Is "umap" obviously not an identity relation because
2712 * it maps elements from one space to another space?
2714 * As soon as a map has been found that maps elements to a different space,
2715 * non_identity is changed and the search is aborted.
2717 static isl_bool
isl_union_map_plain_is_not_identity(
2718 __isl_keep isl_union_map
*umap
)
2720 isl_bool non_identity
;
2722 non_identity
= isl_bool_false
;
2723 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2724 &non_identity
) < 0 &&
2725 non_identity
== isl_bool_false
)
2726 return isl_bool_error
;
2728 return non_identity
;
2731 /* Does "map" only map elements to themselves?
2732 * Update *identity accordingly.
2734 * If "map" is determined not to be an identity relation,
2735 * then the search is aborted.
2737 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2739 isl_bool
*identity
= user
;
2741 *identity
= isl_map_is_identity(map
);
2744 if (*identity
< 0 || !*identity
)
2745 return isl_stat_error
;
2750 /* Does "umap" only map elements to themselves?
2752 * First check if there are any maps that map elements to different spaces.
2753 * If not, then check that all the maps (between identical spaces)
2754 * are identity relations.
2756 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2758 isl_bool non_identity
;
2761 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2762 if (non_identity
< 0 || non_identity
)
2763 return isl_bool_not(non_identity
);
2765 identity
= isl_bool_true
;
2766 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2767 identity
== isl_bool_true
)
2768 return isl_bool_error
;
2773 /* Represents a map that has a fixed value (v) for one of its
2775 * The map in this structure is not reference counted, so it
2776 * is only valid while the isl_union_map from which it was
2777 * obtained is still alive.
2779 struct isl_fixed_map
{
2784 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2788 struct isl_fixed_map
*v
;
2790 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2793 for (i
= 0; i
< n
; ++i
)
2794 isl_int_init(v
[i
].v
);
2798 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2804 for (i
= 0; i
< n
; ++i
)
2805 isl_int_clear(v
[i
].v
);
2809 /* Compare the "v" field of two isl_fixed_map structs.
2811 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2813 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2814 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2816 return isl_int_cmp(e1
->v
, e2
->v
);
2819 /* Internal data structure used while checking whether all maps
2820 * in a union_map have a fixed value for a given output dimension.
2821 * v is the list of maps, with the fixed value for the dimension
2822 * n is the number of maps considered so far
2823 * pos is the output dimension under investigation
2825 struct isl_fixed_dim_data
{
2826 struct isl_fixed_map
*v
;
2831 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2833 struct isl_fixed_dim_data
*data
= user
;
2835 data
->v
[data
->n
].map
= map
;
2836 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2837 &data
->v
[data
->n
++].v
);
2840 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2841 int first
, int n_range
);
2843 /* Given a list of the maps, with their fixed values at output dimension "pos",
2844 * check whether the ranges of the maps form an obvious partition.
2846 * We first sort the maps according to their fixed values.
2847 * If all maps have a different value, then we know the ranges form
2849 * Otherwise, we collect the maps with the same fixed value and
2850 * check whether each such collection is obviously injective
2851 * based on later dimensions.
2853 static int separates(struct isl_fixed_map
*v
, int n
,
2854 __isl_take isl_space
*dim
, int pos
, int n_range
)
2861 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2863 for (i
= 0; i
+ 1 < n
; ++i
) {
2865 isl_union_map
*part
;
2868 for (j
= i
+ 1; j
< n
; ++j
)
2869 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2875 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2876 for (k
= i
; k
< j
; ++k
)
2877 part
= isl_union_map_add_map(part
,
2878 isl_map_copy(v
[k
].map
));
2880 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2889 isl_space_free(dim
);
2890 free_isl_fixed_map_array(v
, n
);
2893 isl_space_free(dim
);
2894 free_isl_fixed_map_array(v
, n
);
2898 /* Check whether the maps in umap have obviously distinct ranges.
2899 * In particular, check for an output dimension in the range
2900 * [first,n_range) for which all maps have a fixed value
2901 * and then check if these values, possibly along with fixed values
2902 * at later dimensions, entail distinct ranges.
2904 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2905 int first
, int n_range
)
2909 struct isl_fixed_dim_data data
= { NULL
};
2911 ctx
= isl_union_map_get_ctx(umap
);
2913 n
= isl_union_map_n_map(umap
);
2918 isl_union_map_free(umap
);
2919 return isl_bool_true
;
2922 if (first
>= n_range
) {
2923 isl_union_map_free(umap
);
2924 return isl_bool_false
;
2927 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2931 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2937 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2942 dim
= isl_union_map_get_space(umap
);
2943 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2944 isl_union_map_free(umap
);
2948 free_isl_fixed_map_array(data
.v
, n
);
2949 isl_union_map_free(umap
);
2951 return isl_bool_false
;
2953 free_isl_fixed_map_array(data
.v
, n
);
2954 isl_union_map_free(umap
);
2955 return isl_bool_error
;
2958 /* Check whether the maps in umap that map to subsets of "ran"
2959 * have obviously distinct ranges.
2961 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
2964 isl_union_map
*umap
= user
;
2966 umap
= isl_union_map_copy(umap
);
2967 umap
= isl_union_map_intersect_range(umap
,
2968 isl_union_set_from_set(isl_set_copy(ran
)));
2969 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2972 /* Check if the given union_map is obviously injective.
2974 * In particular, we first check if all individual maps are obviously
2975 * injective and then check if all the ranges of these maps are
2976 * obviously disjoint.
2978 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2981 isl_union_map
*univ
;
2984 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2986 return isl_bool_error
;
2988 return isl_bool_false
;
2990 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2991 ran
= isl_union_map_range(univ
);
2993 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2995 isl_union_set_free(ran
);
3000 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
3004 sv
= isl_union_map_is_single_valued(umap
);
3008 return isl_union_map_is_injective(umap
);
3011 static isl_stat
zip_entry(void **entry
, void *user
)
3013 isl_map
*map
= *entry
;
3014 isl_union_map
**res
= user
;
3016 if (!isl_map_can_zip(map
))
3019 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
3024 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
3026 return cond_un_op(umap
, &zip_entry
);
3029 static isl_stat
uncurry_entry(void **entry
, void *user
)
3031 isl_map
*map
= *entry
;
3032 isl_union_map
**res
= user
;
3034 if (!isl_map_can_uncurry(map
))
3037 *res
= isl_union_map_add_map(*res
, isl_map_uncurry(isl_map_copy(map
)));
3042 /* Given a union map, take the maps of the form A -> (B -> C) and
3043 * return the union of the corresponding maps (A -> B) -> C.
3045 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
3047 return cond_un_op(umap
, &uncurry_entry
);
3050 static isl_stat
curry_entry(void **entry
, void *user
)
3052 isl_map
*map
= *entry
;
3053 isl_union_map
**res
= user
;
3055 if (!isl_map_can_curry(map
))
3058 *res
= isl_union_map_add_map(*res
, isl_map_curry(isl_map_copy(map
)));
3063 /* Given a union map, take the maps of the form (A -> B) -> C and
3064 * return the union of the corresponding maps A -> (B -> C).
3066 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
3068 return cond_un_op(umap
, &curry_entry
);
3071 /* If *entry is of the form A -> ((B -> C) -> D), then apply
3072 * isl_map_range_curry to it and add the result to *res.
3074 static isl_stat
range_curry_entry(void **entry
, void *user
)
3076 isl_map
*map
= *entry
;
3077 isl_union_map
**res
= user
;
3079 if (!isl_map_can_range_curry(map
))
3082 map
= isl_map_range_curry(isl_map_copy(map
));
3083 *res
= isl_union_map_add_map(*res
, map
);
3088 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3089 * return the union of the corresponding maps A -> (B -> (C -> D)).
3091 __isl_give isl_union_map
*isl_union_map_range_curry(
3092 __isl_take isl_union_map
*umap
)
3094 return cond_un_op(umap
, &range_curry_entry
);
3097 static isl_stat
lift_entry(void **entry
, void *user
)
3099 isl_set
*set
= *entry
;
3100 isl_union_set
**res
= user
;
3102 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
3107 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
3109 return cond_un_op(uset
, &lift_entry
);
3112 static isl_stat
coefficients_entry(void **entry
, void *user
)
3114 isl_set
*set
= *entry
;
3115 isl_union_set
**res
= user
;
3117 set
= isl_set_copy(set
);
3118 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
3119 *res
= isl_union_set_add_set(*res
, set
);
3124 __isl_give isl_union_set
*isl_union_set_coefficients(
3125 __isl_take isl_union_set
*uset
)
3134 ctx
= isl_union_set_get_ctx(uset
);
3135 dim
= isl_space_set_alloc(ctx
, 0, 0);
3136 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
3137 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3138 &coefficients_entry
, &res
) < 0)
3141 isl_union_set_free(uset
);
3144 isl_union_set_free(uset
);
3145 isl_union_set_free(res
);
3149 static isl_stat
solutions_entry(void **entry
, void *user
)
3151 isl_set
*set
= *entry
;
3152 isl_union_set
**res
= user
;
3154 set
= isl_set_copy(set
);
3155 set
= isl_set_from_basic_set(isl_set_solutions(set
));
3157 *res
= isl_union_set_from_set(set
);
3159 *res
= isl_union_set_add_set(*res
, set
);
3162 return isl_stat_error
;
3167 __isl_give isl_union_set
*isl_union_set_solutions(
3168 __isl_take isl_union_set
*uset
)
3170 isl_union_set
*res
= NULL
;
3175 if (uset
->table
.n
== 0) {
3176 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
3177 isl_union_set_free(uset
);
3181 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3182 &solutions_entry
, &res
) < 0)
3185 isl_union_set_free(uset
);
3188 isl_union_set_free(uset
);
3189 isl_union_set_free(res
);
3193 /* Is the domain space of "map" equal to "space"?
3195 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3197 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
3198 space
, isl_dim_out
);
3201 /* Is the range space of "map" equal to "space"?
3203 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3205 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3206 space
, isl_dim_out
);
3209 /* Is the set space of "map" equal to "space"?
3211 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3213 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3214 space
, isl_dim_out
);
3217 /* Internal data structure for preimage_pw_multi_aff.
3219 * "pma" is the function under which the preimage should be taken.
3220 * "space" is the space of "pma".
3221 * "res" collects the results.
3222 * "fn" computes the preimage for a given map.
3223 * "match" returns true if "fn" can be called.
3225 struct isl_union_map_preimage_data
{
3227 isl_pw_multi_aff
*pma
;
3229 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3230 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3231 __isl_take isl_pw_multi_aff
*pma
);
3234 /* Call data->fn to compute the preimage of the domain or range of *entry
3235 * under the function represented by data->pma, provided the domain/range
3236 * space of *entry matches the target space of data->pma
3237 * (as given by data->match), and add the result to data->res.
3239 static isl_stat
preimage_entry(void **entry
, void *user
)
3242 isl_map
*map
= *entry
;
3243 struct isl_union_map_preimage_data
*data
= user
;
3246 m
= data
->match(map
, data
->space
);
3248 return isl_stat_error
;
3252 map
= isl_map_copy(map
);
3253 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3255 empty
= isl_map_is_empty(map
);
3256 if (empty
< 0 || empty
) {
3258 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3261 data
->res
= isl_union_map_add_map(data
->res
, map
);
3266 /* Compute the preimage of the domain or range of "umap" under the function
3267 * represented by "pma".
3268 * In other words, plug in "pma" in the domain or range of "umap".
3269 * The function "fn" performs the actual preimage computation on a map,
3270 * while "match" determines to which maps the function should be applied.
3272 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3273 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3274 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3275 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3276 __isl_take isl_pw_multi_aff
*pma
))
3280 struct isl_union_map_preimage_data data
;
3282 umap
= isl_union_map_align_params(umap
,
3283 isl_pw_multi_aff_get_space(pma
));
3284 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3289 ctx
= isl_union_map_get_ctx(umap
);
3290 space
= isl_union_map_get_space(umap
);
3291 data
.space
= isl_pw_multi_aff_get_space(pma
);
3293 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3296 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3298 data
.res
= isl_union_map_free(data
.res
);
3300 isl_space_free(data
.space
);
3301 isl_union_map_free(umap
);
3302 isl_pw_multi_aff_free(pma
);
3305 isl_union_map_free(umap
);
3306 isl_pw_multi_aff_free(pma
);
3310 /* Compute the preimage of the domain of "umap" under the function
3311 * represented by "pma".
3312 * In other words, plug in "pma" in the domain of "umap".
3313 * The result contains maps that live in the same spaces as the maps of "umap"
3314 * with domain space equal to the target space of "pma",
3315 * except that the domain has been replaced by the domain space of "pma".
3317 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3318 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3320 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3321 &isl_map_preimage_domain_pw_multi_aff
);
3324 /* Compute the preimage of the range of "umap" under the function
3325 * represented by "pma".
3326 * In other words, plug in "pma" in the range of "umap".
3327 * The result contains maps that live in the same spaces as the maps of "umap"
3328 * with range space equal to the target space of "pma",
3329 * except that the range has been replaced by the domain space of "pma".
3331 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3332 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3334 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3335 &isl_map_preimage_range_pw_multi_aff
);
3338 /* Compute the preimage of "uset" under the function represented by "pma".
3339 * In other words, plug in "pma" in "uset".
3340 * The result contains sets that live in the same spaces as the sets of "uset"
3341 * with space equal to the target space of "pma",
3342 * except that the space has been replaced by the domain space of "pma".
3344 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3345 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3347 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3348 &isl_set_preimage_pw_multi_aff
);
3351 /* Compute the preimage of the domain of "umap" under the function
3352 * represented by "ma".
3353 * In other words, plug in "ma" in the domain of "umap".
3354 * The result contains maps that live in the same spaces as the maps of "umap"
3355 * with domain space equal to the target space of "ma",
3356 * except that the domain has been replaced by the domain space of "ma".
3358 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3359 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3361 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3362 isl_pw_multi_aff_from_multi_aff(ma
));
3365 /* Compute the preimage of the range of "umap" under the function
3366 * represented by "ma".
3367 * In other words, plug in "ma" in the range of "umap".
3368 * The result contains maps that live in the same spaces as the maps of "umap"
3369 * with range space equal to the target space of "ma",
3370 * except that the range has been replaced by the domain space of "ma".
3372 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3373 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3375 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3376 isl_pw_multi_aff_from_multi_aff(ma
));
3379 /* Compute the preimage of "uset" under the function represented by "ma".
3380 * In other words, plug in "ma" in "uset".
3381 * The result contains sets that live in the same spaces as the sets of "uset"
3382 * with space equal to the target space of "ma",
3383 * except that the space has been replaced by the domain space of "ma".
3385 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3386 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3388 return isl_union_set_preimage_pw_multi_aff(uset
,
3389 isl_pw_multi_aff_from_multi_aff(ma
));
3392 /* Internal data structure for preimage_multi_pw_aff.
3394 * "mpa" is the function under which the preimage should be taken.
3395 * "space" is the space of "mpa".
3396 * "res" collects the results.
3397 * "fn" computes the preimage for a given map.
3398 * "match" returns true if "fn" can be called.
3400 struct isl_union_map_preimage_mpa_data
{
3402 isl_multi_pw_aff
*mpa
;
3404 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3405 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3406 __isl_take isl_multi_pw_aff
*mpa
);
3409 /* Call data->fn to compute the preimage of the domain or range of *entry
3410 * under the function represented by data->mpa, provided the domain/range
3411 * space of *entry matches the target space of data->mpa
3412 * (as given by data->match), and add the result to data->res.
3414 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3417 isl_map
*map
= *entry
;
3418 struct isl_union_map_preimage_mpa_data
*data
= user
;
3421 m
= data
->match(map
, data
->space
);
3423 return isl_stat_error
;
3427 map
= isl_map_copy(map
);
3428 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3430 empty
= isl_map_is_empty(map
);
3431 if (empty
< 0 || empty
) {
3433 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3436 data
->res
= isl_union_map_add_map(data
->res
, map
);
3441 /* Compute the preimage of the domain or range of "umap" under the function
3442 * represented by "mpa".
3443 * In other words, plug in "mpa" in the domain or range of "umap".
3444 * The function "fn" performs the actual preimage computation on a map,
3445 * while "match" determines to which maps the function should be applied.
3447 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3448 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3449 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3450 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3451 __isl_take isl_multi_pw_aff
*mpa
))
3455 struct isl_union_map_preimage_mpa_data data
;
3457 umap
= isl_union_map_align_params(umap
,
3458 isl_multi_pw_aff_get_space(mpa
));
3459 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3464 ctx
= isl_union_map_get_ctx(umap
);
3465 space
= isl_union_map_get_space(umap
);
3466 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3468 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3471 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3473 data
.res
= isl_union_map_free(data
.res
);
3475 isl_space_free(data
.space
);
3476 isl_union_map_free(umap
);
3477 isl_multi_pw_aff_free(mpa
);
3480 isl_union_map_free(umap
);
3481 isl_multi_pw_aff_free(mpa
);
3485 /* Compute the preimage of the domain of "umap" under the function
3486 * represented by "mpa".
3487 * In other words, plug in "mpa" in the domain of "umap".
3488 * The result contains maps that live in the same spaces as the maps of "umap"
3489 * with domain space equal to the target space of "mpa",
3490 * except that the domain has been replaced by the domain space of "mpa".
3492 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3493 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3495 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3496 &isl_map_preimage_domain_multi_pw_aff
);
3499 /* Internal data structure for preimage_upma.
3501 * "umap" is the map of which the preimage should be computed.
3502 * "res" collects the results.
3503 * "fn" computes the preimage for a given piecewise multi-affine function.
3505 struct isl_union_map_preimage_upma_data
{
3506 isl_union_map
*umap
;
3508 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3509 __isl_take isl_pw_multi_aff
*pma
);
3512 /* Call data->fn to compute the preimage of the domain or range of data->umap
3513 * under the function represented by pma and add the result to data->res.
3515 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3517 struct isl_union_map_preimage_upma_data
*data
= user
;
3518 isl_union_map
*umap
;
3520 umap
= isl_union_map_copy(data
->umap
);
3521 umap
= data
->fn(umap
, pma
);
3522 data
->res
= isl_union_map_union(data
->res
, umap
);
3524 return data
->res
? isl_stat_ok
: isl_stat_error
;
3527 /* Compute the preimage of the domain or range of "umap" under the function
3528 * represented by "upma".
3529 * In other words, plug in "upma" in the domain or range of "umap".
3530 * The function "fn" performs the actual preimage computation
3531 * on a piecewise multi-affine function.
3533 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3534 __isl_take isl_union_map
*umap
,
3535 __isl_take isl_union_pw_multi_aff
*upma
,
3536 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3537 __isl_take isl_pw_multi_aff
*pma
))
3539 struct isl_union_map_preimage_upma_data data
;
3542 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3544 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3545 &preimage_upma
, &data
) < 0)
3546 data
.res
= isl_union_map_free(data
.res
);
3548 isl_union_map_free(umap
);
3549 isl_union_pw_multi_aff_free(upma
);
3554 /* Compute the preimage of the domain of "umap" under the function
3555 * represented by "upma".
3556 * In other words, plug in "upma" in the domain of "umap".
3557 * The result contains maps that live in the same spaces as the maps of "umap"
3558 * with domain space equal to one of the target spaces of "upma",
3559 * except that the domain has been replaced by one of the the domain spaces that
3560 * corresponds to that target space of "upma".
3562 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3563 __isl_take isl_union_map
*umap
,
3564 __isl_take isl_union_pw_multi_aff
*upma
)
3566 return preimage_union_pw_multi_aff(umap
, upma
,
3567 &isl_union_map_preimage_domain_pw_multi_aff
);
3570 /* Compute the preimage of the range of "umap" under the function
3571 * represented by "upma".
3572 * In other words, plug in "upma" in the range of "umap".
3573 * The result contains maps that live in the same spaces as the maps of "umap"
3574 * with range space equal to one of the target spaces of "upma",
3575 * except that the range has been replaced by one of the the domain spaces that
3576 * corresponds to that target space of "upma".
3578 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3579 __isl_take isl_union_map
*umap
,
3580 __isl_take isl_union_pw_multi_aff
*upma
)
3582 return preimage_union_pw_multi_aff(umap
, upma
,
3583 &isl_union_map_preimage_range_pw_multi_aff
);
3586 /* Compute the preimage of "uset" under the function represented by "upma".
3587 * In other words, plug in "upma" in the range of "uset".
3588 * The result contains sets that live in the same spaces as the sets of "uset"
3589 * with space equal to one of the target spaces of "upma",
3590 * except that the space has been replaced by one of the the domain spaces that
3591 * corresponds to that target space of "upma".
3593 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3594 __isl_take isl_union_set
*uset
,
3595 __isl_take isl_union_pw_multi_aff
*upma
)
3597 return preimage_union_pw_multi_aff(uset
, upma
,
3598 &isl_union_set_preimage_pw_multi_aff
);
3601 /* Reset the user pointer on all identifiers of parameters and tuples
3602 * of the space of *entry.
3604 static isl_stat
reset_user(void **entry
, void *user
)
3606 isl_map
**map
= (isl_map
**)entry
;
3608 *map
= isl_map_reset_user(*map
);
3610 return *map
? isl_stat_ok
: isl_stat_error
;
3613 /* Reset the user pointer on all identifiers of parameters and tuples
3614 * of the spaces of "umap".
3616 __isl_give isl_union_map
*isl_union_map_reset_user(
3617 __isl_take isl_union_map
*umap
)
3619 umap
= isl_union_map_cow(umap
);
3622 umap
->dim
= isl_space_reset_user(umap
->dim
);
3624 return isl_union_map_free(umap
);
3625 umap
= un_op(umap
, &reset_user
);
3630 /* Reset the user pointer on all identifiers of parameters and tuples
3631 * of the spaces of "uset".
3633 __isl_give isl_union_set
*isl_union_set_reset_user(
3634 __isl_take isl_union_set
*uset
)
3636 return isl_union_map_reset_user(uset
);
3639 /* Internal data structure for isl_union_map_project_out.
3640 * "type", "first" and "n" are the arguments for the isl_map_project_out
3642 * "res" collects the results.
3644 struct isl_union_map_project_out_data
{
3645 enum isl_dim_type type
;
3652 /* Turn the data->n dimensions of type data->type, starting at data->first
3653 * into existentially quantified variables and add the result to data->res.
3655 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3657 struct isl_union_map_project_out_data
*data
= user
;
3659 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3660 data
->res
= isl_union_map_add_map(data
->res
, map
);
3665 /* Turn the "n" dimensions of type "type", starting at "first"
3666 * into existentially quantified variables.
3667 * Since the space of an isl_union_map only contains parameters,
3668 * type is required to be equal to isl_dim_param.
3670 __isl_give isl_union_map
*isl_union_map_project_out(
3671 __isl_take isl_union_map
*umap
,
3672 enum isl_dim_type type
, unsigned first
, unsigned n
)
3675 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3680 if (type
!= isl_dim_param
)
3681 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3682 "can only project out parameters",
3683 return isl_union_map_free(umap
));
3685 space
= isl_union_map_get_space(umap
);
3686 space
= isl_space_drop_dims(space
, type
, first
, n
);
3687 data
.res
= isl_union_map_empty(space
);
3688 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3689 data
.res
= isl_union_map_free(data
.res
);
3691 isl_union_map_free(umap
);
3696 /* Turn the "n" dimensions of type "type", starting at "first"
3697 * into existentially quantified variables.
3698 * Since the space of an isl_union_set only contains parameters,
3699 * "type" is required to be equal to isl_dim_param.
3701 __isl_give isl_union_set
*isl_union_set_project_out(
3702 __isl_take isl_union_set
*uset
,
3703 enum isl_dim_type type
, unsigned first
, unsigned n
)
3705 return isl_union_map_project_out(uset
, type
, first
, n
);
3708 /* Internal data structure for isl_union_map_involves_dims.
3709 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3711 struct isl_union_map_involves_dims_data
{
3716 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3718 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3720 struct isl_union_map_involves_dims_data
*data
= user
;
3723 involves
= isl_map_involves_dims(map
,
3724 isl_dim_param
, data
->first
, data
->n
);
3726 return isl_bool_error
;
3730 /* Does "umap" involve any of the n parameters starting at first?
3731 * "type" is required to be set to isl_dim_param.
3733 * "umap" involves any of those parameters if any of its maps
3734 * involve the parameters. In other words, "umap" does not
3735 * involve any of the parameters if all its maps to not
3736 * involve the parameters.
3738 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3739 enum isl_dim_type type
, unsigned first
, unsigned n
)
3741 struct isl_union_map_involves_dims_data data
= { first
, n
};
3744 if (type
!= isl_dim_param
)
3745 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3746 "can only reference parameters", return isl_bool_error
);
3748 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3751 return isl_bool_error
;
3756 /* Internal data structure for isl_union_map_reset_range_space.
3757 * "range" is the space from which to set the range space.
3758 * "res" collects the results.
3760 struct isl_union_map_reset_range_space_data
{
3765 /* Replace the range space of "map" by the range space of data->range and
3766 * add the result to data->res.
3768 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3770 struct isl_union_map_reset_range_space_data
*data
= user
;
3773 space
= isl_map_get_space(map
);
3774 space
= isl_space_domain(space
);
3775 space
= isl_space_extend_domain_with_range(space
,
3776 isl_space_copy(data
->range
));
3777 map
= isl_map_reset_space(map
, space
);
3778 data
->res
= isl_union_map_add_map(data
->res
, map
);
3780 return data
->res
? isl_stat_ok
: isl_stat_error
;
3783 /* Replace the range space of all the maps in "umap" by
3784 * the range space of "space".
3786 * This assumes that all maps have the same output dimension.
3787 * This function should therefore not be made publicly available.
3789 * Since the spaces of the maps change, so do their hash value.
3790 * We therefore need to create a new isl_union_map.
3792 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3793 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3795 struct isl_union_map_reset_range_space_data data
= { space
};
3797 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3798 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
3799 data
.res
= isl_union_map_free(data
.res
);
3801 isl_space_free(space
);
3802 isl_union_map_free(umap
);
3806 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3807 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3808 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3809 * to the domain and the range of each map.
3810 * "res" collects the results.
3812 struct isl_union_order_at_data
{
3813 isl_multi_union_pw_aff
*mupa
;
3814 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3815 __isl_take isl_multi_pw_aff
*mpa2
);
3819 /* Intersect "map" with the result of applying data->order to
3820 * the functions in data->mupa that apply to the domain and the range
3821 * of "map" and add the result to data->res.
3823 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
3825 struct isl_union_order_at_data
*data
= user
;
3827 isl_multi_pw_aff
*mpa1
, *mpa2
;
3830 space
= isl_space_domain(isl_map_get_space(map
));
3831 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3832 space
= isl_space_range(isl_map_get_space(map
));
3833 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3834 order
= data
->order(mpa1
, mpa2
);
3835 map
= isl_map_intersect(map
, order
);
3836 data
->res
= isl_union_map_add_map(data
->res
, map
);
3838 return data
->res
? isl_stat_ok
: isl_stat_error
;
3841 /* Intersect each map in "umap" with the result of calling "order"
3842 * on the functions is "mupa" that apply to the domain and the range
3845 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
3846 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
3847 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3848 __isl_take isl_multi_pw_aff
*mpa2
))
3850 struct isl_union_order_at_data data
;
3852 umap
= isl_union_map_align_params(umap
,
3853 isl_multi_union_pw_aff_get_space(mupa
));
3854 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
3855 isl_union_map_get_space(umap
));
3858 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3859 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
3860 data
.res
= isl_union_map_free(data
.res
);
3862 isl_multi_union_pw_aff_free(mupa
);
3863 isl_union_map_free(umap
);
3867 /* Return the subset of "umap" where the domain and the range
3868 * have equal "mupa" values.
3870 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
3871 __isl_take isl_union_map
*umap
,
3872 __isl_take isl_multi_union_pw_aff
*mupa
)
3874 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3875 &isl_multi_pw_aff_eq_map
);
3878 /* Return the subset of "umap" where the domain has a lexicographically
3879 * smaller "mupa" value than the range.
3881 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
3882 __isl_take isl_union_map
*umap
,
3883 __isl_take isl_multi_union_pw_aff
*mupa
)
3885 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3886 &isl_multi_pw_aff_lex_lt_map
);
3889 /* Return the subset of "umap" where the domain has a lexicographically
3890 * greater "mupa" value than the range.
3892 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
3893 __isl_take isl_union_map
*umap
,
3894 __isl_take isl_multi_union_pw_aff
*mupa
)
3896 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3897 &isl_multi_pw_aff_lex_gt_map
);
3900 /* Return the union of the elements in the list "list".
3902 __isl_give isl_union_set
*isl_union_set_list_union(
3903 __isl_take isl_union_set_list
*list
)
3913 ctx
= isl_union_set_list_get_ctx(list
);
3914 space
= isl_space_params_alloc(ctx
, 0);
3915 res
= isl_union_set_empty(space
);
3917 n
= isl_union_set_list_n_union_set(list
);
3918 for (i
= 0; i
< n
; ++i
) {
3919 isl_union_set
*uset_i
;
3921 uset_i
= isl_union_set_list_get_union_set(list
, i
);
3922 res
= isl_union_set_union(res
, uset_i
);
3925 isl_union_set_list_free(list
);
3929 /* Update *hash with the hash value of "map".
3931 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
3933 uint32_t *hash
= user
;
3936 map_hash
= isl_map_get_hash(map
);
3937 isl_hash_hash(*hash
, map_hash
);
3943 /* Return a hash value that digests "umap".
3945 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
3952 hash
= isl_hash_init();
3953 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
3959 /* Return a hash value that digests "uset".
3961 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
3963 return isl_union_map_get_hash(uset
);