2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
6 * Copyright 2022 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
11 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
15 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
22 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl/union_set.h>
27 #include <isl_maybe_map.h>
28 #include <isl_id_private.h>
30 #include <bset_from_bmap.c>
31 #include <set_to_map.c>
32 #include <set_from_map.c>
33 #include <uset_to_umap.c>
34 #include <uset_from_umap.c>
35 #include <set_list_from_map_list_inl.c>
38 #define TYPE isl_union_map
40 #include "has_single_reference_templ.c"
42 #include "check_single_reference_templ.c"
44 /* Return the number of parameters of "umap", where "type"
45 * is required to be set to isl_dim_param.
47 isl_size
isl_union_map_dim(__isl_keep isl_union_map
*umap
,
48 enum isl_dim_type type
)
51 return isl_size_error
;
53 if (type
!= isl_dim_param
)
54 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
55 "can only reference parameters", return isl_size_error
);
57 return isl_space_dim(umap
->dim
, type
);
60 /* Return the number of parameters of "uset", where "type"
61 * is required to be set to isl_dim_param.
63 isl_size
isl_union_set_dim(__isl_keep isl_union_set
*uset
,
64 enum isl_dim_type type
)
66 return isl_union_map_dim(uset
, type
);
69 /* Return the id of the specified dimension.
71 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
72 enum isl_dim_type type
, unsigned pos
)
77 if (type
!= isl_dim_param
)
78 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
79 "can only reference parameters", return NULL
);
81 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
84 /* Is this union set a parameter domain?
86 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
92 return isl_bool_error
;
93 if (uset
->table
.n
!= 1)
94 return isl_bool_false
;
96 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
97 params
= isl_set_is_params(set
);
102 /* Is this union map actually a parameter domain?
103 * Users should never call this function. Outside of isl,
104 * a union map can never be a parameter domain.
106 isl_bool
isl_union_map_is_params(__isl_keep isl_union_map
*umap
)
108 return isl_union_set_is_params(uset_from_umap(umap
));
111 static __isl_give isl_union_map
*isl_union_map_alloc(
112 __isl_take isl_space
*space
, int size
)
116 space
= isl_space_params(space
);
120 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
122 isl_space_free(space
);
128 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
129 return isl_union_map_free(umap
);
134 /* Create an empty union map without specifying any parameters.
136 __isl_give isl_union_map
*isl_union_map_empty_ctx(isl_ctx
*ctx
)
138 return isl_union_map_empty_space(isl_space_unit(ctx
));
141 __isl_give isl_union_map
*isl_union_map_empty_space(__isl_take isl_space
*space
)
143 return isl_union_map_alloc(space
, 16);
146 /* This is an alternative name for the function above.
148 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*space
)
150 return isl_union_map_empty_space(space
);
153 /* Create an empty union set without specifying any parameters.
155 __isl_give isl_union_set
*isl_union_set_empty_ctx(isl_ctx
*ctx
)
157 return uset_from_umap(isl_union_map_empty_ctx(ctx
));
160 __isl_give isl_union_set
*isl_union_set_empty_space(__isl_take isl_space
*space
)
162 return uset_from_umap(isl_union_map_empty_space(space
));
165 /* This is an alternative name for the function above.
167 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*space
)
169 return isl_union_set_empty_space(space
);
172 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
174 return umap
? umap
->dim
->ctx
: NULL
;
177 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
179 return uset
? uset
->dim
->ctx
: NULL
;
182 /* Return the space of "umap".
184 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
186 return umap
? umap
->dim
: NULL
;
189 /* Return the space of "uset".
191 __isl_keep isl_space
*isl_union_set_peek_space(__isl_keep isl_union_set
*uset
)
193 return isl_union_map_peek_space(uset_to_umap(uset
));
196 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
198 return isl_space_copy(isl_union_map_peek_space(umap
));
201 /* Return the position of the parameter with the given name
203 * Return -1 if no such dimension can be found.
205 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
206 enum isl_dim_type type
, const char *name
)
210 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
213 /* Return the position of the parameter with id "id" in "umap".
214 * Return -1 if no such dimension can be found.
216 static int isl_union_map_find_dim_by_id(__isl_keep isl_union_map
*umap
,
217 enum isl_dim_type type
, __isl_keep isl_id
*id
)
221 space
= isl_union_map_peek_space(umap
);
222 return isl_space_find_dim_by_id(space
, type
, id
);
225 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
227 return isl_union_map_get_space(uset
);
230 static isl_stat
free_umap_entry(void **entry
, void *user
)
232 isl_map
*map
= *entry
;
237 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
239 isl_union_map
**umap
= (isl_union_map
**)user
;
241 *umap
= isl_union_map_add_map(*umap
, map
);
246 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
253 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
254 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
258 isl_union_map_free(dup
);
262 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
270 return isl_union_map_dup(umap
);
273 struct isl_union_align
{
278 static isl_stat
align_entry(void **entry
, void *user
)
280 isl_map
*map
= *entry
;
282 struct isl_union_align
*data
= user
;
284 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
285 isl_map_get_space(map
));
287 data
->res
= isl_union_map_add_map(data
->res
,
288 isl_map_realign(isl_map_copy(map
), exp
));
293 /* Align the parameters of umap along those of model.
294 * The result has the parameters of model first, in the same order
295 * as they appear in model, followed by any remaining parameters of
296 * umap that do not appear in model.
298 __isl_give isl_union_map
*isl_union_map_align_params(
299 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
301 struct isl_union_align data
= { NULL
, NULL
};
303 isl_bool equal_params
;
305 space
= isl_union_map_peek_space(umap
);
306 equal_params
= isl_space_has_equal_params(space
, model
);
307 if (equal_params
< 0)
310 isl_space_free(model
);
314 data
.exp
= isl_parameter_alignment_reordering(space
, model
);
318 data
.res
= isl_union_map_alloc(isl_reordering_get_space(data
.exp
),
320 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
), &umap
->table
,
321 &align_entry
, &data
) < 0)
324 isl_reordering_free(data
.exp
);
325 isl_union_map_free(umap
);
326 isl_space_free(model
);
329 isl_reordering_free(data
.exp
);
330 isl_union_map_free(umap
);
331 isl_union_map_free(data
.res
);
332 isl_space_free(model
);
336 __isl_give isl_union_set
*isl_union_set_align_params(
337 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
339 return isl_union_map_align_params(uset
, model
);
342 /* This is a wrapper around isl_union_map_project_out for use
343 * by isl_union_map_drop_unused_params.
345 * In particular, this function is only called on parameters
346 * that are not involved in the description of "umap".
347 * Dropping those parameters is therefore equivalent
348 * to projecting them out.
350 static __isl_give isl_union_map
*isl_union_map_drop_dims(
351 __isl_take isl_union_map
*umap
,
352 enum isl_dim_type type
, unsigned first
, unsigned n
)
354 return isl_union_map_project_out(umap
, type
, first
, n
);
358 #define TYPE isl_union_map
359 #include "isl_check_named_params_templ.c"
360 #include "isl_drop_unused_params_templ.c"
362 /* Drop all parameters not referenced by "uset".
364 __isl_give isl_union_set
*isl_union_set_drop_unused_params(
365 __isl_take isl_union_set
*uset
)
369 umap
= isl_union_map_drop_unused_params(uset_to_umap(uset
));
370 return uset_from_umap(umap
);
373 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
374 __isl_take isl_union_map
*umap2
)
376 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
377 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
379 umap1
= isl_union_map_cow(umap1
);
381 if (!umap1
|| !umap2
)
384 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
387 isl_union_map_free(umap2
);
391 isl_union_map_free(umap1
);
392 isl_union_map_free(umap2
);
396 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
397 __isl_take isl_union_set
*uset2
)
399 return isl_union_map_union(uset1
, uset2
);
402 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
411 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
413 return isl_union_map_copy(uset
);
416 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
424 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
425 &free_umap_entry
, NULL
);
426 isl_hash_table_clear(&umap
->table
);
427 isl_space_free(umap
->dim
);
432 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
434 return isl_union_map_free(uset
);
437 /* Do "umap" and "space" have the same parameters?
439 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
440 __isl_keep isl_space
*space
)
442 isl_space
*umap_space
;
444 umap_space
= isl_union_map_peek_space(umap
);
445 return isl_space_has_equal_params(umap_space
, space
);
448 /* Do "uset" and "space" have the same parameters?
450 isl_bool
isl_union_set_space_has_equal_params(__isl_keep isl_union_set
*uset
,
451 __isl_keep isl_space
*space
)
453 return isl_union_map_space_has_equal_params(uset_to_umap(uset
), space
);
456 /* Is the space of the map at "entry" equal to "space", ignoring parameters?
458 static isl_bool
has_space_tuples(const void *entry
, const void *val
)
460 isl_map
*map
= (isl_map
*)entry
;
461 isl_space
*space
= (isl_space
*) val
;
463 return isl_map_has_space_tuples(map
, space
);
466 /* Find the entry in "umap" with space "space" (ignoring parameters),
467 * returning isl_hash_table_entry_none if no such entry appears in "umap" and
469 * If "reserve" is set, then an entry is created if it does
470 * not exist already. Since this modifies the hash table in-place,
471 * this means "umap" must have a single reference when "reserve" is set.
473 static struct isl_hash_table_entry
*isl_union_map_find_entry(
474 __isl_keep isl_union_map
*umap
, __isl_keep isl_space
*space
,
481 if (reserve
&& isl_union_map_check_single_reference(umap
) < 0)
484 hash
= isl_space_get_tuple_hash(space
);
485 return isl_hash_table_find(isl_union_map_get_ctx(umap
), &umap
->table
,
486 hash
, &has_space_tuples
, space
, reserve
);
489 /* Find the entry in "uset" with space "space" (ignoring parameters),
490 * returning isl_hash_table_entry_none if no such entry appears in "uset" and
492 * If "reserve" is set, then an entry is created if it does
493 * not exist already. In this case, a NULL return indicates an error.
495 struct isl_hash_table_entry
*isl_union_set_find_entry(
496 __isl_keep isl_union_set
*uset
, __isl_keep isl_space
*space
,
499 return isl_union_map_find_entry(uset_to_umap(uset
), space
, reserve
);
502 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
503 __isl_take isl_map
*map
)
505 struct isl_hash_table_entry
*entry
;
512 if (isl_map_plain_is_empty(map
)) {
517 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
521 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
522 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
525 umap
= isl_union_map_cow(umap
);
527 space
= isl_map_peek_space(map
);
528 entry
= isl_union_map_find_entry(umap
, space
, 1);
535 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
544 isl_union_map_free(umap
);
548 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
549 __isl_take isl_set
*set
)
551 return isl_union_map_add_map(uset
, set_to_map(set
));
554 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
562 space
= isl_map_get_space(map
);
563 space
= isl_space_params(space
);
564 umap
= isl_union_map_empty(space
);
565 umap
= isl_union_map_add_map(umap
, map
);
570 /* This function performs the same operation as isl_union_map_from_map,
571 * but is considered as a function on an isl_map when exported.
573 __isl_give isl_union_map
*isl_map_to_union_map(__isl_take isl_map
*map
)
575 return isl_union_map_from_map(map
);
578 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
580 return isl_union_map_from_map(set_to_map(set
));
583 /* This function performs the same operation as isl_union_set_from_set,
584 * but is considered as a function on an isl_set when exported.
586 __isl_give isl_union_set
*isl_set_to_union_set(__isl_take isl_set
*set
)
588 return isl_union_set_from_set(set
);
591 __isl_give isl_union_map
*isl_union_map_from_basic_map(
592 __isl_take isl_basic_map
*bmap
)
594 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
597 __isl_give isl_union_set
*isl_union_set_from_basic_set(
598 __isl_take isl_basic_set
*bset
)
600 return isl_union_map_from_basic_map(bset
);
603 struct isl_union_map_foreach_data
605 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
609 static isl_stat
call_on_copy(void **entry
, void *user
)
611 isl_map
*map
= *entry
;
612 struct isl_union_map_foreach_data
*data
;
613 data
= (struct isl_union_map_foreach_data
*)user
;
615 return data
->fn(isl_map_copy(map
), data
->user
);
618 isl_size
isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
620 return umap
? umap
->table
.n
: isl_size_error
;
623 isl_size
isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
625 return uset
? uset
->table
.n
: isl_size_error
;
628 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
629 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
631 struct isl_union_map_foreach_data data
= { fn
, user
};
634 return isl_stat_error
;
636 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
637 &call_on_copy
, &data
);
640 /* Internal data structure for isl_union_map_every_map.
642 * "test" is the user-specified callback function.
643 * "user" is the user-specified callback function argument.
645 * "failed" is initialized to 0 and set to 1 if "test" fails
648 struct isl_union_map_every_data
{
649 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
);
654 /* Call data->test on "map".
655 * If this fails, then set data->failed and abort.
657 static isl_stat
call_every(void **entry
, void *user
)
659 isl_map
*map
= *entry
;
660 struct isl_union_map_every_data
*data
= user
;
663 r
= data
->test(map
, data
->user
);
665 return isl_stat_error
;
669 return isl_stat_error
;
672 /* Does "test" succeed on every map in "umap"?
674 isl_bool
isl_union_map_every_map(__isl_keep isl_union_map
*umap
,
675 isl_bool (*test
)(__isl_keep isl_map
*map
, void *user
), void *user
)
677 struct isl_union_map_every_data data
= { test
, user
, 0 };
681 return isl_bool_error
;
683 r
= isl_hash_table_foreach(isl_union_map_get_ctx(umap
), &umap
->table
,
686 return isl_bool_true
;
688 return isl_bool_false
;
689 return isl_bool_error
;
692 /* Add "map" to "list".
694 static isl_stat
add_list_map(__isl_take isl_map
*map
, void *user
)
696 isl_map_list
**list
= user
;
698 *list
= isl_map_list_add(*list
, map
);
701 return isl_stat_error
;
705 /* Return the maps in "umap" as a list.
707 * First construct a list of the appropriate size and then add all the
710 __isl_give isl_map_list
*isl_union_map_get_map_list(
711 __isl_keep isl_union_map
*umap
)
717 n_maps
= isl_union_map_n_map(umap
);
720 ctx
= isl_union_map_get_ctx(umap
);
721 list
= isl_map_list_alloc(ctx
, n_maps
);
723 if (isl_union_map_foreach_map(umap
, &add_list_map
, &list
) < 0)
724 list
= isl_map_list_free(list
);
729 /* Return the sets in "uset" as a list.
731 __isl_give isl_set_list
*isl_union_set_get_set_list(
732 __isl_keep isl_union_set
*uset
)
734 return set_list_from_map_list(
735 isl_union_map_get_map_list(uset_to_umap(uset
)));
738 /* Can "umap" be converted to an isl_map?
739 * That is, does it contain elements in exactly one space?
741 isl_bool
isl_union_map_isa_map(__isl_keep isl_union_map
*umap
)
745 n
= isl_union_map_n_map(umap
);
747 return isl_bool_error
;
748 return isl_bool_ok(n
== 1);
751 /* Can "uset" be converted to an isl_set?
752 * That is, does it contain elements in exactly one space?
754 isl_bool
isl_union_set_isa_set(__isl_keep isl_union_set
*uset
)
756 return isl_union_map_isa_map(uset_to_umap(uset
));
759 static isl_stat
copy_map(void **entry
, void *user
)
761 isl_map
*map
= *entry
;
762 isl_map
**map_p
= user
;
764 *map_p
= isl_map_copy(map
);
766 return isl_stat_error
;
769 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
775 is_map
= isl_union_map_isa_map(umap
);
778 ctx
= isl_union_map_get_ctx(umap
);
780 isl_die(ctx
, isl_error_invalid
,
781 "union map needs to contain elements in exactly "
782 "one space", goto error
);
784 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
786 isl_union_map_free(umap
);
790 isl_union_map_free(umap
);
794 /* This function performs the same operation as isl_map_from_union_map,
795 * but is considered as a function on an isl_union_map when exported.
797 __isl_give isl_map
*isl_union_map_as_map(__isl_take isl_union_map
*umap
)
799 return isl_map_from_union_map(umap
);
802 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
804 return isl_map_from_union_map(uset
);
807 /* This function performs the same operation as isl_set_from_union_set,
808 * but is considered as a function on an isl_union_set when exported.
810 __isl_give isl_set
*isl_union_set_as_set(__isl_take isl_union_set
*uset
)
812 return isl_set_from_union_set(uset
);
815 /* Extract the map in "umap" that lives in the given space (ignoring
818 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
819 __isl_take isl_space
*space
)
821 struct isl_hash_table_entry
*entry
;
823 entry
= isl_union_map_find_entry(umap
, space
, 0);
826 if (entry
== isl_hash_table_entry_none
)
827 return isl_map_empty(space
);
828 isl_space_free(space
);
829 return isl_map_copy(entry
->data
);
831 isl_space_free(space
);
835 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
836 __isl_take isl_space
*space
)
838 return set_from_map(isl_union_map_extract_map(uset
, space
));
841 /* Check if umap contains a map in the given space (ignoring parameters).
843 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
844 __isl_keep isl_space
*space
)
846 struct isl_hash_table_entry
*entry
;
848 space
= isl_space_drop_all_params(isl_space_copy(space
));
849 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
850 entry
= isl_union_map_find_entry(umap
, space
, 0);
851 isl_space_free(space
);
853 return isl_bool_error
;
854 return isl_bool_ok(entry
!= isl_hash_table_entry_none
);
857 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
858 __isl_keep isl_space
*space
)
860 return isl_union_map_contains(uset
, space
);
863 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
864 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
866 return isl_union_map_foreach_map(uset
,
867 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
870 /* Internal data structure for isl_union_set_every_set.
872 * "test" is the user-specified callback function.
873 * "user" is the user-specified callback function argument.
875 struct isl_test_set_from_map_data
{
876 isl_bool (*test
)(__isl_keep isl_set
*set
, void *user
);
880 /* Call data->test on "map", which is part of an isl_union_set and
881 * therefore known to be an isl_set.
883 static isl_bool
test_set_from_map(__isl_keep isl_map
*map
, void *user
)
885 struct isl_test_set_from_map_data
*data
= user
;
887 return data
->test(set_from_map(map
), data
->user
);
890 /* Does "test" succeed on every set in "uset"?
892 isl_bool
isl_union_set_every_set(__isl_keep isl_union_set
*uset
,
893 isl_bool (*test
)(__isl_keep isl_set
*set
, void *user
), void *user
)
895 struct isl_test_set_from_map_data data
= { test
, user
};
897 return isl_union_map_every_map(uset_to_umap(uset
),
898 &test_set_from_map
, &data
);
901 struct isl_union_set_foreach_point_data
{
902 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
906 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
908 struct isl_union_set_foreach_point_data
*data
= user
;
911 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
917 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
918 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
920 struct isl_union_set_foreach_point_data data
= { fn
, user
};
921 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
924 /* Data structure that specifies how gen_bin_op should
925 * construct results from the inputs.
927 * If "subtract" is set, then a map in the first input is copied to the result
928 * if there is no corresponding map in the second input.
929 * Otherwise, a map in the first input with no corresponding map
930 * in the second input is ignored.
931 * If "filter" is not NULL, then it specifies which maps in the first
932 * input may have a matching map in the second input.
933 * In particular, it makes sure that "match_space" can be called
934 * on the space of the map.
935 * "match_space" specifies how to transform the space of a map
936 * in the first input to the space of the corresponding map
937 * in the second input.
938 * "fn_map" specifies how the matching maps, one from each input,
939 * should be combined to form a map in the result.
941 struct isl_bin_op_control
{
943 isl_bool (*filter
)(__isl_keep isl_map
*map
);
944 __isl_give isl_space
*(*match_space
)(__isl_take isl_space
*space
);
945 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map1
,
946 __isl_take isl_map
*map2
);
949 /* Internal data structure for gen_bin_op.
950 * "control" specifies how the maps in the result should be constructed.
951 * "umap2" is a pointer to the second argument.
952 * "res" collects the results.
954 struct isl_union_map_gen_bin_data
{
955 struct isl_bin_op_control
*control
;
956 isl_union_map
*umap2
;
960 /* Add a copy of "map" to "res" and return the result.
962 static __isl_give isl_union_map
*bin_add_map(__isl_take isl_union_map
*res
,
963 __isl_keep isl_map
*map
)
965 return isl_union_map_add_map(res
, isl_map_copy(map
));
968 /* Combine "map1" and "map2", add the result to "res" and return the result.
969 * Check whether the result is empty before adding it to "res".
971 static __isl_give isl_union_map
*bin_add_pair(__isl_take isl_union_map
*res
,
972 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
973 struct isl_union_map_gen_bin_data
*data
)
978 map
= data
->control
->fn_map(isl_map_copy(map1
), isl_map_copy(map2
));
979 empty
= isl_map_is_empty(map
);
980 if (empty
< 0 || empty
) {
983 return isl_union_map_free(res
);
986 return isl_union_map_add_map(res
, map
);
989 /* Dummy match_space function that simply returns the input space.
991 static __isl_give isl_space
*identity(__isl_take isl_space
*space
)
996 /* Look for the map in data->umap2 that corresponds to "map", if any.
997 * Return (isl_bool_true, matching map) if there is one,
998 * (isl_bool_false, NULL) if there is no matching map and
999 * (isl_bool_error, NULL) on error.
1001 * If not NULL, then data->control->filter specifies whether "map"
1002 * can have any matching map. If so,
1003 * data->control->match_space specifies which map in data->umap2
1004 * corresponds to "map".
1006 static __isl_keep isl_maybe_isl_map
bin_try_get_match(
1007 struct isl_union_map_gen_bin_data
*data
, __isl_keep isl_map
*map
)
1009 struct isl_hash_table_entry
*entry2
;
1011 isl_maybe_isl_map res
= { isl_bool_error
, NULL
};
1013 if (data
->control
->filter
) {
1014 res
.valid
= data
->control
->filter(map
);
1015 if (res
.valid
< 0 || !res
.valid
)
1017 res
.valid
= isl_bool_error
;
1020 space
= isl_map_get_space(map
);
1021 if (data
->control
->match_space
!= &identity
)
1022 space
= data
->control
->match_space(space
);
1023 entry2
= isl_union_map_find_entry(data
->umap2
, space
, 0);
1024 isl_space_free(space
);
1026 res
.valid
= isl_bool_ok(entry2
!= isl_hash_table_entry_none
);
1027 if (res
.valid
>= 0 && res
.valid
)
1028 res
.value
= entry2
->data
;
1033 /* isl_hash_table_foreach callback for gen_bin_op.
1034 * Look for the map in data->umap2 that corresponds
1035 * to the map that "entry" points to, apply the binary operation and
1036 * add the result to data->res.
1038 * If no corresponding map can be found, then the effect depends
1039 * on data->control->subtract. If it is set, then the current map
1040 * is added directly to the result. Otherwise, it is ignored.
1042 static isl_stat
gen_bin_entry(void **entry
, void *user
)
1044 struct isl_union_map_gen_bin_data
*data
= user
;
1045 isl_map
*map
= *entry
;
1046 isl_maybe_isl_map m
;
1048 m
= bin_try_get_match(data
, map
);
1050 return isl_stat_error
;
1051 if (!m
.valid
&& !data
->control
->subtract
)
1055 data
->res
= bin_add_map(data
->res
, map
);
1057 data
->res
= bin_add_pair(data
->res
, map
, m
.value
, data
);
1059 return isl_stat_error
;
1064 /* Apply a binary operation to "umap1" and "umap2" based on "control".
1065 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
1068 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
1069 __isl_take isl_union_map
*umap2
, struct isl_bin_op_control
*control
)
1071 struct isl_union_map_gen_bin_data data
= { control
, NULL
, NULL
};
1073 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1074 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1076 if (!umap1
|| !umap2
)
1080 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1082 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1083 &gen_bin_entry
, &data
) < 0)
1086 isl_union_map_free(umap1
);
1087 isl_union_map_free(umap2
);
1090 isl_union_map_free(umap1
);
1091 isl_union_map_free(umap2
);
1092 isl_union_map_free(data
.res
);
1096 __isl_give isl_union_map
*isl_union_map_subtract(
1097 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1099 struct isl_bin_op_control control
= {
1101 .match_space
= &identity
,
1102 .fn_map
= &isl_map_subtract
,
1105 return gen_bin_op(umap1
, umap2
, &control
);
1108 __isl_give isl_union_set
*isl_union_set_subtract(
1109 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1111 return isl_union_map_subtract(uset1
, uset2
);
1114 struct isl_union_map_gen_bin_set_data
{
1119 static isl_stat
intersect_params_entry(void **entry
, void *user
)
1121 struct isl_union_map_gen_bin_set_data
*data
= user
;
1122 isl_map
*map
= *entry
;
1125 map
= isl_map_copy(map
);
1126 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
1128 empty
= isl_map_is_empty(map
);
1131 return isl_stat_error
;
1134 data
->res
= isl_union_map_add_map(data
->res
, map
);
1139 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
1140 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
1142 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
1144 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
1145 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
1151 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
1153 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
1157 isl_union_map_free(umap
);
1161 isl_union_map_free(umap
);
1163 isl_union_map_free(data
.res
);
1167 /* Intersect "umap" with the parameter domain "set".
1169 * If "set" does not have any constraints, then we can return immediately.
1171 __isl_give isl_union_map
*isl_union_map_intersect_params(
1172 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1176 is_universe
= isl_set_plain_is_universe(set
);
1177 if (is_universe
< 0)
1184 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
1186 isl_union_map_free(umap
);
1191 __isl_give isl_union_set
*isl_union_set_intersect_params(
1192 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1194 return isl_union_map_intersect_params(uset
, set
);
1197 static __isl_give isl_union_map
*union_map_intersect_params(
1198 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1200 return isl_union_map_intersect_params(umap
,
1201 isl_set_from_union_set(uset
));
1204 static __isl_give isl_union_map
*union_map_gist_params(
1205 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1207 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
1210 struct isl_union_map_match_bin_data
{
1211 isl_union_map
*umap2
;
1213 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
1216 static isl_stat
match_bin_entry(void **entry
, void *user
)
1218 struct isl_union_map_match_bin_data
*data
= user
;
1219 struct isl_hash_table_entry
*entry2
;
1221 isl_map
*map
= *entry
;
1224 space
= isl_map_peek_space(map
);
1225 entry2
= isl_union_map_find_entry(data
->umap2
, space
, 0);
1227 return isl_stat_error
;
1228 if (entry2
== isl_hash_table_entry_none
)
1231 map
= isl_map_copy(map
);
1232 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
1234 empty
= isl_map_is_empty(map
);
1237 return isl_stat_error
;
1244 data
->res
= isl_union_map_add_map(data
->res
, map
);
1249 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
1250 __isl_take isl_union_map
*umap2
,
1251 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
1253 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
1255 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1256 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1258 if (!umap1
|| !umap2
)
1262 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1264 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1265 &match_bin_entry
, &data
) < 0)
1268 isl_union_map_free(umap1
);
1269 isl_union_map_free(umap2
);
1272 isl_union_map_free(umap1
);
1273 isl_union_map_free(umap2
);
1274 isl_union_map_free(data
.res
);
1278 __isl_give isl_union_map
*isl_union_map_intersect(
1279 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1281 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
1284 /* Compute the intersection of the two union_sets.
1285 * As a special case, if exactly one of the two union_sets
1286 * is a parameter domain, then intersect the parameter domain
1287 * of the other one with this set.
1289 __isl_give isl_union_set
*isl_union_set_intersect(
1290 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1294 p1
= isl_union_set_is_params(uset1
);
1295 p2
= isl_union_set_is_params(uset2
);
1296 if (p1
< 0 || p2
< 0)
1299 return union_map_intersect_params(uset1
, uset2
);
1301 return union_map_intersect_params(uset2
, uset1
);
1302 return isl_union_map_intersect(uset1
, uset2
);
1304 isl_union_set_free(uset1
);
1305 isl_union_set_free(uset2
);
1309 static isl_stat
gist_params_entry(void **entry
, void *user
)
1311 struct isl_union_map_gen_bin_set_data
*data
= user
;
1312 isl_map
*map
= *entry
;
1315 map
= isl_map_copy(map
);
1316 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
1318 empty
= isl_map_is_empty(map
);
1321 return isl_stat_error
;
1324 data
->res
= isl_union_map_add_map(data
->res
, map
);
1329 __isl_give isl_union_map
*isl_union_map_gist_params(
1330 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1332 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
1335 __isl_give isl_union_set
*isl_union_set_gist_params(
1336 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1338 return isl_union_map_gist_params(uset
, set
);
1341 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
1342 __isl_take isl_union_map
*context
)
1344 return match_bin_op(umap
, context
, &isl_map_gist
);
1347 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
1348 __isl_take isl_union_set
*context
)
1350 if (isl_union_set_is_params(context
))
1351 return union_map_gist_params(uset
, context
);
1352 return isl_union_map_gist(uset
, context
);
1355 /* For each map in "umap", remove the constraints in the corresponding map
1357 * Each map in "context" is assumed to consist of a single disjunct and
1358 * to have explicit representations for all local variables.
1360 __isl_give isl_union_map
*isl_union_map_plain_gist(
1361 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*context
)
1363 return match_bin_op(umap
, context
, &isl_map_plain_gist
);
1366 /* For each set in "uset", remove the constraints in the corresponding set
1368 * Each set in "context" is assumed to consist of a single disjunct and
1369 * to have explicit representations for all local variables.
1371 __isl_give isl_union_set
*isl_union_set_plain_gist(
1372 __isl_take isl_union_set
*uset
, __isl_take isl_union_set
*context
)
1374 return isl_union_map_plain_gist(uset
, context
);
1377 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
1378 __isl_take isl_map
*set2
)
1380 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
1383 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
1384 __isl_take isl_map
*set2
)
1386 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
1389 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
1390 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1392 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
1395 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
1396 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1398 return match_bin_op(uset1
, uset2
, &lex_le_set
);
1401 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
1402 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1404 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
1407 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
1408 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1410 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
1413 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
1414 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1416 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
1419 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
1420 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1422 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
1425 /* Intersect the domain of "umap" with "uset".
1427 static __isl_give isl_union_map
*union_map_intersect_domain(
1428 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1430 struct isl_bin_op_control control
= {
1431 .match_space
= &isl_space_domain
,
1432 .fn_map
= &isl_map_intersect_domain
,
1435 return gen_bin_op(umap
, uset
, &control
);
1438 /* Intersect the domain of "umap" with "uset".
1439 * If "uset" is a parameters domain, then intersect the parameter
1440 * domain of "umap" with this set.
1442 __isl_give isl_union_map
*isl_union_map_intersect_domain_union_set(
1443 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1445 if (isl_union_set_is_params(uset
))
1446 return union_map_intersect_params(umap
, uset
);
1448 return union_map_intersect_domain(umap
, uset
);
1451 /* This is an alternative name for the function above.
1453 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1454 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1456 return isl_union_map_intersect_domain_union_set(umap
, uset
);
1459 /* Remove the elements of "uset" from the domain of "umap".
1461 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1462 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1464 struct isl_bin_op_control control
= {
1466 .match_space
= &isl_space_domain
,
1467 .fn_map
= &isl_map_subtract_domain
,
1470 return gen_bin_op(umap
, dom
, &control
);
1473 /* Remove the elements of "uset" from the range of "umap".
1475 __isl_give isl_union_map
*isl_union_map_subtract_range(
1476 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1478 struct isl_bin_op_control control
= {
1480 .match_space
= &isl_space_range
,
1481 .fn_map
= &isl_map_subtract_range
,
1484 return gen_bin_op(umap
, dom
, &control
);
1487 /* Compute the gist of "umap" with respect to the domain "uset".
1489 static __isl_give isl_union_map
*union_map_gist_domain(
1490 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1492 struct isl_bin_op_control control
= {
1493 .match_space
= &isl_space_domain
,
1494 .fn_map
= &isl_map_gist_domain
,
1497 return gen_bin_op(umap
, uset
, &control
);
1500 /* Compute the gist of "umap" with respect to the domain "uset".
1501 * If "uset" is a parameters domain, then compute the gist
1502 * with respect to this parameter domain.
1504 __isl_give isl_union_map
*isl_union_map_gist_domain(
1505 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1507 if (isl_union_set_is_params(uset
))
1508 return union_map_gist_params(umap
, uset
);
1510 return union_map_gist_domain(umap
, uset
);
1513 /* Compute the gist of "umap" with respect to the range "uset".
1515 __isl_give isl_union_map
*isl_union_map_gist_range(
1516 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1518 struct isl_bin_op_control control
= {
1519 .match_space
= &isl_space_range
,
1520 .fn_map
= &isl_map_gist_range
,
1523 return gen_bin_op(umap
, uset
, &control
);
1526 __isl_give isl_union_map
*isl_union_map_intersect_range_union_set(
1527 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1529 struct isl_bin_op_control control
= {
1530 .match_space
= &isl_space_range
,
1531 .fn_map
= &isl_map_intersect_range
,
1534 return gen_bin_op(umap
, uset
, &control
);
1537 /* This is an alternative name for the function above.
1539 __isl_give isl_union_map
*isl_union_map_intersect_range(
1540 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1542 return isl_union_map_intersect_range_union_set(umap
, uset
);
1545 /* Intersect each map in "umap" in a space [A -> B] -> C
1546 * with the corresponding map in "factor" in the space A -> C and
1547 * collect the results.
1549 __isl_give isl_union_map
*isl_union_map_intersect_domain_factor_domain(
1550 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1552 struct isl_bin_op_control control
= {
1553 .filter
= &isl_map_domain_is_wrapping
,
1554 .match_space
= &isl_space_domain_factor_domain
,
1555 .fn_map
= &isl_map_intersect_domain_factor_domain
,
1558 return gen_bin_op(umap
, factor
, &control
);
1561 /* Intersect each map in "umap" in a space [A -> B] -> C
1562 * with the corresponding map in "factor" in the space B -> C and
1563 * collect the results.
1565 __isl_give isl_union_map
*isl_union_map_intersect_domain_factor_range(
1566 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1568 struct isl_bin_op_control control
= {
1569 .filter
= &isl_map_domain_is_wrapping
,
1570 .match_space
= &isl_space_domain_factor_range
,
1571 .fn_map
= &isl_map_intersect_domain_factor_range
,
1574 return gen_bin_op(umap
, factor
, &control
);
1577 /* Intersect each map in "umap" in a space A -> [B -> C]
1578 * with the corresponding map in "factor" in the space A -> B and
1579 * collect the results.
1581 __isl_give isl_union_map
*isl_union_map_intersect_range_factor_domain(
1582 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1584 struct isl_bin_op_control control
= {
1585 .filter
= &isl_map_range_is_wrapping
,
1586 .match_space
= &isl_space_range_factor_domain
,
1587 .fn_map
= &isl_map_intersect_range_factor_domain
,
1590 return gen_bin_op(umap
, factor
, &control
);
1593 /* Intersect each map in "umap" in a space A -> [B -> C]
1594 * with the corresponding map in "factor" in the space A -> C and
1595 * collect the results.
1597 __isl_give isl_union_map
*isl_union_map_intersect_range_factor_range(
1598 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*factor
)
1600 struct isl_bin_op_control control
= {
1601 .filter
= &isl_map_range_is_wrapping
,
1602 .match_space
= &isl_space_range_factor_range
,
1603 .fn_map
= &isl_map_intersect_range_factor_range
,
1606 return gen_bin_op(umap
, factor
, &control
);
1609 struct isl_union_map_bin_data
{
1610 isl_union_map
*umap2
;
1613 isl_stat (*fn
)(void **entry
, void *user
);
1616 static isl_stat
apply_range_entry(void **entry
, void *user
)
1618 struct isl_union_map_bin_data
*data
= user
;
1619 isl_map
*map2
= *entry
;
1620 isl_bool empty
, match
;
1622 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_out
,
1625 return isl_stat_error
;
1629 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1631 empty
= isl_map_is_empty(map2
);
1634 return isl_stat_error
;
1641 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1646 static isl_stat
bin_entry(void **entry
, void *user
)
1648 struct isl_union_map_bin_data
*data
= user
;
1649 isl_map
*map
= *entry
;
1652 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1653 data
->fn
, data
) < 0)
1654 return isl_stat_error
;
1659 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1660 __isl_take isl_union_map
*umap2
,
1661 isl_stat (*fn
)(void **entry
, void *user
))
1663 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1665 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1666 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1668 if (!umap1
|| !umap2
)
1672 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1674 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1675 &bin_entry
, &data
) < 0)
1678 isl_union_map_free(umap1
);
1679 isl_union_map_free(umap2
);
1682 isl_union_map_free(umap1
);
1683 isl_union_map_free(umap2
);
1684 isl_union_map_free(data
.res
);
1688 /* Intersect each map in "umap" in a space [A -> B] -> C
1689 * with the corresponding set in "domain" in the space A and
1690 * collect the results.
1692 __isl_give isl_union_map
*
1693 isl_union_map_intersect_domain_wrapped_domain_union_set(
1694 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*domain
)
1696 struct isl_bin_op_control control
= {
1697 .filter
= &isl_map_domain_is_wrapping
,
1698 .match_space
= &isl_space_domain_wrapped_domain
,
1699 .fn_map
= &isl_map_intersect_domain_wrapped_domain
,
1702 return gen_bin_op(umap
, domain
, &control
);
1705 /* Intersect each map in "umap" in a space A -> [B -> C]
1706 * with the corresponding set in "domain" in the space B and
1707 * collect the results.
1709 __isl_give isl_union_map
*
1710 isl_union_map_intersect_range_wrapped_domain_union_set(
1711 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*domain
)
1713 struct isl_bin_op_control control
= {
1714 .filter
= &isl_map_range_is_wrapping
,
1715 .match_space
= &isl_space_range_wrapped_domain
,
1716 .fn_map
= &isl_map_intersect_range_wrapped_domain
,
1719 return gen_bin_op(umap
, domain
, &control
);
1722 __isl_give isl_union_map
*isl_union_map_apply_range(
1723 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1725 return bin_op(umap1
, umap2
, &apply_range_entry
);
1728 __isl_give isl_union_map
*isl_union_map_apply_domain(
1729 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1731 umap1
= isl_union_map_reverse(umap1
);
1732 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1733 return isl_union_map_reverse(umap1
);
1736 __isl_give isl_union_set
*isl_union_set_apply(
1737 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1739 return isl_union_map_apply_range(uset
, umap
);
1742 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1744 struct isl_union_map_bin_data
*data
= user
;
1745 isl_map
*map2
= *entry
;
1748 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_out
,
1751 return isl_stat_error
;
1755 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1757 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1762 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1763 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1765 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1768 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1770 struct isl_union_map_bin_data
*data
= user
;
1771 isl_map
*map2
= *entry
;
1774 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_out
,
1777 return isl_stat_error
;
1781 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1783 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1788 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1789 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1791 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1794 static isl_stat
product_entry(void **entry
, void *user
)
1796 struct isl_union_map_bin_data
*data
= user
;
1797 isl_map
*map2
= *entry
;
1799 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1801 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1806 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1807 __isl_take isl_union_map
*umap2
)
1809 return bin_op(umap1
, umap2
, &product_entry
);
1812 static isl_stat
set_product_entry(void **entry
, void *user
)
1814 struct isl_union_map_bin_data
*data
= user
;
1815 isl_set
*set2
= *entry
;
1817 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1819 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1824 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1825 __isl_take isl_union_set
*uset2
)
1827 return bin_op(uset1
, uset2
, &set_product_entry
);
1830 static isl_stat
domain_product_entry(void **entry
, void *user
)
1832 struct isl_union_map_bin_data
*data
= user
;
1833 isl_map
*map2
= *entry
;
1836 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_out
,
1839 return isl_stat_error
;
1843 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1844 isl_map_copy(map2
));
1846 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1851 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1853 __isl_give isl_union_map
*isl_union_map_domain_product(
1854 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1856 return bin_op(umap1
, umap2
, &domain_product_entry
);
1859 static isl_stat
range_product_entry(void **entry
, void *user
)
1861 struct isl_union_map_bin_data
*data
= user
;
1862 isl_map
*map2
= *entry
;
1865 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_in
, map2
, isl_dim_in
);
1867 return isl_stat_error
;
1871 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1872 isl_map_copy(map2
));
1874 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1879 __isl_give isl_union_map
*isl_union_map_range_product(
1880 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1882 return bin_op(umap1
, umap2
, &range_product_entry
);
1885 /* If data->map A -> B and "map2" C -> D have the same range space,
1886 * then add (A, C) -> (B * D) to data->res.
1888 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1890 struct isl_union_map_bin_data
*data
= user
;
1891 isl_map
*map2
= *entry
;
1894 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_out
,
1897 return isl_stat_error
;
1901 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1902 isl_map_copy(map2
));
1904 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1909 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1911 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1912 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1914 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1917 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1919 struct isl_union_map_bin_data
*data
= user
;
1920 isl_map
*map2
= *entry
;
1923 match
= isl_map_tuple_is_equal(data
->map
, isl_dim_in
, map2
, isl_dim_in
);
1925 return isl_stat_error
;
1929 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1930 isl_map_copy(map2
));
1932 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1937 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1938 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1940 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1943 /* Data structure that specifies how un_op should modify
1944 * the maps in the union map.
1946 * If "inplace" is set, then the maps in the input union map
1947 * are modified in place. This means that "fn_map" should not
1948 * change the meaning of the map or that the union map only
1949 * has a single reference.
1950 * If "total" is set, then all maps need to be modified and
1951 * the results need to live in the same space.
1952 * Otherwise, a new union map is constructed to store the results.
1953 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1954 * are taken into account. "filter_user" is passed as the second argument
1955 * to "filter". No filter can be set if "inplace" or
1957 * At most one of "fn_map" or "fn_map2" can be set, specifying
1958 * how the maps (selected by "filter") should be transformed.
1959 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1961 struct isl_un_op_control
{
1964 isl_bool (*filter
)(__isl_keep isl_map
*map
, void *user
);
1966 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map
);
1967 __isl_give isl_map
*(*fn_map2
)(__isl_take isl_map
*map
, void *user
);
1971 /* Data structure for wrapping the data for un_op_filter_drop_user.
1972 * "filter" is the function that is being wrapped.
1974 struct isl_un_op_drop_user_data
{
1975 isl_bool (*filter
)(__isl_keep isl_map
*map
);
1978 /* Wrapper for isl_un_op_control filters that do not require
1979 * a second argument.
1980 * Simply call data->filter without the second argument.
1982 static isl_bool
un_op_filter_drop_user(__isl_keep isl_map
*map
, void *user
)
1984 struct isl_un_op_drop_user_data
*data
= user
;
1985 return data
->filter(map
);
1988 /* Internal data structure for "un_op".
1989 * "control" specifies how the maps in the union map should be modified.
1990 * "res" collects the results.
1992 struct isl_union_map_un_data
{
1993 struct isl_un_op_control
*control
;
1997 /* isl_hash_table_foreach callback for un_op.
1998 * Handle the map that "entry" points to.
2000 * If control->filter is set, then check if this map satisfies the filter.
2001 * If so (or if control->filter is not set), modify the map
2002 * by calling control->fn_map or control->fn_map2 (if set) and
2003 * either add the result to data->res or
2004 * replace the original entry by the result (if control->inplace is set).
2006 static isl_stat
un_entry(void **entry
, void *user
)
2008 struct isl_union_map_un_data
*data
= user
;
2009 struct isl_un_op_control
*control
= data
->control
;
2010 isl_map
*map
= *entry
;
2012 if (control
->filter
) {
2015 ok
= control
->filter(map
, control
->filter_user
);
2017 return isl_stat_error
;
2022 map
= isl_map_copy(map
);
2023 if (control
->fn_map2
!= NULL
)
2024 map
= control
->fn_map2(map
, control
->fn_map2_user
);
2025 else if (control
->fn_map
!= NULL
)
2026 map
= control
->fn_map(map
);
2028 return isl_stat_error
;
2029 if (control
->inplace
) {
2030 isl_map_free(*entry
);
2033 data
->res
= isl_union_map_add_map(data
->res
, map
);
2035 return isl_stat_error
;
2041 /* Modify the maps in "umap" based on "control".
2042 * If control->inplace is set, then modify the maps in "umap" in-place.
2043 * Otherwise, create a new union map to hold the results.
2044 * If control->total is set, then perform an inplace computation
2045 * if "umap" is only referenced once. Otherwise, create a new union map
2046 * to store the results.
2048 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
2049 struct isl_un_op_control
*control
)
2051 struct isl_union_map_un_data data
= { control
};
2055 if (!!control
->fn_map
&& !!control
->fn_map2
)
2056 isl_die(isl_union_map_get_ctx(umap
), isl_error_internal
,
2057 "at most one mapping function can be specified",
2058 return isl_union_map_free(umap
));
2059 if ((control
->inplace
|| control
->total
) && control
->filter
)
2060 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
2061 "inplace/total modification cannot be filtered",
2062 return isl_union_map_free(umap
));
2064 if (control
->total
&& umap
->ref
== 1)
2065 control
->inplace
= 1;
2066 if (control
->inplace
) {
2071 space
= isl_union_map_get_space(umap
);
2072 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
2074 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
2075 &umap
->table
, &un_entry
, &data
) < 0)
2076 data
.res
= isl_union_map_free(data
.res
);
2078 if (control
->inplace
)
2080 isl_union_map_free(umap
);
2084 __isl_give isl_union_map
*isl_union_map_from_range(
2085 __isl_take isl_union_set
*uset
)
2087 struct isl_un_op_control control
= {
2088 .fn_map
= &isl_map_from_range
,
2090 return un_op(uset
, &control
);
2093 __isl_give isl_union_map
*isl_union_map_from_domain(
2094 __isl_take isl_union_set
*uset
)
2096 return isl_union_map_reverse(isl_union_map_from_range(uset
));
2099 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
2100 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
2102 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
2103 isl_union_map_from_range(range
));
2106 /* Modify the maps in "umap" by applying "fn" on them.
2107 * "fn" should apply to all maps in "umap" and should not modify the space.
2109 static __isl_give isl_union_map
*total(__isl_take isl_union_map
*umap
,
2110 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
2112 struct isl_un_op_control control
= {
2117 return un_op(umap
, &control
);
2120 /* Compute the affine hull of "map" and return the result as an isl_map.
2122 static __isl_give isl_map
*isl_map_affine_hull_map(__isl_take isl_map
*map
)
2124 return isl_map_from_basic_map(isl_map_affine_hull(map
));
2127 __isl_give isl_union_map
*isl_union_map_affine_hull(
2128 __isl_take isl_union_map
*umap
)
2130 return total(umap
, &isl_map_affine_hull_map
);
2133 __isl_give isl_union_set
*isl_union_set_affine_hull(
2134 __isl_take isl_union_set
*uset
)
2136 return isl_union_map_affine_hull(uset
);
2139 /* Wrapper around isl_set_combined_lineality_space
2140 * that returns the combined lineality space in the form of an isl_set
2141 * instead of an isl_basic_set.
2143 static __isl_give isl_set
*combined_lineality_space(__isl_take isl_set
*set
)
2145 return isl_set_from_basic_set(isl_set_combined_lineality_space(set
));
2148 /* For each set in "uset", compute the (linear) hull
2149 * of the lineality spaces of its basic sets and
2150 * collect and return the results.
2152 __isl_give isl_union_set
*isl_union_set_combined_lineality_space(
2153 __isl_take isl_union_set
*uset
)
2155 struct isl_un_op_control control
= {
2156 .fn_map
= &combined_lineality_space
,
2158 return un_op(uset
, &control
);
2161 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
2163 static __isl_give isl_map
*isl_map_polyhedral_hull_map(__isl_take isl_map
*map
)
2165 return isl_map_from_basic_map(isl_map_polyhedral_hull(map
));
2168 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
2169 __isl_take isl_union_map
*umap
)
2171 return total(umap
, &isl_map_polyhedral_hull_map
);
2174 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
2175 __isl_take isl_union_set
*uset
)
2177 return isl_union_map_polyhedral_hull(uset
);
2180 /* Compute a superset of the convex hull of "map" that is described
2181 * by only translates of the constraints in the constituents of "map" and
2182 * return the result as an isl_map.
2184 static __isl_give isl_map
*isl_map_simple_hull_map(__isl_take isl_map
*map
)
2186 return isl_map_from_basic_map(isl_map_simple_hull(map
));
2189 __isl_give isl_union_map
*isl_union_map_simple_hull(
2190 __isl_take isl_union_map
*umap
)
2192 return total(umap
, &isl_map_simple_hull_map
);
2195 __isl_give isl_union_set
*isl_union_set_simple_hull(
2196 __isl_take isl_union_set
*uset
)
2198 return isl_union_map_simple_hull(uset
);
2201 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
2202 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
2204 struct isl_un_op_control control
= {
2209 return un_op(umap
, &control
);
2212 /* Remove redundant constraints in each of the basic maps of "umap".
2213 * Since removing redundant constraints does not change the meaning
2214 * or the space, the operation can be performed in-place.
2216 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
2217 __isl_take isl_union_map
*umap
)
2219 return inplace(umap
, &isl_map_remove_redundancies
);
2222 /* Remove redundant constraints in each of the basic sets of "uset".
2224 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
2225 __isl_take isl_union_set
*uset
)
2227 return isl_union_map_remove_redundancies(uset
);
2230 __isl_give isl_union_map
*isl_union_map_coalesce(
2231 __isl_take isl_union_map
*umap
)
2233 return inplace(umap
, &isl_map_coalesce
);
2236 __isl_give isl_union_set
*isl_union_set_coalesce(
2237 __isl_take isl_union_set
*uset
)
2239 return isl_union_map_coalesce(uset
);
2242 __isl_give isl_union_map
*isl_union_map_detect_equalities(
2243 __isl_take isl_union_map
*umap
)
2245 return inplace(umap
, &isl_map_detect_equalities
);
2248 __isl_give isl_union_set
*isl_union_set_detect_equalities(
2249 __isl_take isl_union_set
*uset
)
2251 return isl_union_map_detect_equalities(uset
);
2254 __isl_give isl_union_map
*isl_union_map_compute_divs(
2255 __isl_take isl_union_map
*umap
)
2257 return inplace(umap
, &isl_map_compute_divs
);
2260 __isl_give isl_union_set
*isl_union_set_compute_divs(
2261 __isl_take isl_union_set
*uset
)
2263 return isl_union_map_compute_divs(uset
);
2266 __isl_give isl_union_map
*isl_union_map_lexmin(
2267 __isl_take isl_union_map
*umap
)
2269 return total(umap
, &isl_map_lexmin
);
2272 __isl_give isl_union_set
*isl_union_set_lexmin(
2273 __isl_take isl_union_set
*uset
)
2275 return isl_union_map_lexmin(uset
);
2278 __isl_give isl_union_map
*isl_union_map_lexmax(
2279 __isl_take isl_union_map
*umap
)
2281 return total(umap
, &isl_map_lexmax
);
2284 __isl_give isl_union_set
*isl_union_set_lexmax(
2285 __isl_take isl_union_set
*uset
)
2287 return isl_union_map_lexmax(uset
);
2290 /* Return the universe in the space of "map".
2292 static __isl_give isl_map
*universe(__isl_take isl_map
*map
)
2296 space
= isl_map_get_space(map
);
2298 return isl_map_universe(space
);
2301 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
2303 struct isl_un_op_control control
= {
2304 .fn_map
= &universe
,
2306 return un_op(umap
, &control
);
2309 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
2311 return isl_union_map_universe(uset
);
2314 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
2316 struct isl_un_op_control control
= {
2317 .fn_map
= &isl_map_reverse
,
2319 return un_op(umap
, &control
);
2322 /* Given a union map, take the maps of the form (A -> B) -> C and
2323 * return the union of the corresponding maps (B -> A) -> C.
2325 __isl_give isl_union_map
*isl_union_map_domain_reverse(
2326 __isl_take isl_union_map
*umap
)
2328 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2329 struct isl_un_op_control control
= {
2330 .filter
= &un_op_filter_drop_user
,
2331 .filter_user
= &data
,
2332 .fn_map
= &isl_map_domain_reverse
,
2334 return un_op(umap
, &control
);
2337 /* Given a union map, take the maps of the form A -> (B -> C) and
2338 * return the union of the corresponding maps A -> (C -> B).
2340 __isl_give isl_union_map
*isl_union_map_range_reverse(
2341 __isl_take isl_union_map
*umap
)
2343 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2344 struct isl_un_op_control control
= {
2345 .filter
= &un_op_filter_drop_user
,
2346 .filter_user
= &data
,
2347 .fn_map
= &isl_map_range_reverse
,
2349 return un_op(umap
, &control
);
2352 /* Compute the parameter domain of the given union map.
2354 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
2356 struct isl_un_op_control control
= {
2357 .fn_map
= &isl_map_params
,
2361 empty
= isl_union_map_is_empty(umap
);
2366 space
= isl_union_map_get_space(umap
);
2367 isl_union_map_free(umap
);
2368 return isl_set_empty(space
);
2370 return isl_set_from_union_set(un_op(umap
, &control
));
2372 isl_union_map_free(umap
);
2376 /* Compute the parameter domain of the given union set.
2378 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
2380 return isl_union_map_params(uset
);
2383 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
2385 struct isl_un_op_control control
= {
2386 .fn_map
= &isl_map_domain
,
2388 return un_op(umap
, &control
);
2391 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
2393 struct isl_un_op_control control
= {
2394 .fn_map
= &isl_map_range
,
2396 return un_op(umap
, &control
);
2399 __isl_give isl_union_map
*isl_union_map_domain_map(
2400 __isl_take isl_union_map
*umap
)
2402 struct isl_un_op_control control
= {
2403 .fn_map
= &isl_map_domain_map
,
2405 return un_op(umap
, &control
);
2408 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2409 * add the result to "res".
2411 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
2413 isl_union_pw_multi_aff
**res
= user
;
2415 isl_pw_multi_aff
*pma
;
2417 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
2418 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
2419 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2421 return *res
? isl_stat_ok
: isl_stat_error
;
2425 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2428 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
2429 __isl_take isl_union_map
*umap
)
2431 isl_union_pw_multi_aff
*res
;
2433 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
2434 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
2435 res
= isl_union_pw_multi_aff_free(res
);
2437 isl_union_map_free(umap
);
2441 __isl_give isl_union_map
*isl_union_map_range_map(
2442 __isl_take isl_union_map
*umap
)
2444 struct isl_un_op_control control
= {
2445 .fn_map
= &isl_map_range_map
,
2447 return un_op(umap
, &control
);
2450 /* Given a collection of wrapped maps of the form A[B -> C],
2451 * return the collection of maps A[B -> C] -> B.
2453 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
2454 __isl_take isl_union_set
*uset
)
2456 struct isl_un_op_drop_user_data data
= { &isl_set_is_wrapping
};
2457 struct isl_un_op_control control
= {
2458 .filter
= &un_op_filter_drop_user
,
2459 .filter_user
= &data
,
2460 .fn_map
= &isl_set_wrapped_domain_map
,
2462 return un_op(uset
, &control
);
2465 /* Does "map" relate elements from the same space?
2467 static isl_bool
equal_tuples(__isl_keep isl_map
*map
, void *user
)
2469 return isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
2472 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
2474 struct isl_un_op_control control
= {
2475 .filter
= &equal_tuples
,
2476 .fn_map
= &isl_map_deltas
,
2478 return un_op(umap
, &control
);
2481 __isl_give isl_union_map
*isl_union_map_deltas_map(
2482 __isl_take isl_union_map
*umap
)
2484 struct isl_un_op_control control
= {
2485 .filter
= &equal_tuples
,
2486 .fn_map
= &isl_map_deltas_map
,
2488 return un_op(umap
, &control
);
2491 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
2493 struct isl_un_op_control control
= {
2494 .fn_map
= &isl_set_identity
,
2496 return un_op(uset
, &control
);
2499 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2501 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
2503 isl_union_pw_multi_aff
**res
= user
;
2505 isl_pw_multi_aff
*pma
;
2507 space
= isl_space_map_from_set(isl_set_get_space(set
));
2508 pma
= isl_pw_multi_aff_identity(space
);
2509 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
2510 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
2512 return *res
? isl_stat_ok
: isl_stat_error
;
2515 /* Return an identity function on "uset" in the form
2516 * of an isl_union_pw_multi_aff.
2518 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
2519 __isl_take isl_union_set
*uset
)
2521 isl_union_pw_multi_aff
*res
;
2523 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
2524 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
2525 res
= isl_union_pw_multi_aff_free(res
);
2527 isl_union_set_free(uset
);
2531 /* For each map in "umap" of the form [A -> B] -> C,
2532 * construct the map A -> C and collect the results.
2534 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2535 __isl_take isl_union_map
*umap
)
2537 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2538 struct isl_un_op_control control
= {
2539 .filter
= &un_op_filter_drop_user
,
2540 .filter_user
= &data
,
2541 .fn_map
= &isl_map_domain_factor_domain
,
2543 return un_op(umap
, &control
);
2546 /* For each map in "umap" of the form [A -> B] -> C,
2547 * construct the map B -> C and collect the results.
2549 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2550 __isl_take isl_union_map
*umap
)
2552 struct isl_un_op_drop_user_data data
= { &isl_map_domain_is_wrapping
};
2553 struct isl_un_op_control control
= {
2554 .filter
= &un_op_filter_drop_user
,
2555 .filter_user
= &data
,
2556 .fn_map
= &isl_map_domain_factor_range
,
2558 return un_op(umap
, &control
);
2561 /* For each map in "umap" of the form A -> [B -> C],
2562 * construct the map A -> B and collect the results.
2564 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2565 __isl_take isl_union_map
*umap
)
2567 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2568 struct isl_un_op_control control
= {
2569 .filter
= &un_op_filter_drop_user
,
2570 .filter_user
= &data
,
2571 .fn_map
= &isl_map_range_factor_domain
,
2573 return un_op(umap
, &control
);
2576 /* For each map in "umap" of the form A -> [B -> C],
2577 * construct the map A -> C and collect the results.
2579 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2580 __isl_take isl_union_map
*umap
)
2582 struct isl_un_op_drop_user_data data
= { &isl_map_range_is_wrapping
};
2583 struct isl_un_op_control control
= {
2584 .filter
= &un_op_filter_drop_user
,
2585 .filter_user
= &data
,
2586 .fn_map
= &isl_map_range_factor_range
,
2588 return un_op(umap
, &control
);
2591 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2592 * construct the map A -> C and collect the results.
2594 __isl_give isl_union_map
*isl_union_map_factor_domain(
2595 __isl_take isl_union_map
*umap
)
2597 struct isl_un_op_drop_user_data data
= { &isl_map_is_product
};
2598 struct isl_un_op_control control
= {
2599 .filter
= &un_op_filter_drop_user
,
2600 .filter_user
= &data
,
2601 .fn_map
= &isl_map_factor_domain
,
2603 return un_op(umap
, &control
);
2606 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2607 * construct the map B -> D and collect the results.
2609 __isl_give isl_union_map
*isl_union_map_factor_range(
2610 __isl_take isl_union_map
*umap
)
2612 struct isl_un_op_drop_user_data data
= { &isl_map_is_product
};
2613 struct isl_un_op_control control
= {
2614 .filter
= &un_op_filter_drop_user
,
2615 .filter_user
= &data
,
2616 .fn_map
= &isl_map_factor_range
,
2618 return un_op(umap
, &control
);
2621 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2623 struct isl_un_op_drop_user_data data
= { &isl_set_is_wrapping
};
2624 struct isl_un_op_control control
= {
2625 .filter
= &un_op_filter_drop_user
,
2626 .filter_user
= &data
,
2627 .fn_map
= &isl_set_unwrap
,
2629 return un_op(uset
, &control
);
2632 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2634 struct isl_un_op_control control
= {
2635 .fn_map
= &isl_map_wrap
,
2637 return un_op(umap
, &control
);
2640 struct isl_union_map_is_subset_data
{
2641 isl_union_map
*umap2
;
2645 static isl_stat
is_subset_entry(void **entry
, void *user
)
2647 struct isl_union_map_is_subset_data
*data
= user
;
2648 struct isl_hash_table_entry
*entry2
;
2650 isl_map
*map
= *entry
;
2652 space
= isl_map_peek_space(map
);
2653 entry2
= isl_union_map_find_entry(data
->umap2
, space
, 0);
2655 return isl_stat_error
;
2656 if (entry2
== isl_hash_table_entry_none
) {
2657 int empty
= isl_map_is_empty(map
);
2659 return isl_stat_error
;
2662 data
->is_subset
= isl_bool_false
;
2663 return isl_stat_error
;
2666 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2667 if (data
->is_subset
< 0 || !data
->is_subset
)
2668 return isl_stat_error
;
2673 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2674 __isl_keep isl_union_map
*umap2
)
2676 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2678 if (!umap1
|| !umap2
)
2679 return isl_bool_error
;
2682 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2683 &is_subset_entry
, &data
) < 0 &&
2685 return isl_bool_error
;
2687 return data
.is_subset
;
2690 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2691 __isl_keep isl_union_set
*uset2
)
2693 return isl_union_map_is_subset(uset1
, uset2
);
2696 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2697 __isl_keep isl_union_map
*umap2
)
2701 if (!umap1
|| !umap2
)
2702 return isl_bool_error
;
2703 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2704 if (is_subset
!= isl_bool_true
)
2706 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2710 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2711 __isl_keep isl_union_set
*uset2
)
2713 return isl_union_map_is_equal(uset1
, uset2
);
2716 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2717 __isl_keep isl_union_map
*umap2
)
2721 if (!umap1
|| !umap2
)
2722 return isl_bool_error
;
2723 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2724 if (is_subset
!= isl_bool_true
)
2726 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2727 return isl_bool_not(is_subset
);
2730 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2731 __isl_keep isl_union_set
*uset2
)
2733 return isl_union_map_is_strict_subset(uset1
, uset2
);
2736 /* Internal data structure for isl_union_map_is_disjoint.
2737 * umap2 is the union map with which we are comparing.
2738 * is_disjoint is initialized to 1 and is set to 0 as soon
2739 * as the union maps turn out not to be disjoint.
2741 struct isl_union_map_is_disjoint_data
{
2742 isl_union_map
*umap2
;
2743 isl_bool is_disjoint
;
2746 /* Check if "map" is disjoint from data->umap2 and abort
2747 * the search if it is not.
2749 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2751 struct isl_union_map_is_disjoint_data
*data
= user
;
2752 struct isl_hash_table_entry
*entry2
;
2754 isl_map
*map
= *entry
;
2756 space
= isl_map_peek_space(map
);
2757 entry2
= isl_union_map_find_entry(data
->umap2
, space
, 0);
2759 return isl_stat_error
;
2760 if (entry2
== isl_hash_table_entry_none
)
2763 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2764 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2765 return isl_stat_error
;
2770 /* Are "umap1" and "umap2" disjoint?
2772 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2773 __isl_keep isl_union_map
*umap2
)
2775 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2777 umap1
= isl_union_map_copy(umap1
);
2778 umap2
= isl_union_map_copy(umap2
);
2779 umap1
= isl_union_map_align_params(umap1
,
2780 isl_union_map_get_space(umap2
));
2781 umap2
= isl_union_map_align_params(umap2
,
2782 isl_union_map_get_space(umap1
));
2784 if (!umap1
|| !umap2
)
2788 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2789 &is_disjoint_entry
, &data
) < 0 &&
2793 isl_union_map_free(umap1
);
2794 isl_union_map_free(umap2
);
2796 return data
.is_disjoint
;
2798 isl_union_map_free(umap1
);
2799 isl_union_map_free(umap2
);
2800 return isl_bool_error
;
2803 /* Are "uset1" and "uset2" disjoint?
2805 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2806 __isl_keep isl_union_set
*uset2
)
2808 return isl_union_map_is_disjoint(uset1
, uset2
);
2811 static isl_stat
sample_entry(void **entry
, void *user
)
2813 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2814 isl_map
*map
= *entry
;
2816 *sample
= isl_map_sample(isl_map_copy(map
));
2818 return isl_stat_error
;
2819 if (!isl_basic_map_plain_is_empty(*sample
))
2820 return isl_stat_error
;
2824 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2826 isl_basic_map
*sample
= NULL
;
2831 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2832 &sample_entry
, &sample
) < 0 &&
2837 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2839 isl_union_map_free(umap
);
2843 isl_union_map_free(umap
);
2847 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2849 return bset_from_bmap(isl_union_map_sample(uset
));
2852 /* Return an element in "uset" in the form of an isl_point.
2853 * Return a void isl_point if "uset" is empty.
2855 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2857 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2860 struct isl_forall_data
{
2862 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2865 static isl_stat
forall_entry(void **entry
, void *user
)
2867 struct isl_forall_data
*data
= user
;
2868 isl_map
*map
= *entry
;
2870 data
->res
= data
->fn(map
);
2872 return isl_stat_error
;
2875 return isl_stat_error
;
2880 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2881 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2883 struct isl_forall_data data
= { isl_bool_true
, fn
};
2886 return isl_bool_error
;
2888 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2889 &forall_entry
, &data
) < 0 && data
.res
)
2890 return isl_bool_error
;
2895 struct isl_forall_user_data
{
2897 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2901 static isl_stat
forall_user_entry(void **entry
, void *user
)
2903 struct isl_forall_user_data
*data
= user
;
2904 isl_map
*map
= *entry
;
2906 data
->res
= data
->fn(map
, data
->user
);
2908 return isl_stat_error
;
2911 return isl_stat_error
;
2916 /* Check if fn(map, user) returns true for all maps "map" in umap.
2918 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2919 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2921 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2924 return isl_bool_error
;
2926 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2927 &forall_user_entry
, &data
) < 0 && data
.res
)
2928 return isl_bool_error
;
2933 /* Is "umap" obviously empty?
2935 isl_bool
isl_union_map_plain_is_empty(__isl_keep isl_union_map
*umap
)
2939 n
= isl_union_map_n_map(umap
);
2941 return isl_bool_error
;
2945 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2947 return union_map_forall(umap
, &isl_map_is_empty
);
2950 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2952 return isl_union_map_is_empty(uset
);
2955 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2962 match
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
2964 return isl_bool_error
;
2966 return isl_bool_false
;
2968 space
= isl_map_get_space(map
);
2969 id
= isl_map_identity(space
);
2971 is_subset
= isl_map_is_subset(map
, id
);
2978 /* Given an isl_union_map that consists of a single map, check
2979 * if it is single-valued.
2981 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2986 umap
= isl_union_map_copy(umap
);
2987 map
= isl_map_from_union_map(umap
);
2988 sv
= isl_map_is_single_valued(map
);
2994 /* Internal data structure for single_valued_on_domain.
2996 * "umap" is the union map to be tested.
2997 * "sv" is set to 1 as long as "umap" may still be single-valued.
2999 struct isl_union_map_is_sv_data
{
3000 isl_union_map
*umap
;
3004 /* Check if the data->umap is single-valued on "set".
3006 * If data->umap consists of a single map on "set", then test it
3009 * Otherwise, compute
3013 * check if the result is a subset of the identity mapping and
3014 * store the result in data->sv.
3016 * Terminate as soon as data->umap has been determined not to
3019 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
3021 struct isl_union_map_is_sv_data
*data
= user
;
3022 isl_union_map
*umap
, *test
;
3025 umap
= isl_union_map_copy(data
->umap
);
3026 umap
= isl_union_map_intersect_domain(umap
,
3027 isl_union_set_from_set(set
));
3029 n
= isl_union_map_n_map(umap
);
3031 data
->sv
= isl_bool_error
;
3032 } else if (n
== 1) {
3033 data
->sv
= single_map_is_single_valued(umap
);
3034 isl_union_map_free(umap
);
3036 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
3037 test
= isl_union_map_apply_range(test
, umap
);
3039 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
3041 isl_union_map_free(test
);
3044 if (data
->sv
< 0 || !data
->sv
)
3045 return isl_stat_error
;
3049 /* Check if the given map is single-valued.
3051 * If the union map consists of a single map, then test it as an isl_map.
3052 * Otherwise, check if the union map is single-valued on each of its
3055 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
3057 isl_union_map
*universe
;
3058 isl_union_set
*domain
;
3059 struct isl_union_map_is_sv_data data
;
3062 n
= isl_union_map_n_map(umap
);
3064 return isl_bool_error
;
3066 return single_map_is_single_valued(umap
);
3068 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
3069 domain
= isl_union_map_domain(universe
);
3071 data
.sv
= isl_bool_true
;
3073 if (isl_union_set_foreach_set(domain
,
3074 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
3075 data
.sv
= isl_bool_error
;
3076 isl_union_set_free(domain
);
3081 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
3085 umap
= isl_union_map_copy(umap
);
3086 umap
= isl_union_map_reverse(umap
);
3087 in
= isl_union_map_is_single_valued(umap
);
3088 isl_union_map_free(umap
);
3093 /* Is "map" obviously not an identity relation because
3094 * it maps elements from one space to another space?
3095 * Update *non_identity accordingly.
3097 * In particular, if the domain and range spaces are the same,
3098 * then the map is not considered to obviously not be an identity relation.
3099 * Otherwise, the map is considered to obviously not be an identity relation
3100 * if it is is non-empty.
3102 * If "map" is determined to obviously not be an identity relation,
3103 * then the search is aborted.
3105 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
3107 isl_bool
*non_identity
= user
;
3110 equal
= isl_map_tuple_is_equal(map
, isl_dim_in
, map
, isl_dim_out
);
3111 if (equal
>= 0 && !equal
)
3112 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
3114 *non_identity
= isl_bool_not(equal
);
3117 if (*non_identity
< 0 || *non_identity
)
3118 return isl_stat_error
;
3123 /* Is "umap" obviously not an identity relation because
3124 * it maps elements from one space to another space?
3126 * As soon as a map has been found that maps elements to a different space,
3127 * non_identity is changed and the search is aborted.
3129 static isl_bool
isl_union_map_plain_is_not_identity(
3130 __isl_keep isl_union_map
*umap
)
3132 isl_bool non_identity
;
3134 non_identity
= isl_bool_false
;
3135 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
3136 &non_identity
) < 0 &&
3137 non_identity
== isl_bool_false
)
3138 return isl_bool_error
;
3140 return non_identity
;
3143 /* Does "map" only map elements to themselves?
3144 * Update *identity accordingly.
3146 * If "map" is determined not to be an identity relation,
3147 * then the search is aborted.
3149 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
3151 isl_bool
*identity
= user
;
3153 *identity
= isl_map_is_identity(map
);
3156 if (*identity
< 0 || !*identity
)
3157 return isl_stat_error
;
3162 /* Does "umap" only map elements to themselves?
3164 * First check if there are any maps that map elements to different spaces.
3165 * If not, then check that all the maps (between identical spaces)
3166 * are identity relations.
3168 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
3170 isl_bool non_identity
;
3173 non_identity
= isl_union_map_plain_is_not_identity(umap
);
3174 if (non_identity
< 0 || non_identity
)
3175 return isl_bool_not(non_identity
);
3177 identity
= isl_bool_true
;
3178 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
3179 identity
== isl_bool_true
)
3180 return isl_bool_error
;
3185 /* Represents a map that has a fixed value (v) for one of its
3187 * The map in this structure is not reference counted, so it
3188 * is only valid while the isl_union_map from which it was
3189 * obtained is still alive.
3191 struct isl_fixed_map
{
3196 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
3200 struct isl_fixed_map
*v
;
3202 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
3205 for (i
= 0; i
< n
; ++i
)
3206 isl_int_init(v
[i
].v
);
3210 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
3216 for (i
= 0; i
< n
; ++i
)
3217 isl_int_clear(v
[i
].v
);
3221 /* Compare the "v" field of two isl_fixed_map structs.
3223 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
3225 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
3226 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
3228 return isl_int_cmp(e1
->v
, e2
->v
);
3231 /* Internal data structure used while checking whether all maps
3232 * in a union_map have a fixed value for a given output dimension.
3233 * v is the list of maps, with the fixed value for the dimension
3234 * n is the number of maps considered so far
3235 * pos is the output dimension under investigation
3237 struct isl_fixed_dim_data
{
3238 struct isl_fixed_map
*v
;
3243 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
3245 struct isl_fixed_dim_data
*data
= user
;
3247 data
->v
[data
->n
].map
= map
;
3248 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
3249 &data
->v
[data
->n
++].v
);
3252 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
3253 int first
, int n_range
);
3255 /* Given a list of the maps, with their fixed values at output dimension "pos",
3256 * check whether the ranges of the maps form an obvious partition.
3258 * We first sort the maps according to their fixed values.
3259 * If all maps have a different value, then we know the ranges form
3261 * Otherwise, we collect the maps with the same fixed value and
3262 * check whether each such collection is obviously injective
3263 * based on later dimensions.
3265 static int separates(struct isl_fixed_map
*v
, int n
,
3266 __isl_take isl_space
*space
, int pos
, int n_range
)
3273 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
3275 for (i
= 0; i
+ 1 < n
; ++i
) {
3277 isl_union_map
*part
;
3280 for (j
= i
+ 1; j
< n
; ++j
)
3281 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
3287 part
= isl_union_map_alloc(isl_space_copy(space
), j
- i
);
3288 for (k
= i
; k
< j
; ++k
)
3289 part
= isl_union_map_add_map(part
,
3290 isl_map_copy(v
[k
].map
));
3292 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
3301 isl_space_free(space
);
3302 free_isl_fixed_map_array(v
, n
);
3305 isl_space_free(space
);
3306 free_isl_fixed_map_array(v
, n
);
3310 /* Check whether the maps in umap have obviously distinct ranges.
3311 * In particular, check for an output dimension in the range
3312 * [first,n_range) for which all maps have a fixed value
3313 * and then check if these values, possibly along with fixed values
3314 * at later dimensions, entail distinct ranges.
3316 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
3317 int first
, int n_range
)
3321 struct isl_fixed_dim_data data
= { NULL
};
3323 ctx
= isl_union_map_get_ctx(umap
);
3325 n
= isl_union_map_n_map(umap
);
3330 isl_union_map_free(umap
);
3331 return isl_bool_true
;
3334 if (first
>= n_range
) {
3335 isl_union_map_free(umap
);
3336 return isl_bool_false
;
3339 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
3343 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
3349 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
3354 space
= isl_union_map_get_space(umap
);
3355 injective
= separates(data
.v
, n
, space
, data
.pos
, n_range
);
3356 isl_union_map_free(umap
);
3360 free_isl_fixed_map_array(data
.v
, n
);
3361 isl_union_map_free(umap
);
3363 return isl_bool_false
;
3365 free_isl_fixed_map_array(data
.v
, n
);
3366 isl_union_map_free(umap
);
3367 return isl_bool_error
;
3370 /* Check whether the maps in umap that map to subsets of "ran"
3371 * have obviously distinct ranges.
3373 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
3377 isl_union_map
*umap
= user
;
3379 dim
= isl_set_dim(ran
, isl_dim_set
);
3381 return isl_bool_error
;
3383 umap
= isl_union_map_copy(umap
);
3384 umap
= isl_union_map_intersect_range(umap
,
3385 isl_union_set_from_set(isl_set_copy(ran
)));
3386 return plain_injective_on_range(umap
, 0, dim
);
3389 /* Check if the given union_map is obviously injective.
3391 * In particular, we first check if all individual maps are obviously
3392 * injective and then check if all the ranges of these maps are
3393 * obviously disjoint.
3395 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
3398 isl_union_map
*univ
;
3401 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
3403 return isl_bool_error
;
3405 return isl_bool_false
;
3407 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
3408 ran
= isl_union_map_range(univ
);
3410 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
3412 isl_union_set_free(ran
);
3417 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
3421 sv
= isl_union_map_is_single_valued(umap
);
3425 return isl_union_map_is_injective(umap
);
3428 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
3430 struct isl_un_op_drop_user_data data
= { &isl_map_can_zip
};
3431 struct isl_un_op_control control
= {
3432 .filter
= &un_op_filter_drop_user
,
3433 .filter_user
= &data
,
3434 .fn_map
= &isl_map_zip
,
3436 return un_op(umap
, &control
);
3439 /* Given a union map, take the maps of the form A -> (B -> C) and
3440 * return the union of the corresponding maps (A -> B) -> C.
3442 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
3444 struct isl_un_op_drop_user_data data
= { &isl_map_can_uncurry
};
3445 struct isl_un_op_control control
= {
3446 .filter
= &un_op_filter_drop_user
,
3447 .filter_user
= &data
,
3448 .fn_map
= &isl_map_uncurry
,
3450 return un_op(umap
, &control
);
3453 /* Given a union map, take the maps of the form (A -> B) -> C and
3454 * return the union of the corresponding maps A -> (B -> C).
3456 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
3458 struct isl_un_op_drop_user_data data
= { &isl_map_can_curry
};
3459 struct isl_un_op_control control
= {
3460 .filter
= &un_op_filter_drop_user
,
3461 .filter_user
= &data
,
3462 .fn_map
= &isl_map_curry
,
3464 return un_op(umap
, &control
);
3467 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3468 * return the union of the corresponding maps A -> (B -> (C -> D)).
3470 __isl_give isl_union_map
*isl_union_map_range_curry(
3471 __isl_take isl_union_map
*umap
)
3473 struct isl_un_op_drop_user_data data
= { &isl_map_can_range_curry
};
3474 struct isl_un_op_control control
= {
3475 .filter
= &un_op_filter_drop_user
,
3476 .filter_user
= &data
,
3477 .fn_map
= &isl_map_range_curry
,
3479 return un_op(umap
, &control
);
3482 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
3484 struct isl_un_op_control control
= {
3485 .fn_map
= &isl_set_lift
,
3487 return un_op(uset
, &control
);
3490 static isl_stat
coefficients_entry(void **entry
, void *user
)
3492 isl_set
*set
= *entry
;
3493 isl_union_set
**res
= user
;
3495 set
= isl_set_copy(set
);
3496 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
3497 *res
= isl_union_set_add_set(*res
, set
);
3502 __isl_give isl_union_set
*isl_union_set_coefficients(
3503 __isl_take isl_union_set
*uset
)
3512 ctx
= isl_union_set_get_ctx(uset
);
3513 space
= isl_space_set_alloc(ctx
, 0, 0);
3514 res
= isl_union_map_alloc(space
, uset
->table
.n
);
3515 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3516 &coefficients_entry
, &res
) < 0)
3519 isl_union_set_free(uset
);
3522 isl_union_set_free(uset
);
3523 isl_union_set_free(res
);
3527 static isl_stat
solutions_entry(void **entry
, void *user
)
3529 isl_set
*set
= *entry
;
3530 isl_union_set
**res
= user
;
3532 set
= isl_set_copy(set
);
3533 set
= isl_set_from_basic_set(isl_set_solutions(set
));
3535 *res
= isl_union_set_from_set(set
);
3537 *res
= isl_union_set_add_set(*res
, set
);
3540 return isl_stat_error
;
3545 __isl_give isl_union_set
*isl_union_set_solutions(
3546 __isl_take isl_union_set
*uset
)
3548 isl_union_set
*res
= NULL
;
3553 if (uset
->table
.n
== 0) {
3554 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
3555 isl_union_set_free(uset
);
3559 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3560 &solutions_entry
, &res
) < 0)
3563 isl_union_set_free(uset
);
3566 isl_union_set_free(uset
);
3567 isl_union_set_free(res
);
3571 /* Is the domain space of "map" equal to "space"?
3573 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3575 return isl_map_space_tuple_is_equal(map
, isl_dim_in
,
3576 space
, isl_dim_out
);
3579 /* Is the range space of "map" equal to "space"?
3581 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3583 return isl_map_space_tuple_is_equal(map
, isl_dim_out
,
3584 space
, isl_dim_out
);
3587 /* Is the set space of "map" equal to "space"?
3589 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3591 return isl_map_space_tuple_is_equal(map
, isl_dim_set
,
3592 space
, isl_dim_out
);
3595 /* Internal data structure for preimage_pw_multi_aff.
3597 * "pma" is the function under which the preimage should be taken.
3598 * "space" is the space of "pma".
3599 * "res" collects the results.
3600 * "fn" computes the preimage for a given map.
3601 * "match" returns true if "fn" can be called.
3603 struct isl_union_map_preimage_data
{
3605 isl_pw_multi_aff
*pma
;
3607 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3608 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3609 __isl_take isl_pw_multi_aff
*pma
);
3612 /* Call data->fn to compute the preimage of the domain or range of *entry
3613 * under the function represented by data->pma, provided the domain/range
3614 * space of *entry matches the target space of data->pma
3615 * (as given by data->match), and add the result to data->res.
3617 static isl_stat
preimage_entry(void **entry
, void *user
)
3620 isl_map
*map
= *entry
;
3621 struct isl_union_map_preimage_data
*data
= user
;
3624 m
= data
->match(map
, data
->space
);
3626 return isl_stat_error
;
3630 map
= isl_map_copy(map
);
3631 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3633 empty
= isl_map_is_empty(map
);
3634 if (empty
< 0 || empty
) {
3636 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3639 data
->res
= isl_union_map_add_map(data
->res
, map
);
3644 /* Compute the preimage of the domain or range of "umap" under the function
3645 * represented by "pma".
3646 * In other words, plug in "pma" in the domain or range of "umap".
3647 * The function "fn" performs the actual preimage computation on a map,
3648 * while "match" determines to which maps the function should be applied.
3650 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3651 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3652 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3653 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3654 __isl_take isl_pw_multi_aff
*pma
))
3658 struct isl_union_map_preimage_data data
;
3660 umap
= isl_union_map_align_params(umap
,
3661 isl_pw_multi_aff_get_space(pma
));
3662 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3667 ctx
= isl_union_map_get_ctx(umap
);
3668 space
= isl_union_map_get_space(umap
);
3669 data
.space
= isl_pw_multi_aff_get_space(pma
);
3671 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3674 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3676 data
.res
= isl_union_map_free(data
.res
);
3678 isl_space_free(data
.space
);
3679 isl_union_map_free(umap
);
3680 isl_pw_multi_aff_free(pma
);
3683 isl_union_map_free(umap
);
3684 isl_pw_multi_aff_free(pma
);
3688 /* Compute the preimage of the domain of "umap" under the function
3689 * represented by "pma".
3690 * In other words, plug in "pma" in the domain of "umap".
3691 * The result contains maps that live in the same spaces as the maps of "umap"
3692 * with domain space equal to the target space of "pma",
3693 * except that the domain has been replaced by the domain space of "pma".
3695 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3696 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3698 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3699 &isl_map_preimage_domain_pw_multi_aff
);
3702 /* Compute the preimage of the range of "umap" under the function
3703 * represented by "pma".
3704 * In other words, plug in "pma" in the range of "umap".
3705 * The result contains maps that live in the same spaces as the maps of "umap"
3706 * with range space equal to the target space of "pma",
3707 * except that the range has been replaced by the domain space of "pma".
3709 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3710 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3712 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3713 &isl_map_preimage_range_pw_multi_aff
);
3716 /* Compute the preimage of "uset" under the function represented by "pma".
3717 * In other words, plug in "pma" in "uset".
3718 * The result contains sets that live in the same spaces as the sets of "uset"
3719 * with space equal to the target space of "pma",
3720 * except that the space has been replaced by the domain space of "pma".
3722 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3723 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3725 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3726 &isl_set_preimage_pw_multi_aff
);
3729 /* Compute the preimage of the domain of "umap" under the function
3730 * represented by "ma".
3731 * In other words, plug in "ma" in the domain of "umap".
3732 * The result contains maps that live in the same spaces as the maps of "umap"
3733 * with domain space equal to the target space of "ma",
3734 * except that the domain has been replaced by the domain space of "ma".
3736 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3737 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3739 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3740 isl_pw_multi_aff_from_multi_aff(ma
));
3743 /* Compute the preimage of the range of "umap" under the function
3744 * represented by "ma".
3745 * In other words, plug in "ma" in the range of "umap".
3746 * The result contains maps that live in the same spaces as the maps of "umap"
3747 * with range space equal to the target space of "ma",
3748 * except that the range has been replaced by the domain space of "ma".
3750 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3751 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3753 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3754 isl_pw_multi_aff_from_multi_aff(ma
));
3757 /* Compute the preimage of "uset" under the function represented by "ma".
3758 * In other words, plug in "ma" in "uset".
3759 * The result contains sets that live in the same spaces as the sets of "uset"
3760 * with space equal to the target space of "ma",
3761 * except that the space has been replaced by the domain space of "ma".
3763 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3764 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3766 return isl_union_set_preimage_pw_multi_aff(uset
,
3767 isl_pw_multi_aff_from_multi_aff(ma
));
3770 /* Internal data structure for preimage_multi_pw_aff.
3772 * "mpa" is the function under which the preimage should be taken.
3773 * "space" is the space of "mpa".
3774 * "res" collects the results.
3775 * "fn" computes the preimage for a given map.
3776 * "match" returns true if "fn" can be called.
3778 struct isl_union_map_preimage_mpa_data
{
3780 isl_multi_pw_aff
*mpa
;
3782 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3783 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3784 __isl_take isl_multi_pw_aff
*mpa
);
3787 /* Call data->fn to compute the preimage of the domain or range of *entry
3788 * under the function represented by data->mpa, provided the domain/range
3789 * space of *entry matches the target space of data->mpa
3790 * (as given by data->match), and add the result to data->res.
3792 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3795 isl_map
*map
= *entry
;
3796 struct isl_union_map_preimage_mpa_data
*data
= user
;
3799 m
= data
->match(map
, data
->space
);
3801 return isl_stat_error
;
3805 map
= isl_map_copy(map
);
3806 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3808 empty
= isl_map_is_empty(map
);
3809 if (empty
< 0 || empty
) {
3811 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3814 data
->res
= isl_union_map_add_map(data
->res
, map
);
3819 /* Compute the preimage of the domain or range of "umap" under the function
3820 * represented by "mpa".
3821 * In other words, plug in "mpa" in the domain or range of "umap".
3822 * The function "fn" performs the actual preimage computation on a map,
3823 * while "match" determines to which maps the function should be applied.
3825 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3826 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3827 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3828 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3829 __isl_take isl_multi_pw_aff
*mpa
))
3833 struct isl_union_map_preimage_mpa_data data
;
3835 umap
= isl_union_map_align_params(umap
,
3836 isl_multi_pw_aff_get_space(mpa
));
3837 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3842 ctx
= isl_union_map_get_ctx(umap
);
3843 space
= isl_union_map_get_space(umap
);
3844 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3846 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3849 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3851 data
.res
= isl_union_map_free(data
.res
);
3853 isl_space_free(data
.space
);
3854 isl_union_map_free(umap
);
3855 isl_multi_pw_aff_free(mpa
);
3858 isl_union_map_free(umap
);
3859 isl_multi_pw_aff_free(mpa
);
3863 /* Compute the preimage of the domain of "umap" under the function
3864 * represented by "mpa".
3865 * In other words, plug in "mpa" in the domain of "umap".
3866 * The result contains maps that live in the same spaces as the maps of "umap"
3867 * with domain space equal to the target space of "mpa",
3868 * except that the domain has been replaced by the domain space of "mpa".
3870 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3871 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3873 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3874 &isl_map_preimage_domain_multi_pw_aff
);
3877 /* Internal data structure for preimage_upma.
3879 * "umap" is the map of which the preimage should be computed.
3880 * "res" collects the results.
3881 * "fn" computes the preimage for a given piecewise multi-affine function.
3883 struct isl_union_map_preimage_upma_data
{
3884 isl_union_map
*umap
;
3886 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3887 __isl_take isl_pw_multi_aff
*pma
);
3890 /* Call data->fn to compute the preimage of the domain or range of data->umap
3891 * under the function represented by pma and add the result to data->res.
3893 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3895 struct isl_union_map_preimage_upma_data
*data
= user
;
3896 isl_union_map
*umap
;
3898 umap
= isl_union_map_copy(data
->umap
);
3899 umap
= data
->fn(umap
, pma
);
3900 data
->res
= isl_union_map_union(data
->res
, umap
);
3902 return data
->res
? isl_stat_ok
: isl_stat_error
;
3905 /* Compute the preimage of the domain or range of "umap" under the function
3906 * represented by "upma".
3907 * In other words, plug in "upma" in the domain or range of "umap".
3908 * The function "fn" performs the actual preimage computation
3909 * on a piecewise multi-affine function.
3911 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3912 __isl_take isl_union_map
*umap
,
3913 __isl_take isl_union_pw_multi_aff
*upma
,
3914 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3915 __isl_take isl_pw_multi_aff
*pma
))
3917 struct isl_union_map_preimage_upma_data data
;
3920 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3922 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3923 &preimage_upma
, &data
) < 0)
3924 data
.res
= isl_union_map_free(data
.res
);
3926 isl_union_map_free(umap
);
3927 isl_union_pw_multi_aff_free(upma
);
3932 /* Compute the preimage of the domain of "umap" under the function
3933 * represented by "upma".
3934 * In other words, plug in "upma" in the domain of "umap".
3935 * The result contains maps that live in the same spaces as the maps of "umap"
3936 * with domain space equal to one of the target spaces of "upma",
3937 * except that the domain has been replaced by one of the domain spaces that
3938 * correspond to that target space of "upma".
3940 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3941 __isl_take isl_union_map
*umap
,
3942 __isl_take isl_union_pw_multi_aff
*upma
)
3944 return preimage_union_pw_multi_aff(umap
, upma
,
3945 &isl_union_map_preimage_domain_pw_multi_aff
);
3948 /* Compute the preimage of the range of "umap" under the function
3949 * represented by "upma".
3950 * In other words, plug in "upma" in the range of "umap".
3951 * The result contains maps that live in the same spaces as the maps of "umap"
3952 * with range space equal to one of the target spaces of "upma",
3953 * except that the range has been replaced by one of the domain spaces that
3954 * correspond to that target space of "upma".
3956 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3957 __isl_take isl_union_map
*umap
,
3958 __isl_take isl_union_pw_multi_aff
*upma
)
3960 return preimage_union_pw_multi_aff(umap
, upma
,
3961 &isl_union_map_preimage_range_pw_multi_aff
);
3964 /* Compute the preimage of "uset" under the function represented by "upma".
3965 * In other words, plug in "upma" in the range of "uset".
3966 * The result contains sets that live in the same spaces as the sets of "uset"
3967 * with space equal to one of the target spaces of "upma",
3968 * except that the space has been replaced by one of the domain spaces that
3969 * correspond to that target space of "upma".
3971 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3972 __isl_take isl_union_set
*uset
,
3973 __isl_take isl_union_pw_multi_aff
*upma
)
3975 return preimage_union_pw_multi_aff(uset
, upma
,
3976 &isl_union_set_preimage_pw_multi_aff
);
3979 /* Reset the user pointer on all identifiers of parameters and tuples
3980 * of the spaces of "umap".
3982 __isl_give isl_union_map
*isl_union_map_reset_user(
3983 __isl_take isl_union_map
*umap
)
3985 umap
= isl_union_map_cow(umap
);
3988 umap
->dim
= isl_space_reset_user(umap
->dim
);
3990 return isl_union_map_free(umap
);
3991 return total(umap
, &isl_map_reset_user
);
3994 /* Reset the user pointer on all identifiers of parameters and tuples
3995 * of the spaces of "uset".
3997 __isl_give isl_union_set
*isl_union_set_reset_user(
3998 __isl_take isl_union_set
*uset
)
4000 return isl_union_map_reset_user(uset
);
4003 /* Remove all existentially quantified variables and integer divisions
4004 * from "umap" using Fourier-Motzkin elimination.
4006 __isl_give isl_union_map
*isl_union_map_remove_divs(
4007 __isl_take isl_union_map
*umap
)
4009 return total(umap
, &isl_map_remove_divs
);
4012 /* Remove all existentially quantified variables and integer divisions
4013 * from "uset" using Fourier-Motzkin elimination.
4015 __isl_give isl_union_set
*isl_union_set_remove_divs(
4016 __isl_take isl_union_set
*uset
)
4018 return isl_union_map_remove_divs(uset
);
4021 /* Internal data structure for isl_union_map_project_out.
4022 * "type", "first" and "n" are the arguments for the isl_map_project_out
4024 * "res" collects the results.
4026 struct isl_union_map_project_out_data
{
4027 enum isl_dim_type type
;
4034 /* Turn the data->n dimensions of type data->type, starting at data->first
4035 * into existentially quantified variables and add the result to data->res.
4037 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
4039 struct isl_union_map_project_out_data
*data
= user
;
4041 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
4042 data
->res
= isl_union_map_add_map(data
->res
, map
);
4047 /* Turn the "n" dimensions of type "type", starting at "first"
4048 * into existentially quantified variables.
4049 * Since the space of an isl_union_map only contains parameters,
4050 * type is required to be equal to isl_dim_param.
4052 __isl_give isl_union_map
*isl_union_map_project_out(
4053 __isl_take isl_union_map
*umap
,
4054 enum isl_dim_type type
, unsigned first
, unsigned n
)
4057 struct isl_union_map_project_out_data data
= { type
, first
, n
};
4062 if (type
!= isl_dim_param
)
4063 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
4064 "can only project out parameters",
4065 return isl_union_map_free(umap
));
4067 space
= isl_union_map_get_space(umap
);
4068 space
= isl_space_drop_dims(space
, type
, first
, n
);
4069 data
.res
= isl_union_map_empty(space
);
4070 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
4071 data
.res
= isl_union_map_free(data
.res
);
4073 isl_union_map_free(umap
);
4079 #define TYPE isl_union_map
4080 #include "isl_project_out_all_params_templ.c"
4081 #include "isl_project_out_param_templ.c"
4083 /* Turn the "n" dimensions of type "type", starting at "first"
4084 * into existentially quantified variables.
4085 * Since the space of an isl_union_set only contains parameters,
4086 * "type" is required to be equal to isl_dim_param.
4088 __isl_give isl_union_set
*isl_union_set_project_out(
4089 __isl_take isl_union_set
*uset
,
4090 enum isl_dim_type type
, unsigned first
, unsigned n
)
4092 return isl_union_map_project_out(uset
, type
, first
, n
);
4095 /* Project out all parameters from "uset" by existentially quantifying
4098 __isl_give isl_union_set
*isl_union_set_project_out_all_params(
4099 __isl_take isl_union_set
*uset
)
4101 return uset_from_umap(
4102 isl_union_map_project_out_all_params(uset_to_umap(uset
)));
4105 /* Internal data structure for isl_union_map_involves_dims.
4106 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
4108 struct isl_union_map_involves_dims_data
{
4113 /* Does "map" _not_ involve the data->n parameters starting at data->first?
4115 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
4117 struct isl_union_map_involves_dims_data
*data
= user
;
4120 involves
= isl_map_involves_dims(map
,
4121 isl_dim_param
, data
->first
, data
->n
);
4122 return isl_bool_not(involves
);
4125 /* Does "umap" involve any of the n parameters starting at first?
4126 * "type" is required to be set to isl_dim_param.
4128 * "umap" involves any of those parameters if any of its maps
4129 * involve the parameters. In other words, "umap" does not
4130 * involve any of the parameters if all its maps to not
4131 * involve the parameters.
4133 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
4134 enum isl_dim_type type
, unsigned first
, unsigned n
)
4136 struct isl_union_map_involves_dims_data data
= { first
, n
};
4139 if (type
!= isl_dim_param
)
4140 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
4141 "can only reference parameters", return isl_bool_error
);
4143 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
4145 return isl_bool_not(excludes
);
4148 /* Internal data structure for isl_union_map_reset_range_space.
4149 * "range" is the space from which to set the range space.
4150 * "res" collects the results.
4152 struct isl_union_map_reset_range_space_data
{
4157 /* Replace the range space of "map" by the range space of data->range and
4158 * add the result to data->res.
4160 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
4162 struct isl_union_map_reset_range_space_data
*data
= user
;
4165 space
= isl_map_get_space(map
);
4166 space
= isl_space_domain(space
);
4167 space
= isl_space_extend_domain_with_range(space
,
4168 isl_space_copy(data
->range
));
4169 map
= isl_map_reset_space(map
, space
);
4170 data
->res
= isl_union_map_add_map(data
->res
, map
);
4172 return data
->res
? isl_stat_ok
: isl_stat_error
;
4175 /* Replace the range space of all the maps in "umap" by
4176 * the range space of "space".
4178 * This assumes that all maps have the same output dimension.
4179 * This function should therefore not be made publicly available.
4181 * Since the spaces of the maps change, so do their hash value.
4182 * We therefore need to create a new isl_union_map.
4184 __isl_give isl_union_map
*isl_union_map_reset_range_space(
4185 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
4187 struct isl_union_map_reset_range_space_data data
= { space
};
4189 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
4190 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
4191 data
.res
= isl_union_map_free(data
.res
);
4193 isl_space_free(space
);
4194 isl_union_map_free(umap
);
4198 /* Check that "umap" and "space" have the same number of parameters.
4200 static isl_stat
check_union_map_space_equal_dim(__isl_keep isl_union_map
*umap
,
4201 __isl_keep isl_space
*space
)
4203 isl_size dim1
, dim2
;
4205 dim1
= isl_union_map_dim(umap
, isl_dim_param
);
4206 dim2
= isl_space_dim(space
, isl_dim_param
);
4207 if (dim1
< 0 || dim2
< 0)
4208 return isl_stat_error
;
4211 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
4212 "number of parameters does not match", return isl_stat_error
);
4215 /* Internal data structure for isl_union_map_reset_equal_dim_space.
4216 * "space" is the target space.
4217 * "res" collects the results.
4219 struct isl_union_map_reset_params_data
{
4224 /* Replace the parameters of "map" by those of data->space and
4225 * add the result to data->res.
4227 static isl_stat
reset_params(__isl_take isl_map
*map
, void *user
)
4229 struct isl_union_map_reset_params_data
*data
= user
;
4232 space
= isl_map_get_space(map
);
4233 space
= isl_space_replace_params(space
, data
->space
);
4234 map
= isl_map_reset_equal_dim_space(map
, space
);
4235 data
->res
= isl_union_map_add_map(data
->res
, map
);
4237 return data
->res
? isl_stat_ok
: isl_stat_error
;
4240 /* Replace the space of "umap" by "space", without modifying
4241 * the dimension of "umap", i.e., the number of parameters of "umap".
4243 * Since the hash values of the maps in the union map depend
4244 * on the parameters, a new union map needs to be constructed.
4246 __isl_give isl_union_map
*isl_union_map_reset_equal_dim_space(
4247 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
4249 struct isl_union_map_reset_params_data data
= { space
};
4251 isl_space
*umap_space
;
4253 umap_space
= isl_union_map_peek_space(umap
);
4254 equal
= isl_space_is_equal(umap_space
, space
);
4258 isl_space_free(space
);
4261 if (check_union_map_space_equal_dim(umap
, space
) < 0)
4264 data
.res
= isl_union_map_empty(isl_space_copy(space
));
4265 if (isl_union_map_foreach_map(umap
, &reset_params
, &data
) < 0)
4266 data
.res
= isl_union_map_free(data
.res
);
4268 isl_space_free(space
);
4269 isl_union_map_free(umap
);
4272 isl_union_map_free(umap
);
4273 isl_space_free(space
);
4277 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4278 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4279 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4280 * to the domain and the range of each map.
4281 * "res" collects the results.
4283 struct isl_union_order_at_data
{
4284 isl_multi_union_pw_aff
*mupa
;
4285 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
4286 __isl_take isl_multi_pw_aff
*mpa2
);
4290 /* Intersect "map" with the result of applying data->order to
4291 * the functions in data->mupa that apply to the domain and the range
4292 * of "map" and add the result to data->res.
4294 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
4296 struct isl_union_order_at_data
*data
= user
;
4298 isl_multi_pw_aff
*mpa1
, *mpa2
;
4301 space
= isl_space_domain(isl_map_get_space(map
));
4302 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
4303 space
= isl_space_range(isl_map_get_space(map
));
4304 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
4305 order
= data
->order(mpa1
, mpa2
);
4306 map
= isl_map_intersect(map
, order
);
4307 data
->res
= isl_union_map_add_map(data
->res
, map
);
4309 return data
->res
? isl_stat_ok
: isl_stat_error
;
4312 /* If "mupa" has a non-trivial explicit domain, then intersect
4313 * domain and range of "umap" with this explicit domain.
4314 * If the explicit domain only describes constraints on the parameters,
4315 * then the intersection only needs to be performed once.
4317 static __isl_give isl_union_map
*intersect_explicit_domain(
4318 __isl_take isl_union_map
*umap
, __isl_keep isl_multi_union_pw_aff
*mupa
)
4320 isl_bool non_trivial
, is_params
;
4323 non_trivial
= isl_multi_union_pw_aff_has_non_trivial_domain(mupa
);
4324 if (non_trivial
< 0)
4325 return isl_union_map_free(umap
);
4328 mupa
= isl_multi_union_pw_aff_copy(mupa
);
4329 dom
= isl_multi_union_pw_aff_domain(mupa
);
4330 is_params
= isl_union_set_is_params(dom
);
4331 if (is_params
< 0) {
4332 isl_union_set_free(dom
);
4333 return isl_union_map_free(umap
);
4338 set
= isl_union_set_params(dom
);
4339 umap
= isl_union_map_intersect_params(umap
, set
);
4342 umap
= isl_union_map_intersect_domain(umap
, isl_union_set_copy(dom
));
4343 umap
= isl_union_map_intersect_range(umap
, dom
);
4347 /* Intersect each map in "umap" with the result of calling "order"
4348 * on the functions is "mupa" that apply to the domain and the range
4351 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
4352 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
4353 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
4354 __isl_take isl_multi_pw_aff
*mpa2
))
4356 struct isl_union_order_at_data data
;
4358 umap
= isl_union_map_align_params(umap
,
4359 isl_multi_union_pw_aff_get_space(mupa
));
4360 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
4361 isl_union_map_get_space(umap
));
4362 umap
= intersect_explicit_domain(umap
, mupa
);
4365 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
4366 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
4367 data
.res
= isl_union_map_free(data
.res
);
4369 isl_multi_union_pw_aff_free(mupa
);
4370 isl_union_map_free(umap
);
4374 /* Return the subset of "umap" where the domain and the range
4375 * have equal "mupa" values.
4377 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
4378 __isl_take isl_union_map
*umap
,
4379 __isl_take isl_multi_union_pw_aff
*mupa
)
4381 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
4382 &isl_multi_pw_aff_eq_map
);
4387 #include "isl_union_map_lex_templ.c"
4391 #include "isl_union_map_lex_templ.c"
4395 #include "isl_union_map_lex_templ.c"
4399 #include "isl_union_map_lex_templ.c"
4401 /* Return the union of the elements in the list "list".
4403 __isl_give isl_union_set
*isl_union_set_list_union(
4404 __isl_take isl_union_set_list
*list
)
4412 n
= isl_union_set_list_n_union_set(list
);
4416 ctx
= isl_union_set_list_get_ctx(list
);
4417 space
= isl_space_params_alloc(ctx
, 0);
4418 res
= isl_union_set_empty(space
);
4420 for (i
= 0; i
< n
; ++i
) {
4421 isl_union_set
*uset_i
;
4423 uset_i
= isl_union_set_list_get_union_set(list
, i
);
4424 res
= isl_union_set_union(res
, uset_i
);
4427 isl_union_set_list_free(list
);
4430 isl_union_set_list_free(list
);
4434 /* Update *hash with the hash value of "map".
4436 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
4438 uint32_t *hash
= user
;
4441 map_hash
= isl_map_get_hash(map
);
4442 isl_hash_hash(*hash
, map_hash
);
4448 /* Return a hash value that digests "umap".
4450 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
4457 hash
= isl_hash_init();
4458 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
4464 /* Return a hash value that digests "uset".
4466 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
4468 return isl_union_map_get_hash(uset
);
4471 /* Add the number of basic sets in "set" to "n".
4473 static isl_stat
add_n(__isl_take isl_set
*set
, void *user
)
4478 set_n
= isl_set_n_basic_set(set
);
4482 return set_n
< 0 ? isl_stat_error
: isl_stat_ok
;
4485 /* Return the total number of basic sets in "uset".
4487 int isl_union_set_n_basic_set(__isl_keep isl_union_set
*uset
)
4491 if (isl_union_set_foreach_set(uset
, &add_n
, &n
) < 0)
4497 /* Add the basic sets in "set" to "list".
4499 static isl_stat
add_list(__isl_take isl_set
*set
, void *user
)
4501 isl_basic_set_list
**list
= user
;
4502 isl_basic_set_list
*list_i
;
4504 list_i
= isl_set_get_basic_set_list(set
);
4505 *list
= isl_basic_set_list_concat(*list
, list_i
);
4509 return isl_stat_error
;
4513 /* Return a list containing all the basic sets in "uset".
4515 * First construct a list of the appropriate size and
4516 * then add all the elements.
4518 __isl_give isl_basic_set_list
*isl_union_set_get_basic_set_list(
4519 __isl_keep isl_union_set
*uset
)
4523 isl_basic_set_list
*list
;
4527 ctx
= isl_union_set_get_ctx(uset
);
4528 n
= isl_union_set_n_basic_set(uset
);
4531 list
= isl_basic_set_list_alloc(ctx
, n
);
4532 if (isl_union_set_foreach_set(uset
, &add_list
, &list
) < 0)
4533 list
= isl_basic_set_list_free(list
);
4538 /* Internal data structure for isl_union_map_remove_map_if.
4539 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4541 struct isl_union_map_remove_map_if_data
{
4542 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
4546 /* isl_un_op_control filter that negates the result of data->fn
4549 static isl_bool
not(__isl_keep isl_map
*map
, void *user
)
4551 struct isl_union_map_remove_map_if_data
*data
= user
;
4553 return isl_bool_not(data
->fn(map
, data
->user
));
4556 /* Dummy isl_un_op_control transformation callback that
4557 * simply returns the input.
4559 static __isl_give isl_map
*map_id(__isl_take isl_map
*map
)
4564 /* Call "fn" on every map in "umap" and remove those maps
4565 * for which the callback returns true.
4567 * Use un_op to keep only those maps that are not filtered out,
4568 * applying an identity transformation on them.
4570 __isl_give isl_union_map
*isl_union_map_remove_map_if(
4571 __isl_take isl_union_map
*umap
,
4572 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
4574 struct isl_union_map_remove_map_if_data data
= { fn
, user
};
4575 struct isl_un_op_control control
= {
4577 .filter_user
= &data
,
4580 return un_op(umap
, &control
);
4583 /* Does "map" have "space" as domain (ignoring parameters)?
4585 static isl_bool
has_domain_space_tuples(__isl_keep isl_map
*map
, void *user
)
4587 isl_space
*space
= user
;
4589 return isl_space_has_domain_tuples(space
, isl_map_peek_space(map
));
4592 /* Does "map" have "space" as range (ignoring parameters)?
4594 static isl_bool
has_range_space_tuples(__isl_keep isl_map
*map
, void *user
)
4596 isl_space
*space
= user
;
4598 return isl_space_has_range_tuples(space
, isl_map_peek_space(map
));
4601 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4603 static __isl_give isl_map
*bind_range(__isl_take isl_map
*map
, void *user
)
4605 isl_multi_id
*tuple
= user
;
4607 return isl_map_bind_range(map
, isl_multi_id_copy(tuple
));
4610 /* Bind the output dimensions of "umap" to parameters with identifiers
4611 * specified by "tuple", living in the range space of "umap",
4612 * for those maps that have a matching range space.
4614 __isl_give isl_union_set
*isl_union_map_bind_range(
4615 __isl_take isl_union_map
*umap
, __isl_take isl_multi_id
*tuple
)
4617 struct isl_un_op_control control
= {
4618 .filter
= &has_range_space_tuples
,
4619 .filter_user
= isl_multi_id_peek_space(tuple
),
4620 .fn_map2
= &bind_range
,
4621 .fn_map2_user
= tuple
,
4623 isl_union_set
*bound
;
4625 bound
= uset_from_umap(un_op(umap
, &control
));
4626 isl_multi_id_free(tuple
);
4630 /* Only keep those elements in "umap" that have a domain in "space".
4632 __isl_give isl_union_map
*isl_union_map_intersect_domain_space(
4633 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
4635 struct isl_un_op_control control
= {
4636 .filter
= &has_domain_space_tuples
,
4637 .filter_user
= space
,
4640 umap
= un_op(umap
, &control
);
4641 isl_space_free(space
);
4645 /* Only keep those elements in "umap" that have a range in "space".
4647 __isl_give isl_union_map
*isl_union_map_intersect_range_space(
4648 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
4650 struct isl_un_op_control control
= {
4651 .filter
= &has_range_space_tuples
,
4652 .filter_user
= space
,
4655 umap
= un_op(umap
, &control
);
4656 isl_space_free(space
);