mark isl_space_extend as deprecated
[isl.git] / isl_union_map.c
blob53b0960ddabf1e965f98c06aeed8ebaa1b935c70
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 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
32 /* Return the number of parameters of "umap", where "type"
33 * is required to be set to isl_dim_param.
35 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
36 enum isl_dim_type type)
38 if (!umap)
39 return 0;
41 if (type != isl_dim_param)
42 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
43 "can only reference parameters", return 0);
45 return isl_space_dim(umap->dim, type);
48 /* Return the number of parameters of "uset", where "type"
49 * is required to be set to isl_dim_param.
51 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
52 enum isl_dim_type type)
54 return isl_union_map_dim(uset, type);
57 /* Return the id of the specified dimension.
59 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
60 enum isl_dim_type type, unsigned pos)
62 if (!umap)
63 return NULL;
65 if (type != isl_dim_param)
66 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
67 "can only reference parameters", return NULL);
69 return isl_space_get_dim_id(umap->dim, type, pos);
72 /* Is this union set a parameter domain?
74 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
76 isl_set *set;
77 isl_bool params;
79 if (!uset)
80 return isl_bool_error;
81 if (uset->table.n != 1)
82 return isl_bool_false;
84 set = isl_set_from_union_set(isl_union_set_copy(uset));
85 params = isl_set_is_params(set);
86 isl_set_free(set);
87 return params;
90 static __isl_give isl_union_map *isl_union_map_alloc(
91 __isl_take isl_space *space, int size)
93 isl_union_map *umap;
95 space = isl_space_params(space);
96 if (!space)
97 return NULL;
99 umap = isl_calloc_type(space->ctx, isl_union_map);
100 if (!umap) {
101 isl_space_free(space);
102 return NULL;
105 umap->ref = 1;
106 umap->dim = space;
107 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
108 return isl_union_map_free(umap);
110 return umap;
113 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
115 return isl_union_map_alloc(dim, 16);
118 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
120 return isl_union_map_empty(dim);
123 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
125 return umap ? umap->dim->ctx : NULL;
128 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
130 return uset ? uset->dim->ctx : NULL;
133 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
135 if (!umap)
136 return NULL;
137 return isl_space_copy(umap->dim);
140 /* Return the position of the parameter with the given name
141 * in "umap".
142 * Return -1 if no such dimension can be found.
144 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
145 enum isl_dim_type type, const char *name)
147 if (!umap)
148 return -1;
149 return isl_space_find_dim_by_name(umap->dim, type, name);
152 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
154 return isl_union_map_get_space(uset);
157 static isl_stat free_umap_entry(void **entry, void *user)
159 isl_map *map = *entry;
160 isl_map_free(map);
161 return isl_stat_ok;
164 static isl_stat add_map(__isl_take isl_map *map, void *user)
166 isl_union_map **umap = (isl_union_map **)user;
168 *umap = isl_union_map_add_map(*umap, map);
170 return isl_stat_ok;
173 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
175 isl_union_map *dup;
177 if (!umap)
178 return NULL;
180 dup = isl_union_map_empty(isl_space_copy(umap->dim));
181 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
182 goto error;
183 return dup;
184 error:
185 isl_union_map_free(dup);
186 return NULL;
189 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
191 if (!umap)
192 return NULL;
194 if (umap->ref == 1)
195 return umap;
196 umap->ref--;
197 return isl_union_map_dup(umap);
200 struct isl_union_align {
201 isl_reordering *exp;
202 isl_union_map *res;
205 static isl_stat align_entry(void **entry, void *user)
207 isl_map *map = *entry;
208 isl_reordering *exp;
209 struct isl_union_align *data = user;
211 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
212 isl_map_get_space(map));
214 data->res = isl_union_map_add_map(data->res,
215 isl_map_realign(isl_map_copy(map), exp));
217 return isl_stat_ok;
220 /* Align the parameters of umap along those of model.
221 * The result has the parameters of model first, in the same order
222 * as they appear in model, followed by any remaining parameters of
223 * umap that do not appear in model.
225 __isl_give isl_union_map *isl_union_map_align_params(
226 __isl_take isl_union_map *umap, __isl_take isl_space *model)
228 struct isl_union_align data = { NULL, NULL };
230 if (!umap || !model)
231 goto error;
233 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
234 isl_space_free(model);
235 return umap;
238 model = isl_space_params(model);
239 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
240 if (!data.exp)
241 goto error;
243 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
244 umap->table.n);
245 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
246 &align_entry, &data) < 0)
247 goto error;
249 isl_reordering_free(data.exp);
250 isl_union_map_free(umap);
251 isl_space_free(model);
252 return data.res;
253 error:
254 isl_reordering_free(data.exp);
255 isl_union_map_free(umap);
256 isl_union_map_free(data.res);
257 isl_space_free(model);
258 return NULL;
261 __isl_give isl_union_set *isl_union_set_align_params(
262 __isl_take isl_union_set *uset, __isl_take isl_space *model)
264 return isl_union_map_align_params(uset, model);
267 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
268 __isl_take isl_union_map *umap2)
270 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
271 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
273 umap1 = isl_union_map_cow(umap1);
275 if (!umap1 || !umap2)
276 goto error;
278 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
279 goto error;
281 isl_union_map_free(umap2);
283 return umap1;
284 error:
285 isl_union_map_free(umap1);
286 isl_union_map_free(umap2);
287 return NULL;
290 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
291 __isl_take isl_union_set *uset2)
293 return isl_union_map_union(uset1, uset2);
296 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
298 if (!umap)
299 return NULL;
301 umap->ref++;
302 return umap;
305 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
307 return isl_union_map_copy(uset);
310 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
312 if (!umap)
313 return NULL;
315 if (--umap->ref > 0)
316 return NULL;
318 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
319 &free_umap_entry, NULL);
320 isl_hash_table_clear(&umap->table);
321 isl_space_free(umap->dim);
322 free(umap);
323 return NULL;
326 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
328 return isl_union_map_free(uset);
331 static int has_dim(const void *entry, const void *val)
333 isl_map *map = (isl_map *)entry;
334 isl_space *dim = (isl_space *)val;
336 return isl_space_is_equal(map->dim, dim);
339 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
340 __isl_take isl_map *map)
342 uint32_t hash;
343 struct isl_hash_table_entry *entry;
345 if (!map || !umap)
346 goto error;
348 if (isl_map_plain_is_empty(map)) {
349 isl_map_free(map);
350 return umap;
353 if (!isl_space_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
354 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
355 map = isl_map_align_params(map, isl_union_map_get_space(umap));
358 umap = isl_union_map_cow(umap);
360 if (!map || !umap)
361 goto error;
363 hash = isl_space_get_hash(map->dim);
364 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
365 &has_dim, map->dim, 1);
366 if (!entry)
367 goto error;
369 if (!entry->data)
370 entry->data = map;
371 else {
372 entry->data = isl_map_union(entry->data, isl_map_copy(map));
373 if (!entry->data)
374 goto error;
375 isl_map_free(map);
378 return umap;
379 error:
380 isl_map_free(map);
381 isl_union_map_free(umap);
382 return NULL;
385 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
386 __isl_take isl_set *set)
388 return isl_union_map_add_map(uset, set_to_map(set));
391 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
393 isl_space *dim;
394 isl_union_map *umap;
396 if (!map)
397 return NULL;
399 dim = isl_map_get_space(map);
400 dim = isl_space_params(dim);
401 umap = isl_union_map_empty(dim);
402 umap = isl_union_map_add_map(umap, map);
404 return umap;
407 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
409 return isl_union_map_from_map(set_to_map(set));
412 __isl_give isl_union_map *isl_union_map_from_basic_map(
413 __isl_take isl_basic_map *bmap)
415 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
418 __isl_give isl_union_set *isl_union_set_from_basic_set(
419 __isl_take isl_basic_set *bset)
421 return isl_union_map_from_basic_map(bset);
424 struct isl_union_map_foreach_data
426 isl_stat (*fn)(__isl_take isl_map *map, void *user);
427 void *user;
430 static isl_stat call_on_copy(void **entry, void *user)
432 isl_map *map = *entry;
433 struct isl_union_map_foreach_data *data;
434 data = (struct isl_union_map_foreach_data *)user;
436 return data->fn(isl_map_copy(map), data->user);
439 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
441 return umap ? umap->table.n : 0;
444 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
446 return uset ? uset->table.n : 0;
449 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
450 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
452 struct isl_union_map_foreach_data data = { fn, user };
454 if (!umap)
455 return isl_stat_error;
457 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
458 &call_on_copy, &data);
461 static isl_stat copy_map(void **entry, void *user)
463 isl_map *map = *entry;
464 isl_map **map_p = user;
466 *map_p = isl_map_copy(map);
468 return isl_stat_error;
471 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
473 isl_ctx *ctx;
474 isl_map *map = NULL;
476 if (!umap)
477 return NULL;
478 ctx = isl_union_map_get_ctx(umap);
479 if (umap->table.n != 1)
480 isl_die(ctx, isl_error_invalid,
481 "union map needs to contain elements in exactly "
482 "one space", goto error);
484 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
486 isl_union_map_free(umap);
488 return map;
489 error:
490 isl_union_map_free(umap);
491 return NULL;
494 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
496 return isl_map_from_union_map(uset);
499 /* Extract the map in "umap" that lives in the given space (ignoring
500 * parameters).
502 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
503 __isl_take isl_space *space)
505 uint32_t hash;
506 struct isl_hash_table_entry *entry;
508 space = isl_space_drop_dims(space, isl_dim_param,
509 0, isl_space_dim(space, isl_dim_param));
510 space = isl_space_align_params(space, isl_union_map_get_space(umap));
511 if (!umap || !space)
512 goto error;
514 hash = isl_space_get_hash(space);
515 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
516 &has_dim, space, 0);
517 if (!entry)
518 return isl_map_empty(space);
519 isl_space_free(space);
520 return isl_map_copy(entry->data);
521 error:
522 isl_space_free(space);
523 return NULL;
526 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
527 __isl_take isl_space *dim)
529 return set_from_map(isl_union_map_extract_map(uset, dim));
532 /* Check if umap contains a map in the given space.
534 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
535 __isl_keep isl_space *space)
537 uint32_t hash;
538 struct isl_hash_table_entry *entry;
540 if (!umap || !space)
541 return isl_bool_error;
543 hash = isl_space_get_hash(space);
544 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
545 &has_dim, space, 0);
546 return !!entry;
549 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
550 __isl_keep isl_space *space)
552 return isl_union_map_contains(uset, space);
555 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
556 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
558 return isl_union_map_foreach_map(uset,
559 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
562 struct isl_union_set_foreach_point_data {
563 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
564 void *user;
567 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
569 struct isl_union_set_foreach_point_data *data = user;
570 isl_stat r;
572 r = isl_set_foreach_point(set, data->fn, data->user);
573 isl_set_free(set);
575 return r;
578 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
579 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
581 struct isl_union_set_foreach_point_data data = { fn, user };
582 return isl_union_set_foreach_set(uset, &foreach_point, &data);
585 struct isl_union_map_gen_bin_data {
586 isl_union_map *umap2;
587 isl_union_map *res;
590 static isl_stat subtract_entry(void **entry, void *user)
592 struct isl_union_map_gen_bin_data *data = user;
593 uint32_t hash;
594 struct isl_hash_table_entry *entry2;
595 isl_map *map = *entry;
597 hash = isl_space_get_hash(map->dim);
598 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
599 hash, &has_dim, map->dim, 0);
600 map = isl_map_copy(map);
601 if (entry2) {
602 int empty;
603 map = isl_map_subtract(map, isl_map_copy(entry2->data));
605 empty = isl_map_is_empty(map);
606 if (empty < 0) {
607 isl_map_free(map);
608 return isl_stat_error;
610 if (empty) {
611 isl_map_free(map);
612 return isl_stat_ok;
615 data->res = isl_union_map_add_map(data->res, map);
617 return isl_stat_ok;
620 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
621 __isl_take isl_union_map *umap2, isl_stat (*fn)(void **, void *))
623 struct isl_union_map_gen_bin_data data = { NULL, NULL };
625 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
626 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
628 if (!umap1 || !umap2)
629 goto error;
631 data.umap2 = umap2;
632 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
633 umap1->table.n);
634 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
635 fn, &data) < 0)
636 goto error;
638 isl_union_map_free(umap1);
639 isl_union_map_free(umap2);
640 return data.res;
641 error:
642 isl_union_map_free(umap1);
643 isl_union_map_free(umap2);
644 isl_union_map_free(data.res);
645 return NULL;
648 __isl_give isl_union_map *isl_union_map_subtract(
649 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
651 return gen_bin_op(umap1, umap2, &subtract_entry);
654 __isl_give isl_union_set *isl_union_set_subtract(
655 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
657 return isl_union_map_subtract(uset1, uset2);
660 struct isl_union_map_gen_bin_set_data {
661 isl_set *set;
662 isl_union_map *res;
665 static isl_stat intersect_params_entry(void **entry, void *user)
667 struct isl_union_map_gen_bin_set_data *data = user;
668 isl_map *map = *entry;
669 int empty;
671 map = isl_map_copy(map);
672 map = isl_map_intersect_params(map, isl_set_copy(data->set));
674 empty = isl_map_is_empty(map);
675 if (empty < 0) {
676 isl_map_free(map);
677 return isl_stat_error;
680 data->res = isl_union_map_add_map(data->res, map);
682 return isl_stat_ok;
685 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
686 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
688 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
690 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
691 set = isl_set_align_params(set, isl_union_map_get_space(umap));
693 if (!umap || !set)
694 goto error;
696 data.set = set;
697 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
698 umap->table.n);
699 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
700 fn, &data) < 0)
701 goto error;
703 isl_union_map_free(umap);
704 isl_set_free(set);
705 return data.res;
706 error:
707 isl_union_map_free(umap);
708 isl_set_free(set);
709 isl_union_map_free(data.res);
710 return NULL;
713 /* Intersect "umap" with the parameter domain "set".
715 * If "set" does not have any constraints, then we can return immediately.
717 __isl_give isl_union_map *isl_union_map_intersect_params(
718 __isl_take isl_union_map *umap, __isl_take isl_set *set)
720 int is_universe;
722 is_universe = isl_set_plain_is_universe(set);
723 if (is_universe < 0)
724 goto error;
725 if (is_universe) {
726 isl_set_free(set);
727 return umap;
730 return gen_bin_set_op(umap, set, &intersect_params_entry);
731 error:
732 isl_union_map_free(umap);
733 isl_set_free(set);
734 return NULL;
737 __isl_give isl_union_set *isl_union_set_intersect_params(
738 __isl_take isl_union_set *uset, __isl_take isl_set *set)
740 return isl_union_map_intersect_params(uset, set);
743 static __isl_give isl_union_map *union_map_intersect_params(
744 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
746 return isl_union_map_intersect_params(umap,
747 isl_set_from_union_set(uset));
750 static __isl_give isl_union_map *union_map_gist_params(
751 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
753 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
756 struct isl_union_map_match_bin_data {
757 isl_union_map *umap2;
758 isl_union_map *res;
759 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
762 static isl_stat match_bin_entry(void **entry, void *user)
764 struct isl_union_map_match_bin_data *data = user;
765 uint32_t hash;
766 struct isl_hash_table_entry *entry2;
767 isl_map *map = *entry;
768 int empty;
770 hash = isl_space_get_hash(map->dim);
771 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
772 hash, &has_dim, map->dim, 0);
773 if (!entry2)
774 return isl_stat_ok;
776 map = isl_map_copy(map);
777 map = data->fn(map, isl_map_copy(entry2->data));
779 empty = isl_map_is_empty(map);
780 if (empty < 0) {
781 isl_map_free(map);
782 return isl_stat_error;
784 if (empty) {
785 isl_map_free(map);
786 return isl_stat_ok;
789 data->res = isl_union_map_add_map(data->res, map);
791 return isl_stat_ok;
794 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
795 __isl_take isl_union_map *umap2,
796 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
798 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
800 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
801 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
803 if (!umap1 || !umap2)
804 goto error;
806 data.umap2 = umap2;
807 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
808 umap1->table.n);
809 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
810 &match_bin_entry, &data) < 0)
811 goto error;
813 isl_union_map_free(umap1);
814 isl_union_map_free(umap2);
815 return data.res;
816 error:
817 isl_union_map_free(umap1);
818 isl_union_map_free(umap2);
819 isl_union_map_free(data.res);
820 return NULL;
823 __isl_give isl_union_map *isl_union_map_intersect(
824 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
826 return match_bin_op(umap1, umap2, &isl_map_intersect);
829 /* Compute the intersection of the two union_sets.
830 * As a special case, if exactly one of the two union_sets
831 * is a parameter domain, then intersect the parameter domain
832 * of the other one with this set.
834 __isl_give isl_union_set *isl_union_set_intersect(
835 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
837 int p1, p2;
839 p1 = isl_union_set_is_params(uset1);
840 p2 = isl_union_set_is_params(uset2);
841 if (p1 < 0 || p2 < 0)
842 goto error;
843 if (!p1 && p2)
844 return union_map_intersect_params(uset1, uset2);
845 if (p1 && !p2)
846 return union_map_intersect_params(uset2, uset1);
847 return isl_union_map_intersect(uset1, uset2);
848 error:
849 isl_union_set_free(uset1);
850 isl_union_set_free(uset2);
851 return NULL;
854 static isl_stat gist_params_entry(void **entry, void *user)
856 struct isl_union_map_gen_bin_set_data *data = user;
857 isl_map *map = *entry;
858 int empty;
860 map = isl_map_copy(map);
861 map = isl_map_gist_params(map, isl_set_copy(data->set));
863 empty = isl_map_is_empty(map);
864 if (empty < 0) {
865 isl_map_free(map);
866 return isl_stat_error;
869 data->res = isl_union_map_add_map(data->res, map);
871 return isl_stat_ok;
874 __isl_give isl_union_map *isl_union_map_gist_params(
875 __isl_take isl_union_map *umap, __isl_take isl_set *set)
877 return gen_bin_set_op(umap, set, &gist_params_entry);
880 __isl_give isl_union_set *isl_union_set_gist_params(
881 __isl_take isl_union_set *uset, __isl_take isl_set *set)
883 return isl_union_map_gist_params(uset, set);
886 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
887 __isl_take isl_union_map *context)
889 return match_bin_op(umap, context, &isl_map_gist);
892 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
893 __isl_take isl_union_set *context)
895 if (isl_union_set_is_params(context))
896 return union_map_gist_params(uset, context);
897 return isl_union_map_gist(uset, context);
900 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
901 __isl_take isl_map *set2)
903 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
906 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
907 __isl_take isl_map *set2)
909 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
912 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
913 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
915 return match_bin_op(uset1, uset2, &lex_lt_set);
918 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
919 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
921 return match_bin_op(uset1, uset2, &lex_le_set);
924 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
925 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
927 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
930 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
931 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
933 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
936 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
937 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
939 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
942 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
943 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
945 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
948 static isl_stat intersect_domain_entry(void **entry, void *user)
950 struct isl_union_map_gen_bin_data *data = user;
951 uint32_t hash;
952 struct isl_hash_table_entry *entry2;
953 isl_space *dim;
954 isl_map *map = *entry;
955 isl_bool empty;
957 dim = isl_map_get_space(map);
958 dim = isl_space_domain(dim);
959 hash = isl_space_get_hash(dim);
960 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
961 hash, &has_dim, dim, 0);
962 isl_space_free(dim);
963 if (!entry2)
964 return isl_stat_ok;
966 map = isl_map_copy(map);
967 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
969 empty = isl_map_is_empty(map);
970 if (empty < 0) {
971 isl_map_free(map);
972 return isl_stat_error;
974 if (empty) {
975 isl_map_free(map);
976 return isl_stat_ok;
979 data->res = isl_union_map_add_map(data->res, map);
981 return isl_stat_ok;
984 /* Intersect the domain of "umap" with "uset".
985 * If "uset" is a parameters domain, then intersect the parameter
986 * domain of "umap" with this set.
988 __isl_give isl_union_map *isl_union_map_intersect_domain(
989 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
991 if (isl_union_set_is_params(uset))
992 return union_map_intersect_params(umap, uset);
993 return gen_bin_op(umap, uset, &intersect_domain_entry);
996 /* Remove the elements of data->umap2 from the domain of *entry
997 * and add the result to data->res.
999 static isl_stat subtract_domain_entry(void **entry, void *user)
1001 struct isl_union_map_gen_bin_data *data = user;
1002 uint32_t hash;
1003 struct isl_hash_table_entry *entry2;
1004 isl_space *dim;
1005 isl_map *map = *entry;
1006 isl_bool empty;
1008 dim = isl_map_get_space(map);
1009 dim = isl_space_domain(dim);
1010 hash = isl_space_get_hash(dim);
1011 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1012 hash, &has_dim, dim, 0);
1013 isl_space_free(dim);
1015 map = isl_map_copy(map);
1017 if (!entry2) {
1018 data->res = isl_union_map_add_map(data->res, map);
1019 return isl_stat_ok;
1022 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1024 empty = isl_map_is_empty(map);
1025 if (empty < 0) {
1026 isl_map_free(map);
1027 return isl_stat_error;
1029 if (empty) {
1030 isl_map_free(map);
1031 return isl_stat_ok;
1034 data->res = isl_union_map_add_map(data->res, map);
1036 return isl_stat_ok;
1039 /* Remove the elements of "uset" from the domain of "umap".
1041 __isl_give isl_union_map *isl_union_map_subtract_domain(
1042 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1044 return gen_bin_op(umap, dom, &subtract_domain_entry);
1047 /* Remove the elements of data->umap2 from the range of *entry
1048 * and add the result to data->res.
1050 static isl_stat subtract_range_entry(void **entry, void *user)
1052 struct isl_union_map_gen_bin_data *data = user;
1053 uint32_t hash;
1054 struct isl_hash_table_entry *entry2;
1055 isl_space *space;
1056 isl_map *map = *entry;
1057 isl_bool empty;
1059 space = isl_map_get_space(map);
1060 space = isl_space_range(space);
1061 hash = isl_space_get_hash(space);
1062 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1063 hash, &has_dim, space, 0);
1064 isl_space_free(space);
1066 map = isl_map_copy(map);
1068 if (!entry2) {
1069 data->res = isl_union_map_add_map(data->res, map);
1070 return isl_stat_ok;
1073 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1075 empty = isl_map_is_empty(map);
1076 if (empty < 0) {
1077 isl_map_free(map);
1078 return isl_stat_error;
1080 if (empty) {
1081 isl_map_free(map);
1082 return isl_stat_ok;
1085 data->res = isl_union_map_add_map(data->res, map);
1087 return isl_stat_ok;
1090 /* Remove the elements of "uset" from the range of "umap".
1092 __isl_give isl_union_map *isl_union_map_subtract_range(
1093 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1095 return gen_bin_op(umap, dom, &subtract_range_entry);
1098 static isl_stat gist_domain_entry(void **entry, void *user)
1100 struct isl_union_map_gen_bin_data *data = user;
1101 uint32_t hash;
1102 struct isl_hash_table_entry *entry2;
1103 isl_space *dim;
1104 isl_map *map = *entry;
1105 isl_bool empty;
1107 dim = isl_map_get_space(map);
1108 dim = isl_space_domain(dim);
1109 hash = isl_space_get_hash(dim);
1110 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1111 hash, &has_dim, dim, 0);
1112 isl_space_free(dim);
1113 if (!entry2)
1114 return isl_stat_ok;
1116 map = isl_map_copy(map);
1117 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1119 empty = isl_map_is_empty(map);
1120 if (empty < 0) {
1121 isl_map_free(map);
1122 return isl_stat_error;
1125 data->res = isl_union_map_add_map(data->res, map);
1127 return isl_stat_ok;
1130 /* Compute the gist of "umap" with respect to the domain "uset".
1131 * If "uset" is a parameters domain, then compute the gist
1132 * with respect to this parameter domain.
1134 __isl_give isl_union_map *isl_union_map_gist_domain(
1135 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1137 if (isl_union_set_is_params(uset))
1138 return union_map_gist_params(umap, uset);
1139 return gen_bin_op(umap, uset, &gist_domain_entry);
1142 static isl_stat gist_range_entry(void **entry, void *user)
1144 struct isl_union_map_gen_bin_data *data = user;
1145 uint32_t hash;
1146 struct isl_hash_table_entry *entry2;
1147 isl_space *space;
1148 isl_map *map = *entry;
1149 isl_bool empty;
1151 space = isl_map_get_space(map);
1152 space = isl_space_range(space);
1153 hash = isl_space_get_hash(space);
1154 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1155 hash, &has_dim, space, 0);
1156 isl_space_free(space);
1157 if (!entry2)
1158 return isl_stat_ok;
1160 map = isl_map_copy(map);
1161 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1163 empty = isl_map_is_empty(map);
1164 if (empty < 0) {
1165 isl_map_free(map);
1166 return isl_stat_error;
1169 data->res = isl_union_map_add_map(data->res, map);
1171 return isl_stat_ok;
1174 /* Compute the gist of "umap" with respect to the range "uset".
1176 __isl_give isl_union_map *isl_union_map_gist_range(
1177 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1179 return gen_bin_op(umap, uset, &gist_range_entry);
1182 static isl_stat intersect_range_entry(void **entry, void *user)
1184 struct isl_union_map_gen_bin_data *data = user;
1185 uint32_t hash;
1186 struct isl_hash_table_entry *entry2;
1187 isl_space *dim;
1188 isl_map *map = *entry;
1189 isl_bool empty;
1191 dim = isl_map_get_space(map);
1192 dim = isl_space_range(dim);
1193 hash = isl_space_get_hash(dim);
1194 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1195 hash, &has_dim, dim, 0);
1196 isl_space_free(dim);
1197 if (!entry2)
1198 return isl_stat_ok;
1200 map = isl_map_copy(map);
1201 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1203 empty = isl_map_is_empty(map);
1204 if (empty < 0) {
1205 isl_map_free(map);
1206 return isl_stat_error;
1208 if (empty) {
1209 isl_map_free(map);
1210 return isl_stat_ok;
1213 data->res = isl_union_map_add_map(data->res, map);
1215 return isl_stat_ok;
1218 __isl_give isl_union_map *isl_union_map_intersect_range(
1219 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1221 return gen_bin_op(umap, uset, &intersect_range_entry);
1224 struct isl_union_map_bin_data {
1225 isl_union_map *umap2;
1226 isl_union_map *res;
1227 isl_map *map;
1228 isl_stat (*fn)(void **entry, void *user);
1231 static isl_stat apply_range_entry(void **entry, void *user)
1233 struct isl_union_map_bin_data *data = user;
1234 isl_map *map2 = *entry;
1235 isl_bool empty;
1237 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1238 map2->dim, isl_dim_in))
1239 return isl_stat_ok;
1241 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1243 empty = isl_map_is_empty(map2);
1244 if (empty < 0) {
1245 isl_map_free(map2);
1246 return isl_stat_error;
1248 if (empty) {
1249 isl_map_free(map2);
1250 return isl_stat_ok;
1253 data->res = isl_union_map_add_map(data->res, map2);
1255 return isl_stat_ok;
1258 static isl_stat bin_entry(void **entry, void *user)
1260 struct isl_union_map_bin_data *data = user;
1261 isl_map *map = *entry;
1263 data->map = map;
1264 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1265 data->fn, data) < 0)
1266 return isl_stat_error;
1268 return isl_stat_ok;
1271 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1272 __isl_take isl_union_map *umap2,
1273 isl_stat (*fn)(void **entry, void *user))
1275 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1277 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1278 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1280 if (!umap1 || !umap2)
1281 goto error;
1283 data.umap2 = umap2;
1284 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1285 umap1->table.n);
1286 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1287 &bin_entry, &data) < 0)
1288 goto error;
1290 isl_union_map_free(umap1);
1291 isl_union_map_free(umap2);
1292 return data.res;
1293 error:
1294 isl_union_map_free(umap1);
1295 isl_union_map_free(umap2);
1296 isl_union_map_free(data.res);
1297 return NULL;
1300 __isl_give isl_union_map *isl_union_map_apply_range(
1301 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1303 return bin_op(umap1, umap2, &apply_range_entry);
1306 __isl_give isl_union_map *isl_union_map_apply_domain(
1307 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1309 umap1 = isl_union_map_reverse(umap1);
1310 umap1 = isl_union_map_apply_range(umap1, umap2);
1311 return isl_union_map_reverse(umap1);
1314 __isl_give isl_union_set *isl_union_set_apply(
1315 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1317 return isl_union_map_apply_range(uset, umap);
1320 static isl_stat map_lex_lt_entry(void **entry, void *user)
1322 struct isl_union_map_bin_data *data = user;
1323 isl_map *map2 = *entry;
1325 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1326 map2->dim, isl_dim_out))
1327 return isl_stat_ok;
1329 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1331 data->res = isl_union_map_add_map(data->res, map2);
1333 return isl_stat_ok;
1336 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1337 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1339 return bin_op(umap1, umap2, &map_lex_lt_entry);
1342 static isl_stat map_lex_le_entry(void **entry, void *user)
1344 struct isl_union_map_bin_data *data = user;
1345 isl_map *map2 = *entry;
1347 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1348 map2->dim, isl_dim_out))
1349 return isl_stat_ok;
1351 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1353 data->res = isl_union_map_add_map(data->res, map2);
1355 return isl_stat_ok;
1358 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1359 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1361 return bin_op(umap1, umap2, &map_lex_le_entry);
1364 static isl_stat product_entry(void **entry, void *user)
1366 struct isl_union_map_bin_data *data = user;
1367 isl_map *map2 = *entry;
1369 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1371 data->res = isl_union_map_add_map(data->res, map2);
1373 return isl_stat_ok;
1376 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1377 __isl_take isl_union_map *umap2)
1379 return bin_op(umap1, umap2, &product_entry);
1382 static isl_stat set_product_entry(void **entry, void *user)
1384 struct isl_union_map_bin_data *data = user;
1385 isl_set *set2 = *entry;
1387 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1389 data->res = isl_union_set_add_set(data->res, set2);
1391 return isl_stat_ok;
1394 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1395 __isl_take isl_union_set *uset2)
1397 return bin_op(uset1, uset2, &set_product_entry);
1400 static isl_stat domain_product_entry(void **entry, void *user)
1402 struct isl_union_map_bin_data *data = user;
1403 isl_map *map2 = *entry;
1405 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1406 map2->dim, isl_dim_out))
1407 return isl_stat_ok;
1409 map2 = isl_map_domain_product(isl_map_copy(data->map),
1410 isl_map_copy(map2));
1412 data->res = isl_union_map_add_map(data->res, map2);
1414 return isl_stat_ok;
1417 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1419 __isl_give isl_union_map *isl_union_map_domain_product(
1420 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1422 return bin_op(umap1, umap2, &domain_product_entry);
1425 static isl_stat range_product_entry(void **entry, void *user)
1427 struct isl_union_map_bin_data *data = user;
1428 isl_map *map2 = *entry;
1430 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1431 map2->dim, isl_dim_in))
1432 return isl_stat_ok;
1434 map2 = isl_map_range_product(isl_map_copy(data->map),
1435 isl_map_copy(map2));
1437 data->res = isl_union_map_add_map(data->res, map2);
1439 return isl_stat_ok;
1442 __isl_give isl_union_map *isl_union_map_range_product(
1443 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1445 return bin_op(umap1, umap2, &range_product_entry);
1448 /* If data->map A -> B and "map2" C -> D have the same range space,
1449 * then add (A, C) -> (B * D) to data->res.
1451 static isl_stat flat_domain_product_entry(void **entry, void *user)
1453 struct isl_union_map_bin_data *data = user;
1454 isl_map *map2 = *entry;
1456 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1457 map2->dim, isl_dim_out))
1458 return isl_stat_ok;
1460 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1461 isl_map_copy(map2));
1463 data->res = isl_union_map_add_map(data->res, map2);
1465 return isl_stat_ok;
1468 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1470 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1471 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1473 return bin_op(umap1, umap2, &flat_domain_product_entry);
1476 static isl_stat flat_range_product_entry(void **entry, void *user)
1478 struct isl_union_map_bin_data *data = user;
1479 isl_map *map2 = *entry;
1481 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1482 map2->dim, isl_dim_in))
1483 return isl_stat_ok;
1485 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1486 isl_map_copy(map2));
1488 data->res = isl_union_map_add_map(data->res, map2);
1490 return isl_stat_ok;
1493 __isl_give isl_union_map *isl_union_map_flat_range_product(
1494 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1496 return bin_op(umap1, umap2, &flat_range_product_entry);
1499 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1500 isl_stat (*fn)(void **, void *))
1502 isl_union_set *res;
1504 if (!umap)
1505 return NULL;
1507 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1508 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1509 goto error;
1511 isl_union_map_free(umap);
1512 return res;
1513 error:
1514 isl_union_map_free(umap);
1515 isl_union_set_free(res);
1516 return NULL;
1519 static isl_stat from_range_entry(void **entry, void *user)
1521 isl_map *set = *entry;
1522 isl_union_set **res = user;
1524 *res = isl_union_map_add_map(*res,
1525 isl_map_from_range(isl_set_copy(set)));
1527 return isl_stat_ok;
1530 __isl_give isl_union_map *isl_union_map_from_range(
1531 __isl_take isl_union_set *uset)
1533 return cond_un_op(uset, &from_range_entry);
1536 __isl_give isl_union_map *isl_union_map_from_domain(
1537 __isl_take isl_union_set *uset)
1539 return isl_union_map_reverse(isl_union_map_from_range(uset));
1542 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1543 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1545 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1546 isl_union_map_from_range(range));
1549 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1550 isl_stat (*fn)(void **, void *))
1552 umap = isl_union_map_cow(umap);
1553 if (!umap)
1554 return NULL;
1556 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1557 goto error;
1559 return umap;
1560 error:
1561 isl_union_map_free(umap);
1562 return NULL;
1565 static isl_stat affine_entry(void **entry, void *user)
1567 isl_map **map = (isl_map **)entry;
1569 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1571 return *map ? isl_stat_ok : isl_stat_error;
1574 __isl_give isl_union_map *isl_union_map_affine_hull(
1575 __isl_take isl_union_map *umap)
1577 return un_op(umap, &affine_entry);
1580 __isl_give isl_union_set *isl_union_set_affine_hull(
1581 __isl_take isl_union_set *uset)
1583 return isl_union_map_affine_hull(uset);
1586 static isl_stat polyhedral_entry(void **entry, void *user)
1588 isl_map **map = (isl_map **)entry;
1590 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1592 return *map ? isl_stat_ok : isl_stat_error;
1595 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1596 __isl_take isl_union_map *umap)
1598 return un_op(umap, &polyhedral_entry);
1601 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1602 __isl_take isl_union_set *uset)
1604 return isl_union_map_polyhedral_hull(uset);
1607 static isl_stat simple_entry(void **entry, void *user)
1609 isl_map **map = (isl_map **)entry;
1611 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1613 return *map ? isl_stat_ok : isl_stat_error;
1616 __isl_give isl_union_map *isl_union_map_simple_hull(
1617 __isl_take isl_union_map *umap)
1619 return un_op(umap, &simple_entry);
1622 __isl_give isl_union_set *isl_union_set_simple_hull(
1623 __isl_take isl_union_set *uset)
1625 return isl_union_map_simple_hull(uset);
1628 static isl_stat inplace_entry(void **entry, void *user)
1630 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1631 isl_map **map = (isl_map **)entry;
1632 isl_map *copy;
1634 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1635 copy = fn(isl_map_copy(*map));
1636 if (!copy)
1637 return isl_stat_error;
1639 isl_map_free(*map);
1640 *map = copy;
1642 return isl_stat_ok;
1645 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1646 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1648 if (!umap)
1649 return NULL;
1651 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1652 &inplace_entry, &fn) < 0)
1653 goto error;
1655 return umap;
1656 error:
1657 isl_union_map_free(umap);
1658 return NULL;
1661 /* Remove redundant constraints in each of the basic maps of "umap".
1662 * Since removing redundant constraints does not change the meaning
1663 * or the space, the operation can be performed in-place.
1665 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1666 __isl_take isl_union_map *umap)
1668 return inplace(umap, &isl_map_remove_redundancies);
1671 /* Remove redundant constraints in each of the basic sets of "uset".
1673 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1674 __isl_take isl_union_set *uset)
1676 return isl_union_map_remove_redundancies(uset);
1679 __isl_give isl_union_map *isl_union_map_coalesce(
1680 __isl_take isl_union_map *umap)
1682 return inplace(umap, &isl_map_coalesce);
1685 __isl_give isl_union_set *isl_union_set_coalesce(
1686 __isl_take isl_union_set *uset)
1688 return isl_union_map_coalesce(uset);
1691 __isl_give isl_union_map *isl_union_map_detect_equalities(
1692 __isl_take isl_union_map *umap)
1694 return inplace(umap, &isl_map_detect_equalities);
1697 __isl_give isl_union_set *isl_union_set_detect_equalities(
1698 __isl_take isl_union_set *uset)
1700 return isl_union_map_detect_equalities(uset);
1703 __isl_give isl_union_map *isl_union_map_compute_divs(
1704 __isl_take isl_union_map *umap)
1706 return inplace(umap, &isl_map_compute_divs);
1709 __isl_give isl_union_set *isl_union_set_compute_divs(
1710 __isl_take isl_union_set *uset)
1712 return isl_union_map_compute_divs(uset);
1715 static isl_stat lexmin_entry(void **entry, void *user)
1717 isl_map **map = (isl_map **)entry;
1719 *map = isl_map_lexmin(*map);
1721 return *map ? isl_stat_ok : isl_stat_error;
1724 __isl_give isl_union_map *isl_union_map_lexmin(
1725 __isl_take isl_union_map *umap)
1727 return un_op(umap, &lexmin_entry);
1730 __isl_give isl_union_set *isl_union_set_lexmin(
1731 __isl_take isl_union_set *uset)
1733 return isl_union_map_lexmin(uset);
1736 static isl_stat lexmax_entry(void **entry, void *user)
1738 isl_map **map = (isl_map **)entry;
1740 *map = isl_map_lexmax(*map);
1742 return *map ? isl_stat_ok : isl_stat_error;
1745 __isl_give isl_union_map *isl_union_map_lexmax(
1746 __isl_take isl_union_map *umap)
1748 return un_op(umap, &lexmax_entry);
1751 __isl_give isl_union_set *isl_union_set_lexmax(
1752 __isl_take isl_union_set *uset)
1754 return isl_union_map_lexmax(uset);
1757 static isl_stat universe_entry(void **entry, void *user)
1759 isl_map *map = *entry;
1760 isl_union_map **res = user;
1762 map = isl_map_universe(isl_map_get_space(map));
1763 *res = isl_union_map_add_map(*res, map);
1765 return isl_stat_ok;
1768 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1770 return cond_un_op(umap, &universe_entry);
1773 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1775 return isl_union_map_universe(uset);
1778 static isl_stat reverse_entry(void **entry, void *user)
1780 isl_map *map = *entry;
1781 isl_union_map **res = user;
1783 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1785 return isl_stat_ok;
1788 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1790 return cond_un_op(umap, &reverse_entry);
1793 static isl_stat params_entry(void **entry, void *user)
1795 isl_map *map = *entry;
1796 isl_union_set **res = user;
1798 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1800 return isl_stat_ok;
1803 /* Compute the parameter domain of the given union map.
1805 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1807 int empty;
1809 empty = isl_union_map_is_empty(umap);
1810 if (empty < 0)
1811 goto error;
1812 if (empty) {
1813 isl_space *space;
1814 space = isl_union_map_get_space(umap);
1815 isl_union_map_free(umap);
1816 return isl_set_empty(space);
1818 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1819 error:
1820 isl_union_map_free(umap);
1821 return NULL;
1824 /* Compute the parameter domain of the given union set.
1826 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1828 return isl_union_map_params(uset);
1831 static isl_stat domain_entry(void **entry, void *user)
1833 isl_map *map = *entry;
1834 isl_union_set **res = user;
1836 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1838 return isl_stat_ok;
1841 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1843 return cond_un_op(umap, &domain_entry);
1846 static isl_stat range_entry(void **entry, void *user)
1848 isl_map *map = *entry;
1849 isl_union_set **res = user;
1851 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1853 return isl_stat_ok;
1856 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1858 return cond_un_op(umap, &range_entry);
1861 static isl_stat domain_map_entry(void **entry, void *user)
1863 isl_map *map = *entry;
1864 isl_union_set **res = user;
1866 *res = isl_union_map_add_map(*res,
1867 isl_map_domain_map(isl_map_copy(map)));
1869 return isl_stat_ok;
1872 __isl_give isl_union_map *isl_union_map_domain_map(
1873 __isl_take isl_union_map *umap)
1875 return cond_un_op(umap, &domain_map_entry);
1878 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1879 * add the result to "res".
1881 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1883 isl_union_pw_multi_aff **res = user;
1884 isl_multi_aff *ma;
1885 isl_pw_multi_aff *pma;
1887 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1888 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1889 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1891 return *res ? isl_stat_ok : isl_stat_error;
1895 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1896 * to its domain.
1898 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1899 __isl_take isl_union_map *umap)
1901 isl_union_pw_multi_aff *res;
1903 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1904 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1905 res = isl_union_pw_multi_aff_free(res);
1907 isl_union_map_free(umap);
1908 return res;
1911 static isl_stat range_map_entry(void **entry, void *user)
1913 isl_map *map = *entry;
1914 isl_union_set **res = user;
1916 *res = isl_union_map_add_map(*res,
1917 isl_map_range_map(isl_map_copy(map)));
1919 return isl_stat_ok;
1922 __isl_give isl_union_map *isl_union_map_range_map(
1923 __isl_take isl_union_map *umap)
1925 return cond_un_op(umap, &range_map_entry);
1928 /* Check if "set" is of the form A[B -> C].
1929 * If so, add A[B -> C] -> B to "res".
1931 static isl_stat wrapped_domain_map_entry(void **entry, void *user)
1933 isl_set *set = *entry;
1934 isl_union_set **res = user;
1935 int wrapping;
1937 wrapping = isl_set_is_wrapping(set);
1938 if (wrapping < 0)
1939 return isl_stat_error;
1940 if (!wrapping)
1941 return isl_stat_ok;
1943 *res = isl_union_map_add_map(*res,
1944 isl_set_wrapped_domain_map(isl_set_copy(set)));
1946 return isl_stat_ok;
1949 /* Given a collection of wrapped maps of the form A[B -> C],
1950 * return the collection of maps A[B -> C] -> B.
1952 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1953 __isl_take isl_union_set *uset)
1955 return cond_un_op(uset, &wrapped_domain_map_entry);
1958 static isl_stat deltas_entry(void **entry, void *user)
1960 isl_map *map = *entry;
1961 isl_union_set **res = user;
1963 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1964 map->dim, isl_dim_out))
1965 return isl_stat_ok;
1967 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1969 return isl_stat_ok;
1972 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1974 return cond_un_op(umap, &deltas_entry);
1977 static isl_stat deltas_map_entry(void **entry, void *user)
1979 isl_map *map = *entry;
1980 isl_union_map **res = user;
1982 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1983 map->dim, isl_dim_out))
1984 return isl_stat_ok;
1986 *res = isl_union_map_add_map(*res,
1987 isl_map_deltas_map(isl_map_copy(map)));
1989 return isl_stat_ok;
1992 __isl_give isl_union_map *isl_union_map_deltas_map(
1993 __isl_take isl_union_map *umap)
1995 return cond_un_op(umap, &deltas_map_entry);
1998 static isl_stat identity_entry(void **entry, void *user)
2000 isl_set *set = *entry;
2001 isl_union_map **res = user;
2003 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
2005 return isl_stat_ok;
2008 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2010 return cond_un_op(uset, &identity_entry);
2013 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2015 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2017 isl_union_pw_multi_aff **res = user;
2018 isl_space *space;
2019 isl_pw_multi_aff *pma;
2021 space = isl_space_map_from_set(isl_set_get_space(set));
2022 pma = isl_pw_multi_aff_identity(space);
2023 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2024 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2026 return *res ? isl_stat_ok : isl_stat_error;
2029 /* Return an identity function on "uset" in the form
2030 * of an isl_union_pw_multi_aff.
2032 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2033 __isl_take isl_union_set *uset)
2035 isl_union_pw_multi_aff *res;
2037 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2038 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2039 res = isl_union_pw_multi_aff_free(res);
2041 isl_union_set_free(uset);
2042 return res;
2045 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
2047 static isl_stat domain_factor_domain_entry(void **entry, void *user)
2049 isl_map *map = *entry;
2050 isl_union_map **res = user;
2052 if (!isl_map_domain_is_wrapping(map))
2053 return isl_stat_ok;
2055 *res = isl_union_map_add_map(*res,
2056 isl_map_domain_factor_domain(isl_map_copy(map)));
2058 return *res ? isl_stat_ok : isl_stat_error;
2061 /* For each map in "umap" of the form [A -> B] -> C,
2062 * construct the map A -> C and collect the results.
2064 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2065 __isl_take isl_union_map *umap)
2067 return cond_un_op(umap, &domain_factor_domain_entry);
2070 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2072 static isl_stat domain_factor_range_entry(void **entry, void *user)
2074 isl_map *map = *entry;
2075 isl_union_map **res = user;
2077 if (!isl_map_domain_is_wrapping(map))
2078 return isl_stat_ok;
2080 *res = isl_union_map_add_map(*res,
2081 isl_map_domain_factor_range(isl_map_copy(map)));
2083 return *res ? isl_stat_ok : isl_stat_error;
2086 /* For each map in "umap" of the form [A -> B] -> C,
2087 * construct the map B -> C and collect the results.
2089 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2090 __isl_take isl_union_map *umap)
2092 return cond_un_op(umap, &domain_factor_range_entry);
2095 /* If "map" is of the form A -> [B -> C], then add A -> B to "res".
2097 static isl_stat range_factor_domain_entry(void **entry, void *user)
2099 isl_map *map = *entry;
2100 isl_union_map **res = user;
2102 if (!isl_map_range_is_wrapping(map))
2103 return isl_stat_ok;
2105 *res = isl_union_map_add_map(*res,
2106 isl_map_range_factor_domain(isl_map_copy(map)));
2108 return *res ? isl_stat_ok : isl_stat_error;
2111 /* For each map in "umap" of the form A -> [B -> C],
2112 * construct the map A -> B and collect the results.
2114 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2115 __isl_take isl_union_map *umap)
2117 return cond_un_op(umap, &range_factor_domain_entry);
2120 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2122 static isl_stat range_factor_range_entry(void **entry, void *user)
2124 isl_map *map = *entry;
2125 isl_union_map **res = user;
2127 if (!isl_map_range_is_wrapping(map))
2128 return isl_stat_ok;
2130 *res = isl_union_map_add_map(*res,
2131 isl_map_range_factor_range(isl_map_copy(map)));
2133 return *res ? isl_stat_ok : isl_stat_error;
2136 /* For each map in "umap" of the form A -> [B -> C],
2137 * construct the map A -> C and collect the results.
2139 __isl_give isl_union_map *isl_union_map_range_factor_range(
2140 __isl_take isl_union_map *umap)
2142 return cond_un_op(umap, &range_factor_range_entry);
2145 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2147 static isl_stat factor_domain_entry(void **entry, void *user)
2149 isl_map *map = *entry;
2150 isl_union_map **res = user;
2152 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2153 return isl_stat_ok;
2155 *res = isl_union_map_add_map(*res,
2156 isl_map_factor_domain(isl_map_copy(map)));
2158 return *res ? isl_stat_ok : isl_stat_error;
2161 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2162 * construct the map A -> C and collect the results.
2164 __isl_give isl_union_map *isl_union_map_factor_domain(
2165 __isl_take isl_union_map *umap)
2167 return cond_un_op(umap, &factor_domain_entry);
2170 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2172 static isl_stat factor_range_entry(void **entry, void *user)
2174 isl_map *map = *entry;
2175 isl_union_map **res = user;
2177 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2178 return isl_stat_ok;
2180 *res = isl_union_map_add_map(*res,
2181 isl_map_factor_range(isl_map_copy(map)));
2183 return *res ? isl_stat_ok : isl_stat_error;
2186 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2187 * construct the map B -> D and collect the results.
2189 __isl_give isl_union_map *isl_union_map_factor_range(
2190 __isl_take isl_union_map *umap)
2192 return cond_un_op(umap, &factor_range_entry);
2195 static isl_stat unwrap_entry(void **entry, void *user)
2197 isl_set *set = *entry;
2198 isl_union_set **res = user;
2200 if (!isl_set_is_wrapping(set))
2201 return isl_stat_ok;
2203 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
2205 return isl_stat_ok;
2208 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2210 return cond_un_op(uset, &unwrap_entry);
2213 static isl_stat wrap_entry(void **entry, void *user)
2215 isl_map *map = *entry;
2216 isl_union_set **res = user;
2218 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2220 return isl_stat_ok;
2223 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2225 return cond_un_op(umap, &wrap_entry);
2228 struct isl_union_map_is_subset_data {
2229 isl_union_map *umap2;
2230 isl_bool is_subset;
2233 static isl_stat is_subset_entry(void **entry, void *user)
2235 struct isl_union_map_is_subset_data *data = user;
2236 uint32_t hash;
2237 struct isl_hash_table_entry *entry2;
2238 isl_map *map = *entry;
2240 hash = isl_space_get_hash(map->dim);
2241 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2242 hash, &has_dim, map->dim, 0);
2243 if (!entry2) {
2244 int empty = isl_map_is_empty(map);
2245 if (empty < 0)
2246 return isl_stat_error;
2247 if (empty)
2248 return isl_stat_ok;
2249 data->is_subset = 0;
2250 return isl_stat_error;
2253 data->is_subset = isl_map_is_subset(map, entry2->data);
2254 if (data->is_subset < 0 || !data->is_subset)
2255 return isl_stat_error;
2257 return isl_stat_ok;
2260 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2261 __isl_keep isl_union_map *umap2)
2263 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2265 umap1 = isl_union_map_copy(umap1);
2266 umap2 = isl_union_map_copy(umap2);
2267 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2268 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2270 if (!umap1 || !umap2)
2271 goto error;
2273 data.umap2 = umap2;
2274 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2275 &is_subset_entry, &data) < 0 &&
2276 data.is_subset)
2277 goto error;
2279 isl_union_map_free(umap1);
2280 isl_union_map_free(umap2);
2282 return data.is_subset;
2283 error:
2284 isl_union_map_free(umap1);
2285 isl_union_map_free(umap2);
2286 return isl_bool_error;
2289 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2290 __isl_keep isl_union_set *uset2)
2292 return isl_union_map_is_subset(uset1, uset2);
2295 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2296 __isl_keep isl_union_map *umap2)
2298 isl_bool is_subset;
2300 if (!umap1 || !umap2)
2301 return isl_bool_error;
2302 is_subset = isl_union_map_is_subset(umap1, umap2);
2303 if (is_subset != isl_bool_true)
2304 return is_subset;
2305 is_subset = isl_union_map_is_subset(umap2, umap1);
2306 return is_subset;
2309 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2310 __isl_keep isl_union_set *uset2)
2312 return isl_union_map_is_equal(uset1, uset2);
2315 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2316 __isl_keep isl_union_map *umap2)
2318 isl_bool is_subset;
2320 if (!umap1 || !umap2)
2321 return isl_bool_error;
2322 is_subset = isl_union_map_is_subset(umap1, umap2);
2323 if (is_subset != isl_bool_true)
2324 return is_subset;
2325 is_subset = isl_union_map_is_subset(umap2, umap1);
2326 if (is_subset == isl_bool_error)
2327 return is_subset;
2328 return !is_subset;
2331 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2332 __isl_keep isl_union_set *uset2)
2334 return isl_union_map_is_strict_subset(uset1, uset2);
2337 /* Internal data structure for isl_union_map_is_disjoint.
2338 * umap2 is the union map with which we are comparing.
2339 * is_disjoint is initialized to 1 and is set to 0 as soon
2340 * as the union maps turn out not to be disjoint.
2342 struct isl_union_map_is_disjoint_data {
2343 isl_union_map *umap2;
2344 isl_bool is_disjoint;
2347 /* Check if "map" is disjoint from data->umap2 and abort
2348 * the search if it is not.
2350 static isl_stat is_disjoint_entry(void **entry, void *user)
2352 struct isl_union_map_is_disjoint_data *data = user;
2353 uint32_t hash;
2354 struct isl_hash_table_entry *entry2;
2355 isl_map *map = *entry;
2357 hash = isl_space_get_hash(map->dim);
2358 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2359 hash, &has_dim, map->dim, 0);
2360 if (!entry2)
2361 return isl_stat_ok;
2363 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2364 if (data->is_disjoint < 0 || !data->is_disjoint)
2365 return isl_stat_error;
2367 return isl_stat_ok;
2370 /* Are "umap1" and "umap2" disjoint?
2372 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2373 __isl_keep isl_union_map *umap2)
2375 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2377 umap1 = isl_union_map_copy(umap1);
2378 umap2 = isl_union_map_copy(umap2);
2379 umap1 = isl_union_map_align_params(umap1,
2380 isl_union_map_get_space(umap2));
2381 umap2 = isl_union_map_align_params(umap2,
2382 isl_union_map_get_space(umap1));
2384 if (!umap1 || !umap2)
2385 goto error;
2387 data.umap2 = umap2;
2388 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2389 &is_disjoint_entry, &data) < 0 &&
2390 data.is_disjoint)
2391 goto error;
2393 isl_union_map_free(umap1);
2394 isl_union_map_free(umap2);
2396 return data.is_disjoint;
2397 error:
2398 isl_union_map_free(umap1);
2399 isl_union_map_free(umap2);
2400 return isl_bool_error;
2403 /* Are "uset1" and "uset2" disjoint?
2405 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2406 __isl_keep isl_union_set *uset2)
2408 return isl_union_map_is_disjoint(uset1, uset2);
2411 static isl_stat sample_entry(void **entry, void *user)
2413 isl_basic_map **sample = (isl_basic_map **)user;
2414 isl_map *map = *entry;
2416 *sample = isl_map_sample(isl_map_copy(map));
2417 if (!*sample)
2418 return isl_stat_error;
2419 if (!isl_basic_map_plain_is_empty(*sample))
2420 return isl_stat_error;
2421 return isl_stat_ok;
2424 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2426 isl_basic_map *sample = NULL;
2428 if (!umap)
2429 return NULL;
2431 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2432 &sample_entry, &sample) < 0 &&
2433 !sample)
2434 goto error;
2436 if (!sample)
2437 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2439 isl_union_map_free(umap);
2441 return sample;
2442 error:
2443 isl_union_map_free(umap);
2444 return NULL;
2447 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2449 return bset_from_bmap(isl_union_map_sample(uset));
2452 /* Return an element in "uset" in the form of an isl_point.
2453 * Return a void isl_point if "uset" is empty.
2455 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2457 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2460 struct isl_forall_data {
2461 isl_bool res;
2462 isl_bool (*fn)(__isl_keep isl_map *map);
2465 static isl_stat forall_entry(void **entry, void *user)
2467 struct isl_forall_data *data = user;
2468 isl_map *map = *entry;
2470 data->res = data->fn(map);
2471 if (data->res < 0)
2472 return isl_stat_error;
2474 if (!data->res)
2475 return isl_stat_error;
2477 return isl_stat_ok;
2480 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2481 isl_bool (*fn)(__isl_keep isl_map *map))
2483 struct isl_forall_data data = { isl_bool_true, fn };
2485 if (!umap)
2486 return isl_bool_error;
2488 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2489 &forall_entry, &data) < 0 && data.res)
2490 return isl_bool_error;
2492 return data.res;
2495 struct isl_forall_user_data {
2496 isl_bool res;
2497 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2498 void *user;
2501 static isl_stat forall_user_entry(void **entry, void *user)
2503 struct isl_forall_user_data *data = user;
2504 isl_map *map = *entry;
2506 data->res = data->fn(map, data->user);
2507 if (data->res < 0)
2508 return isl_stat_error;
2510 if (!data->res)
2511 return isl_stat_error;
2513 return isl_stat_ok;
2516 /* Check if fn(map, user) returns true for all maps "map" in umap.
2518 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2519 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2521 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2523 if (!umap)
2524 return isl_bool_error;
2526 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2527 &forall_user_entry, &data) < 0 && data.res)
2528 return isl_bool_error;
2530 return data.res;
2533 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2535 return union_map_forall(umap, &isl_map_is_empty);
2538 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2540 return isl_union_map_is_empty(uset);
2543 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2545 isl_bool is_subset;
2546 isl_space *dim;
2547 isl_map *id;
2549 if (!map)
2550 return isl_bool_error;
2552 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2553 map->dim, isl_dim_out))
2554 return isl_bool_false;
2556 dim = isl_map_get_space(map);
2557 id = isl_map_identity(dim);
2559 is_subset = isl_map_is_subset(map, id);
2561 isl_map_free(id);
2563 return is_subset;
2566 /* Given an isl_union_map that consists of a single map, check
2567 * if it is single-valued.
2569 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2571 isl_map *map;
2572 isl_bool sv;
2574 umap = isl_union_map_copy(umap);
2575 map = isl_map_from_union_map(umap);
2576 sv = isl_map_is_single_valued(map);
2577 isl_map_free(map);
2579 return sv;
2582 /* Internal data structure for single_valued_on_domain.
2584 * "umap" is the union map to be tested.
2585 * "sv" is set to 1 as long as "umap" may still be single-valued.
2587 struct isl_union_map_is_sv_data {
2588 isl_union_map *umap;
2589 isl_bool sv;
2592 /* Check if the data->umap is single-valued on "set".
2594 * If data->umap consists of a single map on "set", then test it
2595 * as an isl_map.
2597 * Otherwise, compute
2599 * M \circ M^-1
2601 * check if the result is a subset of the identity mapping and
2602 * store the result in data->sv.
2604 * Terminate as soon as data->umap has been determined not to
2605 * be single-valued.
2607 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2609 struct isl_union_map_is_sv_data *data = user;
2610 isl_union_map *umap, *test;
2612 umap = isl_union_map_copy(data->umap);
2613 umap = isl_union_map_intersect_domain(umap,
2614 isl_union_set_from_set(set));
2616 if (isl_union_map_n_map(umap) == 1) {
2617 data->sv = single_map_is_single_valued(umap);
2618 isl_union_map_free(umap);
2619 } else {
2620 test = isl_union_map_reverse(isl_union_map_copy(umap));
2621 test = isl_union_map_apply_range(test, umap);
2623 data->sv = union_map_forall(test, &is_subset_of_identity);
2625 isl_union_map_free(test);
2628 if (data->sv < 0 || !data->sv)
2629 return isl_stat_error;
2630 return isl_stat_ok;
2633 /* Check if the given map is single-valued.
2635 * If the union map consists of a single map, then test it as an isl_map.
2636 * Otherwise, check if the union map is single-valued on each of its
2637 * domain spaces.
2639 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2641 isl_union_map *universe;
2642 isl_union_set *domain;
2643 struct isl_union_map_is_sv_data data;
2645 if (isl_union_map_n_map(umap) == 1)
2646 return single_map_is_single_valued(umap);
2648 universe = isl_union_map_universe(isl_union_map_copy(umap));
2649 domain = isl_union_map_domain(universe);
2651 data.sv = isl_bool_true;
2652 data.umap = umap;
2653 if (isl_union_set_foreach_set(domain,
2654 &single_valued_on_domain, &data) < 0 && data.sv)
2655 data.sv = isl_bool_error;
2656 isl_union_set_free(domain);
2658 return data.sv;
2661 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2663 isl_bool in;
2665 umap = isl_union_map_copy(umap);
2666 umap = isl_union_map_reverse(umap);
2667 in = isl_union_map_is_single_valued(umap);
2668 isl_union_map_free(umap);
2670 return in;
2673 /* Is "map" obviously not an identity relation because
2674 * it maps elements from one space to another space?
2675 * Update *non_identity accordingly.
2677 * In particular, if the domain and range spaces are the same,
2678 * then the map is not considered to obviously not be an identity relation.
2679 * Otherwise, the map is considered to obviously not be an identity relation
2680 * if it is is non-empty.
2682 * If "map" is determined to obviously not be an identity relation,
2683 * then the search is aborted.
2685 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2687 isl_bool *non_identity = user;
2688 isl_bool equal;
2689 isl_space *space;
2691 space = isl_map_get_space(map);
2692 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2693 if (equal >= 0 && !equal)
2694 *non_identity = isl_bool_not(isl_map_is_empty(map));
2695 else
2696 *non_identity = isl_bool_not(equal);
2697 isl_space_free(space);
2698 isl_map_free(map);
2700 if (*non_identity < 0 || *non_identity)
2701 return isl_stat_error;
2703 return isl_stat_ok;
2706 /* Is "umap" obviously not an identity relation because
2707 * it maps elements from one space to another space?
2709 * As soon as a map has been found that maps elements to a different space,
2710 * non_identity is changed and the search is aborted.
2712 static isl_bool isl_union_map_plain_is_not_identity(
2713 __isl_keep isl_union_map *umap)
2715 isl_bool non_identity;
2717 non_identity = isl_bool_false;
2718 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2719 &non_identity) < 0 &&
2720 non_identity == isl_bool_false)
2721 return isl_bool_error;
2723 return non_identity;
2726 /* Does "map" only map elements to themselves?
2727 * Update *identity accordingly.
2729 * If "map" is determined not to be an identity relation,
2730 * then the search is aborted.
2732 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2734 isl_bool *identity = user;
2736 *identity = isl_map_is_identity(map);
2737 isl_map_free(map);
2739 if (*identity < 0 || !*identity)
2740 return isl_stat_error;
2742 return isl_stat_ok;
2745 /* Does "umap" only map elements to themselves?
2747 * First check if there are any maps that map elements to different spaces.
2748 * If not, then check that all the maps (between identical spaces)
2749 * are identity relations.
2751 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2753 isl_bool non_identity;
2754 isl_bool identity;
2756 non_identity = isl_union_map_plain_is_not_identity(umap);
2757 if (non_identity < 0 || non_identity)
2758 return isl_bool_not(non_identity);
2760 identity = isl_bool_true;
2761 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2762 identity == isl_bool_true)
2763 return isl_bool_error;
2765 return identity;
2768 /* Represents a map that has a fixed value (v) for one of its
2769 * range dimensions.
2770 * The map in this structure is not reference counted, so it
2771 * is only valid while the isl_union_map from which it was
2772 * obtained is still alive.
2774 struct isl_fixed_map {
2775 isl_int v;
2776 isl_map *map;
2779 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2780 int n)
2782 int i;
2783 struct isl_fixed_map *v;
2785 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2786 if (!v)
2787 return NULL;
2788 for (i = 0; i < n; ++i)
2789 isl_int_init(v[i].v);
2790 return v;
2793 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2795 int i;
2797 if (!v)
2798 return;
2799 for (i = 0; i < n; ++i)
2800 isl_int_clear(v[i].v);
2801 free(v);
2804 /* Compare the "v" field of two isl_fixed_map structs.
2806 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2808 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2809 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2811 return isl_int_cmp(e1->v, e2->v);
2814 /* Internal data structure used while checking whether all maps
2815 * in a union_map have a fixed value for a given output dimension.
2816 * v is the list of maps, with the fixed value for the dimension
2817 * n is the number of maps considered so far
2818 * pos is the output dimension under investigation
2820 struct isl_fixed_dim_data {
2821 struct isl_fixed_map *v;
2822 int n;
2823 int pos;
2826 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2828 struct isl_fixed_dim_data *data = user;
2830 data->v[data->n].map = map;
2831 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2832 &data->v[data->n++].v);
2835 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2836 int first, int n_range);
2838 /* Given a list of the maps, with their fixed values at output dimension "pos",
2839 * check whether the ranges of the maps form an obvious partition.
2841 * We first sort the maps according to their fixed values.
2842 * If all maps have a different value, then we know the ranges form
2843 * a partition.
2844 * Otherwise, we collect the maps with the same fixed value and
2845 * check whether each such collection is obviously injective
2846 * based on later dimensions.
2848 static int separates(struct isl_fixed_map *v, int n,
2849 __isl_take isl_space *dim, int pos, int n_range)
2851 int i;
2853 if (!v)
2854 goto error;
2856 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2858 for (i = 0; i + 1 < n; ++i) {
2859 int j, k;
2860 isl_union_map *part;
2861 int injective;
2863 for (j = i + 1; j < n; ++j)
2864 if (isl_int_ne(v[i].v, v[j].v))
2865 break;
2867 if (j == i + 1)
2868 continue;
2870 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2871 for (k = i; k < j; ++k)
2872 part = isl_union_map_add_map(part,
2873 isl_map_copy(v[k].map));
2875 injective = plain_injective_on_range(part, pos + 1, n_range);
2876 if (injective < 0)
2877 goto error;
2878 if (!injective)
2879 break;
2881 i = j - 1;
2884 isl_space_free(dim);
2885 free_isl_fixed_map_array(v, n);
2886 return i + 1 >= n;
2887 error:
2888 isl_space_free(dim);
2889 free_isl_fixed_map_array(v, n);
2890 return -1;
2893 /* Check whether the maps in umap have obviously distinct ranges.
2894 * In particular, check for an output dimension in the range
2895 * [first,n_range) for which all maps have a fixed value
2896 * and then check if these values, possibly along with fixed values
2897 * at later dimensions, entail distinct ranges.
2899 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2900 int first, int n_range)
2902 isl_ctx *ctx;
2903 int n;
2904 struct isl_fixed_dim_data data = { NULL };
2906 ctx = isl_union_map_get_ctx(umap);
2908 n = isl_union_map_n_map(umap);
2909 if (!umap)
2910 goto error;
2912 if (n <= 1) {
2913 isl_union_map_free(umap);
2914 return isl_bool_true;
2917 if (first >= n_range) {
2918 isl_union_map_free(umap);
2919 return isl_bool_false;
2922 data.v = alloc_isl_fixed_map_array(ctx, n);
2923 if (!data.v)
2924 goto error;
2926 for (data.pos = first; data.pos < n_range; ++data.pos) {
2927 isl_bool fixed;
2928 int injective;
2929 isl_space *dim;
2931 data.n = 0;
2932 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2933 if (fixed < 0)
2934 goto error;
2935 if (!fixed)
2936 continue;
2937 dim = isl_union_map_get_space(umap);
2938 injective = separates(data.v, n, dim, data.pos, n_range);
2939 isl_union_map_free(umap);
2940 return injective;
2943 free_isl_fixed_map_array(data.v, n);
2944 isl_union_map_free(umap);
2946 return isl_bool_false;
2947 error:
2948 free_isl_fixed_map_array(data.v, n);
2949 isl_union_map_free(umap);
2950 return isl_bool_error;
2953 /* Check whether the maps in umap that map to subsets of "ran"
2954 * have obviously distinct ranges.
2956 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2957 void *user)
2959 isl_union_map *umap = user;
2961 umap = isl_union_map_copy(umap);
2962 umap = isl_union_map_intersect_range(umap,
2963 isl_union_set_from_set(isl_set_copy(ran)));
2964 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2967 /* Check if the given union_map is obviously injective.
2969 * In particular, we first check if all individual maps are obviously
2970 * injective and then check if all the ranges of these maps are
2971 * obviously disjoint.
2973 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2975 isl_bool in;
2976 isl_union_map *univ;
2977 isl_union_set *ran;
2979 in = union_map_forall(umap, &isl_map_plain_is_injective);
2980 if (in < 0)
2981 return isl_bool_error;
2982 if (!in)
2983 return isl_bool_false;
2985 univ = isl_union_map_universe(isl_union_map_copy(umap));
2986 ran = isl_union_map_range(univ);
2988 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2990 isl_union_set_free(ran);
2992 return in;
2995 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2997 isl_bool sv;
2999 sv = isl_union_map_is_single_valued(umap);
3000 if (sv < 0 || !sv)
3001 return sv;
3003 return isl_union_map_is_injective(umap);
3006 static isl_stat zip_entry(void **entry, void *user)
3008 isl_map *map = *entry;
3009 isl_union_map **res = user;
3011 if (!isl_map_can_zip(map))
3012 return isl_stat_ok;
3014 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
3016 return isl_stat_ok;
3019 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3021 return cond_un_op(umap, &zip_entry);
3024 static isl_stat uncurry_entry(void **entry, void *user)
3026 isl_map *map = *entry;
3027 isl_union_map **res = user;
3029 if (!isl_map_can_uncurry(map))
3030 return isl_stat_ok;
3032 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
3034 return isl_stat_ok;
3037 /* Given a union map, take the maps of the form A -> (B -> C) and
3038 * return the union of the corresponding maps (A -> B) -> C.
3040 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3042 return cond_un_op(umap, &uncurry_entry);
3045 static isl_stat curry_entry(void **entry, void *user)
3047 isl_map *map = *entry;
3048 isl_union_map **res = user;
3050 if (!isl_map_can_curry(map))
3051 return isl_stat_ok;
3053 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
3055 return isl_stat_ok;
3058 /* Given a union map, take the maps of the form (A -> B) -> C and
3059 * return the union of the corresponding maps A -> (B -> C).
3061 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3063 return cond_un_op(umap, &curry_entry);
3066 /* If *entry is of the form A -> ((B -> C) -> D), then apply
3067 * isl_map_range_curry to it and add the result to *res.
3069 static isl_stat range_curry_entry(void **entry, void *user)
3071 isl_map *map = *entry;
3072 isl_union_map **res = user;
3074 if (!isl_map_can_range_curry(map))
3075 return isl_stat_ok;
3077 map = isl_map_range_curry(isl_map_copy(map));
3078 *res = isl_union_map_add_map(*res, map);
3080 return isl_stat_ok;
3083 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3084 * return the union of the corresponding maps A -> (B -> (C -> D)).
3086 __isl_give isl_union_map *isl_union_map_range_curry(
3087 __isl_take isl_union_map *umap)
3089 return cond_un_op(umap, &range_curry_entry);
3092 static isl_stat lift_entry(void **entry, void *user)
3094 isl_set *set = *entry;
3095 isl_union_set **res = user;
3097 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
3099 return isl_stat_ok;
3102 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3104 return cond_un_op(uset, &lift_entry);
3107 static isl_stat coefficients_entry(void **entry, void *user)
3109 isl_set *set = *entry;
3110 isl_union_set **res = user;
3112 set = isl_set_copy(set);
3113 set = isl_set_from_basic_set(isl_set_coefficients(set));
3114 *res = isl_union_set_add_set(*res, set);
3116 return isl_stat_ok;
3119 __isl_give isl_union_set *isl_union_set_coefficients(
3120 __isl_take isl_union_set *uset)
3122 isl_ctx *ctx;
3123 isl_space *dim;
3124 isl_union_set *res;
3126 if (!uset)
3127 return NULL;
3129 ctx = isl_union_set_get_ctx(uset);
3130 dim = isl_space_set_alloc(ctx, 0, 0);
3131 res = isl_union_map_alloc(dim, uset->table.n);
3132 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3133 &coefficients_entry, &res) < 0)
3134 goto error;
3136 isl_union_set_free(uset);
3137 return res;
3138 error:
3139 isl_union_set_free(uset);
3140 isl_union_set_free(res);
3141 return NULL;
3144 static isl_stat solutions_entry(void **entry, void *user)
3146 isl_set *set = *entry;
3147 isl_union_set **res = user;
3149 set = isl_set_copy(set);
3150 set = isl_set_from_basic_set(isl_set_solutions(set));
3151 if (!*res)
3152 *res = isl_union_set_from_set(set);
3153 else
3154 *res = isl_union_set_add_set(*res, set);
3156 if (!*res)
3157 return isl_stat_error;
3159 return isl_stat_ok;
3162 __isl_give isl_union_set *isl_union_set_solutions(
3163 __isl_take isl_union_set *uset)
3165 isl_union_set *res = NULL;
3167 if (!uset)
3168 return NULL;
3170 if (uset->table.n == 0) {
3171 res = isl_union_set_empty(isl_union_set_get_space(uset));
3172 isl_union_set_free(uset);
3173 return res;
3176 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3177 &solutions_entry, &res) < 0)
3178 goto error;
3180 isl_union_set_free(uset);
3181 return res;
3182 error:
3183 isl_union_set_free(uset);
3184 isl_union_set_free(res);
3185 return NULL;
3188 /* Is the domain space of "map" equal to "space"?
3190 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3192 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3193 space, isl_dim_out);
3196 /* Is the range space of "map" equal to "space"?
3198 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3200 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3201 space, isl_dim_out);
3204 /* Is the set space of "map" equal to "space"?
3206 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3208 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3209 space, isl_dim_out);
3212 /* Internal data structure for preimage_pw_multi_aff.
3214 * "pma" is the function under which the preimage should be taken.
3215 * "space" is the space of "pma".
3216 * "res" collects the results.
3217 * "fn" computes the preimage for a given map.
3218 * "match" returns true if "fn" can be called.
3220 struct isl_union_map_preimage_data {
3221 isl_space *space;
3222 isl_pw_multi_aff *pma;
3223 isl_union_map *res;
3224 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3225 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3226 __isl_take isl_pw_multi_aff *pma);
3229 /* Call data->fn to compute the preimage of the domain or range of *entry
3230 * under the function represented by data->pma, provided the domain/range
3231 * space of *entry matches the target space of data->pma
3232 * (as given by data->match), and add the result to data->res.
3234 static isl_stat preimage_entry(void **entry, void *user)
3236 int m;
3237 isl_map *map = *entry;
3238 struct isl_union_map_preimage_data *data = user;
3239 isl_bool empty;
3241 m = data->match(map, data->space);
3242 if (m < 0)
3243 return isl_stat_error;
3244 if (!m)
3245 return isl_stat_ok;
3247 map = isl_map_copy(map);
3248 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3250 empty = isl_map_is_empty(map);
3251 if (empty < 0 || empty) {
3252 isl_map_free(map);
3253 return empty < 0 ? isl_stat_error : isl_stat_ok;
3256 data->res = isl_union_map_add_map(data->res, map);
3258 return isl_stat_ok;
3261 /* Compute the preimage of the domain or range of "umap" under the function
3262 * represented by "pma".
3263 * In other words, plug in "pma" in the domain or range of "umap".
3264 * The function "fn" performs the actual preimage computation on a map,
3265 * while "match" determines to which maps the function should be applied.
3267 static __isl_give isl_union_map *preimage_pw_multi_aff(
3268 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3269 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3270 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3271 __isl_take isl_pw_multi_aff *pma))
3273 isl_ctx *ctx;
3274 isl_space *space;
3275 struct isl_union_map_preimage_data data;
3277 umap = isl_union_map_align_params(umap,
3278 isl_pw_multi_aff_get_space(pma));
3279 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3281 if (!umap || !pma)
3282 goto error;
3284 ctx = isl_union_map_get_ctx(umap);
3285 space = isl_union_map_get_space(umap);
3286 data.space = isl_pw_multi_aff_get_space(pma);
3287 data.pma = pma;
3288 data.res = isl_union_map_alloc(space, umap->table.n);
3289 data.match = match;
3290 data.fn = fn;
3291 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3292 &data) < 0)
3293 data.res = isl_union_map_free(data.res);
3295 isl_space_free(data.space);
3296 isl_union_map_free(umap);
3297 isl_pw_multi_aff_free(pma);
3298 return data.res;
3299 error:
3300 isl_union_map_free(umap);
3301 isl_pw_multi_aff_free(pma);
3302 return NULL;
3305 /* Compute the preimage of the domain of "umap" under the function
3306 * represented by "pma".
3307 * In other words, plug in "pma" in the domain of "umap".
3308 * The result contains maps that live in the same spaces as the maps of "umap"
3309 * with domain space equal to the target space of "pma",
3310 * except that the domain has been replaced by the domain space of "pma".
3312 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3313 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3315 return preimage_pw_multi_aff(umap, pma, &domain_match,
3316 &isl_map_preimage_domain_pw_multi_aff);
3319 /* Compute the preimage of the range of "umap" under the function
3320 * represented by "pma".
3321 * In other words, plug in "pma" in the range of "umap".
3322 * The result contains maps that live in the same spaces as the maps of "umap"
3323 * with range space equal to the target space of "pma",
3324 * except that the range has been replaced by the domain space of "pma".
3326 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3327 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3329 return preimage_pw_multi_aff(umap, pma, &range_match,
3330 &isl_map_preimage_range_pw_multi_aff);
3333 /* Compute the preimage of "uset" under the function represented by "pma".
3334 * In other words, plug in "pma" in "uset".
3335 * The result contains sets that live in the same spaces as the sets of "uset"
3336 * with space equal to the target space of "pma",
3337 * except that the space has been replaced by the domain space of "pma".
3339 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3340 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3342 return preimage_pw_multi_aff(uset, pma, &set_match,
3343 &isl_set_preimage_pw_multi_aff);
3346 /* Compute the preimage of the domain of "umap" under the function
3347 * represented by "ma".
3348 * In other words, plug in "ma" in the domain of "umap".
3349 * The result contains maps that live in the same spaces as the maps of "umap"
3350 * with domain space equal to the target space of "ma",
3351 * except that the domain has been replaced by the domain space of "ma".
3353 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3354 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3356 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3357 isl_pw_multi_aff_from_multi_aff(ma));
3360 /* Compute the preimage of the range of "umap" under the function
3361 * represented by "ma".
3362 * In other words, plug in "ma" in the range of "umap".
3363 * The result contains maps that live in the same spaces as the maps of "umap"
3364 * with range space equal to the target space of "ma",
3365 * except that the range has been replaced by the domain space of "ma".
3367 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3368 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3370 return isl_union_map_preimage_range_pw_multi_aff(umap,
3371 isl_pw_multi_aff_from_multi_aff(ma));
3374 /* Compute the preimage of "uset" under the function represented by "ma".
3375 * In other words, plug in "ma" in "uset".
3376 * The result contains sets that live in the same spaces as the sets of "uset"
3377 * with space equal to the target space of "ma",
3378 * except that the space has been replaced by the domain space of "ma".
3380 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3381 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3383 return isl_union_set_preimage_pw_multi_aff(uset,
3384 isl_pw_multi_aff_from_multi_aff(ma));
3387 /* Internal data structure for preimage_multi_pw_aff.
3389 * "mpa" is the function under which the preimage should be taken.
3390 * "space" is the space of "mpa".
3391 * "res" collects the results.
3392 * "fn" computes the preimage for a given map.
3393 * "match" returns true if "fn" can be called.
3395 struct isl_union_map_preimage_mpa_data {
3396 isl_space *space;
3397 isl_multi_pw_aff *mpa;
3398 isl_union_map *res;
3399 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3400 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3401 __isl_take isl_multi_pw_aff *mpa);
3404 /* Call data->fn to compute the preimage of the domain or range of *entry
3405 * under the function represented by data->mpa, provided the domain/range
3406 * space of *entry matches the target space of data->mpa
3407 * (as given by data->match), and add the result to data->res.
3409 static isl_stat preimage_mpa_entry(void **entry, void *user)
3411 int m;
3412 isl_map *map = *entry;
3413 struct isl_union_map_preimage_mpa_data *data = user;
3414 isl_bool empty;
3416 m = data->match(map, data->space);
3417 if (m < 0)
3418 return isl_stat_error;
3419 if (!m)
3420 return isl_stat_ok;
3422 map = isl_map_copy(map);
3423 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3425 empty = isl_map_is_empty(map);
3426 if (empty < 0 || empty) {
3427 isl_map_free(map);
3428 return empty < 0 ? isl_stat_error : isl_stat_ok;
3431 data->res = isl_union_map_add_map(data->res, map);
3433 return isl_stat_ok;
3436 /* Compute the preimage of the domain or range of "umap" under the function
3437 * represented by "mpa".
3438 * In other words, plug in "mpa" in the domain or range of "umap".
3439 * The function "fn" performs the actual preimage computation on a map,
3440 * while "match" determines to which maps the function should be applied.
3442 static __isl_give isl_union_map *preimage_multi_pw_aff(
3443 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3444 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3445 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3446 __isl_take isl_multi_pw_aff *mpa))
3448 isl_ctx *ctx;
3449 isl_space *space;
3450 struct isl_union_map_preimage_mpa_data data;
3452 umap = isl_union_map_align_params(umap,
3453 isl_multi_pw_aff_get_space(mpa));
3454 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3456 if (!umap || !mpa)
3457 goto error;
3459 ctx = isl_union_map_get_ctx(umap);
3460 space = isl_union_map_get_space(umap);
3461 data.space = isl_multi_pw_aff_get_space(mpa);
3462 data.mpa = mpa;
3463 data.res = isl_union_map_alloc(space, umap->table.n);
3464 data.match = match;
3465 data.fn = fn;
3466 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3467 &data) < 0)
3468 data.res = isl_union_map_free(data.res);
3470 isl_space_free(data.space);
3471 isl_union_map_free(umap);
3472 isl_multi_pw_aff_free(mpa);
3473 return data.res;
3474 error:
3475 isl_union_map_free(umap);
3476 isl_multi_pw_aff_free(mpa);
3477 return NULL;
3480 /* Compute the preimage of the domain of "umap" under the function
3481 * represented by "mpa".
3482 * In other words, plug in "mpa" in the domain of "umap".
3483 * The result contains maps that live in the same spaces as the maps of "umap"
3484 * with domain space equal to the target space of "mpa",
3485 * except that the domain has been replaced by the domain space of "mpa".
3487 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3488 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3490 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3491 &isl_map_preimage_domain_multi_pw_aff);
3494 /* Internal data structure for preimage_upma.
3496 * "umap" is the map of which the preimage should be computed.
3497 * "res" collects the results.
3498 * "fn" computes the preimage for a given piecewise multi-affine function.
3500 struct isl_union_map_preimage_upma_data {
3501 isl_union_map *umap;
3502 isl_union_map *res;
3503 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3504 __isl_take isl_pw_multi_aff *pma);
3507 /* Call data->fn to compute the preimage of the domain or range of data->umap
3508 * under the function represented by pma and add the result to data->res.
3510 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3512 struct isl_union_map_preimage_upma_data *data = user;
3513 isl_union_map *umap;
3515 umap = isl_union_map_copy(data->umap);
3516 umap = data->fn(umap, pma);
3517 data->res = isl_union_map_union(data->res, umap);
3519 return data->res ? isl_stat_ok : isl_stat_error;
3522 /* Compute the preimage of the domain or range of "umap" under the function
3523 * represented by "upma".
3524 * In other words, plug in "upma" in the domain or range of "umap".
3525 * The function "fn" performs the actual preimage computation
3526 * on a piecewise multi-affine function.
3528 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3529 __isl_take isl_union_map *umap,
3530 __isl_take isl_union_pw_multi_aff *upma,
3531 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3532 __isl_take isl_pw_multi_aff *pma))
3534 struct isl_union_map_preimage_upma_data data;
3536 data.umap = umap;
3537 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3538 data.fn = fn;
3539 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3540 &preimage_upma, &data) < 0)
3541 data.res = isl_union_map_free(data.res);
3543 isl_union_map_free(umap);
3544 isl_union_pw_multi_aff_free(upma);
3546 return data.res;
3549 /* Compute the preimage of the domain of "umap" under the function
3550 * represented by "upma".
3551 * In other words, plug in "upma" in the domain of "umap".
3552 * The result contains maps that live in the same spaces as the maps of "umap"
3553 * with domain space equal to one of the target spaces of "upma",
3554 * except that the domain has been replaced by one of the the domain spaces that
3555 * corresponds to that target space of "upma".
3557 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3558 __isl_take isl_union_map *umap,
3559 __isl_take isl_union_pw_multi_aff *upma)
3561 return preimage_union_pw_multi_aff(umap, upma,
3562 &isl_union_map_preimage_domain_pw_multi_aff);
3565 /* Compute the preimage of the range of "umap" under the function
3566 * represented by "upma".
3567 * In other words, plug in "upma" in the range of "umap".
3568 * The result contains maps that live in the same spaces as the maps of "umap"
3569 * with range space equal to one of the target spaces of "upma",
3570 * except that the range has been replaced by one of the the domain spaces that
3571 * corresponds to that target space of "upma".
3573 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3574 __isl_take isl_union_map *umap,
3575 __isl_take isl_union_pw_multi_aff *upma)
3577 return preimage_union_pw_multi_aff(umap, upma,
3578 &isl_union_map_preimage_range_pw_multi_aff);
3581 /* Compute the preimage of "uset" under the function represented by "upma".
3582 * In other words, plug in "upma" in the range of "uset".
3583 * The result contains sets that live in the same spaces as the sets of "uset"
3584 * with space equal to one of the target spaces of "upma",
3585 * except that the space has been replaced by one of the the domain spaces that
3586 * corresponds to that target space of "upma".
3588 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3589 __isl_take isl_union_set *uset,
3590 __isl_take isl_union_pw_multi_aff *upma)
3592 return preimage_union_pw_multi_aff(uset, upma,
3593 &isl_union_set_preimage_pw_multi_aff);
3596 /* Reset the user pointer on all identifiers of parameters and tuples
3597 * of the space of *entry.
3599 static isl_stat reset_user(void **entry, void *user)
3601 isl_map **map = (isl_map **)entry;
3603 *map = isl_map_reset_user(*map);
3605 return *map ? isl_stat_ok : isl_stat_error;
3608 /* Reset the user pointer on all identifiers of parameters and tuples
3609 * of the spaces of "umap".
3611 __isl_give isl_union_map *isl_union_map_reset_user(
3612 __isl_take isl_union_map *umap)
3614 umap = isl_union_map_cow(umap);
3615 if (!umap)
3616 return NULL;
3617 umap->dim = isl_space_reset_user(umap->dim);
3618 if (!umap->dim)
3619 return isl_union_map_free(umap);
3620 umap = un_op(umap, &reset_user);
3622 return umap;
3625 /* Reset the user pointer on all identifiers of parameters and tuples
3626 * of the spaces of "uset".
3628 __isl_give isl_union_set *isl_union_set_reset_user(
3629 __isl_take isl_union_set *uset)
3631 return isl_union_map_reset_user(uset);
3634 /* Internal data structure for isl_union_map_project_out.
3635 * "type", "first" and "n" are the arguments for the isl_map_project_out
3636 * call.
3637 * "res" collects the results.
3639 struct isl_union_map_project_out_data {
3640 enum isl_dim_type type;
3641 unsigned first;
3642 unsigned n;
3644 isl_union_map *res;
3647 /* Turn the data->n dimensions of type data->type, starting at data->first
3648 * into existentially quantified variables and add the result to data->res.
3650 static isl_stat project_out(__isl_take isl_map *map, void *user)
3652 struct isl_union_map_project_out_data *data = user;
3654 map = isl_map_project_out(map, data->type, data->first, data->n);
3655 data->res = isl_union_map_add_map(data->res, map);
3657 return isl_stat_ok;
3660 /* Turn the "n" dimensions of type "type", starting at "first"
3661 * into existentially quantified variables.
3662 * Since the space of an isl_union_map only contains parameters,
3663 * type is required to be equal to isl_dim_param.
3665 __isl_give isl_union_map *isl_union_map_project_out(
3666 __isl_take isl_union_map *umap,
3667 enum isl_dim_type type, unsigned first, unsigned n)
3669 isl_space *space;
3670 struct isl_union_map_project_out_data data = { type, first, n };
3672 if (!umap)
3673 return NULL;
3675 if (type != isl_dim_param)
3676 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3677 "can only project out parameters",
3678 return isl_union_map_free(umap));
3680 space = isl_union_map_get_space(umap);
3681 space = isl_space_drop_dims(space, type, first, n);
3682 data.res = isl_union_map_empty(space);
3683 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3684 data.res = isl_union_map_free(data.res);
3686 isl_union_map_free(umap);
3688 return data.res;
3691 /* Turn the "n" dimensions of type "type", starting at "first"
3692 * into existentially quantified variables.
3693 * Since the space of an isl_union_set only contains parameters,
3694 * "type" is required to be equal to isl_dim_param.
3696 __isl_give isl_union_set *isl_union_set_project_out(
3697 __isl_take isl_union_set *uset,
3698 enum isl_dim_type type, unsigned first, unsigned n)
3700 return isl_union_map_project_out(uset, type, first, n);
3703 /* Internal data structure for isl_union_map_involves_dims.
3704 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3706 struct isl_union_map_involves_dims_data {
3707 unsigned first;
3708 unsigned n;
3711 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3713 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3715 struct isl_union_map_involves_dims_data *data = user;
3716 isl_bool involves;
3718 involves = isl_map_involves_dims(map,
3719 isl_dim_param, data->first, data->n);
3720 if (involves < 0)
3721 return isl_bool_error;
3722 return !involves;
3725 /* Does "umap" involve any of the n parameters starting at first?
3726 * "type" is required to be set to isl_dim_param.
3728 * "umap" involves any of those parameters if any of its maps
3729 * involve the parameters. In other words, "umap" does not
3730 * involve any of the parameters if all its maps to not
3731 * involve the parameters.
3733 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3734 enum isl_dim_type type, unsigned first, unsigned n)
3736 struct isl_union_map_involves_dims_data data = { first, n };
3737 isl_bool excludes;
3739 if (type != isl_dim_param)
3740 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3741 "can only reference parameters", return isl_bool_error);
3743 excludes = union_map_forall_user(umap, &map_excludes, &data);
3745 if (excludes < 0)
3746 return isl_bool_error;
3748 return !excludes;
3751 /* Internal data structure for isl_union_map_reset_range_space.
3752 * "range" is the space from which to set the range space.
3753 * "res" collects the results.
3755 struct isl_union_map_reset_range_space_data {
3756 isl_space *range;
3757 isl_union_map *res;
3760 /* Replace the range space of "map" by the range space of data->range and
3761 * add the result to data->res.
3763 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3765 struct isl_union_map_reset_range_space_data *data = user;
3766 isl_space *space;
3768 space = isl_map_get_space(map);
3769 space = isl_space_domain(space);
3770 space = isl_space_extend_domain_with_range(space,
3771 isl_space_copy(data->range));
3772 map = isl_map_reset_space(map, space);
3773 data->res = isl_union_map_add_map(data->res, map);
3775 return data->res ? isl_stat_ok : isl_stat_error;
3778 /* Replace the range space of all the maps in "umap" by
3779 * the range space of "space".
3781 * This assumes that all maps have the same output dimension.
3782 * This function should therefore not be made publicly available.
3784 * Since the spaces of the maps change, so do their hash value.
3785 * We therefore need to create a new isl_union_map.
3787 __isl_give isl_union_map *isl_union_map_reset_range_space(
3788 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3790 struct isl_union_map_reset_range_space_data data = { space };
3792 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3793 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3794 data.res = isl_union_map_free(data.res);
3796 isl_space_free(space);
3797 isl_union_map_free(umap);
3798 return data.res;
3801 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3802 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3803 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3804 * to the domain and the range of each map.
3805 * "res" collects the results.
3807 struct isl_union_order_at_data {
3808 isl_multi_union_pw_aff *mupa;
3809 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3810 __isl_take isl_multi_pw_aff *mpa2);
3811 isl_union_map *res;
3814 /* Intersect "map" with the result of applying data->order to
3815 * the functions in data->mupa that apply to the domain and the range
3816 * of "map" and add the result to data->res.
3818 static isl_stat order_at(__isl_take isl_map *map, void *user)
3820 struct isl_union_order_at_data *data = user;
3821 isl_space *space;
3822 isl_multi_pw_aff *mpa1, *mpa2;
3823 isl_map *order;
3825 space = isl_space_domain(isl_map_get_space(map));
3826 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3827 space = isl_space_range(isl_map_get_space(map));
3828 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3829 order = data->order(mpa1, mpa2);
3830 map = isl_map_intersect(map, order);
3831 data->res = isl_union_map_add_map(data->res, map);
3833 return data->res ? isl_stat_ok : isl_stat_error;
3836 /* Intersect each map in "umap" with the result of calling "order"
3837 * on the functions is "mupa" that apply to the domain and the range
3838 * of the map.
3840 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3841 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3842 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3843 __isl_take isl_multi_pw_aff *mpa2))
3845 struct isl_union_order_at_data data;
3847 umap = isl_union_map_align_params(umap,
3848 isl_multi_union_pw_aff_get_space(mupa));
3849 mupa = isl_multi_union_pw_aff_align_params(mupa,
3850 isl_union_map_get_space(umap));
3851 data.mupa = mupa;
3852 data.order = order;
3853 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3854 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3855 data.res = isl_union_map_free(data.res);
3857 isl_multi_union_pw_aff_free(mupa);
3858 isl_union_map_free(umap);
3859 return data.res;
3862 /* Return the subset of "umap" where the domain and the range
3863 * have equal "mupa" values.
3865 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3866 __isl_take isl_union_map *umap,
3867 __isl_take isl_multi_union_pw_aff *mupa)
3869 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3870 &isl_multi_pw_aff_eq_map);
3873 /* Return the subset of "umap" where the domain has a lexicographically
3874 * smaller "mupa" value than the range.
3876 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3877 __isl_take isl_union_map *umap,
3878 __isl_take isl_multi_union_pw_aff *mupa)
3880 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3881 &isl_multi_pw_aff_lex_lt_map);
3884 /* Return the subset of "umap" where the domain has a lexicographically
3885 * greater "mupa" value than the range.
3887 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3888 __isl_take isl_union_map *umap,
3889 __isl_take isl_multi_union_pw_aff *mupa)
3891 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3892 &isl_multi_pw_aff_lex_gt_map);
3895 /* Return the union of the elements in the list "list".
3897 __isl_give isl_union_set *isl_union_set_list_union(
3898 __isl_take isl_union_set_list *list)
3900 int i, n;
3901 isl_ctx *ctx;
3902 isl_space *space;
3903 isl_union_set *res;
3905 if (!list)
3906 return NULL;
3908 ctx = isl_union_set_list_get_ctx(list);
3909 space = isl_space_params_alloc(ctx, 0);
3910 res = isl_union_set_empty(space);
3912 n = isl_union_set_list_n_union_set(list);
3913 for (i = 0; i < n; ++i) {
3914 isl_union_set *uset_i;
3916 uset_i = isl_union_set_list_get_union_set(list, i);
3917 res = isl_union_set_union(res, uset_i);
3920 isl_union_set_list_free(list);
3921 return res;
3924 /* Update *hash with the hash value of "map".
3926 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3928 uint32_t *hash = user;
3929 uint32_t map_hash;
3931 map_hash = isl_map_get_hash(map);
3932 isl_hash_hash(*hash, map_hash);
3934 isl_map_free(map);
3935 return isl_stat_ok;
3938 /* Return a hash value that digests "umap".
3940 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3942 uint32_t hash;
3944 if (!umap)
3945 return 0;
3947 hash = isl_hash_init();
3948 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3949 return 0;
3951 return hash;
3954 /* Return a hash value that digests "uset".
3956 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3958 return isl_union_map_get_hash(uset);