2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl/deprecated/union_map_int.h>
27 /* Return the number of parameters of "umap", where "type"
28 * is required to be set to isl_dim_param.
30 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
31 enum isl_dim_type type
)
36 if (type
!= isl_dim_param
)
37 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
38 "can only reference parameters", return 0);
40 return isl_space_dim(umap
->dim
, type
);
43 /* Return the number of parameters of "uset", where "type"
44 * is required to be set to isl_dim_param.
46 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
47 enum isl_dim_type type
)
49 return isl_union_map_dim(uset
, type
);
52 /* Return the id of the specified dimension.
54 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
55 enum isl_dim_type type
, unsigned pos
)
60 if (type
!= isl_dim_param
)
61 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
62 "can only reference parameters", return NULL
);
64 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
67 /* Is this union set a parameter domain?
69 int isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
76 if (uset
->table
.n
!= 1)
79 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
80 params
= isl_set_is_params(set
);
85 static __isl_give isl_union_map
*isl_union_map_alloc(
86 __isl_take isl_space
*space
, int size
)
90 space
= isl_space_params(space
);
94 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
96 isl_space_free(space
);
102 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
103 return isl_union_map_free(umap
);
108 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
110 return isl_union_map_alloc(dim
, 16);
113 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
115 return isl_union_map_empty(dim
);
118 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
120 return umap
? umap
->dim
->ctx
: NULL
;
123 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
125 return uset
? uset
->dim
->ctx
: NULL
;
128 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
132 return isl_space_copy(umap
->dim
);
135 /* Return the position of the parameter with the given name
137 * Return -1 if no such dimension can be found.
139 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
140 enum isl_dim_type type
, const char *name
)
144 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
147 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
149 return isl_union_map_get_space(uset
);
152 static int free_umap_entry(void **entry
, void *user
)
154 isl_map
*map
= *entry
;
159 static int add_map(__isl_take isl_map
*map
, void *user
)
161 isl_union_map
**umap
= (isl_union_map
**)user
;
163 *umap
= isl_union_map_add_map(*umap
, map
);
168 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
175 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
176 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
180 isl_union_map_free(dup
);
184 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
192 return isl_union_map_dup(umap
);
195 struct isl_union_align
{
200 static int align_entry(void **entry
, void *user
)
202 isl_map
*map
= *entry
;
204 struct isl_union_align
*data
= user
;
206 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
207 isl_map_get_space(map
));
209 data
->res
= isl_union_map_add_map(data
->res
,
210 isl_map_realign(isl_map_copy(map
), exp
));
215 /* Align the parameters of umap along those of model.
216 * The result has the parameters of model first, in the same order
217 * as they appear in model, followed by any remaining parameters of
218 * umap that do not appear in model.
220 __isl_give isl_union_map
*isl_union_map_align_params(
221 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
223 struct isl_union_align data
= { NULL
, NULL
};
228 if (isl_space_match(umap
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
229 isl_space_free(model
);
233 model
= isl_space_params(model
);
234 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
238 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
240 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
241 &align_entry
, &data
) < 0)
244 isl_reordering_free(data
.exp
);
245 isl_union_map_free(umap
);
246 isl_space_free(model
);
249 isl_reordering_free(data
.exp
);
250 isl_union_map_free(umap
);
251 isl_union_map_free(data
.res
);
252 isl_space_free(model
);
256 __isl_give isl_union_set
*isl_union_set_align_params(
257 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
259 return isl_union_map_align_params(uset
, model
);
262 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
263 __isl_take isl_union_map
*umap2
)
265 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
266 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
268 umap1
= isl_union_map_cow(umap1
);
270 if (!umap1
|| !umap2
)
273 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
276 isl_union_map_free(umap2
);
280 isl_union_map_free(umap1
);
281 isl_union_map_free(umap2
);
285 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
286 __isl_take isl_union_set
*uset2
)
288 return isl_union_map_union(uset1
, uset2
);
291 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
300 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
302 return isl_union_map_copy(uset
);
305 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
313 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
314 &free_umap_entry
, NULL
);
315 isl_hash_table_clear(&umap
->table
);
316 isl_space_free(umap
->dim
);
321 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
323 return isl_union_map_free(uset
);
326 static int has_dim(const void *entry
, const void *val
)
328 isl_map
*map
= (isl_map
*)entry
;
329 isl_space
*dim
= (isl_space
*)val
;
331 return isl_space_is_equal(map
->dim
, dim
);
334 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
335 __isl_take isl_map
*map
)
338 struct isl_hash_table_entry
*entry
;
343 if (isl_map_plain_is_empty(map
)) {
348 if (!isl_space_match(map
->dim
, isl_dim_param
, umap
->dim
, isl_dim_param
)) {
349 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
350 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
353 umap
= isl_union_map_cow(umap
);
358 hash
= isl_space_get_hash(map
->dim
);
359 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
360 &has_dim
, map
->dim
, 1);
367 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
376 isl_union_map_free(umap
);
380 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
381 __isl_take isl_set
*set
)
383 return isl_union_map_add_map(uset
, (isl_map
*)set
);
386 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
394 dim
= isl_map_get_space(map
);
395 dim
= isl_space_params(dim
);
396 umap
= isl_union_map_empty(dim
);
397 umap
= isl_union_map_add_map(umap
, map
);
402 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
404 return isl_union_map_from_map((isl_map
*)set
);
407 __isl_give isl_union_map
*isl_union_map_from_basic_map(
408 __isl_take isl_basic_map
*bmap
)
410 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
413 __isl_give isl_union_set
*isl_union_set_from_basic_set(
414 __isl_take isl_basic_set
*bset
)
416 return isl_union_map_from_basic_map(bset
);
419 struct isl_union_map_foreach_data
421 int (*fn
)(__isl_take isl_map
*map
, void *user
);
425 static int call_on_copy(void **entry
, void *user
)
427 isl_map
*map
= *entry
;
428 struct isl_union_map_foreach_data
*data
;
429 data
= (struct isl_union_map_foreach_data
*)user
;
431 return data
->fn(isl_map_copy(map
), data
->user
);
434 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
436 return umap
? umap
->table
.n
: 0;
439 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
441 return uset
? uset
->table
.n
: 0;
444 int isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
445 int (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
447 struct isl_union_map_foreach_data data
= { fn
, user
};
452 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
453 &call_on_copy
, &data
);
456 static int copy_map(void **entry
, void *user
)
458 isl_map
*map
= *entry
;
459 isl_map
**map_p
= user
;
461 *map_p
= isl_map_copy(map
);
466 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
473 ctx
= isl_union_map_get_ctx(umap
);
474 if (umap
->table
.n
!= 1)
475 isl_die(ctx
, isl_error_invalid
,
476 "union map needs to contain elements in exactly "
477 "one space", goto error
);
479 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
481 isl_union_map_free(umap
);
485 isl_union_map_free(umap
);
489 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
491 return isl_map_from_union_map(uset
);
494 /* Extract the map in "umap" that lives in the given space (ignoring
497 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
498 __isl_take isl_space
*space
)
501 struct isl_hash_table_entry
*entry
;
503 space
= isl_space_drop_dims(space
, isl_dim_param
,
504 0, isl_space_dim(space
, isl_dim_param
));
505 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
509 hash
= isl_space_get_hash(space
);
510 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
513 return isl_map_empty(space
);
514 isl_space_free(space
);
515 return isl_map_copy(entry
->data
);
517 isl_space_free(space
);
521 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
522 __isl_take isl_space
*dim
)
524 return (isl_set
*)isl_union_map_extract_map(uset
, dim
);
527 /* Check if umap contains a map in the given space.
529 __isl_give
int isl_union_map_contains(__isl_keep isl_union_map
*umap
,
530 __isl_keep isl_space
*dim
)
533 struct isl_hash_table_entry
*entry
;
538 hash
= isl_space_get_hash(dim
);
539 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
544 __isl_give
int isl_union_set_contains(__isl_keep isl_union_set
*uset
,
545 __isl_keep isl_space
*dim
)
547 return isl_union_map_contains(uset
, dim
);
550 int isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
551 int (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
553 return isl_union_map_foreach_map(uset
,
554 (int(*)(__isl_take isl_map
*, void*))fn
, user
);
557 struct isl_union_set_foreach_point_data
{
558 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
562 static int foreach_point(__isl_take isl_set
*set
, void *user
)
564 struct isl_union_set_foreach_point_data
*data
= user
;
567 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
573 int isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
574 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
576 struct isl_union_set_foreach_point_data data
= { fn
, user
};
577 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
580 struct isl_union_map_gen_bin_data
{
581 isl_union_map
*umap2
;
585 static int subtract_entry(void **entry
, void *user
)
587 struct isl_union_map_gen_bin_data
*data
= user
;
589 struct isl_hash_table_entry
*entry2
;
590 isl_map
*map
= *entry
;
592 hash
= isl_space_get_hash(map
->dim
);
593 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
594 hash
, &has_dim
, map
->dim
, 0);
595 map
= isl_map_copy(map
);
598 map
= isl_map_subtract(map
, isl_map_copy(entry2
->data
));
600 empty
= isl_map_is_empty(map
);
610 data
->res
= isl_union_map_add_map(data
->res
, map
);
615 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
616 __isl_take isl_union_map
*umap2
, int (*fn
)(void **, void *))
618 struct isl_union_map_gen_bin_data data
= { NULL
, NULL
};
620 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
621 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
623 if (!umap1
|| !umap2
)
627 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
629 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
633 isl_union_map_free(umap1
);
634 isl_union_map_free(umap2
);
637 isl_union_map_free(umap1
);
638 isl_union_map_free(umap2
);
639 isl_union_map_free(data
.res
);
643 __isl_give isl_union_map
*isl_union_map_subtract(
644 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
646 return gen_bin_op(umap1
, umap2
, &subtract_entry
);
649 __isl_give isl_union_set
*isl_union_set_subtract(
650 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
652 return isl_union_map_subtract(uset1
, uset2
);
655 struct isl_union_map_gen_bin_set_data
{
660 static int intersect_params_entry(void **entry
, void *user
)
662 struct isl_union_map_gen_bin_set_data
*data
= user
;
663 isl_map
*map
= *entry
;
666 map
= isl_map_copy(map
);
667 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
669 empty
= isl_map_is_empty(map
);
675 data
->res
= isl_union_map_add_map(data
->res
, map
);
680 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
681 __isl_take isl_set
*set
, int (*fn
)(void **, void *))
683 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
685 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
686 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
692 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
694 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
698 isl_union_map_free(umap
);
702 isl_union_map_free(umap
);
704 isl_union_map_free(data
.res
);
708 __isl_give isl_union_map
*isl_union_map_intersect_params(
709 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
711 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
714 __isl_give isl_union_set
*isl_union_set_intersect_params(
715 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
717 return isl_union_map_intersect_params(uset
, set
);
720 static __isl_give isl_union_map
*union_map_intersect_params(
721 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
723 return isl_union_map_intersect_params(umap
,
724 isl_set_from_union_set(uset
));
727 static __isl_give isl_union_map
*union_map_gist_params(
728 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
730 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
733 struct isl_union_map_match_bin_data
{
734 isl_union_map
*umap2
;
736 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
739 static int match_bin_entry(void **entry
, void *user
)
741 struct isl_union_map_match_bin_data
*data
= user
;
743 struct isl_hash_table_entry
*entry2
;
744 isl_map
*map
= *entry
;
747 hash
= isl_space_get_hash(map
->dim
);
748 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
749 hash
, &has_dim
, map
->dim
, 0);
753 map
= isl_map_copy(map
);
754 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
756 empty
= isl_map_is_empty(map
);
766 data
->res
= isl_union_map_add_map(data
->res
, map
);
771 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
772 __isl_take isl_union_map
*umap2
,
773 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
775 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
777 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
778 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
780 if (!umap1
|| !umap2
)
784 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
786 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
787 &match_bin_entry
, &data
) < 0)
790 isl_union_map_free(umap1
);
791 isl_union_map_free(umap2
);
794 isl_union_map_free(umap1
);
795 isl_union_map_free(umap2
);
796 isl_union_map_free(data
.res
);
800 __isl_give isl_union_map
*isl_union_map_intersect(
801 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
803 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
806 /* Compute the intersection of the two union_sets.
807 * As a special case, if exactly one of the two union_sets
808 * is a parameter domain, then intersect the parameter domain
809 * of the other one with this set.
811 __isl_give isl_union_set
*isl_union_set_intersect(
812 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
816 p1
= isl_union_set_is_params(uset1
);
817 p2
= isl_union_set_is_params(uset2
);
818 if (p1
< 0 || p2
< 0)
821 return union_map_intersect_params(uset1
, uset2
);
823 return union_map_intersect_params(uset2
, uset1
);
824 return isl_union_map_intersect(uset1
, uset2
);
826 isl_union_set_free(uset1
);
827 isl_union_set_free(uset2
);
831 static int gist_params_entry(void **entry
, void *user
)
833 struct isl_union_map_gen_bin_set_data
*data
= user
;
834 isl_map
*map
= *entry
;
837 map
= isl_map_copy(map
);
838 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
840 empty
= isl_map_is_empty(map
);
846 data
->res
= isl_union_map_add_map(data
->res
, map
);
851 __isl_give isl_union_map
*isl_union_map_gist_params(
852 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
854 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
857 __isl_give isl_union_set
*isl_union_set_gist_params(
858 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
860 return isl_union_map_gist_params(uset
, set
);
863 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
864 __isl_take isl_union_map
*context
)
866 return match_bin_op(umap
, context
, &isl_map_gist
);
869 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
870 __isl_take isl_union_set
*context
)
872 if (isl_union_set_is_params(context
))
873 return union_map_gist_params(uset
, context
);
874 return isl_union_map_gist(uset
, context
);
877 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
878 __isl_take isl_map
*set2
)
880 return isl_set_lex_le_set((isl_set
*)set1
, (isl_set
*)set2
);
883 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
884 __isl_take isl_map
*set2
)
886 return isl_set_lex_lt_set((isl_set
*)set1
, (isl_set
*)set2
);
889 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
890 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
892 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
895 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
896 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
898 return match_bin_op(uset1
, uset2
, &lex_le_set
);
901 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
902 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
904 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
907 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
908 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
910 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
913 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
914 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
916 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
919 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
920 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
922 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
925 static int intersect_domain_entry(void **entry
, void *user
)
927 struct isl_union_map_gen_bin_data
*data
= user
;
929 struct isl_hash_table_entry
*entry2
;
931 isl_map
*map
= *entry
;
934 dim
= isl_map_get_space(map
);
935 dim
= isl_space_domain(dim
);
936 hash
= isl_space_get_hash(dim
);
937 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
938 hash
, &has_dim
, dim
, 0);
943 map
= isl_map_copy(map
);
944 map
= isl_map_intersect_domain(map
, isl_set_copy(entry2
->data
));
946 empty
= isl_map_is_empty(map
);
956 data
->res
= isl_union_map_add_map(data
->res
, map
);
961 /* Intersect the domain of "umap" with "uset".
962 * If "uset" is a parameters domain, then intersect the parameter
963 * domain of "umap" with this set.
965 __isl_give isl_union_map
*isl_union_map_intersect_domain(
966 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
968 if (isl_union_set_is_params(uset
))
969 return union_map_intersect_params(umap
, uset
);
970 return gen_bin_op(umap
, uset
, &intersect_domain_entry
);
973 /* Remove the elements of data->umap2 from the domain of *entry
974 * and add the result to data->res.
976 static int subtract_domain_entry(void **entry
, void *user
)
978 struct isl_union_map_gen_bin_data
*data
= user
;
980 struct isl_hash_table_entry
*entry2
;
982 isl_map
*map
= *entry
;
985 dim
= isl_map_get_space(map
);
986 dim
= isl_space_domain(dim
);
987 hash
= isl_space_get_hash(dim
);
988 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
989 hash
, &has_dim
, dim
, 0);
992 map
= isl_map_copy(map
);
995 data
->res
= isl_union_map_add_map(data
->res
, map
);
999 map
= isl_map_subtract_domain(map
, isl_set_copy(entry2
->data
));
1001 empty
= isl_map_is_empty(map
);
1011 data
->res
= isl_union_map_add_map(data
->res
, map
);
1016 /* Remove the elements of "uset" from the domain of "umap".
1018 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1019 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1021 return gen_bin_op(umap
, dom
, &subtract_domain_entry
);
1024 /* Remove the elements of data->umap2 from the range of *entry
1025 * and add the result to data->res.
1027 static int subtract_range_entry(void **entry
, void *user
)
1029 struct isl_union_map_gen_bin_data
*data
= user
;
1031 struct isl_hash_table_entry
*entry2
;
1033 isl_map
*map
= *entry
;
1036 space
= isl_map_get_space(map
);
1037 space
= isl_space_range(space
);
1038 hash
= isl_space_get_hash(space
);
1039 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1040 hash
, &has_dim
, space
, 0);
1041 isl_space_free(space
);
1043 map
= isl_map_copy(map
);
1046 data
->res
= isl_union_map_add_map(data
->res
, map
);
1050 map
= isl_map_subtract_range(map
, isl_set_copy(entry2
->data
));
1052 empty
= isl_map_is_empty(map
);
1062 data
->res
= isl_union_map_add_map(data
->res
, map
);
1067 /* Remove the elements of "uset" from the range of "umap".
1069 __isl_give isl_union_map
*isl_union_map_subtract_range(
1070 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1072 return gen_bin_op(umap
, dom
, &subtract_range_entry
);
1075 static int gist_domain_entry(void **entry
, void *user
)
1077 struct isl_union_map_gen_bin_data
*data
= user
;
1079 struct isl_hash_table_entry
*entry2
;
1081 isl_map
*map
= *entry
;
1084 dim
= isl_map_get_space(map
);
1085 dim
= isl_space_domain(dim
);
1086 hash
= isl_space_get_hash(dim
);
1087 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1088 hash
, &has_dim
, dim
, 0);
1089 isl_space_free(dim
);
1093 map
= isl_map_copy(map
);
1094 map
= isl_map_gist_domain(map
, isl_set_copy(entry2
->data
));
1096 empty
= isl_map_is_empty(map
);
1102 data
->res
= isl_union_map_add_map(data
->res
, map
);
1107 /* Compute the gist of "umap" with respect to the domain "uset".
1108 * If "uset" is a parameters domain, then compute the gist
1109 * with respect to this parameter domain.
1111 __isl_give isl_union_map
*isl_union_map_gist_domain(
1112 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1114 if (isl_union_set_is_params(uset
))
1115 return union_map_gist_params(umap
, uset
);
1116 return gen_bin_op(umap
, uset
, &gist_domain_entry
);
1119 static int gist_range_entry(void **entry
, void *user
)
1121 struct isl_union_map_gen_bin_data
*data
= user
;
1123 struct isl_hash_table_entry
*entry2
;
1125 isl_map
*map
= *entry
;
1128 space
= isl_map_get_space(map
);
1129 space
= isl_space_range(space
);
1130 hash
= isl_space_get_hash(space
);
1131 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1132 hash
, &has_dim
, space
, 0);
1133 isl_space_free(space
);
1137 map
= isl_map_copy(map
);
1138 map
= isl_map_gist_range(map
, isl_set_copy(entry2
->data
));
1140 empty
= isl_map_is_empty(map
);
1146 data
->res
= isl_union_map_add_map(data
->res
, map
);
1151 /* Compute the gist of "umap" with respect to the range "uset".
1153 __isl_give isl_union_map
*isl_union_map_gist_range(
1154 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1156 return gen_bin_op(umap
, uset
, &gist_range_entry
);
1159 static int intersect_range_entry(void **entry
, void *user
)
1161 struct isl_union_map_gen_bin_data
*data
= user
;
1163 struct isl_hash_table_entry
*entry2
;
1165 isl_map
*map
= *entry
;
1168 dim
= isl_map_get_space(map
);
1169 dim
= isl_space_range(dim
);
1170 hash
= isl_space_get_hash(dim
);
1171 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1172 hash
, &has_dim
, dim
, 0);
1173 isl_space_free(dim
);
1177 map
= isl_map_copy(map
);
1178 map
= isl_map_intersect_range(map
, isl_set_copy(entry2
->data
));
1180 empty
= isl_map_is_empty(map
);
1190 data
->res
= isl_union_map_add_map(data
->res
, map
);
1195 __isl_give isl_union_map
*isl_union_map_intersect_range(
1196 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1198 return gen_bin_op(umap
, uset
, &intersect_range_entry
);
1201 struct isl_union_map_bin_data
{
1202 isl_union_map
*umap2
;
1205 int (*fn
)(void **entry
, void *user
);
1208 static int apply_range_entry(void **entry
, void *user
)
1210 struct isl_union_map_bin_data
*data
= user
;
1211 isl_map
*map2
= *entry
;
1214 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1215 map2
->dim
, isl_dim_in
))
1218 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1220 empty
= isl_map_is_empty(map2
);
1230 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1235 static int bin_entry(void **entry
, void *user
)
1237 struct isl_union_map_bin_data
*data
= user
;
1238 isl_map
*map
= *entry
;
1241 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1242 data
->fn
, data
) < 0)
1248 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1249 __isl_take isl_union_map
*umap2
, int (*fn
)(void **entry
, void *user
))
1251 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1253 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1254 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1256 if (!umap1
|| !umap2
)
1260 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1262 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1263 &bin_entry
, &data
) < 0)
1266 isl_union_map_free(umap1
);
1267 isl_union_map_free(umap2
);
1270 isl_union_map_free(umap1
);
1271 isl_union_map_free(umap2
);
1272 isl_union_map_free(data
.res
);
1276 __isl_give isl_union_map
*isl_union_map_apply_range(
1277 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1279 return bin_op(umap1
, umap2
, &apply_range_entry
);
1282 __isl_give isl_union_map
*isl_union_map_apply_domain(
1283 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1285 umap1
= isl_union_map_reverse(umap1
);
1286 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1287 return isl_union_map_reverse(umap1
);
1290 __isl_give isl_union_set
*isl_union_set_apply(
1291 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1293 return isl_union_map_apply_range(uset
, umap
);
1296 static int map_lex_lt_entry(void **entry
, void *user
)
1298 struct isl_union_map_bin_data
*data
= user
;
1299 isl_map
*map2
= *entry
;
1301 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1302 map2
->dim
, isl_dim_out
))
1305 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1307 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1312 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1313 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1315 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1318 static int map_lex_le_entry(void **entry
, void *user
)
1320 struct isl_union_map_bin_data
*data
= user
;
1321 isl_map
*map2
= *entry
;
1323 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1324 map2
->dim
, isl_dim_out
))
1327 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1329 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1334 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1335 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1337 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1340 static int product_entry(void **entry
, void *user
)
1342 struct isl_union_map_bin_data
*data
= user
;
1343 isl_map
*map2
= *entry
;
1345 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1347 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1352 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1353 __isl_take isl_union_map
*umap2
)
1355 return bin_op(umap1
, umap2
, &product_entry
);
1358 static int set_product_entry(void **entry
, void *user
)
1360 struct isl_union_map_bin_data
*data
= user
;
1361 isl_set
*set2
= *entry
;
1363 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1365 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1370 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1371 __isl_take isl_union_set
*uset2
)
1373 return bin_op(uset1
, uset2
, &set_product_entry
);
1376 static int domain_product_entry(void **entry
, void *user
)
1378 struct isl_union_map_bin_data
*data
= user
;
1379 isl_map
*map2
= *entry
;
1381 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1382 map2
->dim
, isl_dim_out
))
1385 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1386 isl_map_copy(map2
));
1388 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1393 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1395 __isl_give isl_union_map
*isl_union_map_domain_product(
1396 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1398 return bin_op(umap1
, umap2
, &domain_product_entry
);
1401 static int range_product_entry(void **entry
, void *user
)
1403 struct isl_union_map_bin_data
*data
= user
;
1404 isl_map
*map2
= *entry
;
1406 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1407 map2
->dim
, isl_dim_in
))
1410 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1411 isl_map_copy(map2
));
1413 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1418 __isl_give isl_union_map
*isl_union_map_range_product(
1419 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1421 return bin_op(umap1
, umap2
, &range_product_entry
);
1424 /* If data->map A -> B and "map2" C -> D have the same range space,
1425 * then add (A, C) -> (B * D) to data->res.
1427 static int flat_domain_product_entry(void **entry
, void *user
)
1429 struct isl_union_map_bin_data
*data
= user
;
1430 isl_map
*map2
= *entry
;
1432 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1433 map2
->dim
, isl_dim_out
))
1436 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1437 isl_map_copy(map2
));
1439 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1444 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1446 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1447 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1449 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1452 static int flat_range_product_entry(void **entry
, void *user
)
1454 struct isl_union_map_bin_data
*data
= user
;
1455 isl_map
*map2
= *entry
;
1457 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1458 map2
->dim
, isl_dim_in
))
1461 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1462 isl_map_copy(map2
));
1464 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1469 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1470 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1472 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1475 static __isl_give isl_union_set
*cond_un_op(__isl_take isl_union_map
*umap
,
1476 int (*fn
)(void **, void *))
1483 res
= isl_union_map_alloc(isl_space_copy(umap
->dim
), umap
->table
.n
);
1484 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, &res
) < 0)
1487 isl_union_map_free(umap
);
1490 isl_union_map_free(umap
);
1491 isl_union_set_free(res
);
1495 static int from_range_entry(void **entry
, void *user
)
1497 isl_map
*set
= *entry
;
1498 isl_union_set
**res
= user
;
1500 *res
= isl_union_map_add_map(*res
,
1501 isl_map_from_range(isl_set_copy(set
)));
1506 __isl_give isl_union_map
*isl_union_map_from_range(
1507 __isl_take isl_union_set
*uset
)
1509 return cond_un_op(uset
, &from_range_entry
);
1512 __isl_give isl_union_map
*isl_union_map_from_domain(
1513 __isl_take isl_union_set
*uset
)
1515 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1518 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1519 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1521 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1522 isl_union_map_from_range(range
));
1525 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1526 int (*fn
)(void **, void *))
1528 umap
= isl_union_map_cow(umap
);
1532 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
, fn
, NULL
) < 0)
1537 isl_union_map_free(umap
);
1541 static int affine_entry(void **entry
, void *user
)
1543 isl_map
**map
= (isl_map
**)entry
;
1545 *map
= isl_map_from_basic_map(isl_map_affine_hull(*map
));
1547 return *map
? 0 : -1;
1550 __isl_give isl_union_map
*isl_union_map_affine_hull(
1551 __isl_take isl_union_map
*umap
)
1553 return un_op(umap
, &affine_entry
);
1556 __isl_give isl_union_set
*isl_union_set_affine_hull(
1557 __isl_take isl_union_set
*uset
)
1559 return isl_union_map_affine_hull(uset
);
1562 static int polyhedral_entry(void **entry
, void *user
)
1564 isl_map
**map
= (isl_map
**)entry
;
1566 *map
= isl_map_from_basic_map(isl_map_polyhedral_hull(*map
));
1568 return *map
? 0 : -1;
1571 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1572 __isl_take isl_union_map
*umap
)
1574 return un_op(umap
, &polyhedral_entry
);
1577 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1578 __isl_take isl_union_set
*uset
)
1580 return isl_union_map_polyhedral_hull(uset
);
1583 static int simple_entry(void **entry
, void *user
)
1585 isl_map
**map
= (isl_map
**)entry
;
1587 *map
= isl_map_from_basic_map(isl_map_simple_hull(*map
));
1589 return *map
? 0 : -1;
1592 __isl_give isl_union_map
*isl_union_map_simple_hull(
1593 __isl_take isl_union_map
*umap
)
1595 return un_op(umap
, &simple_entry
);
1598 __isl_give isl_union_set
*isl_union_set_simple_hull(
1599 __isl_take isl_union_set
*uset
)
1601 return isl_union_map_simple_hull(uset
);
1604 static int inplace_entry(void **entry
, void *user
)
1606 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*);
1607 isl_map
**map
= (isl_map
**)entry
;
1610 fn
= *(__isl_give isl_map
*(**)(__isl_take isl_map
*)) user
;
1611 copy
= fn(isl_map_copy(*map
));
1621 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1622 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1627 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1628 &inplace_entry
, &fn
) < 0)
1633 isl_union_map_free(umap
);
1637 /* Remove redundant constraints in each of the basic maps of "umap".
1638 * Since removing redundant constraints does not change the meaning
1639 * or the space, the operation can be performed in-place.
1641 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1642 __isl_take isl_union_map
*umap
)
1644 return inplace(umap
, &isl_map_remove_redundancies
);
1647 /* Remove redundant constraints in each of the basic sets of "uset".
1649 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1650 __isl_take isl_union_set
*uset
)
1652 return isl_union_map_remove_redundancies(uset
);
1655 __isl_give isl_union_map
*isl_union_map_coalesce(
1656 __isl_take isl_union_map
*umap
)
1658 return inplace(umap
, &isl_map_coalesce
);
1661 __isl_give isl_union_set
*isl_union_set_coalesce(
1662 __isl_take isl_union_set
*uset
)
1664 return isl_union_map_coalesce(uset
);
1667 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1668 __isl_take isl_union_map
*umap
)
1670 return inplace(umap
, &isl_map_detect_equalities
);
1673 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1674 __isl_take isl_union_set
*uset
)
1676 return isl_union_map_detect_equalities(uset
);
1679 __isl_give isl_union_map
*isl_union_map_compute_divs(
1680 __isl_take isl_union_map
*umap
)
1682 return inplace(umap
, &isl_map_compute_divs
);
1685 __isl_give isl_union_set
*isl_union_set_compute_divs(
1686 __isl_take isl_union_set
*uset
)
1688 return isl_union_map_compute_divs(uset
);
1691 static int lexmin_entry(void **entry
, void *user
)
1693 isl_map
**map
= (isl_map
**)entry
;
1695 *map
= isl_map_lexmin(*map
);
1697 return *map
? 0 : -1;
1700 __isl_give isl_union_map
*isl_union_map_lexmin(
1701 __isl_take isl_union_map
*umap
)
1703 return un_op(umap
, &lexmin_entry
);
1706 __isl_give isl_union_set
*isl_union_set_lexmin(
1707 __isl_take isl_union_set
*uset
)
1709 return isl_union_map_lexmin(uset
);
1712 static int lexmax_entry(void **entry
, void *user
)
1714 isl_map
**map
= (isl_map
**)entry
;
1716 *map
= isl_map_lexmax(*map
);
1718 return *map
? 0 : -1;
1721 __isl_give isl_union_map
*isl_union_map_lexmax(
1722 __isl_take isl_union_map
*umap
)
1724 return un_op(umap
, &lexmax_entry
);
1727 __isl_give isl_union_set
*isl_union_set_lexmax(
1728 __isl_take isl_union_set
*uset
)
1730 return isl_union_map_lexmax(uset
);
1733 static int universe_entry(void **entry
, void *user
)
1735 isl_map
*map
= *entry
;
1736 isl_union_map
**res
= user
;
1738 map
= isl_map_universe(isl_map_get_space(map
));
1739 *res
= isl_union_map_add_map(*res
, map
);
1744 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1746 return cond_un_op(umap
, &universe_entry
);
1749 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1751 return isl_union_map_universe(uset
);
1754 static int reverse_entry(void **entry
, void *user
)
1756 isl_map
*map
= *entry
;
1757 isl_union_map
**res
= user
;
1759 *res
= isl_union_map_add_map(*res
, isl_map_reverse(isl_map_copy(map
)));
1764 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1766 return cond_un_op(umap
, &reverse_entry
);
1769 static int params_entry(void **entry
, void *user
)
1771 isl_map
*map
= *entry
;
1772 isl_union_set
**res
= user
;
1774 *res
= isl_union_set_add_set(*res
, isl_map_params(isl_map_copy(map
)));
1779 /* Compute the parameter domain of the given union map.
1781 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1785 empty
= isl_union_map_is_empty(umap
);
1790 space
= isl_union_map_get_space(umap
);
1791 isl_union_map_free(umap
);
1792 return isl_set_empty(space
);
1794 return isl_set_from_union_set(cond_un_op(umap
, ¶ms_entry
));
1796 isl_union_map_free(umap
);
1800 /* Compute the parameter domain of the given union set.
1802 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1804 return isl_union_map_params(uset
);
1807 static int domain_entry(void **entry
, void *user
)
1809 isl_map
*map
= *entry
;
1810 isl_union_set
**res
= user
;
1812 *res
= isl_union_set_add_set(*res
, isl_map_domain(isl_map_copy(map
)));
1817 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1819 return cond_un_op(umap
, &domain_entry
);
1822 static int range_entry(void **entry
, void *user
)
1824 isl_map
*map
= *entry
;
1825 isl_union_set
**res
= user
;
1827 *res
= isl_union_set_add_set(*res
, isl_map_range(isl_map_copy(map
)));
1832 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1834 return cond_un_op(umap
, &range_entry
);
1837 static int domain_map_entry(void **entry
, void *user
)
1839 isl_map
*map
= *entry
;
1840 isl_union_set
**res
= user
;
1842 *res
= isl_union_map_add_map(*res
,
1843 isl_map_domain_map(isl_map_copy(map
)));
1848 __isl_give isl_union_map
*isl_union_map_domain_map(
1849 __isl_take isl_union_map
*umap
)
1851 return cond_un_op(umap
, &domain_map_entry
);
1854 static int range_map_entry(void **entry
, void *user
)
1856 isl_map
*map
= *entry
;
1857 isl_union_set
**res
= user
;
1859 *res
= isl_union_map_add_map(*res
,
1860 isl_map_range_map(isl_map_copy(map
)));
1865 __isl_give isl_union_map
*isl_union_map_range_map(
1866 __isl_take isl_union_map
*umap
)
1868 return cond_un_op(umap
, &range_map_entry
);
1871 /* Check if "set" is of the form A[B -> C].
1872 * If so, add A[B -> C] -> B to "res".
1874 static int wrapped_domain_map_entry(void **entry
, void *user
)
1876 isl_set
*set
= *entry
;
1877 isl_union_set
**res
= user
;
1880 wrapping
= isl_set_is_wrapping(set
);
1886 *res
= isl_union_map_add_map(*res
,
1887 isl_set_wrapped_domain_map(isl_set_copy(set
)));
1892 /* Given a collection of wrapped maps of the form A[B -> C],
1893 * return the collection of maps A[B -> C] -> B.
1895 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
1896 __isl_take isl_union_set
*uset
)
1898 return cond_un_op(uset
, &wrapped_domain_map_entry
);
1901 static int deltas_entry(void **entry
, void *user
)
1903 isl_map
*map
= *entry
;
1904 isl_union_set
**res
= user
;
1906 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1907 map
->dim
, isl_dim_out
))
1910 *res
= isl_union_set_add_set(*res
, isl_map_deltas(isl_map_copy(map
)));
1915 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1917 return cond_un_op(umap
, &deltas_entry
);
1920 static int deltas_map_entry(void **entry
, void *user
)
1922 isl_map
*map
= *entry
;
1923 isl_union_map
**res
= user
;
1925 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1926 map
->dim
, isl_dim_out
))
1929 *res
= isl_union_map_add_map(*res
,
1930 isl_map_deltas_map(isl_map_copy(map
)));
1935 __isl_give isl_union_map
*isl_union_map_deltas_map(
1936 __isl_take isl_union_map
*umap
)
1938 return cond_un_op(umap
, &deltas_map_entry
);
1941 static int identity_entry(void **entry
, void *user
)
1943 isl_set
*set
= *entry
;
1944 isl_union_map
**res
= user
;
1946 *res
= isl_union_map_add_map(*res
, isl_set_identity(isl_set_copy(set
)));
1951 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1953 return cond_un_op(uset
, &identity_entry
);
1956 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
1958 static int identity_upma(__isl_take isl_set
*set
, void *user
)
1960 isl_union_pw_multi_aff
**res
= user
;
1962 isl_pw_multi_aff
*pma
;
1964 space
= isl_space_map_from_set(isl_set_get_space(set
));
1965 pma
= isl_pw_multi_aff_identity(space
);
1966 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
1967 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1969 return *res
? 0 : -1;
1972 /* Return an identity function on "uset" in the form
1973 * of an isl_union_pw_multi_aff.
1975 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
1976 __isl_take isl_union_set
*uset
)
1978 isl_union_pw_multi_aff
*res
;
1980 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
1981 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
1982 res
= isl_union_pw_multi_aff_free(res
);
1984 isl_union_set_free(uset
);
1988 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
1990 static int domain_factor_domain_entry(void **entry
, void *user
)
1992 isl_map
*map
= *entry
;
1993 isl_union_map
**res
= user
;
1995 if (!isl_map_domain_is_wrapping(map
))
1998 *res
= isl_union_map_add_map(*res
,
1999 isl_map_domain_factor_domain(isl_map_copy(map
)));
2001 return *res
? 0 : -1;
2004 /* For each map in "umap" of the form [A -> B] -> C,
2005 * construct the map A -> C and collect the results.
2007 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2008 __isl_take isl_union_map
*umap
)
2010 return cond_un_op(umap
, &domain_factor_domain_entry
);
2013 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2015 static int domain_factor_range_entry(void **entry
, void *user
)
2017 isl_map
*map
= *entry
;
2018 isl_union_map
**res
= user
;
2020 if (!isl_map_domain_is_wrapping(map
))
2023 *res
= isl_union_map_add_map(*res
,
2024 isl_map_domain_factor_range(isl_map_copy(map
)));
2026 return *res
? 0 : -1;
2029 /* For each map in "umap" of the form [A -> B] -> C,
2030 * construct the map B -> C and collect the results.
2032 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2033 __isl_take isl_union_map
*umap
)
2035 return cond_un_op(umap
, &domain_factor_range_entry
);
2038 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2040 static int range_factor_range_entry(void **entry
, void *user
)
2042 isl_map
*map
= *entry
;
2043 isl_union_map
**res
= user
;
2045 if (!isl_map_range_is_wrapping(map
))
2048 *res
= isl_union_map_add_map(*res
,
2049 isl_map_range_factor_range(isl_map_copy(map
)));
2051 return *res
? 0 : -1;
2054 /* For each map in "umap" of the form A -> [B -> C],
2055 * construct the map A -> C and collect the results.
2057 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2058 __isl_take isl_union_map
*umap
)
2060 return cond_un_op(umap
, &range_factor_range_entry
);
2063 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2065 static int factor_domain_entry(void **entry
, void *user
)
2067 isl_map
*map
= *entry
;
2068 isl_union_map
**res
= user
;
2070 if (!isl_map_domain_is_wrapping(map
) || !isl_map_range_is_wrapping(map
))
2073 *res
= isl_union_map_add_map(*res
,
2074 isl_map_factor_domain(isl_map_copy(map
)));
2076 return *res
? 0 : -1;
2079 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2080 * construct the map A -> C and collect the results.
2082 __isl_give isl_union_map
*isl_union_map_factor_domain(
2083 __isl_take isl_union_map
*umap
)
2085 return cond_un_op(umap
, &factor_domain_entry
);
2088 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2090 static int factor_range_entry(void **entry
, void *user
)
2092 isl_map
*map
= *entry
;
2093 isl_union_map
**res
= user
;
2095 if (!isl_map_domain_is_wrapping(map
) || !isl_map_range_is_wrapping(map
))
2098 *res
= isl_union_map_add_map(*res
,
2099 isl_map_factor_range(isl_map_copy(map
)));
2101 return *res
? 0 : -1;
2104 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2105 * construct the map B -> D and collect the results.
2107 __isl_give isl_union_map
*isl_union_map_factor_range(
2108 __isl_take isl_union_map
*umap
)
2110 return cond_un_op(umap
, &factor_range_entry
);
2113 static int unwrap_entry(void **entry
, void *user
)
2115 isl_set
*set
= *entry
;
2116 isl_union_set
**res
= user
;
2118 if (!isl_set_is_wrapping(set
))
2121 *res
= isl_union_map_add_map(*res
, isl_set_unwrap(isl_set_copy(set
)));
2126 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2128 return cond_un_op(uset
, &unwrap_entry
);
2131 static int wrap_entry(void **entry
, void *user
)
2133 isl_map
*map
= *entry
;
2134 isl_union_set
**res
= user
;
2136 *res
= isl_union_set_add_set(*res
, isl_map_wrap(isl_map_copy(map
)));
2141 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2143 return cond_un_op(umap
, &wrap_entry
);
2146 struct isl_union_map_is_subset_data
{
2147 isl_union_map
*umap2
;
2151 static int is_subset_entry(void **entry
, void *user
)
2153 struct isl_union_map_is_subset_data
*data
= user
;
2155 struct isl_hash_table_entry
*entry2
;
2156 isl_map
*map
= *entry
;
2158 hash
= isl_space_get_hash(map
->dim
);
2159 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2160 hash
, &has_dim
, map
->dim
, 0);
2162 int empty
= isl_map_is_empty(map
);
2167 data
->is_subset
= 0;
2171 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2172 if (data
->is_subset
< 0 || !data
->is_subset
)
2178 int isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2179 __isl_keep isl_union_map
*umap2
)
2181 struct isl_union_map_is_subset_data data
= { NULL
, 1 };
2183 umap1
= isl_union_map_copy(umap1
);
2184 umap2
= isl_union_map_copy(umap2
);
2185 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2186 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2188 if (!umap1
|| !umap2
)
2192 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2193 &is_subset_entry
, &data
) < 0 &&
2197 isl_union_map_free(umap1
);
2198 isl_union_map_free(umap2
);
2200 return data
.is_subset
;
2202 isl_union_map_free(umap1
);
2203 isl_union_map_free(umap2
);
2207 int isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2208 __isl_keep isl_union_set
*uset2
)
2210 return isl_union_map_is_subset(uset1
, uset2
);
2213 int isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2214 __isl_keep isl_union_map
*umap2
)
2218 if (!umap1
|| !umap2
)
2220 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2223 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2227 int isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2228 __isl_keep isl_union_set
*uset2
)
2230 return isl_union_map_is_equal(uset1
, uset2
);
2233 int isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2234 __isl_keep isl_union_map
*umap2
)
2238 if (!umap1
|| !umap2
)
2240 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2243 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2244 if (is_subset
== -1)
2249 int isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2250 __isl_keep isl_union_set
*uset2
)
2252 return isl_union_map_is_strict_subset(uset1
, uset2
);
2255 /* Internal data structure for isl_union_map_is_disjoint.
2256 * umap2 is the union map with which we are comparing.
2257 * is_disjoint is initialized to 1 and is set to 0 as soon
2258 * as the union maps turn out not to be disjoint.
2260 struct isl_union_map_is_disjoint_data
{
2261 isl_union_map
*umap2
;
2265 /* Check if "map" is disjoint from data->umap2 and abort
2266 * the search if it is not.
2268 static int is_disjoint_entry(void **entry
, void *user
)
2270 struct isl_union_map_is_disjoint_data
*data
= user
;
2272 struct isl_hash_table_entry
*entry2
;
2273 isl_map
*map
= *entry
;
2275 hash
= isl_space_get_hash(map
->dim
);
2276 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2277 hash
, &has_dim
, map
->dim
, 0);
2281 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2282 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2288 /* Are "umap1" and "umap2" disjoint?
2290 int isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2291 __isl_keep isl_union_map
*umap2
)
2293 struct isl_union_map_is_disjoint_data data
= { NULL
, 1 };
2295 umap1
= isl_union_map_copy(umap1
);
2296 umap2
= isl_union_map_copy(umap2
);
2297 umap1
= isl_union_map_align_params(umap1
,
2298 isl_union_map_get_space(umap2
));
2299 umap2
= isl_union_map_align_params(umap2
,
2300 isl_union_map_get_space(umap1
));
2302 if (!umap1
|| !umap2
)
2306 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2307 &is_disjoint_entry
, &data
) < 0 &&
2311 isl_union_map_free(umap1
);
2312 isl_union_map_free(umap2
);
2314 return data
.is_disjoint
;
2316 isl_union_map_free(umap1
);
2317 isl_union_map_free(umap2
);
2321 /* Are "uset1" and "uset2" disjoint?
2323 int isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2324 __isl_keep isl_union_set
*uset2
)
2326 return isl_union_map_is_disjoint(uset1
, uset2
);
2329 static int sample_entry(void **entry
, void *user
)
2331 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2332 isl_map
*map
= *entry
;
2334 *sample
= isl_map_sample(isl_map_copy(map
));
2337 if (!isl_basic_map_plain_is_empty(*sample
))
2342 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2344 isl_basic_map
*sample
= NULL
;
2349 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2350 &sample_entry
, &sample
) < 0 &&
2355 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2357 isl_union_map_free(umap
);
2361 isl_union_map_free(umap
);
2365 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2367 return (isl_basic_set
*)isl_union_map_sample(uset
);
2370 struct isl_forall_data
{
2372 int (*fn
)(__isl_keep isl_map
*map
);
2375 static int forall_entry(void **entry
, void *user
)
2377 struct isl_forall_data
*data
= user
;
2378 isl_map
*map
= *entry
;
2380 data
->res
= data
->fn(map
);
2390 static int union_map_forall(__isl_keep isl_union_map
*umap
,
2391 int (*fn
)(__isl_keep isl_map
*map
))
2393 struct isl_forall_data data
= { 1, fn
};
2398 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2399 &forall_entry
, &data
) < 0 && data
.res
)
2405 struct isl_forall_user_data
{
2407 int (*fn
)(__isl_keep isl_map
*map
, void *user
);
2411 static int forall_user_entry(void **entry
, void *user
)
2413 struct isl_forall_user_data
*data
= user
;
2414 isl_map
*map
= *entry
;
2416 data
->res
= data
->fn(map
, data
->user
);
2426 /* Check if fn(map, user) returns true for all maps "map" in umap.
2428 static int union_map_forall_user(__isl_keep isl_union_map
*umap
,
2429 int (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2431 struct isl_forall_user_data data
= { 1, fn
, user
};
2436 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2437 &forall_user_entry
, &data
) < 0 && data
.res
)
2443 int isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2445 return union_map_forall(umap
, &isl_map_is_empty
);
2448 int isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2450 return isl_union_map_is_empty(uset
);
2453 static int is_subset_of_identity(__isl_keep isl_map
*map
)
2462 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2463 map
->dim
, isl_dim_out
))
2466 dim
= isl_map_get_space(map
);
2467 id
= isl_map_identity(dim
);
2469 is_subset
= isl_map_is_subset(map
, id
);
2476 /* Given an isl_union_map that consists of a single map, check
2477 * if it is single-valued.
2479 static int single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2484 umap
= isl_union_map_copy(umap
);
2485 map
= isl_map_from_union_map(umap
);
2486 sv
= isl_map_is_single_valued(map
);
2492 /* Internal data structure for single_valued_on_domain.
2494 * "umap" is the union map to be tested.
2495 * "sv" is set to 1 as long as "umap" may still be single-valued.
2497 struct isl_union_map_is_sv_data
{
2498 isl_union_map
*umap
;
2502 /* Check if the data->umap is single-valued on "set".
2504 * If data->umap consists of a single map on "set", then test it
2507 * Otherwise, compute
2511 * check if the result is a subset of the identity mapping and
2512 * store the result in data->sv.
2514 * Terminate as soon as data->umap has been determined not to
2517 static int single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2519 struct isl_union_map_is_sv_data
*data
= user
;
2520 isl_union_map
*umap
, *test
;
2522 umap
= isl_union_map_copy(data
->umap
);
2523 umap
= isl_union_map_intersect_domain(umap
,
2524 isl_union_set_from_set(set
));
2526 if (isl_union_map_n_map(umap
) == 1) {
2527 data
->sv
= single_map_is_single_valued(umap
);
2528 isl_union_map_free(umap
);
2530 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2531 test
= isl_union_map_apply_range(test
, umap
);
2533 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2535 isl_union_map_free(test
);
2538 if (data
->sv
< 0 || !data
->sv
)
2543 /* Check if the given map is single-valued.
2545 * If the union map consists of a single map, then test it as an isl_map.
2546 * Otherwise, check if the union map is single-valued on each of its
2549 int isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2551 isl_union_map
*universe
;
2552 isl_union_set
*domain
;
2553 struct isl_union_map_is_sv_data data
;
2555 if (isl_union_map_n_map(umap
) == 1)
2556 return single_map_is_single_valued(umap
);
2558 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2559 domain
= isl_union_map_domain(universe
);
2563 if (isl_union_set_foreach_set(domain
,
2564 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2566 isl_union_set_free(domain
);
2571 int isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2575 umap
= isl_union_map_copy(umap
);
2576 umap
= isl_union_map_reverse(umap
);
2577 in
= isl_union_map_is_single_valued(umap
);
2578 isl_union_map_free(umap
);
2583 /* Represents a map that has a fixed value (v) for one of its
2585 * The map in this structure is not reference counted, so it
2586 * is only valid while the isl_union_map from which it was
2587 * obtained is still alive.
2589 struct isl_fixed_map
{
2594 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2598 struct isl_fixed_map
*v
;
2600 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2603 for (i
= 0; i
< n
; ++i
)
2604 isl_int_init(v
[i
].v
);
2608 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2614 for (i
= 0; i
< n
; ++i
)
2615 isl_int_clear(v
[i
].v
);
2619 /* Compare the "v" field of two isl_fixed_map structs.
2621 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2623 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2624 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2626 return isl_int_cmp(e1
->v
, e2
->v
);
2629 /* Internal data structure used while checking whether all maps
2630 * in a union_map have a fixed value for a given output dimension.
2631 * v is the list of maps, with the fixed value for the dimension
2632 * n is the number of maps considered so far
2633 * pos is the output dimension under investigation
2635 struct isl_fixed_dim_data
{
2636 struct isl_fixed_map
*v
;
2641 static int fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2643 struct isl_fixed_dim_data
*data
= user
;
2645 data
->v
[data
->n
].map
= map
;
2646 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2647 &data
->v
[data
->n
++].v
);
2650 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
2651 int first
, int n_range
);
2653 /* Given a list of the maps, with their fixed values at output dimension "pos",
2654 * check whether the ranges of the maps form an obvious partition.
2656 * We first sort the maps according to their fixed values.
2657 * If all maps have a different value, then we know the ranges form
2659 * Otherwise, we collect the maps with the same fixed value and
2660 * check whether each such collection is obviously injective
2661 * based on later dimensions.
2663 static int separates(struct isl_fixed_map
*v
, int n
,
2664 __isl_take isl_space
*dim
, int pos
, int n_range
)
2671 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2673 for (i
= 0; i
+ 1 < n
; ++i
) {
2675 isl_union_map
*part
;
2678 for (j
= i
+ 1; j
< n
; ++j
)
2679 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2685 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2686 for (k
= i
; k
< j
; ++k
)
2687 part
= isl_union_map_add_map(part
,
2688 isl_map_copy(v
[k
].map
));
2690 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2699 isl_space_free(dim
);
2700 free_isl_fixed_map_array(v
, n
);
2703 isl_space_free(dim
);
2704 free_isl_fixed_map_array(v
, n
);
2708 /* Check whether the maps in umap have obviously distinct ranges.
2709 * In particular, check for an output dimension in the range
2710 * [first,n_range) for which all maps have a fixed value
2711 * and then check if these values, possibly along with fixed values
2712 * at later dimensions, entail distinct ranges.
2714 static int plain_injective_on_range(__isl_take isl_union_map
*umap
,
2715 int first
, int n_range
)
2719 struct isl_fixed_dim_data data
= { NULL
};
2721 ctx
= isl_union_map_get_ctx(umap
);
2723 n
= isl_union_map_n_map(umap
);
2728 isl_union_map_free(umap
);
2732 if (first
>= n_range
) {
2733 isl_union_map_free(umap
);
2737 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2741 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2747 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2752 dim
= isl_union_map_get_space(umap
);
2753 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2754 isl_union_map_free(umap
);
2758 free_isl_fixed_map_array(data
.v
, n
);
2759 isl_union_map_free(umap
);
2763 free_isl_fixed_map_array(data
.v
, n
);
2764 isl_union_map_free(umap
);
2768 /* Check whether the maps in umap that map to subsets of "ran"
2769 * have obviously distinct ranges.
2771 static int plain_injective_on_range_wrap(__isl_keep isl_set
*ran
, void *user
)
2773 isl_union_map
*umap
= user
;
2775 umap
= isl_union_map_copy(umap
);
2776 umap
= isl_union_map_intersect_range(umap
,
2777 isl_union_set_from_set(isl_set_copy(ran
)));
2778 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2781 /* Check if the given union_map is obviously injective.
2783 * In particular, we first check if all individual maps are obviously
2784 * injective and then check if all the ranges of these maps are
2785 * obviously disjoint.
2787 int isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2790 isl_union_map
*univ
;
2793 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2799 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2800 ran
= isl_union_map_range(univ
);
2802 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2804 isl_union_set_free(ran
);
2809 int isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2813 sv
= isl_union_map_is_single_valued(umap
);
2817 return isl_union_map_is_injective(umap
);
2820 static int zip_entry(void **entry
, void *user
)
2822 isl_map
*map
= *entry
;
2823 isl_union_map
**res
= user
;
2825 if (!isl_map_can_zip(map
))
2828 *res
= isl_union_map_add_map(*res
, isl_map_zip(isl_map_copy(map
)));
2833 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2835 return cond_un_op(umap
, &zip_entry
);
2838 static int uncurry_entry(void **entry
, void *user
)
2840 isl_map
*map
= *entry
;
2841 isl_union_map
**res
= user
;
2843 if (!isl_map_can_uncurry(map
))
2846 *res
= isl_union_map_add_map(*res
, isl_map_uncurry(isl_map_copy(map
)));
2851 /* Given a union map, take the maps of the form A -> (B -> C) and
2852 * return the union of the corresponding maps (A -> B) -> C.
2854 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
2856 return cond_un_op(umap
, &uncurry_entry
);
2859 static int curry_entry(void **entry
, void *user
)
2861 isl_map
*map
= *entry
;
2862 isl_union_map
**res
= user
;
2864 if (!isl_map_can_curry(map
))
2867 *res
= isl_union_map_add_map(*res
, isl_map_curry(isl_map_copy(map
)));
2872 /* Given a union map, take the maps of the form (A -> B) -> C and
2873 * return the union of the corresponding maps A -> (B -> C).
2875 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
2877 return cond_un_op(umap
, &curry_entry
);
2880 static int lift_entry(void **entry
, void *user
)
2882 isl_set
*set
= *entry
;
2883 isl_union_set
**res
= user
;
2885 *res
= isl_union_set_add_set(*res
, isl_set_lift(isl_set_copy(set
)));
2890 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
2892 return cond_un_op(uset
, &lift_entry
);
2895 static int coefficients_entry(void **entry
, void *user
)
2897 isl_set
*set
= *entry
;
2898 isl_union_set
**res
= user
;
2900 set
= isl_set_copy(set
);
2901 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
2902 *res
= isl_union_set_add_set(*res
, set
);
2907 __isl_give isl_union_set
*isl_union_set_coefficients(
2908 __isl_take isl_union_set
*uset
)
2917 ctx
= isl_union_set_get_ctx(uset
);
2918 dim
= isl_space_set_alloc(ctx
, 0, 0);
2919 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2920 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2921 &coefficients_entry
, &res
) < 0)
2924 isl_union_set_free(uset
);
2927 isl_union_set_free(uset
);
2928 isl_union_set_free(res
);
2932 static int solutions_entry(void **entry
, void *user
)
2934 isl_set
*set
= *entry
;
2935 isl_union_set
**res
= user
;
2937 set
= isl_set_copy(set
);
2938 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2940 *res
= isl_union_set_from_set(set
);
2942 *res
= isl_union_set_add_set(*res
, set
);
2950 __isl_give isl_union_set
*isl_union_set_solutions(
2951 __isl_take isl_union_set
*uset
)
2953 isl_union_set
*res
= NULL
;
2958 if (uset
->table
.n
== 0) {
2959 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2960 isl_union_set_free(uset
);
2964 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2965 &solutions_entry
, &res
) < 0)
2968 isl_union_set_free(uset
);
2971 isl_union_set_free(uset
);
2972 isl_union_set_free(res
);
2976 /* Is the domain space of "map" equal to "space"?
2978 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2980 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2981 space
, isl_dim_out
);
2984 /* Is the range space of "map" equal to "space"?
2986 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2988 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
2989 space
, isl_dim_out
);
2992 /* Is the set space of "map" equal to "space"?
2994 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2996 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
2997 space
, isl_dim_out
);
3000 /* Internal data structure for preimage_pw_multi_aff.
3002 * "pma" is the function under which the preimage should be taken.
3003 * "space" is the space of "pma".
3004 * "res" collects the results.
3005 * "fn" computes the preimage for a given map.
3006 * "match" returns true if "fn" can be called.
3008 struct isl_union_map_preimage_data
{
3010 isl_pw_multi_aff
*pma
;
3012 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3013 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3014 __isl_take isl_pw_multi_aff
*pma
);
3017 /* Call data->fn to compute the preimage of the domain or range of *entry
3018 * under the function represented by data->pma, provided the domain/range
3019 * space of *entry matches the target space of data->pma
3020 * (as given by data->match), and add the result to data->res.
3022 static int preimage_entry(void **entry
, void *user
)
3025 isl_map
*map
= *entry
;
3026 struct isl_union_map_preimage_data
*data
= user
;
3029 m
= data
->match(map
, data
->space
);
3035 map
= isl_map_copy(map
);
3036 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3038 empty
= isl_map_is_empty(map
);
3039 if (empty
< 0 || empty
) {
3041 return empty
< 0 ? -1 : 0;
3044 data
->res
= isl_union_map_add_map(data
->res
, map
);
3049 /* Compute the preimage of the domain or range of "umap" under the function
3050 * represented by "pma".
3051 * In other words, plug in "pma" in the domain or range of "umap".
3052 * The function "fn" performs the actual preimage computation on a map,
3053 * while "match" determines to which maps the function should be applied.
3055 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3056 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3057 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3058 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3059 __isl_take isl_pw_multi_aff
*pma
))
3063 struct isl_union_map_preimage_data data
;
3065 umap
= isl_union_map_align_params(umap
,
3066 isl_pw_multi_aff_get_space(pma
));
3067 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3072 ctx
= isl_union_map_get_ctx(umap
);
3073 space
= isl_union_map_get_space(umap
);
3074 data
.space
= isl_pw_multi_aff_get_space(pma
);
3076 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3079 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3081 data
.res
= isl_union_map_free(data
.res
);
3083 isl_space_free(data
.space
);
3084 isl_union_map_free(umap
);
3085 isl_pw_multi_aff_free(pma
);
3088 isl_union_map_free(umap
);
3089 isl_pw_multi_aff_free(pma
);
3093 /* Compute the preimage of the domain of "umap" under the function
3094 * represented by "pma".
3095 * In other words, plug in "pma" in the domain of "umap".
3096 * The result contains maps that live in the same spaces as the maps of "umap"
3097 * with domain space equal to the target space of "pma",
3098 * except that the domain has been replaced by the domain space of "pma".
3100 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3101 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3103 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3104 &isl_map_preimage_domain_pw_multi_aff
);
3107 /* Compute the preimage of the range of "umap" under the function
3108 * represented by "pma".
3109 * In other words, plug in "pma" in the range of "umap".
3110 * The result contains maps that live in the same spaces as the maps of "umap"
3111 * with range space equal to the target space of "pma",
3112 * except that the range has been replaced by the domain space of "pma".
3114 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3115 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3117 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3118 &isl_map_preimage_range_pw_multi_aff
);
3121 /* Compute the preimage of "uset" under the function represented by "pma".
3122 * In other words, plug in "pma" in "uset".
3123 * The result contains sets that live in the same spaces as the sets of "uset"
3124 * with space equal to the target space of "pma",
3125 * except that the space has been replaced by the domain space of "pma".
3127 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3128 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3130 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3131 &isl_set_preimage_pw_multi_aff
);
3134 /* Compute the preimage of the domain of "umap" under the function
3135 * represented by "ma".
3136 * In other words, plug in "ma" in the domain of "umap".
3137 * The result contains maps that live in the same spaces as the maps of "umap"
3138 * with domain space equal to the target space of "ma",
3139 * except that the domain has been replaced by the domain space of "ma".
3141 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3142 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3144 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3145 isl_pw_multi_aff_from_multi_aff(ma
));
3148 /* Compute the preimage of the range of "umap" under the function
3149 * represented by "ma".
3150 * In other words, plug in "ma" in the range of "umap".
3151 * The result contains maps that live in the same spaces as the maps of "umap"
3152 * with range space equal to the target space of "ma",
3153 * except that the range has been replaced by the domain space of "ma".
3155 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3156 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3158 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3159 isl_pw_multi_aff_from_multi_aff(ma
));
3162 /* Compute the preimage of "uset" under the function represented by "ma".
3163 * In other words, plug in "ma" in "uset".
3164 * The result contains sets that live in the same spaces as the sets of "uset"
3165 * with space equal to the target space of "ma",
3166 * except that the space has been replaced by the domain space of "ma".
3168 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3169 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3171 return isl_union_set_preimage_pw_multi_aff(uset
,
3172 isl_pw_multi_aff_from_multi_aff(ma
));
3175 /* Internal data structure for preimage_multi_pw_aff.
3177 * "mpa" is the function under which the preimage should be taken.
3178 * "space" is the space of "mpa".
3179 * "res" collects the results.
3180 * "fn" computes the preimage for a given map.
3181 * "match" returns true if "fn" can be called.
3183 struct isl_union_map_preimage_mpa_data
{
3185 isl_multi_pw_aff
*mpa
;
3187 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3188 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3189 __isl_take isl_multi_pw_aff
*mpa
);
3192 /* Call data->fn to compute the preimage of the domain or range of *entry
3193 * under the function represented by data->mpa, provided the domain/range
3194 * space of *entry matches the target space of data->mpa
3195 * (as given by data->match), and add the result to data->res.
3197 static int preimage_mpa_entry(void **entry
, void *user
)
3200 isl_map
*map
= *entry
;
3201 struct isl_union_map_preimage_mpa_data
*data
= user
;
3204 m
= data
->match(map
, data
->space
);
3210 map
= isl_map_copy(map
);
3211 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3213 empty
= isl_map_is_empty(map
);
3214 if (empty
< 0 || empty
) {
3216 return empty
< 0 ? -1 : 0;
3219 data
->res
= isl_union_map_add_map(data
->res
, map
);
3224 /* Compute the preimage of the domain or range of "umap" under the function
3225 * represented by "mpa".
3226 * In other words, plug in "mpa" in the domain or range of "umap".
3227 * The function "fn" performs the actual preimage computation on a map,
3228 * while "match" determines to which maps the function should be applied.
3230 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3231 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3232 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3233 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3234 __isl_take isl_multi_pw_aff
*mpa
))
3238 struct isl_union_map_preimage_mpa_data data
;
3240 umap
= isl_union_map_align_params(umap
,
3241 isl_multi_pw_aff_get_space(mpa
));
3242 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3247 ctx
= isl_union_map_get_ctx(umap
);
3248 space
= isl_union_map_get_space(umap
);
3249 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3251 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3254 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3256 data
.res
= isl_union_map_free(data
.res
);
3258 isl_space_free(data
.space
);
3259 isl_union_map_free(umap
);
3260 isl_multi_pw_aff_free(mpa
);
3263 isl_union_map_free(umap
);
3264 isl_multi_pw_aff_free(mpa
);
3268 /* Compute the preimage of the domain of "umap" under the function
3269 * represented by "mpa".
3270 * In other words, plug in "mpa" in the domain of "umap".
3271 * The result contains maps that live in the same spaces as the maps of "umap"
3272 * with domain space equal to the target space of "mpa",
3273 * except that the domain has been replaced by the domain space of "mpa".
3275 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3276 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3278 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3279 &isl_map_preimage_domain_multi_pw_aff
);
3282 /* Internal data structure for preimage_upma.
3284 * "umap" is the map of which the preimage should be computed.
3285 * "res" collects the results.
3286 * "fn" computes the preimage for a given piecewise multi-affine function.
3288 struct isl_union_map_preimage_upma_data
{
3289 isl_union_map
*umap
;
3291 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3292 __isl_take isl_pw_multi_aff
*pma
);
3295 /* Call data->fn to compute the preimage of the domain or range of data->umap
3296 * under the function represented by pma and add the result to data->res.
3298 static int preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3300 struct isl_union_map_preimage_upma_data
*data
= user
;
3301 isl_union_map
*umap
;
3303 umap
= isl_union_map_copy(data
->umap
);
3304 umap
= data
->fn(umap
, pma
);
3305 data
->res
= isl_union_map_union(data
->res
, umap
);
3307 return data
->res
? 0 : -1;
3310 /* Compute the preimage of the domain or range of "umap" under the function
3311 * represented by "upma".
3312 * In other words, plug in "upma" in the domain or range of "umap".
3313 * The function "fn" performs the actual preimage computation
3314 * on a piecewise multi-affine function.
3316 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3317 __isl_take isl_union_map
*umap
,
3318 __isl_take isl_union_pw_multi_aff
*upma
,
3319 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3320 __isl_take isl_pw_multi_aff
*pma
))
3322 struct isl_union_map_preimage_upma_data data
;
3325 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3327 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3328 &preimage_upma
, &data
) < 0)
3329 data
.res
= isl_union_map_free(data
.res
);
3331 isl_union_map_free(umap
);
3332 isl_union_pw_multi_aff_free(upma
);
3337 /* Compute the preimage of the domain of "umap" under the function
3338 * represented by "upma".
3339 * In other words, plug in "upma" in the domain of "umap".
3340 * The result contains maps that live in the same spaces as the maps of "umap"
3341 * with domain space equal to one of the target spaces of "upma",
3342 * except that the domain has been replaced by one of the the domain spaces that
3343 * corresponds to that target space of "upma".
3345 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3346 __isl_take isl_union_map
*umap
,
3347 __isl_take isl_union_pw_multi_aff
*upma
)
3349 return preimage_union_pw_multi_aff(umap
, upma
,
3350 &isl_union_map_preimage_domain_pw_multi_aff
);
3353 /* Compute the preimage of the range of "umap" under the function
3354 * represented by "upma".
3355 * In other words, plug in "upma" in the range of "umap".
3356 * The result contains maps that live in the same spaces as the maps of "umap"
3357 * with range space equal to one of the target spaces of "upma",
3358 * except that the range has been replaced by one of the the domain spaces that
3359 * corresponds to that target space of "upma".
3361 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3362 __isl_take isl_union_map
*umap
,
3363 __isl_take isl_union_pw_multi_aff
*upma
)
3365 return preimage_union_pw_multi_aff(umap
, upma
,
3366 &isl_union_map_preimage_range_pw_multi_aff
);
3369 /* Compute the preimage of "uset" under the function represented by "upma".
3370 * In other words, plug in "upma" in the range of "uset".
3371 * The result contains sets that live in the same spaces as the sets of "uset"
3372 * with space equal to one of the target spaces of "upma",
3373 * except that the space has been replaced by one of the the domain spaces that
3374 * corresponds to that target space of "upma".
3376 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3377 __isl_take isl_union_set
*uset
,
3378 __isl_take isl_union_pw_multi_aff
*upma
)
3380 return preimage_union_pw_multi_aff(uset
, upma
,
3381 &isl_union_set_preimage_pw_multi_aff
);
3384 /* Reset the user pointer on all identifiers of parameters and tuples
3385 * of the space of *entry.
3387 static int reset_user(void **entry
, void *user
)
3389 isl_map
**map
= (isl_map
**)entry
;
3391 *map
= isl_map_reset_user(*map
);
3393 return *map
? 0 : -1;
3396 /* Reset the user pointer on all identifiers of parameters and tuples
3397 * of the spaces of "umap".
3399 __isl_give isl_union_map
*isl_union_map_reset_user(
3400 __isl_take isl_union_map
*umap
)
3402 umap
= isl_union_map_cow(umap
);
3405 umap
->dim
= isl_space_reset_user(umap
->dim
);
3407 return isl_union_map_free(umap
);
3408 umap
= un_op(umap
, &reset_user
);
3413 /* Reset the user pointer on all identifiers of parameters and tuples
3414 * of the spaces of "uset".
3416 __isl_give isl_union_set
*isl_union_set_reset_user(
3417 __isl_take isl_union_set
*uset
)
3419 return isl_union_map_reset_user(uset
);
3422 /* Internal data structure for isl_union_map_project_out.
3423 * "type", "first" and "n" are the arguments for the isl_map_project_out
3425 * "res" collects the results.
3427 struct isl_union_map_project_out_data
{
3428 enum isl_dim_type type
;
3435 /* Turn the data->n dimensions of type data->type, starting at data->first
3436 * into existentially quantified variables and add the result to data->res.
3438 static int project_out(__isl_take isl_map
*map
, void *user
)
3440 struct isl_union_map_project_out_data
*data
= user
;
3442 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3443 data
->res
= isl_union_map_add_map(data
->res
, map
);
3448 /* Turn the "n" dimensions of type "type", starting at "first"
3449 * into existentially quantified variables.
3450 * Since the space of an isl_union_map only contains parameters,
3451 * type is required to be equal to isl_dim_param.
3453 __isl_give isl_union_map
*isl_union_map_project_out(
3454 __isl_take isl_union_map
*umap
,
3455 enum isl_dim_type type
, unsigned first
, unsigned n
)
3458 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3463 if (type
!= isl_dim_param
)
3464 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3465 "can only project out parameters",
3466 return isl_union_map_free(umap
));
3468 space
= isl_union_map_get_space(umap
);
3469 space
= isl_space_drop_dims(space
, type
, first
, n
);
3470 data
.res
= isl_union_map_empty(space
);
3471 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3472 data
.res
= isl_union_map_free(data
.res
);
3474 isl_union_map_free(umap
);
3479 /* Turn the "n" dimensions of type "type", starting at "first"
3480 * into existentially quantified variables.
3481 * Since the space of an isl_union_set only contains parameters,
3482 * "type" is required to be equal to isl_dim_param.
3484 __isl_give isl_union_set
*isl_union_set_project_out(
3485 __isl_take isl_union_set
*uset
,
3486 enum isl_dim_type type
, unsigned first
, unsigned n
)
3488 return isl_union_map_project_out(uset
, type
, first
, n
);
3491 /* Internal data structure for isl_union_map_involves_dims.
3492 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3494 struct isl_union_map_involves_dims_data
{
3499 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3501 static int map_excludes(__isl_keep isl_map
*map
, void *user
)
3503 struct isl_union_map_involves_dims_data
*data
= user
;
3506 involves
= isl_map_involves_dims(map
,
3507 isl_dim_param
, data
->first
, data
->n
);
3513 /* Does "umap" involve any of the n parameters starting at first?
3514 * "type" is required to be set to isl_dim_param.
3516 * "umap" involves any of those parameters if any of its maps
3517 * involve the parameters. In other words, "umap" does not
3518 * involve any of the parameters if all its maps to not
3519 * involve the parameters.
3521 int isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3522 enum isl_dim_type type
, unsigned first
, unsigned n
)
3524 struct isl_union_map_involves_dims_data data
= { first
, n
};
3527 if (type
!= isl_dim_param
)
3528 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3529 "can only reference parameters", return 0);
3531 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3539 /* Return the union of the elements in the list "list".
3541 __isl_give isl_union_set
*isl_union_set_list_union(
3542 __isl_take isl_union_set_list
*list
)
3552 ctx
= isl_union_set_list_get_ctx(list
);
3553 space
= isl_space_params_alloc(ctx
, 0);
3554 res
= isl_union_set_empty(space
);
3556 n
= isl_union_set_list_n_union_set(list
);
3557 for (i
= 0; i
< n
; ++i
) {
3558 isl_union_set
*uset_i
;
3560 uset_i
= isl_union_set_list_get_union_set(list
, i
);
3561 res
= isl_union_set_union(res
, uset_i
);
3564 isl_union_set_list_free(list
);