[AMDGPU] Test codegen'ing True16 additions.
[llvm-project.git] / polly / lib / External / isl / isl_union_map.c
blobeaafe18027037c753a394c7db147c2ba4fd195ba
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/ctx.h>
19 #include <isl/hash.h>
20 #include <isl_aff_private.h>
21 #include <isl/map.h>
22 #include <isl/set.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl_maybe_map.h>
26 #include <isl_id_private.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
31 #include <uset_to_umap.c>
32 #include <uset_from_umap.c>
33 #include <set_list_from_map_list_inl.c>
35 #undef TYPE
36 #define TYPE isl_union_map
37 static
38 #include "has_single_reference_templ.c"
39 static
40 #include "check_single_reference_templ.c"
42 /* Return the number of parameters of "umap", where "type"
43 * is required to be set to isl_dim_param.
45 isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
46 enum isl_dim_type type)
48 if (!umap)
49 return isl_size_error;
51 if (type != isl_dim_param)
52 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
53 "can only reference parameters", return isl_size_error);
55 return isl_space_dim(umap->dim, type);
58 /* Return the number of parameters of "uset", where "type"
59 * is required to be set to isl_dim_param.
61 isl_size isl_union_set_dim(__isl_keep isl_union_set *uset,
62 enum isl_dim_type type)
64 return isl_union_map_dim(uset, type);
67 /* Return the id of the specified dimension.
69 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
70 enum isl_dim_type type, unsigned pos)
72 if (!umap)
73 return NULL;
75 if (type != isl_dim_param)
76 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
77 "can only reference parameters", return NULL);
79 return isl_space_get_dim_id(umap->dim, type, pos);
82 /* Is this union set a parameter domain?
84 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
86 isl_set *set;
87 isl_bool params;
89 if (!uset)
90 return isl_bool_error;
91 if (uset->table.n != 1)
92 return isl_bool_false;
94 set = isl_set_from_union_set(isl_union_set_copy(uset));
95 params = isl_set_is_params(set);
96 isl_set_free(set);
97 return params;
100 /* Is this union map actually a parameter domain?
101 * Users should never call this function. Outside of isl,
102 * a union map can never be a parameter domain.
104 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
106 return isl_union_set_is_params(uset_from_umap(umap));
109 static __isl_give isl_union_map *isl_union_map_alloc(
110 __isl_take isl_space *space, int size)
112 isl_union_map *umap;
114 space = isl_space_params(space);
115 if (!space)
116 return NULL;
118 umap = isl_calloc_type(space->ctx, isl_union_map);
119 if (!umap) {
120 isl_space_free(space);
121 return NULL;
124 umap->ref = 1;
125 umap->dim = space;
126 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
127 return isl_union_map_free(umap);
129 return umap;
132 /* Create an empty union map without specifying any parameters.
134 __isl_give isl_union_map *isl_union_map_empty_ctx(isl_ctx *ctx)
136 return isl_union_map_empty_space(isl_space_unit(ctx));
139 __isl_give isl_union_map *isl_union_map_empty_space(__isl_take isl_space *space)
141 return isl_union_map_alloc(space, 16);
144 /* This is an alternative name for the function above.
146 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
148 return isl_union_map_empty_space(space);
151 /* Create an empty union set without specifying any parameters.
153 __isl_give isl_union_set *isl_union_set_empty_ctx(isl_ctx *ctx)
155 return uset_from_umap(isl_union_map_empty_ctx(ctx));
158 __isl_give isl_union_set *isl_union_set_empty_space(__isl_take isl_space *space)
160 return uset_from_umap(isl_union_map_empty_space(space));
163 /* This is an alternative name for the function above.
165 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
167 return isl_union_set_empty_space(space);
170 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
172 return umap ? umap->dim->ctx : NULL;
175 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
177 return uset ? uset->dim->ctx : NULL;
180 /* Return the space of "umap".
182 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
184 return umap ? umap->dim : NULL;
187 /* Return the space of "uset".
189 __isl_keep isl_space *isl_union_set_peek_space(__isl_keep isl_union_set *uset)
191 return isl_union_map_peek_space(uset_to_umap(uset));
194 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
196 return isl_space_copy(isl_union_map_peek_space(umap));
199 /* Return the position of the parameter with the given name
200 * in "umap".
201 * Return -1 if no such dimension can be found.
203 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
204 enum isl_dim_type type, const char *name)
206 if (!umap)
207 return -1;
208 return isl_space_find_dim_by_name(umap->dim, type, name);
211 /* Return the position of the parameter with id "id" in "umap".
212 * Return -1 if no such dimension can be found.
214 static int isl_union_map_find_dim_by_id(__isl_keep isl_union_map *umap,
215 enum isl_dim_type type, __isl_keep isl_id *id)
217 isl_space *space;
219 space = isl_union_map_peek_space(umap);
220 return isl_space_find_dim_by_id(space, type, id);
223 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
225 return isl_union_map_get_space(uset);
228 static isl_stat free_umap_entry(void **entry, void *user)
230 isl_map *map = *entry;
231 isl_map_free(map);
232 return isl_stat_ok;
235 static isl_stat add_map(__isl_take isl_map *map, void *user)
237 isl_union_map **umap = (isl_union_map **)user;
239 *umap = isl_union_map_add_map(*umap, map);
241 return isl_stat_ok;
244 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
246 isl_union_map *dup;
248 if (!umap)
249 return NULL;
251 dup = isl_union_map_empty(isl_space_copy(umap->dim));
252 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
253 goto error;
254 return dup;
255 error:
256 isl_union_map_free(dup);
257 return NULL;
260 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
262 if (!umap)
263 return NULL;
265 if (umap->ref == 1)
266 return umap;
267 umap->ref--;
268 return isl_union_map_dup(umap);
271 struct isl_union_align {
272 isl_reordering *exp;
273 isl_union_map *res;
276 static isl_stat align_entry(void **entry, void *user)
278 isl_map *map = *entry;
279 isl_reordering *exp;
280 struct isl_union_align *data = user;
282 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
283 isl_map_get_space(map));
285 data->res = isl_union_map_add_map(data->res,
286 isl_map_realign(isl_map_copy(map), exp));
288 return isl_stat_ok;
291 /* Align the parameters of umap along those of model.
292 * The result has the parameters of model first, in the same order
293 * as they appear in model, followed by any remaining parameters of
294 * umap that do not appear in model.
296 __isl_give isl_union_map *isl_union_map_align_params(
297 __isl_take isl_union_map *umap, __isl_take isl_space *model)
299 struct isl_union_align data = { NULL, NULL };
300 isl_space *space;
301 isl_bool equal_params;
303 space = isl_union_map_peek_space(umap);
304 equal_params = isl_space_has_equal_params(space, model);
305 if (equal_params < 0)
306 goto error;
307 if (equal_params) {
308 isl_space_free(model);
309 return umap;
312 data.exp = isl_parameter_alignment_reordering(space, model);
313 if (!data.exp)
314 goto error;
316 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
317 umap->table.n);
318 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
319 &align_entry, &data) < 0)
320 goto error;
322 isl_reordering_free(data.exp);
323 isl_union_map_free(umap);
324 isl_space_free(model);
325 return data.res;
326 error:
327 isl_reordering_free(data.exp);
328 isl_union_map_free(umap);
329 isl_union_map_free(data.res);
330 isl_space_free(model);
331 return NULL;
334 __isl_give isl_union_set *isl_union_set_align_params(
335 __isl_take isl_union_set *uset, __isl_take isl_space *model)
337 return isl_union_map_align_params(uset, model);
340 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
341 __isl_take isl_union_map *umap2)
343 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
344 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
346 umap1 = isl_union_map_cow(umap1);
348 if (!umap1 || !umap2)
349 goto error;
351 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
352 goto error;
354 isl_union_map_free(umap2);
356 return umap1;
357 error:
358 isl_union_map_free(umap1);
359 isl_union_map_free(umap2);
360 return NULL;
363 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
364 __isl_take isl_union_set *uset2)
366 return isl_union_map_union(uset1, uset2);
369 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
371 if (!umap)
372 return NULL;
374 umap->ref++;
375 return umap;
378 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
380 return isl_union_map_copy(uset);
383 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
385 if (!umap)
386 return NULL;
388 if (--umap->ref > 0)
389 return NULL;
391 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
392 &free_umap_entry, NULL);
393 isl_hash_table_clear(&umap->table);
394 isl_space_free(umap->dim);
395 free(umap);
396 return NULL;
399 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
401 return isl_union_map_free(uset);
404 /* Do "umap" and "space" have the same parameters?
406 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
407 __isl_keep isl_space *space)
409 isl_space *umap_space;
411 umap_space = isl_union_map_peek_space(umap);
412 return isl_space_has_equal_params(umap_space, space);
415 /* Do "uset" and "space" have the same parameters?
417 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
418 __isl_keep isl_space *space)
420 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
423 /* Is the space of the map at "entry" equal to "space", ignoring parameters?
425 static isl_bool has_space_tuples(const void *entry, const void *val)
427 isl_map *map = (isl_map *)entry;
428 isl_space *space = (isl_space *) val;
430 return isl_map_has_space_tuples(map, space);
433 /* Find the entry in "umap" with space "space" (ignoring parameters),
434 * returning isl_hash_table_entry_none if no such entry appears in "umap" and
435 * NULL on error.
436 * If "reserve" is set, then an entry is created if it does
437 * not exist already. Since this modifies the hash table in-place,
438 * this means "umap" must have a single reference when "reserve" is set.
440 static struct isl_hash_table_entry *isl_union_map_find_entry(
441 __isl_keep isl_union_map *umap, __isl_keep isl_space *space,
442 int reserve)
444 uint32_t hash;
446 if (!umap || !space)
447 return NULL;
448 if (reserve && isl_union_map_check_single_reference(umap) < 0)
449 return NULL;
451 hash = isl_space_get_tuple_hash(space);
452 return isl_hash_table_find(isl_union_map_get_ctx(umap), &umap->table,
453 hash, &has_space_tuples, space, reserve);
456 /* Find the entry in "uset" with space "space" (ignoring parameters),
457 * returning isl_hash_table_entry_none if no such entry appears in "uset" and
458 * NULL on error.
459 * If "reserve" is set, then an entry is created if it does
460 * not exist already. In this case, a NULL return indicates an error.
462 struct isl_hash_table_entry *isl_union_set_find_entry(
463 __isl_keep isl_union_set *uset, __isl_keep isl_space *space,
464 int reserve)
466 return isl_union_map_find_entry(uset_to_umap(uset), space, reserve);
469 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
470 __isl_take isl_map *map)
472 struct isl_hash_table_entry *entry;
473 isl_bool aligned;
474 isl_space *space;
476 if (!map || !umap)
477 goto error;
479 if (isl_map_plain_is_empty(map)) {
480 isl_map_free(map);
481 return umap;
484 aligned = isl_map_space_has_equal_params(map, umap->dim);
485 if (aligned < 0)
486 goto error;
487 if (!aligned) {
488 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
489 map = isl_map_align_params(map, isl_union_map_get_space(umap));
492 umap = isl_union_map_cow(umap);
494 space = isl_map_peek_space(map);
495 entry = isl_union_map_find_entry(umap, space, 1);
496 if (!entry)
497 goto error;
499 if (!entry->data)
500 entry->data = map;
501 else {
502 entry->data = isl_map_union(entry->data, isl_map_copy(map));
503 if (!entry->data)
504 goto error;
505 isl_map_free(map);
508 return umap;
509 error:
510 isl_map_free(map);
511 isl_union_map_free(umap);
512 return NULL;
515 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
516 __isl_take isl_set *set)
518 return isl_union_map_add_map(uset, set_to_map(set));
521 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
523 isl_space *space;
524 isl_union_map *umap;
526 if (!map)
527 return NULL;
529 space = isl_map_get_space(map);
530 space = isl_space_params(space);
531 umap = isl_union_map_empty(space);
532 umap = isl_union_map_add_map(umap, map);
534 return umap;
537 /* This function performs the same operation as isl_union_map_from_map,
538 * but is considered as a function on an isl_map when exported.
540 __isl_give isl_union_map *isl_map_to_union_map(__isl_take isl_map *map)
542 return isl_union_map_from_map(map);
545 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
547 return isl_union_map_from_map(set_to_map(set));
550 /* This function performs the same operation as isl_union_set_from_set,
551 * but is considered as a function on an isl_set when exported.
553 __isl_give isl_union_set *isl_set_to_union_set(__isl_take isl_set *set)
555 return isl_union_set_from_set(set);
558 __isl_give isl_union_map *isl_union_map_from_basic_map(
559 __isl_take isl_basic_map *bmap)
561 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
564 __isl_give isl_union_set *isl_union_set_from_basic_set(
565 __isl_take isl_basic_set *bset)
567 return isl_union_map_from_basic_map(bset);
570 struct isl_union_map_foreach_data
572 isl_stat (*fn)(__isl_take isl_map *map, void *user);
573 void *user;
576 static isl_stat call_on_copy(void **entry, void *user)
578 isl_map *map = *entry;
579 struct isl_union_map_foreach_data *data;
580 data = (struct isl_union_map_foreach_data *)user;
582 return data->fn(isl_map_copy(map), data->user);
585 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
587 return umap ? umap->table.n : isl_size_error;
590 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
592 return uset ? uset->table.n : isl_size_error;
595 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
596 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
598 struct isl_union_map_foreach_data data = { fn, user };
600 if (!umap)
601 return isl_stat_error;
603 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
604 &call_on_copy, &data);
607 /* Internal data structure for isl_union_map_every_map.
609 * "test" is the user-specified callback function.
610 * "user" is the user-specified callback function argument.
612 * "failed" is initialized to 0 and set to 1 if "test" fails
613 * on any map.
615 struct isl_union_map_every_data {
616 isl_bool (*test)(__isl_keep isl_map *map, void *user);
617 void *user;
618 int failed;
621 /* Call data->test on "map".
622 * If this fails, then set data->failed and abort.
624 static isl_stat call_every(void **entry, void *user)
626 isl_map *map = *entry;
627 struct isl_union_map_every_data *data = user;
628 isl_bool r;
630 r = data->test(map, data->user);
631 if (r < 0)
632 return isl_stat_error;
633 if (r)
634 return isl_stat_ok;
635 data->failed = 1;
636 return isl_stat_error;
639 /* Does "test" succeed on every map in "umap"?
641 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
642 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
644 struct isl_union_map_every_data data = { test, user, 0 };
645 isl_stat r;
647 if (!umap)
648 return isl_bool_error;
650 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
651 &call_every, &data);
652 if (r >= 0)
653 return isl_bool_true;
654 if (data.failed)
655 return isl_bool_false;
656 return isl_bool_error;
659 /* Add "map" to "list".
661 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
663 isl_map_list **list = user;
665 *list = isl_map_list_add(*list, map);
667 if (!*list)
668 return isl_stat_error;
669 return isl_stat_ok;
672 /* Return the maps in "umap" as a list.
674 * First construct a list of the appropriate size and then add all the
675 * elements.
677 __isl_give isl_map_list *isl_union_map_get_map_list(
678 __isl_keep isl_union_map *umap)
680 isl_size n_maps;
681 isl_ctx *ctx;
682 isl_map_list *list;
684 n_maps = isl_union_map_n_map(umap);
685 if (n_maps < 0)
686 return NULL;
687 ctx = isl_union_map_get_ctx(umap);
688 list = isl_map_list_alloc(ctx, n_maps);
690 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
691 list = isl_map_list_free(list);
693 return list;
696 /* Return the sets in "uset" as a list.
698 __isl_give isl_set_list *isl_union_set_get_set_list(
699 __isl_keep isl_union_set *uset)
701 return set_list_from_map_list(
702 isl_union_map_get_map_list(uset_to_umap(uset)));
705 /* Can "umap" be converted to an isl_map?
706 * That is, does it contain elements in exactly one space?
708 isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
710 isl_size n;
712 n = isl_union_map_n_map(umap);
713 if (n < 0)
714 return isl_bool_error;
715 return isl_bool_ok(n == 1);
718 /* Can "uset" be converted to an isl_set?
719 * That is, does it contain elements in exactly one space?
721 isl_bool isl_union_set_isa_set(__isl_keep isl_union_set *uset)
723 return isl_union_map_isa_map(uset_to_umap(uset));
726 static isl_stat copy_map(void **entry, void *user)
728 isl_map *map = *entry;
729 isl_map **map_p = user;
731 *map_p = isl_map_copy(map);
733 return isl_stat_error;
736 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
738 isl_bool is_map;
739 isl_ctx *ctx;
740 isl_map *map = NULL;
742 is_map = isl_union_map_isa_map(umap);
743 if (is_map < 0)
744 goto error;
745 ctx = isl_union_map_get_ctx(umap);
746 if (!is_map)
747 isl_die(ctx, isl_error_invalid,
748 "union map needs to contain elements in exactly "
749 "one space", goto error);
751 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
753 isl_union_map_free(umap);
755 return map;
756 error:
757 isl_union_map_free(umap);
758 return NULL;
761 /* This function performs the same operation as isl_map_from_union_map,
762 * but is considered as a function on an isl_union_map when exported.
764 __isl_give isl_map *isl_union_map_as_map(__isl_take isl_union_map *umap)
766 return isl_map_from_union_map(umap);
769 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
771 return isl_map_from_union_map(uset);
774 /* This function performs the same operation as isl_set_from_union_set,
775 * but is considered as a function on an isl_union_set when exported.
777 __isl_give isl_set *isl_union_set_as_set(__isl_take isl_union_set *uset)
779 return isl_set_from_union_set(uset);
782 /* Extract the map in "umap" that lives in the given space (ignoring
783 * parameters).
785 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
786 __isl_take isl_space *space)
788 struct isl_hash_table_entry *entry;
790 entry = isl_union_map_find_entry(umap, space, 0);
791 if (!entry)
792 goto error;
793 if (entry == isl_hash_table_entry_none)
794 return isl_map_empty(space);
795 isl_space_free(space);
796 return isl_map_copy(entry->data);
797 error:
798 isl_space_free(space);
799 return NULL;
802 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
803 __isl_take isl_space *space)
805 return set_from_map(isl_union_map_extract_map(uset, space));
808 /* Check if umap contains a map in the given space (ignoring parameters).
810 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
811 __isl_keep isl_space *space)
813 struct isl_hash_table_entry *entry;
815 space = isl_space_drop_all_params(isl_space_copy(space));
816 space = isl_space_align_params(space, isl_union_map_get_space(umap));
817 entry = isl_union_map_find_entry(umap, space, 0);
818 isl_space_free(space);
819 if (!entry)
820 return isl_bool_error;
821 return isl_bool_ok(entry != isl_hash_table_entry_none);
824 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
825 __isl_keep isl_space *space)
827 return isl_union_map_contains(uset, space);
830 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
831 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
833 return isl_union_map_foreach_map(uset,
834 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
837 /* Internal data structure for isl_union_set_every_set.
839 * "test" is the user-specified callback function.
840 * "user" is the user-specified callback function argument.
842 struct isl_test_set_from_map_data {
843 isl_bool (*test)(__isl_keep isl_set *set, void *user);
844 void *user;
847 /* Call data->test on "map", which is part of an isl_union_set and
848 * therefore known to be an isl_set.
850 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
852 struct isl_test_set_from_map_data *data = user;
854 return data->test(set_from_map(map), data->user);
857 /* Does "test" succeed on every set in "uset"?
859 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
860 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
862 struct isl_test_set_from_map_data data = { test, user };
864 return isl_union_map_every_map(uset_to_umap(uset),
865 &test_set_from_map, &data);
868 struct isl_union_set_foreach_point_data {
869 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
870 void *user;
873 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
875 struct isl_union_set_foreach_point_data *data = user;
876 isl_stat r;
878 r = isl_set_foreach_point(set, data->fn, data->user);
879 isl_set_free(set);
881 return r;
884 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
885 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
887 struct isl_union_set_foreach_point_data data = { fn, user };
888 return isl_union_set_foreach_set(uset, &foreach_point, &data);
891 /* Data structure that specifies how gen_bin_op should
892 * construct results from the inputs.
894 * If "subtract" is set, then a map in the first input is copied to the result
895 * if there is no corresponding map in the second input.
896 * Otherwise, a map in the first input with no corresponding map
897 * in the second input is ignored.
898 * If "filter" is not NULL, then it specifies which maps in the first
899 * input may have a matching map in the second input.
900 * In particular, it makes sure that "match_space" can be called
901 * on the space of the map.
902 * "match_space" specifies how to transform the space of a map
903 * in the first input to the space of the corresponding map
904 * in the second input.
905 * "fn_map" specifies how the matching maps, one from each input,
906 * should be combined to form a map in the result.
908 struct isl_bin_op_control {
909 int subtract;
910 isl_bool (*filter)(__isl_keep isl_map *map);
911 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
912 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
913 __isl_take isl_map *map2);
916 /* Internal data structure for gen_bin_op.
917 * "control" specifies how the maps in the result should be constructed.
918 * "umap2" is a pointer to the second argument.
919 * "res" collects the results.
921 struct isl_union_map_gen_bin_data {
922 struct isl_bin_op_control *control;
923 isl_union_map *umap2;
924 isl_union_map *res;
927 /* Add a copy of "map" to "res" and return the result.
929 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
930 __isl_keep isl_map *map)
932 return isl_union_map_add_map(res, isl_map_copy(map));
935 /* Combine "map1" and "map2", add the result to "res" and return the result.
936 * Check whether the result is empty before adding it to "res".
938 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
939 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
940 struct isl_union_map_gen_bin_data *data)
942 isl_bool empty;
943 isl_map *map;
945 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
946 empty = isl_map_is_empty(map);
947 if (empty < 0 || empty) {
948 isl_map_free(map);
949 if (empty < 0)
950 return isl_union_map_free(res);
951 return res;
953 return isl_union_map_add_map(res, map);
956 /* Dummy match_space function that simply returns the input space.
958 static __isl_give isl_space *identity(__isl_take isl_space *space)
960 return space;
963 /* Look for the map in data->umap2 that corresponds to "map", if any.
964 * Return (isl_bool_true, matching map) if there is one,
965 * (isl_bool_false, NULL) if there is no matching map and
966 * (isl_bool_error, NULL) on error.
968 * If not NULL, then data->control->filter specifies whether "map"
969 * can have any matching map. If so,
970 * data->control->match_space specifies which map in data->umap2
971 * corresponds to "map".
973 static __isl_keep isl_maybe_isl_map bin_try_get_match(
974 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
976 struct isl_hash_table_entry *entry2;
977 isl_space *space;
978 isl_maybe_isl_map res = { isl_bool_error, NULL };
980 if (data->control->filter) {
981 res.valid = data->control->filter(map);
982 if (res.valid < 0 || !res.valid)
983 return res;
984 res.valid = isl_bool_error;
987 space = isl_map_get_space(map);
988 if (data->control->match_space != &identity)
989 space = data->control->match_space(space);
990 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
991 isl_space_free(space);
992 if (entry2)
993 res.valid = isl_bool_ok(entry2 != isl_hash_table_entry_none);
994 if (res.valid >= 0 && res.valid)
995 res.value = entry2->data;
997 return res;
1000 /* isl_hash_table_foreach callback for gen_bin_op.
1001 * Look for the map in data->umap2 that corresponds
1002 * to the map that "entry" points to, apply the binary operation and
1003 * add the result to data->res.
1005 * If no corresponding map can be found, then the effect depends
1006 * on data->control->subtract. If it is set, then the current map
1007 * is added directly to the result. Otherwise, it is ignored.
1009 static isl_stat gen_bin_entry(void **entry, void *user)
1011 struct isl_union_map_gen_bin_data *data = user;
1012 isl_map *map = *entry;
1013 isl_maybe_isl_map m;
1015 m = bin_try_get_match(data, map);
1016 if (m.valid < 0)
1017 return isl_stat_error;
1018 if (!m.valid && !data->control->subtract)
1019 return isl_stat_ok;
1021 if (!m.valid)
1022 data->res = bin_add_map(data->res, map);
1023 else
1024 data->res = bin_add_pair(data->res, map, m.value, data);
1025 if (!data->res)
1026 return isl_stat_error;
1028 return isl_stat_ok;
1031 /* Apply a binary operation to "umap1" and "umap2" based on "control".
1032 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
1033 * in gen_bin_entry.
1035 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
1036 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
1038 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
1040 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1041 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1043 if (!umap1 || !umap2)
1044 goto error;
1046 data.umap2 = umap2;
1047 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1048 umap1->table.n);
1049 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1050 &gen_bin_entry, &data) < 0)
1051 goto error;
1053 isl_union_map_free(umap1);
1054 isl_union_map_free(umap2);
1055 return data.res;
1056 error:
1057 isl_union_map_free(umap1);
1058 isl_union_map_free(umap2);
1059 isl_union_map_free(data.res);
1060 return NULL;
1063 __isl_give isl_union_map *isl_union_map_subtract(
1064 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1066 struct isl_bin_op_control control = {
1067 .subtract = 1,
1068 .match_space = &identity,
1069 .fn_map = &isl_map_subtract,
1072 return gen_bin_op(umap1, umap2, &control);
1075 __isl_give isl_union_set *isl_union_set_subtract(
1076 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1078 return isl_union_map_subtract(uset1, uset2);
1081 struct isl_union_map_gen_bin_set_data {
1082 isl_set *set;
1083 isl_union_map *res;
1086 static isl_stat intersect_params_entry(void **entry, void *user)
1088 struct isl_union_map_gen_bin_set_data *data = user;
1089 isl_map *map = *entry;
1090 int empty;
1092 map = isl_map_copy(map);
1093 map = isl_map_intersect_params(map, isl_set_copy(data->set));
1095 empty = isl_map_is_empty(map);
1096 if (empty < 0) {
1097 isl_map_free(map);
1098 return isl_stat_error;
1101 data->res = isl_union_map_add_map(data->res, map);
1103 return isl_stat_ok;
1106 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
1107 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
1109 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
1111 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1112 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1114 if (!umap || !set)
1115 goto error;
1117 data.set = set;
1118 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1119 umap->table.n);
1120 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1121 fn, &data) < 0)
1122 goto error;
1124 isl_union_map_free(umap);
1125 isl_set_free(set);
1126 return data.res;
1127 error:
1128 isl_union_map_free(umap);
1129 isl_set_free(set);
1130 isl_union_map_free(data.res);
1131 return NULL;
1134 /* Intersect "umap" with the parameter domain "set".
1136 * If "set" does not have any constraints, then we can return immediately.
1138 __isl_give isl_union_map *isl_union_map_intersect_params(
1139 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1141 int is_universe;
1143 is_universe = isl_set_plain_is_universe(set);
1144 if (is_universe < 0)
1145 goto error;
1146 if (is_universe) {
1147 isl_set_free(set);
1148 return umap;
1151 return gen_bin_set_op(umap, set, &intersect_params_entry);
1152 error:
1153 isl_union_map_free(umap);
1154 isl_set_free(set);
1155 return NULL;
1158 __isl_give isl_union_set *isl_union_set_intersect_params(
1159 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1161 return isl_union_map_intersect_params(uset, set);
1164 static __isl_give isl_union_map *union_map_intersect_params(
1165 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1167 return isl_union_map_intersect_params(umap,
1168 isl_set_from_union_set(uset));
1171 static __isl_give isl_union_map *union_map_gist_params(
1172 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1174 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1177 struct isl_union_map_match_bin_data {
1178 isl_union_map *umap2;
1179 isl_union_map *res;
1180 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1183 static isl_stat match_bin_entry(void **entry, void *user)
1185 struct isl_union_map_match_bin_data *data = user;
1186 struct isl_hash_table_entry *entry2;
1187 isl_space *space;
1188 isl_map *map = *entry;
1189 int empty;
1191 space = isl_map_peek_space(map);
1192 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
1193 if (!entry2)
1194 return isl_stat_error;
1195 if (entry2 == isl_hash_table_entry_none)
1196 return isl_stat_ok;
1198 map = isl_map_copy(map);
1199 map = data->fn(map, isl_map_copy(entry2->data));
1201 empty = isl_map_is_empty(map);
1202 if (empty < 0) {
1203 isl_map_free(map);
1204 return isl_stat_error;
1206 if (empty) {
1207 isl_map_free(map);
1208 return isl_stat_ok;
1211 data->res = isl_union_map_add_map(data->res, map);
1213 return isl_stat_ok;
1216 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1217 __isl_take isl_union_map *umap2,
1218 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1220 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1222 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1223 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1225 if (!umap1 || !umap2)
1226 goto error;
1228 data.umap2 = umap2;
1229 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1230 umap1->table.n);
1231 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1232 &match_bin_entry, &data) < 0)
1233 goto error;
1235 isl_union_map_free(umap1);
1236 isl_union_map_free(umap2);
1237 return data.res;
1238 error:
1239 isl_union_map_free(umap1);
1240 isl_union_map_free(umap2);
1241 isl_union_map_free(data.res);
1242 return NULL;
1245 __isl_give isl_union_map *isl_union_map_intersect(
1246 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1248 return match_bin_op(umap1, umap2, &isl_map_intersect);
1251 /* Compute the intersection of the two union_sets.
1252 * As a special case, if exactly one of the two union_sets
1253 * is a parameter domain, then intersect the parameter domain
1254 * of the other one with this set.
1256 __isl_give isl_union_set *isl_union_set_intersect(
1257 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1259 int p1, p2;
1261 p1 = isl_union_set_is_params(uset1);
1262 p2 = isl_union_set_is_params(uset2);
1263 if (p1 < 0 || p2 < 0)
1264 goto error;
1265 if (!p1 && p2)
1266 return union_map_intersect_params(uset1, uset2);
1267 if (p1 && !p2)
1268 return union_map_intersect_params(uset2, uset1);
1269 return isl_union_map_intersect(uset1, uset2);
1270 error:
1271 isl_union_set_free(uset1);
1272 isl_union_set_free(uset2);
1273 return NULL;
1276 static isl_stat gist_params_entry(void **entry, void *user)
1278 struct isl_union_map_gen_bin_set_data *data = user;
1279 isl_map *map = *entry;
1280 int empty;
1282 map = isl_map_copy(map);
1283 map = isl_map_gist_params(map, isl_set_copy(data->set));
1285 empty = isl_map_is_empty(map);
1286 if (empty < 0) {
1287 isl_map_free(map);
1288 return isl_stat_error;
1291 data->res = isl_union_map_add_map(data->res, map);
1293 return isl_stat_ok;
1296 __isl_give isl_union_map *isl_union_map_gist_params(
1297 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1299 return gen_bin_set_op(umap, set, &gist_params_entry);
1302 __isl_give isl_union_set *isl_union_set_gist_params(
1303 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1305 return isl_union_map_gist_params(uset, set);
1308 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1309 __isl_take isl_union_map *context)
1311 return match_bin_op(umap, context, &isl_map_gist);
1314 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1315 __isl_take isl_union_set *context)
1317 if (isl_union_set_is_params(context))
1318 return union_map_gist_params(uset, context);
1319 return isl_union_map_gist(uset, context);
1322 /* For each map in "umap", remove the constraints in the corresponding map
1323 * of "context".
1324 * Each map 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_map *isl_union_map_plain_gist(
1328 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1330 return match_bin_op(umap, context, &isl_map_plain_gist);
1333 /* For each set in "uset", remove the constraints in the corresponding set
1334 * of "context".
1335 * Each set in "context" is assumed to consist of a single disjunct and
1336 * to have explicit representations for all local variables.
1338 __isl_give isl_union_set *isl_union_set_plain_gist(
1339 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1341 return isl_union_map_plain_gist(uset, context);
1344 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1345 __isl_take isl_map *set2)
1347 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1350 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1351 __isl_take isl_map *set2)
1353 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1356 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1357 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1359 return match_bin_op(uset1, uset2, &lex_lt_set);
1362 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1363 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1365 return match_bin_op(uset1, uset2, &lex_le_set);
1368 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1369 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1371 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1374 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1375 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1377 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1380 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1381 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1383 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1386 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1387 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1389 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1392 /* Intersect the domain of "umap" with "uset".
1394 static __isl_give isl_union_map *union_map_intersect_domain(
1395 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1397 struct isl_bin_op_control control = {
1398 .match_space = &isl_space_domain,
1399 .fn_map = &isl_map_intersect_domain,
1402 return gen_bin_op(umap, uset, &control);
1405 /* Intersect the domain of "umap" with "uset".
1406 * If "uset" is a parameters domain, then intersect the parameter
1407 * domain of "umap" with this set.
1409 __isl_give isl_union_map *isl_union_map_intersect_domain_union_set(
1410 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1412 if (isl_union_set_is_params(uset))
1413 return union_map_intersect_params(umap, uset);
1414 else
1415 return union_map_intersect_domain(umap, uset);
1418 /* This is an alternative name for the function above.
1420 __isl_give isl_union_map *isl_union_map_intersect_domain(
1421 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1423 return isl_union_map_intersect_domain_union_set(umap, uset);
1426 /* Remove the elements of "uset" from the domain of "umap".
1428 __isl_give isl_union_map *isl_union_map_subtract_domain(
1429 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1431 struct isl_bin_op_control control = {
1432 .subtract = 1,
1433 .match_space = &isl_space_domain,
1434 .fn_map = &isl_map_subtract_domain,
1437 return gen_bin_op(umap, dom, &control);
1440 /* Remove the elements of "uset" from the range of "umap".
1442 __isl_give isl_union_map *isl_union_map_subtract_range(
1443 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1445 struct isl_bin_op_control control = {
1446 .subtract = 1,
1447 .match_space = &isl_space_range,
1448 .fn_map = &isl_map_subtract_range,
1451 return gen_bin_op(umap, dom, &control);
1454 /* Compute the gist of "umap" with respect to the domain "uset".
1456 static __isl_give isl_union_map *union_map_gist_domain(
1457 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1459 struct isl_bin_op_control control = {
1460 .match_space = &isl_space_domain,
1461 .fn_map = &isl_map_gist_domain,
1464 return gen_bin_op(umap, uset, &control);
1467 /* Compute the gist of "umap" with respect to the domain "uset".
1468 * If "uset" is a parameters domain, then compute the gist
1469 * with respect to this parameter domain.
1471 __isl_give isl_union_map *isl_union_map_gist_domain(
1472 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1474 if (isl_union_set_is_params(uset))
1475 return union_map_gist_params(umap, uset);
1476 else
1477 return union_map_gist_domain(umap, uset);
1480 /* Compute the gist of "umap" with respect to the range "uset".
1482 __isl_give isl_union_map *isl_union_map_gist_range(
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_gist_range,
1490 return gen_bin_op(umap, uset, &control);
1493 __isl_give isl_union_map *isl_union_map_intersect_range_union_set(
1494 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1496 struct isl_bin_op_control control = {
1497 .match_space = &isl_space_range,
1498 .fn_map = &isl_map_intersect_range,
1501 return gen_bin_op(umap, uset, &control);
1504 /* This is an alternative name for the function above.
1506 __isl_give isl_union_map *isl_union_map_intersect_range(
1507 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1509 return isl_union_map_intersect_range_union_set(umap, uset);
1512 /* Intersect each map in "umap" in a space [A -> B] -> C
1513 * with the corresponding map in "factor" in the space A -> C and
1514 * collect the results.
1516 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_domain(
1517 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1519 struct isl_bin_op_control control = {
1520 .filter = &isl_map_domain_is_wrapping,
1521 .match_space = &isl_space_domain_factor_domain,
1522 .fn_map = &isl_map_intersect_domain_factor_domain,
1525 return gen_bin_op(umap, factor, &control);
1528 /* Intersect each map in "umap" in a space [A -> B] -> C
1529 * with the corresponding map in "factor" in the space B -> C and
1530 * collect the results.
1532 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_range(
1533 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1535 struct isl_bin_op_control control = {
1536 .filter = &isl_map_domain_is_wrapping,
1537 .match_space = &isl_space_domain_factor_range,
1538 .fn_map = &isl_map_intersect_domain_factor_range,
1541 return gen_bin_op(umap, factor, &control);
1544 /* Intersect each map in "umap" in a space A -> [B -> C]
1545 * with the corresponding map in "factor" in the space A -> B and
1546 * collect the results.
1548 __isl_give isl_union_map *isl_union_map_intersect_range_factor_domain(
1549 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1551 struct isl_bin_op_control control = {
1552 .filter = &isl_map_range_is_wrapping,
1553 .match_space = &isl_space_range_factor_domain,
1554 .fn_map = &isl_map_intersect_range_factor_domain,
1557 return gen_bin_op(umap, factor, &control);
1560 /* Intersect each map in "umap" in a space A -> [B -> C]
1561 * with the corresponding map in "factor" in the space A -> C and
1562 * collect the results.
1564 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1565 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1567 struct isl_bin_op_control control = {
1568 .filter = &isl_map_range_is_wrapping,
1569 .match_space = &isl_space_range_factor_range,
1570 .fn_map = &isl_map_intersect_range_factor_range,
1573 return gen_bin_op(umap, factor, &control);
1576 struct isl_union_map_bin_data {
1577 isl_union_map *umap2;
1578 isl_union_map *res;
1579 isl_map *map;
1580 isl_stat (*fn)(void **entry, void *user);
1583 static isl_stat apply_range_entry(void **entry, void *user)
1585 struct isl_union_map_bin_data *data = user;
1586 isl_map *map2 = *entry;
1587 isl_bool empty, match;
1589 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1590 map2, isl_dim_in);
1591 if (match < 0)
1592 return isl_stat_error;
1593 if (!match)
1594 return isl_stat_ok;
1596 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1598 empty = isl_map_is_empty(map2);
1599 if (empty < 0) {
1600 isl_map_free(map2);
1601 return isl_stat_error;
1603 if (empty) {
1604 isl_map_free(map2);
1605 return isl_stat_ok;
1608 data->res = isl_union_map_add_map(data->res, map2);
1610 return isl_stat_ok;
1613 static isl_stat bin_entry(void **entry, void *user)
1615 struct isl_union_map_bin_data *data = user;
1616 isl_map *map = *entry;
1618 data->map = map;
1619 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1620 data->fn, data) < 0)
1621 return isl_stat_error;
1623 return isl_stat_ok;
1626 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1627 __isl_take isl_union_map *umap2,
1628 isl_stat (*fn)(void **entry, void *user))
1630 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1632 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1633 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1635 if (!umap1 || !umap2)
1636 goto error;
1638 data.umap2 = umap2;
1639 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1640 umap1->table.n);
1641 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1642 &bin_entry, &data) < 0)
1643 goto error;
1645 isl_union_map_free(umap1);
1646 isl_union_map_free(umap2);
1647 return data.res;
1648 error:
1649 isl_union_map_free(umap1);
1650 isl_union_map_free(umap2);
1651 isl_union_map_free(data.res);
1652 return NULL;
1655 /* Intersect each map in "umap" in a space [A -> B] -> C
1656 * with the corresponding set in "domain" in the space A and
1657 * collect the results.
1659 __isl_give isl_union_map *
1660 isl_union_map_intersect_domain_wrapped_domain_union_set(
1661 __isl_take isl_union_map *umap, __isl_take isl_union_set *domain)
1663 struct isl_bin_op_control control = {
1664 .filter = &isl_map_domain_is_wrapping,
1665 .match_space = &isl_space_domain_wrapped_domain,
1666 .fn_map = &isl_map_intersect_domain_wrapped_domain,
1669 return gen_bin_op(umap, domain, &control);
1672 /* Intersect each map in "umap" in a space A -> [B -> C]
1673 * with the corresponding set in "domain" in the space B and
1674 * collect the results.
1676 __isl_give isl_union_map *
1677 isl_union_map_intersect_range_wrapped_domain_union_set(
1678 __isl_take isl_union_map *umap, __isl_take isl_union_set *domain)
1680 struct isl_bin_op_control control = {
1681 .filter = &isl_map_range_is_wrapping,
1682 .match_space = &isl_space_range_wrapped_domain,
1683 .fn_map = &isl_map_intersect_range_wrapped_domain,
1686 return gen_bin_op(umap, domain, &control);
1689 __isl_give isl_union_map *isl_union_map_apply_range(
1690 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1692 return bin_op(umap1, umap2, &apply_range_entry);
1695 __isl_give isl_union_map *isl_union_map_apply_domain(
1696 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1698 umap1 = isl_union_map_reverse(umap1);
1699 umap1 = isl_union_map_apply_range(umap1, umap2);
1700 return isl_union_map_reverse(umap1);
1703 __isl_give isl_union_set *isl_union_set_apply(
1704 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1706 return isl_union_map_apply_range(uset, umap);
1709 static isl_stat map_lex_lt_entry(void **entry, void *user)
1711 struct isl_union_map_bin_data *data = user;
1712 isl_map *map2 = *entry;
1713 isl_bool match;
1715 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1716 map2, isl_dim_out);
1717 if (match < 0)
1718 return isl_stat_error;
1719 if (!match)
1720 return isl_stat_ok;
1722 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1724 data->res = isl_union_map_add_map(data->res, map2);
1726 return isl_stat_ok;
1729 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1730 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1732 return bin_op(umap1, umap2, &map_lex_lt_entry);
1735 static isl_stat map_lex_le_entry(void **entry, void *user)
1737 struct isl_union_map_bin_data *data = user;
1738 isl_map *map2 = *entry;
1739 isl_bool match;
1741 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1742 map2, isl_dim_out);
1743 if (match < 0)
1744 return isl_stat_error;
1745 if (!match)
1746 return isl_stat_ok;
1748 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1750 data->res = isl_union_map_add_map(data->res, map2);
1752 return isl_stat_ok;
1755 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1756 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1758 return bin_op(umap1, umap2, &map_lex_le_entry);
1761 static isl_stat product_entry(void **entry, void *user)
1763 struct isl_union_map_bin_data *data = user;
1764 isl_map *map2 = *entry;
1766 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1768 data->res = isl_union_map_add_map(data->res, map2);
1770 return isl_stat_ok;
1773 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1774 __isl_take isl_union_map *umap2)
1776 return bin_op(umap1, umap2, &product_entry);
1779 static isl_stat set_product_entry(void **entry, void *user)
1781 struct isl_union_map_bin_data *data = user;
1782 isl_set *set2 = *entry;
1784 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1786 data->res = isl_union_set_add_set(data->res, set2);
1788 return isl_stat_ok;
1791 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1792 __isl_take isl_union_set *uset2)
1794 return bin_op(uset1, uset2, &set_product_entry);
1797 static isl_stat domain_product_entry(void **entry, void *user)
1799 struct isl_union_map_bin_data *data = user;
1800 isl_map *map2 = *entry;
1801 isl_bool match;
1803 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1804 map2, isl_dim_out);
1805 if (match < 0)
1806 return isl_stat_error;
1807 if (!match)
1808 return isl_stat_ok;
1810 map2 = isl_map_domain_product(isl_map_copy(data->map),
1811 isl_map_copy(map2));
1813 data->res = isl_union_map_add_map(data->res, map2);
1815 return isl_stat_ok;
1818 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1820 __isl_give isl_union_map *isl_union_map_domain_product(
1821 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1823 return bin_op(umap1, umap2, &domain_product_entry);
1826 static isl_stat range_product_entry(void **entry, void *user)
1828 struct isl_union_map_bin_data *data = user;
1829 isl_map *map2 = *entry;
1830 isl_bool match;
1832 match = isl_map_tuple_is_equal(data->map, isl_dim_in, map2, isl_dim_in);
1833 if (match < 0)
1834 return isl_stat_error;
1835 if (!match)
1836 return isl_stat_ok;
1838 map2 = isl_map_range_product(isl_map_copy(data->map),
1839 isl_map_copy(map2));
1841 data->res = isl_union_map_add_map(data->res, map2);
1843 return isl_stat_ok;
1846 __isl_give isl_union_map *isl_union_map_range_product(
1847 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1849 return bin_op(umap1, umap2, &range_product_entry);
1852 /* If data->map A -> B and "map2" C -> D have the same range space,
1853 * then add (A, C) -> (B * D) to data->res.
1855 static isl_stat flat_domain_product_entry(void **entry, void *user)
1857 struct isl_union_map_bin_data *data = user;
1858 isl_map *map2 = *entry;
1859 isl_bool match;
1861 match = isl_map_tuple_is_equal(data->map, isl_dim_out,
1862 map2, isl_dim_out);
1863 if (match < 0)
1864 return isl_stat_error;
1865 if (!match)
1866 return isl_stat_ok;
1868 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1869 isl_map_copy(map2));
1871 data->res = isl_union_map_add_map(data->res, map2);
1873 return isl_stat_ok;
1876 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1878 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1879 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1881 return bin_op(umap1, umap2, &flat_domain_product_entry);
1884 static isl_stat flat_range_product_entry(void **entry, void *user)
1886 struct isl_union_map_bin_data *data = user;
1887 isl_map *map2 = *entry;
1888 isl_bool match;
1890 match = isl_map_tuple_is_equal(data->map, isl_dim_in, map2, isl_dim_in);
1891 if (match < 0)
1892 return isl_stat_error;
1893 if (!match)
1894 return isl_stat_ok;
1896 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1897 isl_map_copy(map2));
1899 data->res = isl_union_map_add_map(data->res, map2);
1901 return isl_stat_ok;
1904 __isl_give isl_union_map *isl_union_map_flat_range_product(
1905 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1907 return bin_op(umap1, umap2, &flat_range_product_entry);
1910 /* Data structure that specifies how un_op should modify
1911 * the maps in the union map.
1913 * If "inplace" is set, then the maps in the input union map
1914 * are modified in place. This means that "fn_map" should not
1915 * change the meaning of the map or that the union map only
1916 * has a single reference.
1917 * If "total" is set, then all maps need to be modified and
1918 * the results need to live in the same space.
1919 * Otherwise, a new union map is constructed to store the results.
1920 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1921 * are taken into account. "filter_user" is passed as the second argument
1922 * to "filter". No filter can be set if "inplace" or
1923 * "total" is set.
1924 * At most one of "fn_map" or "fn_map2" can be set, specifying
1925 * how the maps (selected by "filter") should be transformed.
1926 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1928 struct isl_un_op_control {
1929 int inplace;
1930 int total;
1931 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1932 void *filter_user;
1933 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1934 __isl_give isl_map *(*fn_map2)(__isl_take isl_map *map, void *user);
1935 void *fn_map2_user;
1938 /* Data structure for wrapping the data for un_op_filter_drop_user.
1939 * "filter" is the function that is being wrapped.
1941 struct isl_un_op_drop_user_data {
1942 isl_bool (*filter)(__isl_keep isl_map *map);
1945 /* Wrapper for isl_un_op_control filters that do not require
1946 * a second argument.
1947 * Simply call data->filter without the second argument.
1949 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1951 struct isl_un_op_drop_user_data *data = user;
1952 return data->filter(map);
1955 /* Internal data structure for "un_op".
1956 * "control" specifies how the maps in the union map should be modified.
1957 * "res" collects the results.
1959 struct isl_union_map_un_data {
1960 struct isl_un_op_control *control;
1961 isl_union_map *res;
1964 /* isl_hash_table_foreach callback for un_op.
1965 * Handle the map that "entry" points to.
1967 * If control->filter is set, then check if this map satisfies the filter.
1968 * If so (or if control->filter is not set), modify the map
1969 * by calling control->fn_map or control->fn_map2 (if set) and
1970 * either add the result to data->res or
1971 * replace the original entry by the result (if control->inplace is set).
1973 static isl_stat un_entry(void **entry, void *user)
1975 struct isl_union_map_un_data *data = user;
1976 struct isl_un_op_control *control = data->control;
1977 isl_map *map = *entry;
1979 if (control->filter) {
1980 isl_bool ok;
1982 ok = control->filter(map, control->filter_user);
1983 if (ok < 0)
1984 return isl_stat_error;
1985 if (!ok)
1986 return isl_stat_ok;
1989 map = isl_map_copy(map);
1990 if (control->fn_map2 != NULL)
1991 map = control->fn_map2(map, control->fn_map2_user);
1992 else if (control->fn_map != NULL)
1993 map = control->fn_map(map);
1994 if (!map)
1995 return isl_stat_error;
1996 if (control->inplace) {
1997 isl_map_free(*entry);
1998 *entry = map;
1999 } else {
2000 data->res = isl_union_map_add_map(data->res, map);
2001 if (!data->res)
2002 return isl_stat_error;
2005 return isl_stat_ok;
2008 /* Modify the maps in "umap" based on "control".
2009 * If control->inplace is set, then modify the maps in "umap" in-place.
2010 * Otherwise, create a new union map to hold the results.
2011 * If control->total is set, then perform an inplace computation
2012 * if "umap" is only referenced once. Otherwise, create a new union map
2013 * to store the results.
2015 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
2016 struct isl_un_op_control *control)
2018 struct isl_union_map_un_data data = { control };
2020 if (!umap)
2021 return NULL;
2022 if (!!control->fn_map && !!control->fn_map2)
2023 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
2024 "at most one mapping function can be specified",
2025 return isl_union_map_free(umap));
2026 if ((control->inplace || control->total) && control->filter)
2027 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
2028 "inplace/total modification cannot be filtered",
2029 return isl_union_map_free(umap));
2031 if (control->total && umap->ref == 1)
2032 control->inplace = 1;
2033 if (control->inplace) {
2034 data.res = umap;
2035 } else {
2036 isl_space *space;
2038 space = isl_union_map_get_space(umap);
2039 data.res = isl_union_map_alloc(space, umap->table.n);
2041 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
2042 &umap->table, &un_entry, &data) < 0)
2043 data.res = isl_union_map_free(data.res);
2045 if (control->inplace)
2046 return data.res;
2047 isl_union_map_free(umap);
2048 return data.res;
2051 __isl_give isl_union_map *isl_union_map_from_range(
2052 __isl_take isl_union_set *uset)
2054 struct isl_un_op_control control = {
2055 .fn_map = &isl_map_from_range,
2057 return un_op(uset, &control);
2060 __isl_give isl_union_map *isl_union_map_from_domain(
2061 __isl_take isl_union_set *uset)
2063 return isl_union_map_reverse(isl_union_map_from_range(uset));
2066 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
2067 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
2069 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
2070 isl_union_map_from_range(range));
2073 /* Modify the maps in "umap" by applying "fn" on them.
2074 * "fn" should apply to all maps in "umap" and should not modify the space.
2076 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
2077 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2079 struct isl_un_op_control control = {
2080 .total = 1,
2081 .fn_map = fn,
2084 return un_op(umap, &control);
2087 /* Compute the affine hull of "map" and return the result as an isl_map.
2089 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
2091 return isl_map_from_basic_map(isl_map_affine_hull(map));
2094 __isl_give isl_union_map *isl_union_map_affine_hull(
2095 __isl_take isl_union_map *umap)
2097 return total(umap, &isl_map_affine_hull_map);
2100 __isl_give isl_union_set *isl_union_set_affine_hull(
2101 __isl_take isl_union_set *uset)
2103 return isl_union_map_affine_hull(uset);
2106 /* Wrapper around isl_set_combined_lineality_space
2107 * that returns the combined lineality space in the form of an isl_set
2108 * instead of an isl_basic_set.
2110 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
2112 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
2115 /* For each set in "uset", compute the (linear) hull
2116 * of the lineality spaces of its basic sets and
2117 * collect and return the results.
2119 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
2120 __isl_take isl_union_set *uset)
2122 struct isl_un_op_control control = {
2123 .fn_map = &combined_lineality_space,
2125 return un_op(uset, &control);
2128 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
2130 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
2132 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
2135 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
2136 __isl_take isl_union_map *umap)
2138 return total(umap, &isl_map_polyhedral_hull_map);
2141 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
2142 __isl_take isl_union_set *uset)
2144 return isl_union_map_polyhedral_hull(uset);
2147 /* Compute a superset of the convex hull of "map" that is described
2148 * by only translates of the constraints in the constituents of "map" and
2149 * return the result as an isl_map.
2151 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
2153 return isl_map_from_basic_map(isl_map_simple_hull(map));
2156 __isl_give isl_union_map *isl_union_map_simple_hull(
2157 __isl_take isl_union_map *umap)
2159 return total(umap, &isl_map_simple_hull_map);
2162 __isl_give isl_union_set *isl_union_set_simple_hull(
2163 __isl_take isl_union_set *uset)
2165 return isl_union_map_simple_hull(uset);
2168 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
2169 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2171 struct isl_un_op_control control = {
2172 .inplace = 1,
2173 .fn_map = fn,
2176 return un_op(umap, &control);
2179 /* Remove redundant constraints in each of the basic maps of "umap".
2180 * Since removing redundant constraints does not change the meaning
2181 * or the space, the operation can be performed in-place.
2183 __isl_give isl_union_map *isl_union_map_remove_redundancies(
2184 __isl_take isl_union_map *umap)
2186 return inplace(umap, &isl_map_remove_redundancies);
2189 /* Remove redundant constraints in each of the basic sets of "uset".
2191 __isl_give isl_union_set *isl_union_set_remove_redundancies(
2192 __isl_take isl_union_set *uset)
2194 return isl_union_map_remove_redundancies(uset);
2197 __isl_give isl_union_map *isl_union_map_coalesce(
2198 __isl_take isl_union_map *umap)
2200 return inplace(umap, &isl_map_coalesce);
2203 __isl_give isl_union_set *isl_union_set_coalesce(
2204 __isl_take isl_union_set *uset)
2206 return isl_union_map_coalesce(uset);
2209 __isl_give isl_union_map *isl_union_map_detect_equalities(
2210 __isl_take isl_union_map *umap)
2212 return inplace(umap, &isl_map_detect_equalities);
2215 __isl_give isl_union_set *isl_union_set_detect_equalities(
2216 __isl_take isl_union_set *uset)
2218 return isl_union_map_detect_equalities(uset);
2221 __isl_give isl_union_map *isl_union_map_compute_divs(
2222 __isl_take isl_union_map *umap)
2224 return inplace(umap, &isl_map_compute_divs);
2227 __isl_give isl_union_set *isl_union_set_compute_divs(
2228 __isl_take isl_union_set *uset)
2230 return isl_union_map_compute_divs(uset);
2233 __isl_give isl_union_map *isl_union_map_lexmin(
2234 __isl_take isl_union_map *umap)
2236 return total(umap, &isl_map_lexmin);
2239 __isl_give isl_union_set *isl_union_set_lexmin(
2240 __isl_take isl_union_set *uset)
2242 return isl_union_map_lexmin(uset);
2245 __isl_give isl_union_map *isl_union_map_lexmax(
2246 __isl_take isl_union_map *umap)
2248 return total(umap, &isl_map_lexmax);
2251 __isl_give isl_union_set *isl_union_set_lexmax(
2252 __isl_take isl_union_set *uset)
2254 return isl_union_map_lexmax(uset);
2257 /* Return the universe in the space of "map".
2259 static __isl_give isl_map *universe(__isl_take isl_map *map)
2261 isl_space *space;
2263 space = isl_map_get_space(map);
2264 isl_map_free(map);
2265 return isl_map_universe(space);
2268 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2270 struct isl_un_op_control control = {
2271 .fn_map = &universe,
2273 return un_op(umap, &control);
2276 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2278 return isl_union_map_universe(uset);
2281 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2283 struct isl_un_op_control control = {
2284 .fn_map = &isl_map_reverse,
2286 return un_op(umap, &control);
2289 /* Given a union map, take the maps of the form A -> (B -> C) and
2290 * return the union of the corresponding maps A -> (C -> B).
2292 __isl_give isl_union_map *isl_union_map_range_reverse(
2293 __isl_take isl_union_map *umap)
2295 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2296 struct isl_un_op_control control = {
2297 .filter = &un_op_filter_drop_user,
2298 .filter_user = &data,
2299 .fn_map = &isl_map_range_reverse,
2301 return un_op(umap, &control);
2304 /* Compute the parameter domain of the given union map.
2306 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2308 struct isl_un_op_control control = {
2309 .fn_map = &isl_map_params,
2311 int empty;
2313 empty = isl_union_map_is_empty(umap);
2314 if (empty < 0)
2315 goto error;
2316 if (empty) {
2317 isl_space *space;
2318 space = isl_union_map_get_space(umap);
2319 isl_union_map_free(umap);
2320 return isl_set_empty(space);
2322 return isl_set_from_union_set(un_op(umap, &control));
2323 error:
2324 isl_union_map_free(umap);
2325 return NULL;
2328 /* Compute the parameter domain of the given union set.
2330 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2332 return isl_union_map_params(uset);
2335 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2337 struct isl_un_op_control control = {
2338 .fn_map = &isl_map_domain,
2340 return un_op(umap, &control);
2343 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2345 struct isl_un_op_control control = {
2346 .fn_map = &isl_map_range,
2348 return un_op(umap, &control);
2351 __isl_give isl_union_map *isl_union_map_domain_map(
2352 __isl_take isl_union_map *umap)
2354 struct isl_un_op_control control = {
2355 .fn_map = &isl_map_domain_map,
2357 return un_op(umap, &control);
2360 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2361 * add the result to "res".
2363 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2365 isl_union_pw_multi_aff **res = user;
2366 isl_multi_aff *ma;
2367 isl_pw_multi_aff *pma;
2369 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2370 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2371 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2373 return *res ? isl_stat_ok : isl_stat_error;
2377 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2378 * to its domain.
2380 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2381 __isl_take isl_union_map *umap)
2383 isl_union_pw_multi_aff *res;
2385 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2386 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2387 res = isl_union_pw_multi_aff_free(res);
2389 isl_union_map_free(umap);
2390 return res;
2393 __isl_give isl_union_map *isl_union_map_range_map(
2394 __isl_take isl_union_map *umap)
2396 struct isl_un_op_control control = {
2397 .fn_map = &isl_map_range_map,
2399 return un_op(umap, &control);
2402 /* Given a collection of wrapped maps of the form A[B -> C],
2403 * return the collection of maps A[B -> C] -> B.
2405 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2406 __isl_take isl_union_set *uset)
2408 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2409 struct isl_un_op_control control = {
2410 .filter = &un_op_filter_drop_user,
2411 .filter_user = &data,
2412 .fn_map = &isl_set_wrapped_domain_map,
2414 return un_op(uset, &control);
2417 /* Does "map" relate elements from the same space?
2419 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2421 return isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
2424 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2426 struct isl_un_op_control control = {
2427 .filter = &equal_tuples,
2428 .fn_map = &isl_map_deltas,
2430 return un_op(umap, &control);
2433 __isl_give isl_union_map *isl_union_map_deltas_map(
2434 __isl_take isl_union_map *umap)
2436 struct isl_un_op_control control = {
2437 .filter = &equal_tuples,
2438 .fn_map = &isl_map_deltas_map,
2440 return un_op(umap, &control);
2443 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2445 struct isl_un_op_control control = {
2446 .fn_map = &isl_set_identity,
2448 return un_op(uset, &control);
2451 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2453 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2455 isl_union_pw_multi_aff **res = user;
2456 isl_space *space;
2457 isl_pw_multi_aff *pma;
2459 space = isl_space_map_from_set(isl_set_get_space(set));
2460 pma = isl_pw_multi_aff_identity(space);
2461 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2462 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2464 return *res ? isl_stat_ok : isl_stat_error;
2467 /* Return an identity function on "uset" in the form
2468 * of an isl_union_pw_multi_aff.
2470 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2471 __isl_take isl_union_set *uset)
2473 isl_union_pw_multi_aff *res;
2475 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2476 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2477 res = isl_union_pw_multi_aff_free(res);
2479 isl_union_set_free(uset);
2480 return res;
2483 /* For each map in "umap" of the form [A -> B] -> C,
2484 * construct the map A -> C and collect the results.
2486 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2487 __isl_take isl_union_map *umap)
2489 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2490 struct isl_un_op_control control = {
2491 .filter = &un_op_filter_drop_user,
2492 .filter_user = &data,
2493 .fn_map = &isl_map_domain_factor_domain,
2495 return un_op(umap, &control);
2498 /* For each map in "umap" of the form [A -> B] -> C,
2499 * construct the map B -> C and collect the results.
2501 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2502 __isl_take isl_union_map *umap)
2504 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2505 struct isl_un_op_control control = {
2506 .filter = &un_op_filter_drop_user,
2507 .filter_user = &data,
2508 .fn_map = &isl_map_domain_factor_range,
2510 return un_op(umap, &control);
2513 /* For each map in "umap" of the form A -> [B -> C],
2514 * construct the map A -> B and collect the results.
2516 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2517 __isl_take isl_union_map *umap)
2519 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2520 struct isl_un_op_control control = {
2521 .filter = &un_op_filter_drop_user,
2522 .filter_user = &data,
2523 .fn_map = &isl_map_range_factor_domain,
2525 return un_op(umap, &control);
2528 /* For each map in "umap" of the form A -> [B -> C],
2529 * construct the map A -> C and collect the results.
2531 __isl_give isl_union_map *isl_union_map_range_factor_range(
2532 __isl_take isl_union_map *umap)
2534 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2535 struct isl_un_op_control control = {
2536 .filter = &un_op_filter_drop_user,
2537 .filter_user = &data,
2538 .fn_map = &isl_map_range_factor_range,
2540 return un_op(umap, &control);
2543 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2544 * construct the map A -> C and collect the results.
2546 __isl_give isl_union_map *isl_union_map_factor_domain(
2547 __isl_take isl_union_map *umap)
2549 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2550 struct isl_un_op_control control = {
2551 .filter = &un_op_filter_drop_user,
2552 .filter_user = &data,
2553 .fn_map = &isl_map_factor_domain,
2555 return un_op(umap, &control);
2558 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2559 * construct the map B -> D and collect the results.
2561 __isl_give isl_union_map *isl_union_map_factor_range(
2562 __isl_take isl_union_map *umap)
2564 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2565 struct isl_un_op_control control = {
2566 .filter = &un_op_filter_drop_user,
2567 .filter_user = &data,
2568 .fn_map = &isl_map_factor_range,
2570 return un_op(umap, &control);
2573 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2575 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2576 struct isl_un_op_control control = {
2577 .filter = &un_op_filter_drop_user,
2578 .filter_user = &data,
2579 .fn_map = &isl_set_unwrap,
2581 return un_op(uset, &control);
2584 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2586 struct isl_un_op_control control = {
2587 .fn_map = &isl_map_wrap,
2589 return un_op(umap, &control);
2592 struct isl_union_map_is_subset_data {
2593 isl_union_map *umap2;
2594 isl_bool is_subset;
2597 static isl_stat is_subset_entry(void **entry, void *user)
2599 struct isl_union_map_is_subset_data *data = user;
2600 struct isl_hash_table_entry *entry2;
2601 isl_space *space;
2602 isl_map *map = *entry;
2604 space = isl_map_peek_space(map);
2605 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
2606 if (!entry2)
2607 return isl_stat_error;
2608 if (entry2 == isl_hash_table_entry_none) {
2609 int empty = isl_map_is_empty(map);
2610 if (empty < 0)
2611 return isl_stat_error;
2612 if (empty)
2613 return isl_stat_ok;
2614 data->is_subset = isl_bool_false;
2615 return isl_stat_error;
2618 data->is_subset = isl_map_is_subset(map, entry2->data);
2619 if (data->is_subset < 0 || !data->is_subset)
2620 return isl_stat_error;
2622 return isl_stat_ok;
2625 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2626 __isl_keep isl_union_map *umap2)
2628 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2630 if (!umap1 || !umap2)
2631 return isl_bool_error;
2633 data.umap2 = umap2;
2634 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2635 &is_subset_entry, &data) < 0 &&
2636 data.is_subset)
2637 return isl_bool_error;
2639 return data.is_subset;
2642 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2643 __isl_keep isl_union_set *uset2)
2645 return isl_union_map_is_subset(uset1, uset2);
2648 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2649 __isl_keep isl_union_map *umap2)
2651 isl_bool is_subset;
2653 if (!umap1 || !umap2)
2654 return isl_bool_error;
2655 is_subset = isl_union_map_is_subset(umap1, umap2);
2656 if (is_subset != isl_bool_true)
2657 return is_subset;
2658 is_subset = isl_union_map_is_subset(umap2, umap1);
2659 return is_subset;
2662 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2663 __isl_keep isl_union_set *uset2)
2665 return isl_union_map_is_equal(uset1, uset2);
2668 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2669 __isl_keep isl_union_map *umap2)
2671 isl_bool is_subset;
2673 if (!umap1 || !umap2)
2674 return isl_bool_error;
2675 is_subset = isl_union_map_is_subset(umap1, umap2);
2676 if (is_subset != isl_bool_true)
2677 return is_subset;
2678 is_subset = isl_union_map_is_subset(umap2, umap1);
2679 return isl_bool_not(is_subset);
2682 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2683 __isl_keep isl_union_set *uset2)
2685 return isl_union_map_is_strict_subset(uset1, uset2);
2688 /* Internal data structure for isl_union_map_is_disjoint.
2689 * umap2 is the union map with which we are comparing.
2690 * is_disjoint is initialized to 1 and is set to 0 as soon
2691 * as the union maps turn out not to be disjoint.
2693 struct isl_union_map_is_disjoint_data {
2694 isl_union_map *umap2;
2695 isl_bool is_disjoint;
2698 /* Check if "map" is disjoint from data->umap2 and abort
2699 * the search if it is not.
2701 static isl_stat is_disjoint_entry(void **entry, void *user)
2703 struct isl_union_map_is_disjoint_data *data = user;
2704 struct isl_hash_table_entry *entry2;
2705 isl_space *space;
2706 isl_map *map = *entry;
2708 space = isl_map_peek_space(map);
2709 entry2 = isl_union_map_find_entry(data->umap2, space, 0);
2710 if (!entry2)
2711 return isl_stat_error;
2712 if (entry2 == isl_hash_table_entry_none)
2713 return isl_stat_ok;
2715 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2716 if (data->is_disjoint < 0 || !data->is_disjoint)
2717 return isl_stat_error;
2719 return isl_stat_ok;
2722 /* Are "umap1" and "umap2" disjoint?
2724 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2725 __isl_keep isl_union_map *umap2)
2727 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2729 umap1 = isl_union_map_copy(umap1);
2730 umap2 = isl_union_map_copy(umap2);
2731 umap1 = isl_union_map_align_params(umap1,
2732 isl_union_map_get_space(umap2));
2733 umap2 = isl_union_map_align_params(umap2,
2734 isl_union_map_get_space(umap1));
2736 if (!umap1 || !umap2)
2737 goto error;
2739 data.umap2 = umap2;
2740 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2741 &is_disjoint_entry, &data) < 0 &&
2742 data.is_disjoint)
2743 goto error;
2745 isl_union_map_free(umap1);
2746 isl_union_map_free(umap2);
2748 return data.is_disjoint;
2749 error:
2750 isl_union_map_free(umap1);
2751 isl_union_map_free(umap2);
2752 return isl_bool_error;
2755 /* Are "uset1" and "uset2" disjoint?
2757 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2758 __isl_keep isl_union_set *uset2)
2760 return isl_union_map_is_disjoint(uset1, uset2);
2763 static isl_stat sample_entry(void **entry, void *user)
2765 isl_basic_map **sample = (isl_basic_map **)user;
2766 isl_map *map = *entry;
2768 *sample = isl_map_sample(isl_map_copy(map));
2769 if (!*sample)
2770 return isl_stat_error;
2771 if (!isl_basic_map_plain_is_empty(*sample))
2772 return isl_stat_error;
2773 return isl_stat_ok;
2776 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2778 isl_basic_map *sample = NULL;
2780 if (!umap)
2781 return NULL;
2783 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2784 &sample_entry, &sample) < 0 &&
2785 !sample)
2786 goto error;
2788 if (!sample)
2789 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2791 isl_union_map_free(umap);
2793 return sample;
2794 error:
2795 isl_union_map_free(umap);
2796 return NULL;
2799 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2801 return bset_from_bmap(isl_union_map_sample(uset));
2804 /* Return an element in "uset" in the form of an isl_point.
2805 * Return a void isl_point if "uset" is empty.
2807 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2809 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2812 struct isl_forall_data {
2813 isl_bool res;
2814 isl_bool (*fn)(__isl_keep isl_map *map);
2817 static isl_stat forall_entry(void **entry, void *user)
2819 struct isl_forall_data *data = user;
2820 isl_map *map = *entry;
2822 data->res = data->fn(map);
2823 if (data->res < 0)
2824 return isl_stat_error;
2826 if (!data->res)
2827 return isl_stat_error;
2829 return isl_stat_ok;
2832 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2833 isl_bool (*fn)(__isl_keep isl_map *map))
2835 struct isl_forall_data data = { isl_bool_true, fn };
2837 if (!umap)
2838 return isl_bool_error;
2840 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2841 &forall_entry, &data) < 0 && data.res)
2842 return isl_bool_error;
2844 return data.res;
2847 struct isl_forall_user_data {
2848 isl_bool res;
2849 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2850 void *user;
2853 static isl_stat forall_user_entry(void **entry, void *user)
2855 struct isl_forall_user_data *data = user;
2856 isl_map *map = *entry;
2858 data->res = data->fn(map, data->user);
2859 if (data->res < 0)
2860 return isl_stat_error;
2862 if (!data->res)
2863 return isl_stat_error;
2865 return isl_stat_ok;
2868 /* Check if fn(map, user) returns true for all maps "map" in umap.
2870 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2871 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2873 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2875 if (!umap)
2876 return isl_bool_error;
2878 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2879 &forall_user_entry, &data) < 0 && data.res)
2880 return isl_bool_error;
2882 return data.res;
2885 /* Is "umap" obviously empty?
2887 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2889 isl_size n;
2891 n = isl_union_map_n_map(umap);
2892 if (n < 0)
2893 return isl_bool_error;
2894 return n == 0;
2897 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2899 return union_map_forall(umap, &isl_map_is_empty);
2902 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2904 return isl_union_map_is_empty(uset);
2907 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2909 isl_bool is_subset;
2910 isl_space *space;
2911 isl_map *id;
2912 isl_bool match;
2914 match = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
2915 if (match < 0)
2916 return isl_bool_error;
2917 if (!match)
2918 return isl_bool_false;
2920 space = isl_map_get_space(map);
2921 id = isl_map_identity(space);
2923 is_subset = isl_map_is_subset(map, id);
2925 isl_map_free(id);
2927 return is_subset;
2930 /* Given an isl_union_map that consists of a single map, check
2931 * if it is single-valued.
2933 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2935 isl_map *map;
2936 isl_bool sv;
2938 umap = isl_union_map_copy(umap);
2939 map = isl_map_from_union_map(umap);
2940 sv = isl_map_is_single_valued(map);
2941 isl_map_free(map);
2943 return sv;
2946 /* Internal data structure for single_valued_on_domain.
2948 * "umap" is the union map to be tested.
2949 * "sv" is set to 1 as long as "umap" may still be single-valued.
2951 struct isl_union_map_is_sv_data {
2952 isl_union_map *umap;
2953 isl_bool sv;
2956 /* Check if the data->umap is single-valued on "set".
2958 * If data->umap consists of a single map on "set", then test it
2959 * as an isl_map.
2961 * Otherwise, compute
2963 * M \circ M^-1
2965 * check if the result is a subset of the identity mapping and
2966 * store the result in data->sv.
2968 * Terminate as soon as data->umap has been determined not to
2969 * be single-valued.
2971 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2973 struct isl_union_map_is_sv_data *data = user;
2974 isl_union_map *umap, *test;
2975 isl_size n;
2977 umap = isl_union_map_copy(data->umap);
2978 umap = isl_union_map_intersect_domain(umap,
2979 isl_union_set_from_set(set));
2981 n = isl_union_map_n_map(umap);
2982 if (n < 0) {
2983 data->sv = isl_bool_error;
2984 } else if (n == 1) {
2985 data->sv = single_map_is_single_valued(umap);
2986 isl_union_map_free(umap);
2987 } else {
2988 test = isl_union_map_reverse(isl_union_map_copy(umap));
2989 test = isl_union_map_apply_range(test, umap);
2991 data->sv = union_map_forall(test, &is_subset_of_identity);
2993 isl_union_map_free(test);
2996 if (data->sv < 0 || !data->sv)
2997 return isl_stat_error;
2998 return isl_stat_ok;
3001 /* Check if the given map is single-valued.
3003 * If the union map consists of a single map, then test it as an isl_map.
3004 * Otherwise, check if the union map is single-valued on each of its
3005 * domain spaces.
3007 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
3009 isl_union_map *universe;
3010 isl_union_set *domain;
3011 struct isl_union_map_is_sv_data data;
3012 isl_size n;
3014 n = isl_union_map_n_map(umap);
3015 if (n < 0)
3016 return isl_bool_error;
3017 if (n == 1)
3018 return single_map_is_single_valued(umap);
3020 universe = isl_union_map_universe(isl_union_map_copy(umap));
3021 domain = isl_union_map_domain(universe);
3023 data.sv = isl_bool_true;
3024 data.umap = umap;
3025 if (isl_union_set_foreach_set(domain,
3026 &single_valued_on_domain, &data) < 0 && data.sv)
3027 data.sv = isl_bool_error;
3028 isl_union_set_free(domain);
3030 return data.sv;
3033 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
3035 isl_bool in;
3037 umap = isl_union_map_copy(umap);
3038 umap = isl_union_map_reverse(umap);
3039 in = isl_union_map_is_single_valued(umap);
3040 isl_union_map_free(umap);
3042 return in;
3045 /* Is "map" obviously not an identity relation because
3046 * it maps elements from one space to another space?
3047 * Update *non_identity accordingly.
3049 * In particular, if the domain and range spaces are the same,
3050 * then the map is not considered to obviously not be an identity relation.
3051 * Otherwise, the map is considered to obviously not be an identity relation
3052 * if it is is non-empty.
3054 * If "map" is determined to obviously not be an identity relation,
3055 * then the search is aborted.
3057 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
3059 isl_bool *non_identity = user;
3060 isl_bool equal;
3062 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
3063 if (equal >= 0 && !equal)
3064 *non_identity = isl_bool_not(isl_map_is_empty(map));
3065 else
3066 *non_identity = isl_bool_not(equal);
3067 isl_map_free(map);
3069 if (*non_identity < 0 || *non_identity)
3070 return isl_stat_error;
3072 return isl_stat_ok;
3075 /* Is "umap" obviously not an identity relation because
3076 * it maps elements from one space to another space?
3078 * As soon as a map has been found that maps elements to a different space,
3079 * non_identity is changed and the search is aborted.
3081 static isl_bool isl_union_map_plain_is_not_identity(
3082 __isl_keep isl_union_map *umap)
3084 isl_bool non_identity;
3086 non_identity = isl_bool_false;
3087 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
3088 &non_identity) < 0 &&
3089 non_identity == isl_bool_false)
3090 return isl_bool_error;
3092 return non_identity;
3095 /* Does "map" only map elements to themselves?
3096 * Update *identity accordingly.
3098 * If "map" is determined not to be an identity relation,
3099 * then the search is aborted.
3101 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
3103 isl_bool *identity = user;
3105 *identity = isl_map_is_identity(map);
3106 isl_map_free(map);
3108 if (*identity < 0 || !*identity)
3109 return isl_stat_error;
3111 return isl_stat_ok;
3114 /* Does "umap" only map elements to themselves?
3116 * First check if there are any maps that map elements to different spaces.
3117 * If not, then check that all the maps (between identical spaces)
3118 * are identity relations.
3120 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
3122 isl_bool non_identity;
3123 isl_bool identity;
3125 non_identity = isl_union_map_plain_is_not_identity(umap);
3126 if (non_identity < 0 || non_identity)
3127 return isl_bool_not(non_identity);
3129 identity = isl_bool_true;
3130 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
3131 identity == isl_bool_true)
3132 return isl_bool_error;
3134 return identity;
3137 /* Represents a map that has a fixed value (v) for one of its
3138 * range dimensions.
3139 * The map in this structure is not reference counted, so it
3140 * is only valid while the isl_union_map from which it was
3141 * obtained is still alive.
3143 struct isl_fixed_map {
3144 isl_int v;
3145 isl_map *map;
3148 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
3149 int n)
3151 int i;
3152 struct isl_fixed_map *v;
3154 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
3155 if (!v)
3156 return NULL;
3157 for (i = 0; i < n; ++i)
3158 isl_int_init(v[i].v);
3159 return v;
3162 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
3164 int i;
3166 if (!v)
3167 return;
3168 for (i = 0; i < n; ++i)
3169 isl_int_clear(v[i].v);
3170 free(v);
3173 /* Compare the "v" field of two isl_fixed_map structs.
3175 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
3177 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
3178 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
3180 return isl_int_cmp(e1->v, e2->v);
3183 /* Internal data structure used while checking whether all maps
3184 * in a union_map have a fixed value for a given output dimension.
3185 * v is the list of maps, with the fixed value for the dimension
3186 * n is the number of maps considered so far
3187 * pos is the output dimension under investigation
3189 struct isl_fixed_dim_data {
3190 struct isl_fixed_map *v;
3191 int n;
3192 int pos;
3195 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
3197 struct isl_fixed_dim_data *data = user;
3199 data->v[data->n].map = map;
3200 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
3201 &data->v[data->n++].v);
3204 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3205 int first, int n_range);
3207 /* Given a list of the maps, with their fixed values at output dimension "pos",
3208 * check whether the ranges of the maps form an obvious partition.
3210 * We first sort the maps according to their fixed values.
3211 * If all maps have a different value, then we know the ranges form
3212 * a partition.
3213 * Otherwise, we collect the maps with the same fixed value and
3214 * check whether each such collection is obviously injective
3215 * based on later dimensions.
3217 static int separates(struct isl_fixed_map *v, int n,
3218 __isl_take isl_space *space, int pos, int n_range)
3220 int i;
3222 if (!v)
3223 goto error;
3225 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
3227 for (i = 0; i + 1 < n; ++i) {
3228 int j, k;
3229 isl_union_map *part;
3230 int injective;
3232 for (j = i + 1; j < n; ++j)
3233 if (isl_int_ne(v[i].v, v[j].v))
3234 break;
3236 if (j == i + 1)
3237 continue;
3239 part = isl_union_map_alloc(isl_space_copy(space), j - i);
3240 for (k = i; k < j; ++k)
3241 part = isl_union_map_add_map(part,
3242 isl_map_copy(v[k].map));
3244 injective = plain_injective_on_range(part, pos + 1, n_range);
3245 if (injective < 0)
3246 goto error;
3247 if (!injective)
3248 break;
3250 i = j - 1;
3253 isl_space_free(space);
3254 free_isl_fixed_map_array(v, n);
3255 return i + 1 >= n;
3256 error:
3257 isl_space_free(space);
3258 free_isl_fixed_map_array(v, n);
3259 return -1;
3262 /* Check whether the maps in umap have obviously distinct ranges.
3263 * In particular, check for an output dimension in the range
3264 * [first,n_range) for which all maps have a fixed value
3265 * and then check if these values, possibly along with fixed values
3266 * at later dimensions, entail distinct ranges.
3268 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3269 int first, int n_range)
3271 isl_ctx *ctx;
3272 isl_size n;
3273 struct isl_fixed_dim_data data = { NULL };
3275 ctx = isl_union_map_get_ctx(umap);
3277 n = isl_union_map_n_map(umap);
3278 if (n < 0)
3279 goto error;
3281 if (n <= 1) {
3282 isl_union_map_free(umap);
3283 return isl_bool_true;
3286 if (first >= n_range) {
3287 isl_union_map_free(umap);
3288 return isl_bool_false;
3291 data.v = alloc_isl_fixed_map_array(ctx, n);
3292 if (!data.v)
3293 goto error;
3295 for (data.pos = first; data.pos < n_range; ++data.pos) {
3296 isl_bool fixed;
3297 int injective;
3298 isl_space *space;
3300 data.n = 0;
3301 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3302 if (fixed < 0)
3303 goto error;
3304 if (!fixed)
3305 continue;
3306 space = isl_union_map_get_space(umap);
3307 injective = separates(data.v, n, space, data.pos, n_range);
3308 isl_union_map_free(umap);
3309 return injective;
3312 free_isl_fixed_map_array(data.v, n);
3313 isl_union_map_free(umap);
3315 return isl_bool_false;
3316 error:
3317 free_isl_fixed_map_array(data.v, n);
3318 isl_union_map_free(umap);
3319 return isl_bool_error;
3322 /* Check whether the maps in umap that map to subsets of "ran"
3323 * have obviously distinct ranges.
3325 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3326 void *user)
3328 isl_size dim;
3329 isl_union_map *umap = user;
3331 dim = isl_set_dim(ran, isl_dim_set);
3332 if (dim < 0)
3333 return isl_bool_error;
3335 umap = isl_union_map_copy(umap);
3336 umap = isl_union_map_intersect_range(umap,
3337 isl_union_set_from_set(isl_set_copy(ran)));
3338 return plain_injective_on_range(umap, 0, dim);
3341 /* Check if the given union_map is obviously injective.
3343 * In particular, we first check if all individual maps are obviously
3344 * injective and then check if all the ranges of these maps are
3345 * obviously disjoint.
3347 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3349 isl_bool in;
3350 isl_union_map *univ;
3351 isl_union_set *ran;
3353 in = union_map_forall(umap, &isl_map_plain_is_injective);
3354 if (in < 0)
3355 return isl_bool_error;
3356 if (!in)
3357 return isl_bool_false;
3359 univ = isl_union_map_universe(isl_union_map_copy(umap));
3360 ran = isl_union_map_range(univ);
3362 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3364 isl_union_set_free(ran);
3366 return in;
3369 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3371 isl_bool sv;
3373 sv = isl_union_map_is_single_valued(umap);
3374 if (sv < 0 || !sv)
3375 return sv;
3377 return isl_union_map_is_injective(umap);
3380 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3382 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3383 struct isl_un_op_control control = {
3384 .filter = &un_op_filter_drop_user,
3385 .filter_user = &data,
3386 .fn_map = &isl_map_zip,
3388 return un_op(umap, &control);
3391 /* Given a union map, take the maps of the form A -> (B -> C) and
3392 * return the union of the corresponding maps (A -> B) -> C.
3394 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3396 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3397 struct isl_un_op_control control = {
3398 .filter = &un_op_filter_drop_user,
3399 .filter_user = &data,
3400 .fn_map = &isl_map_uncurry,
3402 return un_op(umap, &control);
3405 /* Given a union map, take the maps of the form (A -> B) -> C and
3406 * return the union of the corresponding maps A -> (B -> C).
3408 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3410 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3411 struct isl_un_op_control control = {
3412 .filter = &un_op_filter_drop_user,
3413 .filter_user = &data,
3414 .fn_map = &isl_map_curry,
3416 return un_op(umap, &control);
3419 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3420 * return the union of the corresponding maps A -> (B -> (C -> D)).
3422 __isl_give isl_union_map *isl_union_map_range_curry(
3423 __isl_take isl_union_map *umap)
3425 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3426 struct isl_un_op_control control = {
3427 .filter = &un_op_filter_drop_user,
3428 .filter_user = &data,
3429 .fn_map = &isl_map_range_curry,
3431 return un_op(umap, &control);
3434 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3436 struct isl_un_op_control control = {
3437 .fn_map = &isl_set_lift,
3439 return un_op(uset, &control);
3442 static isl_stat coefficients_entry(void **entry, void *user)
3444 isl_set *set = *entry;
3445 isl_union_set **res = user;
3447 set = isl_set_copy(set);
3448 set = isl_set_from_basic_set(isl_set_coefficients(set));
3449 *res = isl_union_set_add_set(*res, set);
3451 return isl_stat_ok;
3454 __isl_give isl_union_set *isl_union_set_coefficients(
3455 __isl_take isl_union_set *uset)
3457 isl_ctx *ctx;
3458 isl_space *space;
3459 isl_union_set *res;
3461 if (!uset)
3462 return NULL;
3464 ctx = isl_union_set_get_ctx(uset);
3465 space = isl_space_set_alloc(ctx, 0, 0);
3466 res = isl_union_map_alloc(space, uset->table.n);
3467 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3468 &coefficients_entry, &res) < 0)
3469 goto error;
3471 isl_union_set_free(uset);
3472 return res;
3473 error:
3474 isl_union_set_free(uset);
3475 isl_union_set_free(res);
3476 return NULL;
3479 static isl_stat solutions_entry(void **entry, void *user)
3481 isl_set *set = *entry;
3482 isl_union_set **res = user;
3484 set = isl_set_copy(set);
3485 set = isl_set_from_basic_set(isl_set_solutions(set));
3486 if (!*res)
3487 *res = isl_union_set_from_set(set);
3488 else
3489 *res = isl_union_set_add_set(*res, set);
3491 if (!*res)
3492 return isl_stat_error;
3494 return isl_stat_ok;
3497 __isl_give isl_union_set *isl_union_set_solutions(
3498 __isl_take isl_union_set *uset)
3500 isl_union_set *res = NULL;
3502 if (!uset)
3503 return NULL;
3505 if (uset->table.n == 0) {
3506 res = isl_union_set_empty(isl_union_set_get_space(uset));
3507 isl_union_set_free(uset);
3508 return res;
3511 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3512 &solutions_entry, &res) < 0)
3513 goto error;
3515 isl_union_set_free(uset);
3516 return res;
3517 error:
3518 isl_union_set_free(uset);
3519 isl_union_set_free(res);
3520 return NULL;
3523 /* Is the domain space of "map" equal to "space"?
3525 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3527 return isl_map_space_tuple_is_equal(map, isl_dim_in,
3528 space, isl_dim_out);
3531 /* Is the range space of "map" equal to "space"?
3533 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3535 return isl_map_space_tuple_is_equal(map, isl_dim_out,
3536 space, isl_dim_out);
3539 /* Is the set space of "map" equal to "space"?
3541 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3543 return isl_map_space_tuple_is_equal(map, isl_dim_set,
3544 space, isl_dim_out);
3547 /* Internal data structure for preimage_pw_multi_aff.
3549 * "pma" is the function under which the preimage should be taken.
3550 * "space" is the space of "pma".
3551 * "res" collects the results.
3552 * "fn" computes the preimage for a given map.
3553 * "match" returns true if "fn" can be called.
3555 struct isl_union_map_preimage_data {
3556 isl_space *space;
3557 isl_pw_multi_aff *pma;
3558 isl_union_map *res;
3559 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3560 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3561 __isl_take isl_pw_multi_aff *pma);
3564 /* Call data->fn to compute the preimage of the domain or range of *entry
3565 * under the function represented by data->pma, provided the domain/range
3566 * space of *entry matches the target space of data->pma
3567 * (as given by data->match), and add the result to data->res.
3569 static isl_stat preimage_entry(void **entry, void *user)
3571 int m;
3572 isl_map *map = *entry;
3573 struct isl_union_map_preimage_data *data = user;
3574 isl_bool empty;
3576 m = data->match(map, data->space);
3577 if (m < 0)
3578 return isl_stat_error;
3579 if (!m)
3580 return isl_stat_ok;
3582 map = isl_map_copy(map);
3583 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3585 empty = isl_map_is_empty(map);
3586 if (empty < 0 || empty) {
3587 isl_map_free(map);
3588 return empty < 0 ? isl_stat_error : isl_stat_ok;
3591 data->res = isl_union_map_add_map(data->res, map);
3593 return isl_stat_ok;
3596 /* Compute the preimage of the domain or range of "umap" under the function
3597 * represented by "pma".
3598 * In other words, plug in "pma" in the domain or range of "umap".
3599 * The function "fn" performs the actual preimage computation on a map,
3600 * while "match" determines to which maps the function should be applied.
3602 static __isl_give isl_union_map *preimage_pw_multi_aff(
3603 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3604 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3605 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3606 __isl_take isl_pw_multi_aff *pma))
3608 isl_ctx *ctx;
3609 isl_space *space;
3610 struct isl_union_map_preimage_data data;
3612 umap = isl_union_map_align_params(umap,
3613 isl_pw_multi_aff_get_space(pma));
3614 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3616 if (!umap || !pma)
3617 goto error;
3619 ctx = isl_union_map_get_ctx(umap);
3620 space = isl_union_map_get_space(umap);
3621 data.space = isl_pw_multi_aff_get_space(pma);
3622 data.pma = pma;
3623 data.res = isl_union_map_alloc(space, umap->table.n);
3624 data.match = match;
3625 data.fn = fn;
3626 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3627 &data) < 0)
3628 data.res = isl_union_map_free(data.res);
3630 isl_space_free(data.space);
3631 isl_union_map_free(umap);
3632 isl_pw_multi_aff_free(pma);
3633 return data.res;
3634 error:
3635 isl_union_map_free(umap);
3636 isl_pw_multi_aff_free(pma);
3637 return NULL;
3640 /* Compute the preimage of the domain of "umap" under the function
3641 * represented by "pma".
3642 * In other words, plug in "pma" in the domain of "umap".
3643 * The result contains maps that live in the same spaces as the maps of "umap"
3644 * with domain space equal to the target space of "pma",
3645 * except that the domain has been replaced by the domain space of "pma".
3647 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3648 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3650 return preimage_pw_multi_aff(umap, pma, &domain_match,
3651 &isl_map_preimage_domain_pw_multi_aff);
3654 /* Compute the preimage of the range of "umap" under the function
3655 * represented by "pma".
3656 * In other words, plug in "pma" in the range of "umap".
3657 * The result contains maps that live in the same spaces as the maps of "umap"
3658 * with range space equal to the target space of "pma",
3659 * except that the range has been replaced by the domain space of "pma".
3661 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3662 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3664 return preimage_pw_multi_aff(umap, pma, &range_match,
3665 &isl_map_preimage_range_pw_multi_aff);
3668 /* Compute the preimage of "uset" under the function represented by "pma".
3669 * In other words, plug in "pma" in "uset".
3670 * The result contains sets that live in the same spaces as the sets of "uset"
3671 * with space equal to the target space of "pma",
3672 * except that the space has been replaced by the domain space of "pma".
3674 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3675 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3677 return preimage_pw_multi_aff(uset, pma, &set_match,
3678 &isl_set_preimage_pw_multi_aff);
3681 /* Compute the preimage of the domain of "umap" under the function
3682 * represented by "ma".
3683 * In other words, plug in "ma" in the domain of "umap".
3684 * The result contains maps that live in the same spaces as the maps of "umap"
3685 * with domain space equal to the target space of "ma",
3686 * except that the domain has been replaced by the domain space of "ma".
3688 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3689 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3691 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3692 isl_pw_multi_aff_from_multi_aff(ma));
3695 /* Compute the preimage of the range of "umap" under the function
3696 * represented by "ma".
3697 * In other words, plug in "ma" in the range of "umap".
3698 * The result contains maps that live in the same spaces as the maps of "umap"
3699 * with range space equal to the target space of "ma",
3700 * except that the range has been replaced by the domain space of "ma".
3702 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3703 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3705 return isl_union_map_preimage_range_pw_multi_aff(umap,
3706 isl_pw_multi_aff_from_multi_aff(ma));
3709 /* Compute the preimage of "uset" under the function represented by "ma".
3710 * In other words, plug in "ma" in "uset".
3711 * The result contains sets that live in the same spaces as the sets of "uset"
3712 * with space equal to the target space of "ma",
3713 * except that the space has been replaced by the domain space of "ma".
3715 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3716 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3718 return isl_union_set_preimage_pw_multi_aff(uset,
3719 isl_pw_multi_aff_from_multi_aff(ma));
3722 /* Internal data structure for preimage_multi_pw_aff.
3724 * "mpa" is the function under which the preimage should be taken.
3725 * "space" is the space of "mpa".
3726 * "res" collects the results.
3727 * "fn" computes the preimage for a given map.
3728 * "match" returns true if "fn" can be called.
3730 struct isl_union_map_preimage_mpa_data {
3731 isl_space *space;
3732 isl_multi_pw_aff *mpa;
3733 isl_union_map *res;
3734 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3735 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3736 __isl_take isl_multi_pw_aff *mpa);
3739 /* Call data->fn to compute the preimage of the domain or range of *entry
3740 * under the function represented by data->mpa, provided the domain/range
3741 * space of *entry matches the target space of data->mpa
3742 * (as given by data->match), and add the result to data->res.
3744 static isl_stat preimage_mpa_entry(void **entry, void *user)
3746 int m;
3747 isl_map *map = *entry;
3748 struct isl_union_map_preimage_mpa_data *data = user;
3749 isl_bool empty;
3751 m = data->match(map, data->space);
3752 if (m < 0)
3753 return isl_stat_error;
3754 if (!m)
3755 return isl_stat_ok;
3757 map = isl_map_copy(map);
3758 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3760 empty = isl_map_is_empty(map);
3761 if (empty < 0 || empty) {
3762 isl_map_free(map);
3763 return empty < 0 ? isl_stat_error : isl_stat_ok;
3766 data->res = isl_union_map_add_map(data->res, map);
3768 return isl_stat_ok;
3771 /* Compute the preimage of the domain or range of "umap" under the function
3772 * represented by "mpa".
3773 * In other words, plug in "mpa" in the domain or range of "umap".
3774 * The function "fn" performs the actual preimage computation on a map,
3775 * while "match" determines to which maps the function should be applied.
3777 static __isl_give isl_union_map *preimage_multi_pw_aff(
3778 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3779 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3780 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3781 __isl_take isl_multi_pw_aff *mpa))
3783 isl_ctx *ctx;
3784 isl_space *space;
3785 struct isl_union_map_preimage_mpa_data data;
3787 umap = isl_union_map_align_params(umap,
3788 isl_multi_pw_aff_get_space(mpa));
3789 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3791 if (!umap || !mpa)
3792 goto error;
3794 ctx = isl_union_map_get_ctx(umap);
3795 space = isl_union_map_get_space(umap);
3796 data.space = isl_multi_pw_aff_get_space(mpa);
3797 data.mpa = mpa;
3798 data.res = isl_union_map_alloc(space, umap->table.n);
3799 data.match = match;
3800 data.fn = fn;
3801 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3802 &data) < 0)
3803 data.res = isl_union_map_free(data.res);
3805 isl_space_free(data.space);
3806 isl_union_map_free(umap);
3807 isl_multi_pw_aff_free(mpa);
3808 return data.res;
3809 error:
3810 isl_union_map_free(umap);
3811 isl_multi_pw_aff_free(mpa);
3812 return NULL;
3815 /* Compute the preimage of the domain of "umap" under the function
3816 * represented by "mpa".
3817 * In other words, plug in "mpa" in the domain of "umap".
3818 * The result contains maps that live in the same spaces as the maps of "umap"
3819 * with domain space equal to the target space of "mpa",
3820 * except that the domain has been replaced by the domain space of "mpa".
3822 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3823 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3825 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3826 &isl_map_preimage_domain_multi_pw_aff);
3829 /* Internal data structure for preimage_upma.
3831 * "umap" is the map of which the preimage should be computed.
3832 * "res" collects the results.
3833 * "fn" computes the preimage for a given piecewise multi-affine function.
3835 struct isl_union_map_preimage_upma_data {
3836 isl_union_map *umap;
3837 isl_union_map *res;
3838 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3839 __isl_take isl_pw_multi_aff *pma);
3842 /* Call data->fn to compute the preimage of the domain or range of data->umap
3843 * under the function represented by pma and add the result to data->res.
3845 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3847 struct isl_union_map_preimage_upma_data *data = user;
3848 isl_union_map *umap;
3850 umap = isl_union_map_copy(data->umap);
3851 umap = data->fn(umap, pma);
3852 data->res = isl_union_map_union(data->res, umap);
3854 return data->res ? isl_stat_ok : isl_stat_error;
3857 /* Compute the preimage of the domain or range of "umap" under the function
3858 * represented by "upma".
3859 * In other words, plug in "upma" in the domain or range of "umap".
3860 * The function "fn" performs the actual preimage computation
3861 * on a piecewise multi-affine function.
3863 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3864 __isl_take isl_union_map *umap,
3865 __isl_take isl_union_pw_multi_aff *upma,
3866 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3867 __isl_take isl_pw_multi_aff *pma))
3869 struct isl_union_map_preimage_upma_data data;
3871 data.umap = umap;
3872 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3873 data.fn = fn;
3874 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3875 &preimage_upma, &data) < 0)
3876 data.res = isl_union_map_free(data.res);
3878 isl_union_map_free(umap);
3879 isl_union_pw_multi_aff_free(upma);
3881 return data.res;
3884 /* Compute the preimage of the domain of "umap" under the function
3885 * represented by "upma".
3886 * In other words, plug in "upma" in the domain of "umap".
3887 * The result contains maps that live in the same spaces as the maps of "umap"
3888 * with domain space equal to one of the target spaces of "upma",
3889 * except that the domain has been replaced by one of the domain spaces that
3890 * correspond to that target space of "upma".
3892 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3893 __isl_take isl_union_map *umap,
3894 __isl_take isl_union_pw_multi_aff *upma)
3896 return preimage_union_pw_multi_aff(umap, upma,
3897 &isl_union_map_preimage_domain_pw_multi_aff);
3900 /* Compute the preimage of the range of "umap" under the function
3901 * represented by "upma".
3902 * In other words, plug in "upma" in the range of "umap".
3903 * The result contains maps that live in the same spaces as the maps of "umap"
3904 * with range space equal to one of the target spaces of "upma",
3905 * except that the range has been replaced by one of the domain spaces that
3906 * correspond to that target space of "upma".
3908 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3909 __isl_take isl_union_map *umap,
3910 __isl_take isl_union_pw_multi_aff *upma)
3912 return preimage_union_pw_multi_aff(umap, upma,
3913 &isl_union_map_preimage_range_pw_multi_aff);
3916 /* Compute the preimage of "uset" under the function represented by "upma".
3917 * In other words, plug in "upma" in the range of "uset".
3918 * The result contains sets that live in the same spaces as the sets of "uset"
3919 * with space equal to one of the target spaces of "upma",
3920 * except that the space has been replaced by one of the domain spaces that
3921 * correspond to that target space of "upma".
3923 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3924 __isl_take isl_union_set *uset,
3925 __isl_take isl_union_pw_multi_aff *upma)
3927 return preimage_union_pw_multi_aff(uset, upma,
3928 &isl_union_set_preimage_pw_multi_aff);
3931 /* Reset the user pointer on all identifiers of parameters and tuples
3932 * of the spaces of "umap".
3934 __isl_give isl_union_map *isl_union_map_reset_user(
3935 __isl_take isl_union_map *umap)
3937 umap = isl_union_map_cow(umap);
3938 if (!umap)
3939 return NULL;
3940 umap->dim = isl_space_reset_user(umap->dim);
3941 if (!umap->dim)
3942 return isl_union_map_free(umap);
3943 return total(umap, &isl_map_reset_user);
3946 /* Reset the user pointer on all identifiers of parameters and tuples
3947 * of the spaces of "uset".
3949 __isl_give isl_union_set *isl_union_set_reset_user(
3950 __isl_take isl_union_set *uset)
3952 return isl_union_map_reset_user(uset);
3955 /* Remove all existentially quantified variables and integer divisions
3956 * from "umap" using Fourier-Motzkin elimination.
3958 __isl_give isl_union_map *isl_union_map_remove_divs(
3959 __isl_take isl_union_map *umap)
3961 return total(umap, &isl_map_remove_divs);
3964 /* Remove all existentially quantified variables and integer divisions
3965 * from "uset" using Fourier-Motzkin elimination.
3967 __isl_give isl_union_set *isl_union_set_remove_divs(
3968 __isl_take isl_union_set *uset)
3970 return isl_union_map_remove_divs(uset);
3973 /* Internal data structure for isl_union_map_project_out.
3974 * "type", "first" and "n" are the arguments for the isl_map_project_out
3975 * call.
3976 * "res" collects the results.
3978 struct isl_union_map_project_out_data {
3979 enum isl_dim_type type;
3980 unsigned first;
3981 unsigned n;
3983 isl_union_map *res;
3986 /* Turn the data->n dimensions of type data->type, starting at data->first
3987 * into existentially quantified variables and add the result to data->res.
3989 static isl_stat project_out(__isl_take isl_map *map, void *user)
3991 struct isl_union_map_project_out_data *data = user;
3993 map = isl_map_project_out(map, data->type, data->first, data->n);
3994 data->res = isl_union_map_add_map(data->res, map);
3996 return isl_stat_ok;
3999 /* Turn the "n" dimensions of type "type", starting at "first"
4000 * into existentially quantified variables.
4001 * Since the space of an isl_union_map only contains parameters,
4002 * type is required to be equal to isl_dim_param.
4004 __isl_give isl_union_map *isl_union_map_project_out(
4005 __isl_take isl_union_map *umap,
4006 enum isl_dim_type type, unsigned first, unsigned n)
4008 isl_space *space;
4009 struct isl_union_map_project_out_data data = { type, first, n };
4011 if (!umap)
4012 return NULL;
4014 if (type != isl_dim_param)
4015 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4016 "can only project out parameters",
4017 return isl_union_map_free(umap));
4019 space = isl_union_map_get_space(umap);
4020 space = isl_space_drop_dims(space, type, first, n);
4021 data.res = isl_union_map_empty(space);
4022 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
4023 data.res = isl_union_map_free(data.res);
4025 isl_union_map_free(umap);
4027 return data.res;
4030 #undef TYPE
4031 #define TYPE isl_union_map
4032 #include "isl_project_out_all_params_templ.c"
4033 #include "isl_project_out_param_templ.c"
4035 /* Turn the "n" dimensions of type "type", starting at "first"
4036 * into existentially quantified variables.
4037 * Since the space of an isl_union_set only contains parameters,
4038 * "type" is required to be equal to isl_dim_param.
4040 __isl_give isl_union_set *isl_union_set_project_out(
4041 __isl_take isl_union_set *uset,
4042 enum isl_dim_type type, unsigned first, unsigned n)
4044 return isl_union_map_project_out(uset, type, first, n);
4047 /* Project out all parameters from "uset" by existentially quantifying
4048 * over them.
4050 __isl_give isl_union_set *isl_union_set_project_out_all_params(
4051 __isl_take isl_union_set *uset)
4053 return uset_from_umap(
4054 isl_union_map_project_out_all_params(uset_to_umap(uset)));
4057 /* Internal data structure for isl_union_map_involves_dims.
4058 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
4060 struct isl_union_map_involves_dims_data {
4061 unsigned first;
4062 unsigned n;
4065 /* Does "map" _not_ involve the data->n parameters starting at data->first?
4067 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
4069 struct isl_union_map_involves_dims_data *data = user;
4070 isl_bool involves;
4072 involves = isl_map_involves_dims(map,
4073 isl_dim_param, data->first, data->n);
4074 return isl_bool_not(involves);
4077 /* Does "umap" involve any of the n parameters starting at first?
4078 * "type" is required to be set to isl_dim_param.
4080 * "umap" involves any of those parameters if any of its maps
4081 * involve the parameters. In other words, "umap" does not
4082 * involve any of the parameters if all its maps to not
4083 * involve the parameters.
4085 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
4086 enum isl_dim_type type, unsigned first, unsigned n)
4088 struct isl_union_map_involves_dims_data data = { first, n };
4089 isl_bool excludes;
4091 if (type != isl_dim_param)
4092 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4093 "can only reference parameters", return isl_bool_error);
4095 excludes = union_map_forall_user(umap, &map_excludes, &data);
4097 return isl_bool_not(excludes);
4100 /* Internal data structure for isl_union_map_reset_range_space.
4101 * "range" is the space from which to set the range space.
4102 * "res" collects the results.
4104 struct isl_union_map_reset_range_space_data {
4105 isl_space *range;
4106 isl_union_map *res;
4109 /* Replace the range space of "map" by the range space of data->range and
4110 * add the result to data->res.
4112 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
4114 struct isl_union_map_reset_range_space_data *data = user;
4115 isl_space *space;
4117 space = isl_map_get_space(map);
4118 space = isl_space_domain(space);
4119 space = isl_space_extend_domain_with_range(space,
4120 isl_space_copy(data->range));
4121 map = isl_map_reset_space(map, space);
4122 data->res = isl_union_map_add_map(data->res, map);
4124 return data->res ? isl_stat_ok : isl_stat_error;
4127 /* Replace the range space of all the maps in "umap" by
4128 * the range space of "space".
4130 * This assumes that all maps have the same output dimension.
4131 * This function should therefore not be made publicly available.
4133 * Since the spaces of the maps change, so do their hash value.
4134 * We therefore need to create a new isl_union_map.
4136 __isl_give isl_union_map *isl_union_map_reset_range_space(
4137 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4139 struct isl_union_map_reset_range_space_data data = { space };
4141 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4142 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
4143 data.res = isl_union_map_free(data.res);
4145 isl_space_free(space);
4146 isl_union_map_free(umap);
4147 return data.res;
4150 /* Check that "umap" and "space" have the same number of parameters.
4152 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
4153 __isl_keep isl_space *space)
4155 isl_size dim1, dim2;
4157 dim1 = isl_union_map_dim(umap, isl_dim_param);
4158 dim2 = isl_space_dim(space, isl_dim_param);
4159 if (dim1 < 0 || dim2 < 0)
4160 return isl_stat_error;
4161 if (dim1 == dim2)
4162 return isl_stat_ok;
4163 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4164 "number of parameters does not match", return isl_stat_error);
4167 /* Internal data structure for isl_union_map_reset_equal_dim_space.
4168 * "space" is the target space.
4169 * "res" collects the results.
4171 struct isl_union_map_reset_params_data {
4172 isl_space *space;
4173 isl_union_map *res;
4176 /* Replace the parameters of "map" by those of data->space and
4177 * add the result to data->res.
4179 static isl_stat reset_params(__isl_take isl_map *map, void *user)
4181 struct isl_union_map_reset_params_data *data = user;
4182 isl_space *space;
4184 space = isl_map_get_space(map);
4185 space = isl_space_replace_params(space, data->space);
4186 map = isl_map_reset_equal_dim_space(map, space);
4187 data->res = isl_union_map_add_map(data->res, map);
4189 return data->res ? isl_stat_ok : isl_stat_error;
4192 /* Replace the space of "umap" by "space", without modifying
4193 * the dimension of "umap", i.e., the number of parameters of "umap".
4195 * Since the hash values of the maps in the union map depend
4196 * on the parameters, a new union map needs to be constructed.
4198 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
4199 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4201 struct isl_union_map_reset_params_data data = { space };
4202 isl_bool equal;
4203 isl_space *umap_space;
4205 umap_space = isl_union_map_peek_space(umap);
4206 equal = isl_space_is_equal(umap_space, space);
4207 if (equal < 0)
4208 goto error;
4209 if (equal) {
4210 isl_space_free(space);
4211 return umap;
4213 if (check_union_map_space_equal_dim(umap, space) < 0)
4214 goto error;
4216 data.res = isl_union_map_empty(isl_space_copy(space));
4217 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
4218 data.res = isl_union_map_free(data.res);
4220 isl_space_free(space);
4221 isl_union_map_free(umap);
4222 return data.res;
4223 error:
4224 isl_union_map_free(umap);
4225 isl_space_free(space);
4226 return NULL;
4229 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4230 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4231 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4232 * to the domain and the range of each map.
4233 * "res" collects the results.
4235 struct isl_union_order_at_data {
4236 isl_multi_union_pw_aff *mupa;
4237 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4238 __isl_take isl_multi_pw_aff *mpa2);
4239 isl_union_map *res;
4242 /* Intersect "map" with the result of applying data->order to
4243 * the functions in data->mupa that apply to the domain and the range
4244 * of "map" and add the result to data->res.
4246 static isl_stat order_at(__isl_take isl_map *map, void *user)
4248 struct isl_union_order_at_data *data = user;
4249 isl_space *space;
4250 isl_multi_pw_aff *mpa1, *mpa2;
4251 isl_map *order;
4253 space = isl_space_domain(isl_map_get_space(map));
4254 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4255 space = isl_space_range(isl_map_get_space(map));
4256 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4257 order = data->order(mpa1, mpa2);
4258 map = isl_map_intersect(map, order);
4259 data->res = isl_union_map_add_map(data->res, map);
4261 return data->res ? isl_stat_ok : isl_stat_error;
4264 /* If "mupa" has a non-trivial explicit domain, then intersect
4265 * domain and range of "umap" with this explicit domain.
4266 * If the explicit domain only describes constraints on the parameters,
4267 * then the intersection only needs to be performed once.
4269 static __isl_give isl_union_map *intersect_explicit_domain(
4270 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4272 isl_bool non_trivial, is_params;
4273 isl_union_set *dom;
4275 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4276 if (non_trivial < 0)
4277 return isl_union_map_free(umap);
4278 if (!non_trivial)
4279 return umap;
4280 mupa = isl_multi_union_pw_aff_copy(mupa);
4281 dom = isl_multi_union_pw_aff_domain(mupa);
4282 is_params = isl_union_set_is_params(dom);
4283 if (is_params < 0) {
4284 isl_union_set_free(dom);
4285 return isl_union_map_free(umap);
4287 if (is_params) {
4288 isl_set *set;
4290 set = isl_union_set_params(dom);
4291 umap = isl_union_map_intersect_params(umap, set);
4292 return umap;
4294 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4295 umap = isl_union_map_intersect_range(umap, dom);
4296 return umap;
4299 /* Intersect each map in "umap" with the result of calling "order"
4300 * on the functions is "mupa" that apply to the domain and the range
4301 * of the map.
4303 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4304 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4305 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4306 __isl_take isl_multi_pw_aff *mpa2))
4308 struct isl_union_order_at_data data;
4310 umap = isl_union_map_align_params(umap,
4311 isl_multi_union_pw_aff_get_space(mupa));
4312 mupa = isl_multi_union_pw_aff_align_params(mupa,
4313 isl_union_map_get_space(umap));
4314 umap = intersect_explicit_domain(umap, mupa);
4315 data.mupa = mupa;
4316 data.order = order;
4317 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4318 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4319 data.res = isl_union_map_free(data.res);
4321 isl_multi_union_pw_aff_free(mupa);
4322 isl_union_map_free(umap);
4323 return data.res;
4326 /* Return the subset of "umap" where the domain and the range
4327 * have equal "mupa" values.
4329 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4330 __isl_take isl_union_map *umap,
4331 __isl_take isl_multi_union_pw_aff *mupa)
4333 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4334 &isl_multi_pw_aff_eq_map);
4337 #undef ORDER
4338 #define ORDER le
4339 #include "isl_union_map_lex_templ.c"
4341 #undef ORDER
4342 #define ORDER lt
4343 #include "isl_union_map_lex_templ.c"
4345 #undef ORDER
4346 #define ORDER ge
4347 #include "isl_union_map_lex_templ.c"
4349 #undef ORDER
4350 #define ORDER gt
4351 #include "isl_union_map_lex_templ.c"
4353 /* Return the union of the elements in the list "list".
4355 __isl_give isl_union_set *isl_union_set_list_union(
4356 __isl_take isl_union_set_list *list)
4358 int i;
4359 isl_size n;
4360 isl_ctx *ctx;
4361 isl_space *space;
4362 isl_union_set *res;
4364 n = isl_union_set_list_n_union_set(list);
4365 if (n < 0)
4366 goto error;
4368 ctx = isl_union_set_list_get_ctx(list);
4369 space = isl_space_params_alloc(ctx, 0);
4370 res = isl_union_set_empty(space);
4372 for (i = 0; i < n; ++i) {
4373 isl_union_set *uset_i;
4375 uset_i = isl_union_set_list_get_union_set(list, i);
4376 res = isl_union_set_union(res, uset_i);
4379 isl_union_set_list_free(list);
4380 return res;
4381 error:
4382 isl_union_set_list_free(list);
4383 return NULL;
4386 /* Update *hash with the hash value of "map".
4388 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4390 uint32_t *hash = user;
4391 uint32_t map_hash;
4393 map_hash = isl_map_get_hash(map);
4394 isl_hash_hash(*hash, map_hash);
4396 isl_map_free(map);
4397 return isl_stat_ok;
4400 /* Return a hash value that digests "umap".
4402 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4404 uint32_t hash;
4406 if (!umap)
4407 return 0;
4409 hash = isl_hash_init();
4410 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4411 return 0;
4413 return hash;
4416 /* Return a hash value that digests "uset".
4418 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4420 return isl_union_map_get_hash(uset);
4423 /* Add the number of basic sets in "set" to "n".
4425 static isl_stat add_n(__isl_take isl_set *set, void *user)
4427 int *n = user;
4428 isl_size set_n;
4430 set_n = isl_set_n_basic_set(set);
4431 *n += set_n;
4432 isl_set_free(set);
4434 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4437 /* Return the total number of basic sets in "uset".
4439 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4441 int n = 0;
4443 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4444 return -1;
4446 return n;
4449 /* Add the basic sets in "set" to "list".
4451 static isl_stat add_list(__isl_take isl_set *set, void *user)
4453 isl_basic_set_list **list = user;
4454 isl_basic_set_list *list_i;
4456 list_i = isl_set_get_basic_set_list(set);
4457 *list = isl_basic_set_list_concat(*list, list_i);
4458 isl_set_free(set);
4460 if (!*list)
4461 return isl_stat_error;
4462 return isl_stat_ok;
4465 /* Return a list containing all the basic sets in "uset".
4467 * First construct a list of the appropriate size and
4468 * then add all the elements.
4470 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4471 __isl_keep isl_union_set *uset)
4473 int n;
4474 isl_ctx *ctx;
4475 isl_basic_set_list *list;
4477 if (!uset)
4478 return NULL;
4479 ctx = isl_union_set_get_ctx(uset);
4480 n = isl_union_set_n_basic_set(uset);
4481 if (n < 0)
4482 return NULL;
4483 list = isl_basic_set_list_alloc(ctx, n);
4484 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4485 list = isl_basic_set_list_free(list);
4487 return list;
4490 /* Internal data structure for isl_union_map_remove_map_if.
4491 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4493 struct isl_union_map_remove_map_if_data {
4494 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4495 void *user;
4498 /* isl_un_op_control filter that negates the result of data->fn
4499 * called on "map".
4501 static isl_bool not(__isl_keep isl_map *map, void *user)
4503 struct isl_union_map_remove_map_if_data *data = user;
4505 return isl_bool_not(data->fn(map, data->user));
4508 /* Dummy isl_un_op_control transformation callback that
4509 * simply returns the input.
4511 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4513 return map;
4516 /* Call "fn" on every map in "umap" and remove those maps
4517 * for which the callback returns true.
4519 * Use un_op to keep only those maps that are not filtered out,
4520 * applying an identity transformation on them.
4522 __isl_give isl_union_map *isl_union_map_remove_map_if(
4523 __isl_take isl_union_map *umap,
4524 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4526 struct isl_union_map_remove_map_if_data data = { fn, user };
4527 struct isl_un_op_control control = {
4528 .filter = &not,
4529 .filter_user = &data,
4530 .fn_map = &map_id,
4532 return un_op(umap, &control);
4535 /* Does "map" have "space" as domain (ignoring parameters)?
4537 static isl_bool has_domain_space_tuples(__isl_keep isl_map *map, void *user)
4539 isl_space *space = user;
4541 return isl_space_has_domain_tuples(space, isl_map_peek_space(map));
4544 /* Does "map" have "space" as range (ignoring parameters)?
4546 static isl_bool has_range_space_tuples(__isl_keep isl_map *map, void *user)
4548 isl_space *space = user;
4550 return isl_space_has_range_tuples(space, isl_map_peek_space(map));
4553 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4555 static __isl_give isl_map *bind_range(__isl_take isl_map *map, void *user)
4557 isl_multi_id *tuple = user;
4559 return isl_map_bind_range(map, isl_multi_id_copy(tuple));
4562 /* Bind the output dimensions of "umap" to parameters with identifiers
4563 * specified by "tuple", living in the range space of "umap",
4564 * for those maps that have a matching range space.
4566 __isl_give isl_union_set *isl_union_map_bind_range(
4567 __isl_take isl_union_map *umap, __isl_take isl_multi_id *tuple)
4569 struct isl_un_op_control control = {
4570 .filter = &has_range_space_tuples,
4571 .filter_user = isl_multi_id_peek_space(tuple),
4572 .fn_map2 = &bind_range,
4573 .fn_map2_user = tuple,
4575 isl_union_set *bound;
4577 bound = uset_from_umap(un_op(umap, &control));
4578 isl_multi_id_free(tuple);
4579 return bound;
4582 /* Only keep those elements in "umap" that have a domain in "space".
4584 __isl_give isl_union_map *isl_union_map_intersect_domain_space(
4585 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4587 struct isl_un_op_control control = {
4588 .filter = &has_domain_space_tuples,
4589 .filter_user = space,
4592 umap = un_op(umap, &control);
4593 isl_space_free(space);
4594 return umap;
4597 /* Only keep those elements in "umap" that have a range in "space".
4599 __isl_give isl_union_map *isl_union_map_intersect_range_space(
4600 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4602 struct isl_un_op_control control = {
4603 .filter = &has_range_space_tuples,
4604 .filter_user = space,
4607 umap = un_op(umap, &control);
4608 isl_space_free(space);
4609 return umap;