isl_map_simplify.c: lower_bound_is_cst: return -1 on error
[isl.git] / isl_union_map.c
blob36cab8100fa3f5511a5418939d183bc0e32caae7
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>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
30 #include <uset_to_umap.c>
31 #include <uset_from_umap.c>
32 #include <set_list_from_map_list_inl.c>
34 /* Return the number of parameters of "umap", where "type"
35 * is required to be set to isl_dim_param.
37 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
38 enum isl_dim_type type)
40 if (!umap)
41 return 0;
43 if (type != isl_dim_param)
44 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
45 "can only reference parameters", return 0);
47 return isl_space_dim(umap->dim, type);
50 /* Return the number of parameters of "uset", where "type"
51 * is required to be set to isl_dim_param.
53 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
54 enum isl_dim_type type)
56 return isl_union_map_dim(uset, type);
59 /* Return the id of the specified dimension.
61 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
62 enum isl_dim_type type, unsigned pos)
64 if (!umap)
65 return NULL;
67 if (type != isl_dim_param)
68 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
69 "can only reference parameters", return NULL);
71 return isl_space_get_dim_id(umap->dim, type, pos);
74 /* Is this union set a parameter domain?
76 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
78 isl_set *set;
79 isl_bool params;
81 if (!uset)
82 return isl_bool_error;
83 if (uset->table.n != 1)
84 return isl_bool_false;
86 set = isl_set_from_union_set(isl_union_set_copy(uset));
87 params = isl_set_is_params(set);
88 isl_set_free(set);
89 return params;
92 /* Is this union map actually a parameter domain?
93 * Users should never call this function. Outside of isl,
94 * a union map can never be a parameter domain.
96 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
98 return isl_union_set_is_params(uset_from_umap(umap));
101 static __isl_give isl_union_map *isl_union_map_alloc(
102 __isl_take isl_space *space, int size)
104 isl_union_map *umap;
106 space = isl_space_params(space);
107 if (!space)
108 return NULL;
110 umap = isl_calloc_type(space->ctx, isl_union_map);
111 if (!umap) {
112 isl_space_free(space);
113 return NULL;
116 umap->ref = 1;
117 umap->dim = space;
118 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
119 return isl_union_map_free(umap);
121 return umap;
124 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
126 return isl_union_map_alloc(space, 16);
129 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
131 return isl_union_map_empty(space);
134 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
136 return umap ? umap->dim->ctx : NULL;
139 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
141 return uset ? uset->dim->ctx : NULL;
144 /* Return the space of "umap".
146 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
148 return umap ? umap->dim : NULL;
151 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
153 return isl_space_copy(isl_union_map_peek_space(umap));
156 /* Return the position of the parameter with the given name
157 * in "umap".
158 * Return -1 if no such dimension can be found.
160 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
161 enum isl_dim_type type, const char *name)
163 if (!umap)
164 return -1;
165 return isl_space_find_dim_by_name(umap->dim, type, name);
168 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
170 return isl_union_map_get_space(uset);
173 static isl_stat free_umap_entry(void **entry, void *user)
175 isl_map *map = *entry;
176 isl_map_free(map);
177 return isl_stat_ok;
180 static isl_stat add_map(__isl_take isl_map *map, void *user)
182 isl_union_map **umap = (isl_union_map **)user;
184 *umap = isl_union_map_add_map(*umap, map);
186 return isl_stat_ok;
189 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
191 isl_union_map *dup;
193 if (!umap)
194 return NULL;
196 dup = isl_union_map_empty(isl_space_copy(umap->dim));
197 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
198 goto error;
199 return dup;
200 error:
201 isl_union_map_free(dup);
202 return NULL;
205 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
207 if (!umap)
208 return NULL;
210 if (umap->ref == 1)
211 return umap;
212 umap->ref--;
213 return isl_union_map_dup(umap);
216 struct isl_union_align {
217 isl_reordering *exp;
218 isl_union_map *res;
221 static isl_stat align_entry(void **entry, void *user)
223 isl_map *map = *entry;
224 isl_reordering *exp;
225 struct isl_union_align *data = user;
227 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
228 isl_map_get_space(map));
230 data->res = isl_union_map_add_map(data->res,
231 isl_map_realign(isl_map_copy(map), exp));
233 return isl_stat_ok;
236 /* Align the parameters of umap along those of model.
237 * The result has the parameters of model first, in the same order
238 * as they appear in model, followed by any remaining parameters of
239 * umap that do not appear in model.
241 __isl_give isl_union_map *isl_union_map_align_params(
242 __isl_take isl_union_map *umap, __isl_take isl_space *model)
244 struct isl_union_align data = { NULL, NULL };
245 isl_bool equal_params;
247 if (!umap || !model)
248 goto error;
250 equal_params = isl_space_has_equal_params(umap->dim, model);
251 if (equal_params < 0)
252 goto error;
253 if (equal_params) {
254 isl_space_free(model);
255 return umap;
258 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
259 if (!data.exp)
260 goto error;
262 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
263 umap->table.n);
264 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
265 &align_entry, &data) < 0)
266 goto error;
268 isl_reordering_free(data.exp);
269 isl_union_map_free(umap);
270 isl_space_free(model);
271 return data.res;
272 error:
273 isl_reordering_free(data.exp);
274 isl_union_map_free(umap);
275 isl_union_map_free(data.res);
276 isl_space_free(model);
277 return NULL;
280 __isl_give isl_union_set *isl_union_set_align_params(
281 __isl_take isl_union_set *uset, __isl_take isl_space *model)
283 return isl_union_map_align_params(uset, model);
286 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
287 __isl_take isl_union_map *umap2)
289 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
290 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
292 umap1 = isl_union_map_cow(umap1);
294 if (!umap1 || !umap2)
295 goto error;
297 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
298 goto error;
300 isl_union_map_free(umap2);
302 return umap1;
303 error:
304 isl_union_map_free(umap1);
305 isl_union_map_free(umap2);
306 return NULL;
309 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
310 __isl_take isl_union_set *uset2)
312 return isl_union_map_union(uset1, uset2);
315 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
317 if (!umap)
318 return NULL;
320 umap->ref++;
321 return umap;
324 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
326 return isl_union_map_copy(uset);
329 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
331 if (!umap)
332 return NULL;
334 if (--umap->ref > 0)
335 return NULL;
337 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
338 &free_umap_entry, NULL);
339 isl_hash_table_clear(&umap->table);
340 isl_space_free(umap->dim);
341 free(umap);
342 return NULL;
345 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
347 return isl_union_map_free(uset);
350 /* Do "umap" and "space" have the same parameters?
352 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
353 __isl_keep isl_space *space)
355 isl_space *umap_space;
357 umap_space = isl_union_map_peek_space(umap);
358 return isl_space_has_equal_params(umap_space, space);
361 /* Do "uset" and "space" have the same parameters?
363 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
364 __isl_keep isl_space *space)
366 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
369 static int has_space(const void *entry, const void *val)
371 isl_map *map = (isl_map *)entry;
372 isl_space *space = (isl_space *) val;
374 return isl_space_is_equal(map->dim, space);
377 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
378 __isl_take isl_map *map)
380 uint32_t hash;
381 struct isl_hash_table_entry *entry;
382 isl_bool aligned;
384 if (!map || !umap)
385 goto error;
387 if (isl_map_plain_is_empty(map)) {
388 isl_map_free(map);
389 return umap;
392 aligned = isl_map_space_has_equal_params(map, umap->dim);
393 if (aligned < 0)
394 goto error;
395 if (!aligned) {
396 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
397 map = isl_map_align_params(map, isl_union_map_get_space(umap));
400 umap = isl_union_map_cow(umap);
402 if (!map || !umap)
403 goto error;
405 hash = isl_space_get_hash(map->dim);
406 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
407 &has_space, map->dim, 1);
408 if (!entry)
409 goto error;
411 if (!entry->data)
412 entry->data = map;
413 else {
414 entry->data = isl_map_union(entry->data, isl_map_copy(map));
415 if (!entry->data)
416 goto error;
417 isl_map_free(map);
420 return umap;
421 error:
422 isl_map_free(map);
423 isl_union_map_free(umap);
424 return NULL;
427 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
428 __isl_take isl_set *set)
430 return isl_union_map_add_map(uset, set_to_map(set));
433 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
435 isl_space *dim;
436 isl_union_map *umap;
438 if (!map)
439 return NULL;
441 dim = isl_map_get_space(map);
442 dim = isl_space_params(dim);
443 umap = isl_union_map_empty(dim);
444 umap = isl_union_map_add_map(umap, map);
446 return umap;
449 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
451 return isl_union_map_from_map(set_to_map(set));
454 __isl_give isl_union_map *isl_union_map_from_basic_map(
455 __isl_take isl_basic_map *bmap)
457 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
460 __isl_give isl_union_set *isl_union_set_from_basic_set(
461 __isl_take isl_basic_set *bset)
463 return isl_union_map_from_basic_map(bset);
466 struct isl_union_map_foreach_data
468 isl_stat (*fn)(__isl_take isl_map *map, void *user);
469 void *user;
472 static isl_stat call_on_copy(void **entry, void *user)
474 isl_map *map = *entry;
475 struct isl_union_map_foreach_data *data;
476 data = (struct isl_union_map_foreach_data *)user;
478 return data->fn(isl_map_copy(map), data->user);
481 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
483 return umap ? umap->table.n : 0;
486 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
488 return uset ? uset->table.n : 0;
491 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
492 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
494 struct isl_union_map_foreach_data data = { fn, user };
496 if (!umap)
497 return isl_stat_error;
499 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
500 &call_on_copy, &data);
503 /* Internal data structure for isl_union_map_every_map.
505 * "test" is the user-specified callback function.
506 * "user" is the user-specified callback function argument.
508 * "failed" is initialized to 0 and set to 1 if "test" fails
509 * on any map.
511 struct isl_union_map_every_data {
512 isl_bool (*test)(__isl_keep isl_map *map, void *user);
513 void *user;
514 int failed;
517 /* Call data->test on "map".
518 * If this fails, then set data->failed and abort.
520 static isl_stat call_every(void **entry, void *user)
522 isl_map *map = *entry;
523 struct isl_union_map_every_data *data = user;
524 isl_bool r;
526 r = data->test(map, data->user);
527 if (r < 0)
528 return isl_stat_error;
529 if (r)
530 return isl_stat_ok;
531 data->failed = 1;
532 return isl_stat_error;
535 /* Does "test" succeed on every map in "umap"?
537 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
538 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
540 struct isl_union_map_every_data data = { test, user, 0 };
541 isl_stat r;
543 if (!umap)
544 return isl_bool_error;
546 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
547 &call_every, &data);
548 if (r >= 0)
549 return isl_bool_true;
550 if (data.failed)
551 return isl_bool_false;
552 return isl_bool_error;
555 /* Add "map" to "list".
557 static isl_stat add_list_map(__isl_take isl_map *map, void *user)
559 isl_map_list **list = user;
561 *list = isl_map_list_add(*list, map);
563 if (!*list)
564 return isl_stat_error;
565 return isl_stat_ok;
568 /* Return the maps in "umap" as a list.
570 * First construct a list of the appropriate size and then add all the
571 * elements.
573 __isl_give isl_map_list *isl_union_map_get_map_list(
574 __isl_keep isl_union_map *umap)
576 int n_maps;
577 isl_ctx *ctx;
578 isl_map_list *list;
580 if (!umap)
581 return NULL;
582 ctx = isl_union_map_get_ctx(umap);
583 n_maps = isl_union_map_n_map(umap);
584 list = isl_map_list_alloc(ctx, n_maps);
586 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
587 list = isl_map_list_free(list);
589 return list;
592 /* Return the sets in "uset" as a list.
594 __isl_give isl_set_list *isl_union_set_get_set_list(
595 __isl_keep isl_union_set *uset)
597 return set_list_from_map_list(
598 isl_union_map_get_map_list(uset_to_umap(uset)));
601 static isl_stat copy_map(void **entry, void *user)
603 isl_map *map = *entry;
604 isl_map **map_p = user;
606 *map_p = isl_map_copy(map);
608 return isl_stat_error;
611 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
613 isl_ctx *ctx;
614 isl_map *map = NULL;
616 if (!umap)
617 return NULL;
618 ctx = isl_union_map_get_ctx(umap);
619 if (umap->table.n != 1)
620 isl_die(ctx, isl_error_invalid,
621 "union map needs to contain elements in exactly "
622 "one space", goto error);
624 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
626 isl_union_map_free(umap);
628 return map;
629 error:
630 isl_union_map_free(umap);
631 return NULL;
634 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
636 return isl_map_from_union_map(uset);
639 /* Extract the map in "umap" that lives in the given space (ignoring
640 * parameters).
642 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
643 __isl_take isl_space *space)
645 uint32_t hash;
646 struct isl_hash_table_entry *entry;
648 space = isl_space_drop_all_params(space);
649 space = isl_space_align_params(space, isl_union_map_get_space(umap));
650 if (!umap || !space)
651 goto error;
653 hash = isl_space_get_hash(space);
654 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
655 &has_space, space, 0);
656 if (!entry)
657 return isl_map_empty(space);
658 isl_space_free(space);
659 return isl_map_copy(entry->data);
660 error:
661 isl_space_free(space);
662 return NULL;
665 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
666 __isl_take isl_space *dim)
668 return set_from_map(isl_union_map_extract_map(uset, dim));
671 /* Check if umap contains a map in the given space.
673 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
674 __isl_keep isl_space *space)
676 uint32_t hash;
677 struct isl_hash_table_entry *entry;
679 if (!umap || !space)
680 return isl_bool_error;
682 hash = isl_space_get_hash(space);
683 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
684 &has_space, space, 0);
685 return !!entry;
688 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
689 __isl_keep isl_space *space)
691 return isl_union_map_contains(uset, space);
694 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
695 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
697 return isl_union_map_foreach_map(uset,
698 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
701 /* Internal data structure for isl_union_set_every_set.
703 * "test" is the user-specified callback function.
704 * "user" is the user-specified callback function argument.
706 struct isl_test_set_from_map_data {
707 isl_bool (*test)(__isl_keep isl_set *set, void *user);
708 void *user;
711 /* Call data->test on "map", which is part of an isl_union_set and
712 * therefore known to be an isl_set.
714 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
716 struct isl_test_set_from_map_data *data = user;
718 return data->test(set_from_map(map), data->user);
721 /* Does "test" succeed on every set in "uset"?
723 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
724 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
726 struct isl_test_set_from_map_data data = { test, user };
728 return isl_union_map_every_map(uset_to_umap(uset),
729 &test_set_from_map, &data);
732 struct isl_union_set_foreach_point_data {
733 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
734 void *user;
737 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
739 struct isl_union_set_foreach_point_data *data = user;
740 isl_stat r;
742 r = isl_set_foreach_point(set, data->fn, data->user);
743 isl_set_free(set);
745 return r;
748 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
749 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
751 struct isl_union_set_foreach_point_data data = { fn, user };
752 return isl_union_set_foreach_set(uset, &foreach_point, &data);
755 /* Data structure that specifies how gen_bin_op should
756 * construct results from the inputs.
758 * If "subtract" is set, then a map in the first input is copied to the result
759 * if there is no corresponding map in the second input.
760 * Otherwise, a map in the first input with no corresponding map
761 * in the second input is ignored.
762 * If "filter" is not NULL, then it specifies which maps in the first
763 * input may have a matching map in the second input.
764 * In particular, it makes sure that "match_space" can be called
765 * on the space of the map.
766 * "match_space" specifies how to transform the space of a map
767 * in the first input to the space of the corresponding map
768 * in the second input.
769 * "fn_map" specifies how the matching maps, one from each input,
770 * should be combined to form a map in the result.
772 struct isl_bin_op_control {
773 int subtract;
774 isl_bool (*filter)(__isl_keep isl_map *map);
775 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
776 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
777 __isl_take isl_map *map2);
780 /* Internal data structure for gen_bin_op.
781 * "control" specifies how the maps in the result should be constructed.
782 * "umap2" is a pointer to the second argument.
783 * "res" collects the results.
785 struct isl_union_map_gen_bin_data {
786 struct isl_bin_op_control *control;
787 isl_union_map *umap2;
788 isl_union_map *res;
791 /* Add a copy of "map" to "res" and return the result.
793 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
794 __isl_keep isl_map *map)
796 return isl_union_map_add_map(res, isl_map_copy(map));
799 /* Combine "map1" and "map2", add the result to "res" and return the result.
800 * Check whether the result is empty before adding it to "res".
802 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
803 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
804 struct isl_union_map_gen_bin_data *data)
806 isl_bool empty;
807 isl_map *map;
809 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
810 empty = isl_map_is_empty(map);
811 if (empty < 0 || empty) {
812 isl_map_free(map);
813 if (empty < 0)
814 return isl_union_map_free(res);
815 return res;
817 return isl_union_map_add_map(res, map);
820 /* Dummy match_space function that simply returns the input space.
822 static __isl_give isl_space *identity(__isl_take isl_space *space)
824 return space;
827 /* Look for the map in data->umap2 that corresponds to "map", if any.
828 * Return (isl_bool_true, matching map) if there is one,
829 * (isl_bool_false, NULL) if there is no matching map and
830 * (isl_bool_error, NULL) on error.
832 * If not NULL, then data->control->filter specifies whether "map"
833 * can have any matching map. If so,
834 * data->control->match_space specifies which map in data->umap2
835 * corresponds to "map".
837 static __isl_keep isl_maybe_isl_map bin_try_get_match(
838 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
840 uint32_t hash;
841 struct isl_hash_table_entry *entry2;
842 isl_space *space;
843 isl_maybe_isl_map res = { isl_bool_error, NULL };
845 if (data->control->filter) {
846 res.valid = data->control->filter(map);
847 if (res.valid < 0 || !res.valid)
848 return res;
849 res.valid = isl_bool_error;
852 space = isl_map_get_space(map);
853 if (data->control->match_space != &identity)
854 space = data->control->match_space(space);
855 if (!space)
856 return res;
857 hash = isl_space_get_hash(space);
858 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
859 &data->umap2->table, hash,
860 &has_space, space, 0);
861 isl_space_free(space);
862 res.valid = entry2 != NULL;
863 if (entry2)
864 res.value = entry2->data;
866 return res;
869 /* isl_hash_table_foreach callback for gen_bin_op.
870 * Look for the map in data->umap2 that corresponds
871 * to the map that "entry" points to, apply the binary operation and
872 * add the result to data->res.
874 * If no corresponding map can be found, then the effect depends
875 * on data->control->subtract. If it is set, then the current map
876 * is added directly to the result. Otherwise, it is ignored.
878 static isl_stat gen_bin_entry(void **entry, void *user)
880 struct isl_union_map_gen_bin_data *data = user;
881 isl_map *map = *entry;
882 isl_maybe_isl_map m;
884 m = bin_try_get_match(data, map);
885 if (m.valid < 0)
886 return isl_stat_error;
887 if (!m.valid && !data->control->subtract)
888 return isl_stat_ok;
890 if (!m.valid)
891 data->res = bin_add_map(data->res, map);
892 else
893 data->res = bin_add_pair(data->res, map, m.value, data);
894 if (!data->res)
895 return isl_stat_error;
897 return isl_stat_ok;
900 /* Apply a binary operation to "umap1" and "umap2" based on "control".
901 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
902 * in gen_bin_entry.
904 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
905 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
907 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
909 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
910 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
912 if (!umap1 || !umap2)
913 goto error;
915 data.umap2 = umap2;
916 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
917 umap1->table.n);
918 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
919 &gen_bin_entry, &data) < 0)
920 goto error;
922 isl_union_map_free(umap1);
923 isl_union_map_free(umap2);
924 return data.res;
925 error:
926 isl_union_map_free(umap1);
927 isl_union_map_free(umap2);
928 isl_union_map_free(data.res);
929 return NULL;
932 __isl_give isl_union_map *isl_union_map_subtract(
933 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
935 struct isl_bin_op_control control = {
936 .subtract = 1,
937 .match_space = &identity,
938 .fn_map = &isl_map_subtract,
941 return gen_bin_op(umap1, umap2, &control);
944 __isl_give isl_union_set *isl_union_set_subtract(
945 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
947 return isl_union_map_subtract(uset1, uset2);
950 struct isl_union_map_gen_bin_set_data {
951 isl_set *set;
952 isl_union_map *res;
955 static isl_stat intersect_params_entry(void **entry, void *user)
957 struct isl_union_map_gen_bin_set_data *data = user;
958 isl_map *map = *entry;
959 int empty;
961 map = isl_map_copy(map);
962 map = isl_map_intersect_params(map, isl_set_copy(data->set));
964 empty = isl_map_is_empty(map);
965 if (empty < 0) {
966 isl_map_free(map);
967 return isl_stat_error;
970 data->res = isl_union_map_add_map(data->res, map);
972 return isl_stat_ok;
975 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
976 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
978 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
980 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
981 set = isl_set_align_params(set, isl_union_map_get_space(umap));
983 if (!umap || !set)
984 goto error;
986 data.set = set;
987 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
988 umap->table.n);
989 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
990 fn, &data) < 0)
991 goto error;
993 isl_union_map_free(umap);
994 isl_set_free(set);
995 return data.res;
996 error:
997 isl_union_map_free(umap);
998 isl_set_free(set);
999 isl_union_map_free(data.res);
1000 return NULL;
1003 /* Intersect "umap" with the parameter domain "set".
1005 * If "set" does not have any constraints, then we can return immediately.
1007 __isl_give isl_union_map *isl_union_map_intersect_params(
1008 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1010 int is_universe;
1012 is_universe = isl_set_plain_is_universe(set);
1013 if (is_universe < 0)
1014 goto error;
1015 if (is_universe) {
1016 isl_set_free(set);
1017 return umap;
1020 return gen_bin_set_op(umap, set, &intersect_params_entry);
1021 error:
1022 isl_union_map_free(umap);
1023 isl_set_free(set);
1024 return NULL;
1027 __isl_give isl_union_set *isl_union_set_intersect_params(
1028 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1030 return isl_union_map_intersect_params(uset, set);
1033 static __isl_give isl_union_map *union_map_intersect_params(
1034 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1036 return isl_union_map_intersect_params(umap,
1037 isl_set_from_union_set(uset));
1040 static __isl_give isl_union_map *union_map_gist_params(
1041 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1043 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1046 struct isl_union_map_match_bin_data {
1047 isl_union_map *umap2;
1048 isl_union_map *res;
1049 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1052 static isl_stat match_bin_entry(void **entry, void *user)
1054 struct isl_union_map_match_bin_data *data = user;
1055 uint32_t hash;
1056 struct isl_hash_table_entry *entry2;
1057 isl_map *map = *entry;
1058 int empty;
1060 hash = isl_space_get_hash(map->dim);
1061 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1062 hash, &has_space, map->dim, 0);
1063 if (!entry2)
1064 return isl_stat_ok;
1066 map = isl_map_copy(map);
1067 map = data->fn(map, isl_map_copy(entry2->data));
1069 empty = isl_map_is_empty(map);
1070 if (empty < 0) {
1071 isl_map_free(map);
1072 return isl_stat_error;
1074 if (empty) {
1075 isl_map_free(map);
1076 return isl_stat_ok;
1079 data->res = isl_union_map_add_map(data->res, map);
1081 return isl_stat_ok;
1084 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1085 __isl_take isl_union_map *umap2,
1086 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1088 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1090 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1091 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1093 if (!umap1 || !umap2)
1094 goto error;
1096 data.umap2 = umap2;
1097 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1098 umap1->table.n);
1099 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1100 &match_bin_entry, &data) < 0)
1101 goto error;
1103 isl_union_map_free(umap1);
1104 isl_union_map_free(umap2);
1105 return data.res;
1106 error:
1107 isl_union_map_free(umap1);
1108 isl_union_map_free(umap2);
1109 isl_union_map_free(data.res);
1110 return NULL;
1113 __isl_give isl_union_map *isl_union_map_intersect(
1114 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1116 return match_bin_op(umap1, umap2, &isl_map_intersect);
1119 /* Compute the intersection of the two union_sets.
1120 * As a special case, if exactly one of the two union_sets
1121 * is a parameter domain, then intersect the parameter domain
1122 * of the other one with this set.
1124 __isl_give isl_union_set *isl_union_set_intersect(
1125 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1127 int p1, p2;
1129 p1 = isl_union_set_is_params(uset1);
1130 p2 = isl_union_set_is_params(uset2);
1131 if (p1 < 0 || p2 < 0)
1132 goto error;
1133 if (!p1 && p2)
1134 return union_map_intersect_params(uset1, uset2);
1135 if (p1 && !p2)
1136 return union_map_intersect_params(uset2, uset1);
1137 return isl_union_map_intersect(uset1, uset2);
1138 error:
1139 isl_union_set_free(uset1);
1140 isl_union_set_free(uset2);
1141 return NULL;
1144 static isl_stat gist_params_entry(void **entry, void *user)
1146 struct isl_union_map_gen_bin_set_data *data = user;
1147 isl_map *map = *entry;
1148 int empty;
1150 map = isl_map_copy(map);
1151 map = isl_map_gist_params(map, isl_set_copy(data->set));
1153 empty = isl_map_is_empty(map);
1154 if (empty < 0) {
1155 isl_map_free(map);
1156 return isl_stat_error;
1159 data->res = isl_union_map_add_map(data->res, map);
1161 return isl_stat_ok;
1164 __isl_give isl_union_map *isl_union_map_gist_params(
1165 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1167 return gen_bin_set_op(umap, set, &gist_params_entry);
1170 __isl_give isl_union_set *isl_union_set_gist_params(
1171 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1173 return isl_union_map_gist_params(uset, set);
1176 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1177 __isl_take isl_union_map *context)
1179 return match_bin_op(umap, context, &isl_map_gist);
1182 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1183 __isl_take isl_union_set *context)
1185 if (isl_union_set_is_params(context))
1186 return union_map_gist_params(uset, context);
1187 return isl_union_map_gist(uset, context);
1190 /* For each map in "umap", remove the constraints in the corresponding map
1191 * of "context".
1192 * Each map in "context" is assumed to consist of a single disjunct and
1193 * to have explicit representations for all local variables.
1195 __isl_give isl_union_map *isl_union_map_plain_gist(
1196 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1198 return match_bin_op(umap, context, &isl_map_plain_gist);
1201 /* For each set in "uset", remove the constraints in the corresponding set
1202 * of "context".
1203 * Each set in "context" is assumed to consist of a single disjunct and
1204 * to have explicit representations for all local variables.
1206 __isl_give isl_union_set *isl_union_set_plain_gist(
1207 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1209 return isl_union_map_plain_gist(uset, context);
1212 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1213 __isl_take isl_map *set2)
1215 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1218 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1219 __isl_take isl_map *set2)
1221 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1224 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1225 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1227 return match_bin_op(uset1, uset2, &lex_lt_set);
1230 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1231 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1233 return match_bin_op(uset1, uset2, &lex_le_set);
1236 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1237 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1239 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1242 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1243 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1245 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1248 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1249 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1251 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1254 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1255 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1257 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1260 /* Intersect the domain of "umap" with "uset".
1262 static __isl_give isl_union_map *union_map_intersect_domain(
1263 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1265 struct isl_bin_op_control control = {
1266 .match_space = &isl_space_domain,
1267 .fn_map = &isl_map_intersect_domain,
1270 return gen_bin_op(umap, uset, &control);
1273 /* Intersect the domain of "umap" with "uset".
1274 * If "uset" is a parameters domain, then intersect the parameter
1275 * domain of "umap" with this set.
1277 __isl_give isl_union_map *isl_union_map_intersect_domain(
1278 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1280 if (isl_union_set_is_params(uset))
1281 return union_map_intersect_params(umap, uset);
1282 else
1283 return union_map_intersect_domain(umap, uset);
1286 /* Remove the elements of "uset" from the domain of "umap".
1288 __isl_give isl_union_map *isl_union_map_subtract_domain(
1289 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1291 struct isl_bin_op_control control = {
1292 .subtract = 1,
1293 .match_space = &isl_space_domain,
1294 .fn_map = &isl_map_subtract_domain,
1297 return gen_bin_op(umap, dom, &control);
1300 /* Remove the elements of "uset" from the range of "umap".
1302 __isl_give isl_union_map *isl_union_map_subtract_range(
1303 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1305 struct isl_bin_op_control control = {
1306 .subtract = 1,
1307 .match_space = &isl_space_range,
1308 .fn_map = &isl_map_subtract_range,
1311 return gen_bin_op(umap, dom, &control);
1314 /* Compute the gist of "umap" with respect to the domain "uset".
1316 static __isl_give isl_union_map *union_map_gist_domain(
1317 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1319 struct isl_bin_op_control control = {
1320 .match_space = &isl_space_domain,
1321 .fn_map = &isl_map_gist_domain,
1324 return gen_bin_op(umap, uset, &control);
1327 /* Compute the gist of "umap" with respect to the domain "uset".
1328 * If "uset" is a parameters domain, then compute the gist
1329 * with respect to this parameter domain.
1331 __isl_give isl_union_map *isl_union_map_gist_domain(
1332 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1334 if (isl_union_set_is_params(uset))
1335 return union_map_gist_params(umap, uset);
1336 else
1337 return union_map_gist_domain(umap, uset);
1340 /* Compute the gist of "umap" with respect to the range "uset".
1342 __isl_give isl_union_map *isl_union_map_gist_range(
1343 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1345 struct isl_bin_op_control control = {
1346 .match_space = &isl_space_range,
1347 .fn_map = &isl_map_gist_range,
1350 return gen_bin_op(umap, uset, &control);
1353 __isl_give isl_union_map *isl_union_map_intersect_range(
1354 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1356 struct isl_bin_op_control control = {
1357 .match_space = &isl_space_range,
1358 .fn_map = &isl_map_intersect_range,
1361 return gen_bin_op(umap, uset, &control);
1364 /* Intersect each map in "umap" in a space A -> [B -> C]
1365 * with the corresponding map in "factor" in the space A -> C and
1366 * collect the results.
1368 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1369 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1371 struct isl_bin_op_control control = {
1372 .filter = &isl_map_range_is_wrapping,
1373 .match_space = &isl_space_range_factor_range,
1374 .fn_map = &isl_map_intersect_range_factor_range,
1377 return gen_bin_op(umap, factor, &control);
1380 struct isl_union_map_bin_data {
1381 isl_union_map *umap2;
1382 isl_union_map *res;
1383 isl_map *map;
1384 isl_stat (*fn)(void **entry, void *user);
1387 static isl_stat apply_range_entry(void **entry, void *user)
1389 struct isl_union_map_bin_data *data = user;
1390 isl_map *map2 = *entry;
1391 isl_bool empty;
1393 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1394 map2->dim, isl_dim_in))
1395 return isl_stat_ok;
1397 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1399 empty = isl_map_is_empty(map2);
1400 if (empty < 0) {
1401 isl_map_free(map2);
1402 return isl_stat_error;
1404 if (empty) {
1405 isl_map_free(map2);
1406 return isl_stat_ok;
1409 data->res = isl_union_map_add_map(data->res, map2);
1411 return isl_stat_ok;
1414 static isl_stat bin_entry(void **entry, void *user)
1416 struct isl_union_map_bin_data *data = user;
1417 isl_map *map = *entry;
1419 data->map = map;
1420 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1421 data->fn, data) < 0)
1422 return isl_stat_error;
1424 return isl_stat_ok;
1427 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1428 __isl_take isl_union_map *umap2,
1429 isl_stat (*fn)(void **entry, void *user))
1431 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1433 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1434 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1436 if (!umap1 || !umap2)
1437 goto error;
1439 data.umap2 = umap2;
1440 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1441 umap1->table.n);
1442 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1443 &bin_entry, &data) < 0)
1444 goto error;
1446 isl_union_map_free(umap1);
1447 isl_union_map_free(umap2);
1448 return data.res;
1449 error:
1450 isl_union_map_free(umap1);
1451 isl_union_map_free(umap2);
1452 isl_union_map_free(data.res);
1453 return NULL;
1456 __isl_give isl_union_map *isl_union_map_apply_range(
1457 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1459 return bin_op(umap1, umap2, &apply_range_entry);
1462 __isl_give isl_union_map *isl_union_map_apply_domain(
1463 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1465 umap1 = isl_union_map_reverse(umap1);
1466 umap1 = isl_union_map_apply_range(umap1, umap2);
1467 return isl_union_map_reverse(umap1);
1470 __isl_give isl_union_set *isl_union_set_apply(
1471 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1473 return isl_union_map_apply_range(uset, umap);
1476 static isl_stat map_lex_lt_entry(void **entry, void *user)
1478 struct isl_union_map_bin_data *data = user;
1479 isl_map *map2 = *entry;
1481 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1482 map2->dim, isl_dim_out))
1483 return isl_stat_ok;
1485 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1487 data->res = isl_union_map_add_map(data->res, map2);
1489 return isl_stat_ok;
1492 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1493 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1495 return bin_op(umap1, umap2, &map_lex_lt_entry);
1498 static isl_stat map_lex_le_entry(void **entry, void *user)
1500 struct isl_union_map_bin_data *data = user;
1501 isl_map *map2 = *entry;
1503 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1504 map2->dim, isl_dim_out))
1505 return isl_stat_ok;
1507 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1509 data->res = isl_union_map_add_map(data->res, map2);
1511 return isl_stat_ok;
1514 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1515 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1517 return bin_op(umap1, umap2, &map_lex_le_entry);
1520 static isl_stat product_entry(void **entry, void *user)
1522 struct isl_union_map_bin_data *data = user;
1523 isl_map *map2 = *entry;
1525 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1527 data->res = isl_union_map_add_map(data->res, map2);
1529 return isl_stat_ok;
1532 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1533 __isl_take isl_union_map *umap2)
1535 return bin_op(umap1, umap2, &product_entry);
1538 static isl_stat set_product_entry(void **entry, void *user)
1540 struct isl_union_map_bin_data *data = user;
1541 isl_set *set2 = *entry;
1543 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1545 data->res = isl_union_set_add_set(data->res, set2);
1547 return isl_stat_ok;
1550 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1551 __isl_take isl_union_set *uset2)
1553 return bin_op(uset1, uset2, &set_product_entry);
1556 static isl_stat domain_product_entry(void **entry, void *user)
1558 struct isl_union_map_bin_data *data = user;
1559 isl_map *map2 = *entry;
1561 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1562 map2->dim, isl_dim_out))
1563 return isl_stat_ok;
1565 map2 = isl_map_domain_product(isl_map_copy(data->map),
1566 isl_map_copy(map2));
1568 data->res = isl_union_map_add_map(data->res, map2);
1570 return isl_stat_ok;
1573 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1575 __isl_give isl_union_map *isl_union_map_domain_product(
1576 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1578 return bin_op(umap1, umap2, &domain_product_entry);
1581 static isl_stat range_product_entry(void **entry, void *user)
1583 struct isl_union_map_bin_data *data = user;
1584 isl_map *map2 = *entry;
1586 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1587 map2->dim, isl_dim_in))
1588 return isl_stat_ok;
1590 map2 = isl_map_range_product(isl_map_copy(data->map),
1591 isl_map_copy(map2));
1593 data->res = isl_union_map_add_map(data->res, map2);
1595 return isl_stat_ok;
1598 __isl_give isl_union_map *isl_union_map_range_product(
1599 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1601 return bin_op(umap1, umap2, &range_product_entry);
1604 /* If data->map A -> B and "map2" C -> D have the same range space,
1605 * then add (A, C) -> (B * D) to data->res.
1607 static isl_stat flat_domain_product_entry(void **entry, void *user)
1609 struct isl_union_map_bin_data *data = user;
1610 isl_map *map2 = *entry;
1612 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1613 map2->dim, isl_dim_out))
1614 return isl_stat_ok;
1616 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1617 isl_map_copy(map2));
1619 data->res = isl_union_map_add_map(data->res, map2);
1621 return isl_stat_ok;
1624 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1626 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1627 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1629 return bin_op(umap1, umap2, &flat_domain_product_entry);
1632 static isl_stat flat_range_product_entry(void **entry, void *user)
1634 struct isl_union_map_bin_data *data = user;
1635 isl_map *map2 = *entry;
1637 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1638 map2->dim, isl_dim_in))
1639 return isl_stat_ok;
1641 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1642 isl_map_copy(map2));
1644 data->res = isl_union_map_add_map(data->res, map2);
1646 return isl_stat_ok;
1649 __isl_give isl_union_map *isl_union_map_flat_range_product(
1650 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1652 return bin_op(umap1, umap2, &flat_range_product_entry);
1655 /* Data structure that specifies how un_op should modify
1656 * the maps in the union map.
1658 * If "inplace" is set, then the maps in the input union map
1659 * are modified in place. This means that "fn_map" should not
1660 * change the meaning of the map or that the union map only
1661 * has a single reference.
1662 * If "total" is set, then all maps need to be modified and
1663 * the results need to live in the same space.
1664 * Otherwise, a new union map is constructed to store the results.
1665 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1666 * are taken into account. "filter_user" is passed as the second argument
1667 * to "filter". No filter can be set if "inplace" or
1668 * "total" is set.
1669 * "fn_map" specifies how the maps (selected by "filter")
1670 * should be transformed.
1672 struct isl_un_op_control {
1673 int inplace;
1674 int total;
1675 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1676 void *filter_user;
1677 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1680 /* Data structure for wrapping the data for un_op_filter_drop_user.
1681 * "filter" is the function that is being wrapped.
1683 struct isl_un_op_drop_user_data {
1684 isl_bool (*filter)(__isl_keep isl_map *map);
1687 /* Wrapper for isl_un_op_control filters that do not require
1688 * a second argument.
1689 * Simply call data->filter without the second argument.
1691 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1693 struct isl_un_op_drop_user_data *data = user;
1694 return data->filter(map);
1697 /* Internal data structure for "un_op".
1698 * "control" specifies how the maps in the union map should be modified.
1699 * "res" collects the results.
1701 struct isl_union_map_un_data {
1702 struct isl_un_op_control *control;
1703 isl_union_map *res;
1706 /* isl_hash_table_foreach callback for un_op.
1707 * Handle the map that "entry" points to.
1709 * If control->filter is set, then check if this map satisfies the filter.
1710 * If so (or if control->filter is not set), modify the map
1711 * by calling control->fn_map and either add the result to data->res or
1712 * replace the original entry by the result (if control->inplace is set).
1714 static isl_stat un_entry(void **entry, void *user)
1716 struct isl_union_map_un_data *data = user;
1717 isl_map *map = *entry;
1719 if (data->control->filter) {
1720 isl_bool ok;
1722 ok = data->control->filter(map, data->control->filter_user);
1723 if (ok < 0)
1724 return isl_stat_error;
1725 if (!ok)
1726 return isl_stat_ok;
1729 map = data->control->fn_map(isl_map_copy(map));
1730 if (!map)
1731 return isl_stat_error;
1732 if (data->control->inplace) {
1733 isl_map_free(*entry);
1734 *entry = map;
1735 } else {
1736 data->res = isl_union_map_add_map(data->res, map);
1737 if (!data->res)
1738 return isl_stat_error;
1741 return isl_stat_ok;
1744 /* Modify the maps in "umap" based on "control".
1745 * If control->inplace is set, then modify the maps in "umap" in-place.
1746 * Otherwise, create a new union map to hold the results.
1747 * If control->total is set, then perform an inplace computation
1748 * if "umap" is only referenced once. Otherwise, create a new union map
1749 * to store the results.
1751 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1752 struct isl_un_op_control *control)
1754 struct isl_union_map_un_data data = { control };
1756 if (!umap)
1757 return NULL;
1758 if ((control->inplace || control->total) && control->filter)
1759 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1760 "inplace/total modification cannot be filtered",
1761 return isl_union_map_free(umap));
1763 if (control->total && umap->ref == 1)
1764 control->inplace = 1;
1765 if (control->inplace) {
1766 data.res = umap;
1767 } else {
1768 isl_space *space;
1770 space = isl_union_map_get_space(umap);
1771 data.res = isl_union_map_alloc(space, umap->table.n);
1773 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1774 &umap->table, &un_entry, &data) < 0)
1775 data.res = isl_union_map_free(data.res);
1777 if (control->inplace)
1778 return data.res;
1779 isl_union_map_free(umap);
1780 return data.res;
1783 __isl_give isl_union_map *isl_union_map_from_range(
1784 __isl_take isl_union_set *uset)
1786 struct isl_un_op_control control = {
1787 .fn_map = &isl_map_from_range,
1789 return un_op(uset, &control);
1792 __isl_give isl_union_map *isl_union_map_from_domain(
1793 __isl_take isl_union_set *uset)
1795 return isl_union_map_reverse(isl_union_map_from_range(uset));
1798 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1799 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1801 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1802 isl_union_map_from_range(range));
1805 /* Modify the maps in "umap" by applying "fn" on them.
1806 * "fn" should apply to all maps in "umap" and should not modify the space.
1808 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1809 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1811 struct isl_un_op_control control = {
1812 .total = 1,
1813 .fn_map = fn,
1816 return un_op(umap, &control);
1819 /* Compute the affine hull of "map" and return the result as an isl_map.
1821 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1823 return isl_map_from_basic_map(isl_map_affine_hull(map));
1826 __isl_give isl_union_map *isl_union_map_affine_hull(
1827 __isl_take isl_union_map *umap)
1829 return total(umap, &isl_map_affine_hull_map);
1832 __isl_give isl_union_set *isl_union_set_affine_hull(
1833 __isl_take isl_union_set *uset)
1835 return isl_union_map_affine_hull(uset);
1838 /* Wrapper around isl_set_combined_lineality_space
1839 * that returns the combined lineality space in the form of an isl_set
1840 * instead of an isl_basic_set.
1842 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1844 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1847 /* For each set in "uset", compute the (linear) hull
1848 * of the lineality spaces of its basic sets and
1849 * collect and return the results.
1851 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1852 __isl_take isl_union_set *uset)
1854 struct isl_un_op_control control = {
1855 .fn_map = &combined_lineality_space,
1857 return un_op(uset, &control);
1860 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1862 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1864 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1867 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1868 __isl_take isl_union_map *umap)
1870 return total(umap, &isl_map_polyhedral_hull_map);
1873 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1874 __isl_take isl_union_set *uset)
1876 return isl_union_map_polyhedral_hull(uset);
1879 /* Compute a superset of the convex hull of "map" that is described
1880 * by only translates of the constraints in the constituents of "map" and
1881 * return the result as an isl_map.
1883 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1885 return isl_map_from_basic_map(isl_map_simple_hull(map));
1888 __isl_give isl_union_map *isl_union_map_simple_hull(
1889 __isl_take isl_union_map *umap)
1891 return total(umap, &isl_map_simple_hull_map);
1894 __isl_give isl_union_set *isl_union_set_simple_hull(
1895 __isl_take isl_union_set *uset)
1897 return isl_union_map_simple_hull(uset);
1900 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1901 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1903 struct isl_un_op_control control = {
1904 .inplace = 1,
1905 .fn_map = fn,
1908 return un_op(umap, &control);
1911 /* Remove redundant constraints in each of the basic maps of "umap".
1912 * Since removing redundant constraints does not change the meaning
1913 * or the space, the operation can be performed in-place.
1915 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1916 __isl_take isl_union_map *umap)
1918 return inplace(umap, &isl_map_remove_redundancies);
1921 /* Remove redundant constraints in each of the basic sets of "uset".
1923 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1924 __isl_take isl_union_set *uset)
1926 return isl_union_map_remove_redundancies(uset);
1929 __isl_give isl_union_map *isl_union_map_coalesce(
1930 __isl_take isl_union_map *umap)
1932 return inplace(umap, &isl_map_coalesce);
1935 __isl_give isl_union_set *isl_union_set_coalesce(
1936 __isl_take isl_union_set *uset)
1938 return isl_union_map_coalesce(uset);
1941 __isl_give isl_union_map *isl_union_map_detect_equalities(
1942 __isl_take isl_union_map *umap)
1944 return inplace(umap, &isl_map_detect_equalities);
1947 __isl_give isl_union_set *isl_union_set_detect_equalities(
1948 __isl_take isl_union_set *uset)
1950 return isl_union_map_detect_equalities(uset);
1953 __isl_give isl_union_map *isl_union_map_compute_divs(
1954 __isl_take isl_union_map *umap)
1956 return inplace(umap, &isl_map_compute_divs);
1959 __isl_give isl_union_set *isl_union_set_compute_divs(
1960 __isl_take isl_union_set *uset)
1962 return isl_union_map_compute_divs(uset);
1965 __isl_give isl_union_map *isl_union_map_lexmin(
1966 __isl_take isl_union_map *umap)
1968 return total(umap, &isl_map_lexmin);
1971 __isl_give isl_union_set *isl_union_set_lexmin(
1972 __isl_take isl_union_set *uset)
1974 return isl_union_map_lexmin(uset);
1977 __isl_give isl_union_map *isl_union_map_lexmax(
1978 __isl_take isl_union_map *umap)
1980 return total(umap, &isl_map_lexmax);
1983 __isl_give isl_union_set *isl_union_set_lexmax(
1984 __isl_take isl_union_set *uset)
1986 return isl_union_map_lexmax(uset);
1989 /* Return the universe in the space of "map".
1991 static __isl_give isl_map *universe(__isl_take isl_map *map)
1993 isl_space *space;
1995 space = isl_map_get_space(map);
1996 isl_map_free(map);
1997 return isl_map_universe(space);
2000 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2002 struct isl_un_op_control control = {
2003 .fn_map = &universe,
2005 return un_op(umap, &control);
2008 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2010 return isl_union_map_universe(uset);
2013 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2015 struct isl_un_op_control control = {
2016 .fn_map = &isl_map_reverse,
2018 return un_op(umap, &control);
2021 /* Compute the parameter domain of the given union map.
2023 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2025 struct isl_un_op_control control = {
2026 .fn_map = &isl_map_params,
2028 int empty;
2030 empty = isl_union_map_is_empty(umap);
2031 if (empty < 0)
2032 goto error;
2033 if (empty) {
2034 isl_space *space;
2035 space = isl_union_map_get_space(umap);
2036 isl_union_map_free(umap);
2037 return isl_set_empty(space);
2039 return isl_set_from_union_set(un_op(umap, &control));
2040 error:
2041 isl_union_map_free(umap);
2042 return NULL;
2045 /* Compute the parameter domain of the given union set.
2047 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2049 return isl_union_map_params(uset);
2052 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2054 struct isl_un_op_control control = {
2055 .fn_map = &isl_map_domain,
2057 return un_op(umap, &control);
2060 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2062 struct isl_un_op_control control = {
2063 .fn_map = &isl_map_range,
2065 return un_op(umap, &control);
2068 __isl_give isl_union_map *isl_union_map_domain_map(
2069 __isl_take isl_union_map *umap)
2071 struct isl_un_op_control control = {
2072 .fn_map = &isl_map_domain_map,
2074 return un_op(umap, &control);
2077 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2078 * add the result to "res".
2080 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2082 isl_union_pw_multi_aff **res = user;
2083 isl_multi_aff *ma;
2084 isl_pw_multi_aff *pma;
2086 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2087 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2088 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2090 return *res ? isl_stat_ok : isl_stat_error;
2094 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2095 * to its domain.
2097 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2098 __isl_take isl_union_map *umap)
2100 isl_union_pw_multi_aff *res;
2102 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2103 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2104 res = isl_union_pw_multi_aff_free(res);
2106 isl_union_map_free(umap);
2107 return res;
2110 __isl_give isl_union_map *isl_union_map_range_map(
2111 __isl_take isl_union_map *umap)
2113 struct isl_un_op_control control = {
2114 .fn_map = &isl_map_range_map,
2116 return un_op(umap, &control);
2119 /* Given a collection of wrapped maps of the form A[B -> C],
2120 * return the collection of maps A[B -> C] -> B.
2122 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2123 __isl_take isl_union_set *uset)
2125 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2126 struct isl_un_op_control control = {
2127 .filter = &un_op_filter_drop_user,
2128 .filter_user = &data,
2129 .fn_map = &isl_set_wrapped_domain_map,
2131 return un_op(uset, &control);
2134 /* Does "map" relate elements from the same space?
2136 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2138 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2139 map->dim, isl_dim_out);
2142 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2144 struct isl_un_op_control control = {
2145 .filter = &equal_tuples,
2146 .fn_map = &isl_map_deltas,
2148 return un_op(umap, &control);
2151 __isl_give isl_union_map *isl_union_map_deltas_map(
2152 __isl_take isl_union_map *umap)
2154 struct isl_un_op_control control = {
2155 .filter = &equal_tuples,
2156 .fn_map = &isl_map_deltas_map,
2158 return un_op(umap, &control);
2161 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2163 struct isl_un_op_control control = {
2164 .fn_map = &isl_set_identity,
2166 return un_op(uset, &control);
2169 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2171 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2173 isl_union_pw_multi_aff **res = user;
2174 isl_space *space;
2175 isl_pw_multi_aff *pma;
2177 space = isl_space_map_from_set(isl_set_get_space(set));
2178 pma = isl_pw_multi_aff_identity(space);
2179 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2180 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2182 return *res ? isl_stat_ok : isl_stat_error;
2185 /* Return an identity function on "uset" in the form
2186 * of an isl_union_pw_multi_aff.
2188 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2189 __isl_take isl_union_set *uset)
2191 isl_union_pw_multi_aff *res;
2193 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2194 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2195 res = isl_union_pw_multi_aff_free(res);
2197 isl_union_set_free(uset);
2198 return res;
2201 /* For each map in "umap" of the form [A -> B] -> C,
2202 * construct the map A -> C and collect the results.
2204 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2205 __isl_take isl_union_map *umap)
2207 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2208 struct isl_un_op_control control = {
2209 .filter = &un_op_filter_drop_user,
2210 .filter_user = &data,
2211 .fn_map = &isl_map_domain_factor_domain,
2213 return un_op(umap, &control);
2216 /* For each map in "umap" of the form [A -> B] -> C,
2217 * construct the map B -> C and collect the results.
2219 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2220 __isl_take isl_union_map *umap)
2222 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2223 struct isl_un_op_control control = {
2224 .filter = &un_op_filter_drop_user,
2225 .filter_user = &data,
2226 .fn_map = &isl_map_domain_factor_range,
2228 return un_op(umap, &control);
2231 /* For each map in "umap" of the form A -> [B -> C],
2232 * construct the map A -> B and collect the results.
2234 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2235 __isl_take isl_union_map *umap)
2237 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2238 struct isl_un_op_control control = {
2239 .filter = &un_op_filter_drop_user,
2240 .filter_user = &data,
2241 .fn_map = &isl_map_range_factor_domain,
2243 return un_op(umap, &control);
2246 /* For each map in "umap" of the form A -> [B -> C],
2247 * construct the map A -> C and collect the results.
2249 __isl_give isl_union_map *isl_union_map_range_factor_range(
2250 __isl_take isl_union_map *umap)
2252 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2253 struct isl_un_op_control control = {
2254 .filter = &un_op_filter_drop_user,
2255 .filter_user = &data,
2256 .fn_map = &isl_map_range_factor_range,
2258 return un_op(umap, &control);
2261 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2262 * construct the map A -> C and collect the results.
2264 __isl_give isl_union_map *isl_union_map_factor_domain(
2265 __isl_take isl_union_map *umap)
2267 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2268 struct isl_un_op_control control = {
2269 .filter = &un_op_filter_drop_user,
2270 .filter_user = &data,
2271 .fn_map = &isl_map_factor_domain,
2273 return un_op(umap, &control);
2276 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2277 * construct the map B -> D and collect the results.
2279 __isl_give isl_union_map *isl_union_map_factor_range(
2280 __isl_take isl_union_map *umap)
2282 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2283 struct isl_un_op_control control = {
2284 .filter = &un_op_filter_drop_user,
2285 .filter_user = &data,
2286 .fn_map = &isl_map_factor_range,
2288 return un_op(umap, &control);
2291 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2293 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2294 struct isl_un_op_control control = {
2295 .filter = &un_op_filter_drop_user,
2296 .filter_user = &data,
2297 .fn_map = &isl_set_unwrap,
2299 return un_op(uset, &control);
2302 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2304 struct isl_un_op_control control = {
2305 .fn_map = &isl_map_wrap,
2307 return un_op(umap, &control);
2310 struct isl_union_map_is_subset_data {
2311 isl_union_map *umap2;
2312 isl_bool is_subset;
2315 static isl_stat is_subset_entry(void **entry, void *user)
2317 struct isl_union_map_is_subset_data *data = user;
2318 uint32_t hash;
2319 struct isl_hash_table_entry *entry2;
2320 isl_map *map = *entry;
2322 hash = isl_space_get_hash(map->dim);
2323 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2324 hash, &has_space, map->dim, 0);
2325 if (!entry2) {
2326 int empty = isl_map_is_empty(map);
2327 if (empty < 0)
2328 return isl_stat_error;
2329 if (empty)
2330 return isl_stat_ok;
2331 data->is_subset = isl_bool_false;
2332 return isl_stat_error;
2335 data->is_subset = isl_map_is_subset(map, entry2->data);
2336 if (data->is_subset < 0 || !data->is_subset)
2337 return isl_stat_error;
2339 return isl_stat_ok;
2342 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2343 __isl_keep isl_union_map *umap2)
2345 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2347 umap1 = isl_union_map_copy(umap1);
2348 umap2 = isl_union_map_copy(umap2);
2349 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2350 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2352 if (!umap1 || !umap2)
2353 goto error;
2355 data.umap2 = umap2;
2356 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2357 &is_subset_entry, &data) < 0 &&
2358 data.is_subset)
2359 goto error;
2361 isl_union_map_free(umap1);
2362 isl_union_map_free(umap2);
2364 return data.is_subset;
2365 error:
2366 isl_union_map_free(umap1);
2367 isl_union_map_free(umap2);
2368 return isl_bool_error;
2371 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2372 __isl_keep isl_union_set *uset2)
2374 return isl_union_map_is_subset(uset1, uset2);
2377 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2378 __isl_keep isl_union_map *umap2)
2380 isl_bool is_subset;
2382 if (!umap1 || !umap2)
2383 return isl_bool_error;
2384 is_subset = isl_union_map_is_subset(umap1, umap2);
2385 if (is_subset != isl_bool_true)
2386 return is_subset;
2387 is_subset = isl_union_map_is_subset(umap2, umap1);
2388 return is_subset;
2391 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2392 __isl_keep isl_union_set *uset2)
2394 return isl_union_map_is_equal(uset1, uset2);
2397 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2398 __isl_keep isl_union_map *umap2)
2400 isl_bool is_subset;
2402 if (!umap1 || !umap2)
2403 return isl_bool_error;
2404 is_subset = isl_union_map_is_subset(umap1, umap2);
2405 if (is_subset != isl_bool_true)
2406 return is_subset;
2407 is_subset = isl_union_map_is_subset(umap2, umap1);
2408 return isl_bool_not(is_subset);
2411 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2412 __isl_keep isl_union_set *uset2)
2414 return isl_union_map_is_strict_subset(uset1, uset2);
2417 /* Internal data structure for isl_union_map_is_disjoint.
2418 * umap2 is the union map with which we are comparing.
2419 * is_disjoint is initialized to 1 and is set to 0 as soon
2420 * as the union maps turn out not to be disjoint.
2422 struct isl_union_map_is_disjoint_data {
2423 isl_union_map *umap2;
2424 isl_bool is_disjoint;
2427 /* Check if "map" is disjoint from data->umap2 and abort
2428 * the search if it is not.
2430 static isl_stat is_disjoint_entry(void **entry, void *user)
2432 struct isl_union_map_is_disjoint_data *data = user;
2433 uint32_t hash;
2434 struct isl_hash_table_entry *entry2;
2435 isl_map *map = *entry;
2437 hash = isl_space_get_hash(map->dim);
2438 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2439 hash, &has_space, map->dim, 0);
2440 if (!entry2)
2441 return isl_stat_ok;
2443 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2444 if (data->is_disjoint < 0 || !data->is_disjoint)
2445 return isl_stat_error;
2447 return isl_stat_ok;
2450 /* Are "umap1" and "umap2" disjoint?
2452 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2453 __isl_keep isl_union_map *umap2)
2455 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2457 umap1 = isl_union_map_copy(umap1);
2458 umap2 = isl_union_map_copy(umap2);
2459 umap1 = isl_union_map_align_params(umap1,
2460 isl_union_map_get_space(umap2));
2461 umap2 = isl_union_map_align_params(umap2,
2462 isl_union_map_get_space(umap1));
2464 if (!umap1 || !umap2)
2465 goto error;
2467 data.umap2 = umap2;
2468 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2469 &is_disjoint_entry, &data) < 0 &&
2470 data.is_disjoint)
2471 goto error;
2473 isl_union_map_free(umap1);
2474 isl_union_map_free(umap2);
2476 return data.is_disjoint;
2477 error:
2478 isl_union_map_free(umap1);
2479 isl_union_map_free(umap2);
2480 return isl_bool_error;
2483 /* Are "uset1" and "uset2" disjoint?
2485 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2486 __isl_keep isl_union_set *uset2)
2488 return isl_union_map_is_disjoint(uset1, uset2);
2491 static isl_stat sample_entry(void **entry, void *user)
2493 isl_basic_map **sample = (isl_basic_map **)user;
2494 isl_map *map = *entry;
2496 *sample = isl_map_sample(isl_map_copy(map));
2497 if (!*sample)
2498 return isl_stat_error;
2499 if (!isl_basic_map_plain_is_empty(*sample))
2500 return isl_stat_error;
2501 return isl_stat_ok;
2504 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2506 isl_basic_map *sample = NULL;
2508 if (!umap)
2509 return NULL;
2511 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2512 &sample_entry, &sample) < 0 &&
2513 !sample)
2514 goto error;
2516 if (!sample)
2517 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2519 isl_union_map_free(umap);
2521 return sample;
2522 error:
2523 isl_union_map_free(umap);
2524 return NULL;
2527 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2529 return bset_from_bmap(isl_union_map_sample(uset));
2532 /* Return an element in "uset" in the form of an isl_point.
2533 * Return a void isl_point if "uset" is empty.
2535 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2537 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2540 struct isl_forall_data {
2541 isl_bool res;
2542 isl_bool (*fn)(__isl_keep isl_map *map);
2545 static isl_stat forall_entry(void **entry, void *user)
2547 struct isl_forall_data *data = user;
2548 isl_map *map = *entry;
2550 data->res = data->fn(map);
2551 if (data->res < 0)
2552 return isl_stat_error;
2554 if (!data->res)
2555 return isl_stat_error;
2557 return isl_stat_ok;
2560 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2561 isl_bool (*fn)(__isl_keep isl_map *map))
2563 struct isl_forall_data data = { isl_bool_true, fn };
2565 if (!umap)
2566 return isl_bool_error;
2568 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2569 &forall_entry, &data) < 0 && data.res)
2570 return isl_bool_error;
2572 return data.res;
2575 struct isl_forall_user_data {
2576 isl_bool res;
2577 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2578 void *user;
2581 static isl_stat forall_user_entry(void **entry, void *user)
2583 struct isl_forall_user_data *data = user;
2584 isl_map *map = *entry;
2586 data->res = data->fn(map, data->user);
2587 if (data->res < 0)
2588 return isl_stat_error;
2590 if (!data->res)
2591 return isl_stat_error;
2593 return isl_stat_ok;
2596 /* Check if fn(map, user) returns true for all maps "map" in umap.
2598 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2599 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2601 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2603 if (!umap)
2604 return isl_bool_error;
2606 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2607 &forall_user_entry, &data) < 0 && data.res)
2608 return isl_bool_error;
2610 return data.res;
2613 /* Is "umap" obviously empty?
2615 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2617 if (!umap)
2618 return isl_bool_error;
2619 return isl_union_map_n_map(umap) == 0;
2622 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2624 return union_map_forall(umap, &isl_map_is_empty);
2627 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2629 return isl_union_map_is_empty(uset);
2632 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2634 isl_bool is_subset;
2635 isl_space *dim;
2636 isl_map *id;
2638 if (!map)
2639 return isl_bool_error;
2641 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2642 map->dim, isl_dim_out))
2643 return isl_bool_false;
2645 dim = isl_map_get_space(map);
2646 id = isl_map_identity(dim);
2648 is_subset = isl_map_is_subset(map, id);
2650 isl_map_free(id);
2652 return is_subset;
2655 /* Given an isl_union_map that consists of a single map, check
2656 * if it is single-valued.
2658 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2660 isl_map *map;
2661 isl_bool sv;
2663 umap = isl_union_map_copy(umap);
2664 map = isl_map_from_union_map(umap);
2665 sv = isl_map_is_single_valued(map);
2666 isl_map_free(map);
2668 return sv;
2671 /* Internal data structure for single_valued_on_domain.
2673 * "umap" is the union map to be tested.
2674 * "sv" is set to 1 as long as "umap" may still be single-valued.
2676 struct isl_union_map_is_sv_data {
2677 isl_union_map *umap;
2678 isl_bool sv;
2681 /* Check if the data->umap is single-valued on "set".
2683 * If data->umap consists of a single map on "set", then test it
2684 * as an isl_map.
2686 * Otherwise, compute
2688 * M \circ M^-1
2690 * check if the result is a subset of the identity mapping and
2691 * store the result in data->sv.
2693 * Terminate as soon as data->umap has been determined not to
2694 * be single-valued.
2696 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2698 struct isl_union_map_is_sv_data *data = user;
2699 isl_union_map *umap, *test;
2701 umap = isl_union_map_copy(data->umap);
2702 umap = isl_union_map_intersect_domain(umap,
2703 isl_union_set_from_set(set));
2705 if (isl_union_map_n_map(umap) == 1) {
2706 data->sv = single_map_is_single_valued(umap);
2707 isl_union_map_free(umap);
2708 } else {
2709 test = isl_union_map_reverse(isl_union_map_copy(umap));
2710 test = isl_union_map_apply_range(test, umap);
2712 data->sv = union_map_forall(test, &is_subset_of_identity);
2714 isl_union_map_free(test);
2717 if (data->sv < 0 || !data->sv)
2718 return isl_stat_error;
2719 return isl_stat_ok;
2722 /* Check if the given map is single-valued.
2724 * If the union map consists of a single map, then test it as an isl_map.
2725 * Otherwise, check if the union map is single-valued on each of its
2726 * domain spaces.
2728 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2730 isl_union_map *universe;
2731 isl_union_set *domain;
2732 struct isl_union_map_is_sv_data data;
2734 if (isl_union_map_n_map(umap) == 1)
2735 return single_map_is_single_valued(umap);
2737 universe = isl_union_map_universe(isl_union_map_copy(umap));
2738 domain = isl_union_map_domain(universe);
2740 data.sv = isl_bool_true;
2741 data.umap = umap;
2742 if (isl_union_set_foreach_set(domain,
2743 &single_valued_on_domain, &data) < 0 && data.sv)
2744 data.sv = isl_bool_error;
2745 isl_union_set_free(domain);
2747 return data.sv;
2750 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2752 isl_bool in;
2754 umap = isl_union_map_copy(umap);
2755 umap = isl_union_map_reverse(umap);
2756 in = isl_union_map_is_single_valued(umap);
2757 isl_union_map_free(umap);
2759 return in;
2762 /* Is "map" obviously not an identity relation because
2763 * it maps elements from one space to another space?
2764 * Update *non_identity accordingly.
2766 * In particular, if the domain and range spaces are the same,
2767 * then the map is not considered to obviously not be an identity relation.
2768 * Otherwise, the map is considered to obviously not be an identity relation
2769 * if it is is non-empty.
2771 * If "map" is determined to obviously not be an identity relation,
2772 * then the search is aborted.
2774 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2776 isl_bool *non_identity = user;
2777 isl_bool equal;
2778 isl_space *space;
2780 space = isl_map_get_space(map);
2781 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2782 if (equal >= 0 && !equal)
2783 *non_identity = isl_bool_not(isl_map_is_empty(map));
2784 else
2785 *non_identity = isl_bool_not(equal);
2786 isl_space_free(space);
2787 isl_map_free(map);
2789 if (*non_identity < 0 || *non_identity)
2790 return isl_stat_error;
2792 return isl_stat_ok;
2795 /* Is "umap" obviously not an identity relation because
2796 * it maps elements from one space to another space?
2798 * As soon as a map has been found that maps elements to a different space,
2799 * non_identity is changed and the search is aborted.
2801 static isl_bool isl_union_map_plain_is_not_identity(
2802 __isl_keep isl_union_map *umap)
2804 isl_bool non_identity;
2806 non_identity = isl_bool_false;
2807 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2808 &non_identity) < 0 &&
2809 non_identity == isl_bool_false)
2810 return isl_bool_error;
2812 return non_identity;
2815 /* Does "map" only map elements to themselves?
2816 * Update *identity accordingly.
2818 * If "map" is determined not to be an identity relation,
2819 * then the search is aborted.
2821 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2823 isl_bool *identity = user;
2825 *identity = isl_map_is_identity(map);
2826 isl_map_free(map);
2828 if (*identity < 0 || !*identity)
2829 return isl_stat_error;
2831 return isl_stat_ok;
2834 /* Does "umap" only map elements to themselves?
2836 * First check if there are any maps that map elements to different spaces.
2837 * If not, then check that all the maps (between identical spaces)
2838 * are identity relations.
2840 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2842 isl_bool non_identity;
2843 isl_bool identity;
2845 non_identity = isl_union_map_plain_is_not_identity(umap);
2846 if (non_identity < 0 || non_identity)
2847 return isl_bool_not(non_identity);
2849 identity = isl_bool_true;
2850 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2851 identity == isl_bool_true)
2852 return isl_bool_error;
2854 return identity;
2857 /* Represents a map that has a fixed value (v) for one of its
2858 * range dimensions.
2859 * The map in this structure is not reference counted, so it
2860 * is only valid while the isl_union_map from which it was
2861 * obtained is still alive.
2863 struct isl_fixed_map {
2864 isl_int v;
2865 isl_map *map;
2868 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2869 int n)
2871 int i;
2872 struct isl_fixed_map *v;
2874 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2875 if (!v)
2876 return NULL;
2877 for (i = 0; i < n; ++i)
2878 isl_int_init(v[i].v);
2879 return v;
2882 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2884 int i;
2886 if (!v)
2887 return;
2888 for (i = 0; i < n; ++i)
2889 isl_int_clear(v[i].v);
2890 free(v);
2893 /* Compare the "v" field of two isl_fixed_map structs.
2895 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2897 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2898 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2900 return isl_int_cmp(e1->v, e2->v);
2903 /* Internal data structure used while checking whether all maps
2904 * in a union_map have a fixed value for a given output dimension.
2905 * v is the list of maps, with the fixed value for the dimension
2906 * n is the number of maps considered so far
2907 * pos is the output dimension under investigation
2909 struct isl_fixed_dim_data {
2910 struct isl_fixed_map *v;
2911 int n;
2912 int pos;
2915 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2917 struct isl_fixed_dim_data *data = user;
2919 data->v[data->n].map = map;
2920 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2921 &data->v[data->n++].v);
2924 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2925 int first, int n_range);
2927 /* Given a list of the maps, with their fixed values at output dimension "pos",
2928 * check whether the ranges of the maps form an obvious partition.
2930 * We first sort the maps according to their fixed values.
2931 * If all maps have a different value, then we know the ranges form
2932 * a partition.
2933 * Otherwise, we collect the maps with the same fixed value and
2934 * check whether each such collection is obviously injective
2935 * based on later dimensions.
2937 static int separates(struct isl_fixed_map *v, int n,
2938 __isl_take isl_space *dim, int pos, int n_range)
2940 int i;
2942 if (!v)
2943 goto error;
2945 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2947 for (i = 0; i + 1 < n; ++i) {
2948 int j, k;
2949 isl_union_map *part;
2950 int injective;
2952 for (j = i + 1; j < n; ++j)
2953 if (isl_int_ne(v[i].v, v[j].v))
2954 break;
2956 if (j == i + 1)
2957 continue;
2959 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2960 for (k = i; k < j; ++k)
2961 part = isl_union_map_add_map(part,
2962 isl_map_copy(v[k].map));
2964 injective = plain_injective_on_range(part, pos + 1, n_range);
2965 if (injective < 0)
2966 goto error;
2967 if (!injective)
2968 break;
2970 i = j - 1;
2973 isl_space_free(dim);
2974 free_isl_fixed_map_array(v, n);
2975 return i + 1 >= n;
2976 error:
2977 isl_space_free(dim);
2978 free_isl_fixed_map_array(v, n);
2979 return -1;
2982 /* Check whether the maps in umap have obviously distinct ranges.
2983 * In particular, check for an output dimension in the range
2984 * [first,n_range) for which all maps have a fixed value
2985 * and then check if these values, possibly along with fixed values
2986 * at later dimensions, entail distinct ranges.
2988 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2989 int first, int n_range)
2991 isl_ctx *ctx;
2992 int n;
2993 struct isl_fixed_dim_data data = { NULL };
2995 ctx = isl_union_map_get_ctx(umap);
2997 n = isl_union_map_n_map(umap);
2998 if (!umap)
2999 goto error;
3001 if (n <= 1) {
3002 isl_union_map_free(umap);
3003 return isl_bool_true;
3006 if (first >= n_range) {
3007 isl_union_map_free(umap);
3008 return isl_bool_false;
3011 data.v = alloc_isl_fixed_map_array(ctx, n);
3012 if (!data.v)
3013 goto error;
3015 for (data.pos = first; data.pos < n_range; ++data.pos) {
3016 isl_bool fixed;
3017 int injective;
3018 isl_space *dim;
3020 data.n = 0;
3021 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3022 if (fixed < 0)
3023 goto error;
3024 if (!fixed)
3025 continue;
3026 dim = isl_union_map_get_space(umap);
3027 injective = separates(data.v, n, dim, data.pos, n_range);
3028 isl_union_map_free(umap);
3029 return injective;
3032 free_isl_fixed_map_array(data.v, n);
3033 isl_union_map_free(umap);
3035 return isl_bool_false;
3036 error:
3037 free_isl_fixed_map_array(data.v, n);
3038 isl_union_map_free(umap);
3039 return isl_bool_error;
3042 /* Check whether the maps in umap that map to subsets of "ran"
3043 * have obviously distinct ranges.
3045 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3046 void *user)
3048 isl_union_map *umap = user;
3050 umap = isl_union_map_copy(umap);
3051 umap = isl_union_map_intersect_range(umap,
3052 isl_union_set_from_set(isl_set_copy(ran)));
3053 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
3056 /* Check if the given union_map is obviously injective.
3058 * In particular, we first check if all individual maps are obviously
3059 * injective and then check if all the ranges of these maps are
3060 * obviously disjoint.
3062 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3064 isl_bool in;
3065 isl_union_map *univ;
3066 isl_union_set *ran;
3068 in = union_map_forall(umap, &isl_map_plain_is_injective);
3069 if (in < 0)
3070 return isl_bool_error;
3071 if (!in)
3072 return isl_bool_false;
3074 univ = isl_union_map_universe(isl_union_map_copy(umap));
3075 ran = isl_union_map_range(univ);
3077 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3079 isl_union_set_free(ran);
3081 return in;
3084 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3086 isl_bool sv;
3088 sv = isl_union_map_is_single_valued(umap);
3089 if (sv < 0 || !sv)
3090 return sv;
3092 return isl_union_map_is_injective(umap);
3095 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3097 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3098 struct isl_un_op_control control = {
3099 .filter = &un_op_filter_drop_user,
3100 .filter_user = &data,
3101 .fn_map = &isl_map_zip,
3103 return un_op(umap, &control);
3106 /* Given a union map, take the maps of the form A -> (B -> C) and
3107 * return the union of the corresponding maps (A -> B) -> C.
3109 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3111 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3112 struct isl_un_op_control control = {
3113 .filter = &un_op_filter_drop_user,
3114 .filter_user = &data,
3115 .fn_map = &isl_map_uncurry,
3117 return un_op(umap, &control);
3120 /* Given a union map, take the maps of the form (A -> B) -> C and
3121 * return the union of the corresponding maps A -> (B -> C).
3123 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3125 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3126 struct isl_un_op_control control = {
3127 .filter = &un_op_filter_drop_user,
3128 .filter_user = &data,
3129 .fn_map = &isl_map_curry,
3131 return un_op(umap, &control);
3134 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3135 * return the union of the corresponding maps A -> (B -> (C -> D)).
3137 __isl_give isl_union_map *isl_union_map_range_curry(
3138 __isl_take isl_union_map *umap)
3140 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3141 struct isl_un_op_control control = {
3142 .filter = &un_op_filter_drop_user,
3143 .filter_user = &data,
3144 .fn_map = &isl_map_range_curry,
3146 return un_op(umap, &control);
3149 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3151 struct isl_un_op_control control = {
3152 .fn_map = &isl_set_lift,
3154 return un_op(uset, &control);
3157 static isl_stat coefficients_entry(void **entry, void *user)
3159 isl_set *set = *entry;
3160 isl_union_set **res = user;
3162 set = isl_set_copy(set);
3163 set = isl_set_from_basic_set(isl_set_coefficients(set));
3164 *res = isl_union_set_add_set(*res, set);
3166 return isl_stat_ok;
3169 __isl_give isl_union_set *isl_union_set_coefficients(
3170 __isl_take isl_union_set *uset)
3172 isl_ctx *ctx;
3173 isl_space *dim;
3174 isl_union_set *res;
3176 if (!uset)
3177 return NULL;
3179 ctx = isl_union_set_get_ctx(uset);
3180 dim = isl_space_set_alloc(ctx, 0, 0);
3181 res = isl_union_map_alloc(dim, uset->table.n);
3182 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3183 &coefficients_entry, &res) < 0)
3184 goto error;
3186 isl_union_set_free(uset);
3187 return res;
3188 error:
3189 isl_union_set_free(uset);
3190 isl_union_set_free(res);
3191 return NULL;
3194 static isl_stat solutions_entry(void **entry, void *user)
3196 isl_set *set = *entry;
3197 isl_union_set **res = user;
3199 set = isl_set_copy(set);
3200 set = isl_set_from_basic_set(isl_set_solutions(set));
3201 if (!*res)
3202 *res = isl_union_set_from_set(set);
3203 else
3204 *res = isl_union_set_add_set(*res, set);
3206 if (!*res)
3207 return isl_stat_error;
3209 return isl_stat_ok;
3212 __isl_give isl_union_set *isl_union_set_solutions(
3213 __isl_take isl_union_set *uset)
3215 isl_union_set *res = NULL;
3217 if (!uset)
3218 return NULL;
3220 if (uset->table.n == 0) {
3221 res = isl_union_set_empty(isl_union_set_get_space(uset));
3222 isl_union_set_free(uset);
3223 return res;
3226 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3227 &solutions_entry, &res) < 0)
3228 goto error;
3230 isl_union_set_free(uset);
3231 return res;
3232 error:
3233 isl_union_set_free(uset);
3234 isl_union_set_free(res);
3235 return NULL;
3238 /* Is the domain space of "map" equal to "space"?
3240 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3242 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3243 space, isl_dim_out);
3246 /* Is the range space of "map" equal to "space"?
3248 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3250 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3251 space, isl_dim_out);
3254 /* Is the set space of "map" equal to "space"?
3256 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3258 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3259 space, isl_dim_out);
3262 /* Internal data structure for preimage_pw_multi_aff.
3264 * "pma" is the function under which the preimage should be taken.
3265 * "space" is the space of "pma".
3266 * "res" collects the results.
3267 * "fn" computes the preimage for a given map.
3268 * "match" returns true if "fn" can be called.
3270 struct isl_union_map_preimage_data {
3271 isl_space *space;
3272 isl_pw_multi_aff *pma;
3273 isl_union_map *res;
3274 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3275 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3276 __isl_take isl_pw_multi_aff *pma);
3279 /* Call data->fn to compute the preimage of the domain or range of *entry
3280 * under the function represented by data->pma, provided the domain/range
3281 * space of *entry matches the target space of data->pma
3282 * (as given by data->match), and add the result to data->res.
3284 static isl_stat preimage_entry(void **entry, void *user)
3286 int m;
3287 isl_map *map = *entry;
3288 struct isl_union_map_preimage_data *data = user;
3289 isl_bool empty;
3291 m = data->match(map, data->space);
3292 if (m < 0)
3293 return isl_stat_error;
3294 if (!m)
3295 return isl_stat_ok;
3297 map = isl_map_copy(map);
3298 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3300 empty = isl_map_is_empty(map);
3301 if (empty < 0 || empty) {
3302 isl_map_free(map);
3303 return empty < 0 ? isl_stat_error : isl_stat_ok;
3306 data->res = isl_union_map_add_map(data->res, map);
3308 return isl_stat_ok;
3311 /* Compute the preimage of the domain or range of "umap" under the function
3312 * represented by "pma".
3313 * In other words, plug in "pma" in the domain or range of "umap".
3314 * The function "fn" performs the actual preimage computation on a map,
3315 * while "match" determines to which maps the function should be applied.
3317 static __isl_give isl_union_map *preimage_pw_multi_aff(
3318 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3319 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3320 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3321 __isl_take isl_pw_multi_aff *pma))
3323 isl_ctx *ctx;
3324 isl_space *space;
3325 struct isl_union_map_preimage_data data;
3327 umap = isl_union_map_align_params(umap,
3328 isl_pw_multi_aff_get_space(pma));
3329 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3331 if (!umap || !pma)
3332 goto error;
3334 ctx = isl_union_map_get_ctx(umap);
3335 space = isl_union_map_get_space(umap);
3336 data.space = isl_pw_multi_aff_get_space(pma);
3337 data.pma = pma;
3338 data.res = isl_union_map_alloc(space, umap->table.n);
3339 data.match = match;
3340 data.fn = fn;
3341 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3342 &data) < 0)
3343 data.res = isl_union_map_free(data.res);
3345 isl_space_free(data.space);
3346 isl_union_map_free(umap);
3347 isl_pw_multi_aff_free(pma);
3348 return data.res;
3349 error:
3350 isl_union_map_free(umap);
3351 isl_pw_multi_aff_free(pma);
3352 return NULL;
3355 /* Compute the preimage of the domain of "umap" under the function
3356 * represented by "pma".
3357 * In other words, plug in "pma" in the domain of "umap".
3358 * The result contains maps that live in the same spaces as the maps of "umap"
3359 * with domain space equal to the target space of "pma",
3360 * except that the domain has been replaced by the domain space of "pma".
3362 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3363 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3365 return preimage_pw_multi_aff(umap, pma, &domain_match,
3366 &isl_map_preimage_domain_pw_multi_aff);
3369 /* Compute the preimage of the range of "umap" under the function
3370 * represented by "pma".
3371 * In other words, plug in "pma" in the range of "umap".
3372 * The result contains maps that live in the same spaces as the maps of "umap"
3373 * with range space equal to the target space of "pma",
3374 * except that the range has been replaced by the domain space of "pma".
3376 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3377 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3379 return preimage_pw_multi_aff(umap, pma, &range_match,
3380 &isl_map_preimage_range_pw_multi_aff);
3383 /* Compute the preimage of "uset" under the function represented by "pma".
3384 * In other words, plug in "pma" in "uset".
3385 * The result contains sets that live in the same spaces as the sets of "uset"
3386 * with space equal to the target space of "pma",
3387 * except that the space has been replaced by the domain space of "pma".
3389 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3390 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3392 return preimage_pw_multi_aff(uset, pma, &set_match,
3393 &isl_set_preimage_pw_multi_aff);
3396 /* Compute the preimage of the domain of "umap" under the function
3397 * represented by "ma".
3398 * In other words, plug in "ma" in the domain of "umap".
3399 * The result contains maps that live in the same spaces as the maps of "umap"
3400 * with domain space equal to the target space of "ma",
3401 * except that the domain has been replaced by the domain space of "ma".
3403 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3404 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3406 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3407 isl_pw_multi_aff_from_multi_aff(ma));
3410 /* Compute the preimage of the range of "umap" under the function
3411 * represented by "ma".
3412 * In other words, plug in "ma" in the range of "umap".
3413 * The result contains maps that live in the same spaces as the maps of "umap"
3414 * with range space equal to the target space of "ma",
3415 * except that the range has been replaced by the domain space of "ma".
3417 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3418 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3420 return isl_union_map_preimage_range_pw_multi_aff(umap,
3421 isl_pw_multi_aff_from_multi_aff(ma));
3424 /* Compute the preimage of "uset" under the function represented by "ma".
3425 * In other words, plug in "ma" in "uset".
3426 * The result contains sets that live in the same spaces as the sets of "uset"
3427 * with space equal to the target space of "ma",
3428 * except that the space has been replaced by the domain space of "ma".
3430 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3431 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3433 return isl_union_set_preimage_pw_multi_aff(uset,
3434 isl_pw_multi_aff_from_multi_aff(ma));
3437 /* Internal data structure for preimage_multi_pw_aff.
3439 * "mpa" is the function under which the preimage should be taken.
3440 * "space" is the space of "mpa".
3441 * "res" collects the results.
3442 * "fn" computes the preimage for a given map.
3443 * "match" returns true if "fn" can be called.
3445 struct isl_union_map_preimage_mpa_data {
3446 isl_space *space;
3447 isl_multi_pw_aff *mpa;
3448 isl_union_map *res;
3449 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3450 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3451 __isl_take isl_multi_pw_aff *mpa);
3454 /* Call data->fn to compute the preimage of the domain or range of *entry
3455 * under the function represented by data->mpa, provided the domain/range
3456 * space of *entry matches the target space of data->mpa
3457 * (as given by data->match), and add the result to data->res.
3459 static isl_stat preimage_mpa_entry(void **entry, void *user)
3461 int m;
3462 isl_map *map = *entry;
3463 struct isl_union_map_preimage_mpa_data *data = user;
3464 isl_bool empty;
3466 m = data->match(map, data->space);
3467 if (m < 0)
3468 return isl_stat_error;
3469 if (!m)
3470 return isl_stat_ok;
3472 map = isl_map_copy(map);
3473 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3475 empty = isl_map_is_empty(map);
3476 if (empty < 0 || empty) {
3477 isl_map_free(map);
3478 return empty < 0 ? isl_stat_error : isl_stat_ok;
3481 data->res = isl_union_map_add_map(data->res, map);
3483 return isl_stat_ok;
3486 /* Compute the preimage of the domain or range of "umap" under the function
3487 * represented by "mpa".
3488 * In other words, plug in "mpa" in the domain or range of "umap".
3489 * The function "fn" performs the actual preimage computation on a map,
3490 * while "match" determines to which maps the function should be applied.
3492 static __isl_give isl_union_map *preimage_multi_pw_aff(
3493 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3494 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3495 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3496 __isl_take isl_multi_pw_aff *mpa))
3498 isl_ctx *ctx;
3499 isl_space *space;
3500 struct isl_union_map_preimage_mpa_data data;
3502 umap = isl_union_map_align_params(umap,
3503 isl_multi_pw_aff_get_space(mpa));
3504 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3506 if (!umap || !mpa)
3507 goto error;
3509 ctx = isl_union_map_get_ctx(umap);
3510 space = isl_union_map_get_space(umap);
3511 data.space = isl_multi_pw_aff_get_space(mpa);
3512 data.mpa = mpa;
3513 data.res = isl_union_map_alloc(space, umap->table.n);
3514 data.match = match;
3515 data.fn = fn;
3516 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3517 &data) < 0)
3518 data.res = isl_union_map_free(data.res);
3520 isl_space_free(data.space);
3521 isl_union_map_free(umap);
3522 isl_multi_pw_aff_free(mpa);
3523 return data.res;
3524 error:
3525 isl_union_map_free(umap);
3526 isl_multi_pw_aff_free(mpa);
3527 return NULL;
3530 /* Compute the preimage of the domain of "umap" under the function
3531 * represented by "mpa".
3532 * In other words, plug in "mpa" in the domain of "umap".
3533 * The result contains maps that live in the same spaces as the maps of "umap"
3534 * with domain space equal to the target space of "mpa",
3535 * except that the domain has been replaced by the domain space of "mpa".
3537 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3538 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3540 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3541 &isl_map_preimage_domain_multi_pw_aff);
3544 /* Internal data structure for preimage_upma.
3546 * "umap" is the map of which the preimage should be computed.
3547 * "res" collects the results.
3548 * "fn" computes the preimage for a given piecewise multi-affine function.
3550 struct isl_union_map_preimage_upma_data {
3551 isl_union_map *umap;
3552 isl_union_map *res;
3553 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3554 __isl_take isl_pw_multi_aff *pma);
3557 /* Call data->fn to compute the preimage of the domain or range of data->umap
3558 * under the function represented by pma and add the result to data->res.
3560 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3562 struct isl_union_map_preimage_upma_data *data = user;
3563 isl_union_map *umap;
3565 umap = isl_union_map_copy(data->umap);
3566 umap = data->fn(umap, pma);
3567 data->res = isl_union_map_union(data->res, umap);
3569 return data->res ? isl_stat_ok : isl_stat_error;
3572 /* Compute the preimage of the domain or range of "umap" under the function
3573 * represented by "upma".
3574 * In other words, plug in "upma" in the domain or range of "umap".
3575 * The function "fn" performs the actual preimage computation
3576 * on a piecewise multi-affine function.
3578 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3579 __isl_take isl_union_map *umap,
3580 __isl_take isl_union_pw_multi_aff *upma,
3581 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3582 __isl_take isl_pw_multi_aff *pma))
3584 struct isl_union_map_preimage_upma_data data;
3586 data.umap = umap;
3587 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3588 data.fn = fn;
3589 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3590 &preimage_upma, &data) < 0)
3591 data.res = isl_union_map_free(data.res);
3593 isl_union_map_free(umap);
3594 isl_union_pw_multi_aff_free(upma);
3596 return data.res;
3599 /* Compute the preimage of the domain of "umap" under the function
3600 * represented by "upma".
3601 * In other words, plug in "upma" in the domain of "umap".
3602 * The result contains maps that live in the same spaces as the maps of "umap"
3603 * with domain space equal to one of the target spaces of "upma",
3604 * except that the domain has been replaced by one of the domain spaces that
3605 * correspond to that target space of "upma".
3607 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3608 __isl_take isl_union_map *umap,
3609 __isl_take isl_union_pw_multi_aff *upma)
3611 return preimage_union_pw_multi_aff(umap, upma,
3612 &isl_union_map_preimage_domain_pw_multi_aff);
3615 /* Compute the preimage of the range of "umap" under the function
3616 * represented by "upma".
3617 * In other words, plug in "upma" in the range of "umap".
3618 * The result contains maps that live in the same spaces as the maps of "umap"
3619 * with range space equal to one of the target spaces of "upma",
3620 * except that the range has been replaced by one of the domain spaces that
3621 * correspond to that target space of "upma".
3623 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3624 __isl_take isl_union_map *umap,
3625 __isl_take isl_union_pw_multi_aff *upma)
3627 return preimage_union_pw_multi_aff(umap, upma,
3628 &isl_union_map_preimage_range_pw_multi_aff);
3631 /* Compute the preimage of "uset" under the function represented by "upma".
3632 * In other words, plug in "upma" in the range of "uset".
3633 * The result contains sets that live in the same spaces as the sets of "uset"
3634 * with space equal to one of the target spaces of "upma",
3635 * except that the space has been replaced by one of the domain spaces that
3636 * correspond to that target space of "upma".
3638 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3639 __isl_take isl_union_set *uset,
3640 __isl_take isl_union_pw_multi_aff *upma)
3642 return preimage_union_pw_multi_aff(uset, upma,
3643 &isl_union_set_preimage_pw_multi_aff);
3646 /* Reset the user pointer on all identifiers of parameters and tuples
3647 * of the spaces of "umap".
3649 __isl_give isl_union_map *isl_union_map_reset_user(
3650 __isl_take isl_union_map *umap)
3652 umap = isl_union_map_cow(umap);
3653 if (!umap)
3654 return NULL;
3655 umap->dim = isl_space_reset_user(umap->dim);
3656 if (!umap->dim)
3657 return isl_union_map_free(umap);
3658 return total(umap, &isl_map_reset_user);
3661 /* Reset the user pointer on all identifiers of parameters and tuples
3662 * of the spaces of "uset".
3664 __isl_give isl_union_set *isl_union_set_reset_user(
3665 __isl_take isl_union_set *uset)
3667 return isl_union_map_reset_user(uset);
3670 /* Remove all existentially quantified variables and integer divisions
3671 * from "umap" using Fourier-Motzkin elimination.
3673 __isl_give isl_union_map *isl_union_map_remove_divs(
3674 __isl_take isl_union_map *umap)
3676 return total(umap, &isl_map_remove_divs);
3679 /* Remove all existentially quantified variables and integer divisions
3680 * from "uset" using Fourier-Motzkin elimination.
3682 __isl_give isl_union_set *isl_union_set_remove_divs(
3683 __isl_take isl_union_set *uset)
3685 return isl_union_map_remove_divs(uset);
3688 /* Internal data structure for isl_union_map_project_out.
3689 * "type", "first" and "n" are the arguments for the isl_map_project_out
3690 * call.
3691 * "res" collects the results.
3693 struct isl_union_map_project_out_data {
3694 enum isl_dim_type type;
3695 unsigned first;
3696 unsigned n;
3698 isl_union_map *res;
3701 /* Turn the data->n dimensions of type data->type, starting at data->first
3702 * into existentially quantified variables and add the result to data->res.
3704 static isl_stat project_out(__isl_take isl_map *map, void *user)
3706 struct isl_union_map_project_out_data *data = user;
3708 map = isl_map_project_out(map, data->type, data->first, data->n);
3709 data->res = isl_union_map_add_map(data->res, map);
3711 return isl_stat_ok;
3714 /* Turn the "n" dimensions of type "type", starting at "first"
3715 * into existentially quantified variables.
3716 * Since the space of an isl_union_map only contains parameters,
3717 * type is required to be equal to isl_dim_param.
3719 __isl_give isl_union_map *isl_union_map_project_out(
3720 __isl_take isl_union_map *umap,
3721 enum isl_dim_type type, unsigned first, unsigned n)
3723 isl_space *space;
3724 struct isl_union_map_project_out_data data = { type, first, n };
3726 if (!umap)
3727 return NULL;
3729 if (type != isl_dim_param)
3730 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3731 "can only project out parameters",
3732 return isl_union_map_free(umap));
3734 space = isl_union_map_get_space(umap);
3735 space = isl_space_drop_dims(space, type, first, n);
3736 data.res = isl_union_map_empty(space);
3737 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3738 data.res = isl_union_map_free(data.res);
3740 isl_union_map_free(umap);
3742 return data.res;
3745 /* Project out all parameters from "umap" by existentially quantifying
3746 * over them.
3748 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3749 __isl_take isl_union_map *umap)
3751 unsigned n;
3753 if (!umap)
3754 return NULL;
3755 n = isl_union_map_dim(umap, isl_dim_param);
3756 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3759 /* Turn the "n" dimensions of type "type", starting at "first"
3760 * into existentially quantified variables.
3761 * Since the space of an isl_union_set only contains parameters,
3762 * "type" is required to be equal to isl_dim_param.
3764 __isl_give isl_union_set *isl_union_set_project_out(
3765 __isl_take isl_union_set *uset,
3766 enum isl_dim_type type, unsigned first, unsigned n)
3768 return isl_union_map_project_out(uset, type, first, n);
3771 /* Project out all parameters from "uset" by existentially quantifying
3772 * over them.
3774 __isl_give isl_union_set *isl_union_set_project_out_all_params(
3775 __isl_take isl_union_set *uset)
3777 return uset_from_umap(
3778 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3781 /* Internal data structure for isl_union_map_involves_dims.
3782 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3784 struct isl_union_map_involves_dims_data {
3785 unsigned first;
3786 unsigned n;
3789 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3791 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3793 struct isl_union_map_involves_dims_data *data = user;
3794 isl_bool involves;
3796 involves = isl_map_involves_dims(map,
3797 isl_dim_param, data->first, data->n);
3798 return isl_bool_not(involves);
3801 /* Does "umap" involve any of the n parameters starting at first?
3802 * "type" is required to be set to isl_dim_param.
3804 * "umap" involves any of those parameters if any of its maps
3805 * involve the parameters. In other words, "umap" does not
3806 * involve any of the parameters if all its maps to not
3807 * involve the parameters.
3809 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3810 enum isl_dim_type type, unsigned first, unsigned n)
3812 struct isl_union_map_involves_dims_data data = { first, n };
3813 isl_bool excludes;
3815 if (type != isl_dim_param)
3816 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3817 "can only reference parameters", return isl_bool_error);
3819 excludes = union_map_forall_user(umap, &map_excludes, &data);
3821 return isl_bool_not(excludes);
3824 /* Internal data structure for isl_union_map_reset_range_space.
3825 * "range" is the space from which to set the range space.
3826 * "res" collects the results.
3828 struct isl_union_map_reset_range_space_data {
3829 isl_space *range;
3830 isl_union_map *res;
3833 /* Replace the range space of "map" by the range space of data->range and
3834 * add the result to data->res.
3836 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3838 struct isl_union_map_reset_range_space_data *data = user;
3839 isl_space *space;
3841 space = isl_map_get_space(map);
3842 space = isl_space_domain(space);
3843 space = isl_space_extend_domain_with_range(space,
3844 isl_space_copy(data->range));
3845 map = isl_map_reset_space(map, space);
3846 data->res = isl_union_map_add_map(data->res, map);
3848 return data->res ? isl_stat_ok : isl_stat_error;
3851 /* Replace the range space of all the maps in "umap" by
3852 * the range space of "space".
3854 * This assumes that all maps have the same output dimension.
3855 * This function should therefore not be made publicly available.
3857 * Since the spaces of the maps change, so do their hash value.
3858 * We therefore need to create a new isl_union_map.
3860 __isl_give isl_union_map *isl_union_map_reset_range_space(
3861 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3863 struct isl_union_map_reset_range_space_data data = { space };
3865 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3866 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3867 data.res = isl_union_map_free(data.res);
3869 isl_space_free(space);
3870 isl_union_map_free(umap);
3871 return data.res;
3874 /* Check that "umap" and "space" have the same number of parameters.
3876 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3877 __isl_keep isl_space *space)
3879 unsigned dim1, dim2;
3881 if (!umap || !space)
3882 return isl_stat_error;
3883 dim1 = isl_union_map_dim(umap, isl_dim_param);
3884 dim2 = isl_space_dim(space, isl_dim_param);
3885 if (dim1 == dim2)
3886 return isl_stat_ok;
3887 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3888 "number of parameters does not match", return isl_stat_error);
3891 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3892 * "space" is the target space.
3893 * "res" collects the results.
3895 struct isl_union_map_reset_params_data {
3896 isl_space *space;
3897 isl_union_map *res;
3900 /* Replace the parameters of "map" by those of data->space and
3901 * add the result to data->res.
3903 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3905 struct isl_union_map_reset_params_data *data = user;
3906 isl_space *space;
3908 space = isl_map_get_space(map);
3909 space = isl_space_replace_params(space, data->space);
3910 map = isl_map_reset_equal_dim_space(map, space);
3911 data->res = isl_union_map_add_map(data->res, map);
3913 return data->res ? isl_stat_ok : isl_stat_error;
3916 /* Replace the space of "umap" by "space", without modifying
3917 * the dimension of "umap", i.e., the number of parameters of "umap".
3919 * Since the hash values of the maps in the union map depend
3920 * on the parameters, a new union map needs to be constructed.
3922 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3923 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3925 struct isl_union_map_reset_params_data data = { space };
3926 isl_bool equal;
3927 isl_space *umap_space;
3929 umap_space = isl_union_map_peek_space(umap);
3930 equal = isl_space_is_equal(umap_space, space);
3931 if (equal < 0)
3932 goto error;
3933 if (equal) {
3934 isl_space_free(space);
3935 return umap;
3937 if (check_union_map_space_equal_dim(umap, space) < 0)
3938 goto error;
3940 data.res = isl_union_map_empty(isl_space_copy(space));
3941 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3942 data.res = isl_union_map_free(data.res);
3944 isl_space_free(space);
3945 isl_union_map_free(umap);
3946 return data.res;
3947 error:
3948 isl_union_map_free(umap);
3949 isl_space_free(space);
3950 return NULL;
3953 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3954 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3955 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3956 * to the domain and the range of each map.
3957 * "res" collects the results.
3959 struct isl_union_order_at_data {
3960 isl_multi_union_pw_aff *mupa;
3961 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3962 __isl_take isl_multi_pw_aff *mpa2);
3963 isl_union_map *res;
3966 /* Intersect "map" with the result of applying data->order to
3967 * the functions in data->mupa that apply to the domain and the range
3968 * of "map" and add the result to data->res.
3970 static isl_stat order_at(__isl_take isl_map *map, void *user)
3972 struct isl_union_order_at_data *data = user;
3973 isl_space *space;
3974 isl_multi_pw_aff *mpa1, *mpa2;
3975 isl_map *order;
3977 space = isl_space_domain(isl_map_get_space(map));
3978 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3979 space = isl_space_range(isl_map_get_space(map));
3980 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3981 order = data->order(mpa1, mpa2);
3982 map = isl_map_intersect(map, order);
3983 data->res = isl_union_map_add_map(data->res, map);
3985 return data->res ? isl_stat_ok : isl_stat_error;
3988 /* If "mupa" has a non-trivial explicit domain, then intersect
3989 * domain and range of "umap" with this explicit domain.
3990 * If the explicit domain only describes constraints on the parameters,
3991 * then the intersection only needs to be performed once.
3993 static __isl_give isl_union_map *intersect_explicit_domain(
3994 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
3996 isl_bool non_trivial, is_params;
3997 isl_union_set *dom;
3999 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4000 if (non_trivial < 0)
4001 return isl_union_map_free(umap);
4002 if (!non_trivial)
4003 return umap;
4004 mupa = isl_multi_union_pw_aff_copy(mupa);
4005 dom = isl_multi_union_pw_aff_domain(mupa);
4006 is_params = isl_union_set_is_params(dom);
4007 if (is_params < 0) {
4008 isl_union_set_free(dom);
4009 return isl_union_map_free(umap);
4011 if (is_params) {
4012 isl_set *set;
4014 set = isl_union_set_params(dom);
4015 umap = isl_union_map_intersect_params(umap, set);
4016 return umap;
4018 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4019 umap = isl_union_map_intersect_range(umap, dom);
4020 return umap;
4023 /* Intersect each map in "umap" with the result of calling "order"
4024 * on the functions is "mupa" that apply to the domain and the range
4025 * of the map.
4027 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4028 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4029 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4030 __isl_take isl_multi_pw_aff *mpa2))
4032 struct isl_union_order_at_data data;
4034 umap = isl_union_map_align_params(umap,
4035 isl_multi_union_pw_aff_get_space(mupa));
4036 mupa = isl_multi_union_pw_aff_align_params(mupa,
4037 isl_union_map_get_space(umap));
4038 umap = intersect_explicit_domain(umap, mupa);
4039 data.mupa = mupa;
4040 data.order = order;
4041 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4042 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4043 data.res = isl_union_map_free(data.res);
4045 isl_multi_union_pw_aff_free(mupa);
4046 isl_union_map_free(umap);
4047 return data.res;
4050 /* Return the subset of "umap" where the domain and the range
4051 * have equal "mupa" values.
4053 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4054 __isl_take isl_union_map *umap,
4055 __isl_take isl_multi_union_pw_aff *mupa)
4057 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4058 &isl_multi_pw_aff_eq_map);
4061 /* Return the subset of "umap" where the domain has a lexicographically
4062 * smaller "mupa" value than the range.
4064 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4065 __isl_take isl_union_map *umap,
4066 __isl_take isl_multi_union_pw_aff *mupa)
4068 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4069 &isl_multi_pw_aff_lex_lt_map);
4072 /* Return the subset of "umap" where the domain has a lexicographically
4073 * greater "mupa" value than the range.
4075 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4076 __isl_take isl_union_map *umap,
4077 __isl_take isl_multi_union_pw_aff *mupa)
4079 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4080 &isl_multi_pw_aff_lex_gt_map);
4083 /* Return the union of the elements in the list "list".
4085 __isl_give isl_union_set *isl_union_set_list_union(
4086 __isl_take isl_union_set_list *list)
4088 int i, n;
4089 isl_ctx *ctx;
4090 isl_space *space;
4091 isl_union_set *res;
4093 if (!list)
4094 return NULL;
4096 ctx = isl_union_set_list_get_ctx(list);
4097 space = isl_space_params_alloc(ctx, 0);
4098 res = isl_union_set_empty(space);
4100 n = isl_union_set_list_n_union_set(list);
4101 for (i = 0; i < n; ++i) {
4102 isl_union_set *uset_i;
4104 uset_i = isl_union_set_list_get_union_set(list, i);
4105 res = isl_union_set_union(res, uset_i);
4108 isl_union_set_list_free(list);
4109 return res;
4112 /* Update *hash with the hash value of "map".
4114 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4116 uint32_t *hash = user;
4117 uint32_t map_hash;
4119 map_hash = isl_map_get_hash(map);
4120 isl_hash_hash(*hash, map_hash);
4122 isl_map_free(map);
4123 return isl_stat_ok;
4126 /* Return a hash value that digests "umap".
4128 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4130 uint32_t hash;
4132 if (!umap)
4133 return 0;
4135 hash = isl_hash_init();
4136 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4137 return 0;
4139 return hash;
4142 /* Return a hash value that digests "uset".
4144 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4146 return isl_union_map_get_hash(uset);
4149 /* Add the number of basic sets in "set" to "n".
4151 static isl_stat add_n(__isl_take isl_set *set, void *user)
4153 int *n = user;
4155 *n += isl_set_n_basic_set(set);
4156 isl_set_free(set);
4158 return isl_stat_ok;
4161 /* Return the total number of basic sets in "uset".
4163 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4165 int n = 0;
4167 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4168 return -1;
4170 return n;
4173 /* Add the basic sets in "set" to "list".
4175 static isl_stat add_list(__isl_take isl_set *set, void *user)
4177 isl_basic_set_list **list = user;
4178 isl_basic_set_list *list_i;
4180 list_i = isl_set_get_basic_set_list(set);
4181 *list = isl_basic_set_list_concat(*list, list_i);
4182 isl_set_free(set);
4184 if (!*list)
4185 return isl_stat_error;
4186 return isl_stat_ok;
4189 /* Return a list containing all the basic sets in "uset".
4191 * First construct a list of the appropriate size and
4192 * then add all the elements.
4194 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4195 __isl_keep isl_union_set *uset)
4197 int n;
4198 isl_ctx *ctx;
4199 isl_basic_set_list *list;
4201 if (!uset)
4202 return NULL;
4203 ctx = isl_union_set_get_ctx(uset);
4204 n = isl_union_set_n_basic_set(uset);
4205 if (n < 0)
4206 return NULL;
4207 list = isl_basic_set_list_alloc(ctx, n);
4208 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4209 list = isl_basic_set_list_free(list);
4211 return list;
4214 /* Internal data structure for isl_union_map_remove_map_if.
4215 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4217 struct isl_union_map_remove_map_if_data {
4218 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4219 void *user;
4222 /* isl_un_op_control filter that negates the result of data->fn
4223 * called on "map".
4225 static isl_bool not(__isl_keep isl_map *map, void *user)
4227 struct isl_union_map_remove_map_if_data *data = user;
4229 return isl_bool_not(data->fn(map, data->user));
4232 /* Dummy isl_un_op_control transformation callback that
4233 * simply returns the input.
4235 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4237 return map;
4240 /* Call "fn" on every map in "umap" and remove those maps
4241 * for which the callback returns true.
4243 * Use un_op to keep only those maps that are not filtered out,
4244 * applying an identity transformation on them.
4246 __isl_give isl_union_map *isl_union_map_remove_map_if(
4247 __isl_take isl_union_map *umap,
4248 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4250 struct isl_union_map_remove_map_if_data data = { fn, user };
4251 struct isl_un_op_control control = {
4252 .filter = &not,
4253 .filter_user = &data,
4254 .fn_map = &map_id,
4256 return un_op(umap, &control);