isl_union_map.c: inplace: implement in terms of cond_un_op
[isl.git] / isl_union_map.c
blob1abb28ef89b02e9f085c37c86a285d2389e7c3f0
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 "inplace" is set, then the maps in the input union map
1527 * are modified in place. This means that "fn_map" should not
1528 * change the meaning of the map.
1529 * Otherwise, a new union map is constructed to store the results.
1530 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1531 * are taken into account. No filter can be set if "inplace" is set.
1532 * "fn_map" specifies how the maps (selected by "filter")
1533 * should be transformed.
1535 struct isl_un_op_control {
1536 int inplace;
1537 isl_bool (*filter)(__isl_keep isl_map *map);
1538 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1541 /* Internal data structure for "cond_un_op".
1542 * "control" specifies how the maps in the union map should be modified.
1543 * "res" collects the results.
1545 struct isl_union_map_un_data {
1546 struct isl_un_op_control *control;
1547 isl_union_map *res;
1550 /* isl_hash_table_foreach callback for cond_un_op.
1551 * Handle the map that "entry" points to.
1553 * If control->filter is set, then check if this map satisfies the filter.
1554 * If so (or if control->filter is not set), modify the map
1555 * by calling control->fn_map and either add the result to data->res or
1556 * replace the original entry by the result (if control->inplace is set).
1558 static isl_stat un_entry(void **entry, void *user)
1560 struct isl_union_map_un_data *data = user;
1561 isl_map *map = *entry;
1563 if (data->control->filter) {
1564 isl_bool ok;
1566 ok = data->control->filter(map);
1567 if (ok < 0)
1568 return isl_stat_error;
1569 if (!ok)
1570 return isl_stat_ok;
1573 map = data->control->fn_map(isl_map_copy(map));
1574 if (!map)
1575 return isl_stat_error;
1576 if (data->control->inplace) {
1577 isl_map_free(*entry);
1578 *entry = map;
1579 } else {
1580 data->res = isl_union_map_add_map(data->res, map);
1581 if (!data->res)
1582 return isl_stat_error;
1585 return isl_stat_ok;
1588 /* Modify the maps in "umap" based on "control".
1589 * If control->inplace is set, then modify the maps in "umap" in-place.
1590 * Otherwise, create a new union map to hold the results.
1592 static __isl_give isl_union_map *cond_un_op(__isl_take isl_union_map *umap,
1593 struct isl_un_op_control *control)
1595 struct isl_union_map_un_data data = { control };
1597 if (!umap)
1598 return NULL;
1599 if (control->inplace && control->filter)
1600 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1601 "inplace modification cannot be filtered",
1602 return isl_union_map_free(umap));
1604 if (control->inplace) {
1605 data.res = umap;
1606 } else {
1607 isl_space *space;
1609 space = isl_union_map_get_space(umap);
1610 data.res = isl_union_map_alloc(space, umap->table.n);
1612 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1613 &umap->table, &un_entry, &data) < 0)
1614 data.res = isl_union_map_free(data.res);
1616 if (control->inplace)
1617 return data.res;
1618 isl_union_map_free(umap);
1619 return data.res;
1622 __isl_give isl_union_map *isl_union_map_from_range(
1623 __isl_take isl_union_set *uset)
1625 struct isl_un_op_control control = {
1626 .fn_map = &isl_map_from_range,
1628 return cond_un_op(uset, &control);
1631 __isl_give isl_union_map *isl_union_map_from_domain(
1632 __isl_take isl_union_set *uset)
1634 return isl_union_map_reverse(isl_union_map_from_range(uset));
1637 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1638 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1640 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1641 isl_union_map_from_range(range));
1644 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1645 isl_stat (*fn)(void **, void *))
1647 umap = isl_union_map_cow(umap);
1648 if (!umap)
1649 return NULL;
1651 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1652 &umap->table, fn, NULL) < 0)
1653 goto error;
1655 return umap;
1656 error:
1657 isl_union_map_free(umap);
1658 return NULL;
1661 static isl_stat affine_entry(void **entry, void *user)
1663 isl_map **map = (isl_map **)entry;
1665 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1667 return *map ? isl_stat_ok : isl_stat_error;
1670 __isl_give isl_union_map *isl_union_map_affine_hull(
1671 __isl_take isl_union_map *umap)
1673 return un_op(umap, &affine_entry);
1676 __isl_give isl_union_set *isl_union_set_affine_hull(
1677 __isl_take isl_union_set *uset)
1679 return isl_union_map_affine_hull(uset);
1682 static isl_stat polyhedral_entry(void **entry, void *user)
1684 isl_map **map = (isl_map **)entry;
1686 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1688 return *map ? isl_stat_ok : isl_stat_error;
1691 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1692 __isl_take isl_union_map *umap)
1694 return un_op(umap, &polyhedral_entry);
1697 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1698 __isl_take isl_union_set *uset)
1700 return isl_union_map_polyhedral_hull(uset);
1703 static isl_stat simple_entry(void **entry, void *user)
1705 isl_map **map = (isl_map **)entry;
1707 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1709 return *map ? isl_stat_ok : isl_stat_error;
1712 __isl_give isl_union_map *isl_union_map_simple_hull(
1713 __isl_take isl_union_map *umap)
1715 return un_op(umap, &simple_entry);
1718 __isl_give isl_union_set *isl_union_set_simple_hull(
1719 __isl_take isl_union_set *uset)
1721 return isl_union_map_simple_hull(uset);
1724 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1725 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1727 struct isl_un_op_control control = {
1728 .inplace = 1,
1729 .fn_map = fn,
1732 return cond_un_op(umap, &control);
1735 /* Remove redundant constraints in each of the basic maps of "umap".
1736 * Since removing redundant constraints does not change the meaning
1737 * or the space, the operation can be performed in-place.
1739 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1740 __isl_take isl_union_map *umap)
1742 return inplace(umap, &isl_map_remove_redundancies);
1745 /* Remove redundant constraints in each of the basic sets of "uset".
1747 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1748 __isl_take isl_union_set *uset)
1750 return isl_union_map_remove_redundancies(uset);
1753 __isl_give isl_union_map *isl_union_map_coalesce(
1754 __isl_take isl_union_map *umap)
1756 return inplace(umap, &isl_map_coalesce);
1759 __isl_give isl_union_set *isl_union_set_coalesce(
1760 __isl_take isl_union_set *uset)
1762 return isl_union_map_coalesce(uset);
1765 __isl_give isl_union_map *isl_union_map_detect_equalities(
1766 __isl_take isl_union_map *umap)
1768 return inplace(umap, &isl_map_detect_equalities);
1771 __isl_give isl_union_set *isl_union_set_detect_equalities(
1772 __isl_take isl_union_set *uset)
1774 return isl_union_map_detect_equalities(uset);
1777 __isl_give isl_union_map *isl_union_map_compute_divs(
1778 __isl_take isl_union_map *umap)
1780 return inplace(umap, &isl_map_compute_divs);
1783 __isl_give isl_union_set *isl_union_set_compute_divs(
1784 __isl_take isl_union_set *uset)
1786 return isl_union_map_compute_divs(uset);
1789 static isl_stat lexmin_entry(void **entry, void *user)
1791 isl_map **map = (isl_map **)entry;
1793 *map = isl_map_lexmin(*map);
1795 return *map ? isl_stat_ok : isl_stat_error;
1798 __isl_give isl_union_map *isl_union_map_lexmin(
1799 __isl_take isl_union_map *umap)
1801 return un_op(umap, &lexmin_entry);
1804 __isl_give isl_union_set *isl_union_set_lexmin(
1805 __isl_take isl_union_set *uset)
1807 return isl_union_map_lexmin(uset);
1810 static isl_stat lexmax_entry(void **entry, void *user)
1812 isl_map **map = (isl_map **)entry;
1814 *map = isl_map_lexmax(*map);
1816 return *map ? isl_stat_ok : isl_stat_error;
1819 __isl_give isl_union_map *isl_union_map_lexmax(
1820 __isl_take isl_union_map *umap)
1822 return un_op(umap, &lexmax_entry);
1825 __isl_give isl_union_set *isl_union_set_lexmax(
1826 __isl_take isl_union_set *uset)
1828 return isl_union_map_lexmax(uset);
1831 /* Return the universe in the space of "map".
1833 static __isl_give isl_map *universe(__isl_take isl_map *map)
1835 isl_space *space;
1837 space = isl_map_get_space(map);
1838 isl_map_free(map);
1839 return isl_map_universe(space);
1842 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1844 struct isl_un_op_control control = {
1845 .fn_map = &universe,
1847 return cond_un_op(umap, &control);
1850 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1852 return isl_union_map_universe(uset);
1855 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1857 struct isl_un_op_control control = {
1858 .fn_map = &isl_map_reverse,
1860 return cond_un_op(umap, &control);
1863 /* Compute the parameter domain of the given union map.
1865 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1867 struct isl_un_op_control control = {
1868 .fn_map = &isl_map_params,
1870 int empty;
1872 empty = isl_union_map_is_empty(umap);
1873 if (empty < 0)
1874 goto error;
1875 if (empty) {
1876 isl_space *space;
1877 space = isl_union_map_get_space(umap);
1878 isl_union_map_free(umap);
1879 return isl_set_empty(space);
1881 return isl_set_from_union_set(cond_un_op(umap, &control));
1882 error:
1883 isl_union_map_free(umap);
1884 return NULL;
1887 /* Compute the parameter domain of the given union set.
1889 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1891 return isl_union_map_params(uset);
1894 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1896 struct isl_un_op_control control = {
1897 .fn_map = &isl_map_domain,
1899 return cond_un_op(umap, &control);
1902 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1904 struct isl_un_op_control control = {
1905 .fn_map = &isl_map_range,
1907 return cond_un_op(umap, &control);
1910 __isl_give isl_union_map *isl_union_map_domain_map(
1911 __isl_take isl_union_map *umap)
1913 struct isl_un_op_control control = {
1914 .fn_map = &isl_map_domain_map,
1916 return cond_un_op(umap, &control);
1919 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1920 * add the result to "res".
1922 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1924 isl_union_pw_multi_aff **res = user;
1925 isl_multi_aff *ma;
1926 isl_pw_multi_aff *pma;
1928 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1929 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1930 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1932 return *res ? isl_stat_ok : isl_stat_error;
1936 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1937 * to its domain.
1939 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1940 __isl_take isl_union_map *umap)
1942 isl_union_pw_multi_aff *res;
1944 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1945 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1946 res = isl_union_pw_multi_aff_free(res);
1948 isl_union_map_free(umap);
1949 return res;
1952 __isl_give isl_union_map *isl_union_map_range_map(
1953 __isl_take isl_union_map *umap)
1955 struct isl_un_op_control control = {
1956 .fn_map = &isl_map_range_map,
1958 return cond_un_op(umap, &control);
1961 /* Given a collection of wrapped maps of the form A[B -> C],
1962 * return the collection of maps A[B -> C] -> B.
1964 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1965 __isl_take isl_union_set *uset)
1967 struct isl_un_op_control control = {
1968 .filter = &isl_set_is_wrapping,
1969 .fn_map = &isl_set_wrapped_domain_map,
1971 return cond_un_op(uset, &control);
1974 /* Does "map" relate elements from the same space?
1976 static isl_bool equal_tuples(__isl_keep isl_map *map)
1978 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
1979 map->dim, isl_dim_out);
1982 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1984 struct isl_un_op_control control = {
1985 .filter = &equal_tuples,
1986 .fn_map = &isl_map_deltas,
1988 return cond_un_op(umap, &control);
1991 __isl_give isl_union_map *isl_union_map_deltas_map(
1992 __isl_take isl_union_map *umap)
1994 struct isl_un_op_control control = {
1995 .filter = &equal_tuples,
1996 .fn_map = &isl_map_deltas_map,
1998 return cond_un_op(umap, &control);
2001 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2003 struct isl_un_op_control control = {
2004 .fn_map = &isl_set_identity,
2006 return cond_un_op(uset, &control);
2009 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2011 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2013 isl_union_pw_multi_aff **res = user;
2014 isl_space *space;
2015 isl_pw_multi_aff *pma;
2017 space = isl_space_map_from_set(isl_set_get_space(set));
2018 pma = isl_pw_multi_aff_identity(space);
2019 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2020 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2022 return *res ? isl_stat_ok : isl_stat_error;
2025 /* Return an identity function on "uset" in the form
2026 * of an isl_union_pw_multi_aff.
2028 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2029 __isl_take isl_union_set *uset)
2031 isl_union_pw_multi_aff *res;
2033 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2034 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2035 res = isl_union_pw_multi_aff_free(res);
2037 isl_union_set_free(uset);
2038 return res;
2041 /* For each map in "umap" of the form [A -> B] -> C,
2042 * construct the map A -> C and collect the results.
2044 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2045 __isl_take isl_union_map *umap)
2047 struct isl_un_op_control control = {
2048 .filter = &isl_map_domain_is_wrapping,
2049 .fn_map = &isl_map_domain_factor_domain,
2051 return cond_un_op(umap, &control);
2054 /* For each map in "umap" of the form [A -> B] -> C,
2055 * construct the map B -> C and collect the results.
2057 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2058 __isl_take isl_union_map *umap)
2060 struct isl_un_op_control control = {
2061 .filter = &isl_map_domain_is_wrapping,
2062 .fn_map = &isl_map_domain_factor_range,
2064 return cond_un_op(umap, &control);
2067 /* For each map in "umap" of the form A -> [B -> C],
2068 * construct the map A -> B and collect the results.
2070 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2071 __isl_take isl_union_map *umap)
2073 struct isl_un_op_control control = {
2074 .filter = &isl_map_range_is_wrapping,
2075 .fn_map = &isl_map_range_factor_domain,
2077 return cond_un_op(umap, &control);
2080 /* For each map in "umap" of the form A -> [B -> C],
2081 * construct the map A -> C and collect the results.
2083 __isl_give isl_union_map *isl_union_map_range_factor_range(
2084 __isl_take isl_union_map *umap)
2086 struct isl_un_op_control control = {
2087 .filter = &isl_map_range_is_wrapping,
2088 .fn_map = &isl_map_range_factor_range,
2090 return cond_un_op(umap, &control);
2093 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2094 * construct the map A -> C and collect the results.
2096 __isl_give isl_union_map *isl_union_map_factor_domain(
2097 __isl_take isl_union_map *umap)
2099 struct isl_un_op_control control = {
2100 .filter = &isl_map_is_product,
2101 .fn_map = &isl_map_factor_domain,
2103 return cond_un_op(umap, &control);
2106 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2107 * construct the map B -> D and collect the results.
2109 __isl_give isl_union_map *isl_union_map_factor_range(
2110 __isl_take isl_union_map *umap)
2112 struct isl_un_op_control control = {
2113 .filter = &isl_map_is_product,
2114 .fn_map = &isl_map_factor_range,
2116 return cond_un_op(umap, &control);
2119 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2121 struct isl_un_op_control control = {
2122 .filter = &isl_set_is_wrapping,
2123 .fn_map = &isl_set_unwrap,
2125 return cond_un_op(uset, &control);
2128 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2130 struct isl_un_op_control control = {
2131 .fn_map = &isl_map_wrap,
2133 return cond_un_op(umap, &control);
2136 struct isl_union_map_is_subset_data {
2137 isl_union_map *umap2;
2138 isl_bool is_subset;
2141 static isl_stat is_subset_entry(void **entry, void *user)
2143 struct isl_union_map_is_subset_data *data = user;
2144 uint32_t hash;
2145 struct isl_hash_table_entry *entry2;
2146 isl_map *map = *entry;
2148 hash = isl_space_get_hash(map->dim);
2149 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2150 hash, &has_dim, map->dim, 0);
2151 if (!entry2) {
2152 int empty = isl_map_is_empty(map);
2153 if (empty < 0)
2154 return isl_stat_error;
2155 if (empty)
2156 return isl_stat_ok;
2157 data->is_subset = 0;
2158 return isl_stat_error;
2161 data->is_subset = isl_map_is_subset(map, entry2->data);
2162 if (data->is_subset < 0 || !data->is_subset)
2163 return isl_stat_error;
2165 return isl_stat_ok;
2168 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2169 __isl_keep isl_union_map *umap2)
2171 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2173 umap1 = isl_union_map_copy(umap1);
2174 umap2 = isl_union_map_copy(umap2);
2175 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2176 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2178 if (!umap1 || !umap2)
2179 goto error;
2181 data.umap2 = umap2;
2182 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2183 &is_subset_entry, &data) < 0 &&
2184 data.is_subset)
2185 goto error;
2187 isl_union_map_free(umap1);
2188 isl_union_map_free(umap2);
2190 return data.is_subset;
2191 error:
2192 isl_union_map_free(umap1);
2193 isl_union_map_free(umap2);
2194 return isl_bool_error;
2197 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2198 __isl_keep isl_union_set *uset2)
2200 return isl_union_map_is_subset(uset1, uset2);
2203 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2204 __isl_keep isl_union_map *umap2)
2206 isl_bool is_subset;
2208 if (!umap1 || !umap2)
2209 return isl_bool_error;
2210 is_subset = isl_union_map_is_subset(umap1, umap2);
2211 if (is_subset != isl_bool_true)
2212 return is_subset;
2213 is_subset = isl_union_map_is_subset(umap2, umap1);
2214 return is_subset;
2217 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2218 __isl_keep isl_union_set *uset2)
2220 return isl_union_map_is_equal(uset1, uset2);
2223 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2224 __isl_keep isl_union_map *umap2)
2226 isl_bool is_subset;
2228 if (!umap1 || !umap2)
2229 return isl_bool_error;
2230 is_subset = isl_union_map_is_subset(umap1, umap2);
2231 if (is_subset != isl_bool_true)
2232 return is_subset;
2233 is_subset = isl_union_map_is_subset(umap2, umap1);
2234 if (is_subset == isl_bool_error)
2235 return is_subset;
2236 return !is_subset;
2239 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2240 __isl_keep isl_union_set *uset2)
2242 return isl_union_map_is_strict_subset(uset1, uset2);
2245 /* Internal data structure for isl_union_map_is_disjoint.
2246 * umap2 is the union map with which we are comparing.
2247 * is_disjoint is initialized to 1 and is set to 0 as soon
2248 * as the union maps turn out not to be disjoint.
2250 struct isl_union_map_is_disjoint_data {
2251 isl_union_map *umap2;
2252 isl_bool is_disjoint;
2255 /* Check if "map" is disjoint from data->umap2 and abort
2256 * the search if it is not.
2258 static isl_stat is_disjoint_entry(void **entry, void *user)
2260 struct isl_union_map_is_disjoint_data *data = user;
2261 uint32_t hash;
2262 struct isl_hash_table_entry *entry2;
2263 isl_map *map = *entry;
2265 hash = isl_space_get_hash(map->dim);
2266 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2267 hash, &has_dim, map->dim, 0);
2268 if (!entry2)
2269 return isl_stat_ok;
2271 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2272 if (data->is_disjoint < 0 || !data->is_disjoint)
2273 return isl_stat_error;
2275 return isl_stat_ok;
2278 /* Are "umap1" and "umap2" disjoint?
2280 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2281 __isl_keep isl_union_map *umap2)
2283 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2285 umap1 = isl_union_map_copy(umap1);
2286 umap2 = isl_union_map_copy(umap2);
2287 umap1 = isl_union_map_align_params(umap1,
2288 isl_union_map_get_space(umap2));
2289 umap2 = isl_union_map_align_params(umap2,
2290 isl_union_map_get_space(umap1));
2292 if (!umap1 || !umap2)
2293 goto error;
2295 data.umap2 = umap2;
2296 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2297 &is_disjoint_entry, &data) < 0 &&
2298 data.is_disjoint)
2299 goto error;
2301 isl_union_map_free(umap1);
2302 isl_union_map_free(umap2);
2304 return data.is_disjoint;
2305 error:
2306 isl_union_map_free(umap1);
2307 isl_union_map_free(umap2);
2308 return isl_bool_error;
2311 /* Are "uset1" and "uset2" disjoint?
2313 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2314 __isl_keep isl_union_set *uset2)
2316 return isl_union_map_is_disjoint(uset1, uset2);
2319 static isl_stat sample_entry(void **entry, void *user)
2321 isl_basic_map **sample = (isl_basic_map **)user;
2322 isl_map *map = *entry;
2324 *sample = isl_map_sample(isl_map_copy(map));
2325 if (!*sample)
2326 return isl_stat_error;
2327 if (!isl_basic_map_plain_is_empty(*sample))
2328 return isl_stat_error;
2329 return isl_stat_ok;
2332 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2334 isl_basic_map *sample = NULL;
2336 if (!umap)
2337 return NULL;
2339 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2340 &sample_entry, &sample) < 0 &&
2341 !sample)
2342 goto error;
2344 if (!sample)
2345 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2347 isl_union_map_free(umap);
2349 return sample;
2350 error:
2351 isl_union_map_free(umap);
2352 return NULL;
2355 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2357 return bset_from_bmap(isl_union_map_sample(uset));
2360 /* Return an element in "uset" in the form of an isl_point.
2361 * Return a void isl_point if "uset" is empty.
2363 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2365 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2368 struct isl_forall_data {
2369 isl_bool res;
2370 isl_bool (*fn)(__isl_keep isl_map *map);
2373 static isl_stat forall_entry(void **entry, void *user)
2375 struct isl_forall_data *data = user;
2376 isl_map *map = *entry;
2378 data->res = data->fn(map);
2379 if (data->res < 0)
2380 return isl_stat_error;
2382 if (!data->res)
2383 return isl_stat_error;
2385 return isl_stat_ok;
2388 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2389 isl_bool (*fn)(__isl_keep isl_map *map))
2391 struct isl_forall_data data = { isl_bool_true, fn };
2393 if (!umap)
2394 return isl_bool_error;
2396 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2397 &forall_entry, &data) < 0 && data.res)
2398 return isl_bool_error;
2400 return data.res;
2403 struct isl_forall_user_data {
2404 isl_bool res;
2405 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2406 void *user;
2409 static isl_stat forall_user_entry(void **entry, void *user)
2411 struct isl_forall_user_data *data = user;
2412 isl_map *map = *entry;
2414 data->res = data->fn(map, data->user);
2415 if (data->res < 0)
2416 return isl_stat_error;
2418 if (!data->res)
2419 return isl_stat_error;
2421 return isl_stat_ok;
2424 /* Check if fn(map, user) returns true for all maps "map" in umap.
2426 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2427 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2429 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2431 if (!umap)
2432 return isl_bool_error;
2434 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2435 &forall_user_entry, &data) < 0 && data.res)
2436 return isl_bool_error;
2438 return data.res;
2441 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2443 return union_map_forall(umap, &isl_map_is_empty);
2446 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2448 return isl_union_map_is_empty(uset);
2451 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2453 isl_bool is_subset;
2454 isl_space *dim;
2455 isl_map *id;
2457 if (!map)
2458 return isl_bool_error;
2460 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2461 map->dim, isl_dim_out))
2462 return isl_bool_false;
2464 dim = isl_map_get_space(map);
2465 id = isl_map_identity(dim);
2467 is_subset = isl_map_is_subset(map, id);
2469 isl_map_free(id);
2471 return is_subset;
2474 /* Given an isl_union_map that consists of a single map, check
2475 * if it is single-valued.
2477 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2479 isl_map *map;
2480 isl_bool sv;
2482 umap = isl_union_map_copy(umap);
2483 map = isl_map_from_union_map(umap);
2484 sv = isl_map_is_single_valued(map);
2485 isl_map_free(map);
2487 return sv;
2490 /* Internal data structure for single_valued_on_domain.
2492 * "umap" is the union map to be tested.
2493 * "sv" is set to 1 as long as "umap" may still be single-valued.
2495 struct isl_union_map_is_sv_data {
2496 isl_union_map *umap;
2497 isl_bool sv;
2500 /* Check if the data->umap is single-valued on "set".
2502 * If data->umap consists of a single map on "set", then test it
2503 * as an isl_map.
2505 * Otherwise, compute
2507 * M \circ M^-1
2509 * check if the result is a subset of the identity mapping and
2510 * store the result in data->sv.
2512 * Terminate as soon as data->umap has been determined not to
2513 * be single-valued.
2515 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2517 struct isl_union_map_is_sv_data *data = user;
2518 isl_union_map *umap, *test;
2520 umap = isl_union_map_copy(data->umap);
2521 umap = isl_union_map_intersect_domain(umap,
2522 isl_union_set_from_set(set));
2524 if (isl_union_map_n_map(umap) == 1) {
2525 data->sv = single_map_is_single_valued(umap);
2526 isl_union_map_free(umap);
2527 } else {
2528 test = isl_union_map_reverse(isl_union_map_copy(umap));
2529 test = isl_union_map_apply_range(test, umap);
2531 data->sv = union_map_forall(test, &is_subset_of_identity);
2533 isl_union_map_free(test);
2536 if (data->sv < 0 || !data->sv)
2537 return isl_stat_error;
2538 return isl_stat_ok;
2541 /* Check if the given map is single-valued.
2543 * If the union map consists of a single map, then test it as an isl_map.
2544 * Otherwise, check if the union map is single-valued on each of its
2545 * domain spaces.
2547 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2549 isl_union_map *universe;
2550 isl_union_set *domain;
2551 struct isl_union_map_is_sv_data data;
2553 if (isl_union_map_n_map(umap) == 1)
2554 return single_map_is_single_valued(umap);
2556 universe = isl_union_map_universe(isl_union_map_copy(umap));
2557 domain = isl_union_map_domain(universe);
2559 data.sv = isl_bool_true;
2560 data.umap = umap;
2561 if (isl_union_set_foreach_set(domain,
2562 &single_valued_on_domain, &data) < 0 && data.sv)
2563 data.sv = isl_bool_error;
2564 isl_union_set_free(domain);
2566 return data.sv;
2569 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2571 isl_bool in;
2573 umap = isl_union_map_copy(umap);
2574 umap = isl_union_map_reverse(umap);
2575 in = isl_union_map_is_single_valued(umap);
2576 isl_union_map_free(umap);
2578 return in;
2581 /* Is "map" obviously not an identity relation because
2582 * it maps elements from one space to another space?
2583 * Update *non_identity accordingly.
2585 * In particular, if the domain and range spaces are the same,
2586 * then the map is not considered to obviously not be an identity relation.
2587 * Otherwise, the map is considered to obviously not be an identity relation
2588 * if it is is non-empty.
2590 * If "map" is determined to obviously not be an identity relation,
2591 * then the search is aborted.
2593 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2595 isl_bool *non_identity = user;
2596 isl_bool equal;
2597 isl_space *space;
2599 space = isl_map_get_space(map);
2600 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2601 if (equal >= 0 && !equal)
2602 *non_identity = isl_bool_not(isl_map_is_empty(map));
2603 else
2604 *non_identity = isl_bool_not(equal);
2605 isl_space_free(space);
2606 isl_map_free(map);
2608 if (*non_identity < 0 || *non_identity)
2609 return isl_stat_error;
2611 return isl_stat_ok;
2614 /* Is "umap" obviously not an identity relation because
2615 * it maps elements from one space to another space?
2617 * As soon as a map has been found that maps elements to a different space,
2618 * non_identity is changed and the search is aborted.
2620 static isl_bool isl_union_map_plain_is_not_identity(
2621 __isl_keep isl_union_map *umap)
2623 isl_bool non_identity;
2625 non_identity = isl_bool_false;
2626 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2627 &non_identity) < 0 &&
2628 non_identity == isl_bool_false)
2629 return isl_bool_error;
2631 return non_identity;
2634 /* Does "map" only map elements to themselves?
2635 * Update *identity accordingly.
2637 * If "map" is determined not to be an identity relation,
2638 * then the search is aborted.
2640 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2642 isl_bool *identity = user;
2644 *identity = isl_map_is_identity(map);
2645 isl_map_free(map);
2647 if (*identity < 0 || !*identity)
2648 return isl_stat_error;
2650 return isl_stat_ok;
2653 /* Does "umap" only map elements to themselves?
2655 * First check if there are any maps that map elements to different spaces.
2656 * If not, then check that all the maps (between identical spaces)
2657 * are identity relations.
2659 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2661 isl_bool non_identity;
2662 isl_bool identity;
2664 non_identity = isl_union_map_plain_is_not_identity(umap);
2665 if (non_identity < 0 || non_identity)
2666 return isl_bool_not(non_identity);
2668 identity = isl_bool_true;
2669 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2670 identity == isl_bool_true)
2671 return isl_bool_error;
2673 return identity;
2676 /* Represents a map that has a fixed value (v) for one of its
2677 * range dimensions.
2678 * The map in this structure is not reference counted, so it
2679 * is only valid while the isl_union_map from which it was
2680 * obtained is still alive.
2682 struct isl_fixed_map {
2683 isl_int v;
2684 isl_map *map;
2687 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2688 int n)
2690 int i;
2691 struct isl_fixed_map *v;
2693 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2694 if (!v)
2695 return NULL;
2696 for (i = 0; i < n; ++i)
2697 isl_int_init(v[i].v);
2698 return v;
2701 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2703 int i;
2705 if (!v)
2706 return;
2707 for (i = 0; i < n; ++i)
2708 isl_int_clear(v[i].v);
2709 free(v);
2712 /* Compare the "v" field of two isl_fixed_map structs.
2714 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2716 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2717 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2719 return isl_int_cmp(e1->v, e2->v);
2722 /* Internal data structure used while checking whether all maps
2723 * in a union_map have a fixed value for a given output dimension.
2724 * v is the list of maps, with the fixed value for the dimension
2725 * n is the number of maps considered so far
2726 * pos is the output dimension under investigation
2728 struct isl_fixed_dim_data {
2729 struct isl_fixed_map *v;
2730 int n;
2731 int pos;
2734 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2736 struct isl_fixed_dim_data *data = user;
2738 data->v[data->n].map = map;
2739 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2740 &data->v[data->n++].v);
2743 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2744 int first, int n_range);
2746 /* Given a list of the maps, with their fixed values at output dimension "pos",
2747 * check whether the ranges of the maps form an obvious partition.
2749 * We first sort the maps according to their fixed values.
2750 * If all maps have a different value, then we know the ranges form
2751 * a partition.
2752 * Otherwise, we collect the maps with the same fixed value and
2753 * check whether each such collection is obviously injective
2754 * based on later dimensions.
2756 static int separates(struct isl_fixed_map *v, int n,
2757 __isl_take isl_space *dim, int pos, int n_range)
2759 int i;
2761 if (!v)
2762 goto error;
2764 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2766 for (i = 0; i + 1 < n; ++i) {
2767 int j, k;
2768 isl_union_map *part;
2769 int injective;
2771 for (j = i + 1; j < n; ++j)
2772 if (isl_int_ne(v[i].v, v[j].v))
2773 break;
2775 if (j == i + 1)
2776 continue;
2778 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2779 for (k = i; k < j; ++k)
2780 part = isl_union_map_add_map(part,
2781 isl_map_copy(v[k].map));
2783 injective = plain_injective_on_range(part, pos + 1, n_range);
2784 if (injective < 0)
2785 goto error;
2786 if (!injective)
2787 break;
2789 i = j - 1;
2792 isl_space_free(dim);
2793 free_isl_fixed_map_array(v, n);
2794 return i + 1 >= n;
2795 error:
2796 isl_space_free(dim);
2797 free_isl_fixed_map_array(v, n);
2798 return -1;
2801 /* Check whether the maps in umap have obviously distinct ranges.
2802 * In particular, check for an output dimension in the range
2803 * [first,n_range) for which all maps have a fixed value
2804 * and then check if these values, possibly along with fixed values
2805 * at later dimensions, entail distinct ranges.
2807 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2808 int first, int n_range)
2810 isl_ctx *ctx;
2811 int n;
2812 struct isl_fixed_dim_data data = { NULL };
2814 ctx = isl_union_map_get_ctx(umap);
2816 n = isl_union_map_n_map(umap);
2817 if (!umap)
2818 goto error;
2820 if (n <= 1) {
2821 isl_union_map_free(umap);
2822 return isl_bool_true;
2825 if (first >= n_range) {
2826 isl_union_map_free(umap);
2827 return isl_bool_false;
2830 data.v = alloc_isl_fixed_map_array(ctx, n);
2831 if (!data.v)
2832 goto error;
2834 for (data.pos = first; data.pos < n_range; ++data.pos) {
2835 isl_bool fixed;
2836 int injective;
2837 isl_space *dim;
2839 data.n = 0;
2840 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2841 if (fixed < 0)
2842 goto error;
2843 if (!fixed)
2844 continue;
2845 dim = isl_union_map_get_space(umap);
2846 injective = separates(data.v, n, dim, data.pos, n_range);
2847 isl_union_map_free(umap);
2848 return injective;
2851 free_isl_fixed_map_array(data.v, n);
2852 isl_union_map_free(umap);
2854 return isl_bool_false;
2855 error:
2856 free_isl_fixed_map_array(data.v, n);
2857 isl_union_map_free(umap);
2858 return isl_bool_error;
2861 /* Check whether the maps in umap that map to subsets of "ran"
2862 * have obviously distinct ranges.
2864 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2865 void *user)
2867 isl_union_map *umap = user;
2869 umap = isl_union_map_copy(umap);
2870 umap = isl_union_map_intersect_range(umap,
2871 isl_union_set_from_set(isl_set_copy(ran)));
2872 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2875 /* Check if the given union_map is obviously injective.
2877 * In particular, we first check if all individual maps are obviously
2878 * injective and then check if all the ranges of these maps are
2879 * obviously disjoint.
2881 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2883 isl_bool in;
2884 isl_union_map *univ;
2885 isl_union_set *ran;
2887 in = union_map_forall(umap, &isl_map_plain_is_injective);
2888 if (in < 0)
2889 return isl_bool_error;
2890 if (!in)
2891 return isl_bool_false;
2893 univ = isl_union_map_universe(isl_union_map_copy(umap));
2894 ran = isl_union_map_range(univ);
2896 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2898 isl_union_set_free(ran);
2900 return in;
2903 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2905 isl_bool sv;
2907 sv = isl_union_map_is_single_valued(umap);
2908 if (sv < 0 || !sv)
2909 return sv;
2911 return isl_union_map_is_injective(umap);
2914 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2916 struct isl_un_op_control control = {
2917 .filter = &isl_map_can_zip,
2918 .fn_map = &isl_map_zip,
2920 return cond_un_op(umap, &control);
2923 /* Given a union map, take the maps of the form A -> (B -> C) and
2924 * return the union of the corresponding maps (A -> B) -> C.
2926 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2928 struct isl_un_op_control control = {
2929 .filter = &isl_map_can_uncurry,
2930 .fn_map = &isl_map_uncurry,
2932 return cond_un_op(umap, &control);
2935 /* Given a union map, take the maps of the form (A -> B) -> C and
2936 * return the union of the corresponding maps A -> (B -> C).
2938 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2940 struct isl_un_op_control control = {
2941 .filter = &isl_map_can_curry,
2942 .fn_map = &isl_map_curry,
2944 return cond_un_op(umap, &control);
2947 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2948 * return the union of the corresponding maps A -> (B -> (C -> D)).
2950 __isl_give isl_union_map *isl_union_map_range_curry(
2951 __isl_take isl_union_map *umap)
2953 struct isl_un_op_control control = {
2954 .filter = &isl_map_can_range_curry,
2955 .fn_map = &isl_map_range_curry,
2957 return cond_un_op(umap, &control);
2960 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2962 struct isl_un_op_control control = {
2963 .fn_map = &isl_set_lift,
2965 return cond_un_op(uset, &control);
2968 static isl_stat coefficients_entry(void **entry, void *user)
2970 isl_set *set = *entry;
2971 isl_union_set **res = user;
2973 set = isl_set_copy(set);
2974 set = isl_set_from_basic_set(isl_set_coefficients(set));
2975 *res = isl_union_set_add_set(*res, set);
2977 return isl_stat_ok;
2980 __isl_give isl_union_set *isl_union_set_coefficients(
2981 __isl_take isl_union_set *uset)
2983 isl_ctx *ctx;
2984 isl_space *dim;
2985 isl_union_set *res;
2987 if (!uset)
2988 return NULL;
2990 ctx = isl_union_set_get_ctx(uset);
2991 dim = isl_space_set_alloc(ctx, 0, 0);
2992 res = isl_union_map_alloc(dim, uset->table.n);
2993 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2994 &coefficients_entry, &res) < 0)
2995 goto error;
2997 isl_union_set_free(uset);
2998 return res;
2999 error:
3000 isl_union_set_free(uset);
3001 isl_union_set_free(res);
3002 return NULL;
3005 static isl_stat solutions_entry(void **entry, void *user)
3007 isl_set *set = *entry;
3008 isl_union_set **res = user;
3010 set = isl_set_copy(set);
3011 set = isl_set_from_basic_set(isl_set_solutions(set));
3012 if (!*res)
3013 *res = isl_union_set_from_set(set);
3014 else
3015 *res = isl_union_set_add_set(*res, set);
3017 if (!*res)
3018 return isl_stat_error;
3020 return isl_stat_ok;
3023 __isl_give isl_union_set *isl_union_set_solutions(
3024 __isl_take isl_union_set *uset)
3026 isl_union_set *res = NULL;
3028 if (!uset)
3029 return NULL;
3031 if (uset->table.n == 0) {
3032 res = isl_union_set_empty(isl_union_set_get_space(uset));
3033 isl_union_set_free(uset);
3034 return res;
3037 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3038 &solutions_entry, &res) < 0)
3039 goto error;
3041 isl_union_set_free(uset);
3042 return res;
3043 error:
3044 isl_union_set_free(uset);
3045 isl_union_set_free(res);
3046 return NULL;
3049 /* Is the domain space of "map" equal to "space"?
3051 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3053 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3054 space, isl_dim_out);
3057 /* Is the range space of "map" equal to "space"?
3059 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3061 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3062 space, isl_dim_out);
3065 /* Is the set space of "map" equal to "space"?
3067 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3069 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3070 space, isl_dim_out);
3073 /* Internal data structure for preimage_pw_multi_aff.
3075 * "pma" is the function under which the preimage should be taken.
3076 * "space" is the space of "pma".
3077 * "res" collects the results.
3078 * "fn" computes the preimage for a given map.
3079 * "match" returns true if "fn" can be called.
3081 struct isl_union_map_preimage_data {
3082 isl_space *space;
3083 isl_pw_multi_aff *pma;
3084 isl_union_map *res;
3085 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3086 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3087 __isl_take isl_pw_multi_aff *pma);
3090 /* Call data->fn to compute the preimage of the domain or range of *entry
3091 * under the function represented by data->pma, provided the domain/range
3092 * space of *entry matches the target space of data->pma
3093 * (as given by data->match), and add the result to data->res.
3095 static isl_stat preimage_entry(void **entry, void *user)
3097 int m;
3098 isl_map *map = *entry;
3099 struct isl_union_map_preimage_data *data = user;
3100 isl_bool empty;
3102 m = data->match(map, data->space);
3103 if (m < 0)
3104 return isl_stat_error;
3105 if (!m)
3106 return isl_stat_ok;
3108 map = isl_map_copy(map);
3109 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3111 empty = isl_map_is_empty(map);
3112 if (empty < 0 || empty) {
3113 isl_map_free(map);
3114 return empty < 0 ? isl_stat_error : isl_stat_ok;
3117 data->res = isl_union_map_add_map(data->res, map);
3119 return isl_stat_ok;
3122 /* Compute the preimage of the domain or range of "umap" under the function
3123 * represented by "pma".
3124 * In other words, plug in "pma" in the domain or range of "umap".
3125 * The function "fn" performs the actual preimage computation on a map,
3126 * while "match" determines to which maps the function should be applied.
3128 static __isl_give isl_union_map *preimage_pw_multi_aff(
3129 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3130 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3131 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3132 __isl_take isl_pw_multi_aff *pma))
3134 isl_ctx *ctx;
3135 isl_space *space;
3136 struct isl_union_map_preimage_data data;
3138 umap = isl_union_map_align_params(umap,
3139 isl_pw_multi_aff_get_space(pma));
3140 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3142 if (!umap || !pma)
3143 goto error;
3145 ctx = isl_union_map_get_ctx(umap);
3146 space = isl_union_map_get_space(umap);
3147 data.space = isl_pw_multi_aff_get_space(pma);
3148 data.pma = pma;
3149 data.res = isl_union_map_alloc(space, umap->table.n);
3150 data.match = match;
3151 data.fn = fn;
3152 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3153 &data) < 0)
3154 data.res = isl_union_map_free(data.res);
3156 isl_space_free(data.space);
3157 isl_union_map_free(umap);
3158 isl_pw_multi_aff_free(pma);
3159 return data.res;
3160 error:
3161 isl_union_map_free(umap);
3162 isl_pw_multi_aff_free(pma);
3163 return NULL;
3166 /* Compute the preimage of the domain of "umap" under the function
3167 * represented by "pma".
3168 * In other words, plug in "pma" in the domain of "umap".
3169 * The result contains maps that live in the same spaces as the maps of "umap"
3170 * with domain space equal to the target space of "pma",
3171 * except that the domain has been replaced by the domain space of "pma".
3173 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3174 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3176 return preimage_pw_multi_aff(umap, pma, &domain_match,
3177 &isl_map_preimage_domain_pw_multi_aff);
3180 /* Compute the preimage of the range of "umap" under the function
3181 * represented by "pma".
3182 * In other words, plug in "pma" in the range of "umap".
3183 * The result contains maps that live in the same spaces as the maps of "umap"
3184 * with range space equal to the target space of "pma",
3185 * except that the range has been replaced by the domain space of "pma".
3187 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3188 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3190 return preimage_pw_multi_aff(umap, pma, &range_match,
3191 &isl_map_preimage_range_pw_multi_aff);
3194 /* Compute the preimage of "uset" under the function represented by "pma".
3195 * In other words, plug in "pma" in "uset".
3196 * The result contains sets that live in the same spaces as the sets of "uset"
3197 * with space equal to the target space of "pma",
3198 * except that the space has been replaced by the domain space of "pma".
3200 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3201 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3203 return preimage_pw_multi_aff(uset, pma, &set_match,
3204 &isl_set_preimage_pw_multi_aff);
3207 /* Compute the preimage of the domain of "umap" under the function
3208 * represented by "ma".
3209 * In other words, plug in "ma" in the domain of "umap".
3210 * The result contains maps that live in the same spaces as the maps of "umap"
3211 * with domain space equal to the target space of "ma",
3212 * except that the domain has been replaced by the domain space of "ma".
3214 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3215 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3217 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3218 isl_pw_multi_aff_from_multi_aff(ma));
3221 /* Compute the preimage of the range of "umap" under the function
3222 * represented by "ma".
3223 * In other words, plug in "ma" in the range of "umap".
3224 * The result contains maps that live in the same spaces as the maps of "umap"
3225 * with range space equal to the target space of "ma",
3226 * except that the range has been replaced by the domain space of "ma".
3228 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3229 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3231 return isl_union_map_preimage_range_pw_multi_aff(umap,
3232 isl_pw_multi_aff_from_multi_aff(ma));
3235 /* Compute the preimage of "uset" under the function represented by "ma".
3236 * In other words, plug in "ma" in "uset".
3237 * The result contains sets that live in the same spaces as the sets of "uset"
3238 * with space equal to the target space of "ma",
3239 * except that the space has been replaced by the domain space of "ma".
3241 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3242 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3244 return isl_union_set_preimage_pw_multi_aff(uset,
3245 isl_pw_multi_aff_from_multi_aff(ma));
3248 /* Internal data structure for preimage_multi_pw_aff.
3250 * "mpa" is the function under which the preimage should be taken.
3251 * "space" is the space of "mpa".
3252 * "res" collects the results.
3253 * "fn" computes the preimage for a given map.
3254 * "match" returns true if "fn" can be called.
3256 struct isl_union_map_preimage_mpa_data {
3257 isl_space *space;
3258 isl_multi_pw_aff *mpa;
3259 isl_union_map *res;
3260 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3261 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3262 __isl_take isl_multi_pw_aff *mpa);
3265 /* Call data->fn to compute the preimage of the domain or range of *entry
3266 * under the function represented by data->mpa, provided the domain/range
3267 * space of *entry matches the target space of data->mpa
3268 * (as given by data->match), and add the result to data->res.
3270 static isl_stat preimage_mpa_entry(void **entry, void *user)
3272 int m;
3273 isl_map *map = *entry;
3274 struct isl_union_map_preimage_mpa_data *data = user;
3275 isl_bool empty;
3277 m = data->match(map, data->space);
3278 if (m < 0)
3279 return isl_stat_error;
3280 if (!m)
3281 return isl_stat_ok;
3283 map = isl_map_copy(map);
3284 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3286 empty = isl_map_is_empty(map);
3287 if (empty < 0 || empty) {
3288 isl_map_free(map);
3289 return empty < 0 ? isl_stat_error : isl_stat_ok;
3292 data->res = isl_union_map_add_map(data->res, map);
3294 return isl_stat_ok;
3297 /* Compute the preimage of the domain or range of "umap" under the function
3298 * represented by "mpa".
3299 * In other words, plug in "mpa" in the domain or range of "umap".
3300 * The function "fn" performs the actual preimage computation on a map,
3301 * while "match" determines to which maps the function should be applied.
3303 static __isl_give isl_union_map *preimage_multi_pw_aff(
3304 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3305 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3306 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3307 __isl_take isl_multi_pw_aff *mpa))
3309 isl_ctx *ctx;
3310 isl_space *space;
3311 struct isl_union_map_preimage_mpa_data data;
3313 umap = isl_union_map_align_params(umap,
3314 isl_multi_pw_aff_get_space(mpa));
3315 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3317 if (!umap || !mpa)
3318 goto error;
3320 ctx = isl_union_map_get_ctx(umap);
3321 space = isl_union_map_get_space(umap);
3322 data.space = isl_multi_pw_aff_get_space(mpa);
3323 data.mpa = mpa;
3324 data.res = isl_union_map_alloc(space, umap->table.n);
3325 data.match = match;
3326 data.fn = fn;
3327 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3328 &data) < 0)
3329 data.res = isl_union_map_free(data.res);
3331 isl_space_free(data.space);
3332 isl_union_map_free(umap);
3333 isl_multi_pw_aff_free(mpa);
3334 return data.res;
3335 error:
3336 isl_union_map_free(umap);
3337 isl_multi_pw_aff_free(mpa);
3338 return NULL;
3341 /* Compute the preimage of the domain of "umap" under the function
3342 * represented by "mpa".
3343 * In other words, plug in "mpa" in the domain of "umap".
3344 * The result contains maps that live in the same spaces as the maps of "umap"
3345 * with domain space equal to the target space of "mpa",
3346 * except that the domain has been replaced by the domain space of "mpa".
3348 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3349 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3351 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3352 &isl_map_preimage_domain_multi_pw_aff);
3355 /* Internal data structure for preimage_upma.
3357 * "umap" is the map of which the preimage should be computed.
3358 * "res" collects the results.
3359 * "fn" computes the preimage for a given piecewise multi-affine function.
3361 struct isl_union_map_preimage_upma_data {
3362 isl_union_map *umap;
3363 isl_union_map *res;
3364 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3365 __isl_take isl_pw_multi_aff *pma);
3368 /* Call data->fn to compute the preimage of the domain or range of data->umap
3369 * under the function represented by pma and add the result to data->res.
3371 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3373 struct isl_union_map_preimage_upma_data *data = user;
3374 isl_union_map *umap;
3376 umap = isl_union_map_copy(data->umap);
3377 umap = data->fn(umap, pma);
3378 data->res = isl_union_map_union(data->res, umap);
3380 return data->res ? isl_stat_ok : isl_stat_error;
3383 /* Compute the preimage of the domain or range of "umap" under the function
3384 * represented by "upma".
3385 * In other words, plug in "upma" in the domain or range of "umap".
3386 * The function "fn" performs the actual preimage computation
3387 * on a piecewise multi-affine function.
3389 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3390 __isl_take isl_union_map *umap,
3391 __isl_take isl_union_pw_multi_aff *upma,
3392 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3393 __isl_take isl_pw_multi_aff *pma))
3395 struct isl_union_map_preimage_upma_data data;
3397 data.umap = umap;
3398 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3399 data.fn = fn;
3400 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3401 &preimage_upma, &data) < 0)
3402 data.res = isl_union_map_free(data.res);
3404 isl_union_map_free(umap);
3405 isl_union_pw_multi_aff_free(upma);
3407 return data.res;
3410 /* Compute the preimage of the domain of "umap" under the function
3411 * represented by "upma".
3412 * In other words, plug in "upma" in the domain of "umap".
3413 * The result contains maps that live in the same spaces as the maps of "umap"
3414 * with domain space equal to one of the target spaces of "upma",
3415 * except that the domain has been replaced by one of the the domain spaces that
3416 * corresponds to that target space of "upma".
3418 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3419 __isl_take isl_union_map *umap,
3420 __isl_take isl_union_pw_multi_aff *upma)
3422 return preimage_union_pw_multi_aff(umap, upma,
3423 &isl_union_map_preimage_domain_pw_multi_aff);
3426 /* Compute the preimage of the range of "umap" under the function
3427 * represented by "upma".
3428 * In other words, plug in "upma" in the range of "umap".
3429 * The result contains maps that live in the same spaces as the maps of "umap"
3430 * with range space equal to one of the target spaces of "upma",
3431 * except that the range has been replaced by one of the the domain spaces that
3432 * corresponds to that target space of "upma".
3434 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3435 __isl_take isl_union_map *umap,
3436 __isl_take isl_union_pw_multi_aff *upma)
3438 return preimage_union_pw_multi_aff(umap, upma,
3439 &isl_union_map_preimage_range_pw_multi_aff);
3442 /* Compute the preimage of "uset" under the function represented by "upma".
3443 * In other words, plug in "upma" in the range of "uset".
3444 * The result contains sets that live in the same spaces as the sets of "uset"
3445 * with space equal to one of the target spaces of "upma",
3446 * except that the space has been replaced by one of the the domain spaces that
3447 * corresponds to that target space of "upma".
3449 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3450 __isl_take isl_union_set *uset,
3451 __isl_take isl_union_pw_multi_aff *upma)
3453 return preimage_union_pw_multi_aff(uset, upma,
3454 &isl_union_set_preimage_pw_multi_aff);
3457 /* Reset the user pointer on all identifiers of parameters and tuples
3458 * of the space of *entry.
3460 static isl_stat reset_user(void **entry, void *user)
3462 isl_map **map = (isl_map **)entry;
3464 *map = isl_map_reset_user(*map);
3466 return *map ? isl_stat_ok : isl_stat_error;
3469 /* Reset the user pointer on all identifiers of parameters and tuples
3470 * of the spaces of "umap".
3472 __isl_give isl_union_map *isl_union_map_reset_user(
3473 __isl_take isl_union_map *umap)
3475 umap = isl_union_map_cow(umap);
3476 if (!umap)
3477 return NULL;
3478 umap->dim = isl_space_reset_user(umap->dim);
3479 if (!umap->dim)
3480 return isl_union_map_free(umap);
3481 umap = un_op(umap, &reset_user);
3483 return umap;
3486 /* Reset the user pointer on all identifiers of parameters and tuples
3487 * of the spaces of "uset".
3489 __isl_give isl_union_set *isl_union_set_reset_user(
3490 __isl_take isl_union_set *uset)
3492 return isl_union_map_reset_user(uset);
3495 /* Internal data structure for isl_union_map_project_out.
3496 * "type", "first" and "n" are the arguments for the isl_map_project_out
3497 * call.
3498 * "res" collects the results.
3500 struct isl_union_map_project_out_data {
3501 enum isl_dim_type type;
3502 unsigned first;
3503 unsigned n;
3505 isl_union_map *res;
3508 /* Turn the data->n dimensions of type data->type, starting at data->first
3509 * into existentially quantified variables and add the result to data->res.
3511 static isl_stat project_out(__isl_take isl_map *map, void *user)
3513 struct isl_union_map_project_out_data *data = user;
3515 map = isl_map_project_out(map, data->type, data->first, data->n);
3516 data->res = isl_union_map_add_map(data->res, map);
3518 return isl_stat_ok;
3521 /* Turn the "n" dimensions of type "type", starting at "first"
3522 * into existentially quantified variables.
3523 * Since the space of an isl_union_map only contains parameters,
3524 * type is required to be equal to isl_dim_param.
3526 __isl_give isl_union_map *isl_union_map_project_out(
3527 __isl_take isl_union_map *umap,
3528 enum isl_dim_type type, unsigned first, unsigned n)
3530 isl_space *space;
3531 struct isl_union_map_project_out_data data = { type, first, n };
3533 if (!umap)
3534 return NULL;
3536 if (type != isl_dim_param)
3537 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3538 "can only project out parameters",
3539 return isl_union_map_free(umap));
3541 space = isl_union_map_get_space(umap);
3542 space = isl_space_drop_dims(space, type, first, n);
3543 data.res = isl_union_map_empty(space);
3544 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3545 data.res = isl_union_map_free(data.res);
3547 isl_union_map_free(umap);
3549 return data.res;
3552 /* Turn the "n" dimensions of type "type", starting at "first"
3553 * into existentially quantified variables.
3554 * Since the space of an isl_union_set only contains parameters,
3555 * "type" is required to be equal to isl_dim_param.
3557 __isl_give isl_union_set *isl_union_set_project_out(
3558 __isl_take isl_union_set *uset,
3559 enum isl_dim_type type, unsigned first, unsigned n)
3561 return isl_union_map_project_out(uset, type, first, n);
3564 /* Internal data structure for isl_union_map_involves_dims.
3565 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3567 struct isl_union_map_involves_dims_data {
3568 unsigned first;
3569 unsigned n;
3572 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3574 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3576 struct isl_union_map_involves_dims_data *data = user;
3577 isl_bool involves;
3579 involves = isl_map_involves_dims(map,
3580 isl_dim_param, data->first, data->n);
3581 if (involves < 0)
3582 return isl_bool_error;
3583 return !involves;
3586 /* Does "umap" involve any of the n parameters starting at first?
3587 * "type" is required to be set to isl_dim_param.
3589 * "umap" involves any of those parameters if any of its maps
3590 * involve the parameters. In other words, "umap" does not
3591 * involve any of the parameters if all its maps to not
3592 * involve the parameters.
3594 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3595 enum isl_dim_type type, unsigned first, unsigned n)
3597 struct isl_union_map_involves_dims_data data = { first, n };
3598 isl_bool excludes;
3600 if (type != isl_dim_param)
3601 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3602 "can only reference parameters", return isl_bool_error);
3604 excludes = union_map_forall_user(umap, &map_excludes, &data);
3606 if (excludes < 0)
3607 return isl_bool_error;
3609 return !excludes;
3612 /* Internal data structure for isl_union_map_reset_range_space.
3613 * "range" is the space from which to set the range space.
3614 * "res" collects the results.
3616 struct isl_union_map_reset_range_space_data {
3617 isl_space *range;
3618 isl_union_map *res;
3621 /* Replace the range space of "map" by the range space of data->range and
3622 * add the result to data->res.
3624 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3626 struct isl_union_map_reset_range_space_data *data = user;
3627 isl_space *space;
3629 space = isl_map_get_space(map);
3630 space = isl_space_domain(space);
3631 space = isl_space_extend_domain_with_range(space,
3632 isl_space_copy(data->range));
3633 map = isl_map_reset_space(map, space);
3634 data->res = isl_union_map_add_map(data->res, map);
3636 return data->res ? isl_stat_ok : isl_stat_error;
3639 /* Replace the range space of all the maps in "umap" by
3640 * the range space of "space".
3642 * This assumes that all maps have the same output dimension.
3643 * This function should therefore not be made publicly available.
3645 * Since the spaces of the maps change, so do their hash value.
3646 * We therefore need to create a new isl_union_map.
3648 __isl_give isl_union_map *isl_union_map_reset_range_space(
3649 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3651 struct isl_union_map_reset_range_space_data data = { space };
3653 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3654 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3655 data.res = isl_union_map_free(data.res);
3657 isl_space_free(space);
3658 isl_union_map_free(umap);
3659 return data.res;
3662 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3663 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3664 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3665 * to the domain and the range of each map.
3666 * "res" collects the results.
3668 struct isl_union_order_at_data {
3669 isl_multi_union_pw_aff *mupa;
3670 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3671 __isl_take isl_multi_pw_aff *mpa2);
3672 isl_union_map *res;
3675 /* Intersect "map" with the result of applying data->order to
3676 * the functions in data->mupa that apply to the domain and the range
3677 * of "map" and add the result to data->res.
3679 static isl_stat order_at(__isl_take isl_map *map, void *user)
3681 struct isl_union_order_at_data *data = user;
3682 isl_space *space;
3683 isl_multi_pw_aff *mpa1, *mpa2;
3684 isl_map *order;
3686 space = isl_space_domain(isl_map_get_space(map));
3687 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3688 space = isl_space_range(isl_map_get_space(map));
3689 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3690 order = data->order(mpa1, mpa2);
3691 map = isl_map_intersect(map, order);
3692 data->res = isl_union_map_add_map(data->res, map);
3694 return data->res ? isl_stat_ok : isl_stat_error;
3697 /* Intersect each map in "umap" with the result of calling "order"
3698 * on the functions is "mupa" that apply to the domain and the range
3699 * of the map.
3701 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3702 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3703 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3704 __isl_take isl_multi_pw_aff *mpa2))
3706 struct isl_union_order_at_data data;
3708 umap = isl_union_map_align_params(umap,
3709 isl_multi_union_pw_aff_get_space(mupa));
3710 mupa = isl_multi_union_pw_aff_align_params(mupa,
3711 isl_union_map_get_space(umap));
3712 data.mupa = mupa;
3713 data.order = order;
3714 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3715 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3716 data.res = isl_union_map_free(data.res);
3718 isl_multi_union_pw_aff_free(mupa);
3719 isl_union_map_free(umap);
3720 return data.res;
3723 /* Return the subset of "umap" where the domain and the range
3724 * have equal "mupa" values.
3726 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3727 __isl_take isl_union_map *umap,
3728 __isl_take isl_multi_union_pw_aff *mupa)
3730 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3731 &isl_multi_pw_aff_eq_map);
3734 /* Return the subset of "umap" where the domain has a lexicographically
3735 * smaller "mupa" value than the range.
3737 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3738 __isl_take isl_union_map *umap,
3739 __isl_take isl_multi_union_pw_aff *mupa)
3741 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3742 &isl_multi_pw_aff_lex_lt_map);
3745 /* Return the subset of "umap" where the domain has a lexicographically
3746 * greater "mupa" value than the range.
3748 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3749 __isl_take isl_union_map *umap,
3750 __isl_take isl_multi_union_pw_aff *mupa)
3752 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3753 &isl_multi_pw_aff_lex_gt_map);
3756 /* Return the union of the elements in the list "list".
3758 __isl_give isl_union_set *isl_union_set_list_union(
3759 __isl_take isl_union_set_list *list)
3761 int i, n;
3762 isl_ctx *ctx;
3763 isl_space *space;
3764 isl_union_set *res;
3766 if (!list)
3767 return NULL;
3769 ctx = isl_union_set_list_get_ctx(list);
3770 space = isl_space_params_alloc(ctx, 0);
3771 res = isl_union_set_empty(space);
3773 n = isl_union_set_list_n_union_set(list);
3774 for (i = 0; i < n; ++i) {
3775 isl_union_set *uset_i;
3777 uset_i = isl_union_set_list_get_union_set(list, i);
3778 res = isl_union_set_union(res, uset_i);
3781 isl_union_set_list_free(list);
3782 return res;
3785 /* Update *hash with the hash value of "map".
3787 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3789 uint32_t *hash = user;
3790 uint32_t map_hash;
3792 map_hash = isl_map_get_hash(map);
3793 isl_hash_hash(*hash, map_hash);
3795 isl_map_free(map);
3796 return isl_stat_ok;
3799 /* Return a hash value that digests "umap".
3801 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3803 uint32_t hash;
3805 if (!umap)
3806 return 0;
3808 hash = isl_hash_init();
3809 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3810 return 0;
3812 return hash;
3815 /* Return a hash value that digests "uset".
3817 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3819 return isl_union_map_get_hash(uset);