add exported isl_multi_aff_involves_locals
[isl.git] / isl_union_map.c
blobde8663284ed280552f4380d72154b4e3bfe9a875
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>
26 #include <isl_id_private.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
31 #include <uset_to_umap.c>
32 #include <uset_from_umap.c>
33 #include <set_list_from_map_list_inl.c>
35 /* Return the number of parameters of "umap", where "type"
36 * is required to be set to isl_dim_param.
38 isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
39 enum isl_dim_type type)
41 if (!umap)
42 return isl_size_error;
44 if (type != isl_dim_param)
45 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
46 "can only reference parameters", return isl_size_error);
48 return isl_space_dim(umap->dim, type);
51 /* Return the number of parameters of "uset", where "type"
52 * is required to be set to isl_dim_param.
54 isl_size isl_union_set_dim(__isl_keep isl_union_set *uset,
55 enum isl_dim_type type)
57 return isl_union_map_dim(uset, type);
60 /* Return the id of the specified dimension.
62 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
63 enum isl_dim_type type, unsigned pos)
65 if (!umap)
66 return NULL;
68 if (type != isl_dim_param)
69 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
70 "can only reference parameters", return NULL);
72 return isl_space_get_dim_id(umap->dim, type, pos);
75 /* Is this union set a parameter domain?
77 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
79 isl_set *set;
80 isl_bool params;
82 if (!uset)
83 return isl_bool_error;
84 if (uset->table.n != 1)
85 return isl_bool_false;
87 set = isl_set_from_union_set(isl_union_set_copy(uset));
88 params = isl_set_is_params(set);
89 isl_set_free(set);
90 return params;
93 /* Is this union map actually a parameter domain?
94 * Users should never call this function. Outside of isl,
95 * a union map can never be a parameter domain.
97 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
99 return isl_union_set_is_params(uset_from_umap(umap));
102 static __isl_give isl_union_map *isl_union_map_alloc(
103 __isl_take isl_space *space, int size)
105 isl_union_map *umap;
107 space = isl_space_params(space);
108 if (!space)
109 return NULL;
111 umap = isl_calloc_type(space->ctx, isl_union_map);
112 if (!umap) {
113 isl_space_free(space);
114 return NULL;
117 umap->ref = 1;
118 umap->dim = space;
119 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
120 return isl_union_map_free(umap);
122 return umap;
125 /* Create an empty union map without specifying any parameters.
127 __isl_give isl_union_map *isl_union_map_empty_ctx(isl_ctx *ctx)
129 return isl_union_map_empty_space(isl_space_unit(ctx));
132 __isl_give isl_union_map *isl_union_map_empty_space(__isl_take isl_space *space)
134 return isl_union_map_alloc(space, 16);
137 /* This is an alternative name for the function above.
139 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
141 return isl_union_map_empty_space(space);
144 /* Create an empty union set without specifying any parameters.
146 __isl_give isl_union_set *isl_union_set_empty_ctx(isl_ctx *ctx)
148 return uset_from_umap(isl_union_map_empty_ctx(ctx));
151 __isl_give isl_union_set *isl_union_set_empty_space(__isl_take isl_space *space)
153 return uset_from_umap(isl_union_map_empty_space(space));
156 /* This is an alternative name for the function above.
158 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
160 return isl_union_set_empty_space(space);
163 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
165 return umap ? umap->dim->ctx : NULL;
168 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
170 return uset ? uset->dim->ctx : NULL;
173 /* Return the space of "umap".
175 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
177 return umap ? umap->dim : NULL;
180 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
182 return isl_space_copy(isl_union_map_peek_space(umap));
185 /* Return the position of the parameter with the given name
186 * in "umap".
187 * Return -1 if no such dimension can be found.
189 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
190 enum isl_dim_type type, const char *name)
192 if (!umap)
193 return -1;
194 return isl_space_find_dim_by_name(umap->dim, type, name);
197 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
199 return isl_union_map_get_space(uset);
202 static isl_stat free_umap_entry(void **entry, void *user)
204 isl_map *map = *entry;
205 isl_map_free(map);
206 return isl_stat_ok;
209 static isl_stat add_map(__isl_take isl_map *map, void *user)
211 isl_union_map **umap = (isl_union_map **)user;
213 *umap = isl_union_map_add_map(*umap, map);
215 return isl_stat_ok;
218 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
220 isl_union_map *dup;
222 if (!umap)
223 return NULL;
225 dup = isl_union_map_empty(isl_space_copy(umap->dim));
226 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
227 goto error;
228 return dup;
229 error:
230 isl_union_map_free(dup);
231 return NULL;
234 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
236 if (!umap)
237 return NULL;
239 if (umap->ref == 1)
240 return umap;
241 umap->ref--;
242 return isl_union_map_dup(umap);
245 struct isl_union_align {
246 isl_reordering *exp;
247 isl_union_map *res;
250 static isl_stat align_entry(void **entry, void *user)
252 isl_map *map = *entry;
253 isl_reordering *exp;
254 struct isl_union_align *data = user;
256 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
257 isl_map_get_space(map));
259 data->res = isl_union_map_add_map(data->res,
260 isl_map_realign(isl_map_copy(map), exp));
262 return isl_stat_ok;
265 /* Align the parameters of umap along those of model.
266 * The result has the parameters of model first, in the same order
267 * as they appear in model, followed by any remaining parameters of
268 * umap that do not appear in model.
270 __isl_give isl_union_map *isl_union_map_align_params(
271 __isl_take isl_union_map *umap, __isl_take isl_space *model)
273 struct isl_union_align data = { NULL, NULL };
274 isl_bool equal_params;
276 if (!umap || !model)
277 goto error;
279 equal_params = isl_space_has_equal_params(umap->dim, model);
280 if (equal_params < 0)
281 goto error;
282 if (equal_params) {
283 isl_space_free(model);
284 return umap;
287 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
288 if (!data.exp)
289 goto error;
291 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
292 umap->table.n);
293 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
294 &align_entry, &data) < 0)
295 goto error;
297 isl_reordering_free(data.exp);
298 isl_union_map_free(umap);
299 isl_space_free(model);
300 return data.res;
301 error:
302 isl_reordering_free(data.exp);
303 isl_union_map_free(umap);
304 isl_union_map_free(data.res);
305 isl_space_free(model);
306 return NULL;
309 __isl_give isl_union_set *isl_union_set_align_params(
310 __isl_take isl_union_set *uset, __isl_take isl_space *model)
312 return isl_union_map_align_params(uset, model);
315 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
316 __isl_take isl_union_map *umap2)
318 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
319 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
321 umap1 = isl_union_map_cow(umap1);
323 if (!umap1 || !umap2)
324 goto error;
326 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
327 goto error;
329 isl_union_map_free(umap2);
331 return umap1;
332 error:
333 isl_union_map_free(umap1);
334 isl_union_map_free(umap2);
335 return NULL;
338 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
339 __isl_take isl_union_set *uset2)
341 return isl_union_map_union(uset1, uset2);
344 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
346 if (!umap)
347 return NULL;
349 umap->ref++;
350 return umap;
353 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
355 return isl_union_map_copy(uset);
358 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
360 if (!umap)
361 return NULL;
363 if (--umap->ref > 0)
364 return NULL;
366 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
367 &free_umap_entry, NULL);
368 isl_hash_table_clear(&umap->table);
369 isl_space_free(umap->dim);
370 free(umap);
371 return NULL;
374 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
376 return isl_union_map_free(uset);
379 /* Do "umap" and "space" have the same parameters?
381 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
382 __isl_keep isl_space *space)
384 isl_space *umap_space;
386 umap_space = isl_union_map_peek_space(umap);
387 return isl_space_has_equal_params(umap_space, space);
390 /* Do "uset" and "space" have the same parameters?
392 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
393 __isl_keep isl_space *space)
395 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
398 static int has_space(const void *entry, const void *val)
400 isl_map *map = (isl_map *)entry;
401 isl_space *space = (isl_space *) val;
403 return isl_space_is_equal(map->dim, space);
406 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
407 __isl_take isl_map *map)
409 uint32_t hash;
410 struct isl_hash_table_entry *entry;
411 isl_bool aligned;
413 if (!map || !umap)
414 goto error;
416 if (isl_map_plain_is_empty(map)) {
417 isl_map_free(map);
418 return umap;
421 aligned = isl_map_space_has_equal_params(map, umap->dim);
422 if (aligned < 0)
423 goto error;
424 if (!aligned) {
425 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
426 map = isl_map_align_params(map, isl_union_map_get_space(umap));
429 umap = isl_union_map_cow(umap);
431 if (!map || !umap)
432 goto error;
434 hash = isl_space_get_hash(map->dim);
435 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
436 &has_space, map->dim, 1);
437 if (!entry)
438 goto error;
440 if (!entry->data)
441 entry->data = map;
442 else {
443 entry->data = isl_map_union(entry->data, isl_map_copy(map));
444 if (!entry->data)
445 goto error;
446 isl_map_free(map);
449 return umap;
450 error:
451 isl_map_free(map);
452 isl_union_map_free(umap);
453 return NULL;
456 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
457 __isl_take isl_set *set)
459 return isl_union_map_add_map(uset, set_to_map(set));
462 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
464 isl_space *dim;
465 isl_union_map *umap;
467 if (!map)
468 return NULL;
470 dim = isl_map_get_space(map);
471 dim = isl_space_params(dim);
472 umap = isl_union_map_empty(dim);
473 umap = isl_union_map_add_map(umap, map);
475 return umap;
478 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
480 return isl_union_map_from_map(set_to_map(set));
483 __isl_give isl_union_map *isl_union_map_from_basic_map(
484 __isl_take isl_basic_map *bmap)
486 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
489 __isl_give isl_union_set *isl_union_set_from_basic_set(
490 __isl_take isl_basic_set *bset)
492 return isl_union_map_from_basic_map(bset);
495 struct isl_union_map_foreach_data
497 isl_stat (*fn)(__isl_take isl_map *map, void *user);
498 void *user;
501 static isl_stat call_on_copy(void **entry, void *user)
503 isl_map *map = *entry;
504 struct isl_union_map_foreach_data *data;
505 data = (struct isl_union_map_foreach_data *)user;
507 return data->fn(isl_map_copy(map), data->user);
510 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
512 return umap ? umap->table.n : isl_size_error;
515 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
517 return uset ? uset->table.n : isl_size_error;
520 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
521 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
523 struct isl_union_map_foreach_data data = { fn, user };
525 if (!umap)
526 return isl_stat_error;
528 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
529 &call_on_copy, &data);
532 /* Internal data structure for isl_union_map_every_map.
534 * "test" is the user-specified callback function.
535 * "user" is the user-specified callback function argument.
537 * "failed" is initialized to 0 and set to 1 if "test" fails
538 * on any map.
540 struct isl_union_map_every_data {
541 isl_bool (*test)(__isl_keep isl_map *map, void *user);
542 void *user;
543 int failed;
546 /* Call data->test on "map".
547 * If this fails, then set data->failed and abort.
549 static isl_stat call_every(void **entry, void *user)
551 isl_map *map = *entry;
552 struct isl_union_map_every_data *data = user;
553 isl_bool r;
555 r = data->test(map, data->user);
556 if (r < 0)
557 return isl_stat_error;
558 if (r)
559 return isl_stat_ok;
560 data->failed = 1;
561 return isl_stat_error;
564 /* Does "test" succeed on every map in "umap"?
566 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
567 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
569 struct isl_union_map_every_data data = { test, user, 0 };
570 isl_stat r;
572 if (!umap)
573 return isl_bool_error;
575 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
576 &call_every, &data);
577 if (r >= 0)
578 return isl_bool_true;
579 if (data.failed)
580 return isl_bool_false;
581 return isl_bool_error;
584 /* Add "map" to "list".
586 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
588 isl_map_list **list = user;
590 *list = isl_map_list_add(*list, map);
592 if (!*list)
593 return isl_stat_error;
594 return isl_stat_ok;
597 /* Return the maps in "umap" as a list.
599 * First construct a list of the appropriate size and then add all the
600 * elements.
602 __isl_give isl_map_list *isl_union_map_get_map_list(
603 __isl_keep isl_union_map *umap)
605 isl_size n_maps;
606 isl_ctx *ctx;
607 isl_map_list *list;
609 n_maps = isl_union_map_n_map(umap);
610 if (n_maps < 0)
611 return NULL;
612 ctx = isl_union_map_get_ctx(umap);
613 list = isl_map_list_alloc(ctx, n_maps);
615 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
616 list = isl_map_list_free(list);
618 return list;
621 /* Return the sets in "uset" as a list.
623 __isl_give isl_set_list *isl_union_set_get_set_list(
624 __isl_keep isl_union_set *uset)
626 return set_list_from_map_list(
627 isl_union_map_get_map_list(uset_to_umap(uset)));
630 /* Can "umap" be converted to an isl_map?
631 * That is, does it contain elements in exactly one space?
633 isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
635 isl_size n;
637 n = isl_union_map_n_map(umap);
638 if (n < 0)
639 return isl_bool_error;
640 return isl_bool_ok(n == 1);
643 /* Can "uset" be converted to an isl_set?
644 * That is, does it contain elements in exactly one space?
646 isl_bool isl_union_set_isa_set(__isl_keep isl_union_set *uset)
648 return isl_union_map_isa_map(uset_to_umap(uset));
651 static isl_stat copy_map(void **entry, void *user)
653 isl_map *map = *entry;
654 isl_map **map_p = user;
656 *map_p = isl_map_copy(map);
658 return isl_stat_error;
661 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
663 isl_bool is_map;
664 isl_ctx *ctx;
665 isl_map *map = NULL;
667 is_map = isl_union_map_isa_map(umap);
668 if (is_map < 0)
669 goto error;
670 ctx = isl_union_map_get_ctx(umap);
671 if (!is_map)
672 isl_die(ctx, isl_error_invalid,
673 "union map needs to contain elements in exactly "
674 "one space", goto error);
676 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
678 isl_union_map_free(umap);
680 return map;
681 error:
682 isl_union_map_free(umap);
683 return NULL;
686 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
688 return isl_map_from_union_map(uset);
691 /* Extract the map in "umap" that lives in the given space (ignoring
692 * parameters).
694 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
695 __isl_take isl_space *space)
697 uint32_t hash;
698 struct isl_hash_table_entry *entry;
700 space = isl_space_drop_all_params(space);
701 space = isl_space_align_params(space, isl_union_map_get_space(umap));
702 if (!umap || !space)
703 goto error;
705 hash = isl_space_get_hash(space);
706 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
707 &has_space, space, 0);
708 if (!entry)
709 return isl_map_empty(space);
710 isl_space_free(space);
711 return isl_map_copy(entry->data);
712 error:
713 isl_space_free(space);
714 return NULL;
717 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
718 __isl_take isl_space *dim)
720 return set_from_map(isl_union_map_extract_map(uset, dim));
723 /* Check if umap contains a map in the given space.
725 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
726 __isl_keep isl_space *space)
728 uint32_t hash;
729 struct isl_hash_table_entry *entry;
731 if (!umap || !space)
732 return isl_bool_error;
734 hash = isl_space_get_hash(space);
735 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
736 &has_space, space, 0);
737 return !!entry;
740 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
741 __isl_keep isl_space *space)
743 return isl_union_map_contains(uset, space);
746 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
747 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
749 return isl_union_map_foreach_map(uset,
750 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
753 /* Internal data structure for isl_union_set_every_set.
755 * "test" is the user-specified callback function.
756 * "user" is the user-specified callback function argument.
758 struct isl_test_set_from_map_data {
759 isl_bool (*test)(__isl_keep isl_set *set, void *user);
760 void *user;
763 /* Call data->test on "map", which is part of an isl_union_set and
764 * therefore known to be an isl_set.
766 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
768 struct isl_test_set_from_map_data *data = user;
770 return data->test(set_from_map(map), data->user);
773 /* Does "test" succeed on every set in "uset"?
775 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
776 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
778 struct isl_test_set_from_map_data data = { test, user };
780 return isl_union_map_every_map(uset_to_umap(uset),
781 &test_set_from_map, &data);
784 struct isl_union_set_foreach_point_data {
785 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
786 void *user;
789 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
791 struct isl_union_set_foreach_point_data *data = user;
792 isl_stat r;
794 r = isl_set_foreach_point(set, data->fn, data->user);
795 isl_set_free(set);
797 return r;
800 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
801 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
803 struct isl_union_set_foreach_point_data data = { fn, user };
804 return isl_union_set_foreach_set(uset, &foreach_point, &data);
807 /* Data structure that specifies how gen_bin_op should
808 * construct results from the inputs.
810 * If "subtract" is set, then a map in the first input is copied to the result
811 * if there is no corresponding map in the second input.
812 * Otherwise, a map in the first input with no corresponding map
813 * in the second input is ignored.
814 * If "filter" is not NULL, then it specifies which maps in the first
815 * input may have a matching map in the second input.
816 * In particular, it makes sure that "match_space" can be called
817 * on the space of the map.
818 * "match_space" specifies how to transform the space of a map
819 * in the first input to the space of the corresponding map
820 * in the second input.
821 * "fn_map" specifies how the matching maps, one from each input,
822 * should be combined to form a map in the result.
824 struct isl_bin_op_control {
825 int subtract;
826 isl_bool (*filter)(__isl_keep isl_map *map);
827 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
828 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
829 __isl_take isl_map *map2);
832 /* Internal data structure for gen_bin_op.
833 * "control" specifies how the maps in the result should be constructed.
834 * "umap2" is a pointer to the second argument.
835 * "res" collects the results.
837 struct isl_union_map_gen_bin_data {
838 struct isl_bin_op_control *control;
839 isl_union_map *umap2;
840 isl_union_map *res;
843 /* Add a copy of "map" to "res" and return the result.
845 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
846 __isl_keep isl_map *map)
848 return isl_union_map_add_map(res, isl_map_copy(map));
851 /* Combine "map1" and "map2", add the result to "res" and return the result.
852 * Check whether the result is empty before adding it to "res".
854 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
855 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
856 struct isl_union_map_gen_bin_data *data)
858 isl_bool empty;
859 isl_map *map;
861 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
862 empty = isl_map_is_empty(map);
863 if (empty < 0 || empty) {
864 isl_map_free(map);
865 if (empty < 0)
866 return isl_union_map_free(res);
867 return res;
869 return isl_union_map_add_map(res, map);
872 /* Dummy match_space function that simply returns the input space.
874 static __isl_give isl_space *identity(__isl_take isl_space *space)
876 return space;
879 /* Look for the map in data->umap2 that corresponds to "map", if any.
880 * Return (isl_bool_true, matching map) if there is one,
881 * (isl_bool_false, NULL) if there is no matching map and
882 * (isl_bool_error, NULL) on error.
884 * If not NULL, then data->control->filter specifies whether "map"
885 * can have any matching map. If so,
886 * data->control->match_space specifies which map in data->umap2
887 * corresponds to "map".
889 static __isl_keep isl_maybe_isl_map bin_try_get_match(
890 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
892 uint32_t hash;
893 struct isl_hash_table_entry *entry2;
894 isl_space *space;
895 isl_maybe_isl_map res = { isl_bool_error, NULL };
897 if (data->control->filter) {
898 res.valid = data->control->filter(map);
899 if (res.valid < 0 || !res.valid)
900 return res;
901 res.valid = isl_bool_error;
904 space = isl_map_get_space(map);
905 if (data->control->match_space != &identity)
906 space = data->control->match_space(space);
907 if (!space)
908 return res;
909 hash = isl_space_get_hash(space);
910 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
911 &data->umap2->table, hash,
912 &has_space, space, 0);
913 isl_space_free(space);
914 res.valid = entry2 != NULL;
915 if (entry2)
916 res.value = entry2->data;
918 return res;
921 /* isl_hash_table_foreach callback for gen_bin_op.
922 * Look for the map in data->umap2 that corresponds
923 * to the map that "entry" points to, apply the binary operation and
924 * add the result to data->res.
926 * If no corresponding map can be found, then the effect depends
927 * on data->control->subtract. If it is set, then the current map
928 * is added directly to the result. Otherwise, it is ignored.
930 static isl_stat gen_bin_entry(void **entry, void *user)
932 struct isl_union_map_gen_bin_data *data = user;
933 isl_map *map = *entry;
934 isl_maybe_isl_map m;
936 m = bin_try_get_match(data, map);
937 if (m.valid < 0)
938 return isl_stat_error;
939 if (!m.valid && !data->control->subtract)
940 return isl_stat_ok;
942 if (!m.valid)
943 data->res = bin_add_map(data->res, map);
944 else
945 data->res = bin_add_pair(data->res, map, m.value, data);
946 if (!data->res)
947 return isl_stat_error;
949 return isl_stat_ok;
952 /* Apply a binary operation to "umap1" and "umap2" based on "control".
953 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
954 * in gen_bin_entry.
956 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
957 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
959 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
961 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
962 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
964 if (!umap1 || !umap2)
965 goto error;
967 data.umap2 = umap2;
968 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
969 umap1->table.n);
970 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
971 &gen_bin_entry, &data) < 0)
972 goto error;
974 isl_union_map_free(umap1);
975 isl_union_map_free(umap2);
976 return data.res;
977 error:
978 isl_union_map_free(umap1);
979 isl_union_map_free(umap2);
980 isl_union_map_free(data.res);
981 return NULL;
984 __isl_give isl_union_map *isl_union_map_subtract(
985 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
987 struct isl_bin_op_control control = {
988 .subtract = 1,
989 .match_space = &identity,
990 .fn_map = &isl_map_subtract,
993 return gen_bin_op(umap1, umap2, &control);
996 __isl_give isl_union_set *isl_union_set_subtract(
997 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
999 return isl_union_map_subtract(uset1, uset2);
1002 struct isl_union_map_gen_bin_set_data {
1003 isl_set *set;
1004 isl_union_map *res;
1007 static isl_stat intersect_params_entry(void **entry, void *user)
1009 struct isl_union_map_gen_bin_set_data *data = user;
1010 isl_map *map = *entry;
1011 int empty;
1013 map = isl_map_copy(map);
1014 map = isl_map_intersect_params(map, isl_set_copy(data->set));
1016 empty = isl_map_is_empty(map);
1017 if (empty < 0) {
1018 isl_map_free(map);
1019 return isl_stat_error;
1022 data->res = isl_union_map_add_map(data->res, map);
1024 return isl_stat_ok;
1027 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
1028 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
1030 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
1032 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1033 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1035 if (!umap || !set)
1036 goto error;
1038 data.set = set;
1039 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1040 umap->table.n);
1041 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1042 fn, &data) < 0)
1043 goto error;
1045 isl_union_map_free(umap);
1046 isl_set_free(set);
1047 return data.res;
1048 error:
1049 isl_union_map_free(umap);
1050 isl_set_free(set);
1051 isl_union_map_free(data.res);
1052 return NULL;
1055 /* Intersect "umap" with the parameter domain "set".
1057 * If "set" does not have any constraints, then we can return immediately.
1059 __isl_give isl_union_map *isl_union_map_intersect_params(
1060 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1062 int is_universe;
1064 is_universe = isl_set_plain_is_universe(set);
1065 if (is_universe < 0)
1066 goto error;
1067 if (is_universe) {
1068 isl_set_free(set);
1069 return umap;
1072 return gen_bin_set_op(umap, set, &intersect_params_entry);
1073 error:
1074 isl_union_map_free(umap);
1075 isl_set_free(set);
1076 return NULL;
1079 __isl_give isl_union_set *isl_union_set_intersect_params(
1080 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1082 return isl_union_map_intersect_params(uset, set);
1085 static __isl_give isl_union_map *union_map_intersect_params(
1086 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1088 return isl_union_map_intersect_params(umap,
1089 isl_set_from_union_set(uset));
1092 static __isl_give isl_union_map *union_map_gist_params(
1093 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1095 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1098 struct isl_union_map_match_bin_data {
1099 isl_union_map *umap2;
1100 isl_union_map *res;
1101 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1104 static isl_stat match_bin_entry(void **entry, void *user)
1106 struct isl_union_map_match_bin_data *data = user;
1107 uint32_t hash;
1108 struct isl_hash_table_entry *entry2;
1109 isl_map *map = *entry;
1110 int empty;
1112 hash = isl_space_get_hash(map->dim);
1113 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1114 hash, &has_space, map->dim, 0);
1115 if (!entry2)
1116 return isl_stat_ok;
1118 map = isl_map_copy(map);
1119 map = data->fn(map, isl_map_copy(entry2->data));
1121 empty = isl_map_is_empty(map);
1122 if (empty < 0) {
1123 isl_map_free(map);
1124 return isl_stat_error;
1126 if (empty) {
1127 isl_map_free(map);
1128 return isl_stat_ok;
1131 data->res = isl_union_map_add_map(data->res, map);
1133 return isl_stat_ok;
1136 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1137 __isl_take isl_union_map *umap2,
1138 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1140 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1142 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1143 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1145 if (!umap1 || !umap2)
1146 goto error;
1148 data.umap2 = umap2;
1149 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1150 umap1->table.n);
1151 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1152 &match_bin_entry, &data) < 0)
1153 goto error;
1155 isl_union_map_free(umap1);
1156 isl_union_map_free(umap2);
1157 return data.res;
1158 error:
1159 isl_union_map_free(umap1);
1160 isl_union_map_free(umap2);
1161 isl_union_map_free(data.res);
1162 return NULL;
1165 __isl_give isl_union_map *isl_union_map_intersect(
1166 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1168 return match_bin_op(umap1, umap2, &isl_map_intersect);
1171 /* Compute the intersection of the two union_sets.
1172 * As a special case, if exactly one of the two union_sets
1173 * is a parameter domain, then intersect the parameter domain
1174 * of the other one with this set.
1176 __isl_give isl_union_set *isl_union_set_intersect(
1177 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1179 int p1, p2;
1181 p1 = isl_union_set_is_params(uset1);
1182 p2 = isl_union_set_is_params(uset2);
1183 if (p1 < 0 || p2 < 0)
1184 goto error;
1185 if (!p1 && p2)
1186 return union_map_intersect_params(uset1, uset2);
1187 if (p1 && !p2)
1188 return union_map_intersect_params(uset2, uset1);
1189 return isl_union_map_intersect(uset1, uset2);
1190 error:
1191 isl_union_set_free(uset1);
1192 isl_union_set_free(uset2);
1193 return NULL;
1196 static isl_stat gist_params_entry(void **entry, void *user)
1198 struct isl_union_map_gen_bin_set_data *data = user;
1199 isl_map *map = *entry;
1200 int empty;
1202 map = isl_map_copy(map);
1203 map = isl_map_gist_params(map, isl_set_copy(data->set));
1205 empty = isl_map_is_empty(map);
1206 if (empty < 0) {
1207 isl_map_free(map);
1208 return isl_stat_error;
1211 data->res = isl_union_map_add_map(data->res, map);
1213 return isl_stat_ok;
1216 __isl_give isl_union_map *isl_union_map_gist_params(
1217 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1219 return gen_bin_set_op(umap, set, &gist_params_entry);
1222 __isl_give isl_union_set *isl_union_set_gist_params(
1223 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1225 return isl_union_map_gist_params(uset, set);
1228 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1229 __isl_take isl_union_map *context)
1231 return match_bin_op(umap, context, &isl_map_gist);
1234 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1235 __isl_take isl_union_set *context)
1237 if (isl_union_set_is_params(context))
1238 return union_map_gist_params(uset, context);
1239 return isl_union_map_gist(uset, context);
1242 /* For each map in "umap", remove the constraints in the corresponding map
1243 * of "context".
1244 * Each map in "context" is assumed to consist of a single disjunct and
1245 * to have explicit representations for all local variables.
1247 __isl_give isl_union_map *isl_union_map_plain_gist(
1248 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1250 return match_bin_op(umap, context, &isl_map_plain_gist);
1253 /* For each set in "uset", remove the constraints in the corresponding set
1254 * of "context".
1255 * Each set in "context" is assumed to consist of a single disjunct and
1256 * to have explicit representations for all local variables.
1258 __isl_give isl_union_set *isl_union_set_plain_gist(
1259 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1261 return isl_union_map_plain_gist(uset, context);
1264 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1265 __isl_take isl_map *set2)
1267 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1270 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1271 __isl_take isl_map *set2)
1273 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1276 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1277 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1279 return match_bin_op(uset1, uset2, &lex_lt_set);
1282 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1283 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1285 return match_bin_op(uset1, uset2, &lex_le_set);
1288 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1289 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1291 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1294 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1295 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1297 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1300 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1301 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1303 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1306 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1307 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1309 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1312 /* Intersect the domain of "umap" with "uset".
1314 static __isl_give isl_union_map *union_map_intersect_domain(
1315 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1317 struct isl_bin_op_control control = {
1318 .match_space = &isl_space_domain,
1319 .fn_map = &isl_map_intersect_domain,
1322 return gen_bin_op(umap, uset, &control);
1325 /* Intersect the domain of "umap" with "uset".
1326 * If "uset" is a parameters domain, then intersect the parameter
1327 * domain of "umap" with this set.
1329 __isl_give isl_union_map *isl_union_map_intersect_domain(
1330 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1332 if (isl_union_set_is_params(uset))
1333 return union_map_intersect_params(umap, uset);
1334 else
1335 return union_map_intersect_domain(umap, uset);
1338 /* Remove the elements of "uset" from the domain of "umap".
1340 __isl_give isl_union_map *isl_union_map_subtract_domain(
1341 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1343 struct isl_bin_op_control control = {
1344 .subtract = 1,
1345 .match_space = &isl_space_domain,
1346 .fn_map = &isl_map_subtract_domain,
1349 return gen_bin_op(umap, dom, &control);
1352 /* Remove the elements of "uset" from the range of "umap".
1354 __isl_give isl_union_map *isl_union_map_subtract_range(
1355 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1357 struct isl_bin_op_control control = {
1358 .subtract = 1,
1359 .match_space = &isl_space_range,
1360 .fn_map = &isl_map_subtract_range,
1363 return gen_bin_op(umap, dom, &control);
1366 /* Compute the gist of "umap" with respect to the domain "uset".
1368 static __isl_give isl_union_map *union_map_gist_domain(
1369 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1371 struct isl_bin_op_control control = {
1372 .match_space = &isl_space_domain,
1373 .fn_map = &isl_map_gist_domain,
1376 return gen_bin_op(umap, uset, &control);
1379 /* Compute the gist of "umap" with respect to the domain "uset".
1380 * If "uset" is a parameters domain, then compute the gist
1381 * with respect to this parameter domain.
1383 __isl_give isl_union_map *isl_union_map_gist_domain(
1384 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1386 if (isl_union_set_is_params(uset))
1387 return union_map_gist_params(umap, uset);
1388 else
1389 return union_map_gist_domain(umap, uset);
1392 /* Compute the gist of "umap" with respect to the range "uset".
1394 __isl_give isl_union_map *isl_union_map_gist_range(
1395 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1397 struct isl_bin_op_control control = {
1398 .match_space = &isl_space_range,
1399 .fn_map = &isl_map_gist_range,
1402 return gen_bin_op(umap, uset, &control);
1405 __isl_give isl_union_map *isl_union_map_intersect_range(
1406 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1408 struct isl_bin_op_control control = {
1409 .match_space = &isl_space_range,
1410 .fn_map = &isl_map_intersect_range,
1413 return gen_bin_op(umap, uset, &control);
1416 /* Intersect each map in "umap" in a space A -> [B -> C]
1417 * with the corresponding map in "factor" in the space A -> C and
1418 * collect the results.
1420 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1421 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1423 struct isl_bin_op_control control = {
1424 .filter = &isl_map_range_is_wrapping,
1425 .match_space = &isl_space_range_factor_range,
1426 .fn_map = &isl_map_intersect_range_factor_range,
1429 return gen_bin_op(umap, factor, &control);
1432 struct isl_union_map_bin_data {
1433 isl_union_map *umap2;
1434 isl_union_map *res;
1435 isl_map *map;
1436 isl_stat (*fn)(void **entry, void *user);
1439 static isl_stat apply_range_entry(void **entry, void *user)
1441 struct isl_union_map_bin_data *data = user;
1442 isl_map *map2 = *entry;
1443 isl_bool empty;
1445 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1446 map2->dim, isl_dim_in))
1447 return isl_stat_ok;
1449 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1451 empty = isl_map_is_empty(map2);
1452 if (empty < 0) {
1453 isl_map_free(map2);
1454 return isl_stat_error;
1456 if (empty) {
1457 isl_map_free(map2);
1458 return isl_stat_ok;
1461 data->res = isl_union_map_add_map(data->res, map2);
1463 return isl_stat_ok;
1466 static isl_stat bin_entry(void **entry, void *user)
1468 struct isl_union_map_bin_data *data = user;
1469 isl_map *map = *entry;
1471 data->map = map;
1472 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1473 data->fn, data) < 0)
1474 return isl_stat_error;
1476 return isl_stat_ok;
1479 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1480 __isl_take isl_union_map *umap2,
1481 isl_stat (*fn)(void **entry, void *user))
1483 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1485 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1486 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1488 if (!umap1 || !umap2)
1489 goto error;
1491 data.umap2 = umap2;
1492 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1493 umap1->table.n);
1494 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1495 &bin_entry, &data) < 0)
1496 goto error;
1498 isl_union_map_free(umap1);
1499 isl_union_map_free(umap2);
1500 return data.res;
1501 error:
1502 isl_union_map_free(umap1);
1503 isl_union_map_free(umap2);
1504 isl_union_map_free(data.res);
1505 return NULL;
1508 __isl_give isl_union_map *isl_union_map_apply_range(
1509 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1511 return bin_op(umap1, umap2, &apply_range_entry);
1514 __isl_give isl_union_map *isl_union_map_apply_domain(
1515 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1517 umap1 = isl_union_map_reverse(umap1);
1518 umap1 = isl_union_map_apply_range(umap1, umap2);
1519 return isl_union_map_reverse(umap1);
1522 __isl_give isl_union_set *isl_union_set_apply(
1523 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1525 return isl_union_map_apply_range(uset, umap);
1528 static isl_stat map_lex_lt_entry(void **entry, void *user)
1530 struct isl_union_map_bin_data *data = user;
1531 isl_map *map2 = *entry;
1533 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1534 map2->dim, isl_dim_out))
1535 return isl_stat_ok;
1537 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1539 data->res = isl_union_map_add_map(data->res, map2);
1541 return isl_stat_ok;
1544 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1545 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1547 return bin_op(umap1, umap2, &map_lex_lt_entry);
1550 static isl_stat map_lex_le_entry(void **entry, void *user)
1552 struct isl_union_map_bin_data *data = user;
1553 isl_map *map2 = *entry;
1555 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1556 map2->dim, isl_dim_out))
1557 return isl_stat_ok;
1559 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1561 data->res = isl_union_map_add_map(data->res, map2);
1563 return isl_stat_ok;
1566 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1567 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1569 return bin_op(umap1, umap2, &map_lex_le_entry);
1572 static isl_stat product_entry(void **entry, void *user)
1574 struct isl_union_map_bin_data *data = user;
1575 isl_map *map2 = *entry;
1577 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1579 data->res = isl_union_map_add_map(data->res, map2);
1581 return isl_stat_ok;
1584 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1585 __isl_take isl_union_map *umap2)
1587 return bin_op(umap1, umap2, &product_entry);
1590 static isl_stat set_product_entry(void **entry, void *user)
1592 struct isl_union_map_bin_data *data = user;
1593 isl_set *set2 = *entry;
1595 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1597 data->res = isl_union_set_add_set(data->res, set2);
1599 return isl_stat_ok;
1602 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1603 __isl_take isl_union_set *uset2)
1605 return bin_op(uset1, uset2, &set_product_entry);
1608 static isl_stat domain_product_entry(void **entry, void *user)
1610 struct isl_union_map_bin_data *data = user;
1611 isl_map *map2 = *entry;
1613 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1614 map2->dim, isl_dim_out))
1615 return isl_stat_ok;
1617 map2 = isl_map_domain_product(isl_map_copy(data->map),
1618 isl_map_copy(map2));
1620 data->res = isl_union_map_add_map(data->res, map2);
1622 return isl_stat_ok;
1625 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1627 __isl_give isl_union_map *isl_union_map_domain_product(
1628 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1630 return bin_op(umap1, umap2, &domain_product_entry);
1633 static isl_stat range_product_entry(void **entry, void *user)
1635 struct isl_union_map_bin_data *data = user;
1636 isl_map *map2 = *entry;
1638 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1639 map2->dim, isl_dim_in))
1640 return isl_stat_ok;
1642 map2 = isl_map_range_product(isl_map_copy(data->map),
1643 isl_map_copy(map2));
1645 data->res = isl_union_map_add_map(data->res, map2);
1647 return isl_stat_ok;
1650 __isl_give isl_union_map *isl_union_map_range_product(
1651 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1653 return bin_op(umap1, umap2, &range_product_entry);
1656 /* If data->map A -> B and "map2" C -> D have the same range space,
1657 * then add (A, C) -> (B * D) to data->res.
1659 static isl_stat flat_domain_product_entry(void **entry, void *user)
1661 struct isl_union_map_bin_data *data = user;
1662 isl_map *map2 = *entry;
1664 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1665 map2->dim, isl_dim_out))
1666 return isl_stat_ok;
1668 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1669 isl_map_copy(map2));
1671 data->res = isl_union_map_add_map(data->res, map2);
1673 return isl_stat_ok;
1676 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1678 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1679 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1681 return bin_op(umap1, umap2, &flat_domain_product_entry);
1684 static isl_stat flat_range_product_entry(void **entry, void *user)
1686 struct isl_union_map_bin_data *data = user;
1687 isl_map *map2 = *entry;
1689 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1690 map2->dim, isl_dim_in))
1691 return isl_stat_ok;
1693 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1694 isl_map_copy(map2));
1696 data->res = isl_union_map_add_map(data->res, map2);
1698 return isl_stat_ok;
1701 __isl_give isl_union_map *isl_union_map_flat_range_product(
1702 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1704 return bin_op(umap1, umap2, &flat_range_product_entry);
1707 /* Data structure that specifies how un_op should modify
1708 * the maps in the union map.
1710 * If "inplace" is set, then the maps in the input union map
1711 * are modified in place. This means that "fn_map" should not
1712 * change the meaning of the map or that the union map only
1713 * has a single reference.
1714 * If "total" is set, then all maps need to be modified and
1715 * the results need to live in the same space.
1716 * Otherwise, a new union map is constructed to store the results.
1717 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1718 * are taken into account. "filter_user" is passed as the second argument
1719 * to "filter". No filter can be set if "inplace" or
1720 * "total" is set.
1721 * Exactly one of "fn_map" or "fn_map2" should be set, specifying
1722 * how the maps (selected by "filter") should be transformed.
1723 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1725 struct isl_un_op_control {
1726 int inplace;
1727 int total;
1728 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1729 void *filter_user;
1730 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1731 __isl_give isl_map *(*fn_map2)(__isl_take isl_map *map, void *user);
1732 void *fn_map2_user;
1735 /* Data structure for wrapping the data for un_op_filter_drop_user.
1736 * "filter" is the function that is being wrapped.
1738 struct isl_un_op_drop_user_data {
1739 isl_bool (*filter)(__isl_keep isl_map *map);
1742 /* Wrapper for isl_un_op_control filters that do not require
1743 * a second argument.
1744 * Simply call data->filter without the second argument.
1746 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1748 struct isl_un_op_drop_user_data *data = user;
1749 return data->filter(map);
1752 /* Internal data structure for "un_op".
1753 * "control" specifies how the maps in the union map should be modified.
1754 * "res" collects the results.
1756 struct isl_union_map_un_data {
1757 struct isl_un_op_control *control;
1758 isl_union_map *res;
1761 /* isl_hash_table_foreach callback for un_op.
1762 * Handle the map that "entry" points to.
1764 * If control->filter is set, then check if this map satisfies the filter.
1765 * If so (or if control->filter is not set), modify the map
1766 * by calling control->fn_map or control->fn_map2 and
1767 * either add the result to data->res or
1768 * replace the original entry by the result (if control->inplace is set).
1770 static isl_stat un_entry(void **entry, void *user)
1772 struct isl_union_map_un_data *data = user;
1773 struct isl_un_op_control *control = data->control;
1774 isl_map *map = *entry;
1776 if (control->filter) {
1777 isl_bool ok;
1779 ok = control->filter(map, control->filter_user);
1780 if (ok < 0)
1781 return isl_stat_error;
1782 if (!ok)
1783 return isl_stat_ok;
1786 map = isl_map_copy(map);
1787 if (control->fn_map2 != NULL)
1788 map = control->fn_map2(map, control->fn_map2_user);
1789 else
1790 map = control->fn_map(map);
1791 if (!map)
1792 return isl_stat_error;
1793 if (control->inplace) {
1794 isl_map_free(*entry);
1795 *entry = map;
1796 } else {
1797 data->res = isl_union_map_add_map(data->res, map);
1798 if (!data->res)
1799 return isl_stat_error;
1802 return isl_stat_ok;
1805 /* Modify the maps in "umap" based on "control".
1806 * If control->inplace is set, then modify the maps in "umap" in-place.
1807 * Otherwise, create a new union map to hold the results.
1808 * If control->total is set, then perform an inplace computation
1809 * if "umap" is only referenced once. Otherwise, create a new union map
1810 * to store the results.
1812 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1813 struct isl_un_op_control *control)
1815 struct isl_union_map_un_data data = { control };
1817 if (!umap)
1818 return NULL;
1819 if (!!control->fn_map == !!control->fn_map2)
1820 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
1821 "exactly one mapping function should be specified",
1822 return isl_union_map_free(umap));
1823 if ((control->inplace || control->total) && control->filter)
1824 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1825 "inplace/total modification cannot be filtered",
1826 return isl_union_map_free(umap));
1828 if (control->total && umap->ref == 1)
1829 control->inplace = 1;
1830 if (control->inplace) {
1831 data.res = umap;
1832 } else {
1833 isl_space *space;
1835 space = isl_union_map_get_space(umap);
1836 data.res = isl_union_map_alloc(space, umap->table.n);
1838 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1839 &umap->table, &un_entry, &data) < 0)
1840 data.res = isl_union_map_free(data.res);
1842 if (control->inplace)
1843 return data.res;
1844 isl_union_map_free(umap);
1845 return data.res;
1848 __isl_give isl_union_map *isl_union_map_from_range(
1849 __isl_take isl_union_set *uset)
1851 struct isl_un_op_control control = {
1852 .fn_map = &isl_map_from_range,
1854 return un_op(uset, &control);
1857 __isl_give isl_union_map *isl_union_map_from_domain(
1858 __isl_take isl_union_set *uset)
1860 return isl_union_map_reverse(isl_union_map_from_range(uset));
1863 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1864 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1866 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1867 isl_union_map_from_range(range));
1870 /* Modify the maps in "umap" by applying "fn" on them.
1871 * "fn" should apply to all maps in "umap" and should not modify the space.
1873 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1874 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1876 struct isl_un_op_control control = {
1877 .total = 1,
1878 .fn_map = fn,
1881 return un_op(umap, &control);
1884 /* Compute the affine hull of "map" and return the result as an isl_map.
1886 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1888 return isl_map_from_basic_map(isl_map_affine_hull(map));
1891 __isl_give isl_union_map *isl_union_map_affine_hull(
1892 __isl_take isl_union_map *umap)
1894 return total(umap, &isl_map_affine_hull_map);
1897 __isl_give isl_union_set *isl_union_set_affine_hull(
1898 __isl_take isl_union_set *uset)
1900 return isl_union_map_affine_hull(uset);
1903 /* Wrapper around isl_set_combined_lineality_space
1904 * that returns the combined lineality space in the form of an isl_set
1905 * instead of an isl_basic_set.
1907 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1909 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1912 /* For each set in "uset", compute the (linear) hull
1913 * of the lineality spaces of its basic sets and
1914 * collect and return the results.
1916 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1917 __isl_take isl_union_set *uset)
1919 struct isl_un_op_control control = {
1920 .fn_map = &combined_lineality_space,
1922 return un_op(uset, &control);
1925 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1927 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1929 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1932 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1933 __isl_take isl_union_map *umap)
1935 return total(umap, &isl_map_polyhedral_hull_map);
1938 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1939 __isl_take isl_union_set *uset)
1941 return isl_union_map_polyhedral_hull(uset);
1944 /* Compute a superset of the convex hull of "map" that is described
1945 * by only translates of the constraints in the constituents of "map" and
1946 * return the result as an isl_map.
1948 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1950 return isl_map_from_basic_map(isl_map_simple_hull(map));
1953 __isl_give isl_union_map *isl_union_map_simple_hull(
1954 __isl_take isl_union_map *umap)
1956 return total(umap, &isl_map_simple_hull_map);
1959 __isl_give isl_union_set *isl_union_set_simple_hull(
1960 __isl_take isl_union_set *uset)
1962 return isl_union_map_simple_hull(uset);
1965 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1966 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1968 struct isl_un_op_control control = {
1969 .inplace = 1,
1970 .fn_map = fn,
1973 return un_op(umap, &control);
1976 /* Remove redundant constraints in each of the basic maps of "umap".
1977 * Since removing redundant constraints does not change the meaning
1978 * or the space, the operation can be performed in-place.
1980 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1981 __isl_take isl_union_map *umap)
1983 return inplace(umap, &isl_map_remove_redundancies);
1986 /* Remove redundant constraints in each of the basic sets of "uset".
1988 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1989 __isl_take isl_union_set *uset)
1991 return isl_union_map_remove_redundancies(uset);
1994 __isl_give isl_union_map *isl_union_map_coalesce(
1995 __isl_take isl_union_map *umap)
1997 return inplace(umap, &isl_map_coalesce);
2000 __isl_give isl_union_set *isl_union_set_coalesce(
2001 __isl_take isl_union_set *uset)
2003 return isl_union_map_coalesce(uset);
2006 __isl_give isl_union_map *isl_union_map_detect_equalities(
2007 __isl_take isl_union_map *umap)
2009 return inplace(umap, &isl_map_detect_equalities);
2012 __isl_give isl_union_set *isl_union_set_detect_equalities(
2013 __isl_take isl_union_set *uset)
2015 return isl_union_map_detect_equalities(uset);
2018 __isl_give isl_union_map *isl_union_map_compute_divs(
2019 __isl_take isl_union_map *umap)
2021 return inplace(umap, &isl_map_compute_divs);
2024 __isl_give isl_union_set *isl_union_set_compute_divs(
2025 __isl_take isl_union_set *uset)
2027 return isl_union_map_compute_divs(uset);
2030 __isl_give isl_union_map *isl_union_map_lexmin(
2031 __isl_take isl_union_map *umap)
2033 return total(umap, &isl_map_lexmin);
2036 __isl_give isl_union_set *isl_union_set_lexmin(
2037 __isl_take isl_union_set *uset)
2039 return isl_union_map_lexmin(uset);
2042 __isl_give isl_union_map *isl_union_map_lexmax(
2043 __isl_take isl_union_map *umap)
2045 return total(umap, &isl_map_lexmax);
2048 __isl_give isl_union_set *isl_union_set_lexmax(
2049 __isl_take isl_union_set *uset)
2051 return isl_union_map_lexmax(uset);
2054 /* Return the universe in the space of "map".
2056 static __isl_give isl_map *universe(__isl_take isl_map *map)
2058 isl_space *space;
2060 space = isl_map_get_space(map);
2061 isl_map_free(map);
2062 return isl_map_universe(space);
2065 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2067 struct isl_un_op_control control = {
2068 .fn_map = &universe,
2070 return un_op(umap, &control);
2073 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2075 return isl_union_map_universe(uset);
2078 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2080 struct isl_un_op_control control = {
2081 .fn_map = &isl_map_reverse,
2083 return un_op(umap, &control);
2086 /* Compute the parameter domain of the given union map.
2088 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2090 struct isl_un_op_control control = {
2091 .fn_map = &isl_map_params,
2093 int empty;
2095 empty = isl_union_map_is_empty(umap);
2096 if (empty < 0)
2097 goto error;
2098 if (empty) {
2099 isl_space *space;
2100 space = isl_union_map_get_space(umap);
2101 isl_union_map_free(umap);
2102 return isl_set_empty(space);
2104 return isl_set_from_union_set(un_op(umap, &control));
2105 error:
2106 isl_union_map_free(umap);
2107 return NULL;
2110 /* Compute the parameter domain of the given union set.
2112 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2114 return isl_union_map_params(uset);
2117 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2119 struct isl_un_op_control control = {
2120 .fn_map = &isl_map_domain,
2122 return un_op(umap, &control);
2125 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2127 struct isl_un_op_control control = {
2128 .fn_map = &isl_map_range,
2130 return un_op(umap, &control);
2133 __isl_give isl_union_map *isl_union_map_domain_map(
2134 __isl_take isl_union_map *umap)
2136 struct isl_un_op_control control = {
2137 .fn_map = &isl_map_domain_map,
2139 return un_op(umap, &control);
2142 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2143 * add the result to "res".
2145 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2147 isl_union_pw_multi_aff **res = user;
2148 isl_multi_aff *ma;
2149 isl_pw_multi_aff *pma;
2151 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2152 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2153 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2155 return *res ? isl_stat_ok : isl_stat_error;
2159 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2160 * to its domain.
2162 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2163 __isl_take isl_union_map *umap)
2165 isl_union_pw_multi_aff *res;
2167 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2168 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2169 res = isl_union_pw_multi_aff_free(res);
2171 isl_union_map_free(umap);
2172 return res;
2175 __isl_give isl_union_map *isl_union_map_range_map(
2176 __isl_take isl_union_map *umap)
2178 struct isl_un_op_control control = {
2179 .fn_map = &isl_map_range_map,
2181 return un_op(umap, &control);
2184 /* Given a collection of wrapped maps of the form A[B -> C],
2185 * return the collection of maps A[B -> C] -> B.
2187 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2188 __isl_take isl_union_set *uset)
2190 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2191 struct isl_un_op_control control = {
2192 .filter = &un_op_filter_drop_user,
2193 .filter_user = &data,
2194 .fn_map = &isl_set_wrapped_domain_map,
2196 return un_op(uset, &control);
2199 /* Does "map" relate elements from the same space?
2201 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2203 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2204 map->dim, isl_dim_out);
2207 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2209 struct isl_un_op_control control = {
2210 .filter = &equal_tuples,
2211 .fn_map = &isl_map_deltas,
2213 return un_op(umap, &control);
2216 __isl_give isl_union_map *isl_union_map_deltas_map(
2217 __isl_take isl_union_map *umap)
2219 struct isl_un_op_control control = {
2220 .filter = &equal_tuples,
2221 .fn_map = &isl_map_deltas_map,
2223 return un_op(umap, &control);
2226 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2228 struct isl_un_op_control control = {
2229 .fn_map = &isl_set_identity,
2231 return un_op(uset, &control);
2234 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2236 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2238 isl_union_pw_multi_aff **res = user;
2239 isl_space *space;
2240 isl_pw_multi_aff *pma;
2242 space = isl_space_map_from_set(isl_set_get_space(set));
2243 pma = isl_pw_multi_aff_identity(space);
2244 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2245 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2247 return *res ? isl_stat_ok : isl_stat_error;
2250 /* Return an identity function on "uset" in the form
2251 * of an isl_union_pw_multi_aff.
2253 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2254 __isl_take isl_union_set *uset)
2256 isl_union_pw_multi_aff *res;
2258 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2259 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2260 res = isl_union_pw_multi_aff_free(res);
2262 isl_union_set_free(uset);
2263 return res;
2266 /* For each map in "umap" of the form [A -> B] -> C,
2267 * construct the map A -> C and collect the results.
2269 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2270 __isl_take isl_union_map *umap)
2272 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2273 struct isl_un_op_control control = {
2274 .filter = &un_op_filter_drop_user,
2275 .filter_user = &data,
2276 .fn_map = &isl_map_domain_factor_domain,
2278 return un_op(umap, &control);
2281 /* For each map in "umap" of the form [A -> B] -> C,
2282 * construct the map B -> C and collect the results.
2284 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2285 __isl_take isl_union_map *umap)
2287 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2288 struct isl_un_op_control control = {
2289 .filter = &un_op_filter_drop_user,
2290 .filter_user = &data,
2291 .fn_map = &isl_map_domain_factor_range,
2293 return un_op(umap, &control);
2296 /* For each map in "umap" of the form A -> [B -> C],
2297 * construct the map A -> B and collect the results.
2299 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2300 __isl_take isl_union_map *umap)
2302 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2303 struct isl_un_op_control control = {
2304 .filter = &un_op_filter_drop_user,
2305 .filter_user = &data,
2306 .fn_map = &isl_map_range_factor_domain,
2308 return un_op(umap, &control);
2311 /* For each map in "umap" of the form A -> [B -> C],
2312 * construct the map A -> C and collect the results.
2314 __isl_give isl_union_map *isl_union_map_range_factor_range(
2315 __isl_take isl_union_map *umap)
2317 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2318 struct isl_un_op_control control = {
2319 .filter = &un_op_filter_drop_user,
2320 .filter_user = &data,
2321 .fn_map = &isl_map_range_factor_range,
2323 return un_op(umap, &control);
2326 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2327 * construct the map A -> C and collect the results.
2329 __isl_give isl_union_map *isl_union_map_factor_domain(
2330 __isl_take isl_union_map *umap)
2332 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2333 struct isl_un_op_control control = {
2334 .filter = &un_op_filter_drop_user,
2335 .filter_user = &data,
2336 .fn_map = &isl_map_factor_domain,
2338 return un_op(umap, &control);
2341 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2342 * construct the map B -> D and collect the results.
2344 __isl_give isl_union_map *isl_union_map_factor_range(
2345 __isl_take isl_union_map *umap)
2347 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2348 struct isl_un_op_control control = {
2349 .filter = &un_op_filter_drop_user,
2350 .filter_user = &data,
2351 .fn_map = &isl_map_factor_range,
2353 return un_op(umap, &control);
2356 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2358 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2359 struct isl_un_op_control control = {
2360 .filter = &un_op_filter_drop_user,
2361 .filter_user = &data,
2362 .fn_map = &isl_set_unwrap,
2364 return un_op(uset, &control);
2367 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2369 struct isl_un_op_control control = {
2370 .fn_map = &isl_map_wrap,
2372 return un_op(umap, &control);
2375 struct isl_union_map_is_subset_data {
2376 isl_union_map *umap2;
2377 isl_bool is_subset;
2380 static isl_stat is_subset_entry(void **entry, void *user)
2382 struct isl_union_map_is_subset_data *data = user;
2383 uint32_t hash;
2384 struct isl_hash_table_entry *entry2;
2385 isl_map *map = *entry;
2387 hash = isl_space_get_hash(map->dim);
2388 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2389 hash, &has_space, map->dim, 0);
2390 if (!entry2) {
2391 int empty = isl_map_is_empty(map);
2392 if (empty < 0)
2393 return isl_stat_error;
2394 if (empty)
2395 return isl_stat_ok;
2396 data->is_subset = isl_bool_false;
2397 return isl_stat_error;
2400 data->is_subset = isl_map_is_subset(map, entry2->data);
2401 if (data->is_subset < 0 || !data->is_subset)
2402 return isl_stat_error;
2404 return isl_stat_ok;
2407 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2408 __isl_keep isl_union_map *umap2)
2410 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2412 umap1 = isl_union_map_copy(umap1);
2413 umap2 = isl_union_map_copy(umap2);
2414 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2415 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2417 if (!umap1 || !umap2)
2418 goto error;
2420 data.umap2 = umap2;
2421 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2422 &is_subset_entry, &data) < 0 &&
2423 data.is_subset)
2424 goto error;
2426 isl_union_map_free(umap1);
2427 isl_union_map_free(umap2);
2429 return data.is_subset;
2430 error:
2431 isl_union_map_free(umap1);
2432 isl_union_map_free(umap2);
2433 return isl_bool_error;
2436 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2437 __isl_keep isl_union_set *uset2)
2439 return isl_union_map_is_subset(uset1, uset2);
2442 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2443 __isl_keep isl_union_map *umap2)
2445 isl_bool is_subset;
2447 if (!umap1 || !umap2)
2448 return isl_bool_error;
2449 is_subset = isl_union_map_is_subset(umap1, umap2);
2450 if (is_subset != isl_bool_true)
2451 return is_subset;
2452 is_subset = isl_union_map_is_subset(umap2, umap1);
2453 return is_subset;
2456 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2457 __isl_keep isl_union_set *uset2)
2459 return isl_union_map_is_equal(uset1, uset2);
2462 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2463 __isl_keep isl_union_map *umap2)
2465 isl_bool is_subset;
2467 if (!umap1 || !umap2)
2468 return isl_bool_error;
2469 is_subset = isl_union_map_is_subset(umap1, umap2);
2470 if (is_subset != isl_bool_true)
2471 return is_subset;
2472 is_subset = isl_union_map_is_subset(umap2, umap1);
2473 return isl_bool_not(is_subset);
2476 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2477 __isl_keep isl_union_set *uset2)
2479 return isl_union_map_is_strict_subset(uset1, uset2);
2482 /* Internal data structure for isl_union_map_is_disjoint.
2483 * umap2 is the union map with which we are comparing.
2484 * is_disjoint is initialized to 1 and is set to 0 as soon
2485 * as the union maps turn out not to be disjoint.
2487 struct isl_union_map_is_disjoint_data {
2488 isl_union_map *umap2;
2489 isl_bool is_disjoint;
2492 /* Check if "map" is disjoint from data->umap2 and abort
2493 * the search if it is not.
2495 static isl_stat is_disjoint_entry(void **entry, void *user)
2497 struct isl_union_map_is_disjoint_data *data = user;
2498 uint32_t hash;
2499 struct isl_hash_table_entry *entry2;
2500 isl_map *map = *entry;
2502 hash = isl_space_get_hash(map->dim);
2503 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2504 hash, &has_space, map->dim, 0);
2505 if (!entry2)
2506 return isl_stat_ok;
2508 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2509 if (data->is_disjoint < 0 || !data->is_disjoint)
2510 return isl_stat_error;
2512 return isl_stat_ok;
2515 /* Are "umap1" and "umap2" disjoint?
2517 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2518 __isl_keep isl_union_map *umap2)
2520 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2522 umap1 = isl_union_map_copy(umap1);
2523 umap2 = isl_union_map_copy(umap2);
2524 umap1 = isl_union_map_align_params(umap1,
2525 isl_union_map_get_space(umap2));
2526 umap2 = isl_union_map_align_params(umap2,
2527 isl_union_map_get_space(umap1));
2529 if (!umap1 || !umap2)
2530 goto error;
2532 data.umap2 = umap2;
2533 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2534 &is_disjoint_entry, &data) < 0 &&
2535 data.is_disjoint)
2536 goto error;
2538 isl_union_map_free(umap1);
2539 isl_union_map_free(umap2);
2541 return data.is_disjoint;
2542 error:
2543 isl_union_map_free(umap1);
2544 isl_union_map_free(umap2);
2545 return isl_bool_error;
2548 /* Are "uset1" and "uset2" disjoint?
2550 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2551 __isl_keep isl_union_set *uset2)
2553 return isl_union_map_is_disjoint(uset1, uset2);
2556 static isl_stat sample_entry(void **entry, void *user)
2558 isl_basic_map **sample = (isl_basic_map **)user;
2559 isl_map *map = *entry;
2561 *sample = isl_map_sample(isl_map_copy(map));
2562 if (!*sample)
2563 return isl_stat_error;
2564 if (!isl_basic_map_plain_is_empty(*sample))
2565 return isl_stat_error;
2566 return isl_stat_ok;
2569 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2571 isl_basic_map *sample = NULL;
2573 if (!umap)
2574 return NULL;
2576 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2577 &sample_entry, &sample) < 0 &&
2578 !sample)
2579 goto error;
2581 if (!sample)
2582 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2584 isl_union_map_free(umap);
2586 return sample;
2587 error:
2588 isl_union_map_free(umap);
2589 return NULL;
2592 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2594 return bset_from_bmap(isl_union_map_sample(uset));
2597 /* Return an element in "uset" in the form of an isl_point.
2598 * Return a void isl_point if "uset" is empty.
2600 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2602 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2605 struct isl_forall_data {
2606 isl_bool res;
2607 isl_bool (*fn)(__isl_keep isl_map *map);
2610 static isl_stat forall_entry(void **entry, void *user)
2612 struct isl_forall_data *data = user;
2613 isl_map *map = *entry;
2615 data->res = data->fn(map);
2616 if (data->res < 0)
2617 return isl_stat_error;
2619 if (!data->res)
2620 return isl_stat_error;
2622 return isl_stat_ok;
2625 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2626 isl_bool (*fn)(__isl_keep isl_map *map))
2628 struct isl_forall_data data = { isl_bool_true, fn };
2630 if (!umap)
2631 return isl_bool_error;
2633 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2634 &forall_entry, &data) < 0 && data.res)
2635 return isl_bool_error;
2637 return data.res;
2640 struct isl_forall_user_data {
2641 isl_bool res;
2642 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2643 void *user;
2646 static isl_stat forall_user_entry(void **entry, void *user)
2648 struct isl_forall_user_data *data = user;
2649 isl_map *map = *entry;
2651 data->res = data->fn(map, data->user);
2652 if (data->res < 0)
2653 return isl_stat_error;
2655 if (!data->res)
2656 return isl_stat_error;
2658 return isl_stat_ok;
2661 /* Check if fn(map, user) returns true for all maps "map" in umap.
2663 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2664 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2666 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2668 if (!umap)
2669 return isl_bool_error;
2671 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2672 &forall_user_entry, &data) < 0 && data.res)
2673 return isl_bool_error;
2675 return data.res;
2678 /* Is "umap" obviously empty?
2680 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2682 isl_size n;
2684 n = isl_union_map_n_map(umap);
2685 if (n < 0)
2686 return isl_bool_error;
2687 return n == 0;
2690 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2692 return union_map_forall(umap, &isl_map_is_empty);
2695 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2697 return isl_union_map_is_empty(uset);
2700 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2702 isl_bool is_subset;
2703 isl_space *dim;
2704 isl_map *id;
2706 if (!map)
2707 return isl_bool_error;
2709 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2710 map->dim, isl_dim_out))
2711 return isl_bool_false;
2713 dim = isl_map_get_space(map);
2714 id = isl_map_identity(dim);
2716 is_subset = isl_map_is_subset(map, id);
2718 isl_map_free(id);
2720 return is_subset;
2723 /* Given an isl_union_map that consists of a single map, check
2724 * if it is single-valued.
2726 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2728 isl_map *map;
2729 isl_bool sv;
2731 umap = isl_union_map_copy(umap);
2732 map = isl_map_from_union_map(umap);
2733 sv = isl_map_is_single_valued(map);
2734 isl_map_free(map);
2736 return sv;
2739 /* Internal data structure for single_valued_on_domain.
2741 * "umap" is the union map to be tested.
2742 * "sv" is set to 1 as long as "umap" may still be single-valued.
2744 struct isl_union_map_is_sv_data {
2745 isl_union_map *umap;
2746 isl_bool sv;
2749 /* Check if the data->umap is single-valued on "set".
2751 * If data->umap consists of a single map on "set", then test it
2752 * as an isl_map.
2754 * Otherwise, compute
2756 * M \circ M^-1
2758 * check if the result is a subset of the identity mapping and
2759 * store the result in data->sv.
2761 * Terminate as soon as data->umap has been determined not to
2762 * be single-valued.
2764 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2766 struct isl_union_map_is_sv_data *data = user;
2767 isl_union_map *umap, *test;
2768 isl_size n;
2770 umap = isl_union_map_copy(data->umap);
2771 umap = isl_union_map_intersect_domain(umap,
2772 isl_union_set_from_set(set));
2774 n = isl_union_map_n_map(umap);
2775 if (n < 0) {
2776 data->sv = isl_bool_error;
2777 } else if (n == 1) {
2778 data->sv = single_map_is_single_valued(umap);
2779 isl_union_map_free(umap);
2780 } else {
2781 test = isl_union_map_reverse(isl_union_map_copy(umap));
2782 test = isl_union_map_apply_range(test, umap);
2784 data->sv = union_map_forall(test, &is_subset_of_identity);
2786 isl_union_map_free(test);
2789 if (data->sv < 0 || !data->sv)
2790 return isl_stat_error;
2791 return isl_stat_ok;
2794 /* Check if the given map is single-valued.
2796 * If the union map consists of a single map, then test it as an isl_map.
2797 * Otherwise, check if the union map is single-valued on each of its
2798 * domain spaces.
2800 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2802 isl_union_map *universe;
2803 isl_union_set *domain;
2804 struct isl_union_map_is_sv_data data;
2805 isl_size n;
2807 n = isl_union_map_n_map(umap);
2808 if (n < 0)
2809 return isl_bool_error;
2810 if (n == 1)
2811 return single_map_is_single_valued(umap);
2813 universe = isl_union_map_universe(isl_union_map_copy(umap));
2814 domain = isl_union_map_domain(universe);
2816 data.sv = isl_bool_true;
2817 data.umap = umap;
2818 if (isl_union_set_foreach_set(domain,
2819 &single_valued_on_domain, &data) < 0 && data.sv)
2820 data.sv = isl_bool_error;
2821 isl_union_set_free(domain);
2823 return data.sv;
2826 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2828 isl_bool in;
2830 umap = isl_union_map_copy(umap);
2831 umap = isl_union_map_reverse(umap);
2832 in = isl_union_map_is_single_valued(umap);
2833 isl_union_map_free(umap);
2835 return in;
2838 /* Is "map" obviously not an identity relation because
2839 * it maps elements from one space to another space?
2840 * Update *non_identity accordingly.
2842 * In particular, if the domain and range spaces are the same,
2843 * then the map is not considered to obviously not be an identity relation.
2844 * Otherwise, the map is considered to obviously not be an identity relation
2845 * if it is is non-empty.
2847 * If "map" is determined to obviously not be an identity relation,
2848 * then the search is aborted.
2850 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2852 isl_bool *non_identity = user;
2853 isl_bool equal;
2854 isl_space *space;
2856 space = isl_map_get_space(map);
2857 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2858 if (equal >= 0 && !equal)
2859 *non_identity = isl_bool_not(isl_map_is_empty(map));
2860 else
2861 *non_identity = isl_bool_not(equal);
2862 isl_space_free(space);
2863 isl_map_free(map);
2865 if (*non_identity < 0 || *non_identity)
2866 return isl_stat_error;
2868 return isl_stat_ok;
2871 /* Is "umap" obviously not an identity relation because
2872 * it maps elements from one space to another space?
2874 * As soon as a map has been found that maps elements to a different space,
2875 * non_identity is changed and the search is aborted.
2877 static isl_bool isl_union_map_plain_is_not_identity(
2878 __isl_keep isl_union_map *umap)
2880 isl_bool non_identity;
2882 non_identity = isl_bool_false;
2883 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2884 &non_identity) < 0 &&
2885 non_identity == isl_bool_false)
2886 return isl_bool_error;
2888 return non_identity;
2891 /* Does "map" only map elements to themselves?
2892 * Update *identity accordingly.
2894 * If "map" is determined not to be an identity relation,
2895 * then the search is aborted.
2897 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2899 isl_bool *identity = user;
2901 *identity = isl_map_is_identity(map);
2902 isl_map_free(map);
2904 if (*identity < 0 || !*identity)
2905 return isl_stat_error;
2907 return isl_stat_ok;
2910 /* Does "umap" only map elements to themselves?
2912 * First check if there are any maps that map elements to different spaces.
2913 * If not, then check that all the maps (between identical spaces)
2914 * are identity relations.
2916 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2918 isl_bool non_identity;
2919 isl_bool identity;
2921 non_identity = isl_union_map_plain_is_not_identity(umap);
2922 if (non_identity < 0 || non_identity)
2923 return isl_bool_not(non_identity);
2925 identity = isl_bool_true;
2926 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2927 identity == isl_bool_true)
2928 return isl_bool_error;
2930 return identity;
2933 /* Represents a map that has a fixed value (v) for one of its
2934 * range dimensions.
2935 * The map in this structure is not reference counted, so it
2936 * is only valid while the isl_union_map from which it was
2937 * obtained is still alive.
2939 struct isl_fixed_map {
2940 isl_int v;
2941 isl_map *map;
2944 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2945 int n)
2947 int i;
2948 struct isl_fixed_map *v;
2950 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2951 if (!v)
2952 return NULL;
2953 for (i = 0; i < n; ++i)
2954 isl_int_init(v[i].v);
2955 return v;
2958 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2960 int i;
2962 if (!v)
2963 return;
2964 for (i = 0; i < n; ++i)
2965 isl_int_clear(v[i].v);
2966 free(v);
2969 /* Compare the "v" field of two isl_fixed_map structs.
2971 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2973 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2974 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2976 return isl_int_cmp(e1->v, e2->v);
2979 /* Internal data structure used while checking whether all maps
2980 * in a union_map have a fixed value for a given output dimension.
2981 * v is the list of maps, with the fixed value for the dimension
2982 * n is the number of maps considered so far
2983 * pos is the output dimension under investigation
2985 struct isl_fixed_dim_data {
2986 struct isl_fixed_map *v;
2987 int n;
2988 int pos;
2991 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2993 struct isl_fixed_dim_data *data = user;
2995 data->v[data->n].map = map;
2996 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2997 &data->v[data->n++].v);
3000 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3001 int first, int n_range);
3003 /* Given a list of the maps, with their fixed values at output dimension "pos",
3004 * check whether the ranges of the maps form an obvious partition.
3006 * We first sort the maps according to their fixed values.
3007 * If all maps have a different value, then we know the ranges form
3008 * a partition.
3009 * Otherwise, we collect the maps with the same fixed value and
3010 * check whether each such collection is obviously injective
3011 * based on later dimensions.
3013 static int separates(struct isl_fixed_map *v, int n,
3014 __isl_take isl_space *dim, int pos, int n_range)
3016 int i;
3018 if (!v)
3019 goto error;
3021 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
3023 for (i = 0; i + 1 < n; ++i) {
3024 int j, k;
3025 isl_union_map *part;
3026 int injective;
3028 for (j = i + 1; j < n; ++j)
3029 if (isl_int_ne(v[i].v, v[j].v))
3030 break;
3032 if (j == i + 1)
3033 continue;
3035 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
3036 for (k = i; k < j; ++k)
3037 part = isl_union_map_add_map(part,
3038 isl_map_copy(v[k].map));
3040 injective = plain_injective_on_range(part, pos + 1, n_range);
3041 if (injective < 0)
3042 goto error;
3043 if (!injective)
3044 break;
3046 i = j - 1;
3049 isl_space_free(dim);
3050 free_isl_fixed_map_array(v, n);
3051 return i + 1 >= n;
3052 error:
3053 isl_space_free(dim);
3054 free_isl_fixed_map_array(v, n);
3055 return -1;
3058 /* Check whether the maps in umap have obviously distinct ranges.
3059 * In particular, check for an output dimension in the range
3060 * [first,n_range) for which all maps have a fixed value
3061 * and then check if these values, possibly along with fixed values
3062 * at later dimensions, entail distinct ranges.
3064 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3065 int first, int n_range)
3067 isl_ctx *ctx;
3068 isl_size n;
3069 struct isl_fixed_dim_data data = { NULL };
3071 ctx = isl_union_map_get_ctx(umap);
3073 n = isl_union_map_n_map(umap);
3074 if (n < 0)
3075 goto error;
3077 if (n <= 1) {
3078 isl_union_map_free(umap);
3079 return isl_bool_true;
3082 if (first >= n_range) {
3083 isl_union_map_free(umap);
3084 return isl_bool_false;
3087 data.v = alloc_isl_fixed_map_array(ctx, n);
3088 if (!data.v)
3089 goto error;
3091 for (data.pos = first; data.pos < n_range; ++data.pos) {
3092 isl_bool fixed;
3093 int injective;
3094 isl_space *dim;
3096 data.n = 0;
3097 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3098 if (fixed < 0)
3099 goto error;
3100 if (!fixed)
3101 continue;
3102 dim = isl_union_map_get_space(umap);
3103 injective = separates(data.v, n, dim, data.pos, n_range);
3104 isl_union_map_free(umap);
3105 return injective;
3108 free_isl_fixed_map_array(data.v, n);
3109 isl_union_map_free(umap);
3111 return isl_bool_false;
3112 error:
3113 free_isl_fixed_map_array(data.v, n);
3114 isl_union_map_free(umap);
3115 return isl_bool_error;
3118 /* Check whether the maps in umap that map to subsets of "ran"
3119 * have obviously distinct ranges.
3121 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3122 void *user)
3124 isl_size dim;
3125 isl_union_map *umap = user;
3127 dim = isl_set_dim(ran, isl_dim_set);
3128 if (dim < 0)
3129 return isl_bool_error;
3131 umap = isl_union_map_copy(umap);
3132 umap = isl_union_map_intersect_range(umap,
3133 isl_union_set_from_set(isl_set_copy(ran)));
3134 return plain_injective_on_range(umap, 0, dim);
3137 /* Check if the given union_map is obviously injective.
3139 * In particular, we first check if all individual maps are obviously
3140 * injective and then check if all the ranges of these maps are
3141 * obviously disjoint.
3143 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3145 isl_bool in;
3146 isl_union_map *univ;
3147 isl_union_set *ran;
3149 in = union_map_forall(umap, &isl_map_plain_is_injective);
3150 if (in < 0)
3151 return isl_bool_error;
3152 if (!in)
3153 return isl_bool_false;
3155 univ = isl_union_map_universe(isl_union_map_copy(umap));
3156 ran = isl_union_map_range(univ);
3158 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3160 isl_union_set_free(ran);
3162 return in;
3165 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3167 isl_bool sv;
3169 sv = isl_union_map_is_single_valued(umap);
3170 if (sv < 0 || !sv)
3171 return sv;
3173 return isl_union_map_is_injective(umap);
3176 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3178 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3179 struct isl_un_op_control control = {
3180 .filter = &un_op_filter_drop_user,
3181 .filter_user = &data,
3182 .fn_map = &isl_map_zip,
3184 return un_op(umap, &control);
3187 /* Given a union map, take the maps of the form A -> (B -> C) and
3188 * return the union of the corresponding maps (A -> B) -> C.
3190 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3192 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3193 struct isl_un_op_control control = {
3194 .filter = &un_op_filter_drop_user,
3195 .filter_user = &data,
3196 .fn_map = &isl_map_uncurry,
3198 return un_op(umap, &control);
3201 /* Given a union map, take the maps of the form (A -> B) -> C and
3202 * return the union of the corresponding maps A -> (B -> C).
3204 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3206 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3207 struct isl_un_op_control control = {
3208 .filter = &un_op_filter_drop_user,
3209 .filter_user = &data,
3210 .fn_map = &isl_map_curry,
3212 return un_op(umap, &control);
3215 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3216 * return the union of the corresponding maps A -> (B -> (C -> D)).
3218 __isl_give isl_union_map *isl_union_map_range_curry(
3219 __isl_take isl_union_map *umap)
3221 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3222 struct isl_un_op_control control = {
3223 .filter = &un_op_filter_drop_user,
3224 .filter_user = &data,
3225 .fn_map = &isl_map_range_curry,
3227 return un_op(umap, &control);
3230 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3232 struct isl_un_op_control control = {
3233 .fn_map = &isl_set_lift,
3235 return un_op(uset, &control);
3238 static isl_stat coefficients_entry(void **entry, void *user)
3240 isl_set *set = *entry;
3241 isl_union_set **res = user;
3243 set = isl_set_copy(set);
3244 set = isl_set_from_basic_set(isl_set_coefficients(set));
3245 *res = isl_union_set_add_set(*res, set);
3247 return isl_stat_ok;
3250 __isl_give isl_union_set *isl_union_set_coefficients(
3251 __isl_take isl_union_set *uset)
3253 isl_ctx *ctx;
3254 isl_space *dim;
3255 isl_union_set *res;
3257 if (!uset)
3258 return NULL;
3260 ctx = isl_union_set_get_ctx(uset);
3261 dim = isl_space_set_alloc(ctx, 0, 0);
3262 res = isl_union_map_alloc(dim, uset->table.n);
3263 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3264 &coefficients_entry, &res) < 0)
3265 goto error;
3267 isl_union_set_free(uset);
3268 return res;
3269 error:
3270 isl_union_set_free(uset);
3271 isl_union_set_free(res);
3272 return NULL;
3275 static isl_stat solutions_entry(void **entry, void *user)
3277 isl_set *set = *entry;
3278 isl_union_set **res = user;
3280 set = isl_set_copy(set);
3281 set = isl_set_from_basic_set(isl_set_solutions(set));
3282 if (!*res)
3283 *res = isl_union_set_from_set(set);
3284 else
3285 *res = isl_union_set_add_set(*res, set);
3287 if (!*res)
3288 return isl_stat_error;
3290 return isl_stat_ok;
3293 __isl_give isl_union_set *isl_union_set_solutions(
3294 __isl_take isl_union_set *uset)
3296 isl_union_set *res = NULL;
3298 if (!uset)
3299 return NULL;
3301 if (uset->table.n == 0) {
3302 res = isl_union_set_empty(isl_union_set_get_space(uset));
3303 isl_union_set_free(uset);
3304 return res;
3307 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3308 &solutions_entry, &res) < 0)
3309 goto error;
3311 isl_union_set_free(uset);
3312 return res;
3313 error:
3314 isl_union_set_free(uset);
3315 isl_union_set_free(res);
3316 return NULL;
3319 /* Is the domain space of "map" equal to "space"?
3321 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3323 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3324 space, isl_dim_out);
3327 /* Is the range space of "map" equal to "space"?
3329 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3331 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3332 space, isl_dim_out);
3335 /* Is the set space of "map" equal to "space"?
3337 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3339 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3340 space, isl_dim_out);
3343 /* Internal data structure for preimage_pw_multi_aff.
3345 * "pma" is the function under which the preimage should be taken.
3346 * "space" is the space of "pma".
3347 * "res" collects the results.
3348 * "fn" computes the preimage for a given map.
3349 * "match" returns true if "fn" can be called.
3351 struct isl_union_map_preimage_data {
3352 isl_space *space;
3353 isl_pw_multi_aff *pma;
3354 isl_union_map *res;
3355 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3356 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3357 __isl_take isl_pw_multi_aff *pma);
3360 /* Call data->fn to compute the preimage of the domain or range of *entry
3361 * under the function represented by data->pma, provided the domain/range
3362 * space of *entry matches the target space of data->pma
3363 * (as given by data->match), and add the result to data->res.
3365 static isl_stat preimage_entry(void **entry, void *user)
3367 int m;
3368 isl_map *map = *entry;
3369 struct isl_union_map_preimage_data *data = user;
3370 isl_bool empty;
3372 m = data->match(map, data->space);
3373 if (m < 0)
3374 return isl_stat_error;
3375 if (!m)
3376 return isl_stat_ok;
3378 map = isl_map_copy(map);
3379 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3381 empty = isl_map_is_empty(map);
3382 if (empty < 0 || empty) {
3383 isl_map_free(map);
3384 return empty < 0 ? isl_stat_error : isl_stat_ok;
3387 data->res = isl_union_map_add_map(data->res, map);
3389 return isl_stat_ok;
3392 /* Compute the preimage of the domain or range of "umap" under the function
3393 * represented by "pma".
3394 * In other words, plug in "pma" in the domain or range of "umap".
3395 * The function "fn" performs the actual preimage computation on a map,
3396 * while "match" determines to which maps the function should be applied.
3398 static __isl_give isl_union_map *preimage_pw_multi_aff(
3399 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3400 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3401 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3402 __isl_take isl_pw_multi_aff *pma))
3404 isl_ctx *ctx;
3405 isl_space *space;
3406 struct isl_union_map_preimage_data data;
3408 umap = isl_union_map_align_params(umap,
3409 isl_pw_multi_aff_get_space(pma));
3410 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3412 if (!umap || !pma)
3413 goto error;
3415 ctx = isl_union_map_get_ctx(umap);
3416 space = isl_union_map_get_space(umap);
3417 data.space = isl_pw_multi_aff_get_space(pma);
3418 data.pma = pma;
3419 data.res = isl_union_map_alloc(space, umap->table.n);
3420 data.match = match;
3421 data.fn = fn;
3422 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3423 &data) < 0)
3424 data.res = isl_union_map_free(data.res);
3426 isl_space_free(data.space);
3427 isl_union_map_free(umap);
3428 isl_pw_multi_aff_free(pma);
3429 return data.res;
3430 error:
3431 isl_union_map_free(umap);
3432 isl_pw_multi_aff_free(pma);
3433 return NULL;
3436 /* Compute the preimage of the domain of "umap" under the function
3437 * represented by "pma".
3438 * In other words, plug in "pma" in the domain of "umap".
3439 * The result contains maps that live in the same spaces as the maps of "umap"
3440 * with domain space equal to the target space of "pma",
3441 * except that the domain has been replaced by the domain space of "pma".
3443 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3444 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3446 return preimage_pw_multi_aff(umap, pma, &domain_match,
3447 &isl_map_preimage_domain_pw_multi_aff);
3450 /* Compute the preimage of the range of "umap" under the function
3451 * represented by "pma".
3452 * In other words, plug in "pma" in the range of "umap".
3453 * The result contains maps that live in the same spaces as the maps of "umap"
3454 * with range space equal to the target space of "pma",
3455 * except that the range has been replaced by the domain space of "pma".
3457 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3458 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3460 return preimage_pw_multi_aff(umap, pma, &range_match,
3461 &isl_map_preimage_range_pw_multi_aff);
3464 /* Compute the preimage of "uset" under the function represented by "pma".
3465 * In other words, plug in "pma" in "uset".
3466 * The result contains sets that live in the same spaces as the sets of "uset"
3467 * with space equal to the target space of "pma",
3468 * except that the space has been replaced by the domain space of "pma".
3470 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3471 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3473 return preimage_pw_multi_aff(uset, pma, &set_match,
3474 &isl_set_preimage_pw_multi_aff);
3477 /* Compute the preimage of the domain of "umap" under the function
3478 * represented by "ma".
3479 * In other words, plug in "ma" in the domain of "umap".
3480 * The result contains maps that live in the same spaces as the maps of "umap"
3481 * with domain space equal to the target space of "ma",
3482 * except that the domain has been replaced by the domain space of "ma".
3484 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3485 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3487 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3488 isl_pw_multi_aff_from_multi_aff(ma));
3491 /* Compute the preimage of the range of "umap" under the function
3492 * represented by "ma".
3493 * In other words, plug in "ma" in the range of "umap".
3494 * The result contains maps that live in the same spaces as the maps of "umap"
3495 * with range space equal to the target space of "ma",
3496 * except that the range has been replaced by the domain space of "ma".
3498 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3499 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3501 return isl_union_map_preimage_range_pw_multi_aff(umap,
3502 isl_pw_multi_aff_from_multi_aff(ma));
3505 /* Compute the preimage of "uset" under the function represented by "ma".
3506 * In other words, plug in "ma" in "uset".
3507 * The result contains sets that live in the same spaces as the sets of "uset"
3508 * with space equal to the target space of "ma",
3509 * except that the space has been replaced by the domain space of "ma".
3511 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3512 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3514 return isl_union_set_preimage_pw_multi_aff(uset,
3515 isl_pw_multi_aff_from_multi_aff(ma));
3518 /* Internal data structure for preimage_multi_pw_aff.
3520 * "mpa" is the function under which the preimage should be taken.
3521 * "space" is the space of "mpa".
3522 * "res" collects the results.
3523 * "fn" computes the preimage for a given map.
3524 * "match" returns true if "fn" can be called.
3526 struct isl_union_map_preimage_mpa_data {
3527 isl_space *space;
3528 isl_multi_pw_aff *mpa;
3529 isl_union_map *res;
3530 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3531 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3532 __isl_take isl_multi_pw_aff *mpa);
3535 /* Call data->fn to compute the preimage of the domain or range of *entry
3536 * under the function represented by data->mpa, provided the domain/range
3537 * space of *entry matches the target space of data->mpa
3538 * (as given by data->match), and add the result to data->res.
3540 static isl_stat preimage_mpa_entry(void **entry, void *user)
3542 int m;
3543 isl_map *map = *entry;
3544 struct isl_union_map_preimage_mpa_data *data = user;
3545 isl_bool empty;
3547 m = data->match(map, data->space);
3548 if (m < 0)
3549 return isl_stat_error;
3550 if (!m)
3551 return isl_stat_ok;
3553 map = isl_map_copy(map);
3554 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3556 empty = isl_map_is_empty(map);
3557 if (empty < 0 || empty) {
3558 isl_map_free(map);
3559 return empty < 0 ? isl_stat_error : isl_stat_ok;
3562 data->res = isl_union_map_add_map(data->res, map);
3564 return isl_stat_ok;
3567 /* Compute the preimage of the domain or range of "umap" under the function
3568 * represented by "mpa".
3569 * In other words, plug in "mpa" in the domain or range of "umap".
3570 * The function "fn" performs the actual preimage computation on a map,
3571 * while "match" determines to which maps the function should be applied.
3573 static __isl_give isl_union_map *preimage_multi_pw_aff(
3574 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3575 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3576 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3577 __isl_take isl_multi_pw_aff *mpa))
3579 isl_ctx *ctx;
3580 isl_space *space;
3581 struct isl_union_map_preimage_mpa_data data;
3583 umap = isl_union_map_align_params(umap,
3584 isl_multi_pw_aff_get_space(mpa));
3585 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3587 if (!umap || !mpa)
3588 goto error;
3590 ctx = isl_union_map_get_ctx(umap);
3591 space = isl_union_map_get_space(umap);
3592 data.space = isl_multi_pw_aff_get_space(mpa);
3593 data.mpa = mpa;
3594 data.res = isl_union_map_alloc(space, umap->table.n);
3595 data.match = match;
3596 data.fn = fn;
3597 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3598 &data) < 0)
3599 data.res = isl_union_map_free(data.res);
3601 isl_space_free(data.space);
3602 isl_union_map_free(umap);
3603 isl_multi_pw_aff_free(mpa);
3604 return data.res;
3605 error:
3606 isl_union_map_free(umap);
3607 isl_multi_pw_aff_free(mpa);
3608 return NULL;
3611 /* Compute the preimage of the domain of "umap" under the function
3612 * represented by "mpa".
3613 * In other words, plug in "mpa" in the domain of "umap".
3614 * The result contains maps that live in the same spaces as the maps of "umap"
3615 * with domain space equal to the target space of "mpa",
3616 * except that the domain has been replaced by the domain space of "mpa".
3618 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3619 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3621 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3622 &isl_map_preimage_domain_multi_pw_aff);
3625 /* Internal data structure for preimage_upma.
3627 * "umap" is the map of which the preimage should be computed.
3628 * "res" collects the results.
3629 * "fn" computes the preimage for a given piecewise multi-affine function.
3631 struct isl_union_map_preimage_upma_data {
3632 isl_union_map *umap;
3633 isl_union_map *res;
3634 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3635 __isl_take isl_pw_multi_aff *pma);
3638 /* Call data->fn to compute the preimage of the domain or range of data->umap
3639 * under the function represented by pma and add the result to data->res.
3641 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3643 struct isl_union_map_preimage_upma_data *data = user;
3644 isl_union_map *umap;
3646 umap = isl_union_map_copy(data->umap);
3647 umap = data->fn(umap, pma);
3648 data->res = isl_union_map_union(data->res, umap);
3650 return data->res ? isl_stat_ok : isl_stat_error;
3653 /* Compute the preimage of the domain or range of "umap" under the function
3654 * represented by "upma".
3655 * In other words, plug in "upma" in the domain or range of "umap".
3656 * The function "fn" performs the actual preimage computation
3657 * on a piecewise multi-affine function.
3659 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3660 __isl_take isl_union_map *umap,
3661 __isl_take isl_union_pw_multi_aff *upma,
3662 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3663 __isl_take isl_pw_multi_aff *pma))
3665 struct isl_union_map_preimage_upma_data data;
3667 data.umap = umap;
3668 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3669 data.fn = fn;
3670 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3671 &preimage_upma, &data) < 0)
3672 data.res = isl_union_map_free(data.res);
3674 isl_union_map_free(umap);
3675 isl_union_pw_multi_aff_free(upma);
3677 return data.res;
3680 /* Compute the preimage of the domain of "umap" under the function
3681 * represented by "upma".
3682 * In other words, plug in "upma" in the domain of "umap".
3683 * The result contains maps that live in the same spaces as the maps of "umap"
3684 * with domain space equal to one of the target spaces of "upma",
3685 * except that the domain has been replaced by one of the domain spaces that
3686 * correspond to that target space of "upma".
3688 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3689 __isl_take isl_union_map *umap,
3690 __isl_take isl_union_pw_multi_aff *upma)
3692 return preimage_union_pw_multi_aff(umap, upma,
3693 &isl_union_map_preimage_domain_pw_multi_aff);
3696 /* Compute the preimage of the range of "umap" under the function
3697 * represented by "upma".
3698 * In other words, plug in "upma" in the range of "umap".
3699 * The result contains maps that live in the same spaces as the maps of "umap"
3700 * with range space equal to one of the target spaces of "upma",
3701 * except that the range has been replaced by one of the domain spaces that
3702 * correspond to that target space of "upma".
3704 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3705 __isl_take isl_union_map *umap,
3706 __isl_take isl_union_pw_multi_aff *upma)
3708 return preimage_union_pw_multi_aff(umap, upma,
3709 &isl_union_map_preimage_range_pw_multi_aff);
3712 /* Compute the preimage of "uset" under the function represented by "upma".
3713 * In other words, plug in "upma" in the range of "uset".
3714 * The result contains sets that live in the same spaces as the sets of "uset"
3715 * with space equal to one of the target spaces of "upma",
3716 * except that the space has been replaced by one of the domain spaces that
3717 * correspond to that target space of "upma".
3719 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3720 __isl_take isl_union_set *uset,
3721 __isl_take isl_union_pw_multi_aff *upma)
3723 return preimage_union_pw_multi_aff(uset, upma,
3724 &isl_union_set_preimage_pw_multi_aff);
3727 /* Reset the user pointer on all identifiers of parameters and tuples
3728 * of the spaces of "umap".
3730 __isl_give isl_union_map *isl_union_map_reset_user(
3731 __isl_take isl_union_map *umap)
3733 umap = isl_union_map_cow(umap);
3734 if (!umap)
3735 return NULL;
3736 umap->dim = isl_space_reset_user(umap->dim);
3737 if (!umap->dim)
3738 return isl_union_map_free(umap);
3739 return total(umap, &isl_map_reset_user);
3742 /* Reset the user pointer on all identifiers of parameters and tuples
3743 * of the spaces of "uset".
3745 __isl_give isl_union_set *isl_union_set_reset_user(
3746 __isl_take isl_union_set *uset)
3748 return isl_union_map_reset_user(uset);
3751 /* Remove all existentially quantified variables and integer divisions
3752 * from "umap" using Fourier-Motzkin elimination.
3754 __isl_give isl_union_map *isl_union_map_remove_divs(
3755 __isl_take isl_union_map *umap)
3757 return total(umap, &isl_map_remove_divs);
3760 /* Remove all existentially quantified variables and integer divisions
3761 * from "uset" using Fourier-Motzkin elimination.
3763 __isl_give isl_union_set *isl_union_set_remove_divs(
3764 __isl_take isl_union_set *uset)
3766 return isl_union_map_remove_divs(uset);
3769 /* Internal data structure for isl_union_map_project_out.
3770 * "type", "first" and "n" are the arguments for the isl_map_project_out
3771 * call.
3772 * "res" collects the results.
3774 struct isl_union_map_project_out_data {
3775 enum isl_dim_type type;
3776 unsigned first;
3777 unsigned n;
3779 isl_union_map *res;
3782 /* Turn the data->n dimensions of type data->type, starting at data->first
3783 * into existentially quantified variables and add the result to data->res.
3785 static isl_stat project_out(__isl_take isl_map *map, void *user)
3787 struct isl_union_map_project_out_data *data = user;
3789 map = isl_map_project_out(map, data->type, data->first, data->n);
3790 data->res = isl_union_map_add_map(data->res, map);
3792 return isl_stat_ok;
3795 /* Turn the "n" dimensions of type "type", starting at "first"
3796 * into existentially quantified variables.
3797 * Since the space of an isl_union_map only contains parameters,
3798 * type is required to be equal to isl_dim_param.
3800 __isl_give isl_union_map *isl_union_map_project_out(
3801 __isl_take isl_union_map *umap,
3802 enum isl_dim_type type, unsigned first, unsigned n)
3804 isl_space *space;
3805 struct isl_union_map_project_out_data data = { type, first, n };
3807 if (!umap)
3808 return NULL;
3810 if (type != isl_dim_param)
3811 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3812 "can only project out parameters",
3813 return isl_union_map_free(umap));
3815 space = isl_union_map_get_space(umap);
3816 space = isl_space_drop_dims(space, type, first, n);
3817 data.res = isl_union_map_empty(space);
3818 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3819 data.res = isl_union_map_free(data.res);
3821 isl_union_map_free(umap);
3823 return data.res;
3826 /* Project out all parameters from "umap" by existentially quantifying
3827 * over them.
3829 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3830 __isl_take isl_union_map *umap)
3832 isl_size n;
3834 n = isl_union_map_dim(umap, isl_dim_param);
3835 if (n < 0)
3836 return isl_union_map_free(umap);
3837 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3840 /* Turn the "n" dimensions of type "type", starting at "first"
3841 * into existentially quantified variables.
3842 * Since the space of an isl_union_set only contains parameters,
3843 * "type" is required to be equal to isl_dim_param.
3845 __isl_give isl_union_set *isl_union_set_project_out(
3846 __isl_take isl_union_set *uset,
3847 enum isl_dim_type type, unsigned first, unsigned n)
3849 return isl_union_map_project_out(uset, type, first, n);
3852 /* Project out all parameters from "uset" by existentially quantifying
3853 * over them.
3855 __isl_give isl_union_set *isl_union_set_project_out_all_params(
3856 __isl_take isl_union_set *uset)
3858 return uset_from_umap(
3859 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3862 /* Internal data structure for isl_union_map_involves_dims.
3863 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3865 struct isl_union_map_involves_dims_data {
3866 unsigned first;
3867 unsigned n;
3870 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3872 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3874 struct isl_union_map_involves_dims_data *data = user;
3875 isl_bool involves;
3877 involves = isl_map_involves_dims(map,
3878 isl_dim_param, data->first, data->n);
3879 return isl_bool_not(involves);
3882 /* Does "umap" involve any of the n parameters starting at first?
3883 * "type" is required to be set to isl_dim_param.
3885 * "umap" involves any of those parameters if any of its maps
3886 * involve the parameters. In other words, "umap" does not
3887 * involve any of the parameters if all its maps to not
3888 * involve the parameters.
3890 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3891 enum isl_dim_type type, unsigned first, unsigned n)
3893 struct isl_union_map_involves_dims_data data = { first, n };
3894 isl_bool excludes;
3896 if (type != isl_dim_param)
3897 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3898 "can only reference parameters", return isl_bool_error);
3900 excludes = union_map_forall_user(umap, &map_excludes, &data);
3902 return isl_bool_not(excludes);
3905 /* Internal data structure for isl_union_map_reset_range_space.
3906 * "range" is the space from which to set the range space.
3907 * "res" collects the results.
3909 struct isl_union_map_reset_range_space_data {
3910 isl_space *range;
3911 isl_union_map *res;
3914 /* Replace the range space of "map" by the range space of data->range and
3915 * add the result to data->res.
3917 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3919 struct isl_union_map_reset_range_space_data *data = user;
3920 isl_space *space;
3922 space = isl_map_get_space(map);
3923 space = isl_space_domain(space);
3924 space = isl_space_extend_domain_with_range(space,
3925 isl_space_copy(data->range));
3926 map = isl_map_reset_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 range space of all the maps in "umap" by
3933 * the range space of "space".
3935 * This assumes that all maps have the same output dimension.
3936 * This function should therefore not be made publicly available.
3938 * Since the spaces of the maps change, so do their hash value.
3939 * We therefore need to create a new isl_union_map.
3941 __isl_give isl_union_map *isl_union_map_reset_range_space(
3942 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3944 struct isl_union_map_reset_range_space_data data = { space };
3946 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3947 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3948 data.res = isl_union_map_free(data.res);
3950 isl_space_free(space);
3951 isl_union_map_free(umap);
3952 return data.res;
3955 /* Check that "umap" and "space" have the same number of parameters.
3957 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3958 __isl_keep isl_space *space)
3960 isl_size dim1, dim2;
3962 dim1 = isl_union_map_dim(umap, isl_dim_param);
3963 dim2 = isl_space_dim(space, isl_dim_param);
3964 if (dim1 < 0 || dim2 < 0)
3965 return isl_stat_error;
3966 if (dim1 == dim2)
3967 return isl_stat_ok;
3968 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3969 "number of parameters does not match", return isl_stat_error);
3972 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3973 * "space" is the target space.
3974 * "res" collects the results.
3976 struct isl_union_map_reset_params_data {
3977 isl_space *space;
3978 isl_union_map *res;
3981 /* Replace the parameters of "map" by those of data->space and
3982 * add the result to data->res.
3984 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3986 struct isl_union_map_reset_params_data *data = user;
3987 isl_space *space;
3989 space = isl_map_get_space(map);
3990 space = isl_space_replace_params(space, data->space);
3991 map = isl_map_reset_equal_dim_space(map, space);
3992 data->res = isl_union_map_add_map(data->res, map);
3994 return data->res ? isl_stat_ok : isl_stat_error;
3997 /* Replace the space of "umap" by "space", without modifying
3998 * the dimension of "umap", i.e., the number of parameters of "umap".
4000 * Since the hash values of the maps in the union map depend
4001 * on the parameters, a new union map needs to be constructed.
4003 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
4004 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4006 struct isl_union_map_reset_params_data data = { space };
4007 isl_bool equal;
4008 isl_space *umap_space;
4010 umap_space = isl_union_map_peek_space(umap);
4011 equal = isl_space_is_equal(umap_space, space);
4012 if (equal < 0)
4013 goto error;
4014 if (equal) {
4015 isl_space_free(space);
4016 return umap;
4018 if (check_union_map_space_equal_dim(umap, space) < 0)
4019 goto error;
4021 data.res = isl_union_map_empty(isl_space_copy(space));
4022 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
4023 data.res = isl_union_map_free(data.res);
4025 isl_space_free(space);
4026 isl_union_map_free(umap);
4027 return data.res;
4028 error:
4029 isl_union_map_free(umap);
4030 isl_space_free(space);
4031 return NULL;
4034 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4035 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4036 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4037 * to the domain and the range of each map.
4038 * "res" collects the results.
4040 struct isl_union_order_at_data {
4041 isl_multi_union_pw_aff *mupa;
4042 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4043 __isl_take isl_multi_pw_aff *mpa2);
4044 isl_union_map *res;
4047 /* Intersect "map" with the result of applying data->order to
4048 * the functions in data->mupa that apply to the domain and the range
4049 * of "map" and add the result to data->res.
4051 static isl_stat order_at(__isl_take isl_map *map, void *user)
4053 struct isl_union_order_at_data *data = user;
4054 isl_space *space;
4055 isl_multi_pw_aff *mpa1, *mpa2;
4056 isl_map *order;
4058 space = isl_space_domain(isl_map_get_space(map));
4059 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4060 space = isl_space_range(isl_map_get_space(map));
4061 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4062 order = data->order(mpa1, mpa2);
4063 map = isl_map_intersect(map, order);
4064 data->res = isl_union_map_add_map(data->res, map);
4066 return data->res ? isl_stat_ok : isl_stat_error;
4069 /* If "mupa" has a non-trivial explicit domain, then intersect
4070 * domain and range of "umap" with this explicit domain.
4071 * If the explicit domain only describes constraints on the parameters,
4072 * then the intersection only needs to be performed once.
4074 static __isl_give isl_union_map *intersect_explicit_domain(
4075 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4077 isl_bool non_trivial, is_params;
4078 isl_union_set *dom;
4080 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4081 if (non_trivial < 0)
4082 return isl_union_map_free(umap);
4083 if (!non_trivial)
4084 return umap;
4085 mupa = isl_multi_union_pw_aff_copy(mupa);
4086 dom = isl_multi_union_pw_aff_domain(mupa);
4087 is_params = isl_union_set_is_params(dom);
4088 if (is_params < 0) {
4089 isl_union_set_free(dom);
4090 return isl_union_map_free(umap);
4092 if (is_params) {
4093 isl_set *set;
4095 set = isl_union_set_params(dom);
4096 umap = isl_union_map_intersect_params(umap, set);
4097 return umap;
4099 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4100 umap = isl_union_map_intersect_range(umap, dom);
4101 return umap;
4104 /* Intersect each map in "umap" with the result of calling "order"
4105 * on the functions is "mupa" that apply to the domain and the range
4106 * of the map.
4108 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4109 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4110 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4111 __isl_take isl_multi_pw_aff *mpa2))
4113 struct isl_union_order_at_data data;
4115 umap = isl_union_map_align_params(umap,
4116 isl_multi_union_pw_aff_get_space(mupa));
4117 mupa = isl_multi_union_pw_aff_align_params(mupa,
4118 isl_union_map_get_space(umap));
4119 umap = intersect_explicit_domain(umap, mupa);
4120 data.mupa = mupa;
4121 data.order = order;
4122 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4123 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4124 data.res = isl_union_map_free(data.res);
4126 isl_multi_union_pw_aff_free(mupa);
4127 isl_union_map_free(umap);
4128 return data.res;
4131 /* Return the subset of "umap" where the domain and the range
4132 * have equal "mupa" values.
4134 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4135 __isl_take isl_union_map *umap,
4136 __isl_take isl_multi_union_pw_aff *mupa)
4138 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4139 &isl_multi_pw_aff_eq_map);
4142 /* Return the subset of "umap" where the domain has a lexicographically
4143 * smaller "mupa" value than the range.
4145 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4146 __isl_take isl_union_map *umap,
4147 __isl_take isl_multi_union_pw_aff *mupa)
4149 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4150 &isl_multi_pw_aff_lex_lt_map);
4153 /* Return the subset of "umap" where the domain has a lexicographically
4154 * greater "mupa" value than the range.
4156 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4157 __isl_take isl_union_map *umap,
4158 __isl_take isl_multi_union_pw_aff *mupa)
4160 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4161 &isl_multi_pw_aff_lex_gt_map);
4164 /* Return the union of the elements in the list "list".
4166 __isl_give isl_union_set *isl_union_set_list_union(
4167 __isl_take isl_union_set_list *list)
4169 int i;
4170 isl_size n;
4171 isl_ctx *ctx;
4172 isl_space *space;
4173 isl_union_set *res;
4175 n = isl_union_set_list_n_union_set(list);
4176 if (n < 0)
4177 goto error;
4179 ctx = isl_union_set_list_get_ctx(list);
4180 space = isl_space_params_alloc(ctx, 0);
4181 res = isl_union_set_empty(space);
4183 for (i = 0; i < n; ++i) {
4184 isl_union_set *uset_i;
4186 uset_i = isl_union_set_list_get_union_set(list, i);
4187 res = isl_union_set_union(res, uset_i);
4190 isl_union_set_list_free(list);
4191 return res;
4192 error:
4193 isl_union_set_list_free(list);
4194 return NULL;
4197 /* Update *hash with the hash value of "map".
4199 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4201 uint32_t *hash = user;
4202 uint32_t map_hash;
4204 map_hash = isl_map_get_hash(map);
4205 isl_hash_hash(*hash, map_hash);
4207 isl_map_free(map);
4208 return isl_stat_ok;
4211 /* Return a hash value that digests "umap".
4213 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4215 uint32_t hash;
4217 if (!umap)
4218 return 0;
4220 hash = isl_hash_init();
4221 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4222 return 0;
4224 return hash;
4227 /* Return a hash value that digests "uset".
4229 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4231 return isl_union_map_get_hash(uset);
4234 /* Add the number of basic sets in "set" to "n".
4236 static isl_stat add_n(__isl_take isl_set *set, void *user)
4238 int *n = user;
4239 isl_size set_n;
4241 set_n = isl_set_n_basic_set(set);
4242 *n += set_n;
4243 isl_set_free(set);
4245 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4248 /* Return the total number of basic sets in "uset".
4250 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4252 int n = 0;
4254 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4255 return -1;
4257 return n;
4260 /* Add the basic sets in "set" to "list".
4262 static isl_stat add_list(__isl_take isl_set *set, void *user)
4264 isl_basic_set_list **list = user;
4265 isl_basic_set_list *list_i;
4267 list_i = isl_set_get_basic_set_list(set);
4268 *list = isl_basic_set_list_concat(*list, list_i);
4269 isl_set_free(set);
4271 if (!*list)
4272 return isl_stat_error;
4273 return isl_stat_ok;
4276 /* Return a list containing all the basic sets in "uset".
4278 * First construct a list of the appropriate size and
4279 * then add all the elements.
4281 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4282 __isl_keep isl_union_set *uset)
4284 int n;
4285 isl_ctx *ctx;
4286 isl_basic_set_list *list;
4288 if (!uset)
4289 return NULL;
4290 ctx = isl_union_set_get_ctx(uset);
4291 n = isl_union_set_n_basic_set(uset);
4292 if (n < 0)
4293 return NULL;
4294 list = isl_basic_set_list_alloc(ctx, n);
4295 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4296 list = isl_basic_set_list_free(list);
4298 return list;
4301 /* Internal data structure for isl_union_map_remove_map_if.
4302 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4304 struct isl_union_map_remove_map_if_data {
4305 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4306 void *user;
4309 /* isl_un_op_control filter that negates the result of data->fn
4310 * called on "map".
4312 static isl_bool not(__isl_keep isl_map *map, void *user)
4314 struct isl_union_map_remove_map_if_data *data = user;
4316 return isl_bool_not(data->fn(map, data->user));
4319 /* Dummy isl_un_op_control transformation callback that
4320 * simply returns the input.
4322 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4324 return map;
4327 /* Call "fn" on every map in "umap" and remove those maps
4328 * for which the callback returns true.
4330 * Use un_op to keep only those maps that are not filtered out,
4331 * applying an identity transformation on them.
4333 __isl_give isl_union_map *isl_union_map_remove_map_if(
4334 __isl_take isl_union_map *umap,
4335 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4337 struct isl_union_map_remove_map_if_data data = { fn, user };
4338 struct isl_un_op_control control = {
4339 .filter = &not,
4340 .filter_user = &data,
4341 .fn_map = &map_id,
4343 return un_op(umap, &control);
4346 /* Does "map" have "space" as range (ignoring parameters)?
4348 static isl_bool has_range_space(__isl_keep isl_map *map, void *user)
4350 isl_space *space = user;
4352 return isl_space_has_range_tuples(space, isl_map_peek_space(map));
4355 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4357 static __isl_give isl_map *bind_range(__isl_take isl_map *map, void *user)
4359 isl_multi_id *tuple = user;
4361 return isl_map_bind_range(map, isl_multi_id_copy(tuple));
4364 /* Bind the output dimensions of "umap" to parameters with identifiers
4365 * specified by "tuple", living in the range space of "umap",
4366 * for those maps that have a matching range space.
4368 __isl_give isl_union_set *isl_union_map_bind_range(
4369 __isl_take isl_union_map *umap, __isl_take isl_multi_id *tuple)
4371 struct isl_un_op_control control = {
4372 .filter = &has_range_space,
4373 .filter_user = isl_multi_id_peek_space(tuple),
4374 .fn_map2 = &bind_range,
4375 .fn_map2_user = tuple,
4377 isl_union_set *bound;
4379 bound = uset_from_umap(un_op(umap, &control));
4380 isl_multi_id_free(tuple);
4381 return bound;