isl_schedule_read.c: extract_key: store key strings in array
[isl.git] / isl_union_map.c
blob6a6976d6dd853884aac632f9528c512a1c33a76b
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 /* For each map in "umap", remove the constraints in the corresponding map
925 * of "context".
926 * Each map in "context" is assumed to consist of a single disjunct and
927 * to have explicit representations for all local variables.
929 __isl_give isl_union_map *isl_union_map_plain_gist(
930 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
932 return match_bin_op(umap, context, &isl_map_plain_gist);
935 /* For each set in "uset", remove the constraints in the corresponding set
936 * of "context".
937 * Each set in "context" is assumed to consist of a single disjunct and
938 * to have explicit representations for all local variables.
940 __isl_give isl_union_set *isl_union_set_plain_gist(
941 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
943 return isl_union_map_plain_gist(uset, context);
946 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
947 __isl_take isl_map *set2)
949 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
952 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
953 __isl_take isl_map *set2)
955 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
958 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
959 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
961 return match_bin_op(uset1, uset2, &lex_lt_set);
964 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
965 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
967 return match_bin_op(uset1, uset2, &lex_le_set);
970 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
971 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
973 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
976 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
977 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
979 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
982 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
983 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
985 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
988 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
989 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
991 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
994 static isl_stat intersect_domain_entry(void **entry, void *user)
996 struct isl_union_map_gen_bin_data *data = user;
997 uint32_t hash;
998 struct isl_hash_table_entry *entry2;
999 isl_space *dim;
1000 isl_map *map = *entry;
1001 isl_bool empty;
1003 dim = isl_map_get_space(map);
1004 dim = isl_space_domain(dim);
1005 hash = isl_space_get_hash(dim);
1006 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1007 hash, &has_dim, dim, 0);
1008 isl_space_free(dim);
1009 if (!entry2)
1010 return isl_stat_ok;
1012 map = isl_map_copy(map);
1013 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
1015 empty = isl_map_is_empty(map);
1016 if (empty < 0) {
1017 isl_map_free(map);
1018 return isl_stat_error;
1020 if (empty) {
1021 isl_map_free(map);
1022 return isl_stat_ok;
1025 data->res = isl_union_map_add_map(data->res, map);
1027 return isl_stat_ok;
1030 /* Intersect the domain of "umap" with "uset".
1031 * If "uset" is a parameters domain, then intersect the parameter
1032 * domain of "umap" with this set.
1034 __isl_give isl_union_map *isl_union_map_intersect_domain(
1035 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1037 if (isl_union_set_is_params(uset))
1038 return union_map_intersect_params(umap, uset);
1039 return gen_bin_op(umap, uset, &intersect_domain_entry);
1042 /* Remove the elements of data->umap2 from the domain of *entry
1043 * and add the result to data->res.
1045 static isl_stat subtract_domain_entry(void **entry, void *user)
1047 struct isl_union_map_gen_bin_data *data = user;
1048 uint32_t hash;
1049 struct isl_hash_table_entry *entry2;
1050 isl_space *dim;
1051 isl_map *map = *entry;
1052 isl_bool empty;
1054 dim = isl_map_get_space(map);
1055 dim = isl_space_domain(dim);
1056 hash = isl_space_get_hash(dim);
1057 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1058 hash, &has_dim, dim, 0);
1059 isl_space_free(dim);
1061 map = isl_map_copy(map);
1063 if (!entry2) {
1064 data->res = isl_union_map_add_map(data->res, map);
1065 return isl_stat_ok;
1068 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1070 empty = isl_map_is_empty(map);
1071 if (empty < 0) {
1072 isl_map_free(map);
1073 return isl_stat_error;
1075 if (empty) {
1076 isl_map_free(map);
1077 return isl_stat_ok;
1080 data->res = isl_union_map_add_map(data->res, map);
1082 return isl_stat_ok;
1085 /* Remove the elements of "uset" from the domain of "umap".
1087 __isl_give isl_union_map *isl_union_map_subtract_domain(
1088 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1090 return gen_bin_op(umap, dom, &subtract_domain_entry);
1093 /* Remove the elements of data->umap2 from the range of *entry
1094 * and add the result to data->res.
1096 static isl_stat subtract_range_entry(void **entry, void *user)
1098 struct isl_union_map_gen_bin_data *data = user;
1099 uint32_t hash;
1100 struct isl_hash_table_entry *entry2;
1101 isl_space *space;
1102 isl_map *map = *entry;
1103 isl_bool empty;
1105 space = isl_map_get_space(map);
1106 space = isl_space_range(space);
1107 hash = isl_space_get_hash(space);
1108 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1109 hash, &has_dim, space, 0);
1110 isl_space_free(space);
1112 map = isl_map_copy(map);
1114 if (!entry2) {
1115 data->res = isl_union_map_add_map(data->res, map);
1116 return isl_stat_ok;
1119 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1121 empty = isl_map_is_empty(map);
1122 if (empty < 0) {
1123 isl_map_free(map);
1124 return isl_stat_error;
1126 if (empty) {
1127 isl_map_free(map);
1128 return isl_stat_ok;
1131 data->res = isl_union_map_add_map(data->res, map);
1133 return isl_stat_ok;
1136 /* Remove the elements of "uset" from the range of "umap".
1138 __isl_give isl_union_map *isl_union_map_subtract_range(
1139 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1141 return gen_bin_op(umap, dom, &subtract_range_entry);
1144 static isl_stat gist_domain_entry(void **entry, void *user)
1146 struct isl_union_map_gen_bin_data *data = user;
1147 uint32_t hash;
1148 struct isl_hash_table_entry *entry2;
1149 isl_space *dim;
1150 isl_map *map = *entry;
1151 isl_bool empty;
1153 dim = isl_map_get_space(map);
1154 dim = isl_space_domain(dim);
1155 hash = isl_space_get_hash(dim);
1156 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1157 hash, &has_dim, dim, 0);
1158 isl_space_free(dim);
1159 if (!entry2)
1160 return isl_stat_ok;
1162 map = isl_map_copy(map);
1163 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1165 empty = isl_map_is_empty(map);
1166 if (empty < 0) {
1167 isl_map_free(map);
1168 return isl_stat_error;
1171 data->res = isl_union_map_add_map(data->res, map);
1173 return isl_stat_ok;
1176 /* Compute the gist of "umap" with respect to the domain "uset".
1177 * If "uset" is a parameters domain, then compute the gist
1178 * with respect to this parameter domain.
1180 __isl_give isl_union_map *isl_union_map_gist_domain(
1181 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1183 if (isl_union_set_is_params(uset))
1184 return union_map_gist_params(umap, uset);
1185 return gen_bin_op(umap, uset, &gist_domain_entry);
1188 static isl_stat gist_range_entry(void **entry, void *user)
1190 struct isl_union_map_gen_bin_data *data = user;
1191 uint32_t hash;
1192 struct isl_hash_table_entry *entry2;
1193 isl_space *space;
1194 isl_map *map = *entry;
1195 isl_bool empty;
1197 space = isl_map_get_space(map);
1198 space = isl_space_range(space);
1199 hash = isl_space_get_hash(space);
1200 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1201 hash, &has_dim, space, 0);
1202 isl_space_free(space);
1203 if (!entry2)
1204 return isl_stat_ok;
1206 map = isl_map_copy(map);
1207 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1209 empty = isl_map_is_empty(map);
1210 if (empty < 0) {
1211 isl_map_free(map);
1212 return isl_stat_error;
1215 data->res = isl_union_map_add_map(data->res, map);
1217 return isl_stat_ok;
1220 /* Compute the gist of "umap" with respect to the range "uset".
1222 __isl_give isl_union_map *isl_union_map_gist_range(
1223 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1225 return gen_bin_op(umap, uset, &gist_range_entry);
1228 static isl_stat intersect_range_entry(void **entry, void *user)
1230 struct isl_union_map_gen_bin_data *data = user;
1231 uint32_t hash;
1232 struct isl_hash_table_entry *entry2;
1233 isl_space *dim;
1234 isl_map *map = *entry;
1235 isl_bool empty;
1237 dim = isl_map_get_space(map);
1238 dim = isl_space_range(dim);
1239 hash = isl_space_get_hash(dim);
1240 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1241 hash, &has_dim, dim, 0);
1242 isl_space_free(dim);
1243 if (!entry2)
1244 return isl_stat_ok;
1246 map = isl_map_copy(map);
1247 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1249 empty = isl_map_is_empty(map);
1250 if (empty < 0) {
1251 isl_map_free(map);
1252 return isl_stat_error;
1254 if (empty) {
1255 isl_map_free(map);
1256 return isl_stat_ok;
1259 data->res = isl_union_map_add_map(data->res, map);
1261 return isl_stat_ok;
1264 __isl_give isl_union_map *isl_union_map_intersect_range(
1265 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1267 return gen_bin_op(umap, uset, &intersect_range_entry);
1270 struct isl_union_map_bin_data {
1271 isl_union_map *umap2;
1272 isl_union_map *res;
1273 isl_map *map;
1274 isl_stat (*fn)(void **entry, void *user);
1277 static isl_stat apply_range_entry(void **entry, void *user)
1279 struct isl_union_map_bin_data *data = user;
1280 isl_map *map2 = *entry;
1281 isl_bool empty;
1283 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1284 map2->dim, isl_dim_in))
1285 return isl_stat_ok;
1287 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1289 empty = isl_map_is_empty(map2);
1290 if (empty < 0) {
1291 isl_map_free(map2);
1292 return isl_stat_error;
1294 if (empty) {
1295 isl_map_free(map2);
1296 return isl_stat_ok;
1299 data->res = isl_union_map_add_map(data->res, map2);
1301 return isl_stat_ok;
1304 static isl_stat bin_entry(void **entry, void *user)
1306 struct isl_union_map_bin_data *data = user;
1307 isl_map *map = *entry;
1309 data->map = map;
1310 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1311 data->fn, data) < 0)
1312 return isl_stat_error;
1314 return isl_stat_ok;
1317 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1318 __isl_take isl_union_map *umap2,
1319 isl_stat (*fn)(void **entry, void *user))
1321 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1323 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1324 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1326 if (!umap1 || !umap2)
1327 goto error;
1329 data.umap2 = umap2;
1330 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1331 umap1->table.n);
1332 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1333 &bin_entry, &data) < 0)
1334 goto error;
1336 isl_union_map_free(umap1);
1337 isl_union_map_free(umap2);
1338 return data.res;
1339 error:
1340 isl_union_map_free(umap1);
1341 isl_union_map_free(umap2);
1342 isl_union_map_free(data.res);
1343 return NULL;
1346 __isl_give isl_union_map *isl_union_map_apply_range(
1347 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1349 return bin_op(umap1, umap2, &apply_range_entry);
1352 __isl_give isl_union_map *isl_union_map_apply_domain(
1353 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1355 umap1 = isl_union_map_reverse(umap1);
1356 umap1 = isl_union_map_apply_range(umap1, umap2);
1357 return isl_union_map_reverse(umap1);
1360 __isl_give isl_union_set *isl_union_set_apply(
1361 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1363 return isl_union_map_apply_range(uset, umap);
1366 static isl_stat map_lex_lt_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_lt_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_lt_union_map(
1383 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1385 return bin_op(umap1, umap2, &map_lex_lt_entry);
1388 static isl_stat map_lex_le_entry(void **entry, void *user)
1390 struct isl_union_map_bin_data *data = user;
1391 isl_map *map2 = *entry;
1393 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1394 map2->dim, isl_dim_out))
1395 return isl_stat_ok;
1397 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1399 data->res = isl_union_map_add_map(data->res, map2);
1401 return isl_stat_ok;
1404 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1405 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1407 return bin_op(umap1, umap2, &map_lex_le_entry);
1410 static isl_stat product_entry(void **entry, void *user)
1412 struct isl_union_map_bin_data *data = user;
1413 isl_map *map2 = *entry;
1415 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1417 data->res = isl_union_map_add_map(data->res, map2);
1419 return isl_stat_ok;
1422 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1423 __isl_take isl_union_map *umap2)
1425 return bin_op(umap1, umap2, &product_entry);
1428 static isl_stat set_product_entry(void **entry, void *user)
1430 struct isl_union_map_bin_data *data = user;
1431 isl_set *set2 = *entry;
1433 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1435 data->res = isl_union_set_add_set(data->res, set2);
1437 return isl_stat_ok;
1440 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1441 __isl_take isl_union_set *uset2)
1443 return bin_op(uset1, uset2, &set_product_entry);
1446 static isl_stat domain_product_entry(void **entry, void *user)
1448 struct isl_union_map_bin_data *data = user;
1449 isl_map *map2 = *entry;
1451 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1452 map2->dim, isl_dim_out))
1453 return isl_stat_ok;
1455 map2 = isl_map_domain_product(isl_map_copy(data->map),
1456 isl_map_copy(map2));
1458 data->res = isl_union_map_add_map(data->res, map2);
1460 return isl_stat_ok;
1463 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1465 __isl_give isl_union_map *isl_union_map_domain_product(
1466 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1468 return bin_op(umap1, umap2, &domain_product_entry);
1471 static isl_stat range_product_entry(void **entry, void *user)
1473 struct isl_union_map_bin_data *data = user;
1474 isl_map *map2 = *entry;
1476 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1477 map2->dim, isl_dim_in))
1478 return isl_stat_ok;
1480 map2 = isl_map_range_product(isl_map_copy(data->map),
1481 isl_map_copy(map2));
1483 data->res = isl_union_map_add_map(data->res, map2);
1485 return isl_stat_ok;
1488 __isl_give isl_union_map *isl_union_map_range_product(
1489 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1491 return bin_op(umap1, umap2, &range_product_entry);
1494 /* If data->map A -> B and "map2" C -> D have the same range space,
1495 * then add (A, C) -> (B * D) to data->res.
1497 static isl_stat flat_domain_product_entry(void **entry, void *user)
1499 struct isl_union_map_bin_data *data = user;
1500 isl_map *map2 = *entry;
1502 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1503 map2->dim, isl_dim_out))
1504 return isl_stat_ok;
1506 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1507 isl_map_copy(map2));
1509 data->res = isl_union_map_add_map(data->res, map2);
1511 return isl_stat_ok;
1514 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1516 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1517 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1519 return bin_op(umap1, umap2, &flat_domain_product_entry);
1522 static isl_stat flat_range_product_entry(void **entry, void *user)
1524 struct isl_union_map_bin_data *data = user;
1525 isl_map *map2 = *entry;
1527 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1528 map2->dim, isl_dim_in))
1529 return isl_stat_ok;
1531 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1532 isl_map_copy(map2));
1534 data->res = isl_union_map_add_map(data->res, map2);
1536 return isl_stat_ok;
1539 __isl_give isl_union_map *isl_union_map_flat_range_product(
1540 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1542 return bin_op(umap1, umap2, &flat_range_product_entry);
1545 /* Data structure that specifies how un_op should modify
1546 * the maps in the union map.
1548 * If "inplace" is set, then the maps in the input union map
1549 * are modified in place. This means that "fn_map" should not
1550 * change the meaning of the map or that the union map only
1551 * has a single reference.
1552 * If "total" is set, then all maps need to be modified and
1553 * the results need to live in the same space.
1554 * Otherwise, a new union map is constructed to store the results.
1555 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1556 * are taken into account. No filter can be set if "inplace" or
1557 * "total" is set.
1558 * "fn_map" specifies how the maps (selected by "filter")
1559 * should be transformed.
1561 struct isl_un_op_control {
1562 int inplace;
1563 int total;
1564 isl_bool (*filter)(__isl_keep isl_map *map);
1565 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1568 /* Internal data structure for "un_op".
1569 * "control" specifies how the maps in the union map should be modified.
1570 * "res" collects the results.
1572 struct isl_union_map_un_data {
1573 struct isl_un_op_control *control;
1574 isl_union_map *res;
1577 /* isl_hash_table_foreach callback for un_op.
1578 * Handle the map that "entry" points to.
1580 * If control->filter is set, then check if this map satisfies the filter.
1581 * If so (or if control->filter is not set), modify the map
1582 * by calling control->fn_map and either add the result to data->res or
1583 * replace the original entry by the result (if control->inplace is set).
1585 static isl_stat un_entry(void **entry, void *user)
1587 struct isl_union_map_un_data *data = user;
1588 isl_map *map = *entry;
1590 if (data->control->filter) {
1591 isl_bool ok;
1593 ok = data->control->filter(map);
1594 if (ok < 0)
1595 return isl_stat_error;
1596 if (!ok)
1597 return isl_stat_ok;
1600 map = data->control->fn_map(isl_map_copy(map));
1601 if (!map)
1602 return isl_stat_error;
1603 if (data->control->inplace) {
1604 isl_map_free(*entry);
1605 *entry = map;
1606 } else {
1607 data->res = isl_union_map_add_map(data->res, map);
1608 if (!data->res)
1609 return isl_stat_error;
1612 return isl_stat_ok;
1615 /* Modify the maps in "umap" based on "control".
1616 * If control->inplace is set, then modify the maps in "umap" in-place.
1617 * Otherwise, create a new union map to hold the results.
1618 * If control->total is set, then perform an inplace computation
1619 * if "umap" is only referenced once. Otherwise, create a new union map
1620 * to store the results.
1622 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1623 struct isl_un_op_control *control)
1625 struct isl_union_map_un_data data = { control };
1627 if (!umap)
1628 return NULL;
1629 if ((control->inplace || control->total) && control->filter)
1630 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1631 "inplace/total modification cannot be filtered",
1632 return isl_union_map_free(umap));
1634 if (control->total && umap->ref == 1)
1635 control->inplace = 1;
1636 if (control->inplace) {
1637 data.res = umap;
1638 } else {
1639 isl_space *space;
1641 space = isl_union_map_get_space(umap);
1642 data.res = isl_union_map_alloc(space, umap->table.n);
1644 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1645 &umap->table, &un_entry, &data) < 0)
1646 data.res = isl_union_map_free(data.res);
1648 if (control->inplace)
1649 return data.res;
1650 isl_union_map_free(umap);
1651 return data.res;
1654 __isl_give isl_union_map *isl_union_map_from_range(
1655 __isl_take isl_union_set *uset)
1657 struct isl_un_op_control control = {
1658 .fn_map = &isl_map_from_range,
1660 return un_op(uset, &control);
1663 __isl_give isl_union_map *isl_union_map_from_domain(
1664 __isl_take isl_union_set *uset)
1666 return isl_union_map_reverse(isl_union_map_from_range(uset));
1669 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1670 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1672 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1673 isl_union_map_from_range(range));
1676 /* Modify the maps in "umap" by applying "fn" on them.
1677 * "fn" should apply to all maps in "umap" and should not modify the space.
1679 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1680 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1682 struct isl_un_op_control control = {
1683 .total = 1,
1684 .fn_map = fn,
1687 return un_op(umap, &control);
1690 /* Compute the affine hull of "map" and return the result as an isl_map.
1692 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1694 return isl_map_from_basic_map(isl_map_affine_hull(map));
1697 __isl_give isl_union_map *isl_union_map_affine_hull(
1698 __isl_take isl_union_map *umap)
1700 return total(umap, &isl_map_affine_hull_map);
1703 __isl_give isl_union_set *isl_union_set_affine_hull(
1704 __isl_take isl_union_set *uset)
1706 return isl_union_map_affine_hull(uset);
1709 /* Wrapper around isl_set_combined_lineality_space
1710 * that returns the combined lineality space in the form of an isl_set
1711 * instead of an isl_basic_set.
1713 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1715 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1718 /* For each set in "uset", compute the (linear) hull
1719 * of the lineality spaces of its basic sets and
1720 * collect and return the results.
1722 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1723 __isl_take isl_union_set *uset)
1725 struct isl_un_op_control control = {
1726 .fn_map = &combined_lineality_space,
1728 return un_op(uset, &control);
1731 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1733 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1735 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1738 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1739 __isl_take isl_union_map *umap)
1741 return total(umap, &isl_map_polyhedral_hull_map);
1744 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1745 __isl_take isl_union_set *uset)
1747 return isl_union_map_polyhedral_hull(uset);
1750 /* Compute a superset of the convex hull of "map" that is described
1751 * by only translates of the constraints in the constituents of "map" and
1752 * return the result as an isl_map.
1754 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1756 return isl_map_from_basic_map(isl_map_simple_hull(map));
1759 __isl_give isl_union_map *isl_union_map_simple_hull(
1760 __isl_take isl_union_map *umap)
1762 return total(umap, &isl_map_simple_hull_map);
1765 __isl_give isl_union_set *isl_union_set_simple_hull(
1766 __isl_take isl_union_set *uset)
1768 return isl_union_map_simple_hull(uset);
1771 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1772 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1774 struct isl_un_op_control control = {
1775 .inplace = 1,
1776 .fn_map = fn,
1779 return un_op(umap, &control);
1782 /* Remove redundant constraints in each of the basic maps of "umap".
1783 * Since removing redundant constraints does not change the meaning
1784 * or the space, the operation can be performed in-place.
1786 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1787 __isl_take isl_union_map *umap)
1789 return inplace(umap, &isl_map_remove_redundancies);
1792 /* Remove redundant constraints in each of the basic sets of "uset".
1794 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1795 __isl_take isl_union_set *uset)
1797 return isl_union_map_remove_redundancies(uset);
1800 __isl_give isl_union_map *isl_union_map_coalesce(
1801 __isl_take isl_union_map *umap)
1803 return inplace(umap, &isl_map_coalesce);
1806 __isl_give isl_union_set *isl_union_set_coalesce(
1807 __isl_take isl_union_set *uset)
1809 return isl_union_map_coalesce(uset);
1812 __isl_give isl_union_map *isl_union_map_detect_equalities(
1813 __isl_take isl_union_map *umap)
1815 return inplace(umap, &isl_map_detect_equalities);
1818 __isl_give isl_union_set *isl_union_set_detect_equalities(
1819 __isl_take isl_union_set *uset)
1821 return isl_union_map_detect_equalities(uset);
1824 __isl_give isl_union_map *isl_union_map_compute_divs(
1825 __isl_take isl_union_map *umap)
1827 return inplace(umap, &isl_map_compute_divs);
1830 __isl_give isl_union_set *isl_union_set_compute_divs(
1831 __isl_take isl_union_set *uset)
1833 return isl_union_map_compute_divs(uset);
1836 __isl_give isl_union_map *isl_union_map_lexmin(
1837 __isl_take isl_union_map *umap)
1839 return total(umap, &isl_map_lexmin);
1842 __isl_give isl_union_set *isl_union_set_lexmin(
1843 __isl_take isl_union_set *uset)
1845 return isl_union_map_lexmin(uset);
1848 __isl_give isl_union_map *isl_union_map_lexmax(
1849 __isl_take isl_union_map *umap)
1851 return total(umap, &isl_map_lexmax);
1854 __isl_give isl_union_set *isl_union_set_lexmax(
1855 __isl_take isl_union_set *uset)
1857 return isl_union_map_lexmax(uset);
1860 /* Return the universe in the space of "map".
1862 static __isl_give isl_map *universe(__isl_take isl_map *map)
1864 isl_space *space;
1866 space = isl_map_get_space(map);
1867 isl_map_free(map);
1868 return isl_map_universe(space);
1871 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1873 struct isl_un_op_control control = {
1874 .fn_map = &universe,
1876 return un_op(umap, &control);
1879 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1881 return isl_union_map_universe(uset);
1884 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1886 struct isl_un_op_control control = {
1887 .fn_map = &isl_map_reverse,
1889 return un_op(umap, &control);
1892 /* Compute the parameter domain of the given union map.
1894 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1896 struct isl_un_op_control control = {
1897 .fn_map = &isl_map_params,
1899 int empty;
1901 empty = isl_union_map_is_empty(umap);
1902 if (empty < 0)
1903 goto error;
1904 if (empty) {
1905 isl_space *space;
1906 space = isl_union_map_get_space(umap);
1907 isl_union_map_free(umap);
1908 return isl_set_empty(space);
1910 return isl_set_from_union_set(un_op(umap, &control));
1911 error:
1912 isl_union_map_free(umap);
1913 return NULL;
1916 /* Compute the parameter domain of the given union set.
1918 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1920 return isl_union_map_params(uset);
1923 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1925 struct isl_un_op_control control = {
1926 .fn_map = &isl_map_domain,
1928 return un_op(umap, &control);
1931 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1933 struct isl_un_op_control control = {
1934 .fn_map = &isl_map_range,
1936 return un_op(umap, &control);
1939 __isl_give isl_union_map *isl_union_map_domain_map(
1940 __isl_take isl_union_map *umap)
1942 struct isl_un_op_control control = {
1943 .fn_map = &isl_map_domain_map,
1945 return un_op(umap, &control);
1948 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1949 * add the result to "res".
1951 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1953 isl_union_pw_multi_aff **res = user;
1954 isl_multi_aff *ma;
1955 isl_pw_multi_aff *pma;
1957 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1958 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1959 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1961 return *res ? isl_stat_ok : isl_stat_error;
1965 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1966 * to its domain.
1968 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1969 __isl_take isl_union_map *umap)
1971 isl_union_pw_multi_aff *res;
1973 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1974 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1975 res = isl_union_pw_multi_aff_free(res);
1977 isl_union_map_free(umap);
1978 return res;
1981 __isl_give isl_union_map *isl_union_map_range_map(
1982 __isl_take isl_union_map *umap)
1984 struct isl_un_op_control control = {
1985 .fn_map = &isl_map_range_map,
1987 return un_op(umap, &control);
1990 /* Given a collection of wrapped maps of the form A[B -> C],
1991 * return the collection of maps A[B -> C] -> B.
1993 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1994 __isl_take isl_union_set *uset)
1996 struct isl_un_op_control control = {
1997 .filter = &isl_set_is_wrapping,
1998 .fn_map = &isl_set_wrapped_domain_map,
2000 return un_op(uset, &control);
2003 /* Does "map" relate elements from the same space?
2005 static isl_bool equal_tuples(__isl_keep isl_map *map)
2007 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2008 map->dim, isl_dim_out);
2011 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2013 struct isl_un_op_control control = {
2014 .filter = &equal_tuples,
2015 .fn_map = &isl_map_deltas,
2017 return un_op(umap, &control);
2020 __isl_give isl_union_map *isl_union_map_deltas_map(
2021 __isl_take isl_union_map *umap)
2023 struct isl_un_op_control control = {
2024 .filter = &equal_tuples,
2025 .fn_map = &isl_map_deltas_map,
2027 return un_op(umap, &control);
2030 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2032 struct isl_un_op_control control = {
2033 .fn_map = &isl_set_identity,
2035 return un_op(uset, &control);
2038 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2040 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2042 isl_union_pw_multi_aff **res = user;
2043 isl_space *space;
2044 isl_pw_multi_aff *pma;
2046 space = isl_space_map_from_set(isl_set_get_space(set));
2047 pma = isl_pw_multi_aff_identity(space);
2048 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2049 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2051 return *res ? isl_stat_ok : isl_stat_error;
2054 /* Return an identity function on "uset" in the form
2055 * of an isl_union_pw_multi_aff.
2057 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2058 __isl_take isl_union_set *uset)
2060 isl_union_pw_multi_aff *res;
2062 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2063 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2064 res = isl_union_pw_multi_aff_free(res);
2066 isl_union_set_free(uset);
2067 return res;
2070 /* For each map in "umap" of the form [A -> B] -> C,
2071 * construct the map A -> C and collect the results.
2073 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2074 __isl_take isl_union_map *umap)
2076 struct isl_un_op_control control = {
2077 .filter = &isl_map_domain_is_wrapping,
2078 .fn_map = &isl_map_domain_factor_domain,
2080 return un_op(umap, &control);
2083 /* For each map in "umap" of the form [A -> B] -> C,
2084 * construct the map B -> C and collect the results.
2086 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2087 __isl_take isl_union_map *umap)
2089 struct isl_un_op_control control = {
2090 .filter = &isl_map_domain_is_wrapping,
2091 .fn_map = &isl_map_domain_factor_range,
2093 return un_op(umap, &control);
2096 /* For each map in "umap" of the form A -> [B -> C],
2097 * construct the map A -> B and collect the results.
2099 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2100 __isl_take isl_union_map *umap)
2102 struct isl_un_op_control control = {
2103 .filter = &isl_map_range_is_wrapping,
2104 .fn_map = &isl_map_range_factor_domain,
2106 return un_op(umap, &control);
2109 /* For each map in "umap" of the form A -> [B -> C],
2110 * construct the map A -> C and collect the results.
2112 __isl_give isl_union_map *isl_union_map_range_factor_range(
2113 __isl_take isl_union_map *umap)
2115 struct isl_un_op_control control = {
2116 .filter = &isl_map_range_is_wrapping,
2117 .fn_map = &isl_map_range_factor_range,
2119 return un_op(umap, &control);
2122 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2123 * construct the map A -> C and collect the results.
2125 __isl_give isl_union_map *isl_union_map_factor_domain(
2126 __isl_take isl_union_map *umap)
2128 struct isl_un_op_control control = {
2129 .filter = &isl_map_is_product,
2130 .fn_map = &isl_map_factor_domain,
2132 return un_op(umap, &control);
2135 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2136 * construct the map B -> D and collect the results.
2138 __isl_give isl_union_map *isl_union_map_factor_range(
2139 __isl_take isl_union_map *umap)
2141 struct isl_un_op_control control = {
2142 .filter = &isl_map_is_product,
2143 .fn_map = &isl_map_factor_range,
2145 return un_op(umap, &control);
2148 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2150 struct isl_un_op_control control = {
2151 .filter = &isl_set_is_wrapping,
2152 .fn_map = &isl_set_unwrap,
2154 return un_op(uset, &control);
2157 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2159 struct isl_un_op_control control = {
2160 .fn_map = &isl_map_wrap,
2162 return un_op(umap, &control);
2165 struct isl_union_map_is_subset_data {
2166 isl_union_map *umap2;
2167 isl_bool is_subset;
2170 static isl_stat is_subset_entry(void **entry, void *user)
2172 struct isl_union_map_is_subset_data *data = user;
2173 uint32_t hash;
2174 struct isl_hash_table_entry *entry2;
2175 isl_map *map = *entry;
2177 hash = isl_space_get_hash(map->dim);
2178 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2179 hash, &has_dim, map->dim, 0);
2180 if (!entry2) {
2181 int empty = isl_map_is_empty(map);
2182 if (empty < 0)
2183 return isl_stat_error;
2184 if (empty)
2185 return isl_stat_ok;
2186 data->is_subset = 0;
2187 return isl_stat_error;
2190 data->is_subset = isl_map_is_subset(map, entry2->data);
2191 if (data->is_subset < 0 || !data->is_subset)
2192 return isl_stat_error;
2194 return isl_stat_ok;
2197 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2198 __isl_keep isl_union_map *umap2)
2200 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2202 umap1 = isl_union_map_copy(umap1);
2203 umap2 = isl_union_map_copy(umap2);
2204 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2205 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2207 if (!umap1 || !umap2)
2208 goto error;
2210 data.umap2 = umap2;
2211 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2212 &is_subset_entry, &data) < 0 &&
2213 data.is_subset)
2214 goto error;
2216 isl_union_map_free(umap1);
2217 isl_union_map_free(umap2);
2219 return data.is_subset;
2220 error:
2221 isl_union_map_free(umap1);
2222 isl_union_map_free(umap2);
2223 return isl_bool_error;
2226 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2227 __isl_keep isl_union_set *uset2)
2229 return isl_union_map_is_subset(uset1, uset2);
2232 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2233 __isl_keep isl_union_map *umap2)
2235 isl_bool is_subset;
2237 if (!umap1 || !umap2)
2238 return isl_bool_error;
2239 is_subset = isl_union_map_is_subset(umap1, umap2);
2240 if (is_subset != isl_bool_true)
2241 return is_subset;
2242 is_subset = isl_union_map_is_subset(umap2, umap1);
2243 return is_subset;
2246 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2247 __isl_keep isl_union_set *uset2)
2249 return isl_union_map_is_equal(uset1, uset2);
2252 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2253 __isl_keep isl_union_map *umap2)
2255 isl_bool is_subset;
2257 if (!umap1 || !umap2)
2258 return isl_bool_error;
2259 is_subset = isl_union_map_is_subset(umap1, umap2);
2260 if (is_subset != isl_bool_true)
2261 return is_subset;
2262 is_subset = isl_union_map_is_subset(umap2, umap1);
2263 if (is_subset == isl_bool_error)
2264 return is_subset;
2265 return !is_subset;
2268 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2269 __isl_keep isl_union_set *uset2)
2271 return isl_union_map_is_strict_subset(uset1, uset2);
2274 /* Internal data structure for isl_union_map_is_disjoint.
2275 * umap2 is the union map with which we are comparing.
2276 * is_disjoint is initialized to 1 and is set to 0 as soon
2277 * as the union maps turn out not to be disjoint.
2279 struct isl_union_map_is_disjoint_data {
2280 isl_union_map *umap2;
2281 isl_bool is_disjoint;
2284 /* Check if "map" is disjoint from data->umap2 and abort
2285 * the search if it is not.
2287 static isl_stat is_disjoint_entry(void **entry, void *user)
2289 struct isl_union_map_is_disjoint_data *data = user;
2290 uint32_t hash;
2291 struct isl_hash_table_entry *entry2;
2292 isl_map *map = *entry;
2294 hash = isl_space_get_hash(map->dim);
2295 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2296 hash, &has_dim, map->dim, 0);
2297 if (!entry2)
2298 return isl_stat_ok;
2300 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2301 if (data->is_disjoint < 0 || !data->is_disjoint)
2302 return isl_stat_error;
2304 return isl_stat_ok;
2307 /* Are "umap1" and "umap2" disjoint?
2309 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2310 __isl_keep isl_union_map *umap2)
2312 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2314 umap1 = isl_union_map_copy(umap1);
2315 umap2 = isl_union_map_copy(umap2);
2316 umap1 = isl_union_map_align_params(umap1,
2317 isl_union_map_get_space(umap2));
2318 umap2 = isl_union_map_align_params(umap2,
2319 isl_union_map_get_space(umap1));
2321 if (!umap1 || !umap2)
2322 goto error;
2324 data.umap2 = umap2;
2325 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2326 &is_disjoint_entry, &data) < 0 &&
2327 data.is_disjoint)
2328 goto error;
2330 isl_union_map_free(umap1);
2331 isl_union_map_free(umap2);
2333 return data.is_disjoint;
2334 error:
2335 isl_union_map_free(umap1);
2336 isl_union_map_free(umap2);
2337 return isl_bool_error;
2340 /* Are "uset1" and "uset2" disjoint?
2342 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2343 __isl_keep isl_union_set *uset2)
2345 return isl_union_map_is_disjoint(uset1, uset2);
2348 static isl_stat sample_entry(void **entry, void *user)
2350 isl_basic_map **sample = (isl_basic_map **)user;
2351 isl_map *map = *entry;
2353 *sample = isl_map_sample(isl_map_copy(map));
2354 if (!*sample)
2355 return isl_stat_error;
2356 if (!isl_basic_map_plain_is_empty(*sample))
2357 return isl_stat_error;
2358 return isl_stat_ok;
2361 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2363 isl_basic_map *sample = NULL;
2365 if (!umap)
2366 return NULL;
2368 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2369 &sample_entry, &sample) < 0 &&
2370 !sample)
2371 goto error;
2373 if (!sample)
2374 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2376 isl_union_map_free(umap);
2378 return sample;
2379 error:
2380 isl_union_map_free(umap);
2381 return NULL;
2384 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2386 return bset_from_bmap(isl_union_map_sample(uset));
2389 /* Return an element in "uset" in the form of an isl_point.
2390 * Return a void isl_point if "uset" is empty.
2392 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2394 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2397 struct isl_forall_data {
2398 isl_bool res;
2399 isl_bool (*fn)(__isl_keep isl_map *map);
2402 static isl_stat forall_entry(void **entry, void *user)
2404 struct isl_forall_data *data = user;
2405 isl_map *map = *entry;
2407 data->res = data->fn(map);
2408 if (data->res < 0)
2409 return isl_stat_error;
2411 if (!data->res)
2412 return isl_stat_error;
2414 return isl_stat_ok;
2417 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2418 isl_bool (*fn)(__isl_keep isl_map *map))
2420 struct isl_forall_data data = { isl_bool_true, fn };
2422 if (!umap)
2423 return isl_bool_error;
2425 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2426 &forall_entry, &data) < 0 && data.res)
2427 return isl_bool_error;
2429 return data.res;
2432 struct isl_forall_user_data {
2433 isl_bool res;
2434 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2435 void *user;
2438 static isl_stat forall_user_entry(void **entry, void *user)
2440 struct isl_forall_user_data *data = user;
2441 isl_map *map = *entry;
2443 data->res = data->fn(map, data->user);
2444 if (data->res < 0)
2445 return isl_stat_error;
2447 if (!data->res)
2448 return isl_stat_error;
2450 return isl_stat_ok;
2453 /* Check if fn(map, user) returns true for all maps "map" in umap.
2455 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2456 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2458 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2460 if (!umap)
2461 return isl_bool_error;
2463 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2464 &forall_user_entry, &data) < 0 && data.res)
2465 return isl_bool_error;
2467 return data.res;
2470 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2472 return union_map_forall(umap, &isl_map_is_empty);
2475 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2477 return isl_union_map_is_empty(uset);
2480 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2482 isl_bool is_subset;
2483 isl_space *dim;
2484 isl_map *id;
2486 if (!map)
2487 return isl_bool_error;
2489 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2490 map->dim, isl_dim_out))
2491 return isl_bool_false;
2493 dim = isl_map_get_space(map);
2494 id = isl_map_identity(dim);
2496 is_subset = isl_map_is_subset(map, id);
2498 isl_map_free(id);
2500 return is_subset;
2503 /* Given an isl_union_map that consists of a single map, check
2504 * if it is single-valued.
2506 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2508 isl_map *map;
2509 isl_bool sv;
2511 umap = isl_union_map_copy(umap);
2512 map = isl_map_from_union_map(umap);
2513 sv = isl_map_is_single_valued(map);
2514 isl_map_free(map);
2516 return sv;
2519 /* Internal data structure for single_valued_on_domain.
2521 * "umap" is the union map to be tested.
2522 * "sv" is set to 1 as long as "umap" may still be single-valued.
2524 struct isl_union_map_is_sv_data {
2525 isl_union_map *umap;
2526 isl_bool sv;
2529 /* Check if the data->umap is single-valued on "set".
2531 * If data->umap consists of a single map on "set", then test it
2532 * as an isl_map.
2534 * Otherwise, compute
2536 * M \circ M^-1
2538 * check if the result is a subset of the identity mapping and
2539 * store the result in data->sv.
2541 * Terminate as soon as data->umap has been determined not to
2542 * be single-valued.
2544 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2546 struct isl_union_map_is_sv_data *data = user;
2547 isl_union_map *umap, *test;
2549 umap = isl_union_map_copy(data->umap);
2550 umap = isl_union_map_intersect_domain(umap,
2551 isl_union_set_from_set(set));
2553 if (isl_union_map_n_map(umap) == 1) {
2554 data->sv = single_map_is_single_valued(umap);
2555 isl_union_map_free(umap);
2556 } else {
2557 test = isl_union_map_reverse(isl_union_map_copy(umap));
2558 test = isl_union_map_apply_range(test, umap);
2560 data->sv = union_map_forall(test, &is_subset_of_identity);
2562 isl_union_map_free(test);
2565 if (data->sv < 0 || !data->sv)
2566 return isl_stat_error;
2567 return isl_stat_ok;
2570 /* Check if the given map is single-valued.
2572 * If the union map consists of a single map, then test it as an isl_map.
2573 * Otherwise, check if the union map is single-valued on each of its
2574 * domain spaces.
2576 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2578 isl_union_map *universe;
2579 isl_union_set *domain;
2580 struct isl_union_map_is_sv_data data;
2582 if (isl_union_map_n_map(umap) == 1)
2583 return single_map_is_single_valued(umap);
2585 universe = isl_union_map_universe(isl_union_map_copy(umap));
2586 domain = isl_union_map_domain(universe);
2588 data.sv = isl_bool_true;
2589 data.umap = umap;
2590 if (isl_union_set_foreach_set(domain,
2591 &single_valued_on_domain, &data) < 0 && data.sv)
2592 data.sv = isl_bool_error;
2593 isl_union_set_free(domain);
2595 return data.sv;
2598 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2600 isl_bool in;
2602 umap = isl_union_map_copy(umap);
2603 umap = isl_union_map_reverse(umap);
2604 in = isl_union_map_is_single_valued(umap);
2605 isl_union_map_free(umap);
2607 return in;
2610 /* Is "map" obviously not an identity relation because
2611 * it maps elements from one space to another space?
2612 * Update *non_identity accordingly.
2614 * In particular, if the domain and range spaces are the same,
2615 * then the map is not considered to obviously not be an identity relation.
2616 * Otherwise, the map is considered to obviously not be an identity relation
2617 * if it is is non-empty.
2619 * If "map" is determined to obviously not be an identity relation,
2620 * then the search is aborted.
2622 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2624 isl_bool *non_identity = user;
2625 isl_bool equal;
2626 isl_space *space;
2628 space = isl_map_get_space(map);
2629 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2630 if (equal >= 0 && !equal)
2631 *non_identity = isl_bool_not(isl_map_is_empty(map));
2632 else
2633 *non_identity = isl_bool_not(equal);
2634 isl_space_free(space);
2635 isl_map_free(map);
2637 if (*non_identity < 0 || *non_identity)
2638 return isl_stat_error;
2640 return isl_stat_ok;
2643 /* Is "umap" obviously not an identity relation because
2644 * it maps elements from one space to another space?
2646 * As soon as a map has been found that maps elements to a different space,
2647 * non_identity is changed and the search is aborted.
2649 static isl_bool isl_union_map_plain_is_not_identity(
2650 __isl_keep isl_union_map *umap)
2652 isl_bool non_identity;
2654 non_identity = isl_bool_false;
2655 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2656 &non_identity) < 0 &&
2657 non_identity == isl_bool_false)
2658 return isl_bool_error;
2660 return non_identity;
2663 /* Does "map" only map elements to themselves?
2664 * Update *identity accordingly.
2666 * If "map" is determined not to be an identity relation,
2667 * then the search is aborted.
2669 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2671 isl_bool *identity = user;
2673 *identity = isl_map_is_identity(map);
2674 isl_map_free(map);
2676 if (*identity < 0 || !*identity)
2677 return isl_stat_error;
2679 return isl_stat_ok;
2682 /* Does "umap" only map elements to themselves?
2684 * First check if there are any maps that map elements to different spaces.
2685 * If not, then check that all the maps (between identical spaces)
2686 * are identity relations.
2688 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2690 isl_bool non_identity;
2691 isl_bool identity;
2693 non_identity = isl_union_map_plain_is_not_identity(umap);
2694 if (non_identity < 0 || non_identity)
2695 return isl_bool_not(non_identity);
2697 identity = isl_bool_true;
2698 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2699 identity == isl_bool_true)
2700 return isl_bool_error;
2702 return identity;
2705 /* Represents a map that has a fixed value (v) for one of its
2706 * range dimensions.
2707 * The map in this structure is not reference counted, so it
2708 * is only valid while the isl_union_map from which it was
2709 * obtained is still alive.
2711 struct isl_fixed_map {
2712 isl_int v;
2713 isl_map *map;
2716 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2717 int n)
2719 int i;
2720 struct isl_fixed_map *v;
2722 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2723 if (!v)
2724 return NULL;
2725 for (i = 0; i < n; ++i)
2726 isl_int_init(v[i].v);
2727 return v;
2730 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2732 int i;
2734 if (!v)
2735 return;
2736 for (i = 0; i < n; ++i)
2737 isl_int_clear(v[i].v);
2738 free(v);
2741 /* Compare the "v" field of two isl_fixed_map structs.
2743 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2745 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2746 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2748 return isl_int_cmp(e1->v, e2->v);
2751 /* Internal data structure used while checking whether all maps
2752 * in a union_map have a fixed value for a given output dimension.
2753 * v is the list of maps, with the fixed value for the dimension
2754 * n is the number of maps considered so far
2755 * pos is the output dimension under investigation
2757 struct isl_fixed_dim_data {
2758 struct isl_fixed_map *v;
2759 int n;
2760 int pos;
2763 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2765 struct isl_fixed_dim_data *data = user;
2767 data->v[data->n].map = map;
2768 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2769 &data->v[data->n++].v);
2772 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2773 int first, int n_range);
2775 /* Given a list of the maps, with their fixed values at output dimension "pos",
2776 * check whether the ranges of the maps form an obvious partition.
2778 * We first sort the maps according to their fixed values.
2779 * If all maps have a different value, then we know the ranges form
2780 * a partition.
2781 * Otherwise, we collect the maps with the same fixed value and
2782 * check whether each such collection is obviously injective
2783 * based on later dimensions.
2785 static int separates(struct isl_fixed_map *v, int n,
2786 __isl_take isl_space *dim, int pos, int n_range)
2788 int i;
2790 if (!v)
2791 goto error;
2793 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2795 for (i = 0; i + 1 < n; ++i) {
2796 int j, k;
2797 isl_union_map *part;
2798 int injective;
2800 for (j = i + 1; j < n; ++j)
2801 if (isl_int_ne(v[i].v, v[j].v))
2802 break;
2804 if (j == i + 1)
2805 continue;
2807 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2808 for (k = i; k < j; ++k)
2809 part = isl_union_map_add_map(part,
2810 isl_map_copy(v[k].map));
2812 injective = plain_injective_on_range(part, pos + 1, n_range);
2813 if (injective < 0)
2814 goto error;
2815 if (!injective)
2816 break;
2818 i = j - 1;
2821 isl_space_free(dim);
2822 free_isl_fixed_map_array(v, n);
2823 return i + 1 >= n;
2824 error:
2825 isl_space_free(dim);
2826 free_isl_fixed_map_array(v, n);
2827 return -1;
2830 /* Check whether the maps in umap have obviously distinct ranges.
2831 * In particular, check for an output dimension in the range
2832 * [first,n_range) for which all maps have a fixed value
2833 * and then check if these values, possibly along with fixed values
2834 * at later dimensions, entail distinct ranges.
2836 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2837 int first, int n_range)
2839 isl_ctx *ctx;
2840 int n;
2841 struct isl_fixed_dim_data data = { NULL };
2843 ctx = isl_union_map_get_ctx(umap);
2845 n = isl_union_map_n_map(umap);
2846 if (!umap)
2847 goto error;
2849 if (n <= 1) {
2850 isl_union_map_free(umap);
2851 return isl_bool_true;
2854 if (first >= n_range) {
2855 isl_union_map_free(umap);
2856 return isl_bool_false;
2859 data.v = alloc_isl_fixed_map_array(ctx, n);
2860 if (!data.v)
2861 goto error;
2863 for (data.pos = first; data.pos < n_range; ++data.pos) {
2864 isl_bool fixed;
2865 int injective;
2866 isl_space *dim;
2868 data.n = 0;
2869 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2870 if (fixed < 0)
2871 goto error;
2872 if (!fixed)
2873 continue;
2874 dim = isl_union_map_get_space(umap);
2875 injective = separates(data.v, n, dim, data.pos, n_range);
2876 isl_union_map_free(umap);
2877 return injective;
2880 free_isl_fixed_map_array(data.v, n);
2881 isl_union_map_free(umap);
2883 return isl_bool_false;
2884 error:
2885 free_isl_fixed_map_array(data.v, n);
2886 isl_union_map_free(umap);
2887 return isl_bool_error;
2890 /* Check whether the maps in umap that map to subsets of "ran"
2891 * have obviously distinct ranges.
2893 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2894 void *user)
2896 isl_union_map *umap = user;
2898 umap = isl_union_map_copy(umap);
2899 umap = isl_union_map_intersect_range(umap,
2900 isl_union_set_from_set(isl_set_copy(ran)));
2901 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2904 /* Check if the given union_map is obviously injective.
2906 * In particular, we first check if all individual maps are obviously
2907 * injective and then check if all the ranges of these maps are
2908 * obviously disjoint.
2910 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2912 isl_bool in;
2913 isl_union_map *univ;
2914 isl_union_set *ran;
2916 in = union_map_forall(umap, &isl_map_plain_is_injective);
2917 if (in < 0)
2918 return isl_bool_error;
2919 if (!in)
2920 return isl_bool_false;
2922 univ = isl_union_map_universe(isl_union_map_copy(umap));
2923 ran = isl_union_map_range(univ);
2925 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2927 isl_union_set_free(ran);
2929 return in;
2932 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2934 isl_bool sv;
2936 sv = isl_union_map_is_single_valued(umap);
2937 if (sv < 0 || !sv)
2938 return sv;
2940 return isl_union_map_is_injective(umap);
2943 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2945 struct isl_un_op_control control = {
2946 .filter = &isl_map_can_zip,
2947 .fn_map = &isl_map_zip,
2949 return un_op(umap, &control);
2952 /* Given a union map, take the maps of the form A -> (B -> C) and
2953 * return the union of the corresponding maps (A -> B) -> C.
2955 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2957 struct isl_un_op_control control = {
2958 .filter = &isl_map_can_uncurry,
2959 .fn_map = &isl_map_uncurry,
2961 return un_op(umap, &control);
2964 /* Given a union map, take the maps of the form (A -> B) -> C and
2965 * return the union of the corresponding maps A -> (B -> C).
2967 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2969 struct isl_un_op_control control = {
2970 .filter = &isl_map_can_curry,
2971 .fn_map = &isl_map_curry,
2973 return un_op(umap, &control);
2976 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2977 * return the union of the corresponding maps A -> (B -> (C -> D)).
2979 __isl_give isl_union_map *isl_union_map_range_curry(
2980 __isl_take isl_union_map *umap)
2982 struct isl_un_op_control control = {
2983 .filter = &isl_map_can_range_curry,
2984 .fn_map = &isl_map_range_curry,
2986 return un_op(umap, &control);
2989 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
2991 struct isl_un_op_control control = {
2992 .fn_map = &isl_set_lift,
2994 return un_op(uset, &control);
2997 static isl_stat coefficients_entry(void **entry, void *user)
2999 isl_set *set = *entry;
3000 isl_union_set **res = user;
3002 set = isl_set_copy(set);
3003 set = isl_set_from_basic_set(isl_set_coefficients(set));
3004 *res = isl_union_set_add_set(*res, set);
3006 return isl_stat_ok;
3009 __isl_give isl_union_set *isl_union_set_coefficients(
3010 __isl_take isl_union_set *uset)
3012 isl_ctx *ctx;
3013 isl_space *dim;
3014 isl_union_set *res;
3016 if (!uset)
3017 return NULL;
3019 ctx = isl_union_set_get_ctx(uset);
3020 dim = isl_space_set_alloc(ctx, 0, 0);
3021 res = isl_union_map_alloc(dim, uset->table.n);
3022 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3023 &coefficients_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 static isl_stat solutions_entry(void **entry, void *user)
3036 isl_set *set = *entry;
3037 isl_union_set **res = user;
3039 set = isl_set_copy(set);
3040 set = isl_set_from_basic_set(isl_set_solutions(set));
3041 if (!*res)
3042 *res = isl_union_set_from_set(set);
3043 else
3044 *res = isl_union_set_add_set(*res, set);
3046 if (!*res)
3047 return isl_stat_error;
3049 return isl_stat_ok;
3052 __isl_give isl_union_set *isl_union_set_solutions(
3053 __isl_take isl_union_set *uset)
3055 isl_union_set *res = NULL;
3057 if (!uset)
3058 return NULL;
3060 if (uset->table.n == 0) {
3061 res = isl_union_set_empty(isl_union_set_get_space(uset));
3062 isl_union_set_free(uset);
3063 return res;
3066 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3067 &solutions_entry, &res) < 0)
3068 goto error;
3070 isl_union_set_free(uset);
3071 return res;
3072 error:
3073 isl_union_set_free(uset);
3074 isl_union_set_free(res);
3075 return NULL;
3078 /* Is the domain space of "map" equal to "space"?
3080 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3082 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3083 space, isl_dim_out);
3086 /* Is the range space of "map" equal to "space"?
3088 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3090 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3091 space, isl_dim_out);
3094 /* Is the set space of "map" equal to "space"?
3096 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3098 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3099 space, isl_dim_out);
3102 /* Internal data structure for preimage_pw_multi_aff.
3104 * "pma" is the function under which the preimage should be taken.
3105 * "space" is the space of "pma".
3106 * "res" collects the results.
3107 * "fn" computes the preimage for a given map.
3108 * "match" returns true if "fn" can be called.
3110 struct isl_union_map_preimage_data {
3111 isl_space *space;
3112 isl_pw_multi_aff *pma;
3113 isl_union_map *res;
3114 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3115 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3116 __isl_take isl_pw_multi_aff *pma);
3119 /* Call data->fn to compute the preimage of the domain or range of *entry
3120 * under the function represented by data->pma, provided the domain/range
3121 * space of *entry matches the target space of data->pma
3122 * (as given by data->match), and add the result to data->res.
3124 static isl_stat preimage_entry(void **entry, void *user)
3126 int m;
3127 isl_map *map = *entry;
3128 struct isl_union_map_preimage_data *data = user;
3129 isl_bool empty;
3131 m = data->match(map, data->space);
3132 if (m < 0)
3133 return isl_stat_error;
3134 if (!m)
3135 return isl_stat_ok;
3137 map = isl_map_copy(map);
3138 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3140 empty = isl_map_is_empty(map);
3141 if (empty < 0 || empty) {
3142 isl_map_free(map);
3143 return empty < 0 ? isl_stat_error : isl_stat_ok;
3146 data->res = isl_union_map_add_map(data->res, map);
3148 return isl_stat_ok;
3151 /* Compute the preimage of the domain or range of "umap" under the function
3152 * represented by "pma".
3153 * In other words, plug in "pma" in the domain or range of "umap".
3154 * The function "fn" performs the actual preimage computation on a map,
3155 * while "match" determines to which maps the function should be applied.
3157 static __isl_give isl_union_map *preimage_pw_multi_aff(
3158 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3159 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3160 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3161 __isl_take isl_pw_multi_aff *pma))
3163 isl_ctx *ctx;
3164 isl_space *space;
3165 struct isl_union_map_preimage_data data;
3167 umap = isl_union_map_align_params(umap,
3168 isl_pw_multi_aff_get_space(pma));
3169 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3171 if (!umap || !pma)
3172 goto error;
3174 ctx = isl_union_map_get_ctx(umap);
3175 space = isl_union_map_get_space(umap);
3176 data.space = isl_pw_multi_aff_get_space(pma);
3177 data.pma = pma;
3178 data.res = isl_union_map_alloc(space, umap->table.n);
3179 data.match = match;
3180 data.fn = fn;
3181 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3182 &data) < 0)
3183 data.res = isl_union_map_free(data.res);
3185 isl_space_free(data.space);
3186 isl_union_map_free(umap);
3187 isl_pw_multi_aff_free(pma);
3188 return data.res;
3189 error:
3190 isl_union_map_free(umap);
3191 isl_pw_multi_aff_free(pma);
3192 return NULL;
3195 /* Compute the preimage of the domain of "umap" under the function
3196 * represented by "pma".
3197 * In other words, plug in "pma" in the domain of "umap".
3198 * The result contains maps that live in the same spaces as the maps of "umap"
3199 * with domain space equal to the target space of "pma",
3200 * except that the domain has been replaced by the domain space of "pma".
3202 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3203 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3205 return preimage_pw_multi_aff(umap, pma, &domain_match,
3206 &isl_map_preimage_domain_pw_multi_aff);
3209 /* Compute the preimage of the range of "umap" under the function
3210 * represented by "pma".
3211 * In other words, plug in "pma" in the range of "umap".
3212 * The result contains maps that live in the same spaces as the maps of "umap"
3213 * with range space equal to the target space of "pma",
3214 * except that the range has been replaced by the domain space of "pma".
3216 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3217 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3219 return preimage_pw_multi_aff(umap, pma, &range_match,
3220 &isl_map_preimage_range_pw_multi_aff);
3223 /* Compute the preimage of "uset" under the function represented by "pma".
3224 * In other words, plug in "pma" in "uset".
3225 * The result contains sets that live in the same spaces as the sets of "uset"
3226 * with space equal to the target space of "pma",
3227 * except that the space has been replaced by the domain space of "pma".
3229 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3230 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3232 return preimage_pw_multi_aff(uset, pma, &set_match,
3233 &isl_set_preimage_pw_multi_aff);
3236 /* Compute the preimage of the domain of "umap" under the function
3237 * represented by "ma".
3238 * In other words, plug in "ma" in the domain of "umap".
3239 * The result contains maps that live in the same spaces as the maps of "umap"
3240 * with domain space equal to the target space of "ma",
3241 * except that the domain has been replaced by the domain space of "ma".
3243 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3244 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3246 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3247 isl_pw_multi_aff_from_multi_aff(ma));
3250 /* Compute the preimage of the range of "umap" under the function
3251 * represented by "ma".
3252 * In other words, plug in "ma" in the range of "umap".
3253 * The result contains maps that live in the same spaces as the maps of "umap"
3254 * with range space equal to the target space of "ma",
3255 * except that the range has been replaced by the domain space of "ma".
3257 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3258 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3260 return isl_union_map_preimage_range_pw_multi_aff(umap,
3261 isl_pw_multi_aff_from_multi_aff(ma));
3264 /* Compute the preimage of "uset" under the function represented by "ma".
3265 * In other words, plug in "ma" in "uset".
3266 * The result contains sets that live in the same spaces as the sets of "uset"
3267 * with space equal to the target space of "ma",
3268 * except that the space has been replaced by the domain space of "ma".
3270 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3271 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3273 return isl_union_set_preimage_pw_multi_aff(uset,
3274 isl_pw_multi_aff_from_multi_aff(ma));
3277 /* Internal data structure for preimage_multi_pw_aff.
3279 * "mpa" is the function under which the preimage should be taken.
3280 * "space" is the space of "mpa".
3281 * "res" collects the results.
3282 * "fn" computes the preimage for a given map.
3283 * "match" returns true if "fn" can be called.
3285 struct isl_union_map_preimage_mpa_data {
3286 isl_space *space;
3287 isl_multi_pw_aff *mpa;
3288 isl_union_map *res;
3289 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3290 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3291 __isl_take isl_multi_pw_aff *mpa);
3294 /* Call data->fn to compute the preimage of the domain or range of *entry
3295 * under the function represented by data->mpa, provided the domain/range
3296 * space of *entry matches the target space of data->mpa
3297 * (as given by data->match), and add the result to data->res.
3299 static isl_stat preimage_mpa_entry(void **entry, void *user)
3301 int m;
3302 isl_map *map = *entry;
3303 struct isl_union_map_preimage_mpa_data *data = user;
3304 isl_bool empty;
3306 m = data->match(map, data->space);
3307 if (m < 0)
3308 return isl_stat_error;
3309 if (!m)
3310 return isl_stat_ok;
3312 map = isl_map_copy(map);
3313 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3315 empty = isl_map_is_empty(map);
3316 if (empty < 0 || empty) {
3317 isl_map_free(map);
3318 return empty < 0 ? isl_stat_error : isl_stat_ok;
3321 data->res = isl_union_map_add_map(data->res, map);
3323 return isl_stat_ok;
3326 /* Compute the preimage of the domain or range of "umap" under the function
3327 * represented by "mpa".
3328 * In other words, plug in "mpa" in the domain or range of "umap".
3329 * The function "fn" performs the actual preimage computation on a map,
3330 * while "match" determines to which maps the function should be applied.
3332 static __isl_give isl_union_map *preimage_multi_pw_aff(
3333 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3334 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3335 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3336 __isl_take isl_multi_pw_aff *mpa))
3338 isl_ctx *ctx;
3339 isl_space *space;
3340 struct isl_union_map_preimage_mpa_data data;
3342 umap = isl_union_map_align_params(umap,
3343 isl_multi_pw_aff_get_space(mpa));
3344 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3346 if (!umap || !mpa)
3347 goto error;
3349 ctx = isl_union_map_get_ctx(umap);
3350 space = isl_union_map_get_space(umap);
3351 data.space = isl_multi_pw_aff_get_space(mpa);
3352 data.mpa = mpa;
3353 data.res = isl_union_map_alloc(space, umap->table.n);
3354 data.match = match;
3355 data.fn = fn;
3356 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3357 &data) < 0)
3358 data.res = isl_union_map_free(data.res);
3360 isl_space_free(data.space);
3361 isl_union_map_free(umap);
3362 isl_multi_pw_aff_free(mpa);
3363 return data.res;
3364 error:
3365 isl_union_map_free(umap);
3366 isl_multi_pw_aff_free(mpa);
3367 return NULL;
3370 /* Compute the preimage of the domain of "umap" under the function
3371 * represented by "mpa".
3372 * In other words, plug in "mpa" in the domain of "umap".
3373 * The result contains maps that live in the same spaces as the maps of "umap"
3374 * with domain space equal to the target space of "mpa",
3375 * except that the domain has been replaced by the domain space of "mpa".
3377 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3378 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3380 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3381 &isl_map_preimage_domain_multi_pw_aff);
3384 /* Internal data structure for preimage_upma.
3386 * "umap" is the map of which the preimage should be computed.
3387 * "res" collects the results.
3388 * "fn" computes the preimage for a given piecewise multi-affine function.
3390 struct isl_union_map_preimage_upma_data {
3391 isl_union_map *umap;
3392 isl_union_map *res;
3393 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3394 __isl_take isl_pw_multi_aff *pma);
3397 /* Call data->fn to compute the preimage of the domain or range of data->umap
3398 * under the function represented by pma and add the result to data->res.
3400 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3402 struct isl_union_map_preimage_upma_data *data = user;
3403 isl_union_map *umap;
3405 umap = isl_union_map_copy(data->umap);
3406 umap = data->fn(umap, pma);
3407 data->res = isl_union_map_union(data->res, umap);
3409 return data->res ? isl_stat_ok : isl_stat_error;
3412 /* Compute the preimage of the domain or range of "umap" under the function
3413 * represented by "upma".
3414 * In other words, plug in "upma" in the domain or range of "umap".
3415 * The function "fn" performs the actual preimage computation
3416 * on a piecewise multi-affine function.
3418 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3419 __isl_take isl_union_map *umap,
3420 __isl_take isl_union_pw_multi_aff *upma,
3421 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3422 __isl_take isl_pw_multi_aff *pma))
3424 struct isl_union_map_preimage_upma_data data;
3426 data.umap = umap;
3427 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3428 data.fn = fn;
3429 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3430 &preimage_upma, &data) < 0)
3431 data.res = isl_union_map_free(data.res);
3433 isl_union_map_free(umap);
3434 isl_union_pw_multi_aff_free(upma);
3436 return data.res;
3439 /* Compute the preimage of the domain of "umap" under the function
3440 * represented by "upma".
3441 * In other words, plug in "upma" in the domain of "umap".
3442 * The result contains maps that live in the same spaces as the maps of "umap"
3443 * with domain space equal to one of the target spaces of "upma",
3444 * except that the domain has been replaced by one of the the domain spaces that
3445 * corresponds to that target space of "upma".
3447 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3448 __isl_take isl_union_map *umap,
3449 __isl_take isl_union_pw_multi_aff *upma)
3451 return preimage_union_pw_multi_aff(umap, upma,
3452 &isl_union_map_preimage_domain_pw_multi_aff);
3455 /* Compute the preimage of the range of "umap" under the function
3456 * represented by "upma".
3457 * In other words, plug in "upma" in the range of "umap".
3458 * The result contains maps that live in the same spaces as the maps of "umap"
3459 * with range space equal to one of the target spaces of "upma",
3460 * except that the range has been replaced by one of the the domain spaces that
3461 * corresponds to that target space of "upma".
3463 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3464 __isl_take isl_union_map *umap,
3465 __isl_take isl_union_pw_multi_aff *upma)
3467 return preimage_union_pw_multi_aff(umap, upma,
3468 &isl_union_map_preimage_range_pw_multi_aff);
3471 /* Compute the preimage of "uset" under the function represented by "upma".
3472 * In other words, plug in "upma" in the range of "uset".
3473 * The result contains sets that live in the same spaces as the sets of "uset"
3474 * with space equal to one of the target spaces of "upma",
3475 * except that the space has been replaced by one of the the domain spaces that
3476 * corresponds to that target space of "upma".
3478 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3479 __isl_take isl_union_set *uset,
3480 __isl_take isl_union_pw_multi_aff *upma)
3482 return preimage_union_pw_multi_aff(uset, upma,
3483 &isl_union_set_preimage_pw_multi_aff);
3486 /* Reset the user pointer on all identifiers of parameters and tuples
3487 * of the spaces of "umap".
3489 __isl_give isl_union_map *isl_union_map_reset_user(
3490 __isl_take isl_union_map *umap)
3492 umap = isl_union_map_cow(umap);
3493 if (!umap)
3494 return NULL;
3495 umap->dim = isl_space_reset_user(umap->dim);
3496 if (!umap->dim)
3497 return isl_union_map_free(umap);
3498 return total(umap, &isl_map_reset_user);
3501 /* Reset the user pointer on all identifiers of parameters and tuples
3502 * of the spaces of "uset".
3504 __isl_give isl_union_set *isl_union_set_reset_user(
3505 __isl_take isl_union_set *uset)
3507 return isl_union_map_reset_user(uset);
3510 /* Remove all existentially quantified variables and integer divisions
3511 * from "umap" using Fourier-Motzkin elimination.
3513 __isl_give isl_union_map *isl_union_map_remove_divs(
3514 __isl_take isl_union_map *umap)
3516 return total(umap, &isl_map_remove_divs);
3519 /* Remove all existentially quantified variables and integer divisions
3520 * from "uset" using Fourier-Motzkin elimination.
3522 __isl_give isl_union_set *isl_union_set_remove_divs(
3523 __isl_take isl_union_set *uset)
3525 return isl_union_map_remove_divs(uset);
3528 /* Internal data structure for isl_union_map_project_out.
3529 * "type", "first" and "n" are the arguments for the isl_map_project_out
3530 * call.
3531 * "res" collects the results.
3533 struct isl_union_map_project_out_data {
3534 enum isl_dim_type type;
3535 unsigned first;
3536 unsigned n;
3538 isl_union_map *res;
3541 /* Turn the data->n dimensions of type data->type, starting at data->first
3542 * into existentially quantified variables and add the result to data->res.
3544 static isl_stat project_out(__isl_take isl_map *map, void *user)
3546 struct isl_union_map_project_out_data *data = user;
3548 map = isl_map_project_out(map, data->type, data->first, data->n);
3549 data->res = isl_union_map_add_map(data->res, map);
3551 return isl_stat_ok;
3554 /* Turn the "n" dimensions of type "type", starting at "first"
3555 * into existentially quantified variables.
3556 * Since the space of an isl_union_map only contains parameters,
3557 * type is required to be equal to isl_dim_param.
3559 __isl_give isl_union_map *isl_union_map_project_out(
3560 __isl_take isl_union_map *umap,
3561 enum isl_dim_type type, unsigned first, unsigned n)
3563 isl_space *space;
3564 struct isl_union_map_project_out_data data = { type, first, n };
3566 if (!umap)
3567 return NULL;
3569 if (type != isl_dim_param)
3570 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3571 "can only project out parameters",
3572 return isl_union_map_free(umap));
3574 space = isl_union_map_get_space(umap);
3575 space = isl_space_drop_dims(space, type, first, n);
3576 data.res = isl_union_map_empty(space);
3577 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3578 data.res = isl_union_map_free(data.res);
3580 isl_union_map_free(umap);
3582 return data.res;
3585 /* Turn the "n" dimensions of type "type", starting at "first"
3586 * into existentially quantified variables.
3587 * Since the space of an isl_union_set only contains parameters,
3588 * "type" is required to be equal to isl_dim_param.
3590 __isl_give isl_union_set *isl_union_set_project_out(
3591 __isl_take isl_union_set *uset,
3592 enum isl_dim_type type, unsigned first, unsigned n)
3594 return isl_union_map_project_out(uset, type, first, n);
3597 /* Internal data structure for isl_union_map_involves_dims.
3598 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3600 struct isl_union_map_involves_dims_data {
3601 unsigned first;
3602 unsigned n;
3605 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3607 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3609 struct isl_union_map_involves_dims_data *data = user;
3610 isl_bool involves;
3612 involves = isl_map_involves_dims(map,
3613 isl_dim_param, data->first, data->n);
3614 if (involves < 0)
3615 return isl_bool_error;
3616 return !involves;
3619 /* Does "umap" involve any of the n parameters starting at first?
3620 * "type" is required to be set to isl_dim_param.
3622 * "umap" involves any of those parameters if any of its maps
3623 * involve the parameters. In other words, "umap" does not
3624 * involve any of the parameters if all its maps to not
3625 * involve the parameters.
3627 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3628 enum isl_dim_type type, unsigned first, unsigned n)
3630 struct isl_union_map_involves_dims_data data = { first, n };
3631 isl_bool excludes;
3633 if (type != isl_dim_param)
3634 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3635 "can only reference parameters", return isl_bool_error);
3637 excludes = union_map_forall_user(umap, &map_excludes, &data);
3639 if (excludes < 0)
3640 return isl_bool_error;
3642 return !excludes;
3645 /* Internal data structure for isl_union_map_reset_range_space.
3646 * "range" is the space from which to set the range space.
3647 * "res" collects the results.
3649 struct isl_union_map_reset_range_space_data {
3650 isl_space *range;
3651 isl_union_map *res;
3654 /* Replace the range space of "map" by the range space of data->range and
3655 * add the result to data->res.
3657 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3659 struct isl_union_map_reset_range_space_data *data = user;
3660 isl_space *space;
3662 space = isl_map_get_space(map);
3663 space = isl_space_domain(space);
3664 space = isl_space_extend_domain_with_range(space,
3665 isl_space_copy(data->range));
3666 map = isl_map_reset_space(map, space);
3667 data->res = isl_union_map_add_map(data->res, map);
3669 return data->res ? isl_stat_ok : isl_stat_error;
3672 /* Replace the range space of all the maps in "umap" by
3673 * the range space of "space".
3675 * This assumes that all maps have the same output dimension.
3676 * This function should therefore not be made publicly available.
3678 * Since the spaces of the maps change, so do their hash value.
3679 * We therefore need to create a new isl_union_map.
3681 __isl_give isl_union_map *isl_union_map_reset_range_space(
3682 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3684 struct isl_union_map_reset_range_space_data data = { space };
3686 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3687 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3688 data.res = isl_union_map_free(data.res);
3690 isl_space_free(space);
3691 isl_union_map_free(umap);
3692 return data.res;
3695 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3696 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3697 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3698 * to the domain and the range of each map.
3699 * "res" collects the results.
3701 struct isl_union_order_at_data {
3702 isl_multi_union_pw_aff *mupa;
3703 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3704 __isl_take isl_multi_pw_aff *mpa2);
3705 isl_union_map *res;
3708 /* Intersect "map" with the result of applying data->order to
3709 * the functions in data->mupa that apply to the domain and the range
3710 * of "map" and add the result to data->res.
3712 static isl_stat order_at(__isl_take isl_map *map, void *user)
3714 struct isl_union_order_at_data *data = user;
3715 isl_space *space;
3716 isl_multi_pw_aff *mpa1, *mpa2;
3717 isl_map *order;
3719 space = isl_space_domain(isl_map_get_space(map));
3720 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3721 space = isl_space_range(isl_map_get_space(map));
3722 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3723 order = data->order(mpa1, mpa2);
3724 map = isl_map_intersect(map, order);
3725 data->res = isl_union_map_add_map(data->res, map);
3727 return data->res ? isl_stat_ok : isl_stat_error;
3730 /* Intersect each map in "umap" with the result of calling "order"
3731 * on the functions is "mupa" that apply to the domain and the range
3732 * of the map.
3734 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3735 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3736 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3737 __isl_take isl_multi_pw_aff *mpa2))
3739 struct isl_union_order_at_data data;
3741 umap = isl_union_map_align_params(umap,
3742 isl_multi_union_pw_aff_get_space(mupa));
3743 mupa = isl_multi_union_pw_aff_align_params(mupa,
3744 isl_union_map_get_space(umap));
3745 data.mupa = mupa;
3746 data.order = order;
3747 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3748 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3749 data.res = isl_union_map_free(data.res);
3751 isl_multi_union_pw_aff_free(mupa);
3752 isl_union_map_free(umap);
3753 return data.res;
3756 /* Return the subset of "umap" where the domain and the range
3757 * have equal "mupa" values.
3759 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3760 __isl_take isl_union_map *umap,
3761 __isl_take isl_multi_union_pw_aff *mupa)
3763 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3764 &isl_multi_pw_aff_eq_map);
3767 /* Return the subset of "umap" where the domain has a lexicographically
3768 * smaller "mupa" value than the range.
3770 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3771 __isl_take isl_union_map *umap,
3772 __isl_take isl_multi_union_pw_aff *mupa)
3774 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3775 &isl_multi_pw_aff_lex_lt_map);
3778 /* Return the subset of "umap" where the domain has a lexicographically
3779 * greater "mupa" value than the range.
3781 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3782 __isl_take isl_union_map *umap,
3783 __isl_take isl_multi_union_pw_aff *mupa)
3785 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3786 &isl_multi_pw_aff_lex_gt_map);
3789 /* Return the union of the elements in the list "list".
3791 __isl_give isl_union_set *isl_union_set_list_union(
3792 __isl_take isl_union_set_list *list)
3794 int i, n;
3795 isl_ctx *ctx;
3796 isl_space *space;
3797 isl_union_set *res;
3799 if (!list)
3800 return NULL;
3802 ctx = isl_union_set_list_get_ctx(list);
3803 space = isl_space_params_alloc(ctx, 0);
3804 res = isl_union_set_empty(space);
3806 n = isl_union_set_list_n_union_set(list);
3807 for (i = 0; i < n; ++i) {
3808 isl_union_set *uset_i;
3810 uset_i = isl_union_set_list_get_union_set(list, i);
3811 res = isl_union_set_union(res, uset_i);
3814 isl_union_set_list_free(list);
3815 return res;
3818 /* Update *hash with the hash value of "map".
3820 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3822 uint32_t *hash = user;
3823 uint32_t map_hash;
3825 map_hash = isl_map_get_hash(map);
3826 isl_hash_hash(*hash, map_hash);
3828 isl_map_free(map);
3829 return isl_stat_ok;
3832 /* Return a hash value that digests "umap".
3834 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3836 uint32_t hash;
3838 if (!umap)
3839 return 0;
3841 hash = isl_hash_init();
3842 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3843 return 0;
3845 return hash;
3848 /* Return a hash value that digests "uset".
3850 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3852 return isl_union_map_get_hash(uset);
3855 /* Add the number of basic sets in "set" to "n".
3857 static isl_stat add_n(__isl_take isl_set *set, void *user)
3859 int *n = user;
3861 *n += isl_set_n_basic_set(set);
3862 isl_set_free(set);
3864 return isl_stat_ok;
3867 /* Return the total number of basic sets in "uset".
3869 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
3871 int n = 0;
3873 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
3874 return -1;
3876 return n;
3879 /* Add the basic sets in "set" to "list".
3881 static isl_stat add_list(__isl_take isl_set *set, void *user)
3883 isl_basic_set_list **list = user;
3884 isl_basic_set_list *list_i;
3886 list_i = isl_set_get_basic_set_list(set);
3887 *list = isl_basic_set_list_concat(*list, list_i);
3888 isl_set_free(set);
3890 if (!*list)
3891 return isl_stat_error;
3892 return isl_stat_ok;
3895 /* Return a list containing all the basic sets in "uset".
3897 * First construct a list of the appropriate size and
3898 * then add all the elements.
3900 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
3901 __isl_keep isl_union_set *uset)
3903 int n;
3904 isl_ctx *ctx;
3905 isl_basic_set_list *list;
3907 if (!uset)
3908 return NULL;
3909 ctx = isl_union_set_get_ctx(uset);
3910 n = isl_union_set_n_basic_set(uset);
3911 if (n < 0)
3912 return NULL;
3913 list = isl_basic_set_list_alloc(ctx, n);
3914 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
3915 list = isl_basic_set_list_free(list);
3917 return list;