2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
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_maybe_map.h>
27 #include <isl/deprecated/union_map_int.h>
29 #include <bset_from_bmap.c>
30 #include <set_to_map.c>
31 #include <set_from_map.c>
33 /* Return the number of parameters of "umap", where "type"
34 * is required to be set to isl_dim_param.
36 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
37 enum isl_dim_type type
)
42 if (type
!= isl_dim_param
)
43 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
44 "can only reference parameters", return 0);
46 return isl_space_dim(umap
->dim
, type
);
49 /* Return the number of parameters of "uset", where "type"
50 * is required to be set to isl_dim_param.
52 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
53 enum isl_dim_type type
)
55 return isl_union_map_dim(uset
, type
);
58 /* Return the id of the specified dimension.
60 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
61 enum isl_dim_type type
, unsigned pos
)
66 if (type
!= isl_dim_param
)
67 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
68 "can only reference parameters", return NULL
);
70 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
73 /* Is this union set a parameter domain?
75 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
81 return isl_bool_error
;
82 if (uset
->table
.n
!= 1)
83 return isl_bool_false
;
85 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
86 params
= isl_set_is_params(set
);
91 static __isl_give isl_union_map
*isl_union_map_alloc(
92 __isl_take isl_space
*space
, int size
)
96 space
= isl_space_params(space
);
100 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
102 isl_space_free(space
);
108 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
109 return isl_union_map_free(umap
);
114 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*space
)
116 return isl_union_map_alloc(space
, 16);
119 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*space
)
121 return isl_union_map_empty(space
);
124 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
126 return umap
? umap
->dim
->ctx
: NULL
;
129 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
131 return uset
? uset
->dim
->ctx
: NULL
;
134 /* Return the space of "umap".
136 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
138 return umap
? umap
->dim
: NULL
;
141 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
143 return isl_space_copy(isl_union_map_peek_space(umap
));
146 /* Return the position of the parameter with the given name
148 * Return -1 if no such dimension can be found.
150 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
151 enum isl_dim_type type
, const char *name
)
155 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
158 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
160 return isl_union_map_get_space(uset
);
163 static isl_stat
free_umap_entry(void **entry
, void *user
)
165 isl_map
*map
= *entry
;
170 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
172 isl_union_map
**umap
= (isl_union_map
**)user
;
174 *umap
= isl_union_map_add_map(*umap
, map
);
179 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
186 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
187 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
191 isl_union_map_free(dup
);
195 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
203 return isl_union_map_dup(umap
);
206 struct isl_union_align
{
211 static isl_stat
align_entry(void **entry
, void *user
)
213 isl_map
*map
= *entry
;
215 struct isl_union_align
*data
= user
;
217 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
218 isl_map_get_space(map
));
220 data
->res
= isl_union_map_add_map(data
->res
,
221 isl_map_realign(isl_map_copy(map
), exp
));
226 /* Align the parameters of umap along those of model.
227 * The result has the parameters of model first, in the same order
228 * as they appear in model, followed by any remaining parameters of
229 * umap that do not appear in model.
231 __isl_give isl_union_map
*isl_union_map_align_params(
232 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
234 struct isl_union_align data
= { NULL
, NULL
};
235 isl_bool equal_params
;
240 equal_params
= isl_space_has_equal_params(umap
->dim
, model
);
241 if (equal_params
< 0)
244 isl_space_free(model
);
248 model
= isl_space_params(model
);
249 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
253 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
255 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
256 &align_entry
, &data
) < 0)
259 isl_reordering_free(data
.exp
);
260 isl_union_map_free(umap
);
261 isl_space_free(model
);
264 isl_reordering_free(data
.exp
);
265 isl_union_map_free(umap
);
266 isl_union_map_free(data
.res
);
267 isl_space_free(model
);
271 __isl_give isl_union_set
*isl_union_set_align_params(
272 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
274 return isl_union_map_align_params(uset
, model
);
277 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
278 __isl_take isl_union_map
*umap2
)
280 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
281 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
283 umap1
= isl_union_map_cow(umap1
);
285 if (!umap1
|| !umap2
)
288 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
291 isl_union_map_free(umap2
);
295 isl_union_map_free(umap1
);
296 isl_union_map_free(umap2
);
300 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
301 __isl_take isl_union_set
*uset2
)
303 return isl_union_map_union(uset1
, uset2
);
306 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
315 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
317 return isl_union_map_copy(uset
);
320 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
328 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
329 &free_umap_entry
, NULL
);
330 isl_hash_table_clear(&umap
->table
);
331 isl_space_free(umap
->dim
);
336 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
338 return isl_union_map_free(uset
);
341 /* Do "umap" and "space" have the same parameters?
343 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
344 __isl_keep isl_space
*space
)
346 isl_space
*umap_space
;
348 umap_space
= isl_union_map_peek_space(umap
);
349 return isl_space_has_equal_params(umap_space
, space
);
352 static int has_space(const void *entry
, const void *val
)
354 isl_map
*map
= (isl_map
*)entry
;
355 isl_space
*space
= (isl_space
*) val
;
357 return isl_space_is_equal(map
->dim
, space
);
360 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
361 __isl_take isl_map
*map
)
364 struct isl_hash_table_entry
*entry
;
370 if (isl_map_plain_is_empty(map
)) {
375 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
379 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
380 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
383 umap
= isl_union_map_cow(umap
);
388 hash
= isl_space_get_hash(map
->dim
);
389 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
390 &has_space
, map
->dim
, 1);
397 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
406 isl_union_map_free(umap
);
410 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
411 __isl_take isl_set
*set
)
413 return isl_union_map_add_map(uset
, set_to_map(set
));
416 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
424 dim
= isl_map_get_space(map
);
425 dim
= isl_space_params(dim
);
426 umap
= isl_union_map_empty(dim
);
427 umap
= isl_union_map_add_map(umap
, map
);
432 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
434 return isl_union_map_from_map(set_to_map(set
));
437 __isl_give isl_union_map
*isl_union_map_from_basic_map(
438 __isl_take isl_basic_map
*bmap
)
440 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
443 __isl_give isl_union_set
*isl_union_set_from_basic_set(
444 __isl_take isl_basic_set
*bset
)
446 return isl_union_map_from_basic_map(bset
);
449 struct isl_union_map_foreach_data
451 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
455 static isl_stat
call_on_copy(void **entry
, void *user
)
457 isl_map
*map
= *entry
;
458 struct isl_union_map_foreach_data
*data
;
459 data
= (struct isl_union_map_foreach_data
*)user
;
461 return data
->fn(isl_map_copy(map
), data
->user
);
464 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
466 return umap
? umap
->table
.n
: 0;
469 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
471 return uset
? uset
->table
.n
: 0;
474 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
475 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
477 struct isl_union_map_foreach_data data
= { fn
, user
};
480 return isl_stat_error
;
482 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
483 &call_on_copy
, &data
);
486 /* Internal data structure for isl_union_map_every_map.
488 * "test" is the user-specified callback function.
489 * "user" is the user-specified callback function argument.
491 * "failed" is initialized to 0 and set to 1 if "test" fails
494 struct isl_union_map_every_data
{
495 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
);
500 /* Call data->test on "map".
501 * If this fails, then set data->failed and abort.
503 static isl_stat
call_every(void **entry
, void *user
)
505 isl_map
*map
= *entry
;
506 struct isl_union_map_every_data
*data
= user
;
509 r
= data
->test(map
, data
->user
);
511 return isl_stat_error
;
515 return isl_stat_error
;
518 /* Does "test" succeed on every map in "umap"?
520 isl_bool
isl_union_map_every_map(__isl_keep isl_union_map
*umap
,
521 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
), void *user
)
523 struct isl_union_map_every_data data
= { test
, user
, 0 };
527 return isl_bool_error
;
529 r
= isl_hash_table_foreach(isl_union_map_get_ctx(umap
), &umap
->table
,
532 return isl_bool_true
;
534 return isl_bool_false
;
535 return isl_bool_error
;
538 static isl_stat
copy_map(void **entry
, void *user
)
540 isl_map
*map
= *entry
;
541 isl_map
**map_p
= user
;
543 *map_p
= isl_map_copy(map
);
545 return isl_stat_error
;
548 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
555 ctx
= isl_union_map_get_ctx(umap
);
556 if (umap
->table
.n
!= 1)
557 isl_die(ctx
, isl_error_invalid
,
558 "union map needs to contain elements in exactly "
559 "one space", goto error
);
561 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
563 isl_union_map_free(umap
);
567 isl_union_map_free(umap
);
571 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
573 return isl_map_from_union_map(uset
);
576 /* Extract the map in "umap" that lives in the given space (ignoring
579 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
580 __isl_take isl_space
*space
)
583 struct isl_hash_table_entry
*entry
;
585 space
= isl_space_drop_dims(space
, isl_dim_param
,
586 0, isl_space_dim(space
, isl_dim_param
));
587 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
591 hash
= isl_space_get_hash(space
);
592 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
593 &has_space
, space
, 0);
595 return isl_map_empty(space
);
596 isl_space_free(space
);
597 return isl_map_copy(entry
->data
);
599 isl_space_free(space
);
603 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
604 __isl_take isl_space
*dim
)
606 return set_from_map(isl_union_map_extract_map(uset
, dim
));
609 /* Check if umap contains a map in the given space.
611 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
612 __isl_keep isl_space
*space
)
615 struct isl_hash_table_entry
*entry
;
618 return isl_bool_error
;
620 hash
= isl_space_get_hash(space
);
621 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
622 &has_space
, space
, 0);
626 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
627 __isl_keep isl_space
*space
)
629 return isl_union_map_contains(uset
, space
);
632 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
633 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
635 return isl_union_map_foreach_map(uset
,
636 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
639 struct isl_union_set_foreach_point_data
{
640 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
644 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
646 struct isl_union_set_foreach_point_data
*data
= user
;
649 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
655 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
656 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
658 struct isl_union_set_foreach_point_data data
= { fn
, user
};
659 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
662 /* Data structure that specifies how gen_bin_op should
663 * construct results from the inputs.
665 * If "subtract" is set, then a map in the first input is copied to the result
666 * if there is no corresponding map in the second input.
667 * Otherwise, a map in the first input with no corresponding map
668 * in the second input is ignored.
669 * If "filter" is not NULL, then it specifies which maps in the first
670 * input may have a matching map in the second input.
671 * In particular, it makes sure that "match_space" can be called
672 * on the space of the map.
673 * "match_space" specifies how to transform the space of a map
674 * in the first input to the space of the corresponding map
675 * in the second input.
676 * "fn_map" specifies how the matching maps, one from each input,
677 * should be combined to form a map in the result.
679 struct isl_bin_op_control
{
681 isl_bool (*filter
)(__isl_keep isl_map
*map
);
682 __isl_give isl_space
*(*match_space
)(__isl_take isl_space
*space
);
683 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map1
,
684 __isl_take isl_map
*map2
);
687 /* Internal data structure for gen_bin_op.
688 * "control" specifies how the maps in the result should be constructed.
689 * "umap2" is a pointer to the second argument.
690 * "res" collects the results.
692 struct isl_union_map_gen_bin_data
{
693 struct isl_bin_op_control
*control
;
694 isl_union_map
*umap2
;
698 /* Add a copy of "map" to "res" and return the result.
700 static __isl_give isl_union_map
*bin_add_map(__isl_take isl_union_map
*res
,
701 __isl_keep isl_map
*map
)
703 return isl_union_map_add_map(res
, isl_map_copy(map
));
706 /* Combine "map1" and "map2", add the result to "res" and return the result.
707 * Check whether the result is empty before adding it to "res".
709 static __isl_give isl_union_map
*bin_add_pair(__isl_take isl_union_map
*res
,
710 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
711 struct isl_union_map_gen_bin_data
*data
)
716 map
= data
->control
->fn_map(isl_map_copy(map1
), isl_map_copy(map2
));
717 empty
= isl_map_is_empty(map
);
718 if (empty
< 0 || empty
) {
721 return isl_union_map_free(res
);
724 return isl_union_map_add_map(res
, map
);
727 /* Dummy match_space function that simply returns the input space.
729 static __isl_give isl_space
*identity(__isl_take isl_space
*space
)
734 /* Look for the map in data->umap2 that corresponds to "map", if any.
735 * Return (isl_bool_true, matching map) if there is one,
736 * (isl_bool_false, NULL) if there is no matching map and
737 * (isl_bool_error, NULL) on error.
739 * If not NULL, then data->control->filter specifies whether "map"
740 * can have any matching map. If so,
741 * data->control->match_space specifies which map in data->umap2
742 * corresponds to "map".
744 static __isl_keep isl_maybe_isl_map
bin_try_get_match(
745 struct isl_union_map_gen_bin_data
*data
, __isl_keep isl_map
*map
)
748 struct isl_hash_table_entry
*entry2
;
750 isl_maybe_isl_map res
= { isl_bool_error
, NULL
};
752 if (data
->control
->filter
) {
753 res
.valid
= data
->control
->filter(map
);
754 if (res
.valid
< 0 || !res
.valid
)
756 res
.valid
= isl_bool_error
;
759 space
= isl_map_get_space(map
);
760 if (data
->control
->match_space
!= &identity
)
761 space
= data
->control
->match_space(space
);
764 hash
= isl_space_get_hash(space
);
765 entry2
= isl_hash_table_find(isl_union_map_get_ctx(data
->umap2
),
766 &data
->umap2
->table
, hash
,
767 &has_space
, space
, 0);
768 isl_space_free(space
);
769 res
.valid
= entry2
!= NULL
;
771 res
.value
= entry2
->data
;
776 /* isl_hash_table_foreach callback for gen_bin_op.
777 * Look for the map in data->umap2 that corresponds
778 * to the map that "entry" points to, apply the binary operation and
779 * add the result to data->res.
781 * If no corresponding map can be found, then the effect depends
782 * on data->control->subtract. If it is set, then the current map
783 * is added directly to the result. Otherwise, it is ignored.
785 static isl_stat
gen_bin_entry(void **entry
, void *user
)
787 struct isl_union_map_gen_bin_data
*data
= user
;
788 isl_map
*map
= *entry
;
791 m
= bin_try_get_match(data
, map
);
793 return isl_stat_error
;
794 if (!m
.valid
&& !data
->control
->subtract
)
798 data
->res
= bin_add_map(data
->res
, map
);
800 data
->res
= bin_add_pair(data
->res
, map
, m
.value
, data
);
802 return isl_stat_error
;
807 /* Apply a binary operation to "umap1" and "umap2" based on "control".
808 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
811 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
812 __isl_take isl_union_map
*umap2
, struct isl_bin_op_control
*control
)
814 struct isl_union_map_gen_bin_data data
= { control
, NULL
, NULL
};
816 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
817 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
819 if (!umap1
|| !umap2
)
823 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
825 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
826 &gen_bin_entry
, &data
) < 0)
829 isl_union_map_free(umap1
);
830 isl_union_map_free(umap2
);
833 isl_union_map_free(umap1
);
834 isl_union_map_free(umap2
);
835 isl_union_map_free(data
.res
);
839 __isl_give isl_union_map
*isl_union_map_subtract(
840 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
842 struct isl_bin_op_control control
= {
844 .match_space
= &identity
,
845 .fn_map
= &isl_map_subtract
,
848 return gen_bin_op(umap1
, umap2
, &control
);
851 __isl_give isl_union_set
*isl_union_set_subtract(
852 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
854 return isl_union_map_subtract(uset1
, uset2
);
857 struct isl_union_map_gen_bin_set_data
{
862 static isl_stat
intersect_params_entry(void **entry
, void *user
)
864 struct isl_union_map_gen_bin_set_data
*data
= user
;
865 isl_map
*map
= *entry
;
868 map
= isl_map_copy(map
);
869 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
871 empty
= isl_map_is_empty(map
);
874 return isl_stat_error
;
877 data
->res
= isl_union_map_add_map(data
->res
, map
);
882 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
883 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
885 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
887 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
888 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
894 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
896 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
900 isl_union_map_free(umap
);
904 isl_union_map_free(umap
);
906 isl_union_map_free(data
.res
);
910 /* Intersect "umap" with the parameter domain "set".
912 * If "set" does not have any constraints, then we can return immediately.
914 __isl_give isl_union_map
*isl_union_map_intersect_params(
915 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
919 is_universe
= isl_set_plain_is_universe(set
);
927 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
929 isl_union_map_free(umap
);
934 __isl_give isl_union_set
*isl_union_set_intersect_params(
935 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
937 return isl_union_map_intersect_params(uset
, set
);
940 static __isl_give isl_union_map
*union_map_intersect_params(
941 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
943 return isl_union_map_intersect_params(umap
,
944 isl_set_from_union_set(uset
));
947 static __isl_give isl_union_map
*union_map_gist_params(
948 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
950 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
953 struct isl_union_map_match_bin_data
{
954 isl_union_map
*umap2
;
956 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
959 static isl_stat
match_bin_entry(void **entry
, void *user
)
961 struct isl_union_map_match_bin_data
*data
= user
;
963 struct isl_hash_table_entry
*entry2
;
964 isl_map
*map
= *entry
;
967 hash
= isl_space_get_hash(map
->dim
);
968 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
969 hash
, &has_space
, map
->dim
, 0);
973 map
= isl_map_copy(map
);
974 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
976 empty
= isl_map_is_empty(map
);
979 return isl_stat_error
;
986 data
->res
= isl_union_map_add_map(data
->res
, map
);
991 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
992 __isl_take isl_union_map
*umap2
,
993 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
995 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
997 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
998 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1000 if (!umap1
|| !umap2
)
1004 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1006 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1007 &match_bin_entry
, &data
) < 0)
1010 isl_union_map_free(umap1
);
1011 isl_union_map_free(umap2
);
1014 isl_union_map_free(umap1
);
1015 isl_union_map_free(umap2
);
1016 isl_union_map_free(data
.res
);
1020 __isl_give isl_union_map
*isl_union_map_intersect(
1021 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1023 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
1026 /* Compute the intersection of the two union_sets.
1027 * As a special case, if exactly one of the two union_sets
1028 * is a parameter domain, then intersect the parameter domain
1029 * of the other one with this set.
1031 __isl_give isl_union_set
*isl_union_set_intersect(
1032 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1036 p1
= isl_union_set_is_params(uset1
);
1037 p2
= isl_union_set_is_params(uset2
);
1038 if (p1
< 0 || p2
< 0)
1041 return union_map_intersect_params(uset1
, uset2
);
1043 return union_map_intersect_params(uset2
, uset1
);
1044 return isl_union_map_intersect(uset1
, uset2
);
1046 isl_union_set_free(uset1
);
1047 isl_union_set_free(uset2
);
1051 static isl_stat
gist_params_entry(void **entry
, void *user
)
1053 struct isl_union_map_gen_bin_set_data
*data
= user
;
1054 isl_map
*map
= *entry
;
1057 map
= isl_map_copy(map
);
1058 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
1060 empty
= isl_map_is_empty(map
);
1063 return isl_stat_error
;
1066 data
->res
= isl_union_map_add_map(data
->res
, map
);
1071 __isl_give isl_union_map
*isl_union_map_gist_params(
1072 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1074 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
1077 __isl_give isl_union_set
*isl_union_set_gist_params(
1078 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1080 return isl_union_map_gist_params(uset
, set
);
1083 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
1084 __isl_take isl_union_map
*context
)
1086 return match_bin_op(umap
, context
, &isl_map_gist
);
1089 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
1090 __isl_take isl_union_set
*context
)
1092 if (isl_union_set_is_params(context
))
1093 return union_map_gist_params(uset
, context
);
1094 return isl_union_map_gist(uset
, context
);
1097 /* For each map in "umap", remove the constraints in the corresponding map
1099 * Each map in "context" is assumed to consist of a single disjunct and
1100 * to have explicit representations for all local variables.
1102 __isl_give isl_union_map
*isl_union_map_plain_gist(
1103 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*context
)
1105 return match_bin_op(umap
, context
, &isl_map_plain_gist
);
1108 /* For each set in "uset", remove the constraints in the corresponding set
1110 * Each set in "context" is assumed to consist of a single disjunct and
1111 * to have explicit representations for all local variables.
1113 __isl_give isl_union_set
*isl_union_set_plain_gist(
1114 __isl_take isl_union_set
*uset
, __isl_take isl_union_set
*context
)
1116 return isl_union_map_plain_gist(uset
, context
);
1119 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
1120 __isl_take isl_map
*set2
)
1122 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
1125 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
1126 __isl_take isl_map
*set2
)
1128 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
1131 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
1132 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1134 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
1137 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
1138 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1140 return match_bin_op(uset1
, uset2
, &lex_le_set
);
1143 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
1144 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1146 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
1149 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
1150 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1152 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
1155 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
1156 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1158 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
1161 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
1162 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1164 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
1167 /* Intersect the domain of "umap" with "uset".
1169 static __isl_give isl_union_map
*union_map_intersect_domain(
1170 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1172 struct isl_bin_op_control control
= {
1173 .match_space
= &isl_space_domain
,
1174 .fn_map
= &isl_map_intersect_domain
,
1177 return gen_bin_op(umap
, uset
, &control
);
1180 /* Intersect the domain of "umap" with "uset".
1181 * If "uset" is a parameters domain, then intersect the parameter
1182 * domain of "umap" with this set.
1184 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1185 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1187 if (isl_union_set_is_params(uset
))
1188 return union_map_intersect_params(umap
, uset
);
1190 return union_map_intersect_domain(umap
, uset
);
1193 /* Remove the elements of "uset" from the domain of "umap".
1195 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1196 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1198 struct isl_bin_op_control control
= {
1200 .match_space
= &isl_space_domain
,
1201 .fn_map
= &isl_map_subtract_domain
,
1204 return gen_bin_op(umap
, dom
, &control
);
1207 /* Remove the elements of "uset" from the range of "umap".
1209 __isl_give isl_union_map
*isl_union_map_subtract_range(
1210 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1212 struct isl_bin_op_control control
= {
1214 .match_space
= &isl_space_range
,
1215 .fn_map
= &isl_map_subtract_range
,
1218 return gen_bin_op(umap
, dom
, &control
);
1221 /* Compute the gist of "umap" with respect to the domain "uset".
1223 static __isl_give isl_union_map
*union_map_gist_domain(
1224 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1226 struct isl_bin_op_control control
= {
1227 .match_space
= &isl_space_domain
,
1228 .fn_map
= &isl_map_gist_domain
,
1231 return gen_bin_op(umap
, uset
, &control
);
1234 /* Compute the gist of "umap" with respect to the domain "uset".
1235 * If "uset" is a parameters domain, then compute the gist
1236 * with respect to this parameter domain.
1238 __isl_give isl_union_map
*isl_union_map_gist_domain(
1239 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1241 if (isl_union_set_is_params(uset
))
1242 return union_map_gist_params(umap
, uset
);
1244 return union_map_gist_domain(umap
, uset
);
1247 /* Compute the gist of "umap" with respect to the range "uset".
1249 __isl_give isl_union_map
*isl_union_map_gist_range(
1250 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1252 struct isl_bin_op_control control
= {
1253 .match_space
= &isl_space_range
,
1254 .fn_map
= &isl_map_gist_range
,
1257 return gen_bin_op(umap
, uset
, &control
);
1260 __isl_give isl_union_map
*isl_union_map_intersect_range(
1261 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1263 struct isl_bin_op_control control
= {
1264 .match_space
= &isl_space_range
,
1265 .fn_map
= &isl_map_intersect_range
,
1268 return gen_bin_op(umap
, uset
, &control
);
1271 /* Intersect each map in "umap" in a space A -> [B -> C]
1272 * with the corresponding map in "factor" in the space A -> C and
1273 * collect the results.
1275 __isl_give isl_union_map
*isl_union_map_intersect_range_factor_range(
1276 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1278 struct isl_bin_op_control control
= {
1279 .filter
= &isl_map_range_is_wrapping
,
1280 .match_space
= &isl_space_range_factor_range
,
1281 .fn_map
= &isl_map_intersect_range_factor_range
,
1284 return gen_bin_op(umap
, factor
, &control
);
1287 struct isl_union_map_bin_data
{
1288 isl_union_map
*umap2
;
1291 isl_stat (*fn
)(void **entry
, void *user
);
1294 static isl_stat
apply_range_entry(void **entry
, void *user
)
1296 struct isl_union_map_bin_data
*data
= user
;
1297 isl_map
*map2
= *entry
;
1300 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1301 map2
->dim
, isl_dim_in
))
1304 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1306 empty
= isl_map_is_empty(map2
);
1309 return isl_stat_error
;
1316 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1321 static isl_stat
bin_entry(void **entry
, void *user
)
1323 struct isl_union_map_bin_data
*data
= user
;
1324 isl_map
*map
= *entry
;
1327 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1328 data
->fn
, data
) < 0)
1329 return isl_stat_error
;
1334 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1335 __isl_take isl_union_map
*umap2
,
1336 isl_stat (*fn
)(void **entry
, void *user
))
1338 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1340 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1341 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1343 if (!umap1
|| !umap2
)
1347 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1349 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1350 &bin_entry
, &data
) < 0)
1353 isl_union_map_free(umap1
);
1354 isl_union_map_free(umap2
);
1357 isl_union_map_free(umap1
);
1358 isl_union_map_free(umap2
);
1359 isl_union_map_free(data
.res
);
1363 __isl_give isl_union_map
*isl_union_map_apply_range(
1364 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1366 return bin_op(umap1
, umap2
, &apply_range_entry
);
1369 __isl_give isl_union_map
*isl_union_map_apply_domain(
1370 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1372 umap1
= isl_union_map_reverse(umap1
);
1373 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1374 return isl_union_map_reverse(umap1
);
1377 __isl_give isl_union_set
*isl_union_set_apply(
1378 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1380 return isl_union_map_apply_range(uset
, umap
);
1383 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1385 struct isl_union_map_bin_data
*data
= user
;
1386 isl_map
*map2
= *entry
;
1388 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1389 map2
->dim
, isl_dim_out
))
1392 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1394 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1399 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1400 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1402 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1405 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1407 struct isl_union_map_bin_data
*data
= user
;
1408 isl_map
*map2
= *entry
;
1410 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1411 map2
->dim
, isl_dim_out
))
1414 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1416 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1421 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1422 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1424 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1427 static isl_stat
product_entry(void **entry
, void *user
)
1429 struct isl_union_map_bin_data
*data
= user
;
1430 isl_map
*map2
= *entry
;
1432 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1434 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1439 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1440 __isl_take isl_union_map
*umap2
)
1442 return bin_op(umap1
, umap2
, &product_entry
);
1445 static isl_stat
set_product_entry(void **entry
, void *user
)
1447 struct isl_union_map_bin_data
*data
= user
;
1448 isl_set
*set2
= *entry
;
1450 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1452 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1457 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1458 __isl_take isl_union_set
*uset2
)
1460 return bin_op(uset1
, uset2
, &set_product_entry
);
1463 static isl_stat
domain_product_entry(void **entry
, void *user
)
1465 struct isl_union_map_bin_data
*data
= user
;
1466 isl_map
*map2
= *entry
;
1468 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1469 map2
->dim
, isl_dim_out
))
1472 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1473 isl_map_copy(map2
));
1475 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1480 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1482 __isl_give isl_union_map
*isl_union_map_domain_product(
1483 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1485 return bin_op(umap1
, umap2
, &domain_product_entry
);
1488 static isl_stat
range_product_entry(void **entry
, void *user
)
1490 struct isl_union_map_bin_data
*data
= user
;
1491 isl_map
*map2
= *entry
;
1493 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1494 map2
->dim
, isl_dim_in
))
1497 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1498 isl_map_copy(map2
));
1500 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1505 __isl_give isl_union_map
*isl_union_map_range_product(
1506 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1508 return bin_op(umap1
, umap2
, &range_product_entry
);
1511 /* If data->map A -> B and "map2" C -> D have the same range space,
1512 * then add (A, C) -> (B * D) to data->res.
1514 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1516 struct isl_union_map_bin_data
*data
= user
;
1517 isl_map
*map2
= *entry
;
1519 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1520 map2
->dim
, isl_dim_out
))
1523 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1524 isl_map_copy(map2
));
1526 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1531 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1533 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1534 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1536 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1539 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1541 struct isl_union_map_bin_data
*data
= user
;
1542 isl_map
*map2
= *entry
;
1544 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1545 map2
->dim
, isl_dim_in
))
1548 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1549 isl_map_copy(map2
));
1551 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1556 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1557 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1559 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1562 /* Data structure that specifies how un_op should modify
1563 * the maps in the union map.
1565 * If "inplace" is set, then the maps in the input union map
1566 * are modified in place. This means that "fn_map" should not
1567 * change the meaning of the map or that the union map only
1568 * has a single reference.
1569 * If "total" is set, then all maps need to be modified and
1570 * the results need to live in the same space.
1571 * Otherwise, a new union map is constructed to store the results.
1572 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1573 * are taken into account. No filter can be set if "inplace" or
1575 * "fn_map" specifies how the maps (selected by "filter")
1576 * should be transformed.
1578 struct isl_un_op_control
{
1581 isl_bool (*filter
)(__isl_keep isl_map
*map
);
1582 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map
);
1585 /* Internal data structure for "un_op".
1586 * "control" specifies how the maps in the union map should be modified.
1587 * "res" collects the results.
1589 struct isl_union_map_un_data
{
1590 struct isl_un_op_control
*control
;
1594 /* isl_hash_table_foreach callback for un_op.
1595 * Handle the map that "entry" points to.
1597 * If control->filter is set, then check if this map satisfies the filter.
1598 * If so (or if control->filter is not set), modify the map
1599 * by calling control->fn_map and either add the result to data->res or
1600 * replace the original entry by the result (if control->inplace is set).
1602 static isl_stat
un_entry(void **entry
, void *user
)
1604 struct isl_union_map_un_data
*data
= user
;
1605 isl_map
*map
= *entry
;
1607 if (data
->control
->filter
) {
1610 ok
= data
->control
->filter(map
);
1612 return isl_stat_error
;
1617 map
= data
->control
->fn_map(isl_map_copy(map
));
1619 return isl_stat_error
;
1620 if (data
->control
->inplace
) {
1621 isl_map_free(*entry
);
1624 data
->res
= isl_union_map_add_map(data
->res
, map
);
1626 return isl_stat_error
;
1632 /* Modify the maps in "umap" based on "control".
1633 * If control->inplace is set, then modify the maps in "umap" in-place.
1634 * Otherwise, create a new union map to hold the results.
1635 * If control->total is set, then perform an inplace computation
1636 * if "umap" is only referenced once. Otherwise, create a new union map
1637 * to store the results.
1639 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1640 struct isl_un_op_control
*control
)
1642 struct isl_union_map_un_data data
= { control
};
1646 if ((control
->inplace
|| control
->total
) && control
->filter
)
1647 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
1648 "inplace/total modification cannot be filtered",
1649 return isl_union_map_free(umap
));
1651 if (control
->total
&& umap
->ref
== 1)
1652 control
->inplace
= 1;
1653 if (control
->inplace
) {
1658 space
= isl_union_map_get_space(umap
);
1659 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
1661 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1662 &umap
->table
, &un_entry
, &data
) < 0)
1663 data
.res
= isl_union_map_free(data
.res
);
1665 if (control
->inplace
)
1667 isl_union_map_free(umap
);
1671 __isl_give isl_union_map
*isl_union_map_from_range(
1672 __isl_take isl_union_set
*uset
)
1674 struct isl_un_op_control control
= {
1675 .fn_map
= &isl_map_from_range
,
1677 return un_op(uset
, &control
);
1680 __isl_give isl_union_map
*isl_union_map_from_domain(
1681 __isl_take isl_union_set
*uset
)
1683 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1686 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1687 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1689 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1690 isl_union_map_from_range(range
));
1693 /* Modify the maps in "umap" by applying "fn" on them.
1694 * "fn" should apply to all maps in "umap" and should not modify the space.
1696 static __isl_give isl_union_map
*total(__isl_take isl_union_map
*umap
,
1697 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1699 struct isl_un_op_control control
= {
1704 return un_op(umap
, &control
);
1707 /* Compute the affine hull of "map" and return the result as an isl_map.
1709 static __isl_give isl_map
*isl_map_affine_hull_map(__isl_take isl_map
*map
)
1711 return isl_map_from_basic_map(isl_map_affine_hull(map
));
1714 __isl_give isl_union_map
*isl_union_map_affine_hull(
1715 __isl_take isl_union_map
*umap
)
1717 return total(umap
, &isl_map_affine_hull_map
);
1720 __isl_give isl_union_set
*isl_union_set_affine_hull(
1721 __isl_take isl_union_set
*uset
)
1723 return isl_union_map_affine_hull(uset
);
1726 /* Wrapper around isl_set_combined_lineality_space
1727 * that returns the combined lineality space in the form of an isl_set
1728 * instead of an isl_basic_set.
1730 static __isl_give isl_set
*combined_lineality_space(__isl_take isl_set
*set
)
1732 return isl_set_from_basic_set(isl_set_combined_lineality_space(set
));
1735 /* For each set in "uset", compute the (linear) hull
1736 * of the lineality spaces of its basic sets and
1737 * collect and return the results.
1739 __isl_give isl_union_set
*isl_union_set_combined_lineality_space(
1740 __isl_take isl_union_set
*uset
)
1742 struct isl_un_op_control control
= {
1743 .fn_map
= &combined_lineality_space
,
1745 return un_op(uset
, &control
);
1748 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1750 static __isl_give isl_map
*isl_map_polyhedral_hull_map(__isl_take isl_map
*map
)
1752 return isl_map_from_basic_map(isl_map_polyhedral_hull(map
));
1755 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1756 __isl_take isl_union_map
*umap
)
1758 return total(umap
, &isl_map_polyhedral_hull_map
);
1761 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1762 __isl_take isl_union_set
*uset
)
1764 return isl_union_map_polyhedral_hull(uset
);
1767 /* Compute a superset of the convex hull of "map" that is described
1768 * by only translates of the constraints in the constituents of "map" and
1769 * return the result as an isl_map.
1771 static __isl_give isl_map
*isl_map_simple_hull_map(__isl_take isl_map
*map
)
1773 return isl_map_from_basic_map(isl_map_simple_hull(map
));
1776 __isl_give isl_union_map
*isl_union_map_simple_hull(
1777 __isl_take isl_union_map
*umap
)
1779 return total(umap
, &isl_map_simple_hull_map
);
1782 __isl_give isl_union_set
*isl_union_set_simple_hull(
1783 __isl_take isl_union_set
*uset
)
1785 return isl_union_map_simple_hull(uset
);
1788 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1789 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1791 struct isl_un_op_control control
= {
1796 return un_op(umap
, &control
);
1799 /* Remove redundant constraints in each of the basic maps of "umap".
1800 * Since removing redundant constraints does not change the meaning
1801 * or the space, the operation can be performed in-place.
1803 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1804 __isl_take isl_union_map
*umap
)
1806 return inplace(umap
, &isl_map_remove_redundancies
);
1809 /* Remove redundant constraints in each of the basic sets of "uset".
1811 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1812 __isl_take isl_union_set
*uset
)
1814 return isl_union_map_remove_redundancies(uset
);
1817 __isl_give isl_union_map
*isl_union_map_coalesce(
1818 __isl_take isl_union_map
*umap
)
1820 return inplace(umap
, &isl_map_coalesce
);
1823 __isl_give isl_union_set
*isl_union_set_coalesce(
1824 __isl_take isl_union_set
*uset
)
1826 return isl_union_map_coalesce(uset
);
1829 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1830 __isl_take isl_union_map
*umap
)
1832 return inplace(umap
, &isl_map_detect_equalities
);
1835 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1836 __isl_take isl_union_set
*uset
)
1838 return isl_union_map_detect_equalities(uset
);
1841 __isl_give isl_union_map
*isl_union_map_compute_divs(
1842 __isl_take isl_union_map
*umap
)
1844 return inplace(umap
, &isl_map_compute_divs
);
1847 __isl_give isl_union_set
*isl_union_set_compute_divs(
1848 __isl_take isl_union_set
*uset
)
1850 return isl_union_map_compute_divs(uset
);
1853 __isl_give isl_union_map
*isl_union_map_lexmin(
1854 __isl_take isl_union_map
*umap
)
1856 return total(umap
, &isl_map_lexmin
);
1859 __isl_give isl_union_set
*isl_union_set_lexmin(
1860 __isl_take isl_union_set
*uset
)
1862 return isl_union_map_lexmin(uset
);
1865 __isl_give isl_union_map
*isl_union_map_lexmax(
1866 __isl_take isl_union_map
*umap
)
1868 return total(umap
, &isl_map_lexmax
);
1871 __isl_give isl_union_set
*isl_union_set_lexmax(
1872 __isl_take isl_union_set
*uset
)
1874 return isl_union_map_lexmax(uset
);
1877 /* Return the universe in the space of "map".
1879 static __isl_give isl_map
*universe(__isl_take isl_map
*map
)
1883 space
= isl_map_get_space(map
);
1885 return isl_map_universe(space
);
1888 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1890 struct isl_un_op_control control
= {
1891 .fn_map
= &universe
,
1893 return un_op(umap
, &control
);
1896 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1898 return isl_union_map_universe(uset
);
1901 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1903 struct isl_un_op_control control
= {
1904 .fn_map
= &isl_map_reverse
,
1906 return un_op(umap
, &control
);
1909 /* Compute the parameter domain of the given union map.
1911 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1913 struct isl_un_op_control control
= {
1914 .fn_map
= &isl_map_params
,
1918 empty
= isl_union_map_is_empty(umap
);
1923 space
= isl_union_map_get_space(umap
);
1924 isl_union_map_free(umap
);
1925 return isl_set_empty(space
);
1927 return isl_set_from_union_set(un_op(umap
, &control
));
1929 isl_union_map_free(umap
);
1933 /* Compute the parameter domain of the given union set.
1935 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1937 return isl_union_map_params(uset
);
1940 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1942 struct isl_un_op_control control
= {
1943 .fn_map
= &isl_map_domain
,
1945 return un_op(umap
, &control
);
1948 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1950 struct isl_un_op_control control
= {
1951 .fn_map
= &isl_map_range
,
1953 return un_op(umap
, &control
);
1956 __isl_give isl_union_map
*isl_union_map_domain_map(
1957 __isl_take isl_union_map
*umap
)
1959 struct isl_un_op_control control
= {
1960 .fn_map
= &isl_map_domain_map
,
1962 return un_op(umap
, &control
);
1965 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1966 * add the result to "res".
1968 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
1970 isl_union_pw_multi_aff
**res
= user
;
1972 isl_pw_multi_aff
*pma
;
1974 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
1975 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
1976 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1978 return *res
? isl_stat_ok
: isl_stat_error
;
1982 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1985 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
1986 __isl_take isl_union_map
*umap
)
1988 isl_union_pw_multi_aff
*res
;
1990 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
1991 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
1992 res
= isl_union_pw_multi_aff_free(res
);
1994 isl_union_map_free(umap
);
1998 __isl_give isl_union_map
*isl_union_map_range_map(
1999 __isl_take isl_union_map
*umap
)
2001 struct isl_un_op_control control
= {
2002 .fn_map
= &isl_map_range_map
,
2004 return un_op(umap
, &control
);
2007 /* Given a collection of wrapped maps of the form A[B -> C],
2008 * return the collection of maps A[B -> C] -> B.
2010 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
2011 __isl_take isl_union_set
*uset
)
2013 struct isl_un_op_control control
= {
2014 .filter
= &isl_set_is_wrapping
,
2015 .fn_map
= &isl_set_wrapped_domain_map
,
2017 return un_op(uset
, &control
);
2020 /* Does "map" relate elements from the same space?
2022 static isl_bool
equal_tuples(__isl_keep isl_map
*map
)
2024 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2025 map
->dim
, isl_dim_out
);
2028 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
2030 struct isl_un_op_control control
= {
2031 .filter
= &equal_tuples
,
2032 .fn_map
= &isl_map_deltas
,
2034 return un_op(umap
, &control
);
2037 __isl_give isl_union_map
*isl_union_map_deltas_map(
2038 __isl_take isl_union_map
*umap
)
2040 struct isl_un_op_control control
= {
2041 .filter
= &equal_tuples
,
2042 .fn_map
= &isl_map_deltas_map
,
2044 return un_op(umap
, &control
);
2047 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
2049 struct isl_un_op_control control
= {
2050 .fn_map
= &isl_set_identity
,
2052 return un_op(uset
, &control
);
2055 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2057 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
2059 isl_union_pw_multi_aff
**res
= user
;
2061 isl_pw_multi_aff
*pma
;
2063 space
= isl_space_map_from_set(isl_set_get_space(set
));
2064 pma
= isl_pw_multi_aff_identity(space
);
2065 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
2066 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2068 return *res
? isl_stat_ok
: isl_stat_error
;
2071 /* Return an identity function on "uset" in the form
2072 * of an isl_union_pw_multi_aff.
2074 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
2075 __isl_take isl_union_set
*uset
)
2077 isl_union_pw_multi_aff
*res
;
2079 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
2080 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
2081 res
= isl_union_pw_multi_aff_free(res
);
2083 isl_union_set_free(uset
);
2087 /* For each map in "umap" of the form [A -> B] -> C,
2088 * construct the map A -> C and collect the results.
2090 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2091 __isl_take isl_union_map
*umap
)
2093 struct isl_un_op_control control
= {
2094 .filter
= &isl_map_domain_is_wrapping
,
2095 .fn_map
= &isl_map_domain_factor_domain
,
2097 return un_op(umap
, &control
);
2100 /* For each map in "umap" of the form [A -> B] -> C,
2101 * construct the map B -> C and collect the results.
2103 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2104 __isl_take isl_union_map
*umap
)
2106 struct isl_un_op_control control
= {
2107 .filter
= &isl_map_domain_is_wrapping
,
2108 .fn_map
= &isl_map_domain_factor_range
,
2110 return un_op(umap
, &control
);
2113 /* For each map in "umap" of the form A -> [B -> C],
2114 * construct the map A -> B and collect the results.
2116 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2117 __isl_take isl_union_map
*umap
)
2119 struct isl_un_op_control control
= {
2120 .filter
= &isl_map_range_is_wrapping
,
2121 .fn_map
= &isl_map_range_factor_domain
,
2123 return un_op(umap
, &control
);
2126 /* For each map in "umap" of the form A -> [B -> C],
2127 * construct the map A -> C and collect the results.
2129 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2130 __isl_take isl_union_map
*umap
)
2132 struct isl_un_op_control control
= {
2133 .filter
= &isl_map_range_is_wrapping
,
2134 .fn_map
= &isl_map_range_factor_range
,
2136 return un_op(umap
, &control
);
2139 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2140 * construct the map A -> C and collect the results.
2142 __isl_give isl_union_map
*isl_union_map_factor_domain(
2143 __isl_take isl_union_map
*umap
)
2145 struct isl_un_op_control control
= {
2146 .filter
= &isl_map_is_product
,
2147 .fn_map
= &isl_map_factor_domain
,
2149 return un_op(umap
, &control
);
2152 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2153 * construct the map B -> D and collect the results.
2155 __isl_give isl_union_map
*isl_union_map_factor_range(
2156 __isl_take isl_union_map
*umap
)
2158 struct isl_un_op_control control
= {
2159 .filter
= &isl_map_is_product
,
2160 .fn_map
= &isl_map_factor_range
,
2162 return un_op(umap
, &control
);
2165 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2167 struct isl_un_op_control control
= {
2168 .filter
= &isl_set_is_wrapping
,
2169 .fn_map
= &isl_set_unwrap
,
2171 return un_op(uset
, &control
);
2174 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2176 struct isl_un_op_control control
= {
2177 .fn_map
= &isl_map_wrap
,
2179 return un_op(umap
, &control
);
2182 struct isl_union_map_is_subset_data
{
2183 isl_union_map
*umap2
;
2187 static isl_stat
is_subset_entry(void **entry
, void *user
)
2189 struct isl_union_map_is_subset_data
*data
= user
;
2191 struct isl_hash_table_entry
*entry2
;
2192 isl_map
*map
= *entry
;
2194 hash
= isl_space_get_hash(map
->dim
);
2195 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2196 hash
, &has_space
, map
->dim
, 0);
2198 int empty
= isl_map_is_empty(map
);
2200 return isl_stat_error
;
2203 data
->is_subset
= 0;
2204 return isl_stat_error
;
2207 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2208 if (data
->is_subset
< 0 || !data
->is_subset
)
2209 return isl_stat_error
;
2214 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2215 __isl_keep isl_union_map
*umap2
)
2217 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2219 umap1
= isl_union_map_copy(umap1
);
2220 umap2
= isl_union_map_copy(umap2
);
2221 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2222 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2224 if (!umap1
|| !umap2
)
2228 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2229 &is_subset_entry
, &data
) < 0 &&
2233 isl_union_map_free(umap1
);
2234 isl_union_map_free(umap2
);
2236 return data
.is_subset
;
2238 isl_union_map_free(umap1
);
2239 isl_union_map_free(umap2
);
2240 return isl_bool_error
;
2243 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2244 __isl_keep isl_union_set
*uset2
)
2246 return isl_union_map_is_subset(uset1
, uset2
);
2249 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2250 __isl_keep isl_union_map
*umap2
)
2254 if (!umap1
|| !umap2
)
2255 return isl_bool_error
;
2256 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2257 if (is_subset
!= isl_bool_true
)
2259 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2263 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2264 __isl_keep isl_union_set
*uset2
)
2266 return isl_union_map_is_equal(uset1
, uset2
);
2269 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2270 __isl_keep isl_union_map
*umap2
)
2274 if (!umap1
|| !umap2
)
2275 return isl_bool_error
;
2276 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2277 if (is_subset
!= isl_bool_true
)
2279 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2280 if (is_subset
== isl_bool_error
)
2285 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2286 __isl_keep isl_union_set
*uset2
)
2288 return isl_union_map_is_strict_subset(uset1
, uset2
);
2291 /* Internal data structure for isl_union_map_is_disjoint.
2292 * umap2 is the union map with which we are comparing.
2293 * is_disjoint is initialized to 1 and is set to 0 as soon
2294 * as the union maps turn out not to be disjoint.
2296 struct isl_union_map_is_disjoint_data
{
2297 isl_union_map
*umap2
;
2298 isl_bool is_disjoint
;
2301 /* Check if "map" is disjoint from data->umap2 and abort
2302 * the search if it is not.
2304 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2306 struct isl_union_map_is_disjoint_data
*data
= user
;
2308 struct isl_hash_table_entry
*entry2
;
2309 isl_map
*map
= *entry
;
2311 hash
= isl_space_get_hash(map
->dim
);
2312 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2313 hash
, &has_space
, map
->dim
, 0);
2317 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2318 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2319 return isl_stat_error
;
2324 /* Are "umap1" and "umap2" disjoint?
2326 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2327 __isl_keep isl_union_map
*umap2
)
2329 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2331 umap1
= isl_union_map_copy(umap1
);
2332 umap2
= isl_union_map_copy(umap2
);
2333 umap1
= isl_union_map_align_params(umap1
,
2334 isl_union_map_get_space(umap2
));
2335 umap2
= isl_union_map_align_params(umap2
,
2336 isl_union_map_get_space(umap1
));
2338 if (!umap1
|| !umap2
)
2342 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2343 &is_disjoint_entry
, &data
) < 0 &&
2347 isl_union_map_free(umap1
);
2348 isl_union_map_free(umap2
);
2350 return data
.is_disjoint
;
2352 isl_union_map_free(umap1
);
2353 isl_union_map_free(umap2
);
2354 return isl_bool_error
;
2357 /* Are "uset1" and "uset2" disjoint?
2359 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2360 __isl_keep isl_union_set
*uset2
)
2362 return isl_union_map_is_disjoint(uset1
, uset2
);
2365 static isl_stat
sample_entry(void **entry
, void *user
)
2367 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2368 isl_map
*map
= *entry
;
2370 *sample
= isl_map_sample(isl_map_copy(map
));
2372 return isl_stat_error
;
2373 if (!isl_basic_map_plain_is_empty(*sample
))
2374 return isl_stat_error
;
2378 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2380 isl_basic_map
*sample
= NULL
;
2385 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2386 &sample_entry
, &sample
) < 0 &&
2391 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2393 isl_union_map_free(umap
);
2397 isl_union_map_free(umap
);
2401 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2403 return bset_from_bmap(isl_union_map_sample(uset
));
2406 /* Return an element in "uset" in the form of an isl_point.
2407 * Return a void isl_point if "uset" is empty.
2409 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2411 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2414 struct isl_forall_data
{
2416 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2419 static isl_stat
forall_entry(void **entry
, void *user
)
2421 struct isl_forall_data
*data
= user
;
2422 isl_map
*map
= *entry
;
2424 data
->res
= data
->fn(map
);
2426 return isl_stat_error
;
2429 return isl_stat_error
;
2434 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2435 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2437 struct isl_forall_data data
= { isl_bool_true
, fn
};
2440 return isl_bool_error
;
2442 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2443 &forall_entry
, &data
) < 0 && data
.res
)
2444 return isl_bool_error
;
2449 struct isl_forall_user_data
{
2451 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2455 static isl_stat
forall_user_entry(void **entry
, void *user
)
2457 struct isl_forall_user_data
*data
= user
;
2458 isl_map
*map
= *entry
;
2460 data
->res
= data
->fn(map
, data
->user
);
2462 return isl_stat_error
;
2465 return isl_stat_error
;
2470 /* Check if fn(map, user) returns true for all maps "map" in umap.
2472 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2473 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2475 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2478 return isl_bool_error
;
2480 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2481 &forall_user_entry
, &data
) < 0 && data
.res
)
2482 return isl_bool_error
;
2487 /* Is "umap" obviously empty?
2489 isl_bool
isl_union_map_plain_is_empty(__isl_keep isl_union_map
*umap
)
2492 return isl_bool_error
;
2493 return isl_union_map_n_map(umap
) == 0;
2496 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2498 return union_map_forall(umap
, &isl_map_is_empty
);
2501 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2503 return isl_union_map_is_empty(uset
);
2506 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2513 return isl_bool_error
;
2515 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2516 map
->dim
, isl_dim_out
))
2517 return isl_bool_false
;
2519 dim
= isl_map_get_space(map
);
2520 id
= isl_map_identity(dim
);
2522 is_subset
= isl_map_is_subset(map
, id
);
2529 /* Given an isl_union_map that consists of a single map, check
2530 * if it is single-valued.
2532 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2537 umap
= isl_union_map_copy(umap
);
2538 map
= isl_map_from_union_map(umap
);
2539 sv
= isl_map_is_single_valued(map
);
2545 /* Internal data structure for single_valued_on_domain.
2547 * "umap" is the union map to be tested.
2548 * "sv" is set to 1 as long as "umap" may still be single-valued.
2550 struct isl_union_map_is_sv_data
{
2551 isl_union_map
*umap
;
2555 /* Check if the data->umap is single-valued on "set".
2557 * If data->umap consists of a single map on "set", then test it
2560 * Otherwise, compute
2564 * check if the result is a subset of the identity mapping and
2565 * store the result in data->sv.
2567 * Terminate as soon as data->umap has been determined not to
2570 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2572 struct isl_union_map_is_sv_data
*data
= user
;
2573 isl_union_map
*umap
, *test
;
2575 umap
= isl_union_map_copy(data
->umap
);
2576 umap
= isl_union_map_intersect_domain(umap
,
2577 isl_union_set_from_set(set
));
2579 if (isl_union_map_n_map(umap
) == 1) {
2580 data
->sv
= single_map_is_single_valued(umap
);
2581 isl_union_map_free(umap
);
2583 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2584 test
= isl_union_map_apply_range(test
, umap
);
2586 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2588 isl_union_map_free(test
);
2591 if (data
->sv
< 0 || !data
->sv
)
2592 return isl_stat_error
;
2596 /* Check if the given map is single-valued.
2598 * If the union map consists of a single map, then test it as an isl_map.
2599 * Otherwise, check if the union map is single-valued on each of its
2602 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2604 isl_union_map
*universe
;
2605 isl_union_set
*domain
;
2606 struct isl_union_map_is_sv_data data
;
2608 if (isl_union_map_n_map(umap
) == 1)
2609 return single_map_is_single_valued(umap
);
2611 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2612 domain
= isl_union_map_domain(universe
);
2614 data
.sv
= isl_bool_true
;
2616 if (isl_union_set_foreach_set(domain
,
2617 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2618 data
.sv
= isl_bool_error
;
2619 isl_union_set_free(domain
);
2624 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2628 umap
= isl_union_map_copy(umap
);
2629 umap
= isl_union_map_reverse(umap
);
2630 in
= isl_union_map_is_single_valued(umap
);
2631 isl_union_map_free(umap
);
2636 /* Is "map" obviously not an identity relation because
2637 * it maps elements from one space to another space?
2638 * Update *non_identity accordingly.
2640 * In particular, if the domain and range spaces are the same,
2641 * then the map is not considered to obviously not be an identity relation.
2642 * Otherwise, the map is considered to obviously not be an identity relation
2643 * if it is is non-empty.
2645 * If "map" is determined to obviously not be an identity relation,
2646 * then the search is aborted.
2648 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2650 isl_bool
*non_identity
= user
;
2654 space
= isl_map_get_space(map
);
2655 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2656 if (equal
>= 0 && !equal
)
2657 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2659 *non_identity
= isl_bool_not(equal
);
2660 isl_space_free(space
);
2663 if (*non_identity
< 0 || *non_identity
)
2664 return isl_stat_error
;
2669 /* Is "umap" obviously not an identity relation because
2670 * it maps elements from one space to another space?
2672 * As soon as a map has been found that maps elements to a different space,
2673 * non_identity is changed and the search is aborted.
2675 static isl_bool
isl_union_map_plain_is_not_identity(
2676 __isl_keep isl_union_map
*umap
)
2678 isl_bool non_identity
;
2680 non_identity
= isl_bool_false
;
2681 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2682 &non_identity
) < 0 &&
2683 non_identity
== isl_bool_false
)
2684 return isl_bool_error
;
2686 return non_identity
;
2689 /* Does "map" only map elements to themselves?
2690 * Update *identity accordingly.
2692 * If "map" is determined not to be an identity relation,
2693 * then the search is aborted.
2695 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2697 isl_bool
*identity
= user
;
2699 *identity
= isl_map_is_identity(map
);
2702 if (*identity
< 0 || !*identity
)
2703 return isl_stat_error
;
2708 /* Does "umap" only map elements to themselves?
2710 * First check if there are any maps that map elements to different spaces.
2711 * If not, then check that all the maps (between identical spaces)
2712 * are identity relations.
2714 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2716 isl_bool non_identity
;
2719 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2720 if (non_identity
< 0 || non_identity
)
2721 return isl_bool_not(non_identity
);
2723 identity
= isl_bool_true
;
2724 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2725 identity
== isl_bool_true
)
2726 return isl_bool_error
;
2731 /* Represents a map that has a fixed value (v) for one of its
2733 * The map in this structure is not reference counted, so it
2734 * is only valid while the isl_union_map from which it was
2735 * obtained is still alive.
2737 struct isl_fixed_map
{
2742 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2746 struct isl_fixed_map
*v
;
2748 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2751 for (i
= 0; i
< n
; ++i
)
2752 isl_int_init(v
[i
].v
);
2756 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2762 for (i
= 0; i
< n
; ++i
)
2763 isl_int_clear(v
[i
].v
);
2767 /* Compare the "v" field of two isl_fixed_map structs.
2769 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2771 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2772 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2774 return isl_int_cmp(e1
->v
, e2
->v
);
2777 /* Internal data structure used while checking whether all maps
2778 * in a union_map have a fixed value for a given output dimension.
2779 * v is the list of maps, with the fixed value for the dimension
2780 * n is the number of maps considered so far
2781 * pos is the output dimension under investigation
2783 struct isl_fixed_dim_data
{
2784 struct isl_fixed_map
*v
;
2789 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2791 struct isl_fixed_dim_data
*data
= user
;
2793 data
->v
[data
->n
].map
= map
;
2794 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2795 &data
->v
[data
->n
++].v
);
2798 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2799 int first
, int n_range
);
2801 /* Given a list of the maps, with their fixed values at output dimension "pos",
2802 * check whether the ranges of the maps form an obvious partition.
2804 * We first sort the maps according to their fixed values.
2805 * If all maps have a different value, then we know the ranges form
2807 * Otherwise, we collect the maps with the same fixed value and
2808 * check whether each such collection is obviously injective
2809 * based on later dimensions.
2811 static int separates(struct isl_fixed_map
*v
, int n
,
2812 __isl_take isl_space
*dim
, int pos
, int n_range
)
2819 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2821 for (i
= 0; i
+ 1 < n
; ++i
) {
2823 isl_union_map
*part
;
2826 for (j
= i
+ 1; j
< n
; ++j
)
2827 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2833 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2834 for (k
= i
; k
< j
; ++k
)
2835 part
= isl_union_map_add_map(part
,
2836 isl_map_copy(v
[k
].map
));
2838 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2847 isl_space_free(dim
);
2848 free_isl_fixed_map_array(v
, n
);
2851 isl_space_free(dim
);
2852 free_isl_fixed_map_array(v
, n
);
2856 /* Check whether the maps in umap have obviously distinct ranges.
2857 * In particular, check for an output dimension in the range
2858 * [first,n_range) for which all maps have a fixed value
2859 * and then check if these values, possibly along with fixed values
2860 * at later dimensions, entail distinct ranges.
2862 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2863 int first
, int n_range
)
2867 struct isl_fixed_dim_data data
= { NULL
};
2869 ctx
= isl_union_map_get_ctx(umap
);
2871 n
= isl_union_map_n_map(umap
);
2876 isl_union_map_free(umap
);
2877 return isl_bool_true
;
2880 if (first
>= n_range
) {
2881 isl_union_map_free(umap
);
2882 return isl_bool_false
;
2885 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2889 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2895 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2900 dim
= isl_union_map_get_space(umap
);
2901 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2902 isl_union_map_free(umap
);
2906 free_isl_fixed_map_array(data
.v
, n
);
2907 isl_union_map_free(umap
);
2909 return isl_bool_false
;
2911 free_isl_fixed_map_array(data
.v
, n
);
2912 isl_union_map_free(umap
);
2913 return isl_bool_error
;
2916 /* Check whether the maps in umap that map to subsets of "ran"
2917 * have obviously distinct ranges.
2919 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
2922 isl_union_map
*umap
= user
;
2924 umap
= isl_union_map_copy(umap
);
2925 umap
= isl_union_map_intersect_range(umap
,
2926 isl_union_set_from_set(isl_set_copy(ran
)));
2927 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2930 /* Check if the given union_map is obviously injective.
2932 * In particular, we first check if all individual maps are obviously
2933 * injective and then check if all the ranges of these maps are
2934 * obviously disjoint.
2936 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2939 isl_union_map
*univ
;
2942 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2944 return isl_bool_error
;
2946 return isl_bool_false
;
2948 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2949 ran
= isl_union_map_range(univ
);
2951 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2953 isl_union_set_free(ran
);
2958 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2962 sv
= isl_union_map_is_single_valued(umap
);
2966 return isl_union_map_is_injective(umap
);
2969 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2971 struct isl_un_op_control control
= {
2972 .filter
= &isl_map_can_zip
,
2973 .fn_map
= &isl_map_zip
,
2975 return un_op(umap
, &control
);
2978 /* Given a union map, take the maps of the form A -> (B -> C) and
2979 * return the union of the corresponding maps (A -> B) -> C.
2981 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
2983 struct isl_un_op_control control
= {
2984 .filter
= &isl_map_can_uncurry
,
2985 .fn_map
= &isl_map_uncurry
,
2987 return un_op(umap
, &control
);
2990 /* Given a union map, take the maps of the form (A -> B) -> C and
2991 * return the union of the corresponding maps A -> (B -> C).
2993 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
2995 struct isl_un_op_control control
= {
2996 .filter
= &isl_map_can_curry
,
2997 .fn_map
= &isl_map_curry
,
2999 return un_op(umap
, &control
);
3002 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3003 * return the union of the corresponding maps A -> (B -> (C -> D)).
3005 __isl_give isl_union_map
*isl_union_map_range_curry(
3006 __isl_take isl_union_map
*umap
)
3008 struct isl_un_op_control control
= {
3009 .filter
= &isl_map_can_range_curry
,
3010 .fn_map
= &isl_map_range_curry
,
3012 return un_op(umap
, &control
);
3015 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
3017 struct isl_un_op_control control
= {
3018 .fn_map
= &isl_set_lift
,
3020 return un_op(uset
, &control
);
3023 static isl_stat
coefficients_entry(void **entry
, void *user
)
3025 isl_set
*set
= *entry
;
3026 isl_union_set
**res
= user
;
3028 set
= isl_set_copy(set
);
3029 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
3030 *res
= isl_union_set_add_set(*res
, set
);
3035 __isl_give isl_union_set
*isl_union_set_coefficients(
3036 __isl_take isl_union_set
*uset
)
3045 ctx
= isl_union_set_get_ctx(uset
);
3046 dim
= isl_space_set_alloc(ctx
, 0, 0);
3047 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
3048 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3049 &coefficients_entry
, &res
) < 0)
3052 isl_union_set_free(uset
);
3055 isl_union_set_free(uset
);
3056 isl_union_set_free(res
);
3060 static isl_stat
solutions_entry(void **entry
, void *user
)
3062 isl_set
*set
= *entry
;
3063 isl_union_set
**res
= user
;
3065 set
= isl_set_copy(set
);
3066 set
= isl_set_from_basic_set(isl_set_solutions(set
));
3068 *res
= isl_union_set_from_set(set
);
3070 *res
= isl_union_set_add_set(*res
, set
);
3073 return isl_stat_error
;
3078 __isl_give isl_union_set
*isl_union_set_solutions(
3079 __isl_take isl_union_set
*uset
)
3081 isl_union_set
*res
= NULL
;
3086 if (uset
->table
.n
== 0) {
3087 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
3088 isl_union_set_free(uset
);
3092 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3093 &solutions_entry
, &res
) < 0)
3096 isl_union_set_free(uset
);
3099 isl_union_set_free(uset
);
3100 isl_union_set_free(res
);
3104 /* Is the domain space of "map" equal to "space"?
3106 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3108 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
3109 space
, isl_dim_out
);
3112 /* Is the range space of "map" equal to "space"?
3114 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3116 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3117 space
, isl_dim_out
);
3120 /* Is the set space of "map" equal to "space"?
3122 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3124 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3125 space
, isl_dim_out
);
3128 /* Internal data structure for preimage_pw_multi_aff.
3130 * "pma" is the function under which the preimage should be taken.
3131 * "space" is the space of "pma".
3132 * "res" collects the results.
3133 * "fn" computes the preimage for a given map.
3134 * "match" returns true if "fn" can be called.
3136 struct isl_union_map_preimage_data
{
3138 isl_pw_multi_aff
*pma
;
3140 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3141 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3142 __isl_take isl_pw_multi_aff
*pma
);
3145 /* Call data->fn to compute the preimage of the domain or range of *entry
3146 * under the function represented by data->pma, provided the domain/range
3147 * space of *entry matches the target space of data->pma
3148 * (as given by data->match), and add the result to data->res.
3150 static isl_stat
preimage_entry(void **entry
, void *user
)
3153 isl_map
*map
= *entry
;
3154 struct isl_union_map_preimage_data
*data
= user
;
3157 m
= data
->match(map
, data
->space
);
3159 return isl_stat_error
;
3163 map
= isl_map_copy(map
);
3164 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3166 empty
= isl_map_is_empty(map
);
3167 if (empty
< 0 || empty
) {
3169 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3172 data
->res
= isl_union_map_add_map(data
->res
, map
);
3177 /* Compute the preimage of the domain or range of "umap" under the function
3178 * represented by "pma".
3179 * In other words, plug in "pma" in the domain or range of "umap".
3180 * The function "fn" performs the actual preimage computation on a map,
3181 * while "match" determines to which maps the function should be applied.
3183 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3184 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3185 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3186 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3187 __isl_take isl_pw_multi_aff
*pma
))
3191 struct isl_union_map_preimage_data data
;
3193 umap
= isl_union_map_align_params(umap
,
3194 isl_pw_multi_aff_get_space(pma
));
3195 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3200 ctx
= isl_union_map_get_ctx(umap
);
3201 space
= isl_union_map_get_space(umap
);
3202 data
.space
= isl_pw_multi_aff_get_space(pma
);
3204 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3207 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3209 data
.res
= isl_union_map_free(data
.res
);
3211 isl_space_free(data
.space
);
3212 isl_union_map_free(umap
);
3213 isl_pw_multi_aff_free(pma
);
3216 isl_union_map_free(umap
);
3217 isl_pw_multi_aff_free(pma
);
3221 /* Compute the preimage of the domain of "umap" under the function
3222 * represented by "pma".
3223 * In other words, plug in "pma" in the domain of "umap".
3224 * The result contains maps that live in the same spaces as the maps of "umap"
3225 * with domain space equal to the target space of "pma",
3226 * except that the domain has been replaced by the domain space of "pma".
3228 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3229 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3231 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3232 &isl_map_preimage_domain_pw_multi_aff
);
3235 /* Compute the preimage of the range of "umap" under the function
3236 * represented by "pma".
3237 * In other words, plug in "pma" in the range of "umap".
3238 * The result contains maps that live in the same spaces as the maps of "umap"
3239 * with range space equal to the target space of "pma",
3240 * except that the range has been replaced by the domain space of "pma".
3242 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3243 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3245 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3246 &isl_map_preimage_range_pw_multi_aff
);
3249 /* Compute the preimage of "uset" under the function represented by "pma".
3250 * In other words, plug in "pma" in "uset".
3251 * The result contains sets that live in the same spaces as the sets of "uset"
3252 * with space equal to the target space of "pma",
3253 * except that the space has been replaced by the domain space of "pma".
3255 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3256 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3258 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3259 &isl_set_preimage_pw_multi_aff
);
3262 /* Compute the preimage of the domain of "umap" under the function
3263 * represented by "ma".
3264 * In other words, plug in "ma" in the domain of "umap".
3265 * The result contains maps that live in the same spaces as the maps of "umap"
3266 * with domain space equal to the target space of "ma",
3267 * except that the domain has been replaced by the domain space of "ma".
3269 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3270 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3272 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3273 isl_pw_multi_aff_from_multi_aff(ma
));
3276 /* Compute the preimage of the range of "umap" under the function
3277 * represented by "ma".
3278 * In other words, plug in "ma" in the range of "umap".
3279 * The result contains maps that live in the same spaces as the maps of "umap"
3280 * with range space equal to the target space of "ma",
3281 * except that the range has been replaced by the domain space of "ma".
3283 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3284 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3286 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3287 isl_pw_multi_aff_from_multi_aff(ma
));
3290 /* Compute the preimage of "uset" under the function represented by "ma".
3291 * In other words, plug in "ma" in "uset".
3292 * The result contains sets that live in the same spaces as the sets of "uset"
3293 * with space equal to the target space of "ma",
3294 * except that the space has been replaced by the domain space of "ma".
3296 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3297 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3299 return isl_union_set_preimage_pw_multi_aff(uset
,
3300 isl_pw_multi_aff_from_multi_aff(ma
));
3303 /* Internal data structure for preimage_multi_pw_aff.
3305 * "mpa" is the function under which the preimage should be taken.
3306 * "space" is the space of "mpa".
3307 * "res" collects the results.
3308 * "fn" computes the preimage for a given map.
3309 * "match" returns true if "fn" can be called.
3311 struct isl_union_map_preimage_mpa_data
{
3313 isl_multi_pw_aff
*mpa
;
3315 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3316 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3317 __isl_take isl_multi_pw_aff
*mpa
);
3320 /* Call data->fn to compute the preimage of the domain or range of *entry
3321 * under the function represented by data->mpa, provided the domain/range
3322 * space of *entry matches the target space of data->mpa
3323 * (as given by data->match), and add the result to data->res.
3325 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3328 isl_map
*map
= *entry
;
3329 struct isl_union_map_preimage_mpa_data
*data
= user
;
3332 m
= data
->match(map
, data
->space
);
3334 return isl_stat_error
;
3338 map
= isl_map_copy(map
);
3339 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3341 empty
= isl_map_is_empty(map
);
3342 if (empty
< 0 || empty
) {
3344 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3347 data
->res
= isl_union_map_add_map(data
->res
, map
);
3352 /* Compute the preimage of the domain or range of "umap" under the function
3353 * represented by "mpa".
3354 * In other words, plug in "mpa" in the domain or range of "umap".
3355 * The function "fn" performs the actual preimage computation on a map,
3356 * while "match" determines to which maps the function should be applied.
3358 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3359 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3360 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3361 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3362 __isl_take isl_multi_pw_aff
*mpa
))
3366 struct isl_union_map_preimage_mpa_data data
;
3368 umap
= isl_union_map_align_params(umap
,
3369 isl_multi_pw_aff_get_space(mpa
));
3370 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3375 ctx
= isl_union_map_get_ctx(umap
);
3376 space
= isl_union_map_get_space(umap
);
3377 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3379 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3382 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3384 data
.res
= isl_union_map_free(data
.res
);
3386 isl_space_free(data
.space
);
3387 isl_union_map_free(umap
);
3388 isl_multi_pw_aff_free(mpa
);
3391 isl_union_map_free(umap
);
3392 isl_multi_pw_aff_free(mpa
);
3396 /* Compute the preimage of the domain of "umap" under the function
3397 * represented by "mpa".
3398 * In other words, plug in "mpa" in the domain of "umap".
3399 * The result contains maps that live in the same spaces as the maps of "umap"
3400 * with domain space equal to the target space of "mpa",
3401 * except that the domain has been replaced by the domain space of "mpa".
3403 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3404 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3406 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3407 &isl_map_preimage_domain_multi_pw_aff
);
3410 /* Internal data structure for preimage_upma.
3412 * "umap" is the map of which the preimage should be computed.
3413 * "res" collects the results.
3414 * "fn" computes the preimage for a given piecewise multi-affine function.
3416 struct isl_union_map_preimage_upma_data
{
3417 isl_union_map
*umap
;
3419 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3420 __isl_take isl_pw_multi_aff
*pma
);
3423 /* Call data->fn to compute the preimage of the domain or range of data->umap
3424 * under the function represented by pma and add the result to data->res.
3426 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3428 struct isl_union_map_preimage_upma_data
*data
= user
;
3429 isl_union_map
*umap
;
3431 umap
= isl_union_map_copy(data
->umap
);
3432 umap
= data
->fn(umap
, pma
);
3433 data
->res
= isl_union_map_union(data
->res
, umap
);
3435 return data
->res
? isl_stat_ok
: isl_stat_error
;
3438 /* Compute the preimage of the domain or range of "umap" under the function
3439 * represented by "upma".
3440 * In other words, plug in "upma" in the domain or range of "umap".
3441 * The function "fn" performs the actual preimage computation
3442 * on a piecewise multi-affine function.
3444 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3445 __isl_take isl_union_map
*umap
,
3446 __isl_take isl_union_pw_multi_aff
*upma
,
3447 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3448 __isl_take isl_pw_multi_aff
*pma
))
3450 struct isl_union_map_preimage_upma_data data
;
3453 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3455 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3456 &preimage_upma
, &data
) < 0)
3457 data
.res
= isl_union_map_free(data
.res
);
3459 isl_union_map_free(umap
);
3460 isl_union_pw_multi_aff_free(upma
);
3465 /* Compute the preimage of the domain of "umap" under the function
3466 * represented by "upma".
3467 * In other words, plug in "upma" in the domain of "umap".
3468 * The result contains maps that live in the same spaces as the maps of "umap"
3469 * with domain space equal to one of the target spaces of "upma",
3470 * except that the domain has been replaced by one of the the domain spaces that
3471 * corresponds to that target space of "upma".
3473 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3474 __isl_take isl_union_map
*umap
,
3475 __isl_take isl_union_pw_multi_aff
*upma
)
3477 return preimage_union_pw_multi_aff(umap
, upma
,
3478 &isl_union_map_preimage_domain_pw_multi_aff
);
3481 /* Compute the preimage of the range of "umap" under the function
3482 * represented by "upma".
3483 * In other words, plug in "upma" in the range of "umap".
3484 * The result contains maps that live in the same spaces as the maps of "umap"
3485 * with range space equal to one of the target spaces of "upma",
3486 * except that the range has been replaced by one of the the domain spaces that
3487 * corresponds to that target space of "upma".
3489 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3490 __isl_take isl_union_map
*umap
,
3491 __isl_take isl_union_pw_multi_aff
*upma
)
3493 return preimage_union_pw_multi_aff(umap
, upma
,
3494 &isl_union_map_preimage_range_pw_multi_aff
);
3497 /* Compute the preimage of "uset" under the function represented by "upma".
3498 * In other words, plug in "upma" in the range of "uset".
3499 * The result contains sets that live in the same spaces as the sets of "uset"
3500 * with space equal to one of the target spaces of "upma",
3501 * except that the space has been replaced by one of the the domain spaces that
3502 * corresponds to that target space of "upma".
3504 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3505 __isl_take isl_union_set
*uset
,
3506 __isl_take isl_union_pw_multi_aff
*upma
)
3508 return preimage_union_pw_multi_aff(uset
, upma
,
3509 &isl_union_set_preimage_pw_multi_aff
);
3512 /* Reset the user pointer on all identifiers of parameters and tuples
3513 * of the spaces of "umap".
3515 __isl_give isl_union_map
*isl_union_map_reset_user(
3516 __isl_take isl_union_map
*umap
)
3518 umap
= isl_union_map_cow(umap
);
3521 umap
->dim
= isl_space_reset_user(umap
->dim
);
3523 return isl_union_map_free(umap
);
3524 return total(umap
, &isl_map_reset_user
);
3527 /* Reset the user pointer on all identifiers of parameters and tuples
3528 * of the spaces of "uset".
3530 __isl_give isl_union_set
*isl_union_set_reset_user(
3531 __isl_take isl_union_set
*uset
)
3533 return isl_union_map_reset_user(uset
);
3536 /* Remove all existentially quantified variables and integer divisions
3537 * from "umap" using Fourier-Motzkin elimination.
3539 __isl_give isl_union_map
*isl_union_map_remove_divs(
3540 __isl_take isl_union_map
*umap
)
3542 return total(umap
, &isl_map_remove_divs
);
3545 /* Remove all existentially quantified variables and integer divisions
3546 * from "uset" using Fourier-Motzkin elimination.
3548 __isl_give isl_union_set
*isl_union_set_remove_divs(
3549 __isl_take isl_union_set
*uset
)
3551 return isl_union_map_remove_divs(uset
);
3554 /* Internal data structure for isl_union_map_project_out.
3555 * "type", "first" and "n" are the arguments for the isl_map_project_out
3557 * "res" collects the results.
3559 struct isl_union_map_project_out_data
{
3560 enum isl_dim_type type
;
3567 /* Turn the data->n dimensions of type data->type, starting at data->first
3568 * into existentially quantified variables and add the result to data->res.
3570 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3572 struct isl_union_map_project_out_data
*data
= user
;
3574 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3575 data
->res
= isl_union_map_add_map(data
->res
, map
);
3580 /* Turn the "n" dimensions of type "type", starting at "first"
3581 * into existentially quantified variables.
3582 * Since the space of an isl_union_map only contains parameters,
3583 * type is required to be equal to isl_dim_param.
3585 __isl_give isl_union_map
*isl_union_map_project_out(
3586 __isl_take isl_union_map
*umap
,
3587 enum isl_dim_type type
, unsigned first
, unsigned n
)
3590 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3595 if (type
!= isl_dim_param
)
3596 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3597 "can only project out parameters",
3598 return isl_union_map_free(umap
));
3600 space
= isl_union_map_get_space(umap
);
3601 space
= isl_space_drop_dims(space
, type
, first
, n
);
3602 data
.res
= isl_union_map_empty(space
);
3603 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3604 data
.res
= isl_union_map_free(data
.res
);
3606 isl_union_map_free(umap
);
3611 /* Turn the "n" dimensions of type "type", starting at "first"
3612 * into existentially quantified variables.
3613 * Since the space of an isl_union_set only contains parameters,
3614 * "type" is required to be equal to isl_dim_param.
3616 __isl_give isl_union_set
*isl_union_set_project_out(
3617 __isl_take isl_union_set
*uset
,
3618 enum isl_dim_type type
, unsigned first
, unsigned n
)
3620 return isl_union_map_project_out(uset
, type
, first
, n
);
3623 /* Internal data structure for isl_union_map_involves_dims.
3624 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3626 struct isl_union_map_involves_dims_data
{
3631 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3633 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3635 struct isl_union_map_involves_dims_data
*data
= user
;
3638 involves
= isl_map_involves_dims(map
,
3639 isl_dim_param
, data
->first
, data
->n
);
3641 return isl_bool_error
;
3645 /* Does "umap" involve any of the n parameters starting at first?
3646 * "type" is required to be set to isl_dim_param.
3648 * "umap" involves any of those parameters if any of its maps
3649 * involve the parameters. In other words, "umap" does not
3650 * involve any of the parameters if all its maps to not
3651 * involve the parameters.
3653 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3654 enum isl_dim_type type
, unsigned first
, unsigned n
)
3656 struct isl_union_map_involves_dims_data data
= { first
, n
};
3659 if (type
!= isl_dim_param
)
3660 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3661 "can only reference parameters", return isl_bool_error
);
3663 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3666 return isl_bool_error
;
3671 /* Internal data structure for isl_union_map_reset_range_space.
3672 * "range" is the space from which to set the range space.
3673 * "res" collects the results.
3675 struct isl_union_map_reset_range_space_data
{
3680 /* Replace the range space of "map" by the range space of data->range and
3681 * add the result to data->res.
3683 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3685 struct isl_union_map_reset_range_space_data
*data
= user
;
3688 space
= isl_map_get_space(map
);
3689 space
= isl_space_domain(space
);
3690 space
= isl_space_extend_domain_with_range(space
,
3691 isl_space_copy(data
->range
));
3692 map
= isl_map_reset_space(map
, space
);
3693 data
->res
= isl_union_map_add_map(data
->res
, map
);
3695 return data
->res
? isl_stat_ok
: isl_stat_error
;
3698 /* Replace the range space of all the maps in "umap" by
3699 * the range space of "space".
3701 * This assumes that all maps have the same output dimension.
3702 * This function should therefore not be made publicly available.
3704 * Since the spaces of the maps change, so do their hash value.
3705 * We therefore need to create a new isl_union_map.
3707 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3708 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3710 struct isl_union_map_reset_range_space_data data
= { space
};
3712 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3713 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
3714 data
.res
= isl_union_map_free(data
.res
);
3716 isl_space_free(space
);
3717 isl_union_map_free(umap
);
3721 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3722 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3723 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3724 * to the domain and the range of each map.
3725 * "res" collects the results.
3727 struct isl_union_order_at_data
{
3728 isl_multi_union_pw_aff
*mupa
;
3729 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3730 __isl_take isl_multi_pw_aff
*mpa2
);
3734 /* Intersect "map" with the result of applying data->order to
3735 * the functions in data->mupa that apply to the domain and the range
3736 * of "map" and add the result to data->res.
3738 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
3740 struct isl_union_order_at_data
*data
= user
;
3742 isl_multi_pw_aff
*mpa1
, *mpa2
;
3745 space
= isl_space_domain(isl_map_get_space(map
));
3746 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3747 space
= isl_space_range(isl_map_get_space(map
));
3748 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3749 order
= data
->order(mpa1
, mpa2
);
3750 map
= isl_map_intersect(map
, order
);
3751 data
->res
= isl_union_map_add_map(data
->res
, map
);
3753 return data
->res
? isl_stat_ok
: isl_stat_error
;
3756 /* Intersect each map in "umap" with the result of calling "order"
3757 * on the functions is "mupa" that apply to the domain and the range
3760 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
3761 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
3762 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3763 __isl_take isl_multi_pw_aff
*mpa2
))
3765 struct isl_union_order_at_data data
;
3767 umap
= isl_union_map_align_params(umap
,
3768 isl_multi_union_pw_aff_get_space(mupa
));
3769 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
3770 isl_union_map_get_space(umap
));
3773 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3774 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
3775 data
.res
= isl_union_map_free(data
.res
);
3777 isl_multi_union_pw_aff_free(mupa
);
3778 isl_union_map_free(umap
);
3782 /* Return the subset of "umap" where the domain and the range
3783 * have equal "mupa" values.
3785 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
3786 __isl_take isl_union_map
*umap
,
3787 __isl_take isl_multi_union_pw_aff
*mupa
)
3789 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3790 &isl_multi_pw_aff_eq_map
);
3793 /* Return the subset of "umap" where the domain has a lexicographically
3794 * smaller "mupa" value than the range.
3796 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
3797 __isl_take isl_union_map
*umap
,
3798 __isl_take isl_multi_union_pw_aff
*mupa
)
3800 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3801 &isl_multi_pw_aff_lex_lt_map
);
3804 /* Return the subset of "umap" where the domain has a lexicographically
3805 * greater "mupa" value than the range.
3807 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
3808 __isl_take isl_union_map
*umap
,
3809 __isl_take isl_multi_union_pw_aff
*mupa
)
3811 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3812 &isl_multi_pw_aff_lex_gt_map
);
3815 /* Return the union of the elements in the list "list".
3817 __isl_give isl_union_set
*isl_union_set_list_union(
3818 __isl_take isl_union_set_list
*list
)
3828 ctx
= isl_union_set_list_get_ctx(list
);
3829 space
= isl_space_params_alloc(ctx
, 0);
3830 res
= isl_union_set_empty(space
);
3832 n
= isl_union_set_list_n_union_set(list
);
3833 for (i
= 0; i
< n
; ++i
) {
3834 isl_union_set
*uset_i
;
3836 uset_i
= isl_union_set_list_get_union_set(list
, i
);
3837 res
= isl_union_set_union(res
, uset_i
);
3840 isl_union_set_list_free(list
);
3844 /* Update *hash with the hash value of "map".
3846 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
3848 uint32_t *hash
= user
;
3851 map_hash
= isl_map_get_hash(map
);
3852 isl_hash_hash(*hash
, map_hash
);
3858 /* Return a hash value that digests "umap".
3860 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
3867 hash
= isl_hash_init();
3868 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
3874 /* Return a hash value that digests "uset".
3876 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
3878 return isl_union_map_get_hash(uset
);
3881 /* Add the number of basic sets in "set" to "n".
3883 static isl_stat
add_n(__isl_take isl_set
*set
, void *user
)
3887 *n
+= isl_set_n_basic_set(set
);
3893 /* Return the total number of basic sets in "uset".
3895 int isl_union_set_n_basic_set(__isl_keep isl_union_set
*uset
)
3899 if (isl_union_set_foreach_set(uset
, &add_n
, &n
) < 0)
3905 /* Add the basic sets in "set" to "list".
3907 static isl_stat
add_list(__isl_take isl_set
*set
, void *user
)
3909 isl_basic_set_list
**list
= user
;
3910 isl_basic_set_list
*list_i
;
3912 list_i
= isl_set_get_basic_set_list(set
);
3913 *list
= isl_basic_set_list_concat(*list
, list_i
);
3917 return isl_stat_error
;
3921 /* Return a list containing all the basic sets in "uset".
3923 * First construct a list of the appropriate size and
3924 * then add all the elements.
3926 __isl_give isl_basic_set_list
*isl_union_set_get_basic_set_list(
3927 __isl_keep isl_union_set
*uset
)
3931 isl_basic_set_list
*list
;
3935 ctx
= isl_union_set_get_ctx(uset
);
3936 n
= isl_union_set_n_basic_set(uset
);
3939 list
= isl_basic_set_list_alloc(ctx
, n
);
3940 if (isl_union_set_foreach_set(uset
, &add_list
, &list
) < 0)
3941 list
= isl_basic_set_list_free(list
);