add isl_union_{set,map}_get_hash
[isl.git] / isl_union_map.c
bloba6863d6d1a00fbbf5906f114886c9b11bda24bee
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
12 * B.P. 105 - 78153 Le Chesnay, France
15 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/ctx.h>
19 #include <isl/hash.h>
20 #include <isl/aff.h>
21 #include <isl/map.h>
22 #include <isl/set.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl/deprecated/union_map_int.h>
27 /* Return the number of parameters of "umap", where "type"
28 * is required to be set to isl_dim_param.
30 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
31 enum isl_dim_type type)
33 if (!umap)
34 return 0;
36 if (type != isl_dim_param)
37 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
38 "can only reference parameters", return 0);
40 return isl_space_dim(umap->dim, type);
43 /* Return the number of parameters of "uset", where "type"
44 * is required to be set to isl_dim_param.
46 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
47 enum isl_dim_type type)
49 return isl_union_map_dim(uset, type);
52 /* Return the id of the specified dimension.
54 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
55 enum isl_dim_type type, unsigned pos)
57 if (!umap)
58 return NULL;
60 if (type != isl_dim_param)
61 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
62 "can only reference parameters", return NULL);
64 return isl_space_get_dim_id(umap->dim, type, pos);
67 /* Is this union set a parameter domain?
69 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
71 isl_set *set;
72 isl_bool params;
74 if (!uset)
75 return isl_bool_error;
76 if (uset->table.n != 1)
77 return isl_bool_false;
79 set = isl_set_from_union_set(isl_union_set_copy(uset));
80 params = isl_set_is_params(set);
81 isl_set_free(set);
82 return params;
85 static __isl_give isl_union_map *isl_union_map_alloc(
86 __isl_take isl_space *space, int size)
88 isl_union_map *umap;
90 space = isl_space_params(space);
91 if (!space)
92 return NULL;
94 umap = isl_calloc_type(space->ctx, isl_union_map);
95 if (!umap) {
96 isl_space_free(space);
97 return NULL;
100 umap->ref = 1;
101 umap->dim = space;
102 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
103 return isl_union_map_free(umap);
105 return umap;
108 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
110 return isl_union_map_alloc(dim, 16);
113 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
115 return isl_union_map_empty(dim);
118 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
120 return umap ? umap->dim->ctx : NULL;
123 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
125 return uset ? uset->dim->ctx : NULL;
128 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
130 if (!umap)
131 return NULL;
132 return isl_space_copy(umap->dim);
135 /* Return the position of the parameter with the given name
136 * in "umap".
137 * Return -1 if no such dimension can be found.
139 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
140 enum isl_dim_type type, const char *name)
142 if (!umap)
143 return -1;
144 return isl_space_find_dim_by_name(umap->dim, type, name);
147 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
149 return isl_union_map_get_space(uset);
152 static isl_stat free_umap_entry(void **entry, void *user)
154 isl_map *map = *entry;
155 isl_map_free(map);
156 return isl_stat_ok;
159 static isl_stat add_map(__isl_take isl_map *map, void *user)
161 isl_union_map **umap = (isl_union_map **)user;
163 *umap = isl_union_map_add_map(*umap, map);
165 return isl_stat_ok;
168 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
170 isl_union_map *dup;
172 if (!umap)
173 return NULL;
175 dup = isl_union_map_empty(isl_space_copy(umap->dim));
176 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
177 goto error;
178 return dup;
179 error:
180 isl_union_map_free(dup);
181 return NULL;
184 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
186 if (!umap)
187 return NULL;
189 if (umap->ref == 1)
190 return umap;
191 umap->ref--;
192 return isl_union_map_dup(umap);
195 struct isl_union_align {
196 isl_reordering *exp;
197 isl_union_map *res;
200 static isl_stat align_entry(void **entry, void *user)
202 isl_map *map = *entry;
203 isl_reordering *exp;
204 struct isl_union_align *data = user;
206 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
207 isl_map_get_space(map));
209 data->res = isl_union_map_add_map(data->res,
210 isl_map_realign(isl_map_copy(map), exp));
212 return isl_stat_ok;
215 /* Align the parameters of umap along those of model.
216 * The result has the parameters of model first, in the same order
217 * as they appear in model, followed by any remaining parameters of
218 * umap that do not appear in model.
220 __isl_give isl_union_map *isl_union_map_align_params(
221 __isl_take isl_union_map *umap, __isl_take isl_space *model)
223 struct isl_union_align data = { NULL, NULL };
225 if (!umap || !model)
226 goto error;
228 if (isl_space_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
229 isl_space_free(model);
230 return umap;
233 model = isl_space_params(model);
234 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
235 if (!data.exp)
236 goto error;
238 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
239 umap->table.n);
240 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
241 &align_entry, &data) < 0)
242 goto error;
244 isl_reordering_free(data.exp);
245 isl_union_map_free(umap);
246 isl_space_free(model);
247 return data.res;
248 error:
249 isl_reordering_free(data.exp);
250 isl_union_map_free(umap);
251 isl_union_map_free(data.res);
252 isl_space_free(model);
253 return NULL;
256 __isl_give isl_union_set *isl_union_set_align_params(
257 __isl_take isl_union_set *uset, __isl_take isl_space *model)
259 return isl_union_map_align_params(uset, model);
262 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
263 __isl_take isl_union_map *umap2)
265 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
266 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
268 umap1 = isl_union_map_cow(umap1);
270 if (!umap1 || !umap2)
271 goto error;
273 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
274 goto error;
276 isl_union_map_free(umap2);
278 return umap1;
279 error:
280 isl_union_map_free(umap1);
281 isl_union_map_free(umap2);
282 return NULL;
285 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
286 __isl_take isl_union_set *uset2)
288 return isl_union_map_union(uset1, uset2);
291 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
293 if (!umap)
294 return NULL;
296 umap->ref++;
297 return umap;
300 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
302 return isl_union_map_copy(uset);
305 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
307 if (!umap)
308 return NULL;
310 if (--umap->ref > 0)
311 return NULL;
313 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
314 &free_umap_entry, NULL);
315 isl_hash_table_clear(&umap->table);
316 isl_space_free(umap->dim);
317 free(umap);
318 return NULL;
321 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
323 return isl_union_map_free(uset);
326 static int has_dim(const void *entry, const void *val)
328 isl_map *map = (isl_map *)entry;
329 isl_space *dim = (isl_space *)val;
331 return isl_space_is_equal(map->dim, dim);
334 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
335 __isl_take isl_map *map)
337 uint32_t hash;
338 struct isl_hash_table_entry *entry;
340 if (!map || !umap)
341 goto error;
343 if (isl_map_plain_is_empty(map)) {
344 isl_map_free(map);
345 return umap;
348 if (!isl_space_match(map->dim, isl_dim_param, umap->dim, isl_dim_param)) {
349 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
350 map = isl_map_align_params(map, isl_union_map_get_space(umap));
353 umap = isl_union_map_cow(umap);
355 if (!map || !umap)
356 goto error;
358 hash = isl_space_get_hash(map->dim);
359 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
360 &has_dim, map->dim, 1);
361 if (!entry)
362 goto error;
364 if (!entry->data)
365 entry->data = map;
366 else {
367 entry->data = isl_map_union(entry->data, isl_map_copy(map));
368 if (!entry->data)
369 goto error;
370 isl_map_free(map);
373 return umap;
374 error:
375 isl_map_free(map);
376 isl_union_map_free(umap);
377 return NULL;
380 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
381 __isl_take isl_set *set)
383 return isl_union_map_add_map(uset, (isl_map *)set);
386 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
388 isl_space *dim;
389 isl_union_map *umap;
391 if (!map)
392 return NULL;
394 dim = isl_map_get_space(map);
395 dim = isl_space_params(dim);
396 umap = isl_union_map_empty(dim);
397 umap = isl_union_map_add_map(umap, map);
399 return umap;
402 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
404 return isl_union_map_from_map((isl_map *)set);
407 __isl_give isl_union_map *isl_union_map_from_basic_map(
408 __isl_take isl_basic_map *bmap)
410 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
413 __isl_give isl_union_set *isl_union_set_from_basic_set(
414 __isl_take isl_basic_set *bset)
416 return isl_union_map_from_basic_map(bset);
419 struct isl_union_map_foreach_data
421 isl_stat (*fn)(__isl_take isl_map *map, void *user);
422 void *user;
425 static isl_stat call_on_copy(void **entry, void *user)
427 isl_map *map = *entry;
428 struct isl_union_map_foreach_data *data;
429 data = (struct isl_union_map_foreach_data *)user;
431 return data->fn(isl_map_copy(map), data->user);
434 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
436 return umap ? umap->table.n : 0;
439 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
441 return uset ? uset->table.n : 0;
444 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
445 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
447 struct isl_union_map_foreach_data data = { fn, user };
449 if (!umap)
450 return isl_stat_error;
452 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
453 &call_on_copy, &data);
456 static isl_stat copy_map(void **entry, void *user)
458 isl_map *map = *entry;
459 isl_map **map_p = user;
461 *map_p = isl_map_copy(map);
463 return isl_stat_error;
466 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
468 isl_ctx *ctx;
469 isl_map *map = NULL;
471 if (!umap)
472 return NULL;
473 ctx = isl_union_map_get_ctx(umap);
474 if (umap->table.n != 1)
475 isl_die(ctx, isl_error_invalid,
476 "union map needs to contain elements in exactly "
477 "one space", goto error);
479 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
481 isl_union_map_free(umap);
483 return map;
484 error:
485 isl_union_map_free(umap);
486 return NULL;
489 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
491 return isl_map_from_union_map(uset);
494 /* Extract the map in "umap" that lives in the given space (ignoring
495 * parameters).
497 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
498 __isl_take isl_space *space)
500 uint32_t hash;
501 struct isl_hash_table_entry *entry;
503 space = isl_space_drop_dims(space, isl_dim_param,
504 0, isl_space_dim(space, isl_dim_param));
505 space = isl_space_align_params(space, isl_union_map_get_space(umap));
506 if (!umap || !space)
507 goto error;
509 hash = isl_space_get_hash(space);
510 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
511 &has_dim, space, 0);
512 if (!entry)
513 return isl_map_empty(space);
514 isl_space_free(space);
515 return isl_map_copy(entry->data);
516 error:
517 isl_space_free(space);
518 return NULL;
521 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
522 __isl_take isl_space *dim)
524 return (isl_set *)isl_union_map_extract_map(uset, dim);
527 /* Check if umap contains a map in the given space.
529 __isl_give int isl_union_map_contains(__isl_keep isl_union_map *umap,
530 __isl_keep isl_space *dim)
532 uint32_t hash;
533 struct isl_hash_table_entry *entry;
535 if (!umap || !dim)
536 return -1;
538 hash = isl_space_get_hash(dim);
539 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
540 &has_dim, dim, 0);
541 return !!entry;
544 __isl_give int isl_union_set_contains(__isl_keep isl_union_set *uset,
545 __isl_keep isl_space *dim)
547 return isl_union_map_contains(uset, dim);
550 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
551 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
553 return isl_union_map_foreach_map(uset,
554 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
557 struct isl_union_set_foreach_point_data {
558 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
559 void *user;
562 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
564 struct isl_union_set_foreach_point_data *data = user;
565 isl_stat r;
567 r = isl_set_foreach_point(set, data->fn, data->user);
568 isl_set_free(set);
570 return r;
573 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
574 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
576 struct isl_union_set_foreach_point_data data = { fn, user };
577 return isl_union_set_foreach_set(uset, &foreach_point, &data);
580 struct isl_union_map_gen_bin_data {
581 isl_union_map *umap2;
582 isl_union_map *res;
585 static isl_stat subtract_entry(void **entry, void *user)
587 struct isl_union_map_gen_bin_data *data = user;
588 uint32_t hash;
589 struct isl_hash_table_entry *entry2;
590 isl_map *map = *entry;
592 hash = isl_space_get_hash(map->dim);
593 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
594 hash, &has_dim, map->dim, 0);
595 map = isl_map_copy(map);
596 if (entry2) {
597 int empty;
598 map = isl_map_subtract(map, isl_map_copy(entry2->data));
600 empty = isl_map_is_empty(map);
601 if (empty < 0) {
602 isl_map_free(map);
603 return isl_stat_error;
605 if (empty) {
606 isl_map_free(map);
607 return isl_stat_ok;
610 data->res = isl_union_map_add_map(data->res, map);
612 return isl_stat_ok;
615 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
616 __isl_take isl_union_map *umap2, isl_stat (*fn)(void **, void *))
618 struct isl_union_map_gen_bin_data data = { NULL, NULL };
620 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
621 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
623 if (!umap1 || !umap2)
624 goto error;
626 data.umap2 = umap2;
627 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
628 umap1->table.n);
629 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
630 fn, &data) < 0)
631 goto error;
633 isl_union_map_free(umap1);
634 isl_union_map_free(umap2);
635 return data.res;
636 error:
637 isl_union_map_free(umap1);
638 isl_union_map_free(umap2);
639 isl_union_map_free(data.res);
640 return NULL;
643 __isl_give isl_union_map *isl_union_map_subtract(
644 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
646 return gen_bin_op(umap1, umap2, &subtract_entry);
649 __isl_give isl_union_set *isl_union_set_subtract(
650 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
652 return isl_union_map_subtract(uset1, uset2);
655 struct isl_union_map_gen_bin_set_data {
656 isl_set *set;
657 isl_union_map *res;
660 static isl_stat intersect_params_entry(void **entry, void *user)
662 struct isl_union_map_gen_bin_set_data *data = user;
663 isl_map *map = *entry;
664 int empty;
666 map = isl_map_copy(map);
667 map = isl_map_intersect_params(map, isl_set_copy(data->set));
669 empty = isl_map_is_empty(map);
670 if (empty < 0) {
671 isl_map_free(map);
672 return isl_stat_error;
675 data->res = isl_union_map_add_map(data->res, map);
677 return isl_stat_ok;
680 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
681 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
683 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
685 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
686 set = isl_set_align_params(set, isl_union_map_get_space(umap));
688 if (!umap || !set)
689 goto error;
691 data.set = set;
692 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
693 umap->table.n);
694 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
695 fn, &data) < 0)
696 goto error;
698 isl_union_map_free(umap);
699 isl_set_free(set);
700 return data.res;
701 error:
702 isl_union_map_free(umap);
703 isl_set_free(set);
704 isl_union_map_free(data.res);
705 return NULL;
708 /* Intersect "umap" with the parameter domain "set".
710 * If "set" does not have any constraints, then we can return immediately.
712 __isl_give isl_union_map *isl_union_map_intersect_params(
713 __isl_take isl_union_map *umap, __isl_take isl_set *set)
715 int is_universe;
717 is_universe = isl_set_plain_is_universe(set);
718 if (is_universe < 0)
719 goto error;
720 if (is_universe) {
721 isl_set_free(set);
722 return umap;
725 return gen_bin_set_op(umap, set, &intersect_params_entry);
726 error:
727 isl_union_map_free(umap);
728 isl_set_free(set);
729 return NULL;
732 __isl_give isl_union_set *isl_union_set_intersect_params(
733 __isl_take isl_union_set *uset, __isl_take isl_set *set)
735 return isl_union_map_intersect_params(uset, set);
738 static __isl_give isl_union_map *union_map_intersect_params(
739 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
741 return isl_union_map_intersect_params(umap,
742 isl_set_from_union_set(uset));
745 static __isl_give isl_union_map *union_map_gist_params(
746 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
748 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
751 struct isl_union_map_match_bin_data {
752 isl_union_map *umap2;
753 isl_union_map *res;
754 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
757 static isl_stat match_bin_entry(void **entry, void *user)
759 struct isl_union_map_match_bin_data *data = user;
760 uint32_t hash;
761 struct isl_hash_table_entry *entry2;
762 isl_map *map = *entry;
763 int empty;
765 hash = isl_space_get_hash(map->dim);
766 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
767 hash, &has_dim, map->dim, 0);
768 if (!entry2)
769 return isl_stat_ok;
771 map = isl_map_copy(map);
772 map = data->fn(map, isl_map_copy(entry2->data));
774 empty = isl_map_is_empty(map);
775 if (empty < 0) {
776 isl_map_free(map);
777 return isl_stat_error;
779 if (empty) {
780 isl_map_free(map);
781 return isl_stat_ok;
784 data->res = isl_union_map_add_map(data->res, map);
786 return isl_stat_ok;
789 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
790 __isl_take isl_union_map *umap2,
791 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
793 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
795 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
796 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
798 if (!umap1 || !umap2)
799 goto error;
801 data.umap2 = umap2;
802 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
803 umap1->table.n);
804 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
805 &match_bin_entry, &data) < 0)
806 goto error;
808 isl_union_map_free(umap1);
809 isl_union_map_free(umap2);
810 return data.res;
811 error:
812 isl_union_map_free(umap1);
813 isl_union_map_free(umap2);
814 isl_union_map_free(data.res);
815 return NULL;
818 __isl_give isl_union_map *isl_union_map_intersect(
819 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
821 return match_bin_op(umap1, umap2, &isl_map_intersect);
824 /* Compute the intersection of the two union_sets.
825 * As a special case, if exactly one of the two union_sets
826 * is a parameter domain, then intersect the parameter domain
827 * of the other one with this set.
829 __isl_give isl_union_set *isl_union_set_intersect(
830 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
832 int p1, p2;
834 p1 = isl_union_set_is_params(uset1);
835 p2 = isl_union_set_is_params(uset2);
836 if (p1 < 0 || p2 < 0)
837 goto error;
838 if (!p1 && p2)
839 return union_map_intersect_params(uset1, uset2);
840 if (p1 && !p2)
841 return union_map_intersect_params(uset2, uset1);
842 return isl_union_map_intersect(uset1, uset2);
843 error:
844 isl_union_set_free(uset1);
845 isl_union_set_free(uset2);
846 return NULL;
849 static isl_stat gist_params_entry(void **entry, void *user)
851 struct isl_union_map_gen_bin_set_data *data = user;
852 isl_map *map = *entry;
853 int empty;
855 map = isl_map_copy(map);
856 map = isl_map_gist_params(map, isl_set_copy(data->set));
858 empty = isl_map_is_empty(map);
859 if (empty < 0) {
860 isl_map_free(map);
861 return isl_stat_error;
864 data->res = isl_union_map_add_map(data->res, map);
866 return isl_stat_ok;
869 __isl_give isl_union_map *isl_union_map_gist_params(
870 __isl_take isl_union_map *umap, __isl_take isl_set *set)
872 return gen_bin_set_op(umap, set, &gist_params_entry);
875 __isl_give isl_union_set *isl_union_set_gist_params(
876 __isl_take isl_union_set *uset, __isl_take isl_set *set)
878 return isl_union_map_gist_params(uset, set);
881 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
882 __isl_take isl_union_map *context)
884 return match_bin_op(umap, context, &isl_map_gist);
887 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
888 __isl_take isl_union_set *context)
890 if (isl_union_set_is_params(context))
891 return union_map_gist_params(uset, context);
892 return isl_union_map_gist(uset, context);
895 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
896 __isl_take isl_map *set2)
898 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
901 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
902 __isl_take isl_map *set2)
904 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
907 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
908 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
910 return match_bin_op(uset1, uset2, &lex_lt_set);
913 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
914 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
916 return match_bin_op(uset1, uset2, &lex_le_set);
919 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
920 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
922 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
925 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
926 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
928 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
931 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
932 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
934 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
937 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
938 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
940 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
943 static isl_stat intersect_domain_entry(void **entry, void *user)
945 struct isl_union_map_gen_bin_data *data = user;
946 uint32_t hash;
947 struct isl_hash_table_entry *entry2;
948 isl_space *dim;
949 isl_map *map = *entry;
950 isl_bool empty;
952 dim = isl_map_get_space(map);
953 dim = isl_space_domain(dim);
954 hash = isl_space_get_hash(dim);
955 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
956 hash, &has_dim, dim, 0);
957 isl_space_free(dim);
958 if (!entry2)
959 return isl_stat_ok;
961 map = isl_map_copy(map);
962 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
964 empty = isl_map_is_empty(map);
965 if (empty < 0) {
966 isl_map_free(map);
967 return isl_stat_error;
969 if (empty) {
970 isl_map_free(map);
971 return isl_stat_ok;
974 data->res = isl_union_map_add_map(data->res, map);
976 return isl_stat_ok;
979 /* Intersect the domain of "umap" with "uset".
980 * If "uset" is a parameters domain, then intersect the parameter
981 * domain of "umap" with this set.
983 __isl_give isl_union_map *isl_union_map_intersect_domain(
984 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
986 if (isl_union_set_is_params(uset))
987 return union_map_intersect_params(umap, uset);
988 return gen_bin_op(umap, uset, &intersect_domain_entry);
991 /* Remove the elements of data->umap2 from the domain of *entry
992 * and add the result to data->res.
994 static isl_stat subtract_domain_entry(void **entry, void *user)
996 struct isl_union_map_gen_bin_data *data = user;
997 uint32_t hash;
998 struct isl_hash_table_entry *entry2;
999 isl_space *dim;
1000 isl_map *map = *entry;
1001 isl_bool empty;
1003 dim = isl_map_get_space(map);
1004 dim = isl_space_domain(dim);
1005 hash = isl_space_get_hash(dim);
1006 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1007 hash, &has_dim, dim, 0);
1008 isl_space_free(dim);
1010 map = isl_map_copy(map);
1012 if (!entry2) {
1013 data->res = isl_union_map_add_map(data->res, map);
1014 return isl_stat_ok;
1017 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1019 empty = isl_map_is_empty(map);
1020 if (empty < 0) {
1021 isl_map_free(map);
1022 return isl_stat_error;
1024 if (empty) {
1025 isl_map_free(map);
1026 return isl_stat_ok;
1029 data->res = isl_union_map_add_map(data->res, map);
1031 return isl_stat_ok;
1034 /* Remove the elements of "uset" from the domain of "umap".
1036 __isl_give isl_union_map *isl_union_map_subtract_domain(
1037 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1039 return gen_bin_op(umap, dom, &subtract_domain_entry);
1042 /* Remove the elements of data->umap2 from the range of *entry
1043 * and add the result to data->res.
1045 static isl_stat subtract_range_entry(void **entry, void *user)
1047 struct isl_union_map_gen_bin_data *data = user;
1048 uint32_t hash;
1049 struct isl_hash_table_entry *entry2;
1050 isl_space *space;
1051 isl_map *map = *entry;
1052 isl_bool empty;
1054 space = isl_map_get_space(map);
1055 space = isl_space_range(space);
1056 hash = isl_space_get_hash(space);
1057 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1058 hash, &has_dim, space, 0);
1059 isl_space_free(space);
1061 map = isl_map_copy(map);
1063 if (!entry2) {
1064 data->res = isl_union_map_add_map(data->res, map);
1065 return isl_stat_ok;
1068 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1070 empty = isl_map_is_empty(map);
1071 if (empty < 0) {
1072 isl_map_free(map);
1073 return isl_stat_error;
1075 if (empty) {
1076 isl_map_free(map);
1077 return isl_stat_ok;
1080 data->res = isl_union_map_add_map(data->res, map);
1082 return isl_stat_ok;
1085 /* Remove the elements of "uset" from the range of "umap".
1087 __isl_give isl_union_map *isl_union_map_subtract_range(
1088 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1090 return gen_bin_op(umap, dom, &subtract_range_entry);
1093 static isl_stat gist_domain_entry(void **entry, void *user)
1095 struct isl_union_map_gen_bin_data *data = user;
1096 uint32_t hash;
1097 struct isl_hash_table_entry *entry2;
1098 isl_space *dim;
1099 isl_map *map = *entry;
1100 isl_bool empty;
1102 dim = isl_map_get_space(map);
1103 dim = isl_space_domain(dim);
1104 hash = isl_space_get_hash(dim);
1105 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1106 hash, &has_dim, dim, 0);
1107 isl_space_free(dim);
1108 if (!entry2)
1109 return isl_stat_ok;
1111 map = isl_map_copy(map);
1112 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1114 empty = isl_map_is_empty(map);
1115 if (empty < 0) {
1116 isl_map_free(map);
1117 return isl_stat_error;
1120 data->res = isl_union_map_add_map(data->res, map);
1122 return isl_stat_ok;
1125 /* Compute the gist of "umap" with respect to the domain "uset".
1126 * If "uset" is a parameters domain, then compute the gist
1127 * with respect to this parameter domain.
1129 __isl_give isl_union_map *isl_union_map_gist_domain(
1130 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1132 if (isl_union_set_is_params(uset))
1133 return union_map_gist_params(umap, uset);
1134 return gen_bin_op(umap, uset, &gist_domain_entry);
1137 static isl_stat gist_range_entry(void **entry, void *user)
1139 struct isl_union_map_gen_bin_data *data = user;
1140 uint32_t hash;
1141 struct isl_hash_table_entry *entry2;
1142 isl_space *space;
1143 isl_map *map = *entry;
1144 isl_bool empty;
1146 space = isl_map_get_space(map);
1147 space = isl_space_range(space);
1148 hash = isl_space_get_hash(space);
1149 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1150 hash, &has_dim, space, 0);
1151 isl_space_free(space);
1152 if (!entry2)
1153 return isl_stat_ok;
1155 map = isl_map_copy(map);
1156 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1158 empty = isl_map_is_empty(map);
1159 if (empty < 0) {
1160 isl_map_free(map);
1161 return isl_stat_error;
1164 data->res = isl_union_map_add_map(data->res, map);
1166 return isl_stat_ok;
1169 /* Compute the gist of "umap" with respect to the range "uset".
1171 __isl_give isl_union_map *isl_union_map_gist_range(
1172 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1174 return gen_bin_op(umap, uset, &gist_range_entry);
1177 static isl_stat intersect_range_entry(void **entry, void *user)
1179 struct isl_union_map_gen_bin_data *data = user;
1180 uint32_t hash;
1181 struct isl_hash_table_entry *entry2;
1182 isl_space *dim;
1183 isl_map *map = *entry;
1184 isl_bool empty;
1186 dim = isl_map_get_space(map);
1187 dim = isl_space_range(dim);
1188 hash = isl_space_get_hash(dim);
1189 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1190 hash, &has_dim, dim, 0);
1191 isl_space_free(dim);
1192 if (!entry2)
1193 return isl_stat_ok;
1195 map = isl_map_copy(map);
1196 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1198 empty = isl_map_is_empty(map);
1199 if (empty < 0) {
1200 isl_map_free(map);
1201 return isl_stat_error;
1203 if (empty) {
1204 isl_map_free(map);
1205 return isl_stat_ok;
1208 data->res = isl_union_map_add_map(data->res, map);
1210 return isl_stat_ok;
1213 __isl_give isl_union_map *isl_union_map_intersect_range(
1214 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1216 return gen_bin_op(umap, uset, &intersect_range_entry);
1219 struct isl_union_map_bin_data {
1220 isl_union_map *umap2;
1221 isl_union_map *res;
1222 isl_map *map;
1223 isl_stat (*fn)(void **entry, void *user);
1226 static isl_stat apply_range_entry(void **entry, void *user)
1228 struct isl_union_map_bin_data *data = user;
1229 isl_map *map2 = *entry;
1230 isl_bool empty;
1232 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1233 map2->dim, isl_dim_in))
1234 return isl_stat_ok;
1236 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1238 empty = isl_map_is_empty(map2);
1239 if (empty < 0) {
1240 isl_map_free(map2);
1241 return isl_stat_error;
1243 if (empty) {
1244 isl_map_free(map2);
1245 return isl_stat_ok;
1248 data->res = isl_union_map_add_map(data->res, map2);
1250 return isl_stat_ok;
1253 static isl_stat bin_entry(void **entry, void *user)
1255 struct isl_union_map_bin_data *data = user;
1256 isl_map *map = *entry;
1258 data->map = map;
1259 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1260 data->fn, data) < 0)
1261 return isl_stat_error;
1263 return isl_stat_ok;
1266 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1267 __isl_take isl_union_map *umap2,
1268 isl_stat (*fn)(void **entry, void *user))
1270 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1272 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1273 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1275 if (!umap1 || !umap2)
1276 goto error;
1278 data.umap2 = umap2;
1279 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1280 umap1->table.n);
1281 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1282 &bin_entry, &data) < 0)
1283 goto error;
1285 isl_union_map_free(umap1);
1286 isl_union_map_free(umap2);
1287 return data.res;
1288 error:
1289 isl_union_map_free(umap1);
1290 isl_union_map_free(umap2);
1291 isl_union_map_free(data.res);
1292 return NULL;
1295 __isl_give isl_union_map *isl_union_map_apply_range(
1296 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1298 return bin_op(umap1, umap2, &apply_range_entry);
1301 __isl_give isl_union_map *isl_union_map_apply_domain(
1302 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1304 umap1 = isl_union_map_reverse(umap1);
1305 umap1 = isl_union_map_apply_range(umap1, umap2);
1306 return isl_union_map_reverse(umap1);
1309 __isl_give isl_union_set *isl_union_set_apply(
1310 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1312 return isl_union_map_apply_range(uset, umap);
1315 static isl_stat map_lex_lt_entry(void **entry, void *user)
1317 struct isl_union_map_bin_data *data = user;
1318 isl_map *map2 = *entry;
1320 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1321 map2->dim, isl_dim_out))
1322 return isl_stat_ok;
1324 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1326 data->res = isl_union_map_add_map(data->res, map2);
1328 return isl_stat_ok;
1331 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1332 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1334 return bin_op(umap1, umap2, &map_lex_lt_entry);
1337 static isl_stat map_lex_le_entry(void **entry, void *user)
1339 struct isl_union_map_bin_data *data = user;
1340 isl_map *map2 = *entry;
1342 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1343 map2->dim, isl_dim_out))
1344 return isl_stat_ok;
1346 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1348 data->res = isl_union_map_add_map(data->res, map2);
1350 return isl_stat_ok;
1353 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1354 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1356 return bin_op(umap1, umap2, &map_lex_le_entry);
1359 static isl_stat product_entry(void **entry, void *user)
1361 struct isl_union_map_bin_data *data = user;
1362 isl_map *map2 = *entry;
1364 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1366 data->res = isl_union_map_add_map(data->res, map2);
1368 return isl_stat_ok;
1371 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1372 __isl_take isl_union_map *umap2)
1374 return bin_op(umap1, umap2, &product_entry);
1377 static isl_stat set_product_entry(void **entry, void *user)
1379 struct isl_union_map_bin_data *data = user;
1380 isl_set *set2 = *entry;
1382 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1384 data->res = isl_union_set_add_set(data->res, set2);
1386 return isl_stat_ok;
1389 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1390 __isl_take isl_union_set *uset2)
1392 return bin_op(uset1, uset2, &set_product_entry);
1395 static isl_stat domain_product_entry(void **entry, void *user)
1397 struct isl_union_map_bin_data *data = user;
1398 isl_map *map2 = *entry;
1400 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1401 map2->dim, isl_dim_out))
1402 return isl_stat_ok;
1404 map2 = isl_map_domain_product(isl_map_copy(data->map),
1405 isl_map_copy(map2));
1407 data->res = isl_union_map_add_map(data->res, map2);
1409 return isl_stat_ok;
1412 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1414 __isl_give isl_union_map *isl_union_map_domain_product(
1415 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1417 return bin_op(umap1, umap2, &domain_product_entry);
1420 static isl_stat range_product_entry(void **entry, void *user)
1422 struct isl_union_map_bin_data *data = user;
1423 isl_map *map2 = *entry;
1425 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1426 map2->dim, isl_dim_in))
1427 return isl_stat_ok;
1429 map2 = isl_map_range_product(isl_map_copy(data->map),
1430 isl_map_copy(map2));
1432 data->res = isl_union_map_add_map(data->res, map2);
1434 return isl_stat_ok;
1437 __isl_give isl_union_map *isl_union_map_range_product(
1438 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1440 return bin_op(umap1, umap2, &range_product_entry);
1443 /* If data->map A -> B and "map2" C -> D have the same range space,
1444 * then add (A, C) -> (B * D) to data->res.
1446 static isl_stat flat_domain_product_entry(void **entry, void *user)
1448 struct isl_union_map_bin_data *data = user;
1449 isl_map *map2 = *entry;
1451 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1452 map2->dim, isl_dim_out))
1453 return isl_stat_ok;
1455 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1456 isl_map_copy(map2));
1458 data->res = isl_union_map_add_map(data->res, map2);
1460 return isl_stat_ok;
1463 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1465 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1466 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1468 return bin_op(umap1, umap2, &flat_domain_product_entry);
1471 static isl_stat flat_range_product_entry(void **entry, void *user)
1473 struct isl_union_map_bin_data *data = user;
1474 isl_map *map2 = *entry;
1476 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1477 map2->dim, isl_dim_in))
1478 return isl_stat_ok;
1480 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1481 isl_map_copy(map2));
1483 data->res = isl_union_map_add_map(data->res, map2);
1485 return isl_stat_ok;
1488 __isl_give isl_union_map *isl_union_map_flat_range_product(
1489 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1491 return bin_op(umap1, umap2, &flat_range_product_entry);
1494 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1495 isl_stat (*fn)(void **, void *))
1497 isl_union_set *res;
1499 if (!umap)
1500 return NULL;
1502 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1503 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1504 goto error;
1506 isl_union_map_free(umap);
1507 return res;
1508 error:
1509 isl_union_map_free(umap);
1510 isl_union_set_free(res);
1511 return NULL;
1514 static isl_stat from_range_entry(void **entry, void *user)
1516 isl_map *set = *entry;
1517 isl_union_set **res = user;
1519 *res = isl_union_map_add_map(*res,
1520 isl_map_from_range(isl_set_copy(set)));
1522 return isl_stat_ok;
1525 __isl_give isl_union_map *isl_union_map_from_range(
1526 __isl_take isl_union_set *uset)
1528 return cond_un_op(uset, &from_range_entry);
1531 __isl_give isl_union_map *isl_union_map_from_domain(
1532 __isl_take isl_union_set *uset)
1534 return isl_union_map_reverse(isl_union_map_from_range(uset));
1537 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1538 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1540 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1541 isl_union_map_from_range(range));
1544 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1545 isl_stat (*fn)(void **, void *))
1547 umap = isl_union_map_cow(umap);
1548 if (!umap)
1549 return NULL;
1551 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1552 goto error;
1554 return umap;
1555 error:
1556 isl_union_map_free(umap);
1557 return NULL;
1560 static isl_stat affine_entry(void **entry, void *user)
1562 isl_map **map = (isl_map **)entry;
1564 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1566 return *map ? isl_stat_ok : isl_stat_error;
1569 __isl_give isl_union_map *isl_union_map_affine_hull(
1570 __isl_take isl_union_map *umap)
1572 return un_op(umap, &affine_entry);
1575 __isl_give isl_union_set *isl_union_set_affine_hull(
1576 __isl_take isl_union_set *uset)
1578 return isl_union_map_affine_hull(uset);
1581 static isl_stat polyhedral_entry(void **entry, void *user)
1583 isl_map **map = (isl_map **)entry;
1585 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1587 return *map ? isl_stat_ok : isl_stat_error;
1590 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1591 __isl_take isl_union_map *umap)
1593 return un_op(umap, &polyhedral_entry);
1596 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1597 __isl_take isl_union_set *uset)
1599 return isl_union_map_polyhedral_hull(uset);
1602 static isl_stat simple_entry(void **entry, void *user)
1604 isl_map **map = (isl_map **)entry;
1606 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1608 return *map ? isl_stat_ok : isl_stat_error;
1611 __isl_give isl_union_map *isl_union_map_simple_hull(
1612 __isl_take isl_union_map *umap)
1614 return un_op(umap, &simple_entry);
1617 __isl_give isl_union_set *isl_union_set_simple_hull(
1618 __isl_take isl_union_set *uset)
1620 return isl_union_map_simple_hull(uset);
1623 static isl_stat inplace_entry(void **entry, void *user)
1625 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1626 isl_map **map = (isl_map **)entry;
1627 isl_map *copy;
1629 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1630 copy = fn(isl_map_copy(*map));
1631 if (!copy)
1632 return isl_stat_error;
1634 isl_map_free(*map);
1635 *map = copy;
1637 return isl_stat_ok;
1640 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1641 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1643 if (!umap)
1644 return NULL;
1646 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1647 &inplace_entry, &fn) < 0)
1648 goto error;
1650 return umap;
1651 error:
1652 isl_union_map_free(umap);
1653 return NULL;
1656 /* Remove redundant constraints in each of the basic maps of "umap".
1657 * Since removing redundant constraints does not change the meaning
1658 * or the space, the operation can be performed in-place.
1660 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1661 __isl_take isl_union_map *umap)
1663 return inplace(umap, &isl_map_remove_redundancies);
1666 /* Remove redundant constraints in each of the basic sets of "uset".
1668 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1669 __isl_take isl_union_set *uset)
1671 return isl_union_map_remove_redundancies(uset);
1674 __isl_give isl_union_map *isl_union_map_coalesce(
1675 __isl_take isl_union_map *umap)
1677 return inplace(umap, &isl_map_coalesce);
1680 __isl_give isl_union_set *isl_union_set_coalesce(
1681 __isl_take isl_union_set *uset)
1683 return isl_union_map_coalesce(uset);
1686 __isl_give isl_union_map *isl_union_map_detect_equalities(
1687 __isl_take isl_union_map *umap)
1689 return inplace(umap, &isl_map_detect_equalities);
1692 __isl_give isl_union_set *isl_union_set_detect_equalities(
1693 __isl_take isl_union_set *uset)
1695 return isl_union_map_detect_equalities(uset);
1698 __isl_give isl_union_map *isl_union_map_compute_divs(
1699 __isl_take isl_union_map *umap)
1701 return inplace(umap, &isl_map_compute_divs);
1704 __isl_give isl_union_set *isl_union_set_compute_divs(
1705 __isl_take isl_union_set *uset)
1707 return isl_union_map_compute_divs(uset);
1710 static isl_stat lexmin_entry(void **entry, void *user)
1712 isl_map **map = (isl_map **)entry;
1714 *map = isl_map_lexmin(*map);
1716 return *map ? isl_stat_ok : isl_stat_error;
1719 __isl_give isl_union_map *isl_union_map_lexmin(
1720 __isl_take isl_union_map *umap)
1722 return un_op(umap, &lexmin_entry);
1725 __isl_give isl_union_set *isl_union_set_lexmin(
1726 __isl_take isl_union_set *uset)
1728 return isl_union_map_lexmin(uset);
1731 static isl_stat lexmax_entry(void **entry, void *user)
1733 isl_map **map = (isl_map **)entry;
1735 *map = isl_map_lexmax(*map);
1737 return *map ? isl_stat_ok : isl_stat_error;
1740 __isl_give isl_union_map *isl_union_map_lexmax(
1741 __isl_take isl_union_map *umap)
1743 return un_op(umap, &lexmax_entry);
1746 __isl_give isl_union_set *isl_union_set_lexmax(
1747 __isl_take isl_union_set *uset)
1749 return isl_union_map_lexmax(uset);
1752 static isl_stat universe_entry(void **entry, void *user)
1754 isl_map *map = *entry;
1755 isl_union_map **res = user;
1757 map = isl_map_universe(isl_map_get_space(map));
1758 *res = isl_union_map_add_map(*res, map);
1760 return isl_stat_ok;
1763 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1765 return cond_un_op(umap, &universe_entry);
1768 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1770 return isl_union_map_universe(uset);
1773 static isl_stat reverse_entry(void **entry, void *user)
1775 isl_map *map = *entry;
1776 isl_union_map **res = user;
1778 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1780 return isl_stat_ok;
1783 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1785 return cond_un_op(umap, &reverse_entry);
1788 static isl_stat params_entry(void **entry, void *user)
1790 isl_map *map = *entry;
1791 isl_union_set **res = user;
1793 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1795 return isl_stat_ok;
1798 /* Compute the parameter domain of the given union map.
1800 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1802 int empty;
1804 empty = isl_union_map_is_empty(umap);
1805 if (empty < 0)
1806 goto error;
1807 if (empty) {
1808 isl_space *space;
1809 space = isl_union_map_get_space(umap);
1810 isl_union_map_free(umap);
1811 return isl_set_empty(space);
1813 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1814 error:
1815 isl_union_map_free(umap);
1816 return NULL;
1819 /* Compute the parameter domain of the given union set.
1821 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1823 return isl_union_map_params(uset);
1826 static isl_stat domain_entry(void **entry, void *user)
1828 isl_map *map = *entry;
1829 isl_union_set **res = user;
1831 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1833 return isl_stat_ok;
1836 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1838 return cond_un_op(umap, &domain_entry);
1841 static isl_stat range_entry(void **entry, void *user)
1843 isl_map *map = *entry;
1844 isl_union_set **res = user;
1846 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1848 return isl_stat_ok;
1851 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1853 return cond_un_op(umap, &range_entry);
1856 static isl_stat domain_map_entry(void **entry, void *user)
1858 isl_map *map = *entry;
1859 isl_union_set **res = user;
1861 *res = isl_union_map_add_map(*res,
1862 isl_map_domain_map(isl_map_copy(map)));
1864 return isl_stat_ok;
1867 __isl_give isl_union_map *isl_union_map_domain_map(
1868 __isl_take isl_union_map *umap)
1870 return cond_un_op(umap, &domain_map_entry);
1873 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1874 * add the result to "res".
1876 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1878 isl_union_pw_multi_aff **res = user;
1879 isl_multi_aff *ma;
1880 isl_pw_multi_aff *pma;
1882 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1883 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1884 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1886 return *res ? isl_stat_ok : isl_stat_error;
1890 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1891 * to its domain.
1893 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1894 __isl_take isl_union_map *umap)
1896 isl_union_pw_multi_aff *res;
1898 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1899 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1900 res = isl_union_pw_multi_aff_free(res);
1902 isl_union_map_free(umap);
1903 return res;
1906 static isl_stat range_map_entry(void **entry, void *user)
1908 isl_map *map = *entry;
1909 isl_union_set **res = user;
1911 *res = isl_union_map_add_map(*res,
1912 isl_map_range_map(isl_map_copy(map)));
1914 return isl_stat_ok;
1917 __isl_give isl_union_map *isl_union_map_range_map(
1918 __isl_take isl_union_map *umap)
1920 return cond_un_op(umap, &range_map_entry);
1923 /* Check if "set" is of the form A[B -> C].
1924 * If so, add A[B -> C] -> B to "res".
1926 static isl_stat wrapped_domain_map_entry(void **entry, void *user)
1928 isl_set *set = *entry;
1929 isl_union_set **res = user;
1930 int wrapping;
1932 wrapping = isl_set_is_wrapping(set);
1933 if (wrapping < 0)
1934 return isl_stat_error;
1935 if (!wrapping)
1936 return isl_stat_ok;
1938 *res = isl_union_map_add_map(*res,
1939 isl_set_wrapped_domain_map(isl_set_copy(set)));
1941 return isl_stat_ok;
1944 /* Given a collection of wrapped maps of the form A[B -> C],
1945 * return the collection of maps A[B -> C] -> B.
1947 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1948 __isl_take isl_union_set *uset)
1950 return cond_un_op(uset, &wrapped_domain_map_entry);
1953 static isl_stat deltas_entry(void **entry, void *user)
1955 isl_map *map = *entry;
1956 isl_union_set **res = user;
1958 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1959 map->dim, isl_dim_out))
1960 return isl_stat_ok;
1962 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1964 return isl_stat_ok;
1967 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1969 return cond_un_op(umap, &deltas_entry);
1972 static isl_stat deltas_map_entry(void **entry, void *user)
1974 isl_map *map = *entry;
1975 isl_union_map **res = user;
1977 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1978 map->dim, isl_dim_out))
1979 return isl_stat_ok;
1981 *res = isl_union_map_add_map(*res,
1982 isl_map_deltas_map(isl_map_copy(map)));
1984 return isl_stat_ok;
1987 __isl_give isl_union_map *isl_union_map_deltas_map(
1988 __isl_take isl_union_map *umap)
1990 return cond_un_op(umap, &deltas_map_entry);
1993 static isl_stat identity_entry(void **entry, void *user)
1995 isl_set *set = *entry;
1996 isl_union_map **res = user;
1998 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
2000 return isl_stat_ok;
2003 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2005 return cond_un_op(uset, &identity_entry);
2008 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2010 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2012 isl_union_pw_multi_aff **res = user;
2013 isl_space *space;
2014 isl_pw_multi_aff *pma;
2016 space = isl_space_map_from_set(isl_set_get_space(set));
2017 pma = isl_pw_multi_aff_identity(space);
2018 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2019 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2021 return *res ? isl_stat_ok : isl_stat_error;
2024 /* Return an identity function on "uset" in the form
2025 * of an isl_union_pw_multi_aff.
2027 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2028 __isl_take isl_union_set *uset)
2030 isl_union_pw_multi_aff *res;
2032 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2033 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2034 res = isl_union_pw_multi_aff_free(res);
2036 isl_union_set_free(uset);
2037 return res;
2040 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
2042 static isl_stat domain_factor_domain_entry(void **entry, void *user)
2044 isl_map *map = *entry;
2045 isl_union_map **res = user;
2047 if (!isl_map_domain_is_wrapping(map))
2048 return isl_stat_ok;
2050 *res = isl_union_map_add_map(*res,
2051 isl_map_domain_factor_domain(isl_map_copy(map)));
2053 return *res ? isl_stat_ok : isl_stat_error;
2056 /* For each map in "umap" of the form [A -> B] -> C,
2057 * construct the map A -> C and collect the results.
2059 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2060 __isl_take isl_union_map *umap)
2062 return cond_un_op(umap, &domain_factor_domain_entry);
2065 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2067 static isl_stat domain_factor_range_entry(void **entry, void *user)
2069 isl_map *map = *entry;
2070 isl_union_map **res = user;
2072 if (!isl_map_domain_is_wrapping(map))
2073 return isl_stat_ok;
2075 *res = isl_union_map_add_map(*res,
2076 isl_map_domain_factor_range(isl_map_copy(map)));
2078 return *res ? isl_stat_ok : isl_stat_error;
2081 /* For each map in "umap" of the form [A -> B] -> C,
2082 * construct the map B -> C and collect the results.
2084 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2085 __isl_take isl_union_map *umap)
2087 return cond_un_op(umap, &domain_factor_range_entry);
2090 /* If "map" is of the form A -> [B -> C], then add A -> B to "res".
2092 static isl_stat range_factor_domain_entry(void **entry, void *user)
2094 isl_map *map = *entry;
2095 isl_union_map **res = user;
2097 if (!isl_map_range_is_wrapping(map))
2098 return isl_stat_ok;
2100 *res = isl_union_map_add_map(*res,
2101 isl_map_range_factor_domain(isl_map_copy(map)));
2103 return *res ? isl_stat_ok : isl_stat_error;
2106 /* For each map in "umap" of the form A -> [B -> C],
2107 * construct the map A -> B and collect the results.
2109 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2110 __isl_take isl_union_map *umap)
2112 return cond_un_op(umap, &range_factor_domain_entry);
2115 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2117 static isl_stat range_factor_range_entry(void **entry, void *user)
2119 isl_map *map = *entry;
2120 isl_union_map **res = user;
2122 if (!isl_map_range_is_wrapping(map))
2123 return isl_stat_ok;
2125 *res = isl_union_map_add_map(*res,
2126 isl_map_range_factor_range(isl_map_copy(map)));
2128 return *res ? isl_stat_ok : isl_stat_error;
2131 /* For each map in "umap" of the form A -> [B -> C],
2132 * construct the map A -> C and collect the results.
2134 __isl_give isl_union_map *isl_union_map_range_factor_range(
2135 __isl_take isl_union_map *umap)
2137 return cond_un_op(umap, &range_factor_range_entry);
2140 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2142 static isl_stat factor_domain_entry(void **entry, void *user)
2144 isl_map *map = *entry;
2145 isl_union_map **res = user;
2147 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2148 return isl_stat_ok;
2150 *res = isl_union_map_add_map(*res,
2151 isl_map_factor_domain(isl_map_copy(map)));
2153 return *res ? isl_stat_ok : isl_stat_error;
2156 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2157 * construct the map A -> C and collect the results.
2159 __isl_give isl_union_map *isl_union_map_factor_domain(
2160 __isl_take isl_union_map *umap)
2162 return cond_un_op(umap, &factor_domain_entry);
2165 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2167 static isl_stat factor_range_entry(void **entry, void *user)
2169 isl_map *map = *entry;
2170 isl_union_map **res = user;
2172 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2173 return isl_stat_ok;
2175 *res = isl_union_map_add_map(*res,
2176 isl_map_factor_range(isl_map_copy(map)));
2178 return *res ? isl_stat_ok : isl_stat_error;
2181 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2182 * construct the map B -> D and collect the results.
2184 __isl_give isl_union_map *isl_union_map_factor_range(
2185 __isl_take isl_union_map *umap)
2187 return cond_un_op(umap, &factor_range_entry);
2190 static isl_stat unwrap_entry(void **entry, void *user)
2192 isl_set *set = *entry;
2193 isl_union_set **res = user;
2195 if (!isl_set_is_wrapping(set))
2196 return isl_stat_ok;
2198 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
2200 return isl_stat_ok;
2203 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2205 return cond_un_op(uset, &unwrap_entry);
2208 static isl_stat wrap_entry(void **entry, void *user)
2210 isl_map *map = *entry;
2211 isl_union_set **res = user;
2213 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2215 return isl_stat_ok;
2218 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2220 return cond_un_op(umap, &wrap_entry);
2223 struct isl_union_map_is_subset_data {
2224 isl_union_map *umap2;
2225 isl_bool is_subset;
2228 static isl_stat is_subset_entry(void **entry, void *user)
2230 struct isl_union_map_is_subset_data *data = user;
2231 uint32_t hash;
2232 struct isl_hash_table_entry *entry2;
2233 isl_map *map = *entry;
2235 hash = isl_space_get_hash(map->dim);
2236 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2237 hash, &has_dim, map->dim, 0);
2238 if (!entry2) {
2239 int empty = isl_map_is_empty(map);
2240 if (empty < 0)
2241 return isl_stat_error;
2242 if (empty)
2243 return isl_stat_ok;
2244 data->is_subset = 0;
2245 return isl_stat_error;
2248 data->is_subset = isl_map_is_subset(map, entry2->data);
2249 if (data->is_subset < 0 || !data->is_subset)
2250 return isl_stat_error;
2252 return isl_stat_ok;
2255 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2256 __isl_keep isl_union_map *umap2)
2258 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2260 umap1 = isl_union_map_copy(umap1);
2261 umap2 = isl_union_map_copy(umap2);
2262 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2263 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2265 if (!umap1 || !umap2)
2266 goto error;
2268 data.umap2 = umap2;
2269 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2270 &is_subset_entry, &data) < 0 &&
2271 data.is_subset)
2272 goto error;
2274 isl_union_map_free(umap1);
2275 isl_union_map_free(umap2);
2277 return data.is_subset;
2278 error:
2279 isl_union_map_free(umap1);
2280 isl_union_map_free(umap2);
2281 return isl_bool_error;
2284 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2285 __isl_keep isl_union_set *uset2)
2287 return isl_union_map_is_subset(uset1, uset2);
2290 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2291 __isl_keep isl_union_map *umap2)
2293 isl_bool is_subset;
2295 if (!umap1 || !umap2)
2296 return isl_bool_error;
2297 is_subset = isl_union_map_is_subset(umap1, umap2);
2298 if (is_subset != isl_bool_true)
2299 return is_subset;
2300 is_subset = isl_union_map_is_subset(umap2, umap1);
2301 return is_subset;
2304 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2305 __isl_keep isl_union_set *uset2)
2307 return isl_union_map_is_equal(uset1, uset2);
2310 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2311 __isl_keep isl_union_map *umap2)
2313 isl_bool is_subset;
2315 if (!umap1 || !umap2)
2316 return isl_bool_error;
2317 is_subset = isl_union_map_is_subset(umap1, umap2);
2318 if (is_subset != isl_bool_true)
2319 return is_subset;
2320 is_subset = isl_union_map_is_subset(umap2, umap1);
2321 if (is_subset == isl_bool_error)
2322 return is_subset;
2323 return !is_subset;
2326 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2327 __isl_keep isl_union_set *uset2)
2329 return isl_union_map_is_strict_subset(uset1, uset2);
2332 /* Internal data structure for isl_union_map_is_disjoint.
2333 * umap2 is the union map with which we are comparing.
2334 * is_disjoint is initialized to 1 and is set to 0 as soon
2335 * as the union maps turn out not to be disjoint.
2337 struct isl_union_map_is_disjoint_data {
2338 isl_union_map *umap2;
2339 isl_bool is_disjoint;
2342 /* Check if "map" is disjoint from data->umap2 and abort
2343 * the search if it is not.
2345 static isl_stat is_disjoint_entry(void **entry, void *user)
2347 struct isl_union_map_is_disjoint_data *data = user;
2348 uint32_t hash;
2349 struct isl_hash_table_entry *entry2;
2350 isl_map *map = *entry;
2352 hash = isl_space_get_hash(map->dim);
2353 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2354 hash, &has_dim, map->dim, 0);
2355 if (!entry2)
2356 return isl_stat_ok;
2358 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2359 if (data->is_disjoint < 0 || !data->is_disjoint)
2360 return isl_stat_error;
2362 return isl_stat_ok;
2365 /* Are "umap1" and "umap2" disjoint?
2367 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2368 __isl_keep isl_union_map *umap2)
2370 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2372 umap1 = isl_union_map_copy(umap1);
2373 umap2 = isl_union_map_copy(umap2);
2374 umap1 = isl_union_map_align_params(umap1,
2375 isl_union_map_get_space(umap2));
2376 umap2 = isl_union_map_align_params(umap2,
2377 isl_union_map_get_space(umap1));
2379 if (!umap1 || !umap2)
2380 goto error;
2382 data.umap2 = umap2;
2383 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2384 &is_disjoint_entry, &data) < 0 &&
2385 data.is_disjoint)
2386 goto error;
2388 isl_union_map_free(umap1);
2389 isl_union_map_free(umap2);
2391 return data.is_disjoint;
2392 error:
2393 isl_union_map_free(umap1);
2394 isl_union_map_free(umap2);
2395 return isl_bool_error;
2398 /* Are "uset1" and "uset2" disjoint?
2400 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2401 __isl_keep isl_union_set *uset2)
2403 return isl_union_map_is_disjoint(uset1, uset2);
2406 static isl_stat sample_entry(void **entry, void *user)
2408 isl_basic_map **sample = (isl_basic_map **)user;
2409 isl_map *map = *entry;
2411 *sample = isl_map_sample(isl_map_copy(map));
2412 if (!*sample)
2413 return isl_stat_error;
2414 if (!isl_basic_map_plain_is_empty(*sample))
2415 return isl_stat_error;
2416 return isl_stat_ok;
2419 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2421 isl_basic_map *sample = NULL;
2423 if (!umap)
2424 return NULL;
2426 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2427 &sample_entry, &sample) < 0 &&
2428 !sample)
2429 goto error;
2431 if (!sample)
2432 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2434 isl_union_map_free(umap);
2436 return sample;
2437 error:
2438 isl_union_map_free(umap);
2439 return NULL;
2442 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2444 return (isl_basic_set *)isl_union_map_sample(uset);
2447 /* Return an element in "uset" in the form of an isl_point.
2448 * Return a void isl_point if "uset" is empty.
2450 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2452 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2455 struct isl_forall_data {
2456 isl_bool res;
2457 isl_bool (*fn)(__isl_keep isl_map *map);
2460 static isl_stat forall_entry(void **entry, void *user)
2462 struct isl_forall_data *data = user;
2463 isl_map *map = *entry;
2465 data->res = data->fn(map);
2466 if (data->res < 0)
2467 return isl_stat_error;
2469 if (!data->res)
2470 return isl_stat_error;
2472 return isl_stat_ok;
2475 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2476 isl_bool (*fn)(__isl_keep isl_map *map))
2478 struct isl_forall_data data = { isl_bool_true, fn };
2480 if (!umap)
2481 return isl_bool_error;
2483 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2484 &forall_entry, &data) < 0 && data.res)
2485 return isl_bool_error;
2487 return data.res;
2490 struct isl_forall_user_data {
2491 isl_bool res;
2492 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2493 void *user;
2496 static isl_stat forall_user_entry(void **entry, void *user)
2498 struct isl_forall_user_data *data = user;
2499 isl_map *map = *entry;
2501 data->res = data->fn(map, data->user);
2502 if (data->res < 0)
2503 return isl_stat_error;
2505 if (!data->res)
2506 return isl_stat_error;
2508 return isl_stat_ok;
2511 /* Check if fn(map, user) returns true for all maps "map" in umap.
2513 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2514 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2516 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2518 if (!umap)
2519 return isl_bool_error;
2521 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2522 &forall_user_entry, &data) < 0 && data.res)
2523 return isl_bool_error;
2525 return data.res;
2528 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2530 return union_map_forall(umap, &isl_map_is_empty);
2533 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2535 return isl_union_map_is_empty(uset);
2538 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2540 isl_bool is_subset;
2541 isl_space *dim;
2542 isl_map *id;
2544 if (!map)
2545 return isl_bool_error;
2547 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2548 map->dim, isl_dim_out))
2549 return isl_bool_false;
2551 dim = isl_map_get_space(map);
2552 id = isl_map_identity(dim);
2554 is_subset = isl_map_is_subset(map, id);
2556 isl_map_free(id);
2558 return is_subset;
2561 /* Given an isl_union_map that consists of a single map, check
2562 * if it is single-valued.
2564 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2566 isl_map *map;
2567 isl_bool sv;
2569 umap = isl_union_map_copy(umap);
2570 map = isl_map_from_union_map(umap);
2571 sv = isl_map_is_single_valued(map);
2572 isl_map_free(map);
2574 return sv;
2577 /* Internal data structure for single_valued_on_domain.
2579 * "umap" is the union map to be tested.
2580 * "sv" is set to 1 as long as "umap" may still be single-valued.
2582 struct isl_union_map_is_sv_data {
2583 isl_union_map *umap;
2584 isl_bool sv;
2587 /* Check if the data->umap is single-valued on "set".
2589 * If data->umap consists of a single map on "set", then test it
2590 * as an isl_map.
2592 * Otherwise, compute
2594 * M \circ M^-1
2596 * check if the result is a subset of the identity mapping and
2597 * store the result in data->sv.
2599 * Terminate as soon as data->umap has been determined not to
2600 * be single-valued.
2602 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2604 struct isl_union_map_is_sv_data *data = user;
2605 isl_union_map *umap, *test;
2607 umap = isl_union_map_copy(data->umap);
2608 umap = isl_union_map_intersect_domain(umap,
2609 isl_union_set_from_set(set));
2611 if (isl_union_map_n_map(umap) == 1) {
2612 data->sv = single_map_is_single_valued(umap);
2613 isl_union_map_free(umap);
2614 } else {
2615 test = isl_union_map_reverse(isl_union_map_copy(umap));
2616 test = isl_union_map_apply_range(test, umap);
2618 data->sv = union_map_forall(test, &is_subset_of_identity);
2620 isl_union_map_free(test);
2623 if (data->sv < 0 || !data->sv)
2624 return isl_stat_error;
2625 return isl_stat_ok;
2628 /* Check if the given map is single-valued.
2630 * If the union map consists of a single map, then test it as an isl_map.
2631 * Otherwise, check if the union map is single-valued on each of its
2632 * domain spaces.
2634 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2636 isl_union_map *universe;
2637 isl_union_set *domain;
2638 struct isl_union_map_is_sv_data data;
2640 if (isl_union_map_n_map(umap) == 1)
2641 return single_map_is_single_valued(umap);
2643 universe = isl_union_map_universe(isl_union_map_copy(umap));
2644 domain = isl_union_map_domain(universe);
2646 data.sv = isl_bool_true;
2647 data.umap = umap;
2648 if (isl_union_set_foreach_set(domain,
2649 &single_valued_on_domain, &data) < 0 && data.sv)
2650 data.sv = isl_bool_error;
2651 isl_union_set_free(domain);
2653 return data.sv;
2656 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2658 isl_bool in;
2660 umap = isl_union_map_copy(umap);
2661 umap = isl_union_map_reverse(umap);
2662 in = isl_union_map_is_single_valued(umap);
2663 isl_union_map_free(umap);
2665 return in;
2668 /* Represents a map that has a fixed value (v) for one of its
2669 * range dimensions.
2670 * The map in this structure is not reference counted, so it
2671 * is only valid while the isl_union_map from which it was
2672 * obtained is still alive.
2674 struct isl_fixed_map {
2675 isl_int v;
2676 isl_map *map;
2679 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2680 int n)
2682 int i;
2683 struct isl_fixed_map *v;
2685 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2686 if (!v)
2687 return NULL;
2688 for (i = 0; i < n; ++i)
2689 isl_int_init(v[i].v);
2690 return v;
2693 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2695 int i;
2697 if (!v)
2698 return;
2699 for (i = 0; i < n; ++i)
2700 isl_int_clear(v[i].v);
2701 free(v);
2704 /* Compare the "v" field of two isl_fixed_map structs.
2706 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2708 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2709 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2711 return isl_int_cmp(e1->v, e2->v);
2714 /* Internal data structure used while checking whether all maps
2715 * in a union_map have a fixed value for a given output dimension.
2716 * v is the list of maps, with the fixed value for the dimension
2717 * n is the number of maps considered so far
2718 * pos is the output dimension under investigation
2720 struct isl_fixed_dim_data {
2721 struct isl_fixed_map *v;
2722 int n;
2723 int pos;
2726 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2728 struct isl_fixed_dim_data *data = user;
2730 data->v[data->n].map = map;
2731 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2732 &data->v[data->n++].v);
2735 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2736 int first, int n_range);
2738 /* Given a list of the maps, with their fixed values at output dimension "pos",
2739 * check whether the ranges of the maps form an obvious partition.
2741 * We first sort the maps according to their fixed values.
2742 * If all maps have a different value, then we know the ranges form
2743 * a partition.
2744 * Otherwise, we collect the maps with the same fixed value and
2745 * check whether each such collection is obviously injective
2746 * based on later dimensions.
2748 static int separates(struct isl_fixed_map *v, int n,
2749 __isl_take isl_space *dim, int pos, int n_range)
2751 int i;
2753 if (!v)
2754 goto error;
2756 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2758 for (i = 0; i + 1 < n; ++i) {
2759 int j, k;
2760 isl_union_map *part;
2761 int injective;
2763 for (j = i + 1; j < n; ++j)
2764 if (isl_int_ne(v[i].v, v[j].v))
2765 break;
2767 if (j == i + 1)
2768 continue;
2770 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2771 for (k = i; k < j; ++k)
2772 part = isl_union_map_add_map(part,
2773 isl_map_copy(v[k].map));
2775 injective = plain_injective_on_range(part, pos + 1, n_range);
2776 if (injective < 0)
2777 goto error;
2778 if (!injective)
2779 break;
2781 i = j - 1;
2784 isl_space_free(dim);
2785 free_isl_fixed_map_array(v, n);
2786 return i + 1 >= n;
2787 error:
2788 isl_space_free(dim);
2789 free_isl_fixed_map_array(v, n);
2790 return -1;
2793 /* Check whether the maps in umap have obviously distinct ranges.
2794 * In particular, check for an output dimension in the range
2795 * [first,n_range) for which all maps have a fixed value
2796 * and then check if these values, possibly along with fixed values
2797 * at later dimensions, entail distinct ranges.
2799 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2800 int first, int n_range)
2802 isl_ctx *ctx;
2803 int n;
2804 struct isl_fixed_dim_data data = { NULL };
2806 ctx = isl_union_map_get_ctx(umap);
2808 n = isl_union_map_n_map(umap);
2809 if (!umap)
2810 goto error;
2812 if (n <= 1) {
2813 isl_union_map_free(umap);
2814 return isl_bool_true;
2817 if (first >= n_range) {
2818 isl_union_map_free(umap);
2819 return isl_bool_false;
2822 data.v = alloc_isl_fixed_map_array(ctx, n);
2823 if (!data.v)
2824 goto error;
2826 for (data.pos = first; data.pos < n_range; ++data.pos) {
2827 isl_bool fixed;
2828 int injective;
2829 isl_space *dim;
2831 data.n = 0;
2832 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2833 if (fixed < 0)
2834 goto error;
2835 if (!fixed)
2836 continue;
2837 dim = isl_union_map_get_space(umap);
2838 injective = separates(data.v, n, dim, data.pos, n_range);
2839 isl_union_map_free(umap);
2840 return injective;
2843 free_isl_fixed_map_array(data.v, n);
2844 isl_union_map_free(umap);
2846 return isl_bool_false;
2847 error:
2848 free_isl_fixed_map_array(data.v, n);
2849 isl_union_map_free(umap);
2850 return isl_bool_error;
2853 /* Check whether the maps in umap that map to subsets of "ran"
2854 * have obviously distinct ranges.
2856 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2857 void *user)
2859 isl_union_map *umap = user;
2861 umap = isl_union_map_copy(umap);
2862 umap = isl_union_map_intersect_range(umap,
2863 isl_union_set_from_set(isl_set_copy(ran)));
2864 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2867 /* Check if the given union_map is obviously injective.
2869 * In particular, we first check if all individual maps are obviously
2870 * injective and then check if all the ranges of these maps are
2871 * obviously disjoint.
2873 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2875 isl_bool in;
2876 isl_union_map *univ;
2877 isl_union_set *ran;
2879 in = union_map_forall(umap, &isl_map_plain_is_injective);
2880 if (in < 0)
2881 return isl_bool_error;
2882 if (!in)
2883 return isl_bool_false;
2885 univ = isl_union_map_universe(isl_union_map_copy(umap));
2886 ran = isl_union_map_range(univ);
2888 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2890 isl_union_set_free(ran);
2892 return in;
2895 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2897 isl_bool sv;
2899 sv = isl_union_map_is_single_valued(umap);
2900 if (sv < 0 || !sv)
2901 return sv;
2903 return isl_union_map_is_injective(umap);
2906 static isl_stat zip_entry(void **entry, void *user)
2908 isl_map *map = *entry;
2909 isl_union_map **res = user;
2911 if (!isl_map_can_zip(map))
2912 return isl_stat_ok;
2914 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2916 return isl_stat_ok;
2919 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2921 return cond_un_op(umap, &zip_entry);
2924 static isl_stat uncurry_entry(void **entry, void *user)
2926 isl_map *map = *entry;
2927 isl_union_map **res = user;
2929 if (!isl_map_can_uncurry(map))
2930 return isl_stat_ok;
2932 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
2934 return isl_stat_ok;
2937 /* Given a union map, take the maps of the form A -> (B -> C) and
2938 * return the union of the corresponding maps (A -> B) -> C.
2940 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2942 return cond_un_op(umap, &uncurry_entry);
2945 static isl_stat curry_entry(void **entry, void *user)
2947 isl_map *map = *entry;
2948 isl_union_map **res = user;
2950 if (!isl_map_can_curry(map))
2951 return isl_stat_ok;
2953 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2955 return isl_stat_ok;
2958 /* Given a union map, take the maps of the form (A -> B) -> C and
2959 * return the union of the corresponding maps A -> (B -> C).
2961 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2963 return cond_un_op(umap, &curry_entry);
2966 /* If *entry is of the form A -> ((B -> C) -> D), then apply
2967 * isl_map_range_curry to it and add the result to *res.
2969 static isl_stat range_curry_entry(void **entry, void *user)
2971 isl_map *map = *entry;
2972 isl_union_map **res = user;
2974 if (!isl_map_can_range_curry(map))
2975 return isl_stat_ok;
2977 map = isl_map_range_curry(isl_map_copy(map));
2978 *res = isl_union_map_add_map(*res, map);
2980 return isl_stat_ok;
2983 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2984 * return the union of the corresponding maps A -> (B -> (C -> D)).
2986 __isl_give isl_union_map *isl_union_map_range_curry(
2987 __isl_take isl_union_map *umap)
2989 return cond_un_op(umap, &range_curry_entry);
2992 static isl_stat lift_entry(void **entry, void *user)
2994 isl_set *set = *entry;
2995 isl_union_set **res = user;
2997 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2999 return isl_stat_ok;
3002 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3004 return cond_un_op(uset, &lift_entry);
3007 static isl_stat coefficients_entry(void **entry, void *user)
3009 isl_set *set = *entry;
3010 isl_union_set **res = user;
3012 set = isl_set_copy(set);
3013 set = isl_set_from_basic_set(isl_set_coefficients(set));
3014 *res = isl_union_set_add_set(*res, set);
3016 return isl_stat_ok;
3019 __isl_give isl_union_set *isl_union_set_coefficients(
3020 __isl_take isl_union_set *uset)
3022 isl_ctx *ctx;
3023 isl_space *dim;
3024 isl_union_set *res;
3026 if (!uset)
3027 return NULL;
3029 ctx = isl_union_set_get_ctx(uset);
3030 dim = isl_space_set_alloc(ctx, 0, 0);
3031 res = isl_union_map_alloc(dim, uset->table.n);
3032 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3033 &coefficients_entry, &res) < 0)
3034 goto error;
3036 isl_union_set_free(uset);
3037 return res;
3038 error:
3039 isl_union_set_free(uset);
3040 isl_union_set_free(res);
3041 return NULL;
3044 static isl_stat solutions_entry(void **entry, void *user)
3046 isl_set *set = *entry;
3047 isl_union_set **res = user;
3049 set = isl_set_copy(set);
3050 set = isl_set_from_basic_set(isl_set_solutions(set));
3051 if (!*res)
3052 *res = isl_union_set_from_set(set);
3053 else
3054 *res = isl_union_set_add_set(*res, set);
3056 if (!*res)
3057 return isl_stat_error;
3059 return isl_stat_ok;
3062 __isl_give isl_union_set *isl_union_set_solutions(
3063 __isl_take isl_union_set *uset)
3065 isl_union_set *res = NULL;
3067 if (!uset)
3068 return NULL;
3070 if (uset->table.n == 0) {
3071 res = isl_union_set_empty(isl_union_set_get_space(uset));
3072 isl_union_set_free(uset);
3073 return res;
3076 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3077 &solutions_entry, &res) < 0)
3078 goto error;
3080 isl_union_set_free(uset);
3081 return res;
3082 error:
3083 isl_union_set_free(uset);
3084 isl_union_set_free(res);
3085 return NULL;
3088 /* Is the domain space of "map" equal to "space"?
3090 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3092 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3093 space, isl_dim_out);
3096 /* Is the range space of "map" equal to "space"?
3098 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3100 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3101 space, isl_dim_out);
3104 /* Is the set space of "map" equal to "space"?
3106 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3108 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3109 space, isl_dim_out);
3112 /* Internal data structure for preimage_pw_multi_aff.
3114 * "pma" is the function under which the preimage should be taken.
3115 * "space" is the space of "pma".
3116 * "res" collects the results.
3117 * "fn" computes the preimage for a given map.
3118 * "match" returns true if "fn" can be called.
3120 struct isl_union_map_preimage_data {
3121 isl_space *space;
3122 isl_pw_multi_aff *pma;
3123 isl_union_map *res;
3124 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3125 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3126 __isl_take isl_pw_multi_aff *pma);
3129 /* Call data->fn to compute the preimage of the domain or range of *entry
3130 * under the function represented by data->pma, provided the domain/range
3131 * space of *entry matches the target space of data->pma
3132 * (as given by data->match), and add the result to data->res.
3134 static isl_stat preimage_entry(void **entry, void *user)
3136 int m;
3137 isl_map *map = *entry;
3138 struct isl_union_map_preimage_data *data = user;
3139 isl_bool empty;
3141 m = data->match(map, data->space);
3142 if (m < 0)
3143 return isl_stat_error;
3144 if (!m)
3145 return isl_stat_ok;
3147 map = isl_map_copy(map);
3148 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3150 empty = isl_map_is_empty(map);
3151 if (empty < 0 || empty) {
3152 isl_map_free(map);
3153 return empty < 0 ? isl_stat_error : isl_stat_ok;
3156 data->res = isl_union_map_add_map(data->res, map);
3158 return isl_stat_ok;
3161 /* Compute the preimage of the domain or range of "umap" under the function
3162 * represented by "pma".
3163 * In other words, plug in "pma" in the domain or range of "umap".
3164 * The function "fn" performs the actual preimage computation on a map,
3165 * while "match" determines to which maps the function should be applied.
3167 static __isl_give isl_union_map *preimage_pw_multi_aff(
3168 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3169 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3170 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3171 __isl_take isl_pw_multi_aff *pma))
3173 isl_ctx *ctx;
3174 isl_space *space;
3175 struct isl_union_map_preimage_data data;
3177 umap = isl_union_map_align_params(umap,
3178 isl_pw_multi_aff_get_space(pma));
3179 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3181 if (!umap || !pma)
3182 goto error;
3184 ctx = isl_union_map_get_ctx(umap);
3185 space = isl_union_map_get_space(umap);
3186 data.space = isl_pw_multi_aff_get_space(pma);
3187 data.pma = pma;
3188 data.res = isl_union_map_alloc(space, umap->table.n);
3189 data.match = match;
3190 data.fn = fn;
3191 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3192 &data) < 0)
3193 data.res = isl_union_map_free(data.res);
3195 isl_space_free(data.space);
3196 isl_union_map_free(umap);
3197 isl_pw_multi_aff_free(pma);
3198 return data.res;
3199 error:
3200 isl_union_map_free(umap);
3201 isl_pw_multi_aff_free(pma);
3202 return NULL;
3205 /* Compute the preimage of the domain of "umap" under the function
3206 * represented by "pma".
3207 * In other words, plug in "pma" in the domain of "umap".
3208 * The result contains maps that live in the same spaces as the maps of "umap"
3209 * with domain space equal to the target space of "pma",
3210 * except that the domain has been replaced by the domain space of "pma".
3212 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3213 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3215 return preimage_pw_multi_aff(umap, pma, &domain_match,
3216 &isl_map_preimage_domain_pw_multi_aff);
3219 /* Compute the preimage of the range of "umap" under the function
3220 * represented by "pma".
3221 * In other words, plug in "pma" in the range of "umap".
3222 * The result contains maps that live in the same spaces as the maps of "umap"
3223 * with range space equal to the target space of "pma",
3224 * except that the range has been replaced by the domain space of "pma".
3226 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3227 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3229 return preimage_pw_multi_aff(umap, pma, &range_match,
3230 &isl_map_preimage_range_pw_multi_aff);
3233 /* Compute the preimage of "uset" under the function represented by "pma".
3234 * In other words, plug in "pma" in "uset".
3235 * The result contains sets that live in the same spaces as the sets of "uset"
3236 * with space equal to the target space of "pma",
3237 * except that the space has been replaced by the domain space of "pma".
3239 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3240 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3242 return preimage_pw_multi_aff(uset, pma, &set_match,
3243 &isl_set_preimage_pw_multi_aff);
3246 /* Compute the preimage of the domain of "umap" under the function
3247 * represented by "ma".
3248 * In other words, plug in "ma" in the domain of "umap".
3249 * The result contains maps that live in the same spaces as the maps of "umap"
3250 * with domain space equal to the target space of "ma",
3251 * except that the domain has been replaced by the domain space of "ma".
3253 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3254 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3256 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3257 isl_pw_multi_aff_from_multi_aff(ma));
3260 /* Compute the preimage of the range of "umap" under the function
3261 * represented by "ma".
3262 * In other words, plug in "ma" in the range of "umap".
3263 * The result contains maps that live in the same spaces as the maps of "umap"
3264 * with range space equal to the target space of "ma",
3265 * except that the range has been replaced by the domain space of "ma".
3267 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3268 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3270 return isl_union_map_preimage_range_pw_multi_aff(umap,
3271 isl_pw_multi_aff_from_multi_aff(ma));
3274 /* Compute the preimage of "uset" under the function represented by "ma".
3275 * In other words, plug in "ma" in "uset".
3276 * The result contains sets that live in the same spaces as the sets of "uset"
3277 * with space equal to the target space of "ma",
3278 * except that the space has been replaced by the domain space of "ma".
3280 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3281 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3283 return isl_union_set_preimage_pw_multi_aff(uset,
3284 isl_pw_multi_aff_from_multi_aff(ma));
3287 /* Internal data structure for preimage_multi_pw_aff.
3289 * "mpa" is the function under which the preimage should be taken.
3290 * "space" is the space of "mpa".
3291 * "res" collects the results.
3292 * "fn" computes the preimage for a given map.
3293 * "match" returns true if "fn" can be called.
3295 struct isl_union_map_preimage_mpa_data {
3296 isl_space *space;
3297 isl_multi_pw_aff *mpa;
3298 isl_union_map *res;
3299 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3300 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3301 __isl_take isl_multi_pw_aff *mpa);
3304 /* Call data->fn to compute the preimage of the domain or range of *entry
3305 * under the function represented by data->mpa, provided the domain/range
3306 * space of *entry matches the target space of data->mpa
3307 * (as given by data->match), and add the result to data->res.
3309 static isl_stat preimage_mpa_entry(void **entry, void *user)
3311 int m;
3312 isl_map *map = *entry;
3313 struct isl_union_map_preimage_mpa_data *data = user;
3314 isl_bool empty;
3316 m = data->match(map, data->space);
3317 if (m < 0)
3318 return isl_stat_error;
3319 if (!m)
3320 return isl_stat_ok;
3322 map = isl_map_copy(map);
3323 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3325 empty = isl_map_is_empty(map);
3326 if (empty < 0 || empty) {
3327 isl_map_free(map);
3328 return empty < 0 ? isl_stat_error : isl_stat_ok;
3331 data->res = isl_union_map_add_map(data->res, map);
3333 return isl_stat_ok;
3336 /* Compute the preimage of the domain or range of "umap" under the function
3337 * represented by "mpa".
3338 * In other words, plug in "mpa" in the domain or range of "umap".
3339 * The function "fn" performs the actual preimage computation on a map,
3340 * while "match" determines to which maps the function should be applied.
3342 static __isl_give isl_union_map *preimage_multi_pw_aff(
3343 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3344 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3345 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3346 __isl_take isl_multi_pw_aff *mpa))
3348 isl_ctx *ctx;
3349 isl_space *space;
3350 struct isl_union_map_preimage_mpa_data data;
3352 umap = isl_union_map_align_params(umap,
3353 isl_multi_pw_aff_get_space(mpa));
3354 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3356 if (!umap || !mpa)
3357 goto error;
3359 ctx = isl_union_map_get_ctx(umap);
3360 space = isl_union_map_get_space(umap);
3361 data.space = isl_multi_pw_aff_get_space(mpa);
3362 data.mpa = mpa;
3363 data.res = isl_union_map_alloc(space, umap->table.n);
3364 data.match = match;
3365 data.fn = fn;
3366 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3367 &data) < 0)
3368 data.res = isl_union_map_free(data.res);
3370 isl_space_free(data.space);
3371 isl_union_map_free(umap);
3372 isl_multi_pw_aff_free(mpa);
3373 return data.res;
3374 error:
3375 isl_union_map_free(umap);
3376 isl_multi_pw_aff_free(mpa);
3377 return NULL;
3380 /* Compute the preimage of the domain of "umap" under the function
3381 * represented by "mpa".
3382 * In other words, plug in "mpa" in the domain of "umap".
3383 * The result contains maps that live in the same spaces as the maps of "umap"
3384 * with domain space equal to the target space of "mpa",
3385 * except that the domain has been replaced by the domain space of "mpa".
3387 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3388 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3390 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3391 &isl_map_preimage_domain_multi_pw_aff);
3394 /* Internal data structure for preimage_upma.
3396 * "umap" is the map of which the preimage should be computed.
3397 * "res" collects the results.
3398 * "fn" computes the preimage for a given piecewise multi-affine function.
3400 struct isl_union_map_preimage_upma_data {
3401 isl_union_map *umap;
3402 isl_union_map *res;
3403 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3404 __isl_take isl_pw_multi_aff *pma);
3407 /* Call data->fn to compute the preimage of the domain or range of data->umap
3408 * under the function represented by pma and add the result to data->res.
3410 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3412 struct isl_union_map_preimage_upma_data *data = user;
3413 isl_union_map *umap;
3415 umap = isl_union_map_copy(data->umap);
3416 umap = data->fn(umap, pma);
3417 data->res = isl_union_map_union(data->res, umap);
3419 return data->res ? isl_stat_ok : isl_stat_error;
3422 /* Compute the preimage of the domain or range of "umap" under the function
3423 * represented by "upma".
3424 * In other words, plug in "upma" in the domain or range of "umap".
3425 * The function "fn" performs the actual preimage computation
3426 * on a piecewise multi-affine function.
3428 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3429 __isl_take isl_union_map *umap,
3430 __isl_take isl_union_pw_multi_aff *upma,
3431 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3432 __isl_take isl_pw_multi_aff *pma))
3434 struct isl_union_map_preimage_upma_data data;
3436 data.umap = umap;
3437 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3438 data.fn = fn;
3439 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3440 &preimage_upma, &data) < 0)
3441 data.res = isl_union_map_free(data.res);
3443 isl_union_map_free(umap);
3444 isl_union_pw_multi_aff_free(upma);
3446 return data.res;
3449 /* Compute the preimage of the domain of "umap" under the function
3450 * represented by "upma".
3451 * In other words, plug in "upma" in the domain of "umap".
3452 * The result contains maps that live in the same spaces as the maps of "umap"
3453 * with domain space equal to one of the target spaces of "upma",
3454 * except that the domain has been replaced by one of the the domain spaces that
3455 * corresponds to that target space of "upma".
3457 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3458 __isl_take isl_union_map *umap,
3459 __isl_take isl_union_pw_multi_aff *upma)
3461 return preimage_union_pw_multi_aff(umap, upma,
3462 &isl_union_map_preimage_domain_pw_multi_aff);
3465 /* Compute the preimage of the range of "umap" under the function
3466 * represented by "upma".
3467 * In other words, plug in "upma" in the range of "umap".
3468 * The result contains maps that live in the same spaces as the maps of "umap"
3469 * with range space equal to one of the target spaces of "upma",
3470 * except that the range has been replaced by one of the the domain spaces that
3471 * corresponds to that target space of "upma".
3473 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3474 __isl_take isl_union_map *umap,
3475 __isl_take isl_union_pw_multi_aff *upma)
3477 return preimage_union_pw_multi_aff(umap, upma,
3478 &isl_union_map_preimage_range_pw_multi_aff);
3481 /* Compute the preimage of "uset" under the function represented by "upma".
3482 * In other words, plug in "upma" in the range of "uset".
3483 * The result contains sets that live in the same spaces as the sets of "uset"
3484 * with space equal to one of the target spaces of "upma",
3485 * except that the space has been replaced by one of the the domain spaces that
3486 * corresponds to that target space of "upma".
3488 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3489 __isl_take isl_union_set *uset,
3490 __isl_take isl_union_pw_multi_aff *upma)
3492 return preimage_union_pw_multi_aff(uset, upma,
3493 &isl_union_set_preimage_pw_multi_aff);
3496 /* Reset the user pointer on all identifiers of parameters and tuples
3497 * of the space of *entry.
3499 static isl_stat reset_user(void **entry, void *user)
3501 isl_map **map = (isl_map **)entry;
3503 *map = isl_map_reset_user(*map);
3505 return *map ? isl_stat_ok : isl_stat_error;
3508 /* Reset the user pointer on all identifiers of parameters and tuples
3509 * of the spaces of "umap".
3511 __isl_give isl_union_map *isl_union_map_reset_user(
3512 __isl_take isl_union_map *umap)
3514 umap = isl_union_map_cow(umap);
3515 if (!umap)
3516 return NULL;
3517 umap->dim = isl_space_reset_user(umap->dim);
3518 if (!umap->dim)
3519 return isl_union_map_free(umap);
3520 umap = un_op(umap, &reset_user);
3522 return umap;
3525 /* Reset the user pointer on all identifiers of parameters and tuples
3526 * of the spaces of "uset".
3528 __isl_give isl_union_set *isl_union_set_reset_user(
3529 __isl_take isl_union_set *uset)
3531 return isl_union_map_reset_user(uset);
3534 /* Internal data structure for isl_union_map_project_out.
3535 * "type", "first" and "n" are the arguments for the isl_map_project_out
3536 * call.
3537 * "res" collects the results.
3539 struct isl_union_map_project_out_data {
3540 enum isl_dim_type type;
3541 unsigned first;
3542 unsigned n;
3544 isl_union_map *res;
3547 /* Turn the data->n dimensions of type data->type, starting at data->first
3548 * into existentially quantified variables and add the result to data->res.
3550 static isl_stat project_out(__isl_take isl_map *map, void *user)
3552 struct isl_union_map_project_out_data *data = user;
3554 map = isl_map_project_out(map, data->type, data->first, data->n);
3555 data->res = isl_union_map_add_map(data->res, map);
3557 return isl_stat_ok;
3560 /* Turn the "n" dimensions of type "type", starting at "first"
3561 * into existentially quantified variables.
3562 * Since the space of an isl_union_map only contains parameters,
3563 * type is required to be equal to isl_dim_param.
3565 __isl_give isl_union_map *isl_union_map_project_out(
3566 __isl_take isl_union_map *umap,
3567 enum isl_dim_type type, unsigned first, unsigned n)
3569 isl_space *space;
3570 struct isl_union_map_project_out_data data = { type, first, n };
3572 if (!umap)
3573 return NULL;
3575 if (type != isl_dim_param)
3576 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3577 "can only project out parameters",
3578 return isl_union_map_free(umap));
3580 space = isl_union_map_get_space(umap);
3581 space = isl_space_drop_dims(space, type, first, n);
3582 data.res = isl_union_map_empty(space);
3583 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3584 data.res = isl_union_map_free(data.res);
3586 isl_union_map_free(umap);
3588 return data.res;
3591 /* Turn the "n" dimensions of type "type", starting at "first"
3592 * into existentially quantified variables.
3593 * Since the space of an isl_union_set only contains parameters,
3594 * "type" is required to be equal to isl_dim_param.
3596 __isl_give isl_union_set *isl_union_set_project_out(
3597 __isl_take isl_union_set *uset,
3598 enum isl_dim_type type, unsigned first, unsigned n)
3600 return isl_union_map_project_out(uset, type, first, n);
3603 /* Internal data structure for isl_union_map_involves_dims.
3604 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3606 struct isl_union_map_involves_dims_data {
3607 unsigned first;
3608 unsigned n;
3611 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3613 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3615 struct isl_union_map_involves_dims_data *data = user;
3616 isl_bool involves;
3618 involves = isl_map_involves_dims(map,
3619 isl_dim_param, data->first, data->n);
3620 if (involves < 0)
3621 return isl_bool_error;
3622 return !involves;
3625 /* Does "umap" involve any of the n parameters starting at first?
3626 * "type" is required to be set to isl_dim_param.
3628 * "umap" involves any of those parameters if any of its maps
3629 * involve the parameters. In other words, "umap" does not
3630 * involve any of the parameters if all its maps to not
3631 * involve the parameters.
3633 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3634 enum isl_dim_type type, unsigned first, unsigned n)
3636 struct isl_union_map_involves_dims_data data = { first, n };
3637 isl_bool excludes;
3639 if (type != isl_dim_param)
3640 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3641 "can only reference parameters", return isl_bool_error);
3643 excludes = union_map_forall_user(umap, &map_excludes, &data);
3645 if (excludes < 0)
3646 return isl_bool_error;
3648 return !excludes;
3651 /* Internal data structure for isl_union_map_reset_range_space.
3652 * "range" is the space from which to set the range space.
3653 * "res" collects the results.
3655 struct isl_union_map_reset_range_space_data {
3656 isl_space *range;
3657 isl_union_map *res;
3660 /* Replace the range space of "map" by the range space of data->range and
3661 * add the result to data->res.
3663 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3665 struct isl_union_map_reset_range_space_data *data = user;
3666 isl_space *space;
3668 space = isl_map_get_space(map);
3669 space = isl_space_domain(space);
3670 space = isl_space_extend_domain_with_range(space,
3671 isl_space_copy(data->range));
3672 map = isl_map_reset_space(map, space);
3673 data->res = isl_union_map_add_map(data->res, map);
3675 return data->res ? isl_stat_ok : isl_stat_error;
3678 /* Replace the range space of all the maps in "umap" by
3679 * the range space of "space".
3681 * This assumes that all maps have the same output dimension.
3682 * This function should therefore not be made publicly available.
3684 * Since the spaces of the maps change, so do their hash value.
3685 * We therefore need to create a new isl_union_map.
3687 __isl_give isl_union_map *isl_union_map_reset_range_space(
3688 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3690 struct isl_union_map_reset_range_space_data data = { space };
3692 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3693 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3694 data.res = isl_union_map_free(data.res);
3696 isl_space_free(space);
3697 isl_union_map_free(umap);
3698 return data.res;
3701 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3702 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3703 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3704 * to the domain and the range of each map.
3705 * "res" collects the results.
3707 struct isl_union_order_at_data {
3708 isl_multi_union_pw_aff *mupa;
3709 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3710 __isl_take isl_multi_pw_aff *mpa2);
3711 isl_union_map *res;
3714 /* Intersect "map" with the result of applying data->order to
3715 * the functions in data->mupa that apply to the domain and the range
3716 * of "map" and add the result to data->res.
3718 static isl_stat order_at(__isl_take isl_map *map, void *user)
3720 struct isl_union_order_at_data *data = user;
3721 isl_space *space;
3722 isl_multi_pw_aff *mpa1, *mpa2;
3723 isl_map *order;
3725 space = isl_space_domain(isl_map_get_space(map));
3726 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3727 space = isl_space_range(isl_map_get_space(map));
3728 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3729 order = data->order(mpa1, mpa2);
3730 map = isl_map_intersect(map, order);
3731 data->res = isl_union_map_add_map(data->res, map);
3733 return data->res ? isl_stat_ok : isl_stat_error;
3736 /* Intersect each map in "umap" with the result of calling "order"
3737 * on the functions is "mupa" that apply to the domain and the range
3738 * of the map.
3740 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3741 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3742 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3743 __isl_take isl_multi_pw_aff *mpa2))
3745 struct isl_union_order_at_data data;
3747 umap = isl_union_map_align_params(umap,
3748 isl_multi_union_pw_aff_get_space(mupa));
3749 mupa = isl_multi_union_pw_aff_align_params(mupa,
3750 isl_union_map_get_space(umap));
3751 data.mupa = mupa;
3752 data.order = order;
3753 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3754 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3755 data.res = isl_union_map_free(data.res);
3757 isl_multi_union_pw_aff_free(mupa);
3758 isl_union_map_free(umap);
3759 return data.res;
3762 /* Return the subset of "umap" where the domain and the range
3763 * have equal "mupa" values.
3765 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3766 __isl_take isl_union_map *umap,
3767 __isl_take isl_multi_union_pw_aff *mupa)
3769 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3770 &isl_multi_pw_aff_eq_map);
3773 /* Return the subset of "umap" where the domain has a lexicographically
3774 * smaller "mupa" value than the range.
3776 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3777 __isl_take isl_union_map *umap,
3778 __isl_take isl_multi_union_pw_aff *mupa)
3780 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3781 &isl_multi_pw_aff_lex_lt_map);
3784 /* Return the subset of "umap" where the domain has a lexicographically
3785 * greater "mupa" value than the range.
3787 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3788 __isl_take isl_union_map *umap,
3789 __isl_take isl_multi_union_pw_aff *mupa)
3791 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3792 &isl_multi_pw_aff_lex_gt_map);
3795 /* Return the union of the elements in the list "list".
3797 __isl_give isl_union_set *isl_union_set_list_union(
3798 __isl_take isl_union_set_list *list)
3800 int i, n;
3801 isl_ctx *ctx;
3802 isl_space *space;
3803 isl_union_set *res;
3805 if (!list)
3806 return NULL;
3808 ctx = isl_union_set_list_get_ctx(list);
3809 space = isl_space_params_alloc(ctx, 0);
3810 res = isl_union_set_empty(space);
3812 n = isl_union_set_list_n_union_set(list);
3813 for (i = 0; i < n; ++i) {
3814 isl_union_set *uset_i;
3816 uset_i = isl_union_set_list_get_union_set(list, i);
3817 res = isl_union_set_union(res, uset_i);
3820 isl_union_set_list_free(list);
3821 return res;
3824 /* Update *hash with the hash value of "map".
3826 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3828 uint32_t *hash = user;
3829 uint32_t map_hash;
3831 map_hash = isl_map_get_hash(map);
3832 isl_hash_hash(*hash, map_hash);
3834 isl_map_free(map);
3835 return isl_stat_ok;
3838 /* Return a hash value that digests "umap".
3840 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3842 uint32_t hash;
3844 if (!umap)
3845 return 0;
3847 hash = isl_hash_init();
3848 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3849 return 0;
3851 return hash;
3854 /* Return a hash value that digests "uset".
3856 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3858 return isl_union_map_get_hash(uset);