export isl_multi_aff_{domain,range}_map
[isl.git] / isl_union_map.c
blobfea092ddaefe4e980c5ef1037f5c506b9ab45c2c
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 isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
38 enum isl_dim_type type)
40 if (!umap)
41 return isl_size_error;
43 if (type != isl_dim_param)
44 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
45 "can only reference parameters", return isl_size_error);
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 isl_size 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 isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
483 return umap ? umap->table.n : isl_size_error;
486 isl_size isl_union_set_n_set(__isl_keep isl_union_set *uset)
488 return uset ? uset->table.n : isl_size_error;
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 isl_size n_maps;
577 isl_ctx *ctx;
578 isl_map_list *list;
580 n_maps = isl_union_map_n_map(umap);
581 if (n_maps < 0)
582 return NULL;
583 ctx = isl_union_map_get_ctx(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 /* Can "umap" be converted to an isl_map?
602 * That is, does it contain elements in exactly one space?
604 isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
606 isl_size n;
608 n = isl_union_map_n_map(umap);
609 if (n < 0)
610 return isl_bool_error;
611 return isl_bool_ok(n == 1);
614 /* Can "uset" be converted to an isl_set?
615 * That is, does it contain elements in exactly one space?
617 isl_bool isl_union_set_isa_set(__isl_keep isl_union_set *uset)
619 return isl_union_map_isa_map(uset_to_umap(uset));
622 static isl_stat copy_map(void **entry, void *user)
624 isl_map *map = *entry;
625 isl_map **map_p = user;
627 *map_p = isl_map_copy(map);
629 return isl_stat_error;
632 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
634 isl_bool is_map;
635 isl_ctx *ctx;
636 isl_map *map = NULL;
638 is_map = isl_union_map_isa_map(umap);
639 if (is_map < 0)
640 goto error;
641 ctx = isl_union_map_get_ctx(umap);
642 if (!is_map)
643 isl_die(ctx, isl_error_invalid,
644 "union map needs to contain elements in exactly "
645 "one space", goto error);
647 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
649 isl_union_map_free(umap);
651 return map;
652 error:
653 isl_union_map_free(umap);
654 return NULL;
657 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
659 return isl_map_from_union_map(uset);
662 /* Extract the map in "umap" that lives in the given space (ignoring
663 * parameters).
665 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
666 __isl_take isl_space *space)
668 uint32_t hash;
669 struct isl_hash_table_entry *entry;
671 space = isl_space_drop_all_params(space);
672 space = isl_space_align_params(space, isl_union_map_get_space(umap));
673 if (!umap || !space)
674 goto error;
676 hash = isl_space_get_hash(space);
677 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
678 &has_space, space, 0);
679 if (!entry)
680 return isl_map_empty(space);
681 isl_space_free(space);
682 return isl_map_copy(entry->data);
683 error:
684 isl_space_free(space);
685 return NULL;
688 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
689 __isl_take isl_space *dim)
691 return set_from_map(isl_union_map_extract_map(uset, dim));
694 /* Check if umap contains a map in the given space.
696 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
697 __isl_keep isl_space *space)
699 uint32_t hash;
700 struct isl_hash_table_entry *entry;
702 if (!umap || !space)
703 return isl_bool_error;
705 hash = isl_space_get_hash(space);
706 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
707 &has_space, space, 0);
708 return !!entry;
711 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
712 __isl_keep isl_space *space)
714 return isl_union_map_contains(uset, space);
717 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
718 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
720 return isl_union_map_foreach_map(uset,
721 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
724 /* Internal data structure for isl_union_set_every_set.
726 * "test" is the user-specified callback function.
727 * "user" is the user-specified callback function argument.
729 struct isl_test_set_from_map_data {
730 isl_bool (*test)(__isl_keep isl_set *set, void *user);
731 void *user;
734 /* Call data->test on "map", which is part of an isl_union_set and
735 * therefore known to be an isl_set.
737 static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
739 struct isl_test_set_from_map_data *data = user;
741 return data->test(set_from_map(map), data->user);
744 /* Does "test" succeed on every set in "uset"?
746 isl_bool isl_union_set_every_set(__isl_keep isl_union_set *uset,
747 isl_bool (*test)(__isl_keep isl_set *set, void *user), void *user)
749 struct isl_test_set_from_map_data data = { test, user };
751 return isl_union_map_every_map(uset_to_umap(uset),
752 &test_set_from_map, &data);
755 struct isl_union_set_foreach_point_data {
756 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
757 void *user;
760 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
762 struct isl_union_set_foreach_point_data *data = user;
763 isl_stat r;
765 r = isl_set_foreach_point(set, data->fn, data->user);
766 isl_set_free(set);
768 return r;
771 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
772 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
774 struct isl_union_set_foreach_point_data data = { fn, user };
775 return isl_union_set_foreach_set(uset, &foreach_point, &data);
778 /* Data structure that specifies how gen_bin_op should
779 * construct results from the inputs.
781 * If "subtract" is set, then a map in the first input is copied to the result
782 * if there is no corresponding map in the second input.
783 * Otherwise, a map in the first input with no corresponding map
784 * in the second input is ignored.
785 * If "filter" is not NULL, then it specifies which maps in the first
786 * input may have a matching map in the second input.
787 * In particular, it makes sure that "match_space" can be called
788 * on the space of the map.
789 * "match_space" specifies how to transform the space of a map
790 * in the first input to the space of the corresponding map
791 * in the second input.
792 * "fn_map" specifies how the matching maps, one from each input,
793 * should be combined to form a map in the result.
795 struct isl_bin_op_control {
796 int subtract;
797 isl_bool (*filter)(__isl_keep isl_map *map);
798 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
799 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
800 __isl_take isl_map *map2);
803 /* Internal data structure for gen_bin_op.
804 * "control" specifies how the maps in the result should be constructed.
805 * "umap2" is a pointer to the second argument.
806 * "res" collects the results.
808 struct isl_union_map_gen_bin_data {
809 struct isl_bin_op_control *control;
810 isl_union_map *umap2;
811 isl_union_map *res;
814 /* Add a copy of "map" to "res" and return the result.
816 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
817 __isl_keep isl_map *map)
819 return isl_union_map_add_map(res, isl_map_copy(map));
822 /* Combine "map1" and "map2", add the result to "res" and return the result.
823 * Check whether the result is empty before adding it to "res".
825 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
826 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
827 struct isl_union_map_gen_bin_data *data)
829 isl_bool empty;
830 isl_map *map;
832 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
833 empty = isl_map_is_empty(map);
834 if (empty < 0 || empty) {
835 isl_map_free(map);
836 if (empty < 0)
837 return isl_union_map_free(res);
838 return res;
840 return isl_union_map_add_map(res, map);
843 /* Dummy match_space function that simply returns the input space.
845 static __isl_give isl_space *identity(__isl_take isl_space *space)
847 return space;
850 /* Look for the map in data->umap2 that corresponds to "map", if any.
851 * Return (isl_bool_true, matching map) if there is one,
852 * (isl_bool_false, NULL) if there is no matching map and
853 * (isl_bool_error, NULL) on error.
855 * If not NULL, then data->control->filter specifies whether "map"
856 * can have any matching map. If so,
857 * data->control->match_space specifies which map in data->umap2
858 * corresponds to "map".
860 static __isl_keep isl_maybe_isl_map bin_try_get_match(
861 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
863 uint32_t hash;
864 struct isl_hash_table_entry *entry2;
865 isl_space *space;
866 isl_maybe_isl_map res = { isl_bool_error, NULL };
868 if (data->control->filter) {
869 res.valid = data->control->filter(map);
870 if (res.valid < 0 || !res.valid)
871 return res;
872 res.valid = isl_bool_error;
875 space = isl_map_get_space(map);
876 if (data->control->match_space != &identity)
877 space = data->control->match_space(space);
878 if (!space)
879 return res;
880 hash = isl_space_get_hash(space);
881 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
882 &data->umap2->table, hash,
883 &has_space, space, 0);
884 isl_space_free(space);
885 res.valid = entry2 != NULL;
886 if (entry2)
887 res.value = entry2->data;
889 return res;
892 /* isl_hash_table_foreach callback for gen_bin_op.
893 * Look for the map in data->umap2 that corresponds
894 * to the map that "entry" points to, apply the binary operation and
895 * add the result to data->res.
897 * If no corresponding map can be found, then the effect depends
898 * on data->control->subtract. If it is set, then the current map
899 * is added directly to the result. Otherwise, it is ignored.
901 static isl_stat gen_bin_entry(void **entry, void *user)
903 struct isl_union_map_gen_bin_data *data = user;
904 isl_map *map = *entry;
905 isl_maybe_isl_map m;
907 m = bin_try_get_match(data, map);
908 if (m.valid < 0)
909 return isl_stat_error;
910 if (!m.valid && !data->control->subtract)
911 return isl_stat_ok;
913 if (!m.valid)
914 data->res = bin_add_map(data->res, map);
915 else
916 data->res = bin_add_pair(data->res, map, m.value, data);
917 if (!data->res)
918 return isl_stat_error;
920 return isl_stat_ok;
923 /* Apply a binary operation to "umap1" and "umap2" based on "control".
924 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
925 * in gen_bin_entry.
927 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
928 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
930 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
932 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
933 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
935 if (!umap1 || !umap2)
936 goto error;
938 data.umap2 = umap2;
939 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
940 umap1->table.n);
941 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
942 &gen_bin_entry, &data) < 0)
943 goto error;
945 isl_union_map_free(umap1);
946 isl_union_map_free(umap2);
947 return data.res;
948 error:
949 isl_union_map_free(umap1);
950 isl_union_map_free(umap2);
951 isl_union_map_free(data.res);
952 return NULL;
955 __isl_give isl_union_map *isl_union_map_subtract(
956 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
958 struct isl_bin_op_control control = {
959 .subtract = 1,
960 .match_space = &identity,
961 .fn_map = &isl_map_subtract,
964 return gen_bin_op(umap1, umap2, &control);
967 __isl_give isl_union_set *isl_union_set_subtract(
968 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
970 return isl_union_map_subtract(uset1, uset2);
973 struct isl_union_map_gen_bin_set_data {
974 isl_set *set;
975 isl_union_map *res;
978 static isl_stat intersect_params_entry(void **entry, void *user)
980 struct isl_union_map_gen_bin_set_data *data = user;
981 isl_map *map = *entry;
982 int empty;
984 map = isl_map_copy(map);
985 map = isl_map_intersect_params(map, isl_set_copy(data->set));
987 empty = isl_map_is_empty(map);
988 if (empty < 0) {
989 isl_map_free(map);
990 return isl_stat_error;
993 data->res = isl_union_map_add_map(data->res, map);
995 return isl_stat_ok;
998 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
999 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
1001 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
1003 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1004 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1006 if (!umap || !set)
1007 goto error;
1009 data.set = set;
1010 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1011 umap->table.n);
1012 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1013 fn, &data) < 0)
1014 goto error;
1016 isl_union_map_free(umap);
1017 isl_set_free(set);
1018 return data.res;
1019 error:
1020 isl_union_map_free(umap);
1021 isl_set_free(set);
1022 isl_union_map_free(data.res);
1023 return NULL;
1026 /* Intersect "umap" with the parameter domain "set".
1028 * If "set" does not have any constraints, then we can return immediately.
1030 __isl_give isl_union_map *isl_union_map_intersect_params(
1031 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1033 int is_universe;
1035 is_universe = isl_set_plain_is_universe(set);
1036 if (is_universe < 0)
1037 goto error;
1038 if (is_universe) {
1039 isl_set_free(set);
1040 return umap;
1043 return gen_bin_set_op(umap, set, &intersect_params_entry);
1044 error:
1045 isl_union_map_free(umap);
1046 isl_set_free(set);
1047 return NULL;
1050 __isl_give isl_union_set *isl_union_set_intersect_params(
1051 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1053 return isl_union_map_intersect_params(uset, set);
1056 static __isl_give isl_union_map *union_map_intersect_params(
1057 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1059 return isl_union_map_intersect_params(umap,
1060 isl_set_from_union_set(uset));
1063 static __isl_give isl_union_map *union_map_gist_params(
1064 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1066 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1069 struct isl_union_map_match_bin_data {
1070 isl_union_map *umap2;
1071 isl_union_map *res;
1072 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1075 static isl_stat match_bin_entry(void **entry, void *user)
1077 struct isl_union_map_match_bin_data *data = user;
1078 uint32_t hash;
1079 struct isl_hash_table_entry *entry2;
1080 isl_map *map = *entry;
1081 int empty;
1083 hash = isl_space_get_hash(map->dim);
1084 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1085 hash, &has_space, map->dim, 0);
1086 if (!entry2)
1087 return isl_stat_ok;
1089 map = isl_map_copy(map);
1090 map = data->fn(map, isl_map_copy(entry2->data));
1092 empty = isl_map_is_empty(map);
1093 if (empty < 0) {
1094 isl_map_free(map);
1095 return isl_stat_error;
1097 if (empty) {
1098 isl_map_free(map);
1099 return isl_stat_ok;
1102 data->res = isl_union_map_add_map(data->res, map);
1104 return isl_stat_ok;
1107 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1108 __isl_take isl_union_map *umap2,
1109 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1111 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1113 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1114 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1116 if (!umap1 || !umap2)
1117 goto error;
1119 data.umap2 = umap2;
1120 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1121 umap1->table.n);
1122 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1123 &match_bin_entry, &data) < 0)
1124 goto error;
1126 isl_union_map_free(umap1);
1127 isl_union_map_free(umap2);
1128 return data.res;
1129 error:
1130 isl_union_map_free(umap1);
1131 isl_union_map_free(umap2);
1132 isl_union_map_free(data.res);
1133 return NULL;
1136 __isl_give isl_union_map *isl_union_map_intersect(
1137 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1139 return match_bin_op(umap1, umap2, &isl_map_intersect);
1142 /* Compute the intersection of the two union_sets.
1143 * As a special case, if exactly one of the two union_sets
1144 * is a parameter domain, then intersect the parameter domain
1145 * of the other one with this set.
1147 __isl_give isl_union_set *isl_union_set_intersect(
1148 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1150 int p1, p2;
1152 p1 = isl_union_set_is_params(uset1);
1153 p2 = isl_union_set_is_params(uset2);
1154 if (p1 < 0 || p2 < 0)
1155 goto error;
1156 if (!p1 && p2)
1157 return union_map_intersect_params(uset1, uset2);
1158 if (p1 && !p2)
1159 return union_map_intersect_params(uset2, uset1);
1160 return isl_union_map_intersect(uset1, uset2);
1161 error:
1162 isl_union_set_free(uset1);
1163 isl_union_set_free(uset2);
1164 return NULL;
1167 static isl_stat gist_params_entry(void **entry, void *user)
1169 struct isl_union_map_gen_bin_set_data *data = user;
1170 isl_map *map = *entry;
1171 int empty;
1173 map = isl_map_copy(map);
1174 map = isl_map_gist_params(map, isl_set_copy(data->set));
1176 empty = isl_map_is_empty(map);
1177 if (empty < 0) {
1178 isl_map_free(map);
1179 return isl_stat_error;
1182 data->res = isl_union_map_add_map(data->res, map);
1184 return isl_stat_ok;
1187 __isl_give isl_union_map *isl_union_map_gist_params(
1188 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1190 return gen_bin_set_op(umap, set, &gist_params_entry);
1193 __isl_give isl_union_set *isl_union_set_gist_params(
1194 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1196 return isl_union_map_gist_params(uset, set);
1199 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1200 __isl_take isl_union_map *context)
1202 return match_bin_op(umap, context, &isl_map_gist);
1205 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1206 __isl_take isl_union_set *context)
1208 if (isl_union_set_is_params(context))
1209 return union_map_gist_params(uset, context);
1210 return isl_union_map_gist(uset, context);
1213 /* For each map in "umap", remove the constraints in the corresponding map
1214 * of "context".
1215 * Each map in "context" is assumed to consist of a single disjunct and
1216 * to have explicit representations for all local variables.
1218 __isl_give isl_union_map *isl_union_map_plain_gist(
1219 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1221 return match_bin_op(umap, context, &isl_map_plain_gist);
1224 /* For each set in "uset", remove the constraints in the corresponding set
1225 * of "context".
1226 * Each set in "context" is assumed to consist of a single disjunct and
1227 * to have explicit representations for all local variables.
1229 __isl_give isl_union_set *isl_union_set_plain_gist(
1230 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1232 return isl_union_map_plain_gist(uset, context);
1235 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1236 __isl_take isl_map *set2)
1238 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1241 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1242 __isl_take isl_map *set2)
1244 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1247 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1248 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1250 return match_bin_op(uset1, uset2, &lex_lt_set);
1253 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1254 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1256 return match_bin_op(uset1, uset2, &lex_le_set);
1259 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1260 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1262 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1265 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1266 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1268 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1271 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1272 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1274 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1277 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1278 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1280 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1283 /* Intersect the domain of "umap" with "uset".
1285 static __isl_give isl_union_map *union_map_intersect_domain(
1286 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1288 struct isl_bin_op_control control = {
1289 .match_space = &isl_space_domain,
1290 .fn_map = &isl_map_intersect_domain,
1293 return gen_bin_op(umap, uset, &control);
1296 /* Intersect the domain of "umap" with "uset".
1297 * If "uset" is a parameters domain, then intersect the parameter
1298 * domain of "umap" with this set.
1300 __isl_give isl_union_map *isl_union_map_intersect_domain(
1301 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1303 if (isl_union_set_is_params(uset))
1304 return union_map_intersect_params(umap, uset);
1305 else
1306 return union_map_intersect_domain(umap, uset);
1309 /* Remove the elements of "uset" from the domain of "umap".
1311 __isl_give isl_union_map *isl_union_map_subtract_domain(
1312 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1314 struct isl_bin_op_control control = {
1315 .subtract = 1,
1316 .match_space = &isl_space_domain,
1317 .fn_map = &isl_map_subtract_domain,
1320 return gen_bin_op(umap, dom, &control);
1323 /* Remove the elements of "uset" from the range of "umap".
1325 __isl_give isl_union_map *isl_union_map_subtract_range(
1326 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1328 struct isl_bin_op_control control = {
1329 .subtract = 1,
1330 .match_space = &isl_space_range,
1331 .fn_map = &isl_map_subtract_range,
1334 return gen_bin_op(umap, dom, &control);
1337 /* Compute the gist of "umap" with respect to the domain "uset".
1339 static __isl_give isl_union_map *union_map_gist_domain(
1340 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1342 struct isl_bin_op_control control = {
1343 .match_space = &isl_space_domain,
1344 .fn_map = &isl_map_gist_domain,
1347 return gen_bin_op(umap, uset, &control);
1350 /* Compute the gist of "umap" with respect to the domain "uset".
1351 * If "uset" is a parameters domain, then compute the gist
1352 * with respect to this parameter domain.
1354 __isl_give isl_union_map *isl_union_map_gist_domain(
1355 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1357 if (isl_union_set_is_params(uset))
1358 return union_map_gist_params(umap, uset);
1359 else
1360 return union_map_gist_domain(umap, uset);
1363 /* Compute the gist of "umap" with respect to the range "uset".
1365 __isl_give isl_union_map *isl_union_map_gist_range(
1366 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1368 struct isl_bin_op_control control = {
1369 .match_space = &isl_space_range,
1370 .fn_map = &isl_map_gist_range,
1373 return gen_bin_op(umap, uset, &control);
1376 __isl_give isl_union_map *isl_union_map_intersect_range(
1377 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1379 struct isl_bin_op_control control = {
1380 .match_space = &isl_space_range,
1381 .fn_map = &isl_map_intersect_range,
1384 return gen_bin_op(umap, uset, &control);
1387 /* Intersect each map in "umap" in a space A -> [B -> C]
1388 * with the corresponding map in "factor" in the space A -> C and
1389 * collect the results.
1391 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1392 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1394 struct isl_bin_op_control control = {
1395 .filter = &isl_map_range_is_wrapping,
1396 .match_space = &isl_space_range_factor_range,
1397 .fn_map = &isl_map_intersect_range_factor_range,
1400 return gen_bin_op(umap, factor, &control);
1403 struct isl_union_map_bin_data {
1404 isl_union_map *umap2;
1405 isl_union_map *res;
1406 isl_map *map;
1407 isl_stat (*fn)(void **entry, void *user);
1410 static isl_stat apply_range_entry(void **entry, void *user)
1412 struct isl_union_map_bin_data *data = user;
1413 isl_map *map2 = *entry;
1414 isl_bool empty;
1416 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1417 map2->dim, isl_dim_in))
1418 return isl_stat_ok;
1420 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1422 empty = isl_map_is_empty(map2);
1423 if (empty < 0) {
1424 isl_map_free(map2);
1425 return isl_stat_error;
1427 if (empty) {
1428 isl_map_free(map2);
1429 return isl_stat_ok;
1432 data->res = isl_union_map_add_map(data->res, map2);
1434 return isl_stat_ok;
1437 static isl_stat bin_entry(void **entry, void *user)
1439 struct isl_union_map_bin_data *data = user;
1440 isl_map *map = *entry;
1442 data->map = map;
1443 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1444 data->fn, data) < 0)
1445 return isl_stat_error;
1447 return isl_stat_ok;
1450 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1451 __isl_take isl_union_map *umap2,
1452 isl_stat (*fn)(void **entry, void *user))
1454 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1456 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1457 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1459 if (!umap1 || !umap2)
1460 goto error;
1462 data.umap2 = umap2;
1463 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1464 umap1->table.n);
1465 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1466 &bin_entry, &data) < 0)
1467 goto error;
1469 isl_union_map_free(umap1);
1470 isl_union_map_free(umap2);
1471 return data.res;
1472 error:
1473 isl_union_map_free(umap1);
1474 isl_union_map_free(umap2);
1475 isl_union_map_free(data.res);
1476 return NULL;
1479 __isl_give isl_union_map *isl_union_map_apply_range(
1480 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1482 return bin_op(umap1, umap2, &apply_range_entry);
1485 __isl_give isl_union_map *isl_union_map_apply_domain(
1486 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1488 umap1 = isl_union_map_reverse(umap1);
1489 umap1 = isl_union_map_apply_range(umap1, umap2);
1490 return isl_union_map_reverse(umap1);
1493 __isl_give isl_union_set *isl_union_set_apply(
1494 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1496 return isl_union_map_apply_range(uset, umap);
1499 static isl_stat map_lex_lt_entry(void **entry, void *user)
1501 struct isl_union_map_bin_data *data = user;
1502 isl_map *map2 = *entry;
1504 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1505 map2->dim, isl_dim_out))
1506 return isl_stat_ok;
1508 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1510 data->res = isl_union_map_add_map(data->res, map2);
1512 return isl_stat_ok;
1515 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1516 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1518 return bin_op(umap1, umap2, &map_lex_lt_entry);
1521 static isl_stat map_lex_le_entry(void **entry, void *user)
1523 struct isl_union_map_bin_data *data = user;
1524 isl_map *map2 = *entry;
1526 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1527 map2->dim, isl_dim_out))
1528 return isl_stat_ok;
1530 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1532 data->res = isl_union_map_add_map(data->res, map2);
1534 return isl_stat_ok;
1537 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1538 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1540 return bin_op(umap1, umap2, &map_lex_le_entry);
1543 static isl_stat product_entry(void **entry, void *user)
1545 struct isl_union_map_bin_data *data = user;
1546 isl_map *map2 = *entry;
1548 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1550 data->res = isl_union_map_add_map(data->res, map2);
1552 return isl_stat_ok;
1555 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1556 __isl_take isl_union_map *umap2)
1558 return bin_op(umap1, umap2, &product_entry);
1561 static isl_stat set_product_entry(void **entry, void *user)
1563 struct isl_union_map_bin_data *data = user;
1564 isl_set *set2 = *entry;
1566 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1568 data->res = isl_union_set_add_set(data->res, set2);
1570 return isl_stat_ok;
1573 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1574 __isl_take isl_union_set *uset2)
1576 return bin_op(uset1, uset2, &set_product_entry);
1579 static isl_stat domain_product_entry(void **entry, void *user)
1581 struct isl_union_map_bin_data *data = user;
1582 isl_map *map2 = *entry;
1584 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1585 map2->dim, isl_dim_out))
1586 return isl_stat_ok;
1588 map2 = isl_map_domain_product(isl_map_copy(data->map),
1589 isl_map_copy(map2));
1591 data->res = isl_union_map_add_map(data->res, map2);
1593 return isl_stat_ok;
1596 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1598 __isl_give isl_union_map *isl_union_map_domain_product(
1599 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1601 return bin_op(umap1, umap2, &domain_product_entry);
1604 static isl_stat range_product_entry(void **entry, void *user)
1606 struct isl_union_map_bin_data *data = user;
1607 isl_map *map2 = *entry;
1609 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1610 map2->dim, isl_dim_in))
1611 return isl_stat_ok;
1613 map2 = isl_map_range_product(isl_map_copy(data->map),
1614 isl_map_copy(map2));
1616 data->res = isl_union_map_add_map(data->res, map2);
1618 return isl_stat_ok;
1621 __isl_give isl_union_map *isl_union_map_range_product(
1622 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1624 return bin_op(umap1, umap2, &range_product_entry);
1627 /* If data->map A -> B and "map2" C -> D have the same range space,
1628 * then add (A, C) -> (B * D) to data->res.
1630 static isl_stat flat_domain_product_entry(void **entry, void *user)
1632 struct isl_union_map_bin_data *data = user;
1633 isl_map *map2 = *entry;
1635 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1636 map2->dim, isl_dim_out))
1637 return isl_stat_ok;
1639 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1640 isl_map_copy(map2));
1642 data->res = isl_union_map_add_map(data->res, map2);
1644 return isl_stat_ok;
1647 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1649 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1650 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1652 return bin_op(umap1, umap2, &flat_domain_product_entry);
1655 static isl_stat flat_range_product_entry(void **entry, void *user)
1657 struct isl_union_map_bin_data *data = user;
1658 isl_map *map2 = *entry;
1660 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1661 map2->dim, isl_dim_in))
1662 return isl_stat_ok;
1664 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1665 isl_map_copy(map2));
1667 data->res = isl_union_map_add_map(data->res, map2);
1669 return isl_stat_ok;
1672 __isl_give isl_union_map *isl_union_map_flat_range_product(
1673 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1675 return bin_op(umap1, umap2, &flat_range_product_entry);
1678 /* Data structure that specifies how un_op should modify
1679 * the maps in the union map.
1681 * If "inplace" is set, then the maps in the input union map
1682 * are modified in place. This means that "fn_map" should not
1683 * change the meaning of the map or that the union map only
1684 * has a single reference.
1685 * If "total" is set, then all maps need to be modified and
1686 * the results need to live in the same space.
1687 * Otherwise, a new union map is constructed to store the results.
1688 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1689 * are taken into account. "filter_user" is passed as the second argument
1690 * to "filter". No filter can be set if "inplace" or
1691 * "total" is set.
1692 * "fn_map" specifies how the maps (selected by "filter")
1693 * should be transformed.
1695 struct isl_un_op_control {
1696 int inplace;
1697 int total;
1698 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1699 void *filter_user;
1700 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1703 /* Data structure for wrapping the data for un_op_filter_drop_user.
1704 * "filter" is the function that is being wrapped.
1706 struct isl_un_op_drop_user_data {
1707 isl_bool (*filter)(__isl_keep isl_map *map);
1710 /* Wrapper for isl_un_op_control filters that do not require
1711 * a second argument.
1712 * Simply call data->filter without the second argument.
1714 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1716 struct isl_un_op_drop_user_data *data = user;
1717 return data->filter(map);
1720 /* Internal data structure for "un_op".
1721 * "control" specifies how the maps in the union map should be modified.
1722 * "res" collects the results.
1724 struct isl_union_map_un_data {
1725 struct isl_un_op_control *control;
1726 isl_union_map *res;
1729 /* isl_hash_table_foreach callback for un_op.
1730 * Handle the map that "entry" points to.
1732 * If control->filter is set, then check if this map satisfies the filter.
1733 * If so (or if control->filter is not set), modify the map
1734 * by calling control->fn_map and either add the result to data->res or
1735 * replace the original entry by the result (if control->inplace is set).
1737 static isl_stat un_entry(void **entry, void *user)
1739 struct isl_union_map_un_data *data = user;
1740 isl_map *map = *entry;
1742 if (data->control->filter) {
1743 isl_bool ok;
1745 ok = data->control->filter(map, data->control->filter_user);
1746 if (ok < 0)
1747 return isl_stat_error;
1748 if (!ok)
1749 return isl_stat_ok;
1752 map = data->control->fn_map(isl_map_copy(map));
1753 if (!map)
1754 return isl_stat_error;
1755 if (data->control->inplace) {
1756 isl_map_free(*entry);
1757 *entry = map;
1758 } else {
1759 data->res = isl_union_map_add_map(data->res, map);
1760 if (!data->res)
1761 return isl_stat_error;
1764 return isl_stat_ok;
1767 /* Modify the maps in "umap" based on "control".
1768 * If control->inplace is set, then modify the maps in "umap" in-place.
1769 * Otherwise, create a new union map to hold the results.
1770 * If control->total is set, then perform an inplace computation
1771 * if "umap" is only referenced once. Otherwise, create a new union map
1772 * to store the results.
1774 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1775 struct isl_un_op_control *control)
1777 struct isl_union_map_un_data data = { control };
1779 if (!umap)
1780 return NULL;
1781 if ((control->inplace || control->total) && control->filter)
1782 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1783 "inplace/total modification cannot be filtered",
1784 return isl_union_map_free(umap));
1786 if (control->total && umap->ref == 1)
1787 control->inplace = 1;
1788 if (control->inplace) {
1789 data.res = umap;
1790 } else {
1791 isl_space *space;
1793 space = isl_union_map_get_space(umap);
1794 data.res = isl_union_map_alloc(space, umap->table.n);
1796 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1797 &umap->table, &un_entry, &data) < 0)
1798 data.res = isl_union_map_free(data.res);
1800 if (control->inplace)
1801 return data.res;
1802 isl_union_map_free(umap);
1803 return data.res;
1806 __isl_give isl_union_map *isl_union_map_from_range(
1807 __isl_take isl_union_set *uset)
1809 struct isl_un_op_control control = {
1810 .fn_map = &isl_map_from_range,
1812 return un_op(uset, &control);
1815 __isl_give isl_union_map *isl_union_map_from_domain(
1816 __isl_take isl_union_set *uset)
1818 return isl_union_map_reverse(isl_union_map_from_range(uset));
1821 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1822 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1824 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1825 isl_union_map_from_range(range));
1828 /* Modify the maps in "umap" by applying "fn" on them.
1829 * "fn" should apply to all maps in "umap" and should not modify the space.
1831 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1832 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1834 struct isl_un_op_control control = {
1835 .total = 1,
1836 .fn_map = fn,
1839 return un_op(umap, &control);
1842 /* Compute the affine hull of "map" and return the result as an isl_map.
1844 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1846 return isl_map_from_basic_map(isl_map_affine_hull(map));
1849 __isl_give isl_union_map *isl_union_map_affine_hull(
1850 __isl_take isl_union_map *umap)
1852 return total(umap, &isl_map_affine_hull_map);
1855 __isl_give isl_union_set *isl_union_set_affine_hull(
1856 __isl_take isl_union_set *uset)
1858 return isl_union_map_affine_hull(uset);
1861 /* Wrapper around isl_set_combined_lineality_space
1862 * that returns the combined lineality space in the form of an isl_set
1863 * instead of an isl_basic_set.
1865 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1867 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1870 /* For each set in "uset", compute the (linear) hull
1871 * of the lineality spaces of its basic sets and
1872 * collect and return the results.
1874 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1875 __isl_take isl_union_set *uset)
1877 struct isl_un_op_control control = {
1878 .fn_map = &combined_lineality_space,
1880 return un_op(uset, &control);
1883 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1885 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1887 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1890 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1891 __isl_take isl_union_map *umap)
1893 return total(umap, &isl_map_polyhedral_hull_map);
1896 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1897 __isl_take isl_union_set *uset)
1899 return isl_union_map_polyhedral_hull(uset);
1902 /* Compute a superset of the convex hull of "map" that is described
1903 * by only translates of the constraints in the constituents of "map" and
1904 * return the result as an isl_map.
1906 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1908 return isl_map_from_basic_map(isl_map_simple_hull(map));
1911 __isl_give isl_union_map *isl_union_map_simple_hull(
1912 __isl_take isl_union_map *umap)
1914 return total(umap, &isl_map_simple_hull_map);
1917 __isl_give isl_union_set *isl_union_set_simple_hull(
1918 __isl_take isl_union_set *uset)
1920 return isl_union_map_simple_hull(uset);
1923 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1924 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1926 struct isl_un_op_control control = {
1927 .inplace = 1,
1928 .fn_map = fn,
1931 return un_op(umap, &control);
1934 /* Remove redundant constraints in each of the basic maps of "umap".
1935 * Since removing redundant constraints does not change the meaning
1936 * or the space, the operation can be performed in-place.
1938 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1939 __isl_take isl_union_map *umap)
1941 return inplace(umap, &isl_map_remove_redundancies);
1944 /* Remove redundant constraints in each of the basic sets of "uset".
1946 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1947 __isl_take isl_union_set *uset)
1949 return isl_union_map_remove_redundancies(uset);
1952 __isl_give isl_union_map *isl_union_map_coalesce(
1953 __isl_take isl_union_map *umap)
1955 return inplace(umap, &isl_map_coalesce);
1958 __isl_give isl_union_set *isl_union_set_coalesce(
1959 __isl_take isl_union_set *uset)
1961 return isl_union_map_coalesce(uset);
1964 __isl_give isl_union_map *isl_union_map_detect_equalities(
1965 __isl_take isl_union_map *umap)
1967 return inplace(umap, &isl_map_detect_equalities);
1970 __isl_give isl_union_set *isl_union_set_detect_equalities(
1971 __isl_take isl_union_set *uset)
1973 return isl_union_map_detect_equalities(uset);
1976 __isl_give isl_union_map *isl_union_map_compute_divs(
1977 __isl_take isl_union_map *umap)
1979 return inplace(umap, &isl_map_compute_divs);
1982 __isl_give isl_union_set *isl_union_set_compute_divs(
1983 __isl_take isl_union_set *uset)
1985 return isl_union_map_compute_divs(uset);
1988 __isl_give isl_union_map *isl_union_map_lexmin(
1989 __isl_take isl_union_map *umap)
1991 return total(umap, &isl_map_lexmin);
1994 __isl_give isl_union_set *isl_union_set_lexmin(
1995 __isl_take isl_union_set *uset)
1997 return isl_union_map_lexmin(uset);
2000 __isl_give isl_union_map *isl_union_map_lexmax(
2001 __isl_take isl_union_map *umap)
2003 return total(umap, &isl_map_lexmax);
2006 __isl_give isl_union_set *isl_union_set_lexmax(
2007 __isl_take isl_union_set *uset)
2009 return isl_union_map_lexmax(uset);
2012 /* Return the universe in the space of "map".
2014 static __isl_give isl_map *universe(__isl_take isl_map *map)
2016 isl_space *space;
2018 space = isl_map_get_space(map);
2019 isl_map_free(map);
2020 return isl_map_universe(space);
2023 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2025 struct isl_un_op_control control = {
2026 .fn_map = &universe,
2028 return un_op(umap, &control);
2031 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
2033 return isl_union_map_universe(uset);
2036 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2038 struct isl_un_op_control control = {
2039 .fn_map = &isl_map_reverse,
2041 return un_op(umap, &control);
2044 /* Compute the parameter domain of the given union map.
2046 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
2048 struct isl_un_op_control control = {
2049 .fn_map = &isl_map_params,
2051 int empty;
2053 empty = isl_union_map_is_empty(umap);
2054 if (empty < 0)
2055 goto error;
2056 if (empty) {
2057 isl_space *space;
2058 space = isl_union_map_get_space(umap);
2059 isl_union_map_free(umap);
2060 return isl_set_empty(space);
2062 return isl_set_from_union_set(un_op(umap, &control));
2063 error:
2064 isl_union_map_free(umap);
2065 return NULL;
2068 /* Compute the parameter domain of the given union set.
2070 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
2072 return isl_union_map_params(uset);
2075 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
2077 struct isl_un_op_control control = {
2078 .fn_map = &isl_map_domain,
2080 return un_op(umap, &control);
2083 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
2085 struct isl_un_op_control control = {
2086 .fn_map = &isl_map_range,
2088 return un_op(umap, &control);
2091 __isl_give isl_union_map *isl_union_map_domain_map(
2092 __isl_take isl_union_map *umap)
2094 struct isl_un_op_control control = {
2095 .fn_map = &isl_map_domain_map,
2097 return un_op(umap, &control);
2100 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2101 * add the result to "res".
2103 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2105 isl_union_pw_multi_aff **res = user;
2106 isl_multi_aff *ma;
2107 isl_pw_multi_aff *pma;
2109 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2110 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2111 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2113 return *res ? isl_stat_ok : isl_stat_error;
2117 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2118 * to its domain.
2120 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2121 __isl_take isl_union_map *umap)
2123 isl_union_pw_multi_aff *res;
2125 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2126 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2127 res = isl_union_pw_multi_aff_free(res);
2129 isl_union_map_free(umap);
2130 return res;
2133 __isl_give isl_union_map *isl_union_map_range_map(
2134 __isl_take isl_union_map *umap)
2136 struct isl_un_op_control control = {
2137 .fn_map = &isl_map_range_map,
2139 return un_op(umap, &control);
2142 /* Given a collection of wrapped maps of the form A[B -> C],
2143 * return the collection of maps A[B -> C] -> B.
2145 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2146 __isl_take isl_union_set *uset)
2148 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2149 struct isl_un_op_control control = {
2150 .filter = &un_op_filter_drop_user,
2151 .filter_user = &data,
2152 .fn_map = &isl_set_wrapped_domain_map,
2154 return un_op(uset, &control);
2157 /* Does "map" relate elements from the same space?
2159 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2161 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2162 map->dim, isl_dim_out);
2165 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2167 struct isl_un_op_control control = {
2168 .filter = &equal_tuples,
2169 .fn_map = &isl_map_deltas,
2171 return un_op(umap, &control);
2174 __isl_give isl_union_map *isl_union_map_deltas_map(
2175 __isl_take isl_union_map *umap)
2177 struct isl_un_op_control control = {
2178 .filter = &equal_tuples,
2179 .fn_map = &isl_map_deltas_map,
2181 return un_op(umap, &control);
2184 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2186 struct isl_un_op_control control = {
2187 .fn_map = &isl_set_identity,
2189 return un_op(uset, &control);
2192 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2194 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2196 isl_union_pw_multi_aff **res = user;
2197 isl_space *space;
2198 isl_pw_multi_aff *pma;
2200 space = isl_space_map_from_set(isl_set_get_space(set));
2201 pma = isl_pw_multi_aff_identity(space);
2202 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2203 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2205 return *res ? isl_stat_ok : isl_stat_error;
2208 /* Return an identity function on "uset" in the form
2209 * of an isl_union_pw_multi_aff.
2211 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2212 __isl_take isl_union_set *uset)
2214 isl_union_pw_multi_aff *res;
2216 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2217 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2218 res = isl_union_pw_multi_aff_free(res);
2220 isl_union_set_free(uset);
2221 return res;
2224 /* For each map in "umap" of the form [A -> B] -> C,
2225 * construct the map A -> C and collect the results.
2227 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2228 __isl_take isl_union_map *umap)
2230 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2231 struct isl_un_op_control control = {
2232 .filter = &un_op_filter_drop_user,
2233 .filter_user = &data,
2234 .fn_map = &isl_map_domain_factor_domain,
2236 return un_op(umap, &control);
2239 /* For each map in "umap" of the form [A -> B] -> C,
2240 * construct the map B -> C and collect the results.
2242 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2243 __isl_take isl_union_map *umap)
2245 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2246 struct isl_un_op_control control = {
2247 .filter = &un_op_filter_drop_user,
2248 .filter_user = &data,
2249 .fn_map = &isl_map_domain_factor_range,
2251 return un_op(umap, &control);
2254 /* For each map in "umap" of the form A -> [B -> C],
2255 * construct the map A -> B and collect the results.
2257 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2258 __isl_take isl_union_map *umap)
2260 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2261 struct isl_un_op_control control = {
2262 .filter = &un_op_filter_drop_user,
2263 .filter_user = &data,
2264 .fn_map = &isl_map_range_factor_domain,
2266 return un_op(umap, &control);
2269 /* For each map in "umap" of the form A -> [B -> C],
2270 * construct the map A -> C and collect the results.
2272 __isl_give isl_union_map *isl_union_map_range_factor_range(
2273 __isl_take isl_union_map *umap)
2275 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2276 struct isl_un_op_control control = {
2277 .filter = &un_op_filter_drop_user,
2278 .filter_user = &data,
2279 .fn_map = &isl_map_range_factor_range,
2281 return un_op(umap, &control);
2284 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2285 * construct the map A -> C and collect the results.
2287 __isl_give isl_union_map *isl_union_map_factor_domain(
2288 __isl_take isl_union_map *umap)
2290 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2291 struct isl_un_op_control control = {
2292 .filter = &un_op_filter_drop_user,
2293 .filter_user = &data,
2294 .fn_map = &isl_map_factor_domain,
2296 return un_op(umap, &control);
2299 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2300 * construct the map B -> D and collect the results.
2302 __isl_give isl_union_map *isl_union_map_factor_range(
2303 __isl_take isl_union_map *umap)
2305 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2306 struct isl_un_op_control control = {
2307 .filter = &un_op_filter_drop_user,
2308 .filter_user = &data,
2309 .fn_map = &isl_map_factor_range,
2311 return un_op(umap, &control);
2314 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2316 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2317 struct isl_un_op_control control = {
2318 .filter = &un_op_filter_drop_user,
2319 .filter_user = &data,
2320 .fn_map = &isl_set_unwrap,
2322 return un_op(uset, &control);
2325 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2327 struct isl_un_op_control control = {
2328 .fn_map = &isl_map_wrap,
2330 return un_op(umap, &control);
2333 struct isl_union_map_is_subset_data {
2334 isl_union_map *umap2;
2335 isl_bool is_subset;
2338 static isl_stat is_subset_entry(void **entry, void *user)
2340 struct isl_union_map_is_subset_data *data = user;
2341 uint32_t hash;
2342 struct isl_hash_table_entry *entry2;
2343 isl_map *map = *entry;
2345 hash = isl_space_get_hash(map->dim);
2346 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2347 hash, &has_space, map->dim, 0);
2348 if (!entry2) {
2349 int empty = isl_map_is_empty(map);
2350 if (empty < 0)
2351 return isl_stat_error;
2352 if (empty)
2353 return isl_stat_ok;
2354 data->is_subset = isl_bool_false;
2355 return isl_stat_error;
2358 data->is_subset = isl_map_is_subset(map, entry2->data);
2359 if (data->is_subset < 0 || !data->is_subset)
2360 return isl_stat_error;
2362 return isl_stat_ok;
2365 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2366 __isl_keep isl_union_map *umap2)
2368 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2370 umap1 = isl_union_map_copy(umap1);
2371 umap2 = isl_union_map_copy(umap2);
2372 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2373 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2375 if (!umap1 || !umap2)
2376 goto error;
2378 data.umap2 = umap2;
2379 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2380 &is_subset_entry, &data) < 0 &&
2381 data.is_subset)
2382 goto error;
2384 isl_union_map_free(umap1);
2385 isl_union_map_free(umap2);
2387 return data.is_subset;
2388 error:
2389 isl_union_map_free(umap1);
2390 isl_union_map_free(umap2);
2391 return isl_bool_error;
2394 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2395 __isl_keep isl_union_set *uset2)
2397 return isl_union_map_is_subset(uset1, uset2);
2400 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2401 __isl_keep isl_union_map *umap2)
2403 isl_bool is_subset;
2405 if (!umap1 || !umap2)
2406 return isl_bool_error;
2407 is_subset = isl_union_map_is_subset(umap1, umap2);
2408 if (is_subset != isl_bool_true)
2409 return is_subset;
2410 is_subset = isl_union_map_is_subset(umap2, umap1);
2411 return is_subset;
2414 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2415 __isl_keep isl_union_set *uset2)
2417 return isl_union_map_is_equal(uset1, uset2);
2420 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2421 __isl_keep isl_union_map *umap2)
2423 isl_bool is_subset;
2425 if (!umap1 || !umap2)
2426 return isl_bool_error;
2427 is_subset = isl_union_map_is_subset(umap1, umap2);
2428 if (is_subset != isl_bool_true)
2429 return is_subset;
2430 is_subset = isl_union_map_is_subset(umap2, umap1);
2431 return isl_bool_not(is_subset);
2434 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2435 __isl_keep isl_union_set *uset2)
2437 return isl_union_map_is_strict_subset(uset1, uset2);
2440 /* Internal data structure for isl_union_map_is_disjoint.
2441 * umap2 is the union map with which we are comparing.
2442 * is_disjoint is initialized to 1 and is set to 0 as soon
2443 * as the union maps turn out not to be disjoint.
2445 struct isl_union_map_is_disjoint_data {
2446 isl_union_map *umap2;
2447 isl_bool is_disjoint;
2450 /* Check if "map" is disjoint from data->umap2 and abort
2451 * the search if it is not.
2453 static isl_stat is_disjoint_entry(void **entry, void *user)
2455 struct isl_union_map_is_disjoint_data *data = user;
2456 uint32_t hash;
2457 struct isl_hash_table_entry *entry2;
2458 isl_map *map = *entry;
2460 hash = isl_space_get_hash(map->dim);
2461 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2462 hash, &has_space, map->dim, 0);
2463 if (!entry2)
2464 return isl_stat_ok;
2466 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2467 if (data->is_disjoint < 0 || !data->is_disjoint)
2468 return isl_stat_error;
2470 return isl_stat_ok;
2473 /* Are "umap1" and "umap2" disjoint?
2475 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2476 __isl_keep isl_union_map *umap2)
2478 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2480 umap1 = isl_union_map_copy(umap1);
2481 umap2 = isl_union_map_copy(umap2);
2482 umap1 = isl_union_map_align_params(umap1,
2483 isl_union_map_get_space(umap2));
2484 umap2 = isl_union_map_align_params(umap2,
2485 isl_union_map_get_space(umap1));
2487 if (!umap1 || !umap2)
2488 goto error;
2490 data.umap2 = umap2;
2491 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2492 &is_disjoint_entry, &data) < 0 &&
2493 data.is_disjoint)
2494 goto error;
2496 isl_union_map_free(umap1);
2497 isl_union_map_free(umap2);
2499 return data.is_disjoint;
2500 error:
2501 isl_union_map_free(umap1);
2502 isl_union_map_free(umap2);
2503 return isl_bool_error;
2506 /* Are "uset1" and "uset2" disjoint?
2508 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2509 __isl_keep isl_union_set *uset2)
2511 return isl_union_map_is_disjoint(uset1, uset2);
2514 static isl_stat sample_entry(void **entry, void *user)
2516 isl_basic_map **sample = (isl_basic_map **)user;
2517 isl_map *map = *entry;
2519 *sample = isl_map_sample(isl_map_copy(map));
2520 if (!*sample)
2521 return isl_stat_error;
2522 if (!isl_basic_map_plain_is_empty(*sample))
2523 return isl_stat_error;
2524 return isl_stat_ok;
2527 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2529 isl_basic_map *sample = NULL;
2531 if (!umap)
2532 return NULL;
2534 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2535 &sample_entry, &sample) < 0 &&
2536 !sample)
2537 goto error;
2539 if (!sample)
2540 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2542 isl_union_map_free(umap);
2544 return sample;
2545 error:
2546 isl_union_map_free(umap);
2547 return NULL;
2550 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2552 return bset_from_bmap(isl_union_map_sample(uset));
2555 /* Return an element in "uset" in the form of an isl_point.
2556 * Return a void isl_point if "uset" is empty.
2558 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2560 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2563 struct isl_forall_data {
2564 isl_bool res;
2565 isl_bool (*fn)(__isl_keep isl_map *map);
2568 static isl_stat forall_entry(void **entry, void *user)
2570 struct isl_forall_data *data = user;
2571 isl_map *map = *entry;
2573 data->res = data->fn(map);
2574 if (data->res < 0)
2575 return isl_stat_error;
2577 if (!data->res)
2578 return isl_stat_error;
2580 return isl_stat_ok;
2583 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2584 isl_bool (*fn)(__isl_keep isl_map *map))
2586 struct isl_forall_data data = { isl_bool_true, fn };
2588 if (!umap)
2589 return isl_bool_error;
2591 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2592 &forall_entry, &data) < 0 && data.res)
2593 return isl_bool_error;
2595 return data.res;
2598 struct isl_forall_user_data {
2599 isl_bool res;
2600 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2601 void *user;
2604 static isl_stat forall_user_entry(void **entry, void *user)
2606 struct isl_forall_user_data *data = user;
2607 isl_map *map = *entry;
2609 data->res = data->fn(map, data->user);
2610 if (data->res < 0)
2611 return isl_stat_error;
2613 if (!data->res)
2614 return isl_stat_error;
2616 return isl_stat_ok;
2619 /* Check if fn(map, user) returns true for all maps "map" in umap.
2621 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2622 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2624 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2626 if (!umap)
2627 return isl_bool_error;
2629 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2630 &forall_user_entry, &data) < 0 && data.res)
2631 return isl_bool_error;
2633 return data.res;
2636 /* Is "umap" obviously empty?
2638 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2640 isl_size n;
2642 n = isl_union_map_n_map(umap);
2643 if (n < 0)
2644 return isl_bool_error;
2645 return n == 0;
2648 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2650 return union_map_forall(umap, &isl_map_is_empty);
2653 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2655 return isl_union_map_is_empty(uset);
2658 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2660 isl_bool is_subset;
2661 isl_space *dim;
2662 isl_map *id;
2664 if (!map)
2665 return isl_bool_error;
2667 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2668 map->dim, isl_dim_out))
2669 return isl_bool_false;
2671 dim = isl_map_get_space(map);
2672 id = isl_map_identity(dim);
2674 is_subset = isl_map_is_subset(map, id);
2676 isl_map_free(id);
2678 return is_subset;
2681 /* Given an isl_union_map that consists of a single map, check
2682 * if it is single-valued.
2684 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2686 isl_map *map;
2687 isl_bool sv;
2689 umap = isl_union_map_copy(umap);
2690 map = isl_map_from_union_map(umap);
2691 sv = isl_map_is_single_valued(map);
2692 isl_map_free(map);
2694 return sv;
2697 /* Internal data structure for single_valued_on_domain.
2699 * "umap" is the union map to be tested.
2700 * "sv" is set to 1 as long as "umap" may still be single-valued.
2702 struct isl_union_map_is_sv_data {
2703 isl_union_map *umap;
2704 isl_bool sv;
2707 /* Check if the data->umap is single-valued on "set".
2709 * If data->umap consists of a single map on "set", then test it
2710 * as an isl_map.
2712 * Otherwise, compute
2714 * M \circ M^-1
2716 * check if the result is a subset of the identity mapping and
2717 * store the result in data->sv.
2719 * Terminate as soon as data->umap has been determined not to
2720 * be single-valued.
2722 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2724 struct isl_union_map_is_sv_data *data = user;
2725 isl_union_map *umap, *test;
2726 isl_size n;
2728 umap = isl_union_map_copy(data->umap);
2729 umap = isl_union_map_intersect_domain(umap,
2730 isl_union_set_from_set(set));
2732 n = isl_union_map_n_map(umap);
2733 if (n < 0) {
2734 data->sv = isl_bool_error;
2735 } else if (n == 1) {
2736 data->sv = single_map_is_single_valued(umap);
2737 isl_union_map_free(umap);
2738 } else {
2739 test = isl_union_map_reverse(isl_union_map_copy(umap));
2740 test = isl_union_map_apply_range(test, umap);
2742 data->sv = union_map_forall(test, &is_subset_of_identity);
2744 isl_union_map_free(test);
2747 if (data->sv < 0 || !data->sv)
2748 return isl_stat_error;
2749 return isl_stat_ok;
2752 /* Check if the given map is single-valued.
2754 * If the union map consists of a single map, then test it as an isl_map.
2755 * Otherwise, check if the union map is single-valued on each of its
2756 * domain spaces.
2758 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2760 isl_union_map *universe;
2761 isl_union_set *domain;
2762 struct isl_union_map_is_sv_data data;
2763 isl_size n;
2765 n = isl_union_map_n_map(umap);
2766 if (n < 0)
2767 return isl_bool_error;
2768 if (n == 1)
2769 return single_map_is_single_valued(umap);
2771 universe = isl_union_map_universe(isl_union_map_copy(umap));
2772 domain = isl_union_map_domain(universe);
2774 data.sv = isl_bool_true;
2775 data.umap = umap;
2776 if (isl_union_set_foreach_set(domain,
2777 &single_valued_on_domain, &data) < 0 && data.sv)
2778 data.sv = isl_bool_error;
2779 isl_union_set_free(domain);
2781 return data.sv;
2784 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2786 isl_bool in;
2788 umap = isl_union_map_copy(umap);
2789 umap = isl_union_map_reverse(umap);
2790 in = isl_union_map_is_single_valued(umap);
2791 isl_union_map_free(umap);
2793 return in;
2796 /* Is "map" obviously not an identity relation because
2797 * it maps elements from one space to another space?
2798 * Update *non_identity accordingly.
2800 * In particular, if the domain and range spaces are the same,
2801 * then the map is not considered to obviously not be an identity relation.
2802 * Otherwise, the map is considered to obviously not be an identity relation
2803 * if it is is non-empty.
2805 * If "map" is determined to obviously not be an identity relation,
2806 * then the search is aborted.
2808 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2810 isl_bool *non_identity = user;
2811 isl_bool equal;
2812 isl_space *space;
2814 space = isl_map_get_space(map);
2815 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2816 if (equal >= 0 && !equal)
2817 *non_identity = isl_bool_not(isl_map_is_empty(map));
2818 else
2819 *non_identity = isl_bool_not(equal);
2820 isl_space_free(space);
2821 isl_map_free(map);
2823 if (*non_identity < 0 || *non_identity)
2824 return isl_stat_error;
2826 return isl_stat_ok;
2829 /* Is "umap" obviously not an identity relation because
2830 * it maps elements from one space to another space?
2832 * As soon as a map has been found that maps elements to a different space,
2833 * non_identity is changed and the search is aborted.
2835 static isl_bool isl_union_map_plain_is_not_identity(
2836 __isl_keep isl_union_map *umap)
2838 isl_bool non_identity;
2840 non_identity = isl_bool_false;
2841 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2842 &non_identity) < 0 &&
2843 non_identity == isl_bool_false)
2844 return isl_bool_error;
2846 return non_identity;
2849 /* Does "map" only map elements to themselves?
2850 * Update *identity accordingly.
2852 * If "map" is determined not to be an identity relation,
2853 * then the search is aborted.
2855 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2857 isl_bool *identity = user;
2859 *identity = isl_map_is_identity(map);
2860 isl_map_free(map);
2862 if (*identity < 0 || !*identity)
2863 return isl_stat_error;
2865 return isl_stat_ok;
2868 /* Does "umap" only map elements to themselves?
2870 * First check if there are any maps that map elements to different spaces.
2871 * If not, then check that all the maps (between identical spaces)
2872 * are identity relations.
2874 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2876 isl_bool non_identity;
2877 isl_bool identity;
2879 non_identity = isl_union_map_plain_is_not_identity(umap);
2880 if (non_identity < 0 || non_identity)
2881 return isl_bool_not(non_identity);
2883 identity = isl_bool_true;
2884 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2885 identity == isl_bool_true)
2886 return isl_bool_error;
2888 return identity;
2891 /* Represents a map that has a fixed value (v) for one of its
2892 * range dimensions.
2893 * The map in this structure is not reference counted, so it
2894 * is only valid while the isl_union_map from which it was
2895 * obtained is still alive.
2897 struct isl_fixed_map {
2898 isl_int v;
2899 isl_map *map;
2902 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2903 int n)
2905 int i;
2906 struct isl_fixed_map *v;
2908 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2909 if (!v)
2910 return NULL;
2911 for (i = 0; i < n; ++i)
2912 isl_int_init(v[i].v);
2913 return v;
2916 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2918 int i;
2920 if (!v)
2921 return;
2922 for (i = 0; i < n; ++i)
2923 isl_int_clear(v[i].v);
2924 free(v);
2927 /* Compare the "v" field of two isl_fixed_map structs.
2929 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2931 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2932 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2934 return isl_int_cmp(e1->v, e2->v);
2937 /* Internal data structure used while checking whether all maps
2938 * in a union_map have a fixed value for a given output dimension.
2939 * v is the list of maps, with the fixed value for the dimension
2940 * n is the number of maps considered so far
2941 * pos is the output dimension under investigation
2943 struct isl_fixed_dim_data {
2944 struct isl_fixed_map *v;
2945 int n;
2946 int pos;
2949 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2951 struct isl_fixed_dim_data *data = user;
2953 data->v[data->n].map = map;
2954 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2955 &data->v[data->n++].v);
2958 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2959 int first, int n_range);
2961 /* Given a list of the maps, with their fixed values at output dimension "pos",
2962 * check whether the ranges of the maps form an obvious partition.
2964 * We first sort the maps according to their fixed values.
2965 * If all maps have a different value, then we know the ranges form
2966 * a partition.
2967 * Otherwise, we collect the maps with the same fixed value and
2968 * check whether each such collection is obviously injective
2969 * based on later dimensions.
2971 static int separates(struct isl_fixed_map *v, int n,
2972 __isl_take isl_space *dim, int pos, int n_range)
2974 int i;
2976 if (!v)
2977 goto error;
2979 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2981 for (i = 0; i + 1 < n; ++i) {
2982 int j, k;
2983 isl_union_map *part;
2984 int injective;
2986 for (j = i + 1; j < n; ++j)
2987 if (isl_int_ne(v[i].v, v[j].v))
2988 break;
2990 if (j == i + 1)
2991 continue;
2993 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2994 for (k = i; k < j; ++k)
2995 part = isl_union_map_add_map(part,
2996 isl_map_copy(v[k].map));
2998 injective = plain_injective_on_range(part, pos + 1, n_range);
2999 if (injective < 0)
3000 goto error;
3001 if (!injective)
3002 break;
3004 i = j - 1;
3007 isl_space_free(dim);
3008 free_isl_fixed_map_array(v, n);
3009 return i + 1 >= n;
3010 error:
3011 isl_space_free(dim);
3012 free_isl_fixed_map_array(v, n);
3013 return -1;
3016 /* Check whether the maps in umap have obviously distinct ranges.
3017 * In particular, check for an output dimension in the range
3018 * [first,n_range) for which all maps have a fixed value
3019 * and then check if these values, possibly along with fixed values
3020 * at later dimensions, entail distinct ranges.
3022 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3023 int first, int n_range)
3025 isl_ctx *ctx;
3026 isl_size n;
3027 struct isl_fixed_dim_data data = { NULL };
3029 ctx = isl_union_map_get_ctx(umap);
3031 n = isl_union_map_n_map(umap);
3032 if (n < 0)
3033 goto error;
3035 if (n <= 1) {
3036 isl_union_map_free(umap);
3037 return isl_bool_true;
3040 if (first >= n_range) {
3041 isl_union_map_free(umap);
3042 return isl_bool_false;
3045 data.v = alloc_isl_fixed_map_array(ctx, n);
3046 if (!data.v)
3047 goto error;
3049 for (data.pos = first; data.pos < n_range; ++data.pos) {
3050 isl_bool fixed;
3051 int injective;
3052 isl_space *dim;
3054 data.n = 0;
3055 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3056 if (fixed < 0)
3057 goto error;
3058 if (!fixed)
3059 continue;
3060 dim = isl_union_map_get_space(umap);
3061 injective = separates(data.v, n, dim, data.pos, n_range);
3062 isl_union_map_free(umap);
3063 return injective;
3066 free_isl_fixed_map_array(data.v, n);
3067 isl_union_map_free(umap);
3069 return isl_bool_false;
3070 error:
3071 free_isl_fixed_map_array(data.v, n);
3072 isl_union_map_free(umap);
3073 return isl_bool_error;
3076 /* Check whether the maps in umap that map to subsets of "ran"
3077 * have obviously distinct ranges.
3079 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
3080 void *user)
3082 isl_size dim;
3083 isl_union_map *umap = user;
3085 dim = isl_set_dim(ran, isl_dim_set);
3086 if (dim < 0)
3087 return isl_bool_error;
3089 umap = isl_union_map_copy(umap);
3090 umap = isl_union_map_intersect_range(umap,
3091 isl_union_set_from_set(isl_set_copy(ran)));
3092 return plain_injective_on_range(umap, 0, dim);
3095 /* Check if the given union_map is obviously injective.
3097 * In particular, we first check if all individual maps are obviously
3098 * injective and then check if all the ranges of these maps are
3099 * obviously disjoint.
3101 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3103 isl_bool in;
3104 isl_union_map *univ;
3105 isl_union_set *ran;
3107 in = union_map_forall(umap, &isl_map_plain_is_injective);
3108 if (in < 0)
3109 return isl_bool_error;
3110 if (!in)
3111 return isl_bool_false;
3113 univ = isl_union_map_universe(isl_union_map_copy(umap));
3114 ran = isl_union_map_range(univ);
3116 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3118 isl_union_set_free(ran);
3120 return in;
3123 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3125 isl_bool sv;
3127 sv = isl_union_map_is_single_valued(umap);
3128 if (sv < 0 || !sv)
3129 return sv;
3131 return isl_union_map_is_injective(umap);
3134 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3136 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3137 struct isl_un_op_control control = {
3138 .filter = &un_op_filter_drop_user,
3139 .filter_user = &data,
3140 .fn_map = &isl_map_zip,
3142 return un_op(umap, &control);
3145 /* Given a union map, take the maps of the form A -> (B -> C) and
3146 * return the union of the corresponding maps (A -> B) -> C.
3148 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3150 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3151 struct isl_un_op_control control = {
3152 .filter = &un_op_filter_drop_user,
3153 .filter_user = &data,
3154 .fn_map = &isl_map_uncurry,
3156 return un_op(umap, &control);
3159 /* Given a union map, take the maps of the form (A -> B) -> C and
3160 * return the union of the corresponding maps A -> (B -> C).
3162 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3164 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3165 struct isl_un_op_control control = {
3166 .filter = &un_op_filter_drop_user,
3167 .filter_user = &data,
3168 .fn_map = &isl_map_curry,
3170 return un_op(umap, &control);
3173 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3174 * return the union of the corresponding maps A -> (B -> (C -> D)).
3176 __isl_give isl_union_map *isl_union_map_range_curry(
3177 __isl_take isl_union_map *umap)
3179 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3180 struct isl_un_op_control control = {
3181 .filter = &un_op_filter_drop_user,
3182 .filter_user = &data,
3183 .fn_map = &isl_map_range_curry,
3185 return un_op(umap, &control);
3188 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3190 struct isl_un_op_control control = {
3191 .fn_map = &isl_set_lift,
3193 return un_op(uset, &control);
3196 static isl_stat coefficients_entry(void **entry, void *user)
3198 isl_set *set = *entry;
3199 isl_union_set **res = user;
3201 set = isl_set_copy(set);
3202 set = isl_set_from_basic_set(isl_set_coefficients(set));
3203 *res = isl_union_set_add_set(*res, set);
3205 return isl_stat_ok;
3208 __isl_give isl_union_set *isl_union_set_coefficients(
3209 __isl_take isl_union_set *uset)
3211 isl_ctx *ctx;
3212 isl_space *dim;
3213 isl_union_set *res;
3215 if (!uset)
3216 return NULL;
3218 ctx = isl_union_set_get_ctx(uset);
3219 dim = isl_space_set_alloc(ctx, 0, 0);
3220 res = isl_union_map_alloc(dim, uset->table.n);
3221 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3222 &coefficients_entry, &res) < 0)
3223 goto error;
3225 isl_union_set_free(uset);
3226 return res;
3227 error:
3228 isl_union_set_free(uset);
3229 isl_union_set_free(res);
3230 return NULL;
3233 static isl_stat solutions_entry(void **entry, void *user)
3235 isl_set *set = *entry;
3236 isl_union_set **res = user;
3238 set = isl_set_copy(set);
3239 set = isl_set_from_basic_set(isl_set_solutions(set));
3240 if (!*res)
3241 *res = isl_union_set_from_set(set);
3242 else
3243 *res = isl_union_set_add_set(*res, set);
3245 if (!*res)
3246 return isl_stat_error;
3248 return isl_stat_ok;
3251 __isl_give isl_union_set *isl_union_set_solutions(
3252 __isl_take isl_union_set *uset)
3254 isl_union_set *res = NULL;
3256 if (!uset)
3257 return NULL;
3259 if (uset->table.n == 0) {
3260 res = isl_union_set_empty(isl_union_set_get_space(uset));
3261 isl_union_set_free(uset);
3262 return res;
3265 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3266 &solutions_entry, &res) < 0)
3267 goto error;
3269 isl_union_set_free(uset);
3270 return res;
3271 error:
3272 isl_union_set_free(uset);
3273 isl_union_set_free(res);
3274 return NULL;
3277 /* Is the domain space of "map" equal to "space"?
3279 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3281 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3282 space, isl_dim_out);
3285 /* Is the range space of "map" equal to "space"?
3287 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3289 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3290 space, isl_dim_out);
3293 /* Is the set space of "map" equal to "space"?
3295 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3297 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3298 space, isl_dim_out);
3301 /* Internal data structure for preimage_pw_multi_aff.
3303 * "pma" is the function under which the preimage should be taken.
3304 * "space" is the space of "pma".
3305 * "res" collects the results.
3306 * "fn" computes the preimage for a given map.
3307 * "match" returns true if "fn" can be called.
3309 struct isl_union_map_preimage_data {
3310 isl_space *space;
3311 isl_pw_multi_aff *pma;
3312 isl_union_map *res;
3313 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3314 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3315 __isl_take isl_pw_multi_aff *pma);
3318 /* Call data->fn to compute the preimage of the domain or range of *entry
3319 * under the function represented by data->pma, provided the domain/range
3320 * space of *entry matches the target space of data->pma
3321 * (as given by data->match), and add the result to data->res.
3323 static isl_stat preimage_entry(void **entry, void *user)
3325 int m;
3326 isl_map *map = *entry;
3327 struct isl_union_map_preimage_data *data = user;
3328 isl_bool empty;
3330 m = data->match(map, data->space);
3331 if (m < 0)
3332 return isl_stat_error;
3333 if (!m)
3334 return isl_stat_ok;
3336 map = isl_map_copy(map);
3337 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3339 empty = isl_map_is_empty(map);
3340 if (empty < 0 || empty) {
3341 isl_map_free(map);
3342 return empty < 0 ? isl_stat_error : isl_stat_ok;
3345 data->res = isl_union_map_add_map(data->res, map);
3347 return isl_stat_ok;
3350 /* Compute the preimage of the domain or range of "umap" under the function
3351 * represented by "pma".
3352 * In other words, plug in "pma" in the domain or range of "umap".
3353 * The function "fn" performs the actual preimage computation on a map,
3354 * while "match" determines to which maps the function should be applied.
3356 static __isl_give isl_union_map *preimage_pw_multi_aff(
3357 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3358 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3359 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3360 __isl_take isl_pw_multi_aff *pma))
3362 isl_ctx *ctx;
3363 isl_space *space;
3364 struct isl_union_map_preimage_data data;
3366 umap = isl_union_map_align_params(umap,
3367 isl_pw_multi_aff_get_space(pma));
3368 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3370 if (!umap || !pma)
3371 goto error;
3373 ctx = isl_union_map_get_ctx(umap);
3374 space = isl_union_map_get_space(umap);
3375 data.space = isl_pw_multi_aff_get_space(pma);
3376 data.pma = pma;
3377 data.res = isl_union_map_alloc(space, umap->table.n);
3378 data.match = match;
3379 data.fn = fn;
3380 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3381 &data) < 0)
3382 data.res = isl_union_map_free(data.res);
3384 isl_space_free(data.space);
3385 isl_union_map_free(umap);
3386 isl_pw_multi_aff_free(pma);
3387 return data.res;
3388 error:
3389 isl_union_map_free(umap);
3390 isl_pw_multi_aff_free(pma);
3391 return NULL;
3394 /* Compute the preimage of the domain of "umap" under the function
3395 * represented by "pma".
3396 * In other words, plug in "pma" in the domain of "umap".
3397 * The result contains maps that live in the same spaces as the maps of "umap"
3398 * with domain space equal to the target space of "pma",
3399 * except that the domain has been replaced by the domain space of "pma".
3401 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3402 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3404 return preimage_pw_multi_aff(umap, pma, &domain_match,
3405 &isl_map_preimage_domain_pw_multi_aff);
3408 /* Compute the preimage of the range of "umap" under the function
3409 * represented by "pma".
3410 * In other words, plug in "pma" in the range of "umap".
3411 * The result contains maps that live in the same spaces as the maps of "umap"
3412 * with range space equal to the target space of "pma",
3413 * except that the range has been replaced by the domain space of "pma".
3415 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3416 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3418 return preimage_pw_multi_aff(umap, pma, &range_match,
3419 &isl_map_preimage_range_pw_multi_aff);
3422 /* Compute the preimage of "uset" under the function represented by "pma".
3423 * In other words, plug in "pma" in "uset".
3424 * The result contains sets that live in the same spaces as the sets of "uset"
3425 * with space equal to the target space of "pma",
3426 * except that the space has been replaced by the domain space of "pma".
3428 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3429 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3431 return preimage_pw_multi_aff(uset, pma, &set_match,
3432 &isl_set_preimage_pw_multi_aff);
3435 /* Compute the preimage of the domain of "umap" under the function
3436 * represented by "ma".
3437 * In other words, plug in "ma" in the domain of "umap".
3438 * The result contains maps that live in the same spaces as the maps of "umap"
3439 * with domain space equal to the target space of "ma",
3440 * except that the domain has been replaced by the domain space of "ma".
3442 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3443 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3445 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3446 isl_pw_multi_aff_from_multi_aff(ma));
3449 /* Compute the preimage of the range of "umap" under the function
3450 * represented by "ma".
3451 * In other words, plug in "ma" in the range of "umap".
3452 * The result contains maps that live in the same spaces as the maps of "umap"
3453 * with range space equal to the target space of "ma",
3454 * except that the range has been replaced by the domain space of "ma".
3456 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3457 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3459 return isl_union_map_preimage_range_pw_multi_aff(umap,
3460 isl_pw_multi_aff_from_multi_aff(ma));
3463 /* Compute the preimage of "uset" under the function represented by "ma".
3464 * In other words, plug in "ma" in "uset".
3465 * The result contains sets that live in the same spaces as the sets of "uset"
3466 * with space equal to the target space of "ma",
3467 * except that the space has been replaced by the domain space of "ma".
3469 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3470 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3472 return isl_union_set_preimage_pw_multi_aff(uset,
3473 isl_pw_multi_aff_from_multi_aff(ma));
3476 /* Internal data structure for preimage_multi_pw_aff.
3478 * "mpa" is the function under which the preimage should be taken.
3479 * "space" is the space of "mpa".
3480 * "res" collects the results.
3481 * "fn" computes the preimage for a given map.
3482 * "match" returns true if "fn" can be called.
3484 struct isl_union_map_preimage_mpa_data {
3485 isl_space *space;
3486 isl_multi_pw_aff *mpa;
3487 isl_union_map *res;
3488 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3489 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3490 __isl_take isl_multi_pw_aff *mpa);
3493 /* Call data->fn to compute the preimage of the domain or range of *entry
3494 * under the function represented by data->mpa, provided the domain/range
3495 * space of *entry matches the target space of data->mpa
3496 * (as given by data->match), and add the result to data->res.
3498 static isl_stat preimage_mpa_entry(void **entry, void *user)
3500 int m;
3501 isl_map *map = *entry;
3502 struct isl_union_map_preimage_mpa_data *data = user;
3503 isl_bool empty;
3505 m = data->match(map, data->space);
3506 if (m < 0)
3507 return isl_stat_error;
3508 if (!m)
3509 return isl_stat_ok;
3511 map = isl_map_copy(map);
3512 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3514 empty = isl_map_is_empty(map);
3515 if (empty < 0 || empty) {
3516 isl_map_free(map);
3517 return empty < 0 ? isl_stat_error : isl_stat_ok;
3520 data->res = isl_union_map_add_map(data->res, map);
3522 return isl_stat_ok;
3525 /* Compute the preimage of the domain or range of "umap" under the function
3526 * represented by "mpa".
3527 * In other words, plug in "mpa" in the domain or range of "umap".
3528 * The function "fn" performs the actual preimage computation on a map,
3529 * while "match" determines to which maps the function should be applied.
3531 static __isl_give isl_union_map *preimage_multi_pw_aff(
3532 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3533 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3534 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3535 __isl_take isl_multi_pw_aff *mpa))
3537 isl_ctx *ctx;
3538 isl_space *space;
3539 struct isl_union_map_preimage_mpa_data data;
3541 umap = isl_union_map_align_params(umap,
3542 isl_multi_pw_aff_get_space(mpa));
3543 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3545 if (!umap || !mpa)
3546 goto error;
3548 ctx = isl_union_map_get_ctx(umap);
3549 space = isl_union_map_get_space(umap);
3550 data.space = isl_multi_pw_aff_get_space(mpa);
3551 data.mpa = mpa;
3552 data.res = isl_union_map_alloc(space, umap->table.n);
3553 data.match = match;
3554 data.fn = fn;
3555 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3556 &data) < 0)
3557 data.res = isl_union_map_free(data.res);
3559 isl_space_free(data.space);
3560 isl_union_map_free(umap);
3561 isl_multi_pw_aff_free(mpa);
3562 return data.res;
3563 error:
3564 isl_union_map_free(umap);
3565 isl_multi_pw_aff_free(mpa);
3566 return NULL;
3569 /* Compute the preimage of the domain of "umap" under the function
3570 * represented by "mpa".
3571 * In other words, plug in "mpa" in the domain of "umap".
3572 * The result contains maps that live in the same spaces as the maps of "umap"
3573 * with domain space equal to the target space of "mpa",
3574 * except that the domain has been replaced by the domain space of "mpa".
3576 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3577 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3579 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3580 &isl_map_preimage_domain_multi_pw_aff);
3583 /* Internal data structure for preimage_upma.
3585 * "umap" is the map of which the preimage should be computed.
3586 * "res" collects the results.
3587 * "fn" computes the preimage for a given piecewise multi-affine function.
3589 struct isl_union_map_preimage_upma_data {
3590 isl_union_map *umap;
3591 isl_union_map *res;
3592 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3593 __isl_take isl_pw_multi_aff *pma);
3596 /* Call data->fn to compute the preimage of the domain or range of data->umap
3597 * under the function represented by pma and add the result to data->res.
3599 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3601 struct isl_union_map_preimage_upma_data *data = user;
3602 isl_union_map *umap;
3604 umap = isl_union_map_copy(data->umap);
3605 umap = data->fn(umap, pma);
3606 data->res = isl_union_map_union(data->res, umap);
3608 return data->res ? isl_stat_ok : isl_stat_error;
3611 /* Compute the preimage of the domain or range of "umap" under the function
3612 * represented by "upma".
3613 * In other words, plug in "upma" in the domain or range of "umap".
3614 * The function "fn" performs the actual preimage computation
3615 * on a piecewise multi-affine function.
3617 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3618 __isl_take isl_union_map *umap,
3619 __isl_take isl_union_pw_multi_aff *upma,
3620 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3621 __isl_take isl_pw_multi_aff *pma))
3623 struct isl_union_map_preimage_upma_data data;
3625 data.umap = umap;
3626 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3627 data.fn = fn;
3628 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3629 &preimage_upma, &data) < 0)
3630 data.res = isl_union_map_free(data.res);
3632 isl_union_map_free(umap);
3633 isl_union_pw_multi_aff_free(upma);
3635 return data.res;
3638 /* Compute the preimage of the domain of "umap" under the function
3639 * represented by "upma".
3640 * In other words, plug in "upma" in the domain of "umap".
3641 * The result contains maps that live in the same spaces as the maps of "umap"
3642 * with domain space equal to one of the target spaces of "upma",
3643 * except that the domain has been replaced by one of the domain spaces that
3644 * correspond to that target space of "upma".
3646 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3647 __isl_take isl_union_map *umap,
3648 __isl_take isl_union_pw_multi_aff *upma)
3650 return preimage_union_pw_multi_aff(umap, upma,
3651 &isl_union_map_preimage_domain_pw_multi_aff);
3654 /* Compute the preimage of the range of "umap" under the function
3655 * represented by "upma".
3656 * In other words, plug in "upma" in the range of "umap".
3657 * The result contains maps that live in the same spaces as the maps of "umap"
3658 * with range space equal to one of the target spaces of "upma",
3659 * except that the range has been replaced by one of the domain spaces that
3660 * correspond to that target space of "upma".
3662 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3663 __isl_take isl_union_map *umap,
3664 __isl_take isl_union_pw_multi_aff *upma)
3666 return preimage_union_pw_multi_aff(umap, upma,
3667 &isl_union_map_preimage_range_pw_multi_aff);
3670 /* Compute the preimage of "uset" under the function represented by "upma".
3671 * In other words, plug in "upma" in the range of "uset".
3672 * The result contains sets that live in the same spaces as the sets of "uset"
3673 * with space equal to one of the target spaces of "upma",
3674 * except that the space has been replaced by one of the domain spaces that
3675 * correspond to that target space of "upma".
3677 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3678 __isl_take isl_union_set *uset,
3679 __isl_take isl_union_pw_multi_aff *upma)
3681 return preimage_union_pw_multi_aff(uset, upma,
3682 &isl_union_set_preimage_pw_multi_aff);
3685 /* Reset the user pointer on all identifiers of parameters and tuples
3686 * of the spaces of "umap".
3688 __isl_give isl_union_map *isl_union_map_reset_user(
3689 __isl_take isl_union_map *umap)
3691 umap = isl_union_map_cow(umap);
3692 if (!umap)
3693 return NULL;
3694 umap->dim = isl_space_reset_user(umap->dim);
3695 if (!umap->dim)
3696 return isl_union_map_free(umap);
3697 return total(umap, &isl_map_reset_user);
3700 /* Reset the user pointer on all identifiers of parameters and tuples
3701 * of the spaces of "uset".
3703 __isl_give isl_union_set *isl_union_set_reset_user(
3704 __isl_take isl_union_set *uset)
3706 return isl_union_map_reset_user(uset);
3709 /* Remove all existentially quantified variables and integer divisions
3710 * from "umap" using Fourier-Motzkin elimination.
3712 __isl_give isl_union_map *isl_union_map_remove_divs(
3713 __isl_take isl_union_map *umap)
3715 return total(umap, &isl_map_remove_divs);
3718 /* Remove all existentially quantified variables and integer divisions
3719 * from "uset" using Fourier-Motzkin elimination.
3721 __isl_give isl_union_set *isl_union_set_remove_divs(
3722 __isl_take isl_union_set *uset)
3724 return isl_union_map_remove_divs(uset);
3727 /* Internal data structure for isl_union_map_project_out.
3728 * "type", "first" and "n" are the arguments for the isl_map_project_out
3729 * call.
3730 * "res" collects the results.
3732 struct isl_union_map_project_out_data {
3733 enum isl_dim_type type;
3734 unsigned first;
3735 unsigned n;
3737 isl_union_map *res;
3740 /* Turn the data->n dimensions of type data->type, starting at data->first
3741 * into existentially quantified variables and add the result to data->res.
3743 static isl_stat project_out(__isl_take isl_map *map, void *user)
3745 struct isl_union_map_project_out_data *data = user;
3747 map = isl_map_project_out(map, data->type, data->first, data->n);
3748 data->res = isl_union_map_add_map(data->res, map);
3750 return isl_stat_ok;
3753 /* Turn the "n" dimensions of type "type", starting at "first"
3754 * into existentially quantified variables.
3755 * Since the space of an isl_union_map only contains parameters,
3756 * type is required to be equal to isl_dim_param.
3758 __isl_give isl_union_map *isl_union_map_project_out(
3759 __isl_take isl_union_map *umap,
3760 enum isl_dim_type type, unsigned first, unsigned n)
3762 isl_space *space;
3763 struct isl_union_map_project_out_data data = { type, first, n };
3765 if (!umap)
3766 return NULL;
3768 if (type != isl_dim_param)
3769 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3770 "can only project out parameters",
3771 return isl_union_map_free(umap));
3773 space = isl_union_map_get_space(umap);
3774 space = isl_space_drop_dims(space, type, first, n);
3775 data.res = isl_union_map_empty(space);
3776 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3777 data.res = isl_union_map_free(data.res);
3779 isl_union_map_free(umap);
3781 return data.res;
3784 /* Project out all parameters from "umap" by existentially quantifying
3785 * over them.
3787 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3788 __isl_take isl_union_map *umap)
3790 isl_size n;
3792 n = isl_union_map_dim(umap, isl_dim_param);
3793 if (n < 0)
3794 return isl_union_map_free(umap);
3795 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3798 /* Turn the "n" dimensions of type "type", starting at "first"
3799 * into existentially quantified variables.
3800 * Since the space of an isl_union_set only contains parameters,
3801 * "type" is required to be equal to isl_dim_param.
3803 __isl_give isl_union_set *isl_union_set_project_out(
3804 __isl_take isl_union_set *uset,
3805 enum isl_dim_type type, unsigned first, unsigned n)
3807 return isl_union_map_project_out(uset, type, first, n);
3810 /* Project out all parameters from "uset" by existentially quantifying
3811 * over them.
3813 __isl_give isl_union_set *isl_union_set_project_out_all_params(
3814 __isl_take isl_union_set *uset)
3816 return uset_from_umap(
3817 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3820 /* Internal data structure for isl_union_map_involves_dims.
3821 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3823 struct isl_union_map_involves_dims_data {
3824 unsigned first;
3825 unsigned n;
3828 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3830 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3832 struct isl_union_map_involves_dims_data *data = user;
3833 isl_bool involves;
3835 involves = isl_map_involves_dims(map,
3836 isl_dim_param, data->first, data->n);
3837 return isl_bool_not(involves);
3840 /* Does "umap" involve any of the n parameters starting at first?
3841 * "type" is required to be set to isl_dim_param.
3843 * "umap" involves any of those parameters if any of its maps
3844 * involve the parameters. In other words, "umap" does not
3845 * involve any of the parameters if all its maps to not
3846 * involve the parameters.
3848 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3849 enum isl_dim_type type, unsigned first, unsigned n)
3851 struct isl_union_map_involves_dims_data data = { first, n };
3852 isl_bool excludes;
3854 if (type != isl_dim_param)
3855 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3856 "can only reference parameters", return isl_bool_error);
3858 excludes = union_map_forall_user(umap, &map_excludes, &data);
3860 return isl_bool_not(excludes);
3863 /* Internal data structure for isl_union_map_reset_range_space.
3864 * "range" is the space from which to set the range space.
3865 * "res" collects the results.
3867 struct isl_union_map_reset_range_space_data {
3868 isl_space *range;
3869 isl_union_map *res;
3872 /* Replace the range space of "map" by the range space of data->range and
3873 * add the result to data->res.
3875 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3877 struct isl_union_map_reset_range_space_data *data = user;
3878 isl_space *space;
3880 space = isl_map_get_space(map);
3881 space = isl_space_domain(space);
3882 space = isl_space_extend_domain_with_range(space,
3883 isl_space_copy(data->range));
3884 map = isl_map_reset_space(map, space);
3885 data->res = isl_union_map_add_map(data->res, map);
3887 return data->res ? isl_stat_ok : isl_stat_error;
3890 /* Replace the range space of all the maps in "umap" by
3891 * the range space of "space".
3893 * This assumes that all maps have the same output dimension.
3894 * This function should therefore not be made publicly available.
3896 * Since the spaces of the maps change, so do their hash value.
3897 * We therefore need to create a new isl_union_map.
3899 __isl_give isl_union_map *isl_union_map_reset_range_space(
3900 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3902 struct isl_union_map_reset_range_space_data data = { space };
3904 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3905 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3906 data.res = isl_union_map_free(data.res);
3908 isl_space_free(space);
3909 isl_union_map_free(umap);
3910 return data.res;
3913 /* Check that "umap" and "space" have the same number of parameters.
3915 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3916 __isl_keep isl_space *space)
3918 isl_size dim1, dim2;
3920 dim1 = isl_union_map_dim(umap, isl_dim_param);
3921 dim2 = isl_space_dim(space, isl_dim_param);
3922 if (dim1 < 0 || dim2 < 0)
3923 return isl_stat_error;
3924 if (dim1 == dim2)
3925 return isl_stat_ok;
3926 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3927 "number of parameters does not match", return isl_stat_error);
3930 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3931 * "space" is the target space.
3932 * "res" collects the results.
3934 struct isl_union_map_reset_params_data {
3935 isl_space *space;
3936 isl_union_map *res;
3939 /* Replace the parameters of "map" by those of data->space and
3940 * add the result to data->res.
3942 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3944 struct isl_union_map_reset_params_data *data = user;
3945 isl_space *space;
3947 space = isl_map_get_space(map);
3948 space = isl_space_replace_params(space, data->space);
3949 map = isl_map_reset_equal_dim_space(map, space);
3950 data->res = isl_union_map_add_map(data->res, map);
3952 return data->res ? isl_stat_ok : isl_stat_error;
3955 /* Replace the space of "umap" by "space", without modifying
3956 * the dimension of "umap", i.e., the number of parameters of "umap".
3958 * Since the hash values of the maps in the union map depend
3959 * on the parameters, a new union map needs to be constructed.
3961 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3962 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3964 struct isl_union_map_reset_params_data data = { space };
3965 isl_bool equal;
3966 isl_space *umap_space;
3968 umap_space = isl_union_map_peek_space(umap);
3969 equal = isl_space_is_equal(umap_space, space);
3970 if (equal < 0)
3971 goto error;
3972 if (equal) {
3973 isl_space_free(space);
3974 return umap;
3976 if (check_union_map_space_equal_dim(umap, space) < 0)
3977 goto error;
3979 data.res = isl_union_map_empty(isl_space_copy(space));
3980 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3981 data.res = isl_union_map_free(data.res);
3983 isl_space_free(space);
3984 isl_union_map_free(umap);
3985 return data.res;
3986 error:
3987 isl_union_map_free(umap);
3988 isl_space_free(space);
3989 return NULL;
3992 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3993 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3994 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3995 * to the domain and the range of each map.
3996 * "res" collects the results.
3998 struct isl_union_order_at_data {
3999 isl_multi_union_pw_aff *mupa;
4000 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4001 __isl_take isl_multi_pw_aff *mpa2);
4002 isl_union_map *res;
4005 /* Intersect "map" with the result of applying data->order to
4006 * the functions in data->mupa that apply to the domain and the range
4007 * of "map" and add the result to data->res.
4009 static isl_stat order_at(__isl_take isl_map *map, void *user)
4011 struct isl_union_order_at_data *data = user;
4012 isl_space *space;
4013 isl_multi_pw_aff *mpa1, *mpa2;
4014 isl_map *order;
4016 space = isl_space_domain(isl_map_get_space(map));
4017 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4018 space = isl_space_range(isl_map_get_space(map));
4019 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4020 order = data->order(mpa1, mpa2);
4021 map = isl_map_intersect(map, order);
4022 data->res = isl_union_map_add_map(data->res, map);
4024 return data->res ? isl_stat_ok : isl_stat_error;
4027 /* If "mupa" has a non-trivial explicit domain, then intersect
4028 * domain and range of "umap" with this explicit domain.
4029 * If the explicit domain only describes constraints on the parameters,
4030 * then the intersection only needs to be performed once.
4032 static __isl_give isl_union_map *intersect_explicit_domain(
4033 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4035 isl_bool non_trivial, is_params;
4036 isl_union_set *dom;
4038 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4039 if (non_trivial < 0)
4040 return isl_union_map_free(umap);
4041 if (!non_trivial)
4042 return umap;
4043 mupa = isl_multi_union_pw_aff_copy(mupa);
4044 dom = isl_multi_union_pw_aff_domain(mupa);
4045 is_params = isl_union_set_is_params(dom);
4046 if (is_params < 0) {
4047 isl_union_set_free(dom);
4048 return isl_union_map_free(umap);
4050 if (is_params) {
4051 isl_set *set;
4053 set = isl_union_set_params(dom);
4054 umap = isl_union_map_intersect_params(umap, set);
4055 return umap;
4057 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4058 umap = isl_union_map_intersect_range(umap, dom);
4059 return umap;
4062 /* Intersect each map in "umap" with the result of calling "order"
4063 * on the functions is "mupa" that apply to the domain and the range
4064 * of the map.
4066 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4067 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4068 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4069 __isl_take isl_multi_pw_aff *mpa2))
4071 struct isl_union_order_at_data data;
4073 umap = isl_union_map_align_params(umap,
4074 isl_multi_union_pw_aff_get_space(mupa));
4075 mupa = isl_multi_union_pw_aff_align_params(mupa,
4076 isl_union_map_get_space(umap));
4077 umap = intersect_explicit_domain(umap, mupa);
4078 data.mupa = mupa;
4079 data.order = order;
4080 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4081 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4082 data.res = isl_union_map_free(data.res);
4084 isl_multi_union_pw_aff_free(mupa);
4085 isl_union_map_free(umap);
4086 return data.res;
4089 /* Return the subset of "umap" where the domain and the range
4090 * have equal "mupa" values.
4092 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4093 __isl_take isl_union_map *umap,
4094 __isl_take isl_multi_union_pw_aff *mupa)
4096 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4097 &isl_multi_pw_aff_eq_map);
4100 /* Return the subset of "umap" where the domain has a lexicographically
4101 * smaller "mupa" value than the range.
4103 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4104 __isl_take isl_union_map *umap,
4105 __isl_take isl_multi_union_pw_aff *mupa)
4107 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4108 &isl_multi_pw_aff_lex_lt_map);
4111 /* Return the subset of "umap" where the domain has a lexicographically
4112 * greater "mupa" value than the range.
4114 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4115 __isl_take isl_union_map *umap,
4116 __isl_take isl_multi_union_pw_aff *mupa)
4118 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4119 &isl_multi_pw_aff_lex_gt_map);
4122 /* Return the union of the elements in the list "list".
4124 __isl_give isl_union_set *isl_union_set_list_union(
4125 __isl_take isl_union_set_list *list)
4127 int i;
4128 isl_size n;
4129 isl_ctx *ctx;
4130 isl_space *space;
4131 isl_union_set *res;
4133 n = isl_union_set_list_n_union_set(list);
4134 if (n < 0)
4135 goto error;
4137 ctx = isl_union_set_list_get_ctx(list);
4138 space = isl_space_params_alloc(ctx, 0);
4139 res = isl_union_set_empty(space);
4141 for (i = 0; i < n; ++i) {
4142 isl_union_set *uset_i;
4144 uset_i = isl_union_set_list_get_union_set(list, i);
4145 res = isl_union_set_union(res, uset_i);
4148 isl_union_set_list_free(list);
4149 return res;
4150 error:
4151 isl_union_set_list_free(list);
4152 return NULL;
4155 /* Update *hash with the hash value of "map".
4157 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4159 uint32_t *hash = user;
4160 uint32_t map_hash;
4162 map_hash = isl_map_get_hash(map);
4163 isl_hash_hash(*hash, map_hash);
4165 isl_map_free(map);
4166 return isl_stat_ok;
4169 /* Return a hash value that digests "umap".
4171 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4173 uint32_t hash;
4175 if (!umap)
4176 return 0;
4178 hash = isl_hash_init();
4179 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4180 return 0;
4182 return hash;
4185 /* Return a hash value that digests "uset".
4187 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4189 return isl_union_map_get_hash(uset);
4192 /* Add the number of basic sets in "set" to "n".
4194 static isl_stat add_n(__isl_take isl_set *set, void *user)
4196 int *n = user;
4197 isl_size set_n;
4199 set_n = isl_set_n_basic_set(set);
4200 *n += set_n;
4201 isl_set_free(set);
4203 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4206 /* Return the total number of basic sets in "uset".
4208 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4210 int n = 0;
4212 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4213 return -1;
4215 return n;
4218 /* Add the basic sets in "set" to "list".
4220 static isl_stat add_list(__isl_take isl_set *set, void *user)
4222 isl_basic_set_list **list = user;
4223 isl_basic_set_list *list_i;
4225 list_i = isl_set_get_basic_set_list(set);
4226 *list = isl_basic_set_list_concat(*list, list_i);
4227 isl_set_free(set);
4229 if (!*list)
4230 return isl_stat_error;
4231 return isl_stat_ok;
4234 /* Return a list containing all the basic sets in "uset".
4236 * First construct a list of the appropriate size and
4237 * then add all the elements.
4239 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4240 __isl_keep isl_union_set *uset)
4242 int n;
4243 isl_ctx *ctx;
4244 isl_basic_set_list *list;
4246 if (!uset)
4247 return NULL;
4248 ctx = isl_union_set_get_ctx(uset);
4249 n = isl_union_set_n_basic_set(uset);
4250 if (n < 0)
4251 return NULL;
4252 list = isl_basic_set_list_alloc(ctx, n);
4253 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4254 list = isl_basic_set_list_free(list);
4256 return list;
4259 /* Internal data structure for isl_union_map_remove_map_if.
4260 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4262 struct isl_union_map_remove_map_if_data {
4263 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4264 void *user;
4267 /* isl_un_op_control filter that negates the result of data->fn
4268 * called on "map".
4270 static isl_bool not(__isl_keep isl_map *map, void *user)
4272 struct isl_union_map_remove_map_if_data *data = user;
4274 return isl_bool_not(data->fn(map, data->user));
4277 /* Dummy isl_un_op_control transformation callback that
4278 * simply returns the input.
4280 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4282 return map;
4285 /* Call "fn" on every map in "umap" and remove those maps
4286 * for which the callback returns true.
4288 * Use un_op to keep only those maps that are not filtered out,
4289 * applying an identity transformation on them.
4291 __isl_give isl_union_map *isl_union_map_remove_map_if(
4292 __isl_take isl_union_map *umap,
4293 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4295 struct isl_union_map_remove_map_if_data data = { fn, user };
4296 struct isl_un_op_control control = {
4297 .filter = &not,
4298 .filter_user = &data,
4299 .fn_map = &map_id,
4301 return un_op(umap, &control);