AST generator: select unroll lower bound introducing fewest integer divisions
[isl.git] / isl_union_map.c
blobb097b380c9b5eb4778bedb9d083c6c9b8955a5d5
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 int isl_union_set_is_params(__isl_keep isl_union_set *uset)
71 isl_set *set;
72 int params;
74 if (!uset)
75 return -1;
76 if (uset->table.n != 1)
77 return 0;
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 int free_umap_entry(void **entry, void *user)
154 isl_map *map = *entry;
155 isl_map_free(map);
156 return 0;
159 static int 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 0;
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 int 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 0;
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 int (*fn)(__isl_take isl_map *map, void *user);
422 void *user;
425 static int 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 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
445 int (*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 -1;
452 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
453 &call_on_copy, &data);
456 static int 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 -1;
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 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
551 int (*fn)(__isl_take isl_set *set, void *user), void *user)
553 return isl_union_map_foreach_map(uset,
554 (int(*)(__isl_take isl_map *, void*))fn, user);
557 struct isl_union_set_foreach_point_data {
558 int (*fn)(__isl_take isl_point *pnt, void *user);
559 void *user;
562 static int foreach_point(__isl_take isl_set *set, void *user)
564 struct isl_union_set_foreach_point_data *data = user;
565 int r;
567 r = isl_set_foreach_point(set, data->fn, data->user);
568 isl_set_free(set);
570 return r;
573 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
574 int (*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 int 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 -1;
605 if (empty) {
606 isl_map_free(map);
607 return 0;
610 data->res = isl_union_map_add_map(data->res, map);
612 return 0;
615 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
616 __isl_take isl_union_map *umap2, int (*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 int 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 -1;
675 data->res = isl_union_map_add_map(data->res, map);
677 return 0;
680 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
681 __isl_take isl_set *set, int (*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 __isl_give isl_union_map *isl_union_map_intersect_params(
709 __isl_take isl_union_map *umap, __isl_take isl_set *set)
711 return gen_bin_set_op(umap, set, &intersect_params_entry);
714 __isl_give isl_union_set *isl_union_set_intersect_params(
715 __isl_take isl_union_set *uset, __isl_take isl_set *set)
717 return isl_union_map_intersect_params(uset, set);
720 static __isl_give isl_union_map *union_map_intersect_params(
721 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
723 return isl_union_map_intersect_params(umap,
724 isl_set_from_union_set(uset));
727 static __isl_give isl_union_map *union_map_gist_params(
728 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
730 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
733 struct isl_union_map_match_bin_data {
734 isl_union_map *umap2;
735 isl_union_map *res;
736 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
739 static int match_bin_entry(void **entry, void *user)
741 struct isl_union_map_match_bin_data *data = user;
742 uint32_t hash;
743 struct isl_hash_table_entry *entry2;
744 isl_map *map = *entry;
745 int empty;
747 hash = isl_space_get_hash(map->dim);
748 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
749 hash, &has_dim, map->dim, 0);
750 if (!entry2)
751 return 0;
753 map = isl_map_copy(map);
754 map = data->fn(map, isl_map_copy(entry2->data));
756 empty = isl_map_is_empty(map);
757 if (empty < 0) {
758 isl_map_free(map);
759 return -1;
761 if (empty) {
762 isl_map_free(map);
763 return 0;
766 data->res = isl_union_map_add_map(data->res, map);
768 return 0;
771 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
772 __isl_take isl_union_map *umap2,
773 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
775 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
777 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
778 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
780 if (!umap1 || !umap2)
781 goto error;
783 data.umap2 = umap2;
784 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
785 umap1->table.n);
786 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
787 &match_bin_entry, &data) < 0)
788 goto error;
790 isl_union_map_free(umap1);
791 isl_union_map_free(umap2);
792 return data.res;
793 error:
794 isl_union_map_free(umap1);
795 isl_union_map_free(umap2);
796 isl_union_map_free(data.res);
797 return NULL;
800 __isl_give isl_union_map *isl_union_map_intersect(
801 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
803 return match_bin_op(umap1, umap2, &isl_map_intersect);
806 /* Compute the intersection of the two union_sets.
807 * As a special case, if exactly one of the two union_sets
808 * is a parameter domain, then intersect the parameter domain
809 * of the other one with this set.
811 __isl_give isl_union_set *isl_union_set_intersect(
812 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
814 int p1, p2;
816 p1 = isl_union_set_is_params(uset1);
817 p2 = isl_union_set_is_params(uset2);
818 if (p1 < 0 || p2 < 0)
819 goto error;
820 if (!p1 && p2)
821 return union_map_intersect_params(uset1, uset2);
822 if (p1 && !p2)
823 return union_map_intersect_params(uset2, uset1);
824 return isl_union_map_intersect(uset1, uset2);
825 error:
826 isl_union_set_free(uset1);
827 isl_union_set_free(uset2);
828 return NULL;
831 static int gist_params_entry(void **entry, void *user)
833 struct isl_union_map_gen_bin_set_data *data = user;
834 isl_map *map = *entry;
835 int empty;
837 map = isl_map_copy(map);
838 map = isl_map_gist_params(map, isl_set_copy(data->set));
840 empty = isl_map_is_empty(map);
841 if (empty < 0) {
842 isl_map_free(map);
843 return -1;
846 data->res = isl_union_map_add_map(data->res, map);
848 return 0;
851 __isl_give isl_union_map *isl_union_map_gist_params(
852 __isl_take isl_union_map *umap, __isl_take isl_set *set)
854 return gen_bin_set_op(umap, set, &gist_params_entry);
857 __isl_give isl_union_set *isl_union_set_gist_params(
858 __isl_take isl_union_set *uset, __isl_take isl_set *set)
860 return isl_union_map_gist_params(uset, set);
863 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
864 __isl_take isl_union_map *context)
866 return match_bin_op(umap, context, &isl_map_gist);
869 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
870 __isl_take isl_union_set *context)
872 if (isl_union_set_is_params(context))
873 return union_map_gist_params(uset, context);
874 return isl_union_map_gist(uset, context);
877 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
878 __isl_take isl_map *set2)
880 return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
883 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
884 __isl_take isl_map *set2)
886 return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
889 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
890 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
892 return match_bin_op(uset1, uset2, &lex_lt_set);
895 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
896 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
898 return match_bin_op(uset1, uset2, &lex_le_set);
901 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
902 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
904 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
907 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
908 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
910 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
913 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
914 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
916 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
919 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
920 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
922 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
925 static int intersect_domain_entry(void **entry, void *user)
927 struct isl_union_map_gen_bin_data *data = user;
928 uint32_t hash;
929 struct isl_hash_table_entry *entry2;
930 isl_space *dim;
931 isl_map *map = *entry;
932 int empty;
934 dim = isl_map_get_space(map);
935 dim = isl_space_domain(dim);
936 hash = isl_space_get_hash(dim);
937 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
938 hash, &has_dim, dim, 0);
939 isl_space_free(dim);
940 if (!entry2)
941 return 0;
943 map = isl_map_copy(map);
944 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
946 empty = isl_map_is_empty(map);
947 if (empty < 0) {
948 isl_map_free(map);
949 return -1;
951 if (empty) {
952 isl_map_free(map);
953 return 0;
956 data->res = isl_union_map_add_map(data->res, map);
958 return 0;
961 /* Intersect the domain of "umap" with "uset".
962 * If "uset" is a parameters domain, then intersect the parameter
963 * domain of "umap" with this set.
965 __isl_give isl_union_map *isl_union_map_intersect_domain(
966 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
968 if (isl_union_set_is_params(uset))
969 return union_map_intersect_params(umap, uset);
970 return gen_bin_op(umap, uset, &intersect_domain_entry);
973 /* Remove the elements of data->umap2 from the domain of *entry
974 * and add the result to data->res.
976 static int subtract_domain_entry(void **entry, void *user)
978 struct isl_union_map_gen_bin_data *data = user;
979 uint32_t hash;
980 struct isl_hash_table_entry *entry2;
981 isl_space *dim;
982 isl_map *map = *entry;
983 int empty;
985 dim = isl_map_get_space(map);
986 dim = isl_space_domain(dim);
987 hash = isl_space_get_hash(dim);
988 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
989 hash, &has_dim, dim, 0);
990 isl_space_free(dim);
992 map = isl_map_copy(map);
994 if (!entry2) {
995 data->res = isl_union_map_add_map(data->res, map);
996 return 0;
999 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1001 empty = isl_map_is_empty(map);
1002 if (empty < 0) {
1003 isl_map_free(map);
1004 return -1;
1006 if (empty) {
1007 isl_map_free(map);
1008 return 0;
1011 data->res = isl_union_map_add_map(data->res, map);
1013 return 0;
1016 /* Remove the elements of "uset" from the domain of "umap".
1018 __isl_give isl_union_map *isl_union_map_subtract_domain(
1019 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1021 return gen_bin_op(umap, dom, &subtract_domain_entry);
1024 /* Remove the elements of data->umap2 from the range of *entry
1025 * and add the result to data->res.
1027 static int subtract_range_entry(void **entry, void *user)
1029 struct isl_union_map_gen_bin_data *data = user;
1030 uint32_t hash;
1031 struct isl_hash_table_entry *entry2;
1032 isl_space *space;
1033 isl_map *map = *entry;
1034 int empty;
1036 space = isl_map_get_space(map);
1037 space = isl_space_range(space);
1038 hash = isl_space_get_hash(space);
1039 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1040 hash, &has_dim, space, 0);
1041 isl_space_free(space);
1043 map = isl_map_copy(map);
1045 if (!entry2) {
1046 data->res = isl_union_map_add_map(data->res, map);
1047 return 0;
1050 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1052 empty = isl_map_is_empty(map);
1053 if (empty < 0) {
1054 isl_map_free(map);
1055 return -1;
1057 if (empty) {
1058 isl_map_free(map);
1059 return 0;
1062 data->res = isl_union_map_add_map(data->res, map);
1064 return 0;
1067 /* Remove the elements of "uset" from the range of "umap".
1069 __isl_give isl_union_map *isl_union_map_subtract_range(
1070 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1072 return gen_bin_op(umap, dom, &subtract_range_entry);
1075 static int gist_domain_entry(void **entry, void *user)
1077 struct isl_union_map_gen_bin_data *data = user;
1078 uint32_t hash;
1079 struct isl_hash_table_entry *entry2;
1080 isl_space *dim;
1081 isl_map *map = *entry;
1082 int empty;
1084 dim = isl_map_get_space(map);
1085 dim = isl_space_domain(dim);
1086 hash = isl_space_get_hash(dim);
1087 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1088 hash, &has_dim, dim, 0);
1089 isl_space_free(dim);
1090 if (!entry2)
1091 return 0;
1093 map = isl_map_copy(map);
1094 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1096 empty = isl_map_is_empty(map);
1097 if (empty < 0) {
1098 isl_map_free(map);
1099 return -1;
1102 data->res = isl_union_map_add_map(data->res, map);
1104 return 0;
1107 /* Compute the gist of "umap" with respect to the domain "uset".
1108 * If "uset" is a parameters domain, then compute the gist
1109 * with respect to this parameter domain.
1111 __isl_give isl_union_map *isl_union_map_gist_domain(
1112 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1114 if (isl_union_set_is_params(uset))
1115 return union_map_gist_params(umap, uset);
1116 return gen_bin_op(umap, uset, &gist_domain_entry);
1119 static int gist_range_entry(void **entry, void *user)
1121 struct isl_union_map_gen_bin_data *data = user;
1122 uint32_t hash;
1123 struct isl_hash_table_entry *entry2;
1124 isl_space *space;
1125 isl_map *map = *entry;
1126 int empty;
1128 space = isl_map_get_space(map);
1129 space = isl_space_range(space);
1130 hash = isl_space_get_hash(space);
1131 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1132 hash, &has_dim, space, 0);
1133 isl_space_free(space);
1134 if (!entry2)
1135 return 0;
1137 map = isl_map_copy(map);
1138 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1140 empty = isl_map_is_empty(map);
1141 if (empty < 0) {
1142 isl_map_free(map);
1143 return -1;
1146 data->res = isl_union_map_add_map(data->res, map);
1148 return 0;
1151 /* Compute the gist of "umap" with respect to the range "uset".
1153 __isl_give isl_union_map *isl_union_map_gist_range(
1154 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1156 return gen_bin_op(umap, uset, &gist_range_entry);
1159 static int intersect_range_entry(void **entry, void *user)
1161 struct isl_union_map_gen_bin_data *data = user;
1162 uint32_t hash;
1163 struct isl_hash_table_entry *entry2;
1164 isl_space *dim;
1165 isl_map *map = *entry;
1166 int empty;
1168 dim = isl_map_get_space(map);
1169 dim = isl_space_range(dim);
1170 hash = isl_space_get_hash(dim);
1171 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1172 hash, &has_dim, dim, 0);
1173 isl_space_free(dim);
1174 if (!entry2)
1175 return 0;
1177 map = isl_map_copy(map);
1178 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1180 empty = isl_map_is_empty(map);
1181 if (empty < 0) {
1182 isl_map_free(map);
1183 return -1;
1185 if (empty) {
1186 isl_map_free(map);
1187 return 0;
1190 data->res = isl_union_map_add_map(data->res, map);
1192 return 0;
1195 __isl_give isl_union_map *isl_union_map_intersect_range(
1196 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1198 return gen_bin_op(umap, uset, &intersect_range_entry);
1201 struct isl_union_map_bin_data {
1202 isl_union_map *umap2;
1203 isl_union_map *res;
1204 isl_map *map;
1205 int (*fn)(void **entry, void *user);
1208 static int apply_range_entry(void **entry, void *user)
1210 struct isl_union_map_bin_data *data = user;
1211 isl_map *map2 = *entry;
1212 int empty;
1214 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1215 map2->dim, isl_dim_in))
1216 return 0;
1218 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1220 empty = isl_map_is_empty(map2);
1221 if (empty < 0) {
1222 isl_map_free(map2);
1223 return -1;
1225 if (empty) {
1226 isl_map_free(map2);
1227 return 0;
1230 data->res = isl_union_map_add_map(data->res, map2);
1232 return 0;
1235 static int bin_entry(void **entry, void *user)
1237 struct isl_union_map_bin_data *data = user;
1238 isl_map *map = *entry;
1240 data->map = map;
1241 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1242 data->fn, data) < 0)
1243 return -1;
1245 return 0;
1248 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1249 __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
1251 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1253 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1254 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1256 if (!umap1 || !umap2)
1257 goto error;
1259 data.umap2 = umap2;
1260 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1261 umap1->table.n);
1262 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1263 &bin_entry, &data) < 0)
1264 goto error;
1266 isl_union_map_free(umap1);
1267 isl_union_map_free(umap2);
1268 return data.res;
1269 error:
1270 isl_union_map_free(umap1);
1271 isl_union_map_free(umap2);
1272 isl_union_map_free(data.res);
1273 return NULL;
1276 __isl_give isl_union_map *isl_union_map_apply_range(
1277 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1279 return bin_op(umap1, umap2, &apply_range_entry);
1282 __isl_give isl_union_map *isl_union_map_apply_domain(
1283 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1285 umap1 = isl_union_map_reverse(umap1);
1286 umap1 = isl_union_map_apply_range(umap1, umap2);
1287 return isl_union_map_reverse(umap1);
1290 __isl_give isl_union_set *isl_union_set_apply(
1291 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1293 return isl_union_map_apply_range(uset, umap);
1296 static int map_lex_lt_entry(void **entry, void *user)
1298 struct isl_union_map_bin_data *data = user;
1299 isl_map *map2 = *entry;
1301 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1302 map2->dim, isl_dim_out))
1303 return 0;
1305 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1307 data->res = isl_union_map_add_map(data->res, map2);
1309 return 0;
1312 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1313 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1315 return bin_op(umap1, umap2, &map_lex_lt_entry);
1318 static int map_lex_le_entry(void **entry, void *user)
1320 struct isl_union_map_bin_data *data = user;
1321 isl_map *map2 = *entry;
1323 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1324 map2->dim, isl_dim_out))
1325 return 0;
1327 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1329 data->res = isl_union_map_add_map(data->res, map2);
1331 return 0;
1334 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1335 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1337 return bin_op(umap1, umap2, &map_lex_le_entry);
1340 static int product_entry(void **entry, void *user)
1342 struct isl_union_map_bin_data *data = user;
1343 isl_map *map2 = *entry;
1345 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1347 data->res = isl_union_map_add_map(data->res, map2);
1349 return 0;
1352 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1353 __isl_take isl_union_map *umap2)
1355 return bin_op(umap1, umap2, &product_entry);
1358 static int set_product_entry(void **entry, void *user)
1360 struct isl_union_map_bin_data *data = user;
1361 isl_set *set2 = *entry;
1363 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1365 data->res = isl_union_set_add_set(data->res, set2);
1367 return 0;
1370 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1371 __isl_take isl_union_set *uset2)
1373 return bin_op(uset1, uset2, &set_product_entry);
1376 static int domain_product_entry(void **entry, void *user)
1378 struct isl_union_map_bin_data *data = user;
1379 isl_map *map2 = *entry;
1381 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1382 map2->dim, isl_dim_out))
1383 return 0;
1385 map2 = isl_map_domain_product(isl_map_copy(data->map),
1386 isl_map_copy(map2));
1388 data->res = isl_union_map_add_map(data->res, map2);
1390 return 0;
1393 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1395 __isl_give isl_union_map *isl_union_map_domain_product(
1396 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1398 return bin_op(umap1, umap2, &domain_product_entry);
1401 static int range_product_entry(void **entry, void *user)
1403 struct isl_union_map_bin_data *data = user;
1404 isl_map *map2 = *entry;
1406 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1407 map2->dim, isl_dim_in))
1408 return 0;
1410 map2 = isl_map_range_product(isl_map_copy(data->map),
1411 isl_map_copy(map2));
1413 data->res = isl_union_map_add_map(data->res, map2);
1415 return 0;
1418 __isl_give isl_union_map *isl_union_map_range_product(
1419 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1421 return bin_op(umap1, umap2, &range_product_entry);
1424 /* If data->map A -> B and "map2" C -> D have the same range space,
1425 * then add (A, C) -> (B * D) to data->res.
1427 static int flat_domain_product_entry(void **entry, void *user)
1429 struct isl_union_map_bin_data *data = user;
1430 isl_map *map2 = *entry;
1432 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1433 map2->dim, isl_dim_out))
1434 return 0;
1436 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1437 isl_map_copy(map2));
1439 data->res = isl_union_map_add_map(data->res, map2);
1441 return 0;
1444 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1446 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1447 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1449 return bin_op(umap1, umap2, &flat_domain_product_entry);
1452 static int flat_range_product_entry(void **entry, void *user)
1454 struct isl_union_map_bin_data *data = user;
1455 isl_map *map2 = *entry;
1457 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1458 map2->dim, isl_dim_in))
1459 return 0;
1461 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1462 isl_map_copy(map2));
1464 data->res = isl_union_map_add_map(data->res, map2);
1466 return 0;
1469 __isl_give isl_union_map *isl_union_map_flat_range_product(
1470 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1472 return bin_op(umap1, umap2, &flat_range_product_entry);
1475 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1476 int (*fn)(void **, void *))
1478 isl_union_set *res;
1480 if (!umap)
1481 return NULL;
1483 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1484 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1485 goto error;
1487 isl_union_map_free(umap);
1488 return res;
1489 error:
1490 isl_union_map_free(umap);
1491 isl_union_set_free(res);
1492 return NULL;
1495 static int from_range_entry(void **entry, void *user)
1497 isl_map *set = *entry;
1498 isl_union_set **res = user;
1500 *res = isl_union_map_add_map(*res,
1501 isl_map_from_range(isl_set_copy(set)));
1503 return 0;
1506 __isl_give isl_union_map *isl_union_map_from_range(
1507 __isl_take isl_union_set *uset)
1509 return cond_un_op(uset, &from_range_entry);
1512 __isl_give isl_union_map *isl_union_map_from_domain(
1513 __isl_take isl_union_set *uset)
1515 return isl_union_map_reverse(isl_union_map_from_range(uset));
1518 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1519 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1521 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1522 isl_union_map_from_range(range));
1525 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1526 int (*fn)(void **, void *))
1528 umap = isl_union_map_cow(umap);
1529 if (!umap)
1530 return NULL;
1532 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1533 goto error;
1535 return umap;
1536 error:
1537 isl_union_map_free(umap);
1538 return NULL;
1541 static int affine_entry(void **entry, void *user)
1543 isl_map **map = (isl_map **)entry;
1545 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1547 return *map ? 0 : -1;
1550 __isl_give isl_union_map *isl_union_map_affine_hull(
1551 __isl_take isl_union_map *umap)
1553 return un_op(umap, &affine_entry);
1556 __isl_give isl_union_set *isl_union_set_affine_hull(
1557 __isl_take isl_union_set *uset)
1559 return isl_union_map_affine_hull(uset);
1562 static int polyhedral_entry(void **entry, void *user)
1564 isl_map **map = (isl_map **)entry;
1566 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1568 return *map ? 0 : -1;
1571 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1572 __isl_take isl_union_map *umap)
1574 return un_op(umap, &polyhedral_entry);
1577 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1578 __isl_take isl_union_set *uset)
1580 return isl_union_map_polyhedral_hull(uset);
1583 static int simple_entry(void **entry, void *user)
1585 isl_map **map = (isl_map **)entry;
1587 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1589 return *map ? 0 : -1;
1592 __isl_give isl_union_map *isl_union_map_simple_hull(
1593 __isl_take isl_union_map *umap)
1595 return un_op(umap, &simple_entry);
1598 __isl_give isl_union_set *isl_union_set_simple_hull(
1599 __isl_take isl_union_set *uset)
1601 return isl_union_map_simple_hull(uset);
1604 static int inplace_entry(void **entry, void *user)
1606 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1607 isl_map **map = (isl_map **)entry;
1608 isl_map *copy;
1610 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1611 copy = fn(isl_map_copy(*map));
1612 if (!copy)
1613 return -1;
1615 isl_map_free(*map);
1616 *map = copy;
1618 return 0;
1621 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1622 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1624 if (!umap)
1625 return NULL;
1627 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1628 &inplace_entry, &fn) < 0)
1629 goto error;
1631 return umap;
1632 error:
1633 isl_union_map_free(umap);
1634 return NULL;
1637 __isl_give isl_union_map *isl_union_map_coalesce(
1638 __isl_take isl_union_map *umap)
1640 return inplace(umap, &isl_map_coalesce);
1643 __isl_give isl_union_set *isl_union_set_coalesce(
1644 __isl_take isl_union_set *uset)
1646 return isl_union_map_coalesce(uset);
1649 __isl_give isl_union_map *isl_union_map_detect_equalities(
1650 __isl_take isl_union_map *umap)
1652 return inplace(umap, &isl_map_detect_equalities);
1655 __isl_give isl_union_set *isl_union_set_detect_equalities(
1656 __isl_take isl_union_set *uset)
1658 return isl_union_map_detect_equalities(uset);
1661 __isl_give isl_union_map *isl_union_map_compute_divs(
1662 __isl_take isl_union_map *umap)
1664 return inplace(umap, &isl_map_compute_divs);
1667 __isl_give isl_union_set *isl_union_set_compute_divs(
1668 __isl_take isl_union_set *uset)
1670 return isl_union_map_compute_divs(uset);
1673 static int lexmin_entry(void **entry, void *user)
1675 isl_map **map = (isl_map **)entry;
1677 *map = isl_map_lexmin(*map);
1679 return *map ? 0 : -1;
1682 __isl_give isl_union_map *isl_union_map_lexmin(
1683 __isl_take isl_union_map *umap)
1685 return un_op(umap, &lexmin_entry);
1688 __isl_give isl_union_set *isl_union_set_lexmin(
1689 __isl_take isl_union_set *uset)
1691 return isl_union_map_lexmin(uset);
1694 static int lexmax_entry(void **entry, void *user)
1696 isl_map **map = (isl_map **)entry;
1698 *map = isl_map_lexmax(*map);
1700 return *map ? 0 : -1;
1703 __isl_give isl_union_map *isl_union_map_lexmax(
1704 __isl_take isl_union_map *umap)
1706 return un_op(umap, &lexmax_entry);
1709 __isl_give isl_union_set *isl_union_set_lexmax(
1710 __isl_take isl_union_set *uset)
1712 return isl_union_map_lexmax(uset);
1715 static int universe_entry(void **entry, void *user)
1717 isl_map *map = *entry;
1718 isl_union_map **res = user;
1720 map = isl_map_universe(isl_map_get_space(map));
1721 *res = isl_union_map_add_map(*res, map);
1723 return 0;
1726 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1728 return cond_un_op(umap, &universe_entry);
1731 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1733 return isl_union_map_universe(uset);
1736 static int reverse_entry(void **entry, void *user)
1738 isl_map *map = *entry;
1739 isl_union_map **res = user;
1741 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1743 return 0;
1746 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1748 return cond_un_op(umap, &reverse_entry);
1751 static int params_entry(void **entry, void *user)
1753 isl_map *map = *entry;
1754 isl_union_set **res = user;
1756 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1758 return 0;
1761 /* Compute the parameter domain of the given union map.
1763 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1765 int empty;
1767 empty = isl_union_map_is_empty(umap);
1768 if (empty < 0)
1769 goto error;
1770 if (empty) {
1771 isl_space *space;
1772 space = isl_union_map_get_space(umap);
1773 isl_union_map_free(umap);
1774 return isl_set_empty(space);
1776 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1777 error:
1778 isl_union_map_free(umap);
1779 return NULL;
1782 /* Compute the parameter domain of the given union set.
1784 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1786 return isl_union_map_params(uset);
1789 static int domain_entry(void **entry, void *user)
1791 isl_map *map = *entry;
1792 isl_union_set **res = user;
1794 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1796 return 0;
1799 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1801 return cond_un_op(umap, &domain_entry);
1804 static int range_entry(void **entry, void *user)
1806 isl_map *map = *entry;
1807 isl_union_set **res = user;
1809 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1811 return 0;
1814 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1816 return cond_un_op(umap, &range_entry);
1819 static int domain_map_entry(void **entry, void *user)
1821 isl_map *map = *entry;
1822 isl_union_set **res = user;
1824 *res = isl_union_map_add_map(*res,
1825 isl_map_domain_map(isl_map_copy(map)));
1827 return 0;
1830 __isl_give isl_union_map *isl_union_map_domain_map(
1831 __isl_take isl_union_map *umap)
1833 return cond_un_op(umap, &domain_map_entry);
1836 static int range_map_entry(void **entry, void *user)
1838 isl_map *map = *entry;
1839 isl_union_set **res = user;
1841 *res = isl_union_map_add_map(*res,
1842 isl_map_range_map(isl_map_copy(map)));
1844 return 0;
1847 __isl_give isl_union_map *isl_union_map_range_map(
1848 __isl_take isl_union_map *umap)
1850 return cond_un_op(umap, &range_map_entry);
1853 /* Check if "set" is of the form A[B -> C].
1854 * If so, add A[B -> C] -> B to "res".
1856 static int wrapped_domain_map_entry(void **entry, void *user)
1858 isl_set *set = *entry;
1859 isl_union_set **res = user;
1860 int wrapping;
1862 wrapping = isl_set_is_wrapping(set);
1863 if (wrapping < 0)
1864 return -1;
1865 if (!wrapping)
1866 return 0;
1868 *res = isl_union_map_add_map(*res,
1869 isl_set_wrapped_domain_map(isl_set_copy(set)));
1871 return 0;
1874 /* Given a collection of wrapped maps of the form A[B -> C],
1875 * return the collection of maps A[B -> C] -> B.
1877 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1878 __isl_take isl_union_set *uset)
1880 return cond_un_op(uset, &wrapped_domain_map_entry);
1883 static int deltas_entry(void **entry, void *user)
1885 isl_map *map = *entry;
1886 isl_union_set **res = user;
1888 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1889 map->dim, isl_dim_out))
1890 return 0;
1892 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1894 return 0;
1897 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1899 return cond_un_op(umap, &deltas_entry);
1902 static int deltas_map_entry(void **entry, void *user)
1904 isl_map *map = *entry;
1905 isl_union_map **res = user;
1907 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1908 map->dim, isl_dim_out))
1909 return 0;
1911 *res = isl_union_map_add_map(*res,
1912 isl_map_deltas_map(isl_map_copy(map)));
1914 return 0;
1917 __isl_give isl_union_map *isl_union_map_deltas_map(
1918 __isl_take isl_union_map *umap)
1920 return cond_un_op(umap, &deltas_map_entry);
1923 static int identity_entry(void **entry, void *user)
1925 isl_set *set = *entry;
1926 isl_union_map **res = user;
1928 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1930 return 0;
1933 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1935 return cond_un_op(uset, &identity_entry);
1938 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
1940 static int identity_upma(__isl_take isl_set *set, void *user)
1942 isl_union_pw_multi_aff **res = user;
1943 isl_space *space;
1944 isl_pw_multi_aff *pma;
1946 space = isl_space_map_from_set(isl_set_get_space(set));
1947 pma = isl_pw_multi_aff_identity(space);
1948 pma = isl_pw_multi_aff_intersect_domain(pma, set);
1949 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1951 return *res ? 0 : -1;
1954 /* Return an identity function on "uset" in the form
1955 * of an isl_union_pw_multi_aff.
1957 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
1958 __isl_take isl_union_set *uset)
1960 isl_union_pw_multi_aff *res;
1962 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
1963 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
1964 res = isl_union_pw_multi_aff_free(res);
1966 isl_union_set_free(uset);
1967 return res;
1970 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
1972 static int domain_factor_domain_entry(void **entry, void *user)
1974 isl_map *map = *entry;
1975 isl_union_map **res = user;
1977 if (!isl_map_domain_is_wrapping(map))
1978 return 0;
1980 *res = isl_union_map_add_map(*res,
1981 isl_map_domain_factor_domain(isl_map_copy(map)));
1983 return *res ? 0 : -1;
1986 /* For each map in "umap" of the form [A -> B] -> C,
1987 * construct the map A -> C and collect the results.
1989 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
1990 __isl_take isl_union_map *umap)
1992 return cond_un_op(umap, &domain_factor_domain_entry);
1995 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
1997 static int domain_factor_range_entry(void **entry, void *user)
1999 isl_map *map = *entry;
2000 isl_union_map **res = user;
2002 if (!isl_map_domain_is_wrapping(map))
2003 return 0;
2005 *res = isl_union_map_add_map(*res,
2006 isl_map_domain_factor_range(isl_map_copy(map)));
2008 return *res ? 0 : -1;
2011 /* For each map in "umap" of the form [A -> B] -> C,
2012 * construct the map B -> C and collect the results.
2014 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2015 __isl_take isl_union_map *umap)
2017 return cond_un_op(umap, &domain_factor_range_entry);
2020 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2022 static int range_factor_range_entry(void **entry, void *user)
2024 isl_map *map = *entry;
2025 isl_union_map **res = user;
2027 if (!isl_map_range_is_wrapping(map))
2028 return 0;
2030 *res = isl_union_map_add_map(*res,
2031 isl_map_range_factor_range(isl_map_copy(map)));
2033 return *res ? 0 : -1;
2036 /* For each map in "umap" of the form A -> [B -> C],
2037 * construct the map A -> C and collect the results.
2039 __isl_give isl_union_map *isl_union_map_range_factor_range(
2040 __isl_take isl_union_map *umap)
2042 return cond_un_op(umap, &range_factor_range_entry);
2045 /* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2047 static int factor_domain_entry(void **entry, void *user)
2049 isl_map *map = *entry;
2050 isl_union_map **res = user;
2052 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2053 return 0;
2055 *res = isl_union_map_add_map(*res,
2056 isl_map_factor_domain(isl_map_copy(map)));
2058 return *res ? 0 : -1;
2061 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2062 * construct the map A -> C and collect the results.
2064 __isl_give isl_union_map *isl_union_map_factor_domain(
2065 __isl_take isl_union_map *umap)
2067 return cond_un_op(umap, &factor_domain_entry);
2070 /* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2072 static int factor_range_entry(void **entry, void *user)
2074 isl_map *map = *entry;
2075 isl_union_map **res = user;
2077 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2078 return 0;
2080 *res = isl_union_map_add_map(*res,
2081 isl_map_factor_range(isl_map_copy(map)));
2083 return *res ? 0 : -1;
2086 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2087 * construct the map B -> D and collect the results.
2089 __isl_give isl_union_map *isl_union_map_factor_range(
2090 __isl_take isl_union_map *umap)
2092 return cond_un_op(umap, &factor_range_entry);
2095 static int unwrap_entry(void **entry, void *user)
2097 isl_set *set = *entry;
2098 isl_union_set **res = user;
2100 if (!isl_set_is_wrapping(set))
2101 return 0;
2103 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
2105 return 0;
2108 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2110 return cond_un_op(uset, &unwrap_entry);
2113 static int wrap_entry(void **entry, void *user)
2115 isl_map *map = *entry;
2116 isl_union_set **res = user;
2118 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2120 return 0;
2123 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2125 return cond_un_op(umap, &wrap_entry);
2128 struct isl_union_map_is_subset_data {
2129 isl_union_map *umap2;
2130 int is_subset;
2133 static int is_subset_entry(void **entry, void *user)
2135 struct isl_union_map_is_subset_data *data = user;
2136 uint32_t hash;
2137 struct isl_hash_table_entry *entry2;
2138 isl_map *map = *entry;
2140 hash = isl_space_get_hash(map->dim);
2141 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2142 hash, &has_dim, map->dim, 0);
2143 if (!entry2) {
2144 int empty = isl_map_is_empty(map);
2145 if (empty < 0)
2146 return -1;
2147 if (empty)
2148 return 0;
2149 data->is_subset = 0;
2150 return -1;
2153 data->is_subset = isl_map_is_subset(map, entry2->data);
2154 if (data->is_subset < 0 || !data->is_subset)
2155 return -1;
2157 return 0;
2160 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2161 __isl_keep isl_union_map *umap2)
2163 struct isl_union_map_is_subset_data data = { NULL, 1 };
2165 umap1 = isl_union_map_copy(umap1);
2166 umap2 = isl_union_map_copy(umap2);
2167 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2168 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2170 if (!umap1 || !umap2)
2171 goto error;
2173 data.umap2 = umap2;
2174 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2175 &is_subset_entry, &data) < 0 &&
2176 data.is_subset)
2177 goto error;
2179 isl_union_map_free(umap1);
2180 isl_union_map_free(umap2);
2182 return data.is_subset;
2183 error:
2184 isl_union_map_free(umap1);
2185 isl_union_map_free(umap2);
2186 return -1;
2189 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2190 __isl_keep isl_union_set *uset2)
2192 return isl_union_map_is_subset(uset1, uset2);
2195 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2196 __isl_keep isl_union_map *umap2)
2198 int is_subset;
2200 if (!umap1 || !umap2)
2201 return -1;
2202 is_subset = isl_union_map_is_subset(umap1, umap2);
2203 if (is_subset != 1)
2204 return is_subset;
2205 is_subset = isl_union_map_is_subset(umap2, umap1);
2206 return is_subset;
2209 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2210 __isl_keep isl_union_set *uset2)
2212 return isl_union_map_is_equal(uset1, uset2);
2215 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2216 __isl_keep isl_union_map *umap2)
2218 int is_subset;
2220 if (!umap1 || !umap2)
2221 return -1;
2222 is_subset = isl_union_map_is_subset(umap1, umap2);
2223 if (is_subset != 1)
2224 return is_subset;
2225 is_subset = isl_union_map_is_subset(umap2, umap1);
2226 if (is_subset == -1)
2227 return is_subset;
2228 return !is_subset;
2231 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2232 __isl_keep isl_union_set *uset2)
2234 return isl_union_map_is_strict_subset(uset1, uset2);
2237 /* Internal data structure for isl_union_map_is_disjoint.
2238 * umap2 is the union map with which we are comparing.
2239 * is_disjoint is initialized to 1 and is set to 0 as soon
2240 * as the union maps turn out not to be disjoint.
2242 struct isl_union_map_is_disjoint_data {
2243 isl_union_map *umap2;
2244 int is_disjoint;
2247 /* Check if "map" is disjoint from data->umap2 and abort
2248 * the search if it is not.
2250 static int is_disjoint_entry(void **entry, void *user)
2252 struct isl_union_map_is_disjoint_data *data = user;
2253 uint32_t hash;
2254 struct isl_hash_table_entry *entry2;
2255 isl_map *map = *entry;
2257 hash = isl_space_get_hash(map->dim);
2258 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2259 hash, &has_dim, map->dim, 0);
2260 if (!entry2)
2261 return 0;
2263 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2264 if (data->is_disjoint < 0 || !data->is_disjoint)
2265 return -1;
2267 return 0;
2270 /* Are "umap1" and "umap2" disjoint?
2272 int isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2273 __isl_keep isl_union_map *umap2)
2275 struct isl_union_map_is_disjoint_data data = { NULL, 1 };
2277 umap1 = isl_union_map_copy(umap1);
2278 umap2 = isl_union_map_copy(umap2);
2279 umap1 = isl_union_map_align_params(umap1,
2280 isl_union_map_get_space(umap2));
2281 umap2 = isl_union_map_align_params(umap2,
2282 isl_union_map_get_space(umap1));
2284 if (!umap1 || !umap2)
2285 goto error;
2287 data.umap2 = umap2;
2288 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2289 &is_disjoint_entry, &data) < 0 &&
2290 data.is_disjoint)
2291 goto error;
2293 isl_union_map_free(umap1);
2294 isl_union_map_free(umap2);
2296 return data.is_disjoint;
2297 error:
2298 isl_union_map_free(umap1);
2299 isl_union_map_free(umap2);
2300 return -1;
2303 /* Are "uset1" and "uset2" disjoint?
2305 int isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2306 __isl_keep isl_union_set *uset2)
2308 return isl_union_map_is_disjoint(uset1, uset2);
2311 static int sample_entry(void **entry, void *user)
2313 isl_basic_map **sample = (isl_basic_map **)user;
2314 isl_map *map = *entry;
2316 *sample = isl_map_sample(isl_map_copy(map));
2317 if (!*sample)
2318 return -1;
2319 if (!isl_basic_map_plain_is_empty(*sample))
2320 return -1;
2321 return 0;
2324 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2326 isl_basic_map *sample = NULL;
2328 if (!umap)
2329 return NULL;
2331 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2332 &sample_entry, &sample) < 0 &&
2333 !sample)
2334 goto error;
2336 if (!sample)
2337 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2339 isl_union_map_free(umap);
2341 return sample;
2342 error:
2343 isl_union_map_free(umap);
2344 return NULL;
2347 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2349 return (isl_basic_set *)isl_union_map_sample(uset);
2352 struct isl_forall_data {
2353 int res;
2354 int (*fn)(__isl_keep isl_map *map);
2357 static int forall_entry(void **entry, void *user)
2359 struct isl_forall_data *data = user;
2360 isl_map *map = *entry;
2362 data->res = data->fn(map);
2363 if (data->res < 0)
2364 return -1;
2366 if (!data->res)
2367 return -1;
2369 return 0;
2372 static int union_map_forall(__isl_keep isl_union_map *umap,
2373 int (*fn)(__isl_keep isl_map *map))
2375 struct isl_forall_data data = { 1, fn };
2377 if (!umap)
2378 return -1;
2380 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2381 &forall_entry, &data) < 0 && data.res)
2382 return -1;
2384 return data.res;
2387 struct isl_forall_user_data {
2388 int res;
2389 int (*fn)(__isl_keep isl_map *map, void *user);
2390 void *user;
2393 static int forall_user_entry(void **entry, void *user)
2395 struct isl_forall_user_data *data = user;
2396 isl_map *map = *entry;
2398 data->res = data->fn(map, data->user);
2399 if (data->res < 0)
2400 return -1;
2402 if (!data->res)
2403 return -1;
2405 return 0;
2408 /* Check if fn(map, user) returns true for all maps "map" in umap.
2410 static int union_map_forall_user(__isl_keep isl_union_map *umap,
2411 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
2413 struct isl_forall_user_data data = { 1, fn, user };
2415 if (!umap)
2416 return -1;
2418 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2419 &forall_user_entry, &data) < 0 && data.res)
2420 return -1;
2422 return data.res;
2425 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2427 return union_map_forall(umap, &isl_map_is_empty);
2430 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2432 return isl_union_map_is_empty(uset);
2435 static int is_subset_of_identity(__isl_keep isl_map *map)
2437 int is_subset;
2438 isl_space *dim;
2439 isl_map *id;
2441 if (!map)
2442 return -1;
2444 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2445 map->dim, isl_dim_out))
2446 return 0;
2448 dim = isl_map_get_space(map);
2449 id = isl_map_identity(dim);
2451 is_subset = isl_map_is_subset(map, id);
2453 isl_map_free(id);
2455 return is_subset;
2458 /* Given an isl_union_map that consists of a single map, check
2459 * if it is single-valued.
2461 static int single_map_is_single_valued(__isl_keep isl_union_map *umap)
2463 isl_map *map;
2464 int sv;
2466 umap = isl_union_map_copy(umap);
2467 map = isl_map_from_union_map(umap);
2468 sv = isl_map_is_single_valued(map);
2469 isl_map_free(map);
2471 return sv;
2474 /* Internal data structure for single_valued_on_domain.
2476 * "umap" is the union map to be tested.
2477 * "sv" is set to 1 as long as "umap" may still be single-valued.
2479 struct isl_union_map_is_sv_data {
2480 isl_union_map *umap;
2481 int sv;
2484 /* Check if the data->umap is single-valued on "set".
2486 * If data->umap consists of a single map on "set", then test it
2487 * as an isl_map.
2489 * Otherwise, compute
2491 * M \circ M^-1
2493 * check if the result is a subset of the identity mapping and
2494 * store the result in data->sv.
2496 * Terminate as soon as data->umap has been determined not to
2497 * be single-valued.
2499 static int single_valued_on_domain(__isl_take isl_set *set, void *user)
2501 struct isl_union_map_is_sv_data *data = user;
2502 isl_union_map *umap, *test;
2504 umap = isl_union_map_copy(data->umap);
2505 umap = isl_union_map_intersect_domain(umap,
2506 isl_union_set_from_set(set));
2508 if (isl_union_map_n_map(umap) == 1) {
2509 data->sv = single_map_is_single_valued(umap);
2510 isl_union_map_free(umap);
2511 } else {
2512 test = isl_union_map_reverse(isl_union_map_copy(umap));
2513 test = isl_union_map_apply_range(test, umap);
2515 data->sv = union_map_forall(test, &is_subset_of_identity);
2517 isl_union_map_free(test);
2520 if (data->sv < 0 || !data->sv)
2521 return -1;
2522 return 0;
2525 /* Check if the given map is single-valued.
2527 * If the union map consists of a single map, then test it as an isl_map.
2528 * Otherwise, check if the union map is single-valued on each of its
2529 * domain spaces.
2531 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2533 isl_union_map *universe;
2534 isl_union_set *domain;
2535 struct isl_union_map_is_sv_data data;
2537 if (isl_union_map_n_map(umap) == 1)
2538 return single_map_is_single_valued(umap);
2540 universe = isl_union_map_universe(isl_union_map_copy(umap));
2541 domain = isl_union_map_domain(universe);
2543 data.sv = 1;
2544 data.umap = umap;
2545 if (isl_union_set_foreach_set(domain,
2546 &single_valued_on_domain, &data) < 0 && data.sv)
2547 data.sv = -1;
2548 isl_union_set_free(domain);
2550 return data.sv;
2553 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2555 int in;
2557 umap = isl_union_map_copy(umap);
2558 umap = isl_union_map_reverse(umap);
2559 in = isl_union_map_is_single_valued(umap);
2560 isl_union_map_free(umap);
2562 return in;
2565 /* Represents a map that has a fixed value (v) for one of its
2566 * range dimensions.
2567 * The map in this structure is not reference counted, so it
2568 * is only valid while the isl_union_map from which it was
2569 * obtained is still alive.
2571 struct isl_fixed_map {
2572 isl_int v;
2573 isl_map *map;
2576 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2577 int n)
2579 int i;
2580 struct isl_fixed_map *v;
2582 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2583 if (!v)
2584 return NULL;
2585 for (i = 0; i < n; ++i)
2586 isl_int_init(v[i].v);
2587 return v;
2590 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2592 int i;
2594 if (!v)
2595 return;
2596 for (i = 0; i < n; ++i)
2597 isl_int_clear(v[i].v);
2598 free(v);
2601 /* Compare the "v" field of two isl_fixed_map structs.
2603 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2605 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2606 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2608 return isl_int_cmp(e1->v, e2->v);
2611 /* Internal data structure used while checking whether all maps
2612 * in a union_map have a fixed value for a given output dimension.
2613 * v is the list of maps, with the fixed value for the dimension
2614 * n is the number of maps considered so far
2615 * pos is the output dimension under investigation
2617 struct isl_fixed_dim_data {
2618 struct isl_fixed_map *v;
2619 int n;
2620 int pos;
2623 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2625 struct isl_fixed_dim_data *data = user;
2627 data->v[data->n].map = map;
2628 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2629 &data->v[data->n++].v);
2632 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2633 int first, int n_range);
2635 /* Given a list of the maps, with their fixed values at output dimension "pos",
2636 * check whether the ranges of the maps form an obvious partition.
2638 * We first sort the maps according to their fixed values.
2639 * If all maps have a different value, then we know the ranges form
2640 * a partition.
2641 * Otherwise, we collect the maps with the same fixed value and
2642 * check whether each such collection is obviously injective
2643 * based on later dimensions.
2645 static int separates(struct isl_fixed_map *v, int n,
2646 __isl_take isl_space *dim, int pos, int n_range)
2648 int i;
2650 if (!v)
2651 goto error;
2653 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2655 for (i = 0; i + 1 < n; ++i) {
2656 int j, k;
2657 isl_union_map *part;
2658 int injective;
2660 for (j = i + 1; j < n; ++j)
2661 if (isl_int_ne(v[i].v, v[j].v))
2662 break;
2664 if (j == i + 1)
2665 continue;
2667 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2668 for (k = i; k < j; ++k)
2669 part = isl_union_map_add_map(part,
2670 isl_map_copy(v[k].map));
2672 injective = plain_injective_on_range(part, pos + 1, n_range);
2673 if (injective < 0)
2674 goto error;
2675 if (!injective)
2676 break;
2678 i = j - 1;
2681 isl_space_free(dim);
2682 free_isl_fixed_map_array(v, n);
2683 return i + 1 >= n;
2684 error:
2685 isl_space_free(dim);
2686 free_isl_fixed_map_array(v, n);
2687 return -1;
2690 /* Check whether the maps in umap have obviously distinct ranges.
2691 * In particular, check for an output dimension in the range
2692 * [first,n_range) for which all maps have a fixed value
2693 * and then check if these values, possibly along with fixed values
2694 * at later dimensions, entail distinct ranges.
2696 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2697 int first, int n_range)
2699 isl_ctx *ctx;
2700 int n;
2701 struct isl_fixed_dim_data data = { NULL };
2703 ctx = isl_union_map_get_ctx(umap);
2705 n = isl_union_map_n_map(umap);
2706 if (!umap)
2707 goto error;
2709 if (n <= 1) {
2710 isl_union_map_free(umap);
2711 return 1;
2714 if (first >= n_range) {
2715 isl_union_map_free(umap);
2716 return 0;
2719 data.v = alloc_isl_fixed_map_array(ctx, n);
2720 if (!data.v)
2721 goto error;
2723 for (data.pos = first; data.pos < n_range; ++data.pos) {
2724 int fixed;
2725 int injective;
2726 isl_space *dim;
2728 data.n = 0;
2729 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2730 if (fixed < 0)
2731 goto error;
2732 if (!fixed)
2733 continue;
2734 dim = isl_union_map_get_space(umap);
2735 injective = separates(data.v, n, dim, data.pos, n_range);
2736 isl_union_map_free(umap);
2737 return injective;
2740 free_isl_fixed_map_array(data.v, n);
2741 isl_union_map_free(umap);
2743 return 0;
2744 error:
2745 free_isl_fixed_map_array(data.v, n);
2746 isl_union_map_free(umap);
2747 return -1;
2750 /* Check whether the maps in umap that map to subsets of "ran"
2751 * have obviously distinct ranges.
2753 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2755 isl_union_map *umap = user;
2757 umap = isl_union_map_copy(umap);
2758 umap = isl_union_map_intersect_range(umap,
2759 isl_union_set_from_set(isl_set_copy(ran)));
2760 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2763 /* Check if the given union_map is obviously injective.
2765 * In particular, we first check if all individual maps are obviously
2766 * injective and then check if all the ranges of these maps are
2767 * obviously disjoint.
2769 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2771 int in;
2772 isl_union_map *univ;
2773 isl_union_set *ran;
2775 in = union_map_forall(umap, &isl_map_plain_is_injective);
2776 if (in < 0)
2777 return -1;
2778 if (!in)
2779 return 0;
2781 univ = isl_union_map_universe(isl_union_map_copy(umap));
2782 ran = isl_union_map_range(univ);
2784 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2786 isl_union_set_free(ran);
2788 return in;
2791 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2793 int sv;
2795 sv = isl_union_map_is_single_valued(umap);
2796 if (sv < 0 || !sv)
2797 return sv;
2799 return isl_union_map_is_injective(umap);
2802 static int zip_entry(void **entry, void *user)
2804 isl_map *map = *entry;
2805 isl_union_map **res = user;
2807 if (!isl_map_can_zip(map))
2808 return 0;
2810 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2812 return 0;
2815 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2817 return cond_un_op(umap, &zip_entry);
2820 static int uncurry_entry(void **entry, void *user)
2822 isl_map *map = *entry;
2823 isl_union_map **res = user;
2825 if (!isl_map_can_uncurry(map))
2826 return 0;
2828 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
2830 return 0;
2833 /* Given a union map, take the maps of the form A -> (B -> C) and
2834 * return the union of the corresponding maps (A -> B) -> C.
2836 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2838 return cond_un_op(umap, &uncurry_entry);
2841 static int curry_entry(void **entry, void *user)
2843 isl_map *map = *entry;
2844 isl_union_map **res = user;
2846 if (!isl_map_can_curry(map))
2847 return 0;
2849 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2851 return 0;
2854 /* Given a union map, take the maps of the form (A -> B) -> C and
2855 * return the union of the corresponding maps A -> (B -> C).
2857 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2859 return cond_un_op(umap, &curry_entry);
2862 static int lift_entry(void **entry, void *user)
2864 isl_set *set = *entry;
2865 isl_union_set **res = user;
2867 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2869 return 0;
2872 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2874 return cond_un_op(uset, &lift_entry);
2877 static int coefficients_entry(void **entry, void *user)
2879 isl_set *set = *entry;
2880 isl_union_set **res = user;
2882 set = isl_set_copy(set);
2883 set = isl_set_from_basic_set(isl_set_coefficients(set));
2884 *res = isl_union_set_add_set(*res, set);
2886 return 0;
2889 __isl_give isl_union_set *isl_union_set_coefficients(
2890 __isl_take isl_union_set *uset)
2892 isl_ctx *ctx;
2893 isl_space *dim;
2894 isl_union_set *res;
2896 if (!uset)
2897 return NULL;
2899 ctx = isl_union_set_get_ctx(uset);
2900 dim = isl_space_set_alloc(ctx, 0, 0);
2901 res = isl_union_map_alloc(dim, uset->table.n);
2902 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2903 &coefficients_entry, &res) < 0)
2904 goto error;
2906 isl_union_set_free(uset);
2907 return res;
2908 error:
2909 isl_union_set_free(uset);
2910 isl_union_set_free(res);
2911 return NULL;
2914 static int solutions_entry(void **entry, void *user)
2916 isl_set *set = *entry;
2917 isl_union_set **res = user;
2919 set = isl_set_copy(set);
2920 set = isl_set_from_basic_set(isl_set_solutions(set));
2921 if (!*res)
2922 *res = isl_union_set_from_set(set);
2923 else
2924 *res = isl_union_set_add_set(*res, set);
2926 if (!*res)
2927 return -1;
2929 return 0;
2932 __isl_give isl_union_set *isl_union_set_solutions(
2933 __isl_take isl_union_set *uset)
2935 isl_union_set *res = NULL;
2937 if (!uset)
2938 return NULL;
2940 if (uset->table.n == 0) {
2941 res = isl_union_set_empty(isl_union_set_get_space(uset));
2942 isl_union_set_free(uset);
2943 return res;
2946 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2947 &solutions_entry, &res) < 0)
2948 goto error;
2950 isl_union_set_free(uset);
2951 return res;
2952 error:
2953 isl_union_set_free(uset);
2954 isl_union_set_free(res);
2955 return NULL;
2958 /* Is the domain space of "map" equal to "space"?
2960 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2962 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2963 space, isl_dim_out);
2966 /* Is the range space of "map" equal to "space"?
2968 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2970 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
2971 space, isl_dim_out);
2974 /* Is the set space of "map" equal to "space"?
2976 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2978 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
2979 space, isl_dim_out);
2982 /* Internal data structure for preimage_pw_multi_aff.
2984 * "pma" is the function under which the preimage should be taken.
2985 * "space" is the space of "pma".
2986 * "res" collects the results.
2987 * "fn" computes the preimage for a given map.
2988 * "match" returns true if "fn" can be called.
2990 struct isl_union_map_preimage_data {
2991 isl_space *space;
2992 isl_pw_multi_aff *pma;
2993 isl_union_map *res;
2994 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2995 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2996 __isl_take isl_pw_multi_aff *pma);
2999 /* Call data->fn to compute the preimage of the domain or range of *entry
3000 * under the function represented by data->pma, provided the domain/range
3001 * space of *entry matches the target space of data->pma
3002 * (as given by data->match), and add the result to data->res.
3004 static int preimage_entry(void **entry, void *user)
3006 int m;
3007 isl_map *map = *entry;
3008 struct isl_union_map_preimage_data *data = user;
3009 int empty;
3011 m = data->match(map, data->space);
3012 if (m < 0)
3013 return -1;
3014 if (!m)
3015 return 0;
3017 map = isl_map_copy(map);
3018 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3020 empty = isl_map_is_empty(map);
3021 if (empty < 0 || empty) {
3022 isl_map_free(map);
3023 return empty < 0 ? -1 : 0;
3026 data->res = isl_union_map_add_map(data->res, map);
3028 return 0;
3031 /* Compute the preimage of the domain or range of "umap" under the function
3032 * represented by "pma".
3033 * In other words, plug in "pma" in the domain or range of "umap".
3034 * The function "fn" performs the actual preimage computation on a map,
3035 * while "match" determines to which maps the function should be applied.
3037 static __isl_give isl_union_map *preimage_pw_multi_aff(
3038 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3039 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3040 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3041 __isl_take isl_pw_multi_aff *pma))
3043 isl_ctx *ctx;
3044 isl_space *space;
3045 struct isl_union_map_preimage_data data;
3047 umap = isl_union_map_align_params(umap,
3048 isl_pw_multi_aff_get_space(pma));
3049 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3051 if (!umap || !pma)
3052 goto error;
3054 ctx = isl_union_map_get_ctx(umap);
3055 space = isl_union_map_get_space(umap);
3056 data.space = isl_pw_multi_aff_get_space(pma);
3057 data.pma = pma;
3058 data.res = isl_union_map_alloc(space, umap->table.n);
3059 data.match = match;
3060 data.fn = fn;
3061 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3062 &data) < 0)
3063 data.res = isl_union_map_free(data.res);
3065 isl_space_free(data.space);
3066 isl_union_map_free(umap);
3067 isl_pw_multi_aff_free(pma);
3068 return data.res;
3069 error:
3070 isl_union_map_free(umap);
3071 isl_pw_multi_aff_free(pma);
3072 return NULL;
3075 /* Compute the preimage of the domain of "umap" under the function
3076 * represented by "pma".
3077 * In other words, plug in "pma" in the domain of "umap".
3078 * The result contains maps that live in the same spaces as the maps of "umap"
3079 * with domain space equal to the target space of "pma",
3080 * except that the domain has been replaced by the domain space of "pma".
3082 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3083 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3085 return preimage_pw_multi_aff(umap, pma, &domain_match,
3086 &isl_map_preimage_domain_pw_multi_aff);
3089 /* Compute the preimage of the range of "umap" under the function
3090 * represented by "pma".
3091 * In other words, plug in "pma" in the range of "umap".
3092 * The result contains maps that live in the same spaces as the maps of "umap"
3093 * with range space equal to the target space of "pma",
3094 * except that the range has been replaced by the domain space of "pma".
3096 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3097 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3099 return preimage_pw_multi_aff(umap, pma, &range_match,
3100 &isl_map_preimage_range_pw_multi_aff);
3103 /* Compute the preimage of "uset" under the function represented by "pma".
3104 * In other words, plug in "pma" in "uset".
3105 * The result contains sets that live in the same spaces as the sets of "uset"
3106 * with space equal to the target space of "pma",
3107 * except that the space has been replaced by the domain space of "pma".
3109 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3110 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3112 return preimage_pw_multi_aff(uset, pma, &set_match,
3113 &isl_set_preimage_pw_multi_aff);
3116 /* Compute the preimage of the domain of "umap" under the function
3117 * represented by "ma".
3118 * In other words, plug in "ma" in the domain of "umap".
3119 * The result contains maps that live in the same spaces as the maps of "umap"
3120 * with domain space equal to the target space of "ma",
3121 * except that the domain has been replaced by the domain space of "ma".
3123 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3124 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3126 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3127 isl_pw_multi_aff_from_multi_aff(ma));
3130 /* Compute the preimage of the range of "umap" under the function
3131 * represented by "ma".
3132 * In other words, plug in "ma" in the range of "umap".
3133 * The result contains maps that live in the same spaces as the maps of "umap"
3134 * with range space equal to the target space of "ma",
3135 * except that the range has been replaced by the domain space of "ma".
3137 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3138 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3140 return isl_union_map_preimage_range_pw_multi_aff(umap,
3141 isl_pw_multi_aff_from_multi_aff(ma));
3144 /* Compute the preimage of "uset" under the function represented by "ma".
3145 * In other words, plug in "ma" in "uset".
3146 * The result contains sets that live in the same spaces as the sets of "uset"
3147 * with space equal to the target space of "ma",
3148 * except that the space has been replaced by the domain space of "ma".
3150 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3151 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3153 return isl_union_set_preimage_pw_multi_aff(uset,
3154 isl_pw_multi_aff_from_multi_aff(ma));
3157 /* Internal data structure for preimage_multi_pw_aff.
3159 * "mpa" is the function under which the preimage should be taken.
3160 * "space" is the space of "mpa".
3161 * "res" collects the results.
3162 * "fn" computes the preimage for a given map.
3163 * "match" returns true if "fn" can be called.
3165 struct isl_union_map_preimage_mpa_data {
3166 isl_space *space;
3167 isl_multi_pw_aff *mpa;
3168 isl_union_map *res;
3169 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3170 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3171 __isl_take isl_multi_pw_aff *mpa);
3174 /* Call data->fn to compute the preimage of the domain or range of *entry
3175 * under the function represented by data->mpa, provided the domain/range
3176 * space of *entry matches the target space of data->mpa
3177 * (as given by data->match), and add the result to data->res.
3179 static int preimage_mpa_entry(void **entry, void *user)
3181 int m;
3182 isl_map *map = *entry;
3183 struct isl_union_map_preimage_mpa_data *data = user;
3184 int empty;
3186 m = data->match(map, data->space);
3187 if (m < 0)
3188 return -1;
3189 if (!m)
3190 return 0;
3192 map = isl_map_copy(map);
3193 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3195 empty = isl_map_is_empty(map);
3196 if (empty < 0 || empty) {
3197 isl_map_free(map);
3198 return empty < 0 ? -1 : 0;
3201 data->res = isl_union_map_add_map(data->res, map);
3203 return 0;
3206 /* Compute the preimage of the domain or range of "umap" under the function
3207 * represented by "mpa".
3208 * In other words, plug in "mpa" in the domain or range of "umap".
3209 * The function "fn" performs the actual preimage computation on a map,
3210 * while "match" determines to which maps the function should be applied.
3212 static __isl_give isl_union_map *preimage_multi_pw_aff(
3213 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3214 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3215 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3216 __isl_take isl_multi_pw_aff *mpa))
3218 isl_ctx *ctx;
3219 isl_space *space;
3220 struct isl_union_map_preimage_mpa_data data;
3222 umap = isl_union_map_align_params(umap,
3223 isl_multi_pw_aff_get_space(mpa));
3224 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3226 if (!umap || !mpa)
3227 goto error;
3229 ctx = isl_union_map_get_ctx(umap);
3230 space = isl_union_map_get_space(umap);
3231 data.space = isl_multi_pw_aff_get_space(mpa);
3232 data.mpa = mpa;
3233 data.res = isl_union_map_alloc(space, umap->table.n);
3234 data.match = match;
3235 data.fn = fn;
3236 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3237 &data) < 0)
3238 data.res = isl_union_map_free(data.res);
3240 isl_space_free(data.space);
3241 isl_union_map_free(umap);
3242 isl_multi_pw_aff_free(mpa);
3243 return data.res;
3244 error:
3245 isl_union_map_free(umap);
3246 isl_multi_pw_aff_free(mpa);
3247 return NULL;
3250 /* Compute the preimage of the domain of "umap" under the function
3251 * represented by "mpa".
3252 * In other words, plug in "mpa" in the domain of "umap".
3253 * The result contains maps that live in the same spaces as the maps of "umap"
3254 * with domain space equal to the target space of "mpa",
3255 * except that the domain has been replaced by the domain space of "mpa".
3257 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3258 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3260 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3261 &isl_map_preimage_domain_multi_pw_aff);
3264 /* Internal data structure for preimage_upma.
3266 * "umap" is the map of which the preimage should be computed.
3267 * "res" collects the results.
3268 * "fn" computes the preimage for a given piecewise multi-affine function.
3270 struct isl_union_map_preimage_upma_data {
3271 isl_union_map *umap;
3272 isl_union_map *res;
3273 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3274 __isl_take isl_pw_multi_aff *pma);
3277 /* Call data->fn to compute the preimage of the domain or range of data->umap
3278 * under the function represented by pma and add the result to data->res.
3280 static int preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3282 struct isl_union_map_preimage_upma_data *data = user;
3283 isl_union_map *umap;
3285 umap = isl_union_map_copy(data->umap);
3286 umap = data->fn(umap, pma);
3287 data->res = isl_union_map_union(data->res, umap);
3289 return data->res ? 0 : -1;
3292 /* Compute the preimage of the domain or range of "umap" under the function
3293 * represented by "upma".
3294 * In other words, plug in "upma" in the domain or range of "umap".
3295 * The function "fn" performs the actual preimage computation
3296 * on a piecewise multi-affine function.
3298 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3299 __isl_take isl_union_map *umap,
3300 __isl_take isl_union_pw_multi_aff *upma,
3301 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3302 __isl_take isl_pw_multi_aff *pma))
3304 struct isl_union_map_preimage_upma_data data;
3306 data.umap = umap;
3307 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3308 data.fn = fn;
3309 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3310 &preimage_upma, &data) < 0)
3311 data.res = isl_union_map_free(data.res);
3313 isl_union_map_free(umap);
3314 isl_union_pw_multi_aff_free(upma);
3316 return data.res;
3319 /* Compute the preimage of the domain of "umap" under the function
3320 * represented by "upma".
3321 * In other words, plug in "upma" in the domain of "umap".
3322 * The result contains maps that live in the same spaces as the maps of "umap"
3323 * with domain space equal to one of the target spaces of "upma",
3324 * except that the domain has been replaced by one of the the domain spaces that
3325 * corresponds to that target space of "upma".
3327 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3328 __isl_take isl_union_map *umap,
3329 __isl_take isl_union_pw_multi_aff *upma)
3331 return preimage_union_pw_multi_aff(umap, upma,
3332 &isl_union_map_preimage_domain_pw_multi_aff);
3335 /* Compute the preimage of the range of "umap" under the function
3336 * represented by "upma".
3337 * In other words, plug in "upma" in the range of "umap".
3338 * The result contains maps that live in the same spaces as the maps of "umap"
3339 * with range space equal to one of the target spaces of "upma",
3340 * except that the range has been replaced by one of the the domain spaces that
3341 * corresponds to that target space of "upma".
3343 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3344 __isl_take isl_union_map *umap,
3345 __isl_take isl_union_pw_multi_aff *upma)
3347 return preimage_union_pw_multi_aff(umap, upma,
3348 &isl_union_map_preimage_range_pw_multi_aff);
3351 /* Compute the preimage of "uset" under the function represented by "upma".
3352 * In other words, plug in "upma" in the range of "uset".
3353 * The result contains sets that live in the same spaces as the sets of "uset"
3354 * with space equal to one of the target spaces of "upma",
3355 * except that the space has been replaced by one of the the domain spaces that
3356 * corresponds to that target space of "upma".
3358 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3359 __isl_take isl_union_set *uset,
3360 __isl_take isl_union_pw_multi_aff *upma)
3362 return preimage_union_pw_multi_aff(uset, upma,
3363 &isl_union_set_preimage_pw_multi_aff);
3366 /* Reset the user pointer on all identifiers of parameters and tuples
3367 * of the space of *entry.
3369 static int reset_user(void **entry, void *user)
3371 isl_map **map = (isl_map **)entry;
3373 *map = isl_map_reset_user(*map);
3375 return *map ? 0 : -1;
3378 /* Reset the user pointer on all identifiers of parameters and tuples
3379 * of the spaces of "umap".
3381 __isl_give isl_union_map *isl_union_map_reset_user(
3382 __isl_take isl_union_map *umap)
3384 umap = isl_union_map_cow(umap);
3385 if (!umap)
3386 return NULL;
3387 umap->dim = isl_space_reset_user(umap->dim);
3388 if (!umap->dim)
3389 return isl_union_map_free(umap);
3390 umap = un_op(umap, &reset_user);
3392 return umap;
3395 /* Reset the user pointer on all identifiers of parameters and tuples
3396 * of the spaces of "uset".
3398 __isl_give isl_union_set *isl_union_set_reset_user(
3399 __isl_take isl_union_set *uset)
3401 return isl_union_map_reset_user(uset);
3404 /* Internal data structure for isl_union_map_project_out.
3405 * "type", "first" and "n" are the arguments for the isl_map_project_out
3406 * call.
3407 * "res" collects the results.
3409 struct isl_union_map_project_out_data {
3410 enum isl_dim_type type;
3411 unsigned first;
3412 unsigned n;
3414 isl_union_map *res;
3417 /* Turn the data->n dimensions of type data->type, starting at data->first
3418 * into existentially quantified variables and add the result to data->res.
3420 static int project_out(__isl_take isl_map *map, void *user)
3422 struct isl_union_map_project_out_data *data = user;
3424 map = isl_map_project_out(map, data->type, data->first, data->n);
3425 data->res = isl_union_map_add_map(data->res, map);
3427 return 0;
3430 /* Turn the "n" dimensions of type "type", starting at "first"
3431 * into existentially quantified variables.
3432 * Since the space of an isl_union_map only contains parameters,
3433 * type is required to be equal to isl_dim_param.
3435 __isl_give isl_union_map *isl_union_map_project_out(
3436 __isl_take isl_union_map *umap,
3437 enum isl_dim_type type, unsigned first, unsigned n)
3439 isl_space *space;
3440 struct isl_union_map_project_out_data data = { type, first, n };
3442 if (!umap)
3443 return NULL;
3445 if (type != isl_dim_param)
3446 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3447 "can only project out parameters",
3448 return isl_union_map_free(umap));
3450 space = isl_union_map_get_space(umap);
3451 space = isl_space_drop_dims(space, type, first, n);
3452 data.res = isl_union_map_empty(space);
3453 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3454 data.res = isl_union_map_free(data.res);
3456 isl_union_map_free(umap);
3458 return data.res;
3461 /* Turn the "n" dimensions of type "type", starting at "first"
3462 * into existentially quantified variables.
3463 * Since the space of an isl_union_set only contains parameters,
3464 * "type" is required to be equal to isl_dim_param.
3466 __isl_give isl_union_set *isl_union_set_project_out(
3467 __isl_take isl_union_set *uset,
3468 enum isl_dim_type type, unsigned first, unsigned n)
3470 return isl_union_map_project_out(uset, type, first, n);
3473 /* Internal data structure for isl_union_map_involves_dims.
3474 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3476 struct isl_union_map_involves_dims_data {
3477 unsigned first;
3478 unsigned n;
3481 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3483 static int map_excludes(__isl_keep isl_map *map, void *user)
3485 struct isl_union_map_involves_dims_data *data = user;
3486 int involves;
3488 involves = isl_map_involves_dims(map,
3489 isl_dim_param, data->first, data->n);
3490 if (involves < 0)
3491 return -1;
3492 return !involves;
3495 /* Does "umap" involve any of the n parameters starting at first?
3496 * "type" is required to be set to isl_dim_param.
3498 * "umap" involves any of those parameters if any of its maps
3499 * involve the parameters. In other words, "umap" does not
3500 * involve any of the parameters if all its maps to not
3501 * involve the parameters.
3503 int isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3504 enum isl_dim_type type, unsigned first, unsigned n)
3506 struct isl_union_map_involves_dims_data data = { first, n };
3507 int excludes;
3509 if (type != isl_dim_param)
3510 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3511 "can only reference parameters", return 0);
3513 excludes = union_map_forall_user(umap, &map_excludes, &data);
3515 if (excludes < 0)
3516 return -1;
3518 return !excludes;
3521 /* Return the union of the elements in the list "list".
3523 __isl_give isl_union_set *isl_union_set_list_union(
3524 __isl_take isl_union_set_list *list)
3526 int i, n;
3527 isl_ctx *ctx;
3528 isl_space *space;
3529 isl_union_set *res;
3531 if (!list)
3532 return NULL;
3534 ctx = isl_union_set_list_get_ctx(list);
3535 space = isl_space_params_alloc(ctx, 0);
3536 res = isl_union_set_empty(space);
3538 n = isl_union_set_list_n_union_set(list);
3539 for (i = 0; i < n; ++i) {
3540 isl_union_set *uset_i;
3542 uset_i = isl_union_set_list_get_union_set(list, i);
3543 res = isl_union_set_union(res, uset_i);
3546 isl_union_set_list_free(list);
3547 return res;