isl_transitive_closure.c: incremental_on_entire_domain: return isl_bool
[isl.git] / isl_union_map.c
blob8acdc8e6cc701ac413cce6d3cbaa86a68491a8d9
1 /*
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,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/ctx.h>
19 #include <isl/hash.h>
20 #include <isl_aff_private.h>
21 #include <isl/map.h>
22 #include <isl/set.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl_maybe_map.h>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
30 #include <uset_to_umap.c>
31 #include <uset_from_umap.c>
32 #include <set_list_from_map_list_inl.c>
34 /* Return the number of parameters of "umap", where "type"
35 * is required to be set to isl_dim_param.
37 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
38 enum isl_dim_type type)
40 if (!umap)
41 return 0;
43 if (type != isl_dim_param)
44 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
45 "can only reference parameters", return 0);
47 return isl_space_dim(umap->dim, type);
50 /* Return the number of parameters of "uset", where "type"
51 * is required to be set to isl_dim_param.
53 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
54 enum isl_dim_type type)
56 return isl_union_map_dim(uset, type);
59 /* Return the id of the specified dimension.
61 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
62 enum isl_dim_type type, unsigned pos)
64 if (!umap)
65 return NULL;
67 if (type != isl_dim_param)
68 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
69 "can only reference parameters", return NULL);
71 return isl_space_get_dim_id(umap->dim, type, pos);
74 /* Is this union set a parameter domain?
76 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
78 isl_set *set;
79 isl_bool params;
81 if (!uset)
82 return isl_bool_error;
83 if (uset->table.n != 1)
84 return isl_bool_false;
86 set = isl_set_from_union_set(isl_union_set_copy(uset));
87 params = isl_set_is_params(set);
88 isl_set_free(set);
89 return params;
92 /* Is this union map actually a parameter domain?
93 * Users should never call this function. Outside of isl,
94 * a union map can never be a parameter domain.
96 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
98 return isl_union_set_is_params(uset_from_umap(umap));
101 static __isl_give isl_union_map *isl_union_map_alloc(
102 __isl_take isl_space *space, int size)
104 isl_union_map *umap;
106 space = isl_space_params(space);
107 if (!space)
108 return NULL;
110 umap = isl_calloc_type(space->ctx, isl_union_map);
111 if (!umap) {
112 isl_space_free(space);
113 return NULL;
116 umap->ref = 1;
117 umap->dim = space;
118 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
119 return isl_union_map_free(umap);
121 return umap;
124 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
126 return isl_union_map_alloc(space, 16);
129 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
131 return isl_union_map_empty(space);
134 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
136 return umap ? umap->dim->ctx : NULL;
139 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
141 return uset ? uset->dim->ctx : NULL;
144 /* Return the space of "umap".
146 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
148 return umap ? umap->dim : NULL;
151 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
153 return isl_space_copy(isl_union_map_peek_space(umap));
156 /* Return the position of the parameter with the given name
157 * in "umap".
158 * Return -1 if no such dimension can be found.
160 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
161 enum isl_dim_type type, const char *name)
163 if (!umap)
164 return -1;
165 return isl_space_find_dim_by_name(umap->dim, type, name);
168 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
170 return isl_union_map_get_space(uset);
173 static isl_stat free_umap_entry(void **entry, void *user)
175 isl_map *map = *entry;
176 isl_map_free(map);
177 return isl_stat_ok;
180 static isl_stat add_map(__isl_take isl_map *map, void *user)
182 isl_union_map **umap = (isl_union_map **)user;
184 *umap = isl_union_map_add_map(*umap, map);
186 return isl_stat_ok;
189 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
191 isl_union_map *dup;
193 if (!umap)
194 return NULL;
196 dup = isl_union_map_empty(isl_space_copy(umap->dim));
197 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
198 goto error;
199 return dup;
200 error:
201 isl_union_map_free(dup);
202 return NULL;
205 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
207 if (!umap)
208 return NULL;
210 if (umap->ref == 1)
211 return umap;
212 umap->ref--;
213 return isl_union_map_dup(umap);
216 struct isl_union_align {
217 isl_reordering *exp;
218 isl_union_map *res;
221 static isl_stat align_entry(void **entry, void *user)
223 isl_map *map = *entry;
224 isl_reordering *exp;
225 struct isl_union_align *data = user;
227 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
228 isl_map_get_space(map));
230 data->res = isl_union_map_add_map(data->res,
231 isl_map_realign(isl_map_copy(map), exp));
233 return isl_stat_ok;
236 /* Align the parameters of umap along those of model.
237 * The result has the parameters of model first, in the same order
238 * as they appear in model, followed by any remaining parameters of
239 * umap that do not appear in model.
241 __isl_give isl_union_map *isl_union_map_align_params(
242 __isl_take isl_union_map *umap, __isl_take isl_space *model)
244 struct isl_union_align data = { NULL, NULL };
245 isl_bool equal_params;
247 if (!umap || !model)
248 goto error;
250 equal_params = isl_space_has_equal_params(umap->dim, model);
251 if (equal_params < 0)
252 goto error;
253 if (equal_params) {
254 isl_space_free(model);
255 return umap;
258 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
259 if (!data.exp)
260 goto error;
262 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
263 umap->table.n);
264 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
265 &align_entry, &data) < 0)
266 goto error;
268 isl_reordering_free(data.exp);
269 isl_union_map_free(umap);
270 isl_space_free(model);
271 return data.res;
272 error:
273 isl_reordering_free(data.exp);
274 isl_union_map_free(umap);
275 isl_union_map_free(data.res);
276 isl_space_free(model);
277 return NULL;
280 __isl_give isl_union_set *isl_union_set_align_params(
281 __isl_take isl_union_set *uset, __isl_take isl_space *model)
283 return isl_union_map_align_params(uset, model);
286 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
287 __isl_take isl_union_map *umap2)
289 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
290 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
292 umap1 = isl_union_map_cow(umap1);
294 if (!umap1 || !umap2)
295 goto error;
297 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
298 goto error;
300 isl_union_map_free(umap2);
302 return umap1;
303 error:
304 isl_union_map_free(umap1);
305 isl_union_map_free(umap2);
306 return NULL;
309 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
310 __isl_take isl_union_set *uset2)
312 return isl_union_map_union(uset1, uset2);
315 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
317 if (!umap)
318 return NULL;
320 umap->ref++;
321 return umap;
324 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
326 return isl_union_map_copy(uset);
329 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
331 if (!umap)
332 return NULL;
334 if (--umap->ref > 0)
335 return NULL;
337 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
338 &free_umap_entry, NULL);
339 isl_hash_table_clear(&umap->table);
340 isl_space_free(umap->dim);
341 free(umap);
342 return NULL;
345 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
347 return isl_union_map_free(uset);
350 /* Do "umap" and "space" have the same parameters?
352 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
353 __isl_keep isl_space *space)
355 isl_space *umap_space;
357 umap_space = isl_union_map_peek_space(umap);
358 return isl_space_has_equal_params(umap_space, space);
361 /* Do "uset" and "space" have the same parameters?
363 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
364 __isl_keep isl_space *space)
366 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
369 static int has_space(const void *entry, const void *val)
371 isl_map *map = (isl_map *)entry;
372 isl_space *space = (isl_space *) val;
374 return isl_space_is_equal(map->dim, space);
377 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
378 __isl_take isl_map *map)
380 uint32_t hash;
381 struct isl_hash_table_entry *entry;
382 isl_bool aligned;
384 if (!map || !umap)
385 goto error;
387 if (isl_map_plain_is_empty(map)) {
388 isl_map_free(map);
389 return umap;
392 aligned = isl_map_space_has_equal_params(map, umap->dim);
393 if (aligned < 0)
394 goto error;
395 if (!aligned) {
396 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
397 map = isl_map_align_params(map, isl_union_map_get_space(umap));
400 umap = isl_union_map_cow(umap);
402 if (!map || !umap)
403 goto error;
405 hash = isl_space_get_hash(map->dim);
406 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
407 &has_space, map->dim, 1);
408 if (!entry)
409 goto error;
411 if (!entry->data)
412 entry->data = map;
413 else {
414 entry->data = isl_map_union(entry->data, isl_map_copy(map));
415 if (!entry->data)
416 goto error;
417 isl_map_free(map);
420 return umap;
421 error:
422 isl_map_free(map);
423 isl_union_map_free(umap);
424 return NULL;
427 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
428 __isl_take isl_set *set)
430 return isl_union_map_add_map(uset, set_to_map(set));
433 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
435 isl_space *dim;
436 isl_union_map *umap;
438 if (!map)
439 return NULL;
441 dim = isl_map_get_space(map);
442 dim = isl_space_params(dim);
443 umap = isl_union_map_empty(dim);
444 umap = isl_union_map_add_map(umap, map);
446 return umap;
449 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
451 return isl_union_map_from_map(set_to_map(set));
454 __isl_give isl_union_map *isl_union_map_from_basic_map(
455 __isl_take isl_basic_map *bmap)
457 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
460 __isl_give isl_union_set *isl_union_set_from_basic_set(
461 __isl_take isl_basic_set *bset)
463 return isl_union_map_from_basic_map(bset);
466 struct isl_union_map_foreach_data
468 isl_stat (*fn)(__isl_take isl_map *map, void *user);
469 void *user;
472 static isl_stat call_on_copy(void **entry, void *user)
474 isl_map *map = *entry;
475 struct isl_union_map_foreach_data *data;
476 data = (struct isl_union_map_foreach_data *)user;
478 return data->fn(isl_map_copy(map), data->user);
481 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
483 return umap ? umap->table.n : 0;
486 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
488 return uset ? uset->table.n : 0;
491 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
492 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
494 struct isl_union_map_foreach_data data = { fn, user };
496 if (!umap)
497 return isl_stat_error;
499 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
500 &call_on_copy, &data);
503 /* Internal data structure for isl_union_map_every_map.
505 * "test" is the user-specified callback function.
506 * "user" is the user-specified callback function argument.
508 * "failed" is initialized to 0 and set to 1 if "test" fails
509 * on any map.
511 struct isl_union_map_every_data {
512 isl_bool (*test)(__isl_keep isl_map *map, void *user);
513 void *user;
514 int failed;
517 /* Call data->test on "map".
518 * If this fails, then set data->failed and abort.
520 static isl_stat call_every(void **entry, void *user)
522 isl_map *map = *entry;
523 struct isl_union_map_every_data *data = user;
524 isl_bool r;
526 r = data->test(map, data->user);
527 if (r < 0)
528 return isl_stat_error;
529 if (r)
530 return isl_stat_ok;
531 data->failed = 1;
532 return isl_stat_error;
535 /* Does "test" succeed on every map in "umap"?
537 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
538 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
540 struct isl_union_map_every_data data = { test, user, 0 };
541 isl_stat r;
543 if (!umap)
544 return isl_bool_error;
546 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
547 &call_every, &data);
548 if (r >= 0)
549 return isl_bool_true;
550 if (data.failed)
551 return isl_bool_false;
552 return isl_bool_error;
555 /* Add "map" to "list".
557 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
559 isl_map_list **list = user;
561 *list = isl_map_list_add(*list, map);
563 if (!*list)
564 return isl_stat_error;
565 return isl_stat_ok;
568 /* Return the maps in "umap" as a list.
570 * First construct a list of the appropriate size and then add all the
571 * elements.
573 __isl_give isl_map_list *isl_union_map_get_map_list(
574 __isl_keep isl_union_map *umap)
576 int n_maps;
577 isl_ctx *ctx;
578 isl_map_list *list;
580 if (!umap)
581 return NULL;
582 ctx = isl_union_map_get_ctx(umap);
583 n_maps = isl_union_map_n_map(umap);
584 list = isl_map_list_alloc(ctx, n_maps);
586 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
587 list = isl_map_list_free(list);
589 return list;
592 /* Return the sets in "uset" as a list.
594 __isl_give isl_set_list *isl_union_set_get_set_list(
595 __isl_keep isl_union_set *uset)
597 return set_list_from_map_list(
598 isl_union_map_get_map_list(uset_to_umap(uset)));
601 static isl_stat copy_map(void **entry, void *user)
603 isl_map *map = *entry;
604 isl_map **map_p = user;
606 *map_p = isl_map_copy(map);
608 return isl_stat_error;
611 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
613 isl_ctx *ctx;
614 isl_map *map = NULL;
616 if (!umap)
617 return NULL;
618 ctx = isl_union_map_get_ctx(umap);
619 if (umap->table.n != 1)
620 isl_die(ctx, isl_error_invalid,
621 "union map needs to contain elements in exactly "
622 "one space", goto error);
624 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
626 isl_union_map_free(umap);
628 return map;
629 error:
630 isl_union_map_free(umap);
631 return NULL;
634 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
636 return isl_map_from_union_map(uset);
639 /* Extract the map in "umap" that lives in the given space (ignoring
640 * parameters).
642 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
643 __isl_take isl_space *space)
645 uint32_t hash;
646 struct isl_hash_table_entry *entry;
648 space = isl_space_drop_all_params(space);
649 space = isl_space_align_params(space, isl_union_map_get_space(umap));
650 if (!umap || !space)
651 goto error;
653 hash = isl_space_get_hash(space);
654 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
655 &has_space, space, 0);
656 if (!entry)
657 return isl_map_empty(space);
658 isl_space_free(space);
659 return isl_map_copy(entry->data);
660 error:
661 isl_space_free(space);
662 return NULL;
665 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
666 __isl_take isl_space *dim)
668 return set_from_map(isl_union_map_extract_map(uset, dim));
671 /* Check if umap contains a map in the given space.
673 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
674 __isl_keep isl_space *space)
676 uint32_t hash;
677 struct isl_hash_table_entry *entry;
679 if (!umap || !space)
680 return isl_bool_error;
682 hash = isl_space_get_hash(space);
683 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
684 &has_space, space, 0);
685 return !!entry;
688 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
689 __isl_keep isl_space *space)
691 return isl_union_map_contains(uset, space);
694 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
695 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
697 return isl_union_map_foreach_map(uset,
698 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
701 struct isl_union_set_foreach_point_data {
702 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
703 void *user;
706 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
708 struct isl_union_set_foreach_point_data *data = user;
709 isl_stat r;
711 r = isl_set_foreach_point(set, data->fn, data->user);
712 isl_set_free(set);
714 return r;
717 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
718 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
720 struct isl_union_set_foreach_point_data data = { fn, user };
721 return isl_union_set_foreach_set(uset, &foreach_point, &data);
724 /* Data structure that specifies how gen_bin_op should
725 * construct results from the inputs.
727 * If "subtract" is set, then a map in the first input is copied to the result
728 * if there is no corresponding map in the second input.
729 * Otherwise, a map in the first input with no corresponding map
730 * in the second input is ignored.
731 * If "filter" is not NULL, then it specifies which maps in the first
732 * input may have a matching map in the second input.
733 * In particular, it makes sure that "match_space" can be called
734 * on the space of the map.
735 * "match_space" specifies how to transform the space of a map
736 * in the first input to the space of the corresponding map
737 * in the second input.
738 * "fn_map" specifies how the matching maps, one from each input,
739 * should be combined to form a map in the result.
741 struct isl_bin_op_control {
742 int subtract;
743 isl_bool (*filter)(__isl_keep isl_map *map);
744 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
745 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
746 __isl_take isl_map *map2);
749 /* Internal data structure for gen_bin_op.
750 * "control" specifies how the maps in the result should be constructed.
751 * "umap2" is a pointer to the second argument.
752 * "res" collects the results.
754 struct isl_union_map_gen_bin_data {
755 struct isl_bin_op_control *control;
756 isl_union_map *umap2;
757 isl_union_map *res;
760 /* Add a copy of "map" to "res" and return the result.
762 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
763 __isl_keep isl_map *map)
765 return isl_union_map_add_map(res, isl_map_copy(map));
768 /* Combine "map1" and "map2", add the result to "res" and return the result.
769 * Check whether the result is empty before adding it to "res".
771 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
772 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
773 struct isl_union_map_gen_bin_data *data)
775 isl_bool empty;
776 isl_map *map;
778 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
779 empty = isl_map_is_empty(map);
780 if (empty < 0 || empty) {
781 isl_map_free(map);
782 if (empty < 0)
783 return isl_union_map_free(res);
784 return res;
786 return isl_union_map_add_map(res, map);
789 /* Dummy match_space function that simply returns the input space.
791 static __isl_give isl_space *identity(__isl_take isl_space *space)
793 return space;
796 /* Look for the map in data->umap2 that corresponds to "map", if any.
797 * Return (isl_bool_true, matching map) if there is one,
798 * (isl_bool_false, NULL) if there is no matching map and
799 * (isl_bool_error, NULL) on error.
801 * If not NULL, then data->control->filter specifies whether "map"
802 * can have any matching map. If so,
803 * data->control->match_space specifies which map in data->umap2
804 * corresponds to "map".
806 static __isl_keep isl_maybe_isl_map bin_try_get_match(
807 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
809 uint32_t hash;
810 struct isl_hash_table_entry *entry2;
811 isl_space *space;
812 isl_maybe_isl_map res = { isl_bool_error, NULL };
814 if (data->control->filter) {
815 res.valid = data->control->filter(map);
816 if (res.valid < 0 || !res.valid)
817 return res;
818 res.valid = isl_bool_error;
821 space = isl_map_get_space(map);
822 if (data->control->match_space != &identity)
823 space = data->control->match_space(space);
824 if (!space)
825 return res;
826 hash = isl_space_get_hash(space);
827 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
828 &data->umap2->table, hash,
829 &has_space, space, 0);
830 isl_space_free(space);
831 res.valid = entry2 != NULL;
832 if (entry2)
833 res.value = entry2->data;
835 return res;
838 /* isl_hash_table_foreach callback for gen_bin_op.
839 * Look for the map in data->umap2 that corresponds
840 * to the map that "entry" points to, apply the binary operation and
841 * add the result to data->res.
843 * If no corresponding map can be found, then the effect depends
844 * on data->control->subtract. If it is set, then the current map
845 * is added directly to the result. Otherwise, it is ignored.
847 static isl_stat gen_bin_entry(void **entry, void *user)
849 struct isl_union_map_gen_bin_data *data = user;
850 isl_map *map = *entry;
851 isl_maybe_isl_map m;
853 m = bin_try_get_match(data, map);
854 if (m.valid < 0)
855 return isl_stat_error;
856 if (!m.valid && !data->control->subtract)
857 return isl_stat_ok;
859 if (!m.valid)
860 data->res = bin_add_map(data->res, map);
861 else
862 data->res = bin_add_pair(data->res, map, m.value, data);
863 if (!data->res)
864 return isl_stat_error;
866 return isl_stat_ok;
869 /* Apply a binary operation to "umap1" and "umap2" based on "control".
870 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
871 * in gen_bin_entry.
873 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
874 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
876 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
878 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
879 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
881 if (!umap1 || !umap2)
882 goto error;
884 data.umap2 = umap2;
885 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
886 umap1->table.n);
887 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
888 &gen_bin_entry, &data) < 0)
889 goto error;
891 isl_union_map_free(umap1);
892 isl_union_map_free(umap2);
893 return data.res;
894 error:
895 isl_union_map_free(umap1);
896 isl_union_map_free(umap2);
897 isl_union_map_free(data.res);
898 return NULL;
901 __isl_give isl_union_map *isl_union_map_subtract(
902 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
904 struct isl_bin_op_control control = {
905 .subtract = 1,
906 .match_space = &identity,
907 .fn_map = &isl_map_subtract,
910 return gen_bin_op(umap1, umap2, &control);
913 __isl_give isl_union_set *isl_union_set_subtract(
914 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
916 return isl_union_map_subtract(uset1, uset2);
919 struct isl_union_map_gen_bin_set_data {
920 isl_set *set;
921 isl_union_map *res;
924 static isl_stat intersect_params_entry(void **entry, void *user)
926 struct isl_union_map_gen_bin_set_data *data = user;
927 isl_map *map = *entry;
928 int empty;
930 map = isl_map_copy(map);
931 map = isl_map_intersect_params(map, isl_set_copy(data->set));
933 empty = isl_map_is_empty(map);
934 if (empty < 0) {
935 isl_map_free(map);
936 return isl_stat_error;
939 data->res = isl_union_map_add_map(data->res, map);
941 return isl_stat_ok;
944 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
945 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
947 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
949 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
950 set = isl_set_align_params(set, isl_union_map_get_space(umap));
952 if (!umap || !set)
953 goto error;
955 data.set = set;
956 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
957 umap->table.n);
958 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
959 fn, &data) < 0)
960 goto error;
962 isl_union_map_free(umap);
963 isl_set_free(set);
964 return data.res;
965 error:
966 isl_union_map_free(umap);
967 isl_set_free(set);
968 isl_union_map_free(data.res);
969 return NULL;
972 /* Intersect "umap" with the parameter domain "set".
974 * If "set" does not have any constraints, then we can return immediately.
976 __isl_give isl_union_map *isl_union_map_intersect_params(
977 __isl_take isl_union_map *umap, __isl_take isl_set *set)
979 int is_universe;
981 is_universe = isl_set_plain_is_universe(set);
982 if (is_universe < 0)
983 goto error;
984 if (is_universe) {
985 isl_set_free(set);
986 return umap;
989 return gen_bin_set_op(umap, set, &intersect_params_entry);
990 error:
991 isl_union_map_free(umap);
992 isl_set_free(set);
993 return NULL;
996 __isl_give isl_union_set *isl_union_set_intersect_params(
997 __isl_take isl_union_set *uset, __isl_take isl_set *set)
999 return isl_union_map_intersect_params(uset, set);
1002 static __isl_give isl_union_map *union_map_intersect_params(
1003 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1005 return isl_union_map_intersect_params(umap,
1006 isl_set_from_union_set(uset));
1009 static __isl_give isl_union_map *union_map_gist_params(
1010 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1012 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1015 struct isl_union_map_match_bin_data {
1016 isl_union_map *umap2;
1017 isl_union_map *res;
1018 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1021 static isl_stat match_bin_entry(void **entry, void *user)
1023 struct isl_union_map_match_bin_data *data = user;
1024 uint32_t hash;
1025 struct isl_hash_table_entry *entry2;
1026 isl_map *map = *entry;
1027 int empty;
1029 hash = isl_space_get_hash(map->dim);
1030 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1031 hash, &has_space, map->dim, 0);
1032 if (!entry2)
1033 return isl_stat_ok;
1035 map = isl_map_copy(map);
1036 map = data->fn(map, isl_map_copy(entry2->data));
1038 empty = isl_map_is_empty(map);
1039 if (empty < 0) {
1040 isl_map_free(map);
1041 return isl_stat_error;
1043 if (empty) {
1044 isl_map_free(map);
1045 return isl_stat_ok;
1048 data->res = isl_union_map_add_map(data->res, map);
1050 return isl_stat_ok;
1053 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1054 __isl_take isl_union_map *umap2,
1055 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1057 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1059 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1060 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1062 if (!umap1 || !umap2)
1063 goto error;
1065 data.umap2 = umap2;
1066 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1067 umap1->table.n);
1068 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1069 &match_bin_entry, &data) < 0)
1070 goto error;
1072 isl_union_map_free(umap1);
1073 isl_union_map_free(umap2);
1074 return data.res;
1075 error:
1076 isl_union_map_free(umap1);
1077 isl_union_map_free(umap2);
1078 isl_union_map_free(data.res);
1079 return NULL;
1082 __isl_give isl_union_map *isl_union_map_intersect(
1083 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1085 return match_bin_op(umap1, umap2, &isl_map_intersect);
1088 /* Compute the intersection of the two union_sets.
1089 * As a special case, if exactly one of the two union_sets
1090 * is a parameter domain, then intersect the parameter domain
1091 * of the other one with this set.
1093 __isl_give isl_union_set *isl_union_set_intersect(
1094 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1096 int p1, p2;
1098 p1 = isl_union_set_is_params(uset1);
1099 p2 = isl_union_set_is_params(uset2);
1100 if (p1 < 0 || p2 < 0)
1101 goto error;
1102 if (!p1 && p2)
1103 return union_map_intersect_params(uset1, uset2);
1104 if (p1 && !p2)
1105 return union_map_intersect_params(uset2, uset1);
1106 return isl_union_map_intersect(uset1, uset2);
1107 error:
1108 isl_union_set_free(uset1);
1109 isl_union_set_free(uset2);
1110 return NULL;
1113 static isl_stat gist_params_entry(void **entry, void *user)
1115 struct isl_union_map_gen_bin_set_data *data = user;
1116 isl_map *map = *entry;
1117 int empty;
1119 map = isl_map_copy(map);
1120 map = isl_map_gist_params(map, isl_set_copy(data->set));
1122 empty = isl_map_is_empty(map);
1123 if (empty < 0) {
1124 isl_map_free(map);
1125 return isl_stat_error;
1128 data->res = isl_union_map_add_map(data->res, map);
1130 return isl_stat_ok;
1133 __isl_give isl_union_map *isl_union_map_gist_params(
1134 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1136 return gen_bin_set_op(umap, set, &gist_params_entry);
1139 __isl_give isl_union_set *isl_union_set_gist_params(
1140 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1142 return isl_union_map_gist_params(uset, set);
1145 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1146 __isl_take isl_union_map *context)
1148 return match_bin_op(umap, context, &isl_map_gist);
1151 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1152 __isl_take isl_union_set *context)
1154 if (isl_union_set_is_params(context))
1155 return union_map_gist_params(uset, context);
1156 return isl_union_map_gist(uset, context);
1159 /* For each map in "umap", remove the constraints in the corresponding map
1160 * of "context".
1161 * Each map in "context" is assumed to consist of a single disjunct and
1162 * to have explicit representations for all local variables.
1164 __isl_give isl_union_map *isl_union_map_plain_gist(
1165 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1167 return match_bin_op(umap, context, &isl_map_plain_gist);
1170 /* For each set in "uset", remove the constraints in the corresponding set
1171 * of "context".
1172 * Each set in "context" is assumed to consist of a single disjunct and
1173 * to have explicit representations for all local variables.
1175 __isl_give isl_union_set *isl_union_set_plain_gist(
1176 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1178 return isl_union_map_plain_gist(uset, context);
1181 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1182 __isl_take isl_map *set2)
1184 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1187 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1188 __isl_take isl_map *set2)
1190 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1193 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1194 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1196 return match_bin_op(uset1, uset2, &lex_lt_set);
1199 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1200 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1202 return match_bin_op(uset1, uset2, &lex_le_set);
1205 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1206 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1208 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1211 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1212 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1214 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1217 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1218 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1220 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1223 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1224 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1226 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1229 /* Intersect the domain of "umap" with "uset".
1231 static __isl_give isl_union_map *union_map_intersect_domain(
1232 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1234 struct isl_bin_op_control control = {
1235 .match_space = &isl_space_domain,
1236 .fn_map = &isl_map_intersect_domain,
1239 return gen_bin_op(umap, uset, &control);
1242 /* Intersect the domain of "umap" with "uset".
1243 * If "uset" is a parameters domain, then intersect the parameter
1244 * domain of "umap" with this set.
1246 __isl_give isl_union_map *isl_union_map_intersect_domain(
1247 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1249 if (isl_union_set_is_params(uset))
1250 return union_map_intersect_params(umap, uset);
1251 else
1252 return union_map_intersect_domain(umap, uset);
1255 /* Remove the elements of "uset" from the domain of "umap".
1257 __isl_give isl_union_map *isl_union_map_subtract_domain(
1258 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1260 struct isl_bin_op_control control = {
1261 .subtract = 1,
1262 .match_space = &isl_space_domain,
1263 .fn_map = &isl_map_subtract_domain,
1266 return gen_bin_op(umap, dom, &control);
1269 /* Remove the elements of "uset" from the range of "umap".
1271 __isl_give isl_union_map *isl_union_map_subtract_range(
1272 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1274 struct isl_bin_op_control control = {
1275 .subtract = 1,
1276 .match_space = &isl_space_range,
1277 .fn_map = &isl_map_subtract_range,
1280 return gen_bin_op(umap, dom, &control);
1283 /* Compute the gist of "umap" with respect to the domain "uset".
1285 static __isl_give isl_union_map *union_map_gist_domain(
1286 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1288 struct isl_bin_op_control control = {
1289 .match_space = &isl_space_domain,
1290 .fn_map = &isl_map_gist_domain,
1293 return gen_bin_op(umap, uset, &control);
1296 /* Compute the gist of "umap" with respect to the domain "uset".
1297 * If "uset" is a parameters domain, then compute the gist
1298 * with respect to this parameter domain.
1300 __isl_give isl_union_map *isl_union_map_gist_domain(
1301 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1303 if (isl_union_set_is_params(uset))
1304 return union_map_gist_params(umap, uset);
1305 else
1306 return union_map_gist_domain(umap, uset);
1309 /* Compute the gist of "umap" with respect to the range "uset".
1311 __isl_give isl_union_map *isl_union_map_gist_range(
1312 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1314 struct isl_bin_op_control control = {
1315 .match_space = &isl_space_range,
1316 .fn_map = &isl_map_gist_range,
1319 return gen_bin_op(umap, uset, &control);
1322 __isl_give isl_union_map *isl_union_map_intersect_range(
1323 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1325 struct isl_bin_op_control control = {
1326 .match_space = &isl_space_range,
1327 .fn_map = &isl_map_intersect_range,
1330 return gen_bin_op(umap, uset, &control);
1333 /* Intersect each map in "umap" in a space A -> [B -> C]
1334 * with the corresponding map in "factor" in the space A -> C and
1335 * collect the results.
1337 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1338 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1340 struct isl_bin_op_control control = {
1341 .filter = &isl_map_range_is_wrapping,
1342 .match_space = &isl_space_range_factor_range,
1343 .fn_map = &isl_map_intersect_range_factor_range,
1346 return gen_bin_op(umap, factor, &control);
1349 struct isl_union_map_bin_data {
1350 isl_union_map *umap2;
1351 isl_union_map *res;
1352 isl_map *map;
1353 isl_stat (*fn)(void **entry, void *user);
1356 static isl_stat apply_range_entry(void **entry, void *user)
1358 struct isl_union_map_bin_data *data = user;
1359 isl_map *map2 = *entry;
1360 isl_bool empty;
1362 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1363 map2->dim, isl_dim_in))
1364 return isl_stat_ok;
1366 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1368 empty = isl_map_is_empty(map2);
1369 if (empty < 0) {
1370 isl_map_free(map2);
1371 return isl_stat_error;
1373 if (empty) {
1374 isl_map_free(map2);
1375 return isl_stat_ok;
1378 data->res = isl_union_map_add_map(data->res, map2);
1380 return isl_stat_ok;
1383 static isl_stat bin_entry(void **entry, void *user)
1385 struct isl_union_map_bin_data *data = user;
1386 isl_map *map = *entry;
1388 data->map = map;
1389 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1390 data->fn, data) < 0)
1391 return isl_stat_error;
1393 return isl_stat_ok;
1396 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1397 __isl_take isl_union_map *umap2,
1398 isl_stat (*fn)(void **entry, void *user))
1400 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1402 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1403 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1405 if (!umap1 || !umap2)
1406 goto error;
1408 data.umap2 = umap2;
1409 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1410 umap1->table.n);
1411 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1412 &bin_entry, &data) < 0)
1413 goto error;
1415 isl_union_map_free(umap1);
1416 isl_union_map_free(umap2);
1417 return data.res;
1418 error:
1419 isl_union_map_free(umap1);
1420 isl_union_map_free(umap2);
1421 isl_union_map_free(data.res);
1422 return NULL;
1425 __isl_give isl_union_map *isl_union_map_apply_range(
1426 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1428 return bin_op(umap1, umap2, &apply_range_entry);
1431 __isl_give isl_union_map *isl_union_map_apply_domain(
1432 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1434 umap1 = isl_union_map_reverse(umap1);
1435 umap1 = isl_union_map_apply_range(umap1, umap2);
1436 return isl_union_map_reverse(umap1);
1439 __isl_give isl_union_set *isl_union_set_apply(
1440 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1442 return isl_union_map_apply_range(uset, umap);
1445 static isl_stat map_lex_lt_entry(void **entry, void *user)
1447 struct isl_union_map_bin_data *data = user;
1448 isl_map *map2 = *entry;
1450 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1451 map2->dim, isl_dim_out))
1452 return isl_stat_ok;
1454 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1456 data->res = isl_union_map_add_map(data->res, map2);
1458 return isl_stat_ok;
1461 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1462 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1464 return bin_op(umap1, umap2, &map_lex_lt_entry);
1467 static isl_stat map_lex_le_entry(void **entry, void *user)
1469 struct isl_union_map_bin_data *data = user;
1470 isl_map *map2 = *entry;
1472 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1473 map2->dim, isl_dim_out))
1474 return isl_stat_ok;
1476 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1478 data->res = isl_union_map_add_map(data->res, map2);
1480 return isl_stat_ok;
1483 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1484 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1486 return bin_op(umap1, umap2, &map_lex_le_entry);
1489 static isl_stat product_entry(void **entry, void *user)
1491 struct isl_union_map_bin_data *data = user;
1492 isl_map *map2 = *entry;
1494 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1496 data->res = isl_union_map_add_map(data->res, map2);
1498 return isl_stat_ok;
1501 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1502 __isl_take isl_union_map *umap2)
1504 return bin_op(umap1, umap2, &product_entry);
1507 static isl_stat set_product_entry(void **entry, void *user)
1509 struct isl_union_map_bin_data *data = user;
1510 isl_set *set2 = *entry;
1512 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1514 data->res = isl_union_set_add_set(data->res, set2);
1516 return isl_stat_ok;
1519 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1520 __isl_take isl_union_set *uset2)
1522 return bin_op(uset1, uset2, &set_product_entry);
1525 static isl_stat domain_product_entry(void **entry, void *user)
1527 struct isl_union_map_bin_data *data = user;
1528 isl_map *map2 = *entry;
1530 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1531 map2->dim, isl_dim_out))
1532 return isl_stat_ok;
1534 map2 = isl_map_domain_product(isl_map_copy(data->map),
1535 isl_map_copy(map2));
1537 data->res = isl_union_map_add_map(data->res, map2);
1539 return isl_stat_ok;
1542 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1544 __isl_give isl_union_map *isl_union_map_domain_product(
1545 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1547 return bin_op(umap1, umap2, &domain_product_entry);
1550 static isl_stat range_product_entry(void **entry, void *user)
1552 struct isl_union_map_bin_data *data = user;
1553 isl_map *map2 = *entry;
1555 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1556 map2->dim, isl_dim_in))
1557 return isl_stat_ok;
1559 map2 = isl_map_range_product(isl_map_copy(data->map),
1560 isl_map_copy(map2));
1562 data->res = isl_union_map_add_map(data->res, map2);
1564 return isl_stat_ok;
1567 __isl_give isl_union_map *isl_union_map_range_product(
1568 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1570 return bin_op(umap1, umap2, &range_product_entry);
1573 /* If data->map A -> B and "map2" C -> D have the same range space,
1574 * then add (A, C) -> (B * D) to data->res.
1576 static isl_stat flat_domain_product_entry(void **entry, void *user)
1578 struct isl_union_map_bin_data *data = user;
1579 isl_map *map2 = *entry;
1581 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1582 map2->dim, isl_dim_out))
1583 return isl_stat_ok;
1585 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1586 isl_map_copy(map2));
1588 data->res = isl_union_map_add_map(data->res, map2);
1590 return isl_stat_ok;
1593 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1595 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1596 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1598 return bin_op(umap1, umap2, &flat_domain_product_entry);
1601 static isl_stat flat_range_product_entry(void **entry, void *user)
1603 struct isl_union_map_bin_data *data = user;
1604 isl_map *map2 = *entry;
1606 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1607 map2->dim, isl_dim_in))
1608 return isl_stat_ok;
1610 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1611 isl_map_copy(map2));
1613 data->res = isl_union_map_add_map(data->res, map2);
1615 return isl_stat_ok;
1618 __isl_give isl_union_map *isl_union_map_flat_range_product(
1619 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1621 return bin_op(umap1, umap2, &flat_range_product_entry);
1624 /* Data structure that specifies how un_op should modify
1625 * the maps in the union map.
1627 * If "inplace" is set, then the maps in the input union map
1628 * are modified in place. This means that "fn_map" should not
1629 * change the meaning of the map or that the union map only
1630 * has a single reference.
1631 * If "total" is set, then all maps need to be modified and
1632 * the results need to live in the same space.
1633 * Otherwise, a new union map is constructed to store the results.
1634 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1635 * are taken into account. "filter_user" is passed as the second argument
1636 * to "filter". No filter can be set if "inplace" or
1637 * "total" is set.
1638 * "fn_map" specifies how the maps (selected by "filter")
1639 * should be transformed.
1641 struct isl_un_op_control {
1642 int inplace;
1643 int total;
1644 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1645 void *filter_user;
1646 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1649 /* Data structure for wrapping the data for un_op_filter_drop_user.
1650 * "filter" is the function that is being wrapped.
1652 struct isl_un_op_drop_user_data {
1653 isl_bool (*filter)(__isl_keep isl_map *map);
1656 /* Wrapper for isl_un_op_control filters that do not require
1657 * a second argument.
1658 * Simply call data->filter without the second argument.
1660 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1662 struct isl_un_op_drop_user_data *data = user;
1663 return data->filter(map);
1666 /* Internal data structure for "un_op".
1667 * "control" specifies how the maps in the union map should be modified.
1668 * "res" collects the results.
1670 struct isl_union_map_un_data {
1671 struct isl_un_op_control *control;
1672 isl_union_map *res;
1675 /* isl_hash_table_foreach callback for un_op.
1676 * Handle the map that "entry" points to.
1678 * If control->filter is set, then check if this map satisfies the filter.
1679 * If so (or if control->filter is not set), modify the map
1680 * by calling control->fn_map and either add the result to data->res or
1681 * replace the original entry by the result (if control->inplace is set).
1683 static isl_stat un_entry(void **entry, void *user)
1685 struct isl_union_map_un_data *data = user;
1686 isl_map *map = *entry;
1688 if (data->control->filter) {
1689 isl_bool ok;
1691 ok = data->control->filter(map, data->control->filter_user);
1692 if (ok < 0)
1693 return isl_stat_error;
1694 if (!ok)
1695 return isl_stat_ok;
1698 map = data->control->fn_map(isl_map_copy(map));
1699 if (!map)
1700 return isl_stat_error;
1701 if (data->control->inplace) {
1702 isl_map_free(*entry);
1703 *entry = map;
1704 } else {
1705 data->res = isl_union_map_add_map(data->res, map);
1706 if (!data->res)
1707 return isl_stat_error;
1710 return isl_stat_ok;
1713 /* Modify the maps in "umap" based on "control".
1714 * If control->inplace is set, then modify the maps in "umap" in-place.
1715 * Otherwise, create a new union map to hold the results.
1716 * If control->total is set, then perform an inplace computation
1717 * if "umap" is only referenced once. Otherwise, create a new union map
1718 * to store the results.
1720 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1721 struct isl_un_op_control *control)
1723 struct isl_union_map_un_data data = { control };
1725 if (!umap)
1726 return NULL;
1727 if ((control->inplace || control->total) && control->filter)
1728 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1729 "inplace/total modification cannot be filtered",
1730 return isl_union_map_free(umap));
1732 if (control->total && umap->ref == 1)
1733 control->inplace = 1;
1734 if (control->inplace) {
1735 data.res = umap;
1736 } else {
1737 isl_space *space;
1739 space = isl_union_map_get_space(umap);
1740 data.res = isl_union_map_alloc(space, umap->table.n);
1742 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1743 &umap->table, &un_entry, &data) < 0)
1744 data.res = isl_union_map_free(data.res);
1746 if (control->inplace)
1747 return data.res;
1748 isl_union_map_free(umap);
1749 return data.res;
1752 __isl_give isl_union_map *isl_union_map_from_range(
1753 __isl_take isl_union_set *uset)
1755 struct isl_un_op_control control = {
1756 .fn_map = &isl_map_from_range,
1758 return un_op(uset, &control);
1761 __isl_give isl_union_map *isl_union_map_from_domain(
1762 __isl_take isl_union_set *uset)
1764 return isl_union_map_reverse(isl_union_map_from_range(uset));
1767 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1768 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1770 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1771 isl_union_map_from_range(range));
1774 /* Modify the maps in "umap" by applying "fn" on them.
1775 * "fn" should apply to all maps in "umap" and should not modify the space.
1777 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1778 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1780 struct isl_un_op_control control = {
1781 .total = 1,
1782 .fn_map = fn,
1785 return un_op(umap, &control);
1788 /* Compute the affine hull of "map" and return the result as an isl_map.
1790 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1792 return isl_map_from_basic_map(isl_map_affine_hull(map));
1795 __isl_give isl_union_map *isl_union_map_affine_hull(
1796 __isl_take isl_union_map *umap)
1798 return total(umap, &isl_map_affine_hull_map);
1801 __isl_give isl_union_set *isl_union_set_affine_hull(
1802 __isl_take isl_union_set *uset)
1804 return isl_union_map_affine_hull(uset);
1807 /* Wrapper around isl_set_combined_lineality_space
1808 * that returns the combined lineality space in the form of an isl_set
1809 * instead of an isl_basic_set.
1811 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1813 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1816 /* For each set in "uset", compute the (linear) hull
1817 * of the lineality spaces of its basic sets and
1818 * collect and return the results.
1820 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1821 __isl_take isl_union_set *uset)
1823 struct isl_un_op_control control = {
1824 .fn_map = &combined_lineality_space,
1826 return un_op(uset, &control);
1829 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1831 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1833 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1836 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1837 __isl_take isl_union_map *umap)
1839 return total(umap, &isl_map_polyhedral_hull_map);
1842 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1843 __isl_take isl_union_set *uset)
1845 return isl_union_map_polyhedral_hull(uset);
1848 /* Compute a superset of the convex hull of "map" that is described
1849 * by only translates of the constraints in the constituents of "map" and
1850 * return the result as an isl_map.
1852 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1854 return isl_map_from_basic_map(isl_map_simple_hull(map));
1857 __isl_give isl_union_map *isl_union_map_simple_hull(
1858 __isl_take isl_union_map *umap)
1860 return total(umap, &isl_map_simple_hull_map);
1863 __isl_give isl_union_set *isl_union_set_simple_hull(
1864 __isl_take isl_union_set *uset)
1866 return isl_union_map_simple_hull(uset);
1869 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1870 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1872 struct isl_un_op_control control = {
1873 .inplace = 1,
1874 .fn_map = fn,
1877 return un_op(umap, &control);
1880 /* Remove redundant constraints in each of the basic maps of "umap".
1881 * Since removing redundant constraints does not change the meaning
1882 * or the space, the operation can be performed in-place.
1884 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1885 __isl_take isl_union_map *umap)
1887 return inplace(umap, &isl_map_remove_redundancies);
1890 /* Remove redundant constraints in each of the basic sets of "uset".
1892 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1893 __isl_take isl_union_set *uset)
1895 return isl_union_map_remove_redundancies(uset);
1898 __isl_give isl_union_map *isl_union_map_coalesce(
1899 __isl_take isl_union_map *umap)
1901 return inplace(umap, &isl_map_coalesce);
1904 __isl_give isl_union_set *isl_union_set_coalesce(
1905 __isl_take isl_union_set *uset)
1907 return isl_union_map_coalesce(uset);
1910 __isl_give isl_union_map *isl_union_map_detect_equalities(
1911 __isl_take isl_union_map *umap)
1913 return inplace(umap, &isl_map_detect_equalities);
1916 __isl_give isl_union_set *isl_union_set_detect_equalities(
1917 __isl_take isl_union_set *uset)
1919 return isl_union_map_detect_equalities(uset);
1922 __isl_give isl_union_map *isl_union_map_compute_divs(
1923 __isl_take isl_union_map *umap)
1925 return inplace(umap, &isl_map_compute_divs);
1928 __isl_give isl_union_set *isl_union_set_compute_divs(
1929 __isl_take isl_union_set *uset)
1931 return isl_union_map_compute_divs(uset);
1934 __isl_give isl_union_map *isl_union_map_lexmin(
1935 __isl_take isl_union_map *umap)
1937 return total(umap, &isl_map_lexmin);
1940 __isl_give isl_union_set *isl_union_set_lexmin(
1941 __isl_take isl_union_set *uset)
1943 return isl_union_map_lexmin(uset);
1946 __isl_give isl_union_map *isl_union_map_lexmax(
1947 __isl_take isl_union_map *umap)
1949 return total(umap, &isl_map_lexmax);
1952 __isl_give isl_union_set *isl_union_set_lexmax(
1953 __isl_take isl_union_set *uset)
1955 return isl_union_map_lexmax(uset);
1958 /* Return the universe in the space of "map".
1960 static __isl_give isl_map *universe(__isl_take isl_map *map)
1962 isl_space *space;
1964 space = isl_map_get_space(map);
1965 isl_map_free(map);
1966 return isl_map_universe(space);
1969 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1971 struct isl_un_op_control control = {
1972 .fn_map = &universe,
1974 return un_op(umap, &control);
1977 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1979 return isl_union_map_universe(uset);
1982 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1984 struct isl_un_op_control control = {
1985 .fn_map = &isl_map_reverse,
1987 return un_op(umap, &control);
1990 /* Compute the parameter domain of the given union map.
1992 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1994 struct isl_un_op_control control = {
1995 .fn_map = &isl_map_params,
1997 int empty;
1999 empty = isl_union_map_is_empty(umap);
2000 if (empty < 0)
2001 goto error;
2002 if (empty) {
2003 isl_space *space;
2004 space = isl_union_map_get_space(umap);
2005 isl_union_map_free(umap);
2006 return isl_set_empty(space);
2008 return isl_set_from_union_set(un_op(umap, &control));
2009 error:
2010 isl_union_map_free(umap);
2011 return NULL;
2014 /* Compute the parameter domain of the given union set.
2016 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2018 return isl_union_map_params(uset);
2021 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2023 struct isl_un_op_control control = {
2024 .fn_map = &isl_map_domain,
2026 return un_op(umap, &control);
2029 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2031 struct isl_un_op_control control = {
2032 .fn_map = &isl_map_range,
2034 return un_op(umap, &control);
2037 __isl_give isl_union_map *isl_union_map_domain_map(
2038 __isl_take isl_union_map *umap)
2040 struct isl_un_op_control control = {
2041 .fn_map = &isl_map_domain_map,
2043 return un_op(umap, &control);
2046 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2047 * add the result to "res".
2049 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2051 isl_union_pw_multi_aff **res = user;
2052 isl_multi_aff *ma;
2053 isl_pw_multi_aff *pma;
2055 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2056 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2057 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2059 return *res ? isl_stat_ok : isl_stat_error;
2063 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2064 * to its domain.
2066 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2067 __isl_take isl_union_map *umap)
2069 isl_union_pw_multi_aff *res;
2071 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2072 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2073 res = isl_union_pw_multi_aff_free(res);
2075 isl_union_map_free(umap);
2076 return res;
2079 __isl_give isl_union_map *isl_union_map_range_map(
2080 __isl_take isl_union_map *umap)
2082 struct isl_un_op_control control = {
2083 .fn_map = &isl_map_range_map,
2085 return un_op(umap, &control);
2088 /* Given a collection of wrapped maps of the form A[B -> C],
2089 * return the collection of maps A[B -> C] -> B.
2091 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2092 __isl_take isl_union_set *uset)
2094 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2095 struct isl_un_op_control control = {
2096 .filter = &un_op_filter_drop_user,
2097 .filter_user = &data,
2098 .fn_map = &isl_set_wrapped_domain_map,
2100 return un_op(uset, &control);
2103 /* Does "map" relate elements from the same space?
2105 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2107 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2108 map->dim, isl_dim_out);
2111 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2113 struct isl_un_op_control control = {
2114 .filter = &equal_tuples,
2115 .fn_map = &isl_map_deltas,
2117 return un_op(umap, &control);
2120 __isl_give isl_union_map *isl_union_map_deltas_map(
2121 __isl_take isl_union_map *umap)
2123 struct isl_un_op_control control = {
2124 .filter = &equal_tuples,
2125 .fn_map = &isl_map_deltas_map,
2127 return un_op(umap, &control);
2130 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2132 struct isl_un_op_control control = {
2133 .fn_map = &isl_set_identity,
2135 return un_op(uset, &control);
2138 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2140 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2142 isl_union_pw_multi_aff **res = user;
2143 isl_space *space;
2144 isl_pw_multi_aff *pma;
2146 space = isl_space_map_from_set(isl_set_get_space(set));
2147 pma = isl_pw_multi_aff_identity(space);
2148 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2149 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2151 return *res ? isl_stat_ok : isl_stat_error;
2154 /* Return an identity function on "uset" in the form
2155 * of an isl_union_pw_multi_aff.
2157 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2158 __isl_take isl_union_set *uset)
2160 isl_union_pw_multi_aff *res;
2162 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2163 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2164 res = isl_union_pw_multi_aff_free(res);
2166 isl_union_set_free(uset);
2167 return res;
2170 /* For each map in "umap" of the form [A -> B] -> C,
2171 * construct the map A -> C and collect the results.
2173 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2174 __isl_take isl_union_map *umap)
2176 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2177 struct isl_un_op_control control = {
2178 .filter = &un_op_filter_drop_user,
2179 .filter_user = &data,
2180 .fn_map = &isl_map_domain_factor_domain,
2182 return un_op(umap, &control);
2185 /* For each map in "umap" of the form [A -> B] -> C,
2186 * construct the map B -> C and collect the results.
2188 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2189 __isl_take isl_union_map *umap)
2191 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2192 struct isl_un_op_control control = {
2193 .filter = &un_op_filter_drop_user,
2194 .filter_user = &data,
2195 .fn_map = &isl_map_domain_factor_range,
2197 return un_op(umap, &control);
2200 /* For each map in "umap" of the form A -> [B -> C],
2201 * construct the map A -> B and collect the results.
2203 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2204 __isl_take isl_union_map *umap)
2206 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2207 struct isl_un_op_control control = {
2208 .filter = &un_op_filter_drop_user,
2209 .filter_user = &data,
2210 .fn_map = &isl_map_range_factor_domain,
2212 return un_op(umap, &control);
2215 /* For each map in "umap" of the form A -> [B -> C],
2216 * construct the map A -> C and collect the results.
2218 __isl_give isl_union_map *isl_union_map_range_factor_range(
2219 __isl_take isl_union_map *umap)
2221 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2222 struct isl_un_op_control control = {
2223 .filter = &un_op_filter_drop_user,
2224 .filter_user = &data,
2225 .fn_map = &isl_map_range_factor_range,
2227 return un_op(umap, &control);
2230 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2231 * construct the map A -> C and collect the results.
2233 __isl_give isl_union_map *isl_union_map_factor_domain(
2234 __isl_take isl_union_map *umap)
2236 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2237 struct isl_un_op_control control = {
2238 .filter = &un_op_filter_drop_user,
2239 .filter_user = &data,
2240 .fn_map = &isl_map_factor_domain,
2242 return un_op(umap, &control);
2245 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2246 * construct the map B -> D and collect the results.
2248 __isl_give isl_union_map *isl_union_map_factor_range(
2249 __isl_take isl_union_map *umap)
2251 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2252 struct isl_un_op_control control = {
2253 .filter = &un_op_filter_drop_user,
2254 .filter_user = &data,
2255 .fn_map = &isl_map_factor_range,
2257 return un_op(umap, &control);
2260 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2262 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2263 struct isl_un_op_control control = {
2264 .filter = &un_op_filter_drop_user,
2265 .filter_user = &data,
2266 .fn_map = &isl_set_unwrap,
2268 return un_op(uset, &control);
2271 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2273 struct isl_un_op_control control = {
2274 .fn_map = &isl_map_wrap,
2276 return un_op(umap, &control);
2279 struct isl_union_map_is_subset_data {
2280 isl_union_map *umap2;
2281 isl_bool is_subset;
2284 static isl_stat is_subset_entry(void **entry, void *user)
2286 struct isl_union_map_is_subset_data *data = user;
2287 uint32_t hash;
2288 struct isl_hash_table_entry *entry2;
2289 isl_map *map = *entry;
2291 hash = isl_space_get_hash(map->dim);
2292 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2293 hash, &has_space, map->dim, 0);
2294 if (!entry2) {
2295 int empty = isl_map_is_empty(map);
2296 if (empty < 0)
2297 return isl_stat_error;
2298 if (empty)
2299 return isl_stat_ok;
2300 data->is_subset = isl_bool_false;
2301 return isl_stat_error;
2304 data->is_subset = isl_map_is_subset(map, entry2->data);
2305 if (data->is_subset < 0 || !data->is_subset)
2306 return isl_stat_error;
2308 return isl_stat_ok;
2311 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2312 __isl_keep isl_union_map *umap2)
2314 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2316 umap1 = isl_union_map_copy(umap1);
2317 umap2 = isl_union_map_copy(umap2);
2318 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2319 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2321 if (!umap1 || !umap2)
2322 goto error;
2324 data.umap2 = umap2;
2325 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2326 &is_subset_entry, &data) < 0 &&
2327 data.is_subset)
2328 goto error;
2330 isl_union_map_free(umap1);
2331 isl_union_map_free(umap2);
2333 return data.is_subset;
2334 error:
2335 isl_union_map_free(umap1);
2336 isl_union_map_free(umap2);
2337 return isl_bool_error;
2340 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2341 __isl_keep isl_union_set *uset2)
2343 return isl_union_map_is_subset(uset1, uset2);
2346 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2347 __isl_keep isl_union_map *umap2)
2349 isl_bool is_subset;
2351 if (!umap1 || !umap2)
2352 return isl_bool_error;
2353 is_subset = isl_union_map_is_subset(umap1, umap2);
2354 if (is_subset != isl_bool_true)
2355 return is_subset;
2356 is_subset = isl_union_map_is_subset(umap2, umap1);
2357 return is_subset;
2360 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2361 __isl_keep isl_union_set *uset2)
2363 return isl_union_map_is_equal(uset1, uset2);
2366 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2367 __isl_keep isl_union_map *umap2)
2369 isl_bool is_subset;
2371 if (!umap1 || !umap2)
2372 return isl_bool_error;
2373 is_subset = isl_union_map_is_subset(umap1, umap2);
2374 if (is_subset != isl_bool_true)
2375 return is_subset;
2376 is_subset = isl_union_map_is_subset(umap2, umap1);
2377 return isl_bool_not(is_subset);
2380 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2381 __isl_keep isl_union_set *uset2)
2383 return isl_union_map_is_strict_subset(uset1, uset2);
2386 /* Internal data structure for isl_union_map_is_disjoint.
2387 * umap2 is the union map with which we are comparing.
2388 * is_disjoint is initialized to 1 and is set to 0 as soon
2389 * as the union maps turn out not to be disjoint.
2391 struct isl_union_map_is_disjoint_data {
2392 isl_union_map *umap2;
2393 isl_bool is_disjoint;
2396 /* Check if "map" is disjoint from data->umap2 and abort
2397 * the search if it is not.
2399 static isl_stat is_disjoint_entry(void **entry, void *user)
2401 struct isl_union_map_is_disjoint_data *data = user;
2402 uint32_t hash;
2403 struct isl_hash_table_entry *entry2;
2404 isl_map *map = *entry;
2406 hash = isl_space_get_hash(map->dim);
2407 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2408 hash, &has_space, map->dim, 0);
2409 if (!entry2)
2410 return isl_stat_ok;
2412 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2413 if (data->is_disjoint < 0 || !data->is_disjoint)
2414 return isl_stat_error;
2416 return isl_stat_ok;
2419 /* Are "umap1" and "umap2" disjoint?
2421 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2422 __isl_keep isl_union_map *umap2)
2424 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2426 umap1 = isl_union_map_copy(umap1);
2427 umap2 = isl_union_map_copy(umap2);
2428 umap1 = isl_union_map_align_params(umap1,
2429 isl_union_map_get_space(umap2));
2430 umap2 = isl_union_map_align_params(umap2,
2431 isl_union_map_get_space(umap1));
2433 if (!umap1 || !umap2)
2434 goto error;
2436 data.umap2 = umap2;
2437 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2438 &is_disjoint_entry, &data) < 0 &&
2439 data.is_disjoint)
2440 goto error;
2442 isl_union_map_free(umap1);
2443 isl_union_map_free(umap2);
2445 return data.is_disjoint;
2446 error:
2447 isl_union_map_free(umap1);
2448 isl_union_map_free(umap2);
2449 return isl_bool_error;
2452 /* Are "uset1" and "uset2" disjoint?
2454 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2455 __isl_keep isl_union_set *uset2)
2457 return isl_union_map_is_disjoint(uset1, uset2);
2460 static isl_stat sample_entry(void **entry, void *user)
2462 isl_basic_map **sample = (isl_basic_map **)user;
2463 isl_map *map = *entry;
2465 *sample = isl_map_sample(isl_map_copy(map));
2466 if (!*sample)
2467 return isl_stat_error;
2468 if (!isl_basic_map_plain_is_empty(*sample))
2469 return isl_stat_error;
2470 return isl_stat_ok;
2473 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2475 isl_basic_map *sample = NULL;
2477 if (!umap)
2478 return NULL;
2480 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2481 &sample_entry, &sample) < 0 &&
2482 !sample)
2483 goto error;
2485 if (!sample)
2486 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2488 isl_union_map_free(umap);
2490 return sample;
2491 error:
2492 isl_union_map_free(umap);
2493 return NULL;
2496 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2498 return bset_from_bmap(isl_union_map_sample(uset));
2501 /* Return an element in "uset" in the form of an isl_point.
2502 * Return a void isl_point if "uset" is empty.
2504 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2506 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2509 struct isl_forall_data {
2510 isl_bool res;
2511 isl_bool (*fn)(__isl_keep isl_map *map);
2514 static isl_stat forall_entry(void **entry, void *user)
2516 struct isl_forall_data *data = user;
2517 isl_map *map = *entry;
2519 data->res = data->fn(map);
2520 if (data->res < 0)
2521 return isl_stat_error;
2523 if (!data->res)
2524 return isl_stat_error;
2526 return isl_stat_ok;
2529 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2530 isl_bool (*fn)(__isl_keep isl_map *map))
2532 struct isl_forall_data data = { isl_bool_true, fn };
2534 if (!umap)
2535 return isl_bool_error;
2537 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2538 &forall_entry, &data) < 0 && data.res)
2539 return isl_bool_error;
2541 return data.res;
2544 struct isl_forall_user_data {
2545 isl_bool res;
2546 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2547 void *user;
2550 static isl_stat forall_user_entry(void **entry, void *user)
2552 struct isl_forall_user_data *data = user;
2553 isl_map *map = *entry;
2555 data->res = data->fn(map, data->user);
2556 if (data->res < 0)
2557 return isl_stat_error;
2559 if (!data->res)
2560 return isl_stat_error;
2562 return isl_stat_ok;
2565 /* Check if fn(map, user) returns true for all maps "map" in umap.
2567 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2568 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2570 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2572 if (!umap)
2573 return isl_bool_error;
2575 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2576 &forall_user_entry, &data) < 0 && data.res)
2577 return isl_bool_error;
2579 return data.res;
2582 /* Is "umap" obviously empty?
2584 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2586 if (!umap)
2587 return isl_bool_error;
2588 return isl_union_map_n_map(umap) == 0;
2591 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2593 return union_map_forall(umap, &isl_map_is_empty);
2596 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2598 return isl_union_map_is_empty(uset);
2601 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2603 isl_bool is_subset;
2604 isl_space *dim;
2605 isl_map *id;
2607 if (!map)
2608 return isl_bool_error;
2610 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2611 map->dim, isl_dim_out))
2612 return isl_bool_false;
2614 dim = isl_map_get_space(map);
2615 id = isl_map_identity(dim);
2617 is_subset = isl_map_is_subset(map, id);
2619 isl_map_free(id);
2621 return is_subset;
2624 /* Given an isl_union_map that consists of a single map, check
2625 * if it is single-valued.
2627 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2629 isl_map *map;
2630 isl_bool sv;
2632 umap = isl_union_map_copy(umap);
2633 map = isl_map_from_union_map(umap);
2634 sv = isl_map_is_single_valued(map);
2635 isl_map_free(map);
2637 return sv;
2640 /* Internal data structure for single_valued_on_domain.
2642 * "umap" is the union map to be tested.
2643 * "sv" is set to 1 as long as "umap" may still be single-valued.
2645 struct isl_union_map_is_sv_data {
2646 isl_union_map *umap;
2647 isl_bool sv;
2650 /* Check if the data->umap is single-valued on "set".
2652 * If data->umap consists of a single map on "set", then test it
2653 * as an isl_map.
2655 * Otherwise, compute
2657 * M \circ M^-1
2659 * check if the result is a subset of the identity mapping and
2660 * store the result in data->sv.
2662 * Terminate as soon as data->umap has been determined not to
2663 * be single-valued.
2665 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2667 struct isl_union_map_is_sv_data *data = user;
2668 isl_union_map *umap, *test;
2670 umap = isl_union_map_copy(data->umap);
2671 umap = isl_union_map_intersect_domain(umap,
2672 isl_union_set_from_set(set));
2674 if (isl_union_map_n_map(umap) == 1) {
2675 data->sv = single_map_is_single_valued(umap);
2676 isl_union_map_free(umap);
2677 } else {
2678 test = isl_union_map_reverse(isl_union_map_copy(umap));
2679 test = isl_union_map_apply_range(test, umap);
2681 data->sv = union_map_forall(test, &is_subset_of_identity);
2683 isl_union_map_free(test);
2686 if (data->sv < 0 || !data->sv)
2687 return isl_stat_error;
2688 return isl_stat_ok;
2691 /* Check if the given map is single-valued.
2693 * If the union map consists of a single map, then test it as an isl_map.
2694 * Otherwise, check if the union map is single-valued on each of its
2695 * domain spaces.
2697 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2699 isl_union_map *universe;
2700 isl_union_set *domain;
2701 struct isl_union_map_is_sv_data data;
2703 if (isl_union_map_n_map(umap) == 1)
2704 return single_map_is_single_valued(umap);
2706 universe = isl_union_map_universe(isl_union_map_copy(umap));
2707 domain = isl_union_map_domain(universe);
2709 data.sv = isl_bool_true;
2710 data.umap = umap;
2711 if (isl_union_set_foreach_set(domain,
2712 &single_valued_on_domain, &data) < 0 && data.sv)
2713 data.sv = isl_bool_error;
2714 isl_union_set_free(domain);
2716 return data.sv;
2719 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2721 isl_bool in;
2723 umap = isl_union_map_copy(umap);
2724 umap = isl_union_map_reverse(umap);
2725 in = isl_union_map_is_single_valued(umap);
2726 isl_union_map_free(umap);
2728 return in;
2731 /* Is "map" obviously not an identity relation because
2732 * it maps elements from one space to another space?
2733 * Update *non_identity accordingly.
2735 * In particular, if the domain and range spaces are the same,
2736 * then the map is not considered to obviously not be an identity relation.
2737 * Otherwise, the map is considered to obviously not be an identity relation
2738 * if it is is non-empty.
2740 * If "map" is determined to obviously not be an identity relation,
2741 * then the search is aborted.
2743 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2745 isl_bool *non_identity = user;
2746 isl_bool equal;
2747 isl_space *space;
2749 space = isl_map_get_space(map);
2750 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2751 if (equal >= 0 && !equal)
2752 *non_identity = isl_bool_not(isl_map_is_empty(map));
2753 else
2754 *non_identity = isl_bool_not(equal);
2755 isl_space_free(space);
2756 isl_map_free(map);
2758 if (*non_identity < 0 || *non_identity)
2759 return isl_stat_error;
2761 return isl_stat_ok;
2764 /* Is "umap" obviously not an identity relation because
2765 * it maps elements from one space to another space?
2767 * As soon as a map has been found that maps elements to a different space,
2768 * non_identity is changed and the search is aborted.
2770 static isl_bool isl_union_map_plain_is_not_identity(
2771 __isl_keep isl_union_map *umap)
2773 isl_bool non_identity;
2775 non_identity = isl_bool_false;
2776 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2777 &non_identity) < 0 &&
2778 non_identity == isl_bool_false)
2779 return isl_bool_error;
2781 return non_identity;
2784 /* Does "map" only map elements to themselves?
2785 * Update *identity accordingly.
2787 * If "map" is determined not to be an identity relation,
2788 * then the search is aborted.
2790 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2792 isl_bool *identity = user;
2794 *identity = isl_map_is_identity(map);
2795 isl_map_free(map);
2797 if (*identity < 0 || !*identity)
2798 return isl_stat_error;
2800 return isl_stat_ok;
2803 /* Does "umap" only map elements to themselves?
2805 * First check if there are any maps that map elements to different spaces.
2806 * If not, then check that all the maps (between identical spaces)
2807 * are identity relations.
2809 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2811 isl_bool non_identity;
2812 isl_bool identity;
2814 non_identity = isl_union_map_plain_is_not_identity(umap);
2815 if (non_identity < 0 || non_identity)
2816 return isl_bool_not(non_identity);
2818 identity = isl_bool_true;
2819 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2820 identity == isl_bool_true)
2821 return isl_bool_error;
2823 return identity;
2826 /* Represents a map that has a fixed value (v) for one of its
2827 * range dimensions.
2828 * The map in this structure is not reference counted, so it
2829 * is only valid while the isl_union_map from which it was
2830 * obtained is still alive.
2832 struct isl_fixed_map {
2833 isl_int v;
2834 isl_map *map;
2837 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2838 int n)
2840 int i;
2841 struct isl_fixed_map *v;
2843 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2844 if (!v)
2845 return NULL;
2846 for (i = 0; i < n; ++i)
2847 isl_int_init(v[i].v);
2848 return v;
2851 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2853 int i;
2855 if (!v)
2856 return;
2857 for (i = 0; i < n; ++i)
2858 isl_int_clear(v[i].v);
2859 free(v);
2862 /* Compare the "v" field of two isl_fixed_map structs.
2864 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2866 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2867 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2869 return isl_int_cmp(e1->v, e2->v);
2872 /* Internal data structure used while checking whether all maps
2873 * in a union_map have a fixed value for a given output dimension.
2874 * v is the list of maps, with the fixed value for the dimension
2875 * n is the number of maps considered so far
2876 * pos is the output dimension under investigation
2878 struct isl_fixed_dim_data {
2879 struct isl_fixed_map *v;
2880 int n;
2881 int pos;
2884 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2886 struct isl_fixed_dim_data *data = user;
2888 data->v[data->n].map = map;
2889 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2890 &data->v[data->n++].v);
2893 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2894 int first, int n_range);
2896 /* Given a list of the maps, with their fixed values at output dimension "pos",
2897 * check whether the ranges of the maps form an obvious partition.
2899 * We first sort the maps according to their fixed values.
2900 * If all maps have a different value, then we know the ranges form
2901 * a partition.
2902 * Otherwise, we collect the maps with the same fixed value and
2903 * check whether each such collection is obviously injective
2904 * based on later dimensions.
2906 static int separates(struct isl_fixed_map *v, int n,
2907 __isl_take isl_space *dim, int pos, int n_range)
2909 int i;
2911 if (!v)
2912 goto error;
2914 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2916 for (i = 0; i + 1 < n; ++i) {
2917 int j, k;
2918 isl_union_map *part;
2919 int injective;
2921 for (j = i + 1; j < n; ++j)
2922 if (isl_int_ne(v[i].v, v[j].v))
2923 break;
2925 if (j == i + 1)
2926 continue;
2928 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2929 for (k = i; k < j; ++k)
2930 part = isl_union_map_add_map(part,
2931 isl_map_copy(v[k].map));
2933 injective = plain_injective_on_range(part, pos + 1, n_range);
2934 if (injective < 0)
2935 goto error;
2936 if (!injective)
2937 break;
2939 i = j - 1;
2942 isl_space_free(dim);
2943 free_isl_fixed_map_array(v, n);
2944 return i + 1 >= n;
2945 error:
2946 isl_space_free(dim);
2947 free_isl_fixed_map_array(v, n);
2948 return -1;
2951 /* Check whether the maps in umap have obviously distinct ranges.
2952 * In particular, check for an output dimension in the range
2953 * [first,n_range) for which all maps have a fixed value
2954 * and then check if these values, possibly along with fixed values
2955 * at later dimensions, entail distinct ranges.
2957 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2958 int first, int n_range)
2960 isl_ctx *ctx;
2961 int n;
2962 struct isl_fixed_dim_data data = { NULL };
2964 ctx = isl_union_map_get_ctx(umap);
2966 n = isl_union_map_n_map(umap);
2967 if (!umap)
2968 goto error;
2970 if (n <= 1) {
2971 isl_union_map_free(umap);
2972 return isl_bool_true;
2975 if (first >= n_range) {
2976 isl_union_map_free(umap);
2977 return isl_bool_false;
2980 data.v = alloc_isl_fixed_map_array(ctx, n);
2981 if (!data.v)
2982 goto error;
2984 for (data.pos = first; data.pos < n_range; ++data.pos) {
2985 isl_bool fixed;
2986 int injective;
2987 isl_space *dim;
2989 data.n = 0;
2990 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2991 if (fixed < 0)
2992 goto error;
2993 if (!fixed)
2994 continue;
2995 dim = isl_union_map_get_space(umap);
2996 injective = separates(data.v, n, dim, data.pos, n_range);
2997 isl_union_map_free(umap);
2998 return injective;
3001 free_isl_fixed_map_array(data.v, n);
3002 isl_union_map_free(umap);
3004 return isl_bool_false;
3005 error:
3006 free_isl_fixed_map_array(data.v, n);
3007 isl_union_map_free(umap);
3008 return isl_bool_error;
3011 /* Check whether the maps in umap that map to subsets of "ran"
3012 * have obviously distinct ranges.
3014 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3015 void *user)
3017 isl_union_map *umap = user;
3019 umap = isl_union_map_copy(umap);
3020 umap = isl_union_map_intersect_range(umap,
3021 isl_union_set_from_set(isl_set_copy(ran)));
3022 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
3025 /* Check if the given union_map is obviously injective.
3027 * In particular, we first check if all individual maps are obviously
3028 * injective and then check if all the ranges of these maps are
3029 * obviously disjoint.
3031 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3033 isl_bool in;
3034 isl_union_map *univ;
3035 isl_union_set *ran;
3037 in = union_map_forall(umap, &isl_map_plain_is_injective);
3038 if (in < 0)
3039 return isl_bool_error;
3040 if (!in)
3041 return isl_bool_false;
3043 univ = isl_union_map_universe(isl_union_map_copy(umap));
3044 ran = isl_union_map_range(univ);
3046 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3048 isl_union_set_free(ran);
3050 return in;
3053 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3055 isl_bool sv;
3057 sv = isl_union_map_is_single_valued(umap);
3058 if (sv < 0 || !sv)
3059 return sv;
3061 return isl_union_map_is_injective(umap);
3064 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3066 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3067 struct isl_un_op_control control = {
3068 .filter = &un_op_filter_drop_user,
3069 .filter_user = &data,
3070 .fn_map = &isl_map_zip,
3072 return un_op(umap, &control);
3075 /* Given a union map, take the maps of the form A -> (B -> C) and
3076 * return the union of the corresponding maps (A -> B) -> C.
3078 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3080 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3081 struct isl_un_op_control control = {
3082 .filter = &un_op_filter_drop_user,
3083 .filter_user = &data,
3084 .fn_map = &isl_map_uncurry,
3086 return un_op(umap, &control);
3089 /* Given a union map, take the maps of the form (A -> B) -> C and
3090 * return the union of the corresponding maps A -> (B -> C).
3092 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3094 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3095 struct isl_un_op_control control = {
3096 .filter = &un_op_filter_drop_user,
3097 .filter_user = &data,
3098 .fn_map = &isl_map_curry,
3100 return un_op(umap, &control);
3103 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3104 * return the union of the corresponding maps A -> (B -> (C -> D)).
3106 __isl_give isl_union_map *isl_union_map_range_curry(
3107 __isl_take isl_union_map *umap)
3109 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3110 struct isl_un_op_control control = {
3111 .filter = &un_op_filter_drop_user,
3112 .filter_user = &data,
3113 .fn_map = &isl_map_range_curry,
3115 return un_op(umap, &control);
3118 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3120 struct isl_un_op_control control = {
3121 .fn_map = &isl_set_lift,
3123 return un_op(uset, &control);
3126 static isl_stat coefficients_entry(void **entry, void *user)
3128 isl_set *set = *entry;
3129 isl_union_set **res = user;
3131 set = isl_set_copy(set);
3132 set = isl_set_from_basic_set(isl_set_coefficients(set));
3133 *res = isl_union_set_add_set(*res, set);
3135 return isl_stat_ok;
3138 __isl_give isl_union_set *isl_union_set_coefficients(
3139 __isl_take isl_union_set *uset)
3141 isl_ctx *ctx;
3142 isl_space *dim;
3143 isl_union_set *res;
3145 if (!uset)
3146 return NULL;
3148 ctx = isl_union_set_get_ctx(uset);
3149 dim = isl_space_set_alloc(ctx, 0, 0);
3150 res = isl_union_map_alloc(dim, uset->table.n);
3151 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3152 &coefficients_entry, &res) < 0)
3153 goto error;
3155 isl_union_set_free(uset);
3156 return res;
3157 error:
3158 isl_union_set_free(uset);
3159 isl_union_set_free(res);
3160 return NULL;
3163 static isl_stat solutions_entry(void **entry, void *user)
3165 isl_set *set = *entry;
3166 isl_union_set **res = user;
3168 set = isl_set_copy(set);
3169 set = isl_set_from_basic_set(isl_set_solutions(set));
3170 if (!*res)
3171 *res = isl_union_set_from_set(set);
3172 else
3173 *res = isl_union_set_add_set(*res, set);
3175 if (!*res)
3176 return isl_stat_error;
3178 return isl_stat_ok;
3181 __isl_give isl_union_set *isl_union_set_solutions(
3182 __isl_take isl_union_set *uset)
3184 isl_union_set *res = NULL;
3186 if (!uset)
3187 return NULL;
3189 if (uset->table.n == 0) {
3190 res = isl_union_set_empty(isl_union_set_get_space(uset));
3191 isl_union_set_free(uset);
3192 return res;
3195 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3196 &solutions_entry, &res) < 0)
3197 goto error;
3199 isl_union_set_free(uset);
3200 return res;
3201 error:
3202 isl_union_set_free(uset);
3203 isl_union_set_free(res);
3204 return NULL;
3207 /* Is the domain space of "map" equal to "space"?
3209 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3211 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3212 space, isl_dim_out);
3215 /* Is the range space of "map" equal to "space"?
3217 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3219 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3220 space, isl_dim_out);
3223 /* Is the set space of "map" equal to "space"?
3225 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3227 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3228 space, isl_dim_out);
3231 /* Internal data structure for preimage_pw_multi_aff.
3233 * "pma" is the function under which the preimage should be taken.
3234 * "space" is the space of "pma".
3235 * "res" collects the results.
3236 * "fn" computes the preimage for a given map.
3237 * "match" returns true if "fn" can be called.
3239 struct isl_union_map_preimage_data {
3240 isl_space *space;
3241 isl_pw_multi_aff *pma;
3242 isl_union_map *res;
3243 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3244 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3245 __isl_take isl_pw_multi_aff *pma);
3248 /* Call data->fn to compute the preimage of the domain or range of *entry
3249 * under the function represented by data->pma, provided the domain/range
3250 * space of *entry matches the target space of data->pma
3251 * (as given by data->match), and add the result to data->res.
3253 static isl_stat preimage_entry(void **entry, void *user)
3255 int m;
3256 isl_map *map = *entry;
3257 struct isl_union_map_preimage_data *data = user;
3258 isl_bool empty;
3260 m = data->match(map, data->space);
3261 if (m < 0)
3262 return isl_stat_error;
3263 if (!m)
3264 return isl_stat_ok;
3266 map = isl_map_copy(map);
3267 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3269 empty = isl_map_is_empty(map);
3270 if (empty < 0 || empty) {
3271 isl_map_free(map);
3272 return empty < 0 ? isl_stat_error : isl_stat_ok;
3275 data->res = isl_union_map_add_map(data->res, map);
3277 return isl_stat_ok;
3280 /* Compute the preimage of the domain or range of "umap" under the function
3281 * represented by "pma".
3282 * In other words, plug in "pma" in the domain or range of "umap".
3283 * The function "fn" performs the actual preimage computation on a map,
3284 * while "match" determines to which maps the function should be applied.
3286 static __isl_give isl_union_map *preimage_pw_multi_aff(
3287 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3288 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3289 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3290 __isl_take isl_pw_multi_aff *pma))
3292 isl_ctx *ctx;
3293 isl_space *space;
3294 struct isl_union_map_preimage_data data;
3296 umap = isl_union_map_align_params(umap,
3297 isl_pw_multi_aff_get_space(pma));
3298 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3300 if (!umap || !pma)
3301 goto error;
3303 ctx = isl_union_map_get_ctx(umap);
3304 space = isl_union_map_get_space(umap);
3305 data.space = isl_pw_multi_aff_get_space(pma);
3306 data.pma = pma;
3307 data.res = isl_union_map_alloc(space, umap->table.n);
3308 data.match = match;
3309 data.fn = fn;
3310 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3311 &data) < 0)
3312 data.res = isl_union_map_free(data.res);
3314 isl_space_free(data.space);
3315 isl_union_map_free(umap);
3316 isl_pw_multi_aff_free(pma);
3317 return data.res;
3318 error:
3319 isl_union_map_free(umap);
3320 isl_pw_multi_aff_free(pma);
3321 return NULL;
3324 /* Compute the preimage of the domain of "umap" under the function
3325 * represented by "pma".
3326 * In other words, plug in "pma" in the domain of "umap".
3327 * The result contains maps that live in the same spaces as the maps of "umap"
3328 * with domain space equal to the target space of "pma",
3329 * except that the domain has been replaced by the domain space of "pma".
3331 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3332 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3334 return preimage_pw_multi_aff(umap, pma, &domain_match,
3335 &isl_map_preimage_domain_pw_multi_aff);
3338 /* Compute the preimage of the range of "umap" under the function
3339 * represented by "pma".
3340 * In other words, plug in "pma" in the range of "umap".
3341 * The result contains maps that live in the same spaces as the maps of "umap"
3342 * with range space equal to the target space of "pma",
3343 * except that the range has been replaced by the domain space of "pma".
3345 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3346 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3348 return preimage_pw_multi_aff(umap, pma, &range_match,
3349 &isl_map_preimage_range_pw_multi_aff);
3352 /* Compute the preimage of "uset" under the function represented by "pma".
3353 * In other words, plug in "pma" in "uset".
3354 * The result contains sets that live in the same spaces as the sets of "uset"
3355 * with space equal to the target space of "pma",
3356 * except that the space has been replaced by the domain space of "pma".
3358 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3359 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3361 return preimage_pw_multi_aff(uset, pma, &set_match,
3362 &isl_set_preimage_pw_multi_aff);
3365 /* Compute the preimage of the domain of "umap" under the function
3366 * represented by "ma".
3367 * In other words, plug in "ma" in the domain of "umap".
3368 * The result contains maps that live in the same spaces as the maps of "umap"
3369 * with domain space equal to the target space of "ma",
3370 * except that the domain has been replaced by the domain space of "ma".
3372 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3373 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3375 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3376 isl_pw_multi_aff_from_multi_aff(ma));
3379 /* Compute the preimage of the range of "umap" under the function
3380 * represented by "ma".
3381 * In other words, plug in "ma" in the range of "umap".
3382 * The result contains maps that live in the same spaces as the maps of "umap"
3383 * with range space equal to the target space of "ma",
3384 * except that the range has been replaced by the domain space of "ma".
3386 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3387 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3389 return isl_union_map_preimage_range_pw_multi_aff(umap,
3390 isl_pw_multi_aff_from_multi_aff(ma));
3393 /* Compute the preimage of "uset" under the function represented by "ma".
3394 * In other words, plug in "ma" in "uset".
3395 * The result contains sets that live in the same spaces as the sets of "uset"
3396 * with space equal to the target space of "ma",
3397 * except that the space has been replaced by the domain space of "ma".
3399 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3400 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3402 return isl_union_set_preimage_pw_multi_aff(uset,
3403 isl_pw_multi_aff_from_multi_aff(ma));
3406 /* Internal data structure for preimage_multi_pw_aff.
3408 * "mpa" is the function under which the preimage should be taken.
3409 * "space" is the space of "mpa".
3410 * "res" collects the results.
3411 * "fn" computes the preimage for a given map.
3412 * "match" returns true if "fn" can be called.
3414 struct isl_union_map_preimage_mpa_data {
3415 isl_space *space;
3416 isl_multi_pw_aff *mpa;
3417 isl_union_map *res;
3418 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3419 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3420 __isl_take isl_multi_pw_aff *mpa);
3423 /* Call data->fn to compute the preimage of the domain or range of *entry
3424 * under the function represented by data->mpa, provided the domain/range
3425 * space of *entry matches the target space of data->mpa
3426 * (as given by data->match), and add the result to data->res.
3428 static isl_stat preimage_mpa_entry(void **entry, void *user)
3430 int m;
3431 isl_map *map = *entry;
3432 struct isl_union_map_preimage_mpa_data *data = user;
3433 isl_bool empty;
3435 m = data->match(map, data->space);
3436 if (m < 0)
3437 return isl_stat_error;
3438 if (!m)
3439 return isl_stat_ok;
3441 map = isl_map_copy(map);
3442 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3444 empty = isl_map_is_empty(map);
3445 if (empty < 0 || empty) {
3446 isl_map_free(map);
3447 return empty < 0 ? isl_stat_error : isl_stat_ok;
3450 data->res = isl_union_map_add_map(data->res, map);
3452 return isl_stat_ok;
3455 /* Compute the preimage of the domain or range of "umap" under the function
3456 * represented by "mpa".
3457 * In other words, plug in "mpa" in the domain or range of "umap".
3458 * The function "fn" performs the actual preimage computation on a map,
3459 * while "match" determines to which maps the function should be applied.
3461 static __isl_give isl_union_map *preimage_multi_pw_aff(
3462 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3463 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3464 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3465 __isl_take isl_multi_pw_aff *mpa))
3467 isl_ctx *ctx;
3468 isl_space *space;
3469 struct isl_union_map_preimage_mpa_data data;
3471 umap = isl_union_map_align_params(umap,
3472 isl_multi_pw_aff_get_space(mpa));
3473 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3475 if (!umap || !mpa)
3476 goto error;
3478 ctx = isl_union_map_get_ctx(umap);
3479 space = isl_union_map_get_space(umap);
3480 data.space = isl_multi_pw_aff_get_space(mpa);
3481 data.mpa = mpa;
3482 data.res = isl_union_map_alloc(space, umap->table.n);
3483 data.match = match;
3484 data.fn = fn;
3485 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3486 &data) < 0)
3487 data.res = isl_union_map_free(data.res);
3489 isl_space_free(data.space);
3490 isl_union_map_free(umap);
3491 isl_multi_pw_aff_free(mpa);
3492 return data.res;
3493 error:
3494 isl_union_map_free(umap);
3495 isl_multi_pw_aff_free(mpa);
3496 return NULL;
3499 /* Compute the preimage of the domain of "umap" under the function
3500 * represented by "mpa".
3501 * In other words, plug in "mpa" in the domain of "umap".
3502 * The result contains maps that live in the same spaces as the maps of "umap"
3503 * with domain space equal to the target space of "mpa",
3504 * except that the domain has been replaced by the domain space of "mpa".
3506 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3507 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3509 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3510 &isl_map_preimage_domain_multi_pw_aff);
3513 /* Internal data structure for preimage_upma.
3515 * "umap" is the map of which the preimage should be computed.
3516 * "res" collects the results.
3517 * "fn" computes the preimage for a given piecewise multi-affine function.
3519 struct isl_union_map_preimage_upma_data {
3520 isl_union_map *umap;
3521 isl_union_map *res;
3522 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3523 __isl_take isl_pw_multi_aff *pma);
3526 /* Call data->fn to compute the preimage of the domain or range of data->umap
3527 * under the function represented by pma and add the result to data->res.
3529 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3531 struct isl_union_map_preimage_upma_data *data = user;
3532 isl_union_map *umap;
3534 umap = isl_union_map_copy(data->umap);
3535 umap = data->fn(umap, pma);
3536 data->res = isl_union_map_union(data->res, umap);
3538 return data->res ? isl_stat_ok : isl_stat_error;
3541 /* Compute the preimage of the domain or range of "umap" under the function
3542 * represented by "upma".
3543 * In other words, plug in "upma" in the domain or range of "umap".
3544 * The function "fn" performs the actual preimage computation
3545 * on a piecewise multi-affine function.
3547 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3548 __isl_take isl_union_map *umap,
3549 __isl_take isl_union_pw_multi_aff *upma,
3550 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3551 __isl_take isl_pw_multi_aff *pma))
3553 struct isl_union_map_preimage_upma_data data;
3555 data.umap = umap;
3556 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3557 data.fn = fn;
3558 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3559 &preimage_upma, &data) < 0)
3560 data.res = isl_union_map_free(data.res);
3562 isl_union_map_free(umap);
3563 isl_union_pw_multi_aff_free(upma);
3565 return data.res;
3568 /* Compute the preimage of the domain of "umap" under the function
3569 * represented by "upma".
3570 * In other words, plug in "upma" in the domain of "umap".
3571 * The result contains maps that live in the same spaces as the maps of "umap"
3572 * with domain space equal to one of the target spaces of "upma",
3573 * except that the domain has been replaced by one of the domain spaces that
3574 * correspond to that target space of "upma".
3576 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3577 __isl_take isl_union_map *umap,
3578 __isl_take isl_union_pw_multi_aff *upma)
3580 return preimage_union_pw_multi_aff(umap, upma,
3581 &isl_union_map_preimage_domain_pw_multi_aff);
3584 /* Compute the preimage of the range of "umap" under the function
3585 * represented by "upma".
3586 * In other words, plug in "upma" in the range of "umap".
3587 * The result contains maps that live in the same spaces as the maps of "umap"
3588 * with range space equal to one of the target spaces of "upma",
3589 * except that the range has been replaced by one of the domain spaces that
3590 * correspond to that target space of "upma".
3592 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3593 __isl_take isl_union_map *umap,
3594 __isl_take isl_union_pw_multi_aff *upma)
3596 return preimage_union_pw_multi_aff(umap, upma,
3597 &isl_union_map_preimage_range_pw_multi_aff);
3600 /* Compute the preimage of "uset" under the function represented by "upma".
3601 * In other words, plug in "upma" in the range of "uset".
3602 * The result contains sets that live in the same spaces as the sets of "uset"
3603 * with space equal to one of the target spaces of "upma",
3604 * except that the space has been replaced by one of the domain spaces that
3605 * correspond to that target space of "upma".
3607 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3608 __isl_take isl_union_set *uset,
3609 __isl_take isl_union_pw_multi_aff *upma)
3611 return preimage_union_pw_multi_aff(uset, upma,
3612 &isl_union_set_preimage_pw_multi_aff);
3615 /* Reset the user pointer on all identifiers of parameters and tuples
3616 * of the spaces of "umap".
3618 __isl_give isl_union_map *isl_union_map_reset_user(
3619 __isl_take isl_union_map *umap)
3621 umap = isl_union_map_cow(umap);
3622 if (!umap)
3623 return NULL;
3624 umap->dim = isl_space_reset_user(umap->dim);
3625 if (!umap->dim)
3626 return isl_union_map_free(umap);
3627 return total(umap, &isl_map_reset_user);
3630 /* Reset the user pointer on all identifiers of parameters and tuples
3631 * of the spaces of "uset".
3633 __isl_give isl_union_set *isl_union_set_reset_user(
3634 __isl_take isl_union_set *uset)
3636 return isl_union_map_reset_user(uset);
3639 /* Remove all existentially quantified variables and integer divisions
3640 * from "umap" using Fourier-Motzkin elimination.
3642 __isl_give isl_union_map *isl_union_map_remove_divs(
3643 __isl_take isl_union_map *umap)
3645 return total(umap, &isl_map_remove_divs);
3648 /* Remove all existentially quantified variables and integer divisions
3649 * from "uset" using Fourier-Motzkin elimination.
3651 __isl_give isl_union_set *isl_union_set_remove_divs(
3652 __isl_take isl_union_set *uset)
3654 return isl_union_map_remove_divs(uset);
3657 /* Internal data structure for isl_union_map_project_out.
3658 * "type", "first" and "n" are the arguments for the isl_map_project_out
3659 * call.
3660 * "res" collects the results.
3662 struct isl_union_map_project_out_data {
3663 enum isl_dim_type type;
3664 unsigned first;
3665 unsigned n;
3667 isl_union_map *res;
3670 /* Turn the data->n dimensions of type data->type, starting at data->first
3671 * into existentially quantified variables and add the result to data->res.
3673 static isl_stat project_out(__isl_take isl_map *map, void *user)
3675 struct isl_union_map_project_out_data *data = user;
3677 map = isl_map_project_out(map, data->type, data->first, data->n);
3678 data->res = isl_union_map_add_map(data->res, map);
3680 return isl_stat_ok;
3683 /* Turn the "n" dimensions of type "type", starting at "first"
3684 * into existentially quantified variables.
3685 * Since the space of an isl_union_map only contains parameters,
3686 * type is required to be equal to isl_dim_param.
3688 __isl_give isl_union_map *isl_union_map_project_out(
3689 __isl_take isl_union_map *umap,
3690 enum isl_dim_type type, unsigned first, unsigned n)
3692 isl_space *space;
3693 struct isl_union_map_project_out_data data = { type, first, n };
3695 if (!umap)
3696 return NULL;
3698 if (type != isl_dim_param)
3699 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3700 "can only project out parameters",
3701 return isl_union_map_free(umap));
3703 space = isl_union_map_get_space(umap);
3704 space = isl_space_drop_dims(space, type, first, n);
3705 data.res = isl_union_map_empty(space);
3706 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3707 data.res = isl_union_map_free(data.res);
3709 isl_union_map_free(umap);
3711 return data.res;
3714 /* Project out all parameters from "umap" by existentially quantifying
3715 * over them.
3717 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3718 __isl_take isl_union_map *umap)
3720 unsigned n;
3722 if (!umap)
3723 return NULL;
3724 n = isl_union_map_dim(umap, isl_dim_param);
3725 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3728 /* Turn the "n" dimensions of type "type", starting at "first"
3729 * into existentially quantified variables.
3730 * Since the space of an isl_union_set only contains parameters,
3731 * "type" is required to be equal to isl_dim_param.
3733 __isl_give isl_union_set *isl_union_set_project_out(
3734 __isl_take isl_union_set *uset,
3735 enum isl_dim_type type, unsigned first, unsigned n)
3737 return isl_union_map_project_out(uset, type, first, n);
3740 /* Project out all parameters from "uset" by existentially quantifying
3741 * over them.
3743 __isl_give isl_union_set *isl_union_set_project_out_all_params(
3744 __isl_take isl_union_set *uset)
3746 return uset_from_umap(
3747 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3750 /* Internal data structure for isl_union_map_involves_dims.
3751 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3753 struct isl_union_map_involves_dims_data {
3754 unsigned first;
3755 unsigned n;
3758 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3760 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3762 struct isl_union_map_involves_dims_data *data = user;
3763 isl_bool involves;
3765 involves = isl_map_involves_dims(map,
3766 isl_dim_param, data->first, data->n);
3767 return isl_bool_not(involves);
3770 /* Does "umap" involve any of the n parameters starting at first?
3771 * "type" is required to be set to isl_dim_param.
3773 * "umap" involves any of those parameters if any of its maps
3774 * involve the parameters. In other words, "umap" does not
3775 * involve any of the parameters if all its maps to not
3776 * involve the parameters.
3778 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3779 enum isl_dim_type type, unsigned first, unsigned n)
3781 struct isl_union_map_involves_dims_data data = { first, n };
3782 isl_bool excludes;
3784 if (type != isl_dim_param)
3785 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3786 "can only reference parameters", return isl_bool_error);
3788 excludes = union_map_forall_user(umap, &map_excludes, &data);
3790 return isl_bool_not(excludes);
3793 /* Internal data structure for isl_union_map_reset_range_space.
3794 * "range" is the space from which to set the range space.
3795 * "res" collects the results.
3797 struct isl_union_map_reset_range_space_data {
3798 isl_space *range;
3799 isl_union_map *res;
3802 /* Replace the range space of "map" by the range space of data->range and
3803 * add the result to data->res.
3805 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3807 struct isl_union_map_reset_range_space_data *data = user;
3808 isl_space *space;
3810 space = isl_map_get_space(map);
3811 space = isl_space_domain(space);
3812 space = isl_space_extend_domain_with_range(space,
3813 isl_space_copy(data->range));
3814 map = isl_map_reset_space(map, space);
3815 data->res = isl_union_map_add_map(data->res, map);
3817 return data->res ? isl_stat_ok : isl_stat_error;
3820 /* Replace the range space of all the maps in "umap" by
3821 * the range space of "space".
3823 * This assumes that all maps have the same output dimension.
3824 * This function should therefore not be made publicly available.
3826 * Since the spaces of the maps change, so do their hash value.
3827 * We therefore need to create a new isl_union_map.
3829 __isl_give isl_union_map *isl_union_map_reset_range_space(
3830 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3832 struct isl_union_map_reset_range_space_data data = { space };
3834 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3835 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3836 data.res = isl_union_map_free(data.res);
3838 isl_space_free(space);
3839 isl_union_map_free(umap);
3840 return data.res;
3843 /* Check that "umap" and "space" have the same number of parameters.
3845 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3846 __isl_keep isl_space *space)
3848 unsigned dim1, dim2;
3850 if (!umap || !space)
3851 return isl_stat_error;
3852 dim1 = isl_union_map_dim(umap, isl_dim_param);
3853 dim2 = isl_space_dim(space, isl_dim_param);
3854 if (dim1 == dim2)
3855 return isl_stat_ok;
3856 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3857 "number of parameters does not match", return isl_stat_error);
3860 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3861 * "space" is the target space.
3862 * "res" collects the results.
3864 struct isl_union_map_reset_params_data {
3865 isl_space *space;
3866 isl_union_map *res;
3869 /* Replace the parameters of "map" by those of data->space and
3870 * add the result to data->res.
3872 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3874 struct isl_union_map_reset_params_data *data = user;
3875 isl_space *space;
3877 space = isl_map_get_space(map);
3878 space = isl_space_replace_params(space, data->space);
3879 map = isl_map_reset_equal_dim_space(map, space);
3880 data->res = isl_union_map_add_map(data->res, map);
3882 return data->res ? isl_stat_ok : isl_stat_error;
3885 /* Replace the space of "umap" by "space", without modifying
3886 * the dimension of "umap", i.e., the number of parameters of "umap".
3888 * Since the hash values of the maps in the union map depend
3889 * on the parameters, a new union map needs to be constructed.
3891 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3892 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3894 struct isl_union_map_reset_params_data data = { space };
3895 isl_bool equal;
3896 isl_space *umap_space;
3898 umap_space = isl_union_map_peek_space(umap);
3899 equal = isl_space_is_equal(umap_space, space);
3900 if (equal < 0)
3901 goto error;
3902 if (equal) {
3903 isl_space_free(space);
3904 return umap;
3906 if (check_union_map_space_equal_dim(umap, space) < 0)
3907 goto error;
3909 data.res = isl_union_map_empty(isl_space_copy(space));
3910 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3911 data.res = isl_union_map_free(data.res);
3913 isl_space_free(space);
3914 isl_union_map_free(umap);
3915 return data.res;
3916 error:
3917 isl_union_map_free(umap);
3918 isl_space_free(space);
3919 return NULL;
3922 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3923 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3924 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3925 * to the domain and the range of each map.
3926 * "res" collects the results.
3928 struct isl_union_order_at_data {
3929 isl_multi_union_pw_aff *mupa;
3930 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3931 __isl_take isl_multi_pw_aff *mpa2);
3932 isl_union_map *res;
3935 /* Intersect "map" with the result of applying data->order to
3936 * the functions in data->mupa that apply to the domain and the range
3937 * of "map" and add the result to data->res.
3939 static isl_stat order_at(__isl_take isl_map *map, void *user)
3941 struct isl_union_order_at_data *data = user;
3942 isl_space *space;
3943 isl_multi_pw_aff *mpa1, *mpa2;
3944 isl_map *order;
3946 space = isl_space_domain(isl_map_get_space(map));
3947 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3948 space = isl_space_range(isl_map_get_space(map));
3949 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3950 order = data->order(mpa1, mpa2);
3951 map = isl_map_intersect(map, order);
3952 data->res = isl_union_map_add_map(data->res, map);
3954 return data->res ? isl_stat_ok : isl_stat_error;
3957 /* If "mupa" has a non-trivial explicit domain, then intersect
3958 * domain and range of "umap" with this explicit domain.
3959 * If the explicit domain only describes constraints on the parameters,
3960 * then the intersection only needs to be performed once.
3962 static __isl_give isl_union_map *intersect_explicit_domain(
3963 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
3965 isl_bool non_trivial, is_params;
3966 isl_union_set *dom;
3968 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3969 if (non_trivial < 0)
3970 return isl_union_map_free(umap);
3971 if (!non_trivial)
3972 return umap;
3973 mupa = isl_multi_union_pw_aff_copy(mupa);
3974 dom = isl_multi_union_pw_aff_domain(mupa);
3975 is_params = isl_union_set_is_params(dom);
3976 if (is_params < 0) {
3977 isl_union_set_free(dom);
3978 return isl_union_map_free(umap);
3980 if (is_params) {
3981 isl_set *set;
3983 set = isl_union_set_params(dom);
3984 umap = isl_union_map_intersect_params(umap, set);
3985 return umap;
3987 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
3988 umap = isl_union_map_intersect_range(umap, dom);
3989 return umap;
3992 /* Intersect each map in "umap" with the result of calling "order"
3993 * on the functions is "mupa" that apply to the domain and the range
3994 * of the map.
3996 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3997 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3998 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3999 __isl_take isl_multi_pw_aff *mpa2))
4001 struct isl_union_order_at_data data;
4003 umap = isl_union_map_align_params(umap,
4004 isl_multi_union_pw_aff_get_space(mupa));
4005 mupa = isl_multi_union_pw_aff_align_params(mupa,
4006 isl_union_map_get_space(umap));
4007 umap = intersect_explicit_domain(umap, mupa);
4008 data.mupa = mupa;
4009 data.order = order;
4010 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4011 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4012 data.res = isl_union_map_free(data.res);
4014 isl_multi_union_pw_aff_free(mupa);
4015 isl_union_map_free(umap);
4016 return data.res;
4019 /* Return the subset of "umap" where the domain and the range
4020 * have equal "mupa" values.
4022 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4023 __isl_take isl_union_map *umap,
4024 __isl_take isl_multi_union_pw_aff *mupa)
4026 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4027 &isl_multi_pw_aff_eq_map);
4030 /* Return the subset of "umap" where the domain has a lexicographically
4031 * smaller "mupa" value than the range.
4033 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4034 __isl_take isl_union_map *umap,
4035 __isl_take isl_multi_union_pw_aff *mupa)
4037 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4038 &isl_multi_pw_aff_lex_lt_map);
4041 /* Return the subset of "umap" where the domain has a lexicographically
4042 * greater "mupa" value than the range.
4044 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4045 __isl_take isl_union_map *umap,
4046 __isl_take isl_multi_union_pw_aff *mupa)
4048 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4049 &isl_multi_pw_aff_lex_gt_map);
4052 /* Return the union of the elements in the list "list".
4054 __isl_give isl_union_set *isl_union_set_list_union(
4055 __isl_take isl_union_set_list *list)
4057 int i, n;
4058 isl_ctx *ctx;
4059 isl_space *space;
4060 isl_union_set *res;
4062 if (!list)
4063 return NULL;
4065 ctx = isl_union_set_list_get_ctx(list);
4066 space = isl_space_params_alloc(ctx, 0);
4067 res = isl_union_set_empty(space);
4069 n = isl_union_set_list_n_union_set(list);
4070 for (i = 0; i < n; ++i) {
4071 isl_union_set *uset_i;
4073 uset_i = isl_union_set_list_get_union_set(list, i);
4074 res = isl_union_set_union(res, uset_i);
4077 isl_union_set_list_free(list);
4078 return res;
4081 /* Update *hash with the hash value of "map".
4083 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4085 uint32_t *hash = user;
4086 uint32_t map_hash;
4088 map_hash = isl_map_get_hash(map);
4089 isl_hash_hash(*hash, map_hash);
4091 isl_map_free(map);
4092 return isl_stat_ok;
4095 /* Return a hash value that digests "umap".
4097 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4099 uint32_t hash;
4101 if (!umap)
4102 return 0;
4104 hash = isl_hash_init();
4105 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4106 return 0;
4108 return hash;
4111 /* Return a hash value that digests "uset".
4113 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4115 return isl_union_map_get_hash(uset);
4118 /* Add the number of basic sets in "set" to "n".
4120 static isl_stat add_n(__isl_take isl_set *set, void *user)
4122 int *n = user;
4124 *n += isl_set_n_basic_set(set);
4125 isl_set_free(set);
4127 return isl_stat_ok;
4130 /* Return the total number of basic sets in "uset".
4132 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4134 int n = 0;
4136 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4137 return -1;
4139 return n;
4142 /* Add the basic sets in "set" to "list".
4144 static isl_stat add_list(__isl_take isl_set *set, void *user)
4146 isl_basic_set_list **list = user;
4147 isl_basic_set_list *list_i;
4149 list_i = isl_set_get_basic_set_list(set);
4150 *list = isl_basic_set_list_concat(*list, list_i);
4151 isl_set_free(set);
4153 if (!*list)
4154 return isl_stat_error;
4155 return isl_stat_ok;
4158 /* Return a list containing all the basic sets in "uset".
4160 * First construct a list of the appropriate size and
4161 * then add all the elements.
4163 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4164 __isl_keep isl_union_set *uset)
4166 int n;
4167 isl_ctx *ctx;
4168 isl_basic_set_list *list;
4170 if (!uset)
4171 return NULL;
4172 ctx = isl_union_set_get_ctx(uset);
4173 n = isl_union_set_n_basic_set(uset);
4174 if (n < 0)
4175 return NULL;
4176 list = isl_basic_set_list_alloc(ctx, n);
4177 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4178 list = isl_basic_set_list_free(list);
4180 return list;
4183 /* Internal data structure for isl_union_map_remove_map_if.
4184 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4186 struct isl_union_map_remove_map_if_data {
4187 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4188 void *user;
4191 /* isl_un_op_control filter that negates the result of data->fn
4192 * called on "map".
4194 static isl_bool not(__isl_keep isl_map *map, void *user)
4196 struct isl_union_map_remove_map_if_data *data = user;
4198 return isl_bool_not(data->fn(map, data->user));
4201 /* Dummy isl_un_op_control transformation callback that
4202 * simply returns the input.
4204 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4206 return map;
4209 /* Call "fn" on every map in "umap" and remove those maps
4210 * for which the callback returns true.
4212 * Use un_op to keep only those maps that are not filtered out,
4213 * applying an identity transformation on them.
4215 __isl_give isl_union_map *isl_union_map_remove_map_if(
4216 __isl_take isl_union_map *umap,
4217 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4219 struct isl_union_map_remove_map_if_data data = { fn, user };
4220 struct isl_un_op_control control = {
4221 .filter = &not,
4222 .filter_user = &data,
4223 .fn_map = &map_id,
4225 return un_op(umap, &control);