isl_polynomial.c: reduce_divs: do not assume input is a monomial
[isl.git] / isl_union_map.c
blob367af024a480390458b24e3b6ded54e28a9d5e19
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #define ISL_DIM_H
17 #include <isl_map_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl/ctx.h>
20 #include <isl/hash.h>
21 #include <isl/aff.h>
22 #include <isl/map.h>
23 #include <isl/set.h>
24 #include <isl_space_private.h>
25 #include <isl/union_set.h>
26 #include <isl/deprecated/union_map_int.h>
28 /* Return the number of parameters of "umap", where "type"
29 * is required to be set to isl_dim_param.
31 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
32 enum isl_dim_type type)
34 if (!umap)
35 return 0;
37 if (type != isl_dim_param)
38 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
39 "can only reference parameters", return 0);
41 return isl_space_dim(umap->dim, type);
44 /* Return the number of parameters of "uset", where "type"
45 * is required to be set to isl_dim_param.
47 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
48 enum isl_dim_type type)
50 return isl_union_map_dim(uset, type);
53 /* Return the id of the specified dimension.
55 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
56 enum isl_dim_type type, unsigned pos)
58 if (!umap)
59 return NULL;
61 if (type != isl_dim_param)
62 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
63 "can only reference parameters", return NULL);
65 return isl_space_get_dim_id(umap->dim, type, pos);
68 /* Is this union set a parameter domain?
70 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
72 isl_set *set;
73 isl_bool params;
75 if (!uset)
76 return isl_bool_error;
77 if (uset->table.n != 1)
78 return isl_bool_false;
80 set = isl_set_from_union_set(isl_union_set_copy(uset));
81 params = isl_set_is_params(set);
82 isl_set_free(set);
83 return params;
86 static __isl_give isl_union_map *isl_union_map_alloc(
87 __isl_take isl_space *space, int size)
89 isl_union_map *umap;
91 space = isl_space_params(space);
92 if (!space)
93 return NULL;
95 umap = isl_calloc_type(space->ctx, isl_union_map);
96 if (!umap) {
97 isl_space_free(space);
98 return NULL;
101 umap->ref = 1;
102 umap->dim = space;
103 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
104 return isl_union_map_free(umap);
106 return umap;
109 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
111 return isl_union_map_alloc(dim, 16);
114 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
116 return isl_union_map_empty(dim);
119 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
121 return umap ? umap->dim->ctx : NULL;
124 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
126 return uset ? uset->dim->ctx : NULL;
129 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
131 if (!umap)
132 return NULL;
133 return isl_space_copy(umap->dim);
136 /* Return the position of the parameter with the given name
137 * in "umap".
138 * Return -1 if no such dimension can be found.
140 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
141 enum isl_dim_type type, const char *name)
143 if (!umap)
144 return -1;
145 return isl_space_find_dim_by_name(umap->dim, type, name);
148 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
150 return isl_union_map_get_space(uset);
153 static isl_stat free_umap_entry(void **entry, void *user)
155 isl_map *map = *entry;
156 isl_map_free(map);
157 return isl_stat_ok;
160 static isl_stat add_map(__isl_take isl_map *map, void *user)
162 isl_union_map **umap = (isl_union_map **)user;
164 *umap = isl_union_map_add_map(*umap, map);
166 return isl_stat_ok;
169 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
171 isl_union_map *dup;
173 if (!umap)
174 return NULL;
176 dup = isl_union_map_empty(isl_space_copy(umap->dim));
177 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
178 goto error;
179 return dup;
180 error:
181 isl_union_map_free(dup);
182 return NULL;
185 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
187 if (!umap)
188 return NULL;
190 if (umap->ref == 1)
191 return umap;
192 umap->ref--;
193 return isl_union_map_dup(umap);
196 struct isl_union_align {
197 isl_reordering *exp;
198 isl_union_map *res;
201 static isl_stat align_entry(void **entry, void *user)
203 isl_map *map = *entry;
204 isl_reordering *exp;
205 struct isl_union_align *data = user;
207 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
208 isl_map_get_space(map));
210 data->res = isl_union_map_add_map(data->res,
211 isl_map_realign(isl_map_copy(map), exp));
213 return isl_stat_ok;
216 /* Align the parameters of umap along those of model.
217 * The result has the parameters of model first, in the same order
218 * as they appear in model, followed by any remaining parameters of
219 * umap that do not appear in model.
221 __isl_give isl_union_map *isl_union_map_align_params(
222 __isl_take isl_union_map *umap, __isl_take isl_space *model)
224 struct isl_union_align data = { NULL, NULL };
226 if (!umap || !model)
227 goto error;
229 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
230 isl_space_free(model);
231 return umap;
234 model = isl_space_params(model);
235 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
236 if (!data.exp)
237 goto error;
239 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
240 umap->table.n);
241 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
242 &align_entry, &data) < 0)
243 goto error;
245 isl_reordering_free(data.exp);
246 isl_union_map_free(umap);
247 isl_space_free(model);
248 return data.res;
249 error:
250 isl_reordering_free(data.exp);
251 isl_union_map_free(umap);
252 isl_union_map_free(data.res);
253 isl_space_free(model);
254 return NULL;
257 __isl_give isl_union_set *isl_union_set_align_params(
258 __isl_take isl_union_set *uset, __isl_take isl_space *model)
260 return isl_union_map_align_params(uset, model);
263 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
264 __isl_take isl_union_map *umap2)
266 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
267 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
269 umap1 = isl_union_map_cow(umap1);
271 if (!umap1 || !umap2)
272 goto error;
274 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
275 goto error;
277 isl_union_map_free(umap2);
279 return umap1;
280 error:
281 isl_union_map_free(umap1);
282 isl_union_map_free(umap2);
283 return NULL;
286 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
287 __isl_take isl_union_set *uset2)
289 return isl_union_map_union(uset1, uset2);
292 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
294 if (!umap)
295 return NULL;
297 umap->ref++;
298 return umap;
301 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
303 return isl_union_map_copy(uset);
306 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
308 if (!umap)
309 return NULL;
311 if (--umap->ref > 0)
312 return NULL;
314 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
315 &free_umap_entry, NULL);
316 isl_hash_table_clear(&umap->table);
317 isl_space_free(umap->dim);
318 free(umap);
319 return NULL;
322 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
324 return isl_union_map_free(uset);
327 static int has_dim(const void *entry, const void *val)
329 isl_map *map = (isl_map *)entry;
330 isl_space *dim = (isl_space *)val;
332 return isl_space_is_equal(map->dim, dim);
335 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
336 __isl_take isl_map *map)
338 uint32_t hash;
339 struct isl_hash_table_entry *entry;
341 if (!map || !umap)
342 goto error;
344 if (isl_map_plain_is_empty(map)) {
345 isl_map_free(map);
346 return umap;
349 if (!isl_space_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
350 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
351 map = isl_map_align_params(map, isl_union_map_get_space(umap));
354 umap = isl_union_map_cow(umap);
356 if (!map || !umap)
357 goto error;
359 hash = isl_space_get_hash(map->dim);
360 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
361 &has_dim, map->dim, 1);
362 if (!entry)
363 goto error;
365 if (!entry->data)
366 entry->data = map;
367 else {
368 entry->data = isl_map_union(entry->data, isl_map_copy(map));
369 if (!entry->data)
370 goto error;
371 isl_map_free(map);
374 return umap;
375 error:
376 isl_map_free(map);
377 isl_union_map_free(umap);
378 return NULL;
381 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
382 __isl_take isl_set *set)
384 return isl_union_map_add_map(uset, (isl_map *)set);
387 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
389 isl_space *dim;
390 isl_union_map *umap;
392 if (!map)
393 return NULL;
395 dim = isl_map_get_space(map);
396 dim = isl_space_params(dim);
397 umap = isl_union_map_empty(dim);
398 umap = isl_union_map_add_map(umap, map);
400 return umap;
403 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
405 return isl_union_map_from_map((isl_map *)set);
408 __isl_give isl_union_map *isl_union_map_from_basic_map(
409 __isl_take isl_basic_map *bmap)
411 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
414 __isl_give isl_union_set *isl_union_set_from_basic_set(
415 __isl_take isl_basic_set *bset)
417 return isl_union_map_from_basic_map(bset);
420 struct isl_union_map_foreach_data
422 isl_stat (*fn)(__isl_take isl_map *map, void *user);
423 void *user;
426 static isl_stat call_on_copy(void **entry, void *user)
428 isl_map *map = *entry;
429 struct isl_union_map_foreach_data *data;
430 data = (struct isl_union_map_foreach_data *)user;
432 return data->fn(isl_map_copy(map), data->user);
435 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
437 return umap ? umap->table.n : 0;
440 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
442 return uset ? uset->table.n : 0;
445 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
446 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
448 struct isl_union_map_foreach_data data = { fn, user };
450 if (!umap)
451 return isl_stat_error;
453 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
454 &call_on_copy, &data);
457 static isl_stat copy_map(void **entry, void *user)
459 isl_map *map = *entry;
460 isl_map **map_p = user;
462 *map_p = isl_map_copy(map);
464 return isl_stat_error;
467 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
469 isl_ctx *ctx;
470 isl_map *map = NULL;
472 if (!umap)
473 return NULL;
474 ctx = isl_union_map_get_ctx(umap);
475 if (umap->table.n != 1)
476 isl_die(ctx, isl_error_invalid,
477 "union map needs to contain elements in exactly "
478 "one space", goto error);
480 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
482 isl_union_map_free(umap);
484 return map;
485 error:
486 isl_union_map_free(umap);
487 return NULL;
490 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
492 return isl_map_from_union_map(uset);
495 /* Extract the map in "umap" that lives in the given space (ignoring
496 * parameters).
498 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
499 __isl_take isl_space *space)
501 uint32_t hash;
502 struct isl_hash_table_entry *entry;
504 space = isl_space_drop_dims(space, isl_dim_param,
505 0, isl_space_dim(space, isl_dim_param));
506 space = isl_space_align_params(space, isl_union_map_get_space(umap));
507 if (!umap || !space)
508 goto error;
510 hash = isl_space_get_hash(space);
511 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
512 &has_dim, space, 0);
513 if (!entry)
514 return isl_map_empty(space);
515 isl_space_free(space);
516 return isl_map_copy(entry->data);
517 error:
518 isl_space_free(space);
519 return NULL;
522 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
523 __isl_take isl_space *dim)
525 return (isl_set *)isl_union_map_extract_map(uset, dim);
528 /* Check if umap contains a map in the given space.
530 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
531 __isl_keep isl_space *dim)
533 uint32_t hash;
534 struct isl_hash_table_entry *entry;
536 if (!umap || !dim)
537 return -1;
539 hash = isl_space_get_hash(dim);
540 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
541 &has_dim, dim, 0);
542 return !!entry;
545 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
546 __isl_keep isl_space *dim)
548 return isl_union_map_contains(uset, dim);
551 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
552 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
554 return isl_union_map_foreach_map(uset,
555 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
558 struct isl_union_set_foreach_point_data {
559 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
560 void *user;
563 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
565 struct isl_union_set_foreach_point_data *data = user;
566 isl_stat r;
568 r = isl_set_foreach_point(set, data->fn, data->user);
569 isl_set_free(set);
571 return r;
574 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
575 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
577 struct isl_union_set_foreach_point_data data = { fn, user };
578 return isl_union_set_foreach_set(uset, &foreach_point, &data);
581 struct isl_union_map_gen_bin_data {
582 isl_union_map *umap2;
583 isl_union_map *res;
586 static isl_stat subtract_entry(void **entry, void *user)
588 struct isl_union_map_gen_bin_data *data = user;
589 uint32_t hash;
590 struct isl_hash_table_entry *entry2;
591 isl_map *map = *entry;
593 hash = isl_space_get_hash(map->dim);
594 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
595 hash, &has_dim, map->dim, 0);
596 map = isl_map_copy(map);
597 if (entry2) {
598 int empty;
599 map = isl_map_subtract(map, isl_map_copy(entry2->data));
601 empty = isl_map_is_empty(map);
602 if (empty < 0) {
603 isl_map_free(map);
604 return isl_stat_error;
606 if (empty) {
607 isl_map_free(map);
608 return isl_stat_ok;
611 data->res = isl_union_map_add_map(data->res, map);
613 return isl_stat_ok;
616 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
617 __isl_take isl_union_map *umap2, isl_stat (*fn)(void **, void *))
619 struct isl_union_map_gen_bin_data data = { NULL, NULL };
621 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
622 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
624 if (!umap1 || !umap2)
625 goto error;
627 data.umap2 = umap2;
628 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
629 umap1->table.n);
630 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
631 fn, &data) < 0)
632 goto error;
634 isl_union_map_free(umap1);
635 isl_union_map_free(umap2);
636 return data.res;
637 error:
638 isl_union_map_free(umap1);
639 isl_union_map_free(umap2);
640 isl_union_map_free(data.res);
641 return NULL;
644 __isl_give isl_union_map *isl_union_map_subtract(
645 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
647 return gen_bin_op(umap1, umap2, &subtract_entry);
650 __isl_give isl_union_set *isl_union_set_subtract(
651 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
653 return isl_union_map_subtract(uset1, uset2);
656 struct isl_union_map_gen_bin_set_data {
657 isl_set *set;
658 isl_union_map *res;
661 static isl_stat intersect_params_entry(void **entry, void *user)
663 struct isl_union_map_gen_bin_set_data *data = user;
664 isl_map *map = *entry;
665 int empty;
667 map = isl_map_copy(map);
668 map = isl_map_intersect_params(map, isl_set_copy(data->set));
670 empty = isl_map_is_empty(map);
671 if (empty < 0) {
672 isl_map_free(map);
673 return isl_stat_error;
676 data->res = isl_union_map_add_map(data->res, map);
678 return isl_stat_ok;
681 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
682 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
684 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
686 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
687 set = isl_set_align_params(set, isl_union_map_get_space(umap));
689 if (!umap || !set)
690 goto error;
692 data.set = set;
693 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
694 umap->table.n);
695 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
696 fn, &data) < 0)
697 goto error;
699 isl_union_map_free(umap);
700 isl_set_free(set);
701 return data.res;
702 error:
703 isl_union_map_free(umap);
704 isl_set_free(set);
705 isl_union_map_free(data.res);
706 return NULL;
709 /* Intersect "umap" with the parameter domain "set".
711 * If "set" does not have any constraints, then we can return immediately.
713 __isl_give isl_union_map *isl_union_map_intersect_params(
714 __isl_take isl_union_map *umap, __isl_take isl_set *set)
716 int is_universe;
718 is_universe = isl_set_plain_is_universe(set);
719 if (is_universe < 0)
720 goto error;
721 if (is_universe) {
722 isl_set_free(set);
723 return umap;
726 return gen_bin_set_op(umap, set, &intersect_params_entry);
727 error:
728 isl_union_map_free(umap);
729 isl_set_free(set);
730 return NULL;
733 __isl_give isl_union_set *isl_union_set_intersect_params(
734 __isl_take isl_union_set *uset, __isl_take isl_set *set)
736 return isl_union_map_intersect_params(uset, set);
739 static __isl_give isl_union_map *union_map_intersect_params(
740 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
742 return isl_union_map_intersect_params(umap,
743 isl_set_from_union_set(uset));
746 static __isl_give isl_union_map *union_map_gist_params(
747 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
749 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
752 struct isl_union_map_match_bin_data {
753 isl_union_map *umap2;
754 isl_union_map *res;
755 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
758 static isl_stat match_bin_entry(void **entry, void *user)
760 struct isl_union_map_match_bin_data *data = user;
761 uint32_t hash;
762 struct isl_hash_table_entry *entry2;
763 isl_map *map = *entry;
764 int empty;
766 hash = isl_space_get_hash(map->dim);
767 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
768 hash, &has_dim, map->dim, 0);
769 if (!entry2)
770 return isl_stat_ok;
772 map = isl_map_copy(map);
773 map = data->fn(map, isl_map_copy(entry2->data));
775 empty = isl_map_is_empty(map);
776 if (empty < 0) {
777 isl_map_free(map);
778 return isl_stat_error;
780 if (empty) {
781 isl_map_free(map);
782 return isl_stat_ok;
785 data->res = isl_union_map_add_map(data->res, map);
787 return isl_stat_ok;
790 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
791 __isl_take isl_union_map *umap2,
792 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
794 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
796 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
797 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
799 if (!umap1 || !umap2)
800 goto error;
802 data.umap2 = umap2;
803 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
804 umap1->table.n);
805 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
806 &match_bin_entry, &data) < 0)
807 goto error;
809 isl_union_map_free(umap1);
810 isl_union_map_free(umap2);
811 return data.res;
812 error:
813 isl_union_map_free(umap1);
814 isl_union_map_free(umap2);
815 isl_union_map_free(data.res);
816 return NULL;
819 __isl_give isl_union_map *isl_union_map_intersect(
820 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
822 return match_bin_op(umap1, umap2, &isl_map_intersect);
825 /* Compute the intersection of the two union_sets.
826 * As a special case, if exactly one of the two union_sets
827 * is a parameter domain, then intersect the parameter domain
828 * of the other one with this set.
830 __isl_give isl_union_set *isl_union_set_intersect(
831 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
833 int p1, p2;
835 p1 = isl_union_set_is_params(uset1);
836 p2 = isl_union_set_is_params(uset2);
837 if (p1 < 0 || p2 < 0)
838 goto error;
839 if (!p1 && p2)
840 return union_map_intersect_params(uset1, uset2);
841 if (p1 && !p2)
842 return union_map_intersect_params(uset2, uset1);
843 return isl_union_map_intersect(uset1, uset2);
844 error:
845 isl_union_set_free(uset1);
846 isl_union_set_free(uset2);
847 return NULL;
850 static isl_stat gist_params_entry(void **entry, void *user)
852 struct isl_union_map_gen_bin_set_data *data = user;
853 isl_map *map = *entry;
854 int empty;
856 map = isl_map_copy(map);
857 map = isl_map_gist_params(map, isl_set_copy(data->set));
859 empty = isl_map_is_empty(map);
860 if (empty < 0) {
861 isl_map_free(map);
862 return isl_stat_error;
865 data->res = isl_union_map_add_map(data->res, map);
867 return isl_stat_ok;
870 __isl_give isl_union_map *isl_union_map_gist_params(
871 __isl_take isl_union_map *umap, __isl_take isl_set *set)
873 return gen_bin_set_op(umap, set, &gist_params_entry);
876 __isl_give isl_union_set *isl_union_set_gist_params(
877 __isl_take isl_union_set *uset, __isl_take isl_set *set)
879 return isl_union_map_gist_params(uset, set);
882 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
883 __isl_take isl_union_map *context)
885 return match_bin_op(umap, context, &isl_map_gist);
888 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
889 __isl_take isl_union_set *context)
891 if (isl_union_set_is_params(context))
892 return union_map_gist_params(uset, context);
893 return isl_union_map_gist(uset, context);
896 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
897 __isl_take isl_map *set2)
899 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
902 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
903 __isl_take isl_map *set2)
905 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
908 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
909 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
911 return match_bin_op(uset1, uset2, &lex_lt_set);
914 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
915 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
917 return match_bin_op(uset1, uset2, &lex_le_set);
920 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
921 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
923 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
926 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
927 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
929 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
932 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
933 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
935 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
938 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
939 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
941 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
944 static isl_stat intersect_domain_entry(void **entry, void *user)
946 struct isl_union_map_gen_bin_data *data = user;
947 uint32_t hash;
948 struct isl_hash_table_entry *entry2;
949 isl_space *dim;
950 isl_map *map = *entry;
951 isl_bool empty;
953 dim = isl_map_get_space(map);
954 dim = isl_space_domain(dim);
955 hash = isl_space_get_hash(dim);
956 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
957 hash, &has_dim, dim, 0);
958 isl_space_free(dim);
959 if (!entry2)
960 return isl_stat_ok;
962 map = isl_map_copy(map);
963 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
965 empty = isl_map_is_empty(map);
966 if (empty < 0) {
967 isl_map_free(map);
968 return isl_stat_error;
970 if (empty) {
971 isl_map_free(map);
972 return isl_stat_ok;
975 data->res = isl_union_map_add_map(data->res, map);
977 return isl_stat_ok;
980 /* Intersect the domain of "umap" with "uset".
981 * If "uset" is a parameters domain, then intersect the parameter
982 * domain of "umap" with this set.
984 __isl_give isl_union_map *isl_union_map_intersect_domain(
985 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
987 if (isl_union_set_is_params(uset))
988 return union_map_intersect_params(umap, uset);
989 return gen_bin_op(umap, uset, &intersect_domain_entry);
992 /* Remove the elements of data->umap2 from the domain of *entry
993 * and add the result to data->res.
995 static isl_stat subtract_domain_entry(void **entry, void *user)
997 struct isl_union_map_gen_bin_data *data = user;
998 uint32_t hash;
999 struct isl_hash_table_entry *entry2;
1000 isl_space *dim;
1001 isl_map *map = *entry;
1002 isl_bool empty;
1004 dim = isl_map_get_space(map);
1005 dim = isl_space_domain(dim);
1006 hash = isl_space_get_hash(dim);
1007 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1008 hash, &has_dim, dim, 0);
1009 isl_space_free(dim);
1011 map = isl_map_copy(map);
1013 if (!entry2) {
1014 data->res = isl_union_map_add_map(data->res, map);
1015 return isl_stat_ok;
1018 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1020 empty = isl_map_is_empty(map);
1021 if (empty < 0) {
1022 isl_map_free(map);
1023 return isl_stat_error;
1025 if (empty) {
1026 isl_map_free(map);
1027 return isl_stat_ok;
1030 data->res = isl_union_map_add_map(data->res, map);
1032 return isl_stat_ok;
1035 /* Remove the elements of "uset" from the domain of "umap".
1037 __isl_give isl_union_map *isl_union_map_subtract_domain(
1038 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1040 return gen_bin_op(umap, dom, &subtract_domain_entry);
1043 /* Remove the elements of data->umap2 from the range of *entry
1044 * and add the result to data->res.
1046 static isl_stat subtract_range_entry(void **entry, void *user)
1048 struct isl_union_map_gen_bin_data *data = user;
1049 uint32_t hash;
1050 struct isl_hash_table_entry *entry2;
1051 isl_space *space;
1052 isl_map *map = *entry;
1053 isl_bool empty;
1055 space = isl_map_get_space(map);
1056 space = isl_space_range(space);
1057 hash = isl_space_get_hash(space);
1058 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1059 hash, &has_dim, space, 0);
1060 isl_space_free(space);
1062 map = isl_map_copy(map);
1064 if (!entry2) {
1065 data->res = isl_union_map_add_map(data->res, map);
1066 return isl_stat_ok;
1069 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1071 empty = isl_map_is_empty(map);
1072 if (empty < 0) {
1073 isl_map_free(map);
1074 return isl_stat_error;
1076 if (empty) {
1077 isl_map_free(map);
1078 return isl_stat_ok;
1081 data->res = isl_union_map_add_map(data->res, map);
1083 return isl_stat_ok;
1086 /* Remove the elements of "uset" from the range of "umap".
1088 __isl_give isl_union_map *isl_union_map_subtract_range(
1089 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1091 return gen_bin_op(umap, dom, &subtract_range_entry);
1094 static isl_stat gist_domain_entry(void **entry, void *user)
1096 struct isl_union_map_gen_bin_data *data = user;
1097 uint32_t hash;
1098 struct isl_hash_table_entry *entry2;
1099 isl_space *dim;
1100 isl_map *map = *entry;
1101 isl_bool empty;
1103 dim = isl_map_get_space(map);
1104 dim = isl_space_domain(dim);
1105 hash = isl_space_get_hash(dim);
1106 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1107 hash, &has_dim, dim, 0);
1108 isl_space_free(dim);
1109 if (!entry2)
1110 return isl_stat_ok;
1112 map = isl_map_copy(map);
1113 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1115 empty = isl_map_is_empty(map);
1116 if (empty < 0) {
1117 isl_map_free(map);
1118 return isl_stat_error;
1121 data->res = isl_union_map_add_map(data->res, map);
1123 return isl_stat_ok;
1126 /* Compute the gist of "umap" with respect to the domain "uset".
1127 * If "uset" is a parameters domain, then compute the gist
1128 * with respect to this parameter domain.
1130 __isl_give isl_union_map *isl_union_map_gist_domain(
1131 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1133 if (isl_union_set_is_params(uset))
1134 return union_map_gist_params(umap, uset);
1135 return gen_bin_op(umap, uset, &gist_domain_entry);
1138 static isl_stat gist_range_entry(void **entry, void *user)
1140 struct isl_union_map_gen_bin_data *data = user;
1141 uint32_t hash;
1142 struct isl_hash_table_entry *entry2;
1143 isl_space *space;
1144 isl_map *map = *entry;
1145 isl_bool empty;
1147 space = isl_map_get_space(map);
1148 space = isl_space_range(space);
1149 hash = isl_space_get_hash(space);
1150 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1151 hash, &has_dim, space, 0);
1152 isl_space_free(space);
1153 if (!entry2)
1154 return isl_stat_ok;
1156 map = isl_map_copy(map);
1157 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1159 empty = isl_map_is_empty(map);
1160 if (empty < 0) {
1161 isl_map_free(map);
1162 return isl_stat_error;
1165 data->res = isl_union_map_add_map(data->res, map);
1167 return isl_stat_ok;
1170 /* Compute the gist of "umap" with respect to the range "uset".
1172 __isl_give isl_union_map *isl_union_map_gist_range(
1173 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1175 return gen_bin_op(umap, uset, &gist_range_entry);
1178 static isl_stat intersect_range_entry(void **entry, void *user)
1180 struct isl_union_map_gen_bin_data *data = user;
1181 uint32_t hash;
1182 struct isl_hash_table_entry *entry2;
1183 isl_space *dim;
1184 isl_map *map = *entry;
1185 isl_bool empty;
1187 dim = isl_map_get_space(map);
1188 dim = isl_space_range(dim);
1189 hash = isl_space_get_hash(dim);
1190 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1191 hash, &has_dim, dim, 0);
1192 isl_space_free(dim);
1193 if (!entry2)
1194 return isl_stat_ok;
1196 map = isl_map_copy(map);
1197 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1199 empty = isl_map_is_empty(map);
1200 if (empty < 0) {
1201 isl_map_free(map);
1202 return isl_stat_error;
1204 if (empty) {
1205 isl_map_free(map);
1206 return isl_stat_ok;
1209 data->res = isl_union_map_add_map(data->res, map);
1211 return isl_stat_ok;
1214 __isl_give isl_union_map *isl_union_map_intersect_range(
1215 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1217 return gen_bin_op(umap, uset, &intersect_range_entry);
1220 struct isl_union_map_bin_data {
1221 isl_union_map *umap2;
1222 isl_union_map *res;
1223 isl_map *map;
1224 isl_stat (*fn)(void **entry, void *user);
1227 static isl_stat apply_range_entry(void **entry, void *user)
1229 struct isl_union_map_bin_data *data = user;
1230 isl_map *map2 = *entry;
1231 isl_bool empty;
1233 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1234 map2->dim, isl_dim_in))
1235 return isl_stat_ok;
1237 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1239 empty = isl_map_is_empty(map2);
1240 if (empty < 0) {
1241 isl_map_free(map2);
1242 return isl_stat_error;
1244 if (empty) {
1245 isl_map_free(map2);
1246 return isl_stat_ok;
1249 data->res = isl_union_map_add_map(data->res, map2);
1251 return isl_stat_ok;
1254 static isl_stat bin_entry(void **entry, void *user)
1256 struct isl_union_map_bin_data *data = user;
1257 isl_map *map = *entry;
1259 data->map = map;
1260 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1261 data->fn, data) < 0)
1262 return isl_stat_error;
1264 return isl_stat_ok;
1267 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1268 __isl_take isl_union_map *umap2,
1269 isl_stat (*fn)(void **entry, void *user))
1271 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1273 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1274 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1276 if (!umap1 || !umap2)
1277 goto error;
1279 data.umap2 = umap2;
1280 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1281 umap1->table.n);
1282 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1283 &bin_entry, &data) < 0)
1284 goto error;
1286 isl_union_map_free(umap1);
1287 isl_union_map_free(umap2);
1288 return data.res;
1289 error:
1290 isl_union_map_free(umap1);
1291 isl_union_map_free(umap2);
1292 isl_union_map_free(data.res);
1293 return NULL;
1296 __isl_give isl_union_map *isl_union_map_apply_range(
1297 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1299 return bin_op(umap1, umap2, &apply_range_entry);
1302 __isl_give isl_union_map *isl_union_map_apply_domain(
1303 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1305 umap1 = isl_union_map_reverse(umap1);
1306 umap1 = isl_union_map_apply_range(umap1, umap2);
1307 return isl_union_map_reverse(umap1);
1310 __isl_give isl_union_set *isl_union_set_apply(
1311 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1313 return isl_union_map_apply_range(uset, umap);
1316 static isl_stat map_lex_lt_entry(void **entry, void *user)
1318 struct isl_union_map_bin_data *data = user;
1319 isl_map *map2 = *entry;
1321 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1322 map2->dim, isl_dim_out))
1323 return isl_stat_ok;
1325 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1327 data->res = isl_union_map_add_map(data->res, map2);
1329 return isl_stat_ok;
1332 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1333 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1335 return bin_op(umap1, umap2, &map_lex_lt_entry);
1338 static isl_stat map_lex_le_entry(void **entry, void *user)
1340 struct isl_union_map_bin_data *data = user;
1341 isl_map *map2 = *entry;
1343 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1344 map2->dim, isl_dim_out))
1345 return isl_stat_ok;
1347 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1349 data->res = isl_union_map_add_map(data->res, map2);
1351 return isl_stat_ok;
1354 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1355 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1357 return bin_op(umap1, umap2, &map_lex_le_entry);
1360 static isl_stat product_entry(void **entry, void *user)
1362 struct isl_union_map_bin_data *data = user;
1363 isl_map *map2 = *entry;
1365 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1367 data->res = isl_union_map_add_map(data->res, map2);
1369 return isl_stat_ok;
1372 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1373 __isl_take isl_union_map *umap2)
1375 return bin_op(umap1, umap2, &product_entry);
1378 static isl_stat set_product_entry(void **entry, void *user)
1380 struct isl_union_map_bin_data *data = user;
1381 isl_set *set2 = *entry;
1383 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1385 data->res = isl_union_set_add_set(data->res, set2);
1387 return isl_stat_ok;
1390 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1391 __isl_take isl_union_set *uset2)
1393 return bin_op(uset1, uset2, &set_product_entry);
1396 static isl_stat domain_product_entry(void **entry, void *user)
1398 struct isl_union_map_bin_data *data = user;
1399 isl_map *map2 = *entry;
1401 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1402 map2->dim, isl_dim_out))
1403 return isl_stat_ok;
1405 map2 = isl_map_domain_product(isl_map_copy(data->map),
1406 isl_map_copy(map2));
1408 data->res = isl_union_map_add_map(data->res, map2);
1410 return isl_stat_ok;
1413 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1415 __isl_give isl_union_map *isl_union_map_domain_product(
1416 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1418 return bin_op(umap1, umap2, &domain_product_entry);
1421 static isl_stat range_product_entry(void **entry, void *user)
1423 struct isl_union_map_bin_data *data = user;
1424 isl_map *map2 = *entry;
1426 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1427 map2->dim, isl_dim_in))
1428 return isl_stat_ok;
1430 map2 = isl_map_range_product(isl_map_copy(data->map),
1431 isl_map_copy(map2));
1433 data->res = isl_union_map_add_map(data->res, map2);
1435 return isl_stat_ok;
1438 __isl_give isl_union_map *isl_union_map_range_product(
1439 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1441 return bin_op(umap1, umap2, &range_product_entry);
1444 /* If data->map A -> B and "map2" C -> D have the same range space,
1445 * then add (A, C) -> (B * D) to data->res.
1447 static isl_stat flat_domain_product_entry(void **entry, void *user)
1449 struct isl_union_map_bin_data *data = user;
1450 isl_map *map2 = *entry;
1452 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1453 map2->dim, isl_dim_out))
1454 return isl_stat_ok;
1456 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1457 isl_map_copy(map2));
1459 data->res = isl_union_map_add_map(data->res, map2);
1461 return isl_stat_ok;
1464 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1466 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1467 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1469 return bin_op(umap1, umap2, &flat_domain_product_entry);
1472 static isl_stat flat_range_product_entry(void **entry, void *user)
1474 struct isl_union_map_bin_data *data = user;
1475 isl_map *map2 = *entry;
1477 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1478 map2->dim, isl_dim_in))
1479 return isl_stat_ok;
1481 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1482 isl_map_copy(map2));
1484 data->res = isl_union_map_add_map(data->res, map2);
1486 return isl_stat_ok;
1489 __isl_give isl_union_map *isl_union_map_flat_range_product(
1490 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1492 return bin_op(umap1, umap2, &flat_range_product_entry);
1495 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1496 isl_stat (*fn)(void **, void *))
1498 isl_union_set *res;
1500 if (!umap)
1501 return NULL;
1503 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1504 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1505 goto error;
1507 isl_union_map_free(umap);
1508 return res;
1509 error:
1510 isl_union_map_free(umap);
1511 isl_union_set_free(res);
1512 return NULL;
1515 static isl_stat from_range_entry(void **entry, void *user)
1517 isl_map *set = *entry;
1518 isl_union_set **res = user;
1520 *res = isl_union_map_add_map(*res,
1521 isl_map_from_range(isl_set_copy(set)));
1523 return isl_stat_ok;
1526 __isl_give isl_union_map *isl_union_map_from_range(
1527 __isl_take isl_union_set *uset)
1529 return cond_un_op(uset, &from_range_entry);
1532 __isl_give isl_union_map *isl_union_map_from_domain(
1533 __isl_take isl_union_set *uset)
1535 return isl_union_map_reverse(isl_union_map_from_range(uset));
1538 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1539 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1541 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1542 isl_union_map_from_range(range));
1545 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1546 isl_stat (*fn)(void **, void *))
1548 umap = isl_union_map_cow(umap);
1549 if (!umap)
1550 return NULL;
1552 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1553 goto error;
1555 return umap;
1556 error:
1557 isl_union_map_free(umap);
1558 return NULL;
1561 static isl_stat affine_entry(void **entry, void *user)
1563 isl_map **map = (isl_map **)entry;
1565 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1567 return *map ? isl_stat_ok : isl_stat_error;
1570 __isl_give isl_union_map *isl_union_map_affine_hull(
1571 __isl_take isl_union_map *umap)
1573 return un_op(umap, &affine_entry);
1576 __isl_give isl_union_set *isl_union_set_affine_hull(
1577 __isl_take isl_union_set *uset)
1579 return isl_union_map_affine_hull(uset);
1582 static isl_stat polyhedral_entry(void **entry, void *user)
1584 isl_map **map = (isl_map **)entry;
1586 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1588 return *map ? isl_stat_ok : isl_stat_error;
1591 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1592 __isl_take isl_union_map *umap)
1594 return un_op(umap, &polyhedral_entry);
1597 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1598 __isl_take isl_union_set *uset)
1600 return isl_union_map_polyhedral_hull(uset);
1603 static isl_stat simple_entry(void **entry, void *user)
1605 isl_map **map = (isl_map **)entry;
1607 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1609 return *map ? isl_stat_ok : isl_stat_error;
1612 __isl_give isl_union_map *isl_union_map_simple_hull(
1613 __isl_take isl_union_map *umap)
1615 return un_op(umap, &simple_entry);
1618 __isl_give isl_union_set *isl_union_set_simple_hull(
1619 __isl_take isl_union_set *uset)
1621 return isl_union_map_simple_hull(uset);
1624 static isl_stat inplace_entry(void **entry, void *user)
1626 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1627 isl_map **map = (isl_map **)entry;
1628 isl_map *copy;
1630 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1631 copy = fn(isl_map_copy(*map));
1632 if (!copy)
1633 return isl_stat_error;
1635 isl_map_free(*map);
1636 *map = copy;
1638 return isl_stat_ok;
1641 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1642 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1644 if (!umap)
1645 return NULL;
1647 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1648 &inplace_entry, &fn) < 0)
1649 goto error;
1651 return umap;
1652 error:
1653 isl_union_map_free(umap);
1654 return NULL;
1657 /* Remove redundant constraints in each of the basic maps of "umap".
1658 * Since removing redundant constraints does not change the meaning
1659 * or the space, the operation can be performed in-place.
1661 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1662 __isl_take isl_union_map *umap)
1664 return inplace(umap, &isl_map_remove_redundancies);
1667 /* Remove redundant constraints in each of the basic sets of "uset".
1669 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1670 __isl_take isl_union_set *uset)
1672 return isl_union_map_remove_redundancies(uset);
1675 __isl_give isl_union_map *isl_union_map_coalesce(
1676 __isl_take isl_union_map *umap)
1678 return inplace(umap, &isl_map_coalesce);
1681 __isl_give isl_union_set *isl_union_set_coalesce(
1682 __isl_take isl_union_set *uset)
1684 return isl_union_map_coalesce(uset);
1687 __isl_give isl_union_map *isl_union_map_detect_equalities(
1688 __isl_take isl_union_map *umap)
1690 return inplace(umap, &isl_map_detect_equalities);
1693 __isl_give isl_union_set *isl_union_set_detect_equalities(
1694 __isl_take isl_union_set *uset)
1696 return isl_union_map_detect_equalities(uset);
1699 __isl_give isl_union_map *isl_union_map_compute_divs(
1700 __isl_take isl_union_map *umap)
1702 return inplace(umap, &isl_map_compute_divs);
1705 __isl_give isl_union_set *isl_union_set_compute_divs(
1706 __isl_take isl_union_set *uset)
1708 return isl_union_map_compute_divs(uset);
1711 static isl_stat lexmin_entry(void **entry, void *user)
1713 isl_map **map = (isl_map **)entry;
1715 *map = isl_map_lexmin(*map);
1717 return *map ? isl_stat_ok : isl_stat_error;
1720 __isl_give isl_union_map *isl_union_map_lexmin(
1721 __isl_take isl_union_map *umap)
1723 return un_op(umap, &lexmin_entry);
1726 __isl_give isl_union_set *isl_union_set_lexmin(
1727 __isl_take isl_union_set *uset)
1729 return isl_union_map_lexmin(uset);
1732 static isl_stat lexmax_entry(void **entry, void *user)
1734 isl_map **map = (isl_map **)entry;
1736 *map = isl_map_lexmax(*map);
1738 return *map ? isl_stat_ok : isl_stat_error;
1741 __isl_give isl_union_map *isl_union_map_lexmax(
1742 __isl_take isl_union_map *umap)
1744 return un_op(umap, &lexmax_entry);
1747 __isl_give isl_union_set *isl_union_set_lexmax(
1748 __isl_take isl_union_set *uset)
1750 return isl_union_map_lexmax(uset);
1753 static isl_stat universe_entry(void **entry, void *user)
1755 isl_map *map = *entry;
1756 isl_union_map **res = user;
1758 map = isl_map_universe(isl_map_get_space(map));
1759 *res = isl_union_map_add_map(*res, map);
1761 return isl_stat_ok;
1764 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1766 return cond_un_op(umap, &universe_entry);
1769 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1771 return isl_union_map_universe(uset);
1774 static isl_stat reverse_entry(void **entry, void *user)
1776 isl_map *map = *entry;
1777 isl_union_map **res = user;
1779 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1781 return isl_stat_ok;
1784 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1786 return cond_un_op(umap, &reverse_entry);
1789 static isl_stat params_entry(void **entry, void *user)
1791 isl_map *map = *entry;
1792 isl_union_set **res = user;
1794 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1796 return isl_stat_ok;
1799 /* Compute the parameter domain of the given union map.
1801 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1803 int empty;
1805 empty = isl_union_map_is_empty(umap);
1806 if (empty < 0)
1807 goto error;
1808 if (empty) {
1809 isl_space *space;
1810 space = isl_union_map_get_space(umap);
1811 isl_union_map_free(umap);
1812 return isl_set_empty(space);
1814 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1815 error:
1816 isl_union_map_free(umap);
1817 return NULL;
1820 /* Compute the parameter domain of the given union set.
1822 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1824 return isl_union_map_params(uset);
1827 static isl_stat domain_entry(void **entry, void *user)
1829 isl_map *map = *entry;
1830 isl_union_set **res = user;
1832 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1834 return isl_stat_ok;
1837 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1839 return cond_un_op(umap, &domain_entry);
1842 static isl_stat range_entry(void **entry, void *user)
1844 isl_map *map = *entry;
1845 isl_union_set **res = user;
1847 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1849 return isl_stat_ok;
1852 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1854 return cond_un_op(umap, &range_entry);
1857 static isl_stat domain_map_entry(void **entry, void *user)
1859 isl_map *map = *entry;
1860 isl_union_set **res = user;
1862 *res = isl_union_map_add_map(*res,
1863 isl_map_domain_map(isl_map_copy(map)));
1865 return isl_stat_ok;
1868 __isl_give isl_union_map *isl_union_map_domain_map(
1869 __isl_take isl_union_map *umap)
1871 return cond_un_op(umap, &domain_map_entry);
1874 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1875 * add the result to "res".
1877 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1879 isl_union_pw_multi_aff **res = user;
1880 isl_multi_aff *ma;
1881 isl_pw_multi_aff *pma;
1883 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1884 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1885 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1887 return *res ? isl_stat_ok : isl_stat_error;
1891 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1892 * to its domain.
1894 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1895 __isl_take isl_union_map *umap)
1897 isl_union_pw_multi_aff *res;
1899 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1900 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1901 res = isl_union_pw_multi_aff_free(res);
1903 isl_union_map_free(umap);
1904 return res;
1907 static isl_stat range_map_entry(void **entry, void *user)
1909 isl_map *map = *entry;
1910 isl_union_set **res = user;
1912 *res = isl_union_map_add_map(*res,
1913 isl_map_range_map(isl_map_copy(map)));
1915 return isl_stat_ok;
1918 __isl_give isl_union_map *isl_union_map_range_map(
1919 __isl_take isl_union_map *umap)
1921 return cond_un_op(umap, &range_map_entry);
1924 /* Check if "set" is of the form A[B -> C].
1925 * If so, add A[B -> C] -> B to "res".
1927 static isl_stat wrapped_domain_map_entry(void **entry, void *user)
1929 isl_set *set = *entry;
1930 isl_union_set **res = user;
1931 int wrapping;
1933 wrapping = isl_set_is_wrapping(set);
1934 if (wrapping < 0)
1935 return isl_stat_error;
1936 if (!wrapping)
1937 return isl_stat_ok;
1939 *res = isl_union_map_add_map(*res,
1940 isl_set_wrapped_domain_map(isl_set_copy(set)));
1942 return isl_stat_ok;
1945 /* Given a collection of wrapped maps of the form A[B -> C],
1946 * return the collection of maps A[B -> C] -> B.
1948 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1949 __isl_take isl_union_set *uset)
1951 return cond_un_op(uset, &wrapped_domain_map_entry);
1954 static isl_stat deltas_entry(void **entry, void *user)
1956 isl_map *map = *entry;
1957 isl_union_set **res = user;
1959 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1960 map->dim, isl_dim_out))
1961 return isl_stat_ok;
1963 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1965 return isl_stat_ok;
1968 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1970 return cond_un_op(umap, &deltas_entry);
1973 static isl_stat deltas_map_entry(void **entry, void *user)
1975 isl_map *map = *entry;
1976 isl_union_map **res = user;
1978 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1979 map->dim, isl_dim_out))
1980 return isl_stat_ok;
1982 *res = isl_union_map_add_map(*res,
1983 isl_map_deltas_map(isl_map_copy(map)));
1985 return isl_stat_ok;
1988 __isl_give isl_union_map *isl_union_map_deltas_map(
1989 __isl_take isl_union_map *umap)
1991 return cond_un_op(umap, &deltas_map_entry);
1994 static isl_stat identity_entry(void **entry, void *user)
1996 isl_set *set = *entry;
1997 isl_union_map **res = user;
1999 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
2001 return isl_stat_ok;
2004 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2006 return cond_un_op(uset, &identity_entry);
2009 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2011 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2013 isl_union_pw_multi_aff **res = user;
2014 isl_space *space;
2015 isl_pw_multi_aff *pma;
2017 space = isl_space_map_from_set(isl_set_get_space(set));
2018 pma = isl_pw_multi_aff_identity(space);
2019 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2020 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2022 return *res ? isl_stat_ok : isl_stat_error;
2025 /* Return an identity function on "uset" in the form
2026 * of an isl_union_pw_multi_aff.
2028 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2029 __isl_take isl_union_set *uset)
2031 isl_union_pw_multi_aff *res;
2033 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2034 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2035 res = isl_union_pw_multi_aff_free(res);
2037 isl_union_set_free(uset);
2038 return res;
2041 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
2043 static isl_stat domain_factor_domain_entry(void **entry, void *user)
2045 isl_map *map = *entry;
2046 isl_union_map **res = user;
2048 if (!isl_map_domain_is_wrapping(map))
2049 return isl_stat_ok;
2051 *res = isl_union_map_add_map(*res,
2052 isl_map_domain_factor_domain(isl_map_copy(map)));
2054 return *res ? isl_stat_ok : isl_stat_error;
2057 /* For each map in "umap" of the form [A -> B] -> C,
2058 * construct the map A -> C and collect the results.
2060 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2061 __isl_take isl_union_map *umap)
2063 return cond_un_op(umap, &domain_factor_domain_entry);
2066 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2068 static isl_stat domain_factor_range_entry(void **entry, void *user)
2070 isl_map *map = *entry;
2071 isl_union_map **res = user;
2073 if (!isl_map_domain_is_wrapping(map))
2074 return isl_stat_ok;
2076 *res = isl_union_map_add_map(*res,
2077 isl_map_domain_factor_range(isl_map_copy(map)));
2079 return *res ? isl_stat_ok : isl_stat_error;
2082 /* For each map in "umap" of the form [A -> B] -> C,
2083 * construct the map B -> C and collect the results.
2085 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2086 __isl_take isl_union_map *umap)
2088 return cond_un_op(umap, &domain_factor_range_entry);
2091 /* If "map" is of the form A -> [B -> C], then add A -> B to "res".
2093 static isl_stat range_factor_domain_entry(void **entry, void *user)
2095 isl_map *map = *entry;
2096 isl_union_map **res = user;
2098 if (!isl_map_range_is_wrapping(map))
2099 return isl_stat_ok;
2101 *res = isl_union_map_add_map(*res,
2102 isl_map_range_factor_domain(isl_map_copy(map)));
2104 return *res ? isl_stat_ok : isl_stat_error;
2107 /* For each map in "umap" of the form A -> [B -> C],
2108 * construct the map A -> B and collect the results.
2110 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2111 __isl_take isl_union_map *umap)
2113 return cond_un_op(umap, &range_factor_domain_entry);
2116 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2118 static isl_stat range_factor_range_entry(void **entry, void *user)
2120 isl_map *map = *entry;
2121 isl_union_map **res = user;
2123 if (!isl_map_range_is_wrapping(map))
2124 return isl_stat_ok;
2126 *res = isl_union_map_add_map(*res,
2127 isl_map_range_factor_range(isl_map_copy(map)));
2129 return *res ? isl_stat_ok : isl_stat_error;
2132 /* For each map in "umap" of the form A -> [B -> C],
2133 * construct the map A -> C and collect the results.
2135 __isl_give isl_union_map *isl_union_map_range_factor_range(
2136 __isl_take isl_union_map *umap)
2138 return cond_un_op(umap, &range_factor_range_entry);
2141 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2143 static isl_stat factor_domain_entry(void **entry, void *user)
2145 isl_map *map = *entry;
2146 isl_union_map **res = user;
2148 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2149 return isl_stat_ok;
2151 *res = isl_union_map_add_map(*res,
2152 isl_map_factor_domain(isl_map_copy(map)));
2154 return *res ? isl_stat_ok : isl_stat_error;
2157 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2158 * construct the map A -> C and collect the results.
2160 __isl_give isl_union_map *isl_union_map_factor_domain(
2161 __isl_take isl_union_map *umap)
2163 return cond_un_op(umap, &factor_domain_entry);
2166 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2168 static isl_stat factor_range_entry(void **entry, void *user)
2170 isl_map *map = *entry;
2171 isl_union_map **res = user;
2173 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2174 return isl_stat_ok;
2176 *res = isl_union_map_add_map(*res,
2177 isl_map_factor_range(isl_map_copy(map)));
2179 return *res ? isl_stat_ok : isl_stat_error;
2182 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2183 * construct the map B -> D and collect the results.
2185 __isl_give isl_union_map *isl_union_map_factor_range(
2186 __isl_take isl_union_map *umap)
2188 return cond_un_op(umap, &factor_range_entry);
2191 static isl_stat unwrap_entry(void **entry, void *user)
2193 isl_set *set = *entry;
2194 isl_union_set **res = user;
2196 if (!isl_set_is_wrapping(set))
2197 return isl_stat_ok;
2199 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
2201 return isl_stat_ok;
2204 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2206 return cond_un_op(uset, &unwrap_entry);
2209 static isl_stat wrap_entry(void **entry, void *user)
2211 isl_map *map = *entry;
2212 isl_union_set **res = user;
2214 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2216 return isl_stat_ok;
2219 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2221 return cond_un_op(umap, &wrap_entry);
2224 struct isl_union_map_is_subset_data {
2225 isl_union_map *umap2;
2226 isl_bool is_subset;
2229 static isl_stat is_subset_entry(void **entry, void *user)
2231 struct isl_union_map_is_subset_data *data = user;
2232 uint32_t hash;
2233 struct isl_hash_table_entry *entry2;
2234 isl_map *map = *entry;
2236 hash = isl_space_get_hash(map->dim);
2237 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2238 hash, &has_dim, map->dim, 0);
2239 if (!entry2) {
2240 int empty = isl_map_is_empty(map);
2241 if (empty < 0)
2242 return isl_stat_error;
2243 if (empty)
2244 return isl_stat_ok;
2245 data->is_subset = 0;
2246 return isl_stat_error;
2249 data->is_subset = isl_map_is_subset(map, entry2->data);
2250 if (data->is_subset < 0 || !data->is_subset)
2251 return isl_stat_error;
2253 return isl_stat_ok;
2256 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2257 __isl_keep isl_union_map *umap2)
2259 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2261 umap1 = isl_union_map_copy(umap1);
2262 umap2 = isl_union_map_copy(umap2);
2263 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2264 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2266 if (!umap1 || !umap2)
2267 goto error;
2269 data.umap2 = umap2;
2270 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2271 &is_subset_entry, &data) < 0 &&
2272 data.is_subset)
2273 goto error;
2275 isl_union_map_free(umap1);
2276 isl_union_map_free(umap2);
2278 return data.is_subset;
2279 error:
2280 isl_union_map_free(umap1);
2281 isl_union_map_free(umap2);
2282 return isl_bool_error;
2285 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2286 __isl_keep isl_union_set *uset2)
2288 return isl_union_map_is_subset(uset1, uset2);
2291 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2292 __isl_keep isl_union_map *umap2)
2294 isl_bool is_subset;
2296 if (!umap1 || !umap2)
2297 return isl_bool_error;
2298 is_subset = isl_union_map_is_subset(umap1, umap2);
2299 if (is_subset != isl_bool_true)
2300 return is_subset;
2301 is_subset = isl_union_map_is_subset(umap2, umap1);
2302 return is_subset;
2305 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2306 __isl_keep isl_union_set *uset2)
2308 return isl_union_map_is_equal(uset1, uset2);
2311 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2312 __isl_keep isl_union_map *umap2)
2314 isl_bool is_subset;
2316 if (!umap1 || !umap2)
2317 return isl_bool_error;
2318 is_subset = isl_union_map_is_subset(umap1, umap2);
2319 if (is_subset != isl_bool_true)
2320 return is_subset;
2321 is_subset = isl_union_map_is_subset(umap2, umap1);
2322 if (is_subset == isl_bool_error)
2323 return is_subset;
2324 return !is_subset;
2327 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2328 __isl_keep isl_union_set *uset2)
2330 return isl_union_map_is_strict_subset(uset1, uset2);
2333 /* Internal data structure for isl_union_map_is_disjoint.
2334 * umap2 is the union map with which we are comparing.
2335 * is_disjoint is initialized to 1 and is set to 0 as soon
2336 * as the union maps turn out not to be disjoint.
2338 struct isl_union_map_is_disjoint_data {
2339 isl_union_map *umap2;
2340 isl_bool is_disjoint;
2343 /* Check if "map" is disjoint from data->umap2 and abort
2344 * the search if it is not.
2346 static isl_stat is_disjoint_entry(void **entry, void *user)
2348 struct isl_union_map_is_disjoint_data *data = user;
2349 uint32_t hash;
2350 struct isl_hash_table_entry *entry2;
2351 isl_map *map = *entry;
2353 hash = isl_space_get_hash(map->dim);
2354 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2355 hash, &has_dim, map->dim, 0);
2356 if (!entry2)
2357 return isl_stat_ok;
2359 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2360 if (data->is_disjoint < 0 || !data->is_disjoint)
2361 return isl_stat_error;
2363 return isl_stat_ok;
2366 /* Are "umap1" and "umap2" disjoint?
2368 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2369 __isl_keep isl_union_map *umap2)
2371 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2373 umap1 = isl_union_map_copy(umap1);
2374 umap2 = isl_union_map_copy(umap2);
2375 umap1 = isl_union_map_align_params(umap1,
2376 isl_union_map_get_space(umap2));
2377 umap2 = isl_union_map_align_params(umap2,
2378 isl_union_map_get_space(umap1));
2380 if (!umap1 || !umap2)
2381 goto error;
2383 data.umap2 = umap2;
2384 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2385 &is_disjoint_entry, &data) < 0 &&
2386 data.is_disjoint)
2387 goto error;
2389 isl_union_map_free(umap1);
2390 isl_union_map_free(umap2);
2392 return data.is_disjoint;
2393 error:
2394 isl_union_map_free(umap1);
2395 isl_union_map_free(umap2);
2396 return isl_bool_error;
2399 /* Are "uset1" and "uset2" disjoint?
2401 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2402 __isl_keep isl_union_set *uset2)
2404 return isl_union_map_is_disjoint(uset1, uset2);
2407 static isl_stat sample_entry(void **entry, void *user)
2409 isl_basic_map **sample = (isl_basic_map **)user;
2410 isl_map *map = *entry;
2412 *sample = isl_map_sample(isl_map_copy(map));
2413 if (!*sample)
2414 return isl_stat_error;
2415 if (!isl_basic_map_plain_is_empty(*sample))
2416 return isl_stat_error;
2417 return isl_stat_ok;
2420 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2422 isl_basic_map *sample = NULL;
2424 if (!umap)
2425 return NULL;
2427 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2428 &sample_entry, &sample) < 0 &&
2429 !sample)
2430 goto error;
2432 if (!sample)
2433 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2435 isl_union_map_free(umap);
2437 return sample;
2438 error:
2439 isl_union_map_free(umap);
2440 return NULL;
2443 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2445 return (isl_basic_set *)isl_union_map_sample(uset);
2448 /* Return an element in "uset" in the form of an isl_point.
2449 * Return a void isl_point if "uset" is empty.
2451 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2453 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2456 struct isl_forall_data {
2457 isl_bool res;
2458 isl_bool (*fn)(__isl_keep isl_map *map);
2461 static isl_stat forall_entry(void **entry, void *user)
2463 struct isl_forall_data *data = user;
2464 isl_map *map = *entry;
2466 data->res = data->fn(map);
2467 if (data->res < 0)
2468 return isl_stat_error;
2470 if (!data->res)
2471 return isl_stat_error;
2473 return isl_stat_ok;
2476 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2477 isl_bool (*fn)(__isl_keep isl_map *map))
2479 struct isl_forall_data data = { isl_bool_true, fn };
2481 if (!umap)
2482 return isl_bool_error;
2484 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2485 &forall_entry, &data) < 0 && data.res)
2486 return isl_bool_error;
2488 return data.res;
2491 struct isl_forall_user_data {
2492 isl_bool res;
2493 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2494 void *user;
2497 static isl_stat forall_user_entry(void **entry, void *user)
2499 struct isl_forall_user_data *data = user;
2500 isl_map *map = *entry;
2502 data->res = data->fn(map, data->user);
2503 if (data->res < 0)
2504 return isl_stat_error;
2506 if (!data->res)
2507 return isl_stat_error;
2509 return isl_stat_ok;
2512 /* Check if fn(map, user) returns true for all maps "map" in umap.
2514 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2515 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2517 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2519 if (!umap)
2520 return isl_bool_error;
2522 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2523 &forall_user_entry, &data) < 0 && data.res)
2524 return isl_bool_error;
2526 return data.res;
2529 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2531 return union_map_forall(umap, &isl_map_is_empty);
2534 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2536 return isl_union_map_is_empty(uset);
2539 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2541 isl_bool is_subset;
2542 isl_space *dim;
2543 isl_map *id;
2545 if (!map)
2546 return isl_bool_error;
2548 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2549 map->dim, isl_dim_out))
2550 return isl_bool_false;
2552 dim = isl_map_get_space(map);
2553 id = isl_map_identity(dim);
2555 is_subset = isl_map_is_subset(map, id);
2557 isl_map_free(id);
2559 return is_subset;
2562 /* Given an isl_union_map that consists of a single map, check
2563 * if it is single-valued.
2565 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2567 isl_map *map;
2568 isl_bool sv;
2570 umap = isl_union_map_copy(umap);
2571 map = isl_map_from_union_map(umap);
2572 sv = isl_map_is_single_valued(map);
2573 isl_map_free(map);
2575 return sv;
2578 /* Internal data structure for single_valued_on_domain.
2580 * "umap" is the union map to be tested.
2581 * "sv" is set to 1 as long as "umap" may still be single-valued.
2583 struct isl_union_map_is_sv_data {
2584 isl_union_map *umap;
2585 isl_bool sv;
2588 /* Check if the data->umap is single-valued on "set".
2590 * If data->umap consists of a single map on "set", then test it
2591 * as an isl_map.
2593 * Otherwise, compute
2595 * M \circ M^-1
2597 * check if the result is a subset of the identity mapping and
2598 * store the result in data->sv.
2600 * Terminate as soon as data->umap has been determined not to
2601 * be single-valued.
2603 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2605 struct isl_union_map_is_sv_data *data = user;
2606 isl_union_map *umap, *test;
2608 umap = isl_union_map_copy(data->umap);
2609 umap = isl_union_map_intersect_domain(umap,
2610 isl_union_set_from_set(set));
2612 if (isl_union_map_n_map(umap) == 1) {
2613 data->sv = single_map_is_single_valued(umap);
2614 isl_union_map_free(umap);
2615 } else {
2616 test = isl_union_map_reverse(isl_union_map_copy(umap));
2617 test = isl_union_map_apply_range(test, umap);
2619 data->sv = union_map_forall(test, &is_subset_of_identity);
2621 isl_union_map_free(test);
2624 if (data->sv < 0 || !data->sv)
2625 return isl_stat_error;
2626 return isl_stat_ok;
2629 /* Check if the given map is single-valued.
2631 * If the union map consists of a single map, then test it as an isl_map.
2632 * Otherwise, check if the union map is single-valued on each of its
2633 * domain spaces.
2635 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2637 isl_union_map *universe;
2638 isl_union_set *domain;
2639 struct isl_union_map_is_sv_data data;
2641 if (isl_union_map_n_map(umap) == 1)
2642 return single_map_is_single_valued(umap);
2644 universe = isl_union_map_universe(isl_union_map_copy(umap));
2645 domain = isl_union_map_domain(universe);
2647 data.sv = isl_bool_true;
2648 data.umap = umap;
2649 if (isl_union_set_foreach_set(domain,
2650 &single_valued_on_domain, &data) < 0 && data.sv)
2651 data.sv = isl_bool_error;
2652 isl_union_set_free(domain);
2654 return data.sv;
2657 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2659 isl_bool in;
2661 umap = isl_union_map_copy(umap);
2662 umap = isl_union_map_reverse(umap);
2663 in = isl_union_map_is_single_valued(umap);
2664 isl_union_map_free(umap);
2666 return in;
2669 /* Is "map" obviously not an identity relation because
2670 * it maps elements from one space to another space?
2671 * Update *non_identity accordingly.
2673 * In particular, if the domain and range spaces are the same,
2674 * then the map is not considered to obviously not be an identity relation.
2675 * Otherwise, the map is considered to obviously not be an identity relation
2676 * if it is is non-empty.
2678 * If "map" is determined to obviously not be an identity relation,
2679 * then the search is aborted.
2681 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2683 isl_bool *non_identity = user;
2684 isl_bool equal;
2685 isl_space *space;
2687 space = isl_map_get_space(map);
2688 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2689 if (equal >= 0 && !equal)
2690 *non_identity = isl_bool_not(isl_map_is_empty(map));
2691 else
2692 *non_identity = isl_bool_not(equal);
2693 isl_space_free(space);
2694 isl_map_free(map);
2696 if (*non_identity < 0 || *non_identity)
2697 return isl_stat_error;
2699 return isl_stat_ok;
2702 /* Is "umap" obviously not an identity relation because
2703 * it maps elements from one space to another space?
2705 * As soon as a map has been found that maps elements to a different space,
2706 * non_identity is changed and the search is aborted.
2708 static isl_bool isl_union_map_plain_is_not_identity(
2709 __isl_keep isl_union_map *umap)
2711 isl_bool non_identity;
2713 non_identity = isl_bool_false;
2714 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2715 &non_identity) < 0 &&
2716 non_identity == isl_bool_false)
2717 return isl_bool_error;
2719 return non_identity;
2722 /* Does "map" only map elements to themselves?
2723 * Update *identity accordingly.
2725 * If "map" is determined not to be an identity relation,
2726 * then the search is aborted.
2728 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2730 isl_bool *identity = user;
2732 *identity = isl_map_is_identity(map);
2733 isl_map_free(map);
2735 if (*identity < 0 || !*identity)
2736 return isl_stat_error;
2738 return isl_stat_ok;
2741 /* Does "umap" only map elements to themselves?
2743 * First check if there are any maps that map elements to different spaces.
2744 * If not, then check that all the maps (between identical spaces)
2745 * are identity relations.
2747 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2749 isl_bool non_identity;
2750 isl_bool identity;
2752 non_identity = isl_union_map_plain_is_not_identity(umap);
2753 if (non_identity < 0 || non_identity)
2754 return isl_bool_not(non_identity);
2756 identity = isl_bool_true;
2757 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2758 identity == isl_bool_true)
2759 return isl_bool_error;
2761 return identity;
2764 /* Represents a map that has a fixed value (v) for one of its
2765 * range dimensions.
2766 * The map in this structure is not reference counted, so it
2767 * is only valid while the isl_union_map from which it was
2768 * obtained is still alive.
2770 struct isl_fixed_map {
2771 isl_int v;
2772 isl_map *map;
2775 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2776 int n)
2778 int i;
2779 struct isl_fixed_map *v;
2781 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2782 if (!v)
2783 return NULL;
2784 for (i = 0; i < n; ++i)
2785 isl_int_init(v[i].v);
2786 return v;
2789 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2791 int i;
2793 if (!v)
2794 return;
2795 for (i = 0; i < n; ++i)
2796 isl_int_clear(v[i].v);
2797 free(v);
2800 /* Compare the "v" field of two isl_fixed_map structs.
2802 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2804 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2805 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2807 return isl_int_cmp(e1->v, e2->v);
2810 /* Internal data structure used while checking whether all maps
2811 * in a union_map have a fixed value for a given output dimension.
2812 * v is the list of maps, with the fixed value for the dimension
2813 * n is the number of maps considered so far
2814 * pos is the output dimension under investigation
2816 struct isl_fixed_dim_data {
2817 struct isl_fixed_map *v;
2818 int n;
2819 int pos;
2822 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2824 struct isl_fixed_dim_data *data = user;
2826 data->v[data->n].map = map;
2827 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2828 &data->v[data->n++].v);
2831 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2832 int first, int n_range);
2834 /* Given a list of the maps, with their fixed values at output dimension "pos",
2835 * check whether the ranges of the maps form an obvious partition.
2837 * We first sort the maps according to their fixed values.
2838 * If all maps have a different value, then we know the ranges form
2839 * a partition.
2840 * Otherwise, we collect the maps with the same fixed value and
2841 * check whether each such collection is obviously injective
2842 * based on later dimensions.
2844 static int separates(struct isl_fixed_map *v, int n,
2845 __isl_take isl_space *dim, int pos, int n_range)
2847 int i;
2849 if (!v)
2850 goto error;
2852 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2854 for (i = 0; i + 1 < n; ++i) {
2855 int j, k;
2856 isl_union_map *part;
2857 int injective;
2859 for (j = i + 1; j < n; ++j)
2860 if (isl_int_ne(v[i].v, v[j].v))
2861 break;
2863 if (j == i + 1)
2864 continue;
2866 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2867 for (k = i; k < j; ++k)
2868 part = isl_union_map_add_map(part,
2869 isl_map_copy(v[k].map));
2871 injective = plain_injective_on_range(part, pos + 1, n_range);
2872 if (injective < 0)
2873 goto error;
2874 if (!injective)
2875 break;
2877 i = j - 1;
2880 isl_space_free(dim);
2881 free_isl_fixed_map_array(v, n);
2882 return i + 1 >= n;
2883 error:
2884 isl_space_free(dim);
2885 free_isl_fixed_map_array(v, n);
2886 return -1;
2889 /* Check whether the maps in umap have obviously distinct ranges.
2890 * In particular, check for an output dimension in the range
2891 * [first,n_range) for which all maps have a fixed value
2892 * and then check if these values, possibly along with fixed values
2893 * at later dimensions, entail distinct ranges.
2895 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2896 int first, int n_range)
2898 isl_ctx *ctx;
2899 int n;
2900 struct isl_fixed_dim_data data = { NULL };
2902 ctx = isl_union_map_get_ctx(umap);
2904 n = isl_union_map_n_map(umap);
2905 if (!umap)
2906 goto error;
2908 if (n <= 1) {
2909 isl_union_map_free(umap);
2910 return isl_bool_true;
2913 if (first >= n_range) {
2914 isl_union_map_free(umap);
2915 return isl_bool_false;
2918 data.v = alloc_isl_fixed_map_array(ctx, n);
2919 if (!data.v)
2920 goto error;
2922 for (data.pos = first; data.pos < n_range; ++data.pos) {
2923 isl_bool fixed;
2924 int injective;
2925 isl_space *dim;
2927 data.n = 0;
2928 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2929 if (fixed < 0)
2930 goto error;
2931 if (!fixed)
2932 continue;
2933 dim = isl_union_map_get_space(umap);
2934 injective = separates(data.v, n, dim, data.pos, n_range);
2935 isl_union_map_free(umap);
2936 return injective;
2939 free_isl_fixed_map_array(data.v, n);
2940 isl_union_map_free(umap);
2942 return isl_bool_false;
2943 error:
2944 free_isl_fixed_map_array(data.v, n);
2945 isl_union_map_free(umap);
2946 return isl_bool_error;
2949 /* Check whether the maps in umap that map to subsets of "ran"
2950 * have obviously distinct ranges.
2952 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2953 void *user)
2955 isl_union_map *umap = user;
2957 umap = isl_union_map_copy(umap);
2958 umap = isl_union_map_intersect_range(umap,
2959 isl_union_set_from_set(isl_set_copy(ran)));
2960 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2963 /* Check if the given union_map is obviously injective.
2965 * In particular, we first check if all individual maps are obviously
2966 * injective and then check if all the ranges of these maps are
2967 * obviously disjoint.
2969 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2971 isl_bool in;
2972 isl_union_map *univ;
2973 isl_union_set *ran;
2975 in = union_map_forall(umap, &isl_map_plain_is_injective);
2976 if (in < 0)
2977 return isl_bool_error;
2978 if (!in)
2979 return isl_bool_false;
2981 univ = isl_union_map_universe(isl_union_map_copy(umap));
2982 ran = isl_union_map_range(univ);
2984 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2986 isl_union_set_free(ran);
2988 return in;
2991 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2993 isl_bool sv;
2995 sv = isl_union_map_is_single_valued(umap);
2996 if (sv < 0 || !sv)
2997 return sv;
2999 return isl_union_map_is_injective(umap);
3002 static isl_stat zip_entry(void **entry, void *user)
3004 isl_map *map = *entry;
3005 isl_union_map **res = user;
3007 if (!isl_map_can_zip(map))
3008 return isl_stat_ok;
3010 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
3012 return isl_stat_ok;
3015 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3017 return cond_un_op(umap, &zip_entry);
3020 static isl_stat uncurry_entry(void **entry, void *user)
3022 isl_map *map = *entry;
3023 isl_union_map **res = user;
3025 if (!isl_map_can_uncurry(map))
3026 return isl_stat_ok;
3028 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
3030 return isl_stat_ok;
3033 /* Given a union map, take the maps of the form A -> (B -> C) and
3034 * return the union of the corresponding maps (A -> B) -> C.
3036 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3038 return cond_un_op(umap, &uncurry_entry);
3041 static isl_stat curry_entry(void **entry, void *user)
3043 isl_map *map = *entry;
3044 isl_union_map **res = user;
3046 if (!isl_map_can_curry(map))
3047 return isl_stat_ok;
3049 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
3051 return isl_stat_ok;
3054 /* Given a union map, take the maps of the form (A -> B) -> C and
3055 * return the union of the corresponding maps A -> (B -> C).
3057 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3059 return cond_un_op(umap, &curry_entry);
3062 /* If *entry is of the form A -> ((B -> C) -> D), then apply
3063 * isl_map_range_curry to it and add the result to *res.
3065 static isl_stat range_curry_entry(void **entry, void *user)
3067 isl_map *map = *entry;
3068 isl_union_map **res = user;
3070 if (!isl_map_can_range_curry(map))
3071 return isl_stat_ok;
3073 map = isl_map_range_curry(isl_map_copy(map));
3074 *res = isl_union_map_add_map(*res, map);
3076 return isl_stat_ok;
3079 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3080 * return the union of the corresponding maps A -> (B -> (C -> D)).
3082 __isl_give isl_union_map *isl_union_map_range_curry(
3083 __isl_take isl_union_map *umap)
3085 return cond_un_op(umap, &range_curry_entry);
3088 static isl_stat lift_entry(void **entry, void *user)
3090 isl_set *set = *entry;
3091 isl_union_set **res = user;
3093 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
3095 return isl_stat_ok;
3098 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3100 return cond_un_op(uset, &lift_entry);
3103 static isl_stat coefficients_entry(void **entry, void *user)
3105 isl_set *set = *entry;
3106 isl_union_set **res = user;
3108 set = isl_set_copy(set);
3109 set = isl_set_from_basic_set(isl_set_coefficients(set));
3110 *res = isl_union_set_add_set(*res, set);
3112 return isl_stat_ok;
3115 __isl_give isl_union_set *isl_union_set_coefficients(
3116 __isl_take isl_union_set *uset)
3118 isl_ctx *ctx;
3119 isl_space *dim;
3120 isl_union_set *res;
3122 if (!uset)
3123 return NULL;
3125 ctx = isl_union_set_get_ctx(uset);
3126 dim = isl_space_set_alloc(ctx, 0, 0);
3127 res = isl_union_map_alloc(dim, uset->table.n);
3128 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3129 &coefficients_entry, &res) < 0)
3130 goto error;
3132 isl_union_set_free(uset);
3133 return res;
3134 error:
3135 isl_union_set_free(uset);
3136 isl_union_set_free(res);
3137 return NULL;
3140 static isl_stat solutions_entry(void **entry, void *user)
3142 isl_set *set = *entry;
3143 isl_union_set **res = user;
3145 set = isl_set_copy(set);
3146 set = isl_set_from_basic_set(isl_set_solutions(set));
3147 if (!*res)
3148 *res = isl_union_set_from_set(set);
3149 else
3150 *res = isl_union_set_add_set(*res, set);
3152 if (!*res)
3153 return isl_stat_error;
3155 return isl_stat_ok;
3158 __isl_give isl_union_set *isl_union_set_solutions(
3159 __isl_take isl_union_set *uset)
3161 isl_union_set *res = NULL;
3163 if (!uset)
3164 return NULL;
3166 if (uset->table.n == 0) {
3167 res = isl_union_set_empty(isl_union_set_get_space(uset));
3168 isl_union_set_free(uset);
3169 return res;
3172 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3173 &solutions_entry, &res) < 0)
3174 goto error;
3176 isl_union_set_free(uset);
3177 return res;
3178 error:
3179 isl_union_set_free(uset);
3180 isl_union_set_free(res);
3181 return NULL;
3184 /* Is the domain space of "map" equal to "space"?
3186 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3188 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3189 space, isl_dim_out);
3192 /* Is the range space of "map" equal to "space"?
3194 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3196 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3197 space, isl_dim_out);
3200 /* Is the set space of "map" equal to "space"?
3202 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3204 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3205 space, isl_dim_out);
3208 /* Internal data structure for preimage_pw_multi_aff.
3210 * "pma" is the function under which the preimage should be taken.
3211 * "space" is the space of "pma".
3212 * "res" collects the results.
3213 * "fn" computes the preimage for a given map.
3214 * "match" returns true if "fn" can be called.
3216 struct isl_union_map_preimage_data {
3217 isl_space *space;
3218 isl_pw_multi_aff *pma;
3219 isl_union_map *res;
3220 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3221 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3222 __isl_take isl_pw_multi_aff *pma);
3225 /* Call data->fn to compute the preimage of the domain or range of *entry
3226 * under the function represented by data->pma, provided the domain/range
3227 * space of *entry matches the target space of data->pma
3228 * (as given by data->match), and add the result to data->res.
3230 static isl_stat preimage_entry(void **entry, void *user)
3232 int m;
3233 isl_map *map = *entry;
3234 struct isl_union_map_preimage_data *data = user;
3235 isl_bool empty;
3237 m = data->match(map, data->space);
3238 if (m < 0)
3239 return isl_stat_error;
3240 if (!m)
3241 return isl_stat_ok;
3243 map = isl_map_copy(map);
3244 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3246 empty = isl_map_is_empty(map);
3247 if (empty < 0 || empty) {
3248 isl_map_free(map);
3249 return empty < 0 ? isl_stat_error : isl_stat_ok;
3252 data->res = isl_union_map_add_map(data->res, map);
3254 return isl_stat_ok;
3257 /* Compute the preimage of the domain or range of "umap" under the function
3258 * represented by "pma".
3259 * In other words, plug in "pma" in the domain or range of "umap".
3260 * The function "fn" performs the actual preimage computation on a map,
3261 * while "match" determines to which maps the function should be applied.
3263 static __isl_give isl_union_map *preimage_pw_multi_aff(
3264 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3265 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3266 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3267 __isl_take isl_pw_multi_aff *pma))
3269 isl_ctx *ctx;
3270 isl_space *space;
3271 struct isl_union_map_preimage_data data;
3273 umap = isl_union_map_align_params(umap,
3274 isl_pw_multi_aff_get_space(pma));
3275 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3277 if (!umap || !pma)
3278 goto error;
3280 ctx = isl_union_map_get_ctx(umap);
3281 space = isl_union_map_get_space(umap);
3282 data.space = isl_pw_multi_aff_get_space(pma);
3283 data.pma = pma;
3284 data.res = isl_union_map_alloc(space, umap->table.n);
3285 data.match = match;
3286 data.fn = fn;
3287 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3288 &data) < 0)
3289 data.res = isl_union_map_free(data.res);
3291 isl_space_free(data.space);
3292 isl_union_map_free(umap);
3293 isl_pw_multi_aff_free(pma);
3294 return data.res;
3295 error:
3296 isl_union_map_free(umap);
3297 isl_pw_multi_aff_free(pma);
3298 return NULL;
3301 /* Compute the preimage of the domain of "umap" under the function
3302 * represented by "pma".
3303 * In other words, plug in "pma" in the domain of "umap".
3304 * The result contains maps that live in the same spaces as the maps of "umap"
3305 * with domain space equal to the target space of "pma",
3306 * except that the domain has been replaced by the domain space of "pma".
3308 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3309 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3311 return preimage_pw_multi_aff(umap, pma, &domain_match,
3312 &isl_map_preimage_domain_pw_multi_aff);
3315 /* Compute the preimage of the range of "umap" under the function
3316 * represented by "pma".
3317 * In other words, plug in "pma" in the range of "umap".
3318 * The result contains maps that live in the same spaces as the maps of "umap"
3319 * with range space equal to the target space of "pma",
3320 * except that the range has been replaced by the domain space of "pma".
3322 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3323 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3325 return preimage_pw_multi_aff(umap, pma, &range_match,
3326 &isl_map_preimage_range_pw_multi_aff);
3329 /* Compute the preimage of "uset" under the function represented by "pma".
3330 * In other words, plug in "pma" in "uset".
3331 * The result contains sets that live in the same spaces as the sets of "uset"
3332 * with space equal to the target space of "pma",
3333 * except that the space has been replaced by the domain space of "pma".
3335 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3336 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3338 return preimage_pw_multi_aff(uset, pma, &set_match,
3339 &isl_set_preimage_pw_multi_aff);
3342 /* Compute the preimage of the domain of "umap" under the function
3343 * represented by "ma".
3344 * In other words, plug in "ma" in the domain of "umap".
3345 * The result contains maps that live in the same spaces as the maps of "umap"
3346 * with domain space equal to the target space of "ma",
3347 * except that the domain has been replaced by the domain space of "ma".
3349 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3350 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3352 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3353 isl_pw_multi_aff_from_multi_aff(ma));
3356 /* Compute the preimage of the range of "umap" under the function
3357 * represented by "ma".
3358 * In other words, plug in "ma" in the range of "umap".
3359 * The result contains maps that live in the same spaces as the maps of "umap"
3360 * with range space equal to the target space of "ma",
3361 * except that the range has been replaced by the domain space of "ma".
3363 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3364 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3366 return isl_union_map_preimage_range_pw_multi_aff(umap,
3367 isl_pw_multi_aff_from_multi_aff(ma));
3370 /* Compute the preimage of "uset" under the function represented by "ma".
3371 * In other words, plug in "ma" in "uset".
3372 * The result contains sets that live in the same spaces as the sets of "uset"
3373 * with space equal to the target space of "ma",
3374 * except that the space has been replaced by the domain space of "ma".
3376 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3377 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3379 return isl_union_set_preimage_pw_multi_aff(uset,
3380 isl_pw_multi_aff_from_multi_aff(ma));
3383 /* Internal data structure for preimage_multi_pw_aff.
3385 * "mpa" is the function under which the preimage should be taken.
3386 * "space" is the space of "mpa".
3387 * "res" collects the results.
3388 * "fn" computes the preimage for a given map.
3389 * "match" returns true if "fn" can be called.
3391 struct isl_union_map_preimage_mpa_data {
3392 isl_space *space;
3393 isl_multi_pw_aff *mpa;
3394 isl_union_map *res;
3395 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3396 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3397 __isl_take isl_multi_pw_aff *mpa);
3400 /* Call data->fn to compute the preimage of the domain or range of *entry
3401 * under the function represented by data->mpa, provided the domain/range
3402 * space of *entry matches the target space of data->mpa
3403 * (as given by data->match), and add the result to data->res.
3405 static isl_stat preimage_mpa_entry(void **entry, void *user)
3407 int m;
3408 isl_map *map = *entry;
3409 struct isl_union_map_preimage_mpa_data *data = user;
3410 isl_bool empty;
3412 m = data->match(map, data->space);
3413 if (m < 0)
3414 return isl_stat_error;
3415 if (!m)
3416 return isl_stat_ok;
3418 map = isl_map_copy(map);
3419 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3421 empty = isl_map_is_empty(map);
3422 if (empty < 0 || empty) {
3423 isl_map_free(map);
3424 return empty < 0 ? isl_stat_error : isl_stat_ok;
3427 data->res = isl_union_map_add_map(data->res, map);
3429 return isl_stat_ok;
3432 /* Compute the preimage of the domain or range of "umap" under the function
3433 * represented by "mpa".
3434 * In other words, plug in "mpa" in the domain or range of "umap".
3435 * The function "fn" performs the actual preimage computation on a map,
3436 * while "match" determines to which maps the function should be applied.
3438 static __isl_give isl_union_map *preimage_multi_pw_aff(
3439 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3440 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3441 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3442 __isl_take isl_multi_pw_aff *mpa))
3444 isl_ctx *ctx;
3445 isl_space *space;
3446 struct isl_union_map_preimage_mpa_data data;
3448 umap = isl_union_map_align_params(umap,
3449 isl_multi_pw_aff_get_space(mpa));
3450 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3452 if (!umap || !mpa)
3453 goto error;
3455 ctx = isl_union_map_get_ctx(umap);
3456 space = isl_union_map_get_space(umap);
3457 data.space = isl_multi_pw_aff_get_space(mpa);
3458 data.mpa = mpa;
3459 data.res = isl_union_map_alloc(space, umap->table.n);
3460 data.match = match;
3461 data.fn = fn;
3462 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3463 &data) < 0)
3464 data.res = isl_union_map_free(data.res);
3466 isl_space_free(data.space);
3467 isl_union_map_free(umap);
3468 isl_multi_pw_aff_free(mpa);
3469 return data.res;
3470 error:
3471 isl_union_map_free(umap);
3472 isl_multi_pw_aff_free(mpa);
3473 return NULL;
3476 /* Compute the preimage of the domain of "umap" under the function
3477 * represented by "mpa".
3478 * In other words, plug in "mpa" in the domain of "umap".
3479 * The result contains maps that live in the same spaces as the maps of "umap"
3480 * with domain space equal to the target space of "mpa",
3481 * except that the domain has been replaced by the domain space of "mpa".
3483 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3484 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3486 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3487 &isl_map_preimage_domain_multi_pw_aff);
3490 /* Internal data structure for preimage_upma.
3492 * "umap" is the map of which the preimage should be computed.
3493 * "res" collects the results.
3494 * "fn" computes the preimage for a given piecewise multi-affine function.
3496 struct isl_union_map_preimage_upma_data {
3497 isl_union_map *umap;
3498 isl_union_map *res;
3499 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3500 __isl_take isl_pw_multi_aff *pma);
3503 /* Call data->fn to compute the preimage of the domain or range of data->umap
3504 * under the function represented by pma and add the result to data->res.
3506 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3508 struct isl_union_map_preimage_upma_data *data = user;
3509 isl_union_map *umap;
3511 umap = isl_union_map_copy(data->umap);
3512 umap = data->fn(umap, pma);
3513 data->res = isl_union_map_union(data->res, umap);
3515 return data->res ? isl_stat_ok : isl_stat_error;
3518 /* Compute the preimage of the domain or range of "umap" under the function
3519 * represented by "upma".
3520 * In other words, plug in "upma" in the domain or range of "umap".
3521 * The function "fn" performs the actual preimage computation
3522 * on a piecewise multi-affine function.
3524 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3525 __isl_take isl_union_map *umap,
3526 __isl_take isl_union_pw_multi_aff *upma,
3527 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3528 __isl_take isl_pw_multi_aff *pma))
3530 struct isl_union_map_preimage_upma_data data;
3532 data.umap = umap;
3533 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3534 data.fn = fn;
3535 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3536 &preimage_upma, &data) < 0)
3537 data.res = isl_union_map_free(data.res);
3539 isl_union_map_free(umap);
3540 isl_union_pw_multi_aff_free(upma);
3542 return data.res;
3545 /* Compute the preimage of the domain of "umap" under the function
3546 * represented by "upma".
3547 * In other words, plug in "upma" in the domain of "umap".
3548 * The result contains maps that live in the same spaces as the maps of "umap"
3549 * with domain space equal to one of the target spaces of "upma",
3550 * except that the domain has been replaced by one of the the domain spaces that
3551 * corresponds to that target space of "upma".
3553 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3554 __isl_take isl_union_map *umap,
3555 __isl_take isl_union_pw_multi_aff *upma)
3557 return preimage_union_pw_multi_aff(umap, upma,
3558 &isl_union_map_preimage_domain_pw_multi_aff);
3561 /* Compute the preimage of the range of "umap" under the function
3562 * represented by "upma".
3563 * In other words, plug in "upma" in the range of "umap".
3564 * The result contains maps that live in the same spaces as the maps of "umap"
3565 * with range space equal to one of the target spaces of "upma",
3566 * except that the range has been replaced by one of the the domain spaces that
3567 * corresponds to that target space of "upma".
3569 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3570 __isl_take isl_union_map *umap,
3571 __isl_take isl_union_pw_multi_aff *upma)
3573 return preimage_union_pw_multi_aff(umap, upma,
3574 &isl_union_map_preimage_range_pw_multi_aff);
3577 /* Compute the preimage of "uset" under the function represented by "upma".
3578 * In other words, plug in "upma" in the range of "uset".
3579 * The result contains sets that live in the same spaces as the sets of "uset"
3580 * with space equal to one of the target spaces of "upma",
3581 * except that the space has been replaced by one of the the domain spaces that
3582 * corresponds to that target space of "upma".
3584 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3585 __isl_take isl_union_set *uset,
3586 __isl_take isl_union_pw_multi_aff *upma)
3588 return preimage_union_pw_multi_aff(uset, upma,
3589 &isl_union_set_preimage_pw_multi_aff);
3592 /* Reset the user pointer on all identifiers of parameters and tuples
3593 * of the space of *entry.
3595 static isl_stat reset_user(void **entry, void *user)
3597 isl_map **map = (isl_map **)entry;
3599 *map = isl_map_reset_user(*map);
3601 return *map ? isl_stat_ok : isl_stat_error;
3604 /* Reset the user pointer on all identifiers of parameters and tuples
3605 * of the spaces of "umap".
3607 __isl_give isl_union_map *isl_union_map_reset_user(
3608 __isl_take isl_union_map *umap)
3610 umap = isl_union_map_cow(umap);
3611 if (!umap)
3612 return NULL;
3613 umap->dim = isl_space_reset_user(umap->dim);
3614 if (!umap->dim)
3615 return isl_union_map_free(umap);
3616 umap = un_op(umap, &reset_user);
3618 return umap;
3621 /* Reset the user pointer on all identifiers of parameters and tuples
3622 * of the spaces of "uset".
3624 __isl_give isl_union_set *isl_union_set_reset_user(
3625 __isl_take isl_union_set *uset)
3627 return isl_union_map_reset_user(uset);
3630 /* Internal data structure for isl_union_map_project_out.
3631 * "type", "first" and "n" are the arguments for the isl_map_project_out
3632 * call.
3633 * "res" collects the results.
3635 struct isl_union_map_project_out_data {
3636 enum isl_dim_type type;
3637 unsigned first;
3638 unsigned n;
3640 isl_union_map *res;
3643 /* Turn the data->n dimensions of type data->type, starting at data->first
3644 * into existentially quantified variables and add the result to data->res.
3646 static isl_stat project_out(__isl_take isl_map *map, void *user)
3648 struct isl_union_map_project_out_data *data = user;
3650 map = isl_map_project_out(map, data->type, data->first, data->n);
3651 data->res = isl_union_map_add_map(data->res, map);
3653 return isl_stat_ok;
3656 /* Turn the "n" dimensions of type "type", starting at "first"
3657 * into existentially quantified variables.
3658 * Since the space of an isl_union_map only contains parameters,
3659 * type is required to be equal to isl_dim_param.
3661 __isl_give isl_union_map *isl_union_map_project_out(
3662 __isl_take isl_union_map *umap,
3663 enum isl_dim_type type, unsigned first, unsigned n)
3665 isl_space *space;
3666 struct isl_union_map_project_out_data data = { type, first, n };
3668 if (!umap)
3669 return NULL;
3671 if (type != isl_dim_param)
3672 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3673 "can only project out parameters",
3674 return isl_union_map_free(umap));
3676 space = isl_union_map_get_space(umap);
3677 space = isl_space_drop_dims(space, type, first, n);
3678 data.res = isl_union_map_empty(space);
3679 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3680 data.res = isl_union_map_free(data.res);
3682 isl_union_map_free(umap);
3684 return data.res;
3687 /* Turn the "n" dimensions of type "type", starting at "first"
3688 * into existentially quantified variables.
3689 * Since the space of an isl_union_set only contains parameters,
3690 * "type" is required to be equal to isl_dim_param.
3692 __isl_give isl_union_set *isl_union_set_project_out(
3693 __isl_take isl_union_set *uset,
3694 enum isl_dim_type type, unsigned first, unsigned n)
3696 return isl_union_map_project_out(uset, type, first, n);
3699 /* Internal data structure for isl_union_map_involves_dims.
3700 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3702 struct isl_union_map_involves_dims_data {
3703 unsigned first;
3704 unsigned n;
3707 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3709 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3711 struct isl_union_map_involves_dims_data *data = user;
3712 isl_bool involves;
3714 involves = isl_map_involves_dims(map,
3715 isl_dim_param, data->first, data->n);
3716 if (involves < 0)
3717 return isl_bool_error;
3718 return !involves;
3721 /* Does "umap" involve any of the n parameters starting at first?
3722 * "type" is required to be set to isl_dim_param.
3724 * "umap" involves any of those parameters if any of its maps
3725 * involve the parameters. In other words, "umap" does not
3726 * involve any of the parameters if all its maps to not
3727 * involve the parameters.
3729 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3730 enum isl_dim_type type, unsigned first, unsigned n)
3732 struct isl_union_map_involves_dims_data data = { first, n };
3733 isl_bool excludes;
3735 if (type != isl_dim_param)
3736 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3737 "can only reference parameters", return isl_bool_error);
3739 excludes = union_map_forall_user(umap, &map_excludes, &data);
3741 if (excludes < 0)
3742 return isl_bool_error;
3744 return !excludes;
3747 /* Internal data structure for isl_union_map_reset_range_space.
3748 * "range" is the space from which to set the range space.
3749 * "res" collects the results.
3751 struct isl_union_map_reset_range_space_data {
3752 isl_space *range;
3753 isl_union_map *res;
3756 /* Replace the range space of "map" by the range space of data->range and
3757 * add the result to data->res.
3759 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3761 struct isl_union_map_reset_range_space_data *data = user;
3762 isl_space *space;
3764 space = isl_map_get_space(map);
3765 space = isl_space_domain(space);
3766 space = isl_space_extend_domain_with_range(space,
3767 isl_space_copy(data->range));
3768 map = isl_map_reset_space(map, space);
3769 data->res = isl_union_map_add_map(data->res, map);
3771 return data->res ? isl_stat_ok : isl_stat_error;
3774 /* Replace the range space of all the maps in "umap" by
3775 * the range space of "space".
3777 * This assumes that all maps have the same output dimension.
3778 * This function should therefore not be made publicly available.
3780 * Since the spaces of the maps change, so do their hash value.
3781 * We therefore need to create a new isl_union_map.
3783 __isl_give isl_union_map *isl_union_map_reset_range_space(
3784 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3786 struct isl_union_map_reset_range_space_data data = { space };
3788 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3789 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3790 data.res = isl_union_map_free(data.res);
3792 isl_space_free(space);
3793 isl_union_map_free(umap);
3794 return data.res;
3797 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3798 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3799 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3800 * to the domain and the range of each map.
3801 * "res" collects the results.
3803 struct isl_union_order_at_data {
3804 isl_multi_union_pw_aff *mupa;
3805 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3806 __isl_take isl_multi_pw_aff *mpa2);
3807 isl_union_map *res;
3810 /* Intersect "map" with the result of applying data->order to
3811 * the functions in data->mupa that apply to the domain and the range
3812 * of "map" and add the result to data->res.
3814 static isl_stat order_at(__isl_take isl_map *map, void *user)
3816 struct isl_union_order_at_data *data = user;
3817 isl_space *space;
3818 isl_multi_pw_aff *mpa1, *mpa2;
3819 isl_map *order;
3821 space = isl_space_domain(isl_map_get_space(map));
3822 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3823 space = isl_space_range(isl_map_get_space(map));
3824 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3825 order = data->order(mpa1, mpa2);
3826 map = isl_map_intersect(map, order);
3827 data->res = isl_union_map_add_map(data->res, map);
3829 return data->res ? isl_stat_ok : isl_stat_error;
3832 /* Intersect each map in "umap" with the result of calling "order"
3833 * on the functions is "mupa" that apply to the domain and the range
3834 * of the map.
3836 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3837 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3838 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3839 __isl_take isl_multi_pw_aff *mpa2))
3841 struct isl_union_order_at_data data;
3843 umap = isl_union_map_align_params(umap,
3844 isl_multi_union_pw_aff_get_space(mupa));
3845 mupa = isl_multi_union_pw_aff_align_params(mupa,
3846 isl_union_map_get_space(umap));
3847 data.mupa = mupa;
3848 data.order = order;
3849 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3850 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3851 data.res = isl_union_map_free(data.res);
3853 isl_multi_union_pw_aff_free(mupa);
3854 isl_union_map_free(umap);
3855 return data.res;
3858 /* Return the subset of "umap" where the domain and the range
3859 * have equal "mupa" values.
3861 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3862 __isl_take isl_union_map *umap,
3863 __isl_take isl_multi_union_pw_aff *mupa)
3865 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3866 &isl_multi_pw_aff_eq_map);
3869 /* Return the subset of "umap" where the domain has a lexicographically
3870 * smaller "mupa" value than the range.
3872 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3873 __isl_take isl_union_map *umap,
3874 __isl_take isl_multi_union_pw_aff *mupa)
3876 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3877 &isl_multi_pw_aff_lex_lt_map);
3880 /* Return the subset of "umap" where the domain has a lexicographically
3881 * greater "mupa" value than the range.
3883 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3884 __isl_take isl_union_map *umap,
3885 __isl_take isl_multi_union_pw_aff *mupa)
3887 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3888 &isl_multi_pw_aff_lex_gt_map);
3891 /* Return the union of the elements in the list "list".
3893 __isl_give isl_union_set *isl_union_set_list_union(
3894 __isl_take isl_union_set_list *list)
3896 int i, n;
3897 isl_ctx *ctx;
3898 isl_space *space;
3899 isl_union_set *res;
3901 if (!list)
3902 return NULL;
3904 ctx = isl_union_set_list_get_ctx(list);
3905 space = isl_space_params_alloc(ctx, 0);
3906 res = isl_union_set_empty(space);
3908 n = isl_union_set_list_n_union_set(list);
3909 for (i = 0; i < n; ++i) {
3910 isl_union_set *uset_i;
3912 uset_i = isl_union_set_list_get_union_set(list, i);
3913 res = isl_union_set_union(res, uset_i);
3916 isl_union_set_list_free(list);
3917 return res;
3920 /* Update *hash with the hash value of "map".
3922 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3924 uint32_t *hash = user;
3925 uint32_t map_hash;
3927 map_hash = isl_map_get_hash(map);
3928 isl_hash_hash(*hash, map_hash);
3930 isl_map_free(map);
3931 return isl_stat_ok;
3934 /* Return a hash value that digests "umap".
3936 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3938 uint32_t hash;
3940 if (!umap)
3941 return 0;
3943 hash = isl_hash_init();
3944 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3945 return 0;
3947 return hash;
3950 /* Return a hash value that digests "uset".
3952 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3954 return isl_union_map_get_hash(uset);