isl_union_map.c: cond_un_op: unify callbacks
[isl.git] / isl_union_map.c
blob835db2de93304f21dcea5ae360e50357c73b76d7
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #define ISL_DIM_H
17 #include <isl_map_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl/ctx.h>
20 #include <isl/hash.h>
21 #include <isl/aff.h>
22 #include <isl/map.h>
23 #include <isl/set.h>
24 #include <isl_space_private.h>
25 #include <isl/union_set.h>
26 #include <isl/deprecated/union_map_int.h>
28 #include <bset_from_bmap.c>
29 #include <set_to_map.c>
30 #include <set_from_map.c>
32 /* Return the number of parameters of "umap", where "type"
33 * is required to be set to isl_dim_param.
35 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
36 enum isl_dim_type type)
38 if (!umap)
39 return 0;
41 if (type != isl_dim_param)
42 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
43 "can only reference parameters", return 0);
45 return isl_space_dim(umap->dim, type);
48 /* Return the number of parameters of "uset", where "type"
49 * is required to be set to isl_dim_param.
51 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
52 enum isl_dim_type type)
54 return isl_union_map_dim(uset, type);
57 /* Return the id of the specified dimension.
59 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
60 enum isl_dim_type type, unsigned pos)
62 if (!umap)
63 return NULL;
65 if (type != isl_dim_param)
66 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
67 "can only reference parameters", return NULL);
69 return isl_space_get_dim_id(umap->dim, type, pos);
72 /* Is this union set a parameter domain?
74 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
76 isl_set *set;
77 isl_bool params;
79 if (!uset)
80 return isl_bool_error;
81 if (uset->table.n != 1)
82 return isl_bool_false;
84 set = isl_set_from_union_set(isl_union_set_copy(uset));
85 params = isl_set_is_params(set);
86 isl_set_free(set);
87 return params;
90 static __isl_give isl_union_map *isl_union_map_alloc(
91 __isl_take isl_space *space, int size)
93 isl_union_map *umap;
95 space = isl_space_params(space);
96 if (!space)
97 return NULL;
99 umap = isl_calloc_type(space->ctx, isl_union_map);
100 if (!umap) {
101 isl_space_free(space);
102 return NULL;
105 umap->ref = 1;
106 umap->dim = space;
107 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
108 return isl_union_map_free(umap);
110 return umap;
113 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
115 return isl_union_map_alloc(dim, 16);
118 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *dim)
120 return isl_union_map_empty(dim);
123 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
125 return umap ? umap->dim->ctx : NULL;
128 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
130 return uset ? uset->dim->ctx : NULL;
133 /* Return the space of "umap".
135 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
137 return umap ? umap->dim : NULL;
140 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
142 return isl_space_copy(isl_union_map_peek_space(umap));
145 /* Return the position of the parameter with the given name
146 * in "umap".
147 * Return -1 if no such dimension can be found.
149 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
150 enum isl_dim_type type, const char *name)
152 if (!umap)
153 return -1;
154 return isl_space_find_dim_by_name(umap->dim, type, name);
157 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
159 return isl_union_map_get_space(uset);
162 static isl_stat free_umap_entry(void **entry, void *user)
164 isl_map *map = *entry;
165 isl_map_free(map);
166 return isl_stat_ok;
169 static isl_stat add_map(__isl_take isl_map *map, void *user)
171 isl_union_map **umap = (isl_union_map **)user;
173 *umap = isl_union_map_add_map(*umap, map);
175 return isl_stat_ok;
178 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
180 isl_union_map *dup;
182 if (!umap)
183 return NULL;
185 dup = isl_union_map_empty(isl_space_copy(umap->dim));
186 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
187 goto error;
188 return dup;
189 error:
190 isl_union_map_free(dup);
191 return NULL;
194 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
196 if (!umap)
197 return NULL;
199 if (umap->ref == 1)
200 return umap;
201 umap->ref--;
202 return isl_union_map_dup(umap);
205 struct isl_union_align {
206 isl_reordering *exp;
207 isl_union_map *res;
210 static isl_stat align_entry(void **entry, void *user)
212 isl_map *map = *entry;
213 isl_reordering *exp;
214 struct isl_union_align *data = user;
216 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
217 isl_map_get_space(map));
219 data->res = isl_union_map_add_map(data->res,
220 isl_map_realign(isl_map_copy(map), exp));
222 return isl_stat_ok;
225 /* Align the parameters of umap along those of model.
226 * The result has the parameters of model first, in the same order
227 * as they appear in model, followed by any remaining parameters of
228 * umap that do not appear in model.
230 __isl_give isl_union_map *isl_union_map_align_params(
231 __isl_take isl_union_map *umap, __isl_take isl_space *model)
233 struct isl_union_align data = { NULL, NULL };
234 isl_bool equal_params;
236 if (!umap || !model)
237 goto error;
239 equal_params = isl_space_has_equal_params(umap->dim, model);
240 if (equal_params < 0)
241 goto error;
242 if (equal_params) {
243 isl_space_free(model);
244 return umap;
247 model = isl_space_params(model);
248 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
249 if (!data.exp)
250 goto error;
252 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
253 umap->table.n);
254 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
255 &align_entry, &data) < 0)
256 goto error;
258 isl_reordering_free(data.exp);
259 isl_union_map_free(umap);
260 isl_space_free(model);
261 return data.res;
262 error:
263 isl_reordering_free(data.exp);
264 isl_union_map_free(umap);
265 isl_union_map_free(data.res);
266 isl_space_free(model);
267 return NULL;
270 __isl_give isl_union_set *isl_union_set_align_params(
271 __isl_take isl_union_set *uset, __isl_take isl_space *model)
273 return isl_union_map_align_params(uset, model);
276 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
277 __isl_take isl_union_map *umap2)
279 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
280 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
282 umap1 = isl_union_map_cow(umap1);
284 if (!umap1 || !umap2)
285 goto error;
287 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
288 goto error;
290 isl_union_map_free(umap2);
292 return umap1;
293 error:
294 isl_union_map_free(umap1);
295 isl_union_map_free(umap2);
296 return NULL;
299 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
300 __isl_take isl_union_set *uset2)
302 return isl_union_map_union(uset1, uset2);
305 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
307 if (!umap)
308 return NULL;
310 umap->ref++;
311 return umap;
314 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
316 return isl_union_map_copy(uset);
319 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
321 if (!umap)
322 return NULL;
324 if (--umap->ref > 0)
325 return NULL;
327 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
328 &free_umap_entry, NULL);
329 isl_hash_table_clear(&umap->table);
330 isl_space_free(umap->dim);
331 free(umap);
332 return NULL;
335 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
337 return isl_union_map_free(uset);
340 /* Do "umap" and "space" have the same parameters?
342 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
343 __isl_keep isl_space *space)
345 isl_space *umap_space;
347 umap_space = isl_union_map_peek_space(umap);
348 return isl_space_has_equal_params(umap_space, space);
351 static int has_dim(const void *entry, const void *val)
353 isl_map *map = (isl_map *)entry;
354 isl_space *dim = (isl_space *)val;
356 return isl_space_is_equal(map->dim, dim);
359 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
360 __isl_take isl_map *map)
362 uint32_t hash;
363 struct isl_hash_table_entry *entry;
364 isl_bool aligned;
366 if (!map || !umap)
367 goto error;
369 if (isl_map_plain_is_empty(map)) {
370 isl_map_free(map);
371 return umap;
374 aligned = isl_map_space_has_equal_params(map, umap->dim);
375 if (aligned < 0)
376 goto error;
377 if (!aligned) {
378 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
379 map = isl_map_align_params(map, isl_union_map_get_space(umap));
382 umap = isl_union_map_cow(umap);
384 if (!map || !umap)
385 goto error;
387 hash = isl_space_get_hash(map->dim);
388 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
389 &has_dim, map->dim, 1);
390 if (!entry)
391 goto error;
393 if (!entry->data)
394 entry->data = map;
395 else {
396 entry->data = isl_map_union(entry->data, isl_map_copy(map));
397 if (!entry->data)
398 goto error;
399 isl_map_free(map);
402 return umap;
403 error:
404 isl_map_free(map);
405 isl_union_map_free(umap);
406 return NULL;
409 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
410 __isl_take isl_set *set)
412 return isl_union_map_add_map(uset, set_to_map(set));
415 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
417 isl_space *dim;
418 isl_union_map *umap;
420 if (!map)
421 return NULL;
423 dim = isl_map_get_space(map);
424 dim = isl_space_params(dim);
425 umap = isl_union_map_empty(dim);
426 umap = isl_union_map_add_map(umap, map);
428 return umap;
431 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
433 return isl_union_map_from_map(set_to_map(set));
436 __isl_give isl_union_map *isl_union_map_from_basic_map(
437 __isl_take isl_basic_map *bmap)
439 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
442 __isl_give isl_union_set *isl_union_set_from_basic_set(
443 __isl_take isl_basic_set *bset)
445 return isl_union_map_from_basic_map(bset);
448 struct isl_union_map_foreach_data
450 isl_stat (*fn)(__isl_take isl_map *map, void *user);
451 void *user;
454 static isl_stat call_on_copy(void **entry, void *user)
456 isl_map *map = *entry;
457 struct isl_union_map_foreach_data *data;
458 data = (struct isl_union_map_foreach_data *)user;
460 return data->fn(isl_map_copy(map), data->user);
463 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
465 return umap ? umap->table.n : 0;
468 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
470 return uset ? uset->table.n : 0;
473 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
474 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
476 struct isl_union_map_foreach_data data = { fn, user };
478 if (!umap)
479 return isl_stat_error;
481 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
482 &call_on_copy, &data);
485 static isl_stat copy_map(void **entry, void *user)
487 isl_map *map = *entry;
488 isl_map **map_p = user;
490 *map_p = isl_map_copy(map);
492 return isl_stat_error;
495 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
497 isl_ctx *ctx;
498 isl_map *map = NULL;
500 if (!umap)
501 return NULL;
502 ctx = isl_union_map_get_ctx(umap);
503 if (umap->table.n != 1)
504 isl_die(ctx, isl_error_invalid,
505 "union map needs to contain elements in exactly "
506 "one space", goto error);
508 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
510 isl_union_map_free(umap);
512 return map;
513 error:
514 isl_union_map_free(umap);
515 return NULL;
518 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
520 return isl_map_from_union_map(uset);
523 /* Extract the map in "umap" that lives in the given space (ignoring
524 * parameters).
526 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
527 __isl_take isl_space *space)
529 uint32_t hash;
530 struct isl_hash_table_entry *entry;
532 space = isl_space_drop_dims(space, isl_dim_param,
533 0, isl_space_dim(space, isl_dim_param));
534 space = isl_space_align_params(space, isl_union_map_get_space(umap));
535 if (!umap || !space)
536 goto error;
538 hash = isl_space_get_hash(space);
539 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
540 &has_dim, space, 0);
541 if (!entry)
542 return isl_map_empty(space);
543 isl_space_free(space);
544 return isl_map_copy(entry->data);
545 error:
546 isl_space_free(space);
547 return NULL;
550 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
551 __isl_take isl_space *dim)
553 return set_from_map(isl_union_map_extract_map(uset, dim));
556 /* Check if umap contains a map in the given space.
558 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
559 __isl_keep isl_space *space)
561 uint32_t hash;
562 struct isl_hash_table_entry *entry;
564 if (!umap || !space)
565 return isl_bool_error;
567 hash = isl_space_get_hash(space);
568 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
569 &has_dim, space, 0);
570 return !!entry;
573 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
574 __isl_keep isl_space *space)
576 return isl_union_map_contains(uset, space);
579 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
580 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
582 return isl_union_map_foreach_map(uset,
583 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
586 struct isl_union_set_foreach_point_data {
587 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
588 void *user;
591 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
593 struct isl_union_set_foreach_point_data *data = user;
594 isl_stat r;
596 r = isl_set_foreach_point(set, data->fn, data->user);
597 isl_set_free(set);
599 return r;
602 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
603 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
605 struct isl_union_set_foreach_point_data data = { fn, user };
606 return isl_union_set_foreach_set(uset, &foreach_point, &data);
609 struct isl_union_map_gen_bin_data {
610 isl_union_map *umap2;
611 isl_union_map *res;
614 static isl_stat subtract_entry(void **entry, void *user)
616 struct isl_union_map_gen_bin_data *data = user;
617 uint32_t hash;
618 struct isl_hash_table_entry *entry2;
619 isl_map *map = *entry;
621 hash = isl_space_get_hash(map->dim);
622 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
623 hash, &has_dim, map->dim, 0);
624 map = isl_map_copy(map);
625 if (entry2) {
626 int empty;
627 map = isl_map_subtract(map, isl_map_copy(entry2->data));
629 empty = isl_map_is_empty(map);
630 if (empty < 0) {
631 isl_map_free(map);
632 return isl_stat_error;
634 if (empty) {
635 isl_map_free(map);
636 return isl_stat_ok;
639 data->res = isl_union_map_add_map(data->res, map);
641 return isl_stat_ok;
644 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
645 __isl_take isl_union_map *umap2, isl_stat (*fn)(void **, void *))
647 struct isl_union_map_gen_bin_data data = { NULL, NULL };
649 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
650 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
652 if (!umap1 || !umap2)
653 goto error;
655 data.umap2 = umap2;
656 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
657 umap1->table.n);
658 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
659 fn, &data) < 0)
660 goto error;
662 isl_union_map_free(umap1);
663 isl_union_map_free(umap2);
664 return data.res;
665 error:
666 isl_union_map_free(umap1);
667 isl_union_map_free(umap2);
668 isl_union_map_free(data.res);
669 return NULL;
672 __isl_give isl_union_map *isl_union_map_subtract(
673 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
675 return gen_bin_op(umap1, umap2, &subtract_entry);
678 __isl_give isl_union_set *isl_union_set_subtract(
679 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
681 return isl_union_map_subtract(uset1, uset2);
684 struct isl_union_map_gen_bin_set_data {
685 isl_set *set;
686 isl_union_map *res;
689 static isl_stat intersect_params_entry(void **entry, void *user)
691 struct isl_union_map_gen_bin_set_data *data = user;
692 isl_map *map = *entry;
693 int empty;
695 map = isl_map_copy(map);
696 map = isl_map_intersect_params(map, isl_set_copy(data->set));
698 empty = isl_map_is_empty(map);
699 if (empty < 0) {
700 isl_map_free(map);
701 return isl_stat_error;
704 data->res = isl_union_map_add_map(data->res, map);
706 return isl_stat_ok;
709 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
710 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
712 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
714 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
715 set = isl_set_align_params(set, isl_union_map_get_space(umap));
717 if (!umap || !set)
718 goto error;
720 data.set = set;
721 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
722 umap->table.n);
723 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
724 fn, &data) < 0)
725 goto error;
727 isl_union_map_free(umap);
728 isl_set_free(set);
729 return data.res;
730 error:
731 isl_union_map_free(umap);
732 isl_set_free(set);
733 isl_union_map_free(data.res);
734 return NULL;
737 /* Intersect "umap" with the parameter domain "set".
739 * If "set" does not have any constraints, then we can return immediately.
741 __isl_give isl_union_map *isl_union_map_intersect_params(
742 __isl_take isl_union_map *umap, __isl_take isl_set *set)
744 int is_universe;
746 is_universe = isl_set_plain_is_universe(set);
747 if (is_universe < 0)
748 goto error;
749 if (is_universe) {
750 isl_set_free(set);
751 return umap;
754 return gen_bin_set_op(umap, set, &intersect_params_entry);
755 error:
756 isl_union_map_free(umap);
757 isl_set_free(set);
758 return NULL;
761 __isl_give isl_union_set *isl_union_set_intersect_params(
762 __isl_take isl_union_set *uset, __isl_take isl_set *set)
764 return isl_union_map_intersect_params(uset, set);
767 static __isl_give isl_union_map *union_map_intersect_params(
768 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
770 return isl_union_map_intersect_params(umap,
771 isl_set_from_union_set(uset));
774 static __isl_give isl_union_map *union_map_gist_params(
775 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
777 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
780 struct isl_union_map_match_bin_data {
781 isl_union_map *umap2;
782 isl_union_map *res;
783 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
786 static isl_stat match_bin_entry(void **entry, void *user)
788 struct isl_union_map_match_bin_data *data = user;
789 uint32_t hash;
790 struct isl_hash_table_entry *entry2;
791 isl_map *map = *entry;
792 int empty;
794 hash = isl_space_get_hash(map->dim);
795 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
796 hash, &has_dim, map->dim, 0);
797 if (!entry2)
798 return isl_stat_ok;
800 map = isl_map_copy(map);
801 map = data->fn(map, isl_map_copy(entry2->data));
803 empty = isl_map_is_empty(map);
804 if (empty < 0) {
805 isl_map_free(map);
806 return isl_stat_error;
808 if (empty) {
809 isl_map_free(map);
810 return isl_stat_ok;
813 data->res = isl_union_map_add_map(data->res, map);
815 return isl_stat_ok;
818 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
819 __isl_take isl_union_map *umap2,
820 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
822 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
824 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
825 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
827 if (!umap1 || !umap2)
828 goto error;
830 data.umap2 = umap2;
831 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
832 umap1->table.n);
833 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
834 &match_bin_entry, &data) < 0)
835 goto error;
837 isl_union_map_free(umap1);
838 isl_union_map_free(umap2);
839 return data.res;
840 error:
841 isl_union_map_free(umap1);
842 isl_union_map_free(umap2);
843 isl_union_map_free(data.res);
844 return NULL;
847 __isl_give isl_union_map *isl_union_map_intersect(
848 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
850 return match_bin_op(umap1, umap2, &isl_map_intersect);
853 /* Compute the intersection of the two union_sets.
854 * As a special case, if exactly one of the two union_sets
855 * is a parameter domain, then intersect the parameter domain
856 * of the other one with this set.
858 __isl_give isl_union_set *isl_union_set_intersect(
859 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
861 int p1, p2;
863 p1 = isl_union_set_is_params(uset1);
864 p2 = isl_union_set_is_params(uset2);
865 if (p1 < 0 || p2 < 0)
866 goto error;
867 if (!p1 && p2)
868 return union_map_intersect_params(uset1, uset2);
869 if (p1 && !p2)
870 return union_map_intersect_params(uset2, uset1);
871 return isl_union_map_intersect(uset1, uset2);
872 error:
873 isl_union_set_free(uset1);
874 isl_union_set_free(uset2);
875 return NULL;
878 static isl_stat gist_params_entry(void **entry, void *user)
880 struct isl_union_map_gen_bin_set_data *data = user;
881 isl_map *map = *entry;
882 int empty;
884 map = isl_map_copy(map);
885 map = isl_map_gist_params(map, isl_set_copy(data->set));
887 empty = isl_map_is_empty(map);
888 if (empty < 0) {
889 isl_map_free(map);
890 return isl_stat_error;
893 data->res = isl_union_map_add_map(data->res, map);
895 return isl_stat_ok;
898 __isl_give isl_union_map *isl_union_map_gist_params(
899 __isl_take isl_union_map *umap, __isl_take isl_set *set)
901 return gen_bin_set_op(umap, set, &gist_params_entry);
904 __isl_give isl_union_set *isl_union_set_gist_params(
905 __isl_take isl_union_set *uset, __isl_take isl_set *set)
907 return isl_union_map_gist_params(uset, set);
910 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
911 __isl_take isl_union_map *context)
913 return match_bin_op(umap, context, &isl_map_gist);
916 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
917 __isl_take isl_union_set *context)
919 if (isl_union_set_is_params(context))
920 return union_map_gist_params(uset, context);
921 return isl_union_map_gist(uset, context);
924 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
925 __isl_take isl_map *set2)
927 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
930 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
931 __isl_take isl_map *set2)
933 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
936 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
937 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
939 return match_bin_op(uset1, uset2, &lex_lt_set);
942 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
943 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
945 return match_bin_op(uset1, uset2, &lex_le_set);
948 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
949 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
951 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
954 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
955 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
957 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
960 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
961 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
963 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
966 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
967 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
969 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
972 static isl_stat intersect_domain_entry(void **entry, void *user)
974 struct isl_union_map_gen_bin_data *data = user;
975 uint32_t hash;
976 struct isl_hash_table_entry *entry2;
977 isl_space *dim;
978 isl_map *map = *entry;
979 isl_bool empty;
981 dim = isl_map_get_space(map);
982 dim = isl_space_domain(dim);
983 hash = isl_space_get_hash(dim);
984 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
985 hash, &has_dim, dim, 0);
986 isl_space_free(dim);
987 if (!entry2)
988 return isl_stat_ok;
990 map = isl_map_copy(map);
991 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
993 empty = isl_map_is_empty(map);
994 if (empty < 0) {
995 isl_map_free(map);
996 return isl_stat_error;
998 if (empty) {
999 isl_map_free(map);
1000 return isl_stat_ok;
1003 data->res = isl_union_map_add_map(data->res, map);
1005 return isl_stat_ok;
1008 /* Intersect the domain of "umap" with "uset".
1009 * If "uset" is a parameters domain, then intersect the parameter
1010 * domain of "umap" with this set.
1012 __isl_give isl_union_map *isl_union_map_intersect_domain(
1013 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1015 if (isl_union_set_is_params(uset))
1016 return union_map_intersect_params(umap, uset);
1017 return gen_bin_op(umap, uset, &intersect_domain_entry);
1020 /* Remove the elements of data->umap2 from the domain of *entry
1021 * and add the result to data->res.
1023 static isl_stat subtract_domain_entry(void **entry, void *user)
1025 struct isl_union_map_gen_bin_data *data = user;
1026 uint32_t hash;
1027 struct isl_hash_table_entry *entry2;
1028 isl_space *dim;
1029 isl_map *map = *entry;
1030 isl_bool empty;
1032 dim = isl_map_get_space(map);
1033 dim = isl_space_domain(dim);
1034 hash = isl_space_get_hash(dim);
1035 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1036 hash, &has_dim, dim, 0);
1037 isl_space_free(dim);
1039 map = isl_map_copy(map);
1041 if (!entry2) {
1042 data->res = isl_union_map_add_map(data->res, map);
1043 return isl_stat_ok;
1046 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1048 empty = isl_map_is_empty(map);
1049 if (empty < 0) {
1050 isl_map_free(map);
1051 return isl_stat_error;
1053 if (empty) {
1054 isl_map_free(map);
1055 return isl_stat_ok;
1058 data->res = isl_union_map_add_map(data->res, map);
1060 return isl_stat_ok;
1063 /* Remove the elements of "uset" from the domain of "umap".
1065 __isl_give isl_union_map *isl_union_map_subtract_domain(
1066 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1068 return gen_bin_op(umap, dom, &subtract_domain_entry);
1071 /* Remove the elements of data->umap2 from the range of *entry
1072 * and add the result to data->res.
1074 static isl_stat subtract_range_entry(void **entry, void *user)
1076 struct isl_union_map_gen_bin_data *data = user;
1077 uint32_t hash;
1078 struct isl_hash_table_entry *entry2;
1079 isl_space *space;
1080 isl_map *map = *entry;
1081 isl_bool empty;
1083 space = isl_map_get_space(map);
1084 space = isl_space_range(space);
1085 hash = isl_space_get_hash(space);
1086 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1087 hash, &has_dim, space, 0);
1088 isl_space_free(space);
1090 map = isl_map_copy(map);
1092 if (!entry2) {
1093 data->res = isl_union_map_add_map(data->res, map);
1094 return isl_stat_ok;
1097 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1099 empty = isl_map_is_empty(map);
1100 if (empty < 0) {
1101 isl_map_free(map);
1102 return isl_stat_error;
1104 if (empty) {
1105 isl_map_free(map);
1106 return isl_stat_ok;
1109 data->res = isl_union_map_add_map(data->res, map);
1111 return isl_stat_ok;
1114 /* Remove the elements of "uset" from the range of "umap".
1116 __isl_give isl_union_map *isl_union_map_subtract_range(
1117 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1119 return gen_bin_op(umap, dom, &subtract_range_entry);
1122 static isl_stat gist_domain_entry(void **entry, void *user)
1124 struct isl_union_map_gen_bin_data *data = user;
1125 uint32_t hash;
1126 struct isl_hash_table_entry *entry2;
1127 isl_space *dim;
1128 isl_map *map = *entry;
1129 isl_bool empty;
1131 dim = isl_map_get_space(map);
1132 dim = isl_space_domain(dim);
1133 hash = isl_space_get_hash(dim);
1134 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1135 hash, &has_dim, dim, 0);
1136 isl_space_free(dim);
1137 if (!entry2)
1138 return isl_stat_ok;
1140 map = isl_map_copy(map);
1141 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1143 empty = isl_map_is_empty(map);
1144 if (empty < 0) {
1145 isl_map_free(map);
1146 return isl_stat_error;
1149 data->res = isl_union_map_add_map(data->res, map);
1151 return isl_stat_ok;
1154 /* Compute the gist of "umap" with respect to the domain "uset".
1155 * If "uset" is a parameters domain, then compute the gist
1156 * with respect to this parameter domain.
1158 __isl_give isl_union_map *isl_union_map_gist_domain(
1159 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1161 if (isl_union_set_is_params(uset))
1162 return union_map_gist_params(umap, uset);
1163 return gen_bin_op(umap, uset, &gist_domain_entry);
1166 static isl_stat gist_range_entry(void **entry, void *user)
1168 struct isl_union_map_gen_bin_data *data = user;
1169 uint32_t hash;
1170 struct isl_hash_table_entry *entry2;
1171 isl_space *space;
1172 isl_map *map = *entry;
1173 isl_bool empty;
1175 space = isl_map_get_space(map);
1176 space = isl_space_range(space);
1177 hash = isl_space_get_hash(space);
1178 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1179 hash, &has_dim, space, 0);
1180 isl_space_free(space);
1181 if (!entry2)
1182 return isl_stat_ok;
1184 map = isl_map_copy(map);
1185 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1187 empty = isl_map_is_empty(map);
1188 if (empty < 0) {
1189 isl_map_free(map);
1190 return isl_stat_error;
1193 data->res = isl_union_map_add_map(data->res, map);
1195 return isl_stat_ok;
1198 /* Compute the gist of "umap" with respect to the range "uset".
1200 __isl_give isl_union_map *isl_union_map_gist_range(
1201 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1203 return gen_bin_op(umap, uset, &gist_range_entry);
1206 static isl_stat intersect_range_entry(void **entry, void *user)
1208 struct isl_union_map_gen_bin_data *data = user;
1209 uint32_t hash;
1210 struct isl_hash_table_entry *entry2;
1211 isl_space *dim;
1212 isl_map *map = *entry;
1213 isl_bool empty;
1215 dim = isl_map_get_space(map);
1216 dim = isl_space_range(dim);
1217 hash = isl_space_get_hash(dim);
1218 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1219 hash, &has_dim, dim, 0);
1220 isl_space_free(dim);
1221 if (!entry2)
1222 return isl_stat_ok;
1224 map = isl_map_copy(map);
1225 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1227 empty = isl_map_is_empty(map);
1228 if (empty < 0) {
1229 isl_map_free(map);
1230 return isl_stat_error;
1232 if (empty) {
1233 isl_map_free(map);
1234 return isl_stat_ok;
1237 data->res = isl_union_map_add_map(data->res, map);
1239 return isl_stat_ok;
1242 __isl_give isl_union_map *isl_union_map_intersect_range(
1243 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1245 return gen_bin_op(umap, uset, &intersect_range_entry);
1248 struct isl_union_map_bin_data {
1249 isl_union_map *umap2;
1250 isl_union_map *res;
1251 isl_map *map;
1252 isl_stat (*fn)(void **entry, void *user);
1255 static isl_stat apply_range_entry(void **entry, void *user)
1257 struct isl_union_map_bin_data *data = user;
1258 isl_map *map2 = *entry;
1259 isl_bool empty;
1261 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1262 map2->dim, isl_dim_in))
1263 return isl_stat_ok;
1265 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1267 empty = isl_map_is_empty(map2);
1268 if (empty < 0) {
1269 isl_map_free(map2);
1270 return isl_stat_error;
1272 if (empty) {
1273 isl_map_free(map2);
1274 return isl_stat_ok;
1277 data->res = isl_union_map_add_map(data->res, map2);
1279 return isl_stat_ok;
1282 static isl_stat bin_entry(void **entry, void *user)
1284 struct isl_union_map_bin_data *data = user;
1285 isl_map *map = *entry;
1287 data->map = map;
1288 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1289 data->fn, data) < 0)
1290 return isl_stat_error;
1292 return isl_stat_ok;
1295 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1296 __isl_take isl_union_map *umap2,
1297 isl_stat (*fn)(void **entry, void *user))
1299 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1301 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1302 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1304 if (!umap1 || !umap2)
1305 goto error;
1307 data.umap2 = umap2;
1308 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1309 umap1->table.n);
1310 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1311 &bin_entry, &data) < 0)
1312 goto error;
1314 isl_union_map_free(umap1);
1315 isl_union_map_free(umap2);
1316 return data.res;
1317 error:
1318 isl_union_map_free(umap1);
1319 isl_union_map_free(umap2);
1320 isl_union_map_free(data.res);
1321 return NULL;
1324 __isl_give isl_union_map *isl_union_map_apply_range(
1325 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1327 return bin_op(umap1, umap2, &apply_range_entry);
1330 __isl_give isl_union_map *isl_union_map_apply_domain(
1331 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1333 umap1 = isl_union_map_reverse(umap1);
1334 umap1 = isl_union_map_apply_range(umap1, umap2);
1335 return isl_union_map_reverse(umap1);
1338 __isl_give isl_union_set *isl_union_set_apply(
1339 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1341 return isl_union_map_apply_range(uset, umap);
1344 static isl_stat map_lex_lt_entry(void **entry, void *user)
1346 struct isl_union_map_bin_data *data = user;
1347 isl_map *map2 = *entry;
1349 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1350 map2->dim, isl_dim_out))
1351 return isl_stat_ok;
1353 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1355 data->res = isl_union_map_add_map(data->res, map2);
1357 return isl_stat_ok;
1360 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1361 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1363 return bin_op(umap1, umap2, &map_lex_lt_entry);
1366 static isl_stat map_lex_le_entry(void **entry, void *user)
1368 struct isl_union_map_bin_data *data = user;
1369 isl_map *map2 = *entry;
1371 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1372 map2->dim, isl_dim_out))
1373 return isl_stat_ok;
1375 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1377 data->res = isl_union_map_add_map(data->res, map2);
1379 return isl_stat_ok;
1382 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1383 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1385 return bin_op(umap1, umap2, &map_lex_le_entry);
1388 static isl_stat product_entry(void **entry, void *user)
1390 struct isl_union_map_bin_data *data = user;
1391 isl_map *map2 = *entry;
1393 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1395 data->res = isl_union_map_add_map(data->res, map2);
1397 return isl_stat_ok;
1400 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1401 __isl_take isl_union_map *umap2)
1403 return bin_op(umap1, umap2, &product_entry);
1406 static isl_stat set_product_entry(void **entry, void *user)
1408 struct isl_union_map_bin_data *data = user;
1409 isl_set *set2 = *entry;
1411 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1413 data->res = isl_union_set_add_set(data->res, set2);
1415 return isl_stat_ok;
1418 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1419 __isl_take isl_union_set *uset2)
1421 return bin_op(uset1, uset2, &set_product_entry);
1424 static isl_stat domain_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_out,
1430 map2->dim, isl_dim_out))
1431 return isl_stat_ok;
1433 map2 = isl_map_domain_product(isl_map_copy(data->map),
1434 isl_map_copy(map2));
1436 data->res = isl_union_map_add_map(data->res, map2);
1438 return isl_stat_ok;
1441 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1443 __isl_give isl_union_map *isl_union_map_domain_product(
1444 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1446 return bin_op(umap1, umap2, &domain_product_entry);
1449 static isl_stat range_product_entry(void **entry, void *user)
1451 struct isl_union_map_bin_data *data = user;
1452 isl_map *map2 = *entry;
1454 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1455 map2->dim, isl_dim_in))
1456 return isl_stat_ok;
1458 map2 = isl_map_range_product(isl_map_copy(data->map),
1459 isl_map_copy(map2));
1461 data->res = isl_union_map_add_map(data->res, map2);
1463 return isl_stat_ok;
1466 __isl_give isl_union_map *isl_union_map_range_product(
1467 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1469 return bin_op(umap1, umap2, &range_product_entry);
1472 /* If data->map A -> B and "map2" C -> D have the same range space,
1473 * then add (A, C) -> (B * D) to data->res.
1475 static isl_stat flat_domain_product_entry(void **entry, void *user)
1477 struct isl_union_map_bin_data *data = user;
1478 isl_map *map2 = *entry;
1480 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1481 map2->dim, isl_dim_out))
1482 return isl_stat_ok;
1484 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1485 isl_map_copy(map2));
1487 data->res = isl_union_map_add_map(data->res, map2);
1489 return isl_stat_ok;
1492 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1494 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1495 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1497 return bin_op(umap1, umap2, &flat_domain_product_entry);
1500 static isl_stat flat_range_product_entry(void **entry, void *user)
1502 struct isl_union_map_bin_data *data = user;
1503 isl_map *map2 = *entry;
1505 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1506 map2->dim, isl_dim_in))
1507 return isl_stat_ok;
1509 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1510 isl_map_copy(map2));
1512 data->res = isl_union_map_add_map(data->res, map2);
1514 return isl_stat_ok;
1517 __isl_give isl_union_map *isl_union_map_flat_range_product(
1518 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1520 return bin_op(umap1, umap2, &flat_range_product_entry);
1523 /* Data structure that specifies how cond_un_op should modify
1524 * the maps in the union map.
1526 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1527 * are taken into account.
1528 * "fn_map" specifies how the maps (selected by "filter")
1529 * should be transformed.
1531 struct isl_un_op_control {
1532 isl_bool (*filter)(__isl_keep isl_map *map);
1533 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1536 /* Internal data structure for "cond_un_op".
1537 * "control" specifies how the maps in the union map should be modified.
1538 * "res" collects the results.
1540 struct isl_union_map_un_data {
1541 struct isl_un_op_control *control;
1542 isl_union_map *res;
1545 /* isl_hash_table_foreach callback for cond_un_op.
1546 * Handle the map that "entry" points to.
1548 * If control->filter is set, then check if this map satisfies the filter.
1549 * If so (or if control->filter is not set), modify the map
1550 * by calling control->fn_map and add the result to data->res.
1552 static isl_stat un_entry(void **entry, void *user)
1554 struct isl_union_map_un_data *data = user;
1555 isl_map *map = *entry;
1557 if (data->control->filter) {
1558 isl_bool ok;
1560 ok = data->control->filter(map);
1561 if (ok < 0)
1562 return isl_stat_error;
1563 if (!ok)
1564 return isl_stat_ok;
1567 map = data->control->fn_map(isl_map_copy(map));
1568 data->res = isl_union_map_add_map(data->res, map);
1569 if (!data->res)
1570 return isl_stat_error;
1572 return isl_stat_ok;
1575 /* Modify the maps in "umap" based on "control".
1577 static __isl_give isl_union_map *cond_un_op(__isl_take isl_union_map *umap,
1578 struct isl_un_op_control *control)
1580 struct isl_union_map_un_data data = { control };
1581 isl_space *space;
1583 if (!umap)
1584 return NULL;
1586 space = isl_union_map_get_space(umap);
1587 data.res = isl_union_map_alloc(space, umap->table.n);
1588 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1589 &umap->table, &un_entry, &data) < 0)
1590 data.res = isl_union_map_free(data.res);
1592 isl_union_map_free(umap);
1593 return data.res;
1596 __isl_give isl_union_map *isl_union_map_from_range(
1597 __isl_take isl_union_set *uset)
1599 struct isl_un_op_control control = {
1600 .fn_map = &isl_map_from_range,
1602 return cond_un_op(uset, &control);
1605 __isl_give isl_union_map *isl_union_map_from_domain(
1606 __isl_take isl_union_set *uset)
1608 return isl_union_map_reverse(isl_union_map_from_range(uset));
1611 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1612 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1614 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1615 isl_union_map_from_range(range));
1618 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1619 isl_stat (*fn)(void **, void *))
1621 umap = isl_union_map_cow(umap);
1622 if (!umap)
1623 return NULL;
1625 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1626 &umap->table, fn, NULL) < 0)
1627 goto error;
1629 return umap;
1630 error:
1631 isl_union_map_free(umap);
1632 return NULL;
1635 static isl_stat affine_entry(void **entry, void *user)
1637 isl_map **map = (isl_map **)entry;
1639 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1641 return *map ? isl_stat_ok : isl_stat_error;
1644 __isl_give isl_union_map *isl_union_map_affine_hull(
1645 __isl_take isl_union_map *umap)
1647 return un_op(umap, &affine_entry);
1650 __isl_give isl_union_set *isl_union_set_affine_hull(
1651 __isl_take isl_union_set *uset)
1653 return isl_union_map_affine_hull(uset);
1656 static isl_stat polyhedral_entry(void **entry, void *user)
1658 isl_map **map = (isl_map **)entry;
1660 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1662 return *map ? isl_stat_ok : isl_stat_error;
1665 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1666 __isl_take isl_union_map *umap)
1668 return un_op(umap, &polyhedral_entry);
1671 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1672 __isl_take isl_union_set *uset)
1674 return isl_union_map_polyhedral_hull(uset);
1677 static isl_stat simple_entry(void **entry, void *user)
1679 isl_map **map = (isl_map **)entry;
1681 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1683 return *map ? isl_stat_ok : isl_stat_error;
1686 __isl_give isl_union_map *isl_union_map_simple_hull(
1687 __isl_take isl_union_map *umap)
1689 return un_op(umap, &simple_entry);
1692 __isl_give isl_union_set *isl_union_set_simple_hull(
1693 __isl_take isl_union_set *uset)
1695 return isl_union_map_simple_hull(uset);
1698 static isl_stat inplace_entry(void **entry, void *user)
1700 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1701 isl_map **map = (isl_map **)entry;
1702 isl_map *copy;
1704 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1705 copy = fn(isl_map_copy(*map));
1706 if (!copy)
1707 return isl_stat_error;
1709 isl_map_free(*map);
1710 *map = copy;
1712 return isl_stat_ok;
1715 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1716 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1718 if (!umap)
1719 return NULL;
1721 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
1722 &inplace_entry, &fn) < 0)
1723 goto error;
1725 return umap;
1726 error:
1727 isl_union_map_free(umap);
1728 return NULL;
1731 /* Remove redundant constraints in each of the basic maps of "umap".
1732 * Since removing redundant constraints does not change the meaning
1733 * or the space, the operation can be performed in-place.
1735 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1736 __isl_take isl_union_map *umap)
1738 return inplace(umap, &isl_map_remove_redundancies);
1741 /* Remove redundant constraints in each of the basic sets of "uset".
1743 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1744 __isl_take isl_union_set *uset)
1746 return isl_union_map_remove_redundancies(uset);
1749 __isl_give isl_union_map *isl_union_map_coalesce(
1750 __isl_take isl_union_map *umap)
1752 return inplace(umap, &isl_map_coalesce);
1755 __isl_give isl_union_set *isl_union_set_coalesce(
1756 __isl_take isl_union_set *uset)
1758 return isl_union_map_coalesce(uset);
1761 __isl_give isl_union_map *isl_union_map_detect_equalities(
1762 __isl_take isl_union_map *umap)
1764 return inplace(umap, &isl_map_detect_equalities);
1767 __isl_give isl_union_set *isl_union_set_detect_equalities(
1768 __isl_take isl_union_set *uset)
1770 return isl_union_map_detect_equalities(uset);
1773 __isl_give isl_union_map *isl_union_map_compute_divs(
1774 __isl_take isl_union_map *umap)
1776 return inplace(umap, &isl_map_compute_divs);
1779 __isl_give isl_union_set *isl_union_set_compute_divs(
1780 __isl_take isl_union_set *uset)
1782 return isl_union_map_compute_divs(uset);
1785 static isl_stat lexmin_entry(void **entry, void *user)
1787 isl_map **map = (isl_map **)entry;
1789 *map = isl_map_lexmin(*map);
1791 return *map ? isl_stat_ok : isl_stat_error;
1794 __isl_give isl_union_map *isl_union_map_lexmin(
1795 __isl_take isl_union_map *umap)
1797 return un_op(umap, &lexmin_entry);
1800 __isl_give isl_union_set *isl_union_set_lexmin(
1801 __isl_take isl_union_set *uset)
1803 return isl_union_map_lexmin(uset);
1806 static isl_stat lexmax_entry(void **entry, void *user)
1808 isl_map **map = (isl_map **)entry;
1810 *map = isl_map_lexmax(*map);
1812 return *map ? isl_stat_ok : isl_stat_error;
1815 __isl_give isl_union_map *isl_union_map_lexmax(
1816 __isl_take isl_union_map *umap)
1818 return un_op(umap, &lexmax_entry);
1821 __isl_give isl_union_set *isl_union_set_lexmax(
1822 __isl_take isl_union_set *uset)
1824 return isl_union_map_lexmax(uset);
1827 /* Return the universe in the space of "map".
1829 static __isl_give isl_map *universe(__isl_take isl_map *map)
1831 isl_space *space;
1833 space = isl_map_get_space(map);
1834 isl_map_free(map);
1835 return isl_map_universe(space);
1838 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1840 struct isl_un_op_control control = {
1841 .fn_map = &universe,
1843 return cond_un_op(umap, &control);
1846 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1848 return isl_union_map_universe(uset);
1851 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1853 struct isl_un_op_control control = {
1854 .fn_map = &isl_map_reverse,
1856 return cond_un_op(umap, &control);
1859 /* Compute the parameter domain of the given union map.
1861 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1863 struct isl_un_op_control control = {
1864 .fn_map = &isl_map_params,
1866 int empty;
1868 empty = isl_union_map_is_empty(umap);
1869 if (empty < 0)
1870 goto error;
1871 if (empty) {
1872 isl_space *space;
1873 space = isl_union_map_get_space(umap);
1874 isl_union_map_free(umap);
1875 return isl_set_empty(space);
1877 return isl_set_from_union_set(cond_un_op(umap, &control));
1878 error:
1879 isl_union_map_free(umap);
1880 return NULL;
1883 /* Compute the parameter domain of the given union set.
1885 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1887 return isl_union_map_params(uset);
1890 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1892 struct isl_un_op_control control = {
1893 .fn_map = &isl_map_domain,
1895 return cond_un_op(umap, &control);
1898 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1900 struct isl_un_op_control control = {
1901 .fn_map = &isl_map_range,
1903 return cond_un_op(umap, &control);
1906 __isl_give isl_union_map *isl_union_map_domain_map(
1907 __isl_take isl_union_map *umap)
1909 struct isl_un_op_control control = {
1910 .fn_map = &isl_map_domain_map,
1912 return cond_un_op(umap, &control);
1915 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1916 * add the result to "res".
1918 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1920 isl_union_pw_multi_aff **res = user;
1921 isl_multi_aff *ma;
1922 isl_pw_multi_aff *pma;
1924 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1925 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1926 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1928 return *res ? isl_stat_ok : isl_stat_error;
1932 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1933 * to its domain.
1935 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1936 __isl_take isl_union_map *umap)
1938 isl_union_pw_multi_aff *res;
1940 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1941 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1942 res = isl_union_pw_multi_aff_free(res);
1944 isl_union_map_free(umap);
1945 return res;
1948 __isl_give isl_union_map *isl_union_map_range_map(
1949 __isl_take isl_union_map *umap)
1951 struct isl_un_op_control control = {
1952 .fn_map = &isl_map_range_map,
1954 return cond_un_op(umap, &control);
1957 /* Given a collection of wrapped maps of the form A[B -> C],
1958 * return the collection of maps A[B -> C] -> B.
1960 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1961 __isl_take isl_union_set *uset)
1963 struct isl_un_op_control control = {
1964 .filter = &isl_set_is_wrapping,
1965 .fn_map = &isl_set_wrapped_domain_map,
1967 return cond_un_op(uset, &control);
1970 /* Does "map" relate elements from the same space?
1972 static isl_bool equal_tuples(__isl_keep isl_map *map)
1974 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
1975 map->dim, isl_dim_out);
1978 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1980 struct isl_un_op_control control = {
1981 .filter = &equal_tuples,
1982 .fn_map = &isl_map_deltas,
1984 return cond_un_op(umap, &control);
1987 __isl_give isl_union_map *isl_union_map_deltas_map(
1988 __isl_take isl_union_map *umap)
1990 struct isl_un_op_control control = {
1991 .filter = &equal_tuples,
1992 .fn_map = &isl_map_deltas_map,
1994 return cond_un_op(umap, &control);
1997 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1999 struct isl_un_op_control control = {
2000 .fn_map = &isl_set_identity,
2002 return cond_un_op(uset, &control);
2005 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2007 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2009 isl_union_pw_multi_aff **res = user;
2010 isl_space *space;
2011 isl_pw_multi_aff *pma;
2013 space = isl_space_map_from_set(isl_set_get_space(set));
2014 pma = isl_pw_multi_aff_identity(space);
2015 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2016 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2018 return *res ? isl_stat_ok : isl_stat_error;
2021 /* Return an identity function on "uset" in the form
2022 * of an isl_union_pw_multi_aff.
2024 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2025 __isl_take isl_union_set *uset)
2027 isl_union_pw_multi_aff *res;
2029 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2030 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2031 res = isl_union_pw_multi_aff_free(res);
2033 isl_union_set_free(uset);
2034 return res;
2037 /* For each map in "umap" of the form [A -> B] -> C,
2038 * construct the map A -> C and collect the results.
2040 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2041 __isl_take isl_union_map *umap)
2043 struct isl_un_op_control control = {
2044 .filter = &isl_map_domain_is_wrapping,
2045 .fn_map = &isl_map_domain_factor_domain,
2047 return cond_un_op(umap, &control);
2050 /* For each map in "umap" of the form [A -> B] -> C,
2051 * construct the map B -> C and collect the results.
2053 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2054 __isl_take isl_union_map *umap)
2056 struct isl_un_op_control control = {
2057 .filter = &isl_map_domain_is_wrapping,
2058 .fn_map = &isl_map_domain_factor_range,
2060 return cond_un_op(umap, &control);
2063 /* For each map in "umap" of the form A -> [B -> C],
2064 * construct the map A -> B and collect the results.
2066 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2067 __isl_take isl_union_map *umap)
2069 struct isl_un_op_control control = {
2070 .filter = &isl_map_range_is_wrapping,
2071 .fn_map = &isl_map_range_factor_domain,
2073 return cond_un_op(umap, &control);
2076 /* For each map in "umap" of the form A -> [B -> C],
2077 * construct the map A -> C and collect the results.
2079 __isl_give isl_union_map *isl_union_map_range_factor_range(
2080 __isl_take isl_union_map *umap)
2082 struct isl_un_op_control control = {
2083 .filter = &isl_map_range_is_wrapping,
2084 .fn_map = &isl_map_range_factor_range,
2086 return cond_un_op(umap, &control);
2089 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2090 * construct the map A -> C and collect the results.
2092 __isl_give isl_union_map *isl_union_map_factor_domain(
2093 __isl_take isl_union_map *umap)
2095 struct isl_un_op_control control = {
2096 .filter = &isl_map_is_product,
2097 .fn_map = &isl_map_factor_domain,
2099 return cond_un_op(umap, &control);
2102 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2103 * construct the map B -> D and collect the results.
2105 __isl_give isl_union_map *isl_union_map_factor_range(
2106 __isl_take isl_union_map *umap)
2108 struct isl_un_op_control control = {
2109 .filter = &isl_map_is_product,
2110 .fn_map = &isl_map_factor_range,
2112 return cond_un_op(umap, &control);
2115 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2117 struct isl_un_op_control control = {
2118 .filter = &isl_set_is_wrapping,
2119 .fn_map = &isl_set_unwrap,
2121 return cond_un_op(uset, &control);
2124 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2126 struct isl_un_op_control control = {
2127 .fn_map = &isl_map_wrap,
2129 return cond_un_op(umap, &control);
2132 struct isl_union_map_is_subset_data {
2133 isl_union_map *umap2;
2134 isl_bool is_subset;
2137 static isl_stat is_subset_entry(void **entry, void *user)
2139 struct isl_union_map_is_subset_data *data = user;
2140 uint32_t hash;
2141 struct isl_hash_table_entry *entry2;
2142 isl_map *map = *entry;
2144 hash = isl_space_get_hash(map->dim);
2145 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2146 hash, &has_dim, map->dim, 0);
2147 if (!entry2) {
2148 int empty = isl_map_is_empty(map);
2149 if (empty < 0)
2150 return isl_stat_error;
2151 if (empty)
2152 return isl_stat_ok;
2153 data->is_subset = 0;
2154 return isl_stat_error;
2157 data->is_subset = isl_map_is_subset(map, entry2->data);
2158 if (data->is_subset < 0 || !data->is_subset)
2159 return isl_stat_error;
2161 return isl_stat_ok;
2164 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2165 __isl_keep isl_union_map *umap2)
2167 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2169 umap1 = isl_union_map_copy(umap1);
2170 umap2 = isl_union_map_copy(umap2);
2171 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2172 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2174 if (!umap1 || !umap2)
2175 goto error;
2177 data.umap2 = umap2;
2178 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2179 &is_subset_entry, &data) < 0 &&
2180 data.is_subset)
2181 goto error;
2183 isl_union_map_free(umap1);
2184 isl_union_map_free(umap2);
2186 return data.is_subset;
2187 error:
2188 isl_union_map_free(umap1);
2189 isl_union_map_free(umap2);
2190 return isl_bool_error;
2193 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2194 __isl_keep isl_union_set *uset2)
2196 return isl_union_map_is_subset(uset1, uset2);
2199 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2200 __isl_keep isl_union_map *umap2)
2202 isl_bool is_subset;
2204 if (!umap1 || !umap2)
2205 return isl_bool_error;
2206 is_subset = isl_union_map_is_subset(umap1, umap2);
2207 if (is_subset != isl_bool_true)
2208 return is_subset;
2209 is_subset = isl_union_map_is_subset(umap2, umap1);
2210 return is_subset;
2213 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2214 __isl_keep isl_union_set *uset2)
2216 return isl_union_map_is_equal(uset1, uset2);
2219 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2220 __isl_keep isl_union_map *umap2)
2222 isl_bool is_subset;
2224 if (!umap1 || !umap2)
2225 return isl_bool_error;
2226 is_subset = isl_union_map_is_subset(umap1, umap2);
2227 if (is_subset != isl_bool_true)
2228 return is_subset;
2229 is_subset = isl_union_map_is_subset(umap2, umap1);
2230 if (is_subset == isl_bool_error)
2231 return is_subset;
2232 return !is_subset;
2235 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2236 __isl_keep isl_union_set *uset2)
2238 return isl_union_map_is_strict_subset(uset1, uset2);
2241 /* Internal data structure for isl_union_map_is_disjoint.
2242 * umap2 is the union map with which we are comparing.
2243 * is_disjoint is initialized to 1 and is set to 0 as soon
2244 * as the union maps turn out not to be disjoint.
2246 struct isl_union_map_is_disjoint_data {
2247 isl_union_map *umap2;
2248 isl_bool is_disjoint;
2251 /* Check if "map" is disjoint from data->umap2 and abort
2252 * the search if it is not.
2254 static isl_stat is_disjoint_entry(void **entry, void *user)
2256 struct isl_union_map_is_disjoint_data *data = user;
2257 uint32_t hash;
2258 struct isl_hash_table_entry *entry2;
2259 isl_map *map = *entry;
2261 hash = isl_space_get_hash(map->dim);
2262 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2263 hash, &has_dim, map->dim, 0);
2264 if (!entry2)
2265 return isl_stat_ok;
2267 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2268 if (data->is_disjoint < 0 || !data->is_disjoint)
2269 return isl_stat_error;
2271 return isl_stat_ok;
2274 /* Are "umap1" and "umap2" disjoint?
2276 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2277 __isl_keep isl_union_map *umap2)
2279 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2281 umap1 = isl_union_map_copy(umap1);
2282 umap2 = isl_union_map_copy(umap2);
2283 umap1 = isl_union_map_align_params(umap1,
2284 isl_union_map_get_space(umap2));
2285 umap2 = isl_union_map_align_params(umap2,
2286 isl_union_map_get_space(umap1));
2288 if (!umap1 || !umap2)
2289 goto error;
2291 data.umap2 = umap2;
2292 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2293 &is_disjoint_entry, &data) < 0 &&
2294 data.is_disjoint)
2295 goto error;
2297 isl_union_map_free(umap1);
2298 isl_union_map_free(umap2);
2300 return data.is_disjoint;
2301 error:
2302 isl_union_map_free(umap1);
2303 isl_union_map_free(umap2);
2304 return isl_bool_error;
2307 /* Are "uset1" and "uset2" disjoint?
2309 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2310 __isl_keep isl_union_set *uset2)
2312 return isl_union_map_is_disjoint(uset1, uset2);
2315 static isl_stat sample_entry(void **entry, void *user)
2317 isl_basic_map **sample = (isl_basic_map **)user;
2318 isl_map *map = *entry;
2320 *sample = isl_map_sample(isl_map_copy(map));
2321 if (!*sample)
2322 return isl_stat_error;
2323 if (!isl_basic_map_plain_is_empty(*sample))
2324 return isl_stat_error;
2325 return isl_stat_ok;
2328 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2330 isl_basic_map *sample = NULL;
2332 if (!umap)
2333 return NULL;
2335 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2336 &sample_entry, &sample) < 0 &&
2337 !sample)
2338 goto error;
2340 if (!sample)
2341 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2343 isl_union_map_free(umap);
2345 return sample;
2346 error:
2347 isl_union_map_free(umap);
2348 return NULL;
2351 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2353 return bset_from_bmap(isl_union_map_sample(uset));
2356 /* Return an element in "uset" in the form of an isl_point.
2357 * Return a void isl_point if "uset" is empty.
2359 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2361 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2364 struct isl_forall_data {
2365 isl_bool res;
2366 isl_bool (*fn)(__isl_keep isl_map *map);
2369 static isl_stat forall_entry(void **entry, void *user)
2371 struct isl_forall_data *data = user;
2372 isl_map *map = *entry;
2374 data->res = data->fn(map);
2375 if (data->res < 0)
2376 return isl_stat_error;
2378 if (!data->res)
2379 return isl_stat_error;
2381 return isl_stat_ok;
2384 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2385 isl_bool (*fn)(__isl_keep isl_map *map))
2387 struct isl_forall_data data = { isl_bool_true, fn };
2389 if (!umap)
2390 return isl_bool_error;
2392 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2393 &forall_entry, &data) < 0 && data.res)
2394 return isl_bool_error;
2396 return data.res;
2399 struct isl_forall_user_data {
2400 isl_bool res;
2401 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2402 void *user;
2405 static isl_stat forall_user_entry(void **entry, void *user)
2407 struct isl_forall_user_data *data = user;
2408 isl_map *map = *entry;
2410 data->res = data->fn(map, data->user);
2411 if (data->res < 0)
2412 return isl_stat_error;
2414 if (!data->res)
2415 return isl_stat_error;
2417 return isl_stat_ok;
2420 /* Check if fn(map, user) returns true for all maps "map" in umap.
2422 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2423 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2425 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2427 if (!umap)
2428 return isl_bool_error;
2430 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2431 &forall_user_entry, &data) < 0 && data.res)
2432 return isl_bool_error;
2434 return data.res;
2437 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2439 return union_map_forall(umap, &isl_map_is_empty);
2442 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2444 return isl_union_map_is_empty(uset);
2447 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2449 isl_bool is_subset;
2450 isl_space *dim;
2451 isl_map *id;
2453 if (!map)
2454 return isl_bool_error;
2456 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2457 map->dim, isl_dim_out))
2458 return isl_bool_false;
2460 dim = isl_map_get_space(map);
2461 id = isl_map_identity(dim);
2463 is_subset = isl_map_is_subset(map, id);
2465 isl_map_free(id);
2467 return is_subset;
2470 /* Given an isl_union_map that consists of a single map, check
2471 * if it is single-valued.
2473 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2475 isl_map *map;
2476 isl_bool sv;
2478 umap = isl_union_map_copy(umap);
2479 map = isl_map_from_union_map(umap);
2480 sv = isl_map_is_single_valued(map);
2481 isl_map_free(map);
2483 return sv;
2486 /* Internal data structure for single_valued_on_domain.
2488 * "umap" is the union map to be tested.
2489 * "sv" is set to 1 as long as "umap" may still be single-valued.
2491 struct isl_union_map_is_sv_data {
2492 isl_union_map *umap;
2493 isl_bool sv;
2496 /* Check if the data->umap is single-valued on "set".
2498 * If data->umap consists of a single map on "set", then test it
2499 * as an isl_map.
2501 * Otherwise, compute
2503 * M \circ M^-1
2505 * check if the result is a subset of the identity mapping and
2506 * store the result in data->sv.
2508 * Terminate as soon as data->umap has been determined not to
2509 * be single-valued.
2511 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2513 struct isl_union_map_is_sv_data *data = user;
2514 isl_union_map *umap, *test;
2516 umap = isl_union_map_copy(data->umap);
2517 umap = isl_union_map_intersect_domain(umap,
2518 isl_union_set_from_set(set));
2520 if (isl_union_map_n_map(umap) == 1) {
2521 data->sv = single_map_is_single_valued(umap);
2522 isl_union_map_free(umap);
2523 } else {
2524 test = isl_union_map_reverse(isl_union_map_copy(umap));
2525 test = isl_union_map_apply_range(test, umap);
2527 data->sv = union_map_forall(test, &is_subset_of_identity);
2529 isl_union_map_free(test);
2532 if (data->sv < 0 || !data->sv)
2533 return isl_stat_error;
2534 return isl_stat_ok;
2537 /* Check if the given map is single-valued.
2539 * If the union map consists of a single map, then test it as an isl_map.
2540 * Otherwise, check if the union map is single-valued on each of its
2541 * domain spaces.
2543 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2545 isl_union_map *universe;
2546 isl_union_set *domain;
2547 struct isl_union_map_is_sv_data data;
2549 if (isl_union_map_n_map(umap) == 1)
2550 return single_map_is_single_valued(umap);
2552 universe = isl_union_map_universe(isl_union_map_copy(umap));
2553 domain = isl_union_map_domain(universe);
2555 data.sv = isl_bool_true;
2556 data.umap = umap;
2557 if (isl_union_set_foreach_set(domain,
2558 &single_valued_on_domain, &data) < 0 && data.sv)
2559 data.sv = isl_bool_error;
2560 isl_union_set_free(domain);
2562 return data.sv;
2565 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2567 isl_bool in;
2569 umap = isl_union_map_copy(umap);
2570 umap = isl_union_map_reverse(umap);
2571 in = isl_union_map_is_single_valued(umap);
2572 isl_union_map_free(umap);
2574 return in;
2577 /* Is "map" obviously not an identity relation because
2578 * it maps elements from one space to another space?
2579 * Update *non_identity accordingly.
2581 * In particular, if the domain and range spaces are the same,
2582 * then the map is not considered to obviously not be an identity relation.
2583 * Otherwise, the map is considered to obviously not be an identity relation
2584 * if it is is non-empty.
2586 * If "map" is determined to obviously not be an identity relation,
2587 * then the search is aborted.
2589 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2591 isl_bool *non_identity = user;
2592 isl_bool equal;
2593 isl_space *space;
2595 space = isl_map_get_space(map);
2596 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2597 if (equal >= 0 && !equal)
2598 *non_identity = isl_bool_not(isl_map_is_empty(map));
2599 else
2600 *non_identity = isl_bool_not(equal);
2601 isl_space_free(space);
2602 isl_map_free(map);
2604 if (*non_identity < 0 || *non_identity)
2605 return isl_stat_error;
2607 return isl_stat_ok;
2610 /* Is "umap" obviously not an identity relation because
2611 * it maps elements from one space to another space?
2613 * As soon as a map has been found that maps elements to a different space,
2614 * non_identity is changed and the search is aborted.
2616 static isl_bool isl_union_map_plain_is_not_identity(
2617 __isl_keep isl_union_map *umap)
2619 isl_bool non_identity;
2621 non_identity = isl_bool_false;
2622 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2623 &non_identity) < 0 &&
2624 non_identity == isl_bool_false)
2625 return isl_bool_error;
2627 return non_identity;
2630 /* Does "map" only map elements to themselves?
2631 * Update *identity accordingly.
2633 * If "map" is determined not to be an identity relation,
2634 * then the search is aborted.
2636 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2638 isl_bool *identity = user;
2640 *identity = isl_map_is_identity(map);
2641 isl_map_free(map);
2643 if (*identity < 0 || !*identity)
2644 return isl_stat_error;
2646 return isl_stat_ok;
2649 /* Does "umap" only map elements to themselves?
2651 * First check if there are any maps that map elements to different spaces.
2652 * If not, then check that all the maps (between identical spaces)
2653 * are identity relations.
2655 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2657 isl_bool non_identity;
2658 isl_bool identity;
2660 non_identity = isl_union_map_plain_is_not_identity(umap);
2661 if (non_identity < 0 || non_identity)
2662 return isl_bool_not(non_identity);
2664 identity = isl_bool_true;
2665 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2666 identity == isl_bool_true)
2667 return isl_bool_error;
2669 return identity;
2672 /* Represents a map that has a fixed value (v) for one of its
2673 * range dimensions.
2674 * The map in this structure is not reference counted, so it
2675 * is only valid while the isl_union_map from which it was
2676 * obtained is still alive.
2678 struct isl_fixed_map {
2679 isl_int v;
2680 isl_map *map;
2683 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2684 int n)
2686 int i;
2687 struct isl_fixed_map *v;
2689 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2690 if (!v)
2691 return NULL;
2692 for (i = 0; i < n; ++i)
2693 isl_int_init(v[i].v);
2694 return v;
2697 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2699 int i;
2701 if (!v)
2702 return;
2703 for (i = 0; i < n; ++i)
2704 isl_int_clear(v[i].v);
2705 free(v);
2708 /* Compare the "v" field of two isl_fixed_map structs.
2710 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2712 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2713 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2715 return isl_int_cmp(e1->v, e2->v);
2718 /* Internal data structure used while checking whether all maps
2719 * in a union_map have a fixed value for a given output dimension.
2720 * v is the list of maps, with the fixed value for the dimension
2721 * n is the number of maps considered so far
2722 * pos is the output dimension under investigation
2724 struct isl_fixed_dim_data {
2725 struct isl_fixed_map *v;
2726 int n;
2727 int pos;
2730 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2732 struct isl_fixed_dim_data *data = user;
2734 data->v[data->n].map = map;
2735 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2736 &data->v[data->n++].v);
2739 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2740 int first, int n_range);
2742 /* Given a list of the maps, with their fixed values at output dimension "pos",
2743 * check whether the ranges of the maps form an obvious partition.
2745 * We first sort the maps according to their fixed values.
2746 * If all maps have a different value, then we know the ranges form
2747 * a partition.
2748 * Otherwise, we collect the maps with the same fixed value and
2749 * check whether each such collection is obviously injective
2750 * based on later dimensions.
2752 static int separates(struct isl_fixed_map *v, int n,
2753 __isl_take isl_space *dim, int pos, int n_range)
2755 int i;
2757 if (!v)
2758 goto error;
2760 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2762 for (i = 0; i + 1 < n; ++i) {
2763 int j, k;
2764 isl_union_map *part;
2765 int injective;
2767 for (j = i + 1; j < n; ++j)
2768 if (isl_int_ne(v[i].v, v[j].v))
2769 break;
2771 if (j == i + 1)
2772 continue;
2774 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2775 for (k = i; k < j; ++k)
2776 part = isl_union_map_add_map(part,
2777 isl_map_copy(v[k].map));
2779 injective = plain_injective_on_range(part, pos + 1, n_range);
2780 if (injective < 0)
2781 goto error;
2782 if (!injective)
2783 break;
2785 i = j - 1;
2788 isl_space_free(dim);
2789 free_isl_fixed_map_array(v, n);
2790 return i + 1 >= n;
2791 error:
2792 isl_space_free(dim);
2793 free_isl_fixed_map_array(v, n);
2794 return -1;
2797 /* Check whether the maps in umap have obviously distinct ranges.
2798 * In particular, check for an output dimension in the range
2799 * [first,n_range) for which all maps have a fixed value
2800 * and then check if these values, possibly along with fixed values
2801 * at later dimensions, entail distinct ranges.
2803 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2804 int first, int n_range)
2806 isl_ctx *ctx;
2807 int n;
2808 struct isl_fixed_dim_data data = { NULL };
2810 ctx = isl_union_map_get_ctx(umap);
2812 n = isl_union_map_n_map(umap);
2813 if (!umap)
2814 goto error;
2816 if (n <= 1) {
2817 isl_union_map_free(umap);
2818 return isl_bool_true;
2821 if (first >= n_range) {
2822 isl_union_map_free(umap);
2823 return isl_bool_false;
2826 data.v = alloc_isl_fixed_map_array(ctx, n);
2827 if (!data.v)
2828 goto error;
2830 for (data.pos = first; data.pos < n_range; ++data.pos) {
2831 isl_bool fixed;
2832 int injective;
2833 isl_space *dim;
2835 data.n = 0;
2836 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2837 if (fixed < 0)
2838 goto error;
2839 if (!fixed)
2840 continue;
2841 dim = isl_union_map_get_space(umap);
2842 injective = separates(data.v, n, dim, data.pos, n_range);
2843 isl_union_map_free(umap);
2844 return injective;
2847 free_isl_fixed_map_array(data.v, n);
2848 isl_union_map_free(umap);
2850 return isl_bool_false;
2851 error:
2852 free_isl_fixed_map_array(data.v, n);
2853 isl_union_map_free(umap);
2854 return isl_bool_error;
2857 /* Check whether the maps in umap that map to subsets of "ran"
2858 * have obviously distinct ranges.
2860 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2861 void *user)
2863 isl_union_map *umap = user;
2865 umap = isl_union_map_copy(umap);
2866 umap = isl_union_map_intersect_range(umap,
2867 isl_union_set_from_set(isl_set_copy(ran)));
2868 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2871 /* Check if the given union_map is obviously injective.
2873 * In particular, we first check if all individual maps are obviously
2874 * injective and then check if all the ranges of these maps are
2875 * obviously disjoint.
2877 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2879 isl_bool in;
2880 isl_union_map *univ;
2881 isl_union_set *ran;
2883 in = union_map_forall(umap, &isl_map_plain_is_injective);
2884 if (in < 0)
2885 return isl_bool_error;
2886 if (!in)
2887 return isl_bool_false;
2889 univ = isl_union_map_universe(isl_union_map_copy(umap));
2890 ran = isl_union_map_range(univ);
2892 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2894 isl_union_set_free(ran);
2896 return in;
2899 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2901 isl_bool sv;
2903 sv = isl_union_map_is_single_valued(umap);
2904 if (sv < 0 || !sv)
2905 return sv;
2907 return isl_union_map_is_injective(umap);
2910 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2912 struct isl_un_op_control control = {
2913 .filter = &isl_map_can_zip,
2914 .fn_map = &isl_map_zip,
2916 return cond_un_op(umap, &control);
2919 /* Given a union map, take the maps of the form A -> (B -> C) and
2920 * return the union of the corresponding maps (A -> B) -> C.
2922 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2924 struct isl_un_op_control control = {
2925 .filter = &isl_map_can_uncurry,
2926 .fn_map = &isl_map_uncurry,
2928 return cond_un_op(umap, &control);
2931 /* Given a union map, take the maps of the form (A -> B) -> C and
2932 * return the union of the corresponding maps A -> (B -> C).
2934 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2936 struct isl_un_op_control control = {
2937 .filter = &isl_map_can_curry,
2938 .fn_map = &isl_map_curry,
2940 return cond_un_op(umap, &control);
2943 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2944 * return the union of the corresponding maps A -> (B -> (C -> D)).
2946 __isl_give isl_union_map *isl_union_map_range_curry(
2947 __isl_take isl_union_map *umap)
2949 struct isl_un_op_control control = {
2950 .filter = &isl_map_can_range_curry,
2951 .fn_map = &isl_map_range_curry,
2953 return cond_un_op(umap, &control);
2956 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2958 struct isl_un_op_control control = {
2959 .fn_map = &isl_set_lift,
2961 return cond_un_op(uset, &control);
2964 static isl_stat coefficients_entry(void **entry, void *user)
2966 isl_set *set = *entry;
2967 isl_union_set **res = user;
2969 set = isl_set_copy(set);
2970 set = isl_set_from_basic_set(isl_set_coefficients(set));
2971 *res = isl_union_set_add_set(*res, set);
2973 return isl_stat_ok;
2976 __isl_give isl_union_set *isl_union_set_coefficients(
2977 __isl_take isl_union_set *uset)
2979 isl_ctx *ctx;
2980 isl_space *dim;
2981 isl_union_set *res;
2983 if (!uset)
2984 return NULL;
2986 ctx = isl_union_set_get_ctx(uset);
2987 dim = isl_space_set_alloc(ctx, 0, 0);
2988 res = isl_union_map_alloc(dim, uset->table.n);
2989 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2990 &coefficients_entry, &res) < 0)
2991 goto error;
2993 isl_union_set_free(uset);
2994 return res;
2995 error:
2996 isl_union_set_free(uset);
2997 isl_union_set_free(res);
2998 return NULL;
3001 static isl_stat solutions_entry(void **entry, void *user)
3003 isl_set *set = *entry;
3004 isl_union_set **res = user;
3006 set = isl_set_copy(set);
3007 set = isl_set_from_basic_set(isl_set_solutions(set));
3008 if (!*res)
3009 *res = isl_union_set_from_set(set);
3010 else
3011 *res = isl_union_set_add_set(*res, set);
3013 if (!*res)
3014 return isl_stat_error;
3016 return isl_stat_ok;
3019 __isl_give isl_union_set *isl_union_set_solutions(
3020 __isl_take isl_union_set *uset)
3022 isl_union_set *res = NULL;
3024 if (!uset)
3025 return NULL;
3027 if (uset->table.n == 0) {
3028 res = isl_union_set_empty(isl_union_set_get_space(uset));
3029 isl_union_set_free(uset);
3030 return res;
3033 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3034 &solutions_entry, &res) < 0)
3035 goto error;
3037 isl_union_set_free(uset);
3038 return res;
3039 error:
3040 isl_union_set_free(uset);
3041 isl_union_set_free(res);
3042 return NULL;
3045 /* Is the domain space of "map" equal to "space"?
3047 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3049 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3050 space, isl_dim_out);
3053 /* Is the range space of "map" equal to "space"?
3055 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3057 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3058 space, isl_dim_out);
3061 /* Is the set space of "map" equal to "space"?
3063 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3065 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3066 space, isl_dim_out);
3069 /* Internal data structure for preimage_pw_multi_aff.
3071 * "pma" is the function under which the preimage should be taken.
3072 * "space" is the space of "pma".
3073 * "res" collects the results.
3074 * "fn" computes the preimage for a given map.
3075 * "match" returns true if "fn" can be called.
3077 struct isl_union_map_preimage_data {
3078 isl_space *space;
3079 isl_pw_multi_aff *pma;
3080 isl_union_map *res;
3081 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3082 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3083 __isl_take isl_pw_multi_aff *pma);
3086 /* Call data->fn to compute the preimage of the domain or range of *entry
3087 * under the function represented by data->pma, provided the domain/range
3088 * space of *entry matches the target space of data->pma
3089 * (as given by data->match), and add the result to data->res.
3091 static isl_stat preimage_entry(void **entry, void *user)
3093 int m;
3094 isl_map *map = *entry;
3095 struct isl_union_map_preimage_data *data = user;
3096 isl_bool empty;
3098 m = data->match(map, data->space);
3099 if (m < 0)
3100 return isl_stat_error;
3101 if (!m)
3102 return isl_stat_ok;
3104 map = isl_map_copy(map);
3105 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3107 empty = isl_map_is_empty(map);
3108 if (empty < 0 || empty) {
3109 isl_map_free(map);
3110 return empty < 0 ? isl_stat_error : isl_stat_ok;
3113 data->res = isl_union_map_add_map(data->res, map);
3115 return isl_stat_ok;
3118 /* Compute the preimage of the domain or range of "umap" under the function
3119 * represented by "pma".
3120 * In other words, plug in "pma" in the domain or range of "umap".
3121 * The function "fn" performs the actual preimage computation on a map,
3122 * while "match" determines to which maps the function should be applied.
3124 static __isl_give isl_union_map *preimage_pw_multi_aff(
3125 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3126 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3127 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3128 __isl_take isl_pw_multi_aff *pma))
3130 isl_ctx *ctx;
3131 isl_space *space;
3132 struct isl_union_map_preimage_data data;
3134 umap = isl_union_map_align_params(umap,
3135 isl_pw_multi_aff_get_space(pma));
3136 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3138 if (!umap || !pma)
3139 goto error;
3141 ctx = isl_union_map_get_ctx(umap);
3142 space = isl_union_map_get_space(umap);
3143 data.space = isl_pw_multi_aff_get_space(pma);
3144 data.pma = pma;
3145 data.res = isl_union_map_alloc(space, umap->table.n);
3146 data.match = match;
3147 data.fn = fn;
3148 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3149 &data) < 0)
3150 data.res = isl_union_map_free(data.res);
3152 isl_space_free(data.space);
3153 isl_union_map_free(umap);
3154 isl_pw_multi_aff_free(pma);
3155 return data.res;
3156 error:
3157 isl_union_map_free(umap);
3158 isl_pw_multi_aff_free(pma);
3159 return NULL;
3162 /* Compute the preimage of the domain of "umap" under the function
3163 * represented by "pma".
3164 * In other words, plug in "pma" in the domain of "umap".
3165 * The result contains maps that live in the same spaces as the maps of "umap"
3166 * with domain space equal to the target space of "pma",
3167 * except that the domain has been replaced by the domain space of "pma".
3169 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3170 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3172 return preimage_pw_multi_aff(umap, pma, &domain_match,
3173 &isl_map_preimage_domain_pw_multi_aff);
3176 /* Compute the preimage of the range of "umap" under the function
3177 * represented by "pma".
3178 * In other words, plug in "pma" in the range of "umap".
3179 * The result contains maps that live in the same spaces as the maps of "umap"
3180 * with range space equal to the target space of "pma",
3181 * except that the range has been replaced by the domain space of "pma".
3183 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3184 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3186 return preimage_pw_multi_aff(umap, pma, &range_match,
3187 &isl_map_preimage_range_pw_multi_aff);
3190 /* Compute the preimage of "uset" under the function represented by "pma".
3191 * In other words, plug in "pma" in "uset".
3192 * The result contains sets that live in the same spaces as the sets of "uset"
3193 * with space equal to the target space of "pma",
3194 * except that the space has been replaced by the domain space of "pma".
3196 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3197 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3199 return preimage_pw_multi_aff(uset, pma, &set_match,
3200 &isl_set_preimage_pw_multi_aff);
3203 /* Compute the preimage of the domain of "umap" under the function
3204 * represented by "ma".
3205 * In other words, plug in "ma" in the domain of "umap".
3206 * The result contains maps that live in the same spaces as the maps of "umap"
3207 * with domain space equal to the target space of "ma",
3208 * except that the domain has been replaced by the domain space of "ma".
3210 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3211 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3213 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3214 isl_pw_multi_aff_from_multi_aff(ma));
3217 /* Compute the preimage of the range of "umap" under the function
3218 * represented by "ma".
3219 * In other words, plug in "ma" in the range of "umap".
3220 * The result contains maps that live in the same spaces as the maps of "umap"
3221 * with range space equal to the target space of "ma",
3222 * except that the range has been replaced by the domain space of "ma".
3224 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3225 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3227 return isl_union_map_preimage_range_pw_multi_aff(umap,
3228 isl_pw_multi_aff_from_multi_aff(ma));
3231 /* Compute the preimage of "uset" under the function represented by "ma".
3232 * In other words, plug in "ma" in "uset".
3233 * The result contains sets that live in the same spaces as the sets of "uset"
3234 * with space equal to the target space of "ma",
3235 * except that the space has been replaced by the domain space of "ma".
3237 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3238 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3240 return isl_union_set_preimage_pw_multi_aff(uset,
3241 isl_pw_multi_aff_from_multi_aff(ma));
3244 /* Internal data structure for preimage_multi_pw_aff.
3246 * "mpa" is the function under which the preimage should be taken.
3247 * "space" is the space of "mpa".
3248 * "res" collects the results.
3249 * "fn" computes the preimage for a given map.
3250 * "match" returns true if "fn" can be called.
3252 struct isl_union_map_preimage_mpa_data {
3253 isl_space *space;
3254 isl_multi_pw_aff *mpa;
3255 isl_union_map *res;
3256 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3257 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3258 __isl_take isl_multi_pw_aff *mpa);
3261 /* Call data->fn to compute the preimage of the domain or range of *entry
3262 * under the function represented by data->mpa, provided the domain/range
3263 * space of *entry matches the target space of data->mpa
3264 * (as given by data->match), and add the result to data->res.
3266 static isl_stat preimage_mpa_entry(void **entry, void *user)
3268 int m;
3269 isl_map *map = *entry;
3270 struct isl_union_map_preimage_mpa_data *data = user;
3271 isl_bool empty;
3273 m = data->match(map, data->space);
3274 if (m < 0)
3275 return isl_stat_error;
3276 if (!m)
3277 return isl_stat_ok;
3279 map = isl_map_copy(map);
3280 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3282 empty = isl_map_is_empty(map);
3283 if (empty < 0 || empty) {
3284 isl_map_free(map);
3285 return empty < 0 ? isl_stat_error : isl_stat_ok;
3288 data->res = isl_union_map_add_map(data->res, map);
3290 return isl_stat_ok;
3293 /* Compute the preimage of the domain or range of "umap" under the function
3294 * represented by "mpa".
3295 * In other words, plug in "mpa" in the domain or range of "umap".
3296 * The function "fn" performs the actual preimage computation on a map,
3297 * while "match" determines to which maps the function should be applied.
3299 static __isl_give isl_union_map *preimage_multi_pw_aff(
3300 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3301 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3302 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3303 __isl_take isl_multi_pw_aff *mpa))
3305 isl_ctx *ctx;
3306 isl_space *space;
3307 struct isl_union_map_preimage_mpa_data data;
3309 umap = isl_union_map_align_params(umap,
3310 isl_multi_pw_aff_get_space(mpa));
3311 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3313 if (!umap || !mpa)
3314 goto error;
3316 ctx = isl_union_map_get_ctx(umap);
3317 space = isl_union_map_get_space(umap);
3318 data.space = isl_multi_pw_aff_get_space(mpa);
3319 data.mpa = mpa;
3320 data.res = isl_union_map_alloc(space, umap->table.n);
3321 data.match = match;
3322 data.fn = fn;
3323 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3324 &data) < 0)
3325 data.res = isl_union_map_free(data.res);
3327 isl_space_free(data.space);
3328 isl_union_map_free(umap);
3329 isl_multi_pw_aff_free(mpa);
3330 return data.res;
3331 error:
3332 isl_union_map_free(umap);
3333 isl_multi_pw_aff_free(mpa);
3334 return NULL;
3337 /* Compute the preimage of the domain of "umap" under the function
3338 * represented by "mpa".
3339 * In other words, plug in "mpa" in the domain of "umap".
3340 * The result contains maps that live in the same spaces as the maps of "umap"
3341 * with domain space equal to the target space of "mpa",
3342 * except that the domain has been replaced by the domain space of "mpa".
3344 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3345 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3347 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3348 &isl_map_preimage_domain_multi_pw_aff);
3351 /* Internal data structure for preimage_upma.
3353 * "umap" is the map of which the preimage should be computed.
3354 * "res" collects the results.
3355 * "fn" computes the preimage for a given piecewise multi-affine function.
3357 struct isl_union_map_preimage_upma_data {
3358 isl_union_map *umap;
3359 isl_union_map *res;
3360 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3361 __isl_take isl_pw_multi_aff *pma);
3364 /* Call data->fn to compute the preimage of the domain or range of data->umap
3365 * under the function represented by pma and add the result to data->res.
3367 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3369 struct isl_union_map_preimage_upma_data *data = user;
3370 isl_union_map *umap;
3372 umap = isl_union_map_copy(data->umap);
3373 umap = data->fn(umap, pma);
3374 data->res = isl_union_map_union(data->res, umap);
3376 return data->res ? isl_stat_ok : isl_stat_error;
3379 /* Compute the preimage of the domain or range of "umap" under the function
3380 * represented by "upma".
3381 * In other words, plug in "upma" in the domain or range of "umap".
3382 * The function "fn" performs the actual preimage computation
3383 * on a piecewise multi-affine function.
3385 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3386 __isl_take isl_union_map *umap,
3387 __isl_take isl_union_pw_multi_aff *upma,
3388 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3389 __isl_take isl_pw_multi_aff *pma))
3391 struct isl_union_map_preimage_upma_data data;
3393 data.umap = umap;
3394 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3395 data.fn = fn;
3396 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3397 &preimage_upma, &data) < 0)
3398 data.res = isl_union_map_free(data.res);
3400 isl_union_map_free(umap);
3401 isl_union_pw_multi_aff_free(upma);
3403 return data.res;
3406 /* Compute the preimage of the domain of "umap" under the function
3407 * represented by "upma".
3408 * In other words, plug in "upma" in the domain of "umap".
3409 * The result contains maps that live in the same spaces as the maps of "umap"
3410 * with domain space equal to one of the target spaces of "upma",
3411 * except that the domain has been replaced by one of the the domain spaces that
3412 * corresponds to that target space of "upma".
3414 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3415 __isl_take isl_union_map *umap,
3416 __isl_take isl_union_pw_multi_aff *upma)
3418 return preimage_union_pw_multi_aff(umap, upma,
3419 &isl_union_map_preimage_domain_pw_multi_aff);
3422 /* Compute the preimage of the range of "umap" under the function
3423 * represented by "upma".
3424 * In other words, plug in "upma" in the range of "umap".
3425 * The result contains maps that live in the same spaces as the maps of "umap"
3426 * with range space equal to one of the target spaces of "upma",
3427 * except that the range has been replaced by one of the the domain spaces that
3428 * corresponds to that target space of "upma".
3430 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3431 __isl_take isl_union_map *umap,
3432 __isl_take isl_union_pw_multi_aff *upma)
3434 return preimage_union_pw_multi_aff(umap, upma,
3435 &isl_union_map_preimage_range_pw_multi_aff);
3438 /* Compute the preimage of "uset" under the function represented by "upma".
3439 * In other words, plug in "upma" in the range of "uset".
3440 * The result contains sets that live in the same spaces as the sets of "uset"
3441 * with space equal to one of the target spaces of "upma",
3442 * except that the space has been replaced by one of the the domain spaces that
3443 * corresponds to that target space of "upma".
3445 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3446 __isl_take isl_union_set *uset,
3447 __isl_take isl_union_pw_multi_aff *upma)
3449 return preimage_union_pw_multi_aff(uset, upma,
3450 &isl_union_set_preimage_pw_multi_aff);
3453 /* Reset the user pointer on all identifiers of parameters and tuples
3454 * of the space of *entry.
3456 static isl_stat reset_user(void **entry, void *user)
3458 isl_map **map = (isl_map **)entry;
3460 *map = isl_map_reset_user(*map);
3462 return *map ? isl_stat_ok : isl_stat_error;
3465 /* Reset the user pointer on all identifiers of parameters and tuples
3466 * of the spaces of "umap".
3468 __isl_give isl_union_map *isl_union_map_reset_user(
3469 __isl_take isl_union_map *umap)
3471 umap = isl_union_map_cow(umap);
3472 if (!umap)
3473 return NULL;
3474 umap->dim = isl_space_reset_user(umap->dim);
3475 if (!umap->dim)
3476 return isl_union_map_free(umap);
3477 umap = un_op(umap, &reset_user);
3479 return umap;
3482 /* Reset the user pointer on all identifiers of parameters and tuples
3483 * of the spaces of "uset".
3485 __isl_give isl_union_set *isl_union_set_reset_user(
3486 __isl_take isl_union_set *uset)
3488 return isl_union_map_reset_user(uset);
3491 /* Internal data structure for isl_union_map_project_out.
3492 * "type", "first" and "n" are the arguments for the isl_map_project_out
3493 * call.
3494 * "res" collects the results.
3496 struct isl_union_map_project_out_data {
3497 enum isl_dim_type type;
3498 unsigned first;
3499 unsigned n;
3501 isl_union_map *res;
3504 /* Turn the data->n dimensions of type data->type, starting at data->first
3505 * into existentially quantified variables and add the result to data->res.
3507 static isl_stat project_out(__isl_take isl_map *map, void *user)
3509 struct isl_union_map_project_out_data *data = user;
3511 map = isl_map_project_out(map, data->type, data->first, data->n);
3512 data->res = isl_union_map_add_map(data->res, map);
3514 return isl_stat_ok;
3517 /* Turn the "n" dimensions of type "type", starting at "first"
3518 * into existentially quantified variables.
3519 * Since the space of an isl_union_map only contains parameters,
3520 * type is required to be equal to isl_dim_param.
3522 __isl_give isl_union_map *isl_union_map_project_out(
3523 __isl_take isl_union_map *umap,
3524 enum isl_dim_type type, unsigned first, unsigned n)
3526 isl_space *space;
3527 struct isl_union_map_project_out_data data = { type, first, n };
3529 if (!umap)
3530 return NULL;
3532 if (type != isl_dim_param)
3533 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3534 "can only project out parameters",
3535 return isl_union_map_free(umap));
3537 space = isl_union_map_get_space(umap);
3538 space = isl_space_drop_dims(space, type, first, n);
3539 data.res = isl_union_map_empty(space);
3540 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3541 data.res = isl_union_map_free(data.res);
3543 isl_union_map_free(umap);
3545 return data.res;
3548 /* Turn the "n" dimensions of type "type", starting at "first"
3549 * into existentially quantified variables.
3550 * Since the space of an isl_union_set only contains parameters,
3551 * "type" is required to be equal to isl_dim_param.
3553 __isl_give isl_union_set *isl_union_set_project_out(
3554 __isl_take isl_union_set *uset,
3555 enum isl_dim_type type, unsigned first, unsigned n)
3557 return isl_union_map_project_out(uset, type, first, n);
3560 /* Internal data structure for isl_union_map_involves_dims.
3561 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3563 struct isl_union_map_involves_dims_data {
3564 unsigned first;
3565 unsigned n;
3568 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3570 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3572 struct isl_union_map_involves_dims_data *data = user;
3573 isl_bool involves;
3575 involves = isl_map_involves_dims(map,
3576 isl_dim_param, data->first, data->n);
3577 if (involves < 0)
3578 return isl_bool_error;
3579 return !involves;
3582 /* Does "umap" involve any of the n parameters starting at first?
3583 * "type" is required to be set to isl_dim_param.
3585 * "umap" involves any of those parameters if any of its maps
3586 * involve the parameters. In other words, "umap" does not
3587 * involve any of the parameters if all its maps to not
3588 * involve the parameters.
3590 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3591 enum isl_dim_type type, unsigned first, unsigned n)
3593 struct isl_union_map_involves_dims_data data = { first, n };
3594 isl_bool excludes;
3596 if (type != isl_dim_param)
3597 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3598 "can only reference parameters", return isl_bool_error);
3600 excludes = union_map_forall_user(umap, &map_excludes, &data);
3602 if (excludes < 0)
3603 return isl_bool_error;
3605 return !excludes;
3608 /* Internal data structure for isl_union_map_reset_range_space.
3609 * "range" is the space from which to set the range space.
3610 * "res" collects the results.
3612 struct isl_union_map_reset_range_space_data {
3613 isl_space *range;
3614 isl_union_map *res;
3617 /* Replace the range space of "map" by the range space of data->range and
3618 * add the result to data->res.
3620 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3622 struct isl_union_map_reset_range_space_data *data = user;
3623 isl_space *space;
3625 space = isl_map_get_space(map);
3626 space = isl_space_domain(space);
3627 space = isl_space_extend_domain_with_range(space,
3628 isl_space_copy(data->range));
3629 map = isl_map_reset_space(map, space);
3630 data->res = isl_union_map_add_map(data->res, map);
3632 return data->res ? isl_stat_ok : isl_stat_error;
3635 /* Replace the range space of all the maps in "umap" by
3636 * the range space of "space".
3638 * This assumes that all maps have the same output dimension.
3639 * This function should therefore not be made publicly available.
3641 * Since the spaces of the maps change, so do their hash value.
3642 * We therefore need to create a new isl_union_map.
3644 __isl_give isl_union_map *isl_union_map_reset_range_space(
3645 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3647 struct isl_union_map_reset_range_space_data data = { space };
3649 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3650 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3651 data.res = isl_union_map_free(data.res);
3653 isl_space_free(space);
3654 isl_union_map_free(umap);
3655 return data.res;
3658 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3659 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3660 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3661 * to the domain and the range of each map.
3662 * "res" collects the results.
3664 struct isl_union_order_at_data {
3665 isl_multi_union_pw_aff *mupa;
3666 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3667 __isl_take isl_multi_pw_aff *mpa2);
3668 isl_union_map *res;
3671 /* Intersect "map" with the result of applying data->order to
3672 * the functions in data->mupa that apply to the domain and the range
3673 * of "map" and add the result to data->res.
3675 static isl_stat order_at(__isl_take isl_map *map, void *user)
3677 struct isl_union_order_at_data *data = user;
3678 isl_space *space;
3679 isl_multi_pw_aff *mpa1, *mpa2;
3680 isl_map *order;
3682 space = isl_space_domain(isl_map_get_space(map));
3683 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3684 space = isl_space_range(isl_map_get_space(map));
3685 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3686 order = data->order(mpa1, mpa2);
3687 map = isl_map_intersect(map, order);
3688 data->res = isl_union_map_add_map(data->res, map);
3690 return data->res ? isl_stat_ok : isl_stat_error;
3693 /* Intersect each map in "umap" with the result of calling "order"
3694 * on the functions is "mupa" that apply to the domain and the range
3695 * of the map.
3697 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3698 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3699 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3700 __isl_take isl_multi_pw_aff *mpa2))
3702 struct isl_union_order_at_data data;
3704 umap = isl_union_map_align_params(umap,
3705 isl_multi_union_pw_aff_get_space(mupa));
3706 mupa = isl_multi_union_pw_aff_align_params(mupa,
3707 isl_union_map_get_space(umap));
3708 data.mupa = mupa;
3709 data.order = order;
3710 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3711 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3712 data.res = isl_union_map_free(data.res);
3714 isl_multi_union_pw_aff_free(mupa);
3715 isl_union_map_free(umap);
3716 return data.res;
3719 /* Return the subset of "umap" where the domain and the range
3720 * have equal "mupa" values.
3722 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3723 __isl_take isl_union_map *umap,
3724 __isl_take isl_multi_union_pw_aff *mupa)
3726 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3727 &isl_multi_pw_aff_eq_map);
3730 /* Return the subset of "umap" where the domain has a lexicographically
3731 * smaller "mupa" value than the range.
3733 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3734 __isl_take isl_union_map *umap,
3735 __isl_take isl_multi_union_pw_aff *mupa)
3737 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3738 &isl_multi_pw_aff_lex_lt_map);
3741 /* Return the subset of "umap" where the domain has a lexicographically
3742 * greater "mupa" value than the range.
3744 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3745 __isl_take isl_union_map *umap,
3746 __isl_take isl_multi_union_pw_aff *mupa)
3748 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3749 &isl_multi_pw_aff_lex_gt_map);
3752 /* Return the union of the elements in the list "list".
3754 __isl_give isl_union_set *isl_union_set_list_union(
3755 __isl_take isl_union_set_list *list)
3757 int i, n;
3758 isl_ctx *ctx;
3759 isl_space *space;
3760 isl_union_set *res;
3762 if (!list)
3763 return NULL;
3765 ctx = isl_union_set_list_get_ctx(list);
3766 space = isl_space_params_alloc(ctx, 0);
3767 res = isl_union_set_empty(space);
3769 n = isl_union_set_list_n_union_set(list);
3770 for (i = 0; i < n; ++i) {
3771 isl_union_set *uset_i;
3773 uset_i = isl_union_set_list_get_union_set(list, i);
3774 res = isl_union_set_union(res, uset_i);
3777 isl_union_set_list_free(list);
3778 return res;
3781 /* Update *hash with the hash value of "map".
3783 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3785 uint32_t *hash = user;
3786 uint32_t map_hash;
3788 map_hash = isl_map_get_hash(map);
3789 isl_hash_hash(*hash, map_hash);
3791 isl_map_free(map);
3792 return isl_stat_ok;
3795 /* Return a hash value that digests "umap".
3797 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3799 uint32_t hash;
3801 if (!umap)
3802 return 0;
3804 hash = isl_hash_init();
3805 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3806 return 0;
3808 return hash;
3811 /* Return a hash value that digests "uset".
3813 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3815 return isl_union_map_get_hash(uset);