isl_map_simplify.c: uset_gist_full: drop redundant isl_basic_set_simplify call
[isl.git] / isl_union_map.c
blob2cae0bc447b1da891f3249bf80728a87f57328cd
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 -> C to "res".
2092 static isl_stat range_factor_range_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_range(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 -> C and collect the results.
2109 __isl_give isl_union_map *isl_union_map_range_factor_range(
2110 __isl_take isl_union_map *umap)
2112 return cond_un_op(umap, &range_factor_range_entry);
2115 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2117 static isl_stat factor_domain_entry(void **entry, void *user)
2119 isl_map *map = *entry;
2120 isl_union_map **res = user;
2122 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2123 return isl_stat_ok;
2125 *res = isl_union_map_add_map(*res,
2126 isl_map_factor_domain(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 -> D],
2132 * construct the map A -> C and collect the results.
2134 __isl_give isl_union_map *isl_union_map_factor_domain(
2135 __isl_take isl_union_map *umap)
2137 return cond_un_op(umap, &factor_domain_entry);
2140 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2142 static isl_stat factor_range_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_range(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 B -> D and collect the results.
2159 __isl_give isl_union_map *isl_union_map_factor_range(
2160 __isl_take isl_union_map *umap)
2162 return cond_un_op(umap, &factor_range_entry);
2165 static isl_stat unwrap_entry(void **entry, void *user)
2167 isl_set *set = *entry;
2168 isl_union_set **res = user;
2170 if (!isl_set_is_wrapping(set))
2171 return isl_stat_ok;
2173 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
2175 return isl_stat_ok;
2178 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2180 return cond_un_op(uset, &unwrap_entry);
2183 static isl_stat wrap_entry(void **entry, void *user)
2185 isl_map *map = *entry;
2186 isl_union_set **res = user;
2188 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2190 return isl_stat_ok;
2193 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2195 return cond_un_op(umap, &wrap_entry);
2198 struct isl_union_map_is_subset_data {
2199 isl_union_map *umap2;
2200 isl_bool is_subset;
2203 static isl_stat is_subset_entry(void **entry, void *user)
2205 struct isl_union_map_is_subset_data *data = user;
2206 uint32_t hash;
2207 struct isl_hash_table_entry *entry2;
2208 isl_map *map = *entry;
2210 hash = isl_space_get_hash(map->dim);
2211 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2212 hash, &has_dim, map->dim, 0);
2213 if (!entry2) {
2214 int empty = isl_map_is_empty(map);
2215 if (empty < 0)
2216 return isl_stat_error;
2217 if (empty)
2218 return isl_stat_ok;
2219 data->is_subset = 0;
2220 return isl_stat_error;
2223 data->is_subset = isl_map_is_subset(map, entry2->data);
2224 if (data->is_subset < 0 || !data->is_subset)
2225 return isl_stat_error;
2227 return isl_stat_ok;
2230 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2231 __isl_keep isl_union_map *umap2)
2233 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2235 umap1 = isl_union_map_copy(umap1);
2236 umap2 = isl_union_map_copy(umap2);
2237 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2238 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2240 if (!umap1 || !umap2)
2241 goto error;
2243 data.umap2 = umap2;
2244 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2245 &is_subset_entry, &data) < 0 &&
2246 data.is_subset)
2247 goto error;
2249 isl_union_map_free(umap1);
2250 isl_union_map_free(umap2);
2252 return data.is_subset;
2253 error:
2254 isl_union_map_free(umap1);
2255 isl_union_map_free(umap2);
2256 return isl_bool_error;
2259 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2260 __isl_keep isl_union_set *uset2)
2262 return isl_union_map_is_subset(uset1, uset2);
2265 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2266 __isl_keep isl_union_map *umap2)
2268 isl_bool is_subset;
2270 if (!umap1 || !umap2)
2271 return isl_bool_error;
2272 is_subset = isl_union_map_is_subset(umap1, umap2);
2273 if (is_subset != isl_bool_true)
2274 return is_subset;
2275 is_subset = isl_union_map_is_subset(umap2, umap1);
2276 return is_subset;
2279 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2280 __isl_keep isl_union_set *uset2)
2282 return isl_union_map_is_equal(uset1, uset2);
2285 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2286 __isl_keep isl_union_map *umap2)
2288 isl_bool is_subset;
2290 if (!umap1 || !umap2)
2291 return isl_bool_error;
2292 is_subset = isl_union_map_is_subset(umap1, umap2);
2293 if (is_subset != isl_bool_true)
2294 return is_subset;
2295 is_subset = isl_union_map_is_subset(umap2, umap1);
2296 if (is_subset == isl_bool_error)
2297 return is_subset;
2298 return !is_subset;
2301 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2302 __isl_keep isl_union_set *uset2)
2304 return isl_union_map_is_strict_subset(uset1, uset2);
2307 /* Internal data structure for isl_union_map_is_disjoint.
2308 * umap2 is the union map with which we are comparing.
2309 * is_disjoint is initialized to 1 and is set to 0 as soon
2310 * as the union maps turn out not to be disjoint.
2312 struct isl_union_map_is_disjoint_data {
2313 isl_union_map *umap2;
2314 isl_bool is_disjoint;
2317 /* Check if "map" is disjoint from data->umap2 and abort
2318 * the search if it is not.
2320 static isl_stat is_disjoint_entry(void **entry, void *user)
2322 struct isl_union_map_is_disjoint_data *data = user;
2323 uint32_t hash;
2324 struct isl_hash_table_entry *entry2;
2325 isl_map *map = *entry;
2327 hash = isl_space_get_hash(map->dim);
2328 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2329 hash, &has_dim, map->dim, 0);
2330 if (!entry2)
2331 return isl_stat_ok;
2333 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2334 if (data->is_disjoint < 0 || !data->is_disjoint)
2335 return isl_stat_error;
2337 return isl_stat_ok;
2340 /* Are "umap1" and "umap2" disjoint?
2342 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2343 __isl_keep isl_union_map *umap2)
2345 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2347 umap1 = isl_union_map_copy(umap1);
2348 umap2 = isl_union_map_copy(umap2);
2349 umap1 = isl_union_map_align_params(umap1,
2350 isl_union_map_get_space(umap2));
2351 umap2 = isl_union_map_align_params(umap2,
2352 isl_union_map_get_space(umap1));
2354 if (!umap1 || !umap2)
2355 goto error;
2357 data.umap2 = umap2;
2358 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2359 &is_disjoint_entry, &data) < 0 &&
2360 data.is_disjoint)
2361 goto error;
2363 isl_union_map_free(umap1);
2364 isl_union_map_free(umap2);
2366 return data.is_disjoint;
2367 error:
2368 isl_union_map_free(umap1);
2369 isl_union_map_free(umap2);
2370 return isl_bool_error;
2373 /* Are "uset1" and "uset2" disjoint?
2375 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2376 __isl_keep isl_union_set *uset2)
2378 return isl_union_map_is_disjoint(uset1, uset2);
2381 static isl_stat sample_entry(void **entry, void *user)
2383 isl_basic_map **sample = (isl_basic_map **)user;
2384 isl_map *map = *entry;
2386 *sample = isl_map_sample(isl_map_copy(map));
2387 if (!*sample)
2388 return isl_stat_error;
2389 if (!isl_basic_map_plain_is_empty(*sample))
2390 return isl_stat_error;
2391 return isl_stat_ok;
2394 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2396 isl_basic_map *sample = NULL;
2398 if (!umap)
2399 return NULL;
2401 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2402 &sample_entry, &sample) < 0 &&
2403 !sample)
2404 goto error;
2406 if (!sample)
2407 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2409 isl_union_map_free(umap);
2411 return sample;
2412 error:
2413 isl_union_map_free(umap);
2414 return NULL;
2417 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2419 return (isl_basic_set *)isl_union_map_sample(uset);
2422 struct isl_forall_data {
2423 isl_bool res;
2424 isl_bool (*fn)(__isl_keep isl_map *map);
2427 static isl_stat forall_entry(void **entry, void *user)
2429 struct isl_forall_data *data = user;
2430 isl_map *map = *entry;
2432 data->res = data->fn(map);
2433 if (data->res < 0)
2434 return isl_stat_error;
2436 if (!data->res)
2437 return isl_stat_error;
2439 return isl_stat_ok;
2442 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2443 isl_bool (*fn)(__isl_keep isl_map *map))
2445 struct isl_forall_data data = { isl_bool_true, fn };
2447 if (!umap)
2448 return isl_bool_error;
2450 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2451 &forall_entry, &data) < 0 && data.res)
2452 return isl_bool_error;
2454 return data.res;
2457 struct isl_forall_user_data {
2458 isl_bool res;
2459 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2460 void *user;
2463 static isl_stat forall_user_entry(void **entry, void *user)
2465 struct isl_forall_user_data *data = user;
2466 isl_map *map = *entry;
2468 data->res = data->fn(map, data->user);
2469 if (data->res < 0)
2470 return isl_stat_error;
2472 if (!data->res)
2473 return isl_stat_error;
2475 return isl_stat_ok;
2478 /* Check if fn(map, user) returns true for all maps "map" in umap.
2480 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2481 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2483 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2485 if (!umap)
2486 return isl_bool_error;
2488 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2489 &forall_user_entry, &data) < 0 && data.res)
2490 return isl_bool_error;
2492 return data.res;
2495 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2497 return union_map_forall(umap, &isl_map_is_empty);
2500 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2502 return isl_union_map_is_empty(uset);
2505 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2507 isl_bool is_subset;
2508 isl_space *dim;
2509 isl_map *id;
2511 if (!map)
2512 return isl_bool_error;
2514 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2515 map->dim, isl_dim_out))
2516 return isl_bool_false;
2518 dim = isl_map_get_space(map);
2519 id = isl_map_identity(dim);
2521 is_subset = isl_map_is_subset(map, id);
2523 isl_map_free(id);
2525 return is_subset;
2528 /* Given an isl_union_map that consists of a single map, check
2529 * if it is single-valued.
2531 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2533 isl_map *map;
2534 isl_bool sv;
2536 umap = isl_union_map_copy(umap);
2537 map = isl_map_from_union_map(umap);
2538 sv = isl_map_is_single_valued(map);
2539 isl_map_free(map);
2541 return sv;
2544 /* Internal data structure for single_valued_on_domain.
2546 * "umap" is the union map to be tested.
2547 * "sv" is set to 1 as long as "umap" may still be single-valued.
2549 struct isl_union_map_is_sv_data {
2550 isl_union_map *umap;
2551 isl_bool sv;
2554 /* Check if the data->umap is single-valued on "set".
2556 * If data->umap consists of a single map on "set", then test it
2557 * as an isl_map.
2559 * Otherwise, compute
2561 * M \circ M^-1
2563 * check if the result is a subset of the identity mapping and
2564 * store the result in data->sv.
2566 * Terminate as soon as data->umap has been determined not to
2567 * be single-valued.
2569 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2571 struct isl_union_map_is_sv_data *data = user;
2572 isl_union_map *umap, *test;
2574 umap = isl_union_map_copy(data->umap);
2575 umap = isl_union_map_intersect_domain(umap,
2576 isl_union_set_from_set(set));
2578 if (isl_union_map_n_map(umap) == 1) {
2579 data->sv = single_map_is_single_valued(umap);
2580 isl_union_map_free(umap);
2581 } else {
2582 test = isl_union_map_reverse(isl_union_map_copy(umap));
2583 test = isl_union_map_apply_range(test, umap);
2585 data->sv = union_map_forall(test, &is_subset_of_identity);
2587 isl_union_map_free(test);
2590 if (data->sv < 0 || !data->sv)
2591 return isl_stat_error;
2592 return isl_stat_ok;
2595 /* Check if the given map is single-valued.
2597 * If the union map consists of a single map, then test it as an isl_map.
2598 * Otherwise, check if the union map is single-valued on each of its
2599 * domain spaces.
2601 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2603 isl_union_map *universe;
2604 isl_union_set *domain;
2605 struct isl_union_map_is_sv_data data;
2607 if (isl_union_map_n_map(umap) == 1)
2608 return single_map_is_single_valued(umap);
2610 universe = isl_union_map_universe(isl_union_map_copy(umap));
2611 domain = isl_union_map_domain(universe);
2613 data.sv = isl_bool_true;
2614 data.umap = umap;
2615 if (isl_union_set_foreach_set(domain,
2616 &single_valued_on_domain, &data) < 0 && data.sv)
2617 data.sv = isl_bool_error;
2618 isl_union_set_free(domain);
2620 return data.sv;
2623 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2625 isl_bool in;
2627 umap = isl_union_map_copy(umap);
2628 umap = isl_union_map_reverse(umap);
2629 in = isl_union_map_is_single_valued(umap);
2630 isl_union_map_free(umap);
2632 return in;
2635 /* Represents a map that has a fixed value (v) for one of its
2636 * range dimensions.
2637 * The map in this structure is not reference counted, so it
2638 * is only valid while the isl_union_map from which it was
2639 * obtained is still alive.
2641 struct isl_fixed_map {
2642 isl_int v;
2643 isl_map *map;
2646 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2647 int n)
2649 int i;
2650 struct isl_fixed_map *v;
2652 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2653 if (!v)
2654 return NULL;
2655 for (i = 0; i < n; ++i)
2656 isl_int_init(v[i].v);
2657 return v;
2660 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2662 int i;
2664 if (!v)
2665 return;
2666 for (i = 0; i < n; ++i)
2667 isl_int_clear(v[i].v);
2668 free(v);
2671 /* Compare the "v" field of two isl_fixed_map structs.
2673 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2675 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2676 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2678 return isl_int_cmp(e1->v, e2->v);
2681 /* Internal data structure used while checking whether all maps
2682 * in a union_map have a fixed value for a given output dimension.
2683 * v is the list of maps, with the fixed value for the dimension
2684 * n is the number of maps considered so far
2685 * pos is the output dimension under investigation
2687 struct isl_fixed_dim_data {
2688 struct isl_fixed_map *v;
2689 int n;
2690 int pos;
2693 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2695 struct isl_fixed_dim_data *data = user;
2697 data->v[data->n].map = map;
2698 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2699 &data->v[data->n++].v);
2702 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2703 int first, int n_range);
2705 /* Given a list of the maps, with their fixed values at output dimension "pos",
2706 * check whether the ranges of the maps form an obvious partition.
2708 * We first sort the maps according to their fixed values.
2709 * If all maps have a different value, then we know the ranges form
2710 * a partition.
2711 * Otherwise, we collect the maps with the same fixed value and
2712 * check whether each such collection is obviously injective
2713 * based on later dimensions.
2715 static int separates(struct isl_fixed_map *v, int n,
2716 __isl_take isl_space *dim, int pos, int n_range)
2718 int i;
2720 if (!v)
2721 goto error;
2723 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2725 for (i = 0; i + 1 < n; ++i) {
2726 int j, k;
2727 isl_union_map *part;
2728 int injective;
2730 for (j = i + 1; j < n; ++j)
2731 if (isl_int_ne(v[i].v, v[j].v))
2732 break;
2734 if (j == i + 1)
2735 continue;
2737 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2738 for (k = i; k < j; ++k)
2739 part = isl_union_map_add_map(part,
2740 isl_map_copy(v[k].map));
2742 injective = plain_injective_on_range(part, pos + 1, n_range);
2743 if (injective < 0)
2744 goto error;
2745 if (!injective)
2746 break;
2748 i = j - 1;
2751 isl_space_free(dim);
2752 free_isl_fixed_map_array(v, n);
2753 return i + 1 >= n;
2754 error:
2755 isl_space_free(dim);
2756 free_isl_fixed_map_array(v, n);
2757 return -1;
2760 /* Check whether the maps in umap have obviously distinct ranges.
2761 * In particular, check for an output dimension in the range
2762 * [first,n_range) for which all maps have a fixed value
2763 * and then check if these values, possibly along with fixed values
2764 * at later dimensions, entail distinct ranges.
2766 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2767 int first, int n_range)
2769 isl_ctx *ctx;
2770 int n;
2771 struct isl_fixed_dim_data data = { NULL };
2773 ctx = isl_union_map_get_ctx(umap);
2775 n = isl_union_map_n_map(umap);
2776 if (!umap)
2777 goto error;
2779 if (n <= 1) {
2780 isl_union_map_free(umap);
2781 return isl_bool_true;
2784 if (first >= n_range) {
2785 isl_union_map_free(umap);
2786 return isl_bool_false;
2789 data.v = alloc_isl_fixed_map_array(ctx, n);
2790 if (!data.v)
2791 goto error;
2793 for (data.pos = first; data.pos < n_range; ++data.pos) {
2794 isl_bool fixed;
2795 int injective;
2796 isl_space *dim;
2798 data.n = 0;
2799 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2800 if (fixed < 0)
2801 goto error;
2802 if (!fixed)
2803 continue;
2804 dim = isl_union_map_get_space(umap);
2805 injective = separates(data.v, n, dim, data.pos, n_range);
2806 isl_union_map_free(umap);
2807 return injective;
2810 free_isl_fixed_map_array(data.v, n);
2811 isl_union_map_free(umap);
2813 return isl_bool_false;
2814 error:
2815 free_isl_fixed_map_array(data.v, n);
2816 isl_union_map_free(umap);
2817 return isl_bool_error;
2820 /* Check whether the maps in umap that map to subsets of "ran"
2821 * have obviously distinct ranges.
2823 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2824 void *user)
2826 isl_union_map *umap = user;
2828 umap = isl_union_map_copy(umap);
2829 umap = isl_union_map_intersect_range(umap,
2830 isl_union_set_from_set(isl_set_copy(ran)));
2831 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2834 /* Check if the given union_map is obviously injective.
2836 * In particular, we first check if all individual maps are obviously
2837 * injective and then check if all the ranges of these maps are
2838 * obviously disjoint.
2840 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2842 isl_bool in;
2843 isl_union_map *univ;
2844 isl_union_set *ran;
2846 in = union_map_forall(umap, &isl_map_plain_is_injective);
2847 if (in < 0)
2848 return isl_bool_error;
2849 if (!in)
2850 return isl_bool_false;
2852 univ = isl_union_map_universe(isl_union_map_copy(umap));
2853 ran = isl_union_map_range(univ);
2855 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2857 isl_union_set_free(ran);
2859 return in;
2862 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2864 isl_bool sv;
2866 sv = isl_union_map_is_single_valued(umap);
2867 if (sv < 0 || !sv)
2868 return sv;
2870 return isl_union_map_is_injective(umap);
2873 static isl_stat zip_entry(void **entry, void *user)
2875 isl_map *map = *entry;
2876 isl_union_map **res = user;
2878 if (!isl_map_can_zip(map))
2879 return isl_stat_ok;
2881 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2883 return isl_stat_ok;
2886 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2888 return cond_un_op(umap, &zip_entry);
2891 static isl_stat uncurry_entry(void **entry, void *user)
2893 isl_map *map = *entry;
2894 isl_union_map **res = user;
2896 if (!isl_map_can_uncurry(map))
2897 return isl_stat_ok;
2899 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
2901 return isl_stat_ok;
2904 /* Given a union map, take the maps of the form A -> (B -> C) and
2905 * return the union of the corresponding maps (A -> B) -> C.
2907 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2909 return cond_un_op(umap, &uncurry_entry);
2912 static isl_stat curry_entry(void **entry, void *user)
2914 isl_map *map = *entry;
2915 isl_union_map **res = user;
2917 if (!isl_map_can_curry(map))
2918 return isl_stat_ok;
2920 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2922 return isl_stat_ok;
2925 /* Given a union map, take the maps of the form (A -> B) -> C and
2926 * return the union of the corresponding maps A -> (B -> C).
2928 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2930 return cond_un_op(umap, &curry_entry);
2933 static isl_stat lift_entry(void **entry, void *user)
2935 isl_set *set = *entry;
2936 isl_union_set **res = user;
2938 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2940 return isl_stat_ok;
2943 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2945 return cond_un_op(uset, &lift_entry);
2948 static isl_stat coefficients_entry(void **entry, void *user)
2950 isl_set *set = *entry;
2951 isl_union_set **res = user;
2953 set = isl_set_copy(set);
2954 set = isl_set_from_basic_set(isl_set_coefficients(set));
2955 *res = isl_union_set_add_set(*res, set);
2957 return isl_stat_ok;
2960 __isl_give isl_union_set *isl_union_set_coefficients(
2961 __isl_take isl_union_set *uset)
2963 isl_ctx *ctx;
2964 isl_space *dim;
2965 isl_union_set *res;
2967 if (!uset)
2968 return NULL;
2970 ctx = isl_union_set_get_ctx(uset);
2971 dim = isl_space_set_alloc(ctx, 0, 0);
2972 res = isl_union_map_alloc(dim, uset->table.n);
2973 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2974 &coefficients_entry, &res) < 0)
2975 goto error;
2977 isl_union_set_free(uset);
2978 return res;
2979 error:
2980 isl_union_set_free(uset);
2981 isl_union_set_free(res);
2982 return NULL;
2985 static isl_stat solutions_entry(void **entry, void *user)
2987 isl_set *set = *entry;
2988 isl_union_set **res = user;
2990 set = isl_set_copy(set);
2991 set = isl_set_from_basic_set(isl_set_solutions(set));
2992 if (!*res)
2993 *res = isl_union_set_from_set(set);
2994 else
2995 *res = isl_union_set_add_set(*res, set);
2997 if (!*res)
2998 return isl_stat_error;
3000 return isl_stat_ok;
3003 __isl_give isl_union_set *isl_union_set_solutions(
3004 __isl_take isl_union_set *uset)
3006 isl_union_set *res = NULL;
3008 if (!uset)
3009 return NULL;
3011 if (uset->table.n == 0) {
3012 res = isl_union_set_empty(isl_union_set_get_space(uset));
3013 isl_union_set_free(uset);
3014 return res;
3017 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3018 &solutions_entry, &res) < 0)
3019 goto error;
3021 isl_union_set_free(uset);
3022 return res;
3023 error:
3024 isl_union_set_free(uset);
3025 isl_union_set_free(res);
3026 return NULL;
3029 /* Is the domain space of "map" equal to "space"?
3031 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3033 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3034 space, isl_dim_out);
3037 /* Is the range space of "map" equal to "space"?
3039 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3041 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3042 space, isl_dim_out);
3045 /* Is the set space of "map" equal to "space"?
3047 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3049 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3050 space, isl_dim_out);
3053 /* Internal data structure for preimage_pw_multi_aff.
3055 * "pma" is the function under which the preimage should be taken.
3056 * "space" is the space of "pma".
3057 * "res" collects the results.
3058 * "fn" computes the preimage for a given map.
3059 * "match" returns true if "fn" can be called.
3061 struct isl_union_map_preimage_data {
3062 isl_space *space;
3063 isl_pw_multi_aff *pma;
3064 isl_union_map *res;
3065 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3066 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3067 __isl_take isl_pw_multi_aff *pma);
3070 /* Call data->fn to compute the preimage of the domain or range of *entry
3071 * under the function represented by data->pma, provided the domain/range
3072 * space of *entry matches the target space of data->pma
3073 * (as given by data->match), and add the result to data->res.
3075 static isl_stat preimage_entry(void **entry, void *user)
3077 int m;
3078 isl_map *map = *entry;
3079 struct isl_union_map_preimage_data *data = user;
3080 isl_bool empty;
3082 m = data->match(map, data->space);
3083 if (m < 0)
3084 return isl_stat_error;
3085 if (!m)
3086 return isl_stat_ok;
3088 map = isl_map_copy(map);
3089 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3091 empty = isl_map_is_empty(map);
3092 if (empty < 0 || empty) {
3093 isl_map_free(map);
3094 return empty < 0 ? isl_stat_error : isl_stat_ok;
3097 data->res = isl_union_map_add_map(data->res, map);
3099 return isl_stat_ok;
3102 /* Compute the preimage of the domain or range of "umap" under the function
3103 * represented by "pma".
3104 * In other words, plug in "pma" in the domain or range of "umap".
3105 * The function "fn" performs the actual preimage computation on a map,
3106 * while "match" determines to which maps the function should be applied.
3108 static __isl_give isl_union_map *preimage_pw_multi_aff(
3109 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3110 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3111 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3112 __isl_take isl_pw_multi_aff *pma))
3114 isl_ctx *ctx;
3115 isl_space *space;
3116 struct isl_union_map_preimage_data data;
3118 umap = isl_union_map_align_params(umap,
3119 isl_pw_multi_aff_get_space(pma));
3120 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3122 if (!umap || !pma)
3123 goto error;
3125 ctx = isl_union_map_get_ctx(umap);
3126 space = isl_union_map_get_space(umap);
3127 data.space = isl_pw_multi_aff_get_space(pma);
3128 data.pma = pma;
3129 data.res = isl_union_map_alloc(space, umap->table.n);
3130 data.match = match;
3131 data.fn = fn;
3132 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3133 &data) < 0)
3134 data.res = isl_union_map_free(data.res);
3136 isl_space_free(data.space);
3137 isl_union_map_free(umap);
3138 isl_pw_multi_aff_free(pma);
3139 return data.res;
3140 error:
3141 isl_union_map_free(umap);
3142 isl_pw_multi_aff_free(pma);
3143 return NULL;
3146 /* Compute the preimage of the domain of "umap" under the function
3147 * represented by "pma".
3148 * In other words, plug in "pma" in the domain of "umap".
3149 * The result contains maps that live in the same spaces as the maps of "umap"
3150 * with domain space equal to the target space of "pma",
3151 * except that the domain has been replaced by the domain space of "pma".
3153 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3154 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3156 return preimage_pw_multi_aff(umap, pma, &domain_match,
3157 &isl_map_preimage_domain_pw_multi_aff);
3160 /* Compute the preimage of the range of "umap" under the function
3161 * represented by "pma".
3162 * In other words, plug in "pma" in the range of "umap".
3163 * The result contains maps that live in the same spaces as the maps of "umap"
3164 * with range space equal to the target space of "pma",
3165 * except that the range has been replaced by the domain space of "pma".
3167 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3168 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3170 return preimage_pw_multi_aff(umap, pma, &range_match,
3171 &isl_map_preimage_range_pw_multi_aff);
3174 /* Compute the preimage of "uset" under the function represented by "pma".
3175 * In other words, plug in "pma" in "uset".
3176 * The result contains sets that live in the same spaces as the sets of "uset"
3177 * with space equal to the target space of "pma",
3178 * except that the space has been replaced by the domain space of "pma".
3180 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3181 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3183 return preimage_pw_multi_aff(uset, pma, &set_match,
3184 &isl_set_preimage_pw_multi_aff);
3187 /* Compute the preimage of the domain of "umap" under the function
3188 * represented by "ma".
3189 * In other words, plug in "ma" in the domain of "umap".
3190 * The result contains maps that live in the same spaces as the maps of "umap"
3191 * with domain space equal to the target space of "ma",
3192 * except that the domain has been replaced by the domain space of "ma".
3194 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3195 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3197 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3198 isl_pw_multi_aff_from_multi_aff(ma));
3201 /* Compute the preimage of the range of "umap" under the function
3202 * represented by "ma".
3203 * In other words, plug in "ma" in the range of "umap".
3204 * The result contains maps that live in the same spaces as the maps of "umap"
3205 * with range space equal to the target space of "ma",
3206 * except that the range has been replaced by the domain space of "ma".
3208 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3209 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3211 return isl_union_map_preimage_range_pw_multi_aff(umap,
3212 isl_pw_multi_aff_from_multi_aff(ma));
3215 /* Compute the preimage of "uset" under the function represented by "ma".
3216 * In other words, plug in "ma" in "uset".
3217 * The result contains sets that live in the same spaces as the sets of "uset"
3218 * with space equal to the target space of "ma",
3219 * except that the space has been replaced by the domain space of "ma".
3221 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3222 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3224 return isl_union_set_preimage_pw_multi_aff(uset,
3225 isl_pw_multi_aff_from_multi_aff(ma));
3228 /* Internal data structure for preimage_multi_pw_aff.
3230 * "mpa" is the function under which the preimage should be taken.
3231 * "space" is the space of "mpa".
3232 * "res" collects the results.
3233 * "fn" computes the preimage for a given map.
3234 * "match" returns true if "fn" can be called.
3236 struct isl_union_map_preimage_mpa_data {
3237 isl_space *space;
3238 isl_multi_pw_aff *mpa;
3239 isl_union_map *res;
3240 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3241 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3242 __isl_take isl_multi_pw_aff *mpa);
3245 /* Call data->fn to compute the preimage of the domain or range of *entry
3246 * under the function represented by data->mpa, provided the domain/range
3247 * space of *entry matches the target space of data->mpa
3248 * (as given by data->match), and add the result to data->res.
3250 static isl_stat preimage_mpa_entry(void **entry, void *user)
3252 int m;
3253 isl_map *map = *entry;
3254 struct isl_union_map_preimage_mpa_data *data = user;
3255 isl_bool empty;
3257 m = data->match(map, data->space);
3258 if (m < 0)
3259 return isl_stat_error;
3260 if (!m)
3261 return isl_stat_ok;
3263 map = isl_map_copy(map);
3264 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3266 empty = isl_map_is_empty(map);
3267 if (empty < 0 || empty) {
3268 isl_map_free(map);
3269 return empty < 0 ? isl_stat_error : isl_stat_ok;
3272 data->res = isl_union_map_add_map(data->res, map);
3274 return isl_stat_ok;
3277 /* Compute the preimage of the domain or range of "umap" under the function
3278 * represented by "mpa".
3279 * In other words, plug in "mpa" in the domain or range of "umap".
3280 * The function "fn" performs the actual preimage computation on a map,
3281 * while "match" determines to which maps the function should be applied.
3283 static __isl_give isl_union_map *preimage_multi_pw_aff(
3284 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3285 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3286 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3287 __isl_take isl_multi_pw_aff *mpa))
3289 isl_ctx *ctx;
3290 isl_space *space;
3291 struct isl_union_map_preimage_mpa_data data;
3293 umap = isl_union_map_align_params(umap,
3294 isl_multi_pw_aff_get_space(mpa));
3295 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3297 if (!umap || !mpa)
3298 goto error;
3300 ctx = isl_union_map_get_ctx(umap);
3301 space = isl_union_map_get_space(umap);
3302 data.space = isl_multi_pw_aff_get_space(mpa);
3303 data.mpa = mpa;
3304 data.res = isl_union_map_alloc(space, umap->table.n);
3305 data.match = match;
3306 data.fn = fn;
3307 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3308 &data) < 0)
3309 data.res = isl_union_map_free(data.res);
3311 isl_space_free(data.space);
3312 isl_union_map_free(umap);
3313 isl_multi_pw_aff_free(mpa);
3314 return data.res;
3315 error:
3316 isl_union_map_free(umap);
3317 isl_multi_pw_aff_free(mpa);
3318 return NULL;
3321 /* Compute the preimage of the domain of "umap" under the function
3322 * represented by "mpa".
3323 * In other words, plug in "mpa" in the domain of "umap".
3324 * The result contains maps that live in the same spaces as the maps of "umap"
3325 * with domain space equal to the target space of "mpa",
3326 * except that the domain has been replaced by the domain space of "mpa".
3328 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3329 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3331 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3332 &isl_map_preimage_domain_multi_pw_aff);
3335 /* Internal data structure for preimage_upma.
3337 * "umap" is the map of which the preimage should be computed.
3338 * "res" collects the results.
3339 * "fn" computes the preimage for a given piecewise multi-affine function.
3341 struct isl_union_map_preimage_upma_data {
3342 isl_union_map *umap;
3343 isl_union_map *res;
3344 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3345 __isl_take isl_pw_multi_aff *pma);
3348 /* Call data->fn to compute the preimage of the domain or range of data->umap
3349 * under the function represented by pma and add the result to data->res.
3351 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3353 struct isl_union_map_preimage_upma_data *data = user;
3354 isl_union_map *umap;
3356 umap = isl_union_map_copy(data->umap);
3357 umap = data->fn(umap, pma);
3358 data->res = isl_union_map_union(data->res, umap);
3360 return data->res ? isl_stat_ok : isl_stat_error;
3363 /* Compute the preimage of the domain or range of "umap" under the function
3364 * represented by "upma".
3365 * In other words, plug in "upma" in the domain or range of "umap".
3366 * The function "fn" performs the actual preimage computation
3367 * on a piecewise multi-affine function.
3369 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3370 __isl_take isl_union_map *umap,
3371 __isl_take isl_union_pw_multi_aff *upma,
3372 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3373 __isl_take isl_pw_multi_aff *pma))
3375 struct isl_union_map_preimage_upma_data data;
3377 data.umap = umap;
3378 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3379 data.fn = fn;
3380 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3381 &preimage_upma, &data) < 0)
3382 data.res = isl_union_map_free(data.res);
3384 isl_union_map_free(umap);
3385 isl_union_pw_multi_aff_free(upma);
3387 return data.res;
3390 /* Compute the preimage of the domain of "umap" under the function
3391 * represented by "upma".
3392 * In other words, plug in "upma" in the domain of "umap".
3393 * The result contains maps that live in the same spaces as the maps of "umap"
3394 * with domain space equal to one of the target spaces of "upma",
3395 * except that the domain has been replaced by one of the the domain spaces that
3396 * corresponds to that target space of "upma".
3398 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3399 __isl_take isl_union_map *umap,
3400 __isl_take isl_union_pw_multi_aff *upma)
3402 return preimage_union_pw_multi_aff(umap, upma,
3403 &isl_union_map_preimage_domain_pw_multi_aff);
3406 /* Compute the preimage of the range of "umap" under the function
3407 * represented by "upma".
3408 * In other words, plug in "upma" in the range of "umap".
3409 * The result contains maps that live in the same spaces as the maps of "umap"
3410 * with range space equal to one of the target spaces of "upma",
3411 * except that the range has been replaced by one of the the domain spaces that
3412 * corresponds to that target space of "upma".
3414 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3415 __isl_take isl_union_map *umap,
3416 __isl_take isl_union_pw_multi_aff *upma)
3418 return preimage_union_pw_multi_aff(umap, upma,
3419 &isl_union_map_preimage_range_pw_multi_aff);
3422 /* Compute the preimage of "uset" under the function represented by "upma".
3423 * In other words, plug in "upma" in the range of "uset".
3424 * The result contains sets that live in the same spaces as the sets of "uset"
3425 * with space equal to one of the target spaces of "upma",
3426 * except that the space has been replaced by one of the the domain spaces that
3427 * corresponds to that target space of "upma".
3429 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3430 __isl_take isl_union_set *uset,
3431 __isl_take isl_union_pw_multi_aff *upma)
3433 return preimage_union_pw_multi_aff(uset, upma,
3434 &isl_union_set_preimage_pw_multi_aff);
3437 /* Reset the user pointer on all identifiers of parameters and tuples
3438 * of the space of *entry.
3440 static isl_stat reset_user(void **entry, void *user)
3442 isl_map **map = (isl_map **)entry;
3444 *map = isl_map_reset_user(*map);
3446 return *map ? isl_stat_ok : isl_stat_error;
3449 /* Reset the user pointer on all identifiers of parameters and tuples
3450 * of the spaces of "umap".
3452 __isl_give isl_union_map *isl_union_map_reset_user(
3453 __isl_take isl_union_map *umap)
3455 umap = isl_union_map_cow(umap);
3456 if (!umap)
3457 return NULL;
3458 umap->dim = isl_space_reset_user(umap->dim);
3459 if (!umap->dim)
3460 return isl_union_map_free(umap);
3461 umap = un_op(umap, &reset_user);
3463 return umap;
3466 /* Reset the user pointer on all identifiers of parameters and tuples
3467 * of the spaces of "uset".
3469 __isl_give isl_union_set *isl_union_set_reset_user(
3470 __isl_take isl_union_set *uset)
3472 return isl_union_map_reset_user(uset);
3475 /* Internal data structure for isl_union_map_project_out.
3476 * "type", "first" and "n" are the arguments for the isl_map_project_out
3477 * call.
3478 * "res" collects the results.
3480 struct isl_union_map_project_out_data {
3481 enum isl_dim_type type;
3482 unsigned first;
3483 unsigned n;
3485 isl_union_map *res;
3488 /* Turn the data->n dimensions of type data->type, starting at data->first
3489 * into existentially quantified variables and add the result to data->res.
3491 static isl_stat project_out(__isl_take isl_map *map, void *user)
3493 struct isl_union_map_project_out_data *data = user;
3495 map = isl_map_project_out(map, data->type, data->first, data->n);
3496 data->res = isl_union_map_add_map(data->res, map);
3498 return isl_stat_ok;
3501 /* Turn the "n" dimensions of type "type", starting at "first"
3502 * into existentially quantified variables.
3503 * Since the space of an isl_union_map only contains parameters,
3504 * type is required to be equal to isl_dim_param.
3506 __isl_give isl_union_map *isl_union_map_project_out(
3507 __isl_take isl_union_map *umap,
3508 enum isl_dim_type type, unsigned first, unsigned n)
3510 isl_space *space;
3511 struct isl_union_map_project_out_data data = { type, first, n };
3513 if (!umap)
3514 return NULL;
3516 if (type != isl_dim_param)
3517 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3518 "can only project out parameters",
3519 return isl_union_map_free(umap));
3521 space = isl_union_map_get_space(umap);
3522 space = isl_space_drop_dims(space, type, first, n);
3523 data.res = isl_union_map_empty(space);
3524 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3525 data.res = isl_union_map_free(data.res);
3527 isl_union_map_free(umap);
3529 return data.res;
3532 /* Turn the "n" dimensions of type "type", starting at "first"
3533 * into existentially quantified variables.
3534 * Since the space of an isl_union_set only contains parameters,
3535 * "type" is required to be equal to isl_dim_param.
3537 __isl_give isl_union_set *isl_union_set_project_out(
3538 __isl_take isl_union_set *uset,
3539 enum isl_dim_type type, unsigned first, unsigned n)
3541 return isl_union_map_project_out(uset, type, first, n);
3544 /* Internal data structure for isl_union_map_involves_dims.
3545 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3547 struct isl_union_map_involves_dims_data {
3548 unsigned first;
3549 unsigned n;
3552 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3554 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3556 struct isl_union_map_involves_dims_data *data = user;
3557 isl_bool involves;
3559 involves = isl_map_involves_dims(map,
3560 isl_dim_param, data->first, data->n);
3561 if (involves < 0)
3562 return isl_bool_error;
3563 return !involves;
3566 /* Does "umap" involve any of the n parameters starting at first?
3567 * "type" is required to be set to isl_dim_param.
3569 * "umap" involves any of those parameters if any of its maps
3570 * involve the parameters. In other words, "umap" does not
3571 * involve any of the parameters if all its maps to not
3572 * involve the parameters.
3574 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3575 enum isl_dim_type type, unsigned first, unsigned n)
3577 struct isl_union_map_involves_dims_data data = { first, n };
3578 isl_bool excludes;
3580 if (type != isl_dim_param)
3581 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3582 "can only reference parameters", return isl_bool_error);
3584 excludes = union_map_forall_user(umap, &map_excludes, &data);
3586 if (excludes < 0)
3587 return isl_bool_error;
3589 return !excludes;
3592 /* Internal data structure for isl_union_map_reset_range_space.
3593 * "range" is the space from which to set the range space.
3594 * "res" collects the results.
3596 struct isl_union_map_reset_range_space_data {
3597 isl_space *range;
3598 isl_union_map *res;
3601 /* Replace the range space of "map" by the range space of data->range and
3602 * add the result to data->res.
3604 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3606 struct isl_union_map_reset_range_space_data *data = user;
3607 isl_space *space;
3609 space = isl_map_get_space(map);
3610 space = isl_space_domain(space);
3611 space = isl_space_extend_domain_with_range(space,
3612 isl_space_copy(data->range));
3613 map = isl_map_reset_space(map, space);
3614 data->res = isl_union_map_add_map(data->res, map);
3616 return data->res ? isl_stat_ok : isl_stat_error;
3619 /* Replace the range space of all the maps in "umap" by
3620 * the range space of "space".
3622 * This assumes that all maps have the same output dimension.
3623 * This function should therefore not be made publicly available.
3625 * Since the spaces of the maps change, so do their hash value.
3626 * We therefore need to create a new isl_union_map.
3628 __isl_give isl_union_map *isl_union_map_reset_range_space(
3629 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3631 struct isl_union_map_reset_range_space_data data = { space };
3633 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3634 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3635 data.res = isl_union_map_free(data.res);
3637 isl_space_free(space);
3638 isl_union_map_free(umap);
3639 return data.res;
3642 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3643 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3644 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3645 * to the domain and the range of each map.
3646 * "res" collects the results.
3648 struct isl_union_order_at_data {
3649 isl_multi_union_pw_aff *mupa;
3650 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3651 __isl_take isl_multi_pw_aff *mpa2);
3652 isl_union_map *res;
3655 /* Intersect "map" with the result of applying data->order to
3656 * the functions in data->mupa that apply to the domain and the range
3657 * of "map" and add the result to data->res.
3659 static isl_stat order_at(__isl_take isl_map *map, void *user)
3661 struct isl_union_order_at_data *data = user;
3662 isl_space *space;
3663 isl_multi_pw_aff *mpa1, *mpa2;
3664 isl_map *order;
3666 space = isl_space_domain(isl_map_get_space(map));
3667 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3668 space = isl_space_range(isl_map_get_space(map));
3669 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3670 order = data->order(mpa1, mpa2);
3671 map = isl_map_intersect(map, order);
3672 data->res = isl_union_map_add_map(data->res, map);
3674 return data->res ? isl_stat_ok : isl_stat_error;
3677 /* Intersect each map in "umap" with the result of calling "order"
3678 * on the functions is "mupa" that apply to the domain and the range
3679 * of the map.
3681 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3682 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3683 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3684 __isl_take isl_multi_pw_aff *mpa2))
3686 struct isl_union_order_at_data data;
3688 umap = isl_union_map_align_params(umap,
3689 isl_multi_union_pw_aff_get_space(mupa));
3690 mupa = isl_multi_union_pw_aff_align_params(mupa,
3691 isl_union_map_get_space(umap));
3692 data.mupa = mupa;
3693 data.order = order;
3694 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3695 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3696 data.res = isl_union_map_free(data.res);
3698 isl_multi_union_pw_aff_free(mupa);
3699 isl_union_map_free(umap);
3700 return data.res;
3703 /* Return the subset of "umap" where the domain and the range
3704 * have equal "mupa" values.
3706 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3707 __isl_take isl_union_map *umap,
3708 __isl_take isl_multi_union_pw_aff *mupa)
3710 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3711 &isl_multi_pw_aff_eq_map);
3714 /* Return the subset of "umap" where the domain has a lexicographically
3715 * smaller "mupa" value than the range.
3717 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3718 __isl_take isl_union_map *umap,
3719 __isl_take isl_multi_union_pw_aff *mupa)
3721 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3722 &isl_multi_pw_aff_lex_lt_map);
3725 /* Return the subset of "umap" where the domain has a lexicographically
3726 * greater "mupa" value than the range.
3728 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3729 __isl_take isl_union_map *umap,
3730 __isl_take isl_multi_union_pw_aff *mupa)
3732 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3733 &isl_multi_pw_aff_lex_gt_map);
3736 /* Return the union of the elements in the list "list".
3738 __isl_give isl_union_set *isl_union_set_list_union(
3739 __isl_take isl_union_set_list *list)
3741 int i, n;
3742 isl_ctx *ctx;
3743 isl_space *space;
3744 isl_union_set *res;
3746 if (!list)
3747 return NULL;
3749 ctx = isl_union_set_list_get_ctx(list);
3750 space = isl_space_params_alloc(ctx, 0);
3751 res = isl_union_set_empty(space);
3753 n = isl_union_set_list_n_union_set(list);
3754 for (i = 0; i < n; ++i) {
3755 isl_union_set *uset_i;
3757 uset_i = isl_union_set_list_get_union_set(list, i);
3758 res = isl_union_set_union(res, uset_i);
3761 isl_union_set_list_free(list);
3762 return res;