2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_map_private.h>
18 #include <isl_union_map_private.h>
24 #include <isl_space_private.h>
25 #include <isl/union_set.h>
26 #include <isl_maybe_map.h>
27 #include <isl/deprecated/union_map_int.h>
29 #include <bset_from_bmap.c>
30 #include <set_to_map.c>
31 #include <set_from_map.c>
33 /* Return the number of parameters of "umap", where "type"
34 * is required to be set to isl_dim_param.
36 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
37 enum isl_dim_type type
)
42 if (type
!= isl_dim_param
)
43 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
44 "can only reference parameters", return 0);
46 return isl_space_dim(umap
->dim
, type
);
49 /* Return the number of parameters of "uset", where "type"
50 * is required to be set to isl_dim_param.
52 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
53 enum isl_dim_type type
)
55 return isl_union_map_dim(uset
, type
);
58 /* Return the id of the specified dimension.
60 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
61 enum isl_dim_type type
, unsigned pos
)
66 if (type
!= isl_dim_param
)
67 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
68 "can only reference parameters", return NULL
);
70 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
73 /* Is this union set a parameter domain?
75 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
81 return isl_bool_error
;
82 if (uset
->table
.n
!= 1)
83 return isl_bool_false
;
85 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
86 params
= isl_set_is_params(set
);
91 static __isl_give isl_union_map
*isl_union_map_alloc(
92 __isl_take isl_space
*space
, int size
)
96 space
= isl_space_params(space
);
100 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
102 isl_space_free(space
);
108 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
109 return isl_union_map_free(umap
);
114 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
116 return isl_union_map_alloc(dim
, 16);
119 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
121 return isl_union_map_empty(dim
);
124 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
126 return umap
? umap
->dim
->ctx
: NULL
;
129 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
131 return uset
? uset
->dim
->ctx
: NULL
;
134 /* Return the space of "umap".
136 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
138 return umap
? umap
->dim
: NULL
;
141 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
143 return isl_space_copy(isl_union_map_peek_space(umap
));
146 /* Return the position of the parameter with the given name
148 * Return -1 if no such dimension can be found.
150 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
151 enum isl_dim_type type
, const char *name
)
155 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
158 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
160 return isl_union_map_get_space(uset
);
163 static isl_stat
free_umap_entry(void **entry
, void *user
)
165 isl_map
*map
= *entry
;
170 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
172 isl_union_map
**umap
= (isl_union_map
**)user
;
174 *umap
= isl_union_map_add_map(*umap
, map
);
179 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
186 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
187 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
191 isl_union_map_free(dup
);
195 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
203 return isl_union_map_dup(umap
);
206 struct isl_union_align
{
211 static isl_stat
align_entry(void **entry
, void *user
)
213 isl_map
*map
= *entry
;
215 struct isl_union_align
*data
= user
;
217 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
218 isl_map_get_space(map
));
220 data
->res
= isl_union_map_add_map(data
->res
,
221 isl_map_realign(isl_map_copy(map
), exp
));
226 /* Align the parameters of umap along those of model.
227 * The result has the parameters of model first, in the same order
228 * as they appear in model, followed by any remaining parameters of
229 * umap that do not appear in model.
231 __isl_give isl_union_map
*isl_union_map_align_params(
232 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
234 struct isl_union_align data
= { NULL
, NULL
};
235 isl_bool equal_params
;
240 equal_params
= isl_space_has_equal_params(umap
->dim
, model
);
241 if (equal_params
< 0)
244 isl_space_free(model
);
248 model
= isl_space_params(model
);
249 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
253 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
255 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
256 &align_entry
, &data
) < 0)
259 isl_reordering_free(data
.exp
);
260 isl_union_map_free(umap
);
261 isl_space_free(model
);
264 isl_reordering_free(data
.exp
);
265 isl_union_map_free(umap
);
266 isl_union_map_free(data
.res
);
267 isl_space_free(model
);
271 __isl_give isl_union_set
*isl_union_set_align_params(
272 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
274 return isl_union_map_align_params(uset
, model
);
277 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
278 __isl_take isl_union_map
*umap2
)
280 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
281 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
283 umap1
= isl_union_map_cow(umap1
);
285 if (!umap1
|| !umap2
)
288 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
291 isl_union_map_free(umap2
);
295 isl_union_map_free(umap1
);
296 isl_union_map_free(umap2
);
300 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
301 __isl_take isl_union_set
*uset2
)
303 return isl_union_map_union(uset1
, uset2
);
306 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
315 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
317 return isl_union_map_copy(uset
);
320 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
328 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
329 &free_umap_entry
, NULL
);
330 isl_hash_table_clear(&umap
->table
);
331 isl_space_free(umap
->dim
);
336 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
338 return isl_union_map_free(uset
);
341 /* Do "umap" and "space" have the same parameters?
343 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
344 __isl_keep isl_space
*space
)
346 isl_space
*umap_space
;
348 umap_space
= isl_union_map_peek_space(umap
);
349 return isl_space_has_equal_params(umap_space
, space
);
352 static int has_space(const void *entry
, const void *val
)
354 isl_map
*map
= (isl_map
*)entry
;
355 isl_space
*space
= (isl_space
*) val
;
357 return isl_space_is_equal(map
->dim
, space
);
360 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
361 __isl_take isl_map
*map
)
364 struct isl_hash_table_entry
*entry
;
370 if (isl_map_plain_is_empty(map
)) {
375 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
379 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
380 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
383 umap
= isl_union_map_cow(umap
);
388 hash
= isl_space_get_hash(map
->dim
);
389 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
390 &has_space
, map
->dim
, 1);
397 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
406 isl_union_map_free(umap
);
410 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
411 __isl_take isl_set
*set
)
413 return isl_union_map_add_map(uset
, set_to_map(set
));
416 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
424 dim
= isl_map_get_space(map
);
425 dim
= isl_space_params(dim
);
426 umap
= isl_union_map_empty(dim
);
427 umap
= isl_union_map_add_map(umap
, map
);
432 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
434 return isl_union_map_from_map(set_to_map(set
));
437 __isl_give isl_union_map
*isl_union_map_from_basic_map(
438 __isl_take isl_basic_map
*bmap
)
440 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
443 __isl_give isl_union_set
*isl_union_set_from_basic_set(
444 __isl_take isl_basic_set
*bset
)
446 return isl_union_map_from_basic_map(bset
);
449 struct isl_union_map_foreach_data
451 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
455 static isl_stat
call_on_copy(void **entry
, void *user
)
457 isl_map
*map
= *entry
;
458 struct isl_union_map_foreach_data
*data
;
459 data
= (struct isl_union_map_foreach_data
*)user
;
461 return data
->fn(isl_map_copy(map
), data
->user
);
464 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
466 return umap
? umap
->table
.n
: 0;
469 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
471 return uset
? uset
->table
.n
: 0;
474 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
475 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
477 struct isl_union_map_foreach_data data
= { fn
, user
};
480 return isl_stat_error
;
482 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
483 &call_on_copy
, &data
);
486 static isl_stat
copy_map(void **entry
, void *user
)
488 isl_map
*map
= *entry
;
489 isl_map
**map_p
= user
;
491 *map_p
= isl_map_copy(map
);
493 return isl_stat_error
;
496 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
503 ctx
= isl_union_map_get_ctx(umap
);
504 if (umap
->table
.n
!= 1)
505 isl_die(ctx
, isl_error_invalid
,
506 "union map needs to contain elements in exactly "
507 "one space", goto error
);
509 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
511 isl_union_map_free(umap
);
515 isl_union_map_free(umap
);
519 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
521 return isl_map_from_union_map(uset
);
524 /* Extract the map in "umap" that lives in the given space (ignoring
527 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
528 __isl_take isl_space
*space
)
531 struct isl_hash_table_entry
*entry
;
533 space
= isl_space_drop_dims(space
, isl_dim_param
,
534 0, isl_space_dim(space
, isl_dim_param
));
535 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
539 hash
= isl_space_get_hash(space
);
540 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
541 &has_space
, space
, 0);
543 return isl_map_empty(space
);
544 isl_space_free(space
);
545 return isl_map_copy(entry
->data
);
547 isl_space_free(space
);
551 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
552 __isl_take isl_space
*dim
)
554 return set_from_map(isl_union_map_extract_map(uset
, dim
));
557 /* Check if umap contains a map in the given space.
559 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
560 __isl_keep isl_space
*space
)
563 struct isl_hash_table_entry
*entry
;
566 return isl_bool_error
;
568 hash
= isl_space_get_hash(space
);
569 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
570 &has_space
, space
, 0);
574 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
575 __isl_keep isl_space
*space
)
577 return isl_union_map_contains(uset
, space
);
580 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
581 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
583 return isl_union_map_foreach_map(uset
,
584 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
587 struct isl_union_set_foreach_point_data
{
588 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
592 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
594 struct isl_union_set_foreach_point_data
*data
= user
;
597 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
603 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
604 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
606 struct isl_union_set_foreach_point_data data
= { fn
, user
};
607 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
610 /* Data structure that specifies how gen_bin_op should
611 * construct results from the inputs.
613 * If "subtract" is set, then a map in the first input is copied to the result
614 * if there is no corresponding map in the second input.
615 * Otherwise, a map in the first input with no corresponding map
616 * in the second input is ignored.
617 * "match_space" specifies how to transform the space of a map
618 * in the first input to the space of the corresponding map
619 * in the second input.
620 * "fn_map" specifies how the matching maps, one from each input,
621 * should be combined to form a map in the result.
623 struct isl_bin_op_control
{
625 __isl_give isl_space
*(*match_space
)(__isl_take isl_space
*space
);
626 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map1
,
627 __isl_take isl_map
*map2
);
630 /* Internal data structure for gen_bin_op.
631 * "control" specifies how the maps in the result should be constructed.
632 * "umap2" is a pointer to the second argument.
633 * "res" collects the results.
635 struct isl_union_map_gen_bin_data
{
636 struct isl_bin_op_control
*control
;
637 isl_union_map
*umap2
;
641 /* Add a copy of "map" to "res" and return the result.
643 static __isl_give isl_union_map
*bin_add_map(__isl_take isl_union_map
*res
,
644 __isl_keep isl_map
*map
)
646 return isl_union_map_add_map(res
, isl_map_copy(map
));
649 /* Combine "map1" and "map2", add the result to "res" and return the result.
650 * Check whether the result is empty before adding it to "res".
652 static __isl_give isl_union_map
*bin_add_pair(__isl_take isl_union_map
*res
,
653 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
654 struct isl_union_map_gen_bin_data
*data
)
659 map
= data
->control
->fn_map(isl_map_copy(map1
), isl_map_copy(map2
));
660 empty
= isl_map_is_empty(map
);
661 if (empty
< 0 || empty
) {
664 return isl_union_map_free(res
);
667 return isl_union_map_add_map(res
, map
);
670 /* Dummy match_space function that simply returns the input space.
672 static __isl_give isl_space
*identity(__isl_take isl_space
*space
)
677 /* Look for the map in data->umap2 that corresponds to "map", if any.
678 * Return (isl_bool_true, matching map) if there is one,
679 * (isl_bool_false, NULL) if there is no matching map and
680 * (isl_bool_error, NULL) on error.
682 * data->control->match_space specifies which map in data->umap2
683 * corresponds to "map".
685 static __isl_keep isl_maybe_isl_map
bin_try_get_match(
686 struct isl_union_map_gen_bin_data
*data
, __isl_keep isl_map
*map
)
689 struct isl_hash_table_entry
*entry2
;
691 isl_maybe_isl_map res
= { isl_bool_error
, NULL
};
693 space
= isl_map_get_space(map
);
694 if (data
->control
->match_space
!= &identity
)
695 space
= data
->control
->match_space(space
);
698 hash
= isl_space_get_hash(space
);
699 entry2
= isl_hash_table_find(isl_union_map_get_ctx(data
->umap2
),
700 &data
->umap2
->table
, hash
,
701 &has_space
, space
, 0);
702 isl_space_free(space
);
703 res
.valid
= entry2
!= NULL
;
705 res
.value
= entry2
->data
;
710 /* isl_hash_table_foreach callback for gen_bin_op.
711 * Look for the map in data->umap2 that corresponds
712 * to the map that "entry" points to, apply the binary operation and
713 * add the result to data->res.
715 * If no corresponding map can be found, then the effect depends
716 * on data->control->subtract. If it is set, then the current map
717 * is added directly to the result. Otherwise, it is ignored.
719 static isl_stat
gen_bin_entry(void **entry
, void *user
)
721 struct isl_union_map_gen_bin_data
*data
= user
;
722 isl_map
*map
= *entry
;
725 m
= bin_try_get_match(data
, map
);
727 return isl_stat_error
;
728 if (!m
.valid
&& !data
->control
->subtract
)
732 data
->res
= bin_add_map(data
->res
, map
);
734 data
->res
= bin_add_pair(data
->res
, map
, m
.value
, data
);
736 return isl_stat_error
;
741 /* Apply a binary operation to "umap1" and "umap2" based on "control".
742 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
745 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
746 __isl_take isl_union_map
*umap2
, struct isl_bin_op_control
*control
)
748 struct isl_union_map_gen_bin_data data
= { control
, NULL
, NULL
};
750 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
751 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
753 if (!umap1
|| !umap2
)
757 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
759 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
760 &gen_bin_entry
, &data
) < 0)
763 isl_union_map_free(umap1
);
764 isl_union_map_free(umap2
);
767 isl_union_map_free(umap1
);
768 isl_union_map_free(umap2
);
769 isl_union_map_free(data
.res
);
773 __isl_give isl_union_map
*isl_union_map_subtract(
774 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
776 struct isl_bin_op_control control
= {
778 .match_space
= &identity
,
779 .fn_map
= &isl_map_subtract
,
782 return gen_bin_op(umap1
, umap2
, &control
);
785 __isl_give isl_union_set
*isl_union_set_subtract(
786 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
788 return isl_union_map_subtract(uset1
, uset2
);
791 struct isl_union_map_gen_bin_set_data
{
796 static isl_stat
intersect_params_entry(void **entry
, void *user
)
798 struct isl_union_map_gen_bin_set_data
*data
= user
;
799 isl_map
*map
= *entry
;
802 map
= isl_map_copy(map
);
803 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
805 empty
= isl_map_is_empty(map
);
808 return isl_stat_error
;
811 data
->res
= isl_union_map_add_map(data
->res
, map
);
816 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
817 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
819 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
821 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
822 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
828 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
830 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
834 isl_union_map_free(umap
);
838 isl_union_map_free(umap
);
840 isl_union_map_free(data
.res
);
844 /* Intersect "umap" with the parameter domain "set".
846 * If "set" does not have any constraints, then we can return immediately.
848 __isl_give isl_union_map
*isl_union_map_intersect_params(
849 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
853 is_universe
= isl_set_plain_is_universe(set
);
861 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
863 isl_union_map_free(umap
);
868 __isl_give isl_union_set
*isl_union_set_intersect_params(
869 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
871 return isl_union_map_intersect_params(uset
, set
);
874 static __isl_give isl_union_map
*union_map_intersect_params(
875 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
877 return isl_union_map_intersect_params(umap
,
878 isl_set_from_union_set(uset
));
881 static __isl_give isl_union_map
*union_map_gist_params(
882 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
884 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
887 struct isl_union_map_match_bin_data
{
888 isl_union_map
*umap2
;
890 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
893 static isl_stat
match_bin_entry(void **entry
, void *user
)
895 struct isl_union_map_match_bin_data
*data
= user
;
897 struct isl_hash_table_entry
*entry2
;
898 isl_map
*map
= *entry
;
901 hash
= isl_space_get_hash(map
->dim
);
902 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
903 hash
, &has_space
, map
->dim
, 0);
907 map
= isl_map_copy(map
);
908 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
910 empty
= isl_map_is_empty(map
);
913 return isl_stat_error
;
920 data
->res
= isl_union_map_add_map(data
->res
, map
);
925 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
926 __isl_take isl_union_map
*umap2
,
927 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
929 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
931 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
932 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
934 if (!umap1
|| !umap2
)
938 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
940 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
941 &match_bin_entry
, &data
) < 0)
944 isl_union_map_free(umap1
);
945 isl_union_map_free(umap2
);
948 isl_union_map_free(umap1
);
949 isl_union_map_free(umap2
);
950 isl_union_map_free(data
.res
);
954 __isl_give isl_union_map
*isl_union_map_intersect(
955 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
957 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
960 /* Compute the intersection of the two union_sets.
961 * As a special case, if exactly one of the two union_sets
962 * is a parameter domain, then intersect the parameter domain
963 * of the other one with this set.
965 __isl_give isl_union_set
*isl_union_set_intersect(
966 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
970 p1
= isl_union_set_is_params(uset1
);
971 p2
= isl_union_set_is_params(uset2
);
972 if (p1
< 0 || p2
< 0)
975 return union_map_intersect_params(uset1
, uset2
);
977 return union_map_intersect_params(uset2
, uset1
);
978 return isl_union_map_intersect(uset1
, uset2
);
980 isl_union_set_free(uset1
);
981 isl_union_set_free(uset2
);
985 static isl_stat
gist_params_entry(void **entry
, void *user
)
987 struct isl_union_map_gen_bin_set_data
*data
= user
;
988 isl_map
*map
= *entry
;
991 map
= isl_map_copy(map
);
992 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
994 empty
= isl_map_is_empty(map
);
997 return isl_stat_error
;
1000 data
->res
= isl_union_map_add_map(data
->res
, map
);
1005 __isl_give isl_union_map
*isl_union_map_gist_params(
1006 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
1008 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
1011 __isl_give isl_union_set
*isl_union_set_gist_params(
1012 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
1014 return isl_union_map_gist_params(uset
, set
);
1017 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
1018 __isl_take isl_union_map
*context
)
1020 return match_bin_op(umap
, context
, &isl_map_gist
);
1023 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
1024 __isl_take isl_union_set
*context
)
1026 if (isl_union_set_is_params(context
))
1027 return union_map_gist_params(uset
, context
);
1028 return isl_union_map_gist(uset
, context
);
1031 /* For each map in "umap", remove the constraints in the corresponding map
1033 * Each map in "context" is assumed to consist of a single disjunct and
1034 * to have explicit representations for all local variables.
1036 __isl_give isl_union_map
*isl_union_map_plain_gist(
1037 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*context
)
1039 return match_bin_op(umap
, context
, &isl_map_plain_gist
);
1042 /* For each set in "uset", remove the constraints in the corresponding set
1044 * Each set in "context" is assumed to consist of a single disjunct and
1045 * to have explicit representations for all local variables.
1047 __isl_give isl_union_set
*isl_union_set_plain_gist(
1048 __isl_take isl_union_set
*uset
, __isl_take isl_union_set
*context
)
1050 return isl_union_map_plain_gist(uset
, context
);
1053 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
1054 __isl_take isl_map
*set2
)
1056 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
1059 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
1060 __isl_take isl_map
*set2
)
1062 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
1065 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
1066 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1068 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
1071 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
1072 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1074 return match_bin_op(uset1
, uset2
, &lex_le_set
);
1077 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
1078 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1080 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
1083 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
1084 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1086 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
1089 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
1090 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1092 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
1095 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
1096 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1098 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
1101 /* Intersect the domain of "umap" with "uset".
1103 static __isl_give isl_union_map
*union_map_intersect_domain(
1104 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1106 struct isl_bin_op_control control
= {
1107 .match_space
= &isl_space_domain
,
1108 .fn_map
= &isl_map_intersect_domain
,
1111 return gen_bin_op(umap
, uset
, &control
);
1114 /* Intersect the domain of "umap" with "uset".
1115 * If "uset" is a parameters domain, then intersect the parameter
1116 * domain of "umap" with this set.
1118 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1119 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1121 if (isl_union_set_is_params(uset
))
1122 return union_map_intersect_params(umap
, uset
);
1124 return union_map_intersect_domain(umap
, uset
);
1127 /* Remove the elements of "uset" from the domain of "umap".
1129 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1130 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1132 struct isl_bin_op_control control
= {
1134 .match_space
= &isl_space_domain
,
1135 .fn_map
= &isl_map_subtract_domain
,
1138 return gen_bin_op(umap
, dom
, &control
);
1141 /* Remove the elements of "uset" from the range of "umap".
1143 __isl_give isl_union_map
*isl_union_map_subtract_range(
1144 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1146 struct isl_bin_op_control control
= {
1148 .match_space
= &isl_space_range
,
1149 .fn_map
= &isl_map_subtract_range
,
1152 return gen_bin_op(umap
, dom
, &control
);
1155 /* Compute the gist of "umap" with respect to the domain "uset".
1157 static __isl_give isl_union_map
*union_map_gist_domain(
1158 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1160 struct isl_bin_op_control control
= {
1161 .match_space
= &isl_space_domain
,
1162 .fn_map
= &isl_map_gist_domain
,
1165 return gen_bin_op(umap
, uset
, &control
);
1168 /* Compute the gist of "umap" with respect to the domain "uset".
1169 * If "uset" is a parameters domain, then compute the gist
1170 * with respect to this parameter domain.
1172 __isl_give isl_union_map
*isl_union_map_gist_domain(
1173 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1175 if (isl_union_set_is_params(uset
))
1176 return union_map_gist_params(umap
, uset
);
1178 return union_map_gist_domain(umap
, uset
);
1181 /* Compute the gist of "umap" with respect to the range "uset".
1183 __isl_give isl_union_map
*isl_union_map_gist_range(
1184 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1186 struct isl_bin_op_control control
= {
1187 .match_space
= &isl_space_range
,
1188 .fn_map
= &isl_map_gist_range
,
1191 return gen_bin_op(umap
, uset
, &control
);
1194 __isl_give isl_union_map
*isl_union_map_intersect_range(
1195 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1197 struct isl_bin_op_control control
= {
1198 .match_space
= &isl_space_range
,
1199 .fn_map
= &isl_map_intersect_range
,
1202 return gen_bin_op(umap
, uset
, &control
);
1205 struct isl_union_map_bin_data
{
1206 isl_union_map
*umap2
;
1209 isl_stat (*fn
)(void **entry
, void *user
);
1212 static isl_stat
apply_range_entry(void **entry
, void *user
)
1214 struct isl_union_map_bin_data
*data
= user
;
1215 isl_map
*map2
= *entry
;
1218 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1219 map2
->dim
, isl_dim_in
))
1222 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1224 empty
= isl_map_is_empty(map2
);
1227 return isl_stat_error
;
1234 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1239 static isl_stat
bin_entry(void **entry
, void *user
)
1241 struct isl_union_map_bin_data
*data
= user
;
1242 isl_map
*map
= *entry
;
1245 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1246 data
->fn
, data
) < 0)
1247 return isl_stat_error
;
1252 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1253 __isl_take isl_union_map
*umap2
,
1254 isl_stat (*fn
)(void **entry
, void *user
))
1256 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1258 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1259 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1261 if (!umap1
|| !umap2
)
1265 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1267 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1268 &bin_entry
, &data
) < 0)
1271 isl_union_map_free(umap1
);
1272 isl_union_map_free(umap2
);
1275 isl_union_map_free(umap1
);
1276 isl_union_map_free(umap2
);
1277 isl_union_map_free(data
.res
);
1281 __isl_give isl_union_map
*isl_union_map_apply_range(
1282 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1284 return bin_op(umap1
, umap2
, &apply_range_entry
);
1287 __isl_give isl_union_map
*isl_union_map_apply_domain(
1288 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1290 umap1
= isl_union_map_reverse(umap1
);
1291 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1292 return isl_union_map_reverse(umap1
);
1295 __isl_give isl_union_set
*isl_union_set_apply(
1296 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1298 return isl_union_map_apply_range(uset
, umap
);
1301 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1303 struct isl_union_map_bin_data
*data
= user
;
1304 isl_map
*map2
= *entry
;
1306 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1307 map2
->dim
, isl_dim_out
))
1310 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1312 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1317 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1318 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1320 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1323 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1325 struct isl_union_map_bin_data
*data
= user
;
1326 isl_map
*map2
= *entry
;
1328 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1329 map2
->dim
, isl_dim_out
))
1332 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1334 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1339 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1340 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1342 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1345 static isl_stat
product_entry(void **entry
, void *user
)
1347 struct isl_union_map_bin_data
*data
= user
;
1348 isl_map
*map2
= *entry
;
1350 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1352 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1357 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1358 __isl_take isl_union_map
*umap2
)
1360 return bin_op(umap1
, umap2
, &product_entry
);
1363 static isl_stat
set_product_entry(void **entry
, void *user
)
1365 struct isl_union_map_bin_data
*data
= user
;
1366 isl_set
*set2
= *entry
;
1368 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1370 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1375 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1376 __isl_take isl_union_set
*uset2
)
1378 return bin_op(uset1
, uset2
, &set_product_entry
);
1381 static isl_stat
domain_product_entry(void **entry
, void *user
)
1383 struct isl_union_map_bin_data
*data
= user
;
1384 isl_map
*map2
= *entry
;
1386 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1387 map2
->dim
, isl_dim_out
))
1390 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1391 isl_map_copy(map2
));
1393 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1398 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1400 __isl_give isl_union_map
*isl_union_map_domain_product(
1401 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1403 return bin_op(umap1
, umap2
, &domain_product_entry
);
1406 static isl_stat
range_product_entry(void **entry
, void *user
)
1408 struct isl_union_map_bin_data
*data
= user
;
1409 isl_map
*map2
= *entry
;
1411 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1412 map2
->dim
, isl_dim_in
))
1415 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1416 isl_map_copy(map2
));
1418 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1423 __isl_give isl_union_map
*isl_union_map_range_product(
1424 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1426 return bin_op(umap1
, umap2
, &range_product_entry
);
1429 /* If data->map A -> B and "map2" C -> D have the same range space,
1430 * then add (A, C) -> (B * D) to data->res.
1432 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1434 struct isl_union_map_bin_data
*data
= user
;
1435 isl_map
*map2
= *entry
;
1437 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1438 map2
->dim
, isl_dim_out
))
1441 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1442 isl_map_copy(map2
));
1444 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1449 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1451 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1452 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1454 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1457 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1459 struct isl_union_map_bin_data
*data
= user
;
1460 isl_map
*map2
= *entry
;
1462 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1463 map2
->dim
, isl_dim_in
))
1466 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1467 isl_map_copy(map2
));
1469 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1474 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1475 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1477 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1480 /* Data structure that specifies how un_op should modify
1481 * the maps in the union map.
1483 * If "inplace" is set, then the maps in the input union map
1484 * are modified in place. This means that "fn_map" should not
1485 * change the meaning of the map or that the union map only
1486 * has a single reference.
1487 * If "total" is set, then all maps need to be modified and
1488 * the results need to live in the same space.
1489 * Otherwise, a new union map is constructed to store the results.
1490 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1491 * are taken into account. No filter can be set if "inplace" or
1493 * "fn_map" specifies how the maps (selected by "filter")
1494 * should be transformed.
1496 struct isl_un_op_control
{
1499 isl_bool (*filter
)(__isl_keep isl_map
*map
);
1500 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map
);
1503 /* Internal data structure for "un_op".
1504 * "control" specifies how the maps in the union map should be modified.
1505 * "res" collects the results.
1507 struct isl_union_map_un_data
{
1508 struct isl_un_op_control
*control
;
1512 /* isl_hash_table_foreach callback for un_op.
1513 * Handle the map that "entry" points to.
1515 * If control->filter is set, then check if this map satisfies the filter.
1516 * If so (or if control->filter is not set), modify the map
1517 * by calling control->fn_map and either add the result to data->res or
1518 * replace the original entry by the result (if control->inplace is set).
1520 static isl_stat
un_entry(void **entry
, void *user
)
1522 struct isl_union_map_un_data
*data
= user
;
1523 isl_map
*map
= *entry
;
1525 if (data
->control
->filter
) {
1528 ok
= data
->control
->filter(map
);
1530 return isl_stat_error
;
1535 map
= data
->control
->fn_map(isl_map_copy(map
));
1537 return isl_stat_error
;
1538 if (data
->control
->inplace
) {
1539 isl_map_free(*entry
);
1542 data
->res
= isl_union_map_add_map(data
->res
, map
);
1544 return isl_stat_error
;
1550 /* Modify the maps in "umap" based on "control".
1551 * If control->inplace is set, then modify the maps in "umap" in-place.
1552 * Otherwise, create a new union map to hold the results.
1553 * If control->total is set, then perform an inplace computation
1554 * if "umap" is only referenced once. Otherwise, create a new union map
1555 * to store the results.
1557 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1558 struct isl_un_op_control
*control
)
1560 struct isl_union_map_un_data data
= { control
};
1564 if ((control
->inplace
|| control
->total
) && control
->filter
)
1565 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
1566 "inplace/total modification cannot be filtered",
1567 return isl_union_map_free(umap
));
1569 if (control
->total
&& umap
->ref
== 1)
1570 control
->inplace
= 1;
1571 if (control
->inplace
) {
1576 space
= isl_union_map_get_space(umap
);
1577 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
1579 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1580 &umap
->table
, &un_entry
, &data
) < 0)
1581 data
.res
= isl_union_map_free(data
.res
);
1583 if (control
->inplace
)
1585 isl_union_map_free(umap
);
1589 __isl_give isl_union_map
*isl_union_map_from_range(
1590 __isl_take isl_union_set
*uset
)
1592 struct isl_un_op_control control
= {
1593 .fn_map
= &isl_map_from_range
,
1595 return un_op(uset
, &control
);
1598 __isl_give isl_union_map
*isl_union_map_from_domain(
1599 __isl_take isl_union_set
*uset
)
1601 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1604 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1605 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1607 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1608 isl_union_map_from_range(range
));
1611 /* Modify the maps in "umap" by applying "fn" on them.
1612 * "fn" should apply to all maps in "umap" and should not modify the space.
1614 static __isl_give isl_union_map
*total(__isl_take isl_union_map
*umap
,
1615 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1617 struct isl_un_op_control control
= {
1622 return un_op(umap
, &control
);
1625 /* Compute the affine hull of "map" and return the result as an isl_map.
1627 static __isl_give isl_map
*isl_map_affine_hull_map(__isl_take isl_map
*map
)
1629 return isl_map_from_basic_map(isl_map_affine_hull(map
));
1632 __isl_give isl_union_map
*isl_union_map_affine_hull(
1633 __isl_take isl_union_map
*umap
)
1635 return total(umap
, &isl_map_affine_hull_map
);
1638 __isl_give isl_union_set
*isl_union_set_affine_hull(
1639 __isl_take isl_union_set
*uset
)
1641 return isl_union_map_affine_hull(uset
);
1644 /* Wrapper around isl_set_combined_lineality_space
1645 * that returns the combined lineality space in the form of an isl_set
1646 * instead of an isl_basic_set.
1648 static __isl_give isl_set
*combined_lineality_space(__isl_take isl_set
*set
)
1650 return isl_set_from_basic_set(isl_set_combined_lineality_space(set
));
1653 /* For each set in "uset", compute the (linear) hull
1654 * of the lineality spaces of its basic sets and
1655 * collect and return the results.
1657 __isl_give isl_union_set
*isl_union_set_combined_lineality_space(
1658 __isl_take isl_union_set
*uset
)
1660 struct isl_un_op_control control
= {
1661 .fn_map
= &combined_lineality_space
,
1663 return un_op(uset
, &control
);
1666 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1668 static __isl_give isl_map
*isl_map_polyhedral_hull_map(__isl_take isl_map
*map
)
1670 return isl_map_from_basic_map(isl_map_polyhedral_hull(map
));
1673 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1674 __isl_take isl_union_map
*umap
)
1676 return total(umap
, &isl_map_polyhedral_hull_map
);
1679 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1680 __isl_take isl_union_set
*uset
)
1682 return isl_union_map_polyhedral_hull(uset
);
1685 /* Compute a superset of the convex hull of "map" that is described
1686 * by only translates of the constraints in the constituents of "map" and
1687 * return the result as an isl_map.
1689 static __isl_give isl_map
*isl_map_simple_hull_map(__isl_take isl_map
*map
)
1691 return isl_map_from_basic_map(isl_map_simple_hull(map
));
1694 __isl_give isl_union_map
*isl_union_map_simple_hull(
1695 __isl_take isl_union_map
*umap
)
1697 return total(umap
, &isl_map_simple_hull_map
);
1700 __isl_give isl_union_set
*isl_union_set_simple_hull(
1701 __isl_take isl_union_set
*uset
)
1703 return isl_union_map_simple_hull(uset
);
1706 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1707 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1709 struct isl_un_op_control control
= {
1714 return un_op(umap
, &control
);
1717 /* Remove redundant constraints in each of the basic maps of "umap".
1718 * Since removing redundant constraints does not change the meaning
1719 * or the space, the operation can be performed in-place.
1721 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1722 __isl_take isl_union_map
*umap
)
1724 return inplace(umap
, &isl_map_remove_redundancies
);
1727 /* Remove redundant constraints in each of the basic sets of "uset".
1729 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1730 __isl_take isl_union_set
*uset
)
1732 return isl_union_map_remove_redundancies(uset
);
1735 __isl_give isl_union_map
*isl_union_map_coalesce(
1736 __isl_take isl_union_map
*umap
)
1738 return inplace(umap
, &isl_map_coalesce
);
1741 __isl_give isl_union_set
*isl_union_set_coalesce(
1742 __isl_take isl_union_set
*uset
)
1744 return isl_union_map_coalesce(uset
);
1747 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1748 __isl_take isl_union_map
*umap
)
1750 return inplace(umap
, &isl_map_detect_equalities
);
1753 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1754 __isl_take isl_union_set
*uset
)
1756 return isl_union_map_detect_equalities(uset
);
1759 __isl_give isl_union_map
*isl_union_map_compute_divs(
1760 __isl_take isl_union_map
*umap
)
1762 return inplace(umap
, &isl_map_compute_divs
);
1765 __isl_give isl_union_set
*isl_union_set_compute_divs(
1766 __isl_take isl_union_set
*uset
)
1768 return isl_union_map_compute_divs(uset
);
1771 __isl_give isl_union_map
*isl_union_map_lexmin(
1772 __isl_take isl_union_map
*umap
)
1774 return total(umap
, &isl_map_lexmin
);
1777 __isl_give isl_union_set
*isl_union_set_lexmin(
1778 __isl_take isl_union_set
*uset
)
1780 return isl_union_map_lexmin(uset
);
1783 __isl_give isl_union_map
*isl_union_map_lexmax(
1784 __isl_take isl_union_map
*umap
)
1786 return total(umap
, &isl_map_lexmax
);
1789 __isl_give isl_union_set
*isl_union_set_lexmax(
1790 __isl_take isl_union_set
*uset
)
1792 return isl_union_map_lexmax(uset
);
1795 /* Return the universe in the space of "map".
1797 static __isl_give isl_map
*universe(__isl_take isl_map
*map
)
1801 space
= isl_map_get_space(map
);
1803 return isl_map_universe(space
);
1806 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1808 struct isl_un_op_control control
= {
1809 .fn_map
= &universe
,
1811 return un_op(umap
, &control
);
1814 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1816 return isl_union_map_universe(uset
);
1819 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1821 struct isl_un_op_control control
= {
1822 .fn_map
= &isl_map_reverse
,
1824 return un_op(umap
, &control
);
1827 /* Compute the parameter domain of the given union map.
1829 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1831 struct isl_un_op_control control
= {
1832 .fn_map
= &isl_map_params
,
1836 empty
= isl_union_map_is_empty(umap
);
1841 space
= isl_union_map_get_space(umap
);
1842 isl_union_map_free(umap
);
1843 return isl_set_empty(space
);
1845 return isl_set_from_union_set(un_op(umap
, &control
));
1847 isl_union_map_free(umap
);
1851 /* Compute the parameter domain of the given union set.
1853 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1855 return isl_union_map_params(uset
);
1858 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1860 struct isl_un_op_control control
= {
1861 .fn_map
= &isl_map_domain
,
1863 return un_op(umap
, &control
);
1866 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1868 struct isl_un_op_control control
= {
1869 .fn_map
= &isl_map_range
,
1871 return un_op(umap
, &control
);
1874 __isl_give isl_union_map
*isl_union_map_domain_map(
1875 __isl_take isl_union_map
*umap
)
1877 struct isl_un_op_control control
= {
1878 .fn_map
= &isl_map_domain_map
,
1880 return un_op(umap
, &control
);
1883 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1884 * add the result to "res".
1886 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
1888 isl_union_pw_multi_aff
**res
= user
;
1890 isl_pw_multi_aff
*pma
;
1892 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
1893 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
1894 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1896 return *res
? isl_stat_ok
: isl_stat_error
;
1900 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1903 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
1904 __isl_take isl_union_map
*umap
)
1906 isl_union_pw_multi_aff
*res
;
1908 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
1909 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
1910 res
= isl_union_pw_multi_aff_free(res
);
1912 isl_union_map_free(umap
);
1916 __isl_give isl_union_map
*isl_union_map_range_map(
1917 __isl_take isl_union_map
*umap
)
1919 struct isl_un_op_control control
= {
1920 .fn_map
= &isl_map_range_map
,
1922 return un_op(umap
, &control
);
1925 /* Given a collection of wrapped maps of the form A[B -> C],
1926 * return the collection of maps A[B -> C] -> B.
1928 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
1929 __isl_take isl_union_set
*uset
)
1931 struct isl_un_op_control control
= {
1932 .filter
= &isl_set_is_wrapping
,
1933 .fn_map
= &isl_set_wrapped_domain_map
,
1935 return un_op(uset
, &control
);
1938 /* Does "map" relate elements from the same space?
1940 static isl_bool
equal_tuples(__isl_keep isl_map
*map
)
1942 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1943 map
->dim
, isl_dim_out
);
1946 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1948 struct isl_un_op_control control
= {
1949 .filter
= &equal_tuples
,
1950 .fn_map
= &isl_map_deltas
,
1952 return un_op(umap
, &control
);
1955 __isl_give isl_union_map
*isl_union_map_deltas_map(
1956 __isl_take isl_union_map
*umap
)
1958 struct isl_un_op_control control
= {
1959 .filter
= &equal_tuples
,
1960 .fn_map
= &isl_map_deltas_map
,
1962 return un_op(umap
, &control
);
1965 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1967 struct isl_un_op_control control
= {
1968 .fn_map
= &isl_set_identity
,
1970 return un_op(uset
, &control
);
1973 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
1975 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
1977 isl_union_pw_multi_aff
**res
= user
;
1979 isl_pw_multi_aff
*pma
;
1981 space
= isl_space_map_from_set(isl_set_get_space(set
));
1982 pma
= isl_pw_multi_aff_identity(space
);
1983 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
1984 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1986 return *res
? isl_stat_ok
: isl_stat_error
;
1989 /* Return an identity function on "uset" in the form
1990 * of an isl_union_pw_multi_aff.
1992 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
1993 __isl_take isl_union_set
*uset
)
1995 isl_union_pw_multi_aff
*res
;
1997 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
1998 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
1999 res
= isl_union_pw_multi_aff_free(res
);
2001 isl_union_set_free(uset
);
2005 /* For each map in "umap" of the form [A -> B] -> C,
2006 * construct the map A -> C and collect the results.
2008 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
2009 __isl_take isl_union_map
*umap
)
2011 struct isl_un_op_control control
= {
2012 .filter
= &isl_map_domain_is_wrapping
,
2013 .fn_map
= &isl_map_domain_factor_domain
,
2015 return un_op(umap
, &control
);
2018 /* For each map in "umap" of the form [A -> B] -> C,
2019 * construct the map B -> C and collect the results.
2021 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
2022 __isl_take isl_union_map
*umap
)
2024 struct isl_un_op_control control
= {
2025 .filter
= &isl_map_domain_is_wrapping
,
2026 .fn_map
= &isl_map_domain_factor_range
,
2028 return un_op(umap
, &control
);
2031 /* For each map in "umap" of the form A -> [B -> C],
2032 * construct the map A -> B and collect the results.
2034 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2035 __isl_take isl_union_map
*umap
)
2037 struct isl_un_op_control control
= {
2038 .filter
= &isl_map_range_is_wrapping
,
2039 .fn_map
= &isl_map_range_factor_domain
,
2041 return un_op(umap
, &control
);
2044 /* For each map in "umap" of the form A -> [B -> C],
2045 * construct the map A -> C and collect the results.
2047 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2048 __isl_take isl_union_map
*umap
)
2050 struct isl_un_op_control control
= {
2051 .filter
= &isl_map_range_is_wrapping
,
2052 .fn_map
= &isl_map_range_factor_range
,
2054 return un_op(umap
, &control
);
2057 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2058 * construct the map A -> C and collect the results.
2060 __isl_give isl_union_map
*isl_union_map_factor_domain(
2061 __isl_take isl_union_map
*umap
)
2063 struct isl_un_op_control control
= {
2064 .filter
= &isl_map_is_product
,
2065 .fn_map
= &isl_map_factor_domain
,
2067 return un_op(umap
, &control
);
2070 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2071 * construct the map B -> D and collect the results.
2073 __isl_give isl_union_map
*isl_union_map_factor_range(
2074 __isl_take isl_union_map
*umap
)
2076 struct isl_un_op_control control
= {
2077 .filter
= &isl_map_is_product
,
2078 .fn_map
= &isl_map_factor_range
,
2080 return un_op(umap
, &control
);
2083 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2085 struct isl_un_op_control control
= {
2086 .filter
= &isl_set_is_wrapping
,
2087 .fn_map
= &isl_set_unwrap
,
2089 return un_op(uset
, &control
);
2092 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2094 struct isl_un_op_control control
= {
2095 .fn_map
= &isl_map_wrap
,
2097 return un_op(umap
, &control
);
2100 struct isl_union_map_is_subset_data
{
2101 isl_union_map
*umap2
;
2105 static isl_stat
is_subset_entry(void **entry
, void *user
)
2107 struct isl_union_map_is_subset_data
*data
= user
;
2109 struct isl_hash_table_entry
*entry2
;
2110 isl_map
*map
= *entry
;
2112 hash
= isl_space_get_hash(map
->dim
);
2113 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2114 hash
, &has_space
, map
->dim
, 0);
2116 int empty
= isl_map_is_empty(map
);
2118 return isl_stat_error
;
2121 data
->is_subset
= 0;
2122 return isl_stat_error
;
2125 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2126 if (data
->is_subset
< 0 || !data
->is_subset
)
2127 return isl_stat_error
;
2132 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2133 __isl_keep isl_union_map
*umap2
)
2135 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2137 umap1
= isl_union_map_copy(umap1
);
2138 umap2
= isl_union_map_copy(umap2
);
2139 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2140 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2142 if (!umap1
|| !umap2
)
2146 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2147 &is_subset_entry
, &data
) < 0 &&
2151 isl_union_map_free(umap1
);
2152 isl_union_map_free(umap2
);
2154 return data
.is_subset
;
2156 isl_union_map_free(umap1
);
2157 isl_union_map_free(umap2
);
2158 return isl_bool_error
;
2161 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2162 __isl_keep isl_union_set
*uset2
)
2164 return isl_union_map_is_subset(uset1
, uset2
);
2167 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2168 __isl_keep isl_union_map
*umap2
)
2172 if (!umap1
|| !umap2
)
2173 return isl_bool_error
;
2174 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2175 if (is_subset
!= isl_bool_true
)
2177 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2181 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2182 __isl_keep isl_union_set
*uset2
)
2184 return isl_union_map_is_equal(uset1
, uset2
);
2187 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2188 __isl_keep isl_union_map
*umap2
)
2192 if (!umap1
|| !umap2
)
2193 return isl_bool_error
;
2194 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2195 if (is_subset
!= isl_bool_true
)
2197 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2198 if (is_subset
== isl_bool_error
)
2203 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2204 __isl_keep isl_union_set
*uset2
)
2206 return isl_union_map_is_strict_subset(uset1
, uset2
);
2209 /* Internal data structure for isl_union_map_is_disjoint.
2210 * umap2 is the union map with which we are comparing.
2211 * is_disjoint is initialized to 1 and is set to 0 as soon
2212 * as the union maps turn out not to be disjoint.
2214 struct isl_union_map_is_disjoint_data
{
2215 isl_union_map
*umap2
;
2216 isl_bool is_disjoint
;
2219 /* Check if "map" is disjoint from data->umap2 and abort
2220 * the search if it is not.
2222 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2224 struct isl_union_map_is_disjoint_data
*data
= user
;
2226 struct isl_hash_table_entry
*entry2
;
2227 isl_map
*map
= *entry
;
2229 hash
= isl_space_get_hash(map
->dim
);
2230 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2231 hash
, &has_space
, map
->dim
, 0);
2235 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2236 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2237 return isl_stat_error
;
2242 /* Are "umap1" and "umap2" disjoint?
2244 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2245 __isl_keep isl_union_map
*umap2
)
2247 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2249 umap1
= isl_union_map_copy(umap1
);
2250 umap2
= isl_union_map_copy(umap2
);
2251 umap1
= isl_union_map_align_params(umap1
,
2252 isl_union_map_get_space(umap2
));
2253 umap2
= isl_union_map_align_params(umap2
,
2254 isl_union_map_get_space(umap1
));
2256 if (!umap1
|| !umap2
)
2260 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2261 &is_disjoint_entry
, &data
) < 0 &&
2265 isl_union_map_free(umap1
);
2266 isl_union_map_free(umap2
);
2268 return data
.is_disjoint
;
2270 isl_union_map_free(umap1
);
2271 isl_union_map_free(umap2
);
2272 return isl_bool_error
;
2275 /* Are "uset1" and "uset2" disjoint?
2277 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2278 __isl_keep isl_union_set
*uset2
)
2280 return isl_union_map_is_disjoint(uset1
, uset2
);
2283 static isl_stat
sample_entry(void **entry
, void *user
)
2285 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2286 isl_map
*map
= *entry
;
2288 *sample
= isl_map_sample(isl_map_copy(map
));
2290 return isl_stat_error
;
2291 if (!isl_basic_map_plain_is_empty(*sample
))
2292 return isl_stat_error
;
2296 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2298 isl_basic_map
*sample
= NULL
;
2303 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2304 &sample_entry
, &sample
) < 0 &&
2309 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2311 isl_union_map_free(umap
);
2315 isl_union_map_free(umap
);
2319 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2321 return bset_from_bmap(isl_union_map_sample(uset
));
2324 /* Return an element in "uset" in the form of an isl_point.
2325 * Return a void isl_point if "uset" is empty.
2327 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2329 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2332 struct isl_forall_data
{
2334 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2337 static isl_stat
forall_entry(void **entry
, void *user
)
2339 struct isl_forall_data
*data
= user
;
2340 isl_map
*map
= *entry
;
2342 data
->res
= data
->fn(map
);
2344 return isl_stat_error
;
2347 return isl_stat_error
;
2352 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2353 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2355 struct isl_forall_data data
= { isl_bool_true
, fn
};
2358 return isl_bool_error
;
2360 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2361 &forall_entry
, &data
) < 0 && data
.res
)
2362 return isl_bool_error
;
2367 struct isl_forall_user_data
{
2369 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2373 static isl_stat
forall_user_entry(void **entry
, void *user
)
2375 struct isl_forall_user_data
*data
= user
;
2376 isl_map
*map
= *entry
;
2378 data
->res
= data
->fn(map
, data
->user
);
2380 return isl_stat_error
;
2383 return isl_stat_error
;
2388 /* Check if fn(map, user) returns true for all maps "map" in umap.
2390 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2391 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2393 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2396 return isl_bool_error
;
2398 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2399 &forall_user_entry
, &data
) < 0 && data
.res
)
2400 return isl_bool_error
;
2405 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2407 return union_map_forall(umap
, &isl_map_is_empty
);
2410 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2412 return isl_union_map_is_empty(uset
);
2415 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2422 return isl_bool_error
;
2424 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2425 map
->dim
, isl_dim_out
))
2426 return isl_bool_false
;
2428 dim
= isl_map_get_space(map
);
2429 id
= isl_map_identity(dim
);
2431 is_subset
= isl_map_is_subset(map
, id
);
2438 /* Given an isl_union_map that consists of a single map, check
2439 * if it is single-valued.
2441 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2446 umap
= isl_union_map_copy(umap
);
2447 map
= isl_map_from_union_map(umap
);
2448 sv
= isl_map_is_single_valued(map
);
2454 /* Internal data structure for single_valued_on_domain.
2456 * "umap" is the union map to be tested.
2457 * "sv" is set to 1 as long as "umap" may still be single-valued.
2459 struct isl_union_map_is_sv_data
{
2460 isl_union_map
*umap
;
2464 /* Check if the data->umap is single-valued on "set".
2466 * If data->umap consists of a single map on "set", then test it
2469 * Otherwise, compute
2473 * check if the result is a subset of the identity mapping and
2474 * store the result in data->sv.
2476 * Terminate as soon as data->umap has been determined not to
2479 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2481 struct isl_union_map_is_sv_data
*data
= user
;
2482 isl_union_map
*umap
, *test
;
2484 umap
= isl_union_map_copy(data
->umap
);
2485 umap
= isl_union_map_intersect_domain(umap
,
2486 isl_union_set_from_set(set
));
2488 if (isl_union_map_n_map(umap
) == 1) {
2489 data
->sv
= single_map_is_single_valued(umap
);
2490 isl_union_map_free(umap
);
2492 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2493 test
= isl_union_map_apply_range(test
, umap
);
2495 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2497 isl_union_map_free(test
);
2500 if (data
->sv
< 0 || !data
->sv
)
2501 return isl_stat_error
;
2505 /* Check if the given map is single-valued.
2507 * If the union map consists of a single map, then test it as an isl_map.
2508 * Otherwise, check if the union map is single-valued on each of its
2511 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2513 isl_union_map
*universe
;
2514 isl_union_set
*domain
;
2515 struct isl_union_map_is_sv_data data
;
2517 if (isl_union_map_n_map(umap
) == 1)
2518 return single_map_is_single_valued(umap
);
2520 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2521 domain
= isl_union_map_domain(universe
);
2523 data
.sv
= isl_bool_true
;
2525 if (isl_union_set_foreach_set(domain
,
2526 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2527 data
.sv
= isl_bool_error
;
2528 isl_union_set_free(domain
);
2533 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2537 umap
= isl_union_map_copy(umap
);
2538 umap
= isl_union_map_reverse(umap
);
2539 in
= isl_union_map_is_single_valued(umap
);
2540 isl_union_map_free(umap
);
2545 /* Is "map" obviously not an identity relation because
2546 * it maps elements from one space to another space?
2547 * Update *non_identity accordingly.
2549 * In particular, if the domain and range spaces are the same,
2550 * then the map is not considered to obviously not be an identity relation.
2551 * Otherwise, the map is considered to obviously not be an identity relation
2552 * if it is is non-empty.
2554 * If "map" is determined to obviously not be an identity relation,
2555 * then the search is aborted.
2557 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2559 isl_bool
*non_identity
= user
;
2563 space
= isl_map_get_space(map
);
2564 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2565 if (equal
>= 0 && !equal
)
2566 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2568 *non_identity
= isl_bool_not(equal
);
2569 isl_space_free(space
);
2572 if (*non_identity
< 0 || *non_identity
)
2573 return isl_stat_error
;
2578 /* Is "umap" obviously not an identity relation because
2579 * it maps elements from one space to another space?
2581 * As soon as a map has been found that maps elements to a different space,
2582 * non_identity is changed and the search is aborted.
2584 static isl_bool
isl_union_map_plain_is_not_identity(
2585 __isl_keep isl_union_map
*umap
)
2587 isl_bool non_identity
;
2589 non_identity
= isl_bool_false
;
2590 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2591 &non_identity
) < 0 &&
2592 non_identity
== isl_bool_false
)
2593 return isl_bool_error
;
2595 return non_identity
;
2598 /* Does "map" only map elements to themselves?
2599 * Update *identity accordingly.
2601 * If "map" is determined not to be an identity relation,
2602 * then the search is aborted.
2604 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2606 isl_bool
*identity
= user
;
2608 *identity
= isl_map_is_identity(map
);
2611 if (*identity
< 0 || !*identity
)
2612 return isl_stat_error
;
2617 /* Does "umap" only map elements to themselves?
2619 * First check if there are any maps that map elements to different spaces.
2620 * If not, then check that all the maps (between identical spaces)
2621 * are identity relations.
2623 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2625 isl_bool non_identity
;
2628 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2629 if (non_identity
< 0 || non_identity
)
2630 return isl_bool_not(non_identity
);
2632 identity
= isl_bool_true
;
2633 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2634 identity
== isl_bool_true
)
2635 return isl_bool_error
;
2640 /* Represents a map that has a fixed value (v) for one of its
2642 * The map in this structure is not reference counted, so it
2643 * is only valid while the isl_union_map from which it was
2644 * obtained is still alive.
2646 struct isl_fixed_map
{
2651 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2655 struct isl_fixed_map
*v
;
2657 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2660 for (i
= 0; i
< n
; ++i
)
2661 isl_int_init(v
[i
].v
);
2665 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2671 for (i
= 0; i
< n
; ++i
)
2672 isl_int_clear(v
[i
].v
);
2676 /* Compare the "v" field of two isl_fixed_map structs.
2678 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2680 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2681 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2683 return isl_int_cmp(e1
->v
, e2
->v
);
2686 /* Internal data structure used while checking whether all maps
2687 * in a union_map have a fixed value for a given output dimension.
2688 * v is the list of maps, with the fixed value for the dimension
2689 * n is the number of maps considered so far
2690 * pos is the output dimension under investigation
2692 struct isl_fixed_dim_data
{
2693 struct isl_fixed_map
*v
;
2698 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2700 struct isl_fixed_dim_data
*data
= user
;
2702 data
->v
[data
->n
].map
= map
;
2703 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2704 &data
->v
[data
->n
++].v
);
2707 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2708 int first
, int n_range
);
2710 /* Given a list of the maps, with their fixed values at output dimension "pos",
2711 * check whether the ranges of the maps form an obvious partition.
2713 * We first sort the maps according to their fixed values.
2714 * If all maps have a different value, then we know the ranges form
2716 * Otherwise, we collect the maps with the same fixed value and
2717 * check whether each such collection is obviously injective
2718 * based on later dimensions.
2720 static int separates(struct isl_fixed_map
*v
, int n
,
2721 __isl_take isl_space
*dim
, int pos
, int n_range
)
2728 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2730 for (i
= 0; i
+ 1 < n
; ++i
) {
2732 isl_union_map
*part
;
2735 for (j
= i
+ 1; j
< n
; ++j
)
2736 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2742 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2743 for (k
= i
; k
< j
; ++k
)
2744 part
= isl_union_map_add_map(part
,
2745 isl_map_copy(v
[k
].map
));
2747 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2756 isl_space_free(dim
);
2757 free_isl_fixed_map_array(v
, n
);
2760 isl_space_free(dim
);
2761 free_isl_fixed_map_array(v
, n
);
2765 /* Check whether the maps in umap have obviously distinct ranges.
2766 * In particular, check for an output dimension in the range
2767 * [first,n_range) for which all maps have a fixed value
2768 * and then check if these values, possibly along with fixed values
2769 * at later dimensions, entail distinct ranges.
2771 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2772 int first
, int n_range
)
2776 struct isl_fixed_dim_data data
= { NULL
};
2778 ctx
= isl_union_map_get_ctx(umap
);
2780 n
= isl_union_map_n_map(umap
);
2785 isl_union_map_free(umap
);
2786 return isl_bool_true
;
2789 if (first
>= n_range
) {
2790 isl_union_map_free(umap
);
2791 return isl_bool_false
;
2794 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2798 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2804 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2809 dim
= isl_union_map_get_space(umap
);
2810 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2811 isl_union_map_free(umap
);
2815 free_isl_fixed_map_array(data
.v
, n
);
2816 isl_union_map_free(umap
);
2818 return isl_bool_false
;
2820 free_isl_fixed_map_array(data
.v
, n
);
2821 isl_union_map_free(umap
);
2822 return isl_bool_error
;
2825 /* Check whether the maps in umap that map to subsets of "ran"
2826 * have obviously distinct ranges.
2828 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
2831 isl_union_map
*umap
= user
;
2833 umap
= isl_union_map_copy(umap
);
2834 umap
= isl_union_map_intersect_range(umap
,
2835 isl_union_set_from_set(isl_set_copy(ran
)));
2836 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2839 /* Check if the given union_map is obviously injective.
2841 * In particular, we first check if all individual maps are obviously
2842 * injective and then check if all the ranges of these maps are
2843 * obviously disjoint.
2845 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2848 isl_union_map
*univ
;
2851 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2853 return isl_bool_error
;
2855 return isl_bool_false
;
2857 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2858 ran
= isl_union_map_range(univ
);
2860 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2862 isl_union_set_free(ran
);
2867 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2871 sv
= isl_union_map_is_single_valued(umap
);
2875 return isl_union_map_is_injective(umap
);
2878 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2880 struct isl_un_op_control control
= {
2881 .filter
= &isl_map_can_zip
,
2882 .fn_map
= &isl_map_zip
,
2884 return un_op(umap
, &control
);
2887 /* Given a union map, take the maps of the form A -> (B -> C) and
2888 * return the union of the corresponding maps (A -> B) -> C.
2890 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
2892 struct isl_un_op_control control
= {
2893 .filter
= &isl_map_can_uncurry
,
2894 .fn_map
= &isl_map_uncurry
,
2896 return un_op(umap
, &control
);
2899 /* Given a union map, take the maps of the form (A -> B) -> C and
2900 * return the union of the corresponding maps A -> (B -> C).
2902 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
2904 struct isl_un_op_control control
= {
2905 .filter
= &isl_map_can_curry
,
2906 .fn_map
= &isl_map_curry
,
2908 return un_op(umap
, &control
);
2911 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2912 * return the union of the corresponding maps A -> (B -> (C -> D)).
2914 __isl_give isl_union_map
*isl_union_map_range_curry(
2915 __isl_take isl_union_map
*umap
)
2917 struct isl_un_op_control control
= {
2918 .filter
= &isl_map_can_range_curry
,
2919 .fn_map
= &isl_map_range_curry
,
2921 return un_op(umap
, &control
);
2924 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
2926 struct isl_un_op_control control
= {
2927 .fn_map
= &isl_set_lift
,
2929 return un_op(uset
, &control
);
2932 static isl_stat
coefficients_entry(void **entry
, void *user
)
2934 isl_set
*set
= *entry
;
2935 isl_union_set
**res
= user
;
2937 set
= isl_set_copy(set
);
2938 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
2939 *res
= isl_union_set_add_set(*res
, set
);
2944 __isl_give isl_union_set
*isl_union_set_coefficients(
2945 __isl_take isl_union_set
*uset
)
2954 ctx
= isl_union_set_get_ctx(uset
);
2955 dim
= isl_space_set_alloc(ctx
, 0, 0);
2956 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2957 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2958 &coefficients_entry
, &res
) < 0)
2961 isl_union_set_free(uset
);
2964 isl_union_set_free(uset
);
2965 isl_union_set_free(res
);
2969 static isl_stat
solutions_entry(void **entry
, void *user
)
2971 isl_set
*set
= *entry
;
2972 isl_union_set
**res
= user
;
2974 set
= isl_set_copy(set
);
2975 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2977 *res
= isl_union_set_from_set(set
);
2979 *res
= isl_union_set_add_set(*res
, set
);
2982 return isl_stat_error
;
2987 __isl_give isl_union_set
*isl_union_set_solutions(
2988 __isl_take isl_union_set
*uset
)
2990 isl_union_set
*res
= NULL
;
2995 if (uset
->table
.n
== 0) {
2996 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2997 isl_union_set_free(uset
);
3001 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
3002 &solutions_entry
, &res
) < 0)
3005 isl_union_set_free(uset
);
3008 isl_union_set_free(uset
);
3009 isl_union_set_free(res
);
3013 /* Is the domain space of "map" equal to "space"?
3015 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3017 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
3018 space
, isl_dim_out
);
3021 /* Is the range space of "map" equal to "space"?
3023 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3025 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3026 space
, isl_dim_out
);
3029 /* Is the set space of "map" equal to "space"?
3031 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3033 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3034 space
, isl_dim_out
);
3037 /* Internal data structure for preimage_pw_multi_aff.
3039 * "pma" is the function under which the preimage should be taken.
3040 * "space" is the space of "pma".
3041 * "res" collects the results.
3042 * "fn" computes the preimage for a given map.
3043 * "match" returns true if "fn" can be called.
3045 struct isl_union_map_preimage_data
{
3047 isl_pw_multi_aff
*pma
;
3049 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3050 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3051 __isl_take isl_pw_multi_aff
*pma
);
3054 /* Call data->fn to compute the preimage of the domain or range of *entry
3055 * under the function represented by data->pma, provided the domain/range
3056 * space of *entry matches the target space of data->pma
3057 * (as given by data->match), and add the result to data->res.
3059 static isl_stat
preimage_entry(void **entry
, void *user
)
3062 isl_map
*map
= *entry
;
3063 struct isl_union_map_preimage_data
*data
= user
;
3066 m
= data
->match(map
, data
->space
);
3068 return isl_stat_error
;
3072 map
= isl_map_copy(map
);
3073 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3075 empty
= isl_map_is_empty(map
);
3076 if (empty
< 0 || empty
) {
3078 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3081 data
->res
= isl_union_map_add_map(data
->res
, map
);
3086 /* Compute the preimage of the domain or range of "umap" under the function
3087 * represented by "pma".
3088 * In other words, plug in "pma" in the domain or range of "umap".
3089 * The function "fn" performs the actual preimage computation on a map,
3090 * while "match" determines to which maps the function should be applied.
3092 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3093 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3094 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3095 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3096 __isl_take isl_pw_multi_aff
*pma
))
3100 struct isl_union_map_preimage_data data
;
3102 umap
= isl_union_map_align_params(umap
,
3103 isl_pw_multi_aff_get_space(pma
));
3104 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3109 ctx
= isl_union_map_get_ctx(umap
);
3110 space
= isl_union_map_get_space(umap
);
3111 data
.space
= isl_pw_multi_aff_get_space(pma
);
3113 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3116 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3118 data
.res
= isl_union_map_free(data
.res
);
3120 isl_space_free(data
.space
);
3121 isl_union_map_free(umap
);
3122 isl_pw_multi_aff_free(pma
);
3125 isl_union_map_free(umap
);
3126 isl_pw_multi_aff_free(pma
);
3130 /* Compute the preimage of the domain of "umap" under the function
3131 * represented by "pma".
3132 * In other words, plug in "pma" in the domain of "umap".
3133 * The result contains maps that live in the same spaces as the maps of "umap"
3134 * with domain space equal to the target space of "pma",
3135 * except that the domain has been replaced by the domain space of "pma".
3137 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3138 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3140 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3141 &isl_map_preimage_domain_pw_multi_aff
);
3144 /* Compute the preimage of the range of "umap" under the function
3145 * represented by "pma".
3146 * In other words, plug in "pma" in the range of "umap".
3147 * The result contains maps that live in the same spaces as the maps of "umap"
3148 * with range space equal to the target space of "pma",
3149 * except that the range has been replaced by the domain space of "pma".
3151 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3152 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3154 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3155 &isl_map_preimage_range_pw_multi_aff
);
3158 /* Compute the preimage of "uset" under the function represented by "pma".
3159 * In other words, plug in "pma" in "uset".
3160 * The result contains sets that live in the same spaces as the sets of "uset"
3161 * with space equal to the target space of "pma",
3162 * except that the space has been replaced by the domain space of "pma".
3164 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3165 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3167 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3168 &isl_set_preimage_pw_multi_aff
);
3171 /* Compute the preimage of the domain of "umap" under the function
3172 * represented by "ma".
3173 * In other words, plug in "ma" in the domain of "umap".
3174 * The result contains maps that live in the same spaces as the maps of "umap"
3175 * with domain space equal to the target space of "ma",
3176 * except that the domain has been replaced by the domain space of "ma".
3178 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3179 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3181 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3182 isl_pw_multi_aff_from_multi_aff(ma
));
3185 /* Compute the preimage of the range of "umap" under the function
3186 * represented by "ma".
3187 * In other words, plug in "ma" in the range of "umap".
3188 * The result contains maps that live in the same spaces as the maps of "umap"
3189 * with range space equal to the target space of "ma",
3190 * except that the range has been replaced by the domain space of "ma".
3192 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3193 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3195 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3196 isl_pw_multi_aff_from_multi_aff(ma
));
3199 /* Compute the preimage of "uset" under the function represented by "ma".
3200 * In other words, plug in "ma" in "uset".
3201 * The result contains sets that live in the same spaces as the sets of "uset"
3202 * with space equal to the target space of "ma",
3203 * except that the space has been replaced by the domain space of "ma".
3205 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3206 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3208 return isl_union_set_preimage_pw_multi_aff(uset
,
3209 isl_pw_multi_aff_from_multi_aff(ma
));
3212 /* Internal data structure for preimage_multi_pw_aff.
3214 * "mpa" is the function under which the preimage should be taken.
3215 * "space" is the space of "mpa".
3216 * "res" collects the results.
3217 * "fn" computes the preimage for a given map.
3218 * "match" returns true if "fn" can be called.
3220 struct isl_union_map_preimage_mpa_data
{
3222 isl_multi_pw_aff
*mpa
;
3224 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3225 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3226 __isl_take isl_multi_pw_aff
*mpa
);
3229 /* Call data->fn to compute the preimage of the domain or range of *entry
3230 * under the function represented by data->mpa, provided the domain/range
3231 * space of *entry matches the target space of data->mpa
3232 * (as given by data->match), and add the result to data->res.
3234 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3237 isl_map
*map
= *entry
;
3238 struct isl_union_map_preimage_mpa_data
*data
= user
;
3241 m
= data
->match(map
, data
->space
);
3243 return isl_stat_error
;
3247 map
= isl_map_copy(map
);
3248 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3250 empty
= isl_map_is_empty(map
);
3251 if (empty
< 0 || empty
) {
3253 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3256 data
->res
= isl_union_map_add_map(data
->res
, map
);
3261 /* Compute the preimage of the domain or range of "umap" under the function
3262 * represented by "mpa".
3263 * In other words, plug in "mpa" in the domain or range of "umap".
3264 * The function "fn" performs the actual preimage computation on a map,
3265 * while "match" determines to which maps the function should be applied.
3267 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3268 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3269 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3270 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3271 __isl_take isl_multi_pw_aff
*mpa
))
3275 struct isl_union_map_preimage_mpa_data data
;
3277 umap
= isl_union_map_align_params(umap
,
3278 isl_multi_pw_aff_get_space(mpa
));
3279 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3284 ctx
= isl_union_map_get_ctx(umap
);
3285 space
= isl_union_map_get_space(umap
);
3286 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3288 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3291 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3293 data
.res
= isl_union_map_free(data
.res
);
3295 isl_space_free(data
.space
);
3296 isl_union_map_free(umap
);
3297 isl_multi_pw_aff_free(mpa
);
3300 isl_union_map_free(umap
);
3301 isl_multi_pw_aff_free(mpa
);
3305 /* Compute the preimage of the domain of "umap" under the function
3306 * represented by "mpa".
3307 * In other words, plug in "mpa" in the domain of "umap".
3308 * The result contains maps that live in the same spaces as the maps of "umap"
3309 * with domain space equal to the target space of "mpa",
3310 * except that the domain has been replaced by the domain space of "mpa".
3312 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3313 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3315 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3316 &isl_map_preimage_domain_multi_pw_aff
);
3319 /* Internal data structure for preimage_upma.
3321 * "umap" is the map of which the preimage should be computed.
3322 * "res" collects the results.
3323 * "fn" computes the preimage for a given piecewise multi-affine function.
3325 struct isl_union_map_preimage_upma_data
{
3326 isl_union_map
*umap
;
3328 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3329 __isl_take isl_pw_multi_aff
*pma
);
3332 /* Call data->fn to compute the preimage of the domain or range of data->umap
3333 * under the function represented by pma and add the result to data->res.
3335 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3337 struct isl_union_map_preimage_upma_data
*data
= user
;
3338 isl_union_map
*umap
;
3340 umap
= isl_union_map_copy(data
->umap
);
3341 umap
= data
->fn(umap
, pma
);
3342 data
->res
= isl_union_map_union(data
->res
, umap
);
3344 return data
->res
? isl_stat_ok
: isl_stat_error
;
3347 /* Compute the preimage of the domain or range of "umap" under the function
3348 * represented by "upma".
3349 * In other words, plug in "upma" in the domain or range of "umap".
3350 * The function "fn" performs the actual preimage computation
3351 * on a piecewise multi-affine function.
3353 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3354 __isl_take isl_union_map
*umap
,
3355 __isl_take isl_union_pw_multi_aff
*upma
,
3356 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3357 __isl_take isl_pw_multi_aff
*pma
))
3359 struct isl_union_map_preimage_upma_data data
;
3362 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3364 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3365 &preimage_upma
, &data
) < 0)
3366 data
.res
= isl_union_map_free(data
.res
);
3368 isl_union_map_free(umap
);
3369 isl_union_pw_multi_aff_free(upma
);
3374 /* Compute the preimage of the domain of "umap" under the function
3375 * represented by "upma".
3376 * In other words, plug in "upma" in the domain of "umap".
3377 * The result contains maps that live in the same spaces as the maps of "umap"
3378 * with domain space equal to one of the target spaces of "upma",
3379 * except that the domain has been replaced by one of the the domain spaces that
3380 * corresponds to that target space of "upma".
3382 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3383 __isl_take isl_union_map
*umap
,
3384 __isl_take isl_union_pw_multi_aff
*upma
)
3386 return preimage_union_pw_multi_aff(umap
, upma
,
3387 &isl_union_map_preimage_domain_pw_multi_aff
);
3390 /* Compute the preimage of the range of "umap" under the function
3391 * represented by "upma".
3392 * In other words, plug in "upma" in the range of "umap".
3393 * The result contains maps that live in the same spaces as the maps of "umap"
3394 * with range space equal to one of the target spaces of "upma",
3395 * except that the range has been replaced by one of the the domain spaces that
3396 * corresponds to that target space of "upma".
3398 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3399 __isl_take isl_union_map
*umap
,
3400 __isl_take isl_union_pw_multi_aff
*upma
)
3402 return preimage_union_pw_multi_aff(umap
, upma
,
3403 &isl_union_map_preimage_range_pw_multi_aff
);
3406 /* Compute the preimage of "uset" under the function represented by "upma".
3407 * In other words, plug in "upma" in the range of "uset".
3408 * The result contains sets that live in the same spaces as the sets of "uset"
3409 * with space equal to one of the target spaces of "upma",
3410 * except that the space has been replaced by one of the the domain spaces that
3411 * corresponds to that target space of "upma".
3413 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3414 __isl_take isl_union_set
*uset
,
3415 __isl_take isl_union_pw_multi_aff
*upma
)
3417 return preimage_union_pw_multi_aff(uset
, upma
,
3418 &isl_union_set_preimage_pw_multi_aff
);
3421 /* Reset the user pointer on all identifiers of parameters and tuples
3422 * of the spaces of "umap".
3424 __isl_give isl_union_map
*isl_union_map_reset_user(
3425 __isl_take isl_union_map
*umap
)
3427 umap
= isl_union_map_cow(umap
);
3430 umap
->dim
= isl_space_reset_user(umap
->dim
);
3432 return isl_union_map_free(umap
);
3433 return total(umap
, &isl_map_reset_user
);
3436 /* Reset the user pointer on all identifiers of parameters and tuples
3437 * of the spaces of "uset".
3439 __isl_give isl_union_set
*isl_union_set_reset_user(
3440 __isl_take isl_union_set
*uset
)
3442 return isl_union_map_reset_user(uset
);
3445 /* Remove all existentially quantified variables and integer divisions
3446 * from "umap" using Fourier-Motzkin elimination.
3448 __isl_give isl_union_map
*isl_union_map_remove_divs(
3449 __isl_take isl_union_map
*umap
)
3451 return total(umap
, &isl_map_remove_divs
);
3454 /* Remove all existentially quantified variables and integer divisions
3455 * from "uset" using Fourier-Motzkin elimination.
3457 __isl_give isl_union_set
*isl_union_set_remove_divs(
3458 __isl_take isl_union_set
*uset
)
3460 return isl_union_map_remove_divs(uset
);
3463 /* Internal data structure for isl_union_map_project_out.
3464 * "type", "first" and "n" are the arguments for the isl_map_project_out
3466 * "res" collects the results.
3468 struct isl_union_map_project_out_data
{
3469 enum isl_dim_type type
;
3476 /* Turn the data->n dimensions of type data->type, starting at data->first
3477 * into existentially quantified variables and add the result to data->res.
3479 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3481 struct isl_union_map_project_out_data
*data
= user
;
3483 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3484 data
->res
= isl_union_map_add_map(data
->res
, map
);
3489 /* Turn the "n" dimensions of type "type", starting at "first"
3490 * into existentially quantified variables.
3491 * Since the space of an isl_union_map only contains parameters,
3492 * type is required to be equal to isl_dim_param.
3494 __isl_give isl_union_map
*isl_union_map_project_out(
3495 __isl_take isl_union_map
*umap
,
3496 enum isl_dim_type type
, unsigned first
, unsigned n
)
3499 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3504 if (type
!= isl_dim_param
)
3505 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3506 "can only project out parameters",
3507 return isl_union_map_free(umap
));
3509 space
= isl_union_map_get_space(umap
);
3510 space
= isl_space_drop_dims(space
, type
, first
, n
);
3511 data
.res
= isl_union_map_empty(space
);
3512 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3513 data
.res
= isl_union_map_free(data
.res
);
3515 isl_union_map_free(umap
);
3520 /* Turn the "n" dimensions of type "type", starting at "first"
3521 * into existentially quantified variables.
3522 * Since the space of an isl_union_set only contains parameters,
3523 * "type" is required to be equal to isl_dim_param.
3525 __isl_give isl_union_set
*isl_union_set_project_out(
3526 __isl_take isl_union_set
*uset
,
3527 enum isl_dim_type type
, unsigned first
, unsigned n
)
3529 return isl_union_map_project_out(uset
, type
, first
, n
);
3532 /* Internal data structure for isl_union_map_involves_dims.
3533 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3535 struct isl_union_map_involves_dims_data
{
3540 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3542 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3544 struct isl_union_map_involves_dims_data
*data
= user
;
3547 involves
= isl_map_involves_dims(map
,
3548 isl_dim_param
, data
->first
, data
->n
);
3550 return isl_bool_error
;
3554 /* Does "umap" involve any of the n parameters starting at first?
3555 * "type" is required to be set to isl_dim_param.
3557 * "umap" involves any of those parameters if any of its maps
3558 * involve the parameters. In other words, "umap" does not
3559 * involve any of the parameters if all its maps to not
3560 * involve the parameters.
3562 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3563 enum isl_dim_type type
, unsigned first
, unsigned n
)
3565 struct isl_union_map_involves_dims_data data
= { first
, n
};
3568 if (type
!= isl_dim_param
)
3569 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3570 "can only reference parameters", return isl_bool_error
);
3572 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3575 return isl_bool_error
;
3580 /* Internal data structure for isl_union_map_reset_range_space.
3581 * "range" is the space from which to set the range space.
3582 * "res" collects the results.
3584 struct isl_union_map_reset_range_space_data
{
3589 /* Replace the range space of "map" by the range space of data->range and
3590 * add the result to data->res.
3592 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3594 struct isl_union_map_reset_range_space_data
*data
= user
;
3597 space
= isl_map_get_space(map
);
3598 space
= isl_space_domain(space
);
3599 space
= isl_space_extend_domain_with_range(space
,
3600 isl_space_copy(data
->range
));
3601 map
= isl_map_reset_space(map
, space
);
3602 data
->res
= isl_union_map_add_map(data
->res
, map
);
3604 return data
->res
? isl_stat_ok
: isl_stat_error
;
3607 /* Replace the range space of all the maps in "umap" by
3608 * the range space of "space".
3610 * This assumes that all maps have the same output dimension.
3611 * This function should therefore not be made publicly available.
3613 * Since the spaces of the maps change, so do their hash value.
3614 * We therefore need to create a new isl_union_map.
3616 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3617 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3619 struct isl_union_map_reset_range_space_data data
= { space
};
3621 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3622 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
3623 data
.res
= isl_union_map_free(data
.res
);
3625 isl_space_free(space
);
3626 isl_union_map_free(umap
);
3630 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3631 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3632 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3633 * to the domain and the range of each map.
3634 * "res" collects the results.
3636 struct isl_union_order_at_data
{
3637 isl_multi_union_pw_aff
*mupa
;
3638 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3639 __isl_take isl_multi_pw_aff
*mpa2
);
3643 /* Intersect "map" with the result of applying data->order to
3644 * the functions in data->mupa that apply to the domain and the range
3645 * of "map" and add the result to data->res.
3647 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
3649 struct isl_union_order_at_data
*data
= user
;
3651 isl_multi_pw_aff
*mpa1
, *mpa2
;
3654 space
= isl_space_domain(isl_map_get_space(map
));
3655 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3656 space
= isl_space_range(isl_map_get_space(map
));
3657 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3658 order
= data
->order(mpa1
, mpa2
);
3659 map
= isl_map_intersect(map
, order
);
3660 data
->res
= isl_union_map_add_map(data
->res
, map
);
3662 return data
->res
? isl_stat_ok
: isl_stat_error
;
3665 /* Intersect each map in "umap" with the result of calling "order"
3666 * on the functions is "mupa" that apply to the domain and the range
3669 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
3670 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
3671 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3672 __isl_take isl_multi_pw_aff
*mpa2
))
3674 struct isl_union_order_at_data data
;
3676 umap
= isl_union_map_align_params(umap
,
3677 isl_multi_union_pw_aff_get_space(mupa
));
3678 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
3679 isl_union_map_get_space(umap
));
3682 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3683 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
3684 data
.res
= isl_union_map_free(data
.res
);
3686 isl_multi_union_pw_aff_free(mupa
);
3687 isl_union_map_free(umap
);
3691 /* Return the subset of "umap" where the domain and the range
3692 * have equal "mupa" values.
3694 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
3695 __isl_take isl_union_map
*umap
,
3696 __isl_take isl_multi_union_pw_aff
*mupa
)
3698 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3699 &isl_multi_pw_aff_eq_map
);
3702 /* Return the subset of "umap" where the domain has a lexicographically
3703 * smaller "mupa" value than the range.
3705 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
3706 __isl_take isl_union_map
*umap
,
3707 __isl_take isl_multi_union_pw_aff
*mupa
)
3709 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3710 &isl_multi_pw_aff_lex_lt_map
);
3713 /* Return the subset of "umap" where the domain has a lexicographically
3714 * greater "mupa" value than the range.
3716 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
3717 __isl_take isl_union_map
*umap
,
3718 __isl_take isl_multi_union_pw_aff
*mupa
)
3720 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3721 &isl_multi_pw_aff_lex_gt_map
);
3724 /* Return the union of the elements in the list "list".
3726 __isl_give isl_union_set
*isl_union_set_list_union(
3727 __isl_take isl_union_set_list
*list
)
3737 ctx
= isl_union_set_list_get_ctx(list
);
3738 space
= isl_space_params_alloc(ctx
, 0);
3739 res
= isl_union_set_empty(space
);
3741 n
= isl_union_set_list_n_union_set(list
);
3742 for (i
= 0; i
< n
; ++i
) {
3743 isl_union_set
*uset_i
;
3745 uset_i
= isl_union_set_list_get_union_set(list
, i
);
3746 res
= isl_union_set_union(res
, uset_i
);
3749 isl_union_set_list_free(list
);
3753 /* Update *hash with the hash value of "map".
3755 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
3757 uint32_t *hash
= user
;
3760 map_hash
= isl_map_get_hash(map
);
3761 isl_hash_hash(*hash
, map_hash
);
3767 /* Return a hash value that digests "umap".
3769 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
3776 hash
= isl_hash_init();
3777 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
3783 /* Return a hash value that digests "uset".
3785 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
3787 return isl_union_map_get_hash(uset
);
3790 /* Add the number of basic sets in "set" to "n".
3792 static isl_stat
add_n(__isl_take isl_set
*set
, void *user
)
3796 *n
+= isl_set_n_basic_set(set
);
3802 /* Return the total number of basic sets in "uset".
3804 int isl_union_set_n_basic_set(__isl_keep isl_union_set
*uset
)
3808 if (isl_union_set_foreach_set(uset
, &add_n
, &n
) < 0)
3814 /* Add the basic sets in "set" to "list".
3816 static isl_stat
add_list(__isl_take isl_set
*set
, void *user
)
3818 isl_basic_set_list
**list
= user
;
3819 isl_basic_set_list
*list_i
;
3821 list_i
= isl_set_get_basic_set_list(set
);
3822 *list
= isl_basic_set_list_concat(*list
, list_i
);
3826 return isl_stat_error
;
3830 /* Return a list containing all the basic sets in "uset".
3832 * First construct a list of the appropriate size and
3833 * then add all the elements.
3835 __isl_give isl_basic_set_list
*isl_union_set_get_basic_set_list(
3836 __isl_keep isl_union_set
*uset
)
3840 isl_basic_set_list
*list
;
3844 ctx
= isl_union_set_get_ctx(uset
);
3845 n
= isl_union_set_n_basic_set(uset
);
3848 list
= isl_basic_set_list_alloc(ctx
, n
);
3849 if (isl_union_set_foreach_set(uset
, &add_list
, &list
) < 0)
3850 list
= isl_basic_set_list_free(list
);