add isl_local_space_find_dim_by_name
[isl.git] / isl_union_map.c
blob8d8fec2ce86d7b1308debc65417ba429488058f5
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
12 #define ISL_DIM_H
13 #include <isl_map_private.h>
14 #include <isl/ctx.h>
15 #include <isl/hash.h>
16 #include <isl/aff.h>
17 #include <isl/map.h>
18 #include <isl/set.h>
19 #include <isl_space_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl/union_set.h>
22 #include <isl/deprecated/union_map_int.h>
24 /* Return the number of parameters of "umap", where "type"
25 * is required to be set to isl_dim_param.
27 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
28 enum isl_dim_type type)
30 if (!umap)
31 return 0;
33 if (type != isl_dim_param)
34 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
35 "can only reference parameters", return 0);
37 return isl_space_dim(umap->dim, type);
40 /* Return the number of parameters of "uset", where "type"
41 * is required to be set to isl_dim_param.
43 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
44 enum isl_dim_type type)
46 return isl_union_map_dim(uset, type);
49 /* Return the id of the specified dimension.
51 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
52 enum isl_dim_type type, unsigned pos)
54 if (!umap)
55 return NULL;
57 if (type != isl_dim_param)
58 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
59 "can only reference parameters", return NULL);
61 return isl_space_get_dim_id(umap->dim, type, pos);
64 /* Is this union set a parameter domain?
66 int isl_union_set_is_params(__isl_keep isl_union_set *uset)
68 isl_set *set;
69 int params;
71 if (!uset)
72 return -1;
73 if (uset->table.n != 1)
74 return 0;
76 set = isl_set_from_union_set(isl_union_set_copy(uset));
77 params = isl_set_is_params(set);
78 isl_set_free(set);
79 return params;
82 static __isl_give isl_union_map *isl_union_map_alloc(
83 __isl_take isl_space *space, int size)
85 isl_union_map *umap;
87 space = isl_space_params(space);
88 if (!space)
89 return NULL;
91 umap = isl_calloc_type(space->ctx, isl_union_map);
92 if (!umap) {
93 isl_space_free(space);
94 return NULL;
97 umap->ref = 1;
98 umap->dim = space;
99 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
100 return isl_union_map_free(umap);
102 return umap;
105 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
107 return isl_union_map_alloc(dim, 16);
110 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
112 return isl_union_map_empty(dim);
115 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
117 return umap ? umap->dim->ctx : NULL;
120 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
122 return uset ? uset->dim->ctx : NULL;
125 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
127 if (!umap)
128 return NULL;
129 return isl_space_copy(umap->dim);
132 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
134 return isl_union_map_get_space(uset);
137 static int free_umap_entry(void **entry, void *user)
139 isl_map *map = *entry;
140 isl_map_free(map);
141 return 0;
144 static int add_map(__isl_take isl_map *map, void *user)
146 isl_union_map **umap = (isl_union_map **)user;
148 *umap = isl_union_map_add_map(*umap, map);
150 return 0;
153 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
155 isl_union_map *dup;
157 if (!umap)
158 return NULL;
160 dup = isl_union_map_empty(isl_space_copy(umap->dim));
161 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
162 goto error;
163 return dup;
164 error:
165 isl_union_map_free(dup);
166 return NULL;
169 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
171 if (!umap)
172 return NULL;
174 if (umap->ref == 1)
175 return umap;
176 umap->ref--;
177 return isl_union_map_dup(umap);
180 struct isl_union_align {
181 isl_reordering *exp;
182 isl_union_map *res;
185 static int align_entry(void **entry, void *user)
187 isl_map *map = *entry;
188 isl_reordering *exp;
189 struct isl_union_align *data = user;
191 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
192 isl_map_get_space(map));
194 data->res = isl_union_map_add_map(data->res,
195 isl_map_realign(isl_map_copy(map), exp));
197 return 0;
200 /* Align the parameters of umap along those of model.
201 * The result has the parameters of model first, in the same order
202 * as they appear in model, followed by any remaining parameters of
203 * umap that do not appear in model.
205 __isl_give isl_union_map *isl_union_map_align_params(
206 __isl_take isl_union_map *umap, __isl_take isl_space *model)
208 struct isl_union_align data = { NULL, NULL };
210 if (!umap || !model)
211 goto error;
213 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
214 isl_space_free(model);
215 return umap;
218 model = isl_space_params(model);
219 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
220 if (!data.exp)
221 goto error;
223 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
224 umap->table.n);
225 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
226 &align_entry, &data) < 0)
227 goto error;
229 isl_reordering_free(data.exp);
230 isl_union_map_free(umap);
231 isl_space_free(model);
232 return data.res;
233 error:
234 isl_reordering_free(data.exp);
235 isl_union_map_free(umap);
236 isl_union_map_free(data.res);
237 isl_space_free(model);
238 return NULL;
241 __isl_give isl_union_set *isl_union_set_align_params(
242 __isl_take isl_union_set *uset, __isl_take isl_space *model)
244 return isl_union_map_align_params(uset, model);
247 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
248 __isl_take isl_union_map *umap2)
250 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
251 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
253 umap1 = isl_union_map_cow(umap1);
255 if (!umap1 || !umap2)
256 goto error;
258 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
259 goto error;
261 isl_union_map_free(umap2);
263 return umap1;
264 error:
265 isl_union_map_free(umap1);
266 isl_union_map_free(umap2);
267 return NULL;
270 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
271 __isl_take isl_union_set *uset2)
273 return isl_union_map_union(uset1, uset2);
276 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
278 if (!umap)
279 return NULL;
281 umap->ref++;
282 return umap;
285 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
287 return isl_union_map_copy(uset);
290 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
292 if (!umap)
293 return NULL;
295 if (--umap->ref > 0)
296 return NULL;
298 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
299 &free_umap_entry, NULL);
300 isl_hash_table_clear(&umap->table);
301 isl_space_free(umap->dim);
302 free(umap);
303 return NULL;
306 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
308 return isl_union_map_free(uset);
311 static int has_dim(const void *entry, const void *val)
313 isl_map *map = (isl_map *)entry;
314 isl_space *dim = (isl_space *)val;
316 return isl_space_is_equal(map->dim, dim);
319 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
320 __isl_take isl_map *map)
322 uint32_t hash;
323 struct isl_hash_table_entry *entry;
325 if (!map || !umap)
326 goto error;
328 if (isl_map_plain_is_empty(map)) {
329 isl_map_free(map);
330 return umap;
333 if (!isl_space_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
334 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
335 map = isl_map_align_params(map, isl_union_map_get_space(umap));
338 umap = isl_union_map_cow(umap);
340 if (!map || !umap)
341 goto error;
343 hash = isl_space_get_hash(map->dim);
344 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
345 &has_dim, map->dim, 1);
346 if (!entry)
347 goto error;
349 if (!entry->data)
350 entry->data = map;
351 else {
352 entry->data = isl_map_union(entry->data, isl_map_copy(map));
353 if (!entry->data)
354 goto error;
355 isl_map_free(map);
358 return umap;
359 error:
360 isl_map_free(map);
361 isl_union_map_free(umap);
362 return NULL;
365 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
366 __isl_take isl_set *set)
368 return isl_union_map_add_map(uset, (isl_map *)set);
371 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
373 isl_space *dim;
374 isl_union_map *umap;
376 if (!map)
377 return NULL;
379 dim = isl_map_get_space(map);
380 dim = isl_space_params(dim);
381 umap = isl_union_map_empty(dim);
382 umap = isl_union_map_add_map(umap, map);
384 return umap;
387 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
389 return isl_union_map_from_map((isl_map *)set);
392 __isl_give isl_union_map *isl_union_map_from_basic_map(
393 __isl_take isl_basic_map *bmap)
395 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
398 __isl_give isl_union_set *isl_union_set_from_basic_set(
399 __isl_take isl_basic_set *bset)
401 return isl_union_map_from_basic_map(bset);
404 struct isl_union_map_foreach_data
406 int (*fn)(__isl_take isl_map *map, void *user);
407 void *user;
410 static int call_on_copy(void **entry, void *user)
412 isl_map *map = *entry;
413 struct isl_union_map_foreach_data *data;
414 data = (struct isl_union_map_foreach_data *)user;
416 return data->fn(isl_map_copy(map), data->user);
419 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
421 return umap ? umap->table.n : 0;
424 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
426 return uset ? uset->table.n : 0;
429 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
430 int (*fn)(__isl_take isl_map *map, void *user), void *user)
432 struct isl_union_map_foreach_data data = { fn, user };
434 if (!umap)
435 return -1;
437 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
438 &call_on_copy, &data);
441 static int copy_map(void **entry, void *user)
443 isl_map *map = *entry;
444 isl_map **map_p = user;
446 *map_p = isl_map_copy(map);
448 return -1;
451 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
453 isl_ctx *ctx;
454 isl_map *map = NULL;
456 if (!umap)
457 return NULL;
458 ctx = isl_union_map_get_ctx(umap);
459 if (umap->table.n != 1)
460 isl_die(ctx, isl_error_invalid,
461 "union map needs to contain elements in exactly "
462 "one space", goto error);
464 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
466 isl_union_map_free(umap);
468 return map;
469 error:
470 isl_union_map_free(umap);
471 return NULL;
474 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
476 return isl_map_from_union_map(uset);
479 /* Extract the map in "umap" that lives in the given space (ignoring
480 * parameters).
482 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
483 __isl_take isl_space *space)
485 uint32_t hash;
486 struct isl_hash_table_entry *entry;
488 space = isl_space_drop_dims(space, isl_dim_param,
489 0, isl_space_dim(space, isl_dim_param));
490 space = isl_space_align_params(space, isl_union_map_get_space(umap));
491 if (!umap || !space)
492 goto error;
494 hash = isl_space_get_hash(space);
495 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
496 &has_dim, space, 0);
497 if (!entry)
498 return isl_map_empty(space);
499 isl_space_free(space);
500 return isl_map_copy(entry->data);
501 error:
502 isl_space_free(space);
503 return NULL;
506 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
507 __isl_take isl_space *dim)
509 return (isl_set *)isl_union_map_extract_map(uset, dim);
512 /* Check if umap contains a map in the given space.
514 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
515 __isl_keep isl_space *dim)
517 uint32_t hash;
518 struct isl_hash_table_entry *entry;
520 if (!umap || !dim)
521 return -1;
523 hash = isl_space_get_hash(dim);
524 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
525 &has_dim, dim, 0);
526 return !!entry;
529 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
530 __isl_keep isl_space *dim)
532 return isl_union_map_contains(uset, dim);
535 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
536 int (*fn)(__isl_take isl_set *set, void *user), void *user)
538 return isl_union_map_foreach_map(uset,
539 (int(*)(__isl_take isl_map *, void*))fn, user);
542 struct isl_union_set_foreach_point_data {
543 int (*fn)(__isl_take isl_point *pnt, void *user);
544 void *user;
547 static int foreach_point(__isl_take isl_set *set, void *user)
549 struct isl_union_set_foreach_point_data *data = user;
550 int r;
552 r = isl_set_foreach_point(set, data->fn, data->user);
553 isl_set_free(set);
555 return r;
558 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
559 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
561 struct isl_union_set_foreach_point_data data = { fn, user };
562 return isl_union_set_foreach_set(uset, &foreach_point, &data);
565 struct isl_union_map_gen_bin_data {
566 isl_union_map *umap2;
567 isl_union_map *res;
570 static int subtract_entry(void **entry, void *user)
572 struct isl_union_map_gen_bin_data *data = user;
573 uint32_t hash;
574 struct isl_hash_table_entry *entry2;
575 isl_map *map = *entry;
577 hash = isl_space_get_hash(map->dim);
578 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
579 hash, &has_dim, map->dim, 0);
580 map = isl_map_copy(map);
581 if (entry2) {
582 int empty;
583 map = isl_map_subtract(map, isl_map_copy(entry2->data));
585 empty = isl_map_is_empty(map);
586 if (empty < 0) {
587 isl_map_free(map);
588 return -1;
590 if (empty) {
591 isl_map_free(map);
592 return 0;
595 data->res = isl_union_map_add_map(data->res, map);
597 return 0;
600 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
601 __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
603 struct isl_union_map_gen_bin_data data = { NULL, NULL };
605 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
606 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
608 if (!umap1 || !umap2)
609 goto error;
611 data.umap2 = umap2;
612 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
613 umap1->table.n);
614 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
615 fn, &data) < 0)
616 goto error;
618 isl_union_map_free(umap1);
619 isl_union_map_free(umap2);
620 return data.res;
621 error:
622 isl_union_map_free(umap1);
623 isl_union_map_free(umap2);
624 isl_union_map_free(data.res);
625 return NULL;
628 __isl_give isl_union_map *isl_union_map_subtract(
629 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
631 return gen_bin_op(umap1, umap2, &subtract_entry);
634 __isl_give isl_union_set *isl_union_set_subtract(
635 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
637 return isl_union_map_subtract(uset1, uset2);
640 struct isl_union_map_gen_bin_set_data {
641 isl_set *set;
642 isl_union_map *res;
645 static int intersect_params_entry(void **entry, void *user)
647 struct isl_union_map_gen_bin_set_data *data = user;
648 isl_map *map = *entry;
649 int empty;
651 map = isl_map_copy(map);
652 map = isl_map_intersect_params(map, isl_set_copy(data->set));
654 empty = isl_map_is_empty(map);
655 if (empty < 0) {
656 isl_map_free(map);
657 return -1;
660 data->res = isl_union_map_add_map(data->res, map);
662 return 0;
665 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
666 __isl_take isl_set *set, int (*fn)(void **, void *))
668 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
670 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
671 set = isl_set_align_params(set, isl_union_map_get_space(umap));
673 if (!umap || !set)
674 goto error;
676 data.set = set;
677 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
678 umap->table.n);
679 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
680 fn, &data) < 0)
681 goto error;
683 isl_union_map_free(umap);
684 isl_set_free(set);
685 return data.res;
686 error:
687 isl_union_map_free(umap);
688 isl_set_free(set);
689 isl_union_map_free(data.res);
690 return NULL;
693 __isl_give isl_union_map *isl_union_map_intersect_params(
694 __isl_take isl_union_map *umap, __isl_take isl_set *set)
696 return gen_bin_set_op(umap, set, &intersect_params_entry);
699 __isl_give isl_union_set *isl_union_set_intersect_params(
700 __isl_take isl_union_set *uset, __isl_take isl_set *set)
702 return isl_union_map_intersect_params(uset, set);
705 static __isl_give isl_union_map *union_map_intersect_params(
706 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
708 return isl_union_map_intersect_params(umap,
709 isl_set_from_union_set(uset));
712 static __isl_give isl_union_map *union_map_gist_params(
713 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
715 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
718 struct isl_union_map_match_bin_data {
719 isl_union_map *umap2;
720 isl_union_map *res;
721 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
724 static int match_bin_entry(void **entry, void *user)
726 struct isl_union_map_match_bin_data *data = user;
727 uint32_t hash;
728 struct isl_hash_table_entry *entry2;
729 isl_map *map = *entry;
730 int empty;
732 hash = isl_space_get_hash(map->dim);
733 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
734 hash, &has_dim, map->dim, 0);
735 if (!entry2)
736 return 0;
738 map = isl_map_copy(map);
739 map = data->fn(map, isl_map_copy(entry2->data));
741 empty = isl_map_is_empty(map);
742 if (empty < 0) {
743 isl_map_free(map);
744 return -1;
746 if (empty) {
747 isl_map_free(map);
748 return 0;
751 data->res = isl_union_map_add_map(data->res, map);
753 return 0;
756 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
757 __isl_take isl_union_map *umap2,
758 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
760 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
762 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
763 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
765 if (!umap1 || !umap2)
766 goto error;
768 data.umap2 = umap2;
769 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
770 umap1->table.n);
771 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
772 &match_bin_entry, &data) < 0)
773 goto error;
775 isl_union_map_free(umap1);
776 isl_union_map_free(umap2);
777 return data.res;
778 error:
779 isl_union_map_free(umap1);
780 isl_union_map_free(umap2);
781 isl_union_map_free(data.res);
782 return NULL;
785 __isl_give isl_union_map *isl_union_map_intersect(
786 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
788 return match_bin_op(umap1, umap2, &isl_map_intersect);
791 /* Compute the intersection of the two union_sets.
792 * As a special case, if exactly one of the two union_sets
793 * is a parameter domain, then intersect the parameter domain
794 * of the other one with this set.
796 __isl_give isl_union_set *isl_union_set_intersect(
797 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
799 int p1, p2;
801 p1 = isl_union_set_is_params(uset1);
802 p2 = isl_union_set_is_params(uset2);
803 if (p1 < 0 || p2 < 0)
804 goto error;
805 if (!p1 && p2)
806 return union_map_intersect_params(uset1, uset2);
807 if (p1 && !p2)
808 return union_map_intersect_params(uset2, uset1);
809 return isl_union_map_intersect(uset1, uset2);
810 error:
811 isl_union_set_free(uset1);
812 isl_union_set_free(uset2);
813 return NULL;
816 static int gist_params_entry(void **entry, void *user)
818 struct isl_union_map_gen_bin_set_data *data = user;
819 isl_map *map = *entry;
820 int empty;
822 map = isl_map_copy(map);
823 map = isl_map_gist_params(map, isl_set_copy(data->set));
825 empty = isl_map_is_empty(map);
826 if (empty < 0) {
827 isl_map_free(map);
828 return -1;
831 data->res = isl_union_map_add_map(data->res, map);
833 return 0;
836 __isl_give isl_union_map *isl_union_map_gist_params(
837 __isl_take isl_union_map *umap, __isl_take isl_set *set)
839 return gen_bin_set_op(umap, set, &gist_params_entry);
842 __isl_give isl_union_set *isl_union_set_gist_params(
843 __isl_take isl_union_set *uset, __isl_take isl_set *set)
845 return isl_union_map_gist_params(uset, set);
848 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
849 __isl_take isl_union_map *context)
851 return match_bin_op(umap, context, &isl_map_gist);
854 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
855 __isl_take isl_union_set *context)
857 if (isl_union_set_is_params(context))
858 return union_map_gist_params(uset, context);
859 return isl_union_map_gist(uset, context);
862 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
863 __isl_take isl_map *set2)
865 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
868 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
869 __isl_take isl_map *set2)
871 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
874 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
875 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
877 return match_bin_op(uset1, uset2, &lex_lt_set);
880 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
881 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
883 return match_bin_op(uset1, uset2, &lex_le_set);
886 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
887 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
889 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
892 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
893 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
895 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
898 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
899 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
901 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
904 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
905 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
907 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
910 static int intersect_domain_entry(void **entry, void *user)
912 struct isl_union_map_gen_bin_data *data = user;
913 uint32_t hash;
914 struct isl_hash_table_entry *entry2;
915 isl_space *dim;
916 isl_map *map = *entry;
917 int empty;
919 dim = isl_map_get_space(map);
920 dim = isl_space_domain(dim);
921 hash = isl_space_get_hash(dim);
922 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
923 hash, &has_dim, dim, 0);
924 isl_space_free(dim);
925 if (!entry2)
926 return 0;
928 map = isl_map_copy(map);
929 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
931 empty = isl_map_is_empty(map);
932 if (empty < 0) {
933 isl_map_free(map);
934 return -1;
936 if (empty) {
937 isl_map_free(map);
938 return 0;
941 data->res = isl_union_map_add_map(data->res, map);
943 return 0;
946 /* Intersect the domain of "umap" with "uset".
947 * If "uset" is a parameters domain, then intersect the parameter
948 * domain of "umap" with this set.
950 __isl_give isl_union_map *isl_union_map_intersect_domain(
951 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
953 if (isl_union_set_is_params(uset))
954 return union_map_intersect_params(umap, uset);
955 return gen_bin_op(umap, uset, &intersect_domain_entry);
958 /* Remove the elements of data->umap2 from the domain of *entry
959 * and add the result to data->res.
961 static int subtract_domain_entry(void **entry, void *user)
963 struct isl_union_map_gen_bin_data *data = user;
964 uint32_t hash;
965 struct isl_hash_table_entry *entry2;
966 isl_space *dim;
967 isl_map *map = *entry;
968 int empty;
970 dim = isl_map_get_space(map);
971 dim = isl_space_domain(dim);
972 hash = isl_space_get_hash(dim);
973 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
974 hash, &has_dim, dim, 0);
975 isl_space_free(dim);
977 map = isl_map_copy(map);
979 if (!entry2) {
980 data->res = isl_union_map_add_map(data->res, map);
981 return 0;
984 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
986 empty = isl_map_is_empty(map);
987 if (empty < 0) {
988 isl_map_free(map);
989 return -1;
991 if (empty) {
992 isl_map_free(map);
993 return 0;
996 data->res = isl_union_map_add_map(data->res, map);
998 return 0;
1001 /* Remove the elements of "uset" from the domain of "umap".
1003 __isl_give isl_union_map *isl_union_map_subtract_domain(
1004 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1006 return gen_bin_op(umap, dom, &subtract_domain_entry);
1009 /* Remove the elements of data->umap2 from the range of *entry
1010 * and add the result to data->res.
1012 static int subtract_range_entry(void **entry, void *user)
1014 struct isl_union_map_gen_bin_data *data = user;
1015 uint32_t hash;
1016 struct isl_hash_table_entry *entry2;
1017 isl_space *space;
1018 isl_map *map = *entry;
1019 int empty;
1021 space = isl_map_get_space(map);
1022 space = isl_space_range(space);
1023 hash = isl_space_get_hash(space);
1024 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1025 hash, &has_dim, space, 0);
1026 isl_space_free(space);
1028 map = isl_map_copy(map);
1030 if (!entry2) {
1031 data->res = isl_union_map_add_map(data->res, map);
1032 return 0;
1035 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1037 empty = isl_map_is_empty(map);
1038 if (empty < 0) {
1039 isl_map_free(map);
1040 return -1;
1042 if (empty) {
1043 isl_map_free(map);
1044 return 0;
1047 data->res = isl_union_map_add_map(data->res, map);
1049 return 0;
1052 /* Remove the elements of "uset" from the range of "umap".
1054 __isl_give isl_union_map *isl_union_map_subtract_range(
1055 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1057 return gen_bin_op(umap, dom, &subtract_range_entry);
1060 static int gist_domain_entry(void **entry, void *user)
1062 struct isl_union_map_gen_bin_data *data = user;
1063 uint32_t hash;
1064 struct isl_hash_table_entry *entry2;
1065 isl_space *dim;
1066 isl_map *map = *entry;
1067 int empty;
1069 dim = isl_map_get_space(map);
1070 dim = isl_space_domain(dim);
1071 hash = isl_space_get_hash(dim);
1072 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1073 hash, &has_dim, dim, 0);
1074 isl_space_free(dim);
1075 if (!entry2)
1076 return 0;
1078 map = isl_map_copy(map);
1079 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1081 empty = isl_map_is_empty(map);
1082 if (empty < 0) {
1083 isl_map_free(map);
1084 return -1;
1087 data->res = isl_union_map_add_map(data->res, map);
1089 return 0;
1092 /* Compute the gist of "umap" with respect to the domain "uset".
1093 * If "uset" is a parameters domain, then compute the gist
1094 * with respect to this parameter domain.
1096 __isl_give isl_union_map *isl_union_map_gist_domain(
1097 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1099 if (isl_union_set_is_params(uset))
1100 return union_map_gist_params(umap, uset);
1101 return gen_bin_op(umap, uset, &gist_domain_entry);
1104 static int gist_range_entry(void **entry, void *user)
1106 struct isl_union_map_gen_bin_data *data = user;
1107 uint32_t hash;
1108 struct isl_hash_table_entry *entry2;
1109 isl_space *space;
1110 isl_map *map = *entry;
1111 int empty;
1113 space = isl_map_get_space(map);
1114 space = isl_space_range(space);
1115 hash = isl_space_get_hash(space);
1116 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1117 hash, &has_dim, space, 0);
1118 isl_space_free(space);
1119 if (!entry2)
1120 return 0;
1122 map = isl_map_copy(map);
1123 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1125 empty = isl_map_is_empty(map);
1126 if (empty < 0) {
1127 isl_map_free(map);
1128 return -1;
1131 data->res = isl_union_map_add_map(data->res, map);
1133 return 0;
1136 /* Compute the gist of "umap" with respect to the range "uset".
1138 __isl_give isl_union_map *isl_union_map_gist_range(
1139 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1141 return gen_bin_op(umap, uset, &gist_range_entry);
1144 static int intersect_range_entry(void **entry, void *user)
1146 struct isl_union_map_gen_bin_data *data = user;
1147 uint32_t hash;
1148 struct isl_hash_table_entry *entry2;
1149 isl_space *dim;
1150 isl_map *map = *entry;
1151 int empty;
1153 dim = isl_map_get_space(map);
1154 dim = isl_space_range(dim);
1155 hash = isl_space_get_hash(dim);
1156 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1157 hash, &has_dim, dim, 0);
1158 isl_space_free(dim);
1159 if (!entry2)
1160 return 0;
1162 map = isl_map_copy(map);
1163 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1165 empty = isl_map_is_empty(map);
1166 if (empty < 0) {
1167 isl_map_free(map);
1168 return -1;
1170 if (empty) {
1171 isl_map_free(map);
1172 return 0;
1175 data->res = isl_union_map_add_map(data->res, map);
1177 return 0;
1180 __isl_give isl_union_map *isl_union_map_intersect_range(
1181 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1183 return gen_bin_op(umap, uset, &intersect_range_entry);
1186 struct isl_union_map_bin_data {
1187 isl_union_map *umap2;
1188 isl_union_map *res;
1189 isl_map *map;
1190 int (*fn)(void **entry, void *user);
1193 static int apply_range_entry(void **entry, void *user)
1195 struct isl_union_map_bin_data *data = user;
1196 isl_map *map2 = *entry;
1197 int empty;
1199 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1200 map2->dim, isl_dim_in))
1201 return 0;
1203 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1205 empty = isl_map_is_empty(map2);
1206 if (empty < 0) {
1207 isl_map_free(map2);
1208 return -1;
1210 if (empty) {
1211 isl_map_free(map2);
1212 return 0;
1215 data->res = isl_union_map_add_map(data->res, map2);
1217 return 0;
1220 static int bin_entry(void **entry, void *user)
1222 struct isl_union_map_bin_data *data = user;
1223 isl_map *map = *entry;
1225 data->map = map;
1226 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1227 data->fn, data) < 0)
1228 return -1;
1230 return 0;
1233 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1234 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
1236 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1238 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1239 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1241 if (!umap1 || !umap2)
1242 goto error;
1244 data.umap2 = umap2;
1245 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1246 umap1->table.n);
1247 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1248 &bin_entry, &data) < 0)
1249 goto error;
1251 isl_union_map_free(umap1);
1252 isl_union_map_free(umap2);
1253 return data.res;
1254 error:
1255 isl_union_map_free(umap1);
1256 isl_union_map_free(umap2);
1257 isl_union_map_free(data.res);
1258 return NULL;
1261 __isl_give isl_union_map *isl_union_map_apply_range(
1262 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1264 return bin_op(umap1, umap2, &apply_range_entry);
1267 __isl_give isl_union_map *isl_union_map_apply_domain(
1268 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1270 umap1 = isl_union_map_reverse(umap1);
1271 umap1 = isl_union_map_apply_range(umap1, umap2);
1272 return isl_union_map_reverse(umap1);
1275 __isl_give isl_union_set *isl_union_set_apply(
1276 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1278 return isl_union_map_apply_range(uset, umap);
1281 static int map_lex_lt_entry(void **entry, void *user)
1283 struct isl_union_map_bin_data *data = user;
1284 isl_map *map2 = *entry;
1286 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1287 map2->dim, isl_dim_out))
1288 return 0;
1290 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1292 data->res = isl_union_map_add_map(data->res, map2);
1294 return 0;
1297 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1298 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1300 return bin_op(umap1, umap2, &map_lex_lt_entry);
1303 static int map_lex_le_entry(void **entry, void *user)
1305 struct isl_union_map_bin_data *data = user;
1306 isl_map *map2 = *entry;
1308 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1309 map2->dim, isl_dim_out))
1310 return 0;
1312 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1314 data->res = isl_union_map_add_map(data->res, map2);
1316 return 0;
1319 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1320 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1322 return bin_op(umap1, umap2, &map_lex_le_entry);
1325 static int product_entry(void **entry, void *user)
1327 struct isl_union_map_bin_data *data = user;
1328 isl_map *map2 = *entry;
1330 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1332 data->res = isl_union_map_add_map(data->res, map2);
1334 return 0;
1337 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1338 __isl_take isl_union_map *umap2)
1340 return bin_op(umap1, umap2, &product_entry);
1343 static int set_product_entry(void **entry, void *user)
1345 struct isl_union_map_bin_data *data = user;
1346 isl_set *set2 = *entry;
1348 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1350 data->res = isl_union_set_add_set(data->res, set2);
1352 return 0;
1355 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1356 __isl_take isl_union_set *uset2)
1358 return bin_op(uset1, uset2, &set_product_entry);
1361 static int domain_product_entry(void **entry, void *user)
1363 struct isl_union_map_bin_data *data = user;
1364 isl_map *map2 = *entry;
1366 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1367 map2->dim, isl_dim_out))
1368 return 0;
1370 map2 = isl_map_domain_product(isl_map_copy(data->map),
1371 isl_map_copy(map2));
1373 data->res = isl_union_map_add_map(data->res, map2);
1375 return 0;
1378 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1380 __isl_give isl_union_map *isl_union_map_domain_product(
1381 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1383 return bin_op(umap1, umap2, &domain_product_entry);
1386 static int range_product_entry(void **entry, void *user)
1388 struct isl_union_map_bin_data *data = user;
1389 isl_map *map2 = *entry;
1391 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1392 map2->dim, isl_dim_in))
1393 return 0;
1395 map2 = isl_map_range_product(isl_map_copy(data->map),
1396 isl_map_copy(map2));
1398 data->res = isl_union_map_add_map(data->res, map2);
1400 return 0;
1403 __isl_give isl_union_map *isl_union_map_range_product(
1404 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1406 return bin_op(umap1, umap2, &range_product_entry);
1409 static int flat_range_product_entry(void **entry, void *user)
1411 struct isl_union_map_bin_data *data = user;
1412 isl_map *map2 = *entry;
1414 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1415 map2->dim, isl_dim_in))
1416 return 0;
1418 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1419 isl_map_copy(map2));
1421 data->res = isl_union_map_add_map(data->res, map2);
1423 return 0;
1426 __isl_give isl_union_map *isl_union_map_flat_range_product(
1427 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1429 return bin_op(umap1, umap2, &flat_range_product_entry);
1432 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1433 int (*fn)(void **, void *))
1435 isl_union_set *res;
1437 if (!umap)
1438 return NULL;
1440 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1441 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1442 goto error;
1444 isl_union_map_free(umap);
1445 return res;
1446 error:
1447 isl_union_map_free(umap);
1448 isl_union_set_free(res);
1449 return NULL;
1452 static int from_range_entry(void **entry, void *user)
1454 isl_map *set = *entry;
1455 isl_union_set **res = user;
1457 *res = isl_union_map_add_map(*res,
1458 isl_map_from_range(isl_set_copy(set)));
1460 return 0;
1463 __isl_give isl_union_map *isl_union_map_from_range(
1464 __isl_take isl_union_set *uset)
1466 return cond_un_op(uset, &from_range_entry);
1469 __isl_give isl_union_map *isl_union_map_from_domain(
1470 __isl_take isl_union_set *uset)
1472 return isl_union_map_reverse(isl_union_map_from_range(uset));
1475 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1476 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1478 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1479 isl_union_map_from_range(range));
1482 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1483 int (*fn)(void **, void *))
1485 umap = isl_union_map_cow(umap);
1486 if (!umap)
1487 return NULL;
1489 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1490 goto error;
1492 return umap;
1493 error:
1494 isl_union_map_free(umap);
1495 return NULL;
1498 static int affine_entry(void **entry, void *user)
1500 isl_map **map = (isl_map **)entry;
1502 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1504 return *map ? 0 : -1;
1507 __isl_give isl_union_map *isl_union_map_affine_hull(
1508 __isl_take isl_union_map *umap)
1510 return un_op(umap, &affine_entry);
1513 __isl_give isl_union_set *isl_union_set_affine_hull(
1514 __isl_take isl_union_set *uset)
1516 return isl_union_map_affine_hull(uset);
1519 static int polyhedral_entry(void **entry, void *user)
1521 isl_map **map = (isl_map **)entry;
1523 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1525 return *map ? 0 : -1;
1528 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1529 __isl_take isl_union_map *umap)
1531 return un_op(umap, &polyhedral_entry);
1534 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1535 __isl_take isl_union_set *uset)
1537 return isl_union_map_polyhedral_hull(uset);
1540 static int simple_entry(void **entry, void *user)
1542 isl_map **map = (isl_map **)entry;
1544 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1546 return *map ? 0 : -1;
1549 __isl_give isl_union_map *isl_union_map_simple_hull(
1550 __isl_take isl_union_map *umap)
1552 return un_op(umap, &simple_entry);
1555 __isl_give isl_union_set *isl_union_set_simple_hull(
1556 __isl_take isl_union_set *uset)
1558 return isl_union_map_simple_hull(uset);
1561 static int inplace_entry(void **entry, void *user)
1563 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1564 isl_map **map = (isl_map **)entry;
1565 isl_map *copy;
1567 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1568 copy = fn(isl_map_copy(*map));
1569 if (!copy)
1570 return -1;
1572 isl_map_free(*map);
1573 *map = copy;
1575 return 0;
1578 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1579 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1581 if (!umap)
1582 return NULL;
1584 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1585 &inplace_entry, &fn) < 0)
1586 goto error;
1588 return umap;
1589 error:
1590 isl_union_map_free(umap);
1591 return NULL;
1594 __isl_give isl_union_map *isl_union_map_coalesce(
1595 __isl_take isl_union_map *umap)
1597 return inplace(umap, &isl_map_coalesce);
1600 __isl_give isl_union_set *isl_union_set_coalesce(
1601 __isl_take isl_union_set *uset)
1603 return isl_union_map_coalesce(uset);
1606 __isl_give isl_union_map *isl_union_map_detect_equalities(
1607 __isl_take isl_union_map *umap)
1609 return inplace(umap, &isl_map_detect_equalities);
1612 __isl_give isl_union_set *isl_union_set_detect_equalities(
1613 __isl_take isl_union_set *uset)
1615 return isl_union_map_detect_equalities(uset);
1618 __isl_give isl_union_map *isl_union_map_compute_divs(
1619 __isl_take isl_union_map *umap)
1621 return inplace(umap, &isl_map_compute_divs);
1624 __isl_give isl_union_set *isl_union_set_compute_divs(
1625 __isl_take isl_union_set *uset)
1627 return isl_union_map_compute_divs(uset);
1630 static int lexmin_entry(void **entry, void *user)
1632 isl_map **map = (isl_map **)entry;
1634 *map = isl_map_lexmin(*map);
1636 return *map ? 0 : -1;
1639 __isl_give isl_union_map *isl_union_map_lexmin(
1640 __isl_take isl_union_map *umap)
1642 return un_op(umap, &lexmin_entry);
1645 __isl_give isl_union_set *isl_union_set_lexmin(
1646 __isl_take isl_union_set *uset)
1648 return isl_union_map_lexmin(uset);
1651 static int lexmax_entry(void **entry, void *user)
1653 isl_map **map = (isl_map **)entry;
1655 *map = isl_map_lexmax(*map);
1657 return *map ? 0 : -1;
1660 __isl_give isl_union_map *isl_union_map_lexmax(
1661 __isl_take isl_union_map *umap)
1663 return un_op(umap, &lexmax_entry);
1666 __isl_give isl_union_set *isl_union_set_lexmax(
1667 __isl_take isl_union_set *uset)
1669 return isl_union_map_lexmax(uset);
1672 static int universe_entry(void **entry, void *user)
1674 isl_map *map = *entry;
1675 isl_union_map **res = user;
1677 map = isl_map_universe(isl_map_get_space(map));
1678 *res = isl_union_map_add_map(*res, map);
1680 return 0;
1683 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1685 return cond_un_op(umap, &universe_entry);
1688 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1690 return isl_union_map_universe(uset);
1693 static int reverse_entry(void **entry, void *user)
1695 isl_map *map = *entry;
1696 isl_union_map **res = user;
1698 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1700 return 0;
1703 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1705 return cond_un_op(umap, &reverse_entry);
1708 static int params_entry(void **entry, void *user)
1710 isl_map *map = *entry;
1711 isl_union_set **res = user;
1713 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1715 return 0;
1718 /* Compute the parameter domain of the given union map.
1720 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1722 int empty;
1724 empty = isl_union_map_is_empty(umap);
1725 if (empty < 0)
1726 goto error;
1727 if (empty) {
1728 isl_space *space;
1729 space = isl_union_map_get_space(umap);
1730 isl_union_map_free(umap);
1731 return isl_set_empty(space);
1733 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1734 error:
1735 isl_union_map_free(umap);
1736 return NULL;
1739 /* Compute the parameter domain of the given union set.
1741 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1743 return isl_union_map_params(uset);
1746 static int domain_entry(void **entry, void *user)
1748 isl_map *map = *entry;
1749 isl_union_set **res = user;
1751 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1753 return 0;
1756 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1758 return cond_un_op(umap, &domain_entry);
1761 static int range_entry(void **entry, void *user)
1763 isl_map *map = *entry;
1764 isl_union_set **res = user;
1766 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1768 return 0;
1771 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1773 return cond_un_op(umap, &range_entry);
1776 static int domain_map_entry(void **entry, void *user)
1778 isl_map *map = *entry;
1779 isl_union_set **res = user;
1781 *res = isl_union_map_add_map(*res,
1782 isl_map_domain_map(isl_map_copy(map)));
1784 return 0;
1787 __isl_give isl_union_map *isl_union_map_domain_map(
1788 __isl_take isl_union_map *umap)
1790 return cond_un_op(umap, &domain_map_entry);
1793 static int range_map_entry(void **entry, void *user)
1795 isl_map *map = *entry;
1796 isl_union_set **res = user;
1798 *res = isl_union_map_add_map(*res,
1799 isl_map_range_map(isl_map_copy(map)));
1801 return 0;
1804 __isl_give isl_union_map *isl_union_map_range_map(
1805 __isl_take isl_union_map *umap)
1807 return cond_un_op(umap, &range_map_entry);
1810 static int deltas_entry(void **entry, void *user)
1812 isl_map *map = *entry;
1813 isl_union_set **res = user;
1815 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1816 map->dim, isl_dim_out))
1817 return 0;
1819 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1821 return 0;
1824 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1826 return cond_un_op(umap, &deltas_entry);
1829 static int deltas_map_entry(void **entry, void *user)
1831 isl_map *map = *entry;
1832 isl_union_map **res = user;
1834 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1835 map->dim, isl_dim_out))
1836 return 0;
1838 *res = isl_union_map_add_map(*res,
1839 isl_map_deltas_map(isl_map_copy(map)));
1841 return 0;
1844 __isl_give isl_union_map *isl_union_map_deltas_map(
1845 __isl_take isl_union_map *umap)
1847 return cond_un_op(umap, &deltas_map_entry);
1850 static int identity_entry(void **entry, void *user)
1852 isl_set *set = *entry;
1853 isl_union_map **res = user;
1855 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1857 return 0;
1860 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1862 return cond_un_op(uset, &identity_entry);
1865 static int unwrap_entry(void **entry, void *user)
1867 isl_set *set = *entry;
1868 isl_union_set **res = user;
1870 if (!isl_set_is_wrapping(set))
1871 return 0;
1873 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1875 return 0;
1878 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1880 return cond_un_op(uset, &unwrap_entry);
1883 static int wrap_entry(void **entry, void *user)
1885 isl_map *map = *entry;
1886 isl_union_set **res = user;
1888 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1890 return 0;
1893 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1895 return cond_un_op(umap, &wrap_entry);
1898 struct isl_union_map_is_subset_data {
1899 isl_union_map *umap2;
1900 int is_subset;
1903 static int is_subset_entry(void **entry, void *user)
1905 struct isl_union_map_is_subset_data *data = user;
1906 uint32_t hash;
1907 struct isl_hash_table_entry *entry2;
1908 isl_map *map = *entry;
1910 hash = isl_space_get_hash(map->dim);
1911 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1912 hash, &has_dim, map->dim, 0);
1913 if (!entry2) {
1914 int empty = isl_map_is_empty(map);
1915 if (empty < 0)
1916 return -1;
1917 if (empty)
1918 return 0;
1919 data->is_subset = 0;
1920 return -1;
1923 data->is_subset = isl_map_is_subset(map, entry2->data);
1924 if (data->is_subset < 0 || !data->is_subset)
1925 return -1;
1927 return 0;
1930 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1931 __isl_keep isl_union_map *umap2)
1933 struct isl_union_map_is_subset_data data = { NULL, 1 };
1935 umap1 = isl_union_map_copy(umap1);
1936 umap2 = isl_union_map_copy(umap2);
1937 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1938 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1940 if (!umap1 || !umap2)
1941 goto error;
1943 data.umap2 = umap2;
1944 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1945 &is_subset_entry, &data) < 0 &&
1946 data.is_subset)
1947 goto error;
1949 isl_union_map_free(umap1);
1950 isl_union_map_free(umap2);
1952 return data.is_subset;
1953 error:
1954 isl_union_map_free(umap1);
1955 isl_union_map_free(umap2);
1956 return -1;
1959 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1960 __isl_keep isl_union_set *uset2)
1962 return isl_union_map_is_subset(uset1, uset2);
1965 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1966 __isl_keep isl_union_map *umap2)
1968 int is_subset;
1970 if (!umap1 || !umap2)
1971 return -1;
1972 is_subset = isl_union_map_is_subset(umap1, umap2);
1973 if (is_subset != 1)
1974 return is_subset;
1975 is_subset = isl_union_map_is_subset(umap2, umap1);
1976 return is_subset;
1979 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1980 __isl_keep isl_union_set *uset2)
1982 return isl_union_map_is_equal(uset1, uset2);
1985 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1986 __isl_keep isl_union_map *umap2)
1988 int is_subset;
1990 if (!umap1 || !umap2)
1991 return -1;
1992 is_subset = isl_union_map_is_subset(umap1, umap2);
1993 if (is_subset != 1)
1994 return is_subset;
1995 is_subset = isl_union_map_is_subset(umap2, umap1);
1996 if (is_subset == -1)
1997 return is_subset;
1998 return !is_subset;
2001 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2002 __isl_keep isl_union_set *uset2)
2004 return isl_union_map_is_strict_subset(uset1, uset2);
2007 static int sample_entry(void **entry, void *user)
2009 isl_basic_map **sample = (isl_basic_map **)user;
2010 isl_map *map = *entry;
2012 *sample = isl_map_sample(isl_map_copy(map));
2013 if (!*sample)
2014 return -1;
2015 if (!isl_basic_map_plain_is_empty(*sample))
2016 return -1;
2017 return 0;
2020 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2022 isl_basic_map *sample = NULL;
2024 if (!umap)
2025 return NULL;
2027 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2028 &sample_entry, &sample) < 0 &&
2029 !sample)
2030 goto error;
2032 if (!sample)
2033 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2035 isl_union_map_free(umap);
2037 return sample;
2038 error:
2039 isl_union_map_free(umap);
2040 return NULL;
2043 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2045 return (isl_basic_set *)isl_union_map_sample(uset);
2048 struct isl_forall_data {
2049 int res;
2050 int (*fn)(__isl_keep isl_map *map);
2053 static int forall_entry(void **entry, void *user)
2055 struct isl_forall_data *data = user;
2056 isl_map *map = *entry;
2058 data->res = data->fn(map);
2059 if (data->res < 0)
2060 return -1;
2062 if (!data->res)
2063 return -1;
2065 return 0;
2068 static int union_map_forall(__isl_keep isl_union_map *umap,
2069 int (*fn)(__isl_keep isl_map *map))
2071 struct isl_forall_data data = { 1, fn };
2073 if (!umap)
2074 return -1;
2076 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2077 &forall_entry, &data) < 0 && data.res)
2078 return -1;
2080 return data.res;
2083 struct isl_forall_user_data {
2084 int res;
2085 int (*fn)(__isl_keep isl_map *map, void *user);
2086 void *user;
2089 static int forall_user_entry(void **entry, void *user)
2091 struct isl_forall_user_data *data = user;
2092 isl_map *map = *entry;
2094 data->res = data->fn(map, data->user);
2095 if (data->res < 0)
2096 return -1;
2098 if (!data->res)
2099 return -1;
2101 return 0;
2104 /* Check if fn(map, user) returns true for all maps "map" in umap.
2106 static int union_map_forall_user(__isl_keep isl_union_map *umap,
2107 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
2109 struct isl_forall_user_data data = { 1, fn, user };
2111 if (!umap)
2112 return -1;
2114 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2115 &forall_user_entry, &data) < 0 && data.res)
2116 return -1;
2118 return data.res;
2121 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2123 return union_map_forall(umap, &isl_map_is_empty);
2126 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2128 return isl_union_map_is_empty(uset);
2131 static int is_subset_of_identity(__isl_keep isl_map *map)
2133 int is_subset;
2134 isl_space *dim;
2135 isl_map *id;
2137 if (!map)
2138 return -1;
2140 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2141 map->dim, isl_dim_out))
2142 return 0;
2144 dim = isl_map_get_space(map);
2145 id = isl_map_identity(dim);
2147 is_subset = isl_map_is_subset(map, id);
2149 isl_map_free(id);
2151 return is_subset;
2154 /* Given an isl_union_map that consists of a single map, check
2155 * if it is single-valued.
2157 static int single_map_is_single_valued(__isl_keep isl_union_map *umap)
2159 isl_map *map;
2160 int sv;
2162 umap = isl_union_map_copy(umap);
2163 map = isl_map_from_union_map(umap);
2164 sv = isl_map_is_single_valued(map);
2165 isl_map_free(map);
2167 return sv;
2170 /* Internal data structure for single_valued_on_domain.
2172 * "umap" is the union map to be tested.
2173 * "sv" is set to 1 as long as "umap" may still be single-valued.
2175 struct isl_union_map_is_sv_data {
2176 isl_union_map *umap;
2177 int sv;
2180 /* Check if the data->umap is single-valued on "set".
2182 * If data->umap consists of a single map on "set", then test it
2183 * as an isl_map.
2185 * Otherwise, compute
2187 * M \circ M^-1
2189 * check if the result is a subset of the identity mapping and
2190 * store the result in data->sv.
2192 * Terminate as soon as data->umap has been determined not to
2193 * be single-valued.
2195 static int single_valued_on_domain(__isl_take isl_set *set, void *user)
2197 struct isl_union_map_is_sv_data *data = user;
2198 isl_union_map *umap, *test;
2200 umap = isl_union_map_copy(data->umap);
2201 umap = isl_union_map_intersect_domain(umap,
2202 isl_union_set_from_set(set));
2204 if (isl_union_map_n_map(umap) == 1) {
2205 data->sv = single_map_is_single_valued(umap);
2206 isl_union_map_free(umap);
2207 } else {
2208 test = isl_union_map_reverse(isl_union_map_copy(umap));
2209 test = isl_union_map_apply_range(test, umap);
2211 data->sv = union_map_forall(test, &is_subset_of_identity);
2213 isl_union_map_free(test);
2216 if (data->sv < 0 || !data->sv)
2217 return -1;
2218 return 0;
2221 /* Check if the given map is single-valued.
2223 * If the union map consists of a single map, then test it as an isl_map.
2224 * Otherwise, check if the union map is single-valued on each of its
2225 * domain spaces.
2227 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2229 isl_union_map *universe;
2230 isl_union_set *domain;
2231 struct isl_union_map_is_sv_data data;
2233 if (isl_union_map_n_map(umap) == 1)
2234 return single_map_is_single_valued(umap);
2236 universe = isl_union_map_universe(isl_union_map_copy(umap));
2237 domain = isl_union_map_domain(universe);
2239 data.sv = 1;
2240 data.umap = umap;
2241 if (isl_union_set_foreach_set(domain,
2242 &single_valued_on_domain, &data) < 0 && data.sv)
2243 data.sv = -1;
2244 isl_union_set_free(domain);
2246 return data.sv;
2249 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2251 int in;
2253 umap = isl_union_map_copy(umap);
2254 umap = isl_union_map_reverse(umap);
2255 in = isl_union_map_is_single_valued(umap);
2256 isl_union_map_free(umap);
2258 return in;
2261 /* Represents a map that has a fixed value (v) for one of its
2262 * range dimensions.
2263 * The map in this structure is not reference counted, so it
2264 * is only valid while the isl_union_map from which it was
2265 * obtained is still alive.
2267 struct isl_fixed_map {
2268 isl_int v;
2269 isl_map *map;
2272 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2273 int n)
2275 int i;
2276 struct isl_fixed_map *v;
2278 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2279 if (!v)
2280 return NULL;
2281 for (i = 0; i < n; ++i)
2282 isl_int_init(v[i].v);
2283 return v;
2286 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2288 int i;
2290 if (!v)
2291 return;
2292 for (i = 0; i < n; ++i)
2293 isl_int_clear(v[i].v);
2294 free(v);
2297 /* Compare the "v" field of two isl_fixed_map structs.
2299 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2301 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2302 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2304 return isl_int_cmp(e1->v, e2->v);
2307 /* Internal data structure used while checking whether all maps
2308 * in a union_map have a fixed value for a given output dimension.
2309 * v is the list of maps, with the fixed value for the dimension
2310 * n is the number of maps considered so far
2311 * pos is the output dimension under investigation
2313 struct isl_fixed_dim_data {
2314 struct isl_fixed_map *v;
2315 int n;
2316 int pos;
2319 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2321 struct isl_fixed_dim_data *data = user;
2323 data->v[data->n].map = map;
2324 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2325 &data->v[data->n++].v);
2328 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2329 int first, int n_range);
2331 /* Given a list of the maps, with their fixed values at output dimension "pos",
2332 * check whether the ranges of the maps form an obvious partition.
2334 * We first sort the maps according to their fixed values.
2335 * If all maps have a different value, then we know the ranges form
2336 * a partition.
2337 * Otherwise, we collect the maps with the same fixed value and
2338 * check whether each such collection is obviously injective
2339 * based on later dimensions.
2341 static int separates(struct isl_fixed_map *v, int n,
2342 __isl_take isl_space *dim, int pos, int n_range)
2344 int i;
2346 if (!v)
2347 goto error;
2349 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2351 for (i = 0; i + 1 < n; ++i) {
2352 int j, k;
2353 isl_union_map *part;
2354 int injective;
2356 for (j = i + 1; j < n; ++j)
2357 if (isl_int_ne(v[i].v, v[j].v))
2358 break;
2360 if (j == i + 1)
2361 continue;
2363 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2364 for (k = i; k < j; ++k)
2365 part = isl_union_map_add_map(part,
2366 isl_map_copy(v[k].map));
2368 injective = plain_injective_on_range(part, pos + 1, n_range);
2369 if (injective < 0)
2370 goto error;
2371 if (!injective)
2372 break;
2374 i = j - 1;
2377 isl_space_free(dim);
2378 free_isl_fixed_map_array(v, n);
2379 return i + 1 >= n;
2380 error:
2381 isl_space_free(dim);
2382 free_isl_fixed_map_array(v, n);
2383 return -1;
2386 /* Check whether the maps in umap have obviously distinct ranges.
2387 * In particular, check for an output dimension in the range
2388 * [first,n_range) for which all maps have a fixed value
2389 * and then check if these values, possibly along with fixed values
2390 * at later dimensions, entail distinct ranges.
2392 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2393 int first, int n_range)
2395 isl_ctx *ctx;
2396 int n;
2397 struct isl_fixed_dim_data data = { NULL };
2399 ctx = isl_union_map_get_ctx(umap);
2401 n = isl_union_map_n_map(umap);
2402 if (!umap)
2403 goto error;
2405 if (n <= 1) {
2406 isl_union_map_free(umap);
2407 return 1;
2410 if (first >= n_range) {
2411 isl_union_map_free(umap);
2412 return 0;
2415 data.v = alloc_isl_fixed_map_array(ctx, n);
2416 if (!data.v)
2417 goto error;
2419 for (data.pos = first; data.pos < n_range; ++data.pos) {
2420 int fixed;
2421 int injective;
2422 isl_space *dim;
2424 data.n = 0;
2425 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2426 if (fixed < 0)
2427 goto error;
2428 if (!fixed)
2429 continue;
2430 dim = isl_union_map_get_space(umap);
2431 injective = separates(data.v, n, dim, data.pos, n_range);
2432 isl_union_map_free(umap);
2433 return injective;
2436 free_isl_fixed_map_array(data.v, n);
2437 isl_union_map_free(umap);
2439 return 0;
2440 error:
2441 free_isl_fixed_map_array(data.v, n);
2442 isl_union_map_free(umap);
2443 return -1;
2446 /* Check whether the maps in umap that map to subsets of "ran"
2447 * have obviously distinct ranges.
2449 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2451 isl_union_map *umap = user;
2453 umap = isl_union_map_copy(umap);
2454 umap = isl_union_map_intersect_range(umap,
2455 isl_union_set_from_set(isl_set_copy(ran)));
2456 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2459 /* Check if the given union_map is obviously injective.
2461 * In particular, we first check if all individual maps are obviously
2462 * injective and then check if all the ranges of these maps are
2463 * obviously disjoint.
2465 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2467 int in;
2468 isl_union_map *univ;
2469 isl_union_set *ran;
2471 in = union_map_forall(umap, &isl_map_plain_is_injective);
2472 if (in < 0)
2473 return -1;
2474 if (!in)
2475 return 0;
2477 univ = isl_union_map_universe(isl_union_map_copy(umap));
2478 ran = isl_union_map_range(univ);
2480 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2482 isl_union_set_free(ran);
2484 return in;
2487 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2489 int sv;
2491 sv = isl_union_map_is_single_valued(umap);
2492 if (sv < 0 || !sv)
2493 return sv;
2495 return isl_union_map_is_injective(umap);
2498 static int zip_entry(void **entry, void *user)
2500 isl_map *map = *entry;
2501 isl_union_map **res = user;
2503 if (!isl_map_can_zip(map))
2504 return 0;
2506 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2508 return 0;
2511 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2513 return cond_un_op(umap, &zip_entry);
2516 static int uncurry_entry(void **entry, void *user)
2518 isl_map *map = *entry;
2519 isl_union_map **res = user;
2521 if (!isl_map_can_uncurry(map))
2522 return 0;
2524 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
2526 return 0;
2529 /* Given a union map, take the maps of the form A -> (B -> C) and
2530 * return the union of the corresponding maps (A -> B) -> C.
2532 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2534 return cond_un_op(umap, &uncurry_entry);
2537 static int curry_entry(void **entry, void *user)
2539 isl_map *map = *entry;
2540 isl_union_map **res = user;
2542 if (!isl_map_can_curry(map))
2543 return 0;
2545 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2547 return 0;
2550 /* Given a union map, take the maps of the form (A -> B) -> C and
2551 * return the union of the corresponding maps A -> (B -> C).
2553 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2555 return cond_un_op(umap, &curry_entry);
2558 static int lift_entry(void **entry, void *user)
2560 isl_set *set = *entry;
2561 isl_union_set **res = user;
2563 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2565 return 0;
2568 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2570 return cond_un_op(uset, &lift_entry);
2573 static int coefficients_entry(void **entry, void *user)
2575 isl_set *set = *entry;
2576 isl_union_set **res = user;
2578 set = isl_set_copy(set);
2579 set = isl_set_from_basic_set(isl_set_coefficients(set));
2580 *res = isl_union_set_add_set(*res, set);
2582 return 0;
2585 __isl_give isl_union_set *isl_union_set_coefficients(
2586 __isl_take isl_union_set *uset)
2588 isl_ctx *ctx;
2589 isl_space *dim;
2590 isl_union_set *res;
2592 if (!uset)
2593 return NULL;
2595 ctx = isl_union_set_get_ctx(uset);
2596 dim = isl_space_set_alloc(ctx, 0, 0);
2597 res = isl_union_map_alloc(dim, uset->table.n);
2598 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2599 &coefficients_entry, &res) < 0)
2600 goto error;
2602 isl_union_set_free(uset);
2603 return res;
2604 error:
2605 isl_union_set_free(uset);
2606 isl_union_set_free(res);
2607 return NULL;
2610 static int solutions_entry(void **entry, void *user)
2612 isl_set *set = *entry;
2613 isl_union_set **res = user;
2615 set = isl_set_copy(set);
2616 set = isl_set_from_basic_set(isl_set_solutions(set));
2617 if (!*res)
2618 *res = isl_union_set_from_set(set);
2619 else
2620 *res = isl_union_set_add_set(*res, set);
2622 if (!*res)
2623 return -1;
2625 return 0;
2628 __isl_give isl_union_set *isl_union_set_solutions(
2629 __isl_take isl_union_set *uset)
2631 isl_union_set *res = NULL;
2633 if (!uset)
2634 return NULL;
2636 if (uset->table.n == 0) {
2637 res = isl_union_set_empty(isl_union_set_get_space(uset));
2638 isl_union_set_free(uset);
2639 return res;
2642 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2643 &solutions_entry, &res) < 0)
2644 goto error;
2646 isl_union_set_free(uset);
2647 return res;
2648 error:
2649 isl_union_set_free(uset);
2650 isl_union_set_free(res);
2651 return NULL;
2654 /* Is the domain space of "map" equal to "space"?
2656 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2658 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2659 space, isl_dim_out);
2662 /* Is the range space of "map" equal to "space"?
2664 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2666 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
2667 space, isl_dim_out);
2670 /* Is the set space of "map" equal to "space"?
2672 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2674 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
2675 space, isl_dim_out);
2678 /* Internal data structure for preimage_pw_multi_aff.
2680 * "pma" is the function under which the preimage should be taken.
2681 * "space" is the space of "pma".
2682 * "res" collects the results.
2683 * "fn" computes the preimage for a given map.
2684 * "match" returns true if "fn" can be called.
2686 struct isl_union_map_preimage_data {
2687 isl_space *space;
2688 isl_pw_multi_aff *pma;
2689 isl_union_map *res;
2690 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2691 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2692 __isl_take isl_pw_multi_aff *pma);
2695 /* Call data->fn to compute the preimage of the domain or range of *entry
2696 * under the function represented by data->pma, provided the domain/range
2697 * space of *entry matches the target space of data->pma
2698 * (as given by data->match), and add the result to data->res.
2700 static int preimage_entry(void **entry, void *user)
2702 int m;
2703 isl_map *map = *entry;
2704 struct isl_union_map_preimage_data *data = user;
2705 int empty;
2707 m = data->match(map, data->space);
2708 if (m < 0)
2709 return -1;
2710 if (!m)
2711 return 0;
2713 map = isl_map_copy(map);
2714 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
2716 empty = isl_map_is_empty(map);
2717 if (empty < 0 || empty) {
2718 isl_map_free(map);
2719 return empty < 0 ? -1 : 0;
2722 data->res = isl_union_map_add_map(data->res, map);
2724 return 0;
2727 /* Compute the preimage of the domain or range of "umap" under the function
2728 * represented by "pma".
2729 * In other words, plug in "pma" in the domain or range of "umap".
2730 * The function "fn" performs the actual preimage computation on a map,
2731 * while "match" determines to which maps the function should be applied.
2733 static __isl_give isl_union_map *preimage_pw_multi_aff(
2734 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
2735 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
2736 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2737 __isl_take isl_pw_multi_aff *pma))
2739 isl_ctx *ctx;
2740 isl_space *space;
2741 struct isl_union_map_preimage_data data;
2743 umap = isl_union_map_align_params(umap,
2744 isl_pw_multi_aff_get_space(pma));
2745 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
2747 if (!umap || !pma)
2748 goto error;
2750 ctx = isl_union_map_get_ctx(umap);
2751 space = isl_union_map_get_space(umap);
2752 data.space = isl_pw_multi_aff_get_space(pma);
2753 data.pma = pma;
2754 data.res = isl_union_map_alloc(space, umap->table.n);
2755 data.match = match;
2756 data.fn = fn;
2757 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
2758 &data) < 0)
2759 data.res = isl_union_map_free(data.res);
2761 isl_space_free(data.space);
2762 isl_union_map_free(umap);
2763 isl_pw_multi_aff_free(pma);
2764 return data.res;
2765 error:
2766 isl_union_map_free(umap);
2767 isl_pw_multi_aff_free(pma);
2768 return NULL;
2771 /* Compute the preimage of the domain of "umap" under the function
2772 * represented by "pma".
2773 * In other words, plug in "pma" in the domain of "umap".
2774 * The result contains maps that live in the same spaces as the maps of "umap"
2775 * with domain space equal to the target space of "pma",
2776 * except that the domain has been replaced by the domain space of "pma".
2778 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
2779 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
2781 return preimage_pw_multi_aff(umap, pma, &domain_match,
2782 &isl_map_preimage_domain_pw_multi_aff);
2785 /* Compute the preimage of the range of "umap" under the function
2786 * represented by "pma".
2787 * In other words, plug in "pma" in the range of "umap".
2788 * The result contains maps that live in the same spaces as the maps of "umap"
2789 * with range space equal to the target space of "pma",
2790 * except that the range has been replaced by the domain space of "pma".
2792 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
2793 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
2795 return preimage_pw_multi_aff(umap, pma, &range_match,
2796 &isl_map_preimage_range_pw_multi_aff);
2799 /* Compute the preimage of "uset" under the function represented by "pma".
2800 * In other words, plug in "pma" in "uset".
2801 * The result contains sets that live in the same spaces as the sets of "uset"
2802 * with space equal to the target space of "pma",
2803 * except that the space has been replaced by the domain space of "pma".
2805 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
2806 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
2808 return preimage_pw_multi_aff(uset, pma, &set_match,
2809 &isl_set_preimage_pw_multi_aff);
2812 /* Compute the preimage of the domain of "umap" under the function
2813 * represented by "ma".
2814 * In other words, plug in "ma" in the domain of "umap".
2815 * The result contains maps that live in the same spaces as the maps of "umap"
2816 * with domain space equal to the target space of "ma",
2817 * except that the domain has been replaced by the domain space of "ma".
2819 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
2820 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
2822 return isl_union_map_preimage_domain_pw_multi_aff(umap,
2823 isl_pw_multi_aff_from_multi_aff(ma));
2826 /* Compute the preimage of the range of "umap" under the function
2827 * represented by "ma".
2828 * In other words, plug in "ma" in the range of "umap".
2829 * The result contains maps that live in the same spaces as the maps of "umap"
2830 * with range space equal to the target space of "ma",
2831 * except that the range has been replaced by the domain space of "ma".
2833 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
2834 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
2836 return isl_union_map_preimage_range_pw_multi_aff(umap,
2837 isl_pw_multi_aff_from_multi_aff(ma));
2840 /* Compute the preimage of "uset" under the function represented by "ma".
2841 * In other words, plug in "ma" in "uset".
2842 * The result contains sets that live in the same spaces as the sets of "uset"
2843 * with space equal to the target space of "ma",
2844 * except that the space has been replaced by the domain space of "ma".
2846 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
2847 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
2849 return isl_union_set_preimage_pw_multi_aff(uset,
2850 isl_pw_multi_aff_from_multi_aff(ma));
2853 /* Internal data structure for preimage_multi_pw_aff.
2855 * "mpa" is the function under which the preimage should be taken.
2856 * "space" is the space of "mpa".
2857 * "res" collects the results.
2858 * "fn" computes the preimage for a given map.
2859 * "match" returns true if "fn" can be called.
2861 struct isl_union_map_preimage_mpa_data {
2862 isl_space *space;
2863 isl_multi_pw_aff *mpa;
2864 isl_union_map *res;
2865 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2866 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2867 __isl_take isl_multi_pw_aff *mpa);
2870 /* Call data->fn to compute the preimage of the domain or range of *entry
2871 * under the function represented by data->mpa, provided the domain/range
2872 * space of *entry matches the target space of data->mpa
2873 * (as given by data->match), and add the result to data->res.
2875 static int preimage_mpa_entry(void **entry, void *user)
2877 int m;
2878 isl_map *map = *entry;
2879 struct isl_union_map_preimage_mpa_data *data = user;
2880 int empty;
2882 m = data->match(map, data->space);
2883 if (m < 0)
2884 return -1;
2885 if (!m)
2886 return 0;
2888 map = isl_map_copy(map);
2889 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
2891 empty = isl_map_is_empty(map);
2892 if (empty < 0 || empty) {
2893 isl_map_free(map);
2894 return empty < 0 ? -1 : 0;
2897 data->res = isl_union_map_add_map(data->res, map);
2899 return 0;
2902 /* Compute the preimage of the domain or range of "umap" under the function
2903 * represented by "mpa".
2904 * In other words, plug in "mpa" in the domain or range of "umap".
2905 * The function "fn" performs the actual preimage computation on a map,
2906 * while "match" determines to which maps the function should be applied.
2908 static __isl_give isl_union_map *preimage_multi_pw_aff(
2909 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
2910 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
2911 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2912 __isl_take isl_multi_pw_aff *mpa))
2914 isl_ctx *ctx;
2915 isl_space *space;
2916 struct isl_union_map_preimage_mpa_data data;
2918 umap = isl_union_map_align_params(umap,
2919 isl_multi_pw_aff_get_space(mpa));
2920 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
2922 if (!umap || !mpa)
2923 goto error;
2925 ctx = isl_union_map_get_ctx(umap);
2926 space = isl_union_map_get_space(umap);
2927 data.space = isl_multi_pw_aff_get_space(mpa);
2928 data.mpa = mpa;
2929 data.res = isl_union_map_alloc(space, umap->table.n);
2930 data.match = match;
2931 data.fn = fn;
2932 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
2933 &data) < 0)
2934 data.res = isl_union_map_free(data.res);
2936 isl_space_free(data.space);
2937 isl_union_map_free(umap);
2938 isl_multi_pw_aff_free(mpa);
2939 return data.res;
2940 error:
2941 isl_union_map_free(umap);
2942 isl_multi_pw_aff_free(mpa);
2943 return NULL;
2946 /* Compute the preimage of the domain of "umap" under the function
2947 * represented by "mpa".
2948 * In other words, plug in "mpa" in the domain of "umap".
2949 * The result contains maps that live in the same spaces as the maps of "umap"
2950 * with domain space equal to the target space of "mpa",
2951 * except that the domain has been replaced by the domain space of "mpa".
2953 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
2954 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
2956 return preimage_multi_pw_aff(umap, mpa, &domain_match,
2957 &isl_map_preimage_domain_multi_pw_aff);
2960 /* Internal data structure for preimage_upma.
2962 * "umap" is the map of which the preimage should be computed.
2963 * "res" collects the results.
2964 * "fn" computes the preimage for a given piecewise multi-affine function.
2966 struct isl_union_map_preimage_upma_data {
2967 isl_union_map *umap;
2968 isl_union_map *res;
2969 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
2970 __isl_take isl_pw_multi_aff *pma);
2973 /* Call data->fn to compute the preimage of the domain or range of data->umap
2974 * under the function represented by pma and add the result to data->res.
2976 static int preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
2978 struct isl_union_map_preimage_upma_data *data = user;
2979 isl_union_map *umap;
2981 umap = isl_union_map_copy(data->umap);
2982 umap = data->fn(umap, pma);
2983 data->res = isl_union_map_union(data->res, umap);
2985 return data->res ? 0 : -1;
2988 /* Compute the preimage of the domain or range of "umap" under the function
2989 * represented by "upma".
2990 * In other words, plug in "upma" in the domain or range of "umap".
2991 * The function "fn" performs the actual preimage computation
2992 * on a piecewise multi-affine function.
2994 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
2995 __isl_take isl_union_map *umap,
2996 __isl_take isl_union_pw_multi_aff *upma,
2997 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
2998 __isl_take isl_pw_multi_aff *pma))
3000 struct isl_union_map_preimage_upma_data data;
3002 data.umap = umap;
3003 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3004 data.fn = fn;
3005 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3006 &preimage_upma, &data) < 0)
3007 data.res = isl_union_map_free(data.res);
3009 isl_union_map_free(umap);
3010 isl_union_pw_multi_aff_free(upma);
3012 return data.res;
3015 /* Compute the preimage of the domain of "umap" under the function
3016 * represented by "upma".
3017 * In other words, plug in "upma" in the domain of "umap".
3018 * The result contains maps that live in the same spaces as the maps of "umap"
3019 * with domain space equal to one of the target spaces of "upma",
3020 * except that the domain has been replaced by one of the the domain spaces that
3021 * corresponds to that target space of "upma".
3023 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3024 __isl_take isl_union_map *umap,
3025 __isl_take isl_union_pw_multi_aff *upma)
3027 return preimage_union_pw_multi_aff(umap, upma,
3028 &isl_union_map_preimage_domain_pw_multi_aff);
3031 /* Compute the preimage of the range of "umap" under the function
3032 * represented by "upma".
3033 * In other words, plug in "upma" in the range of "umap".
3034 * The result contains maps that live in the same spaces as the maps of "umap"
3035 * with range space equal to one of the target spaces of "upma",
3036 * except that the range has been replaced by one of the the domain spaces that
3037 * corresponds to that target space of "upma".
3039 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3040 __isl_take isl_union_map *umap,
3041 __isl_take isl_union_pw_multi_aff *upma)
3043 return preimage_union_pw_multi_aff(umap, upma,
3044 &isl_union_map_preimage_range_pw_multi_aff);
3047 /* Compute the preimage of "uset" under the function represented by "upma".
3048 * In other words, plug in "upma" in the range of "uset".
3049 * The result contains sets that live in the same spaces as the sets of "uset"
3050 * with space equal to one of the target spaces of "upma",
3051 * except that the space has been replaced by one of the the domain spaces that
3052 * corresponds to that target space of "upma".
3054 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3055 __isl_take isl_union_set *uset,
3056 __isl_take isl_union_pw_multi_aff *upma)
3058 return preimage_union_pw_multi_aff(uset, upma,
3059 &isl_union_set_preimage_pw_multi_aff);
3062 /* Reset the user pointer on all identifiers of parameters and tuples
3063 * of the space of *entry.
3065 static int reset_user(void **entry, void *user)
3067 isl_map **map = (isl_map **)entry;
3069 *map = isl_map_reset_user(*map);
3071 return *map ? 0 : -1;
3074 /* Reset the user pointer on all identifiers of parameters and tuples
3075 * of the spaces of "umap".
3077 __isl_give isl_union_map *isl_union_map_reset_user(
3078 __isl_take isl_union_map *umap)
3080 umap = isl_union_map_cow(umap);
3081 if (!umap)
3082 return NULL;
3083 umap->dim = isl_space_reset_user(umap->dim);
3084 if (!umap->dim)
3085 return isl_union_map_free(umap);
3086 umap = un_op(umap, &reset_user);
3088 return umap;
3091 /* Reset the user pointer on all identifiers of parameters and tuples
3092 * of the spaces of "uset".
3094 __isl_give isl_union_set *isl_union_set_reset_user(
3095 __isl_take isl_union_set *uset)
3097 return isl_union_map_reset_user(uset);
3100 /* Internal data structure for isl_union_map_project_out.
3101 * "type", "first" and "n" are the arguments for the isl_map_project_out
3102 * call.
3103 * "res" collects the results.
3105 struct isl_union_map_project_out_data {
3106 enum isl_dim_type type;
3107 unsigned first;
3108 unsigned n;
3110 isl_union_map *res;
3113 /* Turn the data->n dimensions of type data->type, starting at data->first
3114 * into existentially quantified variables and add the result to data->res.
3116 static int project_out(__isl_take isl_map *map, void *user)
3118 struct isl_union_map_project_out_data *data = user;
3120 map = isl_map_project_out(map, data->type, data->first, data->n);
3121 data->res = isl_union_map_add_map(data->res, map);
3123 return 0;
3126 /* Turn the "n" dimensions of type "type", starting at "first"
3127 * into existentially quantified variables.
3128 * Since the space of an isl_union_map only contains parameters,
3129 * type is required to be equal to isl_dim_param.
3131 __isl_give isl_union_map *isl_union_map_project_out(
3132 __isl_take isl_union_map *umap,
3133 enum isl_dim_type type, unsigned first, unsigned n)
3135 isl_space *space;
3136 struct isl_union_map_project_out_data data = { type, first, n };
3138 if (!umap)
3139 return NULL;
3141 if (type != isl_dim_param)
3142 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3143 "can only project out parameters",
3144 return isl_union_map_free(umap));
3146 space = isl_union_map_get_space(umap);
3147 space = isl_space_drop_dims(space, type, first, n);
3148 data.res = isl_union_map_empty(space);
3149 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3150 data.res = isl_union_map_free(data.res);
3152 isl_union_map_free(umap);
3154 return data.res;