isl_multi_templ.c: extract out isl_multi_dims.c
[isl.git] / isl_union_map.c
blobc200e8b4a1e7357fbc08cf676cc5148f4362a048
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_maybe_map.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 *space)
115 return isl_union_map_alloc(space, 16);
118 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
120 return isl_union_map_empty(space);
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_space(const void *entry, const void *val)
353 isl_map *map = (isl_map *)entry;
354 isl_space *space = (isl_space *) val;
356 return isl_space_is_equal(map->dim, space);
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_space, 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 /* Internal data structure for isl_union_map_every_map.
487 * "test" is the user-specified callback function.
488 * "user" is the user-specified callback function argument.
490 * "failed" is initialized to 0 and set to 1 if "test" fails
491 * on any map.
493 struct isl_union_map_every_data {
494 isl_bool (*test)(__isl_keep isl_map *map, void *user);
495 void *user;
496 int failed;
499 /* Call data->test on "map".
500 * If this fails, then set data->failed and abort.
502 static isl_stat call_every(void **entry, void *user)
504 isl_map *map = *entry;
505 struct isl_union_map_every_data *data = user;
506 isl_bool r;
508 r = data->test(map, data->user);
509 if (r < 0)
510 return isl_stat_error;
511 if (r)
512 return isl_stat_ok;
513 data->failed = 1;
514 return isl_stat_error;
517 /* Does "test" succeed on every map in "umap"?
519 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
520 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
522 struct isl_union_map_every_data data = { test, user, 0 };
523 isl_stat r;
525 if (!umap)
526 return isl_bool_error;
528 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
529 &call_every, &data);
530 if (r >= 0)
531 return isl_bool_true;
532 if (data.failed)
533 return isl_bool_false;
534 return isl_bool_error;
537 static isl_stat copy_map(void **entry, void *user)
539 isl_map *map = *entry;
540 isl_map **map_p = user;
542 *map_p = isl_map_copy(map);
544 return isl_stat_error;
547 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
549 isl_ctx *ctx;
550 isl_map *map = NULL;
552 if (!umap)
553 return NULL;
554 ctx = isl_union_map_get_ctx(umap);
555 if (umap->table.n != 1)
556 isl_die(ctx, isl_error_invalid,
557 "union map needs to contain elements in exactly "
558 "one space", goto error);
560 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
562 isl_union_map_free(umap);
564 return map;
565 error:
566 isl_union_map_free(umap);
567 return NULL;
570 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
572 return isl_map_from_union_map(uset);
575 /* Extract the map in "umap" that lives in the given space (ignoring
576 * parameters).
578 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
579 __isl_take isl_space *space)
581 uint32_t hash;
582 struct isl_hash_table_entry *entry;
584 space = isl_space_drop_dims(space, isl_dim_param,
585 0, isl_space_dim(space, isl_dim_param));
586 space = isl_space_align_params(space, isl_union_map_get_space(umap));
587 if (!umap || !space)
588 goto error;
590 hash = isl_space_get_hash(space);
591 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
592 &has_space, space, 0);
593 if (!entry)
594 return isl_map_empty(space);
595 isl_space_free(space);
596 return isl_map_copy(entry->data);
597 error:
598 isl_space_free(space);
599 return NULL;
602 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
603 __isl_take isl_space *dim)
605 return set_from_map(isl_union_map_extract_map(uset, dim));
608 /* Check if umap contains a map in the given space.
610 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
611 __isl_keep isl_space *space)
613 uint32_t hash;
614 struct isl_hash_table_entry *entry;
616 if (!umap || !space)
617 return isl_bool_error;
619 hash = isl_space_get_hash(space);
620 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
621 &has_space, space, 0);
622 return !!entry;
625 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
626 __isl_keep isl_space *space)
628 return isl_union_map_contains(uset, space);
631 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
632 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
634 return isl_union_map_foreach_map(uset,
635 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
638 struct isl_union_set_foreach_point_data {
639 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
640 void *user;
643 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
645 struct isl_union_set_foreach_point_data *data = user;
646 isl_stat r;
648 r = isl_set_foreach_point(set, data->fn, data->user);
649 isl_set_free(set);
651 return r;
654 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
655 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
657 struct isl_union_set_foreach_point_data data = { fn, user };
658 return isl_union_set_foreach_set(uset, &foreach_point, &data);
661 /* Data structure that specifies how gen_bin_op should
662 * construct results from the inputs.
664 * If "subtract" is set, then a map in the first input is copied to the result
665 * if there is no corresponding map in the second input.
666 * Otherwise, a map in the first input with no corresponding map
667 * in the second input is ignored.
668 * If "filter" is not NULL, then it specifies which maps in the first
669 * input may have a matching map in the second input.
670 * In particular, it makes sure that "match_space" can be called
671 * on the space of the map.
672 * "match_space" specifies how to transform the space of a map
673 * in the first input to the space of the corresponding map
674 * in the second input.
675 * "fn_map" specifies how the matching maps, one from each input,
676 * should be combined to form a map in the result.
678 struct isl_bin_op_control {
679 int subtract;
680 isl_bool (*filter)(__isl_keep isl_map *map);
681 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
682 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
683 __isl_take isl_map *map2);
686 /* Internal data structure for gen_bin_op.
687 * "control" specifies how the maps in the result should be constructed.
688 * "umap2" is a pointer to the second argument.
689 * "res" collects the results.
691 struct isl_union_map_gen_bin_data {
692 struct isl_bin_op_control *control;
693 isl_union_map *umap2;
694 isl_union_map *res;
697 /* Add a copy of "map" to "res" and return the result.
699 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
700 __isl_keep isl_map *map)
702 return isl_union_map_add_map(res, isl_map_copy(map));
705 /* Combine "map1" and "map2", add the result to "res" and return the result.
706 * Check whether the result is empty before adding it to "res".
708 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
709 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
710 struct isl_union_map_gen_bin_data *data)
712 isl_bool empty;
713 isl_map *map;
715 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
716 empty = isl_map_is_empty(map);
717 if (empty < 0 || empty) {
718 isl_map_free(map);
719 if (empty < 0)
720 return isl_union_map_free(res);
721 return res;
723 return isl_union_map_add_map(res, map);
726 /* Dummy match_space function that simply returns the input space.
728 static __isl_give isl_space *identity(__isl_take isl_space *space)
730 return space;
733 /* Look for the map in data->umap2 that corresponds to "map", if any.
734 * Return (isl_bool_true, matching map) if there is one,
735 * (isl_bool_false, NULL) if there is no matching map and
736 * (isl_bool_error, NULL) on error.
738 * If not NULL, then data->control->filter specifies whether "map"
739 * can have any matching map. If so,
740 * data->control->match_space specifies which map in data->umap2
741 * corresponds to "map".
743 static __isl_keep isl_maybe_isl_map bin_try_get_match(
744 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
746 uint32_t hash;
747 struct isl_hash_table_entry *entry2;
748 isl_space *space;
749 isl_maybe_isl_map res = { isl_bool_error, NULL };
751 if (data->control->filter) {
752 res.valid = data->control->filter(map);
753 if (res.valid < 0 || !res.valid)
754 return res;
755 res.valid = isl_bool_error;
758 space = isl_map_get_space(map);
759 if (data->control->match_space != &identity)
760 space = data->control->match_space(space);
761 if (!space)
762 return res;
763 hash = isl_space_get_hash(space);
764 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
765 &data->umap2->table, hash,
766 &has_space, space, 0);
767 isl_space_free(space);
768 res.valid = entry2 != NULL;
769 if (entry2)
770 res.value = entry2->data;
772 return res;
775 /* isl_hash_table_foreach callback for gen_bin_op.
776 * Look for the map in data->umap2 that corresponds
777 * to the map that "entry" points to, apply the binary operation and
778 * add the result to data->res.
780 * If no corresponding map can be found, then the effect depends
781 * on data->control->subtract. If it is set, then the current map
782 * is added directly to the result. Otherwise, it is ignored.
784 static isl_stat gen_bin_entry(void **entry, void *user)
786 struct isl_union_map_gen_bin_data *data = user;
787 isl_map *map = *entry;
788 isl_maybe_isl_map m;
790 m = bin_try_get_match(data, map);
791 if (m.valid < 0)
792 return isl_stat_error;
793 if (!m.valid && !data->control->subtract)
794 return isl_stat_ok;
796 if (!m.valid)
797 data->res = bin_add_map(data->res, map);
798 else
799 data->res = bin_add_pair(data->res, map, m.value, data);
800 if (!data->res)
801 return isl_stat_error;
803 return isl_stat_ok;
806 /* Apply a binary operation to "umap1" and "umap2" based on "control".
807 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
808 * in gen_bin_entry.
810 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
811 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
813 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
815 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
816 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
818 if (!umap1 || !umap2)
819 goto error;
821 data.umap2 = umap2;
822 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
823 umap1->table.n);
824 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
825 &gen_bin_entry, &data) < 0)
826 goto error;
828 isl_union_map_free(umap1);
829 isl_union_map_free(umap2);
830 return data.res;
831 error:
832 isl_union_map_free(umap1);
833 isl_union_map_free(umap2);
834 isl_union_map_free(data.res);
835 return NULL;
838 __isl_give isl_union_map *isl_union_map_subtract(
839 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
841 struct isl_bin_op_control control = {
842 .subtract = 1,
843 .match_space = &identity,
844 .fn_map = &isl_map_subtract,
847 return gen_bin_op(umap1, umap2, &control);
850 __isl_give isl_union_set *isl_union_set_subtract(
851 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
853 return isl_union_map_subtract(uset1, uset2);
856 struct isl_union_map_gen_bin_set_data {
857 isl_set *set;
858 isl_union_map *res;
861 static isl_stat intersect_params_entry(void **entry, void *user)
863 struct isl_union_map_gen_bin_set_data *data = user;
864 isl_map *map = *entry;
865 int empty;
867 map = isl_map_copy(map);
868 map = isl_map_intersect_params(map, isl_set_copy(data->set));
870 empty = isl_map_is_empty(map);
871 if (empty < 0) {
872 isl_map_free(map);
873 return isl_stat_error;
876 data->res = isl_union_map_add_map(data->res, map);
878 return isl_stat_ok;
881 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
882 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
884 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
886 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
887 set = isl_set_align_params(set, isl_union_map_get_space(umap));
889 if (!umap || !set)
890 goto error;
892 data.set = set;
893 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
894 umap->table.n);
895 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
896 fn, &data) < 0)
897 goto error;
899 isl_union_map_free(umap);
900 isl_set_free(set);
901 return data.res;
902 error:
903 isl_union_map_free(umap);
904 isl_set_free(set);
905 isl_union_map_free(data.res);
906 return NULL;
909 /* Intersect "umap" with the parameter domain "set".
911 * If "set" does not have any constraints, then we can return immediately.
913 __isl_give isl_union_map *isl_union_map_intersect_params(
914 __isl_take isl_union_map *umap, __isl_take isl_set *set)
916 int is_universe;
918 is_universe = isl_set_plain_is_universe(set);
919 if (is_universe < 0)
920 goto error;
921 if (is_universe) {
922 isl_set_free(set);
923 return umap;
926 return gen_bin_set_op(umap, set, &intersect_params_entry);
927 error:
928 isl_union_map_free(umap);
929 isl_set_free(set);
930 return NULL;
933 __isl_give isl_union_set *isl_union_set_intersect_params(
934 __isl_take isl_union_set *uset, __isl_take isl_set *set)
936 return isl_union_map_intersect_params(uset, set);
939 static __isl_give isl_union_map *union_map_intersect_params(
940 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
942 return isl_union_map_intersect_params(umap,
943 isl_set_from_union_set(uset));
946 static __isl_give isl_union_map *union_map_gist_params(
947 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
949 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
952 struct isl_union_map_match_bin_data {
953 isl_union_map *umap2;
954 isl_union_map *res;
955 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
958 static isl_stat match_bin_entry(void **entry, void *user)
960 struct isl_union_map_match_bin_data *data = user;
961 uint32_t hash;
962 struct isl_hash_table_entry *entry2;
963 isl_map *map = *entry;
964 int empty;
966 hash = isl_space_get_hash(map->dim);
967 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
968 hash, &has_space, map->dim, 0);
969 if (!entry2)
970 return isl_stat_ok;
972 map = isl_map_copy(map);
973 map = data->fn(map, isl_map_copy(entry2->data));
975 empty = isl_map_is_empty(map);
976 if (empty < 0) {
977 isl_map_free(map);
978 return isl_stat_error;
980 if (empty) {
981 isl_map_free(map);
982 return isl_stat_ok;
985 data->res = isl_union_map_add_map(data->res, map);
987 return isl_stat_ok;
990 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
991 __isl_take isl_union_map *umap2,
992 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
994 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
996 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
997 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
999 if (!umap1 || !umap2)
1000 goto error;
1002 data.umap2 = umap2;
1003 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1004 umap1->table.n);
1005 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1006 &match_bin_entry, &data) < 0)
1007 goto error;
1009 isl_union_map_free(umap1);
1010 isl_union_map_free(umap2);
1011 return data.res;
1012 error:
1013 isl_union_map_free(umap1);
1014 isl_union_map_free(umap2);
1015 isl_union_map_free(data.res);
1016 return NULL;
1019 __isl_give isl_union_map *isl_union_map_intersect(
1020 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1022 return match_bin_op(umap1, umap2, &isl_map_intersect);
1025 /* Compute the intersection of the two union_sets.
1026 * As a special case, if exactly one of the two union_sets
1027 * is a parameter domain, then intersect the parameter domain
1028 * of the other one with this set.
1030 __isl_give isl_union_set *isl_union_set_intersect(
1031 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1033 int p1, p2;
1035 p1 = isl_union_set_is_params(uset1);
1036 p2 = isl_union_set_is_params(uset2);
1037 if (p1 < 0 || p2 < 0)
1038 goto error;
1039 if (!p1 && p2)
1040 return union_map_intersect_params(uset1, uset2);
1041 if (p1 && !p2)
1042 return union_map_intersect_params(uset2, uset1);
1043 return isl_union_map_intersect(uset1, uset2);
1044 error:
1045 isl_union_set_free(uset1);
1046 isl_union_set_free(uset2);
1047 return NULL;
1050 static isl_stat gist_params_entry(void **entry, void *user)
1052 struct isl_union_map_gen_bin_set_data *data = user;
1053 isl_map *map = *entry;
1054 int empty;
1056 map = isl_map_copy(map);
1057 map = isl_map_gist_params(map, isl_set_copy(data->set));
1059 empty = isl_map_is_empty(map);
1060 if (empty < 0) {
1061 isl_map_free(map);
1062 return isl_stat_error;
1065 data->res = isl_union_map_add_map(data->res, map);
1067 return isl_stat_ok;
1070 __isl_give isl_union_map *isl_union_map_gist_params(
1071 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1073 return gen_bin_set_op(umap, set, &gist_params_entry);
1076 __isl_give isl_union_set *isl_union_set_gist_params(
1077 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1079 return isl_union_map_gist_params(uset, set);
1082 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1083 __isl_take isl_union_map *context)
1085 return match_bin_op(umap, context, &isl_map_gist);
1088 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1089 __isl_take isl_union_set *context)
1091 if (isl_union_set_is_params(context))
1092 return union_map_gist_params(uset, context);
1093 return isl_union_map_gist(uset, context);
1096 /* For each map in "umap", remove the constraints in the corresponding map
1097 * of "context".
1098 * Each map in "context" is assumed to consist of a single disjunct and
1099 * to have explicit representations for all local variables.
1101 __isl_give isl_union_map *isl_union_map_plain_gist(
1102 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1104 return match_bin_op(umap, context, &isl_map_plain_gist);
1107 /* For each set in "uset", remove the constraints in the corresponding set
1108 * of "context".
1109 * Each set in "context" is assumed to consist of a single disjunct and
1110 * to have explicit representations for all local variables.
1112 __isl_give isl_union_set *isl_union_set_plain_gist(
1113 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1115 return isl_union_map_plain_gist(uset, context);
1118 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1119 __isl_take isl_map *set2)
1121 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1124 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1125 __isl_take isl_map *set2)
1127 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1130 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1131 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1133 return match_bin_op(uset1, uset2, &lex_lt_set);
1136 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1137 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1139 return match_bin_op(uset1, uset2, &lex_le_set);
1142 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1143 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1145 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1148 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1149 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1151 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1154 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1155 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1157 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1160 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1161 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1163 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1166 /* Intersect the domain of "umap" with "uset".
1168 static __isl_give isl_union_map *union_map_intersect_domain(
1169 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1171 struct isl_bin_op_control control = {
1172 .match_space = &isl_space_domain,
1173 .fn_map = &isl_map_intersect_domain,
1176 return gen_bin_op(umap, uset, &control);
1179 /* Intersect the domain of "umap" with "uset".
1180 * If "uset" is a parameters domain, then intersect the parameter
1181 * domain of "umap" with this set.
1183 __isl_give isl_union_map *isl_union_map_intersect_domain(
1184 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1186 if (isl_union_set_is_params(uset))
1187 return union_map_intersect_params(umap, uset);
1188 else
1189 return union_map_intersect_domain(umap, uset);
1192 /* Remove the elements of "uset" from the domain of "umap".
1194 __isl_give isl_union_map *isl_union_map_subtract_domain(
1195 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1197 struct isl_bin_op_control control = {
1198 .subtract = 1,
1199 .match_space = &isl_space_domain,
1200 .fn_map = &isl_map_subtract_domain,
1203 return gen_bin_op(umap, dom, &control);
1206 /* Remove the elements of "uset" from the range of "umap".
1208 __isl_give isl_union_map *isl_union_map_subtract_range(
1209 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1211 struct isl_bin_op_control control = {
1212 .subtract = 1,
1213 .match_space = &isl_space_range,
1214 .fn_map = &isl_map_subtract_range,
1217 return gen_bin_op(umap, dom, &control);
1220 /* Compute the gist of "umap" with respect to the domain "uset".
1222 static __isl_give isl_union_map *union_map_gist_domain(
1223 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1225 struct isl_bin_op_control control = {
1226 .match_space = &isl_space_domain,
1227 .fn_map = &isl_map_gist_domain,
1230 return gen_bin_op(umap, uset, &control);
1233 /* Compute the gist of "umap" with respect to the domain "uset".
1234 * If "uset" is a parameters domain, then compute the gist
1235 * with respect to this parameter domain.
1237 __isl_give isl_union_map *isl_union_map_gist_domain(
1238 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1240 if (isl_union_set_is_params(uset))
1241 return union_map_gist_params(umap, uset);
1242 else
1243 return union_map_gist_domain(umap, uset);
1246 /* Compute the gist of "umap" with respect to the range "uset".
1248 __isl_give isl_union_map *isl_union_map_gist_range(
1249 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1251 struct isl_bin_op_control control = {
1252 .match_space = &isl_space_range,
1253 .fn_map = &isl_map_gist_range,
1256 return gen_bin_op(umap, uset, &control);
1259 __isl_give isl_union_map *isl_union_map_intersect_range(
1260 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1262 struct isl_bin_op_control control = {
1263 .match_space = &isl_space_range,
1264 .fn_map = &isl_map_intersect_range,
1267 return gen_bin_op(umap, uset, &control);
1270 /* Intersect each map in "umap" in a space A -> [B -> C]
1271 * with the corresponding map in "factor" in the space A -> C and
1272 * collect the results.
1274 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1275 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1277 struct isl_bin_op_control control = {
1278 .filter = &isl_map_range_is_wrapping,
1279 .match_space = &isl_space_range_factor_range,
1280 .fn_map = &isl_map_intersect_range_factor_range,
1283 return gen_bin_op(umap, factor, &control);
1286 struct isl_union_map_bin_data {
1287 isl_union_map *umap2;
1288 isl_union_map *res;
1289 isl_map *map;
1290 isl_stat (*fn)(void **entry, void *user);
1293 static isl_stat apply_range_entry(void **entry, void *user)
1295 struct isl_union_map_bin_data *data = user;
1296 isl_map *map2 = *entry;
1297 isl_bool empty;
1299 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1300 map2->dim, isl_dim_in))
1301 return isl_stat_ok;
1303 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1305 empty = isl_map_is_empty(map2);
1306 if (empty < 0) {
1307 isl_map_free(map2);
1308 return isl_stat_error;
1310 if (empty) {
1311 isl_map_free(map2);
1312 return isl_stat_ok;
1315 data->res = isl_union_map_add_map(data->res, map2);
1317 return isl_stat_ok;
1320 static isl_stat bin_entry(void **entry, void *user)
1322 struct isl_union_map_bin_data *data = user;
1323 isl_map *map = *entry;
1325 data->map = map;
1326 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1327 data->fn, data) < 0)
1328 return isl_stat_error;
1330 return isl_stat_ok;
1333 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1334 __isl_take isl_union_map *umap2,
1335 isl_stat (*fn)(void **entry, void *user))
1337 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1339 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1340 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1342 if (!umap1 || !umap2)
1343 goto error;
1345 data.umap2 = umap2;
1346 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1347 umap1->table.n);
1348 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1349 &bin_entry, &data) < 0)
1350 goto error;
1352 isl_union_map_free(umap1);
1353 isl_union_map_free(umap2);
1354 return data.res;
1355 error:
1356 isl_union_map_free(umap1);
1357 isl_union_map_free(umap2);
1358 isl_union_map_free(data.res);
1359 return NULL;
1362 __isl_give isl_union_map *isl_union_map_apply_range(
1363 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1365 return bin_op(umap1, umap2, &apply_range_entry);
1368 __isl_give isl_union_map *isl_union_map_apply_domain(
1369 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1371 umap1 = isl_union_map_reverse(umap1);
1372 umap1 = isl_union_map_apply_range(umap1, umap2);
1373 return isl_union_map_reverse(umap1);
1376 __isl_give isl_union_set *isl_union_set_apply(
1377 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1379 return isl_union_map_apply_range(uset, umap);
1382 static isl_stat map_lex_lt_entry(void **entry, void *user)
1384 struct isl_union_map_bin_data *data = user;
1385 isl_map *map2 = *entry;
1387 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1388 map2->dim, isl_dim_out))
1389 return isl_stat_ok;
1391 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1393 data->res = isl_union_map_add_map(data->res, map2);
1395 return isl_stat_ok;
1398 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1399 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1401 return bin_op(umap1, umap2, &map_lex_lt_entry);
1404 static isl_stat map_lex_le_entry(void **entry, void *user)
1406 struct isl_union_map_bin_data *data = user;
1407 isl_map *map2 = *entry;
1409 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1410 map2->dim, isl_dim_out))
1411 return isl_stat_ok;
1413 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1415 data->res = isl_union_map_add_map(data->res, map2);
1417 return isl_stat_ok;
1420 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1421 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1423 return bin_op(umap1, umap2, &map_lex_le_entry);
1426 static isl_stat product_entry(void **entry, void *user)
1428 struct isl_union_map_bin_data *data = user;
1429 isl_map *map2 = *entry;
1431 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1433 data->res = isl_union_map_add_map(data->res, map2);
1435 return isl_stat_ok;
1438 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1439 __isl_take isl_union_map *umap2)
1441 return bin_op(umap1, umap2, &product_entry);
1444 static isl_stat set_product_entry(void **entry, void *user)
1446 struct isl_union_map_bin_data *data = user;
1447 isl_set *set2 = *entry;
1449 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1451 data->res = isl_union_set_add_set(data->res, set2);
1453 return isl_stat_ok;
1456 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1457 __isl_take isl_union_set *uset2)
1459 return bin_op(uset1, uset2, &set_product_entry);
1462 static isl_stat domain_product_entry(void **entry, void *user)
1464 struct isl_union_map_bin_data *data = user;
1465 isl_map *map2 = *entry;
1467 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1468 map2->dim, isl_dim_out))
1469 return isl_stat_ok;
1471 map2 = isl_map_domain_product(isl_map_copy(data->map),
1472 isl_map_copy(map2));
1474 data->res = isl_union_map_add_map(data->res, map2);
1476 return isl_stat_ok;
1479 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1481 __isl_give isl_union_map *isl_union_map_domain_product(
1482 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1484 return bin_op(umap1, umap2, &domain_product_entry);
1487 static isl_stat range_product_entry(void **entry, void *user)
1489 struct isl_union_map_bin_data *data = user;
1490 isl_map *map2 = *entry;
1492 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1493 map2->dim, isl_dim_in))
1494 return isl_stat_ok;
1496 map2 = isl_map_range_product(isl_map_copy(data->map),
1497 isl_map_copy(map2));
1499 data->res = isl_union_map_add_map(data->res, map2);
1501 return isl_stat_ok;
1504 __isl_give isl_union_map *isl_union_map_range_product(
1505 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1507 return bin_op(umap1, umap2, &range_product_entry);
1510 /* If data->map A -> B and "map2" C -> D have the same range space,
1511 * then add (A, C) -> (B * D) to data->res.
1513 static isl_stat flat_domain_product_entry(void **entry, void *user)
1515 struct isl_union_map_bin_data *data = user;
1516 isl_map *map2 = *entry;
1518 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1519 map2->dim, isl_dim_out))
1520 return isl_stat_ok;
1522 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1523 isl_map_copy(map2));
1525 data->res = isl_union_map_add_map(data->res, map2);
1527 return isl_stat_ok;
1530 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1532 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1533 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1535 return bin_op(umap1, umap2, &flat_domain_product_entry);
1538 static isl_stat flat_range_product_entry(void **entry, void *user)
1540 struct isl_union_map_bin_data *data = user;
1541 isl_map *map2 = *entry;
1543 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1544 map2->dim, isl_dim_in))
1545 return isl_stat_ok;
1547 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1548 isl_map_copy(map2));
1550 data->res = isl_union_map_add_map(data->res, map2);
1552 return isl_stat_ok;
1555 __isl_give isl_union_map *isl_union_map_flat_range_product(
1556 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1558 return bin_op(umap1, umap2, &flat_range_product_entry);
1561 /* Data structure that specifies how un_op should modify
1562 * the maps in the union map.
1564 * If "inplace" is set, then the maps in the input union map
1565 * are modified in place. This means that "fn_map" should not
1566 * change the meaning of the map or that the union map only
1567 * has a single reference.
1568 * If "total" is set, then all maps need to be modified and
1569 * the results need to live in the same space.
1570 * Otherwise, a new union map is constructed to store the results.
1571 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1572 * are taken into account. "filter_user" is passed as the second argument
1573 * to "filter". No filter can be set if "inplace" or
1574 * "total" is set.
1575 * "fn_map" specifies how the maps (selected by "filter")
1576 * should be transformed.
1578 struct isl_un_op_control {
1579 int inplace;
1580 int total;
1581 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1582 void *filter_user;
1583 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1586 /* Data structure for wrapping the data for un_op_filter_drop_user.
1587 * "filter" is the function that is being wrapped.
1589 struct isl_un_op_drop_user_data {
1590 isl_bool (*filter)(__isl_keep isl_map *map);
1593 /* Wrapper for isl_un_op_control filters that do not require
1594 * a second argument.
1595 * Simply call data->filter without the second argument.
1597 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1599 struct isl_un_op_drop_user_data *data = user;
1600 return data->filter(map);
1603 /* Internal data structure for "un_op".
1604 * "control" specifies how the maps in the union map should be modified.
1605 * "res" collects the results.
1607 struct isl_union_map_un_data {
1608 struct isl_un_op_control *control;
1609 isl_union_map *res;
1612 /* isl_hash_table_foreach callback for un_op.
1613 * Handle the map that "entry" points to.
1615 * If control->filter is set, then check if this map satisfies the filter.
1616 * If so (or if control->filter is not set), modify the map
1617 * by calling control->fn_map and either add the result to data->res or
1618 * replace the original entry by the result (if control->inplace is set).
1620 static isl_stat un_entry(void **entry, void *user)
1622 struct isl_union_map_un_data *data = user;
1623 isl_map *map = *entry;
1625 if (data->control->filter) {
1626 isl_bool ok;
1628 ok = data->control->filter(map, data->control->filter_user);
1629 if (ok < 0)
1630 return isl_stat_error;
1631 if (!ok)
1632 return isl_stat_ok;
1635 map = data->control->fn_map(isl_map_copy(map));
1636 if (!map)
1637 return isl_stat_error;
1638 if (data->control->inplace) {
1639 isl_map_free(*entry);
1640 *entry = map;
1641 } else {
1642 data->res = isl_union_map_add_map(data->res, map);
1643 if (!data->res)
1644 return isl_stat_error;
1647 return isl_stat_ok;
1650 /* Modify the maps in "umap" based on "control".
1651 * If control->inplace is set, then modify the maps in "umap" in-place.
1652 * Otherwise, create a new union map to hold the results.
1653 * If control->total is set, then perform an inplace computation
1654 * if "umap" is only referenced once. Otherwise, create a new union map
1655 * to store the results.
1657 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1658 struct isl_un_op_control *control)
1660 struct isl_union_map_un_data data = { control };
1662 if (!umap)
1663 return NULL;
1664 if ((control->inplace || control->total) && control->filter)
1665 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1666 "inplace/total modification cannot be filtered",
1667 return isl_union_map_free(umap));
1669 if (control->total && umap->ref == 1)
1670 control->inplace = 1;
1671 if (control->inplace) {
1672 data.res = umap;
1673 } else {
1674 isl_space *space;
1676 space = isl_union_map_get_space(umap);
1677 data.res = isl_union_map_alloc(space, umap->table.n);
1679 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1680 &umap->table, &un_entry, &data) < 0)
1681 data.res = isl_union_map_free(data.res);
1683 if (control->inplace)
1684 return data.res;
1685 isl_union_map_free(umap);
1686 return data.res;
1689 __isl_give isl_union_map *isl_union_map_from_range(
1690 __isl_take isl_union_set *uset)
1692 struct isl_un_op_control control = {
1693 .fn_map = &isl_map_from_range,
1695 return un_op(uset, &control);
1698 __isl_give isl_union_map *isl_union_map_from_domain(
1699 __isl_take isl_union_set *uset)
1701 return isl_union_map_reverse(isl_union_map_from_range(uset));
1704 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1705 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1707 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1708 isl_union_map_from_range(range));
1711 /* Modify the maps in "umap" by applying "fn" on them.
1712 * "fn" should apply to all maps in "umap" and should not modify the space.
1714 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1715 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1717 struct isl_un_op_control control = {
1718 .total = 1,
1719 .fn_map = fn,
1722 return un_op(umap, &control);
1725 /* Compute the affine hull of "map" and return the result as an isl_map.
1727 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1729 return isl_map_from_basic_map(isl_map_affine_hull(map));
1732 __isl_give isl_union_map *isl_union_map_affine_hull(
1733 __isl_take isl_union_map *umap)
1735 return total(umap, &isl_map_affine_hull_map);
1738 __isl_give isl_union_set *isl_union_set_affine_hull(
1739 __isl_take isl_union_set *uset)
1741 return isl_union_map_affine_hull(uset);
1744 /* Wrapper around isl_set_combined_lineality_space
1745 * that returns the combined lineality space in the form of an isl_set
1746 * instead of an isl_basic_set.
1748 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1750 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1753 /* For each set in "uset", compute the (linear) hull
1754 * of the lineality spaces of its basic sets and
1755 * collect and return the results.
1757 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1758 __isl_take isl_union_set *uset)
1760 struct isl_un_op_control control = {
1761 .fn_map = &combined_lineality_space,
1763 return un_op(uset, &control);
1766 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1768 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1770 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1773 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1774 __isl_take isl_union_map *umap)
1776 return total(umap, &isl_map_polyhedral_hull_map);
1779 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1780 __isl_take isl_union_set *uset)
1782 return isl_union_map_polyhedral_hull(uset);
1785 /* Compute a superset of the convex hull of "map" that is described
1786 * by only translates of the constraints in the constituents of "map" and
1787 * return the result as an isl_map.
1789 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1791 return isl_map_from_basic_map(isl_map_simple_hull(map));
1794 __isl_give isl_union_map *isl_union_map_simple_hull(
1795 __isl_take isl_union_map *umap)
1797 return total(umap, &isl_map_simple_hull_map);
1800 __isl_give isl_union_set *isl_union_set_simple_hull(
1801 __isl_take isl_union_set *uset)
1803 return isl_union_map_simple_hull(uset);
1806 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1807 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1809 struct isl_un_op_control control = {
1810 .inplace = 1,
1811 .fn_map = fn,
1814 return un_op(umap, &control);
1817 /* Remove redundant constraints in each of the basic maps of "umap".
1818 * Since removing redundant constraints does not change the meaning
1819 * or the space, the operation can be performed in-place.
1821 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1822 __isl_take isl_union_map *umap)
1824 return inplace(umap, &isl_map_remove_redundancies);
1827 /* Remove redundant constraints in each of the basic sets of "uset".
1829 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1830 __isl_take isl_union_set *uset)
1832 return isl_union_map_remove_redundancies(uset);
1835 __isl_give isl_union_map *isl_union_map_coalesce(
1836 __isl_take isl_union_map *umap)
1838 return inplace(umap, &isl_map_coalesce);
1841 __isl_give isl_union_set *isl_union_set_coalesce(
1842 __isl_take isl_union_set *uset)
1844 return isl_union_map_coalesce(uset);
1847 __isl_give isl_union_map *isl_union_map_detect_equalities(
1848 __isl_take isl_union_map *umap)
1850 return inplace(umap, &isl_map_detect_equalities);
1853 __isl_give isl_union_set *isl_union_set_detect_equalities(
1854 __isl_take isl_union_set *uset)
1856 return isl_union_map_detect_equalities(uset);
1859 __isl_give isl_union_map *isl_union_map_compute_divs(
1860 __isl_take isl_union_map *umap)
1862 return inplace(umap, &isl_map_compute_divs);
1865 __isl_give isl_union_set *isl_union_set_compute_divs(
1866 __isl_take isl_union_set *uset)
1868 return isl_union_map_compute_divs(uset);
1871 __isl_give isl_union_map *isl_union_map_lexmin(
1872 __isl_take isl_union_map *umap)
1874 return total(umap, &isl_map_lexmin);
1877 __isl_give isl_union_set *isl_union_set_lexmin(
1878 __isl_take isl_union_set *uset)
1880 return isl_union_map_lexmin(uset);
1883 __isl_give isl_union_map *isl_union_map_lexmax(
1884 __isl_take isl_union_map *umap)
1886 return total(umap, &isl_map_lexmax);
1889 __isl_give isl_union_set *isl_union_set_lexmax(
1890 __isl_take isl_union_set *uset)
1892 return isl_union_map_lexmax(uset);
1895 /* Return the universe in the space of "map".
1897 static __isl_give isl_map *universe(__isl_take isl_map *map)
1899 isl_space *space;
1901 space = isl_map_get_space(map);
1902 isl_map_free(map);
1903 return isl_map_universe(space);
1906 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1908 struct isl_un_op_control control = {
1909 .fn_map = &universe,
1911 return un_op(umap, &control);
1914 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1916 return isl_union_map_universe(uset);
1919 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1921 struct isl_un_op_control control = {
1922 .fn_map = &isl_map_reverse,
1924 return un_op(umap, &control);
1927 /* Compute the parameter domain of the given union map.
1929 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1931 struct isl_un_op_control control = {
1932 .fn_map = &isl_map_params,
1934 int empty;
1936 empty = isl_union_map_is_empty(umap);
1937 if (empty < 0)
1938 goto error;
1939 if (empty) {
1940 isl_space *space;
1941 space = isl_union_map_get_space(umap);
1942 isl_union_map_free(umap);
1943 return isl_set_empty(space);
1945 return isl_set_from_union_set(un_op(umap, &control));
1946 error:
1947 isl_union_map_free(umap);
1948 return NULL;
1951 /* Compute the parameter domain of the given union set.
1953 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1955 return isl_union_map_params(uset);
1958 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1960 struct isl_un_op_control control = {
1961 .fn_map = &isl_map_domain,
1963 return un_op(umap, &control);
1966 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1968 struct isl_un_op_control control = {
1969 .fn_map = &isl_map_range,
1971 return un_op(umap, &control);
1974 __isl_give isl_union_map *isl_union_map_domain_map(
1975 __isl_take isl_union_map *umap)
1977 struct isl_un_op_control control = {
1978 .fn_map = &isl_map_domain_map,
1980 return un_op(umap, &control);
1983 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1984 * add the result to "res".
1986 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1988 isl_union_pw_multi_aff **res = user;
1989 isl_multi_aff *ma;
1990 isl_pw_multi_aff *pma;
1992 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1993 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1994 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1996 return *res ? isl_stat_ok : isl_stat_error;
2000 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2001 * to its domain.
2003 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2004 __isl_take isl_union_map *umap)
2006 isl_union_pw_multi_aff *res;
2008 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2009 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2010 res = isl_union_pw_multi_aff_free(res);
2012 isl_union_map_free(umap);
2013 return res;
2016 __isl_give isl_union_map *isl_union_map_range_map(
2017 __isl_take isl_union_map *umap)
2019 struct isl_un_op_control control = {
2020 .fn_map = &isl_map_range_map,
2022 return un_op(umap, &control);
2025 /* Given a collection of wrapped maps of the form A[B -> C],
2026 * return the collection of maps A[B -> C] -> B.
2028 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2029 __isl_take isl_union_set *uset)
2031 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2032 struct isl_un_op_control control = {
2033 .filter = &un_op_filter_drop_user,
2034 .filter_user = &data,
2035 .fn_map = &isl_set_wrapped_domain_map,
2037 return un_op(uset, &control);
2040 /* Does "map" relate elements from the same space?
2042 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2044 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2045 map->dim, isl_dim_out);
2048 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2050 struct isl_un_op_control control = {
2051 .filter = &equal_tuples,
2052 .fn_map = &isl_map_deltas,
2054 return un_op(umap, &control);
2057 __isl_give isl_union_map *isl_union_map_deltas_map(
2058 __isl_take isl_union_map *umap)
2060 struct isl_un_op_control control = {
2061 .filter = &equal_tuples,
2062 .fn_map = &isl_map_deltas_map,
2064 return un_op(umap, &control);
2067 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2069 struct isl_un_op_control control = {
2070 .fn_map = &isl_set_identity,
2072 return un_op(uset, &control);
2075 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2077 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2079 isl_union_pw_multi_aff **res = user;
2080 isl_space *space;
2081 isl_pw_multi_aff *pma;
2083 space = isl_space_map_from_set(isl_set_get_space(set));
2084 pma = isl_pw_multi_aff_identity(space);
2085 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2086 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2088 return *res ? isl_stat_ok : isl_stat_error;
2091 /* Return an identity function on "uset" in the form
2092 * of an isl_union_pw_multi_aff.
2094 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2095 __isl_take isl_union_set *uset)
2097 isl_union_pw_multi_aff *res;
2099 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2100 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2101 res = isl_union_pw_multi_aff_free(res);
2103 isl_union_set_free(uset);
2104 return res;
2107 /* For each map in "umap" of the form [A -> B] -> C,
2108 * construct the map A -> C and collect the results.
2110 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2111 __isl_take isl_union_map *umap)
2113 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2114 struct isl_un_op_control control = {
2115 .filter = &un_op_filter_drop_user,
2116 .filter_user = &data,
2117 .fn_map = &isl_map_domain_factor_domain,
2119 return un_op(umap, &control);
2122 /* For each map in "umap" of the form [A -> B] -> C,
2123 * construct the map B -> C and collect the results.
2125 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2126 __isl_take isl_union_map *umap)
2128 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2129 struct isl_un_op_control control = {
2130 .filter = &un_op_filter_drop_user,
2131 .filter_user = &data,
2132 .fn_map = &isl_map_domain_factor_range,
2134 return un_op(umap, &control);
2137 /* For each map in "umap" of the form A -> [B -> C],
2138 * construct the map A -> B and collect the results.
2140 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2141 __isl_take isl_union_map *umap)
2143 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2144 struct isl_un_op_control control = {
2145 .filter = &un_op_filter_drop_user,
2146 .filter_user = &data,
2147 .fn_map = &isl_map_range_factor_domain,
2149 return un_op(umap, &control);
2152 /* For each map in "umap" of the form A -> [B -> C],
2153 * construct the map A -> C and collect the results.
2155 __isl_give isl_union_map *isl_union_map_range_factor_range(
2156 __isl_take isl_union_map *umap)
2158 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2159 struct isl_un_op_control control = {
2160 .filter = &un_op_filter_drop_user,
2161 .filter_user = &data,
2162 .fn_map = &isl_map_range_factor_range,
2164 return un_op(umap, &control);
2167 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2168 * construct the map A -> C and collect the results.
2170 __isl_give isl_union_map *isl_union_map_factor_domain(
2171 __isl_take isl_union_map *umap)
2173 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2174 struct isl_un_op_control control = {
2175 .filter = &un_op_filter_drop_user,
2176 .filter_user = &data,
2177 .fn_map = &isl_map_factor_domain,
2179 return un_op(umap, &control);
2182 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2183 * construct the map B -> D and collect the results.
2185 __isl_give isl_union_map *isl_union_map_factor_range(
2186 __isl_take isl_union_map *umap)
2188 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2189 struct isl_un_op_control control = {
2190 .filter = &un_op_filter_drop_user,
2191 .filter_user = &data,
2192 .fn_map = &isl_map_factor_range,
2194 return un_op(umap, &control);
2197 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2199 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2200 struct isl_un_op_control control = {
2201 .filter = &un_op_filter_drop_user,
2202 .filter_user = &data,
2203 .fn_map = &isl_set_unwrap,
2205 return un_op(uset, &control);
2208 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2210 struct isl_un_op_control control = {
2211 .fn_map = &isl_map_wrap,
2213 return un_op(umap, &control);
2216 struct isl_union_map_is_subset_data {
2217 isl_union_map *umap2;
2218 isl_bool is_subset;
2221 static isl_stat is_subset_entry(void **entry, void *user)
2223 struct isl_union_map_is_subset_data *data = user;
2224 uint32_t hash;
2225 struct isl_hash_table_entry *entry2;
2226 isl_map *map = *entry;
2228 hash = isl_space_get_hash(map->dim);
2229 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2230 hash, &has_space, map->dim, 0);
2231 if (!entry2) {
2232 int empty = isl_map_is_empty(map);
2233 if (empty < 0)
2234 return isl_stat_error;
2235 if (empty)
2236 return isl_stat_ok;
2237 data->is_subset = 0;
2238 return isl_stat_error;
2241 data->is_subset = isl_map_is_subset(map, entry2->data);
2242 if (data->is_subset < 0 || !data->is_subset)
2243 return isl_stat_error;
2245 return isl_stat_ok;
2248 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2249 __isl_keep isl_union_map *umap2)
2251 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2253 umap1 = isl_union_map_copy(umap1);
2254 umap2 = isl_union_map_copy(umap2);
2255 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2256 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2258 if (!umap1 || !umap2)
2259 goto error;
2261 data.umap2 = umap2;
2262 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2263 &is_subset_entry, &data) < 0 &&
2264 data.is_subset)
2265 goto error;
2267 isl_union_map_free(umap1);
2268 isl_union_map_free(umap2);
2270 return data.is_subset;
2271 error:
2272 isl_union_map_free(umap1);
2273 isl_union_map_free(umap2);
2274 return isl_bool_error;
2277 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2278 __isl_keep isl_union_set *uset2)
2280 return isl_union_map_is_subset(uset1, uset2);
2283 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2284 __isl_keep isl_union_map *umap2)
2286 isl_bool is_subset;
2288 if (!umap1 || !umap2)
2289 return isl_bool_error;
2290 is_subset = isl_union_map_is_subset(umap1, umap2);
2291 if (is_subset != isl_bool_true)
2292 return is_subset;
2293 is_subset = isl_union_map_is_subset(umap2, umap1);
2294 return is_subset;
2297 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2298 __isl_keep isl_union_set *uset2)
2300 return isl_union_map_is_equal(uset1, uset2);
2303 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2304 __isl_keep isl_union_map *umap2)
2306 isl_bool is_subset;
2308 if (!umap1 || !umap2)
2309 return isl_bool_error;
2310 is_subset = isl_union_map_is_subset(umap1, umap2);
2311 if (is_subset != isl_bool_true)
2312 return is_subset;
2313 is_subset = isl_union_map_is_subset(umap2, umap1);
2314 if (is_subset == isl_bool_error)
2315 return is_subset;
2316 return !is_subset;
2319 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2320 __isl_keep isl_union_set *uset2)
2322 return isl_union_map_is_strict_subset(uset1, uset2);
2325 /* Internal data structure for isl_union_map_is_disjoint.
2326 * umap2 is the union map with which we are comparing.
2327 * is_disjoint is initialized to 1 and is set to 0 as soon
2328 * as the union maps turn out not to be disjoint.
2330 struct isl_union_map_is_disjoint_data {
2331 isl_union_map *umap2;
2332 isl_bool is_disjoint;
2335 /* Check if "map" is disjoint from data->umap2 and abort
2336 * the search if it is not.
2338 static isl_stat is_disjoint_entry(void **entry, void *user)
2340 struct isl_union_map_is_disjoint_data *data = user;
2341 uint32_t hash;
2342 struct isl_hash_table_entry *entry2;
2343 isl_map *map = *entry;
2345 hash = isl_space_get_hash(map->dim);
2346 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2347 hash, &has_space, map->dim, 0);
2348 if (!entry2)
2349 return isl_stat_ok;
2351 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2352 if (data->is_disjoint < 0 || !data->is_disjoint)
2353 return isl_stat_error;
2355 return isl_stat_ok;
2358 /* Are "umap1" and "umap2" disjoint?
2360 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2361 __isl_keep isl_union_map *umap2)
2363 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2365 umap1 = isl_union_map_copy(umap1);
2366 umap2 = isl_union_map_copy(umap2);
2367 umap1 = isl_union_map_align_params(umap1,
2368 isl_union_map_get_space(umap2));
2369 umap2 = isl_union_map_align_params(umap2,
2370 isl_union_map_get_space(umap1));
2372 if (!umap1 || !umap2)
2373 goto error;
2375 data.umap2 = umap2;
2376 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2377 &is_disjoint_entry, &data) < 0 &&
2378 data.is_disjoint)
2379 goto error;
2381 isl_union_map_free(umap1);
2382 isl_union_map_free(umap2);
2384 return data.is_disjoint;
2385 error:
2386 isl_union_map_free(umap1);
2387 isl_union_map_free(umap2);
2388 return isl_bool_error;
2391 /* Are "uset1" and "uset2" disjoint?
2393 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2394 __isl_keep isl_union_set *uset2)
2396 return isl_union_map_is_disjoint(uset1, uset2);
2399 static isl_stat sample_entry(void **entry, void *user)
2401 isl_basic_map **sample = (isl_basic_map **)user;
2402 isl_map *map = *entry;
2404 *sample = isl_map_sample(isl_map_copy(map));
2405 if (!*sample)
2406 return isl_stat_error;
2407 if (!isl_basic_map_plain_is_empty(*sample))
2408 return isl_stat_error;
2409 return isl_stat_ok;
2412 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2414 isl_basic_map *sample = NULL;
2416 if (!umap)
2417 return NULL;
2419 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2420 &sample_entry, &sample) < 0 &&
2421 !sample)
2422 goto error;
2424 if (!sample)
2425 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2427 isl_union_map_free(umap);
2429 return sample;
2430 error:
2431 isl_union_map_free(umap);
2432 return NULL;
2435 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2437 return bset_from_bmap(isl_union_map_sample(uset));
2440 /* Return an element in "uset" in the form of an isl_point.
2441 * Return a void isl_point if "uset" is empty.
2443 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2445 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2448 struct isl_forall_data {
2449 isl_bool res;
2450 isl_bool (*fn)(__isl_keep isl_map *map);
2453 static isl_stat forall_entry(void **entry, void *user)
2455 struct isl_forall_data *data = user;
2456 isl_map *map = *entry;
2458 data->res = data->fn(map);
2459 if (data->res < 0)
2460 return isl_stat_error;
2462 if (!data->res)
2463 return isl_stat_error;
2465 return isl_stat_ok;
2468 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2469 isl_bool (*fn)(__isl_keep isl_map *map))
2471 struct isl_forall_data data = { isl_bool_true, fn };
2473 if (!umap)
2474 return isl_bool_error;
2476 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2477 &forall_entry, &data) < 0 && data.res)
2478 return isl_bool_error;
2480 return data.res;
2483 struct isl_forall_user_data {
2484 isl_bool res;
2485 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2486 void *user;
2489 static isl_stat forall_user_entry(void **entry, void *user)
2491 struct isl_forall_user_data *data = user;
2492 isl_map *map = *entry;
2494 data->res = data->fn(map, data->user);
2495 if (data->res < 0)
2496 return isl_stat_error;
2498 if (!data->res)
2499 return isl_stat_error;
2501 return isl_stat_ok;
2504 /* Check if fn(map, user) returns true for all maps "map" in umap.
2506 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2507 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2509 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2511 if (!umap)
2512 return isl_bool_error;
2514 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2515 &forall_user_entry, &data) < 0 && data.res)
2516 return isl_bool_error;
2518 return data.res;
2521 /* Is "umap" obviously empty?
2523 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2525 if (!umap)
2526 return isl_bool_error;
2527 return isl_union_map_n_map(umap) == 0;
2530 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2532 return union_map_forall(umap, &isl_map_is_empty);
2535 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2537 return isl_union_map_is_empty(uset);
2540 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2542 isl_bool is_subset;
2543 isl_space *dim;
2544 isl_map *id;
2546 if (!map)
2547 return isl_bool_error;
2549 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2550 map->dim, isl_dim_out))
2551 return isl_bool_false;
2553 dim = isl_map_get_space(map);
2554 id = isl_map_identity(dim);
2556 is_subset = isl_map_is_subset(map, id);
2558 isl_map_free(id);
2560 return is_subset;
2563 /* Given an isl_union_map that consists of a single map, check
2564 * if it is single-valued.
2566 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2568 isl_map *map;
2569 isl_bool sv;
2571 umap = isl_union_map_copy(umap);
2572 map = isl_map_from_union_map(umap);
2573 sv = isl_map_is_single_valued(map);
2574 isl_map_free(map);
2576 return sv;
2579 /* Internal data structure for single_valued_on_domain.
2581 * "umap" is the union map to be tested.
2582 * "sv" is set to 1 as long as "umap" may still be single-valued.
2584 struct isl_union_map_is_sv_data {
2585 isl_union_map *umap;
2586 isl_bool sv;
2589 /* Check if the data->umap is single-valued on "set".
2591 * If data->umap consists of a single map on "set", then test it
2592 * as an isl_map.
2594 * Otherwise, compute
2596 * M \circ M^-1
2598 * check if the result is a subset of the identity mapping and
2599 * store the result in data->sv.
2601 * Terminate as soon as data->umap has been determined not to
2602 * be single-valued.
2604 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2606 struct isl_union_map_is_sv_data *data = user;
2607 isl_union_map *umap, *test;
2609 umap = isl_union_map_copy(data->umap);
2610 umap = isl_union_map_intersect_domain(umap,
2611 isl_union_set_from_set(set));
2613 if (isl_union_map_n_map(umap) == 1) {
2614 data->sv = single_map_is_single_valued(umap);
2615 isl_union_map_free(umap);
2616 } else {
2617 test = isl_union_map_reverse(isl_union_map_copy(umap));
2618 test = isl_union_map_apply_range(test, umap);
2620 data->sv = union_map_forall(test, &is_subset_of_identity);
2622 isl_union_map_free(test);
2625 if (data->sv < 0 || !data->sv)
2626 return isl_stat_error;
2627 return isl_stat_ok;
2630 /* Check if the given map is single-valued.
2632 * If the union map consists of a single map, then test it as an isl_map.
2633 * Otherwise, check if the union map is single-valued on each of its
2634 * domain spaces.
2636 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2638 isl_union_map *universe;
2639 isl_union_set *domain;
2640 struct isl_union_map_is_sv_data data;
2642 if (isl_union_map_n_map(umap) == 1)
2643 return single_map_is_single_valued(umap);
2645 universe = isl_union_map_universe(isl_union_map_copy(umap));
2646 domain = isl_union_map_domain(universe);
2648 data.sv = isl_bool_true;
2649 data.umap = umap;
2650 if (isl_union_set_foreach_set(domain,
2651 &single_valued_on_domain, &data) < 0 && data.sv)
2652 data.sv = isl_bool_error;
2653 isl_union_set_free(domain);
2655 return data.sv;
2658 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2660 isl_bool in;
2662 umap = isl_union_map_copy(umap);
2663 umap = isl_union_map_reverse(umap);
2664 in = isl_union_map_is_single_valued(umap);
2665 isl_union_map_free(umap);
2667 return in;
2670 /* Is "map" obviously not an identity relation because
2671 * it maps elements from one space to another space?
2672 * Update *non_identity accordingly.
2674 * In particular, if the domain and range spaces are the same,
2675 * then the map is not considered to obviously not be an identity relation.
2676 * Otherwise, the map is considered to obviously not be an identity relation
2677 * if it is is non-empty.
2679 * If "map" is determined to obviously not be an identity relation,
2680 * then the search is aborted.
2682 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2684 isl_bool *non_identity = user;
2685 isl_bool equal;
2686 isl_space *space;
2688 space = isl_map_get_space(map);
2689 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2690 if (equal >= 0 && !equal)
2691 *non_identity = isl_bool_not(isl_map_is_empty(map));
2692 else
2693 *non_identity = isl_bool_not(equal);
2694 isl_space_free(space);
2695 isl_map_free(map);
2697 if (*non_identity < 0 || *non_identity)
2698 return isl_stat_error;
2700 return isl_stat_ok;
2703 /* Is "umap" obviously not an identity relation because
2704 * it maps elements from one space to another space?
2706 * As soon as a map has been found that maps elements to a different space,
2707 * non_identity is changed and the search is aborted.
2709 static isl_bool isl_union_map_plain_is_not_identity(
2710 __isl_keep isl_union_map *umap)
2712 isl_bool non_identity;
2714 non_identity = isl_bool_false;
2715 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2716 &non_identity) < 0 &&
2717 non_identity == isl_bool_false)
2718 return isl_bool_error;
2720 return non_identity;
2723 /* Does "map" only map elements to themselves?
2724 * Update *identity accordingly.
2726 * If "map" is determined not to be an identity relation,
2727 * then the search is aborted.
2729 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2731 isl_bool *identity = user;
2733 *identity = isl_map_is_identity(map);
2734 isl_map_free(map);
2736 if (*identity < 0 || !*identity)
2737 return isl_stat_error;
2739 return isl_stat_ok;
2742 /* Does "umap" only map elements to themselves?
2744 * First check if there are any maps that map elements to different spaces.
2745 * If not, then check that all the maps (between identical spaces)
2746 * are identity relations.
2748 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2750 isl_bool non_identity;
2751 isl_bool identity;
2753 non_identity = isl_union_map_plain_is_not_identity(umap);
2754 if (non_identity < 0 || non_identity)
2755 return isl_bool_not(non_identity);
2757 identity = isl_bool_true;
2758 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2759 identity == isl_bool_true)
2760 return isl_bool_error;
2762 return identity;
2765 /* Represents a map that has a fixed value (v) for one of its
2766 * range dimensions.
2767 * The map in this structure is not reference counted, so it
2768 * is only valid while the isl_union_map from which it was
2769 * obtained is still alive.
2771 struct isl_fixed_map {
2772 isl_int v;
2773 isl_map *map;
2776 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2777 int n)
2779 int i;
2780 struct isl_fixed_map *v;
2782 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2783 if (!v)
2784 return NULL;
2785 for (i = 0; i < n; ++i)
2786 isl_int_init(v[i].v);
2787 return v;
2790 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2792 int i;
2794 if (!v)
2795 return;
2796 for (i = 0; i < n; ++i)
2797 isl_int_clear(v[i].v);
2798 free(v);
2801 /* Compare the "v" field of two isl_fixed_map structs.
2803 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2805 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2806 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2808 return isl_int_cmp(e1->v, e2->v);
2811 /* Internal data structure used while checking whether all maps
2812 * in a union_map have a fixed value for a given output dimension.
2813 * v is the list of maps, with the fixed value for the dimension
2814 * n is the number of maps considered so far
2815 * pos is the output dimension under investigation
2817 struct isl_fixed_dim_data {
2818 struct isl_fixed_map *v;
2819 int n;
2820 int pos;
2823 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2825 struct isl_fixed_dim_data *data = user;
2827 data->v[data->n].map = map;
2828 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2829 &data->v[data->n++].v);
2832 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2833 int first, int n_range);
2835 /* Given a list of the maps, with their fixed values at output dimension "pos",
2836 * check whether the ranges of the maps form an obvious partition.
2838 * We first sort the maps according to their fixed values.
2839 * If all maps have a different value, then we know the ranges form
2840 * a partition.
2841 * Otherwise, we collect the maps with the same fixed value and
2842 * check whether each such collection is obviously injective
2843 * based on later dimensions.
2845 static int separates(struct isl_fixed_map *v, int n,
2846 __isl_take isl_space *dim, int pos, int n_range)
2848 int i;
2850 if (!v)
2851 goto error;
2853 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2855 for (i = 0; i + 1 < n; ++i) {
2856 int j, k;
2857 isl_union_map *part;
2858 int injective;
2860 for (j = i + 1; j < n; ++j)
2861 if (isl_int_ne(v[i].v, v[j].v))
2862 break;
2864 if (j == i + 1)
2865 continue;
2867 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2868 for (k = i; k < j; ++k)
2869 part = isl_union_map_add_map(part,
2870 isl_map_copy(v[k].map));
2872 injective = plain_injective_on_range(part, pos + 1, n_range);
2873 if (injective < 0)
2874 goto error;
2875 if (!injective)
2876 break;
2878 i = j - 1;
2881 isl_space_free(dim);
2882 free_isl_fixed_map_array(v, n);
2883 return i + 1 >= n;
2884 error:
2885 isl_space_free(dim);
2886 free_isl_fixed_map_array(v, n);
2887 return -1;
2890 /* Check whether the maps in umap have obviously distinct ranges.
2891 * In particular, check for an output dimension in the range
2892 * [first,n_range) for which all maps have a fixed value
2893 * and then check if these values, possibly along with fixed values
2894 * at later dimensions, entail distinct ranges.
2896 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2897 int first, int n_range)
2899 isl_ctx *ctx;
2900 int n;
2901 struct isl_fixed_dim_data data = { NULL };
2903 ctx = isl_union_map_get_ctx(umap);
2905 n = isl_union_map_n_map(umap);
2906 if (!umap)
2907 goto error;
2909 if (n <= 1) {
2910 isl_union_map_free(umap);
2911 return isl_bool_true;
2914 if (first >= n_range) {
2915 isl_union_map_free(umap);
2916 return isl_bool_false;
2919 data.v = alloc_isl_fixed_map_array(ctx, n);
2920 if (!data.v)
2921 goto error;
2923 for (data.pos = first; data.pos < n_range; ++data.pos) {
2924 isl_bool fixed;
2925 int injective;
2926 isl_space *dim;
2928 data.n = 0;
2929 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2930 if (fixed < 0)
2931 goto error;
2932 if (!fixed)
2933 continue;
2934 dim = isl_union_map_get_space(umap);
2935 injective = separates(data.v, n, dim, data.pos, n_range);
2936 isl_union_map_free(umap);
2937 return injective;
2940 free_isl_fixed_map_array(data.v, n);
2941 isl_union_map_free(umap);
2943 return isl_bool_false;
2944 error:
2945 free_isl_fixed_map_array(data.v, n);
2946 isl_union_map_free(umap);
2947 return isl_bool_error;
2950 /* Check whether the maps in umap that map to subsets of "ran"
2951 * have obviously distinct ranges.
2953 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2954 void *user)
2956 isl_union_map *umap = user;
2958 umap = isl_union_map_copy(umap);
2959 umap = isl_union_map_intersect_range(umap,
2960 isl_union_set_from_set(isl_set_copy(ran)));
2961 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2964 /* Check if the given union_map is obviously injective.
2966 * In particular, we first check if all individual maps are obviously
2967 * injective and then check if all the ranges of these maps are
2968 * obviously disjoint.
2970 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2972 isl_bool in;
2973 isl_union_map *univ;
2974 isl_union_set *ran;
2976 in = union_map_forall(umap, &isl_map_plain_is_injective);
2977 if (in < 0)
2978 return isl_bool_error;
2979 if (!in)
2980 return isl_bool_false;
2982 univ = isl_union_map_universe(isl_union_map_copy(umap));
2983 ran = isl_union_map_range(univ);
2985 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2987 isl_union_set_free(ran);
2989 return in;
2992 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2994 isl_bool sv;
2996 sv = isl_union_map_is_single_valued(umap);
2997 if (sv < 0 || !sv)
2998 return sv;
3000 return isl_union_map_is_injective(umap);
3003 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3005 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3006 struct isl_un_op_control control = {
3007 .filter = &un_op_filter_drop_user,
3008 .filter_user = &data,
3009 .fn_map = &isl_map_zip,
3011 return un_op(umap, &control);
3014 /* Given a union map, take the maps of the form A -> (B -> C) and
3015 * return the union of the corresponding maps (A -> B) -> C.
3017 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3019 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3020 struct isl_un_op_control control = {
3021 .filter = &un_op_filter_drop_user,
3022 .filter_user = &data,
3023 .fn_map = &isl_map_uncurry,
3025 return un_op(umap, &control);
3028 /* Given a union map, take the maps of the form (A -> B) -> C and
3029 * return the union of the corresponding maps A -> (B -> C).
3031 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3033 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3034 struct isl_un_op_control control = {
3035 .filter = &un_op_filter_drop_user,
3036 .filter_user = &data,
3037 .fn_map = &isl_map_curry,
3039 return un_op(umap, &control);
3042 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3043 * return the union of the corresponding maps A -> (B -> (C -> D)).
3045 __isl_give isl_union_map *isl_union_map_range_curry(
3046 __isl_take isl_union_map *umap)
3048 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3049 struct isl_un_op_control control = {
3050 .filter = &un_op_filter_drop_user,
3051 .filter_user = &data,
3052 .fn_map = &isl_map_range_curry,
3054 return un_op(umap, &control);
3057 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3059 struct isl_un_op_control control = {
3060 .fn_map = &isl_set_lift,
3062 return un_op(uset, &control);
3065 static isl_stat coefficients_entry(void **entry, void *user)
3067 isl_set *set = *entry;
3068 isl_union_set **res = user;
3070 set = isl_set_copy(set);
3071 set = isl_set_from_basic_set(isl_set_coefficients(set));
3072 *res = isl_union_set_add_set(*res, set);
3074 return isl_stat_ok;
3077 __isl_give isl_union_set *isl_union_set_coefficients(
3078 __isl_take isl_union_set *uset)
3080 isl_ctx *ctx;
3081 isl_space *dim;
3082 isl_union_set *res;
3084 if (!uset)
3085 return NULL;
3087 ctx = isl_union_set_get_ctx(uset);
3088 dim = isl_space_set_alloc(ctx, 0, 0);
3089 res = isl_union_map_alloc(dim, uset->table.n);
3090 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3091 &coefficients_entry, &res) < 0)
3092 goto error;
3094 isl_union_set_free(uset);
3095 return res;
3096 error:
3097 isl_union_set_free(uset);
3098 isl_union_set_free(res);
3099 return NULL;
3102 static isl_stat solutions_entry(void **entry, void *user)
3104 isl_set *set = *entry;
3105 isl_union_set **res = user;
3107 set = isl_set_copy(set);
3108 set = isl_set_from_basic_set(isl_set_solutions(set));
3109 if (!*res)
3110 *res = isl_union_set_from_set(set);
3111 else
3112 *res = isl_union_set_add_set(*res, set);
3114 if (!*res)
3115 return isl_stat_error;
3117 return isl_stat_ok;
3120 __isl_give isl_union_set *isl_union_set_solutions(
3121 __isl_take isl_union_set *uset)
3123 isl_union_set *res = NULL;
3125 if (!uset)
3126 return NULL;
3128 if (uset->table.n == 0) {
3129 res = isl_union_set_empty(isl_union_set_get_space(uset));
3130 isl_union_set_free(uset);
3131 return res;
3134 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3135 &solutions_entry, &res) < 0)
3136 goto error;
3138 isl_union_set_free(uset);
3139 return res;
3140 error:
3141 isl_union_set_free(uset);
3142 isl_union_set_free(res);
3143 return NULL;
3146 /* Is the domain space of "map" equal to "space"?
3148 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3150 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3151 space, isl_dim_out);
3154 /* Is the range space of "map" equal to "space"?
3156 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3158 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3159 space, isl_dim_out);
3162 /* Is the set space of "map" equal to "space"?
3164 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3166 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3167 space, isl_dim_out);
3170 /* Internal data structure for preimage_pw_multi_aff.
3172 * "pma" is the function under which the preimage should be taken.
3173 * "space" is the space of "pma".
3174 * "res" collects the results.
3175 * "fn" computes the preimage for a given map.
3176 * "match" returns true if "fn" can be called.
3178 struct isl_union_map_preimage_data {
3179 isl_space *space;
3180 isl_pw_multi_aff *pma;
3181 isl_union_map *res;
3182 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3183 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3184 __isl_take isl_pw_multi_aff *pma);
3187 /* Call data->fn to compute the preimage of the domain or range of *entry
3188 * under the function represented by data->pma, provided the domain/range
3189 * space of *entry matches the target space of data->pma
3190 * (as given by data->match), and add the result to data->res.
3192 static isl_stat preimage_entry(void **entry, void *user)
3194 int m;
3195 isl_map *map = *entry;
3196 struct isl_union_map_preimage_data *data = user;
3197 isl_bool empty;
3199 m = data->match(map, data->space);
3200 if (m < 0)
3201 return isl_stat_error;
3202 if (!m)
3203 return isl_stat_ok;
3205 map = isl_map_copy(map);
3206 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3208 empty = isl_map_is_empty(map);
3209 if (empty < 0 || empty) {
3210 isl_map_free(map);
3211 return empty < 0 ? isl_stat_error : isl_stat_ok;
3214 data->res = isl_union_map_add_map(data->res, map);
3216 return isl_stat_ok;
3219 /* Compute the preimage of the domain or range of "umap" under the function
3220 * represented by "pma".
3221 * In other words, plug in "pma" in the domain or range of "umap".
3222 * The function "fn" performs the actual preimage computation on a map,
3223 * while "match" determines to which maps the function should be applied.
3225 static __isl_give isl_union_map *preimage_pw_multi_aff(
3226 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3227 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3228 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3229 __isl_take isl_pw_multi_aff *pma))
3231 isl_ctx *ctx;
3232 isl_space *space;
3233 struct isl_union_map_preimage_data data;
3235 umap = isl_union_map_align_params(umap,
3236 isl_pw_multi_aff_get_space(pma));
3237 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3239 if (!umap || !pma)
3240 goto error;
3242 ctx = isl_union_map_get_ctx(umap);
3243 space = isl_union_map_get_space(umap);
3244 data.space = isl_pw_multi_aff_get_space(pma);
3245 data.pma = pma;
3246 data.res = isl_union_map_alloc(space, umap->table.n);
3247 data.match = match;
3248 data.fn = fn;
3249 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3250 &data) < 0)
3251 data.res = isl_union_map_free(data.res);
3253 isl_space_free(data.space);
3254 isl_union_map_free(umap);
3255 isl_pw_multi_aff_free(pma);
3256 return data.res;
3257 error:
3258 isl_union_map_free(umap);
3259 isl_pw_multi_aff_free(pma);
3260 return NULL;
3263 /* Compute the preimage of the domain of "umap" under the function
3264 * represented by "pma".
3265 * In other words, plug in "pma" in the domain of "umap".
3266 * The result contains maps that live in the same spaces as the maps of "umap"
3267 * with domain space equal to the target space of "pma",
3268 * except that the domain has been replaced by the domain space of "pma".
3270 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3271 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3273 return preimage_pw_multi_aff(umap, pma, &domain_match,
3274 &isl_map_preimage_domain_pw_multi_aff);
3277 /* Compute the preimage of the range of "umap" under the function
3278 * represented by "pma".
3279 * In other words, plug in "pma" in the range of "umap".
3280 * The result contains maps that live in the same spaces as the maps of "umap"
3281 * with range space equal to the target space of "pma",
3282 * except that the range has been replaced by the domain space of "pma".
3284 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3285 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3287 return preimage_pw_multi_aff(umap, pma, &range_match,
3288 &isl_map_preimage_range_pw_multi_aff);
3291 /* Compute the preimage of "uset" under the function represented by "pma".
3292 * In other words, plug in "pma" in "uset".
3293 * The result contains sets that live in the same spaces as the sets of "uset"
3294 * with space equal to the target space of "pma",
3295 * except that the space has been replaced by the domain space of "pma".
3297 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3298 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3300 return preimage_pw_multi_aff(uset, pma, &set_match,
3301 &isl_set_preimage_pw_multi_aff);
3304 /* Compute the preimage of the domain of "umap" under the function
3305 * represented by "ma".
3306 * In other words, plug in "ma" in the domain of "umap".
3307 * The result contains maps that live in the same spaces as the maps of "umap"
3308 * with domain space equal to the target space of "ma",
3309 * except that the domain has been replaced by the domain space of "ma".
3311 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3312 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3314 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3315 isl_pw_multi_aff_from_multi_aff(ma));
3318 /* Compute the preimage of the range of "umap" under the function
3319 * represented by "ma".
3320 * In other words, plug in "ma" in the range of "umap".
3321 * The result contains maps that live in the same spaces as the maps of "umap"
3322 * with range space equal to the target space of "ma",
3323 * except that the range has been replaced by the domain space of "ma".
3325 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3326 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3328 return isl_union_map_preimage_range_pw_multi_aff(umap,
3329 isl_pw_multi_aff_from_multi_aff(ma));
3332 /* Compute the preimage of "uset" under the function represented by "ma".
3333 * In other words, plug in "ma" in "uset".
3334 * The result contains sets that live in the same spaces as the sets of "uset"
3335 * with space equal to the target space of "ma",
3336 * except that the space has been replaced by the domain space of "ma".
3338 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3339 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3341 return isl_union_set_preimage_pw_multi_aff(uset,
3342 isl_pw_multi_aff_from_multi_aff(ma));
3345 /* Internal data structure for preimage_multi_pw_aff.
3347 * "mpa" is the function under which the preimage should be taken.
3348 * "space" is the space of "mpa".
3349 * "res" collects the results.
3350 * "fn" computes the preimage for a given map.
3351 * "match" returns true if "fn" can be called.
3353 struct isl_union_map_preimage_mpa_data {
3354 isl_space *space;
3355 isl_multi_pw_aff *mpa;
3356 isl_union_map *res;
3357 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3358 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3359 __isl_take isl_multi_pw_aff *mpa);
3362 /* Call data->fn to compute the preimage of the domain or range of *entry
3363 * under the function represented by data->mpa, provided the domain/range
3364 * space of *entry matches the target space of data->mpa
3365 * (as given by data->match), and add the result to data->res.
3367 static isl_stat preimage_mpa_entry(void **entry, void *user)
3369 int m;
3370 isl_map *map = *entry;
3371 struct isl_union_map_preimage_mpa_data *data = user;
3372 isl_bool empty;
3374 m = data->match(map, data->space);
3375 if (m < 0)
3376 return isl_stat_error;
3377 if (!m)
3378 return isl_stat_ok;
3380 map = isl_map_copy(map);
3381 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3383 empty = isl_map_is_empty(map);
3384 if (empty < 0 || empty) {
3385 isl_map_free(map);
3386 return empty < 0 ? isl_stat_error : isl_stat_ok;
3389 data->res = isl_union_map_add_map(data->res, map);
3391 return isl_stat_ok;
3394 /* Compute the preimage of the domain or range of "umap" under the function
3395 * represented by "mpa".
3396 * In other words, plug in "mpa" in the domain or range of "umap".
3397 * The function "fn" performs the actual preimage computation on a map,
3398 * while "match" determines to which maps the function should be applied.
3400 static __isl_give isl_union_map *preimage_multi_pw_aff(
3401 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3402 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3403 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3404 __isl_take isl_multi_pw_aff *mpa))
3406 isl_ctx *ctx;
3407 isl_space *space;
3408 struct isl_union_map_preimage_mpa_data data;
3410 umap = isl_union_map_align_params(umap,
3411 isl_multi_pw_aff_get_space(mpa));
3412 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3414 if (!umap || !mpa)
3415 goto error;
3417 ctx = isl_union_map_get_ctx(umap);
3418 space = isl_union_map_get_space(umap);
3419 data.space = isl_multi_pw_aff_get_space(mpa);
3420 data.mpa = mpa;
3421 data.res = isl_union_map_alloc(space, umap->table.n);
3422 data.match = match;
3423 data.fn = fn;
3424 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3425 &data) < 0)
3426 data.res = isl_union_map_free(data.res);
3428 isl_space_free(data.space);
3429 isl_union_map_free(umap);
3430 isl_multi_pw_aff_free(mpa);
3431 return data.res;
3432 error:
3433 isl_union_map_free(umap);
3434 isl_multi_pw_aff_free(mpa);
3435 return NULL;
3438 /* Compute the preimage of the domain of "umap" under the function
3439 * represented by "mpa".
3440 * In other words, plug in "mpa" in the domain of "umap".
3441 * The result contains maps that live in the same spaces as the maps of "umap"
3442 * with domain space equal to the target space of "mpa",
3443 * except that the domain has been replaced by the domain space of "mpa".
3445 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3446 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3448 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3449 &isl_map_preimage_domain_multi_pw_aff);
3452 /* Internal data structure for preimage_upma.
3454 * "umap" is the map of which the preimage should be computed.
3455 * "res" collects the results.
3456 * "fn" computes the preimage for a given piecewise multi-affine function.
3458 struct isl_union_map_preimage_upma_data {
3459 isl_union_map *umap;
3460 isl_union_map *res;
3461 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3462 __isl_take isl_pw_multi_aff *pma);
3465 /* Call data->fn to compute the preimage of the domain or range of data->umap
3466 * under the function represented by pma and add the result to data->res.
3468 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3470 struct isl_union_map_preimage_upma_data *data = user;
3471 isl_union_map *umap;
3473 umap = isl_union_map_copy(data->umap);
3474 umap = data->fn(umap, pma);
3475 data->res = isl_union_map_union(data->res, umap);
3477 return data->res ? isl_stat_ok : isl_stat_error;
3480 /* Compute the preimage of the domain or range of "umap" under the function
3481 * represented by "upma".
3482 * In other words, plug in "upma" in the domain or range of "umap".
3483 * The function "fn" performs the actual preimage computation
3484 * on a piecewise multi-affine function.
3486 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3487 __isl_take isl_union_map *umap,
3488 __isl_take isl_union_pw_multi_aff *upma,
3489 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3490 __isl_take isl_pw_multi_aff *pma))
3492 struct isl_union_map_preimage_upma_data data;
3494 data.umap = umap;
3495 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3496 data.fn = fn;
3497 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3498 &preimage_upma, &data) < 0)
3499 data.res = isl_union_map_free(data.res);
3501 isl_union_map_free(umap);
3502 isl_union_pw_multi_aff_free(upma);
3504 return data.res;
3507 /* Compute the preimage of the domain of "umap" under the function
3508 * represented by "upma".
3509 * In other words, plug in "upma" in the domain of "umap".
3510 * The result contains maps that live in the same spaces as the maps of "umap"
3511 * with domain space equal to one of the target spaces of "upma",
3512 * except that the domain has been replaced by one of the domain spaces that
3513 * correspond to that target space of "upma".
3515 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3516 __isl_take isl_union_map *umap,
3517 __isl_take isl_union_pw_multi_aff *upma)
3519 return preimage_union_pw_multi_aff(umap, upma,
3520 &isl_union_map_preimage_domain_pw_multi_aff);
3523 /* Compute the preimage of the range of "umap" under the function
3524 * represented by "upma".
3525 * In other words, plug in "upma" in the range of "umap".
3526 * The result contains maps that live in the same spaces as the maps of "umap"
3527 * with range space equal to one of the target spaces of "upma",
3528 * except that the range has been replaced by one of the domain spaces that
3529 * correspond to that target space of "upma".
3531 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3532 __isl_take isl_union_map *umap,
3533 __isl_take isl_union_pw_multi_aff *upma)
3535 return preimage_union_pw_multi_aff(umap, upma,
3536 &isl_union_map_preimage_range_pw_multi_aff);
3539 /* Compute the preimage of "uset" under the function represented by "upma".
3540 * In other words, plug in "upma" in the range of "uset".
3541 * The result contains sets that live in the same spaces as the sets of "uset"
3542 * with space equal to one of the target spaces of "upma",
3543 * except that the space has been replaced by one of the domain spaces that
3544 * correspond to that target space of "upma".
3546 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3547 __isl_take isl_union_set *uset,
3548 __isl_take isl_union_pw_multi_aff *upma)
3550 return preimage_union_pw_multi_aff(uset, upma,
3551 &isl_union_set_preimage_pw_multi_aff);
3554 /* Reset the user pointer on all identifiers of parameters and tuples
3555 * of the spaces of "umap".
3557 __isl_give isl_union_map *isl_union_map_reset_user(
3558 __isl_take isl_union_map *umap)
3560 umap = isl_union_map_cow(umap);
3561 if (!umap)
3562 return NULL;
3563 umap->dim = isl_space_reset_user(umap->dim);
3564 if (!umap->dim)
3565 return isl_union_map_free(umap);
3566 return total(umap, &isl_map_reset_user);
3569 /* Reset the user pointer on all identifiers of parameters and tuples
3570 * of the spaces of "uset".
3572 __isl_give isl_union_set *isl_union_set_reset_user(
3573 __isl_take isl_union_set *uset)
3575 return isl_union_map_reset_user(uset);
3578 /* Remove all existentially quantified variables and integer divisions
3579 * from "umap" using Fourier-Motzkin elimination.
3581 __isl_give isl_union_map *isl_union_map_remove_divs(
3582 __isl_take isl_union_map *umap)
3584 return total(umap, &isl_map_remove_divs);
3587 /* Remove all existentially quantified variables and integer divisions
3588 * from "uset" using Fourier-Motzkin elimination.
3590 __isl_give isl_union_set *isl_union_set_remove_divs(
3591 __isl_take isl_union_set *uset)
3593 return isl_union_map_remove_divs(uset);
3596 /* Internal data structure for isl_union_map_project_out.
3597 * "type", "first" and "n" are the arguments for the isl_map_project_out
3598 * call.
3599 * "res" collects the results.
3601 struct isl_union_map_project_out_data {
3602 enum isl_dim_type type;
3603 unsigned first;
3604 unsigned n;
3606 isl_union_map *res;
3609 /* Turn the data->n dimensions of type data->type, starting at data->first
3610 * into existentially quantified variables and add the result to data->res.
3612 static isl_stat project_out(__isl_take isl_map *map, void *user)
3614 struct isl_union_map_project_out_data *data = user;
3616 map = isl_map_project_out(map, data->type, data->first, data->n);
3617 data->res = isl_union_map_add_map(data->res, map);
3619 return isl_stat_ok;
3622 /* Turn the "n" dimensions of type "type", starting at "first"
3623 * into existentially quantified variables.
3624 * Since the space of an isl_union_map only contains parameters,
3625 * type is required to be equal to isl_dim_param.
3627 __isl_give isl_union_map *isl_union_map_project_out(
3628 __isl_take isl_union_map *umap,
3629 enum isl_dim_type type, unsigned first, unsigned n)
3631 isl_space *space;
3632 struct isl_union_map_project_out_data data = { type, first, n };
3634 if (!umap)
3635 return NULL;
3637 if (type != isl_dim_param)
3638 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3639 "can only project out parameters",
3640 return isl_union_map_free(umap));
3642 space = isl_union_map_get_space(umap);
3643 space = isl_space_drop_dims(space, type, first, n);
3644 data.res = isl_union_map_empty(space);
3645 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3646 data.res = isl_union_map_free(data.res);
3648 isl_union_map_free(umap);
3650 return data.res;
3653 /* Project out all parameters from "umap" by existentially quantifying
3654 * over them.
3656 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3657 __isl_take isl_union_map *umap)
3659 unsigned n;
3661 if (!umap)
3662 return NULL;
3663 n = isl_union_map_dim(umap, isl_dim_param);
3664 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3667 /* Turn the "n" dimensions of type "type", starting at "first"
3668 * into existentially quantified variables.
3669 * Since the space of an isl_union_set only contains parameters,
3670 * "type" is required to be equal to isl_dim_param.
3672 __isl_give isl_union_set *isl_union_set_project_out(
3673 __isl_take isl_union_set *uset,
3674 enum isl_dim_type type, unsigned first, unsigned n)
3676 return isl_union_map_project_out(uset, type, first, n);
3679 /* Internal data structure for isl_union_map_involves_dims.
3680 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3682 struct isl_union_map_involves_dims_data {
3683 unsigned first;
3684 unsigned n;
3687 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3689 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3691 struct isl_union_map_involves_dims_data *data = user;
3692 isl_bool involves;
3694 involves = isl_map_involves_dims(map,
3695 isl_dim_param, data->first, data->n);
3696 if (involves < 0)
3697 return isl_bool_error;
3698 return !involves;
3701 /* Does "umap" involve any of the n parameters starting at first?
3702 * "type" is required to be set to isl_dim_param.
3704 * "umap" involves any of those parameters if any of its maps
3705 * involve the parameters. In other words, "umap" does not
3706 * involve any of the parameters if all its maps to not
3707 * involve the parameters.
3709 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3710 enum isl_dim_type type, unsigned first, unsigned n)
3712 struct isl_union_map_involves_dims_data data = { first, n };
3713 isl_bool excludes;
3715 if (type != isl_dim_param)
3716 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3717 "can only reference parameters", return isl_bool_error);
3719 excludes = union_map_forall_user(umap, &map_excludes, &data);
3721 if (excludes < 0)
3722 return isl_bool_error;
3724 return !excludes;
3727 /* Internal data structure for isl_union_map_reset_range_space.
3728 * "range" is the space from which to set the range space.
3729 * "res" collects the results.
3731 struct isl_union_map_reset_range_space_data {
3732 isl_space *range;
3733 isl_union_map *res;
3736 /* Replace the range space of "map" by the range space of data->range and
3737 * add the result to data->res.
3739 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3741 struct isl_union_map_reset_range_space_data *data = user;
3742 isl_space *space;
3744 space = isl_map_get_space(map);
3745 space = isl_space_domain(space);
3746 space = isl_space_extend_domain_with_range(space,
3747 isl_space_copy(data->range));
3748 map = isl_map_reset_space(map, space);
3749 data->res = isl_union_map_add_map(data->res, map);
3751 return data->res ? isl_stat_ok : isl_stat_error;
3754 /* Replace the range space of all the maps in "umap" by
3755 * the range space of "space".
3757 * This assumes that all maps have the same output dimension.
3758 * This function should therefore not be made publicly available.
3760 * Since the spaces of the maps change, so do their hash value.
3761 * We therefore need to create a new isl_union_map.
3763 __isl_give isl_union_map *isl_union_map_reset_range_space(
3764 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3766 struct isl_union_map_reset_range_space_data data = { space };
3768 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3769 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3770 data.res = isl_union_map_free(data.res);
3772 isl_space_free(space);
3773 isl_union_map_free(umap);
3774 return data.res;
3777 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3778 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3779 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3780 * to the domain and the range of each map.
3781 * "res" collects the results.
3783 struct isl_union_order_at_data {
3784 isl_multi_union_pw_aff *mupa;
3785 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3786 __isl_take isl_multi_pw_aff *mpa2);
3787 isl_union_map *res;
3790 /* Intersect "map" with the result of applying data->order to
3791 * the functions in data->mupa that apply to the domain and the range
3792 * of "map" and add the result to data->res.
3794 static isl_stat order_at(__isl_take isl_map *map, void *user)
3796 struct isl_union_order_at_data *data = user;
3797 isl_space *space;
3798 isl_multi_pw_aff *mpa1, *mpa2;
3799 isl_map *order;
3801 space = isl_space_domain(isl_map_get_space(map));
3802 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3803 space = isl_space_range(isl_map_get_space(map));
3804 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3805 order = data->order(mpa1, mpa2);
3806 map = isl_map_intersect(map, order);
3807 data->res = isl_union_map_add_map(data->res, map);
3809 return data->res ? isl_stat_ok : isl_stat_error;
3812 /* Intersect each map in "umap" with the result of calling "order"
3813 * on the functions is "mupa" that apply to the domain and the range
3814 * of the map.
3816 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3817 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3818 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3819 __isl_take isl_multi_pw_aff *mpa2))
3821 struct isl_union_order_at_data data;
3823 umap = isl_union_map_align_params(umap,
3824 isl_multi_union_pw_aff_get_space(mupa));
3825 mupa = isl_multi_union_pw_aff_align_params(mupa,
3826 isl_union_map_get_space(umap));
3827 data.mupa = mupa;
3828 data.order = order;
3829 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3830 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3831 data.res = isl_union_map_free(data.res);
3833 isl_multi_union_pw_aff_free(mupa);
3834 isl_union_map_free(umap);
3835 return data.res;
3838 /* Return the subset of "umap" where the domain and the range
3839 * have equal "mupa" values.
3841 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3842 __isl_take isl_union_map *umap,
3843 __isl_take isl_multi_union_pw_aff *mupa)
3845 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3846 &isl_multi_pw_aff_eq_map);
3849 /* Return the subset of "umap" where the domain has a lexicographically
3850 * smaller "mupa" value than the range.
3852 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3853 __isl_take isl_union_map *umap,
3854 __isl_take isl_multi_union_pw_aff *mupa)
3856 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3857 &isl_multi_pw_aff_lex_lt_map);
3860 /* Return the subset of "umap" where the domain has a lexicographically
3861 * greater "mupa" value than the range.
3863 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3864 __isl_take isl_union_map *umap,
3865 __isl_take isl_multi_union_pw_aff *mupa)
3867 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3868 &isl_multi_pw_aff_lex_gt_map);
3871 /* Return the union of the elements in the list "list".
3873 __isl_give isl_union_set *isl_union_set_list_union(
3874 __isl_take isl_union_set_list *list)
3876 int i, n;
3877 isl_ctx *ctx;
3878 isl_space *space;
3879 isl_union_set *res;
3881 if (!list)
3882 return NULL;
3884 ctx = isl_union_set_list_get_ctx(list);
3885 space = isl_space_params_alloc(ctx, 0);
3886 res = isl_union_set_empty(space);
3888 n = isl_union_set_list_n_union_set(list);
3889 for (i = 0; i < n; ++i) {
3890 isl_union_set *uset_i;
3892 uset_i = isl_union_set_list_get_union_set(list, i);
3893 res = isl_union_set_union(res, uset_i);
3896 isl_union_set_list_free(list);
3897 return res;
3900 /* Update *hash with the hash value of "map".
3902 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3904 uint32_t *hash = user;
3905 uint32_t map_hash;
3907 map_hash = isl_map_get_hash(map);
3908 isl_hash_hash(*hash, map_hash);
3910 isl_map_free(map);
3911 return isl_stat_ok;
3914 /* Return a hash value that digests "umap".
3916 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3918 uint32_t hash;
3920 if (!umap)
3921 return 0;
3923 hash = isl_hash_init();
3924 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3925 return 0;
3927 return hash;
3930 /* Return a hash value that digests "uset".
3932 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3934 return isl_union_map_get_hash(uset);
3937 /* Add the number of basic sets in "set" to "n".
3939 static isl_stat add_n(__isl_take isl_set *set, void *user)
3941 int *n = user;
3943 *n += isl_set_n_basic_set(set);
3944 isl_set_free(set);
3946 return isl_stat_ok;
3949 /* Return the total number of basic sets in "uset".
3951 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
3953 int n = 0;
3955 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
3956 return -1;
3958 return n;
3961 /* Add the basic sets in "set" to "list".
3963 static isl_stat add_list(__isl_take isl_set *set, void *user)
3965 isl_basic_set_list **list = user;
3966 isl_basic_set_list *list_i;
3968 list_i = isl_set_get_basic_set_list(set);
3969 *list = isl_basic_set_list_concat(*list, list_i);
3970 isl_set_free(set);
3972 if (!*list)
3973 return isl_stat_error;
3974 return isl_stat_ok;
3977 /* Return a list containing all the basic sets in "uset".
3979 * First construct a list of the appropriate size and
3980 * then add all the elements.
3982 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
3983 __isl_keep isl_union_set *uset)
3985 int n;
3986 isl_ctx *ctx;
3987 isl_basic_set_list *list;
3989 if (!uset)
3990 return NULL;
3991 ctx = isl_union_set_get_ctx(uset);
3992 n = isl_union_set_n_basic_set(uset);
3993 if (n < 0)
3994 return NULL;
3995 list = isl_basic_set_list_alloc(ctx, n);
3996 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
3997 list = isl_basic_set_list_free(list);
3999 return list;
4002 /* Internal data structure for isl_union_map_remove_map_if.
4003 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4005 struct isl_union_map_remove_map_if_data {
4006 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4007 void *user;
4010 /* isl_un_op_control filter that negates the result of data->fn
4011 * called on "map".
4013 static isl_bool not(__isl_keep isl_map *map, void *user)
4015 struct isl_union_map_remove_map_if_data *data = user;
4017 return isl_bool_not(data->fn(map, data->user));
4020 /* Dummy isl_un_op_control transformation callback that
4021 * simply returns the input.
4023 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4025 return map;
4028 /* Call "fn" on every map in "umap" and remove those maps
4029 * for which the callback returns true.
4031 * Use un_op to keep only those maps that are not filtered out,
4032 * applying an identity transformation on them.
4034 __isl_give isl_union_map *isl_union_map_remove_map_if(
4035 __isl_take isl_union_map *umap,
4036 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4038 struct isl_union_map_remove_map_if_data data = { fn, user };
4039 struct isl_un_op_control control = {
4040 .filter = &not,
4041 .filter_user = &data,
4042 .fn_map = &map_id,
4044 return un_op(umap, &control);