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/deprecated/union_map_int.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
32 /* Return the number of parameters of "umap", where "type"
33 * is required to be set to isl_dim_param.
35 unsigned isl_union_map_dim(__isl_keep isl_union_map
*umap
,
36 enum isl_dim_type type
)
41 if (type
!= isl_dim_param
)
42 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
43 "can only reference parameters", return 0);
45 return isl_space_dim(umap
->dim
, type
);
48 /* Return the number of parameters of "uset", where "type"
49 * is required to be set to isl_dim_param.
51 unsigned isl_union_set_dim(__isl_keep isl_union_set
*uset
,
52 enum isl_dim_type type
)
54 return isl_union_map_dim(uset
, type
);
57 /* Return the id of the specified dimension.
59 __isl_give isl_id
*isl_union_map_get_dim_id(__isl_keep isl_union_map
*umap
,
60 enum isl_dim_type type
, unsigned pos
)
65 if (type
!= isl_dim_param
)
66 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
67 "can only reference parameters", return NULL
);
69 return isl_space_get_dim_id(umap
->dim
, type
, pos
);
72 /* Is this union set a parameter domain?
74 isl_bool
isl_union_set_is_params(__isl_keep isl_union_set
*uset
)
80 return isl_bool_error
;
81 if (uset
->table
.n
!= 1)
82 return isl_bool_false
;
84 set
= isl_set_from_union_set(isl_union_set_copy(uset
));
85 params
= isl_set_is_params(set
);
90 static __isl_give isl_union_map
*isl_union_map_alloc(
91 __isl_take isl_space
*space
, int size
)
95 space
= isl_space_params(space
);
99 umap
= isl_calloc_type(space
->ctx
, isl_union_map
);
101 isl_space_free(space
);
107 if (isl_hash_table_init(space
->ctx
, &umap
->table
, size
) < 0)
108 return isl_union_map_free(umap
);
113 __isl_give isl_union_map
*isl_union_map_empty(__isl_take isl_space
*dim
)
115 return isl_union_map_alloc(dim
, 16);
118 __isl_give isl_union_set
*isl_union_set_empty(__isl_take isl_space
*dim
)
120 return isl_union_map_empty(dim
);
123 isl_ctx
*isl_union_map_get_ctx(__isl_keep isl_union_map
*umap
)
125 return umap
? umap
->dim
->ctx
: NULL
;
128 isl_ctx
*isl_union_set_get_ctx(__isl_keep isl_union_set
*uset
)
130 return uset
? uset
->dim
->ctx
: NULL
;
133 /* Return the space of "umap".
135 __isl_keep isl_space
*isl_union_map_peek_space(__isl_keep isl_union_map
*umap
)
137 return umap
? umap
->dim
: NULL
;
140 __isl_give isl_space
*isl_union_map_get_space(__isl_keep isl_union_map
*umap
)
142 return isl_space_copy(isl_union_map_peek_space(umap
));
145 /* Return the position of the parameter with the given name
147 * Return -1 if no such dimension can be found.
149 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map
*umap
,
150 enum isl_dim_type type
, const char *name
)
154 return isl_space_find_dim_by_name(umap
->dim
, type
, name
);
157 __isl_give isl_space
*isl_union_set_get_space(__isl_keep isl_union_set
*uset
)
159 return isl_union_map_get_space(uset
);
162 static isl_stat
free_umap_entry(void **entry
, void *user
)
164 isl_map
*map
= *entry
;
169 static isl_stat
add_map(__isl_take isl_map
*map
, void *user
)
171 isl_union_map
**umap
= (isl_union_map
**)user
;
173 *umap
= isl_union_map_add_map(*umap
, map
);
178 __isl_give isl_union_map
*isl_union_map_dup(__isl_keep isl_union_map
*umap
)
185 dup
= isl_union_map_empty(isl_space_copy(umap
->dim
));
186 if (isl_union_map_foreach_map(umap
, &add_map
, &dup
) < 0)
190 isl_union_map_free(dup
);
194 __isl_give isl_union_map
*isl_union_map_cow(__isl_take isl_union_map
*umap
)
202 return isl_union_map_dup(umap
);
205 struct isl_union_align
{
210 static isl_stat
align_entry(void **entry
, void *user
)
212 isl_map
*map
= *entry
;
214 struct isl_union_align
*data
= user
;
216 exp
= isl_reordering_extend_space(isl_reordering_copy(data
->exp
),
217 isl_map_get_space(map
));
219 data
->res
= isl_union_map_add_map(data
->res
,
220 isl_map_realign(isl_map_copy(map
), exp
));
225 /* Align the parameters of umap along those of model.
226 * The result has the parameters of model first, in the same order
227 * as they appear in model, followed by any remaining parameters of
228 * umap that do not appear in model.
230 __isl_give isl_union_map
*isl_union_map_align_params(
231 __isl_take isl_union_map
*umap
, __isl_take isl_space
*model
)
233 struct isl_union_align data
= { NULL
, NULL
};
234 isl_bool equal_params
;
239 equal_params
= isl_space_has_equal_params(umap
->dim
, model
);
240 if (equal_params
< 0)
243 isl_space_free(model
);
247 model
= isl_space_params(model
);
248 data
.exp
= isl_parameter_alignment_reordering(umap
->dim
, model
);
252 data
.res
= isl_union_map_alloc(isl_space_copy(data
.exp
->dim
),
254 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
255 &align_entry
, &data
) < 0)
258 isl_reordering_free(data
.exp
);
259 isl_union_map_free(umap
);
260 isl_space_free(model
);
263 isl_reordering_free(data
.exp
);
264 isl_union_map_free(umap
);
265 isl_union_map_free(data
.res
);
266 isl_space_free(model
);
270 __isl_give isl_union_set
*isl_union_set_align_params(
271 __isl_take isl_union_set
*uset
, __isl_take isl_space
*model
)
273 return isl_union_map_align_params(uset
, model
);
276 __isl_give isl_union_map
*isl_union_map_union(__isl_take isl_union_map
*umap1
,
277 __isl_take isl_union_map
*umap2
)
279 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
280 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
282 umap1
= isl_union_map_cow(umap1
);
284 if (!umap1
|| !umap2
)
287 if (isl_union_map_foreach_map(umap2
, &add_map
, &umap1
) < 0)
290 isl_union_map_free(umap2
);
294 isl_union_map_free(umap1
);
295 isl_union_map_free(umap2
);
299 __isl_give isl_union_set
*isl_union_set_union(__isl_take isl_union_set
*uset1
,
300 __isl_take isl_union_set
*uset2
)
302 return isl_union_map_union(uset1
, uset2
);
305 __isl_give isl_union_map
*isl_union_map_copy(__isl_keep isl_union_map
*umap
)
314 __isl_give isl_union_set
*isl_union_set_copy(__isl_keep isl_union_set
*uset
)
316 return isl_union_map_copy(uset
);
319 __isl_null isl_union_map
*isl_union_map_free(__isl_take isl_union_map
*umap
)
327 isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
328 &free_umap_entry
, NULL
);
329 isl_hash_table_clear(&umap
->table
);
330 isl_space_free(umap
->dim
);
335 __isl_null isl_union_set
*isl_union_set_free(__isl_take isl_union_set
*uset
)
337 return isl_union_map_free(uset
);
340 /* Do "umap" and "space" have the same parameters?
342 isl_bool
isl_union_map_space_has_equal_params(__isl_keep isl_union_map
*umap
,
343 __isl_keep isl_space
*space
)
345 isl_space
*umap_space
;
347 umap_space
= isl_union_map_peek_space(umap
);
348 return isl_space_has_equal_params(umap_space
, space
);
351 static int has_space(const void *entry
, const void *val
)
353 isl_map
*map
= (isl_map
*)entry
;
354 isl_space
*space
= (isl_space
*) val
;
356 return isl_space_is_equal(map
->dim
, space
);
359 __isl_give isl_union_map
*isl_union_map_add_map(__isl_take isl_union_map
*umap
,
360 __isl_take isl_map
*map
)
363 struct isl_hash_table_entry
*entry
;
369 if (isl_map_plain_is_empty(map
)) {
374 aligned
= isl_map_space_has_equal_params(map
, umap
->dim
);
378 umap
= isl_union_map_align_params(umap
, isl_map_get_space(map
));
379 map
= isl_map_align_params(map
, isl_union_map_get_space(umap
));
382 umap
= isl_union_map_cow(umap
);
387 hash
= isl_space_get_hash(map
->dim
);
388 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
389 &has_space
, map
->dim
, 1);
396 entry
->data
= isl_map_union(entry
->data
, isl_map_copy(map
));
405 isl_union_map_free(umap
);
409 __isl_give isl_union_set
*isl_union_set_add_set(__isl_take isl_union_set
*uset
,
410 __isl_take isl_set
*set
)
412 return isl_union_map_add_map(uset
, set_to_map(set
));
415 __isl_give isl_union_map
*isl_union_map_from_map(__isl_take isl_map
*map
)
423 dim
= isl_map_get_space(map
);
424 dim
= isl_space_params(dim
);
425 umap
= isl_union_map_empty(dim
);
426 umap
= isl_union_map_add_map(umap
, map
);
431 __isl_give isl_union_set
*isl_union_set_from_set(__isl_take isl_set
*set
)
433 return isl_union_map_from_map(set_to_map(set
));
436 __isl_give isl_union_map
*isl_union_map_from_basic_map(
437 __isl_take isl_basic_map
*bmap
)
439 return isl_union_map_from_map(isl_map_from_basic_map(bmap
));
442 __isl_give isl_union_set
*isl_union_set_from_basic_set(
443 __isl_take isl_basic_set
*bset
)
445 return isl_union_map_from_basic_map(bset
);
448 struct isl_union_map_foreach_data
450 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
);
454 static isl_stat
call_on_copy(void **entry
, void *user
)
456 isl_map
*map
= *entry
;
457 struct isl_union_map_foreach_data
*data
;
458 data
= (struct isl_union_map_foreach_data
*)user
;
460 return data
->fn(isl_map_copy(map
), data
->user
);
463 int isl_union_map_n_map(__isl_keep isl_union_map
*umap
)
465 return umap
? umap
->table
.n
: 0;
468 int isl_union_set_n_set(__isl_keep isl_union_set
*uset
)
470 return uset
? uset
->table
.n
: 0;
473 isl_stat
isl_union_map_foreach_map(__isl_keep isl_union_map
*umap
,
474 isl_stat (*fn
)(__isl_take isl_map
*map
, void *user
), void *user
)
476 struct isl_union_map_foreach_data data
= { fn
, user
};
479 return isl_stat_error
;
481 return isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
482 &call_on_copy
, &data
);
485 static isl_stat
copy_map(void **entry
, void *user
)
487 isl_map
*map
= *entry
;
488 isl_map
**map_p
= user
;
490 *map_p
= isl_map_copy(map
);
492 return isl_stat_error
;
495 __isl_give isl_map
*isl_map_from_union_map(__isl_take isl_union_map
*umap
)
502 ctx
= isl_union_map_get_ctx(umap
);
503 if (umap
->table
.n
!= 1)
504 isl_die(ctx
, isl_error_invalid
,
505 "union map needs to contain elements in exactly "
506 "one space", goto error
);
508 isl_hash_table_foreach(ctx
, &umap
->table
, ©_map
, &map
);
510 isl_union_map_free(umap
);
514 isl_union_map_free(umap
);
518 __isl_give isl_set
*isl_set_from_union_set(__isl_take isl_union_set
*uset
)
520 return isl_map_from_union_map(uset
);
523 /* Extract the map in "umap" that lives in the given space (ignoring
526 __isl_give isl_map
*isl_union_map_extract_map(__isl_keep isl_union_map
*umap
,
527 __isl_take isl_space
*space
)
530 struct isl_hash_table_entry
*entry
;
532 space
= isl_space_drop_dims(space
, isl_dim_param
,
533 0, isl_space_dim(space
, isl_dim_param
));
534 space
= isl_space_align_params(space
, isl_union_map_get_space(umap
));
538 hash
= isl_space_get_hash(space
);
539 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
540 &has_space
, space
, 0);
542 return isl_map_empty(space
);
543 isl_space_free(space
);
544 return isl_map_copy(entry
->data
);
546 isl_space_free(space
);
550 __isl_give isl_set
*isl_union_set_extract_set(__isl_keep isl_union_set
*uset
,
551 __isl_take isl_space
*dim
)
553 return set_from_map(isl_union_map_extract_map(uset
, dim
));
556 /* Check if umap contains a map in the given space.
558 isl_bool
isl_union_map_contains(__isl_keep isl_union_map
*umap
,
559 __isl_keep isl_space
*space
)
562 struct isl_hash_table_entry
*entry
;
565 return isl_bool_error
;
567 hash
= isl_space_get_hash(space
);
568 entry
= isl_hash_table_find(umap
->dim
->ctx
, &umap
->table
, hash
,
569 &has_space
, space
, 0);
573 isl_bool
isl_union_set_contains(__isl_keep isl_union_set
*uset
,
574 __isl_keep isl_space
*space
)
576 return isl_union_map_contains(uset
, space
);
579 isl_stat
isl_union_set_foreach_set(__isl_keep isl_union_set
*uset
,
580 isl_stat (*fn
)(__isl_take isl_set
*set
, void *user
), void *user
)
582 return isl_union_map_foreach_map(uset
,
583 (isl_stat(*)(__isl_take isl_map
*, void*))fn
, user
);
586 struct isl_union_set_foreach_point_data
{
587 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
591 static isl_stat
foreach_point(__isl_take isl_set
*set
, void *user
)
593 struct isl_union_set_foreach_point_data
*data
= user
;
596 r
= isl_set_foreach_point(set
, data
->fn
, data
->user
);
602 isl_stat
isl_union_set_foreach_point(__isl_keep isl_union_set
*uset
,
603 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
605 struct isl_union_set_foreach_point_data data
= { fn
, user
};
606 return isl_union_set_foreach_set(uset
, &foreach_point
, &data
);
609 /* Data structure that specifies how gen_bin_op should
610 * construct results from the inputs.
612 * If "subtract" is set, then a map in the first input is copied to the result
613 * if there is no corresponding map in the second input.
614 * Otherwise, a map in the first input with no corresponding map
615 * in the second input is ignored.
616 * "match_space" specifies how to transform the space of a map
617 * in the first input to the space of the corresponding map
618 * in the second input.
619 * "fn_map" specifies how the matching maps, one from each input,
620 * should be combined to form a map in the result.
622 struct isl_bin_op_control
{
624 __isl_give isl_space
*(*match_space
)(__isl_take isl_space
*space
);
625 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map1
,
626 __isl_take isl_map
*map2
);
629 /* Internal data structure for gen_bin_op.
630 * "control" specifies how the maps in the result should be constructed.
631 * "umap2" is a pointer to the second argument.
632 * "res" collects the results.
634 struct isl_union_map_gen_bin_data
{
635 struct isl_bin_op_control
*control
;
636 isl_union_map
*umap2
;
640 /* Add a copy of "map" to "res" and return the result.
642 static __isl_give isl_union_map
*bin_add_map(__isl_take isl_union_map
*res
,
643 __isl_keep isl_map
*map
)
645 return isl_union_map_add_map(res
, isl_map_copy(map
));
648 /* Combine "map1" and "map2", add the result to "res" and return the result.
649 * Check whether the result is empty before adding it to "res".
651 static __isl_give isl_union_map
*bin_add_pair(__isl_take isl_union_map
*res
,
652 __isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
653 struct isl_union_map_gen_bin_data
*data
)
658 map
= data
->control
->fn_map(isl_map_copy(map1
), isl_map_copy(map2
));
659 empty
= isl_map_is_empty(map
);
660 if (empty
< 0 || empty
) {
663 return isl_union_map_free(res
);
666 return isl_union_map_add_map(res
, map
);
669 /* Dummy match_space function that simply returns the input space.
671 static __isl_give isl_space
*identity(__isl_take isl_space
*space
)
676 /* isl_hash_table_foreach callback for gen_bin_op.
677 * Look for the map in data->umap2 that corresponds
678 * to the map that "entry" points to, apply the binary operation and
679 * add the result to data->res.
681 * data->control->match_space specifies which map in data->umap2
682 * corresponds to the current map.
683 * If no corresponding map can be found, then the effect depends
684 * on data->control->subtract. If it is set, then the current map
685 * is added directly to the result. Otherwise, it is ignored.
687 static isl_stat
gen_bin_entry(void **entry
, void *user
)
689 struct isl_union_map_gen_bin_data
*data
= user
;
691 struct isl_hash_table_entry
*entry2
;
693 isl_map
*map
= *entry
;
695 space
= isl_map_get_space(map
);
696 if (data
->control
->match_space
!= &identity
)
697 space
= data
->control
->match_space(space
);
699 return isl_stat_error
;
700 hash
= isl_space_get_hash(space
);
701 entry2
= isl_hash_table_find(isl_union_map_get_ctx(data
->umap2
),
702 &data
->umap2
->table
, hash
,
703 &has_space
, space
, 0);
704 isl_space_free(space
);
705 if (!entry2
&& !data
->control
->subtract
)
709 data
->res
= bin_add_map(data
->res
, map
);
711 data
->res
= bin_add_pair(data
->res
, map
, entry2
->data
, data
);
713 return isl_stat_error
;
718 /* Apply a binary operation to "umap1" and "umap2" based on "control".
719 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
722 static __isl_give isl_union_map
*gen_bin_op(__isl_take isl_union_map
*umap1
,
723 __isl_take isl_union_map
*umap2
, struct isl_bin_op_control
*control
)
725 struct isl_union_map_gen_bin_data data
= { control
, NULL
, NULL
};
727 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
728 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
730 if (!umap1
|| !umap2
)
734 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
736 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
737 &gen_bin_entry
, &data
) < 0)
740 isl_union_map_free(umap1
);
741 isl_union_map_free(umap2
);
744 isl_union_map_free(umap1
);
745 isl_union_map_free(umap2
);
746 isl_union_map_free(data
.res
);
750 __isl_give isl_union_map
*isl_union_map_subtract(
751 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
753 struct isl_bin_op_control control
= {
755 .match_space
= &identity
,
756 .fn_map
= &isl_map_subtract
,
759 return gen_bin_op(umap1
, umap2
, &control
);
762 __isl_give isl_union_set
*isl_union_set_subtract(
763 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
765 return isl_union_map_subtract(uset1
, uset2
);
768 struct isl_union_map_gen_bin_set_data
{
773 static isl_stat
intersect_params_entry(void **entry
, void *user
)
775 struct isl_union_map_gen_bin_set_data
*data
= user
;
776 isl_map
*map
= *entry
;
779 map
= isl_map_copy(map
);
780 map
= isl_map_intersect_params(map
, isl_set_copy(data
->set
));
782 empty
= isl_map_is_empty(map
);
785 return isl_stat_error
;
788 data
->res
= isl_union_map_add_map(data
->res
, map
);
793 static __isl_give isl_union_map
*gen_bin_set_op(__isl_take isl_union_map
*umap
,
794 __isl_take isl_set
*set
, isl_stat (*fn
)(void **, void *))
796 struct isl_union_map_gen_bin_set_data data
= { NULL
, NULL
};
798 umap
= isl_union_map_align_params(umap
, isl_set_get_space(set
));
799 set
= isl_set_align_params(set
, isl_union_map_get_space(umap
));
805 data
.res
= isl_union_map_alloc(isl_space_copy(umap
->dim
),
807 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
811 isl_union_map_free(umap
);
815 isl_union_map_free(umap
);
817 isl_union_map_free(data
.res
);
821 /* Intersect "umap" with the parameter domain "set".
823 * If "set" does not have any constraints, then we can return immediately.
825 __isl_give isl_union_map
*isl_union_map_intersect_params(
826 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
830 is_universe
= isl_set_plain_is_universe(set
);
838 return gen_bin_set_op(umap
, set
, &intersect_params_entry
);
840 isl_union_map_free(umap
);
845 __isl_give isl_union_set
*isl_union_set_intersect_params(
846 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
848 return isl_union_map_intersect_params(uset
, set
);
851 static __isl_give isl_union_map
*union_map_intersect_params(
852 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
854 return isl_union_map_intersect_params(umap
,
855 isl_set_from_union_set(uset
));
858 static __isl_give isl_union_map
*union_map_gist_params(
859 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
861 return isl_union_map_gist_params(umap
, isl_set_from_union_set(uset
));
864 struct isl_union_map_match_bin_data
{
865 isl_union_map
*umap2
;
867 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*);
870 static isl_stat
match_bin_entry(void **entry
, void *user
)
872 struct isl_union_map_match_bin_data
*data
= user
;
874 struct isl_hash_table_entry
*entry2
;
875 isl_map
*map
= *entry
;
878 hash
= isl_space_get_hash(map
->dim
);
879 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
880 hash
, &has_space
, map
->dim
, 0);
884 map
= isl_map_copy(map
);
885 map
= data
->fn(map
, isl_map_copy(entry2
->data
));
887 empty
= isl_map_is_empty(map
);
890 return isl_stat_error
;
897 data
->res
= isl_union_map_add_map(data
->res
, map
);
902 static __isl_give isl_union_map
*match_bin_op(__isl_take isl_union_map
*umap1
,
903 __isl_take isl_union_map
*umap2
,
904 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*, __isl_take isl_map
*))
906 struct isl_union_map_match_bin_data data
= { NULL
, NULL
, fn
};
908 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
909 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
911 if (!umap1
|| !umap2
)
915 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
917 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
918 &match_bin_entry
, &data
) < 0)
921 isl_union_map_free(umap1
);
922 isl_union_map_free(umap2
);
925 isl_union_map_free(umap1
);
926 isl_union_map_free(umap2
);
927 isl_union_map_free(data
.res
);
931 __isl_give isl_union_map
*isl_union_map_intersect(
932 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
934 return match_bin_op(umap1
, umap2
, &isl_map_intersect
);
937 /* Compute the intersection of the two union_sets.
938 * As a special case, if exactly one of the two union_sets
939 * is a parameter domain, then intersect the parameter domain
940 * of the other one with this set.
942 __isl_give isl_union_set
*isl_union_set_intersect(
943 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
947 p1
= isl_union_set_is_params(uset1
);
948 p2
= isl_union_set_is_params(uset2
);
949 if (p1
< 0 || p2
< 0)
952 return union_map_intersect_params(uset1
, uset2
);
954 return union_map_intersect_params(uset2
, uset1
);
955 return isl_union_map_intersect(uset1
, uset2
);
957 isl_union_set_free(uset1
);
958 isl_union_set_free(uset2
);
962 static isl_stat
gist_params_entry(void **entry
, void *user
)
964 struct isl_union_map_gen_bin_set_data
*data
= user
;
965 isl_map
*map
= *entry
;
968 map
= isl_map_copy(map
);
969 map
= isl_map_gist_params(map
, isl_set_copy(data
->set
));
971 empty
= isl_map_is_empty(map
);
974 return isl_stat_error
;
977 data
->res
= isl_union_map_add_map(data
->res
, map
);
982 __isl_give isl_union_map
*isl_union_map_gist_params(
983 __isl_take isl_union_map
*umap
, __isl_take isl_set
*set
)
985 return gen_bin_set_op(umap
, set
, &gist_params_entry
);
988 __isl_give isl_union_set
*isl_union_set_gist_params(
989 __isl_take isl_union_set
*uset
, __isl_take isl_set
*set
)
991 return isl_union_map_gist_params(uset
, set
);
994 __isl_give isl_union_map
*isl_union_map_gist(__isl_take isl_union_map
*umap
,
995 __isl_take isl_union_map
*context
)
997 return match_bin_op(umap
, context
, &isl_map_gist
);
1000 __isl_give isl_union_set
*isl_union_set_gist(__isl_take isl_union_set
*uset
,
1001 __isl_take isl_union_set
*context
)
1003 if (isl_union_set_is_params(context
))
1004 return union_map_gist_params(uset
, context
);
1005 return isl_union_map_gist(uset
, context
);
1008 /* For each map in "umap", remove the constraints in the corresponding map
1010 * Each map in "context" is assumed to consist of a single disjunct and
1011 * to have explicit representations for all local variables.
1013 __isl_give isl_union_map
*isl_union_map_plain_gist(
1014 __isl_take isl_union_map
*umap
, __isl_take isl_union_map
*context
)
1016 return match_bin_op(umap
, context
, &isl_map_plain_gist
);
1019 /* For each set in "uset", remove the constraints in the corresponding set
1021 * Each set in "context" is assumed to consist of a single disjunct and
1022 * to have explicit representations for all local variables.
1024 __isl_give isl_union_set
*isl_union_set_plain_gist(
1025 __isl_take isl_union_set
*uset
, __isl_take isl_union_set
*context
)
1027 return isl_union_map_plain_gist(uset
, context
);
1030 static __isl_give isl_map
*lex_le_set(__isl_take isl_map
*set1
,
1031 __isl_take isl_map
*set2
)
1033 return isl_set_lex_le_set(set_from_map(set1
), set_from_map(set2
));
1036 static __isl_give isl_map
*lex_lt_set(__isl_take isl_map
*set1
,
1037 __isl_take isl_map
*set2
)
1039 return isl_set_lex_lt_set(set_from_map(set1
), set_from_map(set2
));
1042 __isl_give isl_union_map
*isl_union_set_lex_lt_union_set(
1043 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1045 return match_bin_op(uset1
, uset2
, &lex_lt_set
);
1048 __isl_give isl_union_map
*isl_union_set_lex_le_union_set(
1049 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1051 return match_bin_op(uset1
, uset2
, &lex_le_set
);
1054 __isl_give isl_union_map
*isl_union_set_lex_gt_union_set(
1055 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1057 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2
, uset1
));
1060 __isl_give isl_union_map
*isl_union_set_lex_ge_union_set(
1061 __isl_take isl_union_set
*uset1
, __isl_take isl_union_set
*uset2
)
1063 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2
, uset1
));
1066 __isl_give isl_union_map
*isl_union_map_lex_gt_union_map(
1067 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1069 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2
, umap1
));
1072 __isl_give isl_union_map
*isl_union_map_lex_ge_union_map(
1073 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1075 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2
, umap1
));
1078 /* Intersect the domain of "umap" with "uset".
1080 static __isl_give isl_union_map
*union_map_intersect_domain(
1081 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1083 struct isl_bin_op_control control
= {
1084 .match_space
= &isl_space_domain
,
1085 .fn_map
= &isl_map_intersect_domain
,
1088 return gen_bin_op(umap
, uset
, &control
);
1091 /* Intersect the domain of "umap" with "uset".
1092 * If "uset" is a parameters domain, then intersect the parameter
1093 * domain of "umap" with this set.
1095 __isl_give isl_union_map
*isl_union_map_intersect_domain(
1096 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1098 if (isl_union_set_is_params(uset
))
1099 return union_map_intersect_params(umap
, uset
);
1101 return union_map_intersect_domain(umap
, uset
);
1104 /* Remove the elements of "uset" from the domain of "umap".
1106 __isl_give isl_union_map
*isl_union_map_subtract_domain(
1107 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1109 struct isl_bin_op_control control
= {
1111 .match_space
= &isl_space_domain
,
1112 .fn_map
= &isl_map_subtract_domain
,
1115 return gen_bin_op(umap
, dom
, &control
);
1118 /* Remove the elements of "uset" from the range of "umap".
1120 __isl_give isl_union_map
*isl_union_map_subtract_range(
1121 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*dom
)
1123 struct isl_bin_op_control control
= {
1125 .match_space
= &isl_space_range
,
1126 .fn_map
= &isl_map_subtract_range
,
1129 return gen_bin_op(umap
, dom
, &control
);
1132 /* Compute the gist of "umap" with respect to the domain "uset".
1134 static __isl_give isl_union_map
*union_map_gist_domain(
1135 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1137 struct isl_bin_op_control control
= {
1138 .match_space
= &isl_space_domain
,
1139 .fn_map
= &isl_map_gist_domain
,
1142 return gen_bin_op(umap
, uset
, &control
);
1145 /* Compute the gist of "umap" with respect to the domain "uset".
1146 * If "uset" is a parameters domain, then compute the gist
1147 * with respect to this parameter domain.
1149 __isl_give isl_union_map
*isl_union_map_gist_domain(
1150 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1152 if (isl_union_set_is_params(uset
))
1153 return union_map_gist_params(umap
, uset
);
1155 return union_map_gist_domain(umap
, uset
);
1158 /* Compute the gist of "umap" with respect to the range "uset".
1160 __isl_give isl_union_map
*isl_union_map_gist_range(
1161 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1163 struct isl_bin_op_control control
= {
1164 .match_space
= &isl_space_range
,
1165 .fn_map
= &isl_map_gist_range
,
1168 return gen_bin_op(umap
, uset
, &control
);
1171 __isl_give isl_union_map
*isl_union_map_intersect_range(
1172 __isl_take isl_union_map
*umap
, __isl_take isl_union_set
*uset
)
1174 struct isl_bin_op_control control
= {
1175 .match_space
= &isl_space_range
,
1176 .fn_map
= &isl_map_intersect_range
,
1179 return gen_bin_op(umap
, uset
, &control
);
1182 struct isl_union_map_bin_data
{
1183 isl_union_map
*umap2
;
1186 isl_stat (*fn
)(void **entry
, void *user
);
1189 static isl_stat
apply_range_entry(void **entry
, void *user
)
1191 struct isl_union_map_bin_data
*data
= user
;
1192 isl_map
*map2
= *entry
;
1195 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1196 map2
->dim
, isl_dim_in
))
1199 map2
= isl_map_apply_range(isl_map_copy(data
->map
), isl_map_copy(map2
));
1201 empty
= isl_map_is_empty(map2
);
1204 return isl_stat_error
;
1211 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1216 static isl_stat
bin_entry(void **entry
, void *user
)
1218 struct isl_union_map_bin_data
*data
= user
;
1219 isl_map
*map
= *entry
;
1222 if (isl_hash_table_foreach(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
1223 data
->fn
, data
) < 0)
1224 return isl_stat_error
;
1229 static __isl_give isl_union_map
*bin_op(__isl_take isl_union_map
*umap1
,
1230 __isl_take isl_union_map
*umap2
,
1231 isl_stat (*fn
)(void **entry
, void *user
))
1233 struct isl_union_map_bin_data data
= { NULL
, NULL
, NULL
, fn
};
1235 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
1236 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
1238 if (!umap1
|| !umap2
)
1242 data
.res
= isl_union_map_alloc(isl_space_copy(umap1
->dim
),
1244 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
1245 &bin_entry
, &data
) < 0)
1248 isl_union_map_free(umap1
);
1249 isl_union_map_free(umap2
);
1252 isl_union_map_free(umap1
);
1253 isl_union_map_free(umap2
);
1254 isl_union_map_free(data
.res
);
1258 __isl_give isl_union_map
*isl_union_map_apply_range(
1259 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1261 return bin_op(umap1
, umap2
, &apply_range_entry
);
1264 __isl_give isl_union_map
*isl_union_map_apply_domain(
1265 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1267 umap1
= isl_union_map_reverse(umap1
);
1268 umap1
= isl_union_map_apply_range(umap1
, umap2
);
1269 return isl_union_map_reverse(umap1
);
1272 __isl_give isl_union_set
*isl_union_set_apply(
1273 __isl_take isl_union_set
*uset
, __isl_take isl_union_map
*umap
)
1275 return isl_union_map_apply_range(uset
, umap
);
1278 static isl_stat
map_lex_lt_entry(void **entry
, void *user
)
1280 struct isl_union_map_bin_data
*data
= user
;
1281 isl_map
*map2
= *entry
;
1283 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1284 map2
->dim
, isl_dim_out
))
1287 map2
= isl_map_lex_lt_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1289 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1294 __isl_give isl_union_map
*isl_union_map_lex_lt_union_map(
1295 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1297 return bin_op(umap1
, umap2
, &map_lex_lt_entry
);
1300 static isl_stat
map_lex_le_entry(void **entry
, void *user
)
1302 struct isl_union_map_bin_data
*data
= user
;
1303 isl_map
*map2
= *entry
;
1305 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1306 map2
->dim
, isl_dim_out
))
1309 map2
= isl_map_lex_le_map(isl_map_copy(data
->map
), isl_map_copy(map2
));
1311 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1316 __isl_give isl_union_map
*isl_union_map_lex_le_union_map(
1317 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1319 return bin_op(umap1
, umap2
, &map_lex_le_entry
);
1322 static isl_stat
product_entry(void **entry
, void *user
)
1324 struct isl_union_map_bin_data
*data
= user
;
1325 isl_map
*map2
= *entry
;
1327 map2
= isl_map_product(isl_map_copy(data
->map
), isl_map_copy(map2
));
1329 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1334 __isl_give isl_union_map
*isl_union_map_product(__isl_take isl_union_map
*umap1
,
1335 __isl_take isl_union_map
*umap2
)
1337 return bin_op(umap1
, umap2
, &product_entry
);
1340 static isl_stat
set_product_entry(void **entry
, void *user
)
1342 struct isl_union_map_bin_data
*data
= user
;
1343 isl_set
*set2
= *entry
;
1345 set2
= isl_set_product(isl_set_copy(data
->map
), isl_set_copy(set2
));
1347 data
->res
= isl_union_set_add_set(data
->res
, set2
);
1352 __isl_give isl_union_set
*isl_union_set_product(__isl_take isl_union_set
*uset1
,
1353 __isl_take isl_union_set
*uset2
)
1355 return bin_op(uset1
, uset2
, &set_product_entry
);
1358 static isl_stat
domain_product_entry(void **entry
, void *user
)
1360 struct isl_union_map_bin_data
*data
= user
;
1361 isl_map
*map2
= *entry
;
1363 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1364 map2
->dim
, isl_dim_out
))
1367 map2
= isl_map_domain_product(isl_map_copy(data
->map
),
1368 isl_map_copy(map2
));
1370 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1375 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1377 __isl_give isl_union_map
*isl_union_map_domain_product(
1378 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1380 return bin_op(umap1
, umap2
, &domain_product_entry
);
1383 static isl_stat
range_product_entry(void **entry
, void *user
)
1385 struct isl_union_map_bin_data
*data
= user
;
1386 isl_map
*map2
= *entry
;
1388 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1389 map2
->dim
, isl_dim_in
))
1392 map2
= isl_map_range_product(isl_map_copy(data
->map
),
1393 isl_map_copy(map2
));
1395 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1400 __isl_give isl_union_map
*isl_union_map_range_product(
1401 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1403 return bin_op(umap1
, umap2
, &range_product_entry
);
1406 /* If data->map A -> B and "map2" C -> D have the same range space,
1407 * then add (A, C) -> (B * D) to data->res.
1409 static isl_stat
flat_domain_product_entry(void **entry
, void *user
)
1411 struct isl_union_map_bin_data
*data
= user
;
1412 isl_map
*map2
= *entry
;
1414 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_out
,
1415 map2
->dim
, isl_dim_out
))
1418 map2
= isl_map_flat_domain_product(isl_map_copy(data
->map
),
1419 isl_map_copy(map2
));
1421 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1426 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1428 __isl_give isl_union_map
*isl_union_map_flat_domain_product(
1429 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1431 return bin_op(umap1
, umap2
, &flat_domain_product_entry
);
1434 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
1436 struct isl_union_map_bin_data
*data
= user
;
1437 isl_map
*map2
= *entry
;
1439 if (!isl_space_tuple_is_equal(data
->map
->dim
, isl_dim_in
,
1440 map2
->dim
, isl_dim_in
))
1443 map2
= isl_map_flat_range_product(isl_map_copy(data
->map
),
1444 isl_map_copy(map2
));
1446 data
->res
= isl_union_map_add_map(data
->res
, map2
);
1451 __isl_give isl_union_map
*isl_union_map_flat_range_product(
1452 __isl_take isl_union_map
*umap1
, __isl_take isl_union_map
*umap2
)
1454 return bin_op(umap1
, umap2
, &flat_range_product_entry
);
1457 /* Data structure that specifies how un_op should modify
1458 * the maps in the union map.
1460 * If "inplace" is set, then the maps in the input union map
1461 * are modified in place. This means that "fn_map" should not
1462 * change the meaning of the map or that the union map only
1463 * has a single reference.
1464 * If "total" is set, then all maps need to be modified and
1465 * the results need to live in the same space.
1466 * Otherwise, a new union map is constructed to store the results.
1467 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1468 * are taken into account. No filter can be set if "inplace" or
1470 * "fn_map" specifies how the maps (selected by "filter")
1471 * should be transformed.
1473 struct isl_un_op_control
{
1476 isl_bool (*filter
)(__isl_keep isl_map
*map
);
1477 __isl_give isl_map
*(*fn_map
)(__isl_take isl_map
*map
);
1480 /* Internal data structure for "un_op".
1481 * "control" specifies how the maps in the union map should be modified.
1482 * "res" collects the results.
1484 struct isl_union_map_un_data
{
1485 struct isl_un_op_control
*control
;
1489 /* isl_hash_table_foreach callback for un_op.
1490 * Handle the map that "entry" points to.
1492 * If control->filter is set, then check if this map satisfies the filter.
1493 * If so (or if control->filter is not set), modify the map
1494 * by calling control->fn_map and either add the result to data->res or
1495 * replace the original entry by the result (if control->inplace is set).
1497 static isl_stat
un_entry(void **entry
, void *user
)
1499 struct isl_union_map_un_data
*data
= user
;
1500 isl_map
*map
= *entry
;
1502 if (data
->control
->filter
) {
1505 ok
= data
->control
->filter(map
);
1507 return isl_stat_error
;
1512 map
= data
->control
->fn_map(isl_map_copy(map
));
1514 return isl_stat_error
;
1515 if (data
->control
->inplace
) {
1516 isl_map_free(*entry
);
1519 data
->res
= isl_union_map_add_map(data
->res
, map
);
1521 return isl_stat_error
;
1527 /* Modify the maps in "umap" based on "control".
1528 * If control->inplace is set, then modify the maps in "umap" in-place.
1529 * Otherwise, create a new union map to hold the results.
1530 * If control->total is set, then perform an inplace computation
1531 * if "umap" is only referenced once. Otherwise, create a new union map
1532 * to store the results.
1534 static __isl_give isl_union_map
*un_op(__isl_take isl_union_map
*umap
,
1535 struct isl_un_op_control
*control
)
1537 struct isl_union_map_un_data data
= { control
};
1541 if ((control
->inplace
|| control
->total
) && control
->filter
)
1542 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
1543 "inplace/total modification cannot be filtered",
1544 return isl_union_map_free(umap
));
1546 if (control
->total
&& umap
->ref
== 1)
1547 control
->inplace
= 1;
1548 if (control
->inplace
) {
1553 space
= isl_union_map_get_space(umap
);
1554 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
1556 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap
),
1557 &umap
->table
, &un_entry
, &data
) < 0)
1558 data
.res
= isl_union_map_free(data
.res
);
1560 if (control
->inplace
)
1562 isl_union_map_free(umap
);
1566 __isl_give isl_union_map
*isl_union_map_from_range(
1567 __isl_take isl_union_set
*uset
)
1569 struct isl_un_op_control control
= {
1570 .fn_map
= &isl_map_from_range
,
1572 return un_op(uset
, &control
);
1575 __isl_give isl_union_map
*isl_union_map_from_domain(
1576 __isl_take isl_union_set
*uset
)
1578 return isl_union_map_reverse(isl_union_map_from_range(uset
));
1581 __isl_give isl_union_map
*isl_union_map_from_domain_and_range(
1582 __isl_take isl_union_set
*domain
, __isl_take isl_union_set
*range
)
1584 return isl_union_map_apply_range(isl_union_map_from_domain(domain
),
1585 isl_union_map_from_range(range
));
1588 /* Modify the maps in "umap" by applying "fn" on them.
1589 * "fn" should apply to all maps in "umap" and should not modify the space.
1591 static __isl_give isl_union_map
*total(__isl_take isl_union_map
*umap
,
1592 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1594 struct isl_un_op_control control
= {
1599 return un_op(umap
, &control
);
1602 /* Compute the affine hull of "map" and return the result as an isl_map.
1604 static __isl_give isl_map
*isl_map_affine_hull_map(__isl_take isl_map
*map
)
1606 return isl_map_from_basic_map(isl_map_affine_hull(map
));
1609 __isl_give isl_union_map
*isl_union_map_affine_hull(
1610 __isl_take isl_union_map
*umap
)
1612 return total(umap
, &isl_map_affine_hull_map
);
1615 __isl_give isl_union_set
*isl_union_set_affine_hull(
1616 __isl_take isl_union_set
*uset
)
1618 return isl_union_map_affine_hull(uset
);
1621 /* Wrapper around isl_set_combined_lineality_space
1622 * that returns the combined lineality space in the form of an isl_set
1623 * instead of an isl_basic_set.
1625 static __isl_give isl_set
*combined_lineality_space(__isl_take isl_set
*set
)
1627 return isl_set_from_basic_set(isl_set_combined_lineality_space(set
));
1630 /* For each set in "uset", compute the (linear) hull
1631 * of the lineality spaces of its basic sets and
1632 * collect and return the results.
1634 __isl_give isl_union_set
*isl_union_set_combined_lineality_space(
1635 __isl_take isl_union_set
*uset
)
1637 struct isl_un_op_control control
= {
1638 .fn_map
= &combined_lineality_space
,
1640 return un_op(uset
, &control
);
1643 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1645 static __isl_give isl_map
*isl_map_polyhedral_hull_map(__isl_take isl_map
*map
)
1647 return isl_map_from_basic_map(isl_map_polyhedral_hull(map
));
1650 __isl_give isl_union_map
*isl_union_map_polyhedral_hull(
1651 __isl_take isl_union_map
*umap
)
1653 return total(umap
, &isl_map_polyhedral_hull_map
);
1656 __isl_give isl_union_set
*isl_union_set_polyhedral_hull(
1657 __isl_take isl_union_set
*uset
)
1659 return isl_union_map_polyhedral_hull(uset
);
1662 /* Compute a superset of the convex hull of "map" that is described
1663 * by only translates of the constraints in the constituents of "map" and
1664 * return the result as an isl_map.
1666 static __isl_give isl_map
*isl_map_simple_hull_map(__isl_take isl_map
*map
)
1668 return isl_map_from_basic_map(isl_map_simple_hull(map
));
1671 __isl_give isl_union_map
*isl_union_map_simple_hull(
1672 __isl_take isl_union_map
*umap
)
1674 return total(umap
, &isl_map_simple_hull_map
);
1677 __isl_give isl_union_set
*isl_union_set_simple_hull(
1678 __isl_take isl_union_set
*uset
)
1680 return isl_union_map_simple_hull(uset
);
1683 static __isl_give isl_union_map
*inplace(__isl_take isl_union_map
*umap
,
1684 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*))
1686 struct isl_un_op_control control
= {
1691 return un_op(umap
, &control
);
1694 /* Remove redundant constraints in each of the basic maps of "umap".
1695 * Since removing redundant constraints does not change the meaning
1696 * or the space, the operation can be performed in-place.
1698 __isl_give isl_union_map
*isl_union_map_remove_redundancies(
1699 __isl_take isl_union_map
*umap
)
1701 return inplace(umap
, &isl_map_remove_redundancies
);
1704 /* Remove redundant constraints in each of the basic sets of "uset".
1706 __isl_give isl_union_set
*isl_union_set_remove_redundancies(
1707 __isl_take isl_union_set
*uset
)
1709 return isl_union_map_remove_redundancies(uset
);
1712 __isl_give isl_union_map
*isl_union_map_coalesce(
1713 __isl_take isl_union_map
*umap
)
1715 return inplace(umap
, &isl_map_coalesce
);
1718 __isl_give isl_union_set
*isl_union_set_coalesce(
1719 __isl_take isl_union_set
*uset
)
1721 return isl_union_map_coalesce(uset
);
1724 __isl_give isl_union_map
*isl_union_map_detect_equalities(
1725 __isl_take isl_union_map
*umap
)
1727 return inplace(umap
, &isl_map_detect_equalities
);
1730 __isl_give isl_union_set
*isl_union_set_detect_equalities(
1731 __isl_take isl_union_set
*uset
)
1733 return isl_union_map_detect_equalities(uset
);
1736 __isl_give isl_union_map
*isl_union_map_compute_divs(
1737 __isl_take isl_union_map
*umap
)
1739 return inplace(umap
, &isl_map_compute_divs
);
1742 __isl_give isl_union_set
*isl_union_set_compute_divs(
1743 __isl_take isl_union_set
*uset
)
1745 return isl_union_map_compute_divs(uset
);
1748 __isl_give isl_union_map
*isl_union_map_lexmin(
1749 __isl_take isl_union_map
*umap
)
1751 return total(umap
, &isl_map_lexmin
);
1754 __isl_give isl_union_set
*isl_union_set_lexmin(
1755 __isl_take isl_union_set
*uset
)
1757 return isl_union_map_lexmin(uset
);
1760 __isl_give isl_union_map
*isl_union_map_lexmax(
1761 __isl_take isl_union_map
*umap
)
1763 return total(umap
, &isl_map_lexmax
);
1766 __isl_give isl_union_set
*isl_union_set_lexmax(
1767 __isl_take isl_union_set
*uset
)
1769 return isl_union_map_lexmax(uset
);
1772 /* Return the universe in the space of "map".
1774 static __isl_give isl_map
*universe(__isl_take isl_map
*map
)
1778 space
= isl_map_get_space(map
);
1780 return isl_map_universe(space
);
1783 __isl_give isl_union_map
*isl_union_map_universe(__isl_take isl_union_map
*umap
)
1785 struct isl_un_op_control control
= {
1786 .fn_map
= &universe
,
1788 return un_op(umap
, &control
);
1791 __isl_give isl_union_set
*isl_union_set_universe(__isl_take isl_union_set
*uset
)
1793 return isl_union_map_universe(uset
);
1796 __isl_give isl_union_map
*isl_union_map_reverse(__isl_take isl_union_map
*umap
)
1798 struct isl_un_op_control control
= {
1799 .fn_map
= &isl_map_reverse
,
1801 return un_op(umap
, &control
);
1804 /* Compute the parameter domain of the given union map.
1806 __isl_give isl_set
*isl_union_map_params(__isl_take isl_union_map
*umap
)
1808 struct isl_un_op_control control
= {
1809 .fn_map
= &isl_map_params
,
1813 empty
= isl_union_map_is_empty(umap
);
1818 space
= isl_union_map_get_space(umap
);
1819 isl_union_map_free(umap
);
1820 return isl_set_empty(space
);
1822 return isl_set_from_union_set(un_op(umap
, &control
));
1824 isl_union_map_free(umap
);
1828 /* Compute the parameter domain of the given union set.
1830 __isl_give isl_set
*isl_union_set_params(__isl_take isl_union_set
*uset
)
1832 return isl_union_map_params(uset
);
1835 __isl_give isl_union_set
*isl_union_map_domain(__isl_take isl_union_map
*umap
)
1837 struct isl_un_op_control control
= {
1838 .fn_map
= &isl_map_domain
,
1840 return un_op(umap
, &control
);
1843 __isl_give isl_union_set
*isl_union_map_range(__isl_take isl_union_map
*umap
)
1845 struct isl_un_op_control control
= {
1846 .fn_map
= &isl_map_range
,
1848 return un_op(umap
, &control
);
1851 __isl_give isl_union_map
*isl_union_map_domain_map(
1852 __isl_take isl_union_map
*umap
)
1854 struct isl_un_op_control control
= {
1855 .fn_map
= &isl_map_domain_map
,
1857 return un_op(umap
, &control
);
1860 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1861 * add the result to "res".
1863 static isl_stat
domain_map_upma(__isl_take isl_map
*map
, void *user
)
1865 isl_union_pw_multi_aff
**res
= user
;
1867 isl_pw_multi_aff
*pma
;
1869 ma
= isl_multi_aff_domain_map(isl_map_get_space(map
));
1870 pma
= isl_pw_multi_aff_alloc(isl_map_wrap(map
), ma
);
1871 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1873 return *res
? isl_stat_ok
: isl_stat_error
;
1877 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1880 __isl_give isl_union_pw_multi_aff
*isl_union_map_domain_map_union_pw_multi_aff(
1881 __isl_take isl_union_map
*umap
)
1883 isl_union_pw_multi_aff
*res
;
1885 res
= isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap
));
1886 if (isl_union_map_foreach_map(umap
, &domain_map_upma
, &res
) < 0)
1887 res
= isl_union_pw_multi_aff_free(res
);
1889 isl_union_map_free(umap
);
1893 __isl_give isl_union_map
*isl_union_map_range_map(
1894 __isl_take isl_union_map
*umap
)
1896 struct isl_un_op_control control
= {
1897 .fn_map
= &isl_map_range_map
,
1899 return un_op(umap
, &control
);
1902 /* Given a collection of wrapped maps of the form A[B -> C],
1903 * return the collection of maps A[B -> C] -> B.
1905 __isl_give isl_union_map
*isl_union_set_wrapped_domain_map(
1906 __isl_take isl_union_set
*uset
)
1908 struct isl_un_op_control control
= {
1909 .filter
= &isl_set_is_wrapping
,
1910 .fn_map
= &isl_set_wrapped_domain_map
,
1912 return un_op(uset
, &control
);
1915 /* Does "map" relate elements from the same space?
1917 static isl_bool
equal_tuples(__isl_keep isl_map
*map
)
1919 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
1920 map
->dim
, isl_dim_out
);
1923 __isl_give isl_union_set
*isl_union_map_deltas(__isl_take isl_union_map
*umap
)
1925 struct isl_un_op_control control
= {
1926 .filter
= &equal_tuples
,
1927 .fn_map
= &isl_map_deltas
,
1929 return un_op(umap
, &control
);
1932 __isl_give isl_union_map
*isl_union_map_deltas_map(
1933 __isl_take isl_union_map
*umap
)
1935 struct isl_un_op_control control
= {
1936 .filter
= &equal_tuples
,
1937 .fn_map
= &isl_map_deltas_map
,
1939 return un_op(umap
, &control
);
1942 __isl_give isl_union_map
*isl_union_set_identity(__isl_take isl_union_set
*uset
)
1944 struct isl_un_op_control control
= {
1945 .fn_map
= &isl_set_identity
,
1947 return un_op(uset
, &control
);
1950 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
1952 static isl_stat
identity_upma(__isl_take isl_set
*set
, void *user
)
1954 isl_union_pw_multi_aff
**res
= user
;
1956 isl_pw_multi_aff
*pma
;
1958 space
= isl_space_map_from_set(isl_set_get_space(set
));
1959 pma
= isl_pw_multi_aff_identity(space
);
1960 pma
= isl_pw_multi_aff_intersect_domain(pma
, set
);
1961 *res
= isl_union_pw_multi_aff_add_pw_multi_aff(*res
, pma
);
1963 return *res
? isl_stat_ok
: isl_stat_error
;
1966 /* Return an identity function on "uset" in the form
1967 * of an isl_union_pw_multi_aff.
1969 __isl_give isl_union_pw_multi_aff
*isl_union_set_identity_union_pw_multi_aff(
1970 __isl_take isl_union_set
*uset
)
1972 isl_union_pw_multi_aff
*res
;
1974 res
= isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset
));
1975 if (isl_union_set_foreach_set(uset
, &identity_upma
, &res
) < 0)
1976 res
= isl_union_pw_multi_aff_free(res
);
1978 isl_union_set_free(uset
);
1982 /* For each map in "umap" of the form [A -> B] -> C,
1983 * construct the map A -> C and collect the results.
1985 __isl_give isl_union_map
*isl_union_map_domain_factor_domain(
1986 __isl_take isl_union_map
*umap
)
1988 struct isl_un_op_control control
= {
1989 .filter
= &isl_map_domain_is_wrapping
,
1990 .fn_map
= &isl_map_domain_factor_domain
,
1992 return un_op(umap
, &control
);
1995 /* For each map in "umap" of the form [A -> B] -> C,
1996 * construct the map B -> C and collect the results.
1998 __isl_give isl_union_map
*isl_union_map_domain_factor_range(
1999 __isl_take isl_union_map
*umap
)
2001 struct isl_un_op_control control
= {
2002 .filter
= &isl_map_domain_is_wrapping
,
2003 .fn_map
= &isl_map_domain_factor_range
,
2005 return un_op(umap
, &control
);
2008 /* For each map in "umap" of the form A -> [B -> C],
2009 * construct the map A -> B and collect the results.
2011 __isl_give isl_union_map
*isl_union_map_range_factor_domain(
2012 __isl_take isl_union_map
*umap
)
2014 struct isl_un_op_control control
= {
2015 .filter
= &isl_map_range_is_wrapping
,
2016 .fn_map
= &isl_map_range_factor_domain
,
2018 return un_op(umap
, &control
);
2021 /* For each map in "umap" of the form A -> [B -> C],
2022 * construct the map A -> C and collect the results.
2024 __isl_give isl_union_map
*isl_union_map_range_factor_range(
2025 __isl_take isl_union_map
*umap
)
2027 struct isl_un_op_control control
= {
2028 .filter
= &isl_map_range_is_wrapping
,
2029 .fn_map
= &isl_map_range_factor_range
,
2031 return un_op(umap
, &control
);
2034 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2035 * construct the map A -> C and collect the results.
2037 __isl_give isl_union_map
*isl_union_map_factor_domain(
2038 __isl_take isl_union_map
*umap
)
2040 struct isl_un_op_control control
= {
2041 .filter
= &isl_map_is_product
,
2042 .fn_map
= &isl_map_factor_domain
,
2044 return un_op(umap
, &control
);
2047 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2048 * construct the map B -> D and collect the results.
2050 __isl_give isl_union_map
*isl_union_map_factor_range(
2051 __isl_take isl_union_map
*umap
)
2053 struct isl_un_op_control control
= {
2054 .filter
= &isl_map_is_product
,
2055 .fn_map
= &isl_map_factor_range
,
2057 return un_op(umap
, &control
);
2060 __isl_give isl_union_map
*isl_union_set_unwrap(__isl_take isl_union_set
*uset
)
2062 struct isl_un_op_control control
= {
2063 .filter
= &isl_set_is_wrapping
,
2064 .fn_map
= &isl_set_unwrap
,
2066 return un_op(uset
, &control
);
2069 __isl_give isl_union_set
*isl_union_map_wrap(__isl_take isl_union_map
*umap
)
2071 struct isl_un_op_control control
= {
2072 .fn_map
= &isl_map_wrap
,
2074 return un_op(umap
, &control
);
2077 struct isl_union_map_is_subset_data
{
2078 isl_union_map
*umap2
;
2082 static isl_stat
is_subset_entry(void **entry
, void *user
)
2084 struct isl_union_map_is_subset_data
*data
= user
;
2086 struct isl_hash_table_entry
*entry2
;
2087 isl_map
*map
= *entry
;
2089 hash
= isl_space_get_hash(map
->dim
);
2090 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2091 hash
, &has_space
, map
->dim
, 0);
2093 int empty
= isl_map_is_empty(map
);
2095 return isl_stat_error
;
2098 data
->is_subset
= 0;
2099 return isl_stat_error
;
2102 data
->is_subset
= isl_map_is_subset(map
, entry2
->data
);
2103 if (data
->is_subset
< 0 || !data
->is_subset
)
2104 return isl_stat_error
;
2109 isl_bool
isl_union_map_is_subset(__isl_keep isl_union_map
*umap1
,
2110 __isl_keep isl_union_map
*umap2
)
2112 struct isl_union_map_is_subset_data data
= { NULL
, isl_bool_true
};
2114 umap1
= isl_union_map_copy(umap1
);
2115 umap2
= isl_union_map_copy(umap2
);
2116 umap1
= isl_union_map_align_params(umap1
, isl_union_map_get_space(umap2
));
2117 umap2
= isl_union_map_align_params(umap2
, isl_union_map_get_space(umap1
));
2119 if (!umap1
|| !umap2
)
2123 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2124 &is_subset_entry
, &data
) < 0 &&
2128 isl_union_map_free(umap1
);
2129 isl_union_map_free(umap2
);
2131 return data
.is_subset
;
2133 isl_union_map_free(umap1
);
2134 isl_union_map_free(umap2
);
2135 return isl_bool_error
;
2138 isl_bool
isl_union_set_is_subset(__isl_keep isl_union_set
*uset1
,
2139 __isl_keep isl_union_set
*uset2
)
2141 return isl_union_map_is_subset(uset1
, uset2
);
2144 isl_bool
isl_union_map_is_equal(__isl_keep isl_union_map
*umap1
,
2145 __isl_keep isl_union_map
*umap2
)
2149 if (!umap1
|| !umap2
)
2150 return isl_bool_error
;
2151 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2152 if (is_subset
!= isl_bool_true
)
2154 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2158 isl_bool
isl_union_set_is_equal(__isl_keep isl_union_set
*uset1
,
2159 __isl_keep isl_union_set
*uset2
)
2161 return isl_union_map_is_equal(uset1
, uset2
);
2164 isl_bool
isl_union_map_is_strict_subset(__isl_keep isl_union_map
*umap1
,
2165 __isl_keep isl_union_map
*umap2
)
2169 if (!umap1
|| !umap2
)
2170 return isl_bool_error
;
2171 is_subset
= isl_union_map_is_subset(umap1
, umap2
);
2172 if (is_subset
!= isl_bool_true
)
2174 is_subset
= isl_union_map_is_subset(umap2
, umap1
);
2175 if (is_subset
== isl_bool_error
)
2180 isl_bool
isl_union_set_is_strict_subset(__isl_keep isl_union_set
*uset1
,
2181 __isl_keep isl_union_set
*uset2
)
2183 return isl_union_map_is_strict_subset(uset1
, uset2
);
2186 /* Internal data structure for isl_union_map_is_disjoint.
2187 * umap2 is the union map with which we are comparing.
2188 * is_disjoint is initialized to 1 and is set to 0 as soon
2189 * as the union maps turn out not to be disjoint.
2191 struct isl_union_map_is_disjoint_data
{
2192 isl_union_map
*umap2
;
2193 isl_bool is_disjoint
;
2196 /* Check if "map" is disjoint from data->umap2 and abort
2197 * the search if it is not.
2199 static isl_stat
is_disjoint_entry(void **entry
, void *user
)
2201 struct isl_union_map_is_disjoint_data
*data
= user
;
2203 struct isl_hash_table_entry
*entry2
;
2204 isl_map
*map
= *entry
;
2206 hash
= isl_space_get_hash(map
->dim
);
2207 entry2
= isl_hash_table_find(data
->umap2
->dim
->ctx
, &data
->umap2
->table
,
2208 hash
, &has_space
, map
->dim
, 0);
2212 data
->is_disjoint
= isl_map_is_disjoint(map
, entry2
->data
);
2213 if (data
->is_disjoint
< 0 || !data
->is_disjoint
)
2214 return isl_stat_error
;
2219 /* Are "umap1" and "umap2" disjoint?
2221 isl_bool
isl_union_map_is_disjoint(__isl_keep isl_union_map
*umap1
,
2222 __isl_keep isl_union_map
*umap2
)
2224 struct isl_union_map_is_disjoint_data data
= { NULL
, isl_bool_true
};
2226 umap1
= isl_union_map_copy(umap1
);
2227 umap2
= isl_union_map_copy(umap2
);
2228 umap1
= isl_union_map_align_params(umap1
,
2229 isl_union_map_get_space(umap2
));
2230 umap2
= isl_union_map_align_params(umap2
,
2231 isl_union_map_get_space(umap1
));
2233 if (!umap1
|| !umap2
)
2237 if (isl_hash_table_foreach(umap1
->dim
->ctx
, &umap1
->table
,
2238 &is_disjoint_entry
, &data
) < 0 &&
2242 isl_union_map_free(umap1
);
2243 isl_union_map_free(umap2
);
2245 return data
.is_disjoint
;
2247 isl_union_map_free(umap1
);
2248 isl_union_map_free(umap2
);
2249 return isl_bool_error
;
2252 /* Are "uset1" and "uset2" disjoint?
2254 isl_bool
isl_union_set_is_disjoint(__isl_keep isl_union_set
*uset1
,
2255 __isl_keep isl_union_set
*uset2
)
2257 return isl_union_map_is_disjoint(uset1
, uset2
);
2260 static isl_stat
sample_entry(void **entry
, void *user
)
2262 isl_basic_map
**sample
= (isl_basic_map
**)user
;
2263 isl_map
*map
= *entry
;
2265 *sample
= isl_map_sample(isl_map_copy(map
));
2267 return isl_stat_error
;
2268 if (!isl_basic_map_plain_is_empty(*sample
))
2269 return isl_stat_error
;
2273 __isl_give isl_basic_map
*isl_union_map_sample(__isl_take isl_union_map
*umap
)
2275 isl_basic_map
*sample
= NULL
;
2280 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2281 &sample_entry
, &sample
) < 0 &&
2286 sample
= isl_basic_map_empty(isl_union_map_get_space(umap
));
2288 isl_union_map_free(umap
);
2292 isl_union_map_free(umap
);
2296 __isl_give isl_basic_set
*isl_union_set_sample(__isl_take isl_union_set
*uset
)
2298 return bset_from_bmap(isl_union_map_sample(uset
));
2301 /* Return an element in "uset" in the form of an isl_point.
2302 * Return a void isl_point if "uset" is empty.
2304 __isl_give isl_point
*isl_union_set_sample_point(__isl_take isl_union_set
*uset
)
2306 return isl_basic_set_sample_point(isl_union_set_sample(uset
));
2309 struct isl_forall_data
{
2311 isl_bool (*fn
)(__isl_keep isl_map
*map
);
2314 static isl_stat
forall_entry(void **entry
, void *user
)
2316 struct isl_forall_data
*data
= user
;
2317 isl_map
*map
= *entry
;
2319 data
->res
= data
->fn(map
);
2321 return isl_stat_error
;
2324 return isl_stat_error
;
2329 static isl_bool
union_map_forall(__isl_keep isl_union_map
*umap
,
2330 isl_bool (*fn
)(__isl_keep isl_map
*map
))
2332 struct isl_forall_data data
= { isl_bool_true
, fn
};
2335 return isl_bool_error
;
2337 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2338 &forall_entry
, &data
) < 0 && data
.res
)
2339 return isl_bool_error
;
2344 struct isl_forall_user_data
{
2346 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
);
2350 static isl_stat
forall_user_entry(void **entry
, void *user
)
2352 struct isl_forall_user_data
*data
= user
;
2353 isl_map
*map
= *entry
;
2355 data
->res
= data
->fn(map
, data
->user
);
2357 return isl_stat_error
;
2360 return isl_stat_error
;
2365 /* Check if fn(map, user) returns true for all maps "map" in umap.
2367 static isl_bool
union_map_forall_user(__isl_keep isl_union_map
*umap
,
2368 isl_bool (*fn
)(__isl_keep isl_map
*map
, void *user
), void *user
)
2370 struct isl_forall_user_data data
= { isl_bool_true
, fn
, user
};
2373 return isl_bool_error
;
2375 if (isl_hash_table_foreach(umap
->dim
->ctx
, &umap
->table
,
2376 &forall_user_entry
, &data
) < 0 && data
.res
)
2377 return isl_bool_error
;
2382 isl_bool
isl_union_map_is_empty(__isl_keep isl_union_map
*umap
)
2384 return union_map_forall(umap
, &isl_map_is_empty
);
2387 isl_bool
isl_union_set_is_empty(__isl_keep isl_union_set
*uset
)
2389 return isl_union_map_is_empty(uset
);
2392 static isl_bool
is_subset_of_identity(__isl_keep isl_map
*map
)
2399 return isl_bool_error
;
2401 if (!isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2402 map
->dim
, isl_dim_out
))
2403 return isl_bool_false
;
2405 dim
= isl_map_get_space(map
);
2406 id
= isl_map_identity(dim
);
2408 is_subset
= isl_map_is_subset(map
, id
);
2415 /* Given an isl_union_map that consists of a single map, check
2416 * if it is single-valued.
2418 static isl_bool
single_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2423 umap
= isl_union_map_copy(umap
);
2424 map
= isl_map_from_union_map(umap
);
2425 sv
= isl_map_is_single_valued(map
);
2431 /* Internal data structure for single_valued_on_domain.
2433 * "umap" is the union map to be tested.
2434 * "sv" is set to 1 as long as "umap" may still be single-valued.
2436 struct isl_union_map_is_sv_data
{
2437 isl_union_map
*umap
;
2441 /* Check if the data->umap is single-valued on "set".
2443 * If data->umap consists of a single map on "set", then test it
2446 * Otherwise, compute
2450 * check if the result is a subset of the identity mapping and
2451 * store the result in data->sv.
2453 * Terminate as soon as data->umap has been determined not to
2456 static isl_stat
single_valued_on_domain(__isl_take isl_set
*set
, void *user
)
2458 struct isl_union_map_is_sv_data
*data
= user
;
2459 isl_union_map
*umap
, *test
;
2461 umap
= isl_union_map_copy(data
->umap
);
2462 umap
= isl_union_map_intersect_domain(umap
,
2463 isl_union_set_from_set(set
));
2465 if (isl_union_map_n_map(umap
) == 1) {
2466 data
->sv
= single_map_is_single_valued(umap
);
2467 isl_union_map_free(umap
);
2469 test
= isl_union_map_reverse(isl_union_map_copy(umap
));
2470 test
= isl_union_map_apply_range(test
, umap
);
2472 data
->sv
= union_map_forall(test
, &is_subset_of_identity
);
2474 isl_union_map_free(test
);
2477 if (data
->sv
< 0 || !data
->sv
)
2478 return isl_stat_error
;
2482 /* Check if the given map is single-valued.
2484 * If the union map consists of a single map, then test it as an isl_map.
2485 * Otherwise, check if the union map is single-valued on each of its
2488 isl_bool
isl_union_map_is_single_valued(__isl_keep isl_union_map
*umap
)
2490 isl_union_map
*universe
;
2491 isl_union_set
*domain
;
2492 struct isl_union_map_is_sv_data data
;
2494 if (isl_union_map_n_map(umap
) == 1)
2495 return single_map_is_single_valued(umap
);
2497 universe
= isl_union_map_universe(isl_union_map_copy(umap
));
2498 domain
= isl_union_map_domain(universe
);
2500 data
.sv
= isl_bool_true
;
2502 if (isl_union_set_foreach_set(domain
,
2503 &single_valued_on_domain
, &data
) < 0 && data
.sv
)
2504 data
.sv
= isl_bool_error
;
2505 isl_union_set_free(domain
);
2510 isl_bool
isl_union_map_is_injective(__isl_keep isl_union_map
*umap
)
2514 umap
= isl_union_map_copy(umap
);
2515 umap
= isl_union_map_reverse(umap
);
2516 in
= isl_union_map_is_single_valued(umap
);
2517 isl_union_map_free(umap
);
2522 /* Is "map" obviously not an identity relation because
2523 * it maps elements from one space to another space?
2524 * Update *non_identity accordingly.
2526 * In particular, if the domain and range spaces are the same,
2527 * then the map is not considered to obviously not be an identity relation.
2528 * Otherwise, the map is considered to obviously not be an identity relation
2529 * if it is is non-empty.
2531 * If "map" is determined to obviously not be an identity relation,
2532 * then the search is aborted.
2534 static isl_stat
map_plain_is_not_identity(__isl_take isl_map
*map
, void *user
)
2536 isl_bool
*non_identity
= user
;
2540 space
= isl_map_get_space(map
);
2541 equal
= isl_space_tuple_is_equal(space
, isl_dim_in
, space
, isl_dim_out
);
2542 if (equal
>= 0 && !equal
)
2543 *non_identity
= isl_bool_not(isl_map_is_empty(map
));
2545 *non_identity
= isl_bool_not(equal
);
2546 isl_space_free(space
);
2549 if (*non_identity
< 0 || *non_identity
)
2550 return isl_stat_error
;
2555 /* Is "umap" obviously not an identity relation because
2556 * it maps elements from one space to another space?
2558 * As soon as a map has been found that maps elements to a different space,
2559 * non_identity is changed and the search is aborted.
2561 static isl_bool
isl_union_map_plain_is_not_identity(
2562 __isl_keep isl_union_map
*umap
)
2564 isl_bool non_identity
;
2566 non_identity
= isl_bool_false
;
2567 if (isl_union_map_foreach_map(umap
, &map_plain_is_not_identity
,
2568 &non_identity
) < 0 &&
2569 non_identity
== isl_bool_false
)
2570 return isl_bool_error
;
2572 return non_identity
;
2575 /* Does "map" only map elements to themselves?
2576 * Update *identity accordingly.
2578 * If "map" is determined not to be an identity relation,
2579 * then the search is aborted.
2581 static isl_stat
map_is_identity(__isl_take isl_map
*map
, void *user
)
2583 isl_bool
*identity
= user
;
2585 *identity
= isl_map_is_identity(map
);
2588 if (*identity
< 0 || !*identity
)
2589 return isl_stat_error
;
2594 /* Does "umap" only map elements to themselves?
2596 * First check if there are any maps that map elements to different spaces.
2597 * If not, then check that all the maps (between identical spaces)
2598 * are identity relations.
2600 isl_bool
isl_union_map_is_identity(__isl_keep isl_union_map
*umap
)
2602 isl_bool non_identity
;
2605 non_identity
= isl_union_map_plain_is_not_identity(umap
);
2606 if (non_identity
< 0 || non_identity
)
2607 return isl_bool_not(non_identity
);
2609 identity
= isl_bool_true
;
2610 if (isl_union_map_foreach_map(umap
, &map_is_identity
, &identity
) < 0 &&
2611 identity
== isl_bool_true
)
2612 return isl_bool_error
;
2617 /* Represents a map that has a fixed value (v) for one of its
2619 * The map in this structure is not reference counted, so it
2620 * is only valid while the isl_union_map from which it was
2621 * obtained is still alive.
2623 struct isl_fixed_map
{
2628 static struct isl_fixed_map
*alloc_isl_fixed_map_array(isl_ctx
*ctx
,
2632 struct isl_fixed_map
*v
;
2634 v
= isl_calloc_array(ctx
, struct isl_fixed_map
, n
);
2637 for (i
= 0; i
< n
; ++i
)
2638 isl_int_init(v
[i
].v
);
2642 static void free_isl_fixed_map_array(struct isl_fixed_map
*v
, int n
)
2648 for (i
= 0; i
< n
; ++i
)
2649 isl_int_clear(v
[i
].v
);
2653 /* Compare the "v" field of two isl_fixed_map structs.
2655 static int qsort_fixed_map_cmp(const void *p1
, const void *p2
)
2657 const struct isl_fixed_map
*e1
= (const struct isl_fixed_map
*) p1
;
2658 const struct isl_fixed_map
*e2
= (const struct isl_fixed_map
*) p2
;
2660 return isl_int_cmp(e1
->v
, e2
->v
);
2663 /* Internal data structure used while checking whether all maps
2664 * in a union_map have a fixed value for a given output dimension.
2665 * v is the list of maps, with the fixed value for the dimension
2666 * n is the number of maps considered so far
2667 * pos is the output dimension under investigation
2669 struct isl_fixed_dim_data
{
2670 struct isl_fixed_map
*v
;
2675 static isl_bool
fixed_at_pos(__isl_keep isl_map
*map
, void *user
)
2677 struct isl_fixed_dim_data
*data
= user
;
2679 data
->v
[data
->n
].map
= map
;
2680 return isl_map_plain_is_fixed(map
, isl_dim_out
, data
->pos
,
2681 &data
->v
[data
->n
++].v
);
2684 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2685 int first
, int n_range
);
2687 /* Given a list of the maps, with their fixed values at output dimension "pos",
2688 * check whether the ranges of the maps form an obvious partition.
2690 * We first sort the maps according to their fixed values.
2691 * If all maps have a different value, then we know the ranges form
2693 * Otherwise, we collect the maps with the same fixed value and
2694 * check whether each such collection is obviously injective
2695 * based on later dimensions.
2697 static int separates(struct isl_fixed_map
*v
, int n
,
2698 __isl_take isl_space
*dim
, int pos
, int n_range
)
2705 qsort(v
, n
, sizeof(*v
), &qsort_fixed_map_cmp
);
2707 for (i
= 0; i
+ 1 < n
; ++i
) {
2709 isl_union_map
*part
;
2712 for (j
= i
+ 1; j
< n
; ++j
)
2713 if (isl_int_ne(v
[i
].v
, v
[j
].v
))
2719 part
= isl_union_map_alloc(isl_space_copy(dim
), j
- i
);
2720 for (k
= i
; k
< j
; ++k
)
2721 part
= isl_union_map_add_map(part
,
2722 isl_map_copy(v
[k
].map
));
2724 injective
= plain_injective_on_range(part
, pos
+ 1, n_range
);
2733 isl_space_free(dim
);
2734 free_isl_fixed_map_array(v
, n
);
2737 isl_space_free(dim
);
2738 free_isl_fixed_map_array(v
, n
);
2742 /* Check whether the maps in umap have obviously distinct ranges.
2743 * In particular, check for an output dimension in the range
2744 * [first,n_range) for which all maps have a fixed value
2745 * and then check if these values, possibly along with fixed values
2746 * at later dimensions, entail distinct ranges.
2748 static isl_bool
plain_injective_on_range(__isl_take isl_union_map
*umap
,
2749 int first
, int n_range
)
2753 struct isl_fixed_dim_data data
= { NULL
};
2755 ctx
= isl_union_map_get_ctx(umap
);
2757 n
= isl_union_map_n_map(umap
);
2762 isl_union_map_free(umap
);
2763 return isl_bool_true
;
2766 if (first
>= n_range
) {
2767 isl_union_map_free(umap
);
2768 return isl_bool_false
;
2771 data
.v
= alloc_isl_fixed_map_array(ctx
, n
);
2775 for (data
.pos
= first
; data
.pos
< n_range
; ++data
.pos
) {
2781 fixed
= union_map_forall_user(umap
, &fixed_at_pos
, &data
);
2786 dim
= isl_union_map_get_space(umap
);
2787 injective
= separates(data
.v
, n
, dim
, data
.pos
, n_range
);
2788 isl_union_map_free(umap
);
2792 free_isl_fixed_map_array(data
.v
, n
);
2793 isl_union_map_free(umap
);
2795 return isl_bool_false
;
2797 free_isl_fixed_map_array(data
.v
, n
);
2798 isl_union_map_free(umap
);
2799 return isl_bool_error
;
2802 /* Check whether the maps in umap that map to subsets of "ran"
2803 * have obviously distinct ranges.
2805 static isl_bool
plain_injective_on_range_wrap(__isl_keep isl_set
*ran
,
2808 isl_union_map
*umap
= user
;
2810 umap
= isl_union_map_copy(umap
);
2811 umap
= isl_union_map_intersect_range(umap
,
2812 isl_union_set_from_set(isl_set_copy(ran
)));
2813 return plain_injective_on_range(umap
, 0, isl_set_dim(ran
, isl_dim_set
));
2816 /* Check if the given union_map is obviously injective.
2818 * In particular, we first check if all individual maps are obviously
2819 * injective and then check if all the ranges of these maps are
2820 * obviously disjoint.
2822 isl_bool
isl_union_map_plain_is_injective(__isl_keep isl_union_map
*umap
)
2825 isl_union_map
*univ
;
2828 in
= union_map_forall(umap
, &isl_map_plain_is_injective
);
2830 return isl_bool_error
;
2832 return isl_bool_false
;
2834 univ
= isl_union_map_universe(isl_union_map_copy(umap
));
2835 ran
= isl_union_map_range(univ
);
2837 in
= union_map_forall_user(ran
, &plain_injective_on_range_wrap
, umap
);
2839 isl_union_set_free(ran
);
2844 isl_bool
isl_union_map_is_bijective(__isl_keep isl_union_map
*umap
)
2848 sv
= isl_union_map_is_single_valued(umap
);
2852 return isl_union_map_is_injective(umap
);
2855 __isl_give isl_union_map
*isl_union_map_zip(__isl_take isl_union_map
*umap
)
2857 struct isl_un_op_control control
= {
2858 .filter
= &isl_map_can_zip
,
2859 .fn_map
= &isl_map_zip
,
2861 return un_op(umap
, &control
);
2864 /* Given a union map, take the maps of the form A -> (B -> C) and
2865 * return the union of the corresponding maps (A -> B) -> C.
2867 __isl_give isl_union_map
*isl_union_map_uncurry(__isl_take isl_union_map
*umap
)
2869 struct isl_un_op_control control
= {
2870 .filter
= &isl_map_can_uncurry
,
2871 .fn_map
= &isl_map_uncurry
,
2873 return un_op(umap
, &control
);
2876 /* Given a union map, take the maps of the form (A -> B) -> C and
2877 * return the union of the corresponding maps A -> (B -> C).
2879 __isl_give isl_union_map
*isl_union_map_curry(__isl_take isl_union_map
*umap
)
2881 struct isl_un_op_control control
= {
2882 .filter
= &isl_map_can_curry
,
2883 .fn_map
= &isl_map_curry
,
2885 return un_op(umap
, &control
);
2888 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2889 * return the union of the corresponding maps A -> (B -> (C -> D)).
2891 __isl_give isl_union_map
*isl_union_map_range_curry(
2892 __isl_take isl_union_map
*umap
)
2894 struct isl_un_op_control control
= {
2895 .filter
= &isl_map_can_range_curry
,
2896 .fn_map
= &isl_map_range_curry
,
2898 return un_op(umap
, &control
);
2901 __isl_give isl_union_set
*isl_union_set_lift(__isl_take isl_union_set
*uset
)
2903 struct isl_un_op_control control
= {
2904 .fn_map
= &isl_set_lift
,
2906 return un_op(uset
, &control
);
2909 static isl_stat
coefficients_entry(void **entry
, void *user
)
2911 isl_set
*set
= *entry
;
2912 isl_union_set
**res
= user
;
2914 set
= isl_set_copy(set
);
2915 set
= isl_set_from_basic_set(isl_set_coefficients(set
));
2916 *res
= isl_union_set_add_set(*res
, set
);
2921 __isl_give isl_union_set
*isl_union_set_coefficients(
2922 __isl_take isl_union_set
*uset
)
2931 ctx
= isl_union_set_get_ctx(uset
);
2932 dim
= isl_space_set_alloc(ctx
, 0, 0);
2933 res
= isl_union_map_alloc(dim
, uset
->table
.n
);
2934 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2935 &coefficients_entry
, &res
) < 0)
2938 isl_union_set_free(uset
);
2941 isl_union_set_free(uset
);
2942 isl_union_set_free(res
);
2946 static isl_stat
solutions_entry(void **entry
, void *user
)
2948 isl_set
*set
= *entry
;
2949 isl_union_set
**res
= user
;
2951 set
= isl_set_copy(set
);
2952 set
= isl_set_from_basic_set(isl_set_solutions(set
));
2954 *res
= isl_union_set_from_set(set
);
2956 *res
= isl_union_set_add_set(*res
, set
);
2959 return isl_stat_error
;
2964 __isl_give isl_union_set
*isl_union_set_solutions(
2965 __isl_take isl_union_set
*uset
)
2967 isl_union_set
*res
= NULL
;
2972 if (uset
->table
.n
== 0) {
2973 res
= isl_union_set_empty(isl_union_set_get_space(uset
));
2974 isl_union_set_free(uset
);
2978 if (isl_hash_table_foreach(uset
->dim
->ctx
, &uset
->table
,
2979 &solutions_entry
, &res
) < 0)
2982 isl_union_set_free(uset
);
2985 isl_union_set_free(uset
);
2986 isl_union_set_free(res
);
2990 /* Is the domain space of "map" equal to "space"?
2992 static int domain_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
2994 return isl_space_tuple_is_equal(map
->dim
, isl_dim_in
,
2995 space
, isl_dim_out
);
2998 /* Is the range space of "map" equal to "space"?
3000 static int range_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3002 return isl_space_tuple_is_equal(map
->dim
, isl_dim_out
,
3003 space
, isl_dim_out
);
3006 /* Is the set space of "map" equal to "space"?
3008 static int set_match(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
)
3010 return isl_space_tuple_is_equal(map
->dim
, isl_dim_set
,
3011 space
, isl_dim_out
);
3014 /* Internal data structure for preimage_pw_multi_aff.
3016 * "pma" is the function under which the preimage should be taken.
3017 * "space" is the space of "pma".
3018 * "res" collects the results.
3019 * "fn" computes the preimage for a given map.
3020 * "match" returns true if "fn" can be called.
3022 struct isl_union_map_preimage_data
{
3024 isl_pw_multi_aff
*pma
;
3026 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3027 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3028 __isl_take isl_pw_multi_aff
*pma
);
3031 /* Call data->fn to compute the preimage of the domain or range of *entry
3032 * under the function represented by data->pma, provided the domain/range
3033 * space of *entry matches the target space of data->pma
3034 * (as given by data->match), and add the result to data->res.
3036 static isl_stat
preimage_entry(void **entry
, void *user
)
3039 isl_map
*map
= *entry
;
3040 struct isl_union_map_preimage_data
*data
= user
;
3043 m
= data
->match(map
, data
->space
);
3045 return isl_stat_error
;
3049 map
= isl_map_copy(map
);
3050 map
= data
->fn(map
, isl_pw_multi_aff_copy(data
->pma
));
3052 empty
= isl_map_is_empty(map
);
3053 if (empty
< 0 || empty
) {
3055 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3058 data
->res
= isl_union_map_add_map(data
->res
, map
);
3063 /* Compute the preimage of the domain or range of "umap" under the function
3064 * represented by "pma".
3065 * In other words, plug in "pma" in the domain or range of "umap".
3066 * The function "fn" performs the actual preimage computation on a map,
3067 * while "match" determines to which maps the function should be applied.
3069 static __isl_give isl_union_map
*preimage_pw_multi_aff(
3070 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
,
3071 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3072 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3073 __isl_take isl_pw_multi_aff
*pma
))
3077 struct isl_union_map_preimage_data data
;
3079 umap
= isl_union_map_align_params(umap
,
3080 isl_pw_multi_aff_get_space(pma
));
3081 pma
= isl_pw_multi_aff_align_params(pma
, isl_union_map_get_space(umap
));
3086 ctx
= isl_union_map_get_ctx(umap
);
3087 space
= isl_union_map_get_space(umap
);
3088 data
.space
= isl_pw_multi_aff_get_space(pma
);
3090 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3093 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_entry
,
3095 data
.res
= isl_union_map_free(data
.res
);
3097 isl_space_free(data
.space
);
3098 isl_union_map_free(umap
);
3099 isl_pw_multi_aff_free(pma
);
3102 isl_union_map_free(umap
);
3103 isl_pw_multi_aff_free(pma
);
3107 /* Compute the preimage of the domain of "umap" under the function
3108 * represented by "pma".
3109 * In other words, plug in "pma" in the domain of "umap".
3110 * The result contains maps that live in the same spaces as the maps of "umap"
3111 * with domain space equal to the target space of "pma",
3112 * except that the domain has been replaced by the domain space of "pma".
3114 __isl_give isl_union_map
*isl_union_map_preimage_domain_pw_multi_aff(
3115 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3117 return preimage_pw_multi_aff(umap
, pma
, &domain_match
,
3118 &isl_map_preimage_domain_pw_multi_aff
);
3121 /* Compute the preimage of the range of "umap" under the function
3122 * represented by "pma".
3123 * In other words, plug in "pma" in the range of "umap".
3124 * The result contains maps that live in the same spaces as the maps of "umap"
3125 * with range space equal to the target space of "pma",
3126 * except that the range has been replaced by the domain space of "pma".
3128 __isl_give isl_union_map
*isl_union_map_preimage_range_pw_multi_aff(
3129 __isl_take isl_union_map
*umap
, __isl_take isl_pw_multi_aff
*pma
)
3131 return preimage_pw_multi_aff(umap
, pma
, &range_match
,
3132 &isl_map_preimage_range_pw_multi_aff
);
3135 /* Compute the preimage of "uset" under the function represented by "pma".
3136 * In other words, plug in "pma" in "uset".
3137 * The result contains sets that live in the same spaces as the sets of "uset"
3138 * with space equal to the target space of "pma",
3139 * except that the space has been replaced by the domain space of "pma".
3141 __isl_give isl_union_set
*isl_union_set_preimage_pw_multi_aff(
3142 __isl_take isl_union_set
*uset
, __isl_take isl_pw_multi_aff
*pma
)
3144 return preimage_pw_multi_aff(uset
, pma
, &set_match
,
3145 &isl_set_preimage_pw_multi_aff
);
3148 /* Compute the preimage of the domain of "umap" under the function
3149 * represented by "ma".
3150 * In other words, plug in "ma" in the domain of "umap".
3151 * The result contains maps that live in the same spaces as the maps of "umap"
3152 * with domain space equal to the target space of "ma",
3153 * except that the domain has been replaced by the domain space of "ma".
3155 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_aff(
3156 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3158 return isl_union_map_preimage_domain_pw_multi_aff(umap
,
3159 isl_pw_multi_aff_from_multi_aff(ma
));
3162 /* Compute the preimage of the range of "umap" under the function
3163 * represented by "ma".
3164 * In other words, plug in "ma" in the range of "umap".
3165 * The result contains maps that live in the same spaces as the maps of "umap"
3166 * with range space equal to the target space of "ma",
3167 * except that the range has been replaced by the domain space of "ma".
3169 __isl_give isl_union_map
*isl_union_map_preimage_range_multi_aff(
3170 __isl_take isl_union_map
*umap
, __isl_take isl_multi_aff
*ma
)
3172 return isl_union_map_preimage_range_pw_multi_aff(umap
,
3173 isl_pw_multi_aff_from_multi_aff(ma
));
3176 /* Compute the preimage of "uset" under the function represented by "ma".
3177 * In other words, plug in "ma" in "uset".
3178 * The result contains sets that live in the same spaces as the sets of "uset"
3179 * with space equal to the target space of "ma",
3180 * except that the space has been replaced by the domain space of "ma".
3182 __isl_give isl_union_map
*isl_union_set_preimage_multi_aff(
3183 __isl_take isl_union_set
*uset
, __isl_take isl_multi_aff
*ma
)
3185 return isl_union_set_preimage_pw_multi_aff(uset
,
3186 isl_pw_multi_aff_from_multi_aff(ma
));
3189 /* Internal data structure for preimage_multi_pw_aff.
3191 * "mpa" is the function under which the preimage should be taken.
3192 * "space" is the space of "mpa".
3193 * "res" collects the results.
3194 * "fn" computes the preimage for a given map.
3195 * "match" returns true if "fn" can be called.
3197 struct isl_union_map_preimage_mpa_data
{
3199 isl_multi_pw_aff
*mpa
;
3201 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
);
3202 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3203 __isl_take isl_multi_pw_aff
*mpa
);
3206 /* Call data->fn to compute the preimage of the domain or range of *entry
3207 * under the function represented by data->mpa, provided the domain/range
3208 * space of *entry matches the target space of data->mpa
3209 * (as given by data->match), and add the result to data->res.
3211 static isl_stat
preimage_mpa_entry(void **entry
, void *user
)
3214 isl_map
*map
= *entry
;
3215 struct isl_union_map_preimage_mpa_data
*data
= user
;
3218 m
= data
->match(map
, data
->space
);
3220 return isl_stat_error
;
3224 map
= isl_map_copy(map
);
3225 map
= data
->fn(map
, isl_multi_pw_aff_copy(data
->mpa
));
3227 empty
= isl_map_is_empty(map
);
3228 if (empty
< 0 || empty
) {
3230 return empty
< 0 ? isl_stat_error
: isl_stat_ok
;
3233 data
->res
= isl_union_map_add_map(data
->res
, map
);
3238 /* Compute the preimage of the domain or range of "umap" under the function
3239 * represented by "mpa".
3240 * In other words, plug in "mpa" in the domain or range of "umap".
3241 * The function "fn" performs the actual preimage computation on a map,
3242 * while "match" determines to which maps the function should be applied.
3244 static __isl_give isl_union_map
*preimage_multi_pw_aff(
3245 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
,
3246 int (*match
)(__isl_keep isl_map
*map
, __isl_keep isl_space
*space
),
3247 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*map
,
3248 __isl_take isl_multi_pw_aff
*mpa
))
3252 struct isl_union_map_preimage_mpa_data data
;
3254 umap
= isl_union_map_align_params(umap
,
3255 isl_multi_pw_aff_get_space(mpa
));
3256 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_union_map_get_space(umap
));
3261 ctx
= isl_union_map_get_ctx(umap
);
3262 space
= isl_union_map_get_space(umap
);
3263 data
.space
= isl_multi_pw_aff_get_space(mpa
);
3265 data
.res
= isl_union_map_alloc(space
, umap
->table
.n
);
3268 if (isl_hash_table_foreach(ctx
, &umap
->table
, &preimage_mpa_entry
,
3270 data
.res
= isl_union_map_free(data
.res
);
3272 isl_space_free(data
.space
);
3273 isl_union_map_free(umap
);
3274 isl_multi_pw_aff_free(mpa
);
3277 isl_union_map_free(umap
);
3278 isl_multi_pw_aff_free(mpa
);
3282 /* Compute the preimage of the domain of "umap" under the function
3283 * represented by "mpa".
3284 * In other words, plug in "mpa" in the domain of "umap".
3285 * The result contains maps that live in the same spaces as the maps of "umap"
3286 * with domain space equal to the target space of "mpa",
3287 * except that the domain has been replaced by the domain space of "mpa".
3289 __isl_give isl_union_map
*isl_union_map_preimage_domain_multi_pw_aff(
3290 __isl_take isl_union_map
*umap
, __isl_take isl_multi_pw_aff
*mpa
)
3292 return preimage_multi_pw_aff(umap
, mpa
, &domain_match
,
3293 &isl_map_preimage_domain_multi_pw_aff
);
3296 /* Internal data structure for preimage_upma.
3298 * "umap" is the map of which the preimage should be computed.
3299 * "res" collects the results.
3300 * "fn" computes the preimage for a given piecewise multi-affine function.
3302 struct isl_union_map_preimage_upma_data
{
3303 isl_union_map
*umap
;
3305 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3306 __isl_take isl_pw_multi_aff
*pma
);
3309 /* Call data->fn to compute the preimage of the domain or range of data->umap
3310 * under the function represented by pma and add the result to data->res.
3312 static isl_stat
preimage_upma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3314 struct isl_union_map_preimage_upma_data
*data
= user
;
3315 isl_union_map
*umap
;
3317 umap
= isl_union_map_copy(data
->umap
);
3318 umap
= data
->fn(umap
, pma
);
3319 data
->res
= isl_union_map_union(data
->res
, umap
);
3321 return data
->res
? isl_stat_ok
: isl_stat_error
;
3324 /* Compute the preimage of the domain or range of "umap" under the function
3325 * represented by "upma".
3326 * In other words, plug in "upma" in the domain or range of "umap".
3327 * The function "fn" performs the actual preimage computation
3328 * on a piecewise multi-affine function.
3330 static __isl_give isl_union_map
*preimage_union_pw_multi_aff(
3331 __isl_take isl_union_map
*umap
,
3332 __isl_take isl_union_pw_multi_aff
*upma
,
3333 __isl_give isl_union_map
*(*fn
)(__isl_take isl_union_map
*umap
,
3334 __isl_take isl_pw_multi_aff
*pma
))
3336 struct isl_union_map_preimage_upma_data data
;
3339 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3341 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3342 &preimage_upma
, &data
) < 0)
3343 data
.res
= isl_union_map_free(data
.res
);
3345 isl_union_map_free(umap
);
3346 isl_union_pw_multi_aff_free(upma
);
3351 /* Compute the preimage of the domain of "umap" under the function
3352 * represented by "upma".
3353 * In other words, plug in "upma" in the domain of "umap".
3354 * The result contains maps that live in the same spaces as the maps of "umap"
3355 * with domain space equal to one of the target spaces of "upma",
3356 * except that the domain has been replaced by one of the the domain spaces that
3357 * corresponds to that target space of "upma".
3359 __isl_give isl_union_map
*isl_union_map_preimage_domain_union_pw_multi_aff(
3360 __isl_take isl_union_map
*umap
,
3361 __isl_take isl_union_pw_multi_aff
*upma
)
3363 return preimage_union_pw_multi_aff(umap
, upma
,
3364 &isl_union_map_preimage_domain_pw_multi_aff
);
3367 /* Compute the preimage of the range of "umap" under the function
3368 * represented by "upma".
3369 * In other words, plug in "upma" in the range of "umap".
3370 * The result contains maps that live in the same spaces as the maps of "umap"
3371 * with range space equal to one of the target spaces of "upma",
3372 * except that the range has been replaced by one of the the domain spaces that
3373 * corresponds to that target space of "upma".
3375 __isl_give isl_union_map
*isl_union_map_preimage_range_union_pw_multi_aff(
3376 __isl_take isl_union_map
*umap
,
3377 __isl_take isl_union_pw_multi_aff
*upma
)
3379 return preimage_union_pw_multi_aff(umap
, upma
,
3380 &isl_union_map_preimage_range_pw_multi_aff
);
3383 /* Compute the preimage of "uset" under the function represented by "upma".
3384 * In other words, plug in "upma" in the range of "uset".
3385 * The result contains sets that live in the same spaces as the sets of "uset"
3386 * with space equal to one of the target spaces of "upma",
3387 * except that the space has been replaced by one of the the domain spaces that
3388 * corresponds to that target space of "upma".
3390 __isl_give isl_union_set
*isl_union_set_preimage_union_pw_multi_aff(
3391 __isl_take isl_union_set
*uset
,
3392 __isl_take isl_union_pw_multi_aff
*upma
)
3394 return preimage_union_pw_multi_aff(uset
, upma
,
3395 &isl_union_set_preimage_pw_multi_aff
);
3398 /* Reset the user pointer on all identifiers of parameters and tuples
3399 * of the spaces of "umap".
3401 __isl_give isl_union_map
*isl_union_map_reset_user(
3402 __isl_take isl_union_map
*umap
)
3404 umap
= isl_union_map_cow(umap
);
3407 umap
->dim
= isl_space_reset_user(umap
->dim
);
3409 return isl_union_map_free(umap
);
3410 return total(umap
, &isl_map_reset_user
);
3413 /* Reset the user pointer on all identifiers of parameters and tuples
3414 * of the spaces of "uset".
3416 __isl_give isl_union_set
*isl_union_set_reset_user(
3417 __isl_take isl_union_set
*uset
)
3419 return isl_union_map_reset_user(uset
);
3422 /* Remove all existentially quantified variables and integer divisions
3423 * from "umap" using Fourier-Motzkin elimination.
3425 __isl_give isl_union_map
*isl_union_map_remove_divs(
3426 __isl_take isl_union_map
*umap
)
3428 return total(umap
, &isl_map_remove_divs
);
3431 /* Remove all existentially quantified variables and integer divisions
3432 * from "uset" using Fourier-Motzkin elimination.
3434 __isl_give isl_union_set
*isl_union_set_remove_divs(
3435 __isl_take isl_union_set
*uset
)
3437 return isl_union_map_remove_divs(uset
);
3440 /* Internal data structure for isl_union_map_project_out.
3441 * "type", "first" and "n" are the arguments for the isl_map_project_out
3443 * "res" collects the results.
3445 struct isl_union_map_project_out_data
{
3446 enum isl_dim_type type
;
3453 /* Turn the data->n dimensions of type data->type, starting at data->first
3454 * into existentially quantified variables and add the result to data->res.
3456 static isl_stat
project_out(__isl_take isl_map
*map
, void *user
)
3458 struct isl_union_map_project_out_data
*data
= user
;
3460 map
= isl_map_project_out(map
, data
->type
, data
->first
, data
->n
);
3461 data
->res
= isl_union_map_add_map(data
->res
, map
);
3466 /* Turn the "n" dimensions of type "type", starting at "first"
3467 * into existentially quantified variables.
3468 * Since the space of an isl_union_map only contains parameters,
3469 * type is required to be equal to isl_dim_param.
3471 __isl_give isl_union_map
*isl_union_map_project_out(
3472 __isl_take isl_union_map
*umap
,
3473 enum isl_dim_type type
, unsigned first
, unsigned n
)
3476 struct isl_union_map_project_out_data data
= { type
, first
, n
};
3481 if (type
!= isl_dim_param
)
3482 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3483 "can only project out parameters",
3484 return isl_union_map_free(umap
));
3486 space
= isl_union_map_get_space(umap
);
3487 space
= isl_space_drop_dims(space
, type
, first
, n
);
3488 data
.res
= isl_union_map_empty(space
);
3489 if (isl_union_map_foreach_map(umap
, &project_out
, &data
) < 0)
3490 data
.res
= isl_union_map_free(data
.res
);
3492 isl_union_map_free(umap
);
3497 /* Turn the "n" dimensions of type "type", starting at "first"
3498 * into existentially quantified variables.
3499 * Since the space of an isl_union_set only contains parameters,
3500 * "type" is required to be equal to isl_dim_param.
3502 __isl_give isl_union_set
*isl_union_set_project_out(
3503 __isl_take isl_union_set
*uset
,
3504 enum isl_dim_type type
, unsigned first
, unsigned n
)
3506 return isl_union_map_project_out(uset
, type
, first
, n
);
3509 /* Internal data structure for isl_union_map_involves_dims.
3510 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3512 struct isl_union_map_involves_dims_data
{
3517 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3519 static isl_bool
map_excludes(__isl_keep isl_map
*map
, void *user
)
3521 struct isl_union_map_involves_dims_data
*data
= user
;
3524 involves
= isl_map_involves_dims(map
,
3525 isl_dim_param
, data
->first
, data
->n
);
3527 return isl_bool_error
;
3531 /* Does "umap" involve any of the n parameters starting at first?
3532 * "type" is required to be set to isl_dim_param.
3534 * "umap" involves any of those parameters if any of its maps
3535 * involve the parameters. In other words, "umap" does not
3536 * involve any of the parameters if all its maps to not
3537 * involve the parameters.
3539 isl_bool
isl_union_map_involves_dims(__isl_keep isl_union_map
*umap
,
3540 enum isl_dim_type type
, unsigned first
, unsigned n
)
3542 struct isl_union_map_involves_dims_data data
= { first
, n
};
3545 if (type
!= isl_dim_param
)
3546 isl_die(isl_union_map_get_ctx(umap
), isl_error_invalid
,
3547 "can only reference parameters", return isl_bool_error
);
3549 excludes
= union_map_forall_user(umap
, &map_excludes
, &data
);
3552 return isl_bool_error
;
3557 /* Internal data structure for isl_union_map_reset_range_space.
3558 * "range" is the space from which to set the range space.
3559 * "res" collects the results.
3561 struct isl_union_map_reset_range_space_data
{
3566 /* Replace the range space of "map" by the range space of data->range and
3567 * add the result to data->res.
3569 static isl_stat
reset_range_space(__isl_take isl_map
*map
, void *user
)
3571 struct isl_union_map_reset_range_space_data
*data
= user
;
3574 space
= isl_map_get_space(map
);
3575 space
= isl_space_domain(space
);
3576 space
= isl_space_extend_domain_with_range(space
,
3577 isl_space_copy(data
->range
));
3578 map
= isl_map_reset_space(map
, space
);
3579 data
->res
= isl_union_map_add_map(data
->res
, map
);
3581 return data
->res
? isl_stat_ok
: isl_stat_error
;
3584 /* Replace the range space of all the maps in "umap" by
3585 * the range space of "space".
3587 * This assumes that all maps have the same output dimension.
3588 * This function should therefore not be made publicly available.
3590 * Since the spaces of the maps change, so do their hash value.
3591 * We therefore need to create a new isl_union_map.
3593 __isl_give isl_union_map
*isl_union_map_reset_range_space(
3594 __isl_take isl_union_map
*umap
, __isl_take isl_space
*space
)
3596 struct isl_union_map_reset_range_space_data data
= { space
};
3598 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3599 if (isl_union_map_foreach_map(umap
, &reset_range_space
, &data
) < 0)
3600 data
.res
= isl_union_map_free(data
.res
);
3602 isl_space_free(space
);
3603 isl_union_map_free(umap
);
3607 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3608 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3609 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3610 * to the domain and the range of each map.
3611 * "res" collects the results.
3613 struct isl_union_order_at_data
{
3614 isl_multi_union_pw_aff
*mupa
;
3615 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3616 __isl_take isl_multi_pw_aff
*mpa2
);
3620 /* Intersect "map" with the result of applying data->order to
3621 * the functions in data->mupa that apply to the domain and the range
3622 * of "map" and add the result to data->res.
3624 static isl_stat
order_at(__isl_take isl_map
*map
, void *user
)
3626 struct isl_union_order_at_data
*data
= user
;
3628 isl_multi_pw_aff
*mpa1
, *mpa2
;
3631 space
= isl_space_domain(isl_map_get_space(map
));
3632 mpa1
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3633 space
= isl_space_range(isl_map_get_space(map
));
3634 mpa2
= isl_multi_union_pw_aff_extract_multi_pw_aff(data
->mupa
, space
);
3635 order
= data
->order(mpa1
, mpa2
);
3636 map
= isl_map_intersect(map
, order
);
3637 data
->res
= isl_union_map_add_map(data
->res
, map
);
3639 return data
->res
? isl_stat_ok
: isl_stat_error
;
3642 /* Intersect each map in "umap" with the result of calling "order"
3643 * on the functions is "mupa" that apply to the domain and the range
3646 static __isl_give isl_union_map
*isl_union_map_order_at_multi_union_pw_aff(
3647 __isl_take isl_union_map
*umap
, __isl_take isl_multi_union_pw_aff
*mupa
,
3648 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
3649 __isl_take isl_multi_pw_aff
*mpa2
))
3651 struct isl_union_order_at_data data
;
3653 umap
= isl_union_map_align_params(umap
,
3654 isl_multi_union_pw_aff_get_space(mupa
));
3655 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
3656 isl_union_map_get_space(umap
));
3659 data
.res
= isl_union_map_empty(isl_union_map_get_space(umap
));
3660 if (isl_union_map_foreach_map(umap
, &order_at
, &data
) < 0)
3661 data
.res
= isl_union_map_free(data
.res
);
3663 isl_multi_union_pw_aff_free(mupa
);
3664 isl_union_map_free(umap
);
3668 /* Return the subset of "umap" where the domain and the range
3669 * have equal "mupa" values.
3671 __isl_give isl_union_map
*isl_union_map_eq_at_multi_union_pw_aff(
3672 __isl_take isl_union_map
*umap
,
3673 __isl_take isl_multi_union_pw_aff
*mupa
)
3675 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3676 &isl_multi_pw_aff_eq_map
);
3679 /* Return the subset of "umap" where the domain has a lexicographically
3680 * smaller "mupa" value than the range.
3682 __isl_give isl_union_map
*isl_union_map_lex_lt_at_multi_union_pw_aff(
3683 __isl_take isl_union_map
*umap
,
3684 __isl_take isl_multi_union_pw_aff
*mupa
)
3686 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3687 &isl_multi_pw_aff_lex_lt_map
);
3690 /* Return the subset of "umap" where the domain has a lexicographically
3691 * greater "mupa" value than the range.
3693 __isl_give isl_union_map
*isl_union_map_lex_gt_at_multi_union_pw_aff(
3694 __isl_take isl_union_map
*umap
,
3695 __isl_take isl_multi_union_pw_aff
*mupa
)
3697 return isl_union_map_order_at_multi_union_pw_aff(umap
, mupa
,
3698 &isl_multi_pw_aff_lex_gt_map
);
3701 /* Return the union of the elements in the list "list".
3703 __isl_give isl_union_set
*isl_union_set_list_union(
3704 __isl_take isl_union_set_list
*list
)
3714 ctx
= isl_union_set_list_get_ctx(list
);
3715 space
= isl_space_params_alloc(ctx
, 0);
3716 res
= isl_union_set_empty(space
);
3718 n
= isl_union_set_list_n_union_set(list
);
3719 for (i
= 0; i
< n
; ++i
) {
3720 isl_union_set
*uset_i
;
3722 uset_i
= isl_union_set_list_get_union_set(list
, i
);
3723 res
= isl_union_set_union(res
, uset_i
);
3726 isl_union_set_list_free(list
);
3730 /* Update *hash with the hash value of "map".
3732 static isl_stat
add_hash(__isl_take isl_map
*map
, void *user
)
3734 uint32_t *hash
= user
;
3737 map_hash
= isl_map_get_hash(map
);
3738 isl_hash_hash(*hash
, map_hash
);
3744 /* Return a hash value that digests "umap".
3746 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map
*umap
)
3753 hash
= isl_hash_init();
3754 if (isl_union_map_foreach_map(umap
, &add_hash
, &hash
) < 0)
3760 /* Return a hash value that digests "uset".
3762 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set
*uset
)
3764 return isl_union_map_get_hash(uset
);
3767 /* Add the number of basic sets in "set" to "n".
3769 static isl_stat
add_n(__isl_take isl_set
*set
, void *user
)
3773 *n
+= isl_set_n_basic_set(set
);
3779 /* Return the total number of basic sets in "uset".
3781 int isl_union_set_n_basic_set(__isl_keep isl_union_set
*uset
)
3785 if (isl_union_set_foreach_set(uset
, &add_n
, &n
) < 0)
3791 /* Add the basic sets in "set" to "list".
3793 static isl_stat
add_list(__isl_take isl_set
*set
, void *user
)
3795 isl_basic_set_list
**list
= user
;
3796 isl_basic_set_list
*list_i
;
3798 list_i
= isl_set_get_basic_set_list(set
);
3799 *list
= isl_basic_set_list_concat(*list
, list_i
);
3803 return isl_stat_error
;
3807 /* Return a list containing all the basic sets in "uset".
3809 * First construct a list of the appropriate size and
3810 * then add all the elements.
3812 __isl_give isl_basic_set_list
*isl_union_set_get_basic_set_list(
3813 __isl_keep isl_union_set
*uset
)
3817 isl_basic_set_list
*list
;
3821 ctx
= isl_union_set_get_ctx(uset
);
3822 n
= isl_union_set_n_basic_set(uset
);
3825 list
= isl_basic_set_list_alloc(ctx
, n
);
3826 if (isl_union_set_foreach_set(uset
, &add_list
, &list
) < 0)
3827 list
= isl_basic_set_list_free(list
);