isl_poly_is_cst: use isl_bool_ok
[isl.git] / isl_union_map.c
blob919f8d33d859f2fa3fef869f25730297e3cef402
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 isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
38 enum isl_dim_type type)
40 if (!umap)
41 return isl_size_error;
43 if (type != isl_dim_param)
44 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
45 "can only reference parameters", return isl_size_error);
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 isl_size 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 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
483 return umap ? umap->table.n : isl_size_error;
486 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
488 return uset ? uset->table.n : isl_size_error;
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 isl_size n_maps;
577 isl_ctx *ctx;
578 isl_map_list *list;
580 n_maps = isl_union_map_n_map(umap);
581 if (n_maps < 0)
582 return NULL;
583 ctx = isl_union_map_get_ctx(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 /* Internal data structure for isl_union_set_every_set.
703 * "test" is the user-specified callback function.
704 * "user" is the user-specified callback function argument.
706 struct isl_test_set_from_map_data {
707 isl_bool (*test)(__isl_keep isl_set *set, void *user);
708 void *user;
711 /* Call data->test on "map", which is part of an isl_union_set and
712 * therefore known to be an isl_set.
714 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
716 struct isl_test_set_from_map_data *data = user;
718 return data->test(set_from_map(map), data->user);
721 /* Does "test" succeed on every set in "uset"?
723 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
724 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
726 struct isl_test_set_from_map_data data = { test, user };
728 return isl_union_map_every_map(uset_to_umap(uset),
729 &test_set_from_map, &data);
732 struct isl_union_set_foreach_point_data {
733 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
734 void *user;
737 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
739 struct isl_union_set_foreach_point_data *data = user;
740 isl_stat r;
742 r = isl_set_foreach_point(set, data->fn, data->user);
743 isl_set_free(set);
745 return r;
748 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
749 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
751 struct isl_union_set_foreach_point_data data = { fn, user };
752 return isl_union_set_foreach_set(uset, &foreach_point, &data);
755 /* Data structure that specifies how gen_bin_op should
756 * construct results from the inputs.
758 * If "subtract" is set, then a map in the first input is copied to the result
759 * if there is no corresponding map in the second input.
760 * Otherwise, a map in the first input with no corresponding map
761 * in the second input is ignored.
762 * If "filter" is not NULL, then it specifies which maps in the first
763 * input may have a matching map in the second input.
764 * In particular, it makes sure that "match_space" can be called
765 * on the space of the map.
766 * "match_space" specifies how to transform the space of a map
767 * in the first input to the space of the corresponding map
768 * in the second input.
769 * "fn_map" specifies how the matching maps, one from each input,
770 * should be combined to form a map in the result.
772 struct isl_bin_op_control {
773 int subtract;
774 isl_bool (*filter)(__isl_keep isl_map *map);
775 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
776 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
777 __isl_take isl_map *map2);
780 /* Internal data structure for gen_bin_op.
781 * "control" specifies how the maps in the result should be constructed.
782 * "umap2" is a pointer to the second argument.
783 * "res" collects the results.
785 struct isl_union_map_gen_bin_data {
786 struct isl_bin_op_control *control;
787 isl_union_map *umap2;
788 isl_union_map *res;
791 /* Add a copy of "map" to "res" and return the result.
793 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
794 __isl_keep isl_map *map)
796 return isl_union_map_add_map(res, isl_map_copy(map));
799 /* Combine "map1" and "map2", add the result to "res" and return the result.
800 * Check whether the result is empty before adding it to "res".
802 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
803 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
804 struct isl_union_map_gen_bin_data *data)
806 isl_bool empty;
807 isl_map *map;
809 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
810 empty = isl_map_is_empty(map);
811 if (empty < 0 || empty) {
812 isl_map_free(map);
813 if (empty < 0)
814 return isl_union_map_free(res);
815 return res;
817 return isl_union_map_add_map(res, map);
820 /* Dummy match_space function that simply returns the input space.
822 static __isl_give isl_space *identity(__isl_take isl_space *space)
824 return space;
827 /* Look for the map in data->umap2 that corresponds to "map", if any.
828 * Return (isl_bool_true, matching map) if there is one,
829 * (isl_bool_false, NULL) if there is no matching map and
830 * (isl_bool_error, NULL) on error.
832 * If not NULL, then data->control->filter specifies whether "map"
833 * can have any matching map. If so,
834 * data->control->match_space specifies which map in data->umap2
835 * corresponds to "map".
837 static __isl_keep isl_maybe_isl_map bin_try_get_match(
838 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
840 uint32_t hash;
841 struct isl_hash_table_entry *entry2;
842 isl_space *space;
843 isl_maybe_isl_map res = { isl_bool_error, NULL };
845 if (data->control->filter) {
846 res.valid = data->control->filter(map);
847 if (res.valid < 0 || !res.valid)
848 return res;
849 res.valid = isl_bool_error;
852 space = isl_map_get_space(map);
853 if (data->control->match_space != &identity)
854 space = data->control->match_space(space);
855 if (!space)
856 return res;
857 hash = isl_space_get_hash(space);
858 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
859 &data->umap2->table, hash,
860 &has_space, space, 0);
861 isl_space_free(space);
862 res.valid = entry2 != NULL;
863 if (entry2)
864 res.value = entry2->data;
866 return res;
869 /* isl_hash_table_foreach callback for gen_bin_op.
870 * Look for the map in data->umap2 that corresponds
871 * to the map that "entry" points to, apply the binary operation and
872 * add the result to data->res.
874 * If no corresponding map can be found, then the effect depends
875 * on data->control->subtract. If it is set, then the current map
876 * is added directly to the result. Otherwise, it is ignored.
878 static isl_stat gen_bin_entry(void **entry, void *user)
880 struct isl_union_map_gen_bin_data *data = user;
881 isl_map *map = *entry;
882 isl_maybe_isl_map m;
884 m = bin_try_get_match(data, map);
885 if (m.valid < 0)
886 return isl_stat_error;
887 if (!m.valid && !data->control->subtract)
888 return isl_stat_ok;
890 if (!m.valid)
891 data->res = bin_add_map(data->res, map);
892 else
893 data->res = bin_add_pair(data->res, map, m.value, data);
894 if (!data->res)
895 return isl_stat_error;
897 return isl_stat_ok;
900 /* Apply a binary operation to "umap1" and "umap2" based on "control".
901 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
902 * in gen_bin_entry.
904 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
905 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
907 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
909 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
910 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
912 if (!umap1 || !umap2)
913 goto error;
915 data.umap2 = umap2;
916 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
917 umap1->table.n);
918 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
919 &gen_bin_entry, &data) < 0)
920 goto error;
922 isl_union_map_free(umap1);
923 isl_union_map_free(umap2);
924 return data.res;
925 error:
926 isl_union_map_free(umap1);
927 isl_union_map_free(umap2);
928 isl_union_map_free(data.res);
929 return NULL;
932 __isl_give isl_union_map *isl_union_map_subtract(
933 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
935 struct isl_bin_op_control control = {
936 .subtract = 1,
937 .match_space = &identity,
938 .fn_map = &isl_map_subtract,
941 return gen_bin_op(umap1, umap2, &control);
944 __isl_give isl_union_set *isl_union_set_subtract(
945 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
947 return isl_union_map_subtract(uset1, uset2);
950 struct isl_union_map_gen_bin_set_data {
951 isl_set *set;
952 isl_union_map *res;
955 static isl_stat intersect_params_entry(void **entry, void *user)
957 struct isl_union_map_gen_bin_set_data *data = user;
958 isl_map *map = *entry;
959 int empty;
961 map = isl_map_copy(map);
962 map = isl_map_intersect_params(map, isl_set_copy(data->set));
964 empty = isl_map_is_empty(map);
965 if (empty < 0) {
966 isl_map_free(map);
967 return isl_stat_error;
970 data->res = isl_union_map_add_map(data->res, map);
972 return isl_stat_ok;
975 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
976 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
978 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
980 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
981 set = isl_set_align_params(set, isl_union_map_get_space(umap));
983 if (!umap || !set)
984 goto error;
986 data.set = set;
987 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
988 umap->table.n);
989 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
990 fn, &data) < 0)
991 goto error;
993 isl_union_map_free(umap);
994 isl_set_free(set);
995 return data.res;
996 error:
997 isl_union_map_free(umap);
998 isl_set_free(set);
999 isl_union_map_free(data.res);
1000 return NULL;
1003 /* Intersect "umap" with the parameter domain "set".
1005 * If "set" does not have any constraints, then we can return immediately.
1007 __isl_give isl_union_map *isl_union_map_intersect_params(
1008 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1010 int is_universe;
1012 is_universe = isl_set_plain_is_universe(set);
1013 if (is_universe < 0)
1014 goto error;
1015 if (is_universe) {
1016 isl_set_free(set);
1017 return umap;
1020 return gen_bin_set_op(umap, set, &intersect_params_entry);
1021 error:
1022 isl_union_map_free(umap);
1023 isl_set_free(set);
1024 return NULL;
1027 __isl_give isl_union_set *isl_union_set_intersect_params(
1028 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1030 return isl_union_map_intersect_params(uset, set);
1033 static __isl_give isl_union_map *union_map_intersect_params(
1034 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1036 return isl_union_map_intersect_params(umap,
1037 isl_set_from_union_set(uset));
1040 static __isl_give isl_union_map *union_map_gist_params(
1041 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1043 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1046 struct isl_union_map_match_bin_data {
1047 isl_union_map *umap2;
1048 isl_union_map *res;
1049 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1052 static isl_stat match_bin_entry(void **entry, void *user)
1054 struct isl_union_map_match_bin_data *data = user;
1055 uint32_t hash;
1056 struct isl_hash_table_entry *entry2;
1057 isl_map *map = *entry;
1058 int empty;
1060 hash = isl_space_get_hash(map->dim);
1061 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1062 hash, &has_space, map->dim, 0);
1063 if (!entry2)
1064 return isl_stat_ok;
1066 map = isl_map_copy(map);
1067 map = data->fn(map, isl_map_copy(entry2->data));
1069 empty = isl_map_is_empty(map);
1070 if (empty < 0) {
1071 isl_map_free(map);
1072 return isl_stat_error;
1074 if (empty) {
1075 isl_map_free(map);
1076 return isl_stat_ok;
1079 data->res = isl_union_map_add_map(data->res, map);
1081 return isl_stat_ok;
1084 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1085 __isl_take isl_union_map *umap2,
1086 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1088 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1090 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1091 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1093 if (!umap1 || !umap2)
1094 goto error;
1096 data.umap2 = umap2;
1097 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1098 umap1->table.n);
1099 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1100 &match_bin_entry, &data) < 0)
1101 goto error;
1103 isl_union_map_free(umap1);
1104 isl_union_map_free(umap2);
1105 return data.res;
1106 error:
1107 isl_union_map_free(umap1);
1108 isl_union_map_free(umap2);
1109 isl_union_map_free(data.res);
1110 return NULL;
1113 __isl_give isl_union_map *isl_union_map_intersect(
1114 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1116 return match_bin_op(umap1, umap2, &isl_map_intersect);
1119 /* Compute the intersection of the two union_sets.
1120 * As a special case, if exactly one of the two union_sets
1121 * is a parameter domain, then intersect the parameter domain
1122 * of the other one with this set.
1124 __isl_give isl_union_set *isl_union_set_intersect(
1125 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1127 int p1, p2;
1129 p1 = isl_union_set_is_params(uset1);
1130 p2 = isl_union_set_is_params(uset2);
1131 if (p1 < 0 || p2 < 0)
1132 goto error;
1133 if (!p1 && p2)
1134 return union_map_intersect_params(uset1, uset2);
1135 if (p1 && !p2)
1136 return union_map_intersect_params(uset2, uset1);
1137 return isl_union_map_intersect(uset1, uset2);
1138 error:
1139 isl_union_set_free(uset1);
1140 isl_union_set_free(uset2);
1141 return NULL;
1144 static isl_stat gist_params_entry(void **entry, void *user)
1146 struct isl_union_map_gen_bin_set_data *data = user;
1147 isl_map *map = *entry;
1148 int empty;
1150 map = isl_map_copy(map);
1151 map = isl_map_gist_params(map, isl_set_copy(data->set));
1153 empty = isl_map_is_empty(map);
1154 if (empty < 0) {
1155 isl_map_free(map);
1156 return isl_stat_error;
1159 data->res = isl_union_map_add_map(data->res, map);
1161 return isl_stat_ok;
1164 __isl_give isl_union_map *isl_union_map_gist_params(
1165 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1167 return gen_bin_set_op(umap, set, &gist_params_entry);
1170 __isl_give isl_union_set *isl_union_set_gist_params(
1171 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1173 return isl_union_map_gist_params(uset, set);
1176 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1177 __isl_take isl_union_map *context)
1179 return match_bin_op(umap, context, &isl_map_gist);
1182 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1183 __isl_take isl_union_set *context)
1185 if (isl_union_set_is_params(context))
1186 return union_map_gist_params(uset, context);
1187 return isl_union_map_gist(uset, context);
1190 /* For each map in "umap", remove the constraints in the corresponding map
1191 * of "context".
1192 * Each map in "context" is assumed to consist of a single disjunct and
1193 * to have explicit representations for all local variables.
1195 __isl_give isl_union_map *isl_union_map_plain_gist(
1196 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1198 return match_bin_op(umap, context, &isl_map_plain_gist);
1201 /* For each set in "uset", remove the constraints in the corresponding set
1202 * of "context".
1203 * Each set in "context" is assumed to consist of a single disjunct and
1204 * to have explicit representations for all local variables.
1206 __isl_give isl_union_set *isl_union_set_plain_gist(
1207 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1209 return isl_union_map_plain_gist(uset, context);
1212 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1213 __isl_take isl_map *set2)
1215 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1218 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1219 __isl_take isl_map *set2)
1221 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1224 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1225 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1227 return match_bin_op(uset1, uset2, &lex_lt_set);
1230 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1231 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1233 return match_bin_op(uset1, uset2, &lex_le_set);
1236 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1237 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1239 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1242 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1243 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1245 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1248 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1249 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1251 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1254 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1255 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1257 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1260 /* Intersect the domain of "umap" with "uset".
1262 static __isl_give isl_union_map *union_map_intersect_domain(
1263 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1265 struct isl_bin_op_control control = {
1266 .match_space = &isl_space_domain,
1267 .fn_map = &isl_map_intersect_domain,
1270 return gen_bin_op(umap, uset, &control);
1273 /* Intersect the domain of "umap" with "uset".
1274 * If "uset" is a parameters domain, then intersect the parameter
1275 * domain of "umap" with this set.
1277 __isl_give isl_union_map *isl_union_map_intersect_domain(
1278 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1280 if (isl_union_set_is_params(uset))
1281 return union_map_intersect_params(umap, uset);
1282 else
1283 return union_map_intersect_domain(umap, uset);
1286 /* Remove the elements of "uset" from the domain of "umap".
1288 __isl_give isl_union_map *isl_union_map_subtract_domain(
1289 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1291 struct isl_bin_op_control control = {
1292 .subtract = 1,
1293 .match_space = &isl_space_domain,
1294 .fn_map = &isl_map_subtract_domain,
1297 return gen_bin_op(umap, dom, &control);
1300 /* Remove the elements of "uset" from the range of "umap".
1302 __isl_give isl_union_map *isl_union_map_subtract_range(
1303 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1305 struct isl_bin_op_control control = {
1306 .subtract = 1,
1307 .match_space = &isl_space_range,
1308 .fn_map = &isl_map_subtract_range,
1311 return gen_bin_op(umap, dom, &control);
1314 /* Compute the gist of "umap" with respect to the domain "uset".
1316 static __isl_give isl_union_map *union_map_gist_domain(
1317 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1319 struct isl_bin_op_control control = {
1320 .match_space = &isl_space_domain,
1321 .fn_map = &isl_map_gist_domain,
1324 return gen_bin_op(umap, uset, &control);
1327 /* Compute the gist of "umap" with respect to the domain "uset".
1328 * If "uset" is a parameters domain, then compute the gist
1329 * with respect to this parameter domain.
1331 __isl_give isl_union_map *isl_union_map_gist_domain(
1332 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1334 if (isl_union_set_is_params(uset))
1335 return union_map_gist_params(umap, uset);
1336 else
1337 return union_map_gist_domain(umap, uset);
1340 /* Compute the gist of "umap" with respect to the range "uset".
1342 __isl_give isl_union_map *isl_union_map_gist_range(
1343 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1345 struct isl_bin_op_control control = {
1346 .match_space = &isl_space_range,
1347 .fn_map = &isl_map_gist_range,
1350 return gen_bin_op(umap, uset, &control);
1353 __isl_give isl_union_map *isl_union_map_intersect_range(
1354 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1356 struct isl_bin_op_control control = {
1357 .match_space = &isl_space_range,
1358 .fn_map = &isl_map_intersect_range,
1361 return gen_bin_op(umap, uset, &control);
1364 /* Intersect each map in "umap" in a space A -> [B -> C]
1365 * with the corresponding map in "factor" in the space A -> C and
1366 * collect the results.
1368 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1369 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1371 struct isl_bin_op_control control = {
1372 .filter = &isl_map_range_is_wrapping,
1373 .match_space = &isl_space_range_factor_range,
1374 .fn_map = &isl_map_intersect_range_factor_range,
1377 return gen_bin_op(umap, factor, &control);
1380 struct isl_union_map_bin_data {
1381 isl_union_map *umap2;
1382 isl_union_map *res;
1383 isl_map *map;
1384 isl_stat (*fn)(void **entry, void *user);
1387 static isl_stat apply_range_entry(void **entry, void *user)
1389 struct isl_union_map_bin_data *data = user;
1390 isl_map *map2 = *entry;
1391 isl_bool empty;
1393 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1394 map2->dim, isl_dim_in))
1395 return isl_stat_ok;
1397 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1399 empty = isl_map_is_empty(map2);
1400 if (empty < 0) {
1401 isl_map_free(map2);
1402 return isl_stat_error;
1404 if (empty) {
1405 isl_map_free(map2);
1406 return isl_stat_ok;
1409 data->res = isl_union_map_add_map(data->res, map2);
1411 return isl_stat_ok;
1414 static isl_stat bin_entry(void **entry, void *user)
1416 struct isl_union_map_bin_data *data = user;
1417 isl_map *map = *entry;
1419 data->map = map;
1420 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1421 data->fn, data) < 0)
1422 return isl_stat_error;
1424 return isl_stat_ok;
1427 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1428 __isl_take isl_union_map *umap2,
1429 isl_stat (*fn)(void **entry, void *user))
1431 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1433 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1434 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1436 if (!umap1 || !umap2)
1437 goto error;
1439 data.umap2 = umap2;
1440 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1441 umap1->table.n);
1442 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1443 &bin_entry, &data) < 0)
1444 goto error;
1446 isl_union_map_free(umap1);
1447 isl_union_map_free(umap2);
1448 return data.res;
1449 error:
1450 isl_union_map_free(umap1);
1451 isl_union_map_free(umap2);
1452 isl_union_map_free(data.res);
1453 return NULL;
1456 __isl_give isl_union_map *isl_union_map_apply_range(
1457 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1459 return bin_op(umap1, umap2, &apply_range_entry);
1462 __isl_give isl_union_map *isl_union_map_apply_domain(
1463 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1465 umap1 = isl_union_map_reverse(umap1);
1466 umap1 = isl_union_map_apply_range(umap1, umap2);
1467 return isl_union_map_reverse(umap1);
1470 __isl_give isl_union_set *isl_union_set_apply(
1471 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1473 return isl_union_map_apply_range(uset, umap);
1476 static isl_stat map_lex_lt_entry(void **entry, void *user)
1478 struct isl_union_map_bin_data *data = user;
1479 isl_map *map2 = *entry;
1481 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1482 map2->dim, isl_dim_out))
1483 return isl_stat_ok;
1485 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1487 data->res = isl_union_map_add_map(data->res, map2);
1489 return isl_stat_ok;
1492 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1493 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1495 return bin_op(umap1, umap2, &map_lex_lt_entry);
1498 static isl_stat map_lex_le_entry(void **entry, void *user)
1500 struct isl_union_map_bin_data *data = user;
1501 isl_map *map2 = *entry;
1503 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1504 map2->dim, isl_dim_out))
1505 return isl_stat_ok;
1507 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1509 data->res = isl_union_map_add_map(data->res, map2);
1511 return isl_stat_ok;
1514 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1515 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1517 return bin_op(umap1, umap2, &map_lex_le_entry);
1520 static isl_stat product_entry(void **entry, void *user)
1522 struct isl_union_map_bin_data *data = user;
1523 isl_map *map2 = *entry;
1525 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1527 data->res = isl_union_map_add_map(data->res, map2);
1529 return isl_stat_ok;
1532 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1533 __isl_take isl_union_map *umap2)
1535 return bin_op(umap1, umap2, &product_entry);
1538 static isl_stat set_product_entry(void **entry, void *user)
1540 struct isl_union_map_bin_data *data = user;
1541 isl_set *set2 = *entry;
1543 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1545 data->res = isl_union_set_add_set(data->res, set2);
1547 return isl_stat_ok;
1550 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1551 __isl_take isl_union_set *uset2)
1553 return bin_op(uset1, uset2, &set_product_entry);
1556 static isl_stat domain_product_entry(void **entry, void *user)
1558 struct isl_union_map_bin_data *data = user;
1559 isl_map *map2 = *entry;
1561 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1562 map2->dim, isl_dim_out))
1563 return isl_stat_ok;
1565 map2 = isl_map_domain_product(isl_map_copy(data->map),
1566 isl_map_copy(map2));
1568 data->res = isl_union_map_add_map(data->res, map2);
1570 return isl_stat_ok;
1573 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1575 __isl_give isl_union_map *isl_union_map_domain_product(
1576 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1578 return bin_op(umap1, umap2, &domain_product_entry);
1581 static isl_stat range_product_entry(void **entry, void *user)
1583 struct isl_union_map_bin_data *data = user;
1584 isl_map *map2 = *entry;
1586 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1587 map2->dim, isl_dim_in))
1588 return isl_stat_ok;
1590 map2 = isl_map_range_product(isl_map_copy(data->map),
1591 isl_map_copy(map2));
1593 data->res = isl_union_map_add_map(data->res, map2);
1595 return isl_stat_ok;
1598 __isl_give isl_union_map *isl_union_map_range_product(
1599 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1601 return bin_op(umap1, umap2, &range_product_entry);
1604 /* If data->map A -> B and "map2" C -> D have the same range space,
1605 * then add (A, C) -> (B * D) to data->res.
1607 static isl_stat flat_domain_product_entry(void **entry, void *user)
1609 struct isl_union_map_bin_data *data = user;
1610 isl_map *map2 = *entry;
1612 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1613 map2->dim, isl_dim_out))
1614 return isl_stat_ok;
1616 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1617 isl_map_copy(map2));
1619 data->res = isl_union_map_add_map(data->res, map2);
1621 return isl_stat_ok;
1624 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1626 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1627 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1629 return bin_op(umap1, umap2, &flat_domain_product_entry);
1632 static isl_stat flat_range_product_entry(void **entry, void *user)
1634 struct isl_union_map_bin_data *data = user;
1635 isl_map *map2 = *entry;
1637 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1638 map2->dim, isl_dim_in))
1639 return isl_stat_ok;
1641 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1642 isl_map_copy(map2));
1644 data->res = isl_union_map_add_map(data->res, map2);
1646 return isl_stat_ok;
1649 __isl_give isl_union_map *isl_union_map_flat_range_product(
1650 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1652 return bin_op(umap1, umap2, &flat_range_product_entry);
1655 /* Data structure that specifies how un_op should modify
1656 * the maps in the union map.
1658 * If "inplace" is set, then the maps in the input union map
1659 * are modified in place. This means that "fn_map" should not
1660 * change the meaning of the map or that the union map only
1661 * has a single reference.
1662 * If "total" is set, then all maps need to be modified and
1663 * the results need to live in the same space.
1664 * Otherwise, a new union map is constructed to store the results.
1665 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1666 * are taken into account. "filter_user" is passed as the second argument
1667 * to "filter". No filter can be set if "inplace" or
1668 * "total" is set.
1669 * "fn_map" specifies how the maps (selected by "filter")
1670 * should be transformed.
1672 struct isl_un_op_control {
1673 int inplace;
1674 int total;
1675 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1676 void *filter_user;
1677 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1680 /* Data structure for wrapping the data for un_op_filter_drop_user.
1681 * "filter" is the function that is being wrapped.
1683 struct isl_un_op_drop_user_data {
1684 isl_bool (*filter)(__isl_keep isl_map *map);
1687 /* Wrapper for isl_un_op_control filters that do not require
1688 * a second argument.
1689 * Simply call data->filter without the second argument.
1691 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1693 struct isl_un_op_drop_user_data *data = user;
1694 return data->filter(map);
1697 /* Internal data structure for "un_op".
1698 * "control" specifies how the maps in the union map should be modified.
1699 * "res" collects the results.
1701 struct isl_union_map_un_data {
1702 struct isl_un_op_control *control;
1703 isl_union_map *res;
1706 /* isl_hash_table_foreach callback for un_op.
1707 * Handle the map that "entry" points to.
1709 * If control->filter is set, then check if this map satisfies the filter.
1710 * If so (or if control->filter is not set), modify the map
1711 * by calling control->fn_map and either add the result to data->res or
1712 * replace the original entry by the result (if control->inplace is set).
1714 static isl_stat un_entry(void **entry, void *user)
1716 struct isl_union_map_un_data *data = user;
1717 isl_map *map = *entry;
1719 if (data->control->filter) {
1720 isl_bool ok;
1722 ok = data->control->filter(map, data->control->filter_user);
1723 if (ok < 0)
1724 return isl_stat_error;
1725 if (!ok)
1726 return isl_stat_ok;
1729 map = data->control->fn_map(isl_map_copy(map));
1730 if (!map)
1731 return isl_stat_error;
1732 if (data->control->inplace) {
1733 isl_map_free(*entry);
1734 *entry = map;
1735 } else {
1736 data->res = isl_union_map_add_map(data->res, map);
1737 if (!data->res)
1738 return isl_stat_error;
1741 return isl_stat_ok;
1744 /* Modify the maps in "umap" based on "control".
1745 * If control->inplace is set, then modify the maps in "umap" in-place.
1746 * Otherwise, create a new union map to hold the results.
1747 * If control->total is set, then perform an inplace computation
1748 * if "umap" is only referenced once. Otherwise, create a new union map
1749 * to store the results.
1751 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1752 struct isl_un_op_control *control)
1754 struct isl_union_map_un_data data = { control };
1756 if (!umap)
1757 return NULL;
1758 if ((control->inplace || control->total) && control->filter)
1759 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1760 "inplace/total modification cannot be filtered",
1761 return isl_union_map_free(umap));
1763 if (control->total && umap->ref == 1)
1764 control->inplace = 1;
1765 if (control->inplace) {
1766 data.res = umap;
1767 } else {
1768 isl_space *space;
1770 space = isl_union_map_get_space(umap);
1771 data.res = isl_union_map_alloc(space, umap->table.n);
1773 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1774 &umap->table, &un_entry, &data) < 0)
1775 data.res = isl_union_map_free(data.res);
1777 if (control->inplace)
1778 return data.res;
1779 isl_union_map_free(umap);
1780 return data.res;
1783 __isl_give isl_union_map *isl_union_map_from_range(
1784 __isl_take isl_union_set *uset)
1786 struct isl_un_op_control control = {
1787 .fn_map = &isl_map_from_range,
1789 return un_op(uset, &control);
1792 __isl_give isl_union_map *isl_union_map_from_domain(
1793 __isl_take isl_union_set *uset)
1795 return isl_union_map_reverse(isl_union_map_from_range(uset));
1798 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1799 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1801 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1802 isl_union_map_from_range(range));
1805 /* Modify the maps in "umap" by applying "fn" on them.
1806 * "fn" should apply to all maps in "umap" and should not modify the space.
1808 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1809 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1811 struct isl_un_op_control control = {
1812 .total = 1,
1813 .fn_map = fn,
1816 return un_op(umap, &control);
1819 /* Compute the affine hull of "map" and return the result as an isl_map.
1821 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1823 return isl_map_from_basic_map(isl_map_affine_hull(map));
1826 __isl_give isl_union_map *isl_union_map_affine_hull(
1827 __isl_take isl_union_map *umap)
1829 return total(umap, &isl_map_affine_hull_map);
1832 __isl_give isl_union_set *isl_union_set_affine_hull(
1833 __isl_take isl_union_set *uset)
1835 return isl_union_map_affine_hull(uset);
1838 /* Wrapper around isl_set_combined_lineality_space
1839 * that returns the combined lineality space in the form of an isl_set
1840 * instead of an isl_basic_set.
1842 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1844 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1847 /* For each set in "uset", compute the (linear) hull
1848 * of the lineality spaces of its basic sets and
1849 * collect and return the results.
1851 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1852 __isl_take isl_union_set *uset)
1854 struct isl_un_op_control control = {
1855 .fn_map = &combined_lineality_space,
1857 return un_op(uset, &control);
1860 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1862 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1864 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1867 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1868 __isl_take isl_union_map *umap)
1870 return total(umap, &isl_map_polyhedral_hull_map);
1873 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1874 __isl_take isl_union_set *uset)
1876 return isl_union_map_polyhedral_hull(uset);
1879 /* Compute a superset of the convex hull of "map" that is described
1880 * by only translates of the constraints in the constituents of "map" and
1881 * return the result as an isl_map.
1883 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1885 return isl_map_from_basic_map(isl_map_simple_hull(map));
1888 __isl_give isl_union_map *isl_union_map_simple_hull(
1889 __isl_take isl_union_map *umap)
1891 return total(umap, &isl_map_simple_hull_map);
1894 __isl_give isl_union_set *isl_union_set_simple_hull(
1895 __isl_take isl_union_set *uset)
1897 return isl_union_map_simple_hull(uset);
1900 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1901 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1903 struct isl_un_op_control control = {
1904 .inplace = 1,
1905 .fn_map = fn,
1908 return un_op(umap, &control);
1911 /* Remove redundant constraints in each of the basic maps of "umap".
1912 * Since removing redundant constraints does not change the meaning
1913 * or the space, the operation can be performed in-place.
1915 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1916 __isl_take isl_union_map *umap)
1918 return inplace(umap, &isl_map_remove_redundancies);
1921 /* Remove redundant constraints in each of the basic sets of "uset".
1923 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1924 __isl_take isl_union_set *uset)
1926 return isl_union_map_remove_redundancies(uset);
1929 __isl_give isl_union_map *isl_union_map_coalesce(
1930 __isl_take isl_union_map *umap)
1932 return inplace(umap, &isl_map_coalesce);
1935 __isl_give isl_union_set *isl_union_set_coalesce(
1936 __isl_take isl_union_set *uset)
1938 return isl_union_map_coalesce(uset);
1941 __isl_give isl_union_map *isl_union_map_detect_equalities(
1942 __isl_take isl_union_map *umap)
1944 return inplace(umap, &isl_map_detect_equalities);
1947 __isl_give isl_union_set *isl_union_set_detect_equalities(
1948 __isl_take isl_union_set *uset)
1950 return isl_union_map_detect_equalities(uset);
1953 __isl_give isl_union_map *isl_union_map_compute_divs(
1954 __isl_take isl_union_map *umap)
1956 return inplace(umap, &isl_map_compute_divs);
1959 __isl_give isl_union_set *isl_union_set_compute_divs(
1960 __isl_take isl_union_set *uset)
1962 return isl_union_map_compute_divs(uset);
1965 __isl_give isl_union_map *isl_union_map_lexmin(
1966 __isl_take isl_union_map *umap)
1968 return total(umap, &isl_map_lexmin);
1971 __isl_give isl_union_set *isl_union_set_lexmin(
1972 __isl_take isl_union_set *uset)
1974 return isl_union_map_lexmin(uset);
1977 __isl_give isl_union_map *isl_union_map_lexmax(
1978 __isl_take isl_union_map *umap)
1980 return total(umap, &isl_map_lexmax);
1983 __isl_give isl_union_set *isl_union_set_lexmax(
1984 __isl_take isl_union_set *uset)
1986 return isl_union_map_lexmax(uset);
1989 /* Return the universe in the space of "map".
1991 static __isl_give isl_map *universe(__isl_take isl_map *map)
1993 isl_space *space;
1995 space = isl_map_get_space(map);
1996 isl_map_free(map);
1997 return isl_map_universe(space);
2000 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2002 struct isl_un_op_control control = {
2003 .fn_map = &universe,
2005 return un_op(umap, &control);
2008 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2010 return isl_union_map_universe(uset);
2013 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2015 struct isl_un_op_control control = {
2016 .fn_map = &isl_map_reverse,
2018 return un_op(umap, &control);
2021 /* Compute the parameter domain of the given union map.
2023 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2025 struct isl_un_op_control control = {
2026 .fn_map = &isl_map_params,
2028 int empty;
2030 empty = isl_union_map_is_empty(umap);
2031 if (empty < 0)
2032 goto error;
2033 if (empty) {
2034 isl_space *space;
2035 space = isl_union_map_get_space(umap);
2036 isl_union_map_free(umap);
2037 return isl_set_empty(space);
2039 return isl_set_from_union_set(un_op(umap, &control));
2040 error:
2041 isl_union_map_free(umap);
2042 return NULL;
2045 /* Compute the parameter domain of the given union set.
2047 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2049 return isl_union_map_params(uset);
2052 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2054 struct isl_un_op_control control = {
2055 .fn_map = &isl_map_domain,
2057 return un_op(umap, &control);
2060 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2062 struct isl_un_op_control control = {
2063 .fn_map = &isl_map_range,
2065 return un_op(umap, &control);
2068 __isl_give isl_union_map *isl_union_map_domain_map(
2069 __isl_take isl_union_map *umap)
2071 struct isl_un_op_control control = {
2072 .fn_map = &isl_map_domain_map,
2074 return un_op(umap, &control);
2077 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2078 * add the result to "res".
2080 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2082 isl_union_pw_multi_aff **res = user;
2083 isl_multi_aff *ma;
2084 isl_pw_multi_aff *pma;
2086 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2087 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2088 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2090 return *res ? isl_stat_ok : isl_stat_error;
2094 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2095 * to its domain.
2097 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2098 __isl_take isl_union_map *umap)
2100 isl_union_pw_multi_aff *res;
2102 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2103 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2104 res = isl_union_pw_multi_aff_free(res);
2106 isl_union_map_free(umap);
2107 return res;
2110 __isl_give isl_union_map *isl_union_map_range_map(
2111 __isl_take isl_union_map *umap)
2113 struct isl_un_op_control control = {
2114 .fn_map = &isl_map_range_map,
2116 return un_op(umap, &control);
2119 /* Given a collection of wrapped maps of the form A[B -> C],
2120 * return the collection of maps A[B -> C] -> B.
2122 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2123 __isl_take isl_union_set *uset)
2125 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2126 struct isl_un_op_control control = {
2127 .filter = &un_op_filter_drop_user,
2128 .filter_user = &data,
2129 .fn_map = &isl_set_wrapped_domain_map,
2131 return un_op(uset, &control);
2134 /* Does "map" relate elements from the same space?
2136 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2138 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2139 map->dim, isl_dim_out);
2142 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2144 struct isl_un_op_control control = {
2145 .filter = &equal_tuples,
2146 .fn_map = &isl_map_deltas,
2148 return un_op(umap, &control);
2151 __isl_give isl_union_map *isl_union_map_deltas_map(
2152 __isl_take isl_union_map *umap)
2154 struct isl_un_op_control control = {
2155 .filter = &equal_tuples,
2156 .fn_map = &isl_map_deltas_map,
2158 return un_op(umap, &control);
2161 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2163 struct isl_un_op_control control = {
2164 .fn_map = &isl_set_identity,
2166 return un_op(uset, &control);
2169 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2171 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2173 isl_union_pw_multi_aff **res = user;
2174 isl_space *space;
2175 isl_pw_multi_aff *pma;
2177 space = isl_space_map_from_set(isl_set_get_space(set));
2178 pma = isl_pw_multi_aff_identity(space);
2179 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2180 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2182 return *res ? isl_stat_ok : isl_stat_error;
2185 /* Return an identity function on "uset" in the form
2186 * of an isl_union_pw_multi_aff.
2188 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2189 __isl_take isl_union_set *uset)
2191 isl_union_pw_multi_aff *res;
2193 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2194 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2195 res = isl_union_pw_multi_aff_free(res);
2197 isl_union_set_free(uset);
2198 return res;
2201 /* For each map in "umap" of the form [A -> B] -> C,
2202 * construct the map A -> C and collect the results.
2204 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2205 __isl_take isl_union_map *umap)
2207 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2208 struct isl_un_op_control control = {
2209 .filter = &un_op_filter_drop_user,
2210 .filter_user = &data,
2211 .fn_map = &isl_map_domain_factor_domain,
2213 return un_op(umap, &control);
2216 /* For each map in "umap" of the form [A -> B] -> C,
2217 * construct the map B -> C and collect the results.
2219 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2220 __isl_take isl_union_map *umap)
2222 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2223 struct isl_un_op_control control = {
2224 .filter = &un_op_filter_drop_user,
2225 .filter_user = &data,
2226 .fn_map = &isl_map_domain_factor_range,
2228 return un_op(umap, &control);
2231 /* For each map in "umap" of the form A -> [B -> C],
2232 * construct the map A -> B and collect the results.
2234 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2235 __isl_take isl_union_map *umap)
2237 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2238 struct isl_un_op_control control = {
2239 .filter = &un_op_filter_drop_user,
2240 .filter_user = &data,
2241 .fn_map = &isl_map_range_factor_domain,
2243 return un_op(umap, &control);
2246 /* For each map in "umap" of the form A -> [B -> C],
2247 * construct the map A -> C and collect the results.
2249 __isl_give isl_union_map *isl_union_map_range_factor_range(
2250 __isl_take isl_union_map *umap)
2252 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2253 struct isl_un_op_control control = {
2254 .filter = &un_op_filter_drop_user,
2255 .filter_user = &data,
2256 .fn_map = &isl_map_range_factor_range,
2258 return un_op(umap, &control);
2261 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2262 * construct the map A -> C and collect the results.
2264 __isl_give isl_union_map *isl_union_map_factor_domain(
2265 __isl_take isl_union_map *umap)
2267 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2268 struct isl_un_op_control control = {
2269 .filter = &un_op_filter_drop_user,
2270 .filter_user = &data,
2271 .fn_map = &isl_map_factor_domain,
2273 return un_op(umap, &control);
2276 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2277 * construct the map B -> D and collect the results.
2279 __isl_give isl_union_map *isl_union_map_factor_range(
2280 __isl_take isl_union_map *umap)
2282 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2283 struct isl_un_op_control control = {
2284 .filter = &un_op_filter_drop_user,
2285 .filter_user = &data,
2286 .fn_map = &isl_map_factor_range,
2288 return un_op(umap, &control);
2291 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2293 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2294 struct isl_un_op_control control = {
2295 .filter = &un_op_filter_drop_user,
2296 .filter_user = &data,
2297 .fn_map = &isl_set_unwrap,
2299 return un_op(uset, &control);
2302 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2304 struct isl_un_op_control control = {
2305 .fn_map = &isl_map_wrap,
2307 return un_op(umap, &control);
2310 struct isl_union_map_is_subset_data {
2311 isl_union_map *umap2;
2312 isl_bool is_subset;
2315 static isl_stat is_subset_entry(void **entry, void *user)
2317 struct isl_union_map_is_subset_data *data = user;
2318 uint32_t hash;
2319 struct isl_hash_table_entry *entry2;
2320 isl_map *map = *entry;
2322 hash = isl_space_get_hash(map->dim);
2323 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2324 hash, &has_space, map->dim, 0);
2325 if (!entry2) {
2326 int empty = isl_map_is_empty(map);
2327 if (empty < 0)
2328 return isl_stat_error;
2329 if (empty)
2330 return isl_stat_ok;
2331 data->is_subset = isl_bool_false;
2332 return isl_stat_error;
2335 data->is_subset = isl_map_is_subset(map, entry2->data);
2336 if (data->is_subset < 0 || !data->is_subset)
2337 return isl_stat_error;
2339 return isl_stat_ok;
2342 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2343 __isl_keep isl_union_map *umap2)
2345 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2347 umap1 = isl_union_map_copy(umap1);
2348 umap2 = isl_union_map_copy(umap2);
2349 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2350 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2352 if (!umap1 || !umap2)
2353 goto error;
2355 data.umap2 = umap2;
2356 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2357 &is_subset_entry, &data) < 0 &&
2358 data.is_subset)
2359 goto error;
2361 isl_union_map_free(umap1);
2362 isl_union_map_free(umap2);
2364 return data.is_subset;
2365 error:
2366 isl_union_map_free(umap1);
2367 isl_union_map_free(umap2);
2368 return isl_bool_error;
2371 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2372 __isl_keep isl_union_set *uset2)
2374 return isl_union_map_is_subset(uset1, uset2);
2377 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2378 __isl_keep isl_union_map *umap2)
2380 isl_bool is_subset;
2382 if (!umap1 || !umap2)
2383 return isl_bool_error;
2384 is_subset = isl_union_map_is_subset(umap1, umap2);
2385 if (is_subset != isl_bool_true)
2386 return is_subset;
2387 is_subset = isl_union_map_is_subset(umap2, umap1);
2388 return is_subset;
2391 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2392 __isl_keep isl_union_set *uset2)
2394 return isl_union_map_is_equal(uset1, uset2);
2397 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2398 __isl_keep isl_union_map *umap2)
2400 isl_bool is_subset;
2402 if (!umap1 || !umap2)
2403 return isl_bool_error;
2404 is_subset = isl_union_map_is_subset(umap1, umap2);
2405 if (is_subset != isl_bool_true)
2406 return is_subset;
2407 is_subset = isl_union_map_is_subset(umap2, umap1);
2408 return isl_bool_not(is_subset);
2411 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2412 __isl_keep isl_union_set *uset2)
2414 return isl_union_map_is_strict_subset(uset1, uset2);
2417 /* Internal data structure for isl_union_map_is_disjoint.
2418 * umap2 is the union map with which we are comparing.
2419 * is_disjoint is initialized to 1 and is set to 0 as soon
2420 * as the union maps turn out not to be disjoint.
2422 struct isl_union_map_is_disjoint_data {
2423 isl_union_map *umap2;
2424 isl_bool is_disjoint;
2427 /* Check if "map" is disjoint from data->umap2 and abort
2428 * the search if it is not.
2430 static isl_stat is_disjoint_entry(void **entry, void *user)
2432 struct isl_union_map_is_disjoint_data *data = user;
2433 uint32_t hash;
2434 struct isl_hash_table_entry *entry2;
2435 isl_map *map = *entry;
2437 hash = isl_space_get_hash(map->dim);
2438 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2439 hash, &has_space, map->dim, 0);
2440 if (!entry2)
2441 return isl_stat_ok;
2443 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2444 if (data->is_disjoint < 0 || !data->is_disjoint)
2445 return isl_stat_error;
2447 return isl_stat_ok;
2450 /* Are "umap1" and "umap2" disjoint?
2452 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2453 __isl_keep isl_union_map *umap2)
2455 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2457 umap1 = isl_union_map_copy(umap1);
2458 umap2 = isl_union_map_copy(umap2);
2459 umap1 = isl_union_map_align_params(umap1,
2460 isl_union_map_get_space(umap2));
2461 umap2 = isl_union_map_align_params(umap2,
2462 isl_union_map_get_space(umap1));
2464 if (!umap1 || !umap2)
2465 goto error;
2467 data.umap2 = umap2;
2468 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2469 &is_disjoint_entry, &data) < 0 &&
2470 data.is_disjoint)
2471 goto error;
2473 isl_union_map_free(umap1);
2474 isl_union_map_free(umap2);
2476 return data.is_disjoint;
2477 error:
2478 isl_union_map_free(umap1);
2479 isl_union_map_free(umap2);
2480 return isl_bool_error;
2483 /* Are "uset1" and "uset2" disjoint?
2485 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2486 __isl_keep isl_union_set *uset2)
2488 return isl_union_map_is_disjoint(uset1, uset2);
2491 static isl_stat sample_entry(void **entry, void *user)
2493 isl_basic_map **sample = (isl_basic_map **)user;
2494 isl_map *map = *entry;
2496 *sample = isl_map_sample(isl_map_copy(map));
2497 if (!*sample)
2498 return isl_stat_error;
2499 if (!isl_basic_map_plain_is_empty(*sample))
2500 return isl_stat_error;
2501 return isl_stat_ok;
2504 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2506 isl_basic_map *sample = NULL;
2508 if (!umap)
2509 return NULL;
2511 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2512 &sample_entry, &sample) < 0 &&
2513 !sample)
2514 goto error;
2516 if (!sample)
2517 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2519 isl_union_map_free(umap);
2521 return sample;
2522 error:
2523 isl_union_map_free(umap);
2524 return NULL;
2527 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2529 return bset_from_bmap(isl_union_map_sample(uset));
2532 /* Return an element in "uset" in the form of an isl_point.
2533 * Return a void isl_point if "uset" is empty.
2535 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2537 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2540 struct isl_forall_data {
2541 isl_bool res;
2542 isl_bool (*fn)(__isl_keep isl_map *map);
2545 static isl_stat forall_entry(void **entry, void *user)
2547 struct isl_forall_data *data = user;
2548 isl_map *map = *entry;
2550 data->res = data->fn(map);
2551 if (data->res < 0)
2552 return isl_stat_error;
2554 if (!data->res)
2555 return isl_stat_error;
2557 return isl_stat_ok;
2560 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2561 isl_bool (*fn)(__isl_keep isl_map *map))
2563 struct isl_forall_data data = { isl_bool_true, fn };
2565 if (!umap)
2566 return isl_bool_error;
2568 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2569 &forall_entry, &data) < 0 && data.res)
2570 return isl_bool_error;
2572 return data.res;
2575 struct isl_forall_user_data {
2576 isl_bool res;
2577 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2578 void *user;
2581 static isl_stat forall_user_entry(void **entry, void *user)
2583 struct isl_forall_user_data *data = user;
2584 isl_map *map = *entry;
2586 data->res = data->fn(map, data->user);
2587 if (data->res < 0)
2588 return isl_stat_error;
2590 if (!data->res)
2591 return isl_stat_error;
2593 return isl_stat_ok;
2596 /* Check if fn(map, user) returns true for all maps "map" in umap.
2598 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2599 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2601 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2603 if (!umap)
2604 return isl_bool_error;
2606 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2607 &forall_user_entry, &data) < 0 && data.res)
2608 return isl_bool_error;
2610 return data.res;
2613 /* Is "umap" obviously empty?
2615 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2617 isl_size n;
2619 n = isl_union_map_n_map(umap);
2620 if (n < 0)
2621 return isl_bool_error;
2622 return n == 0;
2625 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2627 return union_map_forall(umap, &isl_map_is_empty);
2630 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2632 return isl_union_map_is_empty(uset);
2635 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2637 isl_bool is_subset;
2638 isl_space *dim;
2639 isl_map *id;
2641 if (!map)
2642 return isl_bool_error;
2644 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2645 map->dim, isl_dim_out))
2646 return isl_bool_false;
2648 dim = isl_map_get_space(map);
2649 id = isl_map_identity(dim);
2651 is_subset = isl_map_is_subset(map, id);
2653 isl_map_free(id);
2655 return is_subset;
2658 /* Given an isl_union_map that consists of a single map, check
2659 * if it is single-valued.
2661 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2663 isl_map *map;
2664 isl_bool sv;
2666 umap = isl_union_map_copy(umap);
2667 map = isl_map_from_union_map(umap);
2668 sv = isl_map_is_single_valued(map);
2669 isl_map_free(map);
2671 return sv;
2674 /* Internal data structure for single_valued_on_domain.
2676 * "umap" is the union map to be tested.
2677 * "sv" is set to 1 as long as "umap" may still be single-valued.
2679 struct isl_union_map_is_sv_data {
2680 isl_union_map *umap;
2681 isl_bool sv;
2684 /* Check if the data->umap is single-valued on "set".
2686 * If data->umap consists of a single map on "set", then test it
2687 * as an isl_map.
2689 * Otherwise, compute
2691 * M \circ M^-1
2693 * check if the result is a subset of the identity mapping and
2694 * store the result in data->sv.
2696 * Terminate as soon as data->umap has been determined not to
2697 * be single-valued.
2699 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2701 struct isl_union_map_is_sv_data *data = user;
2702 isl_union_map *umap, *test;
2703 isl_size n;
2705 umap = isl_union_map_copy(data->umap);
2706 umap = isl_union_map_intersect_domain(umap,
2707 isl_union_set_from_set(set));
2709 n = isl_union_map_n_map(umap);
2710 if (n < 0) {
2711 data->sv = isl_bool_error;
2712 } else if (n == 1) {
2713 data->sv = single_map_is_single_valued(umap);
2714 isl_union_map_free(umap);
2715 } else {
2716 test = isl_union_map_reverse(isl_union_map_copy(umap));
2717 test = isl_union_map_apply_range(test, umap);
2719 data->sv = union_map_forall(test, &is_subset_of_identity);
2721 isl_union_map_free(test);
2724 if (data->sv < 0 || !data->sv)
2725 return isl_stat_error;
2726 return isl_stat_ok;
2729 /* Check if the given map is single-valued.
2731 * If the union map consists of a single map, then test it as an isl_map.
2732 * Otherwise, check if the union map is single-valued on each of its
2733 * domain spaces.
2735 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2737 isl_union_map *universe;
2738 isl_union_set *domain;
2739 struct isl_union_map_is_sv_data data;
2740 isl_size n;
2742 n = isl_union_map_n_map(umap);
2743 if (n < 0)
2744 return isl_bool_error;
2745 if (n == 1)
2746 return single_map_is_single_valued(umap);
2748 universe = isl_union_map_universe(isl_union_map_copy(umap));
2749 domain = isl_union_map_domain(universe);
2751 data.sv = isl_bool_true;
2752 data.umap = umap;
2753 if (isl_union_set_foreach_set(domain,
2754 &single_valued_on_domain, &data) < 0 && data.sv)
2755 data.sv = isl_bool_error;
2756 isl_union_set_free(domain);
2758 return data.sv;
2761 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2763 isl_bool in;
2765 umap = isl_union_map_copy(umap);
2766 umap = isl_union_map_reverse(umap);
2767 in = isl_union_map_is_single_valued(umap);
2768 isl_union_map_free(umap);
2770 return in;
2773 /* Is "map" obviously not an identity relation because
2774 * it maps elements from one space to another space?
2775 * Update *non_identity accordingly.
2777 * In particular, if the domain and range spaces are the same,
2778 * then the map is not considered to obviously not be an identity relation.
2779 * Otherwise, the map is considered to obviously not be an identity relation
2780 * if it is is non-empty.
2782 * If "map" is determined to obviously not be an identity relation,
2783 * then the search is aborted.
2785 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2787 isl_bool *non_identity = user;
2788 isl_bool equal;
2789 isl_space *space;
2791 space = isl_map_get_space(map);
2792 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2793 if (equal >= 0 && !equal)
2794 *non_identity = isl_bool_not(isl_map_is_empty(map));
2795 else
2796 *non_identity = isl_bool_not(equal);
2797 isl_space_free(space);
2798 isl_map_free(map);
2800 if (*non_identity < 0 || *non_identity)
2801 return isl_stat_error;
2803 return isl_stat_ok;
2806 /* Is "umap" obviously not an identity relation because
2807 * it maps elements from one space to another space?
2809 * As soon as a map has been found that maps elements to a different space,
2810 * non_identity is changed and the search is aborted.
2812 static isl_bool isl_union_map_plain_is_not_identity(
2813 __isl_keep isl_union_map *umap)
2815 isl_bool non_identity;
2817 non_identity = isl_bool_false;
2818 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2819 &non_identity) < 0 &&
2820 non_identity == isl_bool_false)
2821 return isl_bool_error;
2823 return non_identity;
2826 /* Does "map" only map elements to themselves?
2827 * Update *identity accordingly.
2829 * If "map" is determined not to be an identity relation,
2830 * then the search is aborted.
2832 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2834 isl_bool *identity = user;
2836 *identity = isl_map_is_identity(map);
2837 isl_map_free(map);
2839 if (*identity < 0 || !*identity)
2840 return isl_stat_error;
2842 return isl_stat_ok;
2845 /* Does "umap" only map elements to themselves?
2847 * First check if there are any maps that map elements to different spaces.
2848 * If not, then check that all the maps (between identical spaces)
2849 * are identity relations.
2851 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2853 isl_bool non_identity;
2854 isl_bool identity;
2856 non_identity = isl_union_map_plain_is_not_identity(umap);
2857 if (non_identity < 0 || non_identity)
2858 return isl_bool_not(non_identity);
2860 identity = isl_bool_true;
2861 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2862 identity == isl_bool_true)
2863 return isl_bool_error;
2865 return identity;
2868 /* Represents a map that has a fixed value (v) for one of its
2869 * range dimensions.
2870 * The map in this structure is not reference counted, so it
2871 * is only valid while the isl_union_map from which it was
2872 * obtained is still alive.
2874 struct isl_fixed_map {
2875 isl_int v;
2876 isl_map *map;
2879 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2880 int n)
2882 int i;
2883 struct isl_fixed_map *v;
2885 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2886 if (!v)
2887 return NULL;
2888 for (i = 0; i < n; ++i)
2889 isl_int_init(v[i].v);
2890 return v;
2893 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2895 int i;
2897 if (!v)
2898 return;
2899 for (i = 0; i < n; ++i)
2900 isl_int_clear(v[i].v);
2901 free(v);
2904 /* Compare the "v" field of two isl_fixed_map structs.
2906 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2908 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2909 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2911 return isl_int_cmp(e1->v, e2->v);
2914 /* Internal data structure used while checking whether all maps
2915 * in a union_map have a fixed value for a given output dimension.
2916 * v is the list of maps, with the fixed value for the dimension
2917 * n is the number of maps considered so far
2918 * pos is the output dimension under investigation
2920 struct isl_fixed_dim_data {
2921 struct isl_fixed_map *v;
2922 int n;
2923 int pos;
2926 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2928 struct isl_fixed_dim_data *data = user;
2930 data->v[data->n].map = map;
2931 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2932 &data->v[data->n++].v);
2935 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2936 int first, int n_range);
2938 /* Given a list of the maps, with their fixed values at output dimension "pos",
2939 * check whether the ranges of the maps form an obvious partition.
2941 * We first sort the maps according to their fixed values.
2942 * If all maps have a different value, then we know the ranges form
2943 * a partition.
2944 * Otherwise, we collect the maps with the same fixed value and
2945 * check whether each such collection is obviously injective
2946 * based on later dimensions.
2948 static int separates(struct isl_fixed_map *v, int n,
2949 __isl_take isl_space *dim, int pos, int n_range)
2951 int i;
2953 if (!v)
2954 goto error;
2956 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2958 for (i = 0; i + 1 < n; ++i) {
2959 int j, k;
2960 isl_union_map *part;
2961 int injective;
2963 for (j = i + 1; j < n; ++j)
2964 if (isl_int_ne(v[i].v, v[j].v))
2965 break;
2967 if (j == i + 1)
2968 continue;
2970 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2971 for (k = i; k < j; ++k)
2972 part = isl_union_map_add_map(part,
2973 isl_map_copy(v[k].map));
2975 injective = plain_injective_on_range(part, pos + 1, n_range);
2976 if (injective < 0)
2977 goto error;
2978 if (!injective)
2979 break;
2981 i = j - 1;
2984 isl_space_free(dim);
2985 free_isl_fixed_map_array(v, n);
2986 return i + 1 >= n;
2987 error:
2988 isl_space_free(dim);
2989 free_isl_fixed_map_array(v, n);
2990 return -1;
2993 /* Check whether the maps in umap have obviously distinct ranges.
2994 * In particular, check for an output dimension in the range
2995 * [first,n_range) for which all maps have a fixed value
2996 * and then check if these values, possibly along with fixed values
2997 * at later dimensions, entail distinct ranges.
2999 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3000 int first, int n_range)
3002 isl_ctx *ctx;
3003 isl_size n;
3004 struct isl_fixed_dim_data data = { NULL };
3006 ctx = isl_union_map_get_ctx(umap);
3008 n = isl_union_map_n_map(umap);
3009 if (n < 0)
3010 goto error;
3012 if (n <= 1) {
3013 isl_union_map_free(umap);
3014 return isl_bool_true;
3017 if (first >= n_range) {
3018 isl_union_map_free(umap);
3019 return isl_bool_false;
3022 data.v = alloc_isl_fixed_map_array(ctx, n);
3023 if (!data.v)
3024 goto error;
3026 for (data.pos = first; data.pos < n_range; ++data.pos) {
3027 isl_bool fixed;
3028 int injective;
3029 isl_space *dim;
3031 data.n = 0;
3032 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3033 if (fixed < 0)
3034 goto error;
3035 if (!fixed)
3036 continue;
3037 dim = isl_union_map_get_space(umap);
3038 injective = separates(data.v, n, dim, data.pos, n_range);
3039 isl_union_map_free(umap);
3040 return injective;
3043 free_isl_fixed_map_array(data.v, n);
3044 isl_union_map_free(umap);
3046 return isl_bool_false;
3047 error:
3048 free_isl_fixed_map_array(data.v, n);
3049 isl_union_map_free(umap);
3050 return isl_bool_error;
3053 /* Check whether the maps in umap that map to subsets of "ran"
3054 * have obviously distinct ranges.
3056 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3057 void *user)
3059 isl_size dim;
3060 isl_union_map *umap = user;
3062 dim = isl_set_dim(ran, isl_dim_set);
3063 if (dim < 0)
3064 return isl_bool_error;
3066 umap = isl_union_map_copy(umap);
3067 umap = isl_union_map_intersect_range(umap,
3068 isl_union_set_from_set(isl_set_copy(ran)));
3069 return plain_injective_on_range(umap, 0, dim);
3072 /* Check if the given union_map is obviously injective.
3074 * In particular, we first check if all individual maps are obviously
3075 * injective and then check if all the ranges of these maps are
3076 * obviously disjoint.
3078 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3080 isl_bool in;
3081 isl_union_map *univ;
3082 isl_union_set *ran;
3084 in = union_map_forall(umap, &isl_map_plain_is_injective);
3085 if (in < 0)
3086 return isl_bool_error;
3087 if (!in)
3088 return isl_bool_false;
3090 univ = isl_union_map_universe(isl_union_map_copy(umap));
3091 ran = isl_union_map_range(univ);
3093 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3095 isl_union_set_free(ran);
3097 return in;
3100 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3102 isl_bool sv;
3104 sv = isl_union_map_is_single_valued(umap);
3105 if (sv < 0 || !sv)
3106 return sv;
3108 return isl_union_map_is_injective(umap);
3111 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3113 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3114 struct isl_un_op_control control = {
3115 .filter = &un_op_filter_drop_user,
3116 .filter_user = &data,
3117 .fn_map = &isl_map_zip,
3119 return un_op(umap, &control);
3122 /* Given a union map, take the maps of the form A -> (B -> C) and
3123 * return the union of the corresponding maps (A -> B) -> C.
3125 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3127 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3128 struct isl_un_op_control control = {
3129 .filter = &un_op_filter_drop_user,
3130 .filter_user = &data,
3131 .fn_map = &isl_map_uncurry,
3133 return un_op(umap, &control);
3136 /* Given a union map, take the maps of the form (A -> B) -> C and
3137 * return the union of the corresponding maps A -> (B -> C).
3139 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3141 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3142 struct isl_un_op_control control = {
3143 .filter = &un_op_filter_drop_user,
3144 .filter_user = &data,
3145 .fn_map = &isl_map_curry,
3147 return un_op(umap, &control);
3150 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3151 * return the union of the corresponding maps A -> (B -> (C -> D)).
3153 __isl_give isl_union_map *isl_union_map_range_curry(
3154 __isl_take isl_union_map *umap)
3156 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3157 struct isl_un_op_control control = {
3158 .filter = &un_op_filter_drop_user,
3159 .filter_user = &data,
3160 .fn_map = &isl_map_range_curry,
3162 return un_op(umap, &control);
3165 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3167 struct isl_un_op_control control = {
3168 .fn_map = &isl_set_lift,
3170 return un_op(uset, &control);
3173 static isl_stat coefficients_entry(void **entry, void *user)
3175 isl_set *set = *entry;
3176 isl_union_set **res = user;
3178 set = isl_set_copy(set);
3179 set = isl_set_from_basic_set(isl_set_coefficients(set));
3180 *res = isl_union_set_add_set(*res, set);
3182 return isl_stat_ok;
3185 __isl_give isl_union_set *isl_union_set_coefficients(
3186 __isl_take isl_union_set *uset)
3188 isl_ctx *ctx;
3189 isl_space *dim;
3190 isl_union_set *res;
3192 if (!uset)
3193 return NULL;
3195 ctx = isl_union_set_get_ctx(uset);
3196 dim = isl_space_set_alloc(ctx, 0, 0);
3197 res = isl_union_map_alloc(dim, uset->table.n);
3198 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3199 &coefficients_entry, &res) < 0)
3200 goto error;
3202 isl_union_set_free(uset);
3203 return res;
3204 error:
3205 isl_union_set_free(uset);
3206 isl_union_set_free(res);
3207 return NULL;
3210 static isl_stat solutions_entry(void **entry, void *user)
3212 isl_set *set = *entry;
3213 isl_union_set **res = user;
3215 set = isl_set_copy(set);
3216 set = isl_set_from_basic_set(isl_set_solutions(set));
3217 if (!*res)
3218 *res = isl_union_set_from_set(set);
3219 else
3220 *res = isl_union_set_add_set(*res, set);
3222 if (!*res)
3223 return isl_stat_error;
3225 return isl_stat_ok;
3228 __isl_give isl_union_set *isl_union_set_solutions(
3229 __isl_take isl_union_set *uset)
3231 isl_union_set *res = NULL;
3233 if (!uset)
3234 return NULL;
3236 if (uset->table.n == 0) {
3237 res = isl_union_set_empty(isl_union_set_get_space(uset));
3238 isl_union_set_free(uset);
3239 return res;
3242 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3243 &solutions_entry, &res) < 0)
3244 goto error;
3246 isl_union_set_free(uset);
3247 return res;
3248 error:
3249 isl_union_set_free(uset);
3250 isl_union_set_free(res);
3251 return NULL;
3254 /* Is the domain space of "map" equal to "space"?
3256 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3258 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3259 space, isl_dim_out);
3262 /* Is the range space of "map" equal to "space"?
3264 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3266 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3267 space, isl_dim_out);
3270 /* Is the set space of "map" equal to "space"?
3272 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3274 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3275 space, isl_dim_out);
3278 /* Internal data structure for preimage_pw_multi_aff.
3280 * "pma" is the function under which the preimage should be taken.
3281 * "space" is the space of "pma".
3282 * "res" collects the results.
3283 * "fn" computes the preimage for a given map.
3284 * "match" returns true if "fn" can be called.
3286 struct isl_union_map_preimage_data {
3287 isl_space *space;
3288 isl_pw_multi_aff *pma;
3289 isl_union_map *res;
3290 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3291 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3292 __isl_take isl_pw_multi_aff *pma);
3295 /* Call data->fn to compute the preimage of the domain or range of *entry
3296 * under the function represented by data->pma, provided the domain/range
3297 * space of *entry matches the target space of data->pma
3298 * (as given by data->match), and add the result to data->res.
3300 static isl_stat preimage_entry(void **entry, void *user)
3302 int m;
3303 isl_map *map = *entry;
3304 struct isl_union_map_preimage_data *data = user;
3305 isl_bool empty;
3307 m = data->match(map, data->space);
3308 if (m < 0)
3309 return isl_stat_error;
3310 if (!m)
3311 return isl_stat_ok;
3313 map = isl_map_copy(map);
3314 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3316 empty = isl_map_is_empty(map);
3317 if (empty < 0 || empty) {
3318 isl_map_free(map);
3319 return empty < 0 ? isl_stat_error : isl_stat_ok;
3322 data->res = isl_union_map_add_map(data->res, map);
3324 return isl_stat_ok;
3327 /* Compute the preimage of the domain or range of "umap" under the function
3328 * represented by "pma".
3329 * In other words, plug in "pma" in the domain or range of "umap".
3330 * The function "fn" performs the actual preimage computation on a map,
3331 * while "match" determines to which maps the function should be applied.
3333 static __isl_give isl_union_map *preimage_pw_multi_aff(
3334 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3335 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3336 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3337 __isl_take isl_pw_multi_aff *pma))
3339 isl_ctx *ctx;
3340 isl_space *space;
3341 struct isl_union_map_preimage_data data;
3343 umap = isl_union_map_align_params(umap,
3344 isl_pw_multi_aff_get_space(pma));
3345 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3347 if (!umap || !pma)
3348 goto error;
3350 ctx = isl_union_map_get_ctx(umap);
3351 space = isl_union_map_get_space(umap);
3352 data.space = isl_pw_multi_aff_get_space(pma);
3353 data.pma = pma;
3354 data.res = isl_union_map_alloc(space, umap->table.n);
3355 data.match = match;
3356 data.fn = fn;
3357 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3358 &data) < 0)
3359 data.res = isl_union_map_free(data.res);
3361 isl_space_free(data.space);
3362 isl_union_map_free(umap);
3363 isl_pw_multi_aff_free(pma);
3364 return data.res;
3365 error:
3366 isl_union_map_free(umap);
3367 isl_pw_multi_aff_free(pma);
3368 return NULL;
3371 /* Compute the preimage of the domain of "umap" under the function
3372 * represented by "pma".
3373 * In other words, plug in "pma" in the domain of "umap".
3374 * The result contains maps that live in the same spaces as the maps of "umap"
3375 * with domain space equal to the target space of "pma",
3376 * except that the domain has been replaced by the domain space of "pma".
3378 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3379 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3381 return preimage_pw_multi_aff(umap, pma, &domain_match,
3382 &isl_map_preimage_domain_pw_multi_aff);
3385 /* Compute the preimage of the range of "umap" under the function
3386 * represented by "pma".
3387 * In other words, plug in "pma" in the range of "umap".
3388 * The result contains maps that live in the same spaces as the maps of "umap"
3389 * with range space equal to the target space of "pma",
3390 * except that the range has been replaced by the domain space of "pma".
3392 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3393 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3395 return preimage_pw_multi_aff(umap, pma, &range_match,
3396 &isl_map_preimage_range_pw_multi_aff);
3399 /* Compute the preimage of "uset" under the function represented by "pma".
3400 * In other words, plug in "pma" in "uset".
3401 * The result contains sets that live in the same spaces as the sets of "uset"
3402 * with space equal to the target space of "pma",
3403 * except that the space has been replaced by the domain space of "pma".
3405 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3406 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3408 return preimage_pw_multi_aff(uset, pma, &set_match,
3409 &isl_set_preimage_pw_multi_aff);
3412 /* Compute the preimage of the domain of "umap" under the function
3413 * represented by "ma".
3414 * In other words, plug in "ma" in the domain of "umap".
3415 * The result contains maps that live in the same spaces as the maps of "umap"
3416 * with domain space equal to the target space of "ma",
3417 * except that the domain has been replaced by the domain space of "ma".
3419 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3420 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3422 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3423 isl_pw_multi_aff_from_multi_aff(ma));
3426 /* Compute the preimage of the range of "umap" under the function
3427 * represented by "ma".
3428 * In other words, plug in "ma" in the range of "umap".
3429 * The result contains maps that live in the same spaces as the maps of "umap"
3430 * with range space equal to the target space of "ma",
3431 * except that the range has been replaced by the domain space of "ma".
3433 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3434 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3436 return isl_union_map_preimage_range_pw_multi_aff(umap,
3437 isl_pw_multi_aff_from_multi_aff(ma));
3440 /* Compute the preimage of "uset" under the function represented by "ma".
3441 * In other words, plug in "ma" in "uset".
3442 * The result contains sets that live in the same spaces as the sets of "uset"
3443 * with space equal to the target space of "ma",
3444 * except that the space has been replaced by the domain space of "ma".
3446 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3447 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3449 return isl_union_set_preimage_pw_multi_aff(uset,
3450 isl_pw_multi_aff_from_multi_aff(ma));
3453 /* Internal data structure for preimage_multi_pw_aff.
3455 * "mpa" is the function under which the preimage should be taken.
3456 * "space" is the space of "mpa".
3457 * "res" collects the results.
3458 * "fn" computes the preimage for a given map.
3459 * "match" returns true if "fn" can be called.
3461 struct isl_union_map_preimage_mpa_data {
3462 isl_space *space;
3463 isl_multi_pw_aff *mpa;
3464 isl_union_map *res;
3465 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3466 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3467 __isl_take isl_multi_pw_aff *mpa);
3470 /* Call data->fn to compute the preimage of the domain or range of *entry
3471 * under the function represented by data->mpa, provided the domain/range
3472 * space of *entry matches the target space of data->mpa
3473 * (as given by data->match), and add the result to data->res.
3475 static isl_stat preimage_mpa_entry(void **entry, void *user)
3477 int m;
3478 isl_map *map = *entry;
3479 struct isl_union_map_preimage_mpa_data *data = user;
3480 isl_bool empty;
3482 m = data->match(map, data->space);
3483 if (m < 0)
3484 return isl_stat_error;
3485 if (!m)
3486 return isl_stat_ok;
3488 map = isl_map_copy(map);
3489 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3491 empty = isl_map_is_empty(map);
3492 if (empty < 0 || empty) {
3493 isl_map_free(map);
3494 return empty < 0 ? isl_stat_error : isl_stat_ok;
3497 data->res = isl_union_map_add_map(data->res, map);
3499 return isl_stat_ok;
3502 /* Compute the preimage of the domain or range of "umap" under the function
3503 * represented by "mpa".
3504 * In other words, plug in "mpa" in the domain or range of "umap".
3505 * The function "fn" performs the actual preimage computation on a map,
3506 * while "match" determines to which maps the function should be applied.
3508 static __isl_give isl_union_map *preimage_multi_pw_aff(
3509 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3510 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3511 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3512 __isl_take isl_multi_pw_aff *mpa))
3514 isl_ctx *ctx;
3515 isl_space *space;
3516 struct isl_union_map_preimage_mpa_data data;
3518 umap = isl_union_map_align_params(umap,
3519 isl_multi_pw_aff_get_space(mpa));
3520 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3522 if (!umap || !mpa)
3523 goto error;
3525 ctx = isl_union_map_get_ctx(umap);
3526 space = isl_union_map_get_space(umap);
3527 data.space = isl_multi_pw_aff_get_space(mpa);
3528 data.mpa = mpa;
3529 data.res = isl_union_map_alloc(space, umap->table.n);
3530 data.match = match;
3531 data.fn = fn;
3532 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3533 &data) < 0)
3534 data.res = isl_union_map_free(data.res);
3536 isl_space_free(data.space);
3537 isl_union_map_free(umap);
3538 isl_multi_pw_aff_free(mpa);
3539 return data.res;
3540 error:
3541 isl_union_map_free(umap);
3542 isl_multi_pw_aff_free(mpa);
3543 return NULL;
3546 /* Compute the preimage of the domain of "umap" under the function
3547 * represented by "mpa".
3548 * In other words, plug in "mpa" in the domain of "umap".
3549 * The result contains maps that live in the same spaces as the maps of "umap"
3550 * with domain space equal to the target space of "mpa",
3551 * except that the domain has been replaced by the domain space of "mpa".
3553 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3554 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3556 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3557 &isl_map_preimage_domain_multi_pw_aff);
3560 /* Internal data structure for preimage_upma.
3562 * "umap" is the map of which the preimage should be computed.
3563 * "res" collects the results.
3564 * "fn" computes the preimage for a given piecewise multi-affine function.
3566 struct isl_union_map_preimage_upma_data {
3567 isl_union_map *umap;
3568 isl_union_map *res;
3569 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3570 __isl_take isl_pw_multi_aff *pma);
3573 /* Call data->fn to compute the preimage of the domain or range of data->umap
3574 * under the function represented by pma and add the result to data->res.
3576 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3578 struct isl_union_map_preimage_upma_data *data = user;
3579 isl_union_map *umap;
3581 umap = isl_union_map_copy(data->umap);
3582 umap = data->fn(umap, pma);
3583 data->res = isl_union_map_union(data->res, umap);
3585 return data->res ? isl_stat_ok : isl_stat_error;
3588 /* Compute the preimage of the domain or range of "umap" under the function
3589 * represented by "upma".
3590 * In other words, plug in "upma" in the domain or range of "umap".
3591 * The function "fn" performs the actual preimage computation
3592 * on a piecewise multi-affine function.
3594 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3595 __isl_take isl_union_map *umap,
3596 __isl_take isl_union_pw_multi_aff *upma,
3597 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3598 __isl_take isl_pw_multi_aff *pma))
3600 struct isl_union_map_preimage_upma_data data;
3602 data.umap = umap;
3603 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3604 data.fn = fn;
3605 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3606 &preimage_upma, &data) < 0)
3607 data.res = isl_union_map_free(data.res);
3609 isl_union_map_free(umap);
3610 isl_union_pw_multi_aff_free(upma);
3612 return data.res;
3615 /* Compute the preimage of the domain of "umap" under the function
3616 * represented by "upma".
3617 * In other words, plug in "upma" in the domain of "umap".
3618 * The result contains maps that live in the same spaces as the maps of "umap"
3619 * with domain space equal to one of the target spaces of "upma",
3620 * except that the domain has been replaced by one of the domain spaces that
3621 * correspond to that target space of "upma".
3623 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3624 __isl_take isl_union_map *umap,
3625 __isl_take isl_union_pw_multi_aff *upma)
3627 return preimage_union_pw_multi_aff(umap, upma,
3628 &isl_union_map_preimage_domain_pw_multi_aff);
3631 /* Compute the preimage of the range of "umap" under the function
3632 * represented by "upma".
3633 * In other words, plug in "upma" in the range of "umap".
3634 * The result contains maps that live in the same spaces as the maps of "umap"
3635 * with range space equal to one of the target spaces of "upma",
3636 * except that the range has been replaced by one of the domain spaces that
3637 * correspond to that target space of "upma".
3639 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3640 __isl_take isl_union_map *umap,
3641 __isl_take isl_union_pw_multi_aff *upma)
3643 return preimage_union_pw_multi_aff(umap, upma,
3644 &isl_union_map_preimage_range_pw_multi_aff);
3647 /* Compute the preimage of "uset" under the function represented by "upma".
3648 * In other words, plug in "upma" in the range of "uset".
3649 * The result contains sets that live in the same spaces as the sets of "uset"
3650 * with space equal to one of the target spaces of "upma",
3651 * except that the space has been replaced by one of the domain spaces that
3652 * correspond to that target space of "upma".
3654 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3655 __isl_take isl_union_set *uset,
3656 __isl_take isl_union_pw_multi_aff *upma)
3658 return preimage_union_pw_multi_aff(uset, upma,
3659 &isl_union_set_preimage_pw_multi_aff);
3662 /* Reset the user pointer on all identifiers of parameters and tuples
3663 * of the spaces of "umap".
3665 __isl_give isl_union_map *isl_union_map_reset_user(
3666 __isl_take isl_union_map *umap)
3668 umap = isl_union_map_cow(umap);
3669 if (!umap)
3670 return NULL;
3671 umap->dim = isl_space_reset_user(umap->dim);
3672 if (!umap->dim)
3673 return isl_union_map_free(umap);
3674 return total(umap, &isl_map_reset_user);
3677 /* Reset the user pointer on all identifiers of parameters and tuples
3678 * of the spaces of "uset".
3680 __isl_give isl_union_set *isl_union_set_reset_user(
3681 __isl_take isl_union_set *uset)
3683 return isl_union_map_reset_user(uset);
3686 /* Remove all existentially quantified variables and integer divisions
3687 * from "umap" using Fourier-Motzkin elimination.
3689 __isl_give isl_union_map *isl_union_map_remove_divs(
3690 __isl_take isl_union_map *umap)
3692 return total(umap, &isl_map_remove_divs);
3695 /* Remove all existentially quantified variables and integer divisions
3696 * from "uset" using Fourier-Motzkin elimination.
3698 __isl_give isl_union_set *isl_union_set_remove_divs(
3699 __isl_take isl_union_set *uset)
3701 return isl_union_map_remove_divs(uset);
3704 /* Internal data structure for isl_union_map_project_out.
3705 * "type", "first" and "n" are the arguments for the isl_map_project_out
3706 * call.
3707 * "res" collects the results.
3709 struct isl_union_map_project_out_data {
3710 enum isl_dim_type type;
3711 unsigned first;
3712 unsigned n;
3714 isl_union_map *res;
3717 /* Turn the data->n dimensions of type data->type, starting at data->first
3718 * into existentially quantified variables and add the result to data->res.
3720 static isl_stat project_out(__isl_take isl_map *map, void *user)
3722 struct isl_union_map_project_out_data *data = user;
3724 map = isl_map_project_out(map, data->type, data->first, data->n);
3725 data->res = isl_union_map_add_map(data->res, map);
3727 return isl_stat_ok;
3730 /* Turn the "n" dimensions of type "type", starting at "first"
3731 * into existentially quantified variables.
3732 * Since the space of an isl_union_map only contains parameters,
3733 * type is required to be equal to isl_dim_param.
3735 __isl_give isl_union_map *isl_union_map_project_out(
3736 __isl_take isl_union_map *umap,
3737 enum isl_dim_type type, unsigned first, unsigned n)
3739 isl_space *space;
3740 struct isl_union_map_project_out_data data = { type, first, n };
3742 if (!umap)
3743 return NULL;
3745 if (type != isl_dim_param)
3746 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3747 "can only project out parameters",
3748 return isl_union_map_free(umap));
3750 space = isl_union_map_get_space(umap);
3751 space = isl_space_drop_dims(space, type, first, n);
3752 data.res = isl_union_map_empty(space);
3753 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3754 data.res = isl_union_map_free(data.res);
3756 isl_union_map_free(umap);
3758 return data.res;
3761 /* Project out all parameters from "umap" by existentially quantifying
3762 * over them.
3764 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3765 __isl_take isl_union_map *umap)
3767 isl_size n;
3769 n = isl_union_map_dim(umap, isl_dim_param);
3770 if (n < 0)
3771 return isl_union_map_free(umap);
3772 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3775 /* Turn the "n" dimensions of type "type", starting at "first"
3776 * into existentially quantified variables.
3777 * Since the space of an isl_union_set only contains parameters,
3778 * "type" is required to be equal to isl_dim_param.
3780 __isl_give isl_union_set *isl_union_set_project_out(
3781 __isl_take isl_union_set *uset,
3782 enum isl_dim_type type, unsigned first, unsigned n)
3784 return isl_union_map_project_out(uset, type, first, n);
3787 /* Project out all parameters from "uset" by existentially quantifying
3788 * over them.
3790 __isl_give isl_union_set *isl_union_set_project_out_all_params(
3791 __isl_take isl_union_set *uset)
3793 return uset_from_umap(
3794 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3797 /* Internal data structure for isl_union_map_involves_dims.
3798 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3800 struct isl_union_map_involves_dims_data {
3801 unsigned first;
3802 unsigned n;
3805 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3807 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3809 struct isl_union_map_involves_dims_data *data = user;
3810 isl_bool involves;
3812 involves = isl_map_involves_dims(map,
3813 isl_dim_param, data->first, data->n);
3814 return isl_bool_not(involves);
3817 /* Does "umap" involve any of the n parameters starting at first?
3818 * "type" is required to be set to isl_dim_param.
3820 * "umap" involves any of those parameters if any of its maps
3821 * involve the parameters. In other words, "umap" does not
3822 * involve any of the parameters if all its maps to not
3823 * involve the parameters.
3825 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3826 enum isl_dim_type type, unsigned first, unsigned n)
3828 struct isl_union_map_involves_dims_data data = { first, n };
3829 isl_bool excludes;
3831 if (type != isl_dim_param)
3832 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3833 "can only reference parameters", return isl_bool_error);
3835 excludes = union_map_forall_user(umap, &map_excludes, &data);
3837 return isl_bool_not(excludes);
3840 /* Internal data structure for isl_union_map_reset_range_space.
3841 * "range" is the space from which to set the range space.
3842 * "res" collects the results.
3844 struct isl_union_map_reset_range_space_data {
3845 isl_space *range;
3846 isl_union_map *res;
3849 /* Replace the range space of "map" by the range space of data->range and
3850 * add the result to data->res.
3852 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3854 struct isl_union_map_reset_range_space_data *data = user;
3855 isl_space *space;
3857 space = isl_map_get_space(map);
3858 space = isl_space_domain(space);
3859 space = isl_space_extend_domain_with_range(space,
3860 isl_space_copy(data->range));
3861 map = isl_map_reset_space(map, space);
3862 data->res = isl_union_map_add_map(data->res, map);
3864 return data->res ? isl_stat_ok : isl_stat_error;
3867 /* Replace the range space of all the maps in "umap" by
3868 * the range space of "space".
3870 * This assumes that all maps have the same output dimension.
3871 * This function should therefore not be made publicly available.
3873 * Since the spaces of the maps change, so do their hash value.
3874 * We therefore need to create a new isl_union_map.
3876 __isl_give isl_union_map *isl_union_map_reset_range_space(
3877 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3879 struct isl_union_map_reset_range_space_data data = { space };
3881 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3882 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3883 data.res = isl_union_map_free(data.res);
3885 isl_space_free(space);
3886 isl_union_map_free(umap);
3887 return data.res;
3890 /* Check that "umap" and "space" have the same number of parameters.
3892 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3893 __isl_keep isl_space *space)
3895 isl_size dim1, dim2;
3897 dim1 = isl_union_map_dim(umap, isl_dim_param);
3898 dim2 = isl_space_dim(space, isl_dim_param);
3899 if (dim1 < 0 || dim2 < 0)
3900 return isl_stat_error;
3901 if (dim1 == dim2)
3902 return isl_stat_ok;
3903 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3904 "number of parameters does not match", return isl_stat_error);
3907 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3908 * "space" is the target space.
3909 * "res" collects the results.
3911 struct isl_union_map_reset_params_data {
3912 isl_space *space;
3913 isl_union_map *res;
3916 /* Replace the parameters of "map" by those of data->space and
3917 * add the result to data->res.
3919 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3921 struct isl_union_map_reset_params_data *data = user;
3922 isl_space *space;
3924 space = isl_map_get_space(map);
3925 space = isl_space_replace_params(space, data->space);
3926 map = isl_map_reset_equal_dim_space(map, space);
3927 data->res = isl_union_map_add_map(data->res, map);
3929 return data->res ? isl_stat_ok : isl_stat_error;
3932 /* Replace the space of "umap" by "space", without modifying
3933 * the dimension of "umap", i.e., the number of parameters of "umap".
3935 * Since the hash values of the maps in the union map depend
3936 * on the parameters, a new union map needs to be constructed.
3938 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3939 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3941 struct isl_union_map_reset_params_data data = { space };
3942 isl_bool equal;
3943 isl_space *umap_space;
3945 umap_space = isl_union_map_peek_space(umap);
3946 equal = isl_space_is_equal(umap_space, space);
3947 if (equal < 0)
3948 goto error;
3949 if (equal) {
3950 isl_space_free(space);
3951 return umap;
3953 if (check_union_map_space_equal_dim(umap, space) < 0)
3954 goto error;
3956 data.res = isl_union_map_empty(isl_space_copy(space));
3957 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3958 data.res = isl_union_map_free(data.res);
3960 isl_space_free(space);
3961 isl_union_map_free(umap);
3962 return data.res;
3963 error:
3964 isl_union_map_free(umap);
3965 isl_space_free(space);
3966 return NULL;
3969 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3970 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3971 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3972 * to the domain and the range of each map.
3973 * "res" collects the results.
3975 struct isl_union_order_at_data {
3976 isl_multi_union_pw_aff *mupa;
3977 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3978 __isl_take isl_multi_pw_aff *mpa2);
3979 isl_union_map *res;
3982 /* Intersect "map" with the result of applying data->order to
3983 * the functions in data->mupa that apply to the domain and the range
3984 * of "map" and add the result to data->res.
3986 static isl_stat order_at(__isl_take isl_map *map, void *user)
3988 struct isl_union_order_at_data *data = user;
3989 isl_space *space;
3990 isl_multi_pw_aff *mpa1, *mpa2;
3991 isl_map *order;
3993 space = isl_space_domain(isl_map_get_space(map));
3994 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3995 space = isl_space_range(isl_map_get_space(map));
3996 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3997 order = data->order(mpa1, mpa2);
3998 map = isl_map_intersect(map, order);
3999 data->res = isl_union_map_add_map(data->res, map);
4001 return data->res ? isl_stat_ok : isl_stat_error;
4004 /* If "mupa" has a non-trivial explicit domain, then intersect
4005 * domain and range of "umap" with this explicit domain.
4006 * If the explicit domain only describes constraints on the parameters,
4007 * then the intersection only needs to be performed once.
4009 static __isl_give isl_union_map *intersect_explicit_domain(
4010 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4012 isl_bool non_trivial, is_params;
4013 isl_union_set *dom;
4015 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4016 if (non_trivial < 0)
4017 return isl_union_map_free(umap);
4018 if (!non_trivial)
4019 return umap;
4020 mupa = isl_multi_union_pw_aff_copy(mupa);
4021 dom = isl_multi_union_pw_aff_domain(mupa);
4022 is_params = isl_union_set_is_params(dom);
4023 if (is_params < 0) {
4024 isl_union_set_free(dom);
4025 return isl_union_map_free(umap);
4027 if (is_params) {
4028 isl_set *set;
4030 set = isl_union_set_params(dom);
4031 umap = isl_union_map_intersect_params(umap, set);
4032 return umap;
4034 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4035 umap = isl_union_map_intersect_range(umap, dom);
4036 return umap;
4039 /* Intersect each map in "umap" with the result of calling "order"
4040 * on the functions is "mupa" that apply to the domain and the range
4041 * of the map.
4043 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4044 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4045 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4046 __isl_take isl_multi_pw_aff *mpa2))
4048 struct isl_union_order_at_data data;
4050 umap = isl_union_map_align_params(umap,
4051 isl_multi_union_pw_aff_get_space(mupa));
4052 mupa = isl_multi_union_pw_aff_align_params(mupa,
4053 isl_union_map_get_space(umap));
4054 umap = intersect_explicit_domain(umap, mupa);
4055 data.mupa = mupa;
4056 data.order = order;
4057 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4058 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4059 data.res = isl_union_map_free(data.res);
4061 isl_multi_union_pw_aff_free(mupa);
4062 isl_union_map_free(umap);
4063 return data.res;
4066 /* Return the subset of "umap" where the domain and the range
4067 * have equal "mupa" values.
4069 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4070 __isl_take isl_union_map *umap,
4071 __isl_take isl_multi_union_pw_aff *mupa)
4073 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4074 &isl_multi_pw_aff_eq_map);
4077 /* Return the subset of "umap" where the domain has a lexicographically
4078 * smaller "mupa" value than the range.
4080 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4081 __isl_take isl_union_map *umap,
4082 __isl_take isl_multi_union_pw_aff *mupa)
4084 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4085 &isl_multi_pw_aff_lex_lt_map);
4088 /* Return the subset of "umap" where the domain has a lexicographically
4089 * greater "mupa" value than the range.
4091 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4092 __isl_take isl_union_map *umap,
4093 __isl_take isl_multi_union_pw_aff *mupa)
4095 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4096 &isl_multi_pw_aff_lex_gt_map);
4099 /* Return the union of the elements in the list "list".
4101 __isl_give isl_union_set *isl_union_set_list_union(
4102 __isl_take isl_union_set_list *list)
4104 int i;
4105 isl_size n;
4106 isl_ctx *ctx;
4107 isl_space *space;
4108 isl_union_set *res;
4110 n = isl_union_set_list_n_union_set(list);
4111 if (n < 0)
4112 goto error;
4114 ctx = isl_union_set_list_get_ctx(list);
4115 space = isl_space_params_alloc(ctx, 0);
4116 res = isl_union_set_empty(space);
4118 for (i = 0; i < n; ++i) {
4119 isl_union_set *uset_i;
4121 uset_i = isl_union_set_list_get_union_set(list, i);
4122 res = isl_union_set_union(res, uset_i);
4125 isl_union_set_list_free(list);
4126 return res;
4127 error:
4128 isl_union_set_list_free(list);
4129 return NULL;
4132 /* Update *hash with the hash value of "map".
4134 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4136 uint32_t *hash = user;
4137 uint32_t map_hash;
4139 map_hash = isl_map_get_hash(map);
4140 isl_hash_hash(*hash, map_hash);
4142 isl_map_free(map);
4143 return isl_stat_ok;
4146 /* Return a hash value that digests "umap".
4148 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4150 uint32_t hash;
4152 if (!umap)
4153 return 0;
4155 hash = isl_hash_init();
4156 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4157 return 0;
4159 return hash;
4162 /* Return a hash value that digests "uset".
4164 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4166 return isl_union_map_get_hash(uset);
4169 /* Add the number of basic sets in "set" to "n".
4171 static isl_stat add_n(__isl_take isl_set *set, void *user)
4173 int *n = user;
4174 isl_size set_n;
4176 set_n = isl_set_n_basic_set(set);
4177 *n += set_n;
4178 isl_set_free(set);
4180 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4183 /* Return the total number of basic sets in "uset".
4185 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4187 int n = 0;
4189 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4190 return -1;
4192 return n;
4195 /* Add the basic sets in "set" to "list".
4197 static isl_stat add_list(__isl_take isl_set *set, void *user)
4199 isl_basic_set_list **list = user;
4200 isl_basic_set_list *list_i;
4202 list_i = isl_set_get_basic_set_list(set);
4203 *list = isl_basic_set_list_concat(*list, list_i);
4204 isl_set_free(set);
4206 if (!*list)
4207 return isl_stat_error;
4208 return isl_stat_ok;
4211 /* Return a list containing all the basic sets in "uset".
4213 * First construct a list of the appropriate size and
4214 * then add all the elements.
4216 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4217 __isl_keep isl_union_set *uset)
4219 int n;
4220 isl_ctx *ctx;
4221 isl_basic_set_list *list;
4223 if (!uset)
4224 return NULL;
4225 ctx = isl_union_set_get_ctx(uset);
4226 n = isl_union_set_n_basic_set(uset);
4227 if (n < 0)
4228 return NULL;
4229 list = isl_basic_set_list_alloc(ctx, n);
4230 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4231 list = isl_basic_set_list_free(list);
4233 return list;
4236 /* Internal data structure for isl_union_map_remove_map_if.
4237 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4239 struct isl_union_map_remove_map_if_data {
4240 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4241 void *user;
4244 /* isl_un_op_control filter that negates the result of data->fn
4245 * called on "map".
4247 static isl_bool not(__isl_keep isl_map *map, void *user)
4249 struct isl_union_map_remove_map_if_data *data = user;
4251 return isl_bool_not(data->fn(map, data->user));
4254 /* Dummy isl_un_op_control transformation callback that
4255 * simply returns the input.
4257 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4259 return map;
4262 /* Call "fn" on every map in "umap" and remove those maps
4263 * for which the callback returns true.
4265 * Use un_op to keep only those maps that are not filtered out,
4266 * applying an identity transformation on them.
4268 __isl_give isl_union_map *isl_union_map_remove_map_if(
4269 __isl_take isl_union_map *umap,
4270 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4272 struct isl_union_map_remove_map_if_data data = { fn, user };
4273 struct isl_un_op_control control = {
4274 .filter = &not,
4275 .filter_user = &data,
4276 .fn_map = &map_id,
4278 return un_op(umap, &control);