interface: update to move of llvm/Support/Host.h to llvm/TargetParser/Host.h
[isl.git] / isl_union_map.c
blob12b4404a372d736e344097807c72a5f6d0c946a9
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 #undef TYPE
36 #define TYPE isl_union_map
37 static
38 #include "has_single_reference_templ.c"
39 static
40 #include "check_single_reference_templ.c"
42 /* Return the number of parameters of "umap", where "type"
43 * is required to be set to isl_dim_param.
45 isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
46 enum isl_dim_type type)
48 if (!umap)
49 return isl_size_error;
51 if (type != isl_dim_param)
52 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
53 "can only reference parameters", return isl_size_error);
55 return isl_space_dim(umap->dim, type);
58 /* Return the number of parameters of "uset", where "type"
59 * is required to be set to isl_dim_param.
61 isl_size isl_union_set_dim(__isl_keep isl_union_set *uset,
62 enum isl_dim_type type)
64 return isl_union_map_dim(uset, type);
67 /* Return the id of the specified dimension.
69 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
70 enum isl_dim_type type, unsigned pos)
72 if (!umap)
73 return NULL;
75 if (type != isl_dim_param)
76 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
77 "can only reference parameters", return NULL);
79 return isl_space_get_dim_id(umap->dim, type, pos);
82 /* Is this union set a parameter domain?
84 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
86 isl_set *set;
87 isl_bool params;
89 if (!uset)
90 return isl_bool_error;
91 if (uset->table.n != 1)
92 return isl_bool_false;
94 set = isl_set_from_union_set(isl_union_set_copy(uset));
95 params = isl_set_is_params(set);
96 isl_set_free(set);
97 return params;
100 /* Is this union map actually a parameter domain?
101 * Users should never call this function. Outside of isl,
102 * a union map can never be a parameter domain.
104 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
106 return isl_union_set_is_params(uset_from_umap(umap));
109 static __isl_give isl_union_map *isl_union_map_alloc(
110 __isl_take isl_space *space, int size)
112 isl_union_map *umap;
114 space = isl_space_params(space);
115 if (!space)
116 return NULL;
118 umap = isl_calloc_type(space->ctx, isl_union_map);
119 if (!umap) {
120 isl_space_free(space);
121 return NULL;
124 umap->ref = 1;
125 umap->dim = space;
126 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
127 return isl_union_map_free(umap);
129 return umap;
132 /* Create an empty union map without specifying any parameters.
134 __isl_give isl_union_map *isl_union_map_empty_ctx(isl_ctx *ctx)
136 return isl_union_map_empty_space(isl_space_unit(ctx));
139 __isl_give isl_union_map *isl_union_map_empty_space(__isl_take isl_space *space)
141 return isl_union_map_alloc(space, 16);
144 /* This is an alternative name for the function above.
146 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
148 return isl_union_map_empty_space(space);
151 /* Create an empty union set without specifying any parameters.
153 __isl_give isl_union_set *isl_union_set_empty_ctx(isl_ctx *ctx)
155 return uset_from_umap(isl_union_map_empty_ctx(ctx));
158 __isl_give isl_union_set *isl_union_set_empty_space(__isl_take isl_space *space)
160 return uset_from_umap(isl_union_map_empty_space(space));
163 /* This is an alternative name for the function above.
165 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
167 return isl_union_set_empty_space(space);
170 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
172 return umap ? umap->dim->ctx : NULL;
175 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
177 return uset ? uset->dim->ctx : NULL;
180 /* Return the space of "umap".
182 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
184 return umap ? umap->dim : NULL;
187 /* Return the space of "uset".
189 __isl_keep isl_space *isl_union_set_peek_space(__isl_keep isl_union_set *uset)
191 return isl_union_map_peek_space(uset_to_umap(uset));
194 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
196 return isl_space_copy(isl_union_map_peek_space(umap));
199 /* Return the position of the parameter with the given name
200 * in "umap".
201 * Return -1 if no such dimension can be found.
203 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
204 enum isl_dim_type type, const char *name)
206 if (!umap)
207 return -1;
208 return isl_space_find_dim_by_name(umap->dim, type, name);
211 /* Return the position of the parameter with id "id" in "umap".
212 * Return -1 if no such dimension can be found.
214 static int isl_union_map_find_dim_by_id(__isl_keep isl_union_map *umap,
215 enum isl_dim_type type, __isl_keep isl_id *id)
217 isl_space *space;
219 space = isl_union_map_peek_space(umap);
220 return isl_space_find_dim_by_id(space, type, id);
223 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
225 return isl_union_map_get_space(uset);
228 static isl_stat free_umap_entry(void **entry, void *user)
230 isl_map *map = *entry;
231 isl_map_free(map);
232 return isl_stat_ok;
235 static isl_stat add_map(__isl_take isl_map *map, void *user)
237 isl_union_map **umap = (isl_union_map **)user;
239 *umap = isl_union_map_add_map(*umap, map);
241 return isl_stat_ok;
244 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
246 isl_union_map *dup;
248 if (!umap)
249 return NULL;
251 dup = isl_union_map_empty(isl_space_copy(umap->dim));
252 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
253 goto error;
254 return dup;
255 error:
256 isl_union_map_free(dup);
257 return NULL;
260 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
262 if (!umap)
263 return NULL;
265 if (umap->ref == 1)
266 return umap;
267 umap->ref--;
268 return isl_union_map_dup(umap);
271 struct isl_union_align {
272 isl_reordering *exp;
273 isl_union_map *res;
276 static isl_stat align_entry(void **entry, void *user)
278 isl_map *map = *entry;
279 isl_reordering *exp;
280 struct isl_union_align *data = user;
282 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
283 isl_map_get_space(map));
285 data->res = isl_union_map_add_map(data->res,
286 isl_map_realign(isl_map_copy(map), exp));
288 return isl_stat_ok;
291 /* Align the parameters of umap along those of model.
292 * The result has the parameters of model first, in the same order
293 * as they appear in model, followed by any remaining parameters of
294 * umap that do not appear in model.
296 __isl_give isl_union_map *isl_union_map_align_params(
297 __isl_take isl_union_map *umap, __isl_take isl_space *model)
299 struct isl_union_align data = { NULL, NULL };
300 isl_space *space;
301 isl_bool equal_params;
303 space = isl_union_map_peek_space(umap);
304 equal_params = isl_space_has_equal_params(space, model);
305 if (equal_params < 0)
306 goto error;
307 if (equal_params) {
308 isl_space_free(model);
309 return umap;
312 data.exp = isl_parameter_alignment_reordering(space, model);
313 if (!data.exp)
314 goto error;
316 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
317 umap->table.n);
318 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
319 &align_entry, &data) < 0)
320 goto error;
322 isl_reordering_free(data.exp);
323 isl_union_map_free(umap);
324 isl_space_free(model);
325 return data.res;
326 error:
327 isl_reordering_free(data.exp);
328 isl_union_map_free(umap);
329 isl_union_map_free(data.res);
330 isl_space_free(model);
331 return NULL;
334 __isl_give isl_union_set *isl_union_set_align_params(
335 __isl_take isl_union_set *uset, __isl_take isl_space *model)
337 return isl_union_map_align_params(uset, model);
340 /* This is a wrapper around isl_union_map_project_out for use
341 * by isl_union_map_drop_unused_params.
343 * In particular, this function is only called on parameters
344 * that are not involved in the description of "umap".
345 * Dropping those parameters is therefore equivalent
346 * to projecting them out.
348 static __isl_give isl_union_map *isl_union_map_drop_dims(
349 __isl_take isl_union_map *umap,
350 enum isl_dim_type type, unsigned first, unsigned n)
352 return isl_union_map_project_out(umap, type, first, n);
355 #undef TYPE
356 #define TYPE isl_union_map
357 #include "isl_check_named_params_templ.c"
358 #include "isl_drop_unused_params_templ.c"
360 /* Drop all parameters not referenced by "uset".
362 __isl_give isl_union_set *isl_union_set_drop_unused_params(
363 __isl_take isl_union_set *uset)
365 isl_union_map *umap;
367 umap = isl_union_map_drop_unused_params(uset_to_umap(uset));
368 return uset_from_umap(umap);
371 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
372 __isl_take isl_union_map *umap2)
374 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
375 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
377 umap1 = isl_union_map_cow(umap1);
379 if (!umap1 || !umap2)
380 goto error;
382 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
383 goto error;
385 isl_union_map_free(umap2);
387 return umap1;
388 error:
389 isl_union_map_free(umap1);
390 isl_union_map_free(umap2);
391 return NULL;
394 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
395 __isl_take isl_union_set *uset2)
397 return isl_union_map_union(uset1, uset2);
400 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
402 if (!umap)
403 return NULL;
405 umap->ref++;
406 return umap;
409 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
411 return isl_union_map_copy(uset);
414 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
416 if (!umap)
417 return NULL;
419 if (--umap->ref > 0)
420 return NULL;
422 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
423 &free_umap_entry, NULL);
424 isl_hash_table_clear(&umap->table);
425 isl_space_free(umap->dim);
426 free(umap);
427 return NULL;
430 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
432 return isl_union_map_free(uset);
435 /* Do "umap" and "space" have the same parameters?
437 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
438 __isl_keep isl_space *space)
440 isl_space *umap_space;
442 umap_space = isl_union_map_peek_space(umap);
443 return isl_space_has_equal_params(umap_space, space);
446 /* Do "uset" and "space" have the same parameters?
448 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
449 __isl_keep isl_space *space)
451 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
454 /* Is the space of the map at "entry" equal to "space", ignoring parameters?
456 static isl_bool has_space_tuples(const void *entry, const void *val)
458 isl_map *map = (isl_map *)entry;
459 isl_space *space = (isl_space *) val;
461 return isl_map_has_space_tuples(map, space);
464 /* Find the entry in "umap" with space "space" (ignoring parameters),
465 * returning isl_hash_table_entry_none if no such entry appears in "umap" and
466 * NULL on error.
467 * If "reserve" is set, then an entry is created if it does
468 * not exist already. Since this modifies the hash table in-place,
469 * this means "umap" must have a single reference when "reserve" is set.
471 static struct isl_hash_table_entry *isl_union_map_find_entry(
472 __isl_keep isl_union_map *umap, __isl_keep isl_space *space,
473 int reserve)
475 uint32_t hash;
477 if (!umap || !space)
478 return NULL;
479 if (reserve && isl_union_map_check_single_reference(umap) < 0)
480 return NULL;
482 hash = isl_space_get_tuple_hash(space);
483 return isl_hash_table_find(isl_union_map_get_ctx(umap), &umap->table,
484 hash, &has_space_tuples, space, reserve);
487 /* Find the entry in "uset" with space "space" (ignoring parameters),
488 * returning isl_hash_table_entry_none if no such entry appears in "uset" and
489 * NULL on error.
490 * If "reserve" is set, then an entry is created if it does
491 * not exist already. In this case, a NULL return indicates an error.
493 struct isl_hash_table_entry *isl_union_set_find_entry(
494 __isl_keep isl_union_set *uset, __isl_keep isl_space *space,
495 int reserve)
497 return isl_union_map_find_entry(uset_to_umap(uset), space, reserve);
500 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
501 __isl_take isl_map *map)
503 struct isl_hash_table_entry *entry;
504 isl_bool aligned;
505 isl_space *space;
507 if (!map || !umap)
508 goto error;
510 if (isl_map_plain_is_empty(map)) {
511 isl_map_free(map);
512 return umap;
515 aligned = isl_map_space_has_equal_params(map, umap->dim);
516 if (aligned < 0)
517 goto error;
518 if (!aligned) {
519 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
520 map = isl_map_align_params(map, isl_union_map_get_space(umap));
523 umap = isl_union_map_cow(umap);
525 space = isl_map_peek_space(map);
526 entry = isl_union_map_find_entry(umap, space, 1);
527 if (!entry)
528 goto error;
530 if (!entry->data)
531 entry->data = map;
532 else {
533 entry->data = isl_map_union(entry->data, isl_map_copy(map));
534 if (!entry->data)
535 goto error;
536 isl_map_free(map);
539 return umap;
540 error:
541 isl_map_free(map);
542 isl_union_map_free(umap);
543 return NULL;
546 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
547 __isl_take isl_set *set)
549 return isl_union_map_add_map(uset, set_to_map(set));
552 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
554 isl_space *space;
555 isl_union_map *umap;
557 if (!map)
558 return NULL;
560 space = isl_map_get_space(map);
561 space = isl_space_params(space);
562 umap = isl_union_map_empty(space);
563 umap = isl_union_map_add_map(umap, map);
565 return umap;
568 /* This function performs the same operation as isl_union_map_from_map,
569 * but is considered as a function on an isl_map when exported.
571 __isl_give isl_union_map *isl_map_to_union_map(__isl_take isl_map *map)
573 return isl_union_map_from_map(map);
576 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
578 return isl_union_map_from_map(set_to_map(set));
581 /* This function performs the same operation as isl_union_set_from_set,
582 * but is considered as a function on an isl_set when exported.
584 __isl_give isl_union_set *isl_set_to_union_set(__isl_take isl_set *set)
586 return isl_union_set_from_set(set);
589 __isl_give isl_union_map *isl_union_map_from_basic_map(
590 __isl_take isl_basic_map *bmap)
592 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
595 __isl_give isl_union_set *isl_union_set_from_basic_set(
596 __isl_take isl_basic_set *bset)
598 return isl_union_map_from_basic_map(bset);
601 struct isl_union_map_foreach_data
603 isl_stat (*fn)(__isl_take isl_map *map, void *user);
604 void *user;
607 static isl_stat call_on_copy(void **entry, void *user)
609 isl_map *map = *entry;
610 struct isl_union_map_foreach_data *data;
611 data = (struct isl_union_map_foreach_data *)user;
613 return data->fn(isl_map_copy(map), data->user);
616 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
618 return umap ? umap->table.n : isl_size_error;
621 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
623 return uset ? uset->table.n : isl_size_error;
626 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
627 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
629 struct isl_union_map_foreach_data data = { fn, user };
631 if (!umap)
632 return isl_stat_error;
634 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
635 &call_on_copy, &data);
638 /* Internal data structure for isl_union_map_every_map.
640 * "test" is the user-specified callback function.
641 * "user" is the user-specified callback function argument.
643 * "failed" is initialized to 0 and set to 1 if "test" fails
644 * on any map.
646 struct isl_union_map_every_data {
647 isl_bool (*test)(__isl_keep isl_map *map, void *user);
648 void *user;
649 int failed;
652 /* Call data->test on "map".
653 * If this fails, then set data->failed and abort.
655 static isl_stat call_every(void **entry, void *user)
657 isl_map *map = *entry;
658 struct isl_union_map_every_data *data = user;
659 isl_bool r;
661 r = data->test(map, data->user);
662 if (r < 0)
663 return isl_stat_error;
664 if (r)
665 return isl_stat_ok;
666 data->failed = 1;
667 return isl_stat_error;
670 /* Does "test" succeed on every map in "umap"?
672 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
673 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
675 struct isl_union_map_every_data data = { test, user, 0 };
676 isl_stat r;
678 if (!umap)
679 return isl_bool_error;
681 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
682 &call_every, &data);
683 if (r >= 0)
684 return isl_bool_true;
685 if (data.failed)
686 return isl_bool_false;
687 return isl_bool_error;
690 /* Add "map" to "list".
692 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
694 isl_map_list **list = user;
696 *list = isl_map_list_add(*list, map);
698 if (!*list)
699 return isl_stat_error;
700 return isl_stat_ok;
703 /* Return the maps in "umap" as a list.
705 * First construct a list of the appropriate size and then add all the
706 * elements.
708 __isl_give isl_map_list *isl_union_map_get_map_list(
709 __isl_keep isl_union_map *umap)
711 isl_size n_maps;
712 isl_ctx *ctx;
713 isl_map_list *list;
715 n_maps = isl_union_map_n_map(umap);
716 if (n_maps < 0)
717 return NULL;
718 ctx = isl_union_map_get_ctx(umap);
719 list = isl_map_list_alloc(ctx, n_maps);
721 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
722 list = isl_map_list_free(list);
724 return list;
727 /* Return the sets in "uset" as a list.
729 __isl_give isl_set_list *isl_union_set_get_set_list(
730 __isl_keep isl_union_set *uset)
732 return set_list_from_map_list(
733 isl_union_map_get_map_list(uset_to_umap(uset)));
736 /* Can "umap" be converted to an isl_map?
737 * That is, does it contain elements in exactly one space?
739 isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
741 isl_size n;
743 n = isl_union_map_n_map(umap);
744 if (n < 0)
745 return isl_bool_error;
746 return isl_bool_ok(n == 1);
749 /* Can "uset" be converted to an isl_set?
750 * That is, does it contain elements in exactly one space?
752 isl_bool isl_union_set_isa_set(__isl_keep isl_union_set *uset)
754 return isl_union_map_isa_map(uset_to_umap(uset));
757 static isl_stat copy_map(void **entry, void *user)
759 isl_map *map = *entry;
760 isl_map **map_p = user;
762 *map_p = isl_map_copy(map);
764 return isl_stat_error;
767 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
769 isl_bool is_map;
770 isl_ctx *ctx;
771 isl_map *map = NULL;
773 is_map = isl_union_map_isa_map(umap);
774 if (is_map < 0)
775 goto error;
776 ctx = isl_union_map_get_ctx(umap);
777 if (!is_map)
778 isl_die(ctx, isl_error_invalid,
779 "union map needs to contain elements in exactly "
780 "one space", goto error);
782 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
784 isl_union_map_free(umap);
786 return map;
787 error:
788 isl_union_map_free(umap);
789 return NULL;
792 /* This function performs the same operation as isl_map_from_union_map,
793 * but is considered as a function on an isl_union_map when exported.
795 __isl_give isl_map *isl_union_map_as_map(__isl_take isl_union_map *umap)
797 return isl_map_from_union_map(umap);
800 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
802 return isl_map_from_union_map(uset);
805 /* This function performs the same operation as isl_set_from_union_set,
806 * but is considered as a function on an isl_union_set when exported.
808 __isl_give isl_set *isl_union_set_as_set(__isl_take isl_union_set *uset)
810 return isl_set_from_union_set(uset);
813 /* Extract the map in "umap" that lives in the given space (ignoring
814 * parameters).
816 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
817 __isl_take isl_space *space)
819 struct isl_hash_table_entry *entry;
821 entry = isl_union_map_find_entry(umap, space, 0);
822 if (!entry)
823 goto error;
824 if (entry == isl_hash_table_entry_none)
825 return isl_map_empty(space);
826 isl_space_free(space);
827 return isl_map_copy(entry->data);
828 error:
829 isl_space_free(space);
830 return NULL;
833 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
834 __isl_take isl_space *space)
836 return set_from_map(isl_union_map_extract_map(uset, space));
839 /* Check if umap contains a map in the given space (ignoring parameters).
841 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
842 __isl_keep isl_space *space)
844 struct isl_hash_table_entry *entry;
846 space = isl_space_drop_all_params(isl_space_copy(space));
847 space = isl_space_align_params(space, isl_union_map_get_space(umap));
848 entry = isl_union_map_find_entry(umap, space, 0);
849 isl_space_free(space);
850 if (!entry)
851 return isl_bool_error;
852 return isl_bool_ok(entry != isl_hash_table_entry_none);
855 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
856 __isl_keep isl_space *space)
858 return isl_union_map_contains(uset, space);
861 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
862 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
864 return isl_union_map_foreach_map(uset,
865 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
868 /* Internal data structure for isl_union_set_every_set.
870 * "test" is the user-specified callback function.
871 * "user" is the user-specified callback function argument.
873 struct isl_test_set_from_map_data {
874 isl_bool (*test)(__isl_keep isl_set *set, void *user);
875 void *user;
878 /* Call data->test on "map", which is part of an isl_union_set and
879 * therefore known to be an isl_set.
881 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
883 struct isl_test_set_from_map_data *data = user;
885 return data->test(set_from_map(map), data->user);
888 /* Does "test" succeed on every set in "uset"?
890 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
891 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
893 struct isl_test_set_from_map_data data = { test, user };
895 return isl_union_map_every_map(uset_to_umap(uset),
896 &test_set_from_map, &data);
899 struct isl_union_set_foreach_point_data {
900 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
901 void *user;
904 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
906 struct isl_union_set_foreach_point_data *data = user;
907 isl_stat r;
909 r = isl_set_foreach_point(set, data->fn, data->user);
910 isl_set_free(set);
912 return r;
915 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
916 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
918 struct isl_union_set_foreach_point_data data = { fn, user };
919 return isl_union_set_foreach_set(uset, &foreach_point, &data);
922 /* Data structure that specifies how gen_bin_op should
923 * construct results from the inputs.
925 * If "subtract" is set, then a map in the first input is copied to the result
926 * if there is no corresponding map in the second input.
927 * Otherwise, a map in the first input with no corresponding map
928 * in the second input is ignored.
929 * If "filter" is not NULL, then it specifies which maps in the first
930 * input may have a matching map in the second input.
931 * In particular, it makes sure that "match_space" can be called
932 * on the space of the map.
933 * "match_space" specifies how to transform the space of a map
934 * in the first input to the space of the corresponding map
935 * in the second input.
936 * "fn_map" specifies how the matching maps, one from each input,
937 * should be combined to form a map in the result.
939 struct isl_bin_op_control {
940 int subtract;
941 isl_bool (*filter)(__isl_keep isl_map *map);
942 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
943 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
944 __isl_take isl_map *map2);
947 /* Internal data structure for gen_bin_op.
948 * "control" specifies how the maps in the result should be constructed.
949 * "umap2" is a pointer to the second argument.
950 * "res" collects the results.
952 struct isl_union_map_gen_bin_data {
953 struct isl_bin_op_control *control;
954 isl_union_map *umap2;
955 isl_union_map *res;
958 /* Add a copy of "map" to "res" and return the result.
960 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
961 __isl_keep isl_map *map)
963 return isl_union_map_add_map(res, isl_map_copy(map));
966 /* Combine "map1" and "map2", add the result to "res" and return the result.
967 * Check whether the result is empty before adding it to "res".
969 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
970 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
971 struct isl_union_map_gen_bin_data *data)
973 isl_bool empty;
974 isl_map *map;
976 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
977 empty = isl_map_is_empty(map);
978 if (empty < 0 || empty) {
979 isl_map_free(map);
980 if (empty < 0)
981 return isl_union_map_free(res);
982 return res;
984 return isl_union_map_add_map(res, map);
987 /* Dummy match_space function that simply returns the input space.
989 static __isl_give isl_space *identity(__isl_take isl_space *space)
991 return space;
994 /* Look for the map in data->umap2 that corresponds to "map", if any.
995 * Return (isl_bool_true, matching map) if there is one,
996 * (isl_bool_false, NULL) if there is no matching map and
997 * (isl_bool_error, NULL) on error.
999 * If not NULL, then data->control->filter specifies whether "map"
1000 * can have any matching map. If so,
1001 * data->control->match_space specifies which map in data->umap2
1002 * corresponds to "map".
1004 static __isl_keep isl_maybe_isl_map bin_try_get_match(
1005 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
1007 struct isl_hash_table_entry *entry2;
1008 isl_space *space;
1009 isl_maybe_isl_map res = { isl_bool_error, NULL };
1011 if (data->control->filter) {
1012 res.valid = data->control->filter(map);
1013 if (res.valid < 0 || !res.valid)
1014 return res;
1015 res.valid = isl_bool_error;
1018 space = isl_map_get_space(map);
1019 if (data->control->match_space != &identity)
1020 space = data->control->match_space(space);
1021 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
1022 isl_space_free(space);
1023 if (entry2)
1024 res.valid = isl_bool_ok(entry2 != isl_hash_table_entry_none);
1025 if (res.valid >= 0 && res.valid)
1026 res.value = entry2->data;
1028 return res;
1031 /* isl_hash_table_foreach callback for gen_bin_op.
1032 * Look for the map in data->umap2 that corresponds
1033 * to the map that "entry" points to, apply the binary operation and
1034 * add the result to data->res.
1036 * If no corresponding map can be found, then the effect depends
1037 * on data->control->subtract. If it is set, then the current map
1038 * is added directly to the result. Otherwise, it is ignored.
1040 static isl_stat gen_bin_entry(void **entry, void *user)
1042 struct isl_union_map_gen_bin_data *data = user;
1043 isl_map *map = *entry;
1044 isl_maybe_isl_map m;
1046 m = bin_try_get_match(data, map);
1047 if (m.valid < 0)
1048 return isl_stat_error;
1049 if (!m.valid && !data->control->subtract)
1050 return isl_stat_ok;
1052 if (!m.valid)
1053 data->res = bin_add_map(data->res, map);
1054 else
1055 data->res = bin_add_pair(data->res, map, m.value, data);
1056 if (!data->res)
1057 return isl_stat_error;
1059 return isl_stat_ok;
1062 /* Apply a binary operation to "umap1" and "umap2" based on "control".
1063 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
1064 * in gen_bin_entry.
1066 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
1067 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
1069 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
1071 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1072 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1074 if (!umap1 || !umap2)
1075 goto error;
1077 data.umap2 = umap2;
1078 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1079 umap1->table.n);
1080 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1081 &gen_bin_entry, &data) < 0)
1082 goto error;
1084 isl_union_map_free(umap1);
1085 isl_union_map_free(umap2);
1086 return data.res;
1087 error:
1088 isl_union_map_free(umap1);
1089 isl_union_map_free(umap2);
1090 isl_union_map_free(data.res);
1091 return NULL;
1094 __isl_give isl_union_map *isl_union_map_subtract(
1095 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1097 struct isl_bin_op_control control = {
1098 .subtract = 1,
1099 .match_space = &identity,
1100 .fn_map = &isl_map_subtract,
1103 return gen_bin_op(umap1, umap2, &control);
1106 __isl_give isl_union_set *isl_union_set_subtract(
1107 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1109 return isl_union_map_subtract(uset1, uset2);
1112 struct isl_union_map_gen_bin_set_data {
1113 isl_set *set;
1114 isl_union_map *res;
1117 static isl_stat intersect_params_entry(void **entry, void *user)
1119 struct isl_union_map_gen_bin_set_data *data = user;
1120 isl_map *map = *entry;
1121 int empty;
1123 map = isl_map_copy(map);
1124 map = isl_map_intersect_params(map, isl_set_copy(data->set));
1126 empty = isl_map_is_empty(map);
1127 if (empty < 0) {
1128 isl_map_free(map);
1129 return isl_stat_error;
1132 data->res = isl_union_map_add_map(data->res, map);
1134 return isl_stat_ok;
1137 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
1138 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
1140 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
1142 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1143 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1145 if (!umap || !set)
1146 goto error;
1148 data.set = set;
1149 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1150 umap->table.n);
1151 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1152 fn, &data) < 0)
1153 goto error;
1155 isl_union_map_free(umap);
1156 isl_set_free(set);
1157 return data.res;
1158 error:
1159 isl_union_map_free(umap);
1160 isl_set_free(set);
1161 isl_union_map_free(data.res);
1162 return NULL;
1165 /* Intersect "umap" with the parameter domain "set".
1167 * If "set" does not have any constraints, then we can return immediately.
1169 __isl_give isl_union_map *isl_union_map_intersect_params(
1170 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1172 int is_universe;
1174 is_universe = isl_set_plain_is_universe(set);
1175 if (is_universe < 0)
1176 goto error;
1177 if (is_universe) {
1178 isl_set_free(set);
1179 return umap;
1182 return gen_bin_set_op(umap, set, &intersect_params_entry);
1183 error:
1184 isl_union_map_free(umap);
1185 isl_set_free(set);
1186 return NULL;
1189 __isl_give isl_union_set *isl_union_set_intersect_params(
1190 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1192 return isl_union_map_intersect_params(uset, set);
1195 static __isl_give isl_union_map *union_map_intersect_params(
1196 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1198 return isl_union_map_intersect_params(umap,
1199 isl_set_from_union_set(uset));
1202 static __isl_give isl_union_map *union_map_gist_params(
1203 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1205 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1208 struct isl_union_map_match_bin_data {
1209 isl_union_map *umap2;
1210 isl_union_map *res;
1211 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1214 static isl_stat match_bin_entry(void **entry, void *user)
1216 struct isl_union_map_match_bin_data *data = user;
1217 struct isl_hash_table_entry *entry2;
1218 isl_space *space;
1219 isl_map *map = *entry;
1220 int empty;
1222 space = isl_map_peek_space(map);
1223 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
1224 if (!entry2)
1225 return isl_stat_error;
1226 if (entry2 == isl_hash_table_entry_none)
1227 return isl_stat_ok;
1229 map = isl_map_copy(map);
1230 map = data->fn(map, isl_map_copy(entry2->data));
1232 empty = isl_map_is_empty(map);
1233 if (empty < 0) {
1234 isl_map_free(map);
1235 return isl_stat_error;
1237 if (empty) {
1238 isl_map_free(map);
1239 return isl_stat_ok;
1242 data->res = isl_union_map_add_map(data->res, map);
1244 return isl_stat_ok;
1247 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1248 __isl_take isl_union_map *umap2,
1249 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1251 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1253 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1254 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1256 if (!umap1 || !umap2)
1257 goto error;
1259 data.umap2 = umap2;
1260 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1261 umap1->table.n);
1262 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1263 &match_bin_entry, &data) < 0)
1264 goto error;
1266 isl_union_map_free(umap1);
1267 isl_union_map_free(umap2);
1268 return data.res;
1269 error:
1270 isl_union_map_free(umap1);
1271 isl_union_map_free(umap2);
1272 isl_union_map_free(data.res);
1273 return NULL;
1276 __isl_give isl_union_map *isl_union_map_intersect(
1277 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1279 return match_bin_op(umap1, umap2, &isl_map_intersect);
1282 /* Compute the intersection of the two union_sets.
1283 * As a special case, if exactly one of the two union_sets
1284 * is a parameter domain, then intersect the parameter domain
1285 * of the other one with this set.
1287 __isl_give isl_union_set *isl_union_set_intersect(
1288 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1290 int p1, p2;
1292 p1 = isl_union_set_is_params(uset1);
1293 p2 = isl_union_set_is_params(uset2);
1294 if (p1 < 0 || p2 < 0)
1295 goto error;
1296 if (!p1 && p2)
1297 return union_map_intersect_params(uset1, uset2);
1298 if (p1 && !p2)
1299 return union_map_intersect_params(uset2, uset1);
1300 return isl_union_map_intersect(uset1, uset2);
1301 error:
1302 isl_union_set_free(uset1);
1303 isl_union_set_free(uset2);
1304 return NULL;
1307 static isl_stat gist_params_entry(void **entry, void *user)
1309 struct isl_union_map_gen_bin_set_data *data = user;
1310 isl_map *map = *entry;
1311 int empty;
1313 map = isl_map_copy(map);
1314 map = isl_map_gist_params(map, isl_set_copy(data->set));
1316 empty = isl_map_is_empty(map);
1317 if (empty < 0) {
1318 isl_map_free(map);
1319 return isl_stat_error;
1322 data->res = isl_union_map_add_map(data->res, map);
1324 return isl_stat_ok;
1327 __isl_give isl_union_map *isl_union_map_gist_params(
1328 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1330 return gen_bin_set_op(umap, set, &gist_params_entry);
1333 __isl_give isl_union_set *isl_union_set_gist_params(
1334 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1336 return isl_union_map_gist_params(uset, set);
1339 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1340 __isl_take isl_union_map *context)
1342 return match_bin_op(umap, context, &isl_map_gist);
1345 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1346 __isl_take isl_union_set *context)
1348 if (isl_union_set_is_params(context))
1349 return union_map_gist_params(uset, context);
1350 return isl_union_map_gist(uset, context);
1353 /* For each map in "umap", remove the constraints in the corresponding map
1354 * of "context".
1355 * Each map in "context" is assumed to consist of a single disjunct and
1356 * to have explicit representations for all local variables.
1358 __isl_give isl_union_map *isl_union_map_plain_gist(
1359 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1361 return match_bin_op(umap, context, &isl_map_plain_gist);
1364 /* For each set in "uset", remove the constraints in the corresponding set
1365 * of "context".
1366 * Each set in "context" is assumed to consist of a single disjunct and
1367 * to have explicit representations for all local variables.
1369 __isl_give isl_union_set *isl_union_set_plain_gist(
1370 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1372 return isl_union_map_plain_gist(uset, context);
1375 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1376 __isl_take isl_map *set2)
1378 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1381 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1382 __isl_take isl_map *set2)
1384 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1387 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1388 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1390 return match_bin_op(uset1, uset2, &lex_lt_set);
1393 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1394 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1396 return match_bin_op(uset1, uset2, &lex_le_set);
1399 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1400 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1402 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1405 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1406 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1408 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1411 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1412 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1414 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1417 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1418 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1420 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1423 /* Intersect the domain of "umap" with "uset".
1425 static __isl_give isl_union_map *union_map_intersect_domain(
1426 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1428 struct isl_bin_op_control control = {
1429 .match_space = &isl_space_domain,
1430 .fn_map = &isl_map_intersect_domain,
1433 return gen_bin_op(umap, uset, &control);
1436 /* Intersect the domain of "umap" with "uset".
1437 * If "uset" is a parameters domain, then intersect the parameter
1438 * domain of "umap" with this set.
1440 __isl_give isl_union_map *isl_union_map_intersect_domain_union_set(
1441 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1443 if (isl_union_set_is_params(uset))
1444 return union_map_intersect_params(umap, uset);
1445 else
1446 return union_map_intersect_domain(umap, uset);
1449 /* This is an alternative name for the function above.
1451 __isl_give isl_union_map *isl_union_map_intersect_domain(
1452 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1454 return isl_union_map_intersect_domain_union_set(umap, uset);
1457 /* Remove the elements of "uset" from the domain of "umap".
1459 __isl_give isl_union_map *isl_union_map_subtract_domain(
1460 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1462 struct isl_bin_op_control control = {
1463 .subtract = 1,
1464 .match_space = &isl_space_domain,
1465 .fn_map = &isl_map_subtract_domain,
1468 return gen_bin_op(umap, dom, &control);
1471 /* Remove the elements of "uset" from the range of "umap".
1473 __isl_give isl_union_map *isl_union_map_subtract_range(
1474 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1476 struct isl_bin_op_control control = {
1477 .subtract = 1,
1478 .match_space = &isl_space_range,
1479 .fn_map = &isl_map_subtract_range,
1482 return gen_bin_op(umap, dom, &control);
1485 /* Compute the gist of "umap" with respect to the domain "uset".
1487 static __isl_give isl_union_map *union_map_gist_domain(
1488 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1490 struct isl_bin_op_control control = {
1491 .match_space = &isl_space_domain,
1492 .fn_map = &isl_map_gist_domain,
1495 return gen_bin_op(umap, uset, &control);
1498 /* Compute the gist of "umap" with respect to the domain "uset".
1499 * If "uset" is a parameters domain, then compute the gist
1500 * with respect to this parameter domain.
1502 __isl_give isl_union_map *isl_union_map_gist_domain(
1503 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1505 if (isl_union_set_is_params(uset))
1506 return union_map_gist_params(umap, uset);
1507 else
1508 return union_map_gist_domain(umap, uset);
1511 /* Compute the gist of "umap" with respect to the range "uset".
1513 __isl_give isl_union_map *isl_union_map_gist_range(
1514 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1516 struct isl_bin_op_control control = {
1517 .match_space = &isl_space_range,
1518 .fn_map = &isl_map_gist_range,
1521 return gen_bin_op(umap, uset, &control);
1524 __isl_give isl_union_map *isl_union_map_intersect_range_union_set(
1525 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1527 struct isl_bin_op_control control = {
1528 .match_space = &isl_space_range,
1529 .fn_map = &isl_map_intersect_range,
1532 return gen_bin_op(umap, uset, &control);
1535 /* This is an alternative name for the function above.
1537 __isl_give isl_union_map *isl_union_map_intersect_range(
1538 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1540 return isl_union_map_intersect_range_union_set(umap, uset);
1543 /* Intersect each map in "umap" in a space [A -> B] -> C
1544 * with the corresponding map in "factor" in the space A -> C and
1545 * collect the results.
1547 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_domain(
1548 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1550 struct isl_bin_op_control control = {
1551 .filter = &isl_map_domain_is_wrapping,
1552 .match_space = &isl_space_domain_factor_domain,
1553 .fn_map = &isl_map_intersect_domain_factor_domain,
1556 return gen_bin_op(umap, factor, &control);
1559 /* Intersect each map in "umap" in a space [A -> B] -> C
1560 * with the corresponding map in "factor" in the space B -> C and
1561 * collect the results.
1563 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_range(
1564 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1566 struct isl_bin_op_control control = {
1567 .filter = &isl_map_domain_is_wrapping,
1568 .match_space = &isl_space_domain_factor_range,
1569 .fn_map = &isl_map_intersect_domain_factor_range,
1572 return gen_bin_op(umap, factor, &control);
1575 /* Intersect each map in "umap" in a space A -> [B -> C]
1576 * with the corresponding map in "factor" in the space A -> B and
1577 * collect the results.
1579 __isl_give isl_union_map *isl_union_map_intersect_range_factor_domain(
1580 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1582 struct isl_bin_op_control control = {
1583 .filter = &isl_map_range_is_wrapping,
1584 .match_space = &isl_space_range_factor_domain,
1585 .fn_map = &isl_map_intersect_range_factor_domain,
1588 return gen_bin_op(umap, factor, &control);
1591 /* Intersect each map in "umap" in a space A -> [B -> C]
1592 * with the corresponding map in "factor" in the space A -> C and
1593 * collect the results.
1595 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1596 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1598 struct isl_bin_op_control control = {
1599 .filter = &isl_map_range_is_wrapping,
1600 .match_space = &isl_space_range_factor_range,
1601 .fn_map = &isl_map_intersect_range_factor_range,
1604 return gen_bin_op(umap, factor, &control);
1607 struct isl_union_map_bin_data {
1608 isl_union_map *umap2;
1609 isl_union_map *res;
1610 isl_map *map;
1611 isl_stat (*fn)(void **entry, void *user);
1614 static isl_stat apply_range_entry(void **entry, void *user)
1616 struct isl_union_map_bin_data *data = user;
1617 isl_map *map2 = *entry;
1618 isl_bool empty, match;
1620 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1621 map2, isl_dim_in);
1622 if (match < 0)
1623 return isl_stat_error;
1624 if (!match)
1625 return isl_stat_ok;
1627 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1629 empty = isl_map_is_empty(map2);
1630 if (empty < 0) {
1631 isl_map_free(map2);
1632 return isl_stat_error;
1634 if (empty) {
1635 isl_map_free(map2);
1636 return isl_stat_ok;
1639 data->res = isl_union_map_add_map(data->res, map2);
1641 return isl_stat_ok;
1644 static isl_stat bin_entry(void **entry, void *user)
1646 struct isl_union_map_bin_data *data = user;
1647 isl_map *map = *entry;
1649 data->map = map;
1650 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1651 data->fn, data) < 0)
1652 return isl_stat_error;
1654 return isl_stat_ok;
1657 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1658 __isl_take isl_union_map *umap2,
1659 isl_stat (*fn)(void **entry, void *user))
1661 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1663 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1664 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1666 if (!umap1 || !umap2)
1667 goto error;
1669 data.umap2 = umap2;
1670 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1671 umap1->table.n);
1672 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1673 &bin_entry, &data) < 0)
1674 goto error;
1676 isl_union_map_free(umap1);
1677 isl_union_map_free(umap2);
1678 return data.res;
1679 error:
1680 isl_union_map_free(umap1);
1681 isl_union_map_free(umap2);
1682 isl_union_map_free(data.res);
1683 return NULL;
1686 /* Intersect each map in "umap" in a space [A -> B] -> C
1687 * with the corresponding set in "domain" in the space A and
1688 * collect the results.
1690 __isl_give isl_union_map *
1691 isl_union_map_intersect_domain_wrapped_domain_union_set(
1692 __isl_take isl_union_map *umap, __isl_take isl_union_set *domain)
1694 struct isl_bin_op_control control = {
1695 .filter = &isl_map_domain_is_wrapping,
1696 .match_space = &isl_space_domain_wrapped_domain,
1697 .fn_map = &isl_map_intersect_domain_wrapped_domain,
1700 return gen_bin_op(umap, domain, &control);
1703 /* Intersect each map in "umap" in a space A -> [B -> C]
1704 * with the corresponding set in "domain" in the space B and
1705 * collect the results.
1707 __isl_give isl_union_map *
1708 isl_union_map_intersect_range_wrapped_domain_union_set(
1709 __isl_take isl_union_map *umap, __isl_take isl_union_set *domain)
1711 struct isl_bin_op_control control = {
1712 .filter = &isl_map_range_is_wrapping,
1713 .match_space = &isl_space_range_wrapped_domain,
1714 .fn_map = &isl_map_intersect_range_wrapped_domain,
1717 return gen_bin_op(umap, domain, &control);
1720 __isl_give isl_union_map *isl_union_map_apply_range(
1721 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1723 return bin_op(umap1, umap2, &apply_range_entry);
1726 __isl_give isl_union_map *isl_union_map_apply_domain(
1727 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1729 umap1 = isl_union_map_reverse(umap1);
1730 umap1 = isl_union_map_apply_range(umap1, umap2);
1731 return isl_union_map_reverse(umap1);
1734 __isl_give isl_union_set *isl_union_set_apply(
1735 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1737 return isl_union_map_apply_range(uset, umap);
1740 static isl_stat map_lex_lt_entry(void **entry, void *user)
1742 struct isl_union_map_bin_data *data = user;
1743 isl_map *map2 = *entry;
1744 isl_bool match;
1746 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1747 map2, isl_dim_out);
1748 if (match < 0)
1749 return isl_stat_error;
1750 if (!match)
1751 return isl_stat_ok;
1753 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1755 data->res = isl_union_map_add_map(data->res, map2);
1757 return isl_stat_ok;
1760 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1761 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1763 return bin_op(umap1, umap2, &map_lex_lt_entry);
1766 static isl_stat map_lex_le_entry(void **entry, void *user)
1768 struct isl_union_map_bin_data *data = user;
1769 isl_map *map2 = *entry;
1770 isl_bool match;
1772 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1773 map2, isl_dim_out);
1774 if (match < 0)
1775 return isl_stat_error;
1776 if (!match)
1777 return isl_stat_ok;
1779 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1781 data->res = isl_union_map_add_map(data->res, map2);
1783 return isl_stat_ok;
1786 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1787 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1789 return bin_op(umap1, umap2, &map_lex_le_entry);
1792 static isl_stat product_entry(void **entry, void *user)
1794 struct isl_union_map_bin_data *data = user;
1795 isl_map *map2 = *entry;
1797 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1799 data->res = isl_union_map_add_map(data->res, map2);
1801 return isl_stat_ok;
1804 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1805 __isl_take isl_union_map *umap2)
1807 return bin_op(umap1, umap2, &product_entry);
1810 static isl_stat set_product_entry(void **entry, void *user)
1812 struct isl_union_map_bin_data *data = user;
1813 isl_set *set2 = *entry;
1815 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1817 data->res = isl_union_set_add_set(data->res, set2);
1819 return isl_stat_ok;
1822 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1823 __isl_take isl_union_set *uset2)
1825 return bin_op(uset1, uset2, &set_product_entry);
1828 static isl_stat domain_product_entry(void **entry, void *user)
1830 struct isl_union_map_bin_data *data = user;
1831 isl_map *map2 = *entry;
1832 isl_bool match;
1834 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1835 map2, isl_dim_out);
1836 if (match < 0)
1837 return isl_stat_error;
1838 if (!match)
1839 return isl_stat_ok;
1841 map2 = isl_map_domain_product(isl_map_copy(data->map),
1842 isl_map_copy(map2));
1844 data->res = isl_union_map_add_map(data->res, map2);
1846 return isl_stat_ok;
1849 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1851 __isl_give isl_union_map *isl_union_map_domain_product(
1852 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1854 return bin_op(umap1, umap2, &domain_product_entry);
1857 static isl_stat range_product_entry(void **entry, void *user)
1859 struct isl_union_map_bin_data *data = user;
1860 isl_map *map2 = *entry;
1861 isl_bool match;
1863 match = isl_map_tuple_is_equal(data->map, isl_dim_in, map2, isl_dim_in);
1864 if (match < 0)
1865 return isl_stat_error;
1866 if (!match)
1867 return isl_stat_ok;
1869 map2 = isl_map_range_product(isl_map_copy(data->map),
1870 isl_map_copy(map2));
1872 data->res = isl_union_map_add_map(data->res, map2);
1874 return isl_stat_ok;
1877 __isl_give isl_union_map *isl_union_map_range_product(
1878 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1880 return bin_op(umap1, umap2, &range_product_entry);
1883 /* If data->map A -> B and "map2" C -> D have the same range space,
1884 * then add (A, C) -> (B * D) to data->res.
1886 static isl_stat flat_domain_product_entry(void **entry, void *user)
1888 struct isl_union_map_bin_data *data = user;
1889 isl_map *map2 = *entry;
1890 isl_bool match;
1892 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1893 map2, isl_dim_out);
1894 if (match < 0)
1895 return isl_stat_error;
1896 if (!match)
1897 return isl_stat_ok;
1899 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1900 isl_map_copy(map2));
1902 data->res = isl_union_map_add_map(data->res, map2);
1904 return isl_stat_ok;
1907 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1909 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1910 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1912 return bin_op(umap1, umap2, &flat_domain_product_entry);
1915 static isl_stat flat_range_product_entry(void **entry, void *user)
1917 struct isl_union_map_bin_data *data = user;
1918 isl_map *map2 = *entry;
1919 isl_bool match;
1921 match = isl_map_tuple_is_equal(data->map, isl_dim_in, map2, isl_dim_in);
1922 if (match < 0)
1923 return isl_stat_error;
1924 if (!match)
1925 return isl_stat_ok;
1927 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1928 isl_map_copy(map2));
1930 data->res = isl_union_map_add_map(data->res, map2);
1932 return isl_stat_ok;
1935 __isl_give isl_union_map *isl_union_map_flat_range_product(
1936 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1938 return bin_op(umap1, umap2, &flat_range_product_entry);
1941 /* Data structure that specifies how un_op should modify
1942 * the maps in the union map.
1944 * If "inplace" is set, then the maps in the input union map
1945 * are modified in place. This means that "fn_map" should not
1946 * change the meaning of the map or that the union map only
1947 * has a single reference.
1948 * If "total" is set, then all maps need to be modified and
1949 * the results need to live in the same space.
1950 * Otherwise, a new union map is constructed to store the results.
1951 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1952 * are taken into account. "filter_user" is passed as the second argument
1953 * to "filter". No filter can be set if "inplace" or
1954 * "total" is set.
1955 * At most one of "fn_map" or "fn_map2" can be set, specifying
1956 * how the maps (selected by "filter") should be transformed.
1957 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1959 struct isl_un_op_control {
1960 int inplace;
1961 int total;
1962 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1963 void *filter_user;
1964 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1965 __isl_give isl_map *(*fn_map2)(__isl_take isl_map *map, void *user);
1966 void *fn_map2_user;
1969 /* Data structure for wrapping the data for un_op_filter_drop_user.
1970 * "filter" is the function that is being wrapped.
1972 struct isl_un_op_drop_user_data {
1973 isl_bool (*filter)(__isl_keep isl_map *map);
1976 /* Wrapper for isl_un_op_control filters that do not require
1977 * a second argument.
1978 * Simply call data->filter without the second argument.
1980 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1982 struct isl_un_op_drop_user_data *data = user;
1983 return data->filter(map);
1986 /* Internal data structure for "un_op".
1987 * "control" specifies how the maps in the union map should be modified.
1988 * "res" collects the results.
1990 struct isl_union_map_un_data {
1991 struct isl_un_op_control *control;
1992 isl_union_map *res;
1995 /* isl_hash_table_foreach callback for un_op.
1996 * Handle the map that "entry" points to.
1998 * If control->filter is set, then check if this map satisfies the filter.
1999 * If so (or if control->filter is not set), modify the map
2000 * by calling control->fn_map or control->fn_map2 (if set) and
2001 * either add the result to data->res or
2002 * replace the original entry by the result (if control->inplace is set).
2004 static isl_stat un_entry(void **entry, void *user)
2006 struct isl_union_map_un_data *data = user;
2007 struct isl_un_op_control *control = data->control;
2008 isl_map *map = *entry;
2010 if (control->filter) {
2011 isl_bool ok;
2013 ok = control->filter(map, control->filter_user);
2014 if (ok < 0)
2015 return isl_stat_error;
2016 if (!ok)
2017 return isl_stat_ok;
2020 map = isl_map_copy(map);
2021 if (control->fn_map2 != NULL)
2022 map = control->fn_map2(map, control->fn_map2_user);
2023 else if (control->fn_map != NULL)
2024 map = control->fn_map(map);
2025 if (!map)
2026 return isl_stat_error;
2027 if (control->inplace) {
2028 isl_map_free(*entry);
2029 *entry = map;
2030 } else {
2031 data->res = isl_union_map_add_map(data->res, map);
2032 if (!data->res)
2033 return isl_stat_error;
2036 return isl_stat_ok;
2039 /* Modify the maps in "umap" based on "control".
2040 * If control->inplace is set, then modify the maps in "umap" in-place.
2041 * Otherwise, create a new union map to hold the results.
2042 * If control->total is set, then perform an inplace computation
2043 * if "umap" is only referenced once. Otherwise, create a new union map
2044 * to store the results.
2046 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
2047 struct isl_un_op_control *control)
2049 struct isl_union_map_un_data data = { control };
2051 if (!umap)
2052 return NULL;
2053 if (!!control->fn_map && !!control->fn_map2)
2054 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
2055 "at most one mapping function can be specified",
2056 return isl_union_map_free(umap));
2057 if ((control->inplace || control->total) && control->filter)
2058 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
2059 "inplace/total modification cannot be filtered",
2060 return isl_union_map_free(umap));
2062 if (control->total && umap->ref == 1)
2063 control->inplace = 1;
2064 if (control->inplace) {
2065 data.res = umap;
2066 } else {
2067 isl_space *space;
2069 space = isl_union_map_get_space(umap);
2070 data.res = isl_union_map_alloc(space, umap->table.n);
2072 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
2073 &umap->table, &un_entry, &data) < 0)
2074 data.res = isl_union_map_free(data.res);
2076 if (control->inplace)
2077 return data.res;
2078 isl_union_map_free(umap);
2079 return data.res;
2082 __isl_give isl_union_map *isl_union_map_from_range(
2083 __isl_take isl_union_set *uset)
2085 struct isl_un_op_control control = {
2086 .fn_map = &isl_map_from_range,
2088 return un_op(uset, &control);
2091 __isl_give isl_union_map *isl_union_map_from_domain(
2092 __isl_take isl_union_set *uset)
2094 return isl_union_map_reverse(isl_union_map_from_range(uset));
2097 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
2098 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
2100 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
2101 isl_union_map_from_range(range));
2104 /* Modify the maps in "umap" by applying "fn" on them.
2105 * "fn" should apply to all maps in "umap" and should not modify the space.
2107 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
2108 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2110 struct isl_un_op_control control = {
2111 .total = 1,
2112 .fn_map = fn,
2115 return un_op(umap, &control);
2118 /* Compute the affine hull of "map" and return the result as an isl_map.
2120 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
2122 return isl_map_from_basic_map(isl_map_affine_hull(map));
2125 __isl_give isl_union_map *isl_union_map_affine_hull(
2126 __isl_take isl_union_map *umap)
2128 return total(umap, &isl_map_affine_hull_map);
2131 __isl_give isl_union_set *isl_union_set_affine_hull(
2132 __isl_take isl_union_set *uset)
2134 return isl_union_map_affine_hull(uset);
2137 /* Wrapper around isl_set_combined_lineality_space
2138 * that returns the combined lineality space in the form of an isl_set
2139 * instead of an isl_basic_set.
2141 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
2143 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
2146 /* For each set in "uset", compute the (linear) hull
2147 * of the lineality spaces of its basic sets and
2148 * collect and return the results.
2150 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
2151 __isl_take isl_union_set *uset)
2153 struct isl_un_op_control control = {
2154 .fn_map = &combined_lineality_space,
2156 return un_op(uset, &control);
2159 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
2161 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
2163 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
2166 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
2167 __isl_take isl_union_map *umap)
2169 return total(umap, &isl_map_polyhedral_hull_map);
2172 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
2173 __isl_take isl_union_set *uset)
2175 return isl_union_map_polyhedral_hull(uset);
2178 /* Compute a superset of the convex hull of "map" that is described
2179 * by only translates of the constraints in the constituents of "map" and
2180 * return the result as an isl_map.
2182 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
2184 return isl_map_from_basic_map(isl_map_simple_hull(map));
2187 __isl_give isl_union_map *isl_union_map_simple_hull(
2188 __isl_take isl_union_map *umap)
2190 return total(umap, &isl_map_simple_hull_map);
2193 __isl_give isl_union_set *isl_union_set_simple_hull(
2194 __isl_take isl_union_set *uset)
2196 return isl_union_map_simple_hull(uset);
2199 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
2200 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2202 struct isl_un_op_control control = {
2203 .inplace = 1,
2204 .fn_map = fn,
2207 return un_op(umap, &control);
2210 /* Remove redundant constraints in each of the basic maps of "umap".
2211 * Since removing redundant constraints does not change the meaning
2212 * or the space, the operation can be performed in-place.
2214 __isl_give isl_union_map *isl_union_map_remove_redundancies(
2215 __isl_take isl_union_map *umap)
2217 return inplace(umap, &isl_map_remove_redundancies);
2220 /* Remove redundant constraints in each of the basic sets of "uset".
2222 __isl_give isl_union_set *isl_union_set_remove_redundancies(
2223 __isl_take isl_union_set *uset)
2225 return isl_union_map_remove_redundancies(uset);
2228 __isl_give isl_union_map *isl_union_map_coalesce(
2229 __isl_take isl_union_map *umap)
2231 return inplace(umap, &isl_map_coalesce);
2234 __isl_give isl_union_set *isl_union_set_coalesce(
2235 __isl_take isl_union_set *uset)
2237 return isl_union_map_coalesce(uset);
2240 __isl_give isl_union_map *isl_union_map_detect_equalities(
2241 __isl_take isl_union_map *umap)
2243 return inplace(umap, &isl_map_detect_equalities);
2246 __isl_give isl_union_set *isl_union_set_detect_equalities(
2247 __isl_take isl_union_set *uset)
2249 return isl_union_map_detect_equalities(uset);
2252 __isl_give isl_union_map *isl_union_map_compute_divs(
2253 __isl_take isl_union_map *umap)
2255 return inplace(umap, &isl_map_compute_divs);
2258 __isl_give isl_union_set *isl_union_set_compute_divs(
2259 __isl_take isl_union_set *uset)
2261 return isl_union_map_compute_divs(uset);
2264 __isl_give isl_union_map *isl_union_map_lexmin(
2265 __isl_take isl_union_map *umap)
2267 return total(umap, &isl_map_lexmin);
2270 __isl_give isl_union_set *isl_union_set_lexmin(
2271 __isl_take isl_union_set *uset)
2273 return isl_union_map_lexmin(uset);
2276 __isl_give isl_union_map *isl_union_map_lexmax(
2277 __isl_take isl_union_map *umap)
2279 return total(umap, &isl_map_lexmax);
2282 __isl_give isl_union_set *isl_union_set_lexmax(
2283 __isl_take isl_union_set *uset)
2285 return isl_union_map_lexmax(uset);
2288 /* Return the universe in the space of "map".
2290 static __isl_give isl_map *universe(__isl_take isl_map *map)
2292 isl_space *space;
2294 space = isl_map_get_space(map);
2295 isl_map_free(map);
2296 return isl_map_universe(space);
2299 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2301 struct isl_un_op_control control = {
2302 .fn_map = &universe,
2304 return un_op(umap, &control);
2307 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2309 return isl_union_map_universe(uset);
2312 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2314 struct isl_un_op_control control = {
2315 .fn_map = &isl_map_reverse,
2317 return un_op(umap, &control);
2320 /* Given a union map, take the maps of the form A -> (B -> C) and
2321 * return the union of the corresponding maps A -> (C -> B).
2323 __isl_give isl_union_map *isl_union_map_range_reverse(
2324 __isl_take isl_union_map *umap)
2326 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2327 struct isl_un_op_control control = {
2328 .filter = &un_op_filter_drop_user,
2329 .filter_user = &data,
2330 .fn_map = &isl_map_range_reverse,
2332 return un_op(umap, &control);
2335 /* Compute the parameter domain of the given union map.
2337 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2339 struct isl_un_op_control control = {
2340 .fn_map = &isl_map_params,
2342 int empty;
2344 empty = isl_union_map_is_empty(umap);
2345 if (empty < 0)
2346 goto error;
2347 if (empty) {
2348 isl_space *space;
2349 space = isl_union_map_get_space(umap);
2350 isl_union_map_free(umap);
2351 return isl_set_empty(space);
2353 return isl_set_from_union_set(un_op(umap, &control));
2354 error:
2355 isl_union_map_free(umap);
2356 return NULL;
2359 /* Compute the parameter domain of the given union set.
2361 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2363 return isl_union_map_params(uset);
2366 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2368 struct isl_un_op_control control = {
2369 .fn_map = &isl_map_domain,
2371 return un_op(umap, &control);
2374 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2376 struct isl_un_op_control control = {
2377 .fn_map = &isl_map_range,
2379 return un_op(umap, &control);
2382 __isl_give isl_union_map *isl_union_map_domain_map(
2383 __isl_take isl_union_map *umap)
2385 struct isl_un_op_control control = {
2386 .fn_map = &isl_map_domain_map,
2388 return un_op(umap, &control);
2391 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2392 * add the result to "res".
2394 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2396 isl_union_pw_multi_aff **res = user;
2397 isl_multi_aff *ma;
2398 isl_pw_multi_aff *pma;
2400 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2401 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2402 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2404 return *res ? isl_stat_ok : isl_stat_error;
2408 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2409 * to its domain.
2411 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2412 __isl_take isl_union_map *umap)
2414 isl_union_pw_multi_aff *res;
2416 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2417 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2418 res = isl_union_pw_multi_aff_free(res);
2420 isl_union_map_free(umap);
2421 return res;
2424 __isl_give isl_union_map *isl_union_map_range_map(
2425 __isl_take isl_union_map *umap)
2427 struct isl_un_op_control control = {
2428 .fn_map = &isl_map_range_map,
2430 return un_op(umap, &control);
2433 /* Given a collection of wrapped maps of the form A[B -> C],
2434 * return the collection of maps A[B -> C] -> B.
2436 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2437 __isl_take isl_union_set *uset)
2439 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2440 struct isl_un_op_control control = {
2441 .filter = &un_op_filter_drop_user,
2442 .filter_user = &data,
2443 .fn_map = &isl_set_wrapped_domain_map,
2445 return un_op(uset, &control);
2448 /* Does "map" relate elements from the same space?
2450 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2452 return isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
2455 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2457 struct isl_un_op_control control = {
2458 .filter = &equal_tuples,
2459 .fn_map = &isl_map_deltas,
2461 return un_op(umap, &control);
2464 __isl_give isl_union_map *isl_union_map_deltas_map(
2465 __isl_take isl_union_map *umap)
2467 struct isl_un_op_control control = {
2468 .filter = &equal_tuples,
2469 .fn_map = &isl_map_deltas_map,
2471 return un_op(umap, &control);
2474 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2476 struct isl_un_op_control control = {
2477 .fn_map = &isl_set_identity,
2479 return un_op(uset, &control);
2482 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2484 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2486 isl_union_pw_multi_aff **res = user;
2487 isl_space *space;
2488 isl_pw_multi_aff *pma;
2490 space = isl_space_map_from_set(isl_set_get_space(set));
2491 pma = isl_pw_multi_aff_identity(space);
2492 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2493 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2495 return *res ? isl_stat_ok : isl_stat_error;
2498 /* Return an identity function on "uset" in the form
2499 * of an isl_union_pw_multi_aff.
2501 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2502 __isl_take isl_union_set *uset)
2504 isl_union_pw_multi_aff *res;
2506 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2507 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2508 res = isl_union_pw_multi_aff_free(res);
2510 isl_union_set_free(uset);
2511 return res;
2514 /* For each map in "umap" of the form [A -> B] -> C,
2515 * construct the map A -> C and collect the results.
2517 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2518 __isl_take isl_union_map *umap)
2520 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2521 struct isl_un_op_control control = {
2522 .filter = &un_op_filter_drop_user,
2523 .filter_user = &data,
2524 .fn_map = &isl_map_domain_factor_domain,
2526 return un_op(umap, &control);
2529 /* For each map in "umap" of the form [A -> B] -> C,
2530 * construct the map B -> C and collect the results.
2532 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2533 __isl_take isl_union_map *umap)
2535 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2536 struct isl_un_op_control control = {
2537 .filter = &un_op_filter_drop_user,
2538 .filter_user = &data,
2539 .fn_map = &isl_map_domain_factor_range,
2541 return un_op(umap, &control);
2544 /* For each map in "umap" of the form A -> [B -> C],
2545 * construct the map A -> B and collect the results.
2547 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2548 __isl_take isl_union_map *umap)
2550 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2551 struct isl_un_op_control control = {
2552 .filter = &un_op_filter_drop_user,
2553 .filter_user = &data,
2554 .fn_map = &isl_map_range_factor_domain,
2556 return un_op(umap, &control);
2559 /* For each map in "umap" of the form A -> [B -> C],
2560 * construct the map A -> C and collect the results.
2562 __isl_give isl_union_map *isl_union_map_range_factor_range(
2563 __isl_take isl_union_map *umap)
2565 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2566 struct isl_un_op_control control = {
2567 .filter = &un_op_filter_drop_user,
2568 .filter_user = &data,
2569 .fn_map = &isl_map_range_factor_range,
2571 return un_op(umap, &control);
2574 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2575 * construct the map A -> C and collect the results.
2577 __isl_give isl_union_map *isl_union_map_factor_domain(
2578 __isl_take isl_union_map *umap)
2580 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2581 struct isl_un_op_control control = {
2582 .filter = &un_op_filter_drop_user,
2583 .filter_user = &data,
2584 .fn_map = &isl_map_factor_domain,
2586 return un_op(umap, &control);
2589 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2590 * construct the map B -> D and collect the results.
2592 __isl_give isl_union_map *isl_union_map_factor_range(
2593 __isl_take isl_union_map *umap)
2595 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2596 struct isl_un_op_control control = {
2597 .filter = &un_op_filter_drop_user,
2598 .filter_user = &data,
2599 .fn_map = &isl_map_factor_range,
2601 return un_op(umap, &control);
2604 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2606 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2607 struct isl_un_op_control control = {
2608 .filter = &un_op_filter_drop_user,
2609 .filter_user = &data,
2610 .fn_map = &isl_set_unwrap,
2612 return un_op(uset, &control);
2615 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2617 struct isl_un_op_control control = {
2618 .fn_map = &isl_map_wrap,
2620 return un_op(umap, &control);
2623 struct isl_union_map_is_subset_data {
2624 isl_union_map *umap2;
2625 isl_bool is_subset;
2628 static isl_stat is_subset_entry(void **entry, void *user)
2630 struct isl_union_map_is_subset_data *data = user;
2631 struct isl_hash_table_entry *entry2;
2632 isl_space *space;
2633 isl_map *map = *entry;
2635 space = isl_map_peek_space(map);
2636 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
2637 if (!entry2)
2638 return isl_stat_error;
2639 if (entry2 == isl_hash_table_entry_none) {
2640 int empty = isl_map_is_empty(map);
2641 if (empty < 0)
2642 return isl_stat_error;
2643 if (empty)
2644 return isl_stat_ok;
2645 data->is_subset = isl_bool_false;
2646 return isl_stat_error;
2649 data->is_subset = isl_map_is_subset(map, entry2->data);
2650 if (data->is_subset < 0 || !data->is_subset)
2651 return isl_stat_error;
2653 return isl_stat_ok;
2656 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2657 __isl_keep isl_union_map *umap2)
2659 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2661 if (!umap1 || !umap2)
2662 return isl_bool_error;
2664 data.umap2 = umap2;
2665 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2666 &is_subset_entry, &data) < 0 &&
2667 data.is_subset)
2668 return isl_bool_error;
2670 return data.is_subset;
2673 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2674 __isl_keep isl_union_set *uset2)
2676 return isl_union_map_is_subset(uset1, uset2);
2679 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2680 __isl_keep isl_union_map *umap2)
2682 isl_bool is_subset;
2684 if (!umap1 || !umap2)
2685 return isl_bool_error;
2686 is_subset = isl_union_map_is_subset(umap1, umap2);
2687 if (is_subset != isl_bool_true)
2688 return is_subset;
2689 is_subset = isl_union_map_is_subset(umap2, umap1);
2690 return is_subset;
2693 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2694 __isl_keep isl_union_set *uset2)
2696 return isl_union_map_is_equal(uset1, uset2);
2699 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2700 __isl_keep isl_union_map *umap2)
2702 isl_bool is_subset;
2704 if (!umap1 || !umap2)
2705 return isl_bool_error;
2706 is_subset = isl_union_map_is_subset(umap1, umap2);
2707 if (is_subset != isl_bool_true)
2708 return is_subset;
2709 is_subset = isl_union_map_is_subset(umap2, umap1);
2710 return isl_bool_not(is_subset);
2713 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2714 __isl_keep isl_union_set *uset2)
2716 return isl_union_map_is_strict_subset(uset1, uset2);
2719 /* Internal data structure for isl_union_map_is_disjoint.
2720 * umap2 is the union map with which we are comparing.
2721 * is_disjoint is initialized to 1 and is set to 0 as soon
2722 * as the union maps turn out not to be disjoint.
2724 struct isl_union_map_is_disjoint_data {
2725 isl_union_map *umap2;
2726 isl_bool is_disjoint;
2729 /* Check if "map" is disjoint from data->umap2 and abort
2730 * the search if it is not.
2732 static isl_stat is_disjoint_entry(void **entry, void *user)
2734 struct isl_union_map_is_disjoint_data *data = user;
2735 struct isl_hash_table_entry *entry2;
2736 isl_space *space;
2737 isl_map *map = *entry;
2739 space = isl_map_peek_space(map);
2740 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
2741 if (!entry2)
2742 return isl_stat_error;
2743 if (entry2 == isl_hash_table_entry_none)
2744 return isl_stat_ok;
2746 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2747 if (data->is_disjoint < 0 || !data->is_disjoint)
2748 return isl_stat_error;
2750 return isl_stat_ok;
2753 /* Are "umap1" and "umap2" disjoint?
2755 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2756 __isl_keep isl_union_map *umap2)
2758 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2760 umap1 = isl_union_map_copy(umap1);
2761 umap2 = isl_union_map_copy(umap2);
2762 umap1 = isl_union_map_align_params(umap1,
2763 isl_union_map_get_space(umap2));
2764 umap2 = isl_union_map_align_params(umap2,
2765 isl_union_map_get_space(umap1));
2767 if (!umap1 || !umap2)
2768 goto error;
2770 data.umap2 = umap2;
2771 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2772 &is_disjoint_entry, &data) < 0 &&
2773 data.is_disjoint)
2774 goto error;
2776 isl_union_map_free(umap1);
2777 isl_union_map_free(umap2);
2779 return data.is_disjoint;
2780 error:
2781 isl_union_map_free(umap1);
2782 isl_union_map_free(umap2);
2783 return isl_bool_error;
2786 /* Are "uset1" and "uset2" disjoint?
2788 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2789 __isl_keep isl_union_set *uset2)
2791 return isl_union_map_is_disjoint(uset1, uset2);
2794 static isl_stat sample_entry(void **entry, void *user)
2796 isl_basic_map **sample = (isl_basic_map **)user;
2797 isl_map *map = *entry;
2799 *sample = isl_map_sample(isl_map_copy(map));
2800 if (!*sample)
2801 return isl_stat_error;
2802 if (!isl_basic_map_plain_is_empty(*sample))
2803 return isl_stat_error;
2804 return isl_stat_ok;
2807 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2809 isl_basic_map *sample = NULL;
2811 if (!umap)
2812 return NULL;
2814 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2815 &sample_entry, &sample) < 0 &&
2816 !sample)
2817 goto error;
2819 if (!sample)
2820 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2822 isl_union_map_free(umap);
2824 return sample;
2825 error:
2826 isl_union_map_free(umap);
2827 return NULL;
2830 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2832 return bset_from_bmap(isl_union_map_sample(uset));
2835 /* Return an element in "uset" in the form of an isl_point.
2836 * Return a void isl_point if "uset" is empty.
2838 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2840 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2843 struct isl_forall_data {
2844 isl_bool res;
2845 isl_bool (*fn)(__isl_keep isl_map *map);
2848 static isl_stat forall_entry(void **entry, void *user)
2850 struct isl_forall_data *data = user;
2851 isl_map *map = *entry;
2853 data->res = data->fn(map);
2854 if (data->res < 0)
2855 return isl_stat_error;
2857 if (!data->res)
2858 return isl_stat_error;
2860 return isl_stat_ok;
2863 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2864 isl_bool (*fn)(__isl_keep isl_map *map))
2866 struct isl_forall_data data = { isl_bool_true, fn };
2868 if (!umap)
2869 return isl_bool_error;
2871 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2872 &forall_entry, &data) < 0 && data.res)
2873 return isl_bool_error;
2875 return data.res;
2878 struct isl_forall_user_data {
2879 isl_bool res;
2880 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2881 void *user;
2884 static isl_stat forall_user_entry(void **entry, void *user)
2886 struct isl_forall_user_data *data = user;
2887 isl_map *map = *entry;
2889 data->res = data->fn(map, data->user);
2890 if (data->res < 0)
2891 return isl_stat_error;
2893 if (!data->res)
2894 return isl_stat_error;
2896 return isl_stat_ok;
2899 /* Check if fn(map, user) returns true for all maps "map" in umap.
2901 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2902 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2904 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2906 if (!umap)
2907 return isl_bool_error;
2909 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2910 &forall_user_entry, &data) < 0 && data.res)
2911 return isl_bool_error;
2913 return data.res;
2916 /* Is "umap" obviously empty?
2918 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2920 isl_size n;
2922 n = isl_union_map_n_map(umap);
2923 if (n < 0)
2924 return isl_bool_error;
2925 return n == 0;
2928 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2930 return union_map_forall(umap, &isl_map_is_empty);
2933 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2935 return isl_union_map_is_empty(uset);
2938 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2940 isl_bool is_subset;
2941 isl_space *space;
2942 isl_map *id;
2943 isl_bool match;
2945 match = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
2946 if (match < 0)
2947 return isl_bool_error;
2948 if (!match)
2949 return isl_bool_false;
2951 space = isl_map_get_space(map);
2952 id = isl_map_identity(space);
2954 is_subset = isl_map_is_subset(map, id);
2956 isl_map_free(id);
2958 return is_subset;
2961 /* Given an isl_union_map that consists of a single map, check
2962 * if it is single-valued.
2964 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2966 isl_map *map;
2967 isl_bool sv;
2969 umap = isl_union_map_copy(umap);
2970 map = isl_map_from_union_map(umap);
2971 sv = isl_map_is_single_valued(map);
2972 isl_map_free(map);
2974 return sv;
2977 /* Internal data structure for single_valued_on_domain.
2979 * "umap" is the union map to be tested.
2980 * "sv" is set to 1 as long as "umap" may still be single-valued.
2982 struct isl_union_map_is_sv_data {
2983 isl_union_map *umap;
2984 isl_bool sv;
2987 /* Check if the data->umap is single-valued on "set".
2989 * If data->umap consists of a single map on "set", then test it
2990 * as an isl_map.
2992 * Otherwise, compute
2994 * M \circ M^-1
2996 * check if the result is a subset of the identity mapping and
2997 * store the result in data->sv.
2999 * Terminate as soon as data->umap has been determined not to
3000 * be single-valued.
3002 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
3004 struct isl_union_map_is_sv_data *data = user;
3005 isl_union_map *umap, *test;
3006 isl_size n;
3008 umap = isl_union_map_copy(data->umap);
3009 umap = isl_union_map_intersect_domain(umap,
3010 isl_union_set_from_set(set));
3012 n = isl_union_map_n_map(umap);
3013 if (n < 0) {
3014 data->sv = isl_bool_error;
3015 } else if (n == 1) {
3016 data->sv = single_map_is_single_valued(umap);
3017 isl_union_map_free(umap);
3018 } else {
3019 test = isl_union_map_reverse(isl_union_map_copy(umap));
3020 test = isl_union_map_apply_range(test, umap);
3022 data->sv = union_map_forall(test, &is_subset_of_identity);
3024 isl_union_map_free(test);
3027 if (data->sv < 0 || !data->sv)
3028 return isl_stat_error;
3029 return isl_stat_ok;
3032 /* Check if the given map is single-valued.
3034 * If the union map consists of a single map, then test it as an isl_map.
3035 * Otherwise, check if the union map is single-valued on each of its
3036 * domain spaces.
3038 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
3040 isl_union_map *universe;
3041 isl_union_set *domain;
3042 struct isl_union_map_is_sv_data data;
3043 isl_size n;
3045 n = isl_union_map_n_map(umap);
3046 if (n < 0)
3047 return isl_bool_error;
3048 if (n == 1)
3049 return single_map_is_single_valued(umap);
3051 universe = isl_union_map_universe(isl_union_map_copy(umap));
3052 domain = isl_union_map_domain(universe);
3054 data.sv = isl_bool_true;
3055 data.umap = umap;
3056 if (isl_union_set_foreach_set(domain,
3057 &single_valued_on_domain, &data) < 0 && data.sv)
3058 data.sv = isl_bool_error;
3059 isl_union_set_free(domain);
3061 return data.sv;
3064 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
3066 isl_bool in;
3068 umap = isl_union_map_copy(umap);
3069 umap = isl_union_map_reverse(umap);
3070 in = isl_union_map_is_single_valued(umap);
3071 isl_union_map_free(umap);
3073 return in;
3076 /* Is "map" obviously not an identity relation because
3077 * it maps elements from one space to another space?
3078 * Update *non_identity accordingly.
3080 * In particular, if the domain and range spaces are the same,
3081 * then the map is not considered to obviously not be an identity relation.
3082 * Otherwise, the map is considered to obviously not be an identity relation
3083 * if it is is non-empty.
3085 * If "map" is determined to obviously not be an identity relation,
3086 * then the search is aborted.
3088 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
3090 isl_bool *non_identity = user;
3091 isl_bool equal;
3093 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
3094 if (equal >= 0 && !equal)
3095 *non_identity = isl_bool_not(isl_map_is_empty(map));
3096 else
3097 *non_identity = isl_bool_not(equal);
3098 isl_map_free(map);
3100 if (*non_identity < 0 || *non_identity)
3101 return isl_stat_error;
3103 return isl_stat_ok;
3106 /* Is "umap" obviously not an identity relation because
3107 * it maps elements from one space to another space?
3109 * As soon as a map has been found that maps elements to a different space,
3110 * non_identity is changed and the search is aborted.
3112 static isl_bool isl_union_map_plain_is_not_identity(
3113 __isl_keep isl_union_map *umap)
3115 isl_bool non_identity;
3117 non_identity = isl_bool_false;
3118 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
3119 &non_identity) < 0 &&
3120 non_identity == isl_bool_false)
3121 return isl_bool_error;
3123 return non_identity;
3126 /* Does "map" only map elements to themselves?
3127 * Update *identity accordingly.
3129 * If "map" is determined not to be an identity relation,
3130 * then the search is aborted.
3132 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
3134 isl_bool *identity = user;
3136 *identity = isl_map_is_identity(map);
3137 isl_map_free(map);
3139 if (*identity < 0 || !*identity)
3140 return isl_stat_error;
3142 return isl_stat_ok;
3145 /* Does "umap" only map elements to themselves?
3147 * First check if there are any maps that map elements to different spaces.
3148 * If not, then check that all the maps (between identical spaces)
3149 * are identity relations.
3151 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
3153 isl_bool non_identity;
3154 isl_bool identity;
3156 non_identity = isl_union_map_plain_is_not_identity(umap);
3157 if (non_identity < 0 || non_identity)
3158 return isl_bool_not(non_identity);
3160 identity = isl_bool_true;
3161 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
3162 identity == isl_bool_true)
3163 return isl_bool_error;
3165 return identity;
3168 /* Represents a map that has a fixed value (v) for one of its
3169 * range dimensions.
3170 * The map in this structure is not reference counted, so it
3171 * is only valid while the isl_union_map from which it was
3172 * obtained is still alive.
3174 struct isl_fixed_map {
3175 isl_int v;
3176 isl_map *map;
3179 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
3180 int n)
3182 int i;
3183 struct isl_fixed_map *v;
3185 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
3186 if (!v)
3187 return NULL;
3188 for (i = 0; i < n; ++i)
3189 isl_int_init(v[i].v);
3190 return v;
3193 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
3195 int i;
3197 if (!v)
3198 return;
3199 for (i = 0; i < n; ++i)
3200 isl_int_clear(v[i].v);
3201 free(v);
3204 /* Compare the "v" field of two isl_fixed_map structs.
3206 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
3208 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
3209 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
3211 return isl_int_cmp(e1->v, e2->v);
3214 /* Internal data structure used while checking whether all maps
3215 * in a union_map have a fixed value for a given output dimension.
3216 * v is the list of maps, with the fixed value for the dimension
3217 * n is the number of maps considered so far
3218 * pos is the output dimension under investigation
3220 struct isl_fixed_dim_data {
3221 struct isl_fixed_map *v;
3222 int n;
3223 int pos;
3226 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
3228 struct isl_fixed_dim_data *data = user;
3230 data->v[data->n].map = map;
3231 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
3232 &data->v[data->n++].v);
3235 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3236 int first, int n_range);
3238 /* Given a list of the maps, with their fixed values at output dimension "pos",
3239 * check whether the ranges of the maps form an obvious partition.
3241 * We first sort the maps according to their fixed values.
3242 * If all maps have a different value, then we know the ranges form
3243 * a partition.
3244 * Otherwise, we collect the maps with the same fixed value and
3245 * check whether each such collection is obviously injective
3246 * based on later dimensions.
3248 static int separates(struct isl_fixed_map *v, int n,
3249 __isl_take isl_space *space, int pos, int n_range)
3251 int i;
3253 if (!v)
3254 goto error;
3256 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
3258 for (i = 0; i + 1 < n; ++i) {
3259 int j, k;
3260 isl_union_map *part;
3261 int injective;
3263 for (j = i + 1; j < n; ++j)
3264 if (isl_int_ne(v[i].v, v[j].v))
3265 break;
3267 if (j == i + 1)
3268 continue;
3270 part = isl_union_map_alloc(isl_space_copy(space), j - i);
3271 for (k = i; k < j; ++k)
3272 part = isl_union_map_add_map(part,
3273 isl_map_copy(v[k].map));
3275 injective = plain_injective_on_range(part, pos + 1, n_range);
3276 if (injective < 0)
3277 goto error;
3278 if (!injective)
3279 break;
3281 i = j - 1;
3284 isl_space_free(space);
3285 free_isl_fixed_map_array(v, n);
3286 return i + 1 >= n;
3287 error:
3288 isl_space_free(space);
3289 free_isl_fixed_map_array(v, n);
3290 return -1;
3293 /* Check whether the maps in umap have obviously distinct ranges.
3294 * In particular, check for an output dimension in the range
3295 * [first,n_range) for which all maps have a fixed value
3296 * and then check if these values, possibly along with fixed values
3297 * at later dimensions, entail distinct ranges.
3299 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3300 int first, int n_range)
3302 isl_ctx *ctx;
3303 isl_size n;
3304 struct isl_fixed_dim_data data = { NULL };
3306 ctx = isl_union_map_get_ctx(umap);
3308 n = isl_union_map_n_map(umap);
3309 if (n < 0)
3310 goto error;
3312 if (n <= 1) {
3313 isl_union_map_free(umap);
3314 return isl_bool_true;
3317 if (first >= n_range) {
3318 isl_union_map_free(umap);
3319 return isl_bool_false;
3322 data.v = alloc_isl_fixed_map_array(ctx, n);
3323 if (!data.v)
3324 goto error;
3326 for (data.pos = first; data.pos < n_range; ++data.pos) {
3327 isl_bool fixed;
3328 int injective;
3329 isl_space *space;
3331 data.n = 0;
3332 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3333 if (fixed < 0)
3334 goto error;
3335 if (!fixed)
3336 continue;
3337 space = isl_union_map_get_space(umap);
3338 injective = separates(data.v, n, space, data.pos, n_range);
3339 isl_union_map_free(umap);
3340 return injective;
3343 free_isl_fixed_map_array(data.v, n);
3344 isl_union_map_free(umap);
3346 return isl_bool_false;
3347 error:
3348 free_isl_fixed_map_array(data.v, n);
3349 isl_union_map_free(umap);
3350 return isl_bool_error;
3353 /* Check whether the maps in umap that map to subsets of "ran"
3354 * have obviously distinct ranges.
3356 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3357 void *user)
3359 isl_size dim;
3360 isl_union_map *umap = user;
3362 dim = isl_set_dim(ran, isl_dim_set);
3363 if (dim < 0)
3364 return isl_bool_error;
3366 umap = isl_union_map_copy(umap);
3367 umap = isl_union_map_intersect_range(umap,
3368 isl_union_set_from_set(isl_set_copy(ran)));
3369 return plain_injective_on_range(umap, 0, dim);
3372 /* Check if the given union_map is obviously injective.
3374 * In particular, we first check if all individual maps are obviously
3375 * injective and then check if all the ranges of these maps are
3376 * obviously disjoint.
3378 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3380 isl_bool in;
3381 isl_union_map *univ;
3382 isl_union_set *ran;
3384 in = union_map_forall(umap, &isl_map_plain_is_injective);
3385 if (in < 0)
3386 return isl_bool_error;
3387 if (!in)
3388 return isl_bool_false;
3390 univ = isl_union_map_universe(isl_union_map_copy(umap));
3391 ran = isl_union_map_range(univ);
3393 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3395 isl_union_set_free(ran);
3397 return in;
3400 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3402 isl_bool sv;
3404 sv = isl_union_map_is_single_valued(umap);
3405 if (sv < 0 || !sv)
3406 return sv;
3408 return isl_union_map_is_injective(umap);
3411 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3413 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3414 struct isl_un_op_control control = {
3415 .filter = &un_op_filter_drop_user,
3416 .filter_user = &data,
3417 .fn_map = &isl_map_zip,
3419 return un_op(umap, &control);
3422 /* Given a union map, take the maps of the form A -> (B -> C) and
3423 * return the union of the corresponding maps (A -> B) -> C.
3425 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3427 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3428 struct isl_un_op_control control = {
3429 .filter = &un_op_filter_drop_user,
3430 .filter_user = &data,
3431 .fn_map = &isl_map_uncurry,
3433 return un_op(umap, &control);
3436 /* Given a union map, take the maps of the form (A -> B) -> C and
3437 * return the union of the corresponding maps A -> (B -> C).
3439 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3441 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3442 struct isl_un_op_control control = {
3443 .filter = &un_op_filter_drop_user,
3444 .filter_user = &data,
3445 .fn_map = &isl_map_curry,
3447 return un_op(umap, &control);
3450 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3451 * return the union of the corresponding maps A -> (B -> (C -> D)).
3453 __isl_give isl_union_map *isl_union_map_range_curry(
3454 __isl_take isl_union_map *umap)
3456 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3457 struct isl_un_op_control control = {
3458 .filter = &un_op_filter_drop_user,
3459 .filter_user = &data,
3460 .fn_map = &isl_map_range_curry,
3462 return un_op(umap, &control);
3465 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3467 struct isl_un_op_control control = {
3468 .fn_map = &isl_set_lift,
3470 return un_op(uset, &control);
3473 static isl_stat coefficients_entry(void **entry, void *user)
3475 isl_set *set = *entry;
3476 isl_union_set **res = user;
3478 set = isl_set_copy(set);
3479 set = isl_set_from_basic_set(isl_set_coefficients(set));
3480 *res = isl_union_set_add_set(*res, set);
3482 return isl_stat_ok;
3485 __isl_give isl_union_set *isl_union_set_coefficients(
3486 __isl_take isl_union_set *uset)
3488 isl_ctx *ctx;
3489 isl_space *space;
3490 isl_union_set *res;
3492 if (!uset)
3493 return NULL;
3495 ctx = isl_union_set_get_ctx(uset);
3496 space = isl_space_set_alloc(ctx, 0, 0);
3497 res = isl_union_map_alloc(space, uset->table.n);
3498 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3499 &coefficients_entry, &res) < 0)
3500 goto error;
3502 isl_union_set_free(uset);
3503 return res;
3504 error:
3505 isl_union_set_free(uset);
3506 isl_union_set_free(res);
3507 return NULL;
3510 static isl_stat solutions_entry(void **entry, void *user)
3512 isl_set *set = *entry;
3513 isl_union_set **res = user;
3515 set = isl_set_copy(set);
3516 set = isl_set_from_basic_set(isl_set_solutions(set));
3517 if (!*res)
3518 *res = isl_union_set_from_set(set);
3519 else
3520 *res = isl_union_set_add_set(*res, set);
3522 if (!*res)
3523 return isl_stat_error;
3525 return isl_stat_ok;
3528 __isl_give isl_union_set *isl_union_set_solutions(
3529 __isl_take isl_union_set *uset)
3531 isl_union_set *res = NULL;
3533 if (!uset)
3534 return NULL;
3536 if (uset->table.n == 0) {
3537 res = isl_union_set_empty(isl_union_set_get_space(uset));
3538 isl_union_set_free(uset);
3539 return res;
3542 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3543 &solutions_entry, &res) < 0)
3544 goto error;
3546 isl_union_set_free(uset);
3547 return res;
3548 error:
3549 isl_union_set_free(uset);
3550 isl_union_set_free(res);
3551 return NULL;
3554 /* Is the domain space of "map" equal to "space"?
3556 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3558 return isl_map_space_tuple_is_equal(map, isl_dim_in,
3559 space, isl_dim_out);
3562 /* Is the range space of "map" equal to "space"?
3564 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3566 return isl_map_space_tuple_is_equal(map, isl_dim_out,
3567 space, isl_dim_out);
3570 /* Is the set space of "map" equal to "space"?
3572 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3574 return isl_map_space_tuple_is_equal(map, isl_dim_set,
3575 space, isl_dim_out);
3578 /* Internal data structure for preimage_pw_multi_aff.
3580 * "pma" is the function under which the preimage should be taken.
3581 * "space" is the space of "pma".
3582 * "res" collects the results.
3583 * "fn" computes the preimage for a given map.
3584 * "match" returns true if "fn" can be called.
3586 struct isl_union_map_preimage_data {
3587 isl_space *space;
3588 isl_pw_multi_aff *pma;
3589 isl_union_map *res;
3590 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3591 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3592 __isl_take isl_pw_multi_aff *pma);
3595 /* Call data->fn to compute the preimage of the domain or range of *entry
3596 * under the function represented by data->pma, provided the domain/range
3597 * space of *entry matches the target space of data->pma
3598 * (as given by data->match), and add the result to data->res.
3600 static isl_stat preimage_entry(void **entry, void *user)
3602 int m;
3603 isl_map *map = *entry;
3604 struct isl_union_map_preimage_data *data = user;
3605 isl_bool empty;
3607 m = data->match(map, data->space);
3608 if (m < 0)
3609 return isl_stat_error;
3610 if (!m)
3611 return isl_stat_ok;
3613 map = isl_map_copy(map);
3614 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3616 empty = isl_map_is_empty(map);
3617 if (empty < 0 || empty) {
3618 isl_map_free(map);
3619 return empty < 0 ? isl_stat_error : isl_stat_ok;
3622 data->res = isl_union_map_add_map(data->res, map);
3624 return isl_stat_ok;
3627 /* Compute the preimage of the domain or range of "umap" under the function
3628 * represented by "pma".
3629 * In other words, plug in "pma" in the domain or range of "umap".
3630 * The function "fn" performs the actual preimage computation on a map,
3631 * while "match" determines to which maps the function should be applied.
3633 static __isl_give isl_union_map *preimage_pw_multi_aff(
3634 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3635 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3636 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3637 __isl_take isl_pw_multi_aff *pma))
3639 isl_ctx *ctx;
3640 isl_space *space;
3641 struct isl_union_map_preimage_data data;
3643 umap = isl_union_map_align_params(umap,
3644 isl_pw_multi_aff_get_space(pma));
3645 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3647 if (!umap || !pma)
3648 goto error;
3650 ctx = isl_union_map_get_ctx(umap);
3651 space = isl_union_map_get_space(umap);
3652 data.space = isl_pw_multi_aff_get_space(pma);
3653 data.pma = pma;
3654 data.res = isl_union_map_alloc(space, umap->table.n);
3655 data.match = match;
3656 data.fn = fn;
3657 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3658 &data) < 0)
3659 data.res = isl_union_map_free(data.res);
3661 isl_space_free(data.space);
3662 isl_union_map_free(umap);
3663 isl_pw_multi_aff_free(pma);
3664 return data.res;
3665 error:
3666 isl_union_map_free(umap);
3667 isl_pw_multi_aff_free(pma);
3668 return NULL;
3671 /* Compute the preimage of the domain of "umap" under the function
3672 * represented by "pma".
3673 * In other words, plug in "pma" in the domain of "umap".
3674 * The result contains maps that live in the same spaces as the maps of "umap"
3675 * with domain space equal to the target space of "pma",
3676 * except that the domain has been replaced by the domain space of "pma".
3678 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3679 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3681 return preimage_pw_multi_aff(umap, pma, &domain_match,
3682 &isl_map_preimage_domain_pw_multi_aff);
3685 /* Compute the preimage of the range of "umap" under the function
3686 * represented by "pma".
3687 * In other words, plug in "pma" in the range of "umap".
3688 * The result contains maps that live in the same spaces as the maps of "umap"
3689 * with range space equal to the target space of "pma",
3690 * except that the range has been replaced by the domain space of "pma".
3692 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3693 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3695 return preimage_pw_multi_aff(umap, pma, &range_match,
3696 &isl_map_preimage_range_pw_multi_aff);
3699 /* Compute the preimage of "uset" under the function represented by "pma".
3700 * In other words, plug in "pma" in "uset".
3701 * The result contains sets that live in the same spaces as the sets of "uset"
3702 * with space equal to the target space of "pma",
3703 * except that the space has been replaced by the domain space of "pma".
3705 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3706 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3708 return preimage_pw_multi_aff(uset, pma, &set_match,
3709 &isl_set_preimage_pw_multi_aff);
3712 /* Compute the preimage of the domain of "umap" under the function
3713 * represented by "ma".
3714 * In other words, plug in "ma" in the domain of "umap".
3715 * The result contains maps that live in the same spaces as the maps of "umap"
3716 * with domain space equal to the target space of "ma",
3717 * except that the domain has been replaced by the domain space of "ma".
3719 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3720 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3722 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3723 isl_pw_multi_aff_from_multi_aff(ma));
3726 /* Compute the preimage of the range of "umap" under the function
3727 * represented by "ma".
3728 * In other words, plug in "ma" in the range of "umap".
3729 * The result contains maps that live in the same spaces as the maps of "umap"
3730 * with range space equal to the target space of "ma",
3731 * except that the range has been replaced by the domain space of "ma".
3733 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3734 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3736 return isl_union_map_preimage_range_pw_multi_aff(umap,
3737 isl_pw_multi_aff_from_multi_aff(ma));
3740 /* Compute the preimage of "uset" under the function represented by "ma".
3741 * In other words, plug in "ma" in "uset".
3742 * The result contains sets that live in the same spaces as the sets of "uset"
3743 * with space equal to the target space of "ma",
3744 * except that the space has been replaced by the domain space of "ma".
3746 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3747 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3749 return isl_union_set_preimage_pw_multi_aff(uset,
3750 isl_pw_multi_aff_from_multi_aff(ma));
3753 /* Internal data structure for preimage_multi_pw_aff.
3755 * "mpa" is the function under which the preimage should be taken.
3756 * "space" is the space of "mpa".
3757 * "res" collects the results.
3758 * "fn" computes the preimage for a given map.
3759 * "match" returns true if "fn" can be called.
3761 struct isl_union_map_preimage_mpa_data {
3762 isl_space *space;
3763 isl_multi_pw_aff *mpa;
3764 isl_union_map *res;
3765 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3766 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3767 __isl_take isl_multi_pw_aff *mpa);
3770 /* Call data->fn to compute the preimage of the domain or range of *entry
3771 * under the function represented by data->mpa, provided the domain/range
3772 * space of *entry matches the target space of data->mpa
3773 * (as given by data->match), and add the result to data->res.
3775 static isl_stat preimage_mpa_entry(void **entry, void *user)
3777 int m;
3778 isl_map *map = *entry;
3779 struct isl_union_map_preimage_mpa_data *data = user;
3780 isl_bool empty;
3782 m = data->match(map, data->space);
3783 if (m < 0)
3784 return isl_stat_error;
3785 if (!m)
3786 return isl_stat_ok;
3788 map = isl_map_copy(map);
3789 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3791 empty = isl_map_is_empty(map);
3792 if (empty < 0 || empty) {
3793 isl_map_free(map);
3794 return empty < 0 ? isl_stat_error : isl_stat_ok;
3797 data->res = isl_union_map_add_map(data->res, map);
3799 return isl_stat_ok;
3802 /* Compute the preimage of the domain or range of "umap" under the function
3803 * represented by "mpa".
3804 * In other words, plug in "mpa" in the domain or range of "umap".
3805 * The function "fn" performs the actual preimage computation on a map,
3806 * while "match" determines to which maps the function should be applied.
3808 static __isl_give isl_union_map *preimage_multi_pw_aff(
3809 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3810 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3811 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3812 __isl_take isl_multi_pw_aff *mpa))
3814 isl_ctx *ctx;
3815 isl_space *space;
3816 struct isl_union_map_preimage_mpa_data data;
3818 umap = isl_union_map_align_params(umap,
3819 isl_multi_pw_aff_get_space(mpa));
3820 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3822 if (!umap || !mpa)
3823 goto error;
3825 ctx = isl_union_map_get_ctx(umap);
3826 space = isl_union_map_get_space(umap);
3827 data.space = isl_multi_pw_aff_get_space(mpa);
3828 data.mpa = mpa;
3829 data.res = isl_union_map_alloc(space, umap->table.n);
3830 data.match = match;
3831 data.fn = fn;
3832 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3833 &data) < 0)
3834 data.res = isl_union_map_free(data.res);
3836 isl_space_free(data.space);
3837 isl_union_map_free(umap);
3838 isl_multi_pw_aff_free(mpa);
3839 return data.res;
3840 error:
3841 isl_union_map_free(umap);
3842 isl_multi_pw_aff_free(mpa);
3843 return NULL;
3846 /* Compute the preimage of the domain of "umap" under the function
3847 * represented by "mpa".
3848 * In other words, plug in "mpa" in the domain of "umap".
3849 * The result contains maps that live in the same spaces as the maps of "umap"
3850 * with domain space equal to the target space of "mpa",
3851 * except that the domain has been replaced by the domain space of "mpa".
3853 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3854 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3856 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3857 &isl_map_preimage_domain_multi_pw_aff);
3860 /* Internal data structure for preimage_upma.
3862 * "umap" is the map of which the preimage should be computed.
3863 * "res" collects the results.
3864 * "fn" computes the preimage for a given piecewise multi-affine function.
3866 struct isl_union_map_preimage_upma_data {
3867 isl_union_map *umap;
3868 isl_union_map *res;
3869 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3870 __isl_take isl_pw_multi_aff *pma);
3873 /* Call data->fn to compute the preimage of the domain or range of data->umap
3874 * under the function represented by pma and add the result to data->res.
3876 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3878 struct isl_union_map_preimage_upma_data *data = user;
3879 isl_union_map *umap;
3881 umap = isl_union_map_copy(data->umap);
3882 umap = data->fn(umap, pma);
3883 data->res = isl_union_map_union(data->res, umap);
3885 return data->res ? isl_stat_ok : isl_stat_error;
3888 /* Compute the preimage of the domain or range of "umap" under the function
3889 * represented by "upma".
3890 * In other words, plug in "upma" in the domain or range of "umap".
3891 * The function "fn" performs the actual preimage computation
3892 * on a piecewise multi-affine function.
3894 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3895 __isl_take isl_union_map *umap,
3896 __isl_take isl_union_pw_multi_aff *upma,
3897 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3898 __isl_take isl_pw_multi_aff *pma))
3900 struct isl_union_map_preimage_upma_data data;
3902 data.umap = umap;
3903 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3904 data.fn = fn;
3905 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3906 &preimage_upma, &data) < 0)
3907 data.res = isl_union_map_free(data.res);
3909 isl_union_map_free(umap);
3910 isl_union_pw_multi_aff_free(upma);
3912 return data.res;
3915 /* Compute the preimage of the domain of "umap" under the function
3916 * represented by "upma".
3917 * In other words, plug in "upma" in the domain of "umap".
3918 * The result contains maps that live in the same spaces as the maps of "umap"
3919 * with domain space equal to one of the target spaces of "upma",
3920 * except that the domain has been replaced by one of the domain spaces that
3921 * correspond to that target space of "upma".
3923 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3924 __isl_take isl_union_map *umap,
3925 __isl_take isl_union_pw_multi_aff *upma)
3927 return preimage_union_pw_multi_aff(umap, upma,
3928 &isl_union_map_preimage_domain_pw_multi_aff);
3931 /* Compute the preimage of the range of "umap" under the function
3932 * represented by "upma".
3933 * In other words, plug in "upma" in the range of "umap".
3934 * The result contains maps that live in the same spaces as the maps of "umap"
3935 * with range space equal to one of the target spaces of "upma",
3936 * except that the range has been replaced by one of the domain spaces that
3937 * correspond to that target space of "upma".
3939 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3940 __isl_take isl_union_map *umap,
3941 __isl_take isl_union_pw_multi_aff *upma)
3943 return preimage_union_pw_multi_aff(umap, upma,
3944 &isl_union_map_preimage_range_pw_multi_aff);
3947 /* Compute the preimage of "uset" under the function represented by "upma".
3948 * In other words, plug in "upma" in the range of "uset".
3949 * The result contains sets that live in the same spaces as the sets of "uset"
3950 * with space equal to one of the target spaces of "upma",
3951 * except that the space has been replaced by one of the domain spaces that
3952 * correspond to that target space of "upma".
3954 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3955 __isl_take isl_union_set *uset,
3956 __isl_take isl_union_pw_multi_aff *upma)
3958 return preimage_union_pw_multi_aff(uset, upma,
3959 &isl_union_set_preimage_pw_multi_aff);
3962 /* Reset the user pointer on all identifiers of parameters and tuples
3963 * of the spaces of "umap".
3965 __isl_give isl_union_map *isl_union_map_reset_user(
3966 __isl_take isl_union_map *umap)
3968 umap = isl_union_map_cow(umap);
3969 if (!umap)
3970 return NULL;
3971 umap->dim = isl_space_reset_user(umap->dim);
3972 if (!umap->dim)
3973 return isl_union_map_free(umap);
3974 return total(umap, &isl_map_reset_user);
3977 /* Reset the user pointer on all identifiers of parameters and tuples
3978 * of the spaces of "uset".
3980 __isl_give isl_union_set *isl_union_set_reset_user(
3981 __isl_take isl_union_set *uset)
3983 return isl_union_map_reset_user(uset);
3986 /* Remove all existentially quantified variables and integer divisions
3987 * from "umap" using Fourier-Motzkin elimination.
3989 __isl_give isl_union_map *isl_union_map_remove_divs(
3990 __isl_take isl_union_map *umap)
3992 return total(umap, &isl_map_remove_divs);
3995 /* Remove all existentially quantified variables and integer divisions
3996 * from "uset" using Fourier-Motzkin elimination.
3998 __isl_give isl_union_set *isl_union_set_remove_divs(
3999 __isl_take isl_union_set *uset)
4001 return isl_union_map_remove_divs(uset);
4004 /* Internal data structure for isl_union_map_project_out.
4005 * "type", "first" and "n" are the arguments for the isl_map_project_out
4006 * call.
4007 * "res" collects the results.
4009 struct isl_union_map_project_out_data {
4010 enum isl_dim_type type;
4011 unsigned first;
4012 unsigned n;
4014 isl_union_map *res;
4017 /* Turn the data->n dimensions of type data->type, starting at data->first
4018 * into existentially quantified variables and add the result to data->res.
4020 static isl_stat project_out(__isl_take isl_map *map, void *user)
4022 struct isl_union_map_project_out_data *data = user;
4024 map = isl_map_project_out(map, data->type, data->first, data->n);
4025 data->res = isl_union_map_add_map(data->res, map);
4027 return isl_stat_ok;
4030 /* Turn the "n" dimensions of type "type", starting at "first"
4031 * into existentially quantified variables.
4032 * Since the space of an isl_union_map only contains parameters,
4033 * type is required to be equal to isl_dim_param.
4035 __isl_give isl_union_map *isl_union_map_project_out(
4036 __isl_take isl_union_map *umap,
4037 enum isl_dim_type type, unsigned first, unsigned n)
4039 isl_space *space;
4040 struct isl_union_map_project_out_data data = { type, first, n };
4042 if (!umap)
4043 return NULL;
4045 if (type != isl_dim_param)
4046 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4047 "can only project out parameters",
4048 return isl_union_map_free(umap));
4050 space = isl_union_map_get_space(umap);
4051 space = isl_space_drop_dims(space, type, first, n);
4052 data.res = isl_union_map_empty(space);
4053 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
4054 data.res = isl_union_map_free(data.res);
4056 isl_union_map_free(umap);
4058 return data.res;
4061 #undef TYPE
4062 #define TYPE isl_union_map
4063 #include "isl_project_out_all_params_templ.c"
4064 #include "isl_project_out_param_templ.c"
4066 /* Turn the "n" dimensions of type "type", starting at "first"
4067 * into existentially quantified variables.
4068 * Since the space of an isl_union_set only contains parameters,
4069 * "type" is required to be equal to isl_dim_param.
4071 __isl_give isl_union_set *isl_union_set_project_out(
4072 __isl_take isl_union_set *uset,
4073 enum isl_dim_type type, unsigned first, unsigned n)
4075 return isl_union_map_project_out(uset, type, first, n);
4078 /* Project out all parameters from "uset" by existentially quantifying
4079 * over them.
4081 __isl_give isl_union_set *isl_union_set_project_out_all_params(
4082 __isl_take isl_union_set *uset)
4084 return uset_from_umap(
4085 isl_union_map_project_out_all_params(uset_to_umap(uset)));
4088 /* Internal data structure for isl_union_map_involves_dims.
4089 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
4091 struct isl_union_map_involves_dims_data {
4092 unsigned first;
4093 unsigned n;
4096 /* Does "map" _not_ involve the data->n parameters starting at data->first?
4098 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
4100 struct isl_union_map_involves_dims_data *data = user;
4101 isl_bool involves;
4103 involves = isl_map_involves_dims(map,
4104 isl_dim_param, data->first, data->n);
4105 return isl_bool_not(involves);
4108 /* Does "umap" involve any of the n parameters starting at first?
4109 * "type" is required to be set to isl_dim_param.
4111 * "umap" involves any of those parameters if any of its maps
4112 * involve the parameters. In other words, "umap" does not
4113 * involve any of the parameters if all its maps to not
4114 * involve the parameters.
4116 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
4117 enum isl_dim_type type, unsigned first, unsigned n)
4119 struct isl_union_map_involves_dims_data data = { first, n };
4120 isl_bool excludes;
4122 if (type != isl_dim_param)
4123 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4124 "can only reference parameters", return isl_bool_error);
4126 excludes = union_map_forall_user(umap, &map_excludes, &data);
4128 return isl_bool_not(excludes);
4131 /* Internal data structure for isl_union_map_reset_range_space.
4132 * "range" is the space from which to set the range space.
4133 * "res" collects the results.
4135 struct isl_union_map_reset_range_space_data {
4136 isl_space *range;
4137 isl_union_map *res;
4140 /* Replace the range space of "map" by the range space of data->range and
4141 * add the result to data->res.
4143 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
4145 struct isl_union_map_reset_range_space_data *data = user;
4146 isl_space *space;
4148 space = isl_map_get_space(map);
4149 space = isl_space_domain(space);
4150 space = isl_space_extend_domain_with_range(space,
4151 isl_space_copy(data->range));
4152 map = isl_map_reset_space(map, space);
4153 data->res = isl_union_map_add_map(data->res, map);
4155 return data->res ? isl_stat_ok : isl_stat_error;
4158 /* Replace the range space of all the maps in "umap" by
4159 * the range space of "space".
4161 * This assumes that all maps have the same output dimension.
4162 * This function should therefore not be made publicly available.
4164 * Since the spaces of the maps change, so do their hash value.
4165 * We therefore need to create a new isl_union_map.
4167 __isl_give isl_union_map *isl_union_map_reset_range_space(
4168 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4170 struct isl_union_map_reset_range_space_data data = { space };
4172 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4173 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
4174 data.res = isl_union_map_free(data.res);
4176 isl_space_free(space);
4177 isl_union_map_free(umap);
4178 return data.res;
4181 /* Check that "umap" and "space" have the same number of parameters.
4183 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
4184 __isl_keep isl_space *space)
4186 isl_size dim1, dim2;
4188 dim1 = isl_union_map_dim(umap, isl_dim_param);
4189 dim2 = isl_space_dim(space, isl_dim_param);
4190 if (dim1 < 0 || dim2 < 0)
4191 return isl_stat_error;
4192 if (dim1 == dim2)
4193 return isl_stat_ok;
4194 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4195 "number of parameters does not match", return isl_stat_error);
4198 /* Internal data structure for isl_union_map_reset_equal_dim_space.
4199 * "space" is the target space.
4200 * "res" collects the results.
4202 struct isl_union_map_reset_params_data {
4203 isl_space *space;
4204 isl_union_map *res;
4207 /* Replace the parameters of "map" by those of data->space and
4208 * add the result to data->res.
4210 static isl_stat reset_params(__isl_take isl_map *map, void *user)
4212 struct isl_union_map_reset_params_data *data = user;
4213 isl_space *space;
4215 space = isl_map_get_space(map);
4216 space = isl_space_replace_params(space, data->space);
4217 map = isl_map_reset_equal_dim_space(map, space);
4218 data->res = isl_union_map_add_map(data->res, map);
4220 return data->res ? isl_stat_ok : isl_stat_error;
4223 /* Replace the space of "umap" by "space", without modifying
4224 * the dimension of "umap", i.e., the number of parameters of "umap".
4226 * Since the hash values of the maps in the union map depend
4227 * on the parameters, a new union map needs to be constructed.
4229 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
4230 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4232 struct isl_union_map_reset_params_data data = { space };
4233 isl_bool equal;
4234 isl_space *umap_space;
4236 umap_space = isl_union_map_peek_space(umap);
4237 equal = isl_space_is_equal(umap_space, space);
4238 if (equal < 0)
4239 goto error;
4240 if (equal) {
4241 isl_space_free(space);
4242 return umap;
4244 if (check_union_map_space_equal_dim(umap, space) < 0)
4245 goto error;
4247 data.res = isl_union_map_empty(isl_space_copy(space));
4248 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
4249 data.res = isl_union_map_free(data.res);
4251 isl_space_free(space);
4252 isl_union_map_free(umap);
4253 return data.res;
4254 error:
4255 isl_union_map_free(umap);
4256 isl_space_free(space);
4257 return NULL;
4260 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4261 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4262 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4263 * to the domain and the range of each map.
4264 * "res" collects the results.
4266 struct isl_union_order_at_data {
4267 isl_multi_union_pw_aff *mupa;
4268 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4269 __isl_take isl_multi_pw_aff *mpa2);
4270 isl_union_map *res;
4273 /* Intersect "map" with the result of applying data->order to
4274 * the functions in data->mupa that apply to the domain and the range
4275 * of "map" and add the result to data->res.
4277 static isl_stat order_at(__isl_take isl_map *map, void *user)
4279 struct isl_union_order_at_data *data = user;
4280 isl_space *space;
4281 isl_multi_pw_aff *mpa1, *mpa2;
4282 isl_map *order;
4284 space = isl_space_domain(isl_map_get_space(map));
4285 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4286 space = isl_space_range(isl_map_get_space(map));
4287 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4288 order = data->order(mpa1, mpa2);
4289 map = isl_map_intersect(map, order);
4290 data->res = isl_union_map_add_map(data->res, map);
4292 return data->res ? isl_stat_ok : isl_stat_error;
4295 /* If "mupa" has a non-trivial explicit domain, then intersect
4296 * domain and range of "umap" with this explicit domain.
4297 * If the explicit domain only describes constraints on the parameters,
4298 * then the intersection only needs to be performed once.
4300 static __isl_give isl_union_map *intersect_explicit_domain(
4301 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4303 isl_bool non_trivial, is_params;
4304 isl_union_set *dom;
4306 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4307 if (non_trivial < 0)
4308 return isl_union_map_free(umap);
4309 if (!non_trivial)
4310 return umap;
4311 mupa = isl_multi_union_pw_aff_copy(mupa);
4312 dom = isl_multi_union_pw_aff_domain(mupa);
4313 is_params = isl_union_set_is_params(dom);
4314 if (is_params < 0) {
4315 isl_union_set_free(dom);
4316 return isl_union_map_free(umap);
4318 if (is_params) {
4319 isl_set *set;
4321 set = isl_union_set_params(dom);
4322 umap = isl_union_map_intersect_params(umap, set);
4323 return umap;
4325 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4326 umap = isl_union_map_intersect_range(umap, dom);
4327 return umap;
4330 /* Intersect each map in "umap" with the result of calling "order"
4331 * on the functions is "mupa" that apply to the domain and the range
4332 * of the map.
4334 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4335 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4336 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4337 __isl_take isl_multi_pw_aff *mpa2))
4339 struct isl_union_order_at_data data;
4341 umap = isl_union_map_align_params(umap,
4342 isl_multi_union_pw_aff_get_space(mupa));
4343 mupa = isl_multi_union_pw_aff_align_params(mupa,
4344 isl_union_map_get_space(umap));
4345 umap = intersect_explicit_domain(umap, mupa);
4346 data.mupa = mupa;
4347 data.order = order;
4348 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4349 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4350 data.res = isl_union_map_free(data.res);
4352 isl_multi_union_pw_aff_free(mupa);
4353 isl_union_map_free(umap);
4354 return data.res;
4357 /* Return the subset of "umap" where the domain and the range
4358 * have equal "mupa" values.
4360 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4361 __isl_take isl_union_map *umap,
4362 __isl_take isl_multi_union_pw_aff *mupa)
4364 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4365 &isl_multi_pw_aff_eq_map);
4368 #undef ORDER
4369 #define ORDER le
4370 #include "isl_union_map_lex_templ.c"
4372 #undef ORDER
4373 #define ORDER lt
4374 #include "isl_union_map_lex_templ.c"
4376 #undef ORDER
4377 #define ORDER ge
4378 #include "isl_union_map_lex_templ.c"
4380 #undef ORDER
4381 #define ORDER gt
4382 #include "isl_union_map_lex_templ.c"
4384 /* Return the union of the elements in the list "list".
4386 __isl_give isl_union_set *isl_union_set_list_union(
4387 __isl_take isl_union_set_list *list)
4389 int i;
4390 isl_size n;
4391 isl_ctx *ctx;
4392 isl_space *space;
4393 isl_union_set *res;
4395 n = isl_union_set_list_n_union_set(list);
4396 if (n < 0)
4397 goto error;
4399 ctx = isl_union_set_list_get_ctx(list);
4400 space = isl_space_params_alloc(ctx, 0);
4401 res = isl_union_set_empty(space);
4403 for (i = 0; i < n; ++i) {
4404 isl_union_set *uset_i;
4406 uset_i = isl_union_set_list_get_union_set(list, i);
4407 res = isl_union_set_union(res, uset_i);
4410 isl_union_set_list_free(list);
4411 return res;
4412 error:
4413 isl_union_set_list_free(list);
4414 return NULL;
4417 /* Update *hash with the hash value of "map".
4419 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4421 uint32_t *hash = user;
4422 uint32_t map_hash;
4424 map_hash = isl_map_get_hash(map);
4425 isl_hash_hash(*hash, map_hash);
4427 isl_map_free(map);
4428 return isl_stat_ok;
4431 /* Return a hash value that digests "umap".
4433 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4435 uint32_t hash;
4437 if (!umap)
4438 return 0;
4440 hash = isl_hash_init();
4441 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4442 return 0;
4444 return hash;
4447 /* Return a hash value that digests "uset".
4449 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4451 return isl_union_map_get_hash(uset);
4454 /* Add the number of basic sets in "set" to "n".
4456 static isl_stat add_n(__isl_take isl_set *set, void *user)
4458 int *n = user;
4459 isl_size set_n;
4461 set_n = isl_set_n_basic_set(set);
4462 *n += set_n;
4463 isl_set_free(set);
4465 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4468 /* Return the total number of basic sets in "uset".
4470 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4472 int n = 0;
4474 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4475 return -1;
4477 return n;
4480 /* Add the basic sets in "set" to "list".
4482 static isl_stat add_list(__isl_take isl_set *set, void *user)
4484 isl_basic_set_list **list = user;
4485 isl_basic_set_list *list_i;
4487 list_i = isl_set_get_basic_set_list(set);
4488 *list = isl_basic_set_list_concat(*list, list_i);
4489 isl_set_free(set);
4491 if (!*list)
4492 return isl_stat_error;
4493 return isl_stat_ok;
4496 /* Return a list containing all the basic sets in "uset".
4498 * First construct a list of the appropriate size and
4499 * then add all the elements.
4501 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4502 __isl_keep isl_union_set *uset)
4504 int n;
4505 isl_ctx *ctx;
4506 isl_basic_set_list *list;
4508 if (!uset)
4509 return NULL;
4510 ctx = isl_union_set_get_ctx(uset);
4511 n = isl_union_set_n_basic_set(uset);
4512 if (n < 0)
4513 return NULL;
4514 list = isl_basic_set_list_alloc(ctx, n);
4515 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4516 list = isl_basic_set_list_free(list);
4518 return list;
4521 /* Internal data structure for isl_union_map_remove_map_if.
4522 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4524 struct isl_union_map_remove_map_if_data {
4525 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4526 void *user;
4529 /* isl_un_op_control filter that negates the result of data->fn
4530 * called on "map".
4532 static isl_bool not(__isl_keep isl_map *map, void *user)
4534 struct isl_union_map_remove_map_if_data *data = user;
4536 return isl_bool_not(data->fn(map, data->user));
4539 /* Dummy isl_un_op_control transformation callback that
4540 * simply returns the input.
4542 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4544 return map;
4547 /* Call "fn" on every map in "umap" and remove those maps
4548 * for which the callback returns true.
4550 * Use un_op to keep only those maps that are not filtered out,
4551 * applying an identity transformation on them.
4553 __isl_give isl_union_map *isl_union_map_remove_map_if(
4554 __isl_take isl_union_map *umap,
4555 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4557 struct isl_union_map_remove_map_if_data data = { fn, user };
4558 struct isl_un_op_control control = {
4559 .filter = &not,
4560 .filter_user = &data,
4561 .fn_map = &map_id,
4563 return un_op(umap, &control);
4566 /* Does "map" have "space" as domain (ignoring parameters)?
4568 static isl_bool has_domain_space_tuples(__isl_keep isl_map *map, void *user)
4570 isl_space *space = user;
4572 return isl_space_has_domain_tuples(space, isl_map_peek_space(map));
4575 /* Does "map" have "space" as range (ignoring parameters)?
4577 static isl_bool has_range_space_tuples(__isl_keep isl_map *map, void *user)
4579 isl_space *space = user;
4581 return isl_space_has_range_tuples(space, isl_map_peek_space(map));
4584 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4586 static __isl_give isl_map *bind_range(__isl_take isl_map *map, void *user)
4588 isl_multi_id *tuple = user;
4590 return isl_map_bind_range(map, isl_multi_id_copy(tuple));
4593 /* Bind the output dimensions of "umap" to parameters with identifiers
4594 * specified by "tuple", living in the range space of "umap",
4595 * for those maps that have a matching range space.
4597 __isl_give isl_union_set *isl_union_map_bind_range(
4598 __isl_take isl_union_map *umap, __isl_take isl_multi_id *tuple)
4600 struct isl_un_op_control control = {
4601 .filter = &has_range_space_tuples,
4602 .filter_user = isl_multi_id_peek_space(tuple),
4603 .fn_map2 = &bind_range,
4604 .fn_map2_user = tuple,
4606 isl_union_set *bound;
4608 bound = uset_from_umap(un_op(umap, &control));
4609 isl_multi_id_free(tuple);
4610 return bound;
4613 /* Only keep those elements in "umap" that have a domain in "space".
4615 __isl_give isl_union_map *isl_union_map_intersect_domain_space(
4616 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4618 struct isl_un_op_control control = {
4619 .filter = &has_domain_space_tuples,
4620 .filter_user = space,
4623 umap = un_op(umap, &control);
4624 isl_space_free(space);
4625 return umap;
4628 /* Only keep those elements in "umap" that have a range in "space".
4630 __isl_give isl_union_map *isl_union_map_intersect_range_space(
4631 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4633 struct isl_un_op_control control = {
4634 .filter = &has_range_space_tuples,
4635 .filter_user = space,
4638 umap = un_op(umap, &control);
4639 isl_space_free(space);
4640 return umap;