add isl_local_space_is_params
[isl.git] / isl_union_map.c
blob4e52441c8a800d015c6503439cb3513adcab8bd8
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 id of the specified dimension.
42 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
43 enum isl_dim_type type, unsigned pos)
45 if (!umap)
46 return NULL;
48 if (type != isl_dim_param)
49 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
50 "can only reference parameters", return NULL);
52 return isl_space_get_dim_id(umap->dim, type, pos);
55 /* Is this union set a parameter domain?
57 int isl_union_set_is_params(__isl_keep isl_union_set *uset)
59 isl_set *set;
60 int params;
62 if (!uset)
63 return -1;
64 if (uset->table.n != 1)
65 return 0;
67 set = isl_set_from_union_set(isl_union_set_copy(uset));
68 params = isl_set_is_params(set);
69 isl_set_free(set);
70 return params;
73 static __isl_give isl_union_map *isl_union_map_alloc(
74 __isl_take isl_space *space, int size)
76 isl_union_map *umap;
78 space = isl_space_params(space);
79 if (!space)
80 return NULL;
82 umap = isl_calloc_type(space->ctx, isl_union_map);
83 if (!umap) {
84 isl_space_free(space);
85 return NULL;
88 umap->ref = 1;
89 umap->dim = space;
90 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
91 return isl_union_map_free(umap);
93 return umap;
96 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
98 return isl_union_map_alloc(dim, 16);
101 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
103 return isl_union_map_empty(dim);
106 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
108 return umap ? umap->dim->ctx : NULL;
111 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
113 return uset ? uset->dim->ctx : NULL;
116 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
118 if (!umap)
119 return NULL;
120 return isl_space_copy(umap->dim);
123 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
125 return isl_union_map_get_space(uset);
128 static int free_umap_entry(void **entry, void *user)
130 isl_map *map = *entry;
131 isl_map_free(map);
132 return 0;
135 static int add_map(__isl_take isl_map *map, void *user)
137 isl_union_map **umap = (isl_union_map **)user;
139 *umap = isl_union_map_add_map(*umap, map);
141 return 0;
144 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
146 isl_union_map *dup;
148 if (!umap)
149 return NULL;
151 dup = isl_union_map_empty(isl_space_copy(umap->dim));
152 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
153 goto error;
154 return dup;
155 error:
156 isl_union_map_free(dup);
157 return NULL;
160 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
162 if (!umap)
163 return NULL;
165 if (umap->ref == 1)
166 return umap;
167 umap->ref--;
168 return isl_union_map_dup(umap);
171 struct isl_union_align {
172 isl_reordering *exp;
173 isl_union_map *res;
176 static int align_entry(void **entry, void *user)
178 isl_map *map = *entry;
179 isl_reordering *exp;
180 struct isl_union_align *data = user;
182 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
183 isl_map_get_space(map));
185 data->res = isl_union_map_add_map(data->res,
186 isl_map_realign(isl_map_copy(map), exp));
188 return 0;
191 /* Align the parameters of umap along those of model.
192 * The result has the parameters of model first, in the same order
193 * as they appear in model, followed by any remaining parameters of
194 * umap that do not appear in model.
196 __isl_give isl_union_map *isl_union_map_align_params(
197 __isl_take isl_union_map *umap, __isl_take isl_space *model)
199 struct isl_union_align data = { NULL, NULL };
201 if (!umap || !model)
202 goto error;
204 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
205 isl_space_free(model);
206 return umap;
209 model = isl_space_params(model);
210 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
211 if (!data.exp)
212 goto error;
214 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
215 umap->table.n);
216 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
217 &align_entry, &data) < 0)
218 goto error;
220 isl_reordering_free(data.exp);
221 isl_union_map_free(umap);
222 isl_space_free(model);
223 return data.res;
224 error:
225 isl_reordering_free(data.exp);
226 isl_union_map_free(umap);
227 isl_union_map_free(data.res);
228 isl_space_free(model);
229 return NULL;
232 __isl_give isl_union_set *isl_union_set_align_params(
233 __isl_take isl_union_set *uset, __isl_take isl_space *model)
235 return isl_union_map_align_params(uset, model);
238 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
239 __isl_take isl_union_map *umap2)
241 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
242 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
244 umap1 = isl_union_map_cow(umap1);
246 if (!umap1 || !umap2)
247 goto error;
249 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
250 goto error;
252 isl_union_map_free(umap2);
254 return umap1;
255 error:
256 isl_union_map_free(umap1);
257 isl_union_map_free(umap2);
258 return NULL;
261 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
262 __isl_take isl_union_set *uset2)
264 return isl_union_map_union(uset1, uset2);
267 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
269 if (!umap)
270 return NULL;
272 umap->ref++;
273 return umap;
276 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
278 return isl_union_map_copy(uset);
281 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
283 if (!umap)
284 return NULL;
286 if (--umap->ref > 0)
287 return NULL;
289 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
290 &free_umap_entry, NULL);
291 isl_hash_table_clear(&umap->table);
292 isl_space_free(umap->dim);
293 free(umap);
294 return NULL;
297 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
299 return isl_union_map_free(uset);
302 static int has_dim(const void *entry, const void *val)
304 isl_map *map = (isl_map *)entry;
305 isl_space *dim = (isl_space *)val;
307 return isl_space_is_equal(map->dim, dim);
310 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
311 __isl_take isl_map *map)
313 uint32_t hash;
314 struct isl_hash_table_entry *entry;
316 if (!map || !umap)
317 goto error;
319 if (isl_map_plain_is_empty(map)) {
320 isl_map_free(map);
321 return umap;
324 if (!isl_space_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
325 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
326 map = isl_map_align_params(map, isl_union_map_get_space(umap));
329 umap = isl_union_map_cow(umap);
331 if (!map || !umap)
332 goto error;
334 hash = isl_space_get_hash(map->dim);
335 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
336 &has_dim, map->dim, 1);
337 if (!entry)
338 goto error;
340 if (!entry->data)
341 entry->data = map;
342 else {
343 entry->data = isl_map_union(entry->data, isl_map_copy(map));
344 if (!entry->data)
345 goto error;
346 isl_map_free(map);
349 return umap;
350 error:
351 isl_map_free(map);
352 isl_union_map_free(umap);
353 return NULL;
356 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
357 __isl_take isl_set *set)
359 return isl_union_map_add_map(uset, (isl_map *)set);
362 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
364 isl_space *dim;
365 isl_union_map *umap;
367 if (!map)
368 return NULL;
370 dim = isl_map_get_space(map);
371 dim = isl_space_params(dim);
372 umap = isl_union_map_empty(dim);
373 umap = isl_union_map_add_map(umap, map);
375 return umap;
378 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
380 return isl_union_map_from_map((isl_map *)set);
383 __isl_give isl_union_map *isl_union_map_from_basic_map(
384 __isl_take isl_basic_map *bmap)
386 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
389 __isl_give isl_union_set *isl_union_set_from_basic_set(
390 __isl_take isl_basic_set *bset)
392 return isl_union_map_from_basic_map(bset);
395 struct isl_union_map_foreach_data
397 int (*fn)(__isl_take isl_map *map, void *user);
398 void *user;
401 static int call_on_copy(void **entry, void *user)
403 isl_map *map = *entry;
404 struct isl_union_map_foreach_data *data;
405 data = (struct isl_union_map_foreach_data *)user;
407 return data->fn(isl_map_copy(map), data->user);
410 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
412 return umap ? umap->table.n : 0;
415 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
417 return uset ? uset->table.n : 0;
420 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
421 int (*fn)(__isl_take isl_map *map, void *user), void *user)
423 struct isl_union_map_foreach_data data = { fn, user };
425 if (!umap)
426 return -1;
428 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
429 &call_on_copy, &data);
432 static int copy_map(void **entry, void *user)
434 isl_map *map = *entry;
435 isl_map **map_p = user;
437 *map_p = isl_map_copy(map);
439 return -1;
442 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
444 isl_ctx *ctx;
445 isl_map *map = NULL;
447 if (!umap)
448 return NULL;
449 ctx = isl_union_map_get_ctx(umap);
450 if (umap->table.n != 1)
451 isl_die(ctx, isl_error_invalid,
452 "union map needs to contain elements in exactly "
453 "one space", goto error);
455 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
457 isl_union_map_free(umap);
459 return map;
460 error:
461 isl_union_map_free(umap);
462 return NULL;
465 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
467 return isl_map_from_union_map(uset);
470 /* Extract the map in "umap" that lives in the given space (ignoring
471 * parameters).
473 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
474 __isl_take isl_space *space)
476 uint32_t hash;
477 struct isl_hash_table_entry *entry;
479 space = isl_space_drop_dims(space, isl_dim_param,
480 0, isl_space_dim(space, isl_dim_param));
481 space = isl_space_align_params(space, isl_union_map_get_space(umap));
482 if (!umap || !space)
483 goto error;
485 hash = isl_space_get_hash(space);
486 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
487 &has_dim, space, 0);
488 if (!entry)
489 return isl_map_empty(space);
490 isl_space_free(space);
491 return isl_map_copy(entry->data);
492 error:
493 isl_space_free(space);
494 return NULL;
497 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
498 __isl_take isl_space *dim)
500 return (isl_set *)isl_union_map_extract_map(uset, dim);
503 /* Check if umap contains a map in the given space.
505 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
506 __isl_keep isl_space *dim)
508 uint32_t hash;
509 struct isl_hash_table_entry *entry;
511 if (!umap || !dim)
512 return -1;
514 hash = isl_space_get_hash(dim);
515 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
516 &has_dim, dim, 0);
517 return !!entry;
520 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
521 __isl_keep isl_space *dim)
523 return isl_union_map_contains(uset, dim);
526 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
527 int (*fn)(__isl_take isl_set *set, void *user), void *user)
529 return isl_union_map_foreach_map(uset,
530 (int(*)(__isl_take isl_map *, void*))fn, user);
533 struct isl_union_set_foreach_point_data {
534 int (*fn)(__isl_take isl_point *pnt, void *user);
535 void *user;
538 static int foreach_point(__isl_take isl_set *set, void *user)
540 struct isl_union_set_foreach_point_data *data = user;
541 int r;
543 r = isl_set_foreach_point(set, data->fn, data->user);
544 isl_set_free(set);
546 return r;
549 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
550 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
552 struct isl_union_set_foreach_point_data data = { fn, user };
553 return isl_union_set_foreach_set(uset, &foreach_point, &data);
556 struct isl_union_map_gen_bin_data {
557 isl_union_map *umap2;
558 isl_union_map *res;
561 static int subtract_entry(void **entry, void *user)
563 struct isl_union_map_gen_bin_data *data = user;
564 uint32_t hash;
565 struct isl_hash_table_entry *entry2;
566 isl_map *map = *entry;
568 hash = isl_space_get_hash(map->dim);
569 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
570 hash, &has_dim, map->dim, 0);
571 map = isl_map_copy(map);
572 if (entry2) {
573 int empty;
574 map = isl_map_subtract(map, isl_map_copy(entry2->data));
576 empty = isl_map_is_empty(map);
577 if (empty < 0) {
578 isl_map_free(map);
579 return -1;
581 if (empty) {
582 isl_map_free(map);
583 return 0;
586 data->res = isl_union_map_add_map(data->res, map);
588 return 0;
591 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
592 __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
594 struct isl_union_map_gen_bin_data data = { NULL, NULL };
596 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
597 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
599 if (!umap1 || !umap2)
600 goto error;
602 data.umap2 = umap2;
603 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
604 umap1->table.n);
605 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
606 fn, &data) < 0)
607 goto error;
609 isl_union_map_free(umap1);
610 isl_union_map_free(umap2);
611 return data.res;
612 error:
613 isl_union_map_free(umap1);
614 isl_union_map_free(umap2);
615 isl_union_map_free(data.res);
616 return NULL;
619 __isl_give isl_union_map *isl_union_map_subtract(
620 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
622 return gen_bin_op(umap1, umap2, &subtract_entry);
625 __isl_give isl_union_set *isl_union_set_subtract(
626 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
628 return isl_union_map_subtract(uset1, uset2);
631 struct isl_union_map_gen_bin_set_data {
632 isl_set *set;
633 isl_union_map *res;
636 static int intersect_params_entry(void **entry, void *user)
638 struct isl_union_map_gen_bin_set_data *data = user;
639 isl_map *map = *entry;
640 int empty;
642 map = isl_map_copy(map);
643 map = isl_map_intersect_params(map, isl_set_copy(data->set));
645 empty = isl_map_is_empty(map);
646 if (empty < 0) {
647 isl_map_free(map);
648 return -1;
651 data->res = isl_union_map_add_map(data->res, map);
653 return 0;
656 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
657 __isl_take isl_set *set, int (*fn)(void **, void *))
659 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
661 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
662 set = isl_set_align_params(set, isl_union_map_get_space(umap));
664 if (!umap || !set)
665 goto error;
667 data.set = set;
668 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
669 umap->table.n);
670 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
671 fn, &data) < 0)
672 goto error;
674 isl_union_map_free(umap);
675 isl_set_free(set);
676 return data.res;
677 error:
678 isl_union_map_free(umap);
679 isl_set_free(set);
680 isl_union_map_free(data.res);
681 return NULL;
684 __isl_give isl_union_map *isl_union_map_intersect_params(
685 __isl_take isl_union_map *umap, __isl_take isl_set *set)
687 return gen_bin_set_op(umap, set, &intersect_params_entry);
690 __isl_give isl_union_set *isl_union_set_intersect_params(
691 __isl_take isl_union_set *uset, __isl_take isl_set *set)
693 return isl_union_map_intersect_params(uset, set);
696 static __isl_give isl_union_map *union_map_intersect_params(
697 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
699 return isl_union_map_intersect_params(umap,
700 isl_set_from_union_set(uset));
703 static __isl_give isl_union_map *union_map_gist_params(
704 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
706 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
709 struct isl_union_map_match_bin_data {
710 isl_union_map *umap2;
711 isl_union_map *res;
712 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
715 static int match_bin_entry(void **entry, void *user)
717 struct isl_union_map_match_bin_data *data = user;
718 uint32_t hash;
719 struct isl_hash_table_entry *entry2;
720 isl_map *map = *entry;
721 int empty;
723 hash = isl_space_get_hash(map->dim);
724 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
725 hash, &has_dim, map->dim, 0);
726 if (!entry2)
727 return 0;
729 map = isl_map_copy(map);
730 map = data->fn(map, isl_map_copy(entry2->data));
732 empty = isl_map_is_empty(map);
733 if (empty < 0) {
734 isl_map_free(map);
735 return -1;
737 if (empty) {
738 isl_map_free(map);
739 return 0;
742 data->res = isl_union_map_add_map(data->res, map);
744 return 0;
747 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
748 __isl_take isl_union_map *umap2,
749 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
751 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
753 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
754 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
756 if (!umap1 || !umap2)
757 goto error;
759 data.umap2 = umap2;
760 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
761 umap1->table.n);
762 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
763 &match_bin_entry, &data) < 0)
764 goto error;
766 isl_union_map_free(umap1);
767 isl_union_map_free(umap2);
768 return data.res;
769 error:
770 isl_union_map_free(umap1);
771 isl_union_map_free(umap2);
772 isl_union_map_free(data.res);
773 return NULL;
776 __isl_give isl_union_map *isl_union_map_intersect(
777 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
779 return match_bin_op(umap1, umap2, &isl_map_intersect);
782 /* Compute the intersection of the two union_sets.
783 * As a special case, if exactly one of the two union_sets
784 * is a parameter domain, then intersect the parameter domain
785 * of the other one with this set.
787 __isl_give isl_union_set *isl_union_set_intersect(
788 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
790 int p1, p2;
792 p1 = isl_union_set_is_params(uset1);
793 p2 = isl_union_set_is_params(uset2);
794 if (p1 < 0 || p2 < 0)
795 goto error;
796 if (!p1 && p2)
797 return union_map_intersect_params(uset1, uset2);
798 if (p1 && !p2)
799 return union_map_intersect_params(uset2, uset1);
800 return isl_union_map_intersect(uset1, uset2);
801 error:
802 isl_union_set_free(uset1);
803 isl_union_set_free(uset2);
804 return NULL;
807 static int gist_params_entry(void **entry, void *user)
809 struct isl_union_map_gen_bin_set_data *data = user;
810 isl_map *map = *entry;
811 int empty;
813 map = isl_map_copy(map);
814 map = isl_map_gist_params(map, isl_set_copy(data->set));
816 empty = isl_map_is_empty(map);
817 if (empty < 0) {
818 isl_map_free(map);
819 return -1;
822 data->res = isl_union_map_add_map(data->res, map);
824 return 0;
827 __isl_give isl_union_map *isl_union_map_gist_params(
828 __isl_take isl_union_map *umap, __isl_take isl_set *set)
830 return gen_bin_set_op(umap, set, &gist_params_entry);
833 __isl_give isl_union_set *isl_union_set_gist_params(
834 __isl_take isl_union_set *uset, __isl_take isl_set *set)
836 return isl_union_map_gist_params(uset, set);
839 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
840 __isl_take isl_union_map *context)
842 return match_bin_op(umap, context, &isl_map_gist);
845 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
846 __isl_take isl_union_set *context)
848 if (isl_union_set_is_params(context))
849 return union_map_gist_params(uset, context);
850 return isl_union_map_gist(uset, context);
853 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
854 __isl_take isl_map *set2)
856 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
859 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
860 __isl_take isl_map *set2)
862 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
865 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
866 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
868 return match_bin_op(uset1, uset2, &lex_lt_set);
871 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
872 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
874 return match_bin_op(uset1, uset2, &lex_le_set);
877 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
878 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
880 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
883 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
884 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
886 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
889 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
890 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
892 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
895 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
896 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
898 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
901 static int intersect_domain_entry(void **entry, void *user)
903 struct isl_union_map_gen_bin_data *data = user;
904 uint32_t hash;
905 struct isl_hash_table_entry *entry2;
906 isl_space *dim;
907 isl_map *map = *entry;
908 int empty;
910 dim = isl_map_get_space(map);
911 dim = isl_space_domain(dim);
912 hash = isl_space_get_hash(dim);
913 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
914 hash, &has_dim, dim, 0);
915 isl_space_free(dim);
916 if (!entry2)
917 return 0;
919 map = isl_map_copy(map);
920 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
922 empty = isl_map_is_empty(map);
923 if (empty < 0) {
924 isl_map_free(map);
925 return -1;
927 if (empty) {
928 isl_map_free(map);
929 return 0;
932 data->res = isl_union_map_add_map(data->res, map);
934 return 0;
937 /* Intersect the domain of "umap" with "uset".
938 * If "uset" is a parameters domain, then intersect the parameter
939 * domain of "umap" with this set.
941 __isl_give isl_union_map *isl_union_map_intersect_domain(
942 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
944 if (isl_union_set_is_params(uset))
945 return union_map_intersect_params(umap, uset);
946 return gen_bin_op(umap, uset, &intersect_domain_entry);
949 /* Remove the elements of data->umap2 from the domain of *entry
950 * and add the result to data->res.
952 static int subtract_domain_entry(void **entry, void *user)
954 struct isl_union_map_gen_bin_data *data = user;
955 uint32_t hash;
956 struct isl_hash_table_entry *entry2;
957 isl_space *dim;
958 isl_map *map = *entry;
959 int empty;
961 dim = isl_map_get_space(map);
962 dim = isl_space_domain(dim);
963 hash = isl_space_get_hash(dim);
964 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
965 hash, &has_dim, dim, 0);
966 isl_space_free(dim);
968 map = isl_map_copy(map);
970 if (!entry2) {
971 data->res = isl_union_map_add_map(data->res, map);
972 return 0;
975 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
977 empty = isl_map_is_empty(map);
978 if (empty < 0) {
979 isl_map_free(map);
980 return -1;
982 if (empty) {
983 isl_map_free(map);
984 return 0;
987 data->res = isl_union_map_add_map(data->res, map);
989 return 0;
992 /* Remove the elements of "uset" from the domain of "umap".
994 __isl_give isl_union_map *isl_union_map_subtract_domain(
995 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
997 return gen_bin_op(umap, dom, &subtract_domain_entry);
1000 /* Remove the elements of data->umap2 from the range of *entry
1001 * and add the result to data->res.
1003 static int subtract_range_entry(void **entry, void *user)
1005 struct isl_union_map_gen_bin_data *data = user;
1006 uint32_t hash;
1007 struct isl_hash_table_entry *entry2;
1008 isl_space *space;
1009 isl_map *map = *entry;
1010 int empty;
1012 space = isl_map_get_space(map);
1013 space = isl_space_range(space);
1014 hash = isl_space_get_hash(space);
1015 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1016 hash, &has_dim, space, 0);
1017 isl_space_free(space);
1019 map = isl_map_copy(map);
1021 if (!entry2) {
1022 data->res = isl_union_map_add_map(data->res, map);
1023 return 0;
1026 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1028 empty = isl_map_is_empty(map);
1029 if (empty < 0) {
1030 isl_map_free(map);
1031 return -1;
1033 if (empty) {
1034 isl_map_free(map);
1035 return 0;
1038 data->res = isl_union_map_add_map(data->res, map);
1040 return 0;
1043 /* Remove the elements of "uset" from the range of "umap".
1045 __isl_give isl_union_map *isl_union_map_subtract_range(
1046 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1048 return gen_bin_op(umap, dom, &subtract_range_entry);
1051 static int gist_domain_entry(void **entry, void *user)
1053 struct isl_union_map_gen_bin_data *data = user;
1054 uint32_t hash;
1055 struct isl_hash_table_entry *entry2;
1056 isl_space *dim;
1057 isl_map *map = *entry;
1058 int empty;
1060 dim = isl_map_get_space(map);
1061 dim = isl_space_domain(dim);
1062 hash = isl_space_get_hash(dim);
1063 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1064 hash, &has_dim, dim, 0);
1065 isl_space_free(dim);
1066 if (!entry2)
1067 return 0;
1069 map = isl_map_copy(map);
1070 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1072 empty = isl_map_is_empty(map);
1073 if (empty < 0) {
1074 isl_map_free(map);
1075 return -1;
1078 data->res = isl_union_map_add_map(data->res, map);
1080 return 0;
1083 /* Compute the gist of "umap" with respect to the domain "uset".
1084 * If "uset" is a parameters domain, then compute the gist
1085 * with respect to this parameter domain.
1087 __isl_give isl_union_map *isl_union_map_gist_domain(
1088 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1090 if (isl_union_set_is_params(uset))
1091 return union_map_gist_params(umap, uset);
1092 return gen_bin_op(umap, uset, &gist_domain_entry);
1095 static int gist_range_entry(void **entry, void *user)
1097 struct isl_union_map_gen_bin_data *data = user;
1098 uint32_t hash;
1099 struct isl_hash_table_entry *entry2;
1100 isl_space *space;
1101 isl_map *map = *entry;
1102 int empty;
1104 space = isl_map_get_space(map);
1105 space = isl_space_range(space);
1106 hash = isl_space_get_hash(space);
1107 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1108 hash, &has_dim, space, 0);
1109 isl_space_free(space);
1110 if (!entry2)
1111 return 0;
1113 map = isl_map_copy(map);
1114 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1116 empty = isl_map_is_empty(map);
1117 if (empty < 0) {
1118 isl_map_free(map);
1119 return -1;
1122 data->res = isl_union_map_add_map(data->res, map);
1124 return 0;
1127 /* Compute the gist of "umap" with respect to the range "uset".
1129 __isl_give isl_union_map *isl_union_map_gist_range(
1130 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1132 return gen_bin_op(umap, uset, &gist_range_entry);
1135 static int intersect_range_entry(void **entry, void *user)
1137 struct isl_union_map_gen_bin_data *data = user;
1138 uint32_t hash;
1139 struct isl_hash_table_entry *entry2;
1140 isl_space *dim;
1141 isl_map *map = *entry;
1142 int empty;
1144 dim = isl_map_get_space(map);
1145 dim = isl_space_range(dim);
1146 hash = isl_space_get_hash(dim);
1147 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1148 hash, &has_dim, dim, 0);
1149 isl_space_free(dim);
1150 if (!entry2)
1151 return 0;
1153 map = isl_map_copy(map);
1154 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1156 empty = isl_map_is_empty(map);
1157 if (empty < 0) {
1158 isl_map_free(map);
1159 return -1;
1161 if (empty) {
1162 isl_map_free(map);
1163 return 0;
1166 data->res = isl_union_map_add_map(data->res, map);
1168 return 0;
1171 __isl_give isl_union_map *isl_union_map_intersect_range(
1172 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1174 return gen_bin_op(umap, uset, &intersect_range_entry);
1177 struct isl_union_map_bin_data {
1178 isl_union_map *umap2;
1179 isl_union_map *res;
1180 isl_map *map;
1181 int (*fn)(void **entry, void *user);
1184 static int apply_range_entry(void **entry, void *user)
1186 struct isl_union_map_bin_data *data = user;
1187 isl_map *map2 = *entry;
1188 int empty;
1190 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1191 map2->dim, isl_dim_in))
1192 return 0;
1194 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1196 empty = isl_map_is_empty(map2);
1197 if (empty < 0) {
1198 isl_map_free(map2);
1199 return -1;
1201 if (empty) {
1202 isl_map_free(map2);
1203 return 0;
1206 data->res = isl_union_map_add_map(data->res, map2);
1208 return 0;
1211 static int bin_entry(void **entry, void *user)
1213 struct isl_union_map_bin_data *data = user;
1214 isl_map *map = *entry;
1216 data->map = map;
1217 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1218 data->fn, data) < 0)
1219 return -1;
1221 return 0;
1224 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1225 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
1227 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1229 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1230 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1232 if (!umap1 || !umap2)
1233 goto error;
1235 data.umap2 = umap2;
1236 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1237 umap1->table.n);
1238 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1239 &bin_entry, &data) < 0)
1240 goto error;
1242 isl_union_map_free(umap1);
1243 isl_union_map_free(umap2);
1244 return data.res;
1245 error:
1246 isl_union_map_free(umap1);
1247 isl_union_map_free(umap2);
1248 isl_union_map_free(data.res);
1249 return NULL;
1252 __isl_give isl_union_map *isl_union_map_apply_range(
1253 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1255 return bin_op(umap1, umap2, &apply_range_entry);
1258 __isl_give isl_union_map *isl_union_map_apply_domain(
1259 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1261 umap1 = isl_union_map_reverse(umap1);
1262 umap1 = isl_union_map_apply_range(umap1, umap2);
1263 return isl_union_map_reverse(umap1);
1266 __isl_give isl_union_set *isl_union_set_apply(
1267 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1269 return isl_union_map_apply_range(uset, umap);
1272 static int map_lex_lt_entry(void **entry, void *user)
1274 struct isl_union_map_bin_data *data = user;
1275 isl_map *map2 = *entry;
1277 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1278 map2->dim, isl_dim_out))
1279 return 0;
1281 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1283 data->res = isl_union_map_add_map(data->res, map2);
1285 return 0;
1288 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1289 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1291 return bin_op(umap1, umap2, &map_lex_lt_entry);
1294 static int map_lex_le_entry(void **entry, void *user)
1296 struct isl_union_map_bin_data *data = user;
1297 isl_map *map2 = *entry;
1299 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1300 map2->dim, isl_dim_out))
1301 return 0;
1303 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1305 data->res = isl_union_map_add_map(data->res, map2);
1307 return 0;
1310 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1311 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1313 return bin_op(umap1, umap2, &map_lex_le_entry);
1316 static int product_entry(void **entry, void *user)
1318 struct isl_union_map_bin_data *data = user;
1319 isl_map *map2 = *entry;
1321 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1323 data->res = isl_union_map_add_map(data->res, map2);
1325 return 0;
1328 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1329 __isl_take isl_union_map *umap2)
1331 return bin_op(umap1, umap2, &product_entry);
1334 static int set_product_entry(void **entry, void *user)
1336 struct isl_union_map_bin_data *data = user;
1337 isl_set *set2 = *entry;
1339 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1341 data->res = isl_union_set_add_set(data->res, set2);
1343 return 0;
1346 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1347 __isl_take isl_union_set *uset2)
1349 return bin_op(uset1, uset2, &set_product_entry);
1352 static int domain_product_entry(void **entry, void *user)
1354 struct isl_union_map_bin_data *data = user;
1355 isl_map *map2 = *entry;
1357 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1358 map2->dim, isl_dim_out))
1359 return 0;
1361 map2 = isl_map_domain_product(isl_map_copy(data->map),
1362 isl_map_copy(map2));
1364 data->res = isl_union_map_add_map(data->res, map2);
1366 return 0;
1369 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1371 __isl_give isl_union_map *isl_union_map_domain_product(
1372 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1374 return bin_op(umap1, umap2, &domain_product_entry);
1377 static int range_product_entry(void **entry, void *user)
1379 struct isl_union_map_bin_data *data = user;
1380 isl_map *map2 = *entry;
1382 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1383 map2->dim, isl_dim_in))
1384 return 0;
1386 map2 = isl_map_range_product(isl_map_copy(data->map),
1387 isl_map_copy(map2));
1389 data->res = isl_union_map_add_map(data->res, map2);
1391 return 0;
1394 __isl_give isl_union_map *isl_union_map_range_product(
1395 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1397 return bin_op(umap1, umap2, &range_product_entry);
1400 static int flat_range_product_entry(void **entry, void *user)
1402 struct isl_union_map_bin_data *data = user;
1403 isl_map *map2 = *entry;
1405 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1406 map2->dim, isl_dim_in))
1407 return 0;
1409 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1410 isl_map_copy(map2));
1412 data->res = isl_union_map_add_map(data->res, map2);
1414 return 0;
1417 __isl_give isl_union_map *isl_union_map_flat_range_product(
1418 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1420 return bin_op(umap1, umap2, &flat_range_product_entry);
1423 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1424 int (*fn)(void **, void *))
1426 isl_union_set *res;
1428 if (!umap)
1429 return NULL;
1431 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1432 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1433 goto error;
1435 isl_union_map_free(umap);
1436 return res;
1437 error:
1438 isl_union_map_free(umap);
1439 isl_union_set_free(res);
1440 return NULL;
1443 static int from_range_entry(void **entry, void *user)
1445 isl_map *set = *entry;
1446 isl_union_set **res = user;
1448 *res = isl_union_map_add_map(*res,
1449 isl_map_from_range(isl_set_copy(set)));
1451 return 0;
1454 __isl_give isl_union_map *isl_union_map_from_range(
1455 __isl_take isl_union_set *uset)
1457 return cond_un_op(uset, &from_range_entry);
1460 __isl_give isl_union_map *isl_union_map_from_domain(
1461 __isl_take isl_union_set *uset)
1463 return isl_union_map_reverse(isl_union_map_from_range(uset));
1466 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1467 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1469 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1470 isl_union_map_from_range(range));
1473 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1474 int (*fn)(void **, void *))
1476 umap = isl_union_map_cow(umap);
1477 if (!umap)
1478 return NULL;
1480 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1481 goto error;
1483 return umap;
1484 error:
1485 isl_union_map_free(umap);
1486 return NULL;
1489 static int affine_entry(void **entry, void *user)
1491 isl_map **map = (isl_map **)entry;
1493 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1495 return *map ? 0 : -1;
1498 __isl_give isl_union_map *isl_union_map_affine_hull(
1499 __isl_take isl_union_map *umap)
1501 return un_op(umap, &affine_entry);
1504 __isl_give isl_union_set *isl_union_set_affine_hull(
1505 __isl_take isl_union_set *uset)
1507 return isl_union_map_affine_hull(uset);
1510 static int polyhedral_entry(void **entry, void *user)
1512 isl_map **map = (isl_map **)entry;
1514 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1516 return *map ? 0 : -1;
1519 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1520 __isl_take isl_union_map *umap)
1522 return un_op(umap, &polyhedral_entry);
1525 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1526 __isl_take isl_union_set *uset)
1528 return isl_union_map_polyhedral_hull(uset);
1531 static int simple_entry(void **entry, void *user)
1533 isl_map **map = (isl_map **)entry;
1535 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1537 return *map ? 0 : -1;
1540 __isl_give isl_union_map *isl_union_map_simple_hull(
1541 __isl_take isl_union_map *umap)
1543 return un_op(umap, &simple_entry);
1546 __isl_give isl_union_set *isl_union_set_simple_hull(
1547 __isl_take isl_union_set *uset)
1549 return isl_union_map_simple_hull(uset);
1552 static int inplace_entry(void **entry, void *user)
1554 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1555 isl_map **map = (isl_map **)entry;
1556 isl_map *copy;
1558 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1559 copy = fn(isl_map_copy(*map));
1560 if (!copy)
1561 return -1;
1563 isl_map_free(*map);
1564 *map = copy;
1566 return 0;
1569 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1570 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1572 if (!umap)
1573 return NULL;
1575 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1576 &inplace_entry, &fn) < 0)
1577 goto error;
1579 return umap;
1580 error:
1581 isl_union_map_free(umap);
1582 return NULL;
1585 __isl_give isl_union_map *isl_union_map_coalesce(
1586 __isl_take isl_union_map *umap)
1588 return inplace(umap, &isl_map_coalesce);
1591 __isl_give isl_union_set *isl_union_set_coalesce(
1592 __isl_take isl_union_set *uset)
1594 return isl_union_map_coalesce(uset);
1597 __isl_give isl_union_map *isl_union_map_detect_equalities(
1598 __isl_take isl_union_map *umap)
1600 return inplace(umap, &isl_map_detect_equalities);
1603 __isl_give isl_union_set *isl_union_set_detect_equalities(
1604 __isl_take isl_union_set *uset)
1606 return isl_union_map_detect_equalities(uset);
1609 __isl_give isl_union_map *isl_union_map_compute_divs(
1610 __isl_take isl_union_map *umap)
1612 return inplace(umap, &isl_map_compute_divs);
1615 __isl_give isl_union_set *isl_union_set_compute_divs(
1616 __isl_take isl_union_set *uset)
1618 return isl_union_map_compute_divs(uset);
1621 static int lexmin_entry(void **entry, void *user)
1623 isl_map **map = (isl_map **)entry;
1625 *map = isl_map_lexmin(*map);
1627 return *map ? 0 : -1;
1630 __isl_give isl_union_map *isl_union_map_lexmin(
1631 __isl_take isl_union_map *umap)
1633 return un_op(umap, &lexmin_entry);
1636 __isl_give isl_union_set *isl_union_set_lexmin(
1637 __isl_take isl_union_set *uset)
1639 return isl_union_map_lexmin(uset);
1642 static int lexmax_entry(void **entry, void *user)
1644 isl_map **map = (isl_map **)entry;
1646 *map = isl_map_lexmax(*map);
1648 return *map ? 0 : -1;
1651 __isl_give isl_union_map *isl_union_map_lexmax(
1652 __isl_take isl_union_map *umap)
1654 return un_op(umap, &lexmax_entry);
1657 __isl_give isl_union_set *isl_union_set_lexmax(
1658 __isl_take isl_union_set *uset)
1660 return isl_union_map_lexmax(uset);
1663 static int universe_entry(void **entry, void *user)
1665 isl_map *map = *entry;
1666 isl_union_map **res = user;
1668 map = isl_map_universe(isl_map_get_space(map));
1669 *res = isl_union_map_add_map(*res, map);
1671 return 0;
1674 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1676 return cond_un_op(umap, &universe_entry);
1679 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1681 return isl_union_map_universe(uset);
1684 static int reverse_entry(void **entry, void *user)
1686 isl_map *map = *entry;
1687 isl_union_map **res = user;
1689 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1691 return 0;
1694 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1696 return cond_un_op(umap, &reverse_entry);
1699 static int params_entry(void **entry, void *user)
1701 isl_map *map = *entry;
1702 isl_union_set **res = user;
1704 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1706 return 0;
1709 /* Compute the parameter domain of the given union map.
1711 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1713 int empty;
1715 empty = isl_union_map_is_empty(umap);
1716 if (empty < 0)
1717 goto error;
1718 if (empty) {
1719 isl_space *space;
1720 space = isl_union_map_get_space(umap);
1721 isl_union_map_free(umap);
1722 return isl_set_empty(space);
1724 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1725 error:
1726 isl_union_map_free(umap);
1727 return NULL;
1730 /* Compute the parameter domain of the given union set.
1732 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1734 return isl_union_map_params(uset);
1737 static int domain_entry(void **entry, void *user)
1739 isl_map *map = *entry;
1740 isl_union_set **res = user;
1742 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1744 return 0;
1747 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1749 return cond_un_op(umap, &domain_entry);
1752 static int range_entry(void **entry, void *user)
1754 isl_map *map = *entry;
1755 isl_union_set **res = user;
1757 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1759 return 0;
1762 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1764 return cond_un_op(umap, &range_entry);
1767 static int domain_map_entry(void **entry, void *user)
1769 isl_map *map = *entry;
1770 isl_union_set **res = user;
1772 *res = isl_union_map_add_map(*res,
1773 isl_map_domain_map(isl_map_copy(map)));
1775 return 0;
1778 __isl_give isl_union_map *isl_union_map_domain_map(
1779 __isl_take isl_union_map *umap)
1781 return cond_un_op(umap, &domain_map_entry);
1784 static int range_map_entry(void **entry, void *user)
1786 isl_map *map = *entry;
1787 isl_union_set **res = user;
1789 *res = isl_union_map_add_map(*res,
1790 isl_map_range_map(isl_map_copy(map)));
1792 return 0;
1795 __isl_give isl_union_map *isl_union_map_range_map(
1796 __isl_take isl_union_map *umap)
1798 return cond_un_op(umap, &range_map_entry);
1801 static int deltas_entry(void **entry, void *user)
1803 isl_map *map = *entry;
1804 isl_union_set **res = user;
1806 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1807 map->dim, isl_dim_out))
1808 return 0;
1810 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1812 return 0;
1815 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1817 return cond_un_op(umap, &deltas_entry);
1820 static int deltas_map_entry(void **entry, void *user)
1822 isl_map *map = *entry;
1823 isl_union_map **res = user;
1825 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1826 map->dim, isl_dim_out))
1827 return 0;
1829 *res = isl_union_map_add_map(*res,
1830 isl_map_deltas_map(isl_map_copy(map)));
1832 return 0;
1835 __isl_give isl_union_map *isl_union_map_deltas_map(
1836 __isl_take isl_union_map *umap)
1838 return cond_un_op(umap, &deltas_map_entry);
1841 static int identity_entry(void **entry, void *user)
1843 isl_set *set = *entry;
1844 isl_union_map **res = user;
1846 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1848 return 0;
1851 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1853 return cond_un_op(uset, &identity_entry);
1856 static int unwrap_entry(void **entry, void *user)
1858 isl_set *set = *entry;
1859 isl_union_set **res = user;
1861 if (!isl_set_is_wrapping(set))
1862 return 0;
1864 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1866 return 0;
1869 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1871 return cond_un_op(uset, &unwrap_entry);
1874 static int wrap_entry(void **entry, void *user)
1876 isl_map *map = *entry;
1877 isl_union_set **res = user;
1879 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1881 return 0;
1884 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1886 return cond_un_op(umap, &wrap_entry);
1889 struct isl_union_map_is_subset_data {
1890 isl_union_map *umap2;
1891 int is_subset;
1894 static int is_subset_entry(void **entry, void *user)
1896 struct isl_union_map_is_subset_data *data = user;
1897 uint32_t hash;
1898 struct isl_hash_table_entry *entry2;
1899 isl_map *map = *entry;
1901 hash = isl_space_get_hash(map->dim);
1902 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1903 hash, &has_dim, map->dim, 0);
1904 if (!entry2) {
1905 int empty = isl_map_is_empty(map);
1906 if (empty < 0)
1907 return -1;
1908 if (empty)
1909 return 0;
1910 data->is_subset = 0;
1911 return -1;
1914 data->is_subset = isl_map_is_subset(map, entry2->data);
1915 if (data->is_subset < 0 || !data->is_subset)
1916 return -1;
1918 return 0;
1921 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1922 __isl_keep isl_union_map *umap2)
1924 struct isl_union_map_is_subset_data data = { NULL, 1 };
1926 umap1 = isl_union_map_copy(umap1);
1927 umap2 = isl_union_map_copy(umap2);
1928 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1929 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1931 if (!umap1 || !umap2)
1932 goto error;
1934 data.umap2 = umap2;
1935 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1936 &is_subset_entry, &data) < 0 &&
1937 data.is_subset)
1938 goto error;
1940 isl_union_map_free(umap1);
1941 isl_union_map_free(umap2);
1943 return data.is_subset;
1944 error:
1945 isl_union_map_free(umap1);
1946 isl_union_map_free(umap2);
1947 return -1;
1950 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1951 __isl_keep isl_union_set *uset2)
1953 return isl_union_map_is_subset(uset1, uset2);
1956 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1957 __isl_keep isl_union_map *umap2)
1959 int is_subset;
1961 if (!umap1 || !umap2)
1962 return -1;
1963 is_subset = isl_union_map_is_subset(umap1, umap2);
1964 if (is_subset != 1)
1965 return is_subset;
1966 is_subset = isl_union_map_is_subset(umap2, umap1);
1967 return is_subset;
1970 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1971 __isl_keep isl_union_set *uset2)
1973 return isl_union_map_is_equal(uset1, uset2);
1976 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1977 __isl_keep isl_union_map *umap2)
1979 int is_subset;
1981 if (!umap1 || !umap2)
1982 return -1;
1983 is_subset = isl_union_map_is_subset(umap1, umap2);
1984 if (is_subset != 1)
1985 return is_subset;
1986 is_subset = isl_union_map_is_subset(umap2, umap1);
1987 if (is_subset == -1)
1988 return is_subset;
1989 return !is_subset;
1992 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1993 __isl_keep isl_union_set *uset2)
1995 return isl_union_map_is_strict_subset(uset1, uset2);
1998 static int sample_entry(void **entry, void *user)
2000 isl_basic_map **sample = (isl_basic_map **)user;
2001 isl_map *map = *entry;
2003 *sample = isl_map_sample(isl_map_copy(map));
2004 if (!*sample)
2005 return -1;
2006 if (!isl_basic_map_plain_is_empty(*sample))
2007 return -1;
2008 return 0;
2011 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2013 isl_basic_map *sample = NULL;
2015 if (!umap)
2016 return NULL;
2018 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2019 &sample_entry, &sample) < 0 &&
2020 !sample)
2021 goto error;
2023 if (!sample)
2024 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2026 isl_union_map_free(umap);
2028 return sample;
2029 error:
2030 isl_union_map_free(umap);
2031 return NULL;
2034 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2036 return (isl_basic_set *)isl_union_map_sample(uset);
2039 struct isl_forall_data {
2040 int res;
2041 int (*fn)(__isl_keep isl_map *map);
2044 static int forall_entry(void **entry, void *user)
2046 struct isl_forall_data *data = user;
2047 isl_map *map = *entry;
2049 data->res = data->fn(map);
2050 if (data->res < 0)
2051 return -1;
2053 if (!data->res)
2054 return -1;
2056 return 0;
2059 static int union_map_forall(__isl_keep isl_union_map *umap,
2060 int (*fn)(__isl_keep isl_map *map))
2062 struct isl_forall_data data = { 1, fn };
2064 if (!umap)
2065 return -1;
2067 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2068 &forall_entry, &data) < 0 && data.res)
2069 return -1;
2071 return data.res;
2074 struct isl_forall_user_data {
2075 int res;
2076 int (*fn)(__isl_keep isl_map *map, void *user);
2077 void *user;
2080 static int forall_user_entry(void **entry, void *user)
2082 struct isl_forall_user_data *data = user;
2083 isl_map *map = *entry;
2085 data->res = data->fn(map, data->user);
2086 if (data->res < 0)
2087 return -1;
2089 if (!data->res)
2090 return -1;
2092 return 0;
2095 /* Check if fn(map, user) returns true for all maps "map" in umap.
2097 static int union_map_forall_user(__isl_keep isl_union_map *umap,
2098 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
2100 struct isl_forall_user_data data = { 1, fn, user };
2102 if (!umap)
2103 return -1;
2105 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2106 &forall_user_entry, &data) < 0 && data.res)
2107 return -1;
2109 return data.res;
2112 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2114 return union_map_forall(umap, &isl_map_is_empty);
2117 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2119 return isl_union_map_is_empty(uset);
2122 static int is_subset_of_identity(__isl_keep isl_map *map)
2124 int is_subset;
2125 isl_space *dim;
2126 isl_map *id;
2128 if (!map)
2129 return -1;
2131 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2132 map->dim, isl_dim_out))
2133 return 0;
2135 dim = isl_map_get_space(map);
2136 id = isl_map_identity(dim);
2138 is_subset = isl_map_is_subset(map, id);
2140 isl_map_free(id);
2142 return is_subset;
2145 /* Given an isl_union_map that consists of a single map, check
2146 * if it is single-valued.
2148 static int single_map_is_single_valued(__isl_keep isl_union_map *umap)
2150 isl_map *map;
2151 int sv;
2153 umap = isl_union_map_copy(umap);
2154 map = isl_map_from_union_map(umap);
2155 sv = isl_map_is_single_valued(map);
2156 isl_map_free(map);
2158 return sv;
2161 /* Internal data structure for single_valued_on_domain.
2163 * "umap" is the union map to be tested.
2164 * "sv" is set to 1 as long as "umap" may still be single-valued.
2166 struct isl_union_map_is_sv_data {
2167 isl_union_map *umap;
2168 int sv;
2171 /* Check if the data->umap is single-valued on "set".
2173 * If data->umap consists of a single map on "set", then test it
2174 * as an isl_map.
2176 * Otherwise, compute
2178 * M \circ M^-1
2180 * check if the result is a subset of the identity mapping and
2181 * store the result in data->sv.
2183 * Terminate as soon as data->umap has been determined not to
2184 * be single-valued.
2186 static int single_valued_on_domain(__isl_take isl_set *set, void *user)
2188 struct isl_union_map_is_sv_data *data = user;
2189 isl_union_map *umap, *test;
2191 umap = isl_union_map_copy(data->umap);
2192 umap = isl_union_map_intersect_domain(umap,
2193 isl_union_set_from_set(set));
2195 if (isl_union_map_n_map(umap) == 1) {
2196 data->sv = single_map_is_single_valued(umap);
2197 isl_union_map_free(umap);
2198 } else {
2199 test = isl_union_map_reverse(isl_union_map_copy(umap));
2200 test = isl_union_map_apply_range(test, umap);
2202 data->sv = union_map_forall(test, &is_subset_of_identity);
2204 isl_union_map_free(test);
2207 if (data->sv < 0 || !data->sv)
2208 return -1;
2209 return 0;
2212 /* Check if the given map is single-valued.
2214 * If the union map consists of a single map, then test it as an isl_map.
2215 * Otherwise, check if the union map is single-valued on each of its
2216 * domain spaces.
2218 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2220 isl_union_map *universe;
2221 isl_union_set *domain;
2222 struct isl_union_map_is_sv_data data;
2224 if (isl_union_map_n_map(umap) == 1)
2225 return single_map_is_single_valued(umap);
2227 universe = isl_union_map_universe(isl_union_map_copy(umap));
2228 domain = isl_union_map_domain(universe);
2230 data.sv = 1;
2231 data.umap = umap;
2232 if (isl_union_set_foreach_set(domain,
2233 &single_valued_on_domain, &data) < 0 && data.sv)
2234 data.sv = -1;
2235 isl_union_set_free(domain);
2237 return data.sv;
2240 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2242 int in;
2244 umap = isl_union_map_copy(umap);
2245 umap = isl_union_map_reverse(umap);
2246 in = isl_union_map_is_single_valued(umap);
2247 isl_union_map_free(umap);
2249 return in;
2252 /* Represents a map that has a fixed value (v) for one of its
2253 * range dimensions.
2254 * The map in this structure is not reference counted, so it
2255 * is only valid while the isl_union_map from which it was
2256 * obtained is still alive.
2258 struct isl_fixed_map {
2259 isl_int v;
2260 isl_map *map;
2263 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2264 int n)
2266 int i;
2267 struct isl_fixed_map *v;
2269 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2270 if (!v)
2271 return NULL;
2272 for (i = 0; i < n; ++i)
2273 isl_int_init(v[i].v);
2274 return v;
2277 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2279 int i;
2281 if (!v)
2282 return;
2283 for (i = 0; i < n; ++i)
2284 isl_int_clear(v[i].v);
2285 free(v);
2288 /* Compare the "v" field of two isl_fixed_map structs.
2290 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2292 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2293 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2295 return isl_int_cmp(e1->v, e2->v);
2298 /* Internal data structure used while checking whether all maps
2299 * in a union_map have a fixed value for a given output dimension.
2300 * v is the list of maps, with the fixed value for the dimension
2301 * n is the number of maps considered so far
2302 * pos is the output dimension under investigation
2304 struct isl_fixed_dim_data {
2305 struct isl_fixed_map *v;
2306 int n;
2307 int pos;
2310 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2312 struct isl_fixed_dim_data *data = user;
2314 data->v[data->n].map = map;
2315 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2316 &data->v[data->n++].v);
2319 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2320 int first, int n_range);
2322 /* Given a list of the maps, with their fixed values at output dimension "pos",
2323 * check whether the ranges of the maps form an obvious partition.
2325 * We first sort the maps according to their fixed values.
2326 * If all maps have a different value, then we know the ranges form
2327 * a partition.
2328 * Otherwise, we collect the maps with the same fixed value and
2329 * check whether each such collection is obviously injective
2330 * based on later dimensions.
2332 static int separates(struct isl_fixed_map *v, int n,
2333 __isl_take isl_space *dim, int pos, int n_range)
2335 int i;
2337 if (!v)
2338 goto error;
2340 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2342 for (i = 0; i + 1 < n; ++i) {
2343 int j, k;
2344 isl_union_map *part;
2345 int injective;
2347 for (j = i + 1; j < n; ++j)
2348 if (isl_int_ne(v[i].v, v[j].v))
2349 break;
2351 if (j == i + 1)
2352 continue;
2354 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2355 for (k = i; k < j; ++k)
2356 part = isl_union_map_add_map(part,
2357 isl_map_copy(v[k].map));
2359 injective = plain_injective_on_range(part, pos + 1, n_range);
2360 if (injective < 0)
2361 goto error;
2362 if (!injective)
2363 break;
2365 i = j - 1;
2368 isl_space_free(dim);
2369 free_isl_fixed_map_array(v, n);
2370 return i + 1 >= n;
2371 error:
2372 isl_space_free(dim);
2373 free_isl_fixed_map_array(v, n);
2374 return -1;
2377 /* Check whether the maps in umap have obviously distinct ranges.
2378 * In particular, check for an output dimension in the range
2379 * [first,n_range) for which all maps have a fixed value
2380 * and then check if these values, possibly along with fixed values
2381 * at later dimensions, entail distinct ranges.
2383 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2384 int first, int n_range)
2386 isl_ctx *ctx;
2387 int n;
2388 struct isl_fixed_dim_data data = { NULL };
2390 ctx = isl_union_map_get_ctx(umap);
2392 n = isl_union_map_n_map(umap);
2393 if (!umap)
2394 goto error;
2396 if (n <= 1) {
2397 isl_union_map_free(umap);
2398 return 1;
2401 if (first >= n_range) {
2402 isl_union_map_free(umap);
2403 return 0;
2406 data.v = alloc_isl_fixed_map_array(ctx, n);
2407 if (!data.v)
2408 goto error;
2410 for (data.pos = first; data.pos < n_range; ++data.pos) {
2411 int fixed;
2412 int injective;
2413 isl_space *dim;
2415 data.n = 0;
2416 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2417 if (fixed < 0)
2418 goto error;
2419 if (!fixed)
2420 continue;
2421 dim = isl_union_map_get_space(umap);
2422 injective = separates(data.v, n, dim, data.pos, n_range);
2423 isl_union_map_free(umap);
2424 return injective;
2427 free_isl_fixed_map_array(data.v, n);
2428 isl_union_map_free(umap);
2430 return 0;
2431 error:
2432 free_isl_fixed_map_array(data.v, n);
2433 isl_union_map_free(umap);
2434 return -1;
2437 /* Check whether the maps in umap that map to subsets of "ran"
2438 * have obviously distinct ranges.
2440 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2442 isl_union_map *umap = user;
2444 umap = isl_union_map_copy(umap);
2445 umap = isl_union_map_intersect_range(umap,
2446 isl_union_set_from_set(isl_set_copy(ran)));
2447 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2450 /* Check if the given union_map is obviously injective.
2452 * In particular, we first check if all individual maps are obviously
2453 * injective and then check if all the ranges of these maps are
2454 * obviously disjoint.
2456 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2458 int in;
2459 isl_union_map *univ;
2460 isl_union_set *ran;
2462 in = union_map_forall(umap, &isl_map_plain_is_injective);
2463 if (in < 0)
2464 return -1;
2465 if (!in)
2466 return 0;
2468 univ = isl_union_map_universe(isl_union_map_copy(umap));
2469 ran = isl_union_map_range(univ);
2471 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2473 isl_union_set_free(ran);
2475 return in;
2478 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2480 int sv;
2482 sv = isl_union_map_is_single_valued(umap);
2483 if (sv < 0 || !sv)
2484 return sv;
2486 return isl_union_map_is_injective(umap);
2489 static int zip_entry(void **entry, void *user)
2491 isl_map *map = *entry;
2492 isl_union_map **res = user;
2494 if (!isl_map_can_zip(map))
2495 return 0;
2497 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2499 return 0;
2502 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2504 return cond_un_op(umap, &zip_entry);
2507 static int uncurry_entry(void **entry, void *user)
2509 isl_map *map = *entry;
2510 isl_union_map **res = user;
2512 if (!isl_map_can_uncurry(map))
2513 return 0;
2515 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
2517 return 0;
2520 /* Given a union map, take the maps of the form A -> (B -> C) and
2521 * return the union of the corresponding maps (A -> B) -> C.
2523 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2525 return cond_un_op(umap, &uncurry_entry);
2528 static int curry_entry(void **entry, void *user)
2530 isl_map *map = *entry;
2531 isl_union_map **res = user;
2533 if (!isl_map_can_curry(map))
2534 return 0;
2536 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2538 return 0;
2541 /* Given a union map, take the maps of the form (A -> B) -> C and
2542 * return the union of the corresponding maps A -> (B -> C).
2544 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2546 return cond_un_op(umap, &curry_entry);
2549 static int lift_entry(void **entry, void *user)
2551 isl_set *set = *entry;
2552 isl_union_set **res = user;
2554 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2556 return 0;
2559 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2561 return cond_un_op(uset, &lift_entry);
2564 static int coefficients_entry(void **entry, void *user)
2566 isl_set *set = *entry;
2567 isl_union_set **res = user;
2569 set = isl_set_copy(set);
2570 set = isl_set_from_basic_set(isl_set_coefficients(set));
2571 *res = isl_union_set_add_set(*res, set);
2573 return 0;
2576 __isl_give isl_union_set *isl_union_set_coefficients(
2577 __isl_take isl_union_set *uset)
2579 isl_ctx *ctx;
2580 isl_space *dim;
2581 isl_union_set *res;
2583 if (!uset)
2584 return NULL;
2586 ctx = isl_union_set_get_ctx(uset);
2587 dim = isl_space_set_alloc(ctx, 0, 0);
2588 res = isl_union_map_alloc(dim, uset->table.n);
2589 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2590 &coefficients_entry, &res) < 0)
2591 goto error;
2593 isl_union_set_free(uset);
2594 return res;
2595 error:
2596 isl_union_set_free(uset);
2597 isl_union_set_free(res);
2598 return NULL;
2601 static int solutions_entry(void **entry, void *user)
2603 isl_set *set = *entry;
2604 isl_union_set **res = user;
2606 set = isl_set_copy(set);
2607 set = isl_set_from_basic_set(isl_set_solutions(set));
2608 if (!*res)
2609 *res = isl_union_set_from_set(set);
2610 else
2611 *res = isl_union_set_add_set(*res, set);
2613 if (!*res)
2614 return -1;
2616 return 0;
2619 __isl_give isl_union_set *isl_union_set_solutions(
2620 __isl_take isl_union_set *uset)
2622 isl_union_set *res = NULL;
2624 if (!uset)
2625 return NULL;
2627 if (uset->table.n == 0) {
2628 res = isl_union_set_empty(isl_union_set_get_space(uset));
2629 isl_union_set_free(uset);
2630 return res;
2633 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2634 &solutions_entry, &res) < 0)
2635 goto error;
2637 isl_union_set_free(uset);
2638 return res;
2639 error:
2640 isl_union_set_free(uset);
2641 isl_union_set_free(res);
2642 return NULL;
2645 /* Is the domain space of "map" equal to "space"?
2647 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2649 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2650 space, isl_dim_out);
2653 /* Is the range space of "map" equal to "space"?
2655 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2657 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
2658 space, isl_dim_out);
2661 /* Is the set space of "map" equal to "space"?
2663 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2665 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
2666 space, isl_dim_out);
2669 /* Internal data structure for preimage_pw_multi_aff.
2671 * "pma" is the function under which the preimage should be taken.
2672 * "space" is the space of "pma".
2673 * "res" collects the results.
2674 * "fn" computes the preimage for a given map.
2675 * "match" returns true if "fn" can be called.
2677 struct isl_union_map_preimage_data {
2678 isl_space *space;
2679 isl_pw_multi_aff *pma;
2680 isl_union_map *res;
2681 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2682 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2683 __isl_take isl_pw_multi_aff *pma);
2686 /* Call data->fn to compute the preimage of the domain or range of *entry
2687 * under the function represented by data->pma, provided the domain/range
2688 * space of *entry matches the target space of data->pma
2689 * (as given by data->match), and add the result to data->res.
2691 static int preimage_entry(void **entry, void *user)
2693 int m;
2694 isl_map *map = *entry;
2695 struct isl_union_map_preimage_data *data = user;
2696 int empty;
2698 m = data->match(map, data->space);
2699 if (m < 0)
2700 return -1;
2701 if (!m)
2702 return 0;
2704 map = isl_map_copy(map);
2705 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
2707 empty = isl_map_is_empty(map);
2708 if (empty < 0 || empty) {
2709 isl_map_free(map);
2710 return empty < 0 ? -1 : 0;
2713 data->res = isl_union_map_add_map(data->res, map);
2715 return 0;
2718 /* Compute the preimage of the domain or range of "umap" under the function
2719 * represented by "pma".
2720 * In other words, plug in "pma" in the domain or range of "umap".
2721 * The function "fn" performs the actual preimage computation on a map,
2722 * while "match" determines to which maps the function should be applied.
2724 static __isl_give isl_union_map *preimage_pw_multi_aff(
2725 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
2726 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
2727 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2728 __isl_take isl_pw_multi_aff *pma))
2730 isl_ctx *ctx;
2731 isl_space *space;
2732 struct isl_union_map_preimage_data data;
2734 umap = isl_union_map_align_params(umap,
2735 isl_pw_multi_aff_get_space(pma));
2736 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
2738 if (!umap || !pma)
2739 goto error;
2741 ctx = isl_union_map_get_ctx(umap);
2742 space = isl_union_map_get_space(umap);
2743 data.space = isl_pw_multi_aff_get_space(pma);
2744 data.pma = pma;
2745 data.res = isl_union_map_alloc(space, umap->table.n);
2746 data.match = match;
2747 data.fn = fn;
2748 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
2749 &data) < 0)
2750 data.res = isl_union_map_free(data.res);
2752 isl_space_free(data.space);
2753 isl_union_map_free(umap);
2754 isl_pw_multi_aff_free(pma);
2755 return data.res;
2756 error:
2757 isl_union_map_free(umap);
2758 isl_pw_multi_aff_free(pma);
2759 return NULL;
2762 /* Compute the preimage of the domain of "umap" under the function
2763 * represented by "pma".
2764 * In other words, plug in "pma" in the domain of "umap".
2765 * The result contains maps that live in the same spaces as the maps of "umap"
2766 * with domain space equal to the target space of "pma",
2767 * except that the domain has been replaced by the domain space of "pma".
2769 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
2770 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
2772 return preimage_pw_multi_aff(umap, pma, &domain_match,
2773 &isl_map_preimage_domain_pw_multi_aff);
2776 /* Compute the preimage of the range of "umap" under the function
2777 * represented by "pma".
2778 * In other words, plug in "pma" in the range of "umap".
2779 * The result contains maps that live in the same spaces as the maps of "umap"
2780 * with range space equal to the target space of "pma",
2781 * except that the range has been replaced by the domain space of "pma".
2783 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
2784 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
2786 return preimage_pw_multi_aff(umap, pma, &range_match,
2787 &isl_map_preimage_range_pw_multi_aff);
2790 /* Compute the preimage of "uset" under the function represented by "pma".
2791 * In other words, plug in "pma" in "uset".
2792 * The result contains sets that live in the same spaces as the sets of "uset"
2793 * with space equal to the target space of "pma",
2794 * except that the space has been replaced by the domain space of "pma".
2796 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
2797 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
2799 return preimage_pw_multi_aff(uset, pma, &set_match,
2800 &isl_set_preimage_pw_multi_aff);
2803 /* Compute the preimage of the domain of "umap" under the function
2804 * represented by "ma".
2805 * In other words, plug in "ma" in the domain of "umap".
2806 * The result contains maps that live in the same spaces as the maps of "umap"
2807 * with domain space equal to the target space of "ma",
2808 * except that the domain has been replaced by the domain space of "ma".
2810 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
2811 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
2813 return isl_union_map_preimage_domain_pw_multi_aff(umap,
2814 isl_pw_multi_aff_from_multi_aff(ma));
2817 /* Compute the preimage of the range of "umap" under the function
2818 * represented by "ma".
2819 * In other words, plug in "ma" in the range of "umap".
2820 * The result contains maps that live in the same spaces as the maps of "umap"
2821 * with range space equal to the target space of "ma",
2822 * except that the range has been replaced by the domain space of "ma".
2824 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
2825 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
2827 return isl_union_map_preimage_range_pw_multi_aff(umap,
2828 isl_pw_multi_aff_from_multi_aff(ma));
2831 /* Compute the preimage of "uset" under the function represented by "ma".
2832 * In other words, plug in "ma" in "uset".
2833 * The result contains sets that live in the same spaces as the sets of "uset"
2834 * with space equal to the target space of "ma",
2835 * except that the space has been replaced by the domain space of "ma".
2837 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
2838 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
2840 return isl_union_set_preimage_pw_multi_aff(uset,
2841 isl_pw_multi_aff_from_multi_aff(ma));
2844 /* Internal data structure for preimage_multi_pw_aff.
2846 * "mpa" is the function under which the preimage should be taken.
2847 * "space" is the space of "mpa".
2848 * "res" collects the results.
2849 * "fn" computes the preimage for a given map.
2850 * "match" returns true if "fn" can be called.
2852 struct isl_union_map_preimage_mpa_data {
2853 isl_space *space;
2854 isl_multi_pw_aff *mpa;
2855 isl_union_map *res;
2856 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2857 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2858 __isl_take isl_multi_pw_aff *mpa);
2861 /* Call data->fn to compute the preimage of the domain or range of *entry
2862 * under the function represented by data->mpa, provided the domain/range
2863 * space of *entry matches the target space of data->mpa
2864 * (as given by data->match), and add the result to data->res.
2866 static int preimage_mpa_entry(void **entry, void *user)
2868 int m;
2869 isl_map *map = *entry;
2870 struct isl_union_map_preimage_mpa_data *data = user;
2871 int empty;
2873 m = data->match(map, data->space);
2874 if (m < 0)
2875 return -1;
2876 if (!m)
2877 return 0;
2879 map = isl_map_copy(map);
2880 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
2882 empty = isl_map_is_empty(map);
2883 if (empty < 0 || empty) {
2884 isl_map_free(map);
2885 return empty < 0 ? -1 : 0;
2888 data->res = isl_union_map_add_map(data->res, map);
2890 return 0;
2893 /* Compute the preimage of the domain or range of "umap" under the function
2894 * represented by "mpa".
2895 * In other words, plug in "mpa" in the domain or range of "umap".
2896 * The function "fn" performs the actual preimage computation on a map,
2897 * while "match" determines to which maps the function should be applied.
2899 static __isl_give isl_union_map *preimage_multi_pw_aff(
2900 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
2901 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
2902 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2903 __isl_take isl_multi_pw_aff *mpa))
2905 isl_ctx *ctx;
2906 isl_space *space;
2907 struct isl_union_map_preimage_mpa_data data;
2909 umap = isl_union_map_align_params(umap,
2910 isl_multi_pw_aff_get_space(mpa));
2911 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
2913 if (!umap || !mpa)
2914 goto error;
2916 ctx = isl_union_map_get_ctx(umap);
2917 space = isl_union_map_get_space(umap);
2918 data.space = isl_multi_pw_aff_get_space(mpa);
2919 data.mpa = mpa;
2920 data.res = isl_union_map_alloc(space, umap->table.n);
2921 data.match = match;
2922 data.fn = fn;
2923 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
2924 &data) < 0)
2925 data.res = isl_union_map_free(data.res);
2927 isl_space_free(data.space);
2928 isl_union_map_free(umap);
2929 isl_multi_pw_aff_free(mpa);
2930 return data.res;
2931 error:
2932 isl_union_map_free(umap);
2933 isl_multi_pw_aff_free(mpa);
2934 return NULL;
2937 /* Compute the preimage of the domain of "umap" under the function
2938 * represented by "mpa".
2939 * In other words, plug in "mpa" in the domain of "umap".
2940 * The result contains maps that live in the same spaces as the maps of "umap"
2941 * with domain space equal to the target space of "mpa",
2942 * except that the domain has been replaced by the domain space of "mpa".
2944 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
2945 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
2947 return preimage_multi_pw_aff(umap, mpa, &domain_match,
2948 &isl_map_preimage_domain_multi_pw_aff);
2951 /* Internal data structure for preimage_upma.
2953 * "umap" is the map of which the preimage should be computed.
2954 * "res" collects the results.
2955 * "fn" computes the preimage for a given piecewise multi-affine function.
2957 struct isl_union_map_preimage_upma_data {
2958 isl_union_map *umap;
2959 isl_union_map *res;
2960 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
2961 __isl_take isl_pw_multi_aff *pma);
2964 /* Call data->fn to compute the preimage of the domain or range of data->umap
2965 * under the function represented by pma and add the result to data->res.
2967 static int preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
2969 struct isl_union_map_preimage_upma_data *data = user;
2970 isl_union_map *umap;
2972 umap = isl_union_map_copy(data->umap);
2973 umap = data->fn(umap, pma);
2974 data->res = isl_union_map_union(data->res, umap);
2976 return data->res ? 0 : -1;
2979 /* Compute the preimage of the domain or range of "umap" under the function
2980 * represented by "upma".
2981 * In other words, plug in "upma" in the domain or range of "umap".
2982 * The function "fn" performs the actual preimage computation
2983 * on a piecewise multi-affine function.
2985 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
2986 __isl_take isl_union_map *umap,
2987 __isl_take isl_union_pw_multi_aff *upma,
2988 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
2989 __isl_take isl_pw_multi_aff *pma))
2991 struct isl_union_map_preimage_upma_data data;
2993 data.umap = umap;
2994 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
2995 data.fn = fn;
2996 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
2997 &preimage_upma, &data) < 0)
2998 data.res = isl_union_map_free(data.res);
3000 isl_union_map_free(umap);
3001 isl_union_pw_multi_aff_free(upma);
3003 return data.res;
3006 /* Compute the preimage of the domain of "umap" under the function
3007 * represented by "upma".
3008 * In other words, plug in "upma" in the domain of "umap".
3009 * The result contains maps that live in the same spaces as the maps of "umap"
3010 * with domain space equal to one of the target spaces of "upma",
3011 * except that the domain has been replaced by one of the the domain spaces that
3012 * corresponds to that target space of "upma".
3014 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3015 __isl_take isl_union_map *umap,
3016 __isl_take isl_union_pw_multi_aff *upma)
3018 return preimage_union_pw_multi_aff(umap, upma,
3019 &isl_union_map_preimage_domain_pw_multi_aff);
3022 /* Compute the preimage of the range of "umap" under the function
3023 * represented by "upma".
3024 * In other words, plug in "upma" in the range of "umap".
3025 * The result contains maps that live in the same spaces as the maps of "umap"
3026 * with range space equal to one of the target spaces of "upma",
3027 * except that the range has been replaced by one of the the domain spaces that
3028 * corresponds to that target space of "upma".
3030 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3031 __isl_take isl_union_map *umap,
3032 __isl_take isl_union_pw_multi_aff *upma)
3034 return preimage_union_pw_multi_aff(umap, upma,
3035 &isl_union_map_preimage_range_pw_multi_aff);
3038 /* Compute the preimage of "uset" under the function represented by "upma".
3039 * In other words, plug in "upma" in the range of "uset".
3040 * The result contains sets that live in the same spaces as the sets of "uset"
3041 * with space equal to one of the target spaces of "upma",
3042 * except that the space has been replaced by one of the the domain spaces that
3043 * corresponds to that target space of "upma".
3045 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3046 __isl_take isl_union_set *uset,
3047 __isl_take isl_union_pw_multi_aff *upma)
3049 return preimage_union_pw_multi_aff(uset, upma,
3050 &isl_union_set_preimage_pw_multi_aff);
3053 /* Reset the user pointer on all identifiers of parameters and tuples
3054 * of the space of *entry.
3056 static int reset_user(void **entry, void *user)
3058 isl_map **map = (isl_map **)entry;
3060 *map = isl_map_reset_user(*map);
3062 return *map ? 0 : -1;
3065 /* Reset the user pointer on all identifiers of parameters and tuples
3066 * of the spaces of "umap".
3068 __isl_give isl_union_map *isl_union_map_reset_user(
3069 __isl_take isl_union_map *umap)
3071 umap = isl_union_map_cow(umap);
3072 if (!umap)
3073 return NULL;
3074 umap->dim = isl_space_reset_user(umap->dim);
3075 if (!umap->dim)
3076 return isl_union_map_free(umap);
3077 umap = un_op(umap, &reset_user);
3079 return umap;
3082 /* Reset the user pointer on all identifiers of parameters and tuples
3083 * of the spaces of "uset".
3085 __isl_give isl_union_set *isl_union_set_reset_user(
3086 __isl_take isl_union_set *uset)
3088 return isl_union_map_reset_user(uset);
3091 /* Internal data structure for isl_union_map_project_out.
3092 * "type", "first" and "n" are the arguments for the isl_map_project_out
3093 * call.
3094 * "res" collects the results.
3096 struct isl_union_map_project_out_data {
3097 enum isl_dim_type type;
3098 unsigned first;
3099 unsigned n;
3101 isl_union_map *res;
3104 /* Turn the data->n dimensions of type data->type, starting at data->first
3105 * into existentially quantified variables and add the result to data->res.
3107 static int project_out(__isl_take isl_map *map, void *user)
3109 struct isl_union_map_project_out_data *data = user;
3111 map = isl_map_project_out(map, data->type, data->first, data->n);
3112 data->res = isl_union_map_add_map(data->res, map);
3114 return 0;
3117 /* Turn the "n" dimensions of type "type", starting at "first"
3118 * into existentially quantified variables.
3119 * Since the space of an isl_union_map only contains parameters,
3120 * type is required to be equal to isl_dim_param.
3122 __isl_give isl_union_map *isl_union_map_project_out(
3123 __isl_take isl_union_map *umap,
3124 enum isl_dim_type type, unsigned first, unsigned n)
3126 isl_space *space;
3127 struct isl_union_map_project_out_data data = { type, first, n };
3129 if (!umap)
3130 return NULL;
3132 if (type != isl_dim_param)
3133 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3134 "can only project out parameters",
3135 return isl_union_map_free(umap));
3137 space = isl_union_map_get_space(umap);
3138 space = isl_space_drop_dims(space, type, first, n);
3139 data.res = isl_union_map_empty(space);
3140 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3141 data.res = isl_union_map_free(data.res);
3143 isl_union_map_free(umap);
3145 return data.res;