isl_scheduler.c: node_has_space: rename "dim" variable to "space"
[isl.git] / isl_union_map.c
blob5fc9ac5aa43a0759a6e35b40497be26210083b5e
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 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 or that the union map only
1529 * has a single reference.
1530 * If "total" is set, then all maps need to be modified and
1531 * the results need to live in the same space.
1532 * Otherwise, a new union map is constructed to store the results.
1533 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1534 * are taken into account. No filter can be set if "inplace" or
1535 * "total" is set.
1536 * "fn_map" specifies how the maps (selected by "filter")
1537 * should be transformed.
1539 struct isl_un_op_control {
1540 int inplace;
1541 int total;
1542 isl_bool (*filter)(__isl_keep isl_map *map);
1543 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1546 /* Internal data structure for "un_op".
1547 * "control" specifies how the maps in the union map should be modified.
1548 * "res" collects the results.
1550 struct isl_union_map_un_data {
1551 struct isl_un_op_control *control;
1552 isl_union_map *res;
1555 /* isl_hash_table_foreach callback for un_op.
1556 * Handle the map that "entry" points to.
1558 * If control->filter is set, then check if this map satisfies the filter.
1559 * If so (or if control->filter is not set), modify the map
1560 * by calling control->fn_map and either add the result to data->res or
1561 * replace the original entry by the result (if control->inplace is set).
1563 static isl_stat un_entry(void **entry, void *user)
1565 struct isl_union_map_un_data *data = user;
1566 isl_map *map = *entry;
1568 if (data->control->filter) {
1569 isl_bool ok;
1571 ok = data->control->filter(map);
1572 if (ok < 0)
1573 return isl_stat_error;
1574 if (!ok)
1575 return isl_stat_ok;
1578 map = data->control->fn_map(isl_map_copy(map));
1579 if (!map)
1580 return isl_stat_error;
1581 if (data->control->inplace) {
1582 isl_map_free(*entry);
1583 *entry = map;
1584 } else {
1585 data->res = isl_union_map_add_map(data->res, map);
1586 if (!data->res)
1587 return isl_stat_error;
1590 return isl_stat_ok;
1593 /* Modify the maps in "umap" based on "control".
1594 * If control->inplace is set, then modify the maps in "umap" in-place.
1595 * Otherwise, create a new union map to hold the results.
1596 * If control->total is set, then perform an inplace computation
1597 * if "umap" is only referenced once. Otherwise, create a new union map
1598 * to store the results.
1600 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1601 struct isl_un_op_control *control)
1603 struct isl_union_map_un_data data = { control };
1605 if (!umap)
1606 return NULL;
1607 if ((control->inplace || control->total) && control->filter)
1608 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1609 "inplace/total modification cannot be filtered",
1610 return isl_union_map_free(umap));
1612 if (control->total && umap->ref == 1)
1613 control->inplace = 1;
1614 if (control->inplace) {
1615 data.res = umap;
1616 } else {
1617 isl_space *space;
1619 space = isl_union_map_get_space(umap);
1620 data.res = isl_union_map_alloc(space, umap->table.n);
1622 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1623 &umap->table, &un_entry, &data) < 0)
1624 data.res = isl_union_map_free(data.res);
1626 if (control->inplace)
1627 return data.res;
1628 isl_union_map_free(umap);
1629 return data.res;
1632 __isl_give isl_union_map *isl_union_map_from_range(
1633 __isl_take isl_union_set *uset)
1635 struct isl_un_op_control control = {
1636 .fn_map = &isl_map_from_range,
1638 return un_op(uset, &control);
1641 __isl_give isl_union_map *isl_union_map_from_domain(
1642 __isl_take isl_union_set *uset)
1644 return isl_union_map_reverse(isl_union_map_from_range(uset));
1647 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1648 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1650 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1651 isl_union_map_from_range(range));
1654 /* Modify the maps in "umap" by applying "fn" on them.
1655 * "fn" should apply to all maps in "umap" and should not modify the space.
1657 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1658 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1660 struct isl_un_op_control control = {
1661 .total = 1,
1662 .fn_map = fn,
1665 return un_op(umap, &control);
1668 /* Compute the affine hull of "map" and return the result as an isl_map.
1670 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1672 return isl_map_from_basic_map(isl_map_affine_hull(map));
1675 __isl_give isl_union_map *isl_union_map_affine_hull(
1676 __isl_take isl_union_map *umap)
1678 return total(umap, &isl_map_affine_hull_map);
1681 __isl_give isl_union_set *isl_union_set_affine_hull(
1682 __isl_take isl_union_set *uset)
1684 return isl_union_map_affine_hull(uset);
1687 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1689 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1691 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1694 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1695 __isl_take isl_union_map *umap)
1697 return total(umap, &isl_map_polyhedral_hull_map);
1700 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1701 __isl_take isl_union_set *uset)
1703 return isl_union_map_polyhedral_hull(uset);
1706 /* Compute a superset of the convex hull of "map" that is described
1707 * by only translates of the constraints in the constituents of "map" and
1708 * return the result as an isl_map.
1710 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1712 return isl_map_from_basic_map(isl_map_simple_hull(map));
1715 __isl_give isl_union_map *isl_union_map_simple_hull(
1716 __isl_take isl_union_map *umap)
1718 return total(umap, &isl_map_simple_hull_map);
1721 __isl_give isl_union_set *isl_union_set_simple_hull(
1722 __isl_take isl_union_set *uset)
1724 return isl_union_map_simple_hull(uset);
1727 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1728 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1730 struct isl_un_op_control control = {
1731 .inplace = 1,
1732 .fn_map = fn,
1735 return un_op(umap, &control);
1738 /* Remove redundant constraints in each of the basic maps of "umap".
1739 * Since removing redundant constraints does not change the meaning
1740 * or the space, the operation can be performed in-place.
1742 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1743 __isl_take isl_union_map *umap)
1745 return inplace(umap, &isl_map_remove_redundancies);
1748 /* Remove redundant constraints in each of the basic sets of "uset".
1750 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1751 __isl_take isl_union_set *uset)
1753 return isl_union_map_remove_redundancies(uset);
1756 __isl_give isl_union_map *isl_union_map_coalesce(
1757 __isl_take isl_union_map *umap)
1759 return inplace(umap, &isl_map_coalesce);
1762 __isl_give isl_union_set *isl_union_set_coalesce(
1763 __isl_take isl_union_set *uset)
1765 return isl_union_map_coalesce(uset);
1768 __isl_give isl_union_map *isl_union_map_detect_equalities(
1769 __isl_take isl_union_map *umap)
1771 return inplace(umap, &isl_map_detect_equalities);
1774 __isl_give isl_union_set *isl_union_set_detect_equalities(
1775 __isl_take isl_union_set *uset)
1777 return isl_union_map_detect_equalities(uset);
1780 __isl_give isl_union_map *isl_union_map_compute_divs(
1781 __isl_take isl_union_map *umap)
1783 return inplace(umap, &isl_map_compute_divs);
1786 __isl_give isl_union_set *isl_union_set_compute_divs(
1787 __isl_take isl_union_set *uset)
1789 return isl_union_map_compute_divs(uset);
1792 __isl_give isl_union_map *isl_union_map_lexmin(
1793 __isl_take isl_union_map *umap)
1795 return total(umap, &isl_map_lexmin);
1798 __isl_give isl_union_set *isl_union_set_lexmin(
1799 __isl_take isl_union_set *uset)
1801 return isl_union_map_lexmin(uset);
1804 __isl_give isl_union_map *isl_union_map_lexmax(
1805 __isl_take isl_union_map *umap)
1807 return total(umap, &isl_map_lexmax);
1810 __isl_give isl_union_set *isl_union_set_lexmax(
1811 __isl_take isl_union_set *uset)
1813 return isl_union_map_lexmax(uset);
1816 /* Return the universe in the space of "map".
1818 static __isl_give isl_map *universe(__isl_take isl_map *map)
1820 isl_space *space;
1822 space = isl_map_get_space(map);
1823 isl_map_free(map);
1824 return isl_map_universe(space);
1827 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1829 struct isl_un_op_control control = {
1830 .fn_map = &universe,
1832 return un_op(umap, &control);
1835 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1837 return isl_union_map_universe(uset);
1840 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1842 struct isl_un_op_control control = {
1843 .fn_map = &isl_map_reverse,
1845 return un_op(umap, &control);
1848 /* Compute the parameter domain of the given union map.
1850 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1852 struct isl_un_op_control control = {
1853 .fn_map = &isl_map_params,
1855 int empty;
1857 empty = isl_union_map_is_empty(umap);
1858 if (empty < 0)
1859 goto error;
1860 if (empty) {
1861 isl_space *space;
1862 space = isl_union_map_get_space(umap);
1863 isl_union_map_free(umap);
1864 return isl_set_empty(space);
1866 return isl_set_from_union_set(un_op(umap, &control));
1867 error:
1868 isl_union_map_free(umap);
1869 return NULL;
1872 /* Compute the parameter domain of the given union set.
1874 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1876 return isl_union_map_params(uset);
1879 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1881 struct isl_un_op_control control = {
1882 .fn_map = &isl_map_domain,
1884 return un_op(umap, &control);
1887 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1889 struct isl_un_op_control control = {
1890 .fn_map = &isl_map_range,
1892 return un_op(umap, &control);
1895 __isl_give isl_union_map *isl_union_map_domain_map(
1896 __isl_take isl_union_map *umap)
1898 struct isl_un_op_control control = {
1899 .fn_map = &isl_map_domain_map,
1901 return un_op(umap, &control);
1904 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1905 * add the result to "res".
1907 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1909 isl_union_pw_multi_aff **res = user;
1910 isl_multi_aff *ma;
1911 isl_pw_multi_aff *pma;
1913 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1914 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1915 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1917 return *res ? isl_stat_ok : isl_stat_error;
1921 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1922 * to its domain.
1924 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1925 __isl_take isl_union_map *umap)
1927 isl_union_pw_multi_aff *res;
1929 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1930 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1931 res = isl_union_pw_multi_aff_free(res);
1933 isl_union_map_free(umap);
1934 return res;
1937 __isl_give isl_union_map *isl_union_map_range_map(
1938 __isl_take isl_union_map *umap)
1940 struct isl_un_op_control control = {
1941 .fn_map = &isl_map_range_map,
1943 return un_op(umap, &control);
1946 /* Given a collection of wrapped maps of the form A[B -> C],
1947 * return the collection of maps A[B -> C] -> B.
1949 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1950 __isl_take isl_union_set *uset)
1952 struct isl_un_op_control control = {
1953 .filter = &isl_set_is_wrapping,
1954 .fn_map = &isl_set_wrapped_domain_map,
1956 return un_op(uset, &control);
1959 /* Does "map" relate elements from the same space?
1961 static isl_bool equal_tuples(__isl_keep isl_map *map)
1963 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
1964 map->dim, isl_dim_out);
1967 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1969 struct isl_un_op_control control = {
1970 .filter = &equal_tuples,
1971 .fn_map = &isl_map_deltas,
1973 return un_op(umap, &control);
1976 __isl_give isl_union_map *isl_union_map_deltas_map(
1977 __isl_take isl_union_map *umap)
1979 struct isl_un_op_control control = {
1980 .filter = &equal_tuples,
1981 .fn_map = &isl_map_deltas_map,
1983 return un_op(umap, &control);
1986 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1988 struct isl_un_op_control control = {
1989 .fn_map = &isl_set_identity,
1991 return un_op(uset, &control);
1994 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
1996 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
1998 isl_union_pw_multi_aff **res = user;
1999 isl_space *space;
2000 isl_pw_multi_aff *pma;
2002 space = isl_space_map_from_set(isl_set_get_space(set));
2003 pma = isl_pw_multi_aff_identity(space);
2004 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2005 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2007 return *res ? isl_stat_ok : isl_stat_error;
2010 /* Return an identity function on "uset" in the form
2011 * of an isl_union_pw_multi_aff.
2013 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2014 __isl_take isl_union_set *uset)
2016 isl_union_pw_multi_aff *res;
2018 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2019 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2020 res = isl_union_pw_multi_aff_free(res);
2022 isl_union_set_free(uset);
2023 return res;
2026 /* For each map in "umap" of the form [A -> B] -> C,
2027 * construct the map A -> C and collect the results.
2029 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2030 __isl_take isl_union_map *umap)
2032 struct isl_un_op_control control = {
2033 .filter = &isl_map_domain_is_wrapping,
2034 .fn_map = &isl_map_domain_factor_domain,
2036 return un_op(umap, &control);
2039 /* For each map in "umap" of the form [A -> B] -> C,
2040 * construct the map B -> C and collect the results.
2042 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2043 __isl_take isl_union_map *umap)
2045 struct isl_un_op_control control = {
2046 .filter = &isl_map_domain_is_wrapping,
2047 .fn_map = &isl_map_domain_factor_range,
2049 return un_op(umap, &control);
2052 /* For each map in "umap" of the form A -> [B -> C],
2053 * construct the map A -> B and collect the results.
2055 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2056 __isl_take isl_union_map *umap)
2058 struct isl_un_op_control control = {
2059 .filter = &isl_map_range_is_wrapping,
2060 .fn_map = &isl_map_range_factor_domain,
2062 return un_op(umap, &control);
2065 /* For each map in "umap" of the form A -> [B -> C],
2066 * construct the map A -> C and collect the results.
2068 __isl_give isl_union_map *isl_union_map_range_factor_range(
2069 __isl_take isl_union_map *umap)
2071 struct isl_un_op_control control = {
2072 .filter = &isl_map_range_is_wrapping,
2073 .fn_map = &isl_map_range_factor_range,
2075 return un_op(umap, &control);
2078 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2079 * construct the map A -> C and collect the results.
2081 __isl_give isl_union_map *isl_union_map_factor_domain(
2082 __isl_take isl_union_map *umap)
2084 struct isl_un_op_control control = {
2085 .filter = &isl_map_is_product,
2086 .fn_map = &isl_map_factor_domain,
2088 return un_op(umap, &control);
2091 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2092 * construct the map B -> D and collect the results.
2094 __isl_give isl_union_map *isl_union_map_factor_range(
2095 __isl_take isl_union_map *umap)
2097 struct isl_un_op_control control = {
2098 .filter = &isl_map_is_product,
2099 .fn_map = &isl_map_factor_range,
2101 return un_op(umap, &control);
2104 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2106 struct isl_un_op_control control = {
2107 .filter = &isl_set_is_wrapping,
2108 .fn_map = &isl_set_unwrap,
2110 return un_op(uset, &control);
2113 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2115 struct isl_un_op_control control = {
2116 .fn_map = &isl_map_wrap,
2118 return un_op(umap, &control);
2121 struct isl_union_map_is_subset_data {
2122 isl_union_map *umap2;
2123 isl_bool is_subset;
2126 static isl_stat is_subset_entry(void **entry, void *user)
2128 struct isl_union_map_is_subset_data *data = user;
2129 uint32_t hash;
2130 struct isl_hash_table_entry *entry2;
2131 isl_map *map = *entry;
2133 hash = isl_space_get_hash(map->dim);
2134 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2135 hash, &has_dim, map->dim, 0);
2136 if (!entry2) {
2137 int empty = isl_map_is_empty(map);
2138 if (empty < 0)
2139 return isl_stat_error;
2140 if (empty)
2141 return isl_stat_ok;
2142 data->is_subset = 0;
2143 return isl_stat_error;
2146 data->is_subset = isl_map_is_subset(map, entry2->data);
2147 if (data->is_subset < 0 || !data->is_subset)
2148 return isl_stat_error;
2150 return isl_stat_ok;
2153 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2154 __isl_keep isl_union_map *umap2)
2156 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2158 umap1 = isl_union_map_copy(umap1);
2159 umap2 = isl_union_map_copy(umap2);
2160 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2161 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2163 if (!umap1 || !umap2)
2164 goto error;
2166 data.umap2 = umap2;
2167 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2168 &is_subset_entry, &data) < 0 &&
2169 data.is_subset)
2170 goto error;
2172 isl_union_map_free(umap1);
2173 isl_union_map_free(umap2);
2175 return data.is_subset;
2176 error:
2177 isl_union_map_free(umap1);
2178 isl_union_map_free(umap2);
2179 return isl_bool_error;
2182 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2183 __isl_keep isl_union_set *uset2)
2185 return isl_union_map_is_subset(uset1, uset2);
2188 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2189 __isl_keep isl_union_map *umap2)
2191 isl_bool is_subset;
2193 if (!umap1 || !umap2)
2194 return isl_bool_error;
2195 is_subset = isl_union_map_is_subset(umap1, umap2);
2196 if (is_subset != isl_bool_true)
2197 return is_subset;
2198 is_subset = isl_union_map_is_subset(umap2, umap1);
2199 return is_subset;
2202 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2203 __isl_keep isl_union_set *uset2)
2205 return isl_union_map_is_equal(uset1, uset2);
2208 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2209 __isl_keep isl_union_map *umap2)
2211 isl_bool is_subset;
2213 if (!umap1 || !umap2)
2214 return isl_bool_error;
2215 is_subset = isl_union_map_is_subset(umap1, umap2);
2216 if (is_subset != isl_bool_true)
2217 return is_subset;
2218 is_subset = isl_union_map_is_subset(umap2, umap1);
2219 if (is_subset == isl_bool_error)
2220 return is_subset;
2221 return !is_subset;
2224 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2225 __isl_keep isl_union_set *uset2)
2227 return isl_union_map_is_strict_subset(uset1, uset2);
2230 /* Internal data structure for isl_union_map_is_disjoint.
2231 * umap2 is the union map with which we are comparing.
2232 * is_disjoint is initialized to 1 and is set to 0 as soon
2233 * as the union maps turn out not to be disjoint.
2235 struct isl_union_map_is_disjoint_data {
2236 isl_union_map *umap2;
2237 isl_bool is_disjoint;
2240 /* Check if "map" is disjoint from data->umap2 and abort
2241 * the search if it is not.
2243 static isl_stat is_disjoint_entry(void **entry, void *user)
2245 struct isl_union_map_is_disjoint_data *data = user;
2246 uint32_t hash;
2247 struct isl_hash_table_entry *entry2;
2248 isl_map *map = *entry;
2250 hash = isl_space_get_hash(map->dim);
2251 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2252 hash, &has_dim, map->dim, 0);
2253 if (!entry2)
2254 return isl_stat_ok;
2256 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2257 if (data->is_disjoint < 0 || !data->is_disjoint)
2258 return isl_stat_error;
2260 return isl_stat_ok;
2263 /* Are "umap1" and "umap2" disjoint?
2265 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2266 __isl_keep isl_union_map *umap2)
2268 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2270 umap1 = isl_union_map_copy(umap1);
2271 umap2 = isl_union_map_copy(umap2);
2272 umap1 = isl_union_map_align_params(umap1,
2273 isl_union_map_get_space(umap2));
2274 umap2 = isl_union_map_align_params(umap2,
2275 isl_union_map_get_space(umap1));
2277 if (!umap1 || !umap2)
2278 goto error;
2280 data.umap2 = umap2;
2281 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2282 &is_disjoint_entry, &data) < 0 &&
2283 data.is_disjoint)
2284 goto error;
2286 isl_union_map_free(umap1);
2287 isl_union_map_free(umap2);
2289 return data.is_disjoint;
2290 error:
2291 isl_union_map_free(umap1);
2292 isl_union_map_free(umap2);
2293 return isl_bool_error;
2296 /* Are "uset1" and "uset2" disjoint?
2298 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2299 __isl_keep isl_union_set *uset2)
2301 return isl_union_map_is_disjoint(uset1, uset2);
2304 static isl_stat sample_entry(void **entry, void *user)
2306 isl_basic_map **sample = (isl_basic_map **)user;
2307 isl_map *map = *entry;
2309 *sample = isl_map_sample(isl_map_copy(map));
2310 if (!*sample)
2311 return isl_stat_error;
2312 if (!isl_basic_map_plain_is_empty(*sample))
2313 return isl_stat_error;
2314 return isl_stat_ok;
2317 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2319 isl_basic_map *sample = NULL;
2321 if (!umap)
2322 return NULL;
2324 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2325 &sample_entry, &sample) < 0 &&
2326 !sample)
2327 goto error;
2329 if (!sample)
2330 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2332 isl_union_map_free(umap);
2334 return sample;
2335 error:
2336 isl_union_map_free(umap);
2337 return NULL;
2340 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2342 return bset_from_bmap(isl_union_map_sample(uset));
2345 /* Return an element in "uset" in the form of an isl_point.
2346 * Return a void isl_point if "uset" is empty.
2348 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2350 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2353 struct isl_forall_data {
2354 isl_bool res;
2355 isl_bool (*fn)(__isl_keep isl_map *map);
2358 static isl_stat forall_entry(void **entry, void *user)
2360 struct isl_forall_data *data = user;
2361 isl_map *map = *entry;
2363 data->res = data->fn(map);
2364 if (data->res < 0)
2365 return isl_stat_error;
2367 if (!data->res)
2368 return isl_stat_error;
2370 return isl_stat_ok;
2373 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2374 isl_bool (*fn)(__isl_keep isl_map *map))
2376 struct isl_forall_data data = { isl_bool_true, fn };
2378 if (!umap)
2379 return isl_bool_error;
2381 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2382 &forall_entry, &data) < 0 && data.res)
2383 return isl_bool_error;
2385 return data.res;
2388 struct isl_forall_user_data {
2389 isl_bool res;
2390 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2391 void *user;
2394 static isl_stat forall_user_entry(void **entry, void *user)
2396 struct isl_forall_user_data *data = user;
2397 isl_map *map = *entry;
2399 data->res = data->fn(map, data->user);
2400 if (data->res < 0)
2401 return isl_stat_error;
2403 if (!data->res)
2404 return isl_stat_error;
2406 return isl_stat_ok;
2409 /* Check if fn(map, user) returns true for all maps "map" in umap.
2411 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2412 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2414 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2416 if (!umap)
2417 return isl_bool_error;
2419 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2420 &forall_user_entry, &data) < 0 && data.res)
2421 return isl_bool_error;
2423 return data.res;
2426 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2428 return union_map_forall(umap, &isl_map_is_empty);
2431 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2433 return isl_union_map_is_empty(uset);
2436 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2438 isl_bool is_subset;
2439 isl_space *dim;
2440 isl_map *id;
2442 if (!map)
2443 return isl_bool_error;
2445 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2446 map->dim, isl_dim_out))
2447 return isl_bool_false;
2449 dim = isl_map_get_space(map);
2450 id = isl_map_identity(dim);
2452 is_subset = isl_map_is_subset(map, id);
2454 isl_map_free(id);
2456 return is_subset;
2459 /* Given an isl_union_map that consists of a single map, check
2460 * if it is single-valued.
2462 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2464 isl_map *map;
2465 isl_bool sv;
2467 umap = isl_union_map_copy(umap);
2468 map = isl_map_from_union_map(umap);
2469 sv = isl_map_is_single_valued(map);
2470 isl_map_free(map);
2472 return sv;
2475 /* Internal data structure for single_valued_on_domain.
2477 * "umap" is the union map to be tested.
2478 * "sv" is set to 1 as long as "umap" may still be single-valued.
2480 struct isl_union_map_is_sv_data {
2481 isl_union_map *umap;
2482 isl_bool sv;
2485 /* Check if the data->umap is single-valued on "set".
2487 * If data->umap consists of a single map on "set", then test it
2488 * as an isl_map.
2490 * Otherwise, compute
2492 * M \circ M^-1
2494 * check if the result is a subset of the identity mapping and
2495 * store the result in data->sv.
2497 * Terminate as soon as data->umap has been determined not to
2498 * be single-valued.
2500 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2502 struct isl_union_map_is_sv_data *data = user;
2503 isl_union_map *umap, *test;
2505 umap = isl_union_map_copy(data->umap);
2506 umap = isl_union_map_intersect_domain(umap,
2507 isl_union_set_from_set(set));
2509 if (isl_union_map_n_map(umap) == 1) {
2510 data->sv = single_map_is_single_valued(umap);
2511 isl_union_map_free(umap);
2512 } else {
2513 test = isl_union_map_reverse(isl_union_map_copy(umap));
2514 test = isl_union_map_apply_range(test, umap);
2516 data->sv = union_map_forall(test, &is_subset_of_identity);
2518 isl_union_map_free(test);
2521 if (data->sv < 0 || !data->sv)
2522 return isl_stat_error;
2523 return isl_stat_ok;
2526 /* Check if the given map is single-valued.
2528 * If the union map consists of a single map, then test it as an isl_map.
2529 * Otherwise, check if the union map is single-valued on each of its
2530 * domain spaces.
2532 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2534 isl_union_map *universe;
2535 isl_union_set *domain;
2536 struct isl_union_map_is_sv_data data;
2538 if (isl_union_map_n_map(umap) == 1)
2539 return single_map_is_single_valued(umap);
2541 universe = isl_union_map_universe(isl_union_map_copy(umap));
2542 domain = isl_union_map_domain(universe);
2544 data.sv = isl_bool_true;
2545 data.umap = umap;
2546 if (isl_union_set_foreach_set(domain,
2547 &single_valued_on_domain, &data) < 0 && data.sv)
2548 data.sv = isl_bool_error;
2549 isl_union_set_free(domain);
2551 return data.sv;
2554 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2556 isl_bool in;
2558 umap = isl_union_map_copy(umap);
2559 umap = isl_union_map_reverse(umap);
2560 in = isl_union_map_is_single_valued(umap);
2561 isl_union_map_free(umap);
2563 return in;
2566 /* Is "map" obviously not an identity relation because
2567 * it maps elements from one space to another space?
2568 * Update *non_identity accordingly.
2570 * In particular, if the domain and range spaces are the same,
2571 * then the map is not considered to obviously not be an identity relation.
2572 * Otherwise, the map is considered to obviously not be an identity relation
2573 * if it is is non-empty.
2575 * If "map" is determined to obviously not be an identity relation,
2576 * then the search is aborted.
2578 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2580 isl_bool *non_identity = user;
2581 isl_bool equal;
2582 isl_space *space;
2584 space = isl_map_get_space(map);
2585 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2586 if (equal >= 0 && !equal)
2587 *non_identity = isl_bool_not(isl_map_is_empty(map));
2588 else
2589 *non_identity = isl_bool_not(equal);
2590 isl_space_free(space);
2591 isl_map_free(map);
2593 if (*non_identity < 0 || *non_identity)
2594 return isl_stat_error;
2596 return isl_stat_ok;
2599 /* Is "umap" obviously not an identity relation because
2600 * it maps elements from one space to another space?
2602 * As soon as a map has been found that maps elements to a different space,
2603 * non_identity is changed and the search is aborted.
2605 static isl_bool isl_union_map_plain_is_not_identity(
2606 __isl_keep isl_union_map *umap)
2608 isl_bool non_identity;
2610 non_identity = isl_bool_false;
2611 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2612 &non_identity) < 0 &&
2613 non_identity == isl_bool_false)
2614 return isl_bool_error;
2616 return non_identity;
2619 /* Does "map" only map elements to themselves?
2620 * Update *identity accordingly.
2622 * If "map" is determined not to be an identity relation,
2623 * then the search is aborted.
2625 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2627 isl_bool *identity = user;
2629 *identity = isl_map_is_identity(map);
2630 isl_map_free(map);
2632 if (*identity < 0 || !*identity)
2633 return isl_stat_error;
2635 return isl_stat_ok;
2638 /* Does "umap" only map elements to themselves?
2640 * First check if there are any maps that map elements to different spaces.
2641 * If not, then check that all the maps (between identical spaces)
2642 * are identity relations.
2644 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2646 isl_bool non_identity;
2647 isl_bool identity;
2649 non_identity = isl_union_map_plain_is_not_identity(umap);
2650 if (non_identity < 0 || non_identity)
2651 return isl_bool_not(non_identity);
2653 identity = isl_bool_true;
2654 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2655 identity == isl_bool_true)
2656 return isl_bool_error;
2658 return identity;
2661 /* Represents a map that has a fixed value (v) for one of its
2662 * range dimensions.
2663 * The map in this structure is not reference counted, so it
2664 * is only valid while the isl_union_map from which it was
2665 * obtained is still alive.
2667 struct isl_fixed_map {
2668 isl_int v;
2669 isl_map *map;
2672 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2673 int n)
2675 int i;
2676 struct isl_fixed_map *v;
2678 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2679 if (!v)
2680 return NULL;
2681 for (i = 0; i < n; ++i)
2682 isl_int_init(v[i].v);
2683 return v;
2686 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2688 int i;
2690 if (!v)
2691 return;
2692 for (i = 0; i < n; ++i)
2693 isl_int_clear(v[i].v);
2694 free(v);
2697 /* Compare the "v" field of two isl_fixed_map structs.
2699 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2701 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2702 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2704 return isl_int_cmp(e1->v, e2->v);
2707 /* Internal data structure used while checking whether all maps
2708 * in a union_map have a fixed value for a given output dimension.
2709 * v is the list of maps, with the fixed value for the dimension
2710 * n is the number of maps considered so far
2711 * pos is the output dimension under investigation
2713 struct isl_fixed_dim_data {
2714 struct isl_fixed_map *v;
2715 int n;
2716 int pos;
2719 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2721 struct isl_fixed_dim_data *data = user;
2723 data->v[data->n].map = map;
2724 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2725 &data->v[data->n++].v);
2728 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2729 int first, int n_range);
2731 /* Given a list of the maps, with their fixed values at output dimension "pos",
2732 * check whether the ranges of the maps form an obvious partition.
2734 * We first sort the maps according to their fixed values.
2735 * If all maps have a different value, then we know the ranges form
2736 * a partition.
2737 * Otherwise, we collect the maps with the same fixed value and
2738 * check whether each such collection is obviously injective
2739 * based on later dimensions.
2741 static int separates(struct isl_fixed_map *v, int n,
2742 __isl_take isl_space *dim, int pos, int n_range)
2744 int i;
2746 if (!v)
2747 goto error;
2749 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2751 for (i = 0; i + 1 < n; ++i) {
2752 int j, k;
2753 isl_union_map *part;
2754 int injective;
2756 for (j = i + 1; j < n; ++j)
2757 if (isl_int_ne(v[i].v, v[j].v))
2758 break;
2760 if (j == i + 1)
2761 continue;
2763 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2764 for (k = i; k < j; ++k)
2765 part = isl_union_map_add_map(part,
2766 isl_map_copy(v[k].map));
2768 injective = plain_injective_on_range(part, pos + 1, n_range);
2769 if (injective < 0)
2770 goto error;
2771 if (!injective)
2772 break;
2774 i = j - 1;
2777 isl_space_free(dim);
2778 free_isl_fixed_map_array(v, n);
2779 return i + 1 >= n;
2780 error:
2781 isl_space_free(dim);
2782 free_isl_fixed_map_array(v, n);
2783 return -1;
2786 /* Check whether the maps in umap have obviously distinct ranges.
2787 * In particular, check for an output dimension in the range
2788 * [first,n_range) for which all maps have a fixed value
2789 * and then check if these values, possibly along with fixed values
2790 * at later dimensions, entail distinct ranges.
2792 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2793 int first, int n_range)
2795 isl_ctx *ctx;
2796 int n;
2797 struct isl_fixed_dim_data data = { NULL };
2799 ctx = isl_union_map_get_ctx(umap);
2801 n = isl_union_map_n_map(umap);
2802 if (!umap)
2803 goto error;
2805 if (n <= 1) {
2806 isl_union_map_free(umap);
2807 return isl_bool_true;
2810 if (first >= n_range) {
2811 isl_union_map_free(umap);
2812 return isl_bool_false;
2815 data.v = alloc_isl_fixed_map_array(ctx, n);
2816 if (!data.v)
2817 goto error;
2819 for (data.pos = first; data.pos < n_range; ++data.pos) {
2820 isl_bool fixed;
2821 int injective;
2822 isl_space *dim;
2824 data.n = 0;
2825 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2826 if (fixed < 0)
2827 goto error;
2828 if (!fixed)
2829 continue;
2830 dim = isl_union_map_get_space(umap);
2831 injective = separates(data.v, n, dim, data.pos, n_range);
2832 isl_union_map_free(umap);
2833 return injective;
2836 free_isl_fixed_map_array(data.v, n);
2837 isl_union_map_free(umap);
2839 return isl_bool_false;
2840 error:
2841 free_isl_fixed_map_array(data.v, n);
2842 isl_union_map_free(umap);
2843 return isl_bool_error;
2846 /* Check whether the maps in umap that map to subsets of "ran"
2847 * have obviously distinct ranges.
2849 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2850 void *user)
2852 isl_union_map *umap = user;
2854 umap = isl_union_map_copy(umap);
2855 umap = isl_union_map_intersect_range(umap,
2856 isl_union_set_from_set(isl_set_copy(ran)));
2857 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2860 /* Check if the given union_map is obviously injective.
2862 * In particular, we first check if all individual maps are obviously
2863 * injective and then check if all the ranges of these maps are
2864 * obviously disjoint.
2866 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2868 isl_bool in;
2869 isl_union_map *univ;
2870 isl_union_set *ran;
2872 in = union_map_forall(umap, &isl_map_plain_is_injective);
2873 if (in < 0)
2874 return isl_bool_error;
2875 if (!in)
2876 return isl_bool_false;
2878 univ = isl_union_map_universe(isl_union_map_copy(umap));
2879 ran = isl_union_map_range(univ);
2881 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2883 isl_union_set_free(ran);
2885 return in;
2888 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2890 isl_bool sv;
2892 sv = isl_union_map_is_single_valued(umap);
2893 if (sv < 0 || !sv)
2894 return sv;
2896 return isl_union_map_is_injective(umap);
2899 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2901 struct isl_un_op_control control = {
2902 .filter = &isl_map_can_zip,
2903 .fn_map = &isl_map_zip,
2905 return un_op(umap, &control);
2908 /* Given a union map, take the maps of the form A -> (B -> C) and
2909 * return the union of the corresponding maps (A -> B) -> C.
2911 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2913 struct isl_un_op_control control = {
2914 .filter = &isl_map_can_uncurry,
2915 .fn_map = &isl_map_uncurry,
2917 return un_op(umap, &control);
2920 /* Given a union map, take the maps of the form (A -> B) -> C and
2921 * return the union of the corresponding maps A -> (B -> C).
2923 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2925 struct isl_un_op_control control = {
2926 .filter = &isl_map_can_curry,
2927 .fn_map = &isl_map_curry,
2929 return un_op(umap, &control);
2932 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2933 * return the union of the corresponding maps A -> (B -> (C -> D)).
2935 __isl_give isl_union_map *isl_union_map_range_curry(
2936 __isl_take isl_union_map *umap)
2938 struct isl_un_op_control control = {
2939 .filter = &isl_map_can_range_curry,
2940 .fn_map = &isl_map_range_curry,
2942 return un_op(umap, &control);
2945 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2947 struct isl_un_op_control control = {
2948 .fn_map = &isl_set_lift,
2950 return un_op(uset, &control);
2953 static isl_stat coefficients_entry(void **entry, void *user)
2955 isl_set *set = *entry;
2956 isl_union_set **res = user;
2958 set = isl_set_copy(set);
2959 set = isl_set_from_basic_set(isl_set_coefficients(set));
2960 *res = isl_union_set_add_set(*res, set);
2962 return isl_stat_ok;
2965 __isl_give isl_union_set *isl_union_set_coefficients(
2966 __isl_take isl_union_set *uset)
2968 isl_ctx *ctx;
2969 isl_space *dim;
2970 isl_union_set *res;
2972 if (!uset)
2973 return NULL;
2975 ctx = isl_union_set_get_ctx(uset);
2976 dim = isl_space_set_alloc(ctx, 0, 0);
2977 res = isl_union_map_alloc(dim, uset->table.n);
2978 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2979 &coefficients_entry, &res) < 0)
2980 goto error;
2982 isl_union_set_free(uset);
2983 return res;
2984 error:
2985 isl_union_set_free(uset);
2986 isl_union_set_free(res);
2987 return NULL;
2990 static isl_stat solutions_entry(void **entry, void *user)
2992 isl_set *set = *entry;
2993 isl_union_set **res = user;
2995 set = isl_set_copy(set);
2996 set = isl_set_from_basic_set(isl_set_solutions(set));
2997 if (!*res)
2998 *res = isl_union_set_from_set(set);
2999 else
3000 *res = isl_union_set_add_set(*res, set);
3002 if (!*res)
3003 return isl_stat_error;
3005 return isl_stat_ok;
3008 __isl_give isl_union_set *isl_union_set_solutions(
3009 __isl_take isl_union_set *uset)
3011 isl_union_set *res = NULL;
3013 if (!uset)
3014 return NULL;
3016 if (uset->table.n == 0) {
3017 res = isl_union_set_empty(isl_union_set_get_space(uset));
3018 isl_union_set_free(uset);
3019 return res;
3022 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3023 &solutions_entry, &res) < 0)
3024 goto error;
3026 isl_union_set_free(uset);
3027 return res;
3028 error:
3029 isl_union_set_free(uset);
3030 isl_union_set_free(res);
3031 return NULL;
3034 /* Is the domain space of "map" equal to "space"?
3036 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3038 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3039 space, isl_dim_out);
3042 /* Is the range space of "map" equal to "space"?
3044 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3046 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3047 space, isl_dim_out);
3050 /* Is the set space of "map" equal to "space"?
3052 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3054 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3055 space, isl_dim_out);
3058 /* Internal data structure for preimage_pw_multi_aff.
3060 * "pma" is the function under which the preimage should be taken.
3061 * "space" is the space of "pma".
3062 * "res" collects the results.
3063 * "fn" computes the preimage for a given map.
3064 * "match" returns true if "fn" can be called.
3066 struct isl_union_map_preimage_data {
3067 isl_space *space;
3068 isl_pw_multi_aff *pma;
3069 isl_union_map *res;
3070 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3071 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3072 __isl_take isl_pw_multi_aff *pma);
3075 /* Call data->fn to compute the preimage of the domain or range of *entry
3076 * under the function represented by data->pma, provided the domain/range
3077 * space of *entry matches the target space of data->pma
3078 * (as given by data->match), and add the result to data->res.
3080 static isl_stat preimage_entry(void **entry, void *user)
3082 int m;
3083 isl_map *map = *entry;
3084 struct isl_union_map_preimage_data *data = user;
3085 isl_bool empty;
3087 m = data->match(map, data->space);
3088 if (m < 0)
3089 return isl_stat_error;
3090 if (!m)
3091 return isl_stat_ok;
3093 map = isl_map_copy(map);
3094 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3096 empty = isl_map_is_empty(map);
3097 if (empty < 0 || empty) {
3098 isl_map_free(map);
3099 return empty < 0 ? isl_stat_error : isl_stat_ok;
3102 data->res = isl_union_map_add_map(data->res, map);
3104 return isl_stat_ok;
3107 /* Compute the preimage of the domain or range of "umap" under the function
3108 * represented by "pma".
3109 * In other words, plug in "pma" in the domain or range of "umap".
3110 * The function "fn" performs the actual preimage computation on a map,
3111 * while "match" determines to which maps the function should be applied.
3113 static __isl_give isl_union_map *preimage_pw_multi_aff(
3114 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3115 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3116 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3117 __isl_take isl_pw_multi_aff *pma))
3119 isl_ctx *ctx;
3120 isl_space *space;
3121 struct isl_union_map_preimage_data data;
3123 umap = isl_union_map_align_params(umap,
3124 isl_pw_multi_aff_get_space(pma));
3125 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3127 if (!umap || !pma)
3128 goto error;
3130 ctx = isl_union_map_get_ctx(umap);
3131 space = isl_union_map_get_space(umap);
3132 data.space = isl_pw_multi_aff_get_space(pma);
3133 data.pma = pma;
3134 data.res = isl_union_map_alloc(space, umap->table.n);
3135 data.match = match;
3136 data.fn = fn;
3137 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3138 &data) < 0)
3139 data.res = isl_union_map_free(data.res);
3141 isl_space_free(data.space);
3142 isl_union_map_free(umap);
3143 isl_pw_multi_aff_free(pma);
3144 return data.res;
3145 error:
3146 isl_union_map_free(umap);
3147 isl_pw_multi_aff_free(pma);
3148 return NULL;
3151 /* Compute the preimage of the domain of "umap" under the function
3152 * represented by "pma".
3153 * In other words, plug in "pma" in the domain of "umap".
3154 * The result contains maps that live in the same spaces as the maps of "umap"
3155 * with domain space equal to the target space of "pma",
3156 * except that the domain has been replaced by the domain space of "pma".
3158 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3159 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3161 return preimage_pw_multi_aff(umap, pma, &domain_match,
3162 &isl_map_preimage_domain_pw_multi_aff);
3165 /* Compute the preimage of the range of "umap" under the function
3166 * represented by "pma".
3167 * In other words, plug in "pma" in the range of "umap".
3168 * The result contains maps that live in the same spaces as the maps of "umap"
3169 * with range space equal to the target space of "pma",
3170 * except that the range has been replaced by the domain space of "pma".
3172 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3173 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3175 return preimage_pw_multi_aff(umap, pma, &range_match,
3176 &isl_map_preimage_range_pw_multi_aff);
3179 /* Compute the preimage of "uset" under the function represented by "pma".
3180 * In other words, plug in "pma" in "uset".
3181 * The result contains sets that live in the same spaces as the sets of "uset"
3182 * with space equal to the target space of "pma",
3183 * except that the space has been replaced by the domain space of "pma".
3185 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3186 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3188 return preimage_pw_multi_aff(uset, pma, &set_match,
3189 &isl_set_preimage_pw_multi_aff);
3192 /* Compute the preimage of the domain of "umap" under the function
3193 * represented by "ma".
3194 * In other words, plug in "ma" in the domain of "umap".
3195 * The result contains maps that live in the same spaces as the maps of "umap"
3196 * with domain space equal to the target space of "ma",
3197 * except that the domain has been replaced by the domain space of "ma".
3199 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3200 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3202 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3203 isl_pw_multi_aff_from_multi_aff(ma));
3206 /* Compute the preimage of the range of "umap" under the function
3207 * represented by "ma".
3208 * In other words, plug in "ma" in the range of "umap".
3209 * The result contains maps that live in the same spaces as the maps of "umap"
3210 * with range space equal to the target space of "ma",
3211 * except that the range has been replaced by the domain space of "ma".
3213 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3214 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3216 return isl_union_map_preimage_range_pw_multi_aff(umap,
3217 isl_pw_multi_aff_from_multi_aff(ma));
3220 /* Compute the preimage of "uset" under the function represented by "ma".
3221 * In other words, plug in "ma" in "uset".
3222 * The result contains sets that live in the same spaces as the sets of "uset"
3223 * with space equal to the target space of "ma",
3224 * except that the space has been replaced by the domain space of "ma".
3226 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3227 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3229 return isl_union_set_preimage_pw_multi_aff(uset,
3230 isl_pw_multi_aff_from_multi_aff(ma));
3233 /* Internal data structure for preimage_multi_pw_aff.
3235 * "mpa" is the function under which the preimage should be taken.
3236 * "space" is the space of "mpa".
3237 * "res" collects the results.
3238 * "fn" computes the preimage for a given map.
3239 * "match" returns true if "fn" can be called.
3241 struct isl_union_map_preimage_mpa_data {
3242 isl_space *space;
3243 isl_multi_pw_aff *mpa;
3244 isl_union_map *res;
3245 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3246 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3247 __isl_take isl_multi_pw_aff *mpa);
3250 /* Call data->fn to compute the preimage of the domain or range of *entry
3251 * under the function represented by data->mpa, provided the domain/range
3252 * space of *entry matches the target space of data->mpa
3253 * (as given by data->match), and add the result to data->res.
3255 static isl_stat preimage_mpa_entry(void **entry, void *user)
3257 int m;
3258 isl_map *map = *entry;
3259 struct isl_union_map_preimage_mpa_data *data = user;
3260 isl_bool empty;
3262 m = data->match(map, data->space);
3263 if (m < 0)
3264 return isl_stat_error;
3265 if (!m)
3266 return isl_stat_ok;
3268 map = isl_map_copy(map);
3269 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3271 empty = isl_map_is_empty(map);
3272 if (empty < 0 || empty) {
3273 isl_map_free(map);
3274 return empty < 0 ? isl_stat_error : isl_stat_ok;
3277 data->res = isl_union_map_add_map(data->res, map);
3279 return isl_stat_ok;
3282 /* Compute the preimage of the domain or range of "umap" under the function
3283 * represented by "mpa".
3284 * In other words, plug in "mpa" in the domain or range of "umap".
3285 * The function "fn" performs the actual preimage computation on a map,
3286 * while "match" determines to which maps the function should be applied.
3288 static __isl_give isl_union_map *preimage_multi_pw_aff(
3289 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3290 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3291 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3292 __isl_take isl_multi_pw_aff *mpa))
3294 isl_ctx *ctx;
3295 isl_space *space;
3296 struct isl_union_map_preimage_mpa_data data;
3298 umap = isl_union_map_align_params(umap,
3299 isl_multi_pw_aff_get_space(mpa));
3300 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3302 if (!umap || !mpa)
3303 goto error;
3305 ctx = isl_union_map_get_ctx(umap);
3306 space = isl_union_map_get_space(umap);
3307 data.space = isl_multi_pw_aff_get_space(mpa);
3308 data.mpa = mpa;
3309 data.res = isl_union_map_alloc(space, umap->table.n);
3310 data.match = match;
3311 data.fn = fn;
3312 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3313 &data) < 0)
3314 data.res = isl_union_map_free(data.res);
3316 isl_space_free(data.space);
3317 isl_union_map_free(umap);
3318 isl_multi_pw_aff_free(mpa);
3319 return data.res;
3320 error:
3321 isl_union_map_free(umap);
3322 isl_multi_pw_aff_free(mpa);
3323 return NULL;
3326 /* Compute the preimage of the domain of "umap" under the function
3327 * represented by "mpa".
3328 * In other words, plug in "mpa" in the domain of "umap".
3329 * The result contains maps that live in the same spaces as the maps of "umap"
3330 * with domain space equal to the target space of "mpa",
3331 * except that the domain has been replaced by the domain space of "mpa".
3333 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3334 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3336 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3337 &isl_map_preimage_domain_multi_pw_aff);
3340 /* Internal data structure for preimage_upma.
3342 * "umap" is the map of which the preimage should be computed.
3343 * "res" collects the results.
3344 * "fn" computes the preimage for a given piecewise multi-affine function.
3346 struct isl_union_map_preimage_upma_data {
3347 isl_union_map *umap;
3348 isl_union_map *res;
3349 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3350 __isl_take isl_pw_multi_aff *pma);
3353 /* Call data->fn to compute the preimage of the domain or range of data->umap
3354 * under the function represented by pma and add the result to data->res.
3356 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3358 struct isl_union_map_preimage_upma_data *data = user;
3359 isl_union_map *umap;
3361 umap = isl_union_map_copy(data->umap);
3362 umap = data->fn(umap, pma);
3363 data->res = isl_union_map_union(data->res, umap);
3365 return data->res ? isl_stat_ok : isl_stat_error;
3368 /* Compute the preimage of the domain or range of "umap" under the function
3369 * represented by "upma".
3370 * In other words, plug in "upma" in the domain or range of "umap".
3371 * The function "fn" performs the actual preimage computation
3372 * on a piecewise multi-affine function.
3374 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3375 __isl_take isl_union_map *umap,
3376 __isl_take isl_union_pw_multi_aff *upma,
3377 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3378 __isl_take isl_pw_multi_aff *pma))
3380 struct isl_union_map_preimage_upma_data data;
3382 data.umap = umap;
3383 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3384 data.fn = fn;
3385 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3386 &preimage_upma, &data) < 0)
3387 data.res = isl_union_map_free(data.res);
3389 isl_union_map_free(umap);
3390 isl_union_pw_multi_aff_free(upma);
3392 return data.res;
3395 /* Compute the preimage of the domain of "umap" under the function
3396 * represented by "upma".
3397 * In other words, plug in "upma" in the domain of "umap".
3398 * The result contains maps that live in the same spaces as the maps of "umap"
3399 * with domain space equal to one of the target spaces of "upma",
3400 * except that the domain has been replaced by one of the the domain spaces that
3401 * corresponds to that target space of "upma".
3403 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3404 __isl_take isl_union_map *umap,
3405 __isl_take isl_union_pw_multi_aff *upma)
3407 return preimage_union_pw_multi_aff(umap, upma,
3408 &isl_union_map_preimage_domain_pw_multi_aff);
3411 /* Compute the preimage of the range of "umap" under the function
3412 * represented by "upma".
3413 * In other words, plug in "upma" in the range of "umap".
3414 * The result contains maps that live in the same spaces as the maps of "umap"
3415 * with range space equal to one of the target spaces of "upma",
3416 * except that the range has been replaced by one of the the domain spaces that
3417 * corresponds to that target space of "upma".
3419 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3420 __isl_take isl_union_map *umap,
3421 __isl_take isl_union_pw_multi_aff *upma)
3423 return preimage_union_pw_multi_aff(umap, upma,
3424 &isl_union_map_preimage_range_pw_multi_aff);
3427 /* Compute the preimage of "uset" under the function represented by "upma".
3428 * In other words, plug in "upma" in the range of "uset".
3429 * The result contains sets that live in the same spaces as the sets of "uset"
3430 * with space equal to one of the target spaces of "upma",
3431 * except that the space has been replaced by one of the the domain spaces that
3432 * corresponds to that target space of "upma".
3434 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3435 __isl_take isl_union_set *uset,
3436 __isl_take isl_union_pw_multi_aff *upma)
3438 return preimage_union_pw_multi_aff(uset, upma,
3439 &isl_union_set_preimage_pw_multi_aff);
3442 /* Reset the user pointer on all identifiers of parameters and tuples
3443 * of the spaces of "umap".
3445 __isl_give isl_union_map *isl_union_map_reset_user(
3446 __isl_take isl_union_map *umap)
3448 umap = isl_union_map_cow(umap);
3449 if (!umap)
3450 return NULL;
3451 umap->dim = isl_space_reset_user(umap->dim);
3452 if (!umap->dim)
3453 return isl_union_map_free(umap);
3454 return total(umap, &isl_map_reset_user);
3457 /* Reset the user pointer on all identifiers of parameters and tuples
3458 * of the spaces of "uset".
3460 __isl_give isl_union_set *isl_union_set_reset_user(
3461 __isl_take isl_union_set *uset)
3463 return isl_union_map_reset_user(uset);
3466 /* Internal data structure for isl_union_map_project_out.
3467 * "type", "first" and "n" are the arguments for the isl_map_project_out
3468 * call.
3469 * "res" collects the results.
3471 struct isl_union_map_project_out_data {
3472 enum isl_dim_type type;
3473 unsigned first;
3474 unsigned n;
3476 isl_union_map *res;
3479 /* Turn the data->n dimensions of type data->type, starting at data->first
3480 * into existentially quantified variables and add the result to data->res.
3482 static isl_stat project_out(__isl_take isl_map *map, void *user)
3484 struct isl_union_map_project_out_data *data = user;
3486 map = isl_map_project_out(map, data->type, data->first, data->n);
3487 data->res = isl_union_map_add_map(data->res, map);
3489 return isl_stat_ok;
3492 /* Turn the "n" dimensions of type "type", starting at "first"
3493 * into existentially quantified variables.
3494 * Since the space of an isl_union_map only contains parameters,
3495 * type is required to be equal to isl_dim_param.
3497 __isl_give isl_union_map *isl_union_map_project_out(
3498 __isl_take isl_union_map *umap,
3499 enum isl_dim_type type, unsigned first, unsigned n)
3501 isl_space *space;
3502 struct isl_union_map_project_out_data data = { type, first, n };
3504 if (!umap)
3505 return NULL;
3507 if (type != isl_dim_param)
3508 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3509 "can only project out parameters",
3510 return isl_union_map_free(umap));
3512 space = isl_union_map_get_space(umap);
3513 space = isl_space_drop_dims(space, type, first, n);
3514 data.res = isl_union_map_empty(space);
3515 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3516 data.res = isl_union_map_free(data.res);
3518 isl_union_map_free(umap);
3520 return data.res;
3523 /* Turn the "n" dimensions of type "type", starting at "first"
3524 * into existentially quantified variables.
3525 * Since the space of an isl_union_set only contains parameters,
3526 * "type" is required to be equal to isl_dim_param.
3528 __isl_give isl_union_set *isl_union_set_project_out(
3529 __isl_take isl_union_set *uset,
3530 enum isl_dim_type type, unsigned first, unsigned n)
3532 return isl_union_map_project_out(uset, type, first, n);
3535 /* Internal data structure for isl_union_map_involves_dims.
3536 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3538 struct isl_union_map_involves_dims_data {
3539 unsigned first;
3540 unsigned n;
3543 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3545 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3547 struct isl_union_map_involves_dims_data *data = user;
3548 isl_bool involves;
3550 involves = isl_map_involves_dims(map,
3551 isl_dim_param, data->first, data->n);
3552 if (involves < 0)
3553 return isl_bool_error;
3554 return !involves;
3557 /* Does "umap" involve any of the n parameters starting at first?
3558 * "type" is required to be set to isl_dim_param.
3560 * "umap" involves any of those parameters if any of its maps
3561 * involve the parameters. In other words, "umap" does not
3562 * involve any of the parameters if all its maps to not
3563 * involve the parameters.
3565 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3566 enum isl_dim_type type, unsigned first, unsigned n)
3568 struct isl_union_map_involves_dims_data data = { first, n };
3569 isl_bool excludes;
3571 if (type != isl_dim_param)
3572 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3573 "can only reference parameters", return isl_bool_error);
3575 excludes = union_map_forall_user(umap, &map_excludes, &data);
3577 if (excludes < 0)
3578 return isl_bool_error;
3580 return !excludes;
3583 /* Internal data structure for isl_union_map_reset_range_space.
3584 * "range" is the space from which to set the range space.
3585 * "res" collects the results.
3587 struct isl_union_map_reset_range_space_data {
3588 isl_space *range;
3589 isl_union_map *res;
3592 /* Replace the range space of "map" by the range space of data->range and
3593 * add the result to data->res.
3595 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3597 struct isl_union_map_reset_range_space_data *data = user;
3598 isl_space *space;
3600 space = isl_map_get_space(map);
3601 space = isl_space_domain(space);
3602 space = isl_space_extend_domain_with_range(space,
3603 isl_space_copy(data->range));
3604 map = isl_map_reset_space(map, space);
3605 data->res = isl_union_map_add_map(data->res, map);
3607 return data->res ? isl_stat_ok : isl_stat_error;
3610 /* Replace the range space of all the maps in "umap" by
3611 * the range space of "space".
3613 * This assumes that all maps have the same output dimension.
3614 * This function should therefore not be made publicly available.
3616 * Since the spaces of the maps change, so do their hash value.
3617 * We therefore need to create a new isl_union_map.
3619 __isl_give isl_union_map *isl_union_map_reset_range_space(
3620 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3622 struct isl_union_map_reset_range_space_data data = { space };
3624 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3625 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3626 data.res = isl_union_map_free(data.res);
3628 isl_space_free(space);
3629 isl_union_map_free(umap);
3630 return data.res;
3633 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3634 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3635 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3636 * to the domain and the range of each map.
3637 * "res" collects the results.
3639 struct isl_union_order_at_data {
3640 isl_multi_union_pw_aff *mupa;
3641 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3642 __isl_take isl_multi_pw_aff *mpa2);
3643 isl_union_map *res;
3646 /* Intersect "map" with the result of applying data->order to
3647 * the functions in data->mupa that apply to the domain and the range
3648 * of "map" and add the result to data->res.
3650 static isl_stat order_at(__isl_take isl_map *map, void *user)
3652 struct isl_union_order_at_data *data = user;
3653 isl_space *space;
3654 isl_multi_pw_aff *mpa1, *mpa2;
3655 isl_map *order;
3657 space = isl_space_domain(isl_map_get_space(map));
3658 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3659 space = isl_space_range(isl_map_get_space(map));
3660 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3661 order = data->order(mpa1, mpa2);
3662 map = isl_map_intersect(map, order);
3663 data->res = isl_union_map_add_map(data->res, map);
3665 return data->res ? isl_stat_ok : isl_stat_error;
3668 /* Intersect each map in "umap" with the result of calling "order"
3669 * on the functions is "mupa" that apply to the domain and the range
3670 * of the map.
3672 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3673 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3674 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3675 __isl_take isl_multi_pw_aff *mpa2))
3677 struct isl_union_order_at_data data;
3679 umap = isl_union_map_align_params(umap,
3680 isl_multi_union_pw_aff_get_space(mupa));
3681 mupa = isl_multi_union_pw_aff_align_params(mupa,
3682 isl_union_map_get_space(umap));
3683 data.mupa = mupa;
3684 data.order = order;
3685 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3686 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3687 data.res = isl_union_map_free(data.res);
3689 isl_multi_union_pw_aff_free(mupa);
3690 isl_union_map_free(umap);
3691 return data.res;
3694 /* Return the subset of "umap" where the domain and the range
3695 * have equal "mupa" values.
3697 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3698 __isl_take isl_union_map *umap,
3699 __isl_take isl_multi_union_pw_aff *mupa)
3701 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3702 &isl_multi_pw_aff_eq_map);
3705 /* Return the subset of "umap" where the domain has a lexicographically
3706 * smaller "mupa" value than the range.
3708 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3709 __isl_take isl_union_map *umap,
3710 __isl_take isl_multi_union_pw_aff *mupa)
3712 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3713 &isl_multi_pw_aff_lex_lt_map);
3716 /* Return the subset of "umap" where the domain has a lexicographically
3717 * greater "mupa" value than the range.
3719 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3720 __isl_take isl_union_map *umap,
3721 __isl_take isl_multi_union_pw_aff *mupa)
3723 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3724 &isl_multi_pw_aff_lex_gt_map);
3727 /* Return the union of the elements in the list "list".
3729 __isl_give isl_union_set *isl_union_set_list_union(
3730 __isl_take isl_union_set_list *list)
3732 int i, n;
3733 isl_ctx *ctx;
3734 isl_space *space;
3735 isl_union_set *res;
3737 if (!list)
3738 return NULL;
3740 ctx = isl_union_set_list_get_ctx(list);
3741 space = isl_space_params_alloc(ctx, 0);
3742 res = isl_union_set_empty(space);
3744 n = isl_union_set_list_n_union_set(list);
3745 for (i = 0; i < n; ++i) {
3746 isl_union_set *uset_i;
3748 uset_i = isl_union_set_list_get_union_set(list, i);
3749 res = isl_union_set_union(res, uset_i);
3752 isl_union_set_list_free(list);
3753 return res;
3756 /* Update *hash with the hash value of "map".
3758 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3760 uint32_t *hash = user;
3761 uint32_t map_hash;
3763 map_hash = isl_map_get_hash(map);
3764 isl_hash_hash(*hash, map_hash);
3766 isl_map_free(map);
3767 return isl_stat_ok;
3770 /* Return a hash value that digests "umap".
3772 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3774 uint32_t hash;
3776 if (!umap)
3777 return 0;
3779 hash = isl_hash_init();
3780 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3781 return 0;
3783 return hash;
3786 /* Return a hash value that digests "uset".
3788 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3790 return isl_union_map_get_hash(uset);