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
};
234 isl_bool equal_params
;
239 equal_params
= isl_space_has_equal_params(umap
->dim
, model
);
240 if (equal_params
< 0)
243 isl_space_free(model
);
247 model
= isl_space_params(model
);
248 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
252 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
254 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
255 &align_entry
, &data
) < 0)
258 isl_reordering_free(data
.exp
);
259 isl_union_map_free(umap
);
260 isl_space_free(model
);
263 isl_reordering_free(data
.exp
);
264 isl_union_map_free(umap
);
265 isl_union_map_free(data
.res
);
266 isl_space_free(model
);
270 __isl_give isl_union_set
*isl_union_set_align_params(
271 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
273 return isl_union_map_align_params(uset
, model
);
276 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
277 __isl_take isl_union_map
*umap2
)
279 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
280 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
282 umap1
= isl_union_map_cow(umap1
);
284 if (!umap1
|| !umap2
)
287 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
290 isl_union_map_free(umap2
);
294 isl_union_map_free(umap1
);
295 isl_union_map_free(umap2
);
299 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
300 __isl_take isl_union_set
*uset2
)
302 return isl_union_map_union(uset1
, uset2
);
305 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
314 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
316 return isl_union_map_copy(uset
);
319 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
327 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
328 &free_umap_entry
, NULL
);
329 isl_hash_table_clear(&umap
->table
);
330 isl_space_free(umap
->dim
);
335 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
337 return isl_union_map_free(uset
);
340 /* Do "umap" and "space" have the same parameters?
342 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
343 __isl_keep isl_space
*space
)
345 isl_space
*umap_space
;
347 umap_space
= isl_union_map_peek_space(umap
);
348 return isl_space_has_equal_params(umap_space
, space
);
351 static int has_dim(const void *entry
, const void *val
)
353 isl_map
*map
= (isl_map
*)entry
;
354 isl_space
*dim
= (isl_space
*)val
;
356 return isl_space_is_equal(map
->dim
, dim
);
359 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
360 __isl_take isl_map
*map
)
363 struct isl_hash_table_entry
*entry
;
369 if (isl_map_plain_is_empty(map
)) {
374 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
378 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
379 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
382 umap
= isl_union_map_cow(umap
);
387 hash
= isl_space_get_hash(map
->dim
);
388 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
389 &has_dim
, map
->dim
, 1);
396 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
405 isl_union_map_free(umap
);
409 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
410 __isl_take isl_set
*set
)
412 return isl_union_map_add_map(uset
, set_to_map(set
));
415 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
423 dim
= isl_map_get_space(map
);
424 dim
= isl_space_params(dim
);
425 umap
= isl_union_map_empty(dim
);
426 umap
= isl_union_map_add_map(umap
, map
);
431 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
433 return isl_union_map_from_map(set_to_map(set
));
436 __isl_give isl_union_map
*isl_union_map_from_basic_map(
437 __isl_take isl_basic_map
*bmap
)
439 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
442 __isl_give isl_union_set
*isl_union_set_from_basic_set(
443 __isl_take isl_basic_set
*bset
)
445 return isl_union_map_from_basic_map(bset
);
448 struct isl_union_map_foreach_data
450 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
454 static isl_stat
call_on_copy(void **entry
, void *user
)
456 isl_map
*map
= *entry
;
457 struct isl_union_map_foreach_data
*data
;
458 data
= (struct isl_union_map_foreach_data
*)user
;
460 return data
->fn(isl_map_copy(map
), data
->user
);
463 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
465 return umap
? umap
->table
.n
: 0;
468 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
470 return uset
? uset
->table
.n
: 0;
473 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
474 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
476 struct isl_union_map_foreach_data data
= { fn
, user
};
479 return isl_stat_error
;
481 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
482 &call_on_copy
, &data
);
485 static isl_stat
copy_map(void **entry
, void *user
)
487 isl_map
*map
= *entry
;
488 isl_map
**map_p
= user
;
490 *map_p
= isl_map_copy(map
);
492 return isl_stat_error
;
495 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
502 ctx
= isl_union_map_get_ctx(umap
);
503 if (umap
->table
.n
!= 1)
504 isl_die(ctx
, isl_error_invalid
,
505 "union map needs to contain elements in exactly "
506 "one space", goto error
);
508 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
510 isl_union_map_free(umap
);
514 isl_union_map_free(umap
);
518 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
520 return isl_map_from_union_map(uset
);
523 /* Extract the map in "umap" that lives in the given space (ignoring
526 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
527 __isl_take isl_space
*space
)
530 struct isl_hash_table_entry
*entry
;
532 space
= isl_space_drop_dims(space
, isl_dim_param
,
533 0, isl_space_dim(space
, isl_dim_param
));
534 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
538 hash
= isl_space_get_hash(space
);
539 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
542 return isl_map_empty(space
);
543 isl_space_free(space
);
544 return isl_map_copy(entry
->data
);
546 isl_space_free(space
);
550 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
551 __isl_take isl_space
*dim
)
553 return set_from_map(isl_union_map_extract_map(uset
, dim
));
556 /* Check if umap contains a map in the given space.
558 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
559 __isl_keep isl_space
*space
)
562 struct isl_hash_table_entry
*entry
;
565 return isl_bool_error
;
567 hash
= isl_space_get_hash(space
);
568 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
573 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
574 __isl_keep isl_space
*space
)
576 return isl_union_map_contains(uset
, space
);
579 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
580 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
582 return isl_union_map_foreach_map(uset
,
583 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
586 struct isl_union_set_foreach_point_data
{
587 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
591 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
593 struct isl_union_set_foreach_point_data
*data
= user
;
596 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
602 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
603 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
605 struct isl_union_set_foreach_point_data data
= { fn
, user
};
606 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
609 struct isl_union_map_gen_bin_data
{
610 isl_union_map
*umap2
;
614 static isl_stat
subtract_entry(void **entry
, void *user
)
616 struct isl_union_map_gen_bin_data
*data
= user
;
618 struct isl_hash_table_entry
*entry2
;
619 isl_map
*map
= *entry
;
621 hash
= isl_space_get_hash(map
->dim
);
622 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
623 hash
, &has_dim
, map
->dim
, 0);
624 map
= isl_map_copy(map
);
627 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
629 empty
= isl_map_is_empty(map
);
632 return isl_stat_error
;
639 data
->res
= isl_union_map_add_map(data
->res
, map
);
644 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
645 __isl_take isl_union_map
*umap2
, isl_stat (*fn
)(void **, void *))
647 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
649 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
650 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
652 if (!umap1
|| !umap2
)
656 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
658 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
662 isl_union_map_free(umap1
);
663 isl_union_map_free(umap2
);
666 isl_union_map_free(umap1
);
667 isl_union_map_free(umap2
);
668 isl_union_map_free(data
.res
);
672 __isl_give isl_union_map
*isl_union_map_subtract(
673 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
675 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
678 __isl_give isl_union_set
*isl_union_set_subtract(
679 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
681 return isl_union_map_subtract(uset1
, uset2
);
684 struct isl_union_map_gen_bin_set_data
{
689 static isl_stat
intersect_params_entry(void **entry
, void *user
)
691 struct isl_union_map_gen_bin_set_data
*data
= user
;
692 isl_map
*map
= *entry
;
695 map
= isl_map_copy(map
);
696 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
698 empty
= isl_map_is_empty(map
);
701 return isl_stat_error
;
704 data
->res
= isl_union_map_add_map(data
->res
, map
);
709 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
710 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
712 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
714 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
715 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
721 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
723 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
727 isl_union_map_free(umap
);
731 isl_union_map_free(umap
);
733 isl_union_map_free(data
.res
);
737 /* Intersect "umap" with the parameter domain "set".
739 * If "set" does not have any constraints, then we can return immediately.
741 __isl_give isl_union_map
*isl_union_map_intersect_params(
742 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
746 is_universe
= isl_set_plain_is_universe(set
);
754 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
756 isl_union_map_free(umap
);
761 __isl_give isl_union_set
*isl_union_set_intersect_params(
762 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
764 return isl_union_map_intersect_params(uset
, set
);
767 static __isl_give isl_union_map
*union_map_intersect_params(
768 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
770 return isl_union_map_intersect_params(umap
,
771 isl_set_from_union_set(uset
));
774 static __isl_give isl_union_map
*union_map_gist_params(
775 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
777 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
780 struct isl_union_map_match_bin_data
{
781 isl_union_map
*umap2
;
783 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
786 static isl_stat
match_bin_entry(void **entry
, void *user
)
788 struct isl_union_map_match_bin_data
*data
= user
;
790 struct isl_hash_table_entry
*entry2
;
791 isl_map
*map
= *entry
;
794 hash
= isl_space_get_hash(map
->dim
);
795 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
796 hash
, &has_dim
, map
->dim
, 0);
800 map
= isl_map_copy(map
);
801 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
803 empty
= isl_map_is_empty(map
);
806 return isl_stat_error
;
813 data
->res
= isl_union_map_add_map(data
->res
, map
);
818 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
819 __isl_take isl_union_map
*umap2
,
820 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
822 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
824 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
825 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
827 if (!umap1
|| !umap2
)
831 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
833 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
834 &match_bin_entry
, &data
) < 0)
837 isl_union_map_free(umap1
);
838 isl_union_map_free(umap2
);
841 isl_union_map_free(umap1
);
842 isl_union_map_free(umap2
);
843 isl_union_map_free(data
.res
);
847 __isl_give isl_union_map
*isl_union_map_intersect(
848 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
850 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
853 /* Compute the intersection of the two union_sets.
854 * As a special case, if exactly one of the two union_sets
855 * is a parameter domain, then intersect the parameter domain
856 * of the other one with this set.
858 __isl_give isl_union_set
*isl_union_set_intersect(
859 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
863 p1
= isl_union_set_is_params(uset1
);
864 p2
= isl_union_set_is_params(uset2
);
865 if (p1
< 0 || p2
< 0)
868 return union_map_intersect_params(uset1
, uset2
);
870 return union_map_intersect_params(uset2
, uset1
);
871 return isl_union_map_intersect(uset1
, uset2
);
873 isl_union_set_free(uset1
);
874 isl_union_set_free(uset2
);
878 static isl_stat
gist_params_entry(void **entry
, void *user
)
880 struct isl_union_map_gen_bin_set_data
*data
= user
;
881 isl_map
*map
= *entry
;
884 map
= isl_map_copy(map
);
885 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
887 empty
= isl_map_is_empty(map
);
890 return isl_stat_error
;
893 data
->res
= isl_union_map_add_map(data
->res
, map
);
898 __isl_give isl_union_map
*isl_union_map_gist_params(
899 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
901 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
904 __isl_give isl_union_set
*isl_union_set_gist_params(
905 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
907 return isl_union_map_gist_params(uset
, set
);
910 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
911 __isl_take isl_union_map
*context
)
913 return match_bin_op(umap
, context
, &isl_map_gist
);
916 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
917 __isl_take isl_union_set
*context
)
919 if (isl_union_set_is_params(context
))
920 return union_map_gist_params(uset
, context
);
921 return isl_union_map_gist(uset
, context
);
924 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
925 __isl_take isl_map
*set2
)
927 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
930 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
931 __isl_take isl_map
*set2
)
933 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
936 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
937 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
939 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
942 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
943 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
945 return match_bin_op(uset1
, uset2
, &lex_le_set
);
948 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
949 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
951 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
954 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
955 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
957 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
960 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
961 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
963 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
966 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
967 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
969 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
972 static isl_stat
intersect_domain_entry(void **entry
, void *user
)
974 struct isl_union_map_gen_bin_data
*data
= user
;
976 struct isl_hash_table_entry
*entry2
;
978 isl_map
*map
= *entry
;
981 dim
= isl_map_get_space(map
);
982 dim
= isl_space_domain(dim
);
983 hash
= isl_space_get_hash(dim
);
984 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
985 hash
, &has_dim
, dim
, 0);
990 map
= isl_map_copy(map
);
991 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
993 empty
= isl_map_is_empty(map
);
996 return isl_stat_error
;
1003 data
->res
= isl_union_map_add_map(data
->res
, map
);
1008 /* Intersect the domain of "umap" with "uset".
1009 * If "uset" is a parameters domain, then intersect the parameter
1010 * domain of "umap" with this set.
1012 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1013 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1015 if (isl_union_set_is_params(uset
))
1016 return union_map_intersect_params(umap
, uset
);
1017 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
1020 /* Remove the elements of data->umap2 from the domain of *entry
1021 * and add the result to data->res.
1023 static isl_stat
subtract_domain_entry(void **entry
, void *user
)
1025 struct isl_union_map_gen_bin_data
*data
= user
;
1027 struct isl_hash_table_entry
*entry2
;
1029 isl_map
*map
= *entry
;
1032 dim
= isl_map_get_space(map
);
1033 dim
= isl_space_domain(dim
);
1034 hash
= isl_space_get_hash(dim
);
1035 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1036 hash
, &has_dim
, dim
, 0);
1037 isl_space_free(dim
);
1039 map
= isl_map_copy(map
);
1042 data
->res
= isl_union_map_add_map(data
->res
, map
);
1046 map
= isl_map_subtract_domain(map
, isl_set_copy(entry2
->data
));
1048 empty
= isl_map_is_empty(map
);
1051 return isl_stat_error
;
1058 data
->res
= isl_union_map_add_map(data
->res
, map
);
1063 /* Remove the elements of "uset" from the domain of "umap".
1065 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1066 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1068 return gen_bin_op(umap
, dom
, &subtract_domain_entry
);
1071 /* Remove the elements of data->umap2 from the range of *entry
1072 * and add the result to data->res.
1074 static isl_stat
subtract_range_entry(void **entry
, void *user
)
1076 struct isl_union_map_gen_bin_data
*data
= user
;
1078 struct isl_hash_table_entry
*entry2
;
1080 isl_map
*map
= *entry
;
1083 space
= isl_map_get_space(map
);
1084 space
= isl_space_range(space
);
1085 hash
= isl_space_get_hash(space
);
1086 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1087 hash
, &has_dim
, space
, 0);
1088 isl_space_free(space
);
1090 map
= isl_map_copy(map
);
1093 data
->res
= isl_union_map_add_map(data
->res
, map
);
1097 map
= isl_map_subtract_range(map
, isl_set_copy(entry2
->data
));
1099 empty
= isl_map_is_empty(map
);
1102 return isl_stat_error
;
1109 data
->res
= isl_union_map_add_map(data
->res
, map
);
1114 /* Remove the elements of "uset" from the range of "umap".
1116 __isl_give isl_union_map
*isl_union_map_subtract_range(
1117 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1119 return gen_bin_op(umap
, dom
, &subtract_range_entry
);
1122 static isl_stat
gist_domain_entry(void **entry
, void *user
)
1124 struct isl_union_map_gen_bin_data
*data
= user
;
1126 struct isl_hash_table_entry
*entry2
;
1128 isl_map
*map
= *entry
;
1131 dim
= isl_map_get_space(map
);
1132 dim
= isl_space_domain(dim
);
1133 hash
= isl_space_get_hash(dim
);
1134 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1135 hash
, &has_dim
, dim
, 0);
1136 isl_space_free(dim
);
1140 map
= isl_map_copy(map
);
1141 map
= isl_map_gist_domain(map
, isl_set_copy(entry2
->data
));
1143 empty
= isl_map_is_empty(map
);
1146 return isl_stat_error
;
1149 data
->res
= isl_union_map_add_map(data
->res
, map
);
1154 /* Compute the gist of "umap" with respect to the domain "uset".
1155 * If "uset" is a parameters domain, then compute the gist
1156 * with respect to this parameter domain.
1158 __isl_give isl_union_map
*isl_union_map_gist_domain(
1159 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1161 if (isl_union_set_is_params(uset
))
1162 return union_map_gist_params(umap
, uset
);
1163 return gen_bin_op(umap
, uset
, &gist_domain_entry
);
1166 static isl_stat
gist_range_entry(void **entry
, void *user
)
1168 struct isl_union_map_gen_bin_data
*data
= user
;
1170 struct isl_hash_table_entry
*entry2
;
1172 isl_map
*map
= *entry
;
1175 space
= isl_map_get_space(map
);
1176 space
= isl_space_range(space
);
1177 hash
= isl_space_get_hash(space
);
1178 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1179 hash
, &has_dim
, space
, 0);
1180 isl_space_free(space
);
1184 map
= isl_map_copy(map
);
1185 map
= isl_map_gist_range(map
, isl_set_copy(entry2
->data
));
1187 empty
= isl_map_is_empty(map
);
1190 return isl_stat_error
;
1193 data
->res
= isl_union_map_add_map(data
->res
, map
);
1198 /* Compute the gist of "umap" with respect to the range "uset".
1200 __isl_give isl_union_map
*isl_union_map_gist_range(
1201 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1203 return gen_bin_op(umap
, uset
, &gist_range_entry
);
1206 static isl_stat
intersect_range_entry(void **entry
, void *user
)
1208 struct isl_union_map_gen_bin_data
*data
= user
;
1210 struct isl_hash_table_entry
*entry2
;
1212 isl_map
*map
= *entry
;
1215 dim
= isl_map_get_space(map
);
1216 dim
= isl_space_range(dim
);
1217 hash
= isl_space_get_hash(dim
);
1218 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1219 hash
, &has_dim
, dim
, 0);
1220 isl_space_free(dim
);
1224 map
= isl_map_copy(map
);
1225 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
1227 empty
= isl_map_is_empty(map
);
1230 return isl_stat_error
;
1237 data
->res
= isl_union_map_add_map(data
->res
, map
);
1242 __isl_give isl_union_map
*isl_union_map_intersect_range(
1243 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1245 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
1248 struct isl_union_map_bin_data
{
1249 isl_union_map
*umap2
;
1252 isl_stat (*fn
)(void **entry
, void *user
);
1255 static isl_stat
apply_range_entry(void **entry
, void *user
)
1257 struct isl_union_map_bin_data
*data
= user
;
1258 isl_map
*map2
= *entry
;
1261 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1262 map2
->dim
, isl_dim_in
))
1265 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1267 empty
= isl_map_is_empty(map2
);
1270 return isl_stat_error
;
1277 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1282 static isl_stat
bin_entry(void **entry
, void *user
)
1284 struct isl_union_map_bin_data
*data
= user
;
1285 isl_map
*map
= *entry
;
1288 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1289 data
->fn
, data
) < 0)
1290 return isl_stat_error
;
1295 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1296 __isl_take isl_union_map
*umap2
,
1297 isl_stat (*fn
)(void **entry
, void *user
))
1299 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1301 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1302 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1304 if (!umap1
|| !umap2
)
1308 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1310 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1311 &bin_entry
, &data
) < 0)
1314 isl_union_map_free(umap1
);
1315 isl_union_map_free(umap2
);
1318 isl_union_map_free(umap1
);
1319 isl_union_map_free(umap2
);
1320 isl_union_map_free(data
.res
);
1324 __isl_give isl_union_map
*isl_union_map_apply_range(
1325 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1327 return bin_op(umap1
, umap2
, &apply_range_entry
);
1330 __isl_give isl_union_map
*isl_union_map_apply_domain(
1331 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1333 umap1
= isl_union_map_reverse(umap1
);
1334 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1335 return isl_union_map_reverse(umap1
);
1338 __isl_give isl_union_set
*isl_union_set_apply(
1339 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1341 return isl_union_map_apply_range(uset
, umap
);
1344 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1346 struct isl_union_map_bin_data
*data
= user
;
1347 isl_map
*map2
= *entry
;
1349 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1350 map2
->dim
, isl_dim_out
))
1353 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1355 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1360 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1361 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1363 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1366 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1368 struct isl_union_map_bin_data
*data
= user
;
1369 isl_map
*map2
= *entry
;
1371 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1372 map2
->dim
, isl_dim_out
))
1375 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1377 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1382 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1383 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1385 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1388 static isl_stat
product_entry(void **entry
, void *user
)
1390 struct isl_union_map_bin_data
*data
= user
;
1391 isl_map
*map2
= *entry
;
1393 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1395 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1400 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1401 __isl_take isl_union_map
*umap2
)
1403 return bin_op(umap1
, umap2
, &product_entry
);
1406 static isl_stat
set_product_entry(void **entry
, void *user
)
1408 struct isl_union_map_bin_data
*data
= user
;
1409 isl_set
*set2
= *entry
;
1411 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1413 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1418 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1419 __isl_take isl_union_set
*uset2
)
1421 return bin_op(uset1
, uset2
, &set_product_entry
);
1424 static isl_stat
domain_product_entry(void **entry
, void *user
)
1426 struct isl_union_map_bin_data
*data
= user
;
1427 isl_map
*map2
= *entry
;
1429 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1430 map2
->dim
, isl_dim_out
))
1433 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1434 isl_map_copy(map2
));
1436 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1441 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1443 __isl_give isl_union_map
*isl_union_map_domain_product(
1444 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1446 return bin_op(umap1
, umap2
, &domain_product_entry
);
1449 static isl_stat
range_product_entry(void **entry
, void *user
)
1451 struct isl_union_map_bin_data
*data
= user
;
1452 isl_map
*map2
= *entry
;
1454 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1455 map2
->dim
, isl_dim_in
))
1458 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1459 isl_map_copy(map2
));
1461 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1466 __isl_give isl_union_map
*isl_union_map_range_product(
1467 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1469 return bin_op(umap1
, umap2
, &range_product_entry
);
1472 /* If data->map A -> B and "map2" C -> D have the same range space,
1473 * then add (A, C) -> (B * D) to data->res.
1475 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1477 struct isl_union_map_bin_data
*data
= user
;
1478 isl_map
*map2
= *entry
;
1480 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1481 map2
->dim
, isl_dim_out
))
1484 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1485 isl_map_copy(map2
));
1487 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1492 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1494 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1495 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1497 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1500 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1502 struct isl_union_map_bin_data
*data
= user
;
1503 isl_map
*map2
= *entry
;
1505 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1506 map2
->dim
, isl_dim_in
))
1509 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1510 isl_map_copy(map2
));
1512 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1517 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1518 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1520 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1523 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1524 isl_stat (*fn
)(void **, void *))
1532 space
= isl_union_map_get_space(umap
);
1533 res
= isl_union_map_alloc(space
, umap
->table
.n
);
1534 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1535 &umap
->table
, fn
, &res
) < 0)
1538 isl_union_map_free(umap
);
1541 isl_union_map_free(umap
);
1542 isl_union_set_free(res
);
1546 static isl_stat
from_range_entry(void **entry
, void *user
)
1548 isl_map
*set
= *entry
;
1549 isl_union_set
**res
= user
;
1551 *res
= isl_union_map_add_map(*res
,
1552 isl_map_from_range(isl_set_copy(set
)));
1557 __isl_give isl_union_map
*isl_union_map_from_range(
1558 __isl_take isl_union_set
*uset
)
1560 return cond_un_op(uset
, &from_range_entry
);
1563 __isl_give isl_union_map
*isl_union_map_from_domain(
1564 __isl_take isl_union_set
*uset
)
1566 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1569 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1570 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1572 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1573 isl_union_map_from_range(range
));
1576 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1577 isl_stat (*fn
)(void **, void *))
1579 umap
= isl_union_map_cow(umap
);
1583 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1584 &umap
->table
, fn
, NULL
) < 0)
1589 isl_union_map_free(umap
);
1593 static isl_stat
affine_entry(void **entry
, void *user
)
1595 isl_map
**map
= (isl_map
**)entry
;
1597 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1599 return *map
? isl_stat_ok
: isl_stat_error
;
1602 __isl_give isl_union_map
*isl_union_map_affine_hull(
1603 __isl_take isl_union_map
*umap
)
1605 return un_op(umap
, &affine_entry
);
1608 __isl_give isl_union_set
*isl_union_set_affine_hull(
1609 __isl_take isl_union_set
*uset
)
1611 return isl_union_map_affine_hull(uset
);
1614 static isl_stat
polyhedral_entry(void **entry
, void *user
)
1616 isl_map
**map
= (isl_map
**)entry
;
1618 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1620 return *map
? isl_stat_ok
: isl_stat_error
;
1623 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1624 __isl_take isl_union_map
*umap
)
1626 return un_op(umap
, &polyhedral_entry
);
1629 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1630 __isl_take isl_union_set
*uset
)
1632 return isl_union_map_polyhedral_hull(uset
);
1635 static isl_stat
simple_entry(void **entry
, void *user
)
1637 isl_map
**map
= (isl_map
**)entry
;
1639 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1641 return *map
? isl_stat_ok
: isl_stat_error
;
1644 __isl_give isl_union_map
*isl_union_map_simple_hull(
1645 __isl_take isl_union_map
*umap
)
1647 return un_op(umap
, &simple_entry
);
1650 __isl_give isl_union_set
*isl_union_set_simple_hull(
1651 __isl_take isl_union_set
*uset
)
1653 return isl_union_map_simple_hull(uset
);
1656 static isl_stat
inplace_entry(void **entry
, void *user
)
1658 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1659 isl_map
**map
= (isl_map
**)entry
;
1662 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1663 copy
= fn(isl_map_copy(*map
));
1665 return isl_stat_error
;
1673 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1674 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1679 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
), &umap
->table
,
1680 &inplace_entry
, &fn
) < 0)
1685 isl_union_map_free(umap
);
1689 /* Remove redundant constraints in each of the basic maps of "umap".
1690 * Since removing redundant constraints does not change the meaning
1691 * or the space, the operation can be performed in-place.
1693 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1694 __isl_take isl_union_map
*umap
)
1696 return inplace(umap
, &isl_map_remove_redundancies
);
1699 /* Remove redundant constraints in each of the basic sets of "uset".
1701 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1702 __isl_take isl_union_set
*uset
)
1704 return isl_union_map_remove_redundancies(uset
);
1707 __isl_give isl_union_map
*isl_union_map_coalesce(
1708 __isl_take isl_union_map
*umap
)
1710 return inplace(umap
, &isl_map_coalesce
);
1713 __isl_give isl_union_set
*isl_union_set_coalesce(
1714 __isl_take isl_union_set
*uset
)
1716 return isl_union_map_coalesce(uset
);
1719 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1720 __isl_take isl_union_map
*umap
)
1722 return inplace(umap
, &isl_map_detect_equalities
);
1725 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1726 __isl_take isl_union_set
*uset
)
1728 return isl_union_map_detect_equalities(uset
);
1731 __isl_give isl_union_map
*isl_union_map_compute_divs(
1732 __isl_take isl_union_map
*umap
)
1734 return inplace(umap
, &isl_map_compute_divs
);
1737 __isl_give isl_union_set
*isl_union_set_compute_divs(
1738 __isl_take isl_union_set
*uset
)
1740 return isl_union_map_compute_divs(uset
);
1743 static isl_stat
lexmin_entry(void **entry
, void *user
)
1745 isl_map
**map
= (isl_map
**)entry
;
1747 *map
= isl_map_lexmin(*map
);
1749 return *map
? isl_stat_ok
: isl_stat_error
;
1752 __isl_give isl_union_map
*isl_union_map_lexmin(
1753 __isl_take isl_union_map
*umap
)
1755 return un_op(umap
, &lexmin_entry
);
1758 __isl_give isl_union_set
*isl_union_set_lexmin(
1759 __isl_take isl_union_set
*uset
)
1761 return isl_union_map_lexmin(uset
);
1764 static isl_stat
lexmax_entry(void **entry
, void *user
)
1766 isl_map
**map
= (isl_map
**)entry
;
1768 *map
= isl_map_lexmax(*map
);
1770 return *map
? isl_stat_ok
: isl_stat_error
;
1773 __isl_give isl_union_map
*isl_union_map_lexmax(
1774 __isl_take isl_union_map
*umap
)
1776 return un_op(umap
, &lexmax_entry
);
1779 __isl_give isl_union_set
*isl_union_set_lexmax(
1780 __isl_take isl_union_set
*uset
)
1782 return isl_union_map_lexmax(uset
);
1785 static isl_stat
universe_entry(void **entry
, void *user
)
1787 isl_map
*map
= *entry
;
1788 isl_union_map
**res
= user
;
1790 map
= isl_map_universe(isl_map_get_space(map
));
1791 *res
= isl_union_map_add_map(*res
, map
);
1796 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1798 return cond_un_op(umap
, &universe_entry
);
1801 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1803 return isl_union_map_universe(uset
);
1806 static isl_stat
reverse_entry(void **entry
, void *user
)
1808 isl_map
*map
= *entry
;
1809 isl_union_map
**res
= user
;
1811 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1816 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1818 return cond_un_op(umap
, &reverse_entry
);
1821 static isl_stat
params_entry(void **entry
, void *user
)
1823 isl_map
*map
= *entry
;
1824 isl_union_set
**res
= user
;
1826 *res
= isl_union_set_add_set(*res
, isl_map_params(isl_map_copy(map
)));
1831 /* Compute the parameter domain of the given union map.
1833 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1837 empty
= isl_union_map_is_empty(umap
);
1842 space
= isl_union_map_get_space(umap
);
1843 isl_union_map_free(umap
);
1844 return isl_set_empty(space
);
1846 return isl_set_from_union_set(cond_un_op(umap
, ¶ms_entry
));
1848 isl_union_map_free(umap
);
1852 /* Compute the parameter domain of the given union set.
1854 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1856 return isl_union_map_params(uset
);
1859 static isl_stat
domain_entry(void **entry
, void *user
)
1861 isl_map
*map
= *entry
;
1862 isl_union_set
**res
= user
;
1864 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1869 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1871 return cond_un_op(umap
, &domain_entry
);
1874 static isl_stat
range_entry(void **entry
, void *user
)
1876 isl_map
*map
= *entry
;
1877 isl_union_set
**res
= user
;
1879 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1884 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1886 return cond_un_op(umap
, &range_entry
);
1889 static isl_stat
domain_map_entry(void **entry
, void *user
)
1891 isl_map
*map
= *entry
;
1892 isl_union_set
**res
= user
;
1894 *res
= isl_union_map_add_map(*res
,
1895 isl_map_domain_map(isl_map_copy(map
)));
1900 __isl_give isl_union_map
*isl_union_map_domain_map(
1901 __isl_take isl_union_map
*umap
)
1903 return cond_un_op(umap
, &domain_map_entry
);
1906 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1907 * add the result to "res".
1909 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
1911 isl_union_pw_multi_aff
**res
= user
;
1913 isl_pw_multi_aff
*pma
;
1915 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
1916 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
1917 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1919 return *res
? isl_stat_ok
: isl_stat_error
;
1923 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1926 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
1927 __isl_take isl_union_map
*umap
)
1929 isl_union_pw_multi_aff
*res
;
1931 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
1932 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
1933 res
= isl_union_pw_multi_aff_free(res
);
1935 isl_union_map_free(umap
);
1939 static isl_stat
range_map_entry(void **entry
, void *user
)
1941 isl_map
*map
= *entry
;
1942 isl_union_set
**res
= user
;
1944 *res
= isl_union_map_add_map(*res
,
1945 isl_map_range_map(isl_map_copy(map
)));
1950 __isl_give isl_union_map
*isl_union_map_range_map(
1951 __isl_take isl_union_map
*umap
)
1953 return cond_un_op(umap
, &range_map_entry
);
1956 /* Check if "set" is of the form A[B -> C].
1957 * If so, add A[B -> C] -> B to "res".
1959 static isl_stat
wrapped_domain_map_entry(void **entry
, void *user
)
1961 isl_set
*set
= *entry
;
1962 isl_union_set
**res
= user
;
1965 wrapping
= isl_set_is_wrapping(set
);
1967 return isl_stat_error
;
1971 *res
= isl_union_map_add_map(*res
,
1972 isl_set_wrapped_domain_map(isl_set_copy(set
)));
1977 /* Given a collection of wrapped maps of the form A[B -> C],
1978 * return the collection of maps A[B -> C] -> B.
1980 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
1981 __isl_take isl_union_set
*uset
)
1983 return cond_un_op(uset
, &wrapped_domain_map_entry
);
1986 static isl_stat
deltas_entry(void **entry
, void *user
)
1988 isl_map
*map
= *entry
;
1989 isl_union_set
**res
= user
;
1991 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1992 map
->dim
, isl_dim_out
))
1995 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
2000 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
2002 return cond_un_op(umap
, &deltas_entry
);
2005 static isl_stat
deltas_map_entry(void **entry
, void *user
)
2007 isl_map
*map
= *entry
;
2008 isl_union_map
**res
= user
;
2010 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2011 map
->dim
, isl_dim_out
))
2014 *res
= isl_union_map_add_map(*res
,
2015 isl_map_deltas_map(isl_map_copy(map
)));
2020 __isl_give isl_union_map
*isl_union_map_deltas_map(
2021 __isl_take isl_union_map
*umap
)
2023 return cond_un_op(umap
, &deltas_map_entry
);
2026 static isl_stat
identity_entry(void **entry
, void *user
)
2028 isl_set
*set
= *entry
;
2029 isl_union_map
**res
= user
;
2031 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
2036 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
2038 return cond_un_op(uset
, &identity_entry
);
2041 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2043 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
2045 isl_union_pw_multi_aff
**res
= user
;
2047 isl_pw_multi_aff
*pma
;
2049 space
= isl_space_map_from_set(isl_set_get_space(set
));
2050 pma
= isl_pw_multi_aff_identity(space
);
2051 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
2052 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2054 return *res
? isl_stat_ok
: isl_stat_error
;
2057 /* Return an identity function on "uset" in the form
2058 * of an isl_union_pw_multi_aff.
2060 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
2061 __isl_take isl_union_set
*uset
)
2063 isl_union_pw_multi_aff
*res
;
2065 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
2066 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
2067 res
= isl_union_pw_multi_aff_free(res
);
2069 isl_union_set_free(uset
);
2073 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
2075 static isl_stat
domain_factor_domain_entry(void **entry
, void *user
)
2077 isl_map
*map
= *entry
;
2078 isl_union_map
**res
= user
;
2080 if (!isl_map_domain_is_wrapping(map
))
2083 *res
= isl_union_map_add_map(*res
,
2084 isl_map_domain_factor_domain(isl_map_copy(map
)));
2086 return *res
? isl_stat_ok
: isl_stat_error
;
2089 /* For each map in "umap" of the form [A -> B] -> C,
2090 * construct the map A -> C and collect the results.
2092 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2093 __isl_take isl_union_map
*umap
)
2095 return cond_un_op(umap
, &domain_factor_domain_entry
);
2098 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2100 static isl_stat
domain_factor_range_entry(void **entry
, void *user
)
2102 isl_map
*map
= *entry
;
2103 isl_union_map
**res
= user
;
2105 if (!isl_map_domain_is_wrapping(map
))
2108 *res
= isl_union_map_add_map(*res
,
2109 isl_map_domain_factor_range(isl_map_copy(map
)));
2111 return *res
? isl_stat_ok
: isl_stat_error
;
2114 /* For each map in "umap" of the form [A -> B] -> C,
2115 * construct the map B -> C and collect the results.
2117 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2118 __isl_take isl_union_map
*umap
)
2120 return cond_un_op(umap
, &domain_factor_range_entry
);
2123 /* If "map" is of the form A -> [B -> C], then add A -> B to "res".
2125 static isl_stat
range_factor_domain_entry(void **entry
, void *user
)
2127 isl_map
*map
= *entry
;
2128 isl_union_map
**res
= user
;
2130 if (!isl_map_range_is_wrapping(map
))
2133 *res
= isl_union_map_add_map(*res
,
2134 isl_map_range_factor_domain(isl_map_copy(map
)));
2136 return *res
? isl_stat_ok
: isl_stat_error
;
2139 /* For each map in "umap" of the form A -> [B -> C],
2140 * construct the map A -> B and collect the results.
2142 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2143 __isl_take isl_union_map
*umap
)
2145 return cond_un_op(umap
, &range_factor_domain_entry
);
2148 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2150 static isl_stat
range_factor_range_entry(void **entry
, void *user
)
2152 isl_map
*map
= *entry
;
2153 isl_union_map
**res
= user
;
2155 if (!isl_map_range_is_wrapping(map
))
2158 *res
= isl_union_map_add_map(*res
,
2159 isl_map_range_factor_range(isl_map_copy(map
)));
2161 return *res
? isl_stat_ok
: isl_stat_error
;
2164 /* For each map in "umap" of the form A -> [B -> C],
2165 * construct the map A -> C and collect the results.
2167 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2168 __isl_take isl_union_map
*umap
)
2170 return cond_un_op(umap
, &range_factor_range_entry
);
2173 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2175 static isl_stat
factor_domain_entry(void **entry
, void *user
)
2177 isl_map
*map
= *entry
;
2178 isl_union_map
**res
= user
;
2180 if (!isl_map_is_product(map
))
2183 *res
= isl_union_map_add_map(*res
,
2184 isl_map_factor_domain(isl_map_copy(map
)));
2186 return *res
? isl_stat_ok
: isl_stat_error
;
2189 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2190 * construct the map A -> C and collect the results.
2192 __isl_give isl_union_map
*isl_union_map_factor_domain(
2193 __isl_take isl_union_map
*umap
)
2195 return cond_un_op(umap
, &factor_domain_entry
);
2198 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2200 static isl_stat
factor_range_entry(void **entry
, void *user
)
2202 isl_map
*map
= *entry
;
2203 isl_union_map
**res
= user
;
2205 if (!isl_map_is_product(map
))
2208 *res
= isl_union_map_add_map(*res
,
2209 isl_map_factor_range(isl_map_copy(map
)));
2211 return *res
? isl_stat_ok
: isl_stat_error
;
2214 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2215 * construct the map B -> D and collect the results.
2217 __isl_give isl_union_map
*isl_union_map_factor_range(
2218 __isl_take isl_union_map
*umap
)
2220 return cond_un_op(umap
, &factor_range_entry
);
2223 static isl_stat
unwrap_entry(void **entry
, void *user
)
2225 isl_set
*set
= *entry
;
2226 isl_union_set
**res
= user
;
2228 if (!isl_set_is_wrapping(set
))
2231 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
2236 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2238 return cond_un_op(uset
, &unwrap_entry
);
2241 static isl_stat
wrap_entry(void **entry
, void *user
)
2243 isl_map
*map
= *entry
;
2244 isl_union_set
**res
= user
;
2246 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
2251 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2253 return cond_un_op(umap
, &wrap_entry
);
2256 struct isl_union_map_is_subset_data
{
2257 isl_union_map
*umap2
;
2261 static isl_stat
is_subset_entry(void **entry
, void *user
)
2263 struct isl_union_map_is_subset_data
*data
= user
;
2265 struct isl_hash_table_entry
*entry2
;
2266 isl_map
*map
= *entry
;
2268 hash
= isl_space_get_hash(map
->dim
);
2269 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2270 hash
, &has_dim
, map
->dim
, 0);
2272 int empty
= isl_map_is_empty(map
);
2274 return isl_stat_error
;
2277 data
->is_subset
= 0;
2278 return isl_stat_error
;
2281 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2282 if (data
->is_subset
< 0 || !data
->is_subset
)
2283 return isl_stat_error
;
2288 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2289 __isl_keep isl_union_map
*umap2
)
2291 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2293 umap1
= isl_union_map_copy(umap1
);
2294 umap2
= isl_union_map_copy(umap2
);
2295 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2296 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2298 if (!umap1
|| !umap2
)
2302 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2303 &is_subset_entry
, &data
) < 0 &&
2307 isl_union_map_free(umap1
);
2308 isl_union_map_free(umap2
);
2310 return data
.is_subset
;
2312 isl_union_map_free(umap1
);
2313 isl_union_map_free(umap2
);
2314 return isl_bool_error
;
2317 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2318 __isl_keep isl_union_set
*uset2
)
2320 return isl_union_map_is_subset(uset1
, uset2
);
2323 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2324 __isl_keep isl_union_map
*umap2
)
2328 if (!umap1
|| !umap2
)
2329 return isl_bool_error
;
2330 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2331 if (is_subset
!= isl_bool_true
)
2333 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2337 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2338 __isl_keep isl_union_set
*uset2
)
2340 return isl_union_map_is_equal(uset1
, uset2
);
2343 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2344 __isl_keep isl_union_map
*umap2
)
2348 if (!umap1
|| !umap2
)
2349 return isl_bool_error
;
2350 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2351 if (is_subset
!= isl_bool_true
)
2353 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2354 if (is_subset
== isl_bool_error
)
2359 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2360 __isl_keep isl_union_set
*uset2
)
2362 return isl_union_map_is_strict_subset(uset1
, uset2
);
2365 /* Internal data structure for isl_union_map_is_disjoint.
2366 * umap2 is the union map with which we are comparing.
2367 * is_disjoint is initialized to 1 and is set to 0 as soon
2368 * as the union maps turn out not to be disjoint.
2370 struct isl_union_map_is_disjoint_data
{
2371 isl_union_map
*umap2
;
2372 isl_bool is_disjoint
;
2375 /* Check if "map" is disjoint from data->umap2 and abort
2376 * the search if it is not.
2378 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2380 struct isl_union_map_is_disjoint_data
*data
= user
;
2382 struct isl_hash_table_entry
*entry2
;
2383 isl_map
*map
= *entry
;
2385 hash
= isl_space_get_hash(map
->dim
);
2386 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2387 hash
, &has_dim
, map
->dim
, 0);
2391 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2392 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2393 return isl_stat_error
;
2398 /* Are "umap1" and "umap2" disjoint?
2400 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2401 __isl_keep isl_union_map
*umap2
)
2403 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2405 umap1
= isl_union_map_copy(umap1
);
2406 umap2
= isl_union_map_copy(umap2
);
2407 umap1
= isl_union_map_align_params(umap1
,
2408 isl_union_map_get_space(umap2
));
2409 umap2
= isl_union_map_align_params(umap2
,
2410 isl_union_map_get_space(umap1
));
2412 if (!umap1
|| !umap2
)
2416 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2417 &is_disjoint_entry
, &data
) < 0 &&
2421 isl_union_map_free(umap1
);
2422 isl_union_map_free(umap2
);
2424 return data
.is_disjoint
;
2426 isl_union_map_free(umap1
);
2427 isl_union_map_free(umap2
);
2428 return isl_bool_error
;
2431 /* Are "uset1" and "uset2" disjoint?
2433 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2434 __isl_keep isl_union_set
*uset2
)
2436 return isl_union_map_is_disjoint(uset1
, uset2
);
2439 static isl_stat
sample_entry(void **entry
, void *user
)
2441 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2442 isl_map
*map
= *entry
;
2444 *sample
= isl_map_sample(isl_map_copy(map
));
2446 return isl_stat_error
;
2447 if (!isl_basic_map_plain_is_empty(*sample
))
2448 return isl_stat_error
;
2452 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2454 isl_basic_map
*sample
= NULL
;
2459 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2460 &sample_entry
, &sample
) < 0 &&
2465 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2467 isl_union_map_free(umap
);
2471 isl_union_map_free(umap
);
2475 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2477 return bset_from_bmap(isl_union_map_sample(uset
));
2480 /* Return an element in "uset" in the form of an isl_point.
2481 * Return a void isl_point if "uset" is empty.
2483 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2485 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2488 struct isl_forall_data
{
2490 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2493 static isl_stat
forall_entry(void **entry
, void *user
)
2495 struct isl_forall_data
*data
= user
;
2496 isl_map
*map
= *entry
;
2498 data
->res
= data
->fn(map
);
2500 return isl_stat_error
;
2503 return isl_stat_error
;
2508 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2509 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2511 struct isl_forall_data data
= { isl_bool_true
, fn
};
2514 return isl_bool_error
;
2516 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2517 &forall_entry
, &data
) < 0 && data
.res
)
2518 return isl_bool_error
;
2523 struct isl_forall_user_data
{
2525 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2529 static isl_stat
forall_user_entry(void **entry
, void *user
)
2531 struct isl_forall_user_data
*data
= user
;
2532 isl_map
*map
= *entry
;
2534 data
->res
= data
->fn(map
, data
->user
);
2536 return isl_stat_error
;
2539 return isl_stat_error
;
2544 /* Check if fn(map, user) returns true for all maps "map" in umap.
2546 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2547 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2549 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2552 return isl_bool_error
;
2554 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2555 &forall_user_entry
, &data
) < 0 && data
.res
)
2556 return isl_bool_error
;
2561 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2563 return union_map_forall(umap
, &isl_map_is_empty
);
2566 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2568 return isl_union_map_is_empty(uset
);
2571 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2578 return isl_bool_error
;
2580 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2581 map
->dim
, isl_dim_out
))
2582 return isl_bool_false
;
2584 dim
= isl_map_get_space(map
);
2585 id
= isl_map_identity(dim
);
2587 is_subset
= isl_map_is_subset(map
, id
);
2594 /* Given an isl_union_map that consists of a single map, check
2595 * if it is single-valued.
2597 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2602 umap
= isl_union_map_copy(umap
);
2603 map
= isl_map_from_union_map(umap
);
2604 sv
= isl_map_is_single_valued(map
);
2610 /* Internal data structure for single_valued_on_domain.
2612 * "umap" is the union map to be tested.
2613 * "sv" is set to 1 as long as "umap" may still be single-valued.
2615 struct isl_union_map_is_sv_data
{
2616 isl_union_map
*umap
;
2620 /* Check if the data->umap is single-valued on "set".
2622 * If data->umap consists of a single map on "set", then test it
2625 * Otherwise, compute
2629 * check if the result is a subset of the identity mapping and
2630 * store the result in data->sv.
2632 * Terminate as soon as data->umap has been determined not to
2635 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2637 struct isl_union_map_is_sv_data
*data
= user
;
2638 isl_union_map
*umap
, *test
;
2640 umap
= isl_union_map_copy(data
->umap
);
2641 umap
= isl_union_map_intersect_domain(umap
,
2642 isl_union_set_from_set(set
));
2644 if (isl_union_map_n_map(umap
) == 1) {
2645 data
->sv
= single_map_is_single_valued(umap
);
2646 isl_union_map_free(umap
);
2648 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2649 test
= isl_union_map_apply_range(test
, umap
);
2651 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2653 isl_union_map_free(test
);
2656 if (data
->sv
< 0 || !data
->sv
)
2657 return isl_stat_error
;
2661 /* Check if the given map is single-valued.
2663 * If the union map consists of a single map, then test it as an isl_map.
2664 * Otherwise, check if the union map is single-valued on each of its
2667 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2669 isl_union_map
*universe
;
2670 isl_union_set
*domain
;
2671 struct isl_union_map_is_sv_data data
;
2673 if (isl_union_map_n_map(umap
) == 1)
2674 return single_map_is_single_valued(umap
);
2676 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2677 domain
= isl_union_map_domain(universe
);
2679 data
.sv
= isl_bool_true
;
2681 if (isl_union_set_foreach_set(domain
,
2682 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2683 data
.sv
= isl_bool_error
;
2684 isl_union_set_free(domain
);
2689 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2693 umap
= isl_union_map_copy(umap
);
2694 umap
= isl_union_map_reverse(umap
);
2695 in
= isl_union_map_is_single_valued(umap
);
2696 isl_union_map_free(umap
);
2701 /* Is "map" obviously not an identity relation because
2702 * it maps elements from one space to another space?
2703 * Update *non_identity accordingly.
2705 * In particular, if the domain and range spaces are the same,
2706 * then the map is not considered to obviously not be an identity relation.
2707 * Otherwise, the map is considered to obviously not be an identity relation
2708 * if it is is non-empty.
2710 * If "map" is determined to obviously not be an identity relation,
2711 * then the search is aborted.
2713 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2715 isl_bool
*non_identity
= user
;
2719 space
= isl_map_get_space(map
);
2720 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2721 if (equal
>= 0 && !equal
)
2722 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2724 *non_identity
= isl_bool_not(equal
);
2725 isl_space_free(space
);
2728 if (*non_identity
< 0 || *non_identity
)
2729 return isl_stat_error
;
2734 /* Is "umap" obviously not an identity relation because
2735 * it maps elements from one space to another space?
2737 * As soon as a map has been found that maps elements to a different space,
2738 * non_identity is changed and the search is aborted.
2740 static isl_bool
isl_union_map_plain_is_not_identity(
2741 __isl_keep isl_union_map
*umap
)
2743 isl_bool non_identity
;
2745 non_identity
= isl_bool_false
;
2746 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2747 &non_identity
) < 0 &&
2748 non_identity
== isl_bool_false
)
2749 return isl_bool_error
;
2751 return non_identity
;
2754 /* Does "map" only map elements to themselves?
2755 * Update *identity accordingly.
2757 * If "map" is determined not to be an identity relation,
2758 * then the search is aborted.
2760 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2762 isl_bool
*identity
= user
;
2764 *identity
= isl_map_is_identity(map
);
2767 if (*identity
< 0 || !*identity
)
2768 return isl_stat_error
;
2773 /* Does "umap" only map elements to themselves?
2775 * First check if there are any maps that map elements to different spaces.
2776 * If not, then check that all the maps (between identical spaces)
2777 * are identity relations.
2779 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2781 isl_bool non_identity
;
2784 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2785 if (non_identity
< 0 || non_identity
)
2786 return isl_bool_not(non_identity
);
2788 identity
= isl_bool_true
;
2789 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2790 identity
== isl_bool_true
)
2791 return isl_bool_error
;
2796 /* Represents a map that has a fixed value (v) for one of its
2798 * The map in this structure is not reference counted, so it
2799 * is only valid while the isl_union_map from which it was
2800 * obtained is still alive.
2802 struct isl_fixed_map
{
2807 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2811 struct isl_fixed_map
*v
;
2813 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2816 for (i
= 0; i
< n
; ++i
)
2817 isl_int_init(v
[i
].v
);
2821 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2827 for (i
= 0; i
< n
; ++i
)
2828 isl_int_clear(v
[i
].v
);
2832 /* Compare the "v" field of two isl_fixed_map structs.
2834 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2836 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2837 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2839 return isl_int_cmp(e1
->v
, e2
->v
);
2842 /* Internal data structure used while checking whether all maps
2843 * in a union_map have a fixed value for a given output dimension.
2844 * v is the list of maps, with the fixed value for the dimension
2845 * n is the number of maps considered so far
2846 * pos is the output dimension under investigation
2848 struct isl_fixed_dim_data
{
2849 struct isl_fixed_map
*v
;
2854 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2856 struct isl_fixed_dim_data
*data
= user
;
2858 data
->v
[data
->n
].map
= map
;
2859 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2860 &data
->v
[data
->n
++].v
);
2863 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2864 int first
, int n_range
);
2866 /* Given a list of the maps, with their fixed values at output dimension "pos",
2867 * check whether the ranges of the maps form an obvious partition.
2869 * We first sort the maps according to their fixed values.
2870 * If all maps have a different value, then we know the ranges form
2872 * Otherwise, we collect the maps with the same fixed value and
2873 * check whether each such collection is obviously injective
2874 * based on later dimensions.
2876 static int separates(struct isl_fixed_map
*v
, int n
,
2877 __isl_take isl_space
*dim
, int pos
, int n_range
)
2884 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2886 for (i
= 0; i
+ 1 < n
; ++i
) {
2888 isl_union_map
*part
;
2891 for (j
= i
+ 1; j
< n
; ++j
)
2892 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2898 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2899 for (k
= i
; k
< j
; ++k
)
2900 part
= isl_union_map_add_map(part
,
2901 isl_map_copy(v
[k
].map
));
2903 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2912 isl_space_free(dim
);
2913 free_isl_fixed_map_array(v
, n
);
2916 isl_space_free(dim
);
2917 free_isl_fixed_map_array(v
, n
);
2921 /* Check whether the maps in umap have obviously distinct ranges.
2922 * In particular, check for an output dimension in the range
2923 * [first,n_range) for which all maps have a fixed value
2924 * and then check if these values, possibly along with fixed values
2925 * at later dimensions, entail distinct ranges.
2927 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2928 int first
, int n_range
)
2932 struct isl_fixed_dim_data data
= { NULL
};
2934 ctx
= isl_union_map_get_ctx(umap
);
2936 n
= isl_union_map_n_map(umap
);
2941 isl_union_map_free(umap
);
2942 return isl_bool_true
;
2945 if (first
>= n_range
) {
2946 isl_union_map_free(umap
);
2947 return isl_bool_false
;
2950 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2954 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2960 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2965 dim
= isl_union_map_get_space(umap
);
2966 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2967 isl_union_map_free(umap
);
2971 free_isl_fixed_map_array(data
.v
, n
);
2972 isl_union_map_free(umap
);
2974 return isl_bool_false
;
2976 free_isl_fixed_map_array(data
.v
, n
);
2977 isl_union_map_free(umap
);
2978 return isl_bool_error
;
2981 /* Check whether the maps in umap that map to subsets of "ran"
2982 * have obviously distinct ranges.
2984 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
2987 isl_union_map
*umap
= user
;
2989 umap
= isl_union_map_copy(umap
);
2990 umap
= isl_union_map_intersect_range(umap
,
2991 isl_union_set_from_set(isl_set_copy(ran
)));
2992 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2995 /* Check if the given union_map is obviously injective.
2997 * In particular, we first check if all individual maps are obviously
2998 * injective and then check if all the ranges of these maps are
2999 * obviously disjoint.
3001 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
3004 isl_union_map
*univ
;
3007 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
3009 return isl_bool_error
;
3011 return isl_bool_false
;
3013 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
3014 ran
= isl_union_map_range(univ
);
3016 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
3018 isl_union_set_free(ran
);
3023 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
3027 sv
= isl_union_map_is_single_valued(umap
);
3031 return isl_union_map_is_injective(umap
);
3034 static isl_stat
zip_entry(void **entry
, void *user
)
3036 isl_map
*map
= *entry
;
3037 isl_union_map
**res
= user
;
3039 if (!isl_map_can_zip(map
))
3042 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
3047 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
3049 return cond_un_op(umap
, &zip_entry
);
3052 static isl_stat
uncurry_entry(void **entry
, void *user
)
3054 isl_map
*map
= *entry
;
3055 isl_union_map
**res
= user
;
3057 if (!isl_map_can_uncurry(map
))
3060 *res
= isl_union_map_add_map(*res
, isl_map_uncurry(isl_map_copy(map
)));
3065 /* Given a union map, take the maps of the form A -> (B -> C) and
3066 * return the union of the corresponding maps (A -> B) -> C.
3068 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
3070 return cond_un_op(umap
, &uncurry_entry
);
3073 static isl_stat
curry_entry(void **entry
, void *user
)
3075 isl_map
*map
= *entry
;
3076 isl_union_map
**res
= user
;
3078 if (!isl_map_can_curry(map
))
3081 *res
= isl_union_map_add_map(*res
, isl_map_curry(isl_map_copy(map
)));
3086 /* Given a union map, take the maps of the form (A -> B) -> C and
3087 * return the union of the corresponding maps A -> (B -> C).
3089 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
3091 return cond_un_op(umap
, &curry_entry
);
3094 /* If *entry is of the form A -> ((B -> C) -> D), then apply
3095 * isl_map_range_curry to it and add the result to *res.
3097 static isl_stat
range_curry_entry(void **entry
, void *user
)
3099 isl_map
*map
= *entry
;
3100 isl_union_map
**res
= user
;
3102 if (!isl_map_can_range_curry(map
))
3105 map
= isl_map_range_curry(isl_map_copy(map
));
3106 *res
= isl_union_map_add_map(*res
, map
);
3111 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3112 * return the union of the corresponding maps A -> (B -> (C -> D)).
3114 __isl_give isl_union_map
*isl_union_map_range_curry(
3115 __isl_take isl_union_map
*umap
)
3117 return cond_un_op(umap
, &range_curry_entry
);
3120 static isl_stat
lift_entry(void **entry
, void *user
)
3122 isl_set
*set
= *entry
;
3123 isl_union_set
**res
= user
;
3125 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
3130 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
3132 return cond_un_op(uset
, &lift_entry
);
3135 static isl_stat
coefficients_entry(void **entry
, void *user
)
3137 isl_set
*set
= *entry
;
3138 isl_union_set
**res
= user
;
3140 set
= isl_set_copy(set
);
3141 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
3142 *res
= isl_union_set_add_set(*res
, set
);
3147 __isl_give isl_union_set
*isl_union_set_coefficients(
3148 __isl_take isl_union_set
*uset
)
3157 ctx
= isl_union_set_get_ctx(uset
);
3158 dim
= isl_space_set_alloc(ctx
, 0, 0);
3159 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
3160 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3161 &coefficients_entry
, &res
) < 0)
3164 isl_union_set_free(uset
);
3167 isl_union_set_free(uset
);
3168 isl_union_set_free(res
);
3172 static isl_stat
solutions_entry(void **entry
, void *user
)
3174 isl_set
*set
= *entry
;
3175 isl_union_set
**res
= user
;
3177 set
= isl_set_copy(set
);
3178 set
= isl_set_from_basic_set(isl_set_solutions(set
));
3180 *res
= isl_union_set_from_set(set
);
3182 *res
= isl_union_set_add_set(*res
, set
);
3185 return isl_stat_error
;
3190 __isl_give isl_union_set
*isl_union_set_solutions(
3191 __isl_take isl_union_set
*uset
)
3193 isl_union_set
*res
= NULL
;
3198 if (uset
->table
.n
== 0) {
3199 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
3200 isl_union_set_free(uset
);
3204 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3205 &solutions_entry
, &res
) < 0)
3208 isl_union_set_free(uset
);
3211 isl_union_set_free(uset
);
3212 isl_union_set_free(res
);
3216 /* Is the domain space of "map" equal to "space"?
3218 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3220 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
3221 space
, isl_dim_out
);
3224 /* Is the range space of "map" equal to "space"?
3226 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3228 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3229 space
, isl_dim_out
);
3232 /* Is the set space of "map" equal to "space"?
3234 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3236 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3237 space
, isl_dim_out
);
3240 /* Internal data structure for preimage_pw_multi_aff.
3242 * "pma" is the function under which the preimage should be taken.
3243 * "space" is the space of "pma".
3244 * "res" collects the results.
3245 * "fn" computes the preimage for a given map.
3246 * "match" returns true if "fn" can be called.
3248 struct isl_union_map_preimage_data
{
3250 isl_pw_multi_aff
*pma
;
3252 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3253 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3254 __isl_take isl_pw_multi_aff
*pma
);
3257 /* Call data->fn to compute the preimage of the domain or range of *entry
3258 * under the function represented by data->pma, provided the domain/range
3259 * space of *entry matches the target space of data->pma
3260 * (as given by data->match), and add the result to data->res.
3262 static isl_stat
preimage_entry(void **entry
, void *user
)
3265 isl_map
*map
= *entry
;
3266 struct isl_union_map_preimage_data
*data
= user
;
3269 m
= data
->match(map
, data
->space
);
3271 return isl_stat_error
;
3275 map
= isl_map_copy(map
);
3276 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3278 empty
= isl_map_is_empty(map
);
3279 if (empty
< 0 || empty
) {
3281 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3284 data
->res
= isl_union_map_add_map(data
->res
, map
);
3289 /* Compute the preimage of the domain or range of "umap" under the function
3290 * represented by "pma".
3291 * In other words, plug in "pma" in the domain or range of "umap".
3292 * The function "fn" performs the actual preimage computation on a map,
3293 * while "match" determines to which maps the function should be applied.
3295 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3296 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3297 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3298 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3299 __isl_take isl_pw_multi_aff
*pma
))
3303 struct isl_union_map_preimage_data data
;
3305 umap
= isl_union_map_align_params(umap
,
3306 isl_pw_multi_aff_get_space(pma
));
3307 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3312 ctx
= isl_union_map_get_ctx(umap
);
3313 space
= isl_union_map_get_space(umap
);
3314 data
.space
= isl_pw_multi_aff_get_space(pma
);
3316 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3319 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3321 data
.res
= isl_union_map_free(data
.res
);
3323 isl_space_free(data
.space
);
3324 isl_union_map_free(umap
);
3325 isl_pw_multi_aff_free(pma
);
3328 isl_union_map_free(umap
);
3329 isl_pw_multi_aff_free(pma
);
3333 /* Compute the preimage of the domain of "umap" under the function
3334 * represented by "pma".
3335 * In other words, plug in "pma" in the domain of "umap".
3336 * The result contains maps that live in the same spaces as the maps of "umap"
3337 * with domain space equal to the target space of "pma",
3338 * except that the domain has been replaced by the domain space of "pma".
3340 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3341 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3343 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3344 &isl_map_preimage_domain_pw_multi_aff
);
3347 /* Compute the preimage of the range of "umap" under the function
3348 * represented by "pma".
3349 * In other words, plug in "pma" in the range of "umap".
3350 * The result contains maps that live in the same spaces as the maps of "umap"
3351 * with range space equal to the target space of "pma",
3352 * except that the range has been replaced by the domain space of "pma".
3354 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3355 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3357 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3358 &isl_map_preimage_range_pw_multi_aff
);
3361 /* Compute the preimage of "uset" under the function represented by "pma".
3362 * In other words, plug in "pma" in "uset".
3363 * The result contains sets that live in the same spaces as the sets of "uset"
3364 * with space equal to the target space of "pma",
3365 * except that the space has been replaced by the domain space of "pma".
3367 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3368 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3370 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3371 &isl_set_preimage_pw_multi_aff
);
3374 /* Compute the preimage of the domain of "umap" under the function
3375 * represented by "ma".
3376 * In other words, plug in "ma" in the domain of "umap".
3377 * The result contains maps that live in the same spaces as the maps of "umap"
3378 * with domain space equal to the target space of "ma",
3379 * except that the domain has been replaced by the domain space of "ma".
3381 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3382 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3384 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3385 isl_pw_multi_aff_from_multi_aff(ma
));
3388 /* Compute the preimage of the range of "umap" under the function
3389 * represented by "ma".
3390 * In other words, plug in "ma" in the range of "umap".
3391 * The result contains maps that live in the same spaces as the maps of "umap"
3392 * with range space equal to the target space of "ma",
3393 * except that the range has been replaced by the domain space of "ma".
3395 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3396 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3398 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3399 isl_pw_multi_aff_from_multi_aff(ma
));
3402 /* Compute the preimage of "uset" under the function represented by "ma".
3403 * In other words, plug in "ma" in "uset".
3404 * The result contains sets that live in the same spaces as the sets of "uset"
3405 * with space equal to the target space of "ma",
3406 * except that the space has been replaced by the domain space of "ma".
3408 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3409 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3411 return isl_union_set_preimage_pw_multi_aff(uset
,
3412 isl_pw_multi_aff_from_multi_aff(ma
));
3415 /* Internal data structure for preimage_multi_pw_aff.
3417 * "mpa" is the function under which the preimage should be taken.
3418 * "space" is the space of "mpa".
3419 * "res" collects the results.
3420 * "fn" computes the preimage for a given map.
3421 * "match" returns true if "fn" can be called.
3423 struct isl_union_map_preimage_mpa_data
{
3425 isl_multi_pw_aff
*mpa
;
3427 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3428 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3429 __isl_take isl_multi_pw_aff
*mpa
);
3432 /* Call data->fn to compute the preimage of the domain or range of *entry
3433 * under the function represented by data->mpa, provided the domain/range
3434 * space of *entry matches the target space of data->mpa
3435 * (as given by data->match), and add the result to data->res.
3437 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3440 isl_map
*map
= *entry
;
3441 struct isl_union_map_preimage_mpa_data
*data
= user
;
3444 m
= data
->match(map
, data
->space
);
3446 return isl_stat_error
;
3450 map
= isl_map_copy(map
);
3451 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3453 empty
= isl_map_is_empty(map
);
3454 if (empty
< 0 || empty
) {
3456 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3459 data
->res
= isl_union_map_add_map(data
->res
, map
);
3464 /* Compute the preimage of the domain or range of "umap" under the function
3465 * represented by "mpa".
3466 * In other words, plug in "mpa" in the domain or range of "umap".
3467 * The function "fn" performs the actual preimage computation on a map,
3468 * while "match" determines to which maps the function should be applied.
3470 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3471 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3472 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3473 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3474 __isl_take isl_multi_pw_aff
*mpa
))
3478 struct isl_union_map_preimage_mpa_data data
;
3480 umap
= isl_union_map_align_params(umap
,
3481 isl_multi_pw_aff_get_space(mpa
));
3482 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3487 ctx
= isl_union_map_get_ctx(umap
);
3488 space
= isl_union_map_get_space(umap
);
3489 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3491 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3494 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3496 data
.res
= isl_union_map_free(data
.res
);
3498 isl_space_free(data
.space
);
3499 isl_union_map_free(umap
);
3500 isl_multi_pw_aff_free(mpa
);
3503 isl_union_map_free(umap
);
3504 isl_multi_pw_aff_free(mpa
);
3508 /* Compute the preimage of the domain of "umap" under the function
3509 * represented by "mpa".
3510 * In other words, plug in "mpa" in the domain of "umap".
3511 * The result contains maps that live in the same spaces as the maps of "umap"
3512 * with domain space equal to the target space of "mpa",
3513 * except that the domain has been replaced by the domain space of "mpa".
3515 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3516 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3518 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3519 &isl_map_preimage_domain_multi_pw_aff
);
3522 /* Internal data structure for preimage_upma.
3524 * "umap" is the map of which the preimage should be computed.
3525 * "res" collects the results.
3526 * "fn" computes the preimage for a given piecewise multi-affine function.
3528 struct isl_union_map_preimage_upma_data
{
3529 isl_union_map
*umap
;
3531 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3532 __isl_take isl_pw_multi_aff
*pma
);
3535 /* Call data->fn to compute the preimage of the domain or range of data->umap
3536 * under the function represented by pma and add the result to data->res.
3538 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3540 struct isl_union_map_preimage_upma_data
*data
= user
;
3541 isl_union_map
*umap
;
3543 umap
= isl_union_map_copy(data
->umap
);
3544 umap
= data
->fn(umap
, pma
);
3545 data
->res
= isl_union_map_union(data
->res
, umap
);
3547 return data
->res
? isl_stat_ok
: isl_stat_error
;
3550 /* Compute the preimage of the domain or range of "umap" under the function
3551 * represented by "upma".
3552 * In other words, plug in "upma" in the domain or range of "umap".
3553 * The function "fn" performs the actual preimage computation
3554 * on a piecewise multi-affine function.
3556 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3557 __isl_take isl_union_map
*umap
,
3558 __isl_take isl_union_pw_multi_aff
*upma
,
3559 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3560 __isl_take isl_pw_multi_aff
*pma
))
3562 struct isl_union_map_preimage_upma_data data
;
3565 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3567 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3568 &preimage_upma
, &data
) < 0)
3569 data
.res
= isl_union_map_free(data
.res
);
3571 isl_union_map_free(umap
);
3572 isl_union_pw_multi_aff_free(upma
);
3577 /* Compute the preimage of the domain of "umap" under the function
3578 * represented by "upma".
3579 * In other words, plug in "upma" in the domain of "umap".
3580 * The result contains maps that live in the same spaces as the maps of "umap"
3581 * with domain space equal to one of the target spaces of "upma",
3582 * except that the domain has been replaced by one of the the domain spaces that
3583 * corresponds to that target space of "upma".
3585 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3586 __isl_take isl_union_map
*umap
,
3587 __isl_take isl_union_pw_multi_aff
*upma
)
3589 return preimage_union_pw_multi_aff(umap
, upma
,
3590 &isl_union_map_preimage_domain_pw_multi_aff
);
3593 /* Compute the preimage of the range of "umap" under the function
3594 * represented by "upma".
3595 * In other words, plug in "upma" in the range of "umap".
3596 * The result contains maps that live in the same spaces as the maps of "umap"
3597 * with range space equal to one of the target spaces of "upma",
3598 * except that the range has been replaced by one of the the domain spaces that
3599 * corresponds to that target space of "upma".
3601 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3602 __isl_take isl_union_map
*umap
,
3603 __isl_take isl_union_pw_multi_aff
*upma
)
3605 return preimage_union_pw_multi_aff(umap
, upma
,
3606 &isl_union_map_preimage_range_pw_multi_aff
);
3609 /* Compute the preimage of "uset" under the function represented by "upma".
3610 * In other words, plug in "upma" in the range of "uset".
3611 * The result contains sets that live in the same spaces as the sets of "uset"
3612 * with space equal to one of the target spaces of "upma",
3613 * except that the space has been replaced by one of the the domain spaces that
3614 * corresponds to that target space of "upma".
3616 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3617 __isl_take isl_union_set
*uset
,
3618 __isl_take isl_union_pw_multi_aff
*upma
)
3620 return preimage_union_pw_multi_aff(uset
, upma
,
3621 &isl_union_set_preimage_pw_multi_aff
);
3624 /* Reset the user pointer on all identifiers of parameters and tuples
3625 * of the space of *entry.
3627 static isl_stat
reset_user(void **entry
, void *user
)
3629 isl_map
**map
= (isl_map
**)entry
;
3631 *map
= isl_map_reset_user(*map
);
3633 return *map
? isl_stat_ok
: isl_stat_error
;
3636 /* Reset the user pointer on all identifiers of parameters and tuples
3637 * of the spaces of "umap".
3639 __isl_give isl_union_map
*isl_union_map_reset_user(
3640 __isl_take isl_union_map
*umap
)
3642 umap
= isl_union_map_cow(umap
);
3645 umap
->dim
= isl_space_reset_user(umap
->dim
);
3647 return isl_union_map_free(umap
);
3648 umap
= un_op(umap
, &reset_user
);
3653 /* Reset the user pointer on all identifiers of parameters and tuples
3654 * of the spaces of "uset".
3656 __isl_give isl_union_set
*isl_union_set_reset_user(
3657 __isl_take isl_union_set
*uset
)
3659 return isl_union_map_reset_user(uset
);
3662 /* Internal data structure for isl_union_map_project_out.
3663 * "type", "first" and "n" are the arguments for the isl_map_project_out
3665 * "res" collects the results.
3667 struct isl_union_map_project_out_data
{
3668 enum isl_dim_type type
;
3675 /* Turn the data->n dimensions of type data->type, starting at data->first
3676 * into existentially quantified variables and add the result to data->res.
3678 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3680 struct isl_union_map_project_out_data
*data
= user
;
3682 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3683 data
->res
= isl_union_map_add_map(data
->res
, map
);
3688 /* Turn the "n" dimensions of type "type", starting at "first"
3689 * into existentially quantified variables.
3690 * Since the space of an isl_union_map only contains parameters,
3691 * type is required to be equal to isl_dim_param.
3693 __isl_give isl_union_map
*isl_union_map_project_out(
3694 __isl_take isl_union_map
*umap
,
3695 enum isl_dim_type type
, unsigned first
, unsigned n
)
3698 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3703 if (type
!= isl_dim_param
)
3704 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3705 "can only project out parameters",
3706 return isl_union_map_free(umap
));
3708 space
= isl_union_map_get_space(umap
);
3709 space
= isl_space_drop_dims(space
, type
, first
, n
);
3710 data
.res
= isl_union_map_empty(space
);
3711 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3712 data
.res
= isl_union_map_free(data
.res
);
3714 isl_union_map_free(umap
);
3719 /* Turn the "n" dimensions of type "type", starting at "first"
3720 * into existentially quantified variables.
3721 * Since the space of an isl_union_set only contains parameters,
3722 * "type" is required to be equal to isl_dim_param.
3724 __isl_give isl_union_set
*isl_union_set_project_out(
3725 __isl_take isl_union_set
*uset
,
3726 enum isl_dim_type type
, unsigned first
, unsigned n
)
3728 return isl_union_map_project_out(uset
, type
, first
, n
);
3731 /* Internal data structure for isl_union_map_involves_dims.
3732 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3734 struct isl_union_map_involves_dims_data
{
3739 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3741 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3743 struct isl_union_map_involves_dims_data
*data
= user
;
3746 involves
= isl_map_involves_dims(map
,
3747 isl_dim_param
, data
->first
, data
->n
);
3749 return isl_bool_error
;
3753 /* Does "umap" involve any of the n parameters starting at first?
3754 * "type" is required to be set to isl_dim_param.
3756 * "umap" involves any of those parameters if any of its maps
3757 * involve the parameters. In other words, "umap" does not
3758 * involve any of the parameters if all its maps to not
3759 * involve the parameters.
3761 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3762 enum isl_dim_type type
, unsigned first
, unsigned n
)
3764 struct isl_union_map_involves_dims_data data
= { first
, n
};
3767 if (type
!= isl_dim_param
)
3768 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3769 "can only reference parameters", return isl_bool_error
);
3771 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3774 return isl_bool_error
;
3779 /* Internal data structure for isl_union_map_reset_range_space.
3780 * "range" is the space from which to set the range space.
3781 * "res" collects the results.
3783 struct isl_union_map_reset_range_space_data
{
3788 /* Replace the range space of "map" by the range space of data->range and
3789 * add the result to data->res.
3791 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3793 struct isl_union_map_reset_range_space_data
*data
= user
;
3796 space
= isl_map_get_space(map
);
3797 space
= isl_space_domain(space
);
3798 space
= isl_space_extend_domain_with_range(space
,
3799 isl_space_copy(data
->range
));
3800 map
= isl_map_reset_space(map
, space
);
3801 data
->res
= isl_union_map_add_map(data
->res
, map
);
3803 return data
->res
? isl_stat_ok
: isl_stat_error
;
3806 /* Replace the range space of all the maps in "umap" by
3807 * the range space of "space".
3809 * This assumes that all maps have the same output dimension.
3810 * This function should therefore not be made publicly available.
3812 * Since the spaces of the maps change, so do their hash value.
3813 * We therefore need to create a new isl_union_map.
3815 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3816 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3818 struct isl_union_map_reset_range_space_data data
= { space
};
3820 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3821 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
3822 data
.res
= isl_union_map_free(data
.res
);
3824 isl_space_free(space
);
3825 isl_union_map_free(umap
);
3829 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3830 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3831 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3832 * to the domain and the range of each map.
3833 * "res" collects the results.
3835 struct isl_union_order_at_data
{
3836 isl_multi_union_pw_aff
*mupa
;
3837 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3838 __isl_take isl_multi_pw_aff
*mpa2
);
3842 /* Intersect "map" with the result of applying data->order to
3843 * the functions in data->mupa that apply to the domain and the range
3844 * of "map" and add the result to data->res.
3846 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
3848 struct isl_union_order_at_data
*data
= user
;
3850 isl_multi_pw_aff
*mpa1
, *mpa2
;
3853 space
= isl_space_domain(isl_map_get_space(map
));
3854 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3855 space
= isl_space_range(isl_map_get_space(map
));
3856 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3857 order
= data
->order(mpa1
, mpa2
);
3858 map
= isl_map_intersect(map
, order
);
3859 data
->res
= isl_union_map_add_map(data
->res
, map
);
3861 return data
->res
? isl_stat_ok
: isl_stat_error
;
3864 /* Intersect each map in "umap" with the result of calling "order"
3865 * on the functions is "mupa" that apply to the domain and the range
3868 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
3869 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
3870 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3871 __isl_take isl_multi_pw_aff
*mpa2
))
3873 struct isl_union_order_at_data data
;
3875 umap
= isl_union_map_align_params(umap
,
3876 isl_multi_union_pw_aff_get_space(mupa
));
3877 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
3878 isl_union_map_get_space(umap
));
3881 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3882 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
3883 data
.res
= isl_union_map_free(data
.res
);
3885 isl_multi_union_pw_aff_free(mupa
);
3886 isl_union_map_free(umap
);
3890 /* Return the subset of "umap" where the domain and the range
3891 * have equal "mupa" values.
3893 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
3894 __isl_take isl_union_map
*umap
,
3895 __isl_take isl_multi_union_pw_aff
*mupa
)
3897 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3898 &isl_multi_pw_aff_eq_map
);
3901 /* Return the subset of "umap" where the domain has a lexicographically
3902 * smaller "mupa" value than the range.
3904 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
3905 __isl_take isl_union_map
*umap
,
3906 __isl_take isl_multi_union_pw_aff
*mupa
)
3908 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3909 &isl_multi_pw_aff_lex_lt_map
);
3912 /* Return the subset of "umap" where the domain has a lexicographically
3913 * greater "mupa" value than the range.
3915 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
3916 __isl_take isl_union_map
*umap
,
3917 __isl_take isl_multi_union_pw_aff
*mupa
)
3919 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3920 &isl_multi_pw_aff_lex_gt_map
);
3923 /* Return the union of the elements in the list "list".
3925 __isl_give isl_union_set
*isl_union_set_list_union(
3926 __isl_take isl_union_set_list
*list
)
3936 ctx
= isl_union_set_list_get_ctx(list
);
3937 space
= isl_space_params_alloc(ctx
, 0);
3938 res
= isl_union_set_empty(space
);
3940 n
= isl_union_set_list_n_union_set(list
);
3941 for (i
= 0; i
< n
; ++i
) {
3942 isl_union_set
*uset_i
;
3944 uset_i
= isl_union_set_list_get_union_set(list
, i
);
3945 res
= isl_union_set_union(res
, uset_i
);
3948 isl_union_set_list_free(list
);
3952 /* Update *hash with the hash value of "map".
3954 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
3956 uint32_t *hash
= user
;
3959 map_hash
= isl_map_get_hash(map
);
3960 isl_hash_hash(*hash
, map_hash
);
3966 /* Return a hash value that digests "umap".
3968 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
3975 hash
= isl_hash_init();
3976 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
3982 /* Return a hash value that digests "uset".
3984 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
3986 return isl_union_map_get_hash(uset
);