add isl_union_map_range_factor_range
[isl.git] / isl_union_map.c
blobd4f9568a4f22cd7855709cf662390945747509ba
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/ctx.h>
18 #include <isl/hash.h>
19 #include <isl/aff.h>
20 #include <isl/map.h>
21 #include <isl/set.h>
22 #include <isl_space_private.h>
23 #include <isl_union_map_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 static int flat_range_product_entry(void **entry, void *user)
1426 struct isl_union_map_bin_data *data = user;
1427 isl_map *map2 = *entry;
1429 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1430 map2->dim, isl_dim_in))
1431 return 0;
1433 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1434 isl_map_copy(map2));
1436 data->res = isl_union_map_add_map(data->res, map2);
1438 return 0;
1441 __isl_give isl_union_map *isl_union_map_flat_range_product(
1442 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1444 return bin_op(umap1, umap2, &flat_range_product_entry);
1447 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1448 int (*fn)(void **, void *))
1450 isl_union_set *res;
1452 if (!umap)
1453 return NULL;
1455 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1456 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1457 goto error;
1459 isl_union_map_free(umap);
1460 return res;
1461 error:
1462 isl_union_map_free(umap);
1463 isl_union_set_free(res);
1464 return NULL;
1467 static int from_range_entry(void **entry, void *user)
1469 isl_map *set = *entry;
1470 isl_union_set **res = user;
1472 *res = isl_union_map_add_map(*res,
1473 isl_map_from_range(isl_set_copy(set)));
1475 return 0;
1478 __isl_give isl_union_map *isl_union_map_from_range(
1479 __isl_take isl_union_set *uset)
1481 return cond_un_op(uset, &from_range_entry);
1484 __isl_give isl_union_map *isl_union_map_from_domain(
1485 __isl_take isl_union_set *uset)
1487 return isl_union_map_reverse(isl_union_map_from_range(uset));
1490 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1491 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1493 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1494 isl_union_map_from_range(range));
1497 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1498 int (*fn)(void **, void *))
1500 umap = isl_union_map_cow(umap);
1501 if (!umap)
1502 return NULL;
1504 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
1505 goto error;
1507 return umap;
1508 error:
1509 isl_union_map_free(umap);
1510 return NULL;
1513 static int affine_entry(void **entry, void *user)
1515 isl_map **map = (isl_map **)entry;
1517 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1519 return *map ? 0 : -1;
1522 __isl_give isl_union_map *isl_union_map_affine_hull(
1523 __isl_take isl_union_map *umap)
1525 return un_op(umap, &affine_entry);
1528 __isl_give isl_union_set *isl_union_set_affine_hull(
1529 __isl_take isl_union_set *uset)
1531 return isl_union_map_affine_hull(uset);
1534 static int polyhedral_entry(void **entry, void *user)
1536 isl_map **map = (isl_map **)entry;
1538 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1540 return *map ? 0 : -1;
1543 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1544 __isl_take isl_union_map *umap)
1546 return un_op(umap, &polyhedral_entry);
1549 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1550 __isl_take isl_union_set *uset)
1552 return isl_union_map_polyhedral_hull(uset);
1555 static int simple_entry(void **entry, void *user)
1557 isl_map **map = (isl_map **)entry;
1559 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1561 return *map ? 0 : -1;
1564 __isl_give isl_union_map *isl_union_map_simple_hull(
1565 __isl_take isl_union_map *umap)
1567 return un_op(umap, &simple_entry);
1570 __isl_give isl_union_set *isl_union_set_simple_hull(
1571 __isl_take isl_union_set *uset)
1573 return isl_union_map_simple_hull(uset);
1576 static int inplace_entry(void **entry, void *user)
1578 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1579 isl_map **map = (isl_map **)entry;
1580 isl_map *copy;
1582 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1583 copy = fn(isl_map_copy(*map));
1584 if (!copy)
1585 return -1;
1587 isl_map_free(*map);
1588 *map = copy;
1590 return 0;
1593 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1594 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1596 if (!umap)
1597 return NULL;
1599 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1600 &inplace_entry, &fn) < 0)
1601 goto error;
1603 return umap;
1604 error:
1605 isl_union_map_free(umap);
1606 return NULL;
1609 __isl_give isl_union_map *isl_union_map_coalesce(
1610 __isl_take isl_union_map *umap)
1612 return inplace(umap, &isl_map_coalesce);
1615 __isl_give isl_union_set *isl_union_set_coalesce(
1616 __isl_take isl_union_set *uset)
1618 return isl_union_map_coalesce(uset);
1621 __isl_give isl_union_map *isl_union_map_detect_equalities(
1622 __isl_take isl_union_map *umap)
1624 return inplace(umap, &isl_map_detect_equalities);
1627 __isl_give isl_union_set *isl_union_set_detect_equalities(
1628 __isl_take isl_union_set *uset)
1630 return isl_union_map_detect_equalities(uset);
1633 __isl_give isl_union_map *isl_union_map_compute_divs(
1634 __isl_take isl_union_map *umap)
1636 return inplace(umap, &isl_map_compute_divs);
1639 __isl_give isl_union_set *isl_union_set_compute_divs(
1640 __isl_take isl_union_set *uset)
1642 return isl_union_map_compute_divs(uset);
1645 static int lexmin_entry(void **entry, void *user)
1647 isl_map **map = (isl_map **)entry;
1649 *map = isl_map_lexmin(*map);
1651 return *map ? 0 : -1;
1654 __isl_give isl_union_map *isl_union_map_lexmin(
1655 __isl_take isl_union_map *umap)
1657 return un_op(umap, &lexmin_entry);
1660 __isl_give isl_union_set *isl_union_set_lexmin(
1661 __isl_take isl_union_set *uset)
1663 return isl_union_map_lexmin(uset);
1666 static int lexmax_entry(void **entry, void *user)
1668 isl_map **map = (isl_map **)entry;
1670 *map = isl_map_lexmax(*map);
1672 return *map ? 0 : -1;
1675 __isl_give isl_union_map *isl_union_map_lexmax(
1676 __isl_take isl_union_map *umap)
1678 return un_op(umap, &lexmax_entry);
1681 __isl_give isl_union_set *isl_union_set_lexmax(
1682 __isl_take isl_union_set *uset)
1684 return isl_union_map_lexmax(uset);
1687 static int universe_entry(void **entry, void *user)
1689 isl_map *map = *entry;
1690 isl_union_map **res = user;
1692 map = isl_map_universe(isl_map_get_space(map));
1693 *res = isl_union_map_add_map(*res, map);
1695 return 0;
1698 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1700 return cond_un_op(umap, &universe_entry);
1703 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1705 return isl_union_map_universe(uset);
1708 static int reverse_entry(void **entry, void *user)
1710 isl_map *map = *entry;
1711 isl_union_map **res = user;
1713 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1715 return 0;
1718 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1720 return cond_un_op(umap, &reverse_entry);
1723 static int params_entry(void **entry, void *user)
1725 isl_map *map = *entry;
1726 isl_union_set **res = user;
1728 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1730 return 0;
1733 /* Compute the parameter domain of the given union map.
1735 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1737 int empty;
1739 empty = isl_union_map_is_empty(umap);
1740 if (empty < 0)
1741 goto error;
1742 if (empty) {
1743 isl_space *space;
1744 space = isl_union_map_get_space(umap);
1745 isl_union_map_free(umap);
1746 return isl_set_empty(space);
1748 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1749 error:
1750 isl_union_map_free(umap);
1751 return NULL;
1754 /* Compute the parameter domain of the given union set.
1756 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1758 return isl_union_map_params(uset);
1761 static int domain_entry(void **entry, void *user)
1763 isl_map *map = *entry;
1764 isl_union_set **res = user;
1766 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1768 return 0;
1771 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1773 return cond_un_op(umap, &domain_entry);
1776 static int range_entry(void **entry, void *user)
1778 isl_map *map = *entry;
1779 isl_union_set **res = user;
1781 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1783 return 0;
1786 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1788 return cond_un_op(umap, &range_entry);
1791 static int domain_map_entry(void **entry, void *user)
1793 isl_map *map = *entry;
1794 isl_union_set **res = user;
1796 *res = isl_union_map_add_map(*res,
1797 isl_map_domain_map(isl_map_copy(map)));
1799 return 0;
1802 __isl_give isl_union_map *isl_union_map_domain_map(
1803 __isl_take isl_union_map *umap)
1805 return cond_un_op(umap, &domain_map_entry);
1808 static int range_map_entry(void **entry, void *user)
1810 isl_map *map = *entry;
1811 isl_union_set **res = user;
1813 *res = isl_union_map_add_map(*res,
1814 isl_map_range_map(isl_map_copy(map)));
1816 return 0;
1819 __isl_give isl_union_map *isl_union_map_range_map(
1820 __isl_take isl_union_map *umap)
1822 return cond_un_op(umap, &range_map_entry);
1825 /* Check if "set" is of the form A[B -> C].
1826 * If so, add A[B -> C] -> B to "res".
1828 static int wrapped_domain_map_entry(void **entry, void *user)
1830 isl_set *set = *entry;
1831 isl_union_set **res = user;
1832 int wrapping;
1834 wrapping = isl_set_is_wrapping(set);
1835 if (wrapping < 0)
1836 return -1;
1837 if (!wrapping)
1838 return 0;
1840 *res = isl_union_map_add_map(*res,
1841 isl_set_wrapped_domain_map(isl_set_copy(set)));
1843 return 0;
1846 /* Given a collection of wrapped maps of the form A[B -> C],
1847 * return the collection of maps A[B -> C] -> B.
1849 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1850 __isl_take isl_union_set *uset)
1852 return cond_un_op(uset, &wrapped_domain_map_entry);
1855 static int deltas_entry(void **entry, void *user)
1857 isl_map *map = *entry;
1858 isl_union_set **res = user;
1860 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1861 map->dim, isl_dim_out))
1862 return 0;
1864 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1866 return 0;
1869 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1871 return cond_un_op(umap, &deltas_entry);
1874 static int deltas_map_entry(void **entry, void *user)
1876 isl_map *map = *entry;
1877 isl_union_map **res = user;
1879 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1880 map->dim, isl_dim_out))
1881 return 0;
1883 *res = isl_union_map_add_map(*res,
1884 isl_map_deltas_map(isl_map_copy(map)));
1886 return 0;
1889 __isl_give isl_union_map *isl_union_map_deltas_map(
1890 __isl_take isl_union_map *umap)
1892 return cond_un_op(umap, &deltas_map_entry);
1895 static int identity_entry(void **entry, void *user)
1897 isl_set *set = *entry;
1898 isl_union_map **res = user;
1900 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1902 return 0;
1905 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1907 return cond_un_op(uset, &identity_entry);
1910 /* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
1912 static int domain_factor_domain_entry(void **entry, void *user)
1914 isl_map *map = *entry;
1915 isl_union_map **res = user;
1917 if (!isl_map_domain_is_wrapping(map))
1918 return 0;
1920 *res = isl_union_map_add_map(*res,
1921 isl_map_domain_factor_domain(isl_map_copy(map)));
1923 return *res ? 0 : -1;
1926 /* For each map in "umap" of the form [A -> B] -> C,
1927 * construct the map A -> C and collect the results.
1929 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
1930 __isl_take isl_union_map *umap)
1932 return cond_un_op(umap, &domain_factor_domain_entry);
1935 /* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
1937 static int domain_factor_range_entry(void **entry, void *user)
1939 isl_map *map = *entry;
1940 isl_union_map **res = user;
1942 if (!isl_map_domain_is_wrapping(map))
1943 return 0;
1945 *res = isl_union_map_add_map(*res,
1946 isl_map_domain_factor_range(isl_map_copy(map)));
1948 return *res ? 0 : -1;
1951 /* For each map in "umap" of the form [A -> B] -> C,
1952 * construct the map B -> C and collect the results.
1954 __isl_give isl_union_map *isl_union_map_domain_factor_range(
1955 __isl_take isl_union_map *umap)
1957 return cond_un_op(umap, &domain_factor_range_entry);
1960 /* If "map" is of the form A -> [B -> C], then add A -> C to "res".
1962 static int range_factor_range_entry(void **entry, void *user)
1964 isl_map *map = *entry;
1965 isl_union_map **res = user;
1967 if (!isl_map_range_is_wrapping(map))
1968 return 0;
1970 *res = isl_union_map_add_map(*res,
1971 isl_map_range_factor_range(isl_map_copy(map)));
1973 return *res ? 0 : -1;
1976 /* For each map in "umap" of the form A -> [B -> C],
1977 * construct the map A -> C and collect the results.
1979 __isl_give isl_union_map *isl_union_map_range_factor_range(
1980 __isl_take isl_union_map *umap)
1982 return cond_un_op(umap, &range_factor_range_entry);
1985 static int unwrap_entry(void **entry, void *user)
1987 isl_set *set = *entry;
1988 isl_union_set **res = user;
1990 if (!isl_set_is_wrapping(set))
1991 return 0;
1993 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1995 return 0;
1998 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2000 return cond_un_op(uset, &unwrap_entry);
2003 static int wrap_entry(void **entry, void *user)
2005 isl_map *map = *entry;
2006 isl_union_set **res = user;
2008 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2010 return 0;
2013 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2015 return cond_un_op(umap, &wrap_entry);
2018 struct isl_union_map_is_subset_data {
2019 isl_union_map *umap2;
2020 int is_subset;
2023 static int is_subset_entry(void **entry, void *user)
2025 struct isl_union_map_is_subset_data *data = user;
2026 uint32_t hash;
2027 struct isl_hash_table_entry *entry2;
2028 isl_map *map = *entry;
2030 hash = isl_space_get_hash(map->dim);
2031 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2032 hash, &has_dim, map->dim, 0);
2033 if (!entry2) {
2034 int empty = isl_map_is_empty(map);
2035 if (empty < 0)
2036 return -1;
2037 if (empty)
2038 return 0;
2039 data->is_subset = 0;
2040 return -1;
2043 data->is_subset = isl_map_is_subset(map, entry2->data);
2044 if (data->is_subset < 0 || !data->is_subset)
2045 return -1;
2047 return 0;
2050 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2051 __isl_keep isl_union_map *umap2)
2053 struct isl_union_map_is_subset_data data = { NULL, 1 };
2055 umap1 = isl_union_map_copy(umap1);
2056 umap2 = isl_union_map_copy(umap2);
2057 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2058 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2060 if (!umap1 || !umap2)
2061 goto error;
2063 data.umap2 = umap2;
2064 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2065 &is_subset_entry, &data) < 0 &&
2066 data.is_subset)
2067 goto error;
2069 isl_union_map_free(umap1);
2070 isl_union_map_free(umap2);
2072 return data.is_subset;
2073 error:
2074 isl_union_map_free(umap1);
2075 isl_union_map_free(umap2);
2076 return -1;
2079 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2080 __isl_keep isl_union_set *uset2)
2082 return isl_union_map_is_subset(uset1, uset2);
2085 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2086 __isl_keep isl_union_map *umap2)
2088 int is_subset;
2090 if (!umap1 || !umap2)
2091 return -1;
2092 is_subset = isl_union_map_is_subset(umap1, umap2);
2093 if (is_subset != 1)
2094 return is_subset;
2095 is_subset = isl_union_map_is_subset(umap2, umap1);
2096 return is_subset;
2099 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2100 __isl_keep isl_union_set *uset2)
2102 return isl_union_map_is_equal(uset1, uset2);
2105 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2106 __isl_keep isl_union_map *umap2)
2108 int is_subset;
2110 if (!umap1 || !umap2)
2111 return -1;
2112 is_subset = isl_union_map_is_subset(umap1, umap2);
2113 if (is_subset != 1)
2114 return is_subset;
2115 is_subset = isl_union_map_is_subset(umap2, umap1);
2116 if (is_subset == -1)
2117 return is_subset;
2118 return !is_subset;
2121 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2122 __isl_keep isl_union_set *uset2)
2124 return isl_union_map_is_strict_subset(uset1, uset2);
2127 static int sample_entry(void **entry, void *user)
2129 isl_basic_map **sample = (isl_basic_map **)user;
2130 isl_map *map = *entry;
2132 *sample = isl_map_sample(isl_map_copy(map));
2133 if (!*sample)
2134 return -1;
2135 if (!isl_basic_map_plain_is_empty(*sample))
2136 return -1;
2137 return 0;
2140 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2142 isl_basic_map *sample = NULL;
2144 if (!umap)
2145 return NULL;
2147 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2148 &sample_entry, &sample) < 0 &&
2149 !sample)
2150 goto error;
2152 if (!sample)
2153 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2155 isl_union_map_free(umap);
2157 return sample;
2158 error:
2159 isl_union_map_free(umap);
2160 return NULL;
2163 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2165 return (isl_basic_set *)isl_union_map_sample(uset);
2168 struct isl_forall_data {
2169 int res;
2170 int (*fn)(__isl_keep isl_map *map);
2173 static int forall_entry(void **entry, void *user)
2175 struct isl_forall_data *data = user;
2176 isl_map *map = *entry;
2178 data->res = data->fn(map);
2179 if (data->res < 0)
2180 return -1;
2182 if (!data->res)
2183 return -1;
2185 return 0;
2188 static int union_map_forall(__isl_keep isl_union_map *umap,
2189 int (*fn)(__isl_keep isl_map *map))
2191 struct isl_forall_data data = { 1, fn };
2193 if (!umap)
2194 return -1;
2196 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2197 &forall_entry, &data) < 0 && data.res)
2198 return -1;
2200 return data.res;
2203 struct isl_forall_user_data {
2204 int res;
2205 int (*fn)(__isl_keep isl_map *map, void *user);
2206 void *user;
2209 static int forall_user_entry(void **entry, void *user)
2211 struct isl_forall_user_data *data = user;
2212 isl_map *map = *entry;
2214 data->res = data->fn(map, data->user);
2215 if (data->res < 0)
2216 return -1;
2218 if (!data->res)
2219 return -1;
2221 return 0;
2224 /* Check if fn(map, user) returns true for all maps "map" in umap.
2226 static int union_map_forall_user(__isl_keep isl_union_map *umap,
2227 int (*fn)(__isl_keep isl_map *map, void *user), void *user)
2229 struct isl_forall_user_data data = { 1, fn, user };
2231 if (!umap)
2232 return -1;
2234 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2235 &forall_user_entry, &data) < 0 && data.res)
2236 return -1;
2238 return data.res;
2241 int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2243 return union_map_forall(umap, &isl_map_is_empty);
2246 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2248 return isl_union_map_is_empty(uset);
2251 static int is_subset_of_identity(__isl_keep isl_map *map)
2253 int is_subset;
2254 isl_space *dim;
2255 isl_map *id;
2257 if (!map)
2258 return -1;
2260 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2261 map->dim, isl_dim_out))
2262 return 0;
2264 dim = isl_map_get_space(map);
2265 id = isl_map_identity(dim);
2267 is_subset = isl_map_is_subset(map, id);
2269 isl_map_free(id);
2271 return is_subset;
2274 /* Given an isl_union_map that consists of a single map, check
2275 * if it is single-valued.
2277 static int single_map_is_single_valued(__isl_keep isl_union_map *umap)
2279 isl_map *map;
2280 int sv;
2282 umap = isl_union_map_copy(umap);
2283 map = isl_map_from_union_map(umap);
2284 sv = isl_map_is_single_valued(map);
2285 isl_map_free(map);
2287 return sv;
2290 /* Internal data structure for single_valued_on_domain.
2292 * "umap" is the union map to be tested.
2293 * "sv" is set to 1 as long as "umap" may still be single-valued.
2295 struct isl_union_map_is_sv_data {
2296 isl_union_map *umap;
2297 int sv;
2300 /* Check if the data->umap is single-valued on "set".
2302 * If data->umap consists of a single map on "set", then test it
2303 * as an isl_map.
2305 * Otherwise, compute
2307 * M \circ M^-1
2309 * check if the result is a subset of the identity mapping and
2310 * store the result in data->sv.
2312 * Terminate as soon as data->umap has been determined not to
2313 * be single-valued.
2315 static int single_valued_on_domain(__isl_take isl_set *set, void *user)
2317 struct isl_union_map_is_sv_data *data = user;
2318 isl_union_map *umap, *test;
2320 umap = isl_union_map_copy(data->umap);
2321 umap = isl_union_map_intersect_domain(umap,
2322 isl_union_set_from_set(set));
2324 if (isl_union_map_n_map(umap) == 1) {
2325 data->sv = single_map_is_single_valued(umap);
2326 isl_union_map_free(umap);
2327 } else {
2328 test = isl_union_map_reverse(isl_union_map_copy(umap));
2329 test = isl_union_map_apply_range(test, umap);
2331 data->sv = union_map_forall(test, &is_subset_of_identity);
2333 isl_union_map_free(test);
2336 if (data->sv < 0 || !data->sv)
2337 return -1;
2338 return 0;
2341 /* Check if the given map is single-valued.
2343 * If the union map consists of a single map, then test it as an isl_map.
2344 * Otherwise, check if the union map is single-valued on each of its
2345 * domain spaces.
2347 int isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2349 isl_union_map *universe;
2350 isl_union_set *domain;
2351 struct isl_union_map_is_sv_data data;
2353 if (isl_union_map_n_map(umap) == 1)
2354 return single_map_is_single_valued(umap);
2356 universe = isl_union_map_universe(isl_union_map_copy(umap));
2357 domain = isl_union_map_domain(universe);
2359 data.sv = 1;
2360 data.umap = umap;
2361 if (isl_union_set_foreach_set(domain,
2362 &single_valued_on_domain, &data) < 0 && data.sv)
2363 data.sv = -1;
2364 isl_union_set_free(domain);
2366 return data.sv;
2369 int isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2371 int in;
2373 umap = isl_union_map_copy(umap);
2374 umap = isl_union_map_reverse(umap);
2375 in = isl_union_map_is_single_valued(umap);
2376 isl_union_map_free(umap);
2378 return in;
2381 /* Represents a map that has a fixed value (v) for one of its
2382 * range dimensions.
2383 * The map in this structure is not reference counted, so it
2384 * is only valid while the isl_union_map from which it was
2385 * obtained is still alive.
2387 struct isl_fixed_map {
2388 isl_int v;
2389 isl_map *map;
2392 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2393 int n)
2395 int i;
2396 struct isl_fixed_map *v;
2398 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2399 if (!v)
2400 return NULL;
2401 for (i = 0; i < n; ++i)
2402 isl_int_init(v[i].v);
2403 return v;
2406 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2408 int i;
2410 if (!v)
2411 return;
2412 for (i = 0; i < n; ++i)
2413 isl_int_clear(v[i].v);
2414 free(v);
2417 /* Compare the "v" field of two isl_fixed_map structs.
2419 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2421 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2422 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2424 return isl_int_cmp(e1->v, e2->v);
2427 /* Internal data structure used while checking whether all maps
2428 * in a union_map have a fixed value for a given output dimension.
2429 * v is the list of maps, with the fixed value for the dimension
2430 * n is the number of maps considered so far
2431 * pos is the output dimension under investigation
2433 struct isl_fixed_dim_data {
2434 struct isl_fixed_map *v;
2435 int n;
2436 int pos;
2439 static int fixed_at_pos(__isl_keep isl_map *map, void *user)
2441 struct isl_fixed_dim_data *data = user;
2443 data->v[data->n].map = map;
2444 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2445 &data->v[data->n++].v);
2448 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2449 int first, int n_range);
2451 /* Given a list of the maps, with their fixed values at output dimension "pos",
2452 * check whether the ranges of the maps form an obvious partition.
2454 * We first sort the maps according to their fixed values.
2455 * If all maps have a different value, then we know the ranges form
2456 * a partition.
2457 * Otherwise, we collect the maps with the same fixed value and
2458 * check whether each such collection is obviously injective
2459 * based on later dimensions.
2461 static int separates(struct isl_fixed_map *v, int n,
2462 __isl_take isl_space *dim, int pos, int n_range)
2464 int i;
2466 if (!v)
2467 goto error;
2469 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2471 for (i = 0; i + 1 < n; ++i) {
2472 int j, k;
2473 isl_union_map *part;
2474 int injective;
2476 for (j = i + 1; j < n; ++j)
2477 if (isl_int_ne(v[i].v, v[j].v))
2478 break;
2480 if (j == i + 1)
2481 continue;
2483 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2484 for (k = i; k < j; ++k)
2485 part = isl_union_map_add_map(part,
2486 isl_map_copy(v[k].map));
2488 injective = plain_injective_on_range(part, pos + 1, n_range);
2489 if (injective < 0)
2490 goto error;
2491 if (!injective)
2492 break;
2494 i = j - 1;
2497 isl_space_free(dim);
2498 free_isl_fixed_map_array(v, n);
2499 return i + 1 >= n;
2500 error:
2501 isl_space_free(dim);
2502 free_isl_fixed_map_array(v, n);
2503 return -1;
2506 /* Check whether the maps in umap have obviously distinct ranges.
2507 * In particular, check for an output dimension in the range
2508 * [first,n_range) for which all maps have a fixed value
2509 * and then check if these values, possibly along with fixed values
2510 * at later dimensions, entail distinct ranges.
2512 static int plain_injective_on_range(__isl_take isl_union_map *umap,
2513 int first, int n_range)
2515 isl_ctx *ctx;
2516 int n;
2517 struct isl_fixed_dim_data data = { NULL };
2519 ctx = isl_union_map_get_ctx(umap);
2521 n = isl_union_map_n_map(umap);
2522 if (!umap)
2523 goto error;
2525 if (n <= 1) {
2526 isl_union_map_free(umap);
2527 return 1;
2530 if (first >= n_range) {
2531 isl_union_map_free(umap);
2532 return 0;
2535 data.v = alloc_isl_fixed_map_array(ctx, n);
2536 if (!data.v)
2537 goto error;
2539 for (data.pos = first; data.pos < n_range; ++data.pos) {
2540 int fixed;
2541 int injective;
2542 isl_space *dim;
2544 data.n = 0;
2545 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2546 if (fixed < 0)
2547 goto error;
2548 if (!fixed)
2549 continue;
2550 dim = isl_union_map_get_space(umap);
2551 injective = separates(data.v, n, dim, data.pos, n_range);
2552 isl_union_map_free(umap);
2553 return injective;
2556 free_isl_fixed_map_array(data.v, n);
2557 isl_union_map_free(umap);
2559 return 0;
2560 error:
2561 free_isl_fixed_map_array(data.v, n);
2562 isl_union_map_free(umap);
2563 return -1;
2566 /* Check whether the maps in umap that map to subsets of "ran"
2567 * have obviously distinct ranges.
2569 static int plain_injective_on_range_wrap(__isl_keep isl_set *ran, void *user)
2571 isl_union_map *umap = user;
2573 umap = isl_union_map_copy(umap);
2574 umap = isl_union_map_intersect_range(umap,
2575 isl_union_set_from_set(isl_set_copy(ran)));
2576 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2579 /* Check if the given union_map is obviously injective.
2581 * In particular, we first check if all individual maps are obviously
2582 * injective and then check if all the ranges of these maps are
2583 * obviously disjoint.
2585 int isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2587 int in;
2588 isl_union_map *univ;
2589 isl_union_set *ran;
2591 in = union_map_forall(umap, &isl_map_plain_is_injective);
2592 if (in < 0)
2593 return -1;
2594 if (!in)
2595 return 0;
2597 univ = isl_union_map_universe(isl_union_map_copy(umap));
2598 ran = isl_union_map_range(univ);
2600 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2602 isl_union_set_free(ran);
2604 return in;
2607 int isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2609 int sv;
2611 sv = isl_union_map_is_single_valued(umap);
2612 if (sv < 0 || !sv)
2613 return sv;
2615 return isl_union_map_is_injective(umap);
2618 static int zip_entry(void **entry, void *user)
2620 isl_map *map = *entry;
2621 isl_union_map **res = user;
2623 if (!isl_map_can_zip(map))
2624 return 0;
2626 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
2628 return 0;
2631 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2633 return cond_un_op(umap, &zip_entry);
2636 static int uncurry_entry(void **entry, void *user)
2638 isl_map *map = *entry;
2639 isl_union_map **res = user;
2641 if (!isl_map_can_uncurry(map))
2642 return 0;
2644 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
2646 return 0;
2649 /* Given a union map, take the maps of the form A -> (B -> C) and
2650 * return the union of the corresponding maps (A -> B) -> C.
2652 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2654 return cond_un_op(umap, &uncurry_entry);
2657 static int curry_entry(void **entry, void *user)
2659 isl_map *map = *entry;
2660 isl_union_map **res = user;
2662 if (!isl_map_can_curry(map))
2663 return 0;
2665 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
2667 return 0;
2670 /* Given a union map, take the maps of the form (A -> B) -> C and
2671 * return the union of the corresponding maps A -> (B -> C).
2673 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2675 return cond_un_op(umap, &curry_entry);
2678 static int lift_entry(void **entry, void *user)
2680 isl_set *set = *entry;
2681 isl_union_set **res = user;
2683 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
2685 return 0;
2688 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2690 return cond_un_op(uset, &lift_entry);
2693 static int coefficients_entry(void **entry, void *user)
2695 isl_set *set = *entry;
2696 isl_union_set **res = user;
2698 set = isl_set_copy(set);
2699 set = isl_set_from_basic_set(isl_set_coefficients(set));
2700 *res = isl_union_set_add_set(*res, set);
2702 return 0;
2705 __isl_give isl_union_set *isl_union_set_coefficients(
2706 __isl_take isl_union_set *uset)
2708 isl_ctx *ctx;
2709 isl_space *dim;
2710 isl_union_set *res;
2712 if (!uset)
2713 return NULL;
2715 ctx = isl_union_set_get_ctx(uset);
2716 dim = isl_space_set_alloc(ctx, 0, 0);
2717 res = isl_union_map_alloc(dim, uset->table.n);
2718 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2719 &coefficients_entry, &res) < 0)
2720 goto error;
2722 isl_union_set_free(uset);
2723 return res;
2724 error:
2725 isl_union_set_free(uset);
2726 isl_union_set_free(res);
2727 return NULL;
2730 static int solutions_entry(void **entry, void *user)
2732 isl_set *set = *entry;
2733 isl_union_set **res = user;
2735 set = isl_set_copy(set);
2736 set = isl_set_from_basic_set(isl_set_solutions(set));
2737 if (!*res)
2738 *res = isl_union_set_from_set(set);
2739 else
2740 *res = isl_union_set_add_set(*res, set);
2742 if (!*res)
2743 return -1;
2745 return 0;
2748 __isl_give isl_union_set *isl_union_set_solutions(
2749 __isl_take isl_union_set *uset)
2751 isl_union_set *res = NULL;
2753 if (!uset)
2754 return NULL;
2756 if (uset->table.n == 0) {
2757 res = isl_union_set_empty(isl_union_set_get_space(uset));
2758 isl_union_set_free(uset);
2759 return res;
2762 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2763 &solutions_entry, &res) < 0)
2764 goto error;
2766 isl_union_set_free(uset);
2767 return res;
2768 error:
2769 isl_union_set_free(uset);
2770 isl_union_set_free(res);
2771 return NULL;
2774 /* Is the domain space of "map" equal to "space"?
2776 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2778 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2779 space, isl_dim_out);
2782 /* Is the range space of "map" equal to "space"?
2784 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2786 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
2787 space, isl_dim_out);
2790 /* Is the set space of "map" equal to "space"?
2792 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
2794 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
2795 space, isl_dim_out);
2798 /* Internal data structure for preimage_pw_multi_aff.
2800 * "pma" is the function under which the preimage should be taken.
2801 * "space" is the space of "pma".
2802 * "res" collects the results.
2803 * "fn" computes the preimage for a given map.
2804 * "match" returns true if "fn" can be called.
2806 struct isl_union_map_preimage_data {
2807 isl_space *space;
2808 isl_pw_multi_aff *pma;
2809 isl_union_map *res;
2810 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2811 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2812 __isl_take isl_pw_multi_aff *pma);
2815 /* Call data->fn to compute the preimage of the domain or range of *entry
2816 * under the function represented by data->pma, provided the domain/range
2817 * space of *entry matches the target space of data->pma
2818 * (as given by data->match), and add the result to data->res.
2820 static int preimage_entry(void **entry, void *user)
2822 int m;
2823 isl_map *map = *entry;
2824 struct isl_union_map_preimage_data *data = user;
2825 int empty;
2827 m = data->match(map, data->space);
2828 if (m < 0)
2829 return -1;
2830 if (!m)
2831 return 0;
2833 map = isl_map_copy(map);
2834 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
2836 empty = isl_map_is_empty(map);
2837 if (empty < 0 || empty) {
2838 isl_map_free(map);
2839 return empty < 0 ? -1 : 0;
2842 data->res = isl_union_map_add_map(data->res, map);
2844 return 0;
2847 /* Compute the preimage of the domain or range of "umap" under the function
2848 * represented by "pma".
2849 * In other words, plug in "pma" in the domain or range of "umap".
2850 * The function "fn" performs the actual preimage computation on a map,
2851 * while "match" determines to which maps the function should be applied.
2853 static __isl_give isl_union_map *preimage_pw_multi_aff(
2854 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
2855 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
2856 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2857 __isl_take isl_pw_multi_aff *pma))
2859 isl_ctx *ctx;
2860 isl_space *space;
2861 struct isl_union_map_preimage_data data;
2863 umap = isl_union_map_align_params(umap,
2864 isl_pw_multi_aff_get_space(pma));
2865 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
2867 if (!umap || !pma)
2868 goto error;
2870 ctx = isl_union_map_get_ctx(umap);
2871 space = isl_union_map_get_space(umap);
2872 data.space = isl_pw_multi_aff_get_space(pma);
2873 data.pma = pma;
2874 data.res = isl_union_map_alloc(space, umap->table.n);
2875 data.match = match;
2876 data.fn = fn;
2877 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
2878 &data) < 0)
2879 data.res = isl_union_map_free(data.res);
2881 isl_space_free(data.space);
2882 isl_union_map_free(umap);
2883 isl_pw_multi_aff_free(pma);
2884 return data.res;
2885 error:
2886 isl_union_map_free(umap);
2887 isl_pw_multi_aff_free(pma);
2888 return NULL;
2891 /* Compute the preimage of the domain of "umap" under the function
2892 * represented by "pma".
2893 * In other words, plug in "pma" in the domain of "umap".
2894 * The result contains maps that live in the same spaces as the maps of "umap"
2895 * with domain space equal to the target space of "pma",
2896 * except that the domain has been replaced by the domain space of "pma".
2898 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
2899 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
2901 return preimage_pw_multi_aff(umap, pma, &domain_match,
2902 &isl_map_preimage_domain_pw_multi_aff);
2905 /* Compute the preimage of the range of "umap" under the function
2906 * represented by "pma".
2907 * In other words, plug in "pma" in the range of "umap".
2908 * The result contains maps that live in the same spaces as the maps of "umap"
2909 * with range space equal to the target space of "pma",
2910 * except that the range has been replaced by the domain space of "pma".
2912 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
2913 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
2915 return preimage_pw_multi_aff(umap, pma, &range_match,
2916 &isl_map_preimage_range_pw_multi_aff);
2919 /* Compute the preimage of "uset" under the function represented by "pma".
2920 * In other words, plug in "pma" in "uset".
2921 * The result contains sets that live in the same spaces as the sets of "uset"
2922 * with space equal to the target space of "pma",
2923 * except that the space has been replaced by the domain space of "pma".
2925 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
2926 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
2928 return preimage_pw_multi_aff(uset, pma, &set_match,
2929 &isl_set_preimage_pw_multi_aff);
2932 /* Compute the preimage of the domain of "umap" under the function
2933 * represented by "ma".
2934 * In other words, plug in "ma" in the domain of "umap".
2935 * The result contains maps that live in the same spaces as the maps of "umap"
2936 * with domain space equal to the target space of "ma",
2937 * except that the domain has been replaced by the domain space of "ma".
2939 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
2940 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
2942 return isl_union_map_preimage_domain_pw_multi_aff(umap,
2943 isl_pw_multi_aff_from_multi_aff(ma));
2946 /* Compute the preimage of the range of "umap" under the function
2947 * represented by "ma".
2948 * In other words, plug in "ma" in the range of "umap".
2949 * The result contains maps that live in the same spaces as the maps of "umap"
2950 * with range space equal to the target space of "ma",
2951 * except that the range has been replaced by the domain space of "ma".
2953 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
2954 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
2956 return isl_union_map_preimage_range_pw_multi_aff(umap,
2957 isl_pw_multi_aff_from_multi_aff(ma));
2960 /* Compute the preimage of "uset" under the function represented by "ma".
2961 * In other words, plug in "ma" in "uset".
2962 * The result contains sets that live in the same spaces as the sets of "uset"
2963 * with space equal to the target space of "ma",
2964 * except that the space has been replaced by the domain space of "ma".
2966 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
2967 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
2969 return isl_union_set_preimage_pw_multi_aff(uset,
2970 isl_pw_multi_aff_from_multi_aff(ma));
2973 /* Internal data structure for preimage_multi_pw_aff.
2975 * "mpa" is the function under which the preimage should be taken.
2976 * "space" is the space of "mpa".
2977 * "res" collects the results.
2978 * "fn" computes the preimage for a given map.
2979 * "match" returns true if "fn" can be called.
2981 struct isl_union_map_preimage_mpa_data {
2982 isl_space *space;
2983 isl_multi_pw_aff *mpa;
2984 isl_union_map *res;
2985 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
2986 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
2987 __isl_take isl_multi_pw_aff *mpa);
2990 /* Call data->fn to compute the preimage of the domain or range of *entry
2991 * under the function represented by data->mpa, provided the domain/range
2992 * space of *entry matches the target space of data->mpa
2993 * (as given by data->match), and add the result to data->res.
2995 static int preimage_mpa_entry(void **entry, void *user)
2997 int m;
2998 isl_map *map = *entry;
2999 struct isl_union_map_preimage_mpa_data *data = user;
3000 int empty;
3002 m = data->match(map, data->space);
3003 if (m < 0)
3004 return -1;
3005 if (!m)
3006 return 0;
3008 map = isl_map_copy(map);
3009 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3011 empty = isl_map_is_empty(map);
3012 if (empty < 0 || empty) {
3013 isl_map_free(map);
3014 return empty < 0 ? -1 : 0;
3017 data->res = isl_union_map_add_map(data->res, map);
3019 return 0;
3022 /* Compute the preimage of the domain or range of "umap" under the function
3023 * represented by "mpa".
3024 * In other words, plug in "mpa" in the domain or range of "umap".
3025 * The function "fn" performs the actual preimage computation on a map,
3026 * while "match" determines to which maps the function should be applied.
3028 static __isl_give isl_union_map *preimage_multi_pw_aff(
3029 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3030 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3031 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3032 __isl_take isl_multi_pw_aff *mpa))
3034 isl_ctx *ctx;
3035 isl_space *space;
3036 struct isl_union_map_preimage_mpa_data data;
3038 umap = isl_union_map_align_params(umap,
3039 isl_multi_pw_aff_get_space(mpa));
3040 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3042 if (!umap || !mpa)
3043 goto error;
3045 ctx = isl_union_map_get_ctx(umap);
3046 space = isl_union_map_get_space(umap);
3047 data.space = isl_multi_pw_aff_get_space(mpa);
3048 data.mpa = mpa;
3049 data.res = isl_union_map_alloc(space, umap->table.n);
3050 data.match = match;
3051 data.fn = fn;
3052 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3053 &data) < 0)
3054 data.res = isl_union_map_free(data.res);
3056 isl_space_free(data.space);
3057 isl_union_map_free(umap);
3058 isl_multi_pw_aff_free(mpa);
3059 return data.res;
3060 error:
3061 isl_union_map_free(umap);
3062 isl_multi_pw_aff_free(mpa);
3063 return NULL;
3066 /* Compute the preimage of the domain of "umap" under the function
3067 * represented by "mpa".
3068 * In other words, plug in "mpa" in the domain of "umap".
3069 * The result contains maps that live in the same spaces as the maps of "umap"
3070 * with domain space equal to the target space of "mpa",
3071 * except that the domain has been replaced by the domain space of "mpa".
3073 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3074 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3076 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3077 &isl_map_preimage_domain_multi_pw_aff);
3080 /* Internal data structure for preimage_upma.
3082 * "umap" is the map of which the preimage should be computed.
3083 * "res" collects the results.
3084 * "fn" computes the preimage for a given piecewise multi-affine function.
3086 struct isl_union_map_preimage_upma_data {
3087 isl_union_map *umap;
3088 isl_union_map *res;
3089 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3090 __isl_take isl_pw_multi_aff *pma);
3093 /* Call data->fn to compute the preimage of the domain or range of data->umap
3094 * under the function represented by pma and add the result to data->res.
3096 static int preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3098 struct isl_union_map_preimage_upma_data *data = user;
3099 isl_union_map *umap;
3101 umap = isl_union_map_copy(data->umap);
3102 umap = data->fn(umap, pma);
3103 data->res = isl_union_map_union(data->res, umap);
3105 return data->res ? 0 : -1;
3108 /* Compute the preimage of the domain or range of "umap" under the function
3109 * represented by "upma".
3110 * In other words, plug in "upma" in the domain or range of "umap".
3111 * The function "fn" performs the actual preimage computation
3112 * on a piecewise multi-affine function.
3114 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3115 __isl_take isl_union_map *umap,
3116 __isl_take isl_union_pw_multi_aff *upma,
3117 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3118 __isl_take isl_pw_multi_aff *pma))
3120 struct isl_union_map_preimage_upma_data data;
3122 data.umap = umap;
3123 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3124 data.fn = fn;
3125 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3126 &preimage_upma, &data) < 0)
3127 data.res = isl_union_map_free(data.res);
3129 isl_union_map_free(umap);
3130 isl_union_pw_multi_aff_free(upma);
3132 return data.res;
3135 /* Compute the preimage of the domain of "umap" under the function
3136 * represented by "upma".
3137 * In other words, plug in "upma" in the domain of "umap".
3138 * The result contains maps that live in the same spaces as the maps of "umap"
3139 * with domain space equal to one of the target spaces of "upma",
3140 * except that the domain has been replaced by one of the the domain spaces that
3141 * corresponds to that target space of "upma".
3143 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3144 __isl_take isl_union_map *umap,
3145 __isl_take isl_union_pw_multi_aff *upma)
3147 return preimage_union_pw_multi_aff(umap, upma,
3148 &isl_union_map_preimage_domain_pw_multi_aff);
3151 /* Compute the preimage of the range of "umap" under the function
3152 * represented by "upma".
3153 * In other words, plug in "upma" in the range of "umap".
3154 * The result contains maps that live in the same spaces as the maps of "umap"
3155 * with range space equal to one of the target spaces of "upma",
3156 * except that the range has been replaced by one of the the domain spaces that
3157 * corresponds to that target space of "upma".
3159 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3160 __isl_take isl_union_map *umap,
3161 __isl_take isl_union_pw_multi_aff *upma)
3163 return preimage_union_pw_multi_aff(umap, upma,
3164 &isl_union_map_preimage_range_pw_multi_aff);
3167 /* Compute the preimage of "uset" under the function represented by "upma".
3168 * In other words, plug in "upma" in the range of "uset".
3169 * The result contains sets that live in the same spaces as the sets of "uset"
3170 * with space equal to one of the target spaces of "upma",
3171 * except that the space has been replaced by one of the the domain spaces that
3172 * corresponds to that target space of "upma".
3174 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3175 __isl_take isl_union_set *uset,
3176 __isl_take isl_union_pw_multi_aff *upma)
3178 return preimage_union_pw_multi_aff(uset, upma,
3179 &isl_union_set_preimage_pw_multi_aff);
3182 /* Reset the user pointer on all identifiers of parameters and tuples
3183 * of the space of *entry.
3185 static int reset_user(void **entry, void *user)
3187 isl_map **map = (isl_map **)entry;
3189 *map = isl_map_reset_user(*map);
3191 return *map ? 0 : -1;
3194 /* Reset the user pointer on all identifiers of parameters and tuples
3195 * of the spaces of "umap".
3197 __isl_give isl_union_map *isl_union_map_reset_user(
3198 __isl_take isl_union_map *umap)
3200 umap = isl_union_map_cow(umap);
3201 if (!umap)
3202 return NULL;
3203 umap->dim = isl_space_reset_user(umap->dim);
3204 if (!umap->dim)
3205 return isl_union_map_free(umap);
3206 umap = un_op(umap, &reset_user);
3208 return umap;
3211 /* Reset the user pointer on all identifiers of parameters and tuples
3212 * of the spaces of "uset".
3214 __isl_give isl_union_set *isl_union_set_reset_user(
3215 __isl_take isl_union_set *uset)
3217 return isl_union_map_reset_user(uset);
3220 /* Internal data structure for isl_union_map_project_out.
3221 * "type", "first" and "n" are the arguments for the isl_map_project_out
3222 * call.
3223 * "res" collects the results.
3225 struct isl_union_map_project_out_data {
3226 enum isl_dim_type type;
3227 unsigned first;
3228 unsigned n;
3230 isl_union_map *res;
3233 /* Turn the data->n dimensions of type data->type, starting at data->first
3234 * into existentially quantified variables and add the result to data->res.
3236 static int project_out(__isl_take isl_map *map, void *user)
3238 struct isl_union_map_project_out_data *data = user;
3240 map = isl_map_project_out(map, data->type, data->first, data->n);
3241 data->res = isl_union_map_add_map(data->res, map);
3243 return 0;
3246 /* Turn the "n" dimensions of type "type", starting at "first"
3247 * into existentially quantified variables.
3248 * Since the space of an isl_union_map only contains parameters,
3249 * type is required to be equal to isl_dim_param.
3251 __isl_give isl_union_map *isl_union_map_project_out(
3252 __isl_take isl_union_map *umap,
3253 enum isl_dim_type type, unsigned first, unsigned n)
3255 isl_space *space;
3256 struct isl_union_map_project_out_data data = { type, first, n };
3258 if (!umap)
3259 return NULL;
3261 if (type != isl_dim_param)
3262 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3263 "can only project out parameters",
3264 return isl_union_map_free(umap));
3266 space = isl_union_map_get_space(umap);
3267 space = isl_space_drop_dims(space, type, first, n);
3268 data.res = isl_union_map_empty(space);
3269 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3270 data.res = isl_union_map_free(data.res);
3272 isl_union_map_free(umap);
3274 return data.res;
3277 /* Internal data structure for isl_union_map_involves_dims.
3278 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3280 struct isl_union_map_involves_dims_data {
3281 unsigned first;
3282 unsigned n;
3285 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3287 static int map_excludes(__isl_keep isl_map *map, void *user)
3289 struct isl_union_map_involves_dims_data *data = user;
3290 int involves;
3292 involves = isl_map_involves_dims(map,
3293 isl_dim_param, data->first, data->n);
3294 if (involves < 0)
3295 return -1;
3296 return !involves;
3299 /* Does "umap" involve any of the n parameters starting at first?
3300 * "type" is required to be set to isl_dim_param.
3302 * "umap" involves any of those parameters if any of its maps
3303 * involve the parameters. In other words, "umap" does not
3304 * involve any of the parameters if all its maps to not
3305 * involve the parameters.
3307 int isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3308 enum isl_dim_type type, unsigned first, unsigned n)
3310 struct isl_union_map_involves_dims_data data = { first, n };
3311 int excludes;
3313 if (type != isl_dim_param)
3314 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3315 "can only reference parameters", return 0);
3317 excludes = union_map_forall_user(umap, &map_excludes, &data);
3319 if (excludes < 0)
3320 return -1;
3322 return !excludes;