isl_reordering: keep track of target length
[isl.git] / isl_union_map.c
blob3e3d40180807e48f21f3040f1bf08726e0d027cb
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 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
213 return isl_union_map_get_space(uset);
216 static isl_stat free_umap_entry(void **entry, void *user)
218 isl_map *map = *entry;
219 isl_map_free(map);
220 return isl_stat_ok;
223 static isl_stat add_map(__isl_take isl_map *map, void *user)
225 isl_union_map **umap = (isl_union_map **)user;
227 *umap = isl_union_map_add_map(*umap, map);
229 return isl_stat_ok;
232 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
234 isl_union_map *dup;
236 if (!umap)
237 return NULL;
239 dup = isl_union_map_empty(isl_space_copy(umap->dim));
240 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
241 goto error;
242 return dup;
243 error:
244 isl_union_map_free(dup);
245 return NULL;
248 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
250 if (!umap)
251 return NULL;
253 if (umap->ref == 1)
254 return umap;
255 umap->ref--;
256 return isl_union_map_dup(umap);
259 struct isl_union_align {
260 isl_reordering *exp;
261 isl_union_map *res;
264 static isl_stat align_entry(void **entry, void *user)
266 isl_map *map = *entry;
267 isl_reordering *exp;
268 struct isl_union_align *data = user;
270 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
271 isl_map_get_space(map));
273 data->res = isl_union_map_add_map(data->res,
274 isl_map_realign(isl_map_copy(map), exp));
276 return isl_stat_ok;
279 /* Align the parameters of umap along those of model.
280 * The result has the parameters of model first, in the same order
281 * as they appear in model, followed by any remaining parameters of
282 * umap that do not appear in model.
284 __isl_give isl_union_map *isl_union_map_align_params(
285 __isl_take isl_union_map *umap, __isl_take isl_space *model)
287 struct isl_union_align data = { NULL, NULL };
288 isl_bool equal_params;
290 if (!umap || !model)
291 goto error;
293 equal_params = isl_space_has_equal_params(umap->dim, model);
294 if (equal_params < 0)
295 goto error;
296 if (equal_params) {
297 isl_space_free(model);
298 return umap;
301 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
302 if (!data.exp)
303 goto error;
305 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
306 umap->table.n);
307 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
308 &align_entry, &data) < 0)
309 goto error;
311 isl_reordering_free(data.exp);
312 isl_union_map_free(umap);
313 isl_space_free(model);
314 return data.res;
315 error:
316 isl_reordering_free(data.exp);
317 isl_union_map_free(umap);
318 isl_union_map_free(data.res);
319 isl_space_free(model);
320 return NULL;
323 __isl_give isl_union_set *isl_union_set_align_params(
324 __isl_take isl_union_set *uset, __isl_take isl_space *model)
326 return isl_union_map_align_params(uset, model);
329 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
330 __isl_take isl_union_map *umap2)
332 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
333 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
335 umap1 = isl_union_map_cow(umap1);
337 if (!umap1 || !umap2)
338 goto error;
340 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
341 goto error;
343 isl_union_map_free(umap2);
345 return umap1;
346 error:
347 isl_union_map_free(umap1);
348 isl_union_map_free(umap2);
349 return NULL;
352 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
353 __isl_take isl_union_set *uset2)
355 return isl_union_map_union(uset1, uset2);
358 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
360 if (!umap)
361 return NULL;
363 umap->ref++;
364 return umap;
367 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
369 return isl_union_map_copy(uset);
372 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
374 if (!umap)
375 return NULL;
377 if (--umap->ref > 0)
378 return NULL;
380 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
381 &free_umap_entry, NULL);
382 isl_hash_table_clear(&umap->table);
383 isl_space_free(umap->dim);
384 free(umap);
385 return NULL;
388 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
390 return isl_union_map_free(uset);
393 /* Do "umap" and "space" have the same parameters?
395 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
396 __isl_keep isl_space *space)
398 isl_space *umap_space;
400 umap_space = isl_union_map_peek_space(umap);
401 return isl_space_has_equal_params(umap_space, space);
404 /* Do "uset" and "space" have the same parameters?
406 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
407 __isl_keep isl_space *space)
409 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
412 /* Is the space of the map at "entry" equal to "space", ignoring parameters?
414 static isl_bool has_space_tuples(const void *entry, const void *val)
416 isl_map *map = (isl_map *)entry;
417 isl_space *space = (isl_space *) val;
419 return isl_map_has_space_tuples(map, space);
422 /* Find the entry in "umap" with space "space" (ignoring parameters),
423 * returning isl_hash_table_entry_none if no such entry appears in "umap" and
424 * NULL on error.
425 * If "reserve" is set, then an entry is created if it does
426 * not exist already. Since this modifies the hash table in-place,
427 * this means "umap" must have a single reference when "reserve" is set.
429 static struct isl_hash_table_entry *isl_union_map_find_entry(
430 __isl_keep isl_union_map *umap, __isl_keep isl_space *space,
431 int reserve)
433 uint32_t hash;
435 if (!umap || !space)
436 return NULL;
437 if (reserve && isl_union_map_check_single_reference(umap) < 0)
438 return NULL;
440 hash = isl_space_get_tuple_hash(space);
441 return isl_hash_table_find(isl_union_map_get_ctx(umap), &umap->table,
442 hash, &has_space_tuples, space, reserve);
445 /* Find the entry in "uset" with space "space" (ignoring parameters),
446 * returning isl_hash_table_entry_none if no such entry appears in "uset" and
447 * NULL on error.
448 * If "reserve" is set, then an entry is created if it does
449 * not exist already. In this case, a NULL return indicates an error.
451 struct isl_hash_table_entry *isl_union_set_find_entry(
452 __isl_keep isl_union_set *uset, __isl_keep isl_space *space,
453 int reserve)
455 return isl_union_map_find_entry(uset_to_umap(uset), space, reserve);
458 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
459 __isl_take isl_map *map)
461 struct isl_hash_table_entry *entry;
462 isl_bool aligned;
463 isl_space *space;
465 if (!map || !umap)
466 goto error;
468 if (isl_map_plain_is_empty(map)) {
469 isl_map_free(map);
470 return umap;
473 aligned = isl_map_space_has_equal_params(map, umap->dim);
474 if (aligned < 0)
475 goto error;
476 if (!aligned) {
477 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
478 map = isl_map_align_params(map, isl_union_map_get_space(umap));
481 umap = isl_union_map_cow(umap);
483 space = isl_map_peek_space(map);
484 entry = isl_union_map_find_entry(umap, space, 1);
485 if (!entry)
486 goto error;
488 if (!entry->data)
489 entry->data = map;
490 else {
491 entry->data = isl_map_union(entry->data, isl_map_copy(map));
492 if (!entry->data)
493 goto error;
494 isl_map_free(map);
497 return umap;
498 error:
499 isl_map_free(map);
500 isl_union_map_free(umap);
501 return NULL;
504 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
505 __isl_take isl_set *set)
507 return isl_union_map_add_map(uset, set_to_map(set));
510 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
512 isl_space *space;
513 isl_union_map *umap;
515 if (!map)
516 return NULL;
518 space = isl_map_get_space(map);
519 space = isl_space_params(space);
520 umap = isl_union_map_empty(space);
521 umap = isl_union_map_add_map(umap, map);
523 return umap;
526 /* This function performs the same operation as isl_union_map_from_map,
527 * but is considered as a function on an isl_map when exported.
529 __isl_give isl_union_map *isl_map_to_union_map(__isl_take isl_map *map)
531 return isl_union_map_from_map(map);
534 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
536 return isl_union_map_from_map(set_to_map(set));
539 /* This function performs the same operation as isl_union_set_from_set,
540 * but is considered as a function on an isl_set when exported.
542 __isl_give isl_union_set *isl_set_to_union_set(__isl_take isl_set *set)
544 return isl_union_set_from_set(set);
547 __isl_give isl_union_map *isl_union_map_from_basic_map(
548 __isl_take isl_basic_map *bmap)
550 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
553 __isl_give isl_union_set *isl_union_set_from_basic_set(
554 __isl_take isl_basic_set *bset)
556 return isl_union_map_from_basic_map(bset);
559 struct isl_union_map_foreach_data
561 isl_stat (*fn)(__isl_take isl_map *map, void *user);
562 void *user;
565 static isl_stat call_on_copy(void **entry, void *user)
567 isl_map *map = *entry;
568 struct isl_union_map_foreach_data *data;
569 data = (struct isl_union_map_foreach_data *)user;
571 return data->fn(isl_map_copy(map), data->user);
574 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
576 return umap ? umap->table.n : isl_size_error;
579 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
581 return uset ? uset->table.n : isl_size_error;
584 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
585 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
587 struct isl_union_map_foreach_data data = { fn, user };
589 if (!umap)
590 return isl_stat_error;
592 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
593 &call_on_copy, &data);
596 /* Internal data structure for isl_union_map_every_map.
598 * "test" is the user-specified callback function.
599 * "user" is the user-specified callback function argument.
601 * "failed" is initialized to 0 and set to 1 if "test" fails
602 * on any map.
604 struct isl_union_map_every_data {
605 isl_bool (*test)(__isl_keep isl_map *map, void *user);
606 void *user;
607 int failed;
610 /* Call data->test on "map".
611 * If this fails, then set data->failed and abort.
613 static isl_stat call_every(void **entry, void *user)
615 isl_map *map = *entry;
616 struct isl_union_map_every_data *data = user;
617 isl_bool r;
619 r = data->test(map, data->user);
620 if (r < 0)
621 return isl_stat_error;
622 if (r)
623 return isl_stat_ok;
624 data->failed = 1;
625 return isl_stat_error;
628 /* Does "test" succeed on every map in "umap"?
630 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
631 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
633 struct isl_union_map_every_data data = { test, user, 0 };
634 isl_stat r;
636 if (!umap)
637 return isl_bool_error;
639 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
640 &call_every, &data);
641 if (r >= 0)
642 return isl_bool_true;
643 if (data.failed)
644 return isl_bool_false;
645 return isl_bool_error;
648 /* Add "map" to "list".
650 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
652 isl_map_list **list = user;
654 *list = isl_map_list_add(*list, map);
656 if (!*list)
657 return isl_stat_error;
658 return isl_stat_ok;
661 /* Return the maps in "umap" as a list.
663 * First construct a list of the appropriate size and then add all the
664 * elements.
666 __isl_give isl_map_list *isl_union_map_get_map_list(
667 __isl_keep isl_union_map *umap)
669 isl_size n_maps;
670 isl_ctx *ctx;
671 isl_map_list *list;
673 n_maps = isl_union_map_n_map(umap);
674 if (n_maps < 0)
675 return NULL;
676 ctx = isl_union_map_get_ctx(umap);
677 list = isl_map_list_alloc(ctx, n_maps);
679 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
680 list = isl_map_list_free(list);
682 return list;
685 /* Return the sets in "uset" as a list.
687 __isl_give isl_set_list *isl_union_set_get_set_list(
688 __isl_keep isl_union_set *uset)
690 return set_list_from_map_list(
691 isl_union_map_get_map_list(uset_to_umap(uset)));
694 /* Can "umap" be converted to an isl_map?
695 * That is, does it contain elements in exactly one space?
697 isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
699 isl_size n;
701 n = isl_union_map_n_map(umap);
702 if (n < 0)
703 return isl_bool_error;
704 return isl_bool_ok(n == 1);
707 /* Can "uset" be converted to an isl_set?
708 * That is, does it contain elements in exactly one space?
710 isl_bool isl_union_set_isa_set(__isl_keep isl_union_set *uset)
712 return isl_union_map_isa_map(uset_to_umap(uset));
715 static isl_stat copy_map(void **entry, void *user)
717 isl_map *map = *entry;
718 isl_map **map_p = user;
720 *map_p = isl_map_copy(map);
722 return isl_stat_error;
725 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
727 isl_bool is_map;
728 isl_ctx *ctx;
729 isl_map *map = NULL;
731 is_map = isl_union_map_isa_map(umap);
732 if (is_map < 0)
733 goto error;
734 ctx = isl_union_map_get_ctx(umap);
735 if (!is_map)
736 isl_die(ctx, isl_error_invalid,
737 "union map needs to contain elements in exactly "
738 "one space", goto error);
740 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
742 isl_union_map_free(umap);
744 return map;
745 error:
746 isl_union_map_free(umap);
747 return NULL;
750 /* This function performs the same operation as isl_map_from_union_map,
751 * but is considered as a function on an isl_union_map when exported.
753 __isl_give isl_map *isl_union_map_as_map(__isl_take isl_union_map *umap)
755 return isl_map_from_union_map(umap);
758 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
760 return isl_map_from_union_map(uset);
763 /* This function performs the same operation as isl_set_from_union_set,
764 * but is considered as a function on an isl_union_set when exported.
766 __isl_give isl_set *isl_union_set_as_set(__isl_take isl_union_set *uset)
768 return isl_set_from_union_set(uset);
771 /* Extract the map in "umap" that lives in the given space (ignoring
772 * parameters).
774 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
775 __isl_take isl_space *space)
777 struct isl_hash_table_entry *entry;
779 entry = isl_union_map_find_entry(umap, space, 0);
780 if (!entry)
781 goto error;
782 if (entry == isl_hash_table_entry_none)
783 return isl_map_empty(space);
784 isl_space_free(space);
785 return isl_map_copy(entry->data);
786 error:
787 isl_space_free(space);
788 return NULL;
791 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
792 __isl_take isl_space *space)
794 return set_from_map(isl_union_map_extract_map(uset, space));
797 /* Check if umap contains a map in the given space (ignoring parameters).
799 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
800 __isl_keep isl_space *space)
802 struct isl_hash_table_entry *entry;
804 space = isl_space_drop_all_params(isl_space_copy(space));
805 space = isl_space_align_params(space, isl_union_map_get_space(umap));
806 entry = isl_union_map_find_entry(umap, space, 0);
807 isl_space_free(space);
808 if (!entry)
809 return isl_bool_error;
810 return isl_bool_ok(entry != isl_hash_table_entry_none);
813 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
814 __isl_keep isl_space *space)
816 return isl_union_map_contains(uset, space);
819 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
820 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
822 return isl_union_map_foreach_map(uset,
823 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
826 /* Internal data structure for isl_union_set_every_set.
828 * "test" is the user-specified callback function.
829 * "user" is the user-specified callback function argument.
831 struct isl_test_set_from_map_data {
832 isl_bool (*test)(__isl_keep isl_set *set, void *user);
833 void *user;
836 /* Call data->test on "map", which is part of an isl_union_set and
837 * therefore known to be an isl_set.
839 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
841 struct isl_test_set_from_map_data *data = user;
843 return data->test(set_from_map(map), data->user);
846 /* Does "test" succeed on every set in "uset"?
848 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
849 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
851 struct isl_test_set_from_map_data data = { test, user };
853 return isl_union_map_every_map(uset_to_umap(uset),
854 &test_set_from_map, &data);
857 struct isl_union_set_foreach_point_data {
858 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
859 void *user;
862 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
864 struct isl_union_set_foreach_point_data *data = user;
865 isl_stat r;
867 r = isl_set_foreach_point(set, data->fn, data->user);
868 isl_set_free(set);
870 return r;
873 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
874 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
876 struct isl_union_set_foreach_point_data data = { fn, user };
877 return isl_union_set_foreach_set(uset, &foreach_point, &data);
880 /* Data structure that specifies how gen_bin_op should
881 * construct results from the inputs.
883 * If "subtract" is set, then a map in the first input is copied to the result
884 * if there is no corresponding map in the second input.
885 * Otherwise, a map in the first input with no corresponding map
886 * in the second input is ignored.
887 * If "filter" is not NULL, then it specifies which maps in the first
888 * input may have a matching map in the second input.
889 * In particular, it makes sure that "match_space" can be called
890 * on the space of the map.
891 * "match_space" specifies how to transform the space of a map
892 * in the first input to the space of the corresponding map
893 * in the second input.
894 * "fn_map" specifies how the matching maps, one from each input,
895 * should be combined to form a map in the result.
897 struct isl_bin_op_control {
898 int subtract;
899 isl_bool (*filter)(__isl_keep isl_map *map);
900 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
901 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
902 __isl_take isl_map *map2);
905 /* Internal data structure for gen_bin_op.
906 * "control" specifies how the maps in the result should be constructed.
907 * "umap2" is a pointer to the second argument.
908 * "res" collects the results.
910 struct isl_union_map_gen_bin_data {
911 struct isl_bin_op_control *control;
912 isl_union_map *umap2;
913 isl_union_map *res;
916 /* Add a copy of "map" to "res" and return the result.
918 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
919 __isl_keep isl_map *map)
921 return isl_union_map_add_map(res, isl_map_copy(map));
924 /* Combine "map1" and "map2", add the result to "res" and return the result.
925 * Check whether the result is empty before adding it to "res".
927 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
928 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
929 struct isl_union_map_gen_bin_data *data)
931 isl_bool empty;
932 isl_map *map;
934 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
935 empty = isl_map_is_empty(map);
936 if (empty < 0 || empty) {
937 isl_map_free(map);
938 if (empty < 0)
939 return isl_union_map_free(res);
940 return res;
942 return isl_union_map_add_map(res, map);
945 /* Dummy match_space function that simply returns the input space.
947 static __isl_give isl_space *identity(__isl_take isl_space *space)
949 return space;
952 /* Look for the map in data->umap2 that corresponds to "map", if any.
953 * Return (isl_bool_true, matching map) if there is one,
954 * (isl_bool_false, NULL) if there is no matching map and
955 * (isl_bool_error, NULL) on error.
957 * If not NULL, then data->control->filter specifies whether "map"
958 * can have any matching map. If so,
959 * data->control->match_space specifies which map in data->umap2
960 * corresponds to "map".
962 static __isl_keep isl_maybe_isl_map bin_try_get_match(
963 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
965 struct isl_hash_table_entry *entry2;
966 isl_space *space;
967 isl_maybe_isl_map res = { isl_bool_error, NULL };
969 if (data->control->filter) {
970 res.valid = data->control->filter(map);
971 if (res.valid < 0 || !res.valid)
972 return res;
973 res.valid = isl_bool_error;
976 space = isl_map_get_space(map);
977 if (data->control->match_space != &identity)
978 space = data->control->match_space(space);
979 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
980 isl_space_free(space);
981 if (entry2)
982 res.valid = isl_bool_ok(entry2 != isl_hash_table_entry_none);
983 if (res.valid >= 0 && res.valid)
984 res.value = entry2->data;
986 return res;
989 /* isl_hash_table_foreach callback for gen_bin_op.
990 * Look for the map in data->umap2 that corresponds
991 * to the map that "entry" points to, apply the binary operation and
992 * add the result to data->res.
994 * If no corresponding map can be found, then the effect depends
995 * on data->control->subtract. If it is set, then the current map
996 * is added directly to the result. Otherwise, it is ignored.
998 static isl_stat gen_bin_entry(void **entry, void *user)
1000 struct isl_union_map_gen_bin_data *data = user;
1001 isl_map *map = *entry;
1002 isl_maybe_isl_map m;
1004 m = bin_try_get_match(data, map);
1005 if (m.valid < 0)
1006 return isl_stat_error;
1007 if (!m.valid && !data->control->subtract)
1008 return isl_stat_ok;
1010 if (!m.valid)
1011 data->res = bin_add_map(data->res, map);
1012 else
1013 data->res = bin_add_pair(data->res, map, m.value, data);
1014 if (!data->res)
1015 return isl_stat_error;
1017 return isl_stat_ok;
1020 /* Apply a binary operation to "umap1" and "umap2" based on "control".
1021 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
1022 * in gen_bin_entry.
1024 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
1025 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
1027 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
1029 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1030 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1032 if (!umap1 || !umap2)
1033 goto error;
1035 data.umap2 = umap2;
1036 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1037 umap1->table.n);
1038 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1039 &gen_bin_entry, &data) < 0)
1040 goto error;
1042 isl_union_map_free(umap1);
1043 isl_union_map_free(umap2);
1044 return data.res;
1045 error:
1046 isl_union_map_free(umap1);
1047 isl_union_map_free(umap2);
1048 isl_union_map_free(data.res);
1049 return NULL;
1052 __isl_give isl_union_map *isl_union_map_subtract(
1053 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1055 struct isl_bin_op_control control = {
1056 .subtract = 1,
1057 .match_space = &identity,
1058 .fn_map = &isl_map_subtract,
1061 return gen_bin_op(umap1, umap2, &control);
1064 __isl_give isl_union_set *isl_union_set_subtract(
1065 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1067 return isl_union_map_subtract(uset1, uset2);
1070 struct isl_union_map_gen_bin_set_data {
1071 isl_set *set;
1072 isl_union_map *res;
1075 static isl_stat intersect_params_entry(void **entry, void *user)
1077 struct isl_union_map_gen_bin_set_data *data = user;
1078 isl_map *map = *entry;
1079 int empty;
1081 map = isl_map_copy(map);
1082 map = isl_map_intersect_params(map, isl_set_copy(data->set));
1084 empty = isl_map_is_empty(map);
1085 if (empty < 0) {
1086 isl_map_free(map);
1087 return isl_stat_error;
1090 data->res = isl_union_map_add_map(data->res, map);
1092 return isl_stat_ok;
1095 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
1096 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
1098 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
1100 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1101 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1103 if (!umap || !set)
1104 goto error;
1106 data.set = set;
1107 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1108 umap->table.n);
1109 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1110 fn, &data) < 0)
1111 goto error;
1113 isl_union_map_free(umap);
1114 isl_set_free(set);
1115 return data.res;
1116 error:
1117 isl_union_map_free(umap);
1118 isl_set_free(set);
1119 isl_union_map_free(data.res);
1120 return NULL;
1123 /* Intersect "umap" with the parameter domain "set".
1125 * If "set" does not have any constraints, then we can return immediately.
1127 __isl_give isl_union_map *isl_union_map_intersect_params(
1128 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1130 int is_universe;
1132 is_universe = isl_set_plain_is_universe(set);
1133 if (is_universe < 0)
1134 goto error;
1135 if (is_universe) {
1136 isl_set_free(set);
1137 return umap;
1140 return gen_bin_set_op(umap, set, &intersect_params_entry);
1141 error:
1142 isl_union_map_free(umap);
1143 isl_set_free(set);
1144 return NULL;
1147 __isl_give isl_union_set *isl_union_set_intersect_params(
1148 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1150 return isl_union_map_intersect_params(uset, set);
1153 static __isl_give isl_union_map *union_map_intersect_params(
1154 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1156 return isl_union_map_intersect_params(umap,
1157 isl_set_from_union_set(uset));
1160 static __isl_give isl_union_map *union_map_gist_params(
1161 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1163 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1166 struct isl_union_map_match_bin_data {
1167 isl_union_map *umap2;
1168 isl_union_map *res;
1169 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1172 static isl_stat match_bin_entry(void **entry, void *user)
1174 struct isl_union_map_match_bin_data *data = user;
1175 struct isl_hash_table_entry *entry2;
1176 isl_space *space;
1177 isl_map *map = *entry;
1178 int empty;
1180 space = isl_map_peek_space(map);
1181 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
1182 if (!entry2)
1183 return isl_stat_error;
1184 if (entry2 == isl_hash_table_entry_none)
1185 return isl_stat_ok;
1187 map = isl_map_copy(map);
1188 map = data->fn(map, isl_map_copy(entry2->data));
1190 empty = isl_map_is_empty(map);
1191 if (empty < 0) {
1192 isl_map_free(map);
1193 return isl_stat_error;
1195 if (empty) {
1196 isl_map_free(map);
1197 return isl_stat_ok;
1200 data->res = isl_union_map_add_map(data->res, map);
1202 return isl_stat_ok;
1205 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1206 __isl_take isl_union_map *umap2,
1207 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1209 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1211 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1212 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1214 if (!umap1 || !umap2)
1215 goto error;
1217 data.umap2 = umap2;
1218 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1219 umap1->table.n);
1220 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1221 &match_bin_entry, &data) < 0)
1222 goto error;
1224 isl_union_map_free(umap1);
1225 isl_union_map_free(umap2);
1226 return data.res;
1227 error:
1228 isl_union_map_free(umap1);
1229 isl_union_map_free(umap2);
1230 isl_union_map_free(data.res);
1231 return NULL;
1234 __isl_give isl_union_map *isl_union_map_intersect(
1235 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1237 return match_bin_op(umap1, umap2, &isl_map_intersect);
1240 /* Compute the intersection of the two union_sets.
1241 * As a special case, if exactly one of the two union_sets
1242 * is a parameter domain, then intersect the parameter domain
1243 * of the other one with this set.
1245 __isl_give isl_union_set *isl_union_set_intersect(
1246 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1248 int p1, p2;
1250 p1 = isl_union_set_is_params(uset1);
1251 p2 = isl_union_set_is_params(uset2);
1252 if (p1 < 0 || p2 < 0)
1253 goto error;
1254 if (!p1 && p2)
1255 return union_map_intersect_params(uset1, uset2);
1256 if (p1 && !p2)
1257 return union_map_intersect_params(uset2, uset1);
1258 return isl_union_map_intersect(uset1, uset2);
1259 error:
1260 isl_union_set_free(uset1);
1261 isl_union_set_free(uset2);
1262 return NULL;
1265 static isl_stat gist_params_entry(void **entry, void *user)
1267 struct isl_union_map_gen_bin_set_data *data = user;
1268 isl_map *map = *entry;
1269 int empty;
1271 map = isl_map_copy(map);
1272 map = isl_map_gist_params(map, isl_set_copy(data->set));
1274 empty = isl_map_is_empty(map);
1275 if (empty < 0) {
1276 isl_map_free(map);
1277 return isl_stat_error;
1280 data->res = isl_union_map_add_map(data->res, map);
1282 return isl_stat_ok;
1285 __isl_give isl_union_map *isl_union_map_gist_params(
1286 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1288 return gen_bin_set_op(umap, set, &gist_params_entry);
1291 __isl_give isl_union_set *isl_union_set_gist_params(
1292 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1294 return isl_union_map_gist_params(uset, set);
1297 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1298 __isl_take isl_union_map *context)
1300 return match_bin_op(umap, context, &isl_map_gist);
1303 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1304 __isl_take isl_union_set *context)
1306 if (isl_union_set_is_params(context))
1307 return union_map_gist_params(uset, context);
1308 return isl_union_map_gist(uset, context);
1311 /* For each map in "umap", remove the constraints in the corresponding map
1312 * of "context".
1313 * Each map in "context" is assumed to consist of a single disjunct and
1314 * to have explicit representations for all local variables.
1316 __isl_give isl_union_map *isl_union_map_plain_gist(
1317 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1319 return match_bin_op(umap, context, &isl_map_plain_gist);
1322 /* For each set in "uset", remove the constraints in the corresponding set
1323 * of "context".
1324 * Each set in "context" is assumed to consist of a single disjunct and
1325 * to have explicit representations for all local variables.
1327 __isl_give isl_union_set *isl_union_set_plain_gist(
1328 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1330 return isl_union_map_plain_gist(uset, context);
1333 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1334 __isl_take isl_map *set2)
1336 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1339 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1340 __isl_take isl_map *set2)
1342 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1345 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1346 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1348 return match_bin_op(uset1, uset2, &lex_lt_set);
1351 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1352 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1354 return match_bin_op(uset1, uset2, &lex_le_set);
1357 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1358 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1360 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1363 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1364 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1366 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1369 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1370 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1372 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1375 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1376 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1378 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1381 /* Intersect the domain of "umap" with "uset".
1383 static __isl_give isl_union_map *union_map_intersect_domain(
1384 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1386 struct isl_bin_op_control control = {
1387 .match_space = &isl_space_domain,
1388 .fn_map = &isl_map_intersect_domain,
1391 return gen_bin_op(umap, uset, &control);
1394 /* Intersect the domain of "umap" with "uset".
1395 * If "uset" is a parameters domain, then intersect the parameter
1396 * domain of "umap" with this set.
1398 __isl_give isl_union_map *isl_union_map_intersect_domain_union_set(
1399 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1401 if (isl_union_set_is_params(uset))
1402 return union_map_intersect_params(umap, uset);
1403 else
1404 return union_map_intersect_domain(umap, uset);
1407 /* This is an alternative name for the function above.
1409 __isl_give isl_union_map *isl_union_map_intersect_domain(
1410 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1412 return isl_union_map_intersect_domain_union_set(umap, uset);
1415 /* Remove the elements of "uset" from the domain of "umap".
1417 __isl_give isl_union_map *isl_union_map_subtract_domain(
1418 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1420 struct isl_bin_op_control control = {
1421 .subtract = 1,
1422 .match_space = &isl_space_domain,
1423 .fn_map = &isl_map_subtract_domain,
1426 return gen_bin_op(umap, dom, &control);
1429 /* Remove the elements of "uset" from the range of "umap".
1431 __isl_give isl_union_map *isl_union_map_subtract_range(
1432 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1434 struct isl_bin_op_control control = {
1435 .subtract = 1,
1436 .match_space = &isl_space_range,
1437 .fn_map = &isl_map_subtract_range,
1440 return gen_bin_op(umap, dom, &control);
1443 /* Compute the gist of "umap" with respect to the domain "uset".
1445 static __isl_give isl_union_map *union_map_gist_domain(
1446 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1448 struct isl_bin_op_control control = {
1449 .match_space = &isl_space_domain,
1450 .fn_map = &isl_map_gist_domain,
1453 return gen_bin_op(umap, uset, &control);
1456 /* Compute the gist of "umap" with respect to the domain "uset".
1457 * If "uset" is a parameters domain, then compute the gist
1458 * with respect to this parameter domain.
1460 __isl_give isl_union_map *isl_union_map_gist_domain(
1461 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1463 if (isl_union_set_is_params(uset))
1464 return union_map_gist_params(umap, uset);
1465 else
1466 return union_map_gist_domain(umap, uset);
1469 /* Compute the gist of "umap" with respect to the range "uset".
1471 __isl_give isl_union_map *isl_union_map_gist_range(
1472 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1474 struct isl_bin_op_control control = {
1475 .match_space = &isl_space_range,
1476 .fn_map = &isl_map_gist_range,
1479 return gen_bin_op(umap, uset, &control);
1482 __isl_give isl_union_map *isl_union_map_intersect_range_union_set(
1483 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1485 struct isl_bin_op_control control = {
1486 .match_space = &isl_space_range,
1487 .fn_map = &isl_map_intersect_range,
1490 return gen_bin_op(umap, uset, &control);
1493 /* This is an alternative name for the function above.
1495 __isl_give isl_union_map *isl_union_map_intersect_range(
1496 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1498 return isl_union_map_intersect_range_union_set(umap, uset);
1501 /* Intersect each map in "umap" in a space [A -> B] -> C
1502 * with the corresponding map in "factor" in the space A -> C and
1503 * collect the results.
1505 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_domain(
1506 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1508 struct isl_bin_op_control control = {
1509 .filter = &isl_map_domain_is_wrapping,
1510 .match_space = &isl_space_domain_factor_domain,
1511 .fn_map = &isl_map_intersect_domain_factor_domain,
1514 return gen_bin_op(umap, factor, &control);
1517 /* Intersect each map in "umap" in a space [A -> B] -> C
1518 * with the corresponding map in "factor" in the space B -> C and
1519 * collect the results.
1521 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_range(
1522 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1524 struct isl_bin_op_control control = {
1525 .filter = &isl_map_domain_is_wrapping,
1526 .match_space = &isl_space_domain_factor_range,
1527 .fn_map = &isl_map_intersect_domain_factor_range,
1530 return gen_bin_op(umap, factor, &control);
1533 /* Intersect each map in "umap" in a space A -> [B -> C]
1534 * with the corresponding map in "factor" in the space A -> B and
1535 * collect the results.
1537 __isl_give isl_union_map *isl_union_map_intersect_range_factor_domain(
1538 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1540 struct isl_bin_op_control control = {
1541 .filter = &isl_map_range_is_wrapping,
1542 .match_space = &isl_space_range_factor_domain,
1543 .fn_map = &isl_map_intersect_range_factor_domain,
1546 return gen_bin_op(umap, factor, &control);
1549 /* Intersect each map in "umap" in a space A -> [B -> C]
1550 * with the corresponding map in "factor" in the space A -> C and
1551 * collect the results.
1553 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1554 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1556 struct isl_bin_op_control control = {
1557 .filter = &isl_map_range_is_wrapping,
1558 .match_space = &isl_space_range_factor_range,
1559 .fn_map = &isl_map_intersect_range_factor_range,
1562 return gen_bin_op(umap, factor, &control);
1565 struct isl_union_map_bin_data {
1566 isl_union_map *umap2;
1567 isl_union_map *res;
1568 isl_map *map;
1569 isl_stat (*fn)(void **entry, void *user);
1572 static isl_stat apply_range_entry(void **entry, void *user)
1574 struct isl_union_map_bin_data *data = user;
1575 isl_map *map2 = *entry;
1576 isl_bool empty, match;
1578 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1579 map2, isl_dim_in);
1580 if (match < 0)
1581 return isl_stat_error;
1582 if (!match)
1583 return isl_stat_ok;
1585 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1587 empty = isl_map_is_empty(map2);
1588 if (empty < 0) {
1589 isl_map_free(map2);
1590 return isl_stat_error;
1592 if (empty) {
1593 isl_map_free(map2);
1594 return isl_stat_ok;
1597 data->res = isl_union_map_add_map(data->res, map2);
1599 return isl_stat_ok;
1602 static isl_stat bin_entry(void **entry, void *user)
1604 struct isl_union_map_bin_data *data = user;
1605 isl_map *map = *entry;
1607 data->map = map;
1608 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1609 data->fn, data) < 0)
1610 return isl_stat_error;
1612 return isl_stat_ok;
1615 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1616 __isl_take isl_union_map *umap2,
1617 isl_stat (*fn)(void **entry, void *user))
1619 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1621 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1622 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1624 if (!umap1 || !umap2)
1625 goto error;
1627 data.umap2 = umap2;
1628 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1629 umap1->table.n);
1630 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1631 &bin_entry, &data) < 0)
1632 goto error;
1634 isl_union_map_free(umap1);
1635 isl_union_map_free(umap2);
1636 return data.res;
1637 error:
1638 isl_union_map_free(umap1);
1639 isl_union_map_free(umap2);
1640 isl_union_map_free(data.res);
1641 return NULL;
1644 /* Intersect each map in "umap" in a space [A -> B] -> C
1645 * with the corresponding set in "domain" in the space A and
1646 * collect the results.
1648 __isl_give isl_union_map *
1649 isl_union_map_intersect_domain_wrapped_domain_union_set(
1650 __isl_take isl_union_map *umap, __isl_take isl_union_set *domain)
1652 struct isl_bin_op_control control = {
1653 .filter = &isl_map_domain_is_wrapping,
1654 .match_space = &isl_space_domain_wrapped_domain,
1655 .fn_map = &isl_map_intersect_domain_wrapped_domain,
1658 return gen_bin_op(umap, domain, &control);
1661 /* Intersect each map in "umap" in a space A -> [B -> C]
1662 * with the corresponding set in "domain" in the space B and
1663 * collect the results.
1665 __isl_give isl_union_map *
1666 isl_union_map_intersect_range_wrapped_domain_union_set(
1667 __isl_take isl_union_map *umap, __isl_take isl_union_set *domain)
1669 struct isl_bin_op_control control = {
1670 .filter = &isl_map_range_is_wrapping,
1671 .match_space = &isl_space_range_wrapped_domain,
1672 .fn_map = &isl_map_intersect_range_wrapped_domain,
1675 return gen_bin_op(umap, domain, &control);
1678 __isl_give isl_union_map *isl_union_map_apply_range(
1679 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1681 return bin_op(umap1, umap2, &apply_range_entry);
1684 __isl_give isl_union_map *isl_union_map_apply_domain(
1685 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1687 umap1 = isl_union_map_reverse(umap1);
1688 umap1 = isl_union_map_apply_range(umap1, umap2);
1689 return isl_union_map_reverse(umap1);
1692 __isl_give isl_union_set *isl_union_set_apply(
1693 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1695 return isl_union_map_apply_range(uset, umap);
1698 static isl_stat map_lex_lt_entry(void **entry, void *user)
1700 struct isl_union_map_bin_data *data = user;
1701 isl_map *map2 = *entry;
1702 isl_bool match;
1704 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1705 map2, isl_dim_out);
1706 if (match < 0)
1707 return isl_stat_error;
1708 if (!match)
1709 return isl_stat_ok;
1711 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1713 data->res = isl_union_map_add_map(data->res, map2);
1715 return isl_stat_ok;
1718 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1719 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1721 return bin_op(umap1, umap2, &map_lex_lt_entry);
1724 static isl_stat map_lex_le_entry(void **entry, void *user)
1726 struct isl_union_map_bin_data *data = user;
1727 isl_map *map2 = *entry;
1728 isl_bool match;
1730 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1731 map2, isl_dim_out);
1732 if (match < 0)
1733 return isl_stat_error;
1734 if (!match)
1735 return isl_stat_ok;
1737 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1739 data->res = isl_union_map_add_map(data->res, map2);
1741 return isl_stat_ok;
1744 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1745 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1747 return bin_op(umap1, umap2, &map_lex_le_entry);
1750 static isl_stat product_entry(void **entry, void *user)
1752 struct isl_union_map_bin_data *data = user;
1753 isl_map *map2 = *entry;
1755 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1757 data->res = isl_union_map_add_map(data->res, map2);
1759 return isl_stat_ok;
1762 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1763 __isl_take isl_union_map *umap2)
1765 return bin_op(umap1, umap2, &product_entry);
1768 static isl_stat set_product_entry(void **entry, void *user)
1770 struct isl_union_map_bin_data *data = user;
1771 isl_set *set2 = *entry;
1773 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1775 data->res = isl_union_set_add_set(data->res, set2);
1777 return isl_stat_ok;
1780 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1781 __isl_take isl_union_set *uset2)
1783 return bin_op(uset1, uset2, &set_product_entry);
1786 static isl_stat domain_product_entry(void **entry, void *user)
1788 struct isl_union_map_bin_data *data = user;
1789 isl_map *map2 = *entry;
1790 isl_bool match;
1792 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1793 map2, isl_dim_out);
1794 if (match < 0)
1795 return isl_stat_error;
1796 if (!match)
1797 return isl_stat_ok;
1799 map2 = isl_map_domain_product(isl_map_copy(data->map),
1800 isl_map_copy(map2));
1802 data->res = isl_union_map_add_map(data->res, map2);
1804 return isl_stat_ok;
1807 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1809 __isl_give isl_union_map *isl_union_map_domain_product(
1810 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1812 return bin_op(umap1, umap2, &domain_product_entry);
1815 static isl_stat range_product_entry(void **entry, void *user)
1817 struct isl_union_map_bin_data *data = user;
1818 isl_map *map2 = *entry;
1819 isl_bool match;
1821 match = isl_map_tuple_is_equal(data->map, isl_dim_in, map2, isl_dim_in);
1822 if (match < 0)
1823 return isl_stat_error;
1824 if (!match)
1825 return isl_stat_ok;
1827 map2 = isl_map_range_product(isl_map_copy(data->map),
1828 isl_map_copy(map2));
1830 data->res = isl_union_map_add_map(data->res, map2);
1832 return isl_stat_ok;
1835 __isl_give isl_union_map *isl_union_map_range_product(
1836 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1838 return bin_op(umap1, umap2, &range_product_entry);
1841 /* If data->map A -> B and "map2" C -> D have the same range space,
1842 * then add (A, C) -> (B * D) to data->res.
1844 static isl_stat flat_domain_product_entry(void **entry, void *user)
1846 struct isl_union_map_bin_data *data = user;
1847 isl_map *map2 = *entry;
1848 isl_bool match;
1850 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1851 map2, isl_dim_out);
1852 if (match < 0)
1853 return isl_stat_error;
1854 if (!match)
1855 return isl_stat_ok;
1857 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1858 isl_map_copy(map2));
1860 data->res = isl_union_map_add_map(data->res, map2);
1862 return isl_stat_ok;
1865 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1867 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1868 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1870 return bin_op(umap1, umap2, &flat_domain_product_entry);
1873 static isl_stat flat_range_product_entry(void **entry, void *user)
1875 struct isl_union_map_bin_data *data = user;
1876 isl_map *map2 = *entry;
1877 isl_bool match;
1879 match = isl_map_tuple_is_equal(data->map, isl_dim_in, map2, isl_dim_in);
1880 if (match < 0)
1881 return isl_stat_error;
1882 if (!match)
1883 return isl_stat_ok;
1885 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1886 isl_map_copy(map2));
1888 data->res = isl_union_map_add_map(data->res, map2);
1890 return isl_stat_ok;
1893 __isl_give isl_union_map *isl_union_map_flat_range_product(
1894 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1896 return bin_op(umap1, umap2, &flat_range_product_entry);
1899 /* Data structure that specifies how un_op should modify
1900 * the maps in the union map.
1902 * If "inplace" is set, then the maps in the input union map
1903 * are modified in place. This means that "fn_map" should not
1904 * change the meaning of the map or that the union map only
1905 * has a single reference.
1906 * If "total" is set, then all maps need to be modified and
1907 * the results need to live in the same space.
1908 * Otherwise, a new union map is constructed to store the results.
1909 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1910 * are taken into account. "filter_user" is passed as the second argument
1911 * to "filter". No filter can be set if "inplace" or
1912 * "total" is set.
1913 * At most one of "fn_map" or "fn_map2" can be set, specifying
1914 * how the maps (selected by "filter") should be transformed.
1915 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1917 struct isl_un_op_control {
1918 int inplace;
1919 int total;
1920 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1921 void *filter_user;
1922 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1923 __isl_give isl_map *(*fn_map2)(__isl_take isl_map *map, void *user);
1924 void *fn_map2_user;
1927 /* Data structure for wrapping the data for un_op_filter_drop_user.
1928 * "filter" is the function that is being wrapped.
1930 struct isl_un_op_drop_user_data {
1931 isl_bool (*filter)(__isl_keep isl_map *map);
1934 /* Wrapper for isl_un_op_control filters that do not require
1935 * a second argument.
1936 * Simply call data->filter without the second argument.
1938 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1940 struct isl_un_op_drop_user_data *data = user;
1941 return data->filter(map);
1944 /* Internal data structure for "un_op".
1945 * "control" specifies how the maps in the union map should be modified.
1946 * "res" collects the results.
1948 struct isl_union_map_un_data {
1949 struct isl_un_op_control *control;
1950 isl_union_map *res;
1953 /* isl_hash_table_foreach callback for un_op.
1954 * Handle the map that "entry" points to.
1956 * If control->filter is set, then check if this map satisfies the filter.
1957 * If so (or if control->filter is not set), modify the map
1958 * by calling control->fn_map or control->fn_map2 (if set) and
1959 * either add the result to data->res or
1960 * replace the original entry by the result (if control->inplace is set).
1962 static isl_stat un_entry(void **entry, void *user)
1964 struct isl_union_map_un_data *data = user;
1965 struct isl_un_op_control *control = data->control;
1966 isl_map *map = *entry;
1968 if (control->filter) {
1969 isl_bool ok;
1971 ok = control->filter(map, control->filter_user);
1972 if (ok < 0)
1973 return isl_stat_error;
1974 if (!ok)
1975 return isl_stat_ok;
1978 map = isl_map_copy(map);
1979 if (control->fn_map2 != NULL)
1980 map = control->fn_map2(map, control->fn_map2_user);
1981 else if (control->fn_map != NULL)
1982 map = control->fn_map(map);
1983 if (!map)
1984 return isl_stat_error;
1985 if (control->inplace) {
1986 isl_map_free(*entry);
1987 *entry = map;
1988 } else {
1989 data->res = isl_union_map_add_map(data->res, map);
1990 if (!data->res)
1991 return isl_stat_error;
1994 return isl_stat_ok;
1997 /* Modify the maps in "umap" based on "control".
1998 * If control->inplace is set, then modify the maps in "umap" in-place.
1999 * Otherwise, create a new union map to hold the results.
2000 * If control->total is set, then perform an inplace computation
2001 * if "umap" is only referenced once. Otherwise, create a new union map
2002 * to store the results.
2004 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
2005 struct isl_un_op_control *control)
2007 struct isl_union_map_un_data data = { control };
2009 if (!umap)
2010 return NULL;
2011 if (!!control->fn_map && !!control->fn_map2)
2012 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
2013 "at most one mapping function can be specified",
2014 return isl_union_map_free(umap));
2015 if ((control->inplace || control->total) && control->filter)
2016 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
2017 "inplace/total modification cannot be filtered",
2018 return isl_union_map_free(umap));
2020 if (control->total && umap->ref == 1)
2021 control->inplace = 1;
2022 if (control->inplace) {
2023 data.res = umap;
2024 } else {
2025 isl_space *space;
2027 space = isl_union_map_get_space(umap);
2028 data.res = isl_union_map_alloc(space, umap->table.n);
2030 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
2031 &umap->table, &un_entry, &data) < 0)
2032 data.res = isl_union_map_free(data.res);
2034 if (control->inplace)
2035 return data.res;
2036 isl_union_map_free(umap);
2037 return data.res;
2040 __isl_give isl_union_map *isl_union_map_from_range(
2041 __isl_take isl_union_set *uset)
2043 struct isl_un_op_control control = {
2044 .fn_map = &isl_map_from_range,
2046 return un_op(uset, &control);
2049 __isl_give isl_union_map *isl_union_map_from_domain(
2050 __isl_take isl_union_set *uset)
2052 return isl_union_map_reverse(isl_union_map_from_range(uset));
2055 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
2056 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
2058 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
2059 isl_union_map_from_range(range));
2062 /* Modify the maps in "umap" by applying "fn" on them.
2063 * "fn" should apply to all maps in "umap" and should not modify the space.
2065 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
2066 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2068 struct isl_un_op_control control = {
2069 .total = 1,
2070 .fn_map = fn,
2073 return un_op(umap, &control);
2076 /* Compute the affine hull of "map" and return the result as an isl_map.
2078 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
2080 return isl_map_from_basic_map(isl_map_affine_hull(map));
2083 __isl_give isl_union_map *isl_union_map_affine_hull(
2084 __isl_take isl_union_map *umap)
2086 return total(umap, &isl_map_affine_hull_map);
2089 __isl_give isl_union_set *isl_union_set_affine_hull(
2090 __isl_take isl_union_set *uset)
2092 return isl_union_map_affine_hull(uset);
2095 /* Wrapper around isl_set_combined_lineality_space
2096 * that returns the combined lineality space in the form of an isl_set
2097 * instead of an isl_basic_set.
2099 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
2101 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
2104 /* For each set in "uset", compute the (linear) hull
2105 * of the lineality spaces of its basic sets and
2106 * collect and return the results.
2108 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
2109 __isl_take isl_union_set *uset)
2111 struct isl_un_op_control control = {
2112 .fn_map = &combined_lineality_space,
2114 return un_op(uset, &control);
2117 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
2119 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
2121 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
2124 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
2125 __isl_take isl_union_map *umap)
2127 return total(umap, &isl_map_polyhedral_hull_map);
2130 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
2131 __isl_take isl_union_set *uset)
2133 return isl_union_map_polyhedral_hull(uset);
2136 /* Compute a superset of the convex hull of "map" that is described
2137 * by only translates of the constraints in the constituents of "map" and
2138 * return the result as an isl_map.
2140 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
2142 return isl_map_from_basic_map(isl_map_simple_hull(map));
2145 __isl_give isl_union_map *isl_union_map_simple_hull(
2146 __isl_take isl_union_map *umap)
2148 return total(umap, &isl_map_simple_hull_map);
2151 __isl_give isl_union_set *isl_union_set_simple_hull(
2152 __isl_take isl_union_set *uset)
2154 return isl_union_map_simple_hull(uset);
2157 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
2158 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2160 struct isl_un_op_control control = {
2161 .inplace = 1,
2162 .fn_map = fn,
2165 return un_op(umap, &control);
2168 /* Remove redundant constraints in each of the basic maps of "umap".
2169 * Since removing redundant constraints does not change the meaning
2170 * or the space, the operation can be performed in-place.
2172 __isl_give isl_union_map *isl_union_map_remove_redundancies(
2173 __isl_take isl_union_map *umap)
2175 return inplace(umap, &isl_map_remove_redundancies);
2178 /* Remove redundant constraints in each of the basic sets of "uset".
2180 __isl_give isl_union_set *isl_union_set_remove_redundancies(
2181 __isl_take isl_union_set *uset)
2183 return isl_union_map_remove_redundancies(uset);
2186 __isl_give isl_union_map *isl_union_map_coalesce(
2187 __isl_take isl_union_map *umap)
2189 return inplace(umap, &isl_map_coalesce);
2192 __isl_give isl_union_set *isl_union_set_coalesce(
2193 __isl_take isl_union_set *uset)
2195 return isl_union_map_coalesce(uset);
2198 __isl_give isl_union_map *isl_union_map_detect_equalities(
2199 __isl_take isl_union_map *umap)
2201 return inplace(umap, &isl_map_detect_equalities);
2204 __isl_give isl_union_set *isl_union_set_detect_equalities(
2205 __isl_take isl_union_set *uset)
2207 return isl_union_map_detect_equalities(uset);
2210 __isl_give isl_union_map *isl_union_map_compute_divs(
2211 __isl_take isl_union_map *umap)
2213 return inplace(umap, &isl_map_compute_divs);
2216 __isl_give isl_union_set *isl_union_set_compute_divs(
2217 __isl_take isl_union_set *uset)
2219 return isl_union_map_compute_divs(uset);
2222 __isl_give isl_union_map *isl_union_map_lexmin(
2223 __isl_take isl_union_map *umap)
2225 return total(umap, &isl_map_lexmin);
2228 __isl_give isl_union_set *isl_union_set_lexmin(
2229 __isl_take isl_union_set *uset)
2231 return isl_union_map_lexmin(uset);
2234 __isl_give isl_union_map *isl_union_map_lexmax(
2235 __isl_take isl_union_map *umap)
2237 return total(umap, &isl_map_lexmax);
2240 __isl_give isl_union_set *isl_union_set_lexmax(
2241 __isl_take isl_union_set *uset)
2243 return isl_union_map_lexmax(uset);
2246 /* Return the universe in the space of "map".
2248 static __isl_give isl_map *universe(__isl_take isl_map *map)
2250 isl_space *space;
2252 space = isl_map_get_space(map);
2253 isl_map_free(map);
2254 return isl_map_universe(space);
2257 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2259 struct isl_un_op_control control = {
2260 .fn_map = &universe,
2262 return un_op(umap, &control);
2265 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2267 return isl_union_map_universe(uset);
2270 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2272 struct isl_un_op_control control = {
2273 .fn_map = &isl_map_reverse,
2275 return un_op(umap, &control);
2278 /* Given a union map, take the maps of the form A -> (B -> C) and
2279 * return the union of the corresponding maps A -> (C -> B).
2281 __isl_give isl_union_map *isl_union_map_range_reverse(
2282 __isl_take isl_union_map *umap)
2284 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2285 struct isl_un_op_control control = {
2286 .filter = &un_op_filter_drop_user,
2287 .filter_user = &data,
2288 .fn_map = &isl_map_range_reverse,
2290 return un_op(umap, &control);
2293 /* Compute the parameter domain of the given union map.
2295 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2297 struct isl_un_op_control control = {
2298 .fn_map = &isl_map_params,
2300 int empty;
2302 empty = isl_union_map_is_empty(umap);
2303 if (empty < 0)
2304 goto error;
2305 if (empty) {
2306 isl_space *space;
2307 space = isl_union_map_get_space(umap);
2308 isl_union_map_free(umap);
2309 return isl_set_empty(space);
2311 return isl_set_from_union_set(un_op(umap, &control));
2312 error:
2313 isl_union_map_free(umap);
2314 return NULL;
2317 /* Compute the parameter domain of the given union set.
2319 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2321 return isl_union_map_params(uset);
2324 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2326 struct isl_un_op_control control = {
2327 .fn_map = &isl_map_domain,
2329 return un_op(umap, &control);
2332 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2334 struct isl_un_op_control control = {
2335 .fn_map = &isl_map_range,
2337 return un_op(umap, &control);
2340 __isl_give isl_union_map *isl_union_map_domain_map(
2341 __isl_take isl_union_map *umap)
2343 struct isl_un_op_control control = {
2344 .fn_map = &isl_map_domain_map,
2346 return un_op(umap, &control);
2349 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2350 * add the result to "res".
2352 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2354 isl_union_pw_multi_aff **res = user;
2355 isl_multi_aff *ma;
2356 isl_pw_multi_aff *pma;
2358 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2359 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2360 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2362 return *res ? isl_stat_ok : isl_stat_error;
2366 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2367 * to its domain.
2369 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2370 __isl_take isl_union_map *umap)
2372 isl_union_pw_multi_aff *res;
2374 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2375 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2376 res = isl_union_pw_multi_aff_free(res);
2378 isl_union_map_free(umap);
2379 return res;
2382 __isl_give isl_union_map *isl_union_map_range_map(
2383 __isl_take isl_union_map *umap)
2385 struct isl_un_op_control control = {
2386 .fn_map = &isl_map_range_map,
2388 return un_op(umap, &control);
2391 /* Given a collection of wrapped maps of the form A[B -> C],
2392 * return the collection of maps A[B -> C] -> B.
2394 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2395 __isl_take isl_union_set *uset)
2397 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2398 struct isl_un_op_control control = {
2399 .filter = &un_op_filter_drop_user,
2400 .filter_user = &data,
2401 .fn_map = &isl_set_wrapped_domain_map,
2403 return un_op(uset, &control);
2406 /* Does "map" relate elements from the same space?
2408 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2410 return isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
2413 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2415 struct isl_un_op_control control = {
2416 .filter = &equal_tuples,
2417 .fn_map = &isl_map_deltas,
2419 return un_op(umap, &control);
2422 __isl_give isl_union_map *isl_union_map_deltas_map(
2423 __isl_take isl_union_map *umap)
2425 struct isl_un_op_control control = {
2426 .filter = &equal_tuples,
2427 .fn_map = &isl_map_deltas_map,
2429 return un_op(umap, &control);
2432 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2434 struct isl_un_op_control control = {
2435 .fn_map = &isl_set_identity,
2437 return un_op(uset, &control);
2440 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2442 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2444 isl_union_pw_multi_aff **res = user;
2445 isl_space *space;
2446 isl_pw_multi_aff *pma;
2448 space = isl_space_map_from_set(isl_set_get_space(set));
2449 pma = isl_pw_multi_aff_identity(space);
2450 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2451 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2453 return *res ? isl_stat_ok : isl_stat_error;
2456 /* Return an identity function on "uset" in the form
2457 * of an isl_union_pw_multi_aff.
2459 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2460 __isl_take isl_union_set *uset)
2462 isl_union_pw_multi_aff *res;
2464 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2465 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2466 res = isl_union_pw_multi_aff_free(res);
2468 isl_union_set_free(uset);
2469 return res;
2472 /* For each map in "umap" of the form [A -> B] -> C,
2473 * construct the map A -> C and collect the results.
2475 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2476 __isl_take isl_union_map *umap)
2478 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2479 struct isl_un_op_control control = {
2480 .filter = &un_op_filter_drop_user,
2481 .filter_user = &data,
2482 .fn_map = &isl_map_domain_factor_domain,
2484 return un_op(umap, &control);
2487 /* For each map in "umap" of the form [A -> B] -> C,
2488 * construct the map B -> C and collect the results.
2490 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2491 __isl_take isl_union_map *umap)
2493 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2494 struct isl_un_op_control control = {
2495 .filter = &un_op_filter_drop_user,
2496 .filter_user = &data,
2497 .fn_map = &isl_map_domain_factor_range,
2499 return un_op(umap, &control);
2502 /* For each map in "umap" of the form A -> [B -> C],
2503 * construct the map A -> B and collect the results.
2505 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2506 __isl_take isl_union_map *umap)
2508 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2509 struct isl_un_op_control control = {
2510 .filter = &un_op_filter_drop_user,
2511 .filter_user = &data,
2512 .fn_map = &isl_map_range_factor_domain,
2514 return un_op(umap, &control);
2517 /* For each map in "umap" of the form A -> [B -> C],
2518 * construct the map A -> C and collect the results.
2520 __isl_give isl_union_map *isl_union_map_range_factor_range(
2521 __isl_take isl_union_map *umap)
2523 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2524 struct isl_un_op_control control = {
2525 .filter = &un_op_filter_drop_user,
2526 .filter_user = &data,
2527 .fn_map = &isl_map_range_factor_range,
2529 return un_op(umap, &control);
2532 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2533 * construct the map A -> C and collect the results.
2535 __isl_give isl_union_map *isl_union_map_factor_domain(
2536 __isl_take isl_union_map *umap)
2538 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2539 struct isl_un_op_control control = {
2540 .filter = &un_op_filter_drop_user,
2541 .filter_user = &data,
2542 .fn_map = &isl_map_factor_domain,
2544 return un_op(umap, &control);
2547 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2548 * construct the map B -> D and collect the results.
2550 __isl_give isl_union_map *isl_union_map_factor_range(
2551 __isl_take isl_union_map *umap)
2553 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2554 struct isl_un_op_control control = {
2555 .filter = &un_op_filter_drop_user,
2556 .filter_user = &data,
2557 .fn_map = &isl_map_factor_range,
2559 return un_op(umap, &control);
2562 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2564 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2565 struct isl_un_op_control control = {
2566 .filter = &un_op_filter_drop_user,
2567 .filter_user = &data,
2568 .fn_map = &isl_set_unwrap,
2570 return un_op(uset, &control);
2573 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2575 struct isl_un_op_control control = {
2576 .fn_map = &isl_map_wrap,
2578 return un_op(umap, &control);
2581 struct isl_union_map_is_subset_data {
2582 isl_union_map *umap2;
2583 isl_bool is_subset;
2586 static isl_stat is_subset_entry(void **entry, void *user)
2588 struct isl_union_map_is_subset_data *data = user;
2589 struct isl_hash_table_entry *entry2;
2590 isl_space *space;
2591 isl_map *map = *entry;
2593 space = isl_map_peek_space(map);
2594 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
2595 if (!entry2)
2596 return isl_stat_error;
2597 if (entry2 == isl_hash_table_entry_none) {
2598 int empty = isl_map_is_empty(map);
2599 if (empty < 0)
2600 return isl_stat_error;
2601 if (empty)
2602 return isl_stat_ok;
2603 data->is_subset = isl_bool_false;
2604 return isl_stat_error;
2607 data->is_subset = isl_map_is_subset(map, entry2->data);
2608 if (data->is_subset < 0 || !data->is_subset)
2609 return isl_stat_error;
2611 return isl_stat_ok;
2614 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2615 __isl_keep isl_union_map *umap2)
2617 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2619 if (!umap1 || !umap2)
2620 return isl_bool_error;
2622 data.umap2 = umap2;
2623 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2624 &is_subset_entry, &data) < 0 &&
2625 data.is_subset)
2626 return isl_bool_error;
2628 return data.is_subset;
2631 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2632 __isl_keep isl_union_set *uset2)
2634 return isl_union_map_is_subset(uset1, uset2);
2637 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2638 __isl_keep isl_union_map *umap2)
2640 isl_bool is_subset;
2642 if (!umap1 || !umap2)
2643 return isl_bool_error;
2644 is_subset = isl_union_map_is_subset(umap1, umap2);
2645 if (is_subset != isl_bool_true)
2646 return is_subset;
2647 is_subset = isl_union_map_is_subset(umap2, umap1);
2648 return is_subset;
2651 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2652 __isl_keep isl_union_set *uset2)
2654 return isl_union_map_is_equal(uset1, uset2);
2657 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2658 __isl_keep isl_union_map *umap2)
2660 isl_bool is_subset;
2662 if (!umap1 || !umap2)
2663 return isl_bool_error;
2664 is_subset = isl_union_map_is_subset(umap1, umap2);
2665 if (is_subset != isl_bool_true)
2666 return is_subset;
2667 is_subset = isl_union_map_is_subset(umap2, umap1);
2668 return isl_bool_not(is_subset);
2671 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2672 __isl_keep isl_union_set *uset2)
2674 return isl_union_map_is_strict_subset(uset1, uset2);
2677 /* Internal data structure for isl_union_map_is_disjoint.
2678 * umap2 is the union map with which we are comparing.
2679 * is_disjoint is initialized to 1 and is set to 0 as soon
2680 * as the union maps turn out not to be disjoint.
2682 struct isl_union_map_is_disjoint_data {
2683 isl_union_map *umap2;
2684 isl_bool is_disjoint;
2687 /* Check if "map" is disjoint from data->umap2 and abort
2688 * the search if it is not.
2690 static isl_stat is_disjoint_entry(void **entry, void *user)
2692 struct isl_union_map_is_disjoint_data *data = user;
2693 struct isl_hash_table_entry *entry2;
2694 isl_space *space;
2695 isl_map *map = *entry;
2697 space = isl_map_peek_space(map);
2698 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
2699 if (!entry2)
2700 return isl_stat_error;
2701 if (entry2 == isl_hash_table_entry_none)
2702 return isl_stat_ok;
2704 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2705 if (data->is_disjoint < 0 || !data->is_disjoint)
2706 return isl_stat_error;
2708 return isl_stat_ok;
2711 /* Are "umap1" and "umap2" disjoint?
2713 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2714 __isl_keep isl_union_map *umap2)
2716 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2718 umap1 = isl_union_map_copy(umap1);
2719 umap2 = isl_union_map_copy(umap2);
2720 umap1 = isl_union_map_align_params(umap1,
2721 isl_union_map_get_space(umap2));
2722 umap2 = isl_union_map_align_params(umap2,
2723 isl_union_map_get_space(umap1));
2725 if (!umap1 || !umap2)
2726 goto error;
2728 data.umap2 = umap2;
2729 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2730 &is_disjoint_entry, &data) < 0 &&
2731 data.is_disjoint)
2732 goto error;
2734 isl_union_map_free(umap1);
2735 isl_union_map_free(umap2);
2737 return data.is_disjoint;
2738 error:
2739 isl_union_map_free(umap1);
2740 isl_union_map_free(umap2);
2741 return isl_bool_error;
2744 /* Are "uset1" and "uset2" disjoint?
2746 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2747 __isl_keep isl_union_set *uset2)
2749 return isl_union_map_is_disjoint(uset1, uset2);
2752 static isl_stat sample_entry(void **entry, void *user)
2754 isl_basic_map **sample = (isl_basic_map **)user;
2755 isl_map *map = *entry;
2757 *sample = isl_map_sample(isl_map_copy(map));
2758 if (!*sample)
2759 return isl_stat_error;
2760 if (!isl_basic_map_plain_is_empty(*sample))
2761 return isl_stat_error;
2762 return isl_stat_ok;
2765 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2767 isl_basic_map *sample = NULL;
2769 if (!umap)
2770 return NULL;
2772 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2773 &sample_entry, &sample) < 0 &&
2774 !sample)
2775 goto error;
2777 if (!sample)
2778 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2780 isl_union_map_free(umap);
2782 return sample;
2783 error:
2784 isl_union_map_free(umap);
2785 return NULL;
2788 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2790 return bset_from_bmap(isl_union_map_sample(uset));
2793 /* Return an element in "uset" in the form of an isl_point.
2794 * Return a void isl_point if "uset" is empty.
2796 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2798 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2801 struct isl_forall_data {
2802 isl_bool res;
2803 isl_bool (*fn)(__isl_keep isl_map *map);
2806 static isl_stat forall_entry(void **entry, void *user)
2808 struct isl_forall_data *data = user;
2809 isl_map *map = *entry;
2811 data->res = data->fn(map);
2812 if (data->res < 0)
2813 return isl_stat_error;
2815 if (!data->res)
2816 return isl_stat_error;
2818 return isl_stat_ok;
2821 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2822 isl_bool (*fn)(__isl_keep isl_map *map))
2824 struct isl_forall_data data = { isl_bool_true, fn };
2826 if (!umap)
2827 return isl_bool_error;
2829 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2830 &forall_entry, &data) < 0 && data.res)
2831 return isl_bool_error;
2833 return data.res;
2836 struct isl_forall_user_data {
2837 isl_bool res;
2838 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2839 void *user;
2842 static isl_stat forall_user_entry(void **entry, void *user)
2844 struct isl_forall_user_data *data = user;
2845 isl_map *map = *entry;
2847 data->res = data->fn(map, data->user);
2848 if (data->res < 0)
2849 return isl_stat_error;
2851 if (!data->res)
2852 return isl_stat_error;
2854 return isl_stat_ok;
2857 /* Check if fn(map, user) returns true for all maps "map" in umap.
2859 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2860 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2862 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2864 if (!umap)
2865 return isl_bool_error;
2867 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2868 &forall_user_entry, &data) < 0 && data.res)
2869 return isl_bool_error;
2871 return data.res;
2874 /* Is "umap" obviously empty?
2876 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2878 isl_size n;
2880 n = isl_union_map_n_map(umap);
2881 if (n < 0)
2882 return isl_bool_error;
2883 return n == 0;
2886 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2888 return union_map_forall(umap, &isl_map_is_empty);
2891 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2893 return isl_union_map_is_empty(uset);
2896 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2898 isl_bool is_subset;
2899 isl_space *space;
2900 isl_map *id;
2901 isl_bool match;
2903 match = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
2904 if (match < 0)
2905 return isl_bool_error;
2906 if (!match)
2907 return isl_bool_false;
2909 space = isl_map_get_space(map);
2910 id = isl_map_identity(space);
2912 is_subset = isl_map_is_subset(map, id);
2914 isl_map_free(id);
2916 return is_subset;
2919 /* Given an isl_union_map that consists of a single map, check
2920 * if it is single-valued.
2922 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2924 isl_map *map;
2925 isl_bool sv;
2927 umap = isl_union_map_copy(umap);
2928 map = isl_map_from_union_map(umap);
2929 sv = isl_map_is_single_valued(map);
2930 isl_map_free(map);
2932 return sv;
2935 /* Internal data structure for single_valued_on_domain.
2937 * "umap" is the union map to be tested.
2938 * "sv" is set to 1 as long as "umap" may still be single-valued.
2940 struct isl_union_map_is_sv_data {
2941 isl_union_map *umap;
2942 isl_bool sv;
2945 /* Check if the data->umap is single-valued on "set".
2947 * If data->umap consists of a single map on "set", then test it
2948 * as an isl_map.
2950 * Otherwise, compute
2952 * M \circ M^-1
2954 * check if the result is a subset of the identity mapping and
2955 * store the result in data->sv.
2957 * Terminate as soon as data->umap has been determined not to
2958 * be single-valued.
2960 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2962 struct isl_union_map_is_sv_data *data = user;
2963 isl_union_map *umap, *test;
2964 isl_size n;
2966 umap = isl_union_map_copy(data->umap);
2967 umap = isl_union_map_intersect_domain(umap,
2968 isl_union_set_from_set(set));
2970 n = isl_union_map_n_map(umap);
2971 if (n < 0) {
2972 data->sv = isl_bool_error;
2973 } else if (n == 1) {
2974 data->sv = single_map_is_single_valued(umap);
2975 isl_union_map_free(umap);
2976 } else {
2977 test = isl_union_map_reverse(isl_union_map_copy(umap));
2978 test = isl_union_map_apply_range(test, umap);
2980 data->sv = union_map_forall(test, &is_subset_of_identity);
2982 isl_union_map_free(test);
2985 if (data->sv < 0 || !data->sv)
2986 return isl_stat_error;
2987 return isl_stat_ok;
2990 /* Check if the given map is single-valued.
2992 * If the union map consists of a single map, then test it as an isl_map.
2993 * Otherwise, check if the union map is single-valued on each of its
2994 * domain spaces.
2996 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2998 isl_union_map *universe;
2999 isl_union_set *domain;
3000 struct isl_union_map_is_sv_data data;
3001 isl_size n;
3003 n = isl_union_map_n_map(umap);
3004 if (n < 0)
3005 return isl_bool_error;
3006 if (n == 1)
3007 return single_map_is_single_valued(umap);
3009 universe = isl_union_map_universe(isl_union_map_copy(umap));
3010 domain = isl_union_map_domain(universe);
3012 data.sv = isl_bool_true;
3013 data.umap = umap;
3014 if (isl_union_set_foreach_set(domain,
3015 &single_valued_on_domain, &data) < 0 && data.sv)
3016 data.sv = isl_bool_error;
3017 isl_union_set_free(domain);
3019 return data.sv;
3022 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
3024 isl_bool in;
3026 umap = isl_union_map_copy(umap);
3027 umap = isl_union_map_reverse(umap);
3028 in = isl_union_map_is_single_valued(umap);
3029 isl_union_map_free(umap);
3031 return in;
3034 /* Is "map" obviously not an identity relation because
3035 * it maps elements from one space to another space?
3036 * Update *non_identity accordingly.
3038 * In particular, if the domain and range spaces are the same,
3039 * then the map is not considered to obviously not be an identity relation.
3040 * Otherwise, the map is considered to obviously not be an identity relation
3041 * if it is is non-empty.
3043 * If "map" is determined to obviously not be an identity relation,
3044 * then the search is aborted.
3046 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
3048 isl_bool *non_identity = user;
3049 isl_bool equal;
3051 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
3052 if (equal >= 0 && !equal)
3053 *non_identity = isl_bool_not(isl_map_is_empty(map));
3054 else
3055 *non_identity = isl_bool_not(equal);
3056 isl_map_free(map);
3058 if (*non_identity < 0 || *non_identity)
3059 return isl_stat_error;
3061 return isl_stat_ok;
3064 /* Is "umap" obviously not an identity relation because
3065 * it maps elements from one space to another space?
3067 * As soon as a map has been found that maps elements to a different space,
3068 * non_identity is changed and the search is aborted.
3070 static isl_bool isl_union_map_plain_is_not_identity(
3071 __isl_keep isl_union_map *umap)
3073 isl_bool non_identity;
3075 non_identity = isl_bool_false;
3076 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
3077 &non_identity) < 0 &&
3078 non_identity == isl_bool_false)
3079 return isl_bool_error;
3081 return non_identity;
3084 /* Does "map" only map elements to themselves?
3085 * Update *identity accordingly.
3087 * If "map" is determined not to be an identity relation,
3088 * then the search is aborted.
3090 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
3092 isl_bool *identity = user;
3094 *identity = isl_map_is_identity(map);
3095 isl_map_free(map);
3097 if (*identity < 0 || !*identity)
3098 return isl_stat_error;
3100 return isl_stat_ok;
3103 /* Does "umap" only map elements to themselves?
3105 * First check if there are any maps that map elements to different spaces.
3106 * If not, then check that all the maps (between identical spaces)
3107 * are identity relations.
3109 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
3111 isl_bool non_identity;
3112 isl_bool identity;
3114 non_identity = isl_union_map_plain_is_not_identity(umap);
3115 if (non_identity < 0 || non_identity)
3116 return isl_bool_not(non_identity);
3118 identity = isl_bool_true;
3119 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
3120 identity == isl_bool_true)
3121 return isl_bool_error;
3123 return identity;
3126 /* Represents a map that has a fixed value (v) for one of its
3127 * range dimensions.
3128 * The map in this structure is not reference counted, so it
3129 * is only valid while the isl_union_map from which it was
3130 * obtained is still alive.
3132 struct isl_fixed_map {
3133 isl_int v;
3134 isl_map *map;
3137 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
3138 int n)
3140 int i;
3141 struct isl_fixed_map *v;
3143 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
3144 if (!v)
3145 return NULL;
3146 for (i = 0; i < n; ++i)
3147 isl_int_init(v[i].v);
3148 return v;
3151 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
3153 int i;
3155 if (!v)
3156 return;
3157 for (i = 0; i < n; ++i)
3158 isl_int_clear(v[i].v);
3159 free(v);
3162 /* Compare the "v" field of two isl_fixed_map structs.
3164 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
3166 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
3167 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
3169 return isl_int_cmp(e1->v, e2->v);
3172 /* Internal data structure used while checking whether all maps
3173 * in a union_map have a fixed value for a given output dimension.
3174 * v is the list of maps, with the fixed value for the dimension
3175 * n is the number of maps considered so far
3176 * pos is the output dimension under investigation
3178 struct isl_fixed_dim_data {
3179 struct isl_fixed_map *v;
3180 int n;
3181 int pos;
3184 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
3186 struct isl_fixed_dim_data *data = user;
3188 data->v[data->n].map = map;
3189 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
3190 &data->v[data->n++].v);
3193 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3194 int first, int n_range);
3196 /* Given a list of the maps, with their fixed values at output dimension "pos",
3197 * check whether the ranges of the maps form an obvious partition.
3199 * We first sort the maps according to their fixed values.
3200 * If all maps have a different value, then we know the ranges form
3201 * a partition.
3202 * Otherwise, we collect the maps with the same fixed value and
3203 * check whether each such collection is obviously injective
3204 * based on later dimensions.
3206 static int separates(struct isl_fixed_map *v, int n,
3207 __isl_take isl_space *space, int pos, int n_range)
3209 int i;
3211 if (!v)
3212 goto error;
3214 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
3216 for (i = 0; i + 1 < n; ++i) {
3217 int j, k;
3218 isl_union_map *part;
3219 int injective;
3221 for (j = i + 1; j < n; ++j)
3222 if (isl_int_ne(v[i].v, v[j].v))
3223 break;
3225 if (j == i + 1)
3226 continue;
3228 part = isl_union_map_alloc(isl_space_copy(space), j - i);
3229 for (k = i; k < j; ++k)
3230 part = isl_union_map_add_map(part,
3231 isl_map_copy(v[k].map));
3233 injective = plain_injective_on_range(part, pos + 1, n_range);
3234 if (injective < 0)
3235 goto error;
3236 if (!injective)
3237 break;
3239 i = j - 1;
3242 isl_space_free(space);
3243 free_isl_fixed_map_array(v, n);
3244 return i + 1 >= n;
3245 error:
3246 isl_space_free(space);
3247 free_isl_fixed_map_array(v, n);
3248 return -1;
3251 /* Check whether the maps in umap have obviously distinct ranges.
3252 * In particular, check for an output dimension in the range
3253 * [first,n_range) for which all maps have a fixed value
3254 * and then check if these values, possibly along with fixed values
3255 * at later dimensions, entail distinct ranges.
3257 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3258 int first, int n_range)
3260 isl_ctx *ctx;
3261 isl_size n;
3262 struct isl_fixed_dim_data data = { NULL };
3264 ctx = isl_union_map_get_ctx(umap);
3266 n = isl_union_map_n_map(umap);
3267 if (n < 0)
3268 goto error;
3270 if (n <= 1) {
3271 isl_union_map_free(umap);
3272 return isl_bool_true;
3275 if (first >= n_range) {
3276 isl_union_map_free(umap);
3277 return isl_bool_false;
3280 data.v = alloc_isl_fixed_map_array(ctx, n);
3281 if (!data.v)
3282 goto error;
3284 for (data.pos = first; data.pos < n_range; ++data.pos) {
3285 isl_bool fixed;
3286 int injective;
3287 isl_space *space;
3289 data.n = 0;
3290 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3291 if (fixed < 0)
3292 goto error;
3293 if (!fixed)
3294 continue;
3295 space = isl_union_map_get_space(umap);
3296 injective = separates(data.v, n, space, data.pos, n_range);
3297 isl_union_map_free(umap);
3298 return injective;
3301 free_isl_fixed_map_array(data.v, n);
3302 isl_union_map_free(umap);
3304 return isl_bool_false;
3305 error:
3306 free_isl_fixed_map_array(data.v, n);
3307 isl_union_map_free(umap);
3308 return isl_bool_error;
3311 /* Check whether the maps in umap that map to subsets of "ran"
3312 * have obviously distinct ranges.
3314 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3315 void *user)
3317 isl_size dim;
3318 isl_union_map *umap = user;
3320 dim = isl_set_dim(ran, isl_dim_set);
3321 if (dim < 0)
3322 return isl_bool_error;
3324 umap = isl_union_map_copy(umap);
3325 umap = isl_union_map_intersect_range(umap,
3326 isl_union_set_from_set(isl_set_copy(ran)));
3327 return plain_injective_on_range(umap, 0, dim);
3330 /* Check if the given union_map is obviously injective.
3332 * In particular, we first check if all individual maps are obviously
3333 * injective and then check if all the ranges of these maps are
3334 * obviously disjoint.
3336 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3338 isl_bool in;
3339 isl_union_map *univ;
3340 isl_union_set *ran;
3342 in = union_map_forall(umap, &isl_map_plain_is_injective);
3343 if (in < 0)
3344 return isl_bool_error;
3345 if (!in)
3346 return isl_bool_false;
3348 univ = isl_union_map_universe(isl_union_map_copy(umap));
3349 ran = isl_union_map_range(univ);
3351 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3353 isl_union_set_free(ran);
3355 return in;
3358 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3360 isl_bool sv;
3362 sv = isl_union_map_is_single_valued(umap);
3363 if (sv < 0 || !sv)
3364 return sv;
3366 return isl_union_map_is_injective(umap);
3369 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3371 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3372 struct isl_un_op_control control = {
3373 .filter = &un_op_filter_drop_user,
3374 .filter_user = &data,
3375 .fn_map = &isl_map_zip,
3377 return un_op(umap, &control);
3380 /* Given a union map, take the maps of the form A -> (B -> C) and
3381 * return the union of the corresponding maps (A -> B) -> C.
3383 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3385 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3386 struct isl_un_op_control control = {
3387 .filter = &un_op_filter_drop_user,
3388 .filter_user = &data,
3389 .fn_map = &isl_map_uncurry,
3391 return un_op(umap, &control);
3394 /* Given a union map, take the maps of the form (A -> B) -> C and
3395 * return the union of the corresponding maps A -> (B -> C).
3397 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3399 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3400 struct isl_un_op_control control = {
3401 .filter = &un_op_filter_drop_user,
3402 .filter_user = &data,
3403 .fn_map = &isl_map_curry,
3405 return un_op(umap, &control);
3408 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3409 * return the union of the corresponding maps A -> (B -> (C -> D)).
3411 __isl_give isl_union_map *isl_union_map_range_curry(
3412 __isl_take isl_union_map *umap)
3414 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3415 struct isl_un_op_control control = {
3416 .filter = &un_op_filter_drop_user,
3417 .filter_user = &data,
3418 .fn_map = &isl_map_range_curry,
3420 return un_op(umap, &control);
3423 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3425 struct isl_un_op_control control = {
3426 .fn_map = &isl_set_lift,
3428 return un_op(uset, &control);
3431 static isl_stat coefficients_entry(void **entry, void *user)
3433 isl_set *set = *entry;
3434 isl_union_set **res = user;
3436 set = isl_set_copy(set);
3437 set = isl_set_from_basic_set(isl_set_coefficients(set));
3438 *res = isl_union_set_add_set(*res, set);
3440 return isl_stat_ok;
3443 __isl_give isl_union_set *isl_union_set_coefficients(
3444 __isl_take isl_union_set *uset)
3446 isl_ctx *ctx;
3447 isl_space *space;
3448 isl_union_set *res;
3450 if (!uset)
3451 return NULL;
3453 ctx = isl_union_set_get_ctx(uset);
3454 space = isl_space_set_alloc(ctx, 0, 0);
3455 res = isl_union_map_alloc(space, uset->table.n);
3456 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3457 &coefficients_entry, &res) < 0)
3458 goto error;
3460 isl_union_set_free(uset);
3461 return res;
3462 error:
3463 isl_union_set_free(uset);
3464 isl_union_set_free(res);
3465 return NULL;
3468 static isl_stat solutions_entry(void **entry, void *user)
3470 isl_set *set = *entry;
3471 isl_union_set **res = user;
3473 set = isl_set_copy(set);
3474 set = isl_set_from_basic_set(isl_set_solutions(set));
3475 if (!*res)
3476 *res = isl_union_set_from_set(set);
3477 else
3478 *res = isl_union_set_add_set(*res, set);
3480 if (!*res)
3481 return isl_stat_error;
3483 return isl_stat_ok;
3486 __isl_give isl_union_set *isl_union_set_solutions(
3487 __isl_take isl_union_set *uset)
3489 isl_union_set *res = NULL;
3491 if (!uset)
3492 return NULL;
3494 if (uset->table.n == 0) {
3495 res = isl_union_set_empty(isl_union_set_get_space(uset));
3496 isl_union_set_free(uset);
3497 return res;
3500 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3501 &solutions_entry, &res) < 0)
3502 goto error;
3504 isl_union_set_free(uset);
3505 return res;
3506 error:
3507 isl_union_set_free(uset);
3508 isl_union_set_free(res);
3509 return NULL;
3512 /* Is the domain space of "map" equal to "space"?
3514 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3516 return isl_map_space_tuple_is_equal(map, isl_dim_in,
3517 space, isl_dim_out);
3520 /* Is the range space of "map" equal to "space"?
3522 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3524 return isl_map_space_tuple_is_equal(map, isl_dim_out,
3525 space, isl_dim_out);
3528 /* Is the set space of "map" equal to "space"?
3530 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3532 return isl_map_space_tuple_is_equal(map, isl_dim_set,
3533 space, isl_dim_out);
3536 /* Internal data structure for preimage_pw_multi_aff.
3538 * "pma" is the function under which the preimage should be taken.
3539 * "space" is the space of "pma".
3540 * "res" collects the results.
3541 * "fn" computes the preimage for a given map.
3542 * "match" returns true if "fn" can be called.
3544 struct isl_union_map_preimage_data {
3545 isl_space *space;
3546 isl_pw_multi_aff *pma;
3547 isl_union_map *res;
3548 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3549 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3550 __isl_take isl_pw_multi_aff *pma);
3553 /* Call data->fn to compute the preimage of the domain or range of *entry
3554 * under the function represented by data->pma, provided the domain/range
3555 * space of *entry matches the target space of data->pma
3556 * (as given by data->match), and add the result to data->res.
3558 static isl_stat preimage_entry(void **entry, void *user)
3560 int m;
3561 isl_map *map = *entry;
3562 struct isl_union_map_preimage_data *data = user;
3563 isl_bool empty;
3565 m = data->match(map, data->space);
3566 if (m < 0)
3567 return isl_stat_error;
3568 if (!m)
3569 return isl_stat_ok;
3571 map = isl_map_copy(map);
3572 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3574 empty = isl_map_is_empty(map);
3575 if (empty < 0 || empty) {
3576 isl_map_free(map);
3577 return empty < 0 ? isl_stat_error : isl_stat_ok;
3580 data->res = isl_union_map_add_map(data->res, map);
3582 return isl_stat_ok;
3585 /* Compute the preimage of the domain or range of "umap" under the function
3586 * represented by "pma".
3587 * In other words, plug in "pma" in the domain or range of "umap".
3588 * The function "fn" performs the actual preimage computation on a map,
3589 * while "match" determines to which maps the function should be applied.
3591 static __isl_give isl_union_map *preimage_pw_multi_aff(
3592 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3593 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3594 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3595 __isl_take isl_pw_multi_aff *pma))
3597 isl_ctx *ctx;
3598 isl_space *space;
3599 struct isl_union_map_preimage_data data;
3601 umap = isl_union_map_align_params(umap,
3602 isl_pw_multi_aff_get_space(pma));
3603 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3605 if (!umap || !pma)
3606 goto error;
3608 ctx = isl_union_map_get_ctx(umap);
3609 space = isl_union_map_get_space(umap);
3610 data.space = isl_pw_multi_aff_get_space(pma);
3611 data.pma = pma;
3612 data.res = isl_union_map_alloc(space, umap->table.n);
3613 data.match = match;
3614 data.fn = fn;
3615 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3616 &data) < 0)
3617 data.res = isl_union_map_free(data.res);
3619 isl_space_free(data.space);
3620 isl_union_map_free(umap);
3621 isl_pw_multi_aff_free(pma);
3622 return data.res;
3623 error:
3624 isl_union_map_free(umap);
3625 isl_pw_multi_aff_free(pma);
3626 return NULL;
3629 /* Compute the preimage of the domain of "umap" under the function
3630 * represented by "pma".
3631 * In other words, plug in "pma" in the domain of "umap".
3632 * The result contains maps that live in the same spaces as the maps of "umap"
3633 * with domain space equal to the target space of "pma",
3634 * except that the domain has been replaced by the domain space of "pma".
3636 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3637 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3639 return preimage_pw_multi_aff(umap, pma, &domain_match,
3640 &isl_map_preimage_domain_pw_multi_aff);
3643 /* Compute the preimage of the range of "umap" under the function
3644 * represented by "pma".
3645 * In other words, plug in "pma" in the range of "umap".
3646 * The result contains maps that live in the same spaces as the maps of "umap"
3647 * with range space equal to the target space of "pma",
3648 * except that the range has been replaced by the domain space of "pma".
3650 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3651 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3653 return preimage_pw_multi_aff(umap, pma, &range_match,
3654 &isl_map_preimage_range_pw_multi_aff);
3657 /* Compute the preimage of "uset" under the function represented by "pma".
3658 * In other words, plug in "pma" in "uset".
3659 * The result contains sets that live in the same spaces as the sets of "uset"
3660 * with space equal to the target space of "pma",
3661 * except that the space has been replaced by the domain space of "pma".
3663 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3664 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3666 return preimage_pw_multi_aff(uset, pma, &set_match,
3667 &isl_set_preimage_pw_multi_aff);
3670 /* Compute the preimage of the domain of "umap" under the function
3671 * represented by "ma".
3672 * In other words, plug in "ma" in the domain of "umap".
3673 * The result contains maps that live in the same spaces as the maps of "umap"
3674 * with domain space equal to the target space of "ma",
3675 * except that the domain has been replaced by the domain space of "ma".
3677 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3678 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3680 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3681 isl_pw_multi_aff_from_multi_aff(ma));
3684 /* Compute the preimage of the range of "umap" under the function
3685 * represented by "ma".
3686 * In other words, plug in "ma" in the range of "umap".
3687 * The result contains maps that live in the same spaces as the maps of "umap"
3688 * with range space equal to the target space of "ma",
3689 * except that the range has been replaced by the domain space of "ma".
3691 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3692 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3694 return isl_union_map_preimage_range_pw_multi_aff(umap,
3695 isl_pw_multi_aff_from_multi_aff(ma));
3698 /* Compute the preimage of "uset" under the function represented by "ma".
3699 * In other words, plug in "ma" in "uset".
3700 * The result contains sets that live in the same spaces as the sets of "uset"
3701 * with space equal to the target space of "ma",
3702 * except that the space has been replaced by the domain space of "ma".
3704 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3705 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3707 return isl_union_set_preimage_pw_multi_aff(uset,
3708 isl_pw_multi_aff_from_multi_aff(ma));
3711 /* Internal data structure for preimage_multi_pw_aff.
3713 * "mpa" is the function under which the preimage should be taken.
3714 * "space" is the space of "mpa".
3715 * "res" collects the results.
3716 * "fn" computes the preimage for a given map.
3717 * "match" returns true if "fn" can be called.
3719 struct isl_union_map_preimage_mpa_data {
3720 isl_space *space;
3721 isl_multi_pw_aff *mpa;
3722 isl_union_map *res;
3723 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3724 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3725 __isl_take isl_multi_pw_aff *mpa);
3728 /* Call data->fn to compute the preimage of the domain or range of *entry
3729 * under the function represented by data->mpa, provided the domain/range
3730 * space of *entry matches the target space of data->mpa
3731 * (as given by data->match), and add the result to data->res.
3733 static isl_stat preimage_mpa_entry(void **entry, void *user)
3735 int m;
3736 isl_map *map = *entry;
3737 struct isl_union_map_preimage_mpa_data *data = user;
3738 isl_bool empty;
3740 m = data->match(map, data->space);
3741 if (m < 0)
3742 return isl_stat_error;
3743 if (!m)
3744 return isl_stat_ok;
3746 map = isl_map_copy(map);
3747 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3749 empty = isl_map_is_empty(map);
3750 if (empty < 0 || empty) {
3751 isl_map_free(map);
3752 return empty < 0 ? isl_stat_error : isl_stat_ok;
3755 data->res = isl_union_map_add_map(data->res, map);
3757 return isl_stat_ok;
3760 /* Compute the preimage of the domain or range of "umap" under the function
3761 * represented by "mpa".
3762 * In other words, plug in "mpa" in the domain or range of "umap".
3763 * The function "fn" performs the actual preimage computation on a map,
3764 * while "match" determines to which maps the function should be applied.
3766 static __isl_give isl_union_map *preimage_multi_pw_aff(
3767 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3768 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3769 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3770 __isl_take isl_multi_pw_aff *mpa))
3772 isl_ctx *ctx;
3773 isl_space *space;
3774 struct isl_union_map_preimage_mpa_data data;
3776 umap = isl_union_map_align_params(umap,
3777 isl_multi_pw_aff_get_space(mpa));
3778 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3780 if (!umap || !mpa)
3781 goto error;
3783 ctx = isl_union_map_get_ctx(umap);
3784 space = isl_union_map_get_space(umap);
3785 data.space = isl_multi_pw_aff_get_space(mpa);
3786 data.mpa = mpa;
3787 data.res = isl_union_map_alloc(space, umap->table.n);
3788 data.match = match;
3789 data.fn = fn;
3790 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3791 &data) < 0)
3792 data.res = isl_union_map_free(data.res);
3794 isl_space_free(data.space);
3795 isl_union_map_free(umap);
3796 isl_multi_pw_aff_free(mpa);
3797 return data.res;
3798 error:
3799 isl_union_map_free(umap);
3800 isl_multi_pw_aff_free(mpa);
3801 return NULL;
3804 /* Compute the preimage of the domain of "umap" under the function
3805 * represented by "mpa".
3806 * In other words, plug in "mpa" in the domain of "umap".
3807 * The result contains maps that live in the same spaces as the maps of "umap"
3808 * with domain space equal to the target space of "mpa",
3809 * except that the domain has been replaced by the domain space of "mpa".
3811 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3812 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3814 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3815 &isl_map_preimage_domain_multi_pw_aff);
3818 /* Internal data structure for preimage_upma.
3820 * "umap" is the map of which the preimage should be computed.
3821 * "res" collects the results.
3822 * "fn" computes the preimage for a given piecewise multi-affine function.
3824 struct isl_union_map_preimage_upma_data {
3825 isl_union_map *umap;
3826 isl_union_map *res;
3827 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3828 __isl_take isl_pw_multi_aff *pma);
3831 /* Call data->fn to compute the preimage of the domain or range of data->umap
3832 * under the function represented by pma and add the result to data->res.
3834 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3836 struct isl_union_map_preimage_upma_data *data = user;
3837 isl_union_map *umap;
3839 umap = isl_union_map_copy(data->umap);
3840 umap = data->fn(umap, pma);
3841 data->res = isl_union_map_union(data->res, umap);
3843 return data->res ? isl_stat_ok : isl_stat_error;
3846 /* Compute the preimage of the domain or range of "umap" under the function
3847 * represented by "upma".
3848 * In other words, plug in "upma" in the domain or range of "umap".
3849 * The function "fn" performs the actual preimage computation
3850 * on a piecewise multi-affine function.
3852 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3853 __isl_take isl_union_map *umap,
3854 __isl_take isl_union_pw_multi_aff *upma,
3855 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3856 __isl_take isl_pw_multi_aff *pma))
3858 struct isl_union_map_preimage_upma_data data;
3860 data.umap = umap;
3861 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3862 data.fn = fn;
3863 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3864 &preimage_upma, &data) < 0)
3865 data.res = isl_union_map_free(data.res);
3867 isl_union_map_free(umap);
3868 isl_union_pw_multi_aff_free(upma);
3870 return data.res;
3873 /* Compute the preimage of the domain of "umap" under the function
3874 * represented by "upma".
3875 * In other words, plug in "upma" in the domain of "umap".
3876 * The result contains maps that live in the same spaces as the maps of "umap"
3877 * with domain space equal to one of the target spaces of "upma",
3878 * except that the domain has been replaced by one of the domain spaces that
3879 * correspond to that target space of "upma".
3881 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3882 __isl_take isl_union_map *umap,
3883 __isl_take isl_union_pw_multi_aff *upma)
3885 return preimage_union_pw_multi_aff(umap, upma,
3886 &isl_union_map_preimage_domain_pw_multi_aff);
3889 /* Compute the preimage of the range of "umap" under the function
3890 * represented by "upma".
3891 * In other words, plug in "upma" in the range of "umap".
3892 * The result contains maps that live in the same spaces as the maps of "umap"
3893 * with range space equal to one of the target spaces of "upma",
3894 * except that the range has been replaced by one of the domain spaces that
3895 * correspond to that target space of "upma".
3897 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3898 __isl_take isl_union_map *umap,
3899 __isl_take isl_union_pw_multi_aff *upma)
3901 return preimage_union_pw_multi_aff(umap, upma,
3902 &isl_union_map_preimage_range_pw_multi_aff);
3905 /* Compute the preimage of "uset" under the function represented by "upma".
3906 * In other words, plug in "upma" in the range of "uset".
3907 * The result contains sets that live in the same spaces as the sets of "uset"
3908 * with space equal to one of the target spaces of "upma",
3909 * except that the space has been replaced by one of the domain spaces that
3910 * correspond to that target space of "upma".
3912 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3913 __isl_take isl_union_set *uset,
3914 __isl_take isl_union_pw_multi_aff *upma)
3916 return preimage_union_pw_multi_aff(uset, upma,
3917 &isl_union_set_preimage_pw_multi_aff);
3920 /* Reset the user pointer on all identifiers of parameters and tuples
3921 * of the spaces of "umap".
3923 __isl_give isl_union_map *isl_union_map_reset_user(
3924 __isl_take isl_union_map *umap)
3926 umap = isl_union_map_cow(umap);
3927 if (!umap)
3928 return NULL;
3929 umap->dim = isl_space_reset_user(umap->dim);
3930 if (!umap->dim)
3931 return isl_union_map_free(umap);
3932 return total(umap, &isl_map_reset_user);
3935 /* Reset the user pointer on all identifiers of parameters and tuples
3936 * of the spaces of "uset".
3938 __isl_give isl_union_set *isl_union_set_reset_user(
3939 __isl_take isl_union_set *uset)
3941 return isl_union_map_reset_user(uset);
3944 /* Remove all existentially quantified variables and integer divisions
3945 * from "umap" using Fourier-Motzkin elimination.
3947 __isl_give isl_union_map *isl_union_map_remove_divs(
3948 __isl_take isl_union_map *umap)
3950 return total(umap, &isl_map_remove_divs);
3953 /* Remove all existentially quantified variables and integer divisions
3954 * from "uset" using Fourier-Motzkin elimination.
3956 __isl_give isl_union_set *isl_union_set_remove_divs(
3957 __isl_take isl_union_set *uset)
3959 return isl_union_map_remove_divs(uset);
3962 /* Internal data structure for isl_union_map_project_out.
3963 * "type", "first" and "n" are the arguments for the isl_map_project_out
3964 * call.
3965 * "res" collects the results.
3967 struct isl_union_map_project_out_data {
3968 enum isl_dim_type type;
3969 unsigned first;
3970 unsigned n;
3972 isl_union_map *res;
3975 /* Turn the data->n dimensions of type data->type, starting at data->first
3976 * into existentially quantified variables and add the result to data->res.
3978 static isl_stat project_out(__isl_take isl_map *map, void *user)
3980 struct isl_union_map_project_out_data *data = user;
3982 map = isl_map_project_out(map, data->type, data->first, data->n);
3983 data->res = isl_union_map_add_map(data->res, map);
3985 return isl_stat_ok;
3988 /* Turn the "n" dimensions of type "type", starting at "first"
3989 * into existentially quantified variables.
3990 * Since the space of an isl_union_map only contains parameters,
3991 * type is required to be equal to isl_dim_param.
3993 __isl_give isl_union_map *isl_union_map_project_out(
3994 __isl_take isl_union_map *umap,
3995 enum isl_dim_type type, unsigned first, unsigned n)
3997 isl_space *space;
3998 struct isl_union_map_project_out_data data = { type, first, n };
4000 if (!umap)
4001 return NULL;
4003 if (type != isl_dim_param)
4004 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4005 "can only project out parameters",
4006 return isl_union_map_free(umap));
4008 space = isl_union_map_get_space(umap);
4009 space = isl_space_drop_dims(space, type, first, n);
4010 data.res = isl_union_map_empty(space);
4011 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
4012 data.res = isl_union_map_free(data.res);
4014 isl_union_map_free(umap);
4016 return data.res;
4019 #undef TYPE
4020 #define TYPE isl_union_map
4021 #include "isl_project_out_all_params_templ.c"
4023 /* Turn the "n" dimensions of type "type", starting at "first"
4024 * into existentially quantified variables.
4025 * Since the space of an isl_union_set only contains parameters,
4026 * "type" is required to be equal to isl_dim_param.
4028 __isl_give isl_union_set *isl_union_set_project_out(
4029 __isl_take isl_union_set *uset,
4030 enum isl_dim_type type, unsigned first, unsigned n)
4032 return isl_union_map_project_out(uset, type, first, n);
4035 /* Project out all parameters from "uset" by existentially quantifying
4036 * over them.
4038 __isl_give isl_union_set *isl_union_set_project_out_all_params(
4039 __isl_take isl_union_set *uset)
4041 return uset_from_umap(
4042 isl_union_map_project_out_all_params(uset_to_umap(uset)));
4045 /* Internal data structure for isl_union_map_involves_dims.
4046 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
4048 struct isl_union_map_involves_dims_data {
4049 unsigned first;
4050 unsigned n;
4053 /* Does "map" _not_ involve the data->n parameters starting at data->first?
4055 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
4057 struct isl_union_map_involves_dims_data *data = user;
4058 isl_bool involves;
4060 involves = isl_map_involves_dims(map,
4061 isl_dim_param, data->first, data->n);
4062 return isl_bool_not(involves);
4065 /* Does "umap" involve any of the n parameters starting at first?
4066 * "type" is required to be set to isl_dim_param.
4068 * "umap" involves any of those parameters if any of its maps
4069 * involve the parameters. In other words, "umap" does not
4070 * involve any of the parameters if all its maps to not
4071 * involve the parameters.
4073 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
4074 enum isl_dim_type type, unsigned first, unsigned n)
4076 struct isl_union_map_involves_dims_data data = { first, n };
4077 isl_bool excludes;
4079 if (type != isl_dim_param)
4080 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4081 "can only reference parameters", return isl_bool_error);
4083 excludes = union_map_forall_user(umap, &map_excludes, &data);
4085 return isl_bool_not(excludes);
4088 /* Internal data structure for isl_union_map_reset_range_space.
4089 * "range" is the space from which to set the range space.
4090 * "res" collects the results.
4092 struct isl_union_map_reset_range_space_data {
4093 isl_space *range;
4094 isl_union_map *res;
4097 /* Replace the range space of "map" by the range space of data->range and
4098 * add the result to data->res.
4100 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
4102 struct isl_union_map_reset_range_space_data *data = user;
4103 isl_space *space;
4105 space = isl_map_get_space(map);
4106 space = isl_space_domain(space);
4107 space = isl_space_extend_domain_with_range(space,
4108 isl_space_copy(data->range));
4109 map = isl_map_reset_space(map, space);
4110 data->res = isl_union_map_add_map(data->res, map);
4112 return data->res ? isl_stat_ok : isl_stat_error;
4115 /* Replace the range space of all the maps in "umap" by
4116 * the range space of "space".
4118 * This assumes that all maps have the same output dimension.
4119 * This function should therefore not be made publicly available.
4121 * Since the spaces of the maps change, so do their hash value.
4122 * We therefore need to create a new isl_union_map.
4124 __isl_give isl_union_map *isl_union_map_reset_range_space(
4125 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4127 struct isl_union_map_reset_range_space_data data = { space };
4129 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4130 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
4131 data.res = isl_union_map_free(data.res);
4133 isl_space_free(space);
4134 isl_union_map_free(umap);
4135 return data.res;
4138 /* Check that "umap" and "space" have the same number of parameters.
4140 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
4141 __isl_keep isl_space *space)
4143 isl_size dim1, dim2;
4145 dim1 = isl_union_map_dim(umap, isl_dim_param);
4146 dim2 = isl_space_dim(space, isl_dim_param);
4147 if (dim1 < 0 || dim2 < 0)
4148 return isl_stat_error;
4149 if (dim1 == dim2)
4150 return isl_stat_ok;
4151 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4152 "number of parameters does not match", return isl_stat_error);
4155 /* Internal data structure for isl_union_map_reset_equal_dim_space.
4156 * "space" is the target space.
4157 * "res" collects the results.
4159 struct isl_union_map_reset_params_data {
4160 isl_space *space;
4161 isl_union_map *res;
4164 /* Replace the parameters of "map" by those of data->space and
4165 * add the result to data->res.
4167 static isl_stat reset_params(__isl_take isl_map *map, void *user)
4169 struct isl_union_map_reset_params_data *data = user;
4170 isl_space *space;
4172 space = isl_map_get_space(map);
4173 space = isl_space_replace_params(space, data->space);
4174 map = isl_map_reset_equal_dim_space(map, space);
4175 data->res = isl_union_map_add_map(data->res, map);
4177 return data->res ? isl_stat_ok : isl_stat_error;
4180 /* Replace the space of "umap" by "space", without modifying
4181 * the dimension of "umap", i.e., the number of parameters of "umap".
4183 * Since the hash values of the maps in the union map depend
4184 * on the parameters, a new union map needs to be constructed.
4186 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
4187 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4189 struct isl_union_map_reset_params_data data = { space };
4190 isl_bool equal;
4191 isl_space *umap_space;
4193 umap_space = isl_union_map_peek_space(umap);
4194 equal = isl_space_is_equal(umap_space, space);
4195 if (equal < 0)
4196 goto error;
4197 if (equal) {
4198 isl_space_free(space);
4199 return umap;
4201 if (check_union_map_space_equal_dim(umap, space) < 0)
4202 goto error;
4204 data.res = isl_union_map_empty(isl_space_copy(space));
4205 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
4206 data.res = isl_union_map_free(data.res);
4208 isl_space_free(space);
4209 isl_union_map_free(umap);
4210 return data.res;
4211 error:
4212 isl_union_map_free(umap);
4213 isl_space_free(space);
4214 return NULL;
4217 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4218 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4219 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4220 * to the domain and the range of each map.
4221 * "res" collects the results.
4223 struct isl_union_order_at_data {
4224 isl_multi_union_pw_aff *mupa;
4225 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4226 __isl_take isl_multi_pw_aff *mpa2);
4227 isl_union_map *res;
4230 /* Intersect "map" with the result of applying data->order to
4231 * the functions in data->mupa that apply to the domain and the range
4232 * of "map" and add the result to data->res.
4234 static isl_stat order_at(__isl_take isl_map *map, void *user)
4236 struct isl_union_order_at_data *data = user;
4237 isl_space *space;
4238 isl_multi_pw_aff *mpa1, *mpa2;
4239 isl_map *order;
4241 space = isl_space_domain(isl_map_get_space(map));
4242 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4243 space = isl_space_range(isl_map_get_space(map));
4244 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4245 order = data->order(mpa1, mpa2);
4246 map = isl_map_intersect(map, order);
4247 data->res = isl_union_map_add_map(data->res, map);
4249 return data->res ? isl_stat_ok : isl_stat_error;
4252 /* If "mupa" has a non-trivial explicit domain, then intersect
4253 * domain and range of "umap" with this explicit domain.
4254 * If the explicit domain only describes constraints on the parameters,
4255 * then the intersection only needs to be performed once.
4257 static __isl_give isl_union_map *intersect_explicit_domain(
4258 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4260 isl_bool non_trivial, is_params;
4261 isl_union_set *dom;
4263 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4264 if (non_trivial < 0)
4265 return isl_union_map_free(umap);
4266 if (!non_trivial)
4267 return umap;
4268 mupa = isl_multi_union_pw_aff_copy(mupa);
4269 dom = isl_multi_union_pw_aff_domain(mupa);
4270 is_params = isl_union_set_is_params(dom);
4271 if (is_params < 0) {
4272 isl_union_set_free(dom);
4273 return isl_union_map_free(umap);
4275 if (is_params) {
4276 isl_set *set;
4278 set = isl_union_set_params(dom);
4279 umap = isl_union_map_intersect_params(umap, set);
4280 return umap;
4282 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4283 umap = isl_union_map_intersect_range(umap, dom);
4284 return umap;
4287 /* Intersect each map in "umap" with the result of calling "order"
4288 * on the functions is "mupa" that apply to the domain and the range
4289 * of the map.
4291 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4292 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4293 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4294 __isl_take isl_multi_pw_aff *mpa2))
4296 struct isl_union_order_at_data data;
4298 umap = isl_union_map_align_params(umap,
4299 isl_multi_union_pw_aff_get_space(mupa));
4300 mupa = isl_multi_union_pw_aff_align_params(mupa,
4301 isl_union_map_get_space(umap));
4302 umap = intersect_explicit_domain(umap, mupa);
4303 data.mupa = mupa;
4304 data.order = order;
4305 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4306 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4307 data.res = isl_union_map_free(data.res);
4309 isl_multi_union_pw_aff_free(mupa);
4310 isl_union_map_free(umap);
4311 return data.res;
4314 /* Return the subset of "umap" where the domain and the range
4315 * have equal "mupa" values.
4317 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4318 __isl_take isl_union_map *umap,
4319 __isl_take isl_multi_union_pw_aff *mupa)
4321 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4322 &isl_multi_pw_aff_eq_map);
4325 #undef ORDER
4326 #define ORDER le
4327 #include "isl_union_map_lex_templ.c"
4329 #undef ORDER
4330 #define ORDER lt
4331 #include "isl_union_map_lex_templ.c"
4333 #undef ORDER
4334 #define ORDER ge
4335 #include "isl_union_map_lex_templ.c"
4337 #undef ORDER
4338 #define ORDER gt
4339 #include "isl_union_map_lex_templ.c"
4341 /* Return the union of the elements in the list "list".
4343 __isl_give isl_union_set *isl_union_set_list_union(
4344 __isl_take isl_union_set_list *list)
4346 int i;
4347 isl_size n;
4348 isl_ctx *ctx;
4349 isl_space *space;
4350 isl_union_set *res;
4352 n = isl_union_set_list_n_union_set(list);
4353 if (n < 0)
4354 goto error;
4356 ctx = isl_union_set_list_get_ctx(list);
4357 space = isl_space_params_alloc(ctx, 0);
4358 res = isl_union_set_empty(space);
4360 for (i = 0; i < n; ++i) {
4361 isl_union_set *uset_i;
4363 uset_i = isl_union_set_list_get_union_set(list, i);
4364 res = isl_union_set_union(res, uset_i);
4367 isl_union_set_list_free(list);
4368 return res;
4369 error:
4370 isl_union_set_list_free(list);
4371 return NULL;
4374 /* Update *hash with the hash value of "map".
4376 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4378 uint32_t *hash = user;
4379 uint32_t map_hash;
4381 map_hash = isl_map_get_hash(map);
4382 isl_hash_hash(*hash, map_hash);
4384 isl_map_free(map);
4385 return isl_stat_ok;
4388 /* Return a hash value that digests "umap".
4390 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4392 uint32_t hash;
4394 if (!umap)
4395 return 0;
4397 hash = isl_hash_init();
4398 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4399 return 0;
4401 return hash;
4404 /* Return a hash value that digests "uset".
4406 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4408 return isl_union_map_get_hash(uset);
4411 /* Add the number of basic sets in "set" to "n".
4413 static isl_stat add_n(__isl_take isl_set *set, void *user)
4415 int *n = user;
4416 isl_size set_n;
4418 set_n = isl_set_n_basic_set(set);
4419 *n += set_n;
4420 isl_set_free(set);
4422 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4425 /* Return the total number of basic sets in "uset".
4427 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4429 int n = 0;
4431 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4432 return -1;
4434 return n;
4437 /* Add the basic sets in "set" to "list".
4439 static isl_stat add_list(__isl_take isl_set *set, void *user)
4441 isl_basic_set_list **list = user;
4442 isl_basic_set_list *list_i;
4444 list_i = isl_set_get_basic_set_list(set);
4445 *list = isl_basic_set_list_concat(*list, list_i);
4446 isl_set_free(set);
4448 if (!*list)
4449 return isl_stat_error;
4450 return isl_stat_ok;
4453 /* Return a list containing all the basic sets in "uset".
4455 * First construct a list of the appropriate size and
4456 * then add all the elements.
4458 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4459 __isl_keep isl_union_set *uset)
4461 int n;
4462 isl_ctx *ctx;
4463 isl_basic_set_list *list;
4465 if (!uset)
4466 return NULL;
4467 ctx = isl_union_set_get_ctx(uset);
4468 n = isl_union_set_n_basic_set(uset);
4469 if (n < 0)
4470 return NULL;
4471 list = isl_basic_set_list_alloc(ctx, n);
4472 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4473 list = isl_basic_set_list_free(list);
4475 return list;
4478 /* Internal data structure for isl_union_map_remove_map_if.
4479 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4481 struct isl_union_map_remove_map_if_data {
4482 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4483 void *user;
4486 /* isl_un_op_control filter that negates the result of data->fn
4487 * called on "map".
4489 static isl_bool not(__isl_keep isl_map *map, void *user)
4491 struct isl_union_map_remove_map_if_data *data = user;
4493 return isl_bool_not(data->fn(map, data->user));
4496 /* Dummy isl_un_op_control transformation callback that
4497 * simply returns the input.
4499 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4501 return map;
4504 /* Call "fn" on every map in "umap" and remove those maps
4505 * for which the callback returns true.
4507 * Use un_op to keep only those maps that are not filtered out,
4508 * applying an identity transformation on them.
4510 __isl_give isl_union_map *isl_union_map_remove_map_if(
4511 __isl_take isl_union_map *umap,
4512 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4514 struct isl_union_map_remove_map_if_data data = { fn, user };
4515 struct isl_un_op_control control = {
4516 .filter = &not,
4517 .filter_user = &data,
4518 .fn_map = &map_id,
4520 return un_op(umap, &control);
4523 /* Does "map" have "space" as domain (ignoring parameters)?
4525 static isl_bool has_domain_space_tuples(__isl_keep isl_map *map, void *user)
4527 isl_space *space = user;
4529 return isl_space_has_domain_tuples(space, isl_map_peek_space(map));
4532 /* Does "map" have "space" as range (ignoring parameters)?
4534 static isl_bool has_range_space_tuples(__isl_keep isl_map *map, void *user)
4536 isl_space *space = user;
4538 return isl_space_has_range_tuples(space, isl_map_peek_space(map));
4541 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4543 static __isl_give isl_map *bind_range(__isl_take isl_map *map, void *user)
4545 isl_multi_id *tuple = user;
4547 return isl_map_bind_range(map, isl_multi_id_copy(tuple));
4550 /* Bind the output dimensions of "umap" to parameters with identifiers
4551 * specified by "tuple", living in the range space of "umap",
4552 * for those maps that have a matching range space.
4554 __isl_give isl_union_set *isl_union_map_bind_range(
4555 __isl_take isl_union_map *umap, __isl_take isl_multi_id *tuple)
4557 struct isl_un_op_control control = {
4558 .filter = &has_range_space_tuples,
4559 .filter_user = isl_multi_id_peek_space(tuple),
4560 .fn_map2 = &bind_range,
4561 .fn_map2_user = tuple,
4563 isl_union_set *bound;
4565 bound = uset_from_umap(un_op(umap, &control));
4566 isl_multi_id_free(tuple);
4567 return bound;
4570 /* Only keep those elements in "umap" that have a domain in "space".
4572 __isl_give isl_union_map *isl_union_map_intersect_domain_space(
4573 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4575 struct isl_un_op_control control = {
4576 .filter = &has_domain_space_tuples,
4577 .filter_user = space,
4580 umap = un_op(umap, &control);
4581 isl_space_free(space);
4582 return umap;
4585 /* Only keep those elements in "umap" that have a range in "space".
4587 __isl_give isl_union_map *isl_union_map_intersect_range_space(
4588 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4590 struct isl_un_op_control control = {
4591 .filter = &has_range_space_tuples,
4592 .filter_user = space,
4595 umap = un_op(umap, &control);
4596 isl_space_free(space);
4597 return umap;