isl_map_coalesce: eliminate unit divs from wrapping constraints
[isl.git] / isl_union_map.c
blob6391d134fa43ec1b674842cd3f362dafe37a6306
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/ctx.h>
19 #include <isl/hash.h>
20 #include <isl_aff_private.h>
21 #include <isl/map.h>
22 #include <isl/set.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl_maybe_map.h>
26 #include <isl_id_private.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
31 #include <uset_to_umap.c>
32 #include <uset_from_umap.c>
33 #include <set_list_from_map_list_inl.c>
35 /* Return the number of parameters of "umap", where "type"
36 * is required to be set to isl_dim_param.
38 isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
39 enum isl_dim_type type)
41 if (!umap)
42 return isl_size_error;
44 if (type != isl_dim_param)
45 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
46 "can only reference parameters", return isl_size_error);
48 return isl_space_dim(umap->dim, type);
51 /* Return the number of parameters of "uset", where "type"
52 * is required to be set to isl_dim_param.
54 isl_size isl_union_set_dim(__isl_keep isl_union_set *uset,
55 enum isl_dim_type type)
57 return isl_union_map_dim(uset, type);
60 /* Return the id of the specified dimension.
62 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
63 enum isl_dim_type type, unsigned pos)
65 if (!umap)
66 return NULL;
68 if (type != isl_dim_param)
69 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
70 "can only reference parameters", return NULL);
72 return isl_space_get_dim_id(umap->dim, type, pos);
75 /* Is this union set a parameter domain?
77 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
79 isl_set *set;
80 isl_bool params;
82 if (!uset)
83 return isl_bool_error;
84 if (uset->table.n != 1)
85 return isl_bool_false;
87 set = isl_set_from_union_set(isl_union_set_copy(uset));
88 params = isl_set_is_params(set);
89 isl_set_free(set);
90 return params;
93 /* Is this union map actually a parameter domain?
94 * Users should never call this function. Outside of isl,
95 * a union map can never be a parameter domain.
97 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
99 return isl_union_set_is_params(uset_from_umap(umap));
102 static __isl_give isl_union_map *isl_union_map_alloc(
103 __isl_take isl_space *space, int size)
105 isl_union_map *umap;
107 space = isl_space_params(space);
108 if (!space)
109 return NULL;
111 umap = isl_calloc_type(space->ctx, isl_union_map);
112 if (!umap) {
113 isl_space_free(space);
114 return NULL;
117 umap->ref = 1;
118 umap->dim = space;
119 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
120 return isl_union_map_free(umap);
122 return umap;
125 /* Create an empty union map without specifying any parameters.
127 __isl_give isl_union_map *isl_union_map_empty_ctx(isl_ctx *ctx)
129 return isl_union_map_empty_space(isl_space_unit(ctx));
132 __isl_give isl_union_map *isl_union_map_empty_space(__isl_take isl_space *space)
134 return isl_union_map_alloc(space, 16);
137 /* This is an alternative name for the function above.
139 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
141 return isl_union_map_empty_space(space);
144 /* Create an empty union set without specifying any parameters.
146 __isl_give isl_union_set *isl_union_set_empty_ctx(isl_ctx *ctx)
148 return uset_from_umap(isl_union_map_empty_ctx(ctx));
151 __isl_give isl_union_set *isl_union_set_empty_space(__isl_take isl_space *space)
153 return uset_from_umap(isl_union_map_empty_space(space));
156 /* This is an alternative name for the function above.
158 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
160 return isl_union_set_empty_space(space);
163 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
165 return umap ? umap->dim->ctx : NULL;
168 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
170 return uset ? uset->dim->ctx : NULL;
173 /* Return the space of "umap".
175 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
177 return umap ? umap->dim : NULL;
180 /* Return the space of "uset".
182 __isl_keep isl_space *isl_union_set_peek_space(__isl_keep isl_union_set *uset)
184 return isl_union_map_peek_space(uset_to_umap(uset));
187 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
189 return isl_space_copy(isl_union_map_peek_space(umap));
192 /* Return the position of the parameter with the given name
193 * in "umap".
194 * Return -1 if no such dimension can be found.
196 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
197 enum isl_dim_type type, const char *name)
199 if (!umap)
200 return -1;
201 return isl_space_find_dim_by_name(umap->dim, type, name);
204 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
206 return isl_union_map_get_space(uset);
209 static isl_stat free_umap_entry(void **entry, void *user)
211 isl_map *map = *entry;
212 isl_map_free(map);
213 return isl_stat_ok;
216 static isl_stat add_map(__isl_take isl_map *map, void *user)
218 isl_union_map **umap = (isl_union_map **)user;
220 *umap = isl_union_map_add_map(*umap, map);
222 return isl_stat_ok;
225 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
227 isl_union_map *dup;
229 if (!umap)
230 return NULL;
232 dup = isl_union_map_empty(isl_space_copy(umap->dim));
233 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
234 goto error;
235 return dup;
236 error:
237 isl_union_map_free(dup);
238 return NULL;
241 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
243 if (!umap)
244 return NULL;
246 if (umap->ref == 1)
247 return umap;
248 umap->ref--;
249 return isl_union_map_dup(umap);
252 struct isl_union_align {
253 isl_reordering *exp;
254 isl_union_map *res;
257 static isl_stat align_entry(void **entry, void *user)
259 isl_map *map = *entry;
260 isl_reordering *exp;
261 struct isl_union_align *data = user;
263 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
264 isl_map_get_space(map));
266 data->res = isl_union_map_add_map(data->res,
267 isl_map_realign(isl_map_copy(map), exp));
269 return isl_stat_ok;
272 /* Align the parameters of umap along those of model.
273 * The result has the parameters of model first, in the same order
274 * as they appear in model, followed by any remaining parameters of
275 * umap that do not appear in model.
277 __isl_give isl_union_map *isl_union_map_align_params(
278 __isl_take isl_union_map *umap, __isl_take isl_space *model)
280 struct isl_union_align data = { NULL, NULL };
281 isl_bool equal_params;
283 if (!umap || !model)
284 goto error;
286 equal_params = isl_space_has_equal_params(umap->dim, model);
287 if (equal_params < 0)
288 goto error;
289 if (equal_params) {
290 isl_space_free(model);
291 return umap;
294 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
295 if (!data.exp)
296 goto error;
298 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
299 umap->table.n);
300 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
301 &align_entry, &data) < 0)
302 goto error;
304 isl_reordering_free(data.exp);
305 isl_union_map_free(umap);
306 isl_space_free(model);
307 return data.res;
308 error:
309 isl_reordering_free(data.exp);
310 isl_union_map_free(umap);
311 isl_union_map_free(data.res);
312 isl_space_free(model);
313 return NULL;
316 __isl_give isl_union_set *isl_union_set_align_params(
317 __isl_take isl_union_set *uset, __isl_take isl_space *model)
319 return isl_union_map_align_params(uset, model);
322 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
323 __isl_take isl_union_map *umap2)
325 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
326 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
328 umap1 = isl_union_map_cow(umap1);
330 if (!umap1 || !umap2)
331 goto error;
333 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
334 goto error;
336 isl_union_map_free(umap2);
338 return umap1;
339 error:
340 isl_union_map_free(umap1);
341 isl_union_map_free(umap2);
342 return NULL;
345 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
346 __isl_take isl_union_set *uset2)
348 return isl_union_map_union(uset1, uset2);
351 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
353 if (!umap)
354 return NULL;
356 umap->ref++;
357 return umap;
360 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
362 return isl_union_map_copy(uset);
365 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
367 if (!umap)
368 return NULL;
370 if (--umap->ref > 0)
371 return NULL;
373 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
374 &free_umap_entry, NULL);
375 isl_hash_table_clear(&umap->table);
376 isl_space_free(umap->dim);
377 free(umap);
378 return NULL;
381 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
383 return isl_union_map_free(uset);
386 /* Do "umap" and "space" have the same parameters?
388 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
389 __isl_keep isl_space *space)
391 isl_space *umap_space;
393 umap_space = isl_union_map_peek_space(umap);
394 return isl_space_has_equal_params(umap_space, space);
397 /* Do "uset" and "space" have the same parameters?
399 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
400 __isl_keep isl_space *space)
402 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
405 static isl_bool has_space(const void *entry, const void *val)
407 isl_map *map = (isl_map *)entry;
408 isl_space *space = (isl_space *) val;
410 return isl_space_is_equal(map->dim, space);
413 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
414 __isl_take isl_map *map)
416 uint32_t hash;
417 struct isl_hash_table_entry *entry;
418 isl_bool aligned;
420 if (!map || !umap)
421 goto error;
423 if (isl_map_plain_is_empty(map)) {
424 isl_map_free(map);
425 return umap;
428 aligned = isl_map_space_has_equal_params(map, umap->dim);
429 if (aligned < 0)
430 goto error;
431 if (!aligned) {
432 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
433 map = isl_map_align_params(map, isl_union_map_get_space(umap));
436 umap = isl_union_map_cow(umap);
438 if (!map || !umap)
439 goto error;
441 hash = isl_space_get_hash(map->dim);
442 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
443 &has_space, map->dim, 1);
444 if (!entry)
445 goto error;
447 if (!entry->data)
448 entry->data = map;
449 else {
450 entry->data = isl_map_union(entry->data, isl_map_copy(map));
451 if (!entry->data)
452 goto error;
453 isl_map_free(map);
456 return umap;
457 error:
458 isl_map_free(map);
459 isl_union_map_free(umap);
460 return NULL;
463 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
464 __isl_take isl_set *set)
466 return isl_union_map_add_map(uset, set_to_map(set));
469 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
471 isl_space *space;
472 isl_union_map *umap;
474 if (!map)
475 return NULL;
477 space = isl_map_get_space(map);
478 space = isl_space_params(space);
479 umap = isl_union_map_empty(space);
480 umap = isl_union_map_add_map(umap, map);
482 return umap;
485 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
487 return isl_union_map_from_map(set_to_map(set));
490 __isl_give isl_union_map *isl_union_map_from_basic_map(
491 __isl_take isl_basic_map *bmap)
493 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
496 __isl_give isl_union_set *isl_union_set_from_basic_set(
497 __isl_take isl_basic_set *bset)
499 return isl_union_map_from_basic_map(bset);
502 struct isl_union_map_foreach_data
504 isl_stat (*fn)(__isl_take isl_map *map, void *user);
505 void *user;
508 static isl_stat call_on_copy(void **entry, void *user)
510 isl_map *map = *entry;
511 struct isl_union_map_foreach_data *data;
512 data = (struct isl_union_map_foreach_data *)user;
514 return data->fn(isl_map_copy(map), data->user);
517 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
519 return umap ? umap->table.n : isl_size_error;
522 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
524 return uset ? uset->table.n : isl_size_error;
527 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
528 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
530 struct isl_union_map_foreach_data data = { fn, user };
532 if (!umap)
533 return isl_stat_error;
535 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
536 &call_on_copy, &data);
539 /* Internal data structure for isl_union_map_every_map.
541 * "test" is the user-specified callback function.
542 * "user" is the user-specified callback function argument.
544 * "failed" is initialized to 0 and set to 1 if "test" fails
545 * on any map.
547 struct isl_union_map_every_data {
548 isl_bool (*test)(__isl_keep isl_map *map, void *user);
549 void *user;
550 int failed;
553 /* Call data->test on "map".
554 * If this fails, then set data->failed and abort.
556 static isl_stat call_every(void **entry, void *user)
558 isl_map *map = *entry;
559 struct isl_union_map_every_data *data = user;
560 isl_bool r;
562 r = data->test(map, data->user);
563 if (r < 0)
564 return isl_stat_error;
565 if (r)
566 return isl_stat_ok;
567 data->failed = 1;
568 return isl_stat_error;
571 /* Does "test" succeed on every map in "umap"?
573 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
574 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
576 struct isl_union_map_every_data data = { test, user, 0 };
577 isl_stat r;
579 if (!umap)
580 return isl_bool_error;
582 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
583 &call_every, &data);
584 if (r >= 0)
585 return isl_bool_true;
586 if (data.failed)
587 return isl_bool_false;
588 return isl_bool_error;
591 /* Add "map" to "list".
593 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
595 isl_map_list **list = user;
597 *list = isl_map_list_add(*list, map);
599 if (!*list)
600 return isl_stat_error;
601 return isl_stat_ok;
604 /* Return the maps in "umap" as a list.
606 * First construct a list of the appropriate size and then add all the
607 * elements.
609 __isl_give isl_map_list *isl_union_map_get_map_list(
610 __isl_keep isl_union_map *umap)
612 isl_size n_maps;
613 isl_ctx *ctx;
614 isl_map_list *list;
616 n_maps = isl_union_map_n_map(umap);
617 if (n_maps < 0)
618 return NULL;
619 ctx = isl_union_map_get_ctx(umap);
620 list = isl_map_list_alloc(ctx, n_maps);
622 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
623 list = isl_map_list_free(list);
625 return list;
628 /* Return the sets in "uset" as a list.
630 __isl_give isl_set_list *isl_union_set_get_set_list(
631 __isl_keep isl_union_set *uset)
633 return set_list_from_map_list(
634 isl_union_map_get_map_list(uset_to_umap(uset)));
637 /* Can "umap" be converted to an isl_map?
638 * That is, does it contain elements in exactly one space?
640 isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
642 isl_size n;
644 n = isl_union_map_n_map(umap);
645 if (n < 0)
646 return isl_bool_error;
647 return isl_bool_ok(n == 1);
650 /* Can "uset" be converted to an isl_set?
651 * That is, does it contain elements in exactly one space?
653 isl_bool isl_union_set_isa_set(__isl_keep isl_union_set *uset)
655 return isl_union_map_isa_map(uset_to_umap(uset));
658 static isl_stat copy_map(void **entry, void *user)
660 isl_map *map = *entry;
661 isl_map **map_p = user;
663 *map_p = isl_map_copy(map);
665 return isl_stat_error;
668 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
670 isl_bool is_map;
671 isl_ctx *ctx;
672 isl_map *map = NULL;
674 is_map = isl_union_map_isa_map(umap);
675 if (is_map < 0)
676 goto error;
677 ctx = isl_union_map_get_ctx(umap);
678 if (!is_map)
679 isl_die(ctx, isl_error_invalid,
680 "union map needs to contain elements in exactly "
681 "one space", goto error);
683 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
685 isl_union_map_free(umap);
687 return map;
688 error:
689 isl_union_map_free(umap);
690 return NULL;
693 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
695 return isl_map_from_union_map(uset);
698 /* Extract the map in "umap" that lives in the given space (ignoring
699 * parameters).
701 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
702 __isl_take isl_space *space)
704 uint32_t hash;
705 struct isl_hash_table_entry *entry;
707 space = isl_space_drop_all_params(space);
708 space = isl_space_align_params(space, isl_union_map_get_space(umap));
709 if (!umap || !space)
710 goto error;
712 hash = isl_space_get_hash(space);
713 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
714 &has_space, space, 0);
715 if (!entry)
716 goto error;
717 if (entry == isl_hash_table_entry_none)
718 return isl_map_empty(space);
719 isl_space_free(space);
720 return isl_map_copy(entry->data);
721 error:
722 isl_space_free(space);
723 return NULL;
726 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
727 __isl_take isl_space *space)
729 return set_from_map(isl_union_map_extract_map(uset, space));
732 /* Check if umap contains a map in the given space.
734 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
735 __isl_keep isl_space *space)
737 uint32_t hash;
738 struct isl_hash_table_entry *entry;
740 if (!umap || !space)
741 return isl_bool_error;
743 hash = isl_space_get_hash(space);
744 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
745 &has_space, space, 0);
746 if (!entry)
747 return isl_bool_error;
748 return isl_bool_ok(entry != isl_hash_table_entry_none);
751 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
752 __isl_keep isl_space *space)
754 return isl_union_map_contains(uset, space);
757 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
758 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
760 return isl_union_map_foreach_map(uset,
761 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
764 /* Internal data structure for isl_union_set_every_set.
766 * "test" is the user-specified callback function.
767 * "user" is the user-specified callback function argument.
769 struct isl_test_set_from_map_data {
770 isl_bool (*test)(__isl_keep isl_set *set, void *user);
771 void *user;
774 /* Call data->test on "map", which is part of an isl_union_set and
775 * therefore known to be an isl_set.
777 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
779 struct isl_test_set_from_map_data *data = user;
781 return data->test(set_from_map(map), data->user);
784 /* Does "test" succeed on every set in "uset"?
786 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
787 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
789 struct isl_test_set_from_map_data data = { test, user };
791 return isl_union_map_every_map(uset_to_umap(uset),
792 &test_set_from_map, &data);
795 struct isl_union_set_foreach_point_data {
796 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
797 void *user;
800 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
802 struct isl_union_set_foreach_point_data *data = user;
803 isl_stat r;
805 r = isl_set_foreach_point(set, data->fn, data->user);
806 isl_set_free(set);
808 return r;
811 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
812 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
814 struct isl_union_set_foreach_point_data data = { fn, user };
815 return isl_union_set_foreach_set(uset, &foreach_point, &data);
818 /* Data structure that specifies how gen_bin_op should
819 * construct results from the inputs.
821 * If "subtract" is set, then a map in the first input is copied to the result
822 * if there is no corresponding map in the second input.
823 * Otherwise, a map in the first input with no corresponding map
824 * in the second input is ignored.
825 * If "filter" is not NULL, then it specifies which maps in the first
826 * input may have a matching map in the second input.
827 * In particular, it makes sure that "match_space" can be called
828 * on the space of the map.
829 * "match_space" specifies how to transform the space of a map
830 * in the first input to the space of the corresponding map
831 * in the second input.
832 * "fn_map" specifies how the matching maps, one from each input,
833 * should be combined to form a map in the result.
835 struct isl_bin_op_control {
836 int subtract;
837 isl_bool (*filter)(__isl_keep isl_map *map);
838 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
839 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
840 __isl_take isl_map *map2);
843 /* Internal data structure for gen_bin_op.
844 * "control" specifies how the maps in the result should be constructed.
845 * "umap2" is a pointer to the second argument.
846 * "res" collects the results.
848 struct isl_union_map_gen_bin_data {
849 struct isl_bin_op_control *control;
850 isl_union_map *umap2;
851 isl_union_map *res;
854 /* Add a copy of "map" to "res" and return the result.
856 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
857 __isl_keep isl_map *map)
859 return isl_union_map_add_map(res, isl_map_copy(map));
862 /* Combine "map1" and "map2", add the result to "res" and return the result.
863 * Check whether the result is empty before adding it to "res".
865 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
866 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
867 struct isl_union_map_gen_bin_data *data)
869 isl_bool empty;
870 isl_map *map;
872 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
873 empty = isl_map_is_empty(map);
874 if (empty < 0 || empty) {
875 isl_map_free(map);
876 if (empty < 0)
877 return isl_union_map_free(res);
878 return res;
880 return isl_union_map_add_map(res, map);
883 /* Dummy match_space function that simply returns the input space.
885 static __isl_give isl_space *identity(__isl_take isl_space *space)
887 return space;
890 /* Look for the map in data->umap2 that corresponds to "map", if any.
891 * Return (isl_bool_true, matching map) if there is one,
892 * (isl_bool_false, NULL) if there is no matching map and
893 * (isl_bool_error, NULL) on error.
895 * If not NULL, then data->control->filter specifies whether "map"
896 * can have any matching map. If so,
897 * data->control->match_space specifies which map in data->umap2
898 * corresponds to "map".
900 static __isl_keep isl_maybe_isl_map bin_try_get_match(
901 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
903 uint32_t hash;
904 struct isl_hash_table_entry *entry2;
905 isl_space *space;
906 isl_maybe_isl_map res = { isl_bool_error, NULL };
908 if (data->control->filter) {
909 res.valid = data->control->filter(map);
910 if (res.valid < 0 || !res.valid)
911 return res;
912 res.valid = isl_bool_error;
915 space = isl_map_get_space(map);
916 if (data->control->match_space != &identity)
917 space = data->control->match_space(space);
918 if (!space)
919 return res;
920 hash = isl_space_get_hash(space);
921 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
922 &data->umap2->table, hash,
923 &has_space, space, 0);
924 isl_space_free(space);
925 if (entry2)
926 res.valid = isl_bool_ok(entry2 != isl_hash_table_entry_none);
927 if (res.valid >= 0 && res.valid)
928 res.value = entry2->data;
930 return res;
933 /* isl_hash_table_foreach callback for gen_bin_op.
934 * Look for the map in data->umap2 that corresponds
935 * to the map that "entry" points to, apply the binary operation and
936 * add the result to data->res.
938 * If no corresponding map can be found, then the effect depends
939 * on data->control->subtract. If it is set, then the current map
940 * is added directly to the result. Otherwise, it is ignored.
942 static isl_stat gen_bin_entry(void **entry, void *user)
944 struct isl_union_map_gen_bin_data *data = user;
945 isl_map *map = *entry;
946 isl_maybe_isl_map m;
948 m = bin_try_get_match(data, map);
949 if (m.valid < 0)
950 return isl_stat_error;
951 if (!m.valid && !data->control->subtract)
952 return isl_stat_ok;
954 if (!m.valid)
955 data->res = bin_add_map(data->res, map);
956 else
957 data->res = bin_add_pair(data->res, map, m.value, data);
958 if (!data->res)
959 return isl_stat_error;
961 return isl_stat_ok;
964 /* Apply a binary operation to "umap1" and "umap2" based on "control".
965 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
966 * in gen_bin_entry.
968 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
969 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
971 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
973 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
974 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
976 if (!umap1 || !umap2)
977 goto error;
979 data.umap2 = umap2;
980 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
981 umap1->table.n);
982 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
983 &gen_bin_entry, &data) < 0)
984 goto error;
986 isl_union_map_free(umap1);
987 isl_union_map_free(umap2);
988 return data.res;
989 error:
990 isl_union_map_free(umap1);
991 isl_union_map_free(umap2);
992 isl_union_map_free(data.res);
993 return NULL;
996 __isl_give isl_union_map *isl_union_map_subtract(
997 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
999 struct isl_bin_op_control control = {
1000 .subtract = 1,
1001 .match_space = &identity,
1002 .fn_map = &isl_map_subtract,
1005 return gen_bin_op(umap1, umap2, &control);
1008 __isl_give isl_union_set *isl_union_set_subtract(
1009 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1011 return isl_union_map_subtract(uset1, uset2);
1014 struct isl_union_map_gen_bin_set_data {
1015 isl_set *set;
1016 isl_union_map *res;
1019 static isl_stat intersect_params_entry(void **entry, void *user)
1021 struct isl_union_map_gen_bin_set_data *data = user;
1022 isl_map *map = *entry;
1023 int empty;
1025 map = isl_map_copy(map);
1026 map = isl_map_intersect_params(map, isl_set_copy(data->set));
1028 empty = isl_map_is_empty(map);
1029 if (empty < 0) {
1030 isl_map_free(map);
1031 return isl_stat_error;
1034 data->res = isl_union_map_add_map(data->res, map);
1036 return isl_stat_ok;
1039 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
1040 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
1042 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
1044 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1045 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1047 if (!umap || !set)
1048 goto error;
1050 data.set = set;
1051 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1052 umap->table.n);
1053 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1054 fn, &data) < 0)
1055 goto error;
1057 isl_union_map_free(umap);
1058 isl_set_free(set);
1059 return data.res;
1060 error:
1061 isl_union_map_free(umap);
1062 isl_set_free(set);
1063 isl_union_map_free(data.res);
1064 return NULL;
1067 /* Intersect "umap" with the parameter domain "set".
1069 * If "set" does not have any constraints, then we can return immediately.
1071 __isl_give isl_union_map *isl_union_map_intersect_params(
1072 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1074 int is_universe;
1076 is_universe = isl_set_plain_is_universe(set);
1077 if (is_universe < 0)
1078 goto error;
1079 if (is_universe) {
1080 isl_set_free(set);
1081 return umap;
1084 return gen_bin_set_op(umap, set, &intersect_params_entry);
1085 error:
1086 isl_union_map_free(umap);
1087 isl_set_free(set);
1088 return NULL;
1091 __isl_give isl_union_set *isl_union_set_intersect_params(
1092 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1094 return isl_union_map_intersect_params(uset, set);
1097 static __isl_give isl_union_map *union_map_intersect_params(
1098 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1100 return isl_union_map_intersect_params(umap,
1101 isl_set_from_union_set(uset));
1104 static __isl_give isl_union_map *union_map_gist_params(
1105 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1107 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1110 struct isl_union_map_match_bin_data {
1111 isl_union_map *umap2;
1112 isl_union_map *res;
1113 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1116 static isl_stat match_bin_entry(void **entry, void *user)
1118 struct isl_union_map_match_bin_data *data = user;
1119 uint32_t hash;
1120 struct isl_hash_table_entry *entry2;
1121 isl_map *map = *entry;
1122 int empty;
1124 hash = isl_space_get_hash(map->dim);
1125 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1126 hash, &has_space, map->dim, 0);
1127 if (!entry2)
1128 return isl_stat_error;
1129 if (entry2 == isl_hash_table_entry_none)
1130 return isl_stat_ok;
1132 map = isl_map_copy(map);
1133 map = data->fn(map, isl_map_copy(entry2->data));
1135 empty = isl_map_is_empty(map);
1136 if (empty < 0) {
1137 isl_map_free(map);
1138 return isl_stat_error;
1140 if (empty) {
1141 isl_map_free(map);
1142 return isl_stat_ok;
1145 data->res = isl_union_map_add_map(data->res, map);
1147 return isl_stat_ok;
1150 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1151 __isl_take isl_union_map *umap2,
1152 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1154 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1156 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1157 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1159 if (!umap1 || !umap2)
1160 goto error;
1162 data.umap2 = umap2;
1163 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1164 umap1->table.n);
1165 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1166 &match_bin_entry, &data) < 0)
1167 goto error;
1169 isl_union_map_free(umap1);
1170 isl_union_map_free(umap2);
1171 return data.res;
1172 error:
1173 isl_union_map_free(umap1);
1174 isl_union_map_free(umap2);
1175 isl_union_map_free(data.res);
1176 return NULL;
1179 __isl_give isl_union_map *isl_union_map_intersect(
1180 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1182 return match_bin_op(umap1, umap2, &isl_map_intersect);
1185 /* Compute the intersection of the two union_sets.
1186 * As a special case, if exactly one of the two union_sets
1187 * is a parameter domain, then intersect the parameter domain
1188 * of the other one with this set.
1190 __isl_give isl_union_set *isl_union_set_intersect(
1191 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1193 int p1, p2;
1195 p1 = isl_union_set_is_params(uset1);
1196 p2 = isl_union_set_is_params(uset2);
1197 if (p1 < 0 || p2 < 0)
1198 goto error;
1199 if (!p1 && p2)
1200 return union_map_intersect_params(uset1, uset2);
1201 if (p1 && !p2)
1202 return union_map_intersect_params(uset2, uset1);
1203 return isl_union_map_intersect(uset1, uset2);
1204 error:
1205 isl_union_set_free(uset1);
1206 isl_union_set_free(uset2);
1207 return NULL;
1210 static isl_stat gist_params_entry(void **entry, void *user)
1212 struct isl_union_map_gen_bin_set_data *data = user;
1213 isl_map *map = *entry;
1214 int empty;
1216 map = isl_map_copy(map);
1217 map = isl_map_gist_params(map, isl_set_copy(data->set));
1219 empty = isl_map_is_empty(map);
1220 if (empty < 0) {
1221 isl_map_free(map);
1222 return isl_stat_error;
1225 data->res = isl_union_map_add_map(data->res, map);
1227 return isl_stat_ok;
1230 __isl_give isl_union_map *isl_union_map_gist_params(
1231 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1233 return gen_bin_set_op(umap, set, &gist_params_entry);
1236 __isl_give isl_union_set *isl_union_set_gist_params(
1237 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1239 return isl_union_map_gist_params(uset, set);
1242 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1243 __isl_take isl_union_map *context)
1245 return match_bin_op(umap, context, &isl_map_gist);
1248 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1249 __isl_take isl_union_set *context)
1251 if (isl_union_set_is_params(context))
1252 return union_map_gist_params(uset, context);
1253 return isl_union_map_gist(uset, context);
1256 /* For each map in "umap", remove the constraints in the corresponding map
1257 * of "context".
1258 * Each map in "context" is assumed to consist of a single disjunct and
1259 * to have explicit representations for all local variables.
1261 __isl_give isl_union_map *isl_union_map_plain_gist(
1262 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1264 return match_bin_op(umap, context, &isl_map_plain_gist);
1267 /* For each set in "uset", remove the constraints in the corresponding set
1268 * of "context".
1269 * Each set in "context" is assumed to consist of a single disjunct and
1270 * to have explicit representations for all local variables.
1272 __isl_give isl_union_set *isl_union_set_plain_gist(
1273 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1275 return isl_union_map_plain_gist(uset, context);
1278 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1279 __isl_take isl_map *set2)
1281 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1284 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1285 __isl_take isl_map *set2)
1287 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1290 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1291 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1293 return match_bin_op(uset1, uset2, &lex_lt_set);
1296 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1297 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1299 return match_bin_op(uset1, uset2, &lex_le_set);
1302 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1303 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1305 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1308 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1309 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1311 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1314 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1315 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1317 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1320 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1321 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1323 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1326 /* Intersect the domain of "umap" with "uset".
1328 static __isl_give isl_union_map *union_map_intersect_domain(
1329 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1331 struct isl_bin_op_control control = {
1332 .match_space = &isl_space_domain,
1333 .fn_map = &isl_map_intersect_domain,
1336 return gen_bin_op(umap, uset, &control);
1339 /* Intersect the domain of "umap" with "uset".
1340 * If "uset" is a parameters domain, then intersect the parameter
1341 * domain of "umap" with this set.
1343 __isl_give isl_union_map *isl_union_map_intersect_domain(
1344 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1346 if (isl_union_set_is_params(uset))
1347 return union_map_intersect_params(umap, uset);
1348 else
1349 return union_map_intersect_domain(umap, uset);
1352 /* Remove the elements of "uset" from the domain of "umap".
1354 __isl_give isl_union_map *isl_union_map_subtract_domain(
1355 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1357 struct isl_bin_op_control control = {
1358 .subtract = 1,
1359 .match_space = &isl_space_domain,
1360 .fn_map = &isl_map_subtract_domain,
1363 return gen_bin_op(umap, dom, &control);
1366 /* Remove the elements of "uset" from the range of "umap".
1368 __isl_give isl_union_map *isl_union_map_subtract_range(
1369 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1371 struct isl_bin_op_control control = {
1372 .subtract = 1,
1373 .match_space = &isl_space_range,
1374 .fn_map = &isl_map_subtract_range,
1377 return gen_bin_op(umap, dom, &control);
1380 /* Compute the gist of "umap" with respect to the domain "uset".
1382 static __isl_give isl_union_map *union_map_gist_domain(
1383 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1385 struct isl_bin_op_control control = {
1386 .match_space = &isl_space_domain,
1387 .fn_map = &isl_map_gist_domain,
1390 return gen_bin_op(umap, uset, &control);
1393 /* Compute the gist of "umap" with respect to the domain "uset".
1394 * If "uset" is a parameters domain, then compute the gist
1395 * with respect to this parameter domain.
1397 __isl_give isl_union_map *isl_union_map_gist_domain(
1398 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1400 if (isl_union_set_is_params(uset))
1401 return union_map_gist_params(umap, uset);
1402 else
1403 return union_map_gist_domain(umap, uset);
1406 /* Compute the gist of "umap" with respect to the range "uset".
1408 __isl_give isl_union_map *isl_union_map_gist_range(
1409 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1411 struct isl_bin_op_control control = {
1412 .match_space = &isl_space_range,
1413 .fn_map = &isl_map_gist_range,
1416 return gen_bin_op(umap, uset, &control);
1419 __isl_give isl_union_map *isl_union_map_intersect_range(
1420 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1422 struct isl_bin_op_control control = {
1423 .match_space = &isl_space_range,
1424 .fn_map = &isl_map_intersect_range,
1427 return gen_bin_op(umap, uset, &control);
1430 /* Intersect each map in "umap" in a space [A -> B] -> C
1431 * with the corresponding map in "factor" in the space B -> C and
1432 * collect the results.
1434 __isl_give isl_union_map *isl_union_map_intersect_domain_factor_range(
1435 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1437 struct isl_bin_op_control control = {
1438 .filter = &isl_map_domain_is_wrapping,
1439 .match_space = &isl_space_domain_factor_range,
1440 .fn_map = &isl_map_intersect_domain_factor_range,
1443 return gen_bin_op(umap, factor, &control);
1446 /* Intersect each map in "umap" in a space A -> [B -> C]
1447 * with the corresponding map in "factor" in the space A -> B and
1448 * collect the results.
1450 __isl_give isl_union_map *isl_union_map_intersect_range_factor_domain(
1451 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1453 struct isl_bin_op_control control = {
1454 .filter = &isl_map_range_is_wrapping,
1455 .match_space = &isl_space_range_factor_domain,
1456 .fn_map = &isl_map_intersect_range_factor_domain,
1459 return gen_bin_op(umap, factor, &control);
1462 /* Intersect each map in "umap" in a space A -> [B -> C]
1463 * with the corresponding map in "factor" in the space A -> C and
1464 * collect the results.
1466 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1467 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1469 struct isl_bin_op_control control = {
1470 .filter = &isl_map_range_is_wrapping,
1471 .match_space = &isl_space_range_factor_range,
1472 .fn_map = &isl_map_intersect_range_factor_range,
1475 return gen_bin_op(umap, factor, &control);
1478 struct isl_union_map_bin_data {
1479 isl_union_map *umap2;
1480 isl_union_map *res;
1481 isl_map *map;
1482 isl_stat (*fn)(void **entry, void *user);
1485 static isl_stat apply_range_entry(void **entry, void *user)
1487 struct isl_union_map_bin_data *data = user;
1488 isl_map *map2 = *entry;
1489 isl_bool empty;
1491 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1492 map2->dim, isl_dim_in))
1493 return isl_stat_ok;
1495 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1497 empty = isl_map_is_empty(map2);
1498 if (empty < 0) {
1499 isl_map_free(map2);
1500 return isl_stat_error;
1502 if (empty) {
1503 isl_map_free(map2);
1504 return isl_stat_ok;
1507 data->res = isl_union_map_add_map(data->res, map2);
1509 return isl_stat_ok;
1512 static isl_stat bin_entry(void **entry, void *user)
1514 struct isl_union_map_bin_data *data = user;
1515 isl_map *map = *entry;
1517 data->map = map;
1518 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1519 data->fn, data) < 0)
1520 return isl_stat_error;
1522 return isl_stat_ok;
1525 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1526 __isl_take isl_union_map *umap2,
1527 isl_stat (*fn)(void **entry, void *user))
1529 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1531 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1532 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1534 if (!umap1 || !umap2)
1535 goto error;
1537 data.umap2 = umap2;
1538 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1539 umap1->table.n);
1540 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1541 &bin_entry, &data) < 0)
1542 goto error;
1544 isl_union_map_free(umap1);
1545 isl_union_map_free(umap2);
1546 return data.res;
1547 error:
1548 isl_union_map_free(umap1);
1549 isl_union_map_free(umap2);
1550 isl_union_map_free(data.res);
1551 return NULL;
1554 __isl_give isl_union_map *isl_union_map_apply_range(
1555 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1557 return bin_op(umap1, umap2, &apply_range_entry);
1560 __isl_give isl_union_map *isl_union_map_apply_domain(
1561 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1563 umap1 = isl_union_map_reverse(umap1);
1564 umap1 = isl_union_map_apply_range(umap1, umap2);
1565 return isl_union_map_reverse(umap1);
1568 __isl_give isl_union_set *isl_union_set_apply(
1569 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1571 return isl_union_map_apply_range(uset, umap);
1574 static isl_stat map_lex_lt_entry(void **entry, void *user)
1576 struct isl_union_map_bin_data *data = user;
1577 isl_map *map2 = *entry;
1579 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1580 map2->dim, isl_dim_out))
1581 return isl_stat_ok;
1583 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1585 data->res = isl_union_map_add_map(data->res, map2);
1587 return isl_stat_ok;
1590 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1591 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1593 return bin_op(umap1, umap2, &map_lex_lt_entry);
1596 static isl_stat map_lex_le_entry(void **entry, void *user)
1598 struct isl_union_map_bin_data *data = user;
1599 isl_map *map2 = *entry;
1601 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1602 map2->dim, isl_dim_out))
1603 return isl_stat_ok;
1605 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1607 data->res = isl_union_map_add_map(data->res, map2);
1609 return isl_stat_ok;
1612 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1613 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1615 return bin_op(umap1, umap2, &map_lex_le_entry);
1618 static isl_stat product_entry(void **entry, void *user)
1620 struct isl_union_map_bin_data *data = user;
1621 isl_map *map2 = *entry;
1623 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1625 data->res = isl_union_map_add_map(data->res, map2);
1627 return isl_stat_ok;
1630 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1631 __isl_take isl_union_map *umap2)
1633 return bin_op(umap1, umap2, &product_entry);
1636 static isl_stat set_product_entry(void **entry, void *user)
1638 struct isl_union_map_bin_data *data = user;
1639 isl_set *set2 = *entry;
1641 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1643 data->res = isl_union_set_add_set(data->res, set2);
1645 return isl_stat_ok;
1648 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1649 __isl_take isl_union_set *uset2)
1651 return bin_op(uset1, uset2, &set_product_entry);
1654 static isl_stat domain_product_entry(void **entry, void *user)
1656 struct isl_union_map_bin_data *data = user;
1657 isl_map *map2 = *entry;
1659 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1660 map2->dim, isl_dim_out))
1661 return isl_stat_ok;
1663 map2 = isl_map_domain_product(isl_map_copy(data->map),
1664 isl_map_copy(map2));
1666 data->res = isl_union_map_add_map(data->res, map2);
1668 return isl_stat_ok;
1671 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1673 __isl_give isl_union_map *isl_union_map_domain_product(
1674 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1676 return bin_op(umap1, umap2, &domain_product_entry);
1679 static isl_stat range_product_entry(void **entry, void *user)
1681 struct isl_union_map_bin_data *data = user;
1682 isl_map *map2 = *entry;
1684 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1685 map2->dim, isl_dim_in))
1686 return isl_stat_ok;
1688 map2 = isl_map_range_product(isl_map_copy(data->map),
1689 isl_map_copy(map2));
1691 data->res = isl_union_map_add_map(data->res, map2);
1693 return isl_stat_ok;
1696 __isl_give isl_union_map *isl_union_map_range_product(
1697 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1699 return bin_op(umap1, umap2, &range_product_entry);
1702 /* If data->map A -> B and "map2" C -> D have the same range space,
1703 * then add (A, C) -> (B * D) to data->res.
1705 static isl_stat flat_domain_product_entry(void **entry, void *user)
1707 struct isl_union_map_bin_data *data = user;
1708 isl_map *map2 = *entry;
1710 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1711 map2->dim, isl_dim_out))
1712 return isl_stat_ok;
1714 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1715 isl_map_copy(map2));
1717 data->res = isl_union_map_add_map(data->res, map2);
1719 return isl_stat_ok;
1722 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1724 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1725 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1727 return bin_op(umap1, umap2, &flat_domain_product_entry);
1730 static isl_stat flat_range_product_entry(void **entry, void *user)
1732 struct isl_union_map_bin_data *data = user;
1733 isl_map *map2 = *entry;
1735 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1736 map2->dim, isl_dim_in))
1737 return isl_stat_ok;
1739 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1740 isl_map_copy(map2));
1742 data->res = isl_union_map_add_map(data->res, map2);
1744 return isl_stat_ok;
1747 __isl_give isl_union_map *isl_union_map_flat_range_product(
1748 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1750 return bin_op(umap1, umap2, &flat_range_product_entry);
1753 /* Data structure that specifies how un_op should modify
1754 * the maps in the union map.
1756 * If "inplace" is set, then the maps in the input union map
1757 * are modified in place. This means that "fn_map" should not
1758 * change the meaning of the map or that the union map only
1759 * has a single reference.
1760 * If "total" is set, then all maps need to be modified and
1761 * the results need to live in the same space.
1762 * Otherwise, a new union map is constructed to store the results.
1763 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1764 * are taken into account. "filter_user" is passed as the second argument
1765 * to "filter". No filter can be set if "inplace" or
1766 * "total" is set.
1767 * Exactly one of "fn_map" or "fn_map2" should be set, specifying
1768 * how the maps (selected by "filter") should be transformed.
1769 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1771 struct isl_un_op_control {
1772 int inplace;
1773 int total;
1774 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1775 void *filter_user;
1776 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1777 __isl_give isl_map *(*fn_map2)(__isl_take isl_map *map, void *user);
1778 void *fn_map2_user;
1781 /* Data structure for wrapping the data for un_op_filter_drop_user.
1782 * "filter" is the function that is being wrapped.
1784 struct isl_un_op_drop_user_data {
1785 isl_bool (*filter)(__isl_keep isl_map *map);
1788 /* Wrapper for isl_un_op_control filters that do not require
1789 * a second argument.
1790 * Simply call data->filter without the second argument.
1792 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1794 struct isl_un_op_drop_user_data *data = user;
1795 return data->filter(map);
1798 /* Internal data structure for "un_op".
1799 * "control" specifies how the maps in the union map should be modified.
1800 * "res" collects the results.
1802 struct isl_union_map_un_data {
1803 struct isl_un_op_control *control;
1804 isl_union_map *res;
1807 /* isl_hash_table_foreach callback for un_op.
1808 * Handle the map that "entry" points to.
1810 * If control->filter is set, then check if this map satisfies the filter.
1811 * If so (or if control->filter is not set), modify the map
1812 * by calling control->fn_map or control->fn_map2 and
1813 * either add the result to data->res or
1814 * replace the original entry by the result (if control->inplace is set).
1816 static isl_stat un_entry(void **entry, void *user)
1818 struct isl_union_map_un_data *data = user;
1819 struct isl_un_op_control *control = data->control;
1820 isl_map *map = *entry;
1822 if (control->filter) {
1823 isl_bool ok;
1825 ok = control->filter(map, control->filter_user);
1826 if (ok < 0)
1827 return isl_stat_error;
1828 if (!ok)
1829 return isl_stat_ok;
1832 map = isl_map_copy(map);
1833 if (control->fn_map2 != NULL)
1834 map = control->fn_map2(map, control->fn_map2_user);
1835 else
1836 map = control->fn_map(map);
1837 if (!map)
1838 return isl_stat_error;
1839 if (control->inplace) {
1840 isl_map_free(*entry);
1841 *entry = map;
1842 } else {
1843 data->res = isl_union_map_add_map(data->res, map);
1844 if (!data->res)
1845 return isl_stat_error;
1848 return isl_stat_ok;
1851 /* Modify the maps in "umap" based on "control".
1852 * If control->inplace is set, then modify the maps in "umap" in-place.
1853 * Otherwise, create a new union map to hold the results.
1854 * If control->total is set, then perform an inplace computation
1855 * if "umap" is only referenced once. Otherwise, create a new union map
1856 * to store the results.
1858 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1859 struct isl_un_op_control *control)
1861 struct isl_union_map_un_data data = { control };
1863 if (!umap)
1864 return NULL;
1865 if (!!control->fn_map == !!control->fn_map2)
1866 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,
1867 "exactly one mapping function should be specified",
1868 return isl_union_map_free(umap));
1869 if ((control->inplace || control->total) && control->filter)
1870 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1871 "inplace/total modification cannot be filtered",
1872 return isl_union_map_free(umap));
1874 if (control->total && umap->ref == 1)
1875 control->inplace = 1;
1876 if (control->inplace) {
1877 data.res = umap;
1878 } else {
1879 isl_space *space;
1881 space = isl_union_map_get_space(umap);
1882 data.res = isl_union_map_alloc(space, umap->table.n);
1884 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1885 &umap->table, &un_entry, &data) < 0)
1886 data.res = isl_union_map_free(data.res);
1888 if (control->inplace)
1889 return data.res;
1890 isl_union_map_free(umap);
1891 return data.res;
1894 __isl_give isl_union_map *isl_union_map_from_range(
1895 __isl_take isl_union_set *uset)
1897 struct isl_un_op_control control = {
1898 .fn_map = &isl_map_from_range,
1900 return un_op(uset, &control);
1903 __isl_give isl_union_map *isl_union_map_from_domain(
1904 __isl_take isl_union_set *uset)
1906 return isl_union_map_reverse(isl_union_map_from_range(uset));
1909 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1910 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1912 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1913 isl_union_map_from_range(range));
1916 /* Modify the maps in "umap" by applying "fn" on them.
1917 * "fn" should apply to all maps in "umap" and should not modify the space.
1919 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1920 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1922 struct isl_un_op_control control = {
1923 .total = 1,
1924 .fn_map = fn,
1927 return un_op(umap, &control);
1930 /* Compute the affine hull of "map" and return the result as an isl_map.
1932 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1934 return isl_map_from_basic_map(isl_map_affine_hull(map));
1937 __isl_give isl_union_map *isl_union_map_affine_hull(
1938 __isl_take isl_union_map *umap)
1940 return total(umap, &isl_map_affine_hull_map);
1943 __isl_give isl_union_set *isl_union_set_affine_hull(
1944 __isl_take isl_union_set *uset)
1946 return isl_union_map_affine_hull(uset);
1949 /* Wrapper around isl_set_combined_lineality_space
1950 * that returns the combined lineality space in the form of an isl_set
1951 * instead of an isl_basic_set.
1953 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1955 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1958 /* For each set in "uset", compute the (linear) hull
1959 * of the lineality spaces of its basic sets and
1960 * collect and return the results.
1962 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1963 __isl_take isl_union_set *uset)
1965 struct isl_un_op_control control = {
1966 .fn_map = &combined_lineality_space,
1968 return un_op(uset, &control);
1971 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1973 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1975 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1978 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1979 __isl_take isl_union_map *umap)
1981 return total(umap, &isl_map_polyhedral_hull_map);
1984 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1985 __isl_take isl_union_set *uset)
1987 return isl_union_map_polyhedral_hull(uset);
1990 /* Compute a superset of the convex hull of "map" that is described
1991 * by only translates of the constraints in the constituents of "map" and
1992 * return the result as an isl_map.
1994 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1996 return isl_map_from_basic_map(isl_map_simple_hull(map));
1999 __isl_give isl_union_map *isl_union_map_simple_hull(
2000 __isl_take isl_union_map *umap)
2002 return total(umap, &isl_map_simple_hull_map);
2005 __isl_give isl_union_set *isl_union_set_simple_hull(
2006 __isl_take isl_union_set *uset)
2008 return isl_union_map_simple_hull(uset);
2011 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
2012 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2014 struct isl_un_op_control control = {
2015 .inplace = 1,
2016 .fn_map = fn,
2019 return un_op(umap, &control);
2022 /* Remove redundant constraints in each of the basic maps of "umap".
2023 * Since removing redundant constraints does not change the meaning
2024 * or the space, the operation can be performed in-place.
2026 __isl_give isl_union_map *isl_union_map_remove_redundancies(
2027 __isl_take isl_union_map *umap)
2029 return inplace(umap, &isl_map_remove_redundancies);
2032 /* Remove redundant constraints in each of the basic sets of "uset".
2034 __isl_give isl_union_set *isl_union_set_remove_redundancies(
2035 __isl_take isl_union_set *uset)
2037 return isl_union_map_remove_redundancies(uset);
2040 __isl_give isl_union_map *isl_union_map_coalesce(
2041 __isl_take isl_union_map *umap)
2043 return inplace(umap, &isl_map_coalesce);
2046 __isl_give isl_union_set *isl_union_set_coalesce(
2047 __isl_take isl_union_set *uset)
2049 return isl_union_map_coalesce(uset);
2052 __isl_give isl_union_map *isl_union_map_detect_equalities(
2053 __isl_take isl_union_map *umap)
2055 return inplace(umap, &isl_map_detect_equalities);
2058 __isl_give isl_union_set *isl_union_set_detect_equalities(
2059 __isl_take isl_union_set *uset)
2061 return isl_union_map_detect_equalities(uset);
2064 __isl_give isl_union_map *isl_union_map_compute_divs(
2065 __isl_take isl_union_map *umap)
2067 return inplace(umap, &isl_map_compute_divs);
2070 __isl_give isl_union_set *isl_union_set_compute_divs(
2071 __isl_take isl_union_set *uset)
2073 return isl_union_map_compute_divs(uset);
2076 __isl_give isl_union_map *isl_union_map_lexmin(
2077 __isl_take isl_union_map *umap)
2079 return total(umap, &isl_map_lexmin);
2082 __isl_give isl_union_set *isl_union_set_lexmin(
2083 __isl_take isl_union_set *uset)
2085 return isl_union_map_lexmin(uset);
2088 __isl_give isl_union_map *isl_union_map_lexmax(
2089 __isl_take isl_union_map *umap)
2091 return total(umap, &isl_map_lexmax);
2094 __isl_give isl_union_set *isl_union_set_lexmax(
2095 __isl_take isl_union_set *uset)
2097 return isl_union_map_lexmax(uset);
2100 /* Return the universe in the space of "map".
2102 static __isl_give isl_map *universe(__isl_take isl_map *map)
2104 isl_space *space;
2106 space = isl_map_get_space(map);
2107 isl_map_free(map);
2108 return isl_map_universe(space);
2111 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2113 struct isl_un_op_control control = {
2114 .fn_map = &universe,
2116 return un_op(umap, &control);
2119 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2121 return isl_union_map_universe(uset);
2124 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2126 struct isl_un_op_control control = {
2127 .fn_map = &isl_map_reverse,
2129 return un_op(umap, &control);
2132 /* Given a union map, take the maps of the form A -> (B -> C) and
2133 * return the union of the corresponding maps A -> (C -> B).
2135 __isl_give isl_union_map *isl_union_map_range_reverse(
2136 __isl_take isl_union_map *umap)
2138 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2139 struct isl_un_op_control control = {
2140 .filter = &un_op_filter_drop_user,
2141 .filter_user = &data,
2142 .fn_map = &isl_map_range_reverse,
2144 return un_op(umap, &control);
2147 /* Compute the parameter domain of the given union map.
2149 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2151 struct isl_un_op_control control = {
2152 .fn_map = &isl_map_params,
2154 int empty;
2156 empty = isl_union_map_is_empty(umap);
2157 if (empty < 0)
2158 goto error;
2159 if (empty) {
2160 isl_space *space;
2161 space = isl_union_map_get_space(umap);
2162 isl_union_map_free(umap);
2163 return isl_set_empty(space);
2165 return isl_set_from_union_set(un_op(umap, &control));
2166 error:
2167 isl_union_map_free(umap);
2168 return NULL;
2171 /* Compute the parameter domain of the given union set.
2173 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2175 return isl_union_map_params(uset);
2178 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2180 struct isl_un_op_control control = {
2181 .fn_map = &isl_map_domain,
2183 return un_op(umap, &control);
2186 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2188 struct isl_un_op_control control = {
2189 .fn_map = &isl_map_range,
2191 return un_op(umap, &control);
2194 __isl_give isl_union_map *isl_union_map_domain_map(
2195 __isl_take isl_union_map *umap)
2197 struct isl_un_op_control control = {
2198 .fn_map = &isl_map_domain_map,
2200 return un_op(umap, &control);
2203 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2204 * add the result to "res".
2206 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2208 isl_union_pw_multi_aff **res = user;
2209 isl_multi_aff *ma;
2210 isl_pw_multi_aff *pma;
2212 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2213 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2214 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2216 return *res ? isl_stat_ok : isl_stat_error;
2220 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2221 * to its domain.
2223 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2224 __isl_take isl_union_map *umap)
2226 isl_union_pw_multi_aff *res;
2228 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2229 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2230 res = isl_union_pw_multi_aff_free(res);
2232 isl_union_map_free(umap);
2233 return res;
2236 __isl_give isl_union_map *isl_union_map_range_map(
2237 __isl_take isl_union_map *umap)
2239 struct isl_un_op_control control = {
2240 .fn_map = &isl_map_range_map,
2242 return un_op(umap, &control);
2245 /* Given a collection of wrapped maps of the form A[B -> C],
2246 * return the collection of maps A[B -> C] -> B.
2248 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2249 __isl_take isl_union_set *uset)
2251 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2252 struct isl_un_op_control control = {
2253 .filter = &un_op_filter_drop_user,
2254 .filter_user = &data,
2255 .fn_map = &isl_set_wrapped_domain_map,
2257 return un_op(uset, &control);
2260 /* Does "map" relate elements from the same space?
2262 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2264 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2265 map->dim, isl_dim_out);
2268 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2270 struct isl_un_op_control control = {
2271 .filter = &equal_tuples,
2272 .fn_map = &isl_map_deltas,
2274 return un_op(umap, &control);
2277 __isl_give isl_union_map *isl_union_map_deltas_map(
2278 __isl_take isl_union_map *umap)
2280 struct isl_un_op_control control = {
2281 .filter = &equal_tuples,
2282 .fn_map = &isl_map_deltas_map,
2284 return un_op(umap, &control);
2287 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2289 struct isl_un_op_control control = {
2290 .fn_map = &isl_set_identity,
2292 return un_op(uset, &control);
2295 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2297 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2299 isl_union_pw_multi_aff **res = user;
2300 isl_space *space;
2301 isl_pw_multi_aff *pma;
2303 space = isl_space_map_from_set(isl_set_get_space(set));
2304 pma = isl_pw_multi_aff_identity(space);
2305 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2306 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2308 return *res ? isl_stat_ok : isl_stat_error;
2311 /* Return an identity function on "uset" in the form
2312 * of an isl_union_pw_multi_aff.
2314 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2315 __isl_take isl_union_set *uset)
2317 isl_union_pw_multi_aff *res;
2319 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2320 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2321 res = isl_union_pw_multi_aff_free(res);
2323 isl_union_set_free(uset);
2324 return res;
2327 /* For each map in "umap" of the form [A -> B] -> C,
2328 * construct the map A -> C and collect the results.
2330 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2331 __isl_take isl_union_map *umap)
2333 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2334 struct isl_un_op_control control = {
2335 .filter = &un_op_filter_drop_user,
2336 .filter_user = &data,
2337 .fn_map = &isl_map_domain_factor_domain,
2339 return un_op(umap, &control);
2342 /* For each map in "umap" of the form [A -> B] -> C,
2343 * construct the map B -> C and collect the results.
2345 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2346 __isl_take isl_union_map *umap)
2348 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2349 struct isl_un_op_control control = {
2350 .filter = &un_op_filter_drop_user,
2351 .filter_user = &data,
2352 .fn_map = &isl_map_domain_factor_range,
2354 return un_op(umap, &control);
2357 /* For each map in "umap" of the form A -> [B -> C],
2358 * construct the map A -> B and collect the results.
2360 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2361 __isl_take isl_union_map *umap)
2363 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2364 struct isl_un_op_control control = {
2365 .filter = &un_op_filter_drop_user,
2366 .filter_user = &data,
2367 .fn_map = &isl_map_range_factor_domain,
2369 return un_op(umap, &control);
2372 /* For each map in "umap" of the form A -> [B -> C],
2373 * construct the map A -> C and collect the results.
2375 __isl_give isl_union_map *isl_union_map_range_factor_range(
2376 __isl_take isl_union_map *umap)
2378 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2379 struct isl_un_op_control control = {
2380 .filter = &un_op_filter_drop_user,
2381 .filter_user = &data,
2382 .fn_map = &isl_map_range_factor_range,
2384 return un_op(umap, &control);
2387 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2388 * construct the map A -> C and collect the results.
2390 __isl_give isl_union_map *isl_union_map_factor_domain(
2391 __isl_take isl_union_map *umap)
2393 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2394 struct isl_un_op_control control = {
2395 .filter = &un_op_filter_drop_user,
2396 .filter_user = &data,
2397 .fn_map = &isl_map_factor_domain,
2399 return un_op(umap, &control);
2402 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2403 * construct the map B -> D and collect the results.
2405 __isl_give isl_union_map *isl_union_map_factor_range(
2406 __isl_take isl_union_map *umap)
2408 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2409 struct isl_un_op_control control = {
2410 .filter = &un_op_filter_drop_user,
2411 .filter_user = &data,
2412 .fn_map = &isl_map_factor_range,
2414 return un_op(umap, &control);
2417 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2419 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2420 struct isl_un_op_control control = {
2421 .filter = &un_op_filter_drop_user,
2422 .filter_user = &data,
2423 .fn_map = &isl_set_unwrap,
2425 return un_op(uset, &control);
2428 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2430 struct isl_un_op_control control = {
2431 .fn_map = &isl_map_wrap,
2433 return un_op(umap, &control);
2436 struct isl_union_map_is_subset_data {
2437 isl_union_map *umap2;
2438 isl_bool is_subset;
2441 static isl_stat is_subset_entry(void **entry, void *user)
2443 struct isl_union_map_is_subset_data *data = user;
2444 uint32_t hash;
2445 struct isl_hash_table_entry *entry2;
2446 isl_map *map = *entry;
2448 hash = isl_space_get_hash(map->dim);
2449 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2450 hash, &has_space, map->dim, 0);
2451 if (!entry2)
2452 return isl_stat_error;
2453 if (entry2 == isl_hash_table_entry_none) {
2454 int empty = isl_map_is_empty(map);
2455 if (empty < 0)
2456 return isl_stat_error;
2457 if (empty)
2458 return isl_stat_ok;
2459 data->is_subset = isl_bool_false;
2460 return isl_stat_error;
2463 data->is_subset = isl_map_is_subset(map, entry2->data);
2464 if (data->is_subset < 0 || !data->is_subset)
2465 return isl_stat_error;
2467 return isl_stat_ok;
2470 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2471 __isl_keep isl_union_map *umap2)
2473 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2475 umap1 = isl_union_map_copy(umap1);
2476 umap2 = isl_union_map_copy(umap2);
2477 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2478 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2480 if (!umap1 || !umap2)
2481 goto error;
2483 data.umap2 = umap2;
2484 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2485 &is_subset_entry, &data) < 0 &&
2486 data.is_subset)
2487 goto error;
2489 isl_union_map_free(umap1);
2490 isl_union_map_free(umap2);
2492 return data.is_subset;
2493 error:
2494 isl_union_map_free(umap1);
2495 isl_union_map_free(umap2);
2496 return isl_bool_error;
2499 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2500 __isl_keep isl_union_set *uset2)
2502 return isl_union_map_is_subset(uset1, uset2);
2505 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2506 __isl_keep isl_union_map *umap2)
2508 isl_bool is_subset;
2510 if (!umap1 || !umap2)
2511 return isl_bool_error;
2512 is_subset = isl_union_map_is_subset(umap1, umap2);
2513 if (is_subset != isl_bool_true)
2514 return is_subset;
2515 is_subset = isl_union_map_is_subset(umap2, umap1);
2516 return is_subset;
2519 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2520 __isl_keep isl_union_set *uset2)
2522 return isl_union_map_is_equal(uset1, uset2);
2525 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2526 __isl_keep isl_union_map *umap2)
2528 isl_bool is_subset;
2530 if (!umap1 || !umap2)
2531 return isl_bool_error;
2532 is_subset = isl_union_map_is_subset(umap1, umap2);
2533 if (is_subset != isl_bool_true)
2534 return is_subset;
2535 is_subset = isl_union_map_is_subset(umap2, umap1);
2536 return isl_bool_not(is_subset);
2539 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2540 __isl_keep isl_union_set *uset2)
2542 return isl_union_map_is_strict_subset(uset1, uset2);
2545 /* Internal data structure for isl_union_map_is_disjoint.
2546 * umap2 is the union map with which we are comparing.
2547 * is_disjoint is initialized to 1 and is set to 0 as soon
2548 * as the union maps turn out not to be disjoint.
2550 struct isl_union_map_is_disjoint_data {
2551 isl_union_map *umap2;
2552 isl_bool is_disjoint;
2555 /* Check if "map" is disjoint from data->umap2 and abort
2556 * the search if it is not.
2558 static isl_stat is_disjoint_entry(void **entry, void *user)
2560 struct isl_union_map_is_disjoint_data *data = user;
2561 uint32_t hash;
2562 struct isl_hash_table_entry *entry2;
2563 isl_map *map = *entry;
2565 hash = isl_space_get_hash(map->dim);
2566 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2567 hash, &has_space, map->dim, 0);
2568 if (!entry2)
2569 return isl_stat_error;
2570 if (entry2 == isl_hash_table_entry_none)
2571 return isl_stat_ok;
2573 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2574 if (data->is_disjoint < 0 || !data->is_disjoint)
2575 return isl_stat_error;
2577 return isl_stat_ok;
2580 /* Are "umap1" and "umap2" disjoint?
2582 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2583 __isl_keep isl_union_map *umap2)
2585 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2587 umap1 = isl_union_map_copy(umap1);
2588 umap2 = isl_union_map_copy(umap2);
2589 umap1 = isl_union_map_align_params(umap1,
2590 isl_union_map_get_space(umap2));
2591 umap2 = isl_union_map_align_params(umap2,
2592 isl_union_map_get_space(umap1));
2594 if (!umap1 || !umap2)
2595 goto error;
2597 data.umap2 = umap2;
2598 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2599 &is_disjoint_entry, &data) < 0 &&
2600 data.is_disjoint)
2601 goto error;
2603 isl_union_map_free(umap1);
2604 isl_union_map_free(umap2);
2606 return data.is_disjoint;
2607 error:
2608 isl_union_map_free(umap1);
2609 isl_union_map_free(umap2);
2610 return isl_bool_error;
2613 /* Are "uset1" and "uset2" disjoint?
2615 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2616 __isl_keep isl_union_set *uset2)
2618 return isl_union_map_is_disjoint(uset1, uset2);
2621 static isl_stat sample_entry(void **entry, void *user)
2623 isl_basic_map **sample = (isl_basic_map **)user;
2624 isl_map *map = *entry;
2626 *sample = isl_map_sample(isl_map_copy(map));
2627 if (!*sample)
2628 return isl_stat_error;
2629 if (!isl_basic_map_plain_is_empty(*sample))
2630 return isl_stat_error;
2631 return isl_stat_ok;
2634 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2636 isl_basic_map *sample = NULL;
2638 if (!umap)
2639 return NULL;
2641 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2642 &sample_entry, &sample) < 0 &&
2643 !sample)
2644 goto error;
2646 if (!sample)
2647 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2649 isl_union_map_free(umap);
2651 return sample;
2652 error:
2653 isl_union_map_free(umap);
2654 return NULL;
2657 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2659 return bset_from_bmap(isl_union_map_sample(uset));
2662 /* Return an element in "uset" in the form of an isl_point.
2663 * Return a void isl_point if "uset" is empty.
2665 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2667 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2670 struct isl_forall_data {
2671 isl_bool res;
2672 isl_bool (*fn)(__isl_keep isl_map *map);
2675 static isl_stat forall_entry(void **entry, void *user)
2677 struct isl_forall_data *data = user;
2678 isl_map *map = *entry;
2680 data->res = data->fn(map);
2681 if (data->res < 0)
2682 return isl_stat_error;
2684 if (!data->res)
2685 return isl_stat_error;
2687 return isl_stat_ok;
2690 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2691 isl_bool (*fn)(__isl_keep isl_map *map))
2693 struct isl_forall_data data = { isl_bool_true, fn };
2695 if (!umap)
2696 return isl_bool_error;
2698 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2699 &forall_entry, &data) < 0 && data.res)
2700 return isl_bool_error;
2702 return data.res;
2705 struct isl_forall_user_data {
2706 isl_bool res;
2707 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2708 void *user;
2711 static isl_stat forall_user_entry(void **entry, void *user)
2713 struct isl_forall_user_data *data = user;
2714 isl_map *map = *entry;
2716 data->res = data->fn(map, data->user);
2717 if (data->res < 0)
2718 return isl_stat_error;
2720 if (!data->res)
2721 return isl_stat_error;
2723 return isl_stat_ok;
2726 /* Check if fn(map, user) returns true for all maps "map" in umap.
2728 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2729 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2731 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2733 if (!umap)
2734 return isl_bool_error;
2736 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2737 &forall_user_entry, &data) < 0 && data.res)
2738 return isl_bool_error;
2740 return data.res;
2743 /* Is "umap" obviously empty?
2745 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2747 isl_size n;
2749 n = isl_union_map_n_map(umap);
2750 if (n < 0)
2751 return isl_bool_error;
2752 return n == 0;
2755 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2757 return union_map_forall(umap, &isl_map_is_empty);
2760 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2762 return isl_union_map_is_empty(uset);
2765 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2767 isl_bool is_subset;
2768 isl_space *space;
2769 isl_map *id;
2771 if (!map)
2772 return isl_bool_error;
2774 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2775 map->dim, isl_dim_out))
2776 return isl_bool_false;
2778 space = isl_map_get_space(map);
2779 id = isl_map_identity(space);
2781 is_subset = isl_map_is_subset(map, id);
2783 isl_map_free(id);
2785 return is_subset;
2788 /* Given an isl_union_map that consists of a single map, check
2789 * if it is single-valued.
2791 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2793 isl_map *map;
2794 isl_bool sv;
2796 umap = isl_union_map_copy(umap);
2797 map = isl_map_from_union_map(umap);
2798 sv = isl_map_is_single_valued(map);
2799 isl_map_free(map);
2801 return sv;
2804 /* Internal data structure for single_valued_on_domain.
2806 * "umap" is the union map to be tested.
2807 * "sv" is set to 1 as long as "umap" may still be single-valued.
2809 struct isl_union_map_is_sv_data {
2810 isl_union_map *umap;
2811 isl_bool sv;
2814 /* Check if the data->umap is single-valued on "set".
2816 * If data->umap consists of a single map on "set", then test it
2817 * as an isl_map.
2819 * Otherwise, compute
2821 * M \circ M^-1
2823 * check if the result is a subset of the identity mapping and
2824 * store the result in data->sv.
2826 * Terminate as soon as data->umap has been determined not to
2827 * be single-valued.
2829 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2831 struct isl_union_map_is_sv_data *data = user;
2832 isl_union_map *umap, *test;
2833 isl_size n;
2835 umap = isl_union_map_copy(data->umap);
2836 umap = isl_union_map_intersect_domain(umap,
2837 isl_union_set_from_set(set));
2839 n = isl_union_map_n_map(umap);
2840 if (n < 0) {
2841 data->sv = isl_bool_error;
2842 } else if (n == 1) {
2843 data->sv = single_map_is_single_valued(umap);
2844 isl_union_map_free(umap);
2845 } else {
2846 test = isl_union_map_reverse(isl_union_map_copy(umap));
2847 test = isl_union_map_apply_range(test, umap);
2849 data->sv = union_map_forall(test, &is_subset_of_identity);
2851 isl_union_map_free(test);
2854 if (data->sv < 0 || !data->sv)
2855 return isl_stat_error;
2856 return isl_stat_ok;
2859 /* Check if the given map is single-valued.
2861 * If the union map consists of a single map, then test it as an isl_map.
2862 * Otherwise, check if the union map is single-valued on each of its
2863 * domain spaces.
2865 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2867 isl_union_map *universe;
2868 isl_union_set *domain;
2869 struct isl_union_map_is_sv_data data;
2870 isl_size n;
2872 n = isl_union_map_n_map(umap);
2873 if (n < 0)
2874 return isl_bool_error;
2875 if (n == 1)
2876 return single_map_is_single_valued(umap);
2878 universe = isl_union_map_universe(isl_union_map_copy(umap));
2879 domain = isl_union_map_domain(universe);
2881 data.sv = isl_bool_true;
2882 data.umap = umap;
2883 if (isl_union_set_foreach_set(domain,
2884 &single_valued_on_domain, &data) < 0 && data.sv)
2885 data.sv = isl_bool_error;
2886 isl_union_set_free(domain);
2888 return data.sv;
2891 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2893 isl_bool in;
2895 umap = isl_union_map_copy(umap);
2896 umap = isl_union_map_reverse(umap);
2897 in = isl_union_map_is_single_valued(umap);
2898 isl_union_map_free(umap);
2900 return in;
2903 /* Is "map" obviously not an identity relation because
2904 * it maps elements from one space to another space?
2905 * Update *non_identity accordingly.
2907 * In particular, if the domain and range spaces are the same,
2908 * then the map is not considered to obviously not be an identity relation.
2909 * Otherwise, the map is considered to obviously not be an identity relation
2910 * if it is is non-empty.
2912 * If "map" is determined to obviously not be an identity relation,
2913 * then the search is aborted.
2915 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2917 isl_bool *non_identity = user;
2918 isl_bool equal;
2919 isl_space *space;
2921 space = isl_map_get_space(map);
2922 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2923 if (equal >= 0 && !equal)
2924 *non_identity = isl_bool_not(isl_map_is_empty(map));
2925 else
2926 *non_identity = isl_bool_not(equal);
2927 isl_space_free(space);
2928 isl_map_free(map);
2930 if (*non_identity < 0 || *non_identity)
2931 return isl_stat_error;
2933 return isl_stat_ok;
2936 /* Is "umap" obviously not an identity relation because
2937 * it maps elements from one space to another space?
2939 * As soon as a map has been found that maps elements to a different space,
2940 * non_identity is changed and the search is aborted.
2942 static isl_bool isl_union_map_plain_is_not_identity(
2943 __isl_keep isl_union_map *umap)
2945 isl_bool non_identity;
2947 non_identity = isl_bool_false;
2948 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2949 &non_identity) < 0 &&
2950 non_identity == isl_bool_false)
2951 return isl_bool_error;
2953 return non_identity;
2956 /* Does "map" only map elements to themselves?
2957 * Update *identity accordingly.
2959 * If "map" is determined not to be an identity relation,
2960 * then the search is aborted.
2962 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2964 isl_bool *identity = user;
2966 *identity = isl_map_is_identity(map);
2967 isl_map_free(map);
2969 if (*identity < 0 || !*identity)
2970 return isl_stat_error;
2972 return isl_stat_ok;
2975 /* Does "umap" only map elements to themselves?
2977 * First check if there are any maps that map elements to different spaces.
2978 * If not, then check that all the maps (between identical spaces)
2979 * are identity relations.
2981 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2983 isl_bool non_identity;
2984 isl_bool identity;
2986 non_identity = isl_union_map_plain_is_not_identity(umap);
2987 if (non_identity < 0 || non_identity)
2988 return isl_bool_not(non_identity);
2990 identity = isl_bool_true;
2991 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2992 identity == isl_bool_true)
2993 return isl_bool_error;
2995 return identity;
2998 /* Represents a map that has a fixed value (v) for one of its
2999 * range dimensions.
3000 * The map in this structure is not reference counted, so it
3001 * is only valid while the isl_union_map from which it was
3002 * obtained is still alive.
3004 struct isl_fixed_map {
3005 isl_int v;
3006 isl_map *map;
3009 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
3010 int n)
3012 int i;
3013 struct isl_fixed_map *v;
3015 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
3016 if (!v)
3017 return NULL;
3018 for (i = 0; i < n; ++i)
3019 isl_int_init(v[i].v);
3020 return v;
3023 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
3025 int i;
3027 if (!v)
3028 return;
3029 for (i = 0; i < n; ++i)
3030 isl_int_clear(v[i].v);
3031 free(v);
3034 /* Compare the "v" field of two isl_fixed_map structs.
3036 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
3038 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
3039 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
3041 return isl_int_cmp(e1->v, e2->v);
3044 /* Internal data structure used while checking whether all maps
3045 * in a union_map have a fixed value for a given output dimension.
3046 * v is the list of maps, with the fixed value for the dimension
3047 * n is the number of maps considered so far
3048 * pos is the output dimension under investigation
3050 struct isl_fixed_dim_data {
3051 struct isl_fixed_map *v;
3052 int n;
3053 int pos;
3056 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
3058 struct isl_fixed_dim_data *data = user;
3060 data->v[data->n].map = map;
3061 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
3062 &data->v[data->n++].v);
3065 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3066 int first, int n_range);
3068 /* Given a list of the maps, with their fixed values at output dimension "pos",
3069 * check whether the ranges of the maps form an obvious partition.
3071 * We first sort the maps according to their fixed values.
3072 * If all maps have a different value, then we know the ranges form
3073 * a partition.
3074 * Otherwise, we collect the maps with the same fixed value and
3075 * check whether each such collection is obviously injective
3076 * based on later dimensions.
3078 static int separates(struct isl_fixed_map *v, int n,
3079 __isl_take isl_space *space, int pos, int n_range)
3081 int i;
3083 if (!v)
3084 goto error;
3086 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
3088 for (i = 0; i + 1 < n; ++i) {
3089 int j, k;
3090 isl_union_map *part;
3091 int injective;
3093 for (j = i + 1; j < n; ++j)
3094 if (isl_int_ne(v[i].v, v[j].v))
3095 break;
3097 if (j == i + 1)
3098 continue;
3100 part = isl_union_map_alloc(isl_space_copy(space), j - i);
3101 for (k = i; k < j; ++k)
3102 part = isl_union_map_add_map(part,
3103 isl_map_copy(v[k].map));
3105 injective = plain_injective_on_range(part, pos + 1, n_range);
3106 if (injective < 0)
3107 goto error;
3108 if (!injective)
3109 break;
3111 i = j - 1;
3114 isl_space_free(space);
3115 free_isl_fixed_map_array(v, n);
3116 return i + 1 >= n;
3117 error:
3118 isl_space_free(space);
3119 free_isl_fixed_map_array(v, n);
3120 return -1;
3123 /* Check whether the maps in umap have obviously distinct ranges.
3124 * In particular, check for an output dimension in the range
3125 * [first,n_range) for which all maps have a fixed value
3126 * and then check if these values, possibly along with fixed values
3127 * at later dimensions, entail distinct ranges.
3129 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3130 int first, int n_range)
3132 isl_ctx *ctx;
3133 isl_size n;
3134 struct isl_fixed_dim_data data = { NULL };
3136 ctx = isl_union_map_get_ctx(umap);
3138 n = isl_union_map_n_map(umap);
3139 if (n < 0)
3140 goto error;
3142 if (n <= 1) {
3143 isl_union_map_free(umap);
3144 return isl_bool_true;
3147 if (first >= n_range) {
3148 isl_union_map_free(umap);
3149 return isl_bool_false;
3152 data.v = alloc_isl_fixed_map_array(ctx, n);
3153 if (!data.v)
3154 goto error;
3156 for (data.pos = first; data.pos < n_range; ++data.pos) {
3157 isl_bool fixed;
3158 int injective;
3159 isl_space *space;
3161 data.n = 0;
3162 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3163 if (fixed < 0)
3164 goto error;
3165 if (!fixed)
3166 continue;
3167 space = isl_union_map_get_space(umap);
3168 injective = separates(data.v, n, space, data.pos, n_range);
3169 isl_union_map_free(umap);
3170 return injective;
3173 free_isl_fixed_map_array(data.v, n);
3174 isl_union_map_free(umap);
3176 return isl_bool_false;
3177 error:
3178 free_isl_fixed_map_array(data.v, n);
3179 isl_union_map_free(umap);
3180 return isl_bool_error;
3183 /* Check whether the maps in umap that map to subsets of "ran"
3184 * have obviously distinct ranges.
3186 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3187 void *user)
3189 isl_size dim;
3190 isl_union_map *umap = user;
3192 dim = isl_set_dim(ran, isl_dim_set);
3193 if (dim < 0)
3194 return isl_bool_error;
3196 umap = isl_union_map_copy(umap);
3197 umap = isl_union_map_intersect_range(umap,
3198 isl_union_set_from_set(isl_set_copy(ran)));
3199 return plain_injective_on_range(umap, 0, dim);
3202 /* Check if the given union_map is obviously injective.
3204 * In particular, we first check if all individual maps are obviously
3205 * injective and then check if all the ranges of these maps are
3206 * obviously disjoint.
3208 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3210 isl_bool in;
3211 isl_union_map *univ;
3212 isl_union_set *ran;
3214 in = union_map_forall(umap, &isl_map_plain_is_injective);
3215 if (in < 0)
3216 return isl_bool_error;
3217 if (!in)
3218 return isl_bool_false;
3220 univ = isl_union_map_universe(isl_union_map_copy(umap));
3221 ran = isl_union_map_range(univ);
3223 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3225 isl_union_set_free(ran);
3227 return in;
3230 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3232 isl_bool sv;
3234 sv = isl_union_map_is_single_valued(umap);
3235 if (sv < 0 || !sv)
3236 return sv;
3238 return isl_union_map_is_injective(umap);
3241 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3243 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3244 struct isl_un_op_control control = {
3245 .filter = &un_op_filter_drop_user,
3246 .filter_user = &data,
3247 .fn_map = &isl_map_zip,
3249 return un_op(umap, &control);
3252 /* Given a union map, take the maps of the form A -> (B -> C) and
3253 * return the union of the corresponding maps (A -> B) -> C.
3255 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3257 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3258 struct isl_un_op_control control = {
3259 .filter = &un_op_filter_drop_user,
3260 .filter_user = &data,
3261 .fn_map = &isl_map_uncurry,
3263 return un_op(umap, &control);
3266 /* Given a union map, take the maps of the form (A -> B) -> C and
3267 * return the union of the corresponding maps A -> (B -> C).
3269 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3271 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3272 struct isl_un_op_control control = {
3273 .filter = &un_op_filter_drop_user,
3274 .filter_user = &data,
3275 .fn_map = &isl_map_curry,
3277 return un_op(umap, &control);
3280 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3281 * return the union of the corresponding maps A -> (B -> (C -> D)).
3283 __isl_give isl_union_map *isl_union_map_range_curry(
3284 __isl_take isl_union_map *umap)
3286 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3287 struct isl_un_op_control control = {
3288 .filter = &un_op_filter_drop_user,
3289 .filter_user = &data,
3290 .fn_map = &isl_map_range_curry,
3292 return un_op(umap, &control);
3295 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3297 struct isl_un_op_control control = {
3298 .fn_map = &isl_set_lift,
3300 return un_op(uset, &control);
3303 static isl_stat coefficients_entry(void **entry, void *user)
3305 isl_set *set = *entry;
3306 isl_union_set **res = user;
3308 set = isl_set_copy(set);
3309 set = isl_set_from_basic_set(isl_set_coefficients(set));
3310 *res = isl_union_set_add_set(*res, set);
3312 return isl_stat_ok;
3315 __isl_give isl_union_set *isl_union_set_coefficients(
3316 __isl_take isl_union_set *uset)
3318 isl_ctx *ctx;
3319 isl_space *space;
3320 isl_union_set *res;
3322 if (!uset)
3323 return NULL;
3325 ctx = isl_union_set_get_ctx(uset);
3326 space = isl_space_set_alloc(ctx, 0, 0);
3327 res = isl_union_map_alloc(space, uset->table.n);
3328 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3329 &coefficients_entry, &res) < 0)
3330 goto error;
3332 isl_union_set_free(uset);
3333 return res;
3334 error:
3335 isl_union_set_free(uset);
3336 isl_union_set_free(res);
3337 return NULL;
3340 static isl_stat solutions_entry(void **entry, void *user)
3342 isl_set *set = *entry;
3343 isl_union_set **res = user;
3345 set = isl_set_copy(set);
3346 set = isl_set_from_basic_set(isl_set_solutions(set));
3347 if (!*res)
3348 *res = isl_union_set_from_set(set);
3349 else
3350 *res = isl_union_set_add_set(*res, set);
3352 if (!*res)
3353 return isl_stat_error;
3355 return isl_stat_ok;
3358 __isl_give isl_union_set *isl_union_set_solutions(
3359 __isl_take isl_union_set *uset)
3361 isl_union_set *res = NULL;
3363 if (!uset)
3364 return NULL;
3366 if (uset->table.n == 0) {
3367 res = isl_union_set_empty(isl_union_set_get_space(uset));
3368 isl_union_set_free(uset);
3369 return res;
3372 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3373 &solutions_entry, &res) < 0)
3374 goto error;
3376 isl_union_set_free(uset);
3377 return res;
3378 error:
3379 isl_union_set_free(uset);
3380 isl_union_set_free(res);
3381 return NULL;
3384 /* Is the domain space of "map" equal to "space"?
3386 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3388 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3389 space, isl_dim_out);
3392 /* Is the range space of "map" equal to "space"?
3394 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3396 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3397 space, isl_dim_out);
3400 /* Is the set space of "map" equal to "space"?
3402 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3404 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3405 space, isl_dim_out);
3408 /* Internal data structure for preimage_pw_multi_aff.
3410 * "pma" is the function under which the preimage should be taken.
3411 * "space" is the space of "pma".
3412 * "res" collects the results.
3413 * "fn" computes the preimage for a given map.
3414 * "match" returns true if "fn" can be called.
3416 struct isl_union_map_preimage_data {
3417 isl_space *space;
3418 isl_pw_multi_aff *pma;
3419 isl_union_map *res;
3420 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3421 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3422 __isl_take isl_pw_multi_aff *pma);
3425 /* Call data->fn to compute the preimage of the domain or range of *entry
3426 * under the function represented by data->pma, provided the domain/range
3427 * space of *entry matches the target space of data->pma
3428 * (as given by data->match), and add the result to data->res.
3430 static isl_stat preimage_entry(void **entry, void *user)
3432 int m;
3433 isl_map *map = *entry;
3434 struct isl_union_map_preimage_data *data = user;
3435 isl_bool empty;
3437 m = data->match(map, data->space);
3438 if (m < 0)
3439 return isl_stat_error;
3440 if (!m)
3441 return isl_stat_ok;
3443 map = isl_map_copy(map);
3444 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3446 empty = isl_map_is_empty(map);
3447 if (empty < 0 || empty) {
3448 isl_map_free(map);
3449 return empty < 0 ? isl_stat_error : isl_stat_ok;
3452 data->res = isl_union_map_add_map(data->res, map);
3454 return isl_stat_ok;
3457 /* Compute the preimage of the domain or range of "umap" under the function
3458 * represented by "pma".
3459 * In other words, plug in "pma" in the domain or range of "umap".
3460 * The function "fn" performs the actual preimage computation on a map,
3461 * while "match" determines to which maps the function should be applied.
3463 static __isl_give isl_union_map *preimage_pw_multi_aff(
3464 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3465 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3466 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3467 __isl_take isl_pw_multi_aff *pma))
3469 isl_ctx *ctx;
3470 isl_space *space;
3471 struct isl_union_map_preimage_data data;
3473 umap = isl_union_map_align_params(umap,
3474 isl_pw_multi_aff_get_space(pma));
3475 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3477 if (!umap || !pma)
3478 goto error;
3480 ctx = isl_union_map_get_ctx(umap);
3481 space = isl_union_map_get_space(umap);
3482 data.space = isl_pw_multi_aff_get_space(pma);
3483 data.pma = pma;
3484 data.res = isl_union_map_alloc(space, umap->table.n);
3485 data.match = match;
3486 data.fn = fn;
3487 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3488 &data) < 0)
3489 data.res = isl_union_map_free(data.res);
3491 isl_space_free(data.space);
3492 isl_union_map_free(umap);
3493 isl_pw_multi_aff_free(pma);
3494 return data.res;
3495 error:
3496 isl_union_map_free(umap);
3497 isl_pw_multi_aff_free(pma);
3498 return NULL;
3501 /* Compute the preimage of the domain of "umap" under the function
3502 * represented by "pma".
3503 * In other words, plug in "pma" in the domain of "umap".
3504 * The result contains maps that live in the same spaces as the maps of "umap"
3505 * with domain space equal to the target space of "pma",
3506 * except that the domain has been replaced by the domain space of "pma".
3508 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3509 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3511 return preimage_pw_multi_aff(umap, pma, &domain_match,
3512 &isl_map_preimage_domain_pw_multi_aff);
3515 /* Compute the preimage of the range of "umap" under the function
3516 * represented by "pma".
3517 * In other words, plug in "pma" in the range of "umap".
3518 * The result contains maps that live in the same spaces as the maps of "umap"
3519 * with range space equal to the target space of "pma",
3520 * except that the range has been replaced by the domain space of "pma".
3522 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3523 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3525 return preimage_pw_multi_aff(umap, pma, &range_match,
3526 &isl_map_preimage_range_pw_multi_aff);
3529 /* Compute the preimage of "uset" under the function represented by "pma".
3530 * In other words, plug in "pma" in "uset".
3531 * The result contains sets that live in the same spaces as the sets of "uset"
3532 * with space equal to the target space of "pma",
3533 * except that the space has been replaced by the domain space of "pma".
3535 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3536 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3538 return preimage_pw_multi_aff(uset, pma, &set_match,
3539 &isl_set_preimage_pw_multi_aff);
3542 /* Compute the preimage of the domain of "umap" under the function
3543 * represented by "ma".
3544 * In other words, plug in "ma" in the domain of "umap".
3545 * The result contains maps that live in the same spaces as the maps of "umap"
3546 * with domain space equal to the target space of "ma",
3547 * except that the domain has been replaced by the domain space of "ma".
3549 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3550 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3552 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3553 isl_pw_multi_aff_from_multi_aff(ma));
3556 /* Compute the preimage of the range of "umap" under the function
3557 * represented by "ma".
3558 * In other words, plug in "ma" in the range of "umap".
3559 * The result contains maps that live in the same spaces as the maps of "umap"
3560 * with range space equal to the target space of "ma",
3561 * except that the range has been replaced by the domain space of "ma".
3563 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3564 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3566 return isl_union_map_preimage_range_pw_multi_aff(umap,
3567 isl_pw_multi_aff_from_multi_aff(ma));
3570 /* Compute the preimage of "uset" under the function represented by "ma".
3571 * In other words, plug in "ma" in "uset".
3572 * The result contains sets that live in the same spaces as the sets of "uset"
3573 * with space equal to the target space of "ma",
3574 * except that the space has been replaced by the domain space of "ma".
3576 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3577 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3579 return isl_union_set_preimage_pw_multi_aff(uset,
3580 isl_pw_multi_aff_from_multi_aff(ma));
3583 /* Internal data structure for preimage_multi_pw_aff.
3585 * "mpa" is the function under which the preimage should be taken.
3586 * "space" is the space of "mpa".
3587 * "res" collects the results.
3588 * "fn" computes the preimage for a given map.
3589 * "match" returns true if "fn" can be called.
3591 struct isl_union_map_preimage_mpa_data {
3592 isl_space *space;
3593 isl_multi_pw_aff *mpa;
3594 isl_union_map *res;
3595 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3596 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3597 __isl_take isl_multi_pw_aff *mpa);
3600 /* Call data->fn to compute the preimage of the domain or range of *entry
3601 * under the function represented by data->mpa, provided the domain/range
3602 * space of *entry matches the target space of data->mpa
3603 * (as given by data->match), and add the result to data->res.
3605 static isl_stat preimage_mpa_entry(void **entry, void *user)
3607 int m;
3608 isl_map *map = *entry;
3609 struct isl_union_map_preimage_mpa_data *data = user;
3610 isl_bool empty;
3612 m = data->match(map, data->space);
3613 if (m < 0)
3614 return isl_stat_error;
3615 if (!m)
3616 return isl_stat_ok;
3618 map = isl_map_copy(map);
3619 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3621 empty = isl_map_is_empty(map);
3622 if (empty < 0 || empty) {
3623 isl_map_free(map);
3624 return empty < 0 ? isl_stat_error : isl_stat_ok;
3627 data->res = isl_union_map_add_map(data->res, map);
3629 return isl_stat_ok;
3632 /* Compute the preimage of the domain or range of "umap" under the function
3633 * represented by "mpa".
3634 * In other words, plug in "mpa" in the domain or range of "umap".
3635 * The function "fn" performs the actual preimage computation on a map,
3636 * while "match" determines to which maps the function should be applied.
3638 static __isl_give isl_union_map *preimage_multi_pw_aff(
3639 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3640 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3641 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3642 __isl_take isl_multi_pw_aff *mpa))
3644 isl_ctx *ctx;
3645 isl_space *space;
3646 struct isl_union_map_preimage_mpa_data data;
3648 umap = isl_union_map_align_params(umap,
3649 isl_multi_pw_aff_get_space(mpa));
3650 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3652 if (!umap || !mpa)
3653 goto error;
3655 ctx = isl_union_map_get_ctx(umap);
3656 space = isl_union_map_get_space(umap);
3657 data.space = isl_multi_pw_aff_get_space(mpa);
3658 data.mpa = mpa;
3659 data.res = isl_union_map_alloc(space, umap->table.n);
3660 data.match = match;
3661 data.fn = fn;
3662 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3663 &data) < 0)
3664 data.res = isl_union_map_free(data.res);
3666 isl_space_free(data.space);
3667 isl_union_map_free(umap);
3668 isl_multi_pw_aff_free(mpa);
3669 return data.res;
3670 error:
3671 isl_union_map_free(umap);
3672 isl_multi_pw_aff_free(mpa);
3673 return NULL;
3676 /* Compute the preimage of the domain of "umap" under the function
3677 * represented by "mpa".
3678 * In other words, plug in "mpa" in the domain of "umap".
3679 * The result contains maps that live in the same spaces as the maps of "umap"
3680 * with domain space equal to the target space of "mpa",
3681 * except that the domain has been replaced by the domain space of "mpa".
3683 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3684 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3686 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3687 &isl_map_preimage_domain_multi_pw_aff);
3690 /* Internal data structure for preimage_upma.
3692 * "umap" is the map of which the preimage should be computed.
3693 * "res" collects the results.
3694 * "fn" computes the preimage for a given piecewise multi-affine function.
3696 struct isl_union_map_preimage_upma_data {
3697 isl_union_map *umap;
3698 isl_union_map *res;
3699 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3700 __isl_take isl_pw_multi_aff *pma);
3703 /* Call data->fn to compute the preimage of the domain or range of data->umap
3704 * under the function represented by pma and add the result to data->res.
3706 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3708 struct isl_union_map_preimage_upma_data *data = user;
3709 isl_union_map *umap;
3711 umap = isl_union_map_copy(data->umap);
3712 umap = data->fn(umap, pma);
3713 data->res = isl_union_map_union(data->res, umap);
3715 return data->res ? isl_stat_ok : isl_stat_error;
3718 /* Compute the preimage of the domain or range of "umap" under the function
3719 * represented by "upma".
3720 * In other words, plug in "upma" in the domain or range of "umap".
3721 * The function "fn" performs the actual preimage computation
3722 * on a piecewise multi-affine function.
3724 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3725 __isl_take isl_union_map *umap,
3726 __isl_take isl_union_pw_multi_aff *upma,
3727 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3728 __isl_take isl_pw_multi_aff *pma))
3730 struct isl_union_map_preimage_upma_data data;
3732 data.umap = umap;
3733 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3734 data.fn = fn;
3735 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3736 &preimage_upma, &data) < 0)
3737 data.res = isl_union_map_free(data.res);
3739 isl_union_map_free(umap);
3740 isl_union_pw_multi_aff_free(upma);
3742 return data.res;
3745 /* Compute the preimage of the domain of "umap" under the function
3746 * represented by "upma".
3747 * In other words, plug in "upma" in the domain of "umap".
3748 * The result contains maps that live in the same spaces as the maps of "umap"
3749 * with domain space equal to one of the target spaces of "upma",
3750 * except that the domain has been replaced by one of the domain spaces that
3751 * correspond to that target space of "upma".
3753 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3754 __isl_take isl_union_map *umap,
3755 __isl_take isl_union_pw_multi_aff *upma)
3757 return preimage_union_pw_multi_aff(umap, upma,
3758 &isl_union_map_preimage_domain_pw_multi_aff);
3761 /* Compute the preimage of the range of "umap" under the function
3762 * represented by "upma".
3763 * In other words, plug in "upma" in the range of "umap".
3764 * The result contains maps that live in the same spaces as the maps of "umap"
3765 * with range space equal to one of the target spaces of "upma",
3766 * except that the range has been replaced by one of the domain spaces that
3767 * correspond to that target space of "upma".
3769 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3770 __isl_take isl_union_map *umap,
3771 __isl_take isl_union_pw_multi_aff *upma)
3773 return preimage_union_pw_multi_aff(umap, upma,
3774 &isl_union_map_preimage_range_pw_multi_aff);
3777 /* Compute the preimage of "uset" under the function represented by "upma".
3778 * In other words, plug in "upma" in the range of "uset".
3779 * The result contains sets that live in the same spaces as the sets of "uset"
3780 * with space equal to one of the target spaces of "upma",
3781 * except that the space has been replaced by one of the domain spaces that
3782 * correspond to that target space of "upma".
3784 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3785 __isl_take isl_union_set *uset,
3786 __isl_take isl_union_pw_multi_aff *upma)
3788 return preimage_union_pw_multi_aff(uset, upma,
3789 &isl_union_set_preimage_pw_multi_aff);
3792 /* Reset the user pointer on all identifiers of parameters and tuples
3793 * of the spaces of "umap".
3795 __isl_give isl_union_map *isl_union_map_reset_user(
3796 __isl_take isl_union_map *umap)
3798 umap = isl_union_map_cow(umap);
3799 if (!umap)
3800 return NULL;
3801 umap->dim = isl_space_reset_user(umap->dim);
3802 if (!umap->dim)
3803 return isl_union_map_free(umap);
3804 return total(umap, &isl_map_reset_user);
3807 /* Reset the user pointer on all identifiers of parameters and tuples
3808 * of the spaces of "uset".
3810 __isl_give isl_union_set *isl_union_set_reset_user(
3811 __isl_take isl_union_set *uset)
3813 return isl_union_map_reset_user(uset);
3816 /* Remove all existentially quantified variables and integer divisions
3817 * from "umap" using Fourier-Motzkin elimination.
3819 __isl_give isl_union_map *isl_union_map_remove_divs(
3820 __isl_take isl_union_map *umap)
3822 return total(umap, &isl_map_remove_divs);
3825 /* Remove all existentially quantified variables and integer divisions
3826 * from "uset" using Fourier-Motzkin elimination.
3828 __isl_give isl_union_set *isl_union_set_remove_divs(
3829 __isl_take isl_union_set *uset)
3831 return isl_union_map_remove_divs(uset);
3834 /* Internal data structure for isl_union_map_project_out.
3835 * "type", "first" and "n" are the arguments for the isl_map_project_out
3836 * call.
3837 * "res" collects the results.
3839 struct isl_union_map_project_out_data {
3840 enum isl_dim_type type;
3841 unsigned first;
3842 unsigned n;
3844 isl_union_map *res;
3847 /* Turn the data->n dimensions of type data->type, starting at data->first
3848 * into existentially quantified variables and add the result to data->res.
3850 static isl_stat project_out(__isl_take isl_map *map, void *user)
3852 struct isl_union_map_project_out_data *data = user;
3854 map = isl_map_project_out(map, data->type, data->first, data->n);
3855 data->res = isl_union_map_add_map(data->res, map);
3857 return isl_stat_ok;
3860 /* Turn the "n" dimensions of type "type", starting at "first"
3861 * into existentially quantified variables.
3862 * Since the space of an isl_union_map only contains parameters,
3863 * type is required to be equal to isl_dim_param.
3865 __isl_give isl_union_map *isl_union_map_project_out(
3866 __isl_take isl_union_map *umap,
3867 enum isl_dim_type type, unsigned first, unsigned n)
3869 isl_space *space;
3870 struct isl_union_map_project_out_data data = { type, first, n };
3872 if (!umap)
3873 return NULL;
3875 if (type != isl_dim_param)
3876 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3877 "can only project out parameters",
3878 return isl_union_map_free(umap));
3880 space = isl_union_map_get_space(umap);
3881 space = isl_space_drop_dims(space, type, first, n);
3882 data.res = isl_union_map_empty(space);
3883 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3884 data.res = isl_union_map_free(data.res);
3886 isl_union_map_free(umap);
3888 return data.res;
3891 #undef TYPE
3892 #define TYPE isl_union_map
3893 #include "isl_project_out_all_params_templ.c"
3895 /* Turn the "n" dimensions of type "type", starting at "first"
3896 * into existentially quantified variables.
3897 * Since the space of an isl_union_set only contains parameters,
3898 * "type" is required to be equal to isl_dim_param.
3900 __isl_give isl_union_set *isl_union_set_project_out(
3901 __isl_take isl_union_set *uset,
3902 enum isl_dim_type type, unsigned first, unsigned n)
3904 return isl_union_map_project_out(uset, type, first, n);
3907 /* Project out all parameters from "uset" by existentially quantifying
3908 * over them.
3910 __isl_give isl_union_set *isl_union_set_project_out_all_params(
3911 __isl_take isl_union_set *uset)
3913 return uset_from_umap(
3914 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3917 /* Internal data structure for isl_union_map_involves_dims.
3918 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3920 struct isl_union_map_involves_dims_data {
3921 unsigned first;
3922 unsigned n;
3925 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3927 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3929 struct isl_union_map_involves_dims_data *data = user;
3930 isl_bool involves;
3932 involves = isl_map_involves_dims(map,
3933 isl_dim_param, data->first, data->n);
3934 return isl_bool_not(involves);
3937 /* Does "umap" involve any of the n parameters starting at first?
3938 * "type" is required to be set to isl_dim_param.
3940 * "umap" involves any of those parameters if any of its maps
3941 * involve the parameters. In other words, "umap" does not
3942 * involve any of the parameters if all its maps to not
3943 * involve the parameters.
3945 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3946 enum isl_dim_type type, unsigned first, unsigned n)
3948 struct isl_union_map_involves_dims_data data = { first, n };
3949 isl_bool excludes;
3951 if (type != isl_dim_param)
3952 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3953 "can only reference parameters", return isl_bool_error);
3955 excludes = union_map_forall_user(umap, &map_excludes, &data);
3957 return isl_bool_not(excludes);
3960 /* Internal data structure for isl_union_map_reset_range_space.
3961 * "range" is the space from which to set the range space.
3962 * "res" collects the results.
3964 struct isl_union_map_reset_range_space_data {
3965 isl_space *range;
3966 isl_union_map *res;
3969 /* Replace the range space of "map" by the range space of data->range and
3970 * add the result to data->res.
3972 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3974 struct isl_union_map_reset_range_space_data *data = user;
3975 isl_space *space;
3977 space = isl_map_get_space(map);
3978 space = isl_space_domain(space);
3979 space = isl_space_extend_domain_with_range(space,
3980 isl_space_copy(data->range));
3981 map = isl_map_reset_space(map, space);
3982 data->res = isl_union_map_add_map(data->res, map);
3984 return data->res ? isl_stat_ok : isl_stat_error;
3987 /* Replace the range space of all the maps in "umap" by
3988 * the range space of "space".
3990 * This assumes that all maps have the same output dimension.
3991 * This function should therefore not be made publicly available.
3993 * Since the spaces of the maps change, so do their hash value.
3994 * We therefore need to create a new isl_union_map.
3996 __isl_give isl_union_map *isl_union_map_reset_range_space(
3997 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3999 struct isl_union_map_reset_range_space_data data = { space };
4001 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4002 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
4003 data.res = isl_union_map_free(data.res);
4005 isl_space_free(space);
4006 isl_union_map_free(umap);
4007 return data.res;
4010 /* Check that "umap" and "space" have the same number of parameters.
4012 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
4013 __isl_keep isl_space *space)
4015 isl_size dim1, dim2;
4017 dim1 = isl_union_map_dim(umap, isl_dim_param);
4018 dim2 = isl_space_dim(space, isl_dim_param);
4019 if (dim1 < 0 || dim2 < 0)
4020 return isl_stat_error;
4021 if (dim1 == dim2)
4022 return isl_stat_ok;
4023 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
4024 "number of parameters does not match", return isl_stat_error);
4027 /* Internal data structure for isl_union_map_reset_equal_dim_space.
4028 * "space" is the target space.
4029 * "res" collects the results.
4031 struct isl_union_map_reset_params_data {
4032 isl_space *space;
4033 isl_union_map *res;
4036 /* Replace the parameters of "map" by those of data->space and
4037 * add the result to data->res.
4039 static isl_stat reset_params(__isl_take isl_map *map, void *user)
4041 struct isl_union_map_reset_params_data *data = user;
4042 isl_space *space;
4044 space = isl_map_get_space(map);
4045 space = isl_space_replace_params(space, data->space);
4046 map = isl_map_reset_equal_dim_space(map, space);
4047 data->res = isl_union_map_add_map(data->res, map);
4049 return data->res ? isl_stat_ok : isl_stat_error;
4052 /* Replace the space of "umap" by "space", without modifying
4053 * the dimension of "umap", i.e., the number of parameters of "umap".
4055 * Since the hash values of the maps in the union map depend
4056 * on the parameters, a new union map needs to be constructed.
4058 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
4059 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4061 struct isl_union_map_reset_params_data data = { space };
4062 isl_bool equal;
4063 isl_space *umap_space;
4065 umap_space = isl_union_map_peek_space(umap);
4066 equal = isl_space_is_equal(umap_space, space);
4067 if (equal < 0)
4068 goto error;
4069 if (equal) {
4070 isl_space_free(space);
4071 return umap;
4073 if (check_union_map_space_equal_dim(umap, space) < 0)
4074 goto error;
4076 data.res = isl_union_map_empty(isl_space_copy(space));
4077 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
4078 data.res = isl_union_map_free(data.res);
4080 isl_space_free(space);
4081 isl_union_map_free(umap);
4082 return data.res;
4083 error:
4084 isl_union_map_free(umap);
4085 isl_space_free(space);
4086 return NULL;
4089 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4090 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4091 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4092 * to the domain and the range of each map.
4093 * "res" collects the results.
4095 struct isl_union_order_at_data {
4096 isl_multi_union_pw_aff *mupa;
4097 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4098 __isl_take isl_multi_pw_aff *mpa2);
4099 isl_union_map *res;
4102 /* Intersect "map" with the result of applying data->order to
4103 * the functions in data->mupa that apply to the domain and the range
4104 * of "map" and add the result to data->res.
4106 static isl_stat order_at(__isl_take isl_map *map, void *user)
4108 struct isl_union_order_at_data *data = user;
4109 isl_space *space;
4110 isl_multi_pw_aff *mpa1, *mpa2;
4111 isl_map *order;
4113 space = isl_space_domain(isl_map_get_space(map));
4114 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4115 space = isl_space_range(isl_map_get_space(map));
4116 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4117 order = data->order(mpa1, mpa2);
4118 map = isl_map_intersect(map, order);
4119 data->res = isl_union_map_add_map(data->res, map);
4121 return data->res ? isl_stat_ok : isl_stat_error;
4124 /* If "mupa" has a non-trivial explicit domain, then intersect
4125 * domain and range of "umap" with this explicit domain.
4126 * If the explicit domain only describes constraints on the parameters,
4127 * then the intersection only needs to be performed once.
4129 static __isl_give isl_union_map *intersect_explicit_domain(
4130 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4132 isl_bool non_trivial, is_params;
4133 isl_union_set *dom;
4135 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4136 if (non_trivial < 0)
4137 return isl_union_map_free(umap);
4138 if (!non_trivial)
4139 return umap;
4140 mupa = isl_multi_union_pw_aff_copy(mupa);
4141 dom = isl_multi_union_pw_aff_domain(mupa);
4142 is_params = isl_union_set_is_params(dom);
4143 if (is_params < 0) {
4144 isl_union_set_free(dom);
4145 return isl_union_map_free(umap);
4147 if (is_params) {
4148 isl_set *set;
4150 set = isl_union_set_params(dom);
4151 umap = isl_union_map_intersect_params(umap, set);
4152 return umap;
4154 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4155 umap = isl_union_map_intersect_range(umap, dom);
4156 return umap;
4159 /* Intersect each map in "umap" with the result of calling "order"
4160 * on the functions is "mupa" that apply to the domain and the range
4161 * of the map.
4163 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4164 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4165 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4166 __isl_take isl_multi_pw_aff *mpa2))
4168 struct isl_union_order_at_data data;
4170 umap = isl_union_map_align_params(umap,
4171 isl_multi_union_pw_aff_get_space(mupa));
4172 mupa = isl_multi_union_pw_aff_align_params(mupa,
4173 isl_union_map_get_space(umap));
4174 umap = intersect_explicit_domain(umap, mupa);
4175 data.mupa = mupa;
4176 data.order = order;
4177 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4178 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4179 data.res = isl_union_map_free(data.res);
4181 isl_multi_union_pw_aff_free(mupa);
4182 isl_union_map_free(umap);
4183 return data.res;
4186 /* Return the subset of "umap" where the domain and the range
4187 * have equal "mupa" values.
4189 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4190 __isl_take isl_union_map *umap,
4191 __isl_take isl_multi_union_pw_aff *mupa)
4193 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4194 &isl_multi_pw_aff_eq_map);
4197 /* Return the subset of "umap" where the domain has a lexicographically
4198 * smaller "mupa" value than the range.
4200 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4201 __isl_take isl_union_map *umap,
4202 __isl_take isl_multi_union_pw_aff *mupa)
4204 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4205 &isl_multi_pw_aff_lex_lt_map);
4208 /* Return the subset of "umap" where the domain has a lexicographically
4209 * greater "mupa" value than the range.
4211 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4212 __isl_take isl_union_map *umap,
4213 __isl_take isl_multi_union_pw_aff *mupa)
4215 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4216 &isl_multi_pw_aff_lex_gt_map);
4219 /* Return the union of the elements in the list "list".
4221 __isl_give isl_union_set *isl_union_set_list_union(
4222 __isl_take isl_union_set_list *list)
4224 int i;
4225 isl_size n;
4226 isl_ctx *ctx;
4227 isl_space *space;
4228 isl_union_set *res;
4230 n = isl_union_set_list_n_union_set(list);
4231 if (n < 0)
4232 goto error;
4234 ctx = isl_union_set_list_get_ctx(list);
4235 space = isl_space_params_alloc(ctx, 0);
4236 res = isl_union_set_empty(space);
4238 for (i = 0; i < n; ++i) {
4239 isl_union_set *uset_i;
4241 uset_i = isl_union_set_list_get_union_set(list, i);
4242 res = isl_union_set_union(res, uset_i);
4245 isl_union_set_list_free(list);
4246 return res;
4247 error:
4248 isl_union_set_list_free(list);
4249 return NULL;
4252 /* Update *hash with the hash value of "map".
4254 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4256 uint32_t *hash = user;
4257 uint32_t map_hash;
4259 map_hash = isl_map_get_hash(map);
4260 isl_hash_hash(*hash, map_hash);
4262 isl_map_free(map);
4263 return isl_stat_ok;
4266 /* Return a hash value that digests "umap".
4268 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4270 uint32_t hash;
4272 if (!umap)
4273 return 0;
4275 hash = isl_hash_init();
4276 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4277 return 0;
4279 return hash;
4282 /* Return a hash value that digests "uset".
4284 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4286 return isl_union_map_get_hash(uset);
4289 /* Add the number of basic sets in "set" to "n".
4291 static isl_stat add_n(__isl_take isl_set *set, void *user)
4293 int *n = user;
4294 isl_size set_n;
4296 set_n = isl_set_n_basic_set(set);
4297 *n += set_n;
4298 isl_set_free(set);
4300 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4303 /* Return the total number of basic sets in "uset".
4305 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4307 int n = 0;
4309 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4310 return -1;
4312 return n;
4315 /* Add the basic sets in "set" to "list".
4317 static isl_stat add_list(__isl_take isl_set *set, void *user)
4319 isl_basic_set_list **list = user;
4320 isl_basic_set_list *list_i;
4322 list_i = isl_set_get_basic_set_list(set);
4323 *list = isl_basic_set_list_concat(*list, list_i);
4324 isl_set_free(set);
4326 if (!*list)
4327 return isl_stat_error;
4328 return isl_stat_ok;
4331 /* Return a list containing all the basic sets in "uset".
4333 * First construct a list of the appropriate size and
4334 * then add all the elements.
4336 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4337 __isl_keep isl_union_set *uset)
4339 int n;
4340 isl_ctx *ctx;
4341 isl_basic_set_list *list;
4343 if (!uset)
4344 return NULL;
4345 ctx = isl_union_set_get_ctx(uset);
4346 n = isl_union_set_n_basic_set(uset);
4347 if (n < 0)
4348 return NULL;
4349 list = isl_basic_set_list_alloc(ctx, n);
4350 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4351 list = isl_basic_set_list_free(list);
4353 return list;
4356 /* Internal data structure for isl_union_map_remove_map_if.
4357 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4359 struct isl_union_map_remove_map_if_data {
4360 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4361 void *user;
4364 /* isl_un_op_control filter that negates the result of data->fn
4365 * called on "map".
4367 static isl_bool not(__isl_keep isl_map *map, void *user)
4369 struct isl_union_map_remove_map_if_data *data = user;
4371 return isl_bool_not(data->fn(map, data->user));
4374 /* Dummy isl_un_op_control transformation callback that
4375 * simply returns the input.
4377 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4379 return map;
4382 /* Call "fn" on every map in "umap" and remove those maps
4383 * for which the callback returns true.
4385 * Use un_op to keep only those maps that are not filtered out,
4386 * applying an identity transformation on them.
4388 __isl_give isl_union_map *isl_union_map_remove_map_if(
4389 __isl_take isl_union_map *umap,
4390 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4392 struct isl_union_map_remove_map_if_data data = { fn, user };
4393 struct isl_un_op_control control = {
4394 .filter = &not,
4395 .filter_user = &data,
4396 .fn_map = &map_id,
4398 return un_op(umap, &control);
4401 /* Does "map" have "space" as range (ignoring parameters)?
4403 static isl_bool has_range_space(__isl_keep isl_map *map, void *user)
4405 isl_space *space = user;
4407 return isl_space_has_range_tuples(space, isl_map_peek_space(map));
4410 /* Wrapper around isl_map_bind_range for use as a un_op callback.
4412 static __isl_give isl_map *bind_range(__isl_take isl_map *map, void *user)
4414 isl_multi_id *tuple = user;
4416 return isl_map_bind_range(map, isl_multi_id_copy(tuple));
4419 /* Bind the output dimensions of "umap" to parameters with identifiers
4420 * specified by "tuple", living in the range space of "umap",
4421 * for those maps that have a matching range space.
4423 __isl_give isl_union_set *isl_union_map_bind_range(
4424 __isl_take isl_union_map *umap, __isl_take isl_multi_id *tuple)
4426 struct isl_un_op_control control = {
4427 .filter = &has_range_space,
4428 .filter_user = isl_multi_id_peek_space(tuple),
4429 .fn_map2 = &bind_range,
4430 .fn_map2_user = tuple,
4432 isl_union_set *bound;
4434 bound = uset_from_umap(un_op(umap, &control));
4435 isl_multi_id_free(tuple);
4436 return bound;