isl_qpolynomial_eval: use isl_vec_add_els
[isl.git] / isl_union_map.c
blob2563e24d116da1df56eac0990d8b5e745d7356af
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_private.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>
31 #include <uset_to_umap.c>
32 #include <uset_from_umap.c>
34 /* Return the number of parameters of "umap", where "type"
35 * is required to be set to isl_dim_param.
37 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
38 enum isl_dim_type type)
40 if (!umap)
41 return 0;
43 if (type != isl_dim_param)
44 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
45 "can only reference parameters", return 0);
47 return isl_space_dim(umap->dim, type);
50 /* Return the number of parameters of "uset", where "type"
51 * is required to be set to isl_dim_param.
53 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
54 enum isl_dim_type type)
56 return isl_union_map_dim(uset, type);
59 /* Return the id of the specified dimension.
61 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
62 enum isl_dim_type type, unsigned pos)
64 if (!umap)
65 return NULL;
67 if (type != isl_dim_param)
68 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
69 "can only reference parameters", return NULL);
71 return isl_space_get_dim_id(umap->dim, type, pos);
74 /* Is this union set a parameter domain?
76 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
78 isl_set *set;
79 isl_bool params;
81 if (!uset)
82 return isl_bool_error;
83 if (uset->table.n != 1)
84 return isl_bool_false;
86 set = isl_set_from_union_set(isl_union_set_copy(uset));
87 params = isl_set_is_params(set);
88 isl_set_free(set);
89 return params;
92 /* Is this union map actually a parameter domain?
93 * Users should never call this function. Outside of isl,
94 * a union map can never be a parameter domain.
96 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
98 return isl_union_set_is_params(uset_from_umap(umap));
101 static __isl_give isl_union_map *isl_union_map_alloc(
102 __isl_take isl_space *space, int size)
104 isl_union_map *umap;
106 space = isl_space_params(space);
107 if (!space)
108 return NULL;
110 umap = isl_calloc_type(space->ctx, isl_union_map);
111 if (!umap) {
112 isl_space_free(space);
113 return NULL;
116 umap->ref = 1;
117 umap->dim = space;
118 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
119 return isl_union_map_free(umap);
121 return umap;
124 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
126 return isl_union_map_alloc(space, 16);
129 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
131 return isl_union_map_empty(space);
134 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
136 return umap ? umap->dim->ctx : NULL;
139 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
141 return uset ? uset->dim->ctx : NULL;
144 /* Return the space of "umap".
146 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
148 return umap ? umap->dim : NULL;
151 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
153 return isl_space_copy(isl_union_map_peek_space(umap));
156 /* Return the position of the parameter with the given name
157 * in "umap".
158 * Return -1 if no such dimension can be found.
160 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
161 enum isl_dim_type type, const char *name)
163 if (!umap)
164 return -1;
165 return isl_space_find_dim_by_name(umap->dim, type, name);
168 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
170 return isl_union_map_get_space(uset);
173 static isl_stat free_umap_entry(void **entry, void *user)
175 isl_map *map = *entry;
176 isl_map_free(map);
177 return isl_stat_ok;
180 static isl_stat add_map(__isl_take isl_map *map, void *user)
182 isl_union_map **umap = (isl_union_map **)user;
184 *umap = isl_union_map_add_map(*umap, map);
186 return isl_stat_ok;
189 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
191 isl_union_map *dup;
193 if (!umap)
194 return NULL;
196 dup = isl_union_map_empty(isl_space_copy(umap->dim));
197 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
198 goto error;
199 return dup;
200 error:
201 isl_union_map_free(dup);
202 return NULL;
205 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
207 if (!umap)
208 return NULL;
210 if (umap->ref == 1)
211 return umap;
212 umap->ref--;
213 return isl_union_map_dup(umap);
216 struct isl_union_align {
217 isl_reordering *exp;
218 isl_union_map *res;
221 static isl_stat align_entry(void **entry, void *user)
223 isl_map *map = *entry;
224 isl_reordering *exp;
225 struct isl_union_align *data = user;
227 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
228 isl_map_get_space(map));
230 data->res = isl_union_map_add_map(data->res,
231 isl_map_realign(isl_map_copy(map), exp));
233 return isl_stat_ok;
236 /* Align the parameters of umap along those of model.
237 * The result has the parameters of model first, in the same order
238 * as they appear in model, followed by any remaining parameters of
239 * umap that do not appear in model.
241 __isl_give isl_union_map *isl_union_map_align_params(
242 __isl_take isl_union_map *umap, __isl_take isl_space *model)
244 struct isl_union_align data = { NULL, NULL };
245 isl_bool equal_params;
247 if (!umap || !model)
248 goto error;
250 equal_params = isl_space_has_equal_params(umap->dim, model);
251 if (equal_params < 0)
252 goto error;
253 if (equal_params) {
254 isl_space_free(model);
255 return umap;
258 model = isl_space_params(model);
259 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
260 if (!data.exp)
261 goto error;
263 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
264 umap->table.n);
265 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
266 &align_entry, &data) < 0)
267 goto error;
269 isl_reordering_free(data.exp);
270 isl_union_map_free(umap);
271 isl_space_free(model);
272 return data.res;
273 error:
274 isl_reordering_free(data.exp);
275 isl_union_map_free(umap);
276 isl_union_map_free(data.res);
277 isl_space_free(model);
278 return NULL;
281 __isl_give isl_union_set *isl_union_set_align_params(
282 __isl_take isl_union_set *uset, __isl_take isl_space *model)
284 return isl_union_map_align_params(uset, model);
287 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
288 __isl_take isl_union_map *umap2)
290 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
291 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
293 umap1 = isl_union_map_cow(umap1);
295 if (!umap1 || !umap2)
296 goto error;
298 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
299 goto error;
301 isl_union_map_free(umap2);
303 return umap1;
304 error:
305 isl_union_map_free(umap1);
306 isl_union_map_free(umap2);
307 return NULL;
310 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
311 __isl_take isl_union_set *uset2)
313 return isl_union_map_union(uset1, uset2);
316 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
318 if (!umap)
319 return NULL;
321 umap->ref++;
322 return umap;
325 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
327 return isl_union_map_copy(uset);
330 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
332 if (!umap)
333 return NULL;
335 if (--umap->ref > 0)
336 return NULL;
338 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
339 &free_umap_entry, NULL);
340 isl_hash_table_clear(&umap->table);
341 isl_space_free(umap->dim);
342 free(umap);
343 return NULL;
346 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
348 return isl_union_map_free(uset);
351 /* Do "umap" and "space" have the same parameters?
353 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
354 __isl_keep isl_space *space)
356 isl_space *umap_space;
358 umap_space = isl_union_map_peek_space(umap);
359 return isl_space_has_equal_params(umap_space, space);
362 /* Do "uset" and "space" have the same parameters?
364 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
365 __isl_keep isl_space *space)
367 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
370 static int has_space(const void *entry, const void *val)
372 isl_map *map = (isl_map *)entry;
373 isl_space *space = (isl_space *) val;
375 return isl_space_is_equal(map->dim, space);
378 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
379 __isl_take isl_map *map)
381 uint32_t hash;
382 struct isl_hash_table_entry *entry;
383 isl_bool aligned;
385 if (!map || !umap)
386 goto error;
388 if (isl_map_plain_is_empty(map)) {
389 isl_map_free(map);
390 return umap;
393 aligned = isl_map_space_has_equal_params(map, umap->dim);
394 if (aligned < 0)
395 goto error;
396 if (!aligned) {
397 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
398 map = isl_map_align_params(map, isl_union_map_get_space(umap));
401 umap = isl_union_map_cow(umap);
403 if (!map || !umap)
404 goto error;
406 hash = isl_space_get_hash(map->dim);
407 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
408 &has_space, map->dim, 1);
409 if (!entry)
410 goto error;
412 if (!entry->data)
413 entry->data = map;
414 else {
415 entry->data = isl_map_union(entry->data, isl_map_copy(map));
416 if (!entry->data)
417 goto error;
418 isl_map_free(map);
421 return umap;
422 error:
423 isl_map_free(map);
424 isl_union_map_free(umap);
425 return NULL;
428 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
429 __isl_take isl_set *set)
431 return isl_union_map_add_map(uset, set_to_map(set));
434 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
436 isl_space *dim;
437 isl_union_map *umap;
439 if (!map)
440 return NULL;
442 dim = isl_map_get_space(map);
443 dim = isl_space_params(dim);
444 umap = isl_union_map_empty(dim);
445 umap = isl_union_map_add_map(umap, map);
447 return umap;
450 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
452 return isl_union_map_from_map(set_to_map(set));
455 __isl_give isl_union_map *isl_union_map_from_basic_map(
456 __isl_take isl_basic_map *bmap)
458 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
461 __isl_give isl_union_set *isl_union_set_from_basic_set(
462 __isl_take isl_basic_set *bset)
464 return isl_union_map_from_basic_map(bset);
467 struct isl_union_map_foreach_data
469 isl_stat (*fn)(__isl_take isl_map *map, void *user);
470 void *user;
473 static isl_stat call_on_copy(void **entry, void *user)
475 isl_map *map = *entry;
476 struct isl_union_map_foreach_data *data;
477 data = (struct isl_union_map_foreach_data *)user;
479 return data->fn(isl_map_copy(map), data->user);
482 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
484 return umap ? umap->table.n : 0;
487 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
489 return uset ? uset->table.n : 0;
492 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
493 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
495 struct isl_union_map_foreach_data data = { fn, user };
497 if (!umap)
498 return isl_stat_error;
500 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
501 &call_on_copy, &data);
504 /* Internal data structure for isl_union_map_every_map.
506 * "test" is the user-specified callback function.
507 * "user" is the user-specified callback function argument.
509 * "failed" is initialized to 0 and set to 1 if "test" fails
510 * on any map.
512 struct isl_union_map_every_data {
513 isl_bool (*test)(__isl_keep isl_map *map, void *user);
514 void *user;
515 int failed;
518 /* Call data->test on "map".
519 * If this fails, then set data->failed and abort.
521 static isl_stat call_every(void **entry, void *user)
523 isl_map *map = *entry;
524 struct isl_union_map_every_data *data = user;
525 isl_bool r;
527 r = data->test(map, data->user);
528 if (r < 0)
529 return isl_stat_error;
530 if (r)
531 return isl_stat_ok;
532 data->failed = 1;
533 return isl_stat_error;
536 /* Does "test" succeed on every map in "umap"?
538 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
539 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
541 struct isl_union_map_every_data data = { test, user, 0 };
542 isl_stat r;
544 if (!umap)
545 return isl_bool_error;
547 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
548 &call_every, &data);
549 if (r >= 0)
550 return isl_bool_true;
551 if (data.failed)
552 return isl_bool_false;
553 return isl_bool_error;
556 static isl_stat copy_map(void **entry, void *user)
558 isl_map *map = *entry;
559 isl_map **map_p = user;
561 *map_p = isl_map_copy(map);
563 return isl_stat_error;
566 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
568 isl_ctx *ctx;
569 isl_map *map = NULL;
571 if (!umap)
572 return NULL;
573 ctx = isl_union_map_get_ctx(umap);
574 if (umap->table.n != 1)
575 isl_die(ctx, isl_error_invalid,
576 "union map needs to contain elements in exactly "
577 "one space", goto error);
579 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
581 isl_union_map_free(umap);
583 return map;
584 error:
585 isl_union_map_free(umap);
586 return NULL;
589 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
591 return isl_map_from_union_map(uset);
594 /* Extract the map in "umap" that lives in the given space (ignoring
595 * parameters).
597 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
598 __isl_take isl_space *space)
600 uint32_t hash;
601 struct isl_hash_table_entry *entry;
603 space = isl_space_drop_dims(space, isl_dim_param,
604 0, isl_space_dim(space, isl_dim_param));
605 space = isl_space_align_params(space, isl_union_map_get_space(umap));
606 if (!umap || !space)
607 goto error;
609 hash = isl_space_get_hash(space);
610 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
611 &has_space, space, 0);
612 if (!entry)
613 return isl_map_empty(space);
614 isl_space_free(space);
615 return isl_map_copy(entry->data);
616 error:
617 isl_space_free(space);
618 return NULL;
621 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
622 __isl_take isl_space *dim)
624 return set_from_map(isl_union_map_extract_map(uset, dim));
627 /* Check if umap contains a map in the given space.
629 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
630 __isl_keep isl_space *space)
632 uint32_t hash;
633 struct isl_hash_table_entry *entry;
635 if (!umap || !space)
636 return isl_bool_error;
638 hash = isl_space_get_hash(space);
639 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
640 &has_space, space, 0);
641 return !!entry;
644 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
645 __isl_keep isl_space *space)
647 return isl_union_map_contains(uset, space);
650 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
651 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
653 return isl_union_map_foreach_map(uset,
654 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
657 struct isl_union_set_foreach_point_data {
658 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
659 void *user;
662 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
664 struct isl_union_set_foreach_point_data *data = user;
665 isl_stat r;
667 r = isl_set_foreach_point(set, data->fn, data->user);
668 isl_set_free(set);
670 return r;
673 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
674 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
676 struct isl_union_set_foreach_point_data data = { fn, user };
677 return isl_union_set_foreach_set(uset, &foreach_point, &data);
680 /* Data structure that specifies how gen_bin_op should
681 * construct results from the inputs.
683 * If "subtract" is set, then a map in the first input is copied to the result
684 * if there is no corresponding map in the second input.
685 * Otherwise, a map in the first input with no corresponding map
686 * in the second input is ignored.
687 * If "filter" is not NULL, then it specifies which maps in the first
688 * input may have a matching map in the second input.
689 * In particular, it makes sure that "match_space" can be called
690 * on the space of the map.
691 * "match_space" specifies how to transform the space of a map
692 * in the first input to the space of the corresponding map
693 * in the second input.
694 * "fn_map" specifies how the matching maps, one from each input,
695 * should be combined to form a map in the result.
697 struct isl_bin_op_control {
698 int subtract;
699 isl_bool (*filter)(__isl_keep isl_map *map);
700 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
701 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
702 __isl_take isl_map *map2);
705 /* Internal data structure for gen_bin_op.
706 * "control" specifies how the maps in the result should be constructed.
707 * "umap2" is a pointer to the second argument.
708 * "res" collects the results.
710 struct isl_union_map_gen_bin_data {
711 struct isl_bin_op_control *control;
712 isl_union_map *umap2;
713 isl_union_map *res;
716 /* Add a copy of "map" to "res" and return the result.
718 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
719 __isl_keep isl_map *map)
721 return isl_union_map_add_map(res, isl_map_copy(map));
724 /* Combine "map1" and "map2", add the result to "res" and return the result.
725 * Check whether the result is empty before adding it to "res".
727 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
728 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
729 struct isl_union_map_gen_bin_data *data)
731 isl_bool empty;
732 isl_map *map;
734 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
735 empty = isl_map_is_empty(map);
736 if (empty < 0 || empty) {
737 isl_map_free(map);
738 if (empty < 0)
739 return isl_union_map_free(res);
740 return res;
742 return isl_union_map_add_map(res, map);
745 /* Dummy match_space function that simply returns the input space.
747 static __isl_give isl_space *identity(__isl_take isl_space *space)
749 return space;
752 /* Look for the map in data->umap2 that corresponds to "map", if any.
753 * Return (isl_bool_true, matching map) if there is one,
754 * (isl_bool_false, NULL) if there is no matching map and
755 * (isl_bool_error, NULL) on error.
757 * If not NULL, then data->control->filter specifies whether "map"
758 * can have any matching map. If so,
759 * data->control->match_space specifies which map in data->umap2
760 * corresponds to "map".
762 static __isl_keep isl_maybe_isl_map bin_try_get_match(
763 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
765 uint32_t hash;
766 struct isl_hash_table_entry *entry2;
767 isl_space *space;
768 isl_maybe_isl_map res = { isl_bool_error, NULL };
770 if (data->control->filter) {
771 res.valid = data->control->filter(map);
772 if (res.valid < 0 || !res.valid)
773 return res;
774 res.valid = isl_bool_error;
777 space = isl_map_get_space(map);
778 if (data->control->match_space != &identity)
779 space = data->control->match_space(space);
780 if (!space)
781 return res;
782 hash = isl_space_get_hash(space);
783 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
784 &data->umap2->table, hash,
785 &has_space, space, 0);
786 isl_space_free(space);
787 res.valid = entry2 != NULL;
788 if (entry2)
789 res.value = entry2->data;
791 return res;
794 /* isl_hash_table_foreach callback for gen_bin_op.
795 * Look for the map in data->umap2 that corresponds
796 * to the map that "entry" points to, apply the binary operation and
797 * add the result to data->res.
799 * If no corresponding map can be found, then the effect depends
800 * on data->control->subtract. If it is set, then the current map
801 * is added directly to the result. Otherwise, it is ignored.
803 static isl_stat gen_bin_entry(void **entry, void *user)
805 struct isl_union_map_gen_bin_data *data = user;
806 isl_map *map = *entry;
807 isl_maybe_isl_map m;
809 m = bin_try_get_match(data, map);
810 if (m.valid < 0)
811 return isl_stat_error;
812 if (!m.valid && !data->control->subtract)
813 return isl_stat_ok;
815 if (!m.valid)
816 data->res = bin_add_map(data->res, map);
817 else
818 data->res = bin_add_pair(data->res, map, m.value, data);
819 if (!data->res)
820 return isl_stat_error;
822 return isl_stat_ok;
825 /* Apply a binary operation to "umap1" and "umap2" based on "control".
826 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
827 * in gen_bin_entry.
829 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
830 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
832 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
834 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
835 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
837 if (!umap1 || !umap2)
838 goto error;
840 data.umap2 = umap2;
841 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
842 umap1->table.n);
843 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
844 &gen_bin_entry, &data) < 0)
845 goto error;
847 isl_union_map_free(umap1);
848 isl_union_map_free(umap2);
849 return data.res;
850 error:
851 isl_union_map_free(umap1);
852 isl_union_map_free(umap2);
853 isl_union_map_free(data.res);
854 return NULL;
857 __isl_give isl_union_map *isl_union_map_subtract(
858 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
860 struct isl_bin_op_control control = {
861 .subtract = 1,
862 .match_space = &identity,
863 .fn_map = &isl_map_subtract,
866 return gen_bin_op(umap1, umap2, &control);
869 __isl_give isl_union_set *isl_union_set_subtract(
870 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
872 return isl_union_map_subtract(uset1, uset2);
875 struct isl_union_map_gen_bin_set_data {
876 isl_set *set;
877 isl_union_map *res;
880 static isl_stat intersect_params_entry(void **entry, void *user)
882 struct isl_union_map_gen_bin_set_data *data = user;
883 isl_map *map = *entry;
884 int empty;
886 map = isl_map_copy(map);
887 map = isl_map_intersect_params(map, isl_set_copy(data->set));
889 empty = isl_map_is_empty(map);
890 if (empty < 0) {
891 isl_map_free(map);
892 return isl_stat_error;
895 data->res = isl_union_map_add_map(data->res, map);
897 return isl_stat_ok;
900 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
901 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
903 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
905 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
906 set = isl_set_align_params(set, isl_union_map_get_space(umap));
908 if (!umap || !set)
909 goto error;
911 data.set = set;
912 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
913 umap->table.n);
914 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
915 fn, &data) < 0)
916 goto error;
918 isl_union_map_free(umap);
919 isl_set_free(set);
920 return data.res;
921 error:
922 isl_union_map_free(umap);
923 isl_set_free(set);
924 isl_union_map_free(data.res);
925 return NULL;
928 /* Intersect "umap" with the parameter domain "set".
930 * If "set" does not have any constraints, then we can return immediately.
932 __isl_give isl_union_map *isl_union_map_intersect_params(
933 __isl_take isl_union_map *umap, __isl_take isl_set *set)
935 int is_universe;
937 is_universe = isl_set_plain_is_universe(set);
938 if (is_universe < 0)
939 goto error;
940 if (is_universe) {
941 isl_set_free(set);
942 return umap;
945 return gen_bin_set_op(umap, set, &intersect_params_entry);
946 error:
947 isl_union_map_free(umap);
948 isl_set_free(set);
949 return NULL;
952 __isl_give isl_union_set *isl_union_set_intersect_params(
953 __isl_take isl_union_set *uset, __isl_take isl_set *set)
955 return isl_union_map_intersect_params(uset, set);
958 static __isl_give isl_union_map *union_map_intersect_params(
959 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
961 return isl_union_map_intersect_params(umap,
962 isl_set_from_union_set(uset));
965 static __isl_give isl_union_map *union_map_gist_params(
966 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
968 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
971 struct isl_union_map_match_bin_data {
972 isl_union_map *umap2;
973 isl_union_map *res;
974 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
977 static isl_stat match_bin_entry(void **entry, void *user)
979 struct isl_union_map_match_bin_data *data = user;
980 uint32_t hash;
981 struct isl_hash_table_entry *entry2;
982 isl_map *map = *entry;
983 int empty;
985 hash = isl_space_get_hash(map->dim);
986 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
987 hash, &has_space, map->dim, 0);
988 if (!entry2)
989 return isl_stat_ok;
991 map = isl_map_copy(map);
992 map = data->fn(map, isl_map_copy(entry2->data));
994 empty = isl_map_is_empty(map);
995 if (empty < 0) {
996 isl_map_free(map);
997 return isl_stat_error;
999 if (empty) {
1000 isl_map_free(map);
1001 return isl_stat_ok;
1004 data->res = isl_union_map_add_map(data->res, map);
1006 return isl_stat_ok;
1009 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1010 __isl_take isl_union_map *umap2,
1011 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1013 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1015 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1016 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1018 if (!umap1 || !umap2)
1019 goto error;
1021 data.umap2 = umap2;
1022 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1023 umap1->table.n);
1024 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1025 &match_bin_entry, &data) < 0)
1026 goto error;
1028 isl_union_map_free(umap1);
1029 isl_union_map_free(umap2);
1030 return data.res;
1031 error:
1032 isl_union_map_free(umap1);
1033 isl_union_map_free(umap2);
1034 isl_union_map_free(data.res);
1035 return NULL;
1038 __isl_give isl_union_map *isl_union_map_intersect(
1039 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1041 return match_bin_op(umap1, umap2, &isl_map_intersect);
1044 /* Compute the intersection of the two union_sets.
1045 * As a special case, if exactly one of the two union_sets
1046 * is a parameter domain, then intersect the parameter domain
1047 * of the other one with this set.
1049 __isl_give isl_union_set *isl_union_set_intersect(
1050 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1052 int p1, p2;
1054 p1 = isl_union_set_is_params(uset1);
1055 p2 = isl_union_set_is_params(uset2);
1056 if (p1 < 0 || p2 < 0)
1057 goto error;
1058 if (!p1 && p2)
1059 return union_map_intersect_params(uset1, uset2);
1060 if (p1 && !p2)
1061 return union_map_intersect_params(uset2, uset1);
1062 return isl_union_map_intersect(uset1, uset2);
1063 error:
1064 isl_union_set_free(uset1);
1065 isl_union_set_free(uset2);
1066 return NULL;
1069 static isl_stat gist_params_entry(void **entry, void *user)
1071 struct isl_union_map_gen_bin_set_data *data = user;
1072 isl_map *map = *entry;
1073 int empty;
1075 map = isl_map_copy(map);
1076 map = isl_map_gist_params(map, isl_set_copy(data->set));
1078 empty = isl_map_is_empty(map);
1079 if (empty < 0) {
1080 isl_map_free(map);
1081 return isl_stat_error;
1084 data->res = isl_union_map_add_map(data->res, map);
1086 return isl_stat_ok;
1089 __isl_give isl_union_map *isl_union_map_gist_params(
1090 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1092 return gen_bin_set_op(umap, set, &gist_params_entry);
1095 __isl_give isl_union_set *isl_union_set_gist_params(
1096 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1098 return isl_union_map_gist_params(uset, set);
1101 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1102 __isl_take isl_union_map *context)
1104 return match_bin_op(umap, context, &isl_map_gist);
1107 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1108 __isl_take isl_union_set *context)
1110 if (isl_union_set_is_params(context))
1111 return union_map_gist_params(uset, context);
1112 return isl_union_map_gist(uset, context);
1115 /* For each map in "umap", remove the constraints in the corresponding map
1116 * of "context".
1117 * Each map in "context" is assumed to consist of a single disjunct and
1118 * to have explicit representations for all local variables.
1120 __isl_give isl_union_map *isl_union_map_plain_gist(
1121 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1123 return match_bin_op(umap, context, &isl_map_plain_gist);
1126 /* For each set in "uset", remove the constraints in the corresponding set
1127 * of "context".
1128 * Each set in "context" is assumed to consist of a single disjunct and
1129 * to have explicit representations for all local variables.
1131 __isl_give isl_union_set *isl_union_set_plain_gist(
1132 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1134 return isl_union_map_plain_gist(uset, context);
1137 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1138 __isl_take isl_map *set2)
1140 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1143 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1144 __isl_take isl_map *set2)
1146 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1149 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1150 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1152 return match_bin_op(uset1, uset2, &lex_lt_set);
1155 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1156 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1158 return match_bin_op(uset1, uset2, &lex_le_set);
1161 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1162 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1164 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1167 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1168 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1170 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1173 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1174 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1176 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1179 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1180 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1182 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1185 /* Intersect the domain of "umap" with "uset".
1187 static __isl_give isl_union_map *union_map_intersect_domain(
1188 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1190 struct isl_bin_op_control control = {
1191 .match_space = &isl_space_domain,
1192 .fn_map = &isl_map_intersect_domain,
1195 return gen_bin_op(umap, uset, &control);
1198 /* Intersect the domain of "umap" with "uset".
1199 * If "uset" is a parameters domain, then intersect the parameter
1200 * domain of "umap" with this set.
1202 __isl_give isl_union_map *isl_union_map_intersect_domain(
1203 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1205 if (isl_union_set_is_params(uset))
1206 return union_map_intersect_params(umap, uset);
1207 else
1208 return union_map_intersect_domain(umap, uset);
1211 /* Remove the elements of "uset" from the domain of "umap".
1213 __isl_give isl_union_map *isl_union_map_subtract_domain(
1214 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1216 struct isl_bin_op_control control = {
1217 .subtract = 1,
1218 .match_space = &isl_space_domain,
1219 .fn_map = &isl_map_subtract_domain,
1222 return gen_bin_op(umap, dom, &control);
1225 /* Remove the elements of "uset" from the range of "umap".
1227 __isl_give isl_union_map *isl_union_map_subtract_range(
1228 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1230 struct isl_bin_op_control control = {
1231 .subtract = 1,
1232 .match_space = &isl_space_range,
1233 .fn_map = &isl_map_subtract_range,
1236 return gen_bin_op(umap, dom, &control);
1239 /* Compute the gist of "umap" with respect to the domain "uset".
1241 static __isl_give isl_union_map *union_map_gist_domain(
1242 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1244 struct isl_bin_op_control control = {
1245 .match_space = &isl_space_domain,
1246 .fn_map = &isl_map_gist_domain,
1249 return gen_bin_op(umap, uset, &control);
1252 /* Compute the gist of "umap" with respect to the domain "uset".
1253 * If "uset" is a parameters domain, then compute the gist
1254 * with respect to this parameter domain.
1256 __isl_give isl_union_map *isl_union_map_gist_domain(
1257 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1259 if (isl_union_set_is_params(uset))
1260 return union_map_gist_params(umap, uset);
1261 else
1262 return union_map_gist_domain(umap, uset);
1265 /* Compute the gist of "umap" with respect to the range "uset".
1267 __isl_give isl_union_map *isl_union_map_gist_range(
1268 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1270 struct isl_bin_op_control control = {
1271 .match_space = &isl_space_range,
1272 .fn_map = &isl_map_gist_range,
1275 return gen_bin_op(umap, uset, &control);
1278 __isl_give isl_union_map *isl_union_map_intersect_range(
1279 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1281 struct isl_bin_op_control control = {
1282 .match_space = &isl_space_range,
1283 .fn_map = &isl_map_intersect_range,
1286 return gen_bin_op(umap, uset, &control);
1289 /* Intersect each map in "umap" in a space A -> [B -> C]
1290 * with the corresponding map in "factor" in the space A -> C and
1291 * collect the results.
1293 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1294 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1296 struct isl_bin_op_control control = {
1297 .filter = &isl_map_range_is_wrapping,
1298 .match_space = &isl_space_range_factor_range,
1299 .fn_map = &isl_map_intersect_range_factor_range,
1302 return gen_bin_op(umap, factor, &control);
1305 struct isl_union_map_bin_data {
1306 isl_union_map *umap2;
1307 isl_union_map *res;
1308 isl_map *map;
1309 isl_stat (*fn)(void **entry, void *user);
1312 static isl_stat apply_range_entry(void **entry, void *user)
1314 struct isl_union_map_bin_data *data = user;
1315 isl_map *map2 = *entry;
1316 isl_bool empty;
1318 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1319 map2->dim, isl_dim_in))
1320 return isl_stat_ok;
1322 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1324 empty = isl_map_is_empty(map2);
1325 if (empty < 0) {
1326 isl_map_free(map2);
1327 return isl_stat_error;
1329 if (empty) {
1330 isl_map_free(map2);
1331 return isl_stat_ok;
1334 data->res = isl_union_map_add_map(data->res, map2);
1336 return isl_stat_ok;
1339 static isl_stat bin_entry(void **entry, void *user)
1341 struct isl_union_map_bin_data *data = user;
1342 isl_map *map = *entry;
1344 data->map = map;
1345 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1346 data->fn, data) < 0)
1347 return isl_stat_error;
1349 return isl_stat_ok;
1352 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1353 __isl_take isl_union_map *umap2,
1354 isl_stat (*fn)(void **entry, void *user))
1356 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1358 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1359 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1361 if (!umap1 || !umap2)
1362 goto error;
1364 data.umap2 = umap2;
1365 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1366 umap1->table.n);
1367 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1368 &bin_entry, &data) < 0)
1369 goto error;
1371 isl_union_map_free(umap1);
1372 isl_union_map_free(umap2);
1373 return data.res;
1374 error:
1375 isl_union_map_free(umap1);
1376 isl_union_map_free(umap2);
1377 isl_union_map_free(data.res);
1378 return NULL;
1381 __isl_give isl_union_map *isl_union_map_apply_range(
1382 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1384 return bin_op(umap1, umap2, &apply_range_entry);
1387 __isl_give isl_union_map *isl_union_map_apply_domain(
1388 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1390 umap1 = isl_union_map_reverse(umap1);
1391 umap1 = isl_union_map_apply_range(umap1, umap2);
1392 return isl_union_map_reverse(umap1);
1395 __isl_give isl_union_set *isl_union_set_apply(
1396 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1398 return isl_union_map_apply_range(uset, umap);
1401 static isl_stat map_lex_lt_entry(void **entry, void *user)
1403 struct isl_union_map_bin_data *data = user;
1404 isl_map *map2 = *entry;
1406 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1407 map2->dim, isl_dim_out))
1408 return isl_stat_ok;
1410 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1412 data->res = isl_union_map_add_map(data->res, map2);
1414 return isl_stat_ok;
1417 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1418 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1420 return bin_op(umap1, umap2, &map_lex_lt_entry);
1423 static isl_stat map_lex_le_entry(void **entry, void *user)
1425 struct isl_union_map_bin_data *data = user;
1426 isl_map *map2 = *entry;
1428 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1429 map2->dim, isl_dim_out))
1430 return isl_stat_ok;
1432 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1434 data->res = isl_union_map_add_map(data->res, map2);
1436 return isl_stat_ok;
1439 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1440 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1442 return bin_op(umap1, umap2, &map_lex_le_entry);
1445 static isl_stat product_entry(void **entry, void *user)
1447 struct isl_union_map_bin_data *data = user;
1448 isl_map *map2 = *entry;
1450 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1452 data->res = isl_union_map_add_map(data->res, map2);
1454 return isl_stat_ok;
1457 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1458 __isl_take isl_union_map *umap2)
1460 return bin_op(umap1, umap2, &product_entry);
1463 static isl_stat set_product_entry(void **entry, void *user)
1465 struct isl_union_map_bin_data *data = user;
1466 isl_set *set2 = *entry;
1468 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1470 data->res = isl_union_set_add_set(data->res, set2);
1472 return isl_stat_ok;
1475 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1476 __isl_take isl_union_set *uset2)
1478 return bin_op(uset1, uset2, &set_product_entry);
1481 static isl_stat domain_product_entry(void **entry, void *user)
1483 struct isl_union_map_bin_data *data = user;
1484 isl_map *map2 = *entry;
1486 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1487 map2->dim, isl_dim_out))
1488 return isl_stat_ok;
1490 map2 = isl_map_domain_product(isl_map_copy(data->map),
1491 isl_map_copy(map2));
1493 data->res = isl_union_map_add_map(data->res, map2);
1495 return isl_stat_ok;
1498 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1500 __isl_give isl_union_map *isl_union_map_domain_product(
1501 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1503 return bin_op(umap1, umap2, &domain_product_entry);
1506 static isl_stat range_product_entry(void **entry, void *user)
1508 struct isl_union_map_bin_data *data = user;
1509 isl_map *map2 = *entry;
1511 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1512 map2->dim, isl_dim_in))
1513 return isl_stat_ok;
1515 map2 = isl_map_range_product(isl_map_copy(data->map),
1516 isl_map_copy(map2));
1518 data->res = isl_union_map_add_map(data->res, map2);
1520 return isl_stat_ok;
1523 __isl_give isl_union_map *isl_union_map_range_product(
1524 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1526 return bin_op(umap1, umap2, &range_product_entry);
1529 /* If data->map A -> B and "map2" C -> D have the same range space,
1530 * then add (A, C) -> (B * D) to data->res.
1532 static isl_stat flat_domain_product_entry(void **entry, void *user)
1534 struct isl_union_map_bin_data *data = user;
1535 isl_map *map2 = *entry;
1537 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1538 map2->dim, isl_dim_out))
1539 return isl_stat_ok;
1541 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1542 isl_map_copy(map2));
1544 data->res = isl_union_map_add_map(data->res, map2);
1546 return isl_stat_ok;
1549 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1551 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1552 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1554 return bin_op(umap1, umap2, &flat_domain_product_entry);
1557 static isl_stat flat_range_product_entry(void **entry, void *user)
1559 struct isl_union_map_bin_data *data = user;
1560 isl_map *map2 = *entry;
1562 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1563 map2->dim, isl_dim_in))
1564 return isl_stat_ok;
1566 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1567 isl_map_copy(map2));
1569 data->res = isl_union_map_add_map(data->res, map2);
1571 return isl_stat_ok;
1574 __isl_give isl_union_map *isl_union_map_flat_range_product(
1575 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1577 return bin_op(umap1, umap2, &flat_range_product_entry);
1580 /* Data structure that specifies how un_op should modify
1581 * the maps in the union map.
1583 * If "inplace" is set, then the maps in the input union map
1584 * are modified in place. This means that "fn_map" should not
1585 * change the meaning of the map or that the union map only
1586 * has a single reference.
1587 * If "total" is set, then all maps need to be modified and
1588 * the results need to live in the same space.
1589 * Otherwise, a new union map is constructed to store the results.
1590 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1591 * are taken into account. "filter_user" is passed as the second argument
1592 * to "filter". No filter can be set if "inplace" or
1593 * "total" is set.
1594 * "fn_map" specifies how the maps (selected by "filter")
1595 * should be transformed.
1597 struct isl_un_op_control {
1598 int inplace;
1599 int total;
1600 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1601 void *filter_user;
1602 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1605 /* Data structure for wrapping the data for un_op_filter_drop_user.
1606 * "filter" is the function that is being wrapped.
1608 struct isl_un_op_drop_user_data {
1609 isl_bool (*filter)(__isl_keep isl_map *map);
1612 /* Wrapper for isl_un_op_control filters that do not require
1613 * a second argument.
1614 * Simply call data->filter without the second argument.
1616 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1618 struct isl_un_op_drop_user_data *data = user;
1619 return data->filter(map);
1622 /* Internal data structure for "un_op".
1623 * "control" specifies how the maps in the union map should be modified.
1624 * "res" collects the results.
1626 struct isl_union_map_un_data {
1627 struct isl_un_op_control *control;
1628 isl_union_map *res;
1631 /* isl_hash_table_foreach callback for un_op.
1632 * Handle the map that "entry" points to.
1634 * If control->filter is set, then check if this map satisfies the filter.
1635 * If so (or if control->filter is not set), modify the map
1636 * by calling control->fn_map and either add the result to data->res or
1637 * replace the original entry by the result (if control->inplace is set).
1639 static isl_stat un_entry(void **entry, void *user)
1641 struct isl_union_map_un_data *data = user;
1642 isl_map *map = *entry;
1644 if (data->control->filter) {
1645 isl_bool ok;
1647 ok = data->control->filter(map, data->control->filter_user);
1648 if (ok < 0)
1649 return isl_stat_error;
1650 if (!ok)
1651 return isl_stat_ok;
1654 map = data->control->fn_map(isl_map_copy(map));
1655 if (!map)
1656 return isl_stat_error;
1657 if (data->control->inplace) {
1658 isl_map_free(*entry);
1659 *entry = map;
1660 } else {
1661 data->res = isl_union_map_add_map(data->res, map);
1662 if (!data->res)
1663 return isl_stat_error;
1666 return isl_stat_ok;
1669 /* Modify the maps in "umap" based on "control".
1670 * If control->inplace is set, then modify the maps in "umap" in-place.
1671 * Otherwise, create a new union map to hold the results.
1672 * If control->total is set, then perform an inplace computation
1673 * if "umap" is only referenced once. Otherwise, create a new union map
1674 * to store the results.
1676 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1677 struct isl_un_op_control *control)
1679 struct isl_union_map_un_data data = { control };
1681 if (!umap)
1682 return NULL;
1683 if ((control->inplace || control->total) && control->filter)
1684 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1685 "inplace/total modification cannot be filtered",
1686 return isl_union_map_free(umap));
1688 if (control->total && umap->ref == 1)
1689 control->inplace = 1;
1690 if (control->inplace) {
1691 data.res = umap;
1692 } else {
1693 isl_space *space;
1695 space = isl_union_map_get_space(umap);
1696 data.res = isl_union_map_alloc(space, umap->table.n);
1698 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1699 &umap->table, &un_entry, &data) < 0)
1700 data.res = isl_union_map_free(data.res);
1702 if (control->inplace)
1703 return data.res;
1704 isl_union_map_free(umap);
1705 return data.res;
1708 __isl_give isl_union_map *isl_union_map_from_range(
1709 __isl_take isl_union_set *uset)
1711 struct isl_un_op_control control = {
1712 .fn_map = &isl_map_from_range,
1714 return un_op(uset, &control);
1717 __isl_give isl_union_map *isl_union_map_from_domain(
1718 __isl_take isl_union_set *uset)
1720 return isl_union_map_reverse(isl_union_map_from_range(uset));
1723 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1724 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1726 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1727 isl_union_map_from_range(range));
1730 /* Modify the maps in "umap" by applying "fn" on them.
1731 * "fn" should apply to all maps in "umap" and should not modify the space.
1733 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1734 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1736 struct isl_un_op_control control = {
1737 .total = 1,
1738 .fn_map = fn,
1741 return un_op(umap, &control);
1744 /* Compute the affine hull of "map" and return the result as an isl_map.
1746 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1748 return isl_map_from_basic_map(isl_map_affine_hull(map));
1751 __isl_give isl_union_map *isl_union_map_affine_hull(
1752 __isl_take isl_union_map *umap)
1754 return total(umap, &isl_map_affine_hull_map);
1757 __isl_give isl_union_set *isl_union_set_affine_hull(
1758 __isl_take isl_union_set *uset)
1760 return isl_union_map_affine_hull(uset);
1763 /* Wrapper around isl_set_combined_lineality_space
1764 * that returns the combined lineality space in the form of an isl_set
1765 * instead of an isl_basic_set.
1767 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1769 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1772 /* For each set in "uset", compute the (linear) hull
1773 * of the lineality spaces of its basic sets and
1774 * collect and return the results.
1776 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1777 __isl_take isl_union_set *uset)
1779 struct isl_un_op_control control = {
1780 .fn_map = &combined_lineality_space,
1782 return un_op(uset, &control);
1785 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1787 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1789 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1792 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1793 __isl_take isl_union_map *umap)
1795 return total(umap, &isl_map_polyhedral_hull_map);
1798 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1799 __isl_take isl_union_set *uset)
1801 return isl_union_map_polyhedral_hull(uset);
1804 /* Compute a superset of the convex hull of "map" that is described
1805 * by only translates of the constraints in the constituents of "map" and
1806 * return the result as an isl_map.
1808 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1810 return isl_map_from_basic_map(isl_map_simple_hull(map));
1813 __isl_give isl_union_map *isl_union_map_simple_hull(
1814 __isl_take isl_union_map *umap)
1816 return total(umap, &isl_map_simple_hull_map);
1819 __isl_give isl_union_set *isl_union_set_simple_hull(
1820 __isl_take isl_union_set *uset)
1822 return isl_union_map_simple_hull(uset);
1825 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1826 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1828 struct isl_un_op_control control = {
1829 .inplace = 1,
1830 .fn_map = fn,
1833 return un_op(umap, &control);
1836 /* Remove redundant constraints in each of the basic maps of "umap".
1837 * Since removing redundant constraints does not change the meaning
1838 * or the space, the operation can be performed in-place.
1840 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1841 __isl_take isl_union_map *umap)
1843 return inplace(umap, &isl_map_remove_redundancies);
1846 /* Remove redundant constraints in each of the basic sets of "uset".
1848 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1849 __isl_take isl_union_set *uset)
1851 return isl_union_map_remove_redundancies(uset);
1854 __isl_give isl_union_map *isl_union_map_coalesce(
1855 __isl_take isl_union_map *umap)
1857 return inplace(umap, &isl_map_coalesce);
1860 __isl_give isl_union_set *isl_union_set_coalesce(
1861 __isl_take isl_union_set *uset)
1863 return isl_union_map_coalesce(uset);
1866 __isl_give isl_union_map *isl_union_map_detect_equalities(
1867 __isl_take isl_union_map *umap)
1869 return inplace(umap, &isl_map_detect_equalities);
1872 __isl_give isl_union_set *isl_union_set_detect_equalities(
1873 __isl_take isl_union_set *uset)
1875 return isl_union_map_detect_equalities(uset);
1878 __isl_give isl_union_map *isl_union_map_compute_divs(
1879 __isl_take isl_union_map *umap)
1881 return inplace(umap, &isl_map_compute_divs);
1884 __isl_give isl_union_set *isl_union_set_compute_divs(
1885 __isl_take isl_union_set *uset)
1887 return isl_union_map_compute_divs(uset);
1890 __isl_give isl_union_map *isl_union_map_lexmin(
1891 __isl_take isl_union_map *umap)
1893 return total(umap, &isl_map_lexmin);
1896 __isl_give isl_union_set *isl_union_set_lexmin(
1897 __isl_take isl_union_set *uset)
1899 return isl_union_map_lexmin(uset);
1902 __isl_give isl_union_map *isl_union_map_lexmax(
1903 __isl_take isl_union_map *umap)
1905 return total(umap, &isl_map_lexmax);
1908 __isl_give isl_union_set *isl_union_set_lexmax(
1909 __isl_take isl_union_set *uset)
1911 return isl_union_map_lexmax(uset);
1914 /* Return the universe in the space of "map".
1916 static __isl_give isl_map *universe(__isl_take isl_map *map)
1918 isl_space *space;
1920 space = isl_map_get_space(map);
1921 isl_map_free(map);
1922 return isl_map_universe(space);
1925 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1927 struct isl_un_op_control control = {
1928 .fn_map = &universe,
1930 return un_op(umap, &control);
1933 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1935 return isl_union_map_universe(uset);
1938 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1940 struct isl_un_op_control control = {
1941 .fn_map = &isl_map_reverse,
1943 return un_op(umap, &control);
1946 /* Compute the parameter domain of the given union map.
1948 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1950 struct isl_un_op_control control = {
1951 .fn_map = &isl_map_params,
1953 int empty;
1955 empty = isl_union_map_is_empty(umap);
1956 if (empty < 0)
1957 goto error;
1958 if (empty) {
1959 isl_space *space;
1960 space = isl_union_map_get_space(umap);
1961 isl_union_map_free(umap);
1962 return isl_set_empty(space);
1964 return isl_set_from_union_set(un_op(umap, &control));
1965 error:
1966 isl_union_map_free(umap);
1967 return NULL;
1970 /* Compute the parameter domain of the given union set.
1972 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1974 return isl_union_map_params(uset);
1977 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1979 struct isl_un_op_control control = {
1980 .fn_map = &isl_map_domain,
1982 return un_op(umap, &control);
1985 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1987 struct isl_un_op_control control = {
1988 .fn_map = &isl_map_range,
1990 return un_op(umap, &control);
1993 __isl_give isl_union_map *isl_union_map_domain_map(
1994 __isl_take isl_union_map *umap)
1996 struct isl_un_op_control control = {
1997 .fn_map = &isl_map_domain_map,
1999 return un_op(umap, &control);
2002 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2003 * add the result to "res".
2005 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2007 isl_union_pw_multi_aff **res = user;
2008 isl_multi_aff *ma;
2009 isl_pw_multi_aff *pma;
2011 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2012 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2013 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2015 return *res ? isl_stat_ok : isl_stat_error;
2019 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2020 * to its domain.
2022 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2023 __isl_take isl_union_map *umap)
2025 isl_union_pw_multi_aff *res;
2027 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2028 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2029 res = isl_union_pw_multi_aff_free(res);
2031 isl_union_map_free(umap);
2032 return res;
2035 __isl_give isl_union_map *isl_union_map_range_map(
2036 __isl_take isl_union_map *umap)
2038 struct isl_un_op_control control = {
2039 .fn_map = &isl_map_range_map,
2041 return un_op(umap, &control);
2044 /* Given a collection of wrapped maps of the form A[B -> C],
2045 * return the collection of maps A[B -> C] -> B.
2047 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2048 __isl_take isl_union_set *uset)
2050 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2051 struct isl_un_op_control control = {
2052 .filter = &un_op_filter_drop_user,
2053 .filter_user = &data,
2054 .fn_map = &isl_set_wrapped_domain_map,
2056 return un_op(uset, &control);
2059 /* Does "map" relate elements from the same space?
2061 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2063 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2064 map->dim, isl_dim_out);
2067 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2069 struct isl_un_op_control control = {
2070 .filter = &equal_tuples,
2071 .fn_map = &isl_map_deltas,
2073 return un_op(umap, &control);
2076 __isl_give isl_union_map *isl_union_map_deltas_map(
2077 __isl_take isl_union_map *umap)
2079 struct isl_un_op_control control = {
2080 .filter = &equal_tuples,
2081 .fn_map = &isl_map_deltas_map,
2083 return un_op(umap, &control);
2086 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2088 struct isl_un_op_control control = {
2089 .fn_map = &isl_set_identity,
2091 return un_op(uset, &control);
2094 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2096 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2098 isl_union_pw_multi_aff **res = user;
2099 isl_space *space;
2100 isl_pw_multi_aff *pma;
2102 space = isl_space_map_from_set(isl_set_get_space(set));
2103 pma = isl_pw_multi_aff_identity(space);
2104 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2105 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2107 return *res ? isl_stat_ok : isl_stat_error;
2110 /* Return an identity function on "uset" in the form
2111 * of an isl_union_pw_multi_aff.
2113 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2114 __isl_take isl_union_set *uset)
2116 isl_union_pw_multi_aff *res;
2118 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2119 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2120 res = isl_union_pw_multi_aff_free(res);
2122 isl_union_set_free(uset);
2123 return res;
2126 /* For each map in "umap" of the form [A -> B] -> C,
2127 * construct the map A -> C and collect the results.
2129 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2130 __isl_take isl_union_map *umap)
2132 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2133 struct isl_un_op_control control = {
2134 .filter = &un_op_filter_drop_user,
2135 .filter_user = &data,
2136 .fn_map = &isl_map_domain_factor_domain,
2138 return un_op(umap, &control);
2141 /* For each map in "umap" of the form [A -> B] -> C,
2142 * construct the map B -> C and collect the results.
2144 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2145 __isl_take isl_union_map *umap)
2147 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2148 struct isl_un_op_control control = {
2149 .filter = &un_op_filter_drop_user,
2150 .filter_user = &data,
2151 .fn_map = &isl_map_domain_factor_range,
2153 return un_op(umap, &control);
2156 /* For each map in "umap" of the form A -> [B -> C],
2157 * construct the map A -> B and collect the results.
2159 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2160 __isl_take isl_union_map *umap)
2162 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2163 struct isl_un_op_control control = {
2164 .filter = &un_op_filter_drop_user,
2165 .filter_user = &data,
2166 .fn_map = &isl_map_range_factor_domain,
2168 return un_op(umap, &control);
2171 /* For each map in "umap" of the form A -> [B -> C],
2172 * construct the map A -> C and collect the results.
2174 __isl_give isl_union_map *isl_union_map_range_factor_range(
2175 __isl_take isl_union_map *umap)
2177 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2178 struct isl_un_op_control control = {
2179 .filter = &un_op_filter_drop_user,
2180 .filter_user = &data,
2181 .fn_map = &isl_map_range_factor_range,
2183 return un_op(umap, &control);
2186 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2187 * construct the map A -> C and collect the results.
2189 __isl_give isl_union_map *isl_union_map_factor_domain(
2190 __isl_take isl_union_map *umap)
2192 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2193 struct isl_un_op_control control = {
2194 .filter = &un_op_filter_drop_user,
2195 .filter_user = &data,
2196 .fn_map = &isl_map_factor_domain,
2198 return un_op(umap, &control);
2201 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2202 * construct the map B -> D and collect the results.
2204 __isl_give isl_union_map *isl_union_map_factor_range(
2205 __isl_take isl_union_map *umap)
2207 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2208 struct isl_un_op_control control = {
2209 .filter = &un_op_filter_drop_user,
2210 .filter_user = &data,
2211 .fn_map = &isl_map_factor_range,
2213 return un_op(umap, &control);
2216 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2218 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2219 struct isl_un_op_control control = {
2220 .filter = &un_op_filter_drop_user,
2221 .filter_user = &data,
2222 .fn_map = &isl_set_unwrap,
2224 return un_op(uset, &control);
2227 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2229 struct isl_un_op_control control = {
2230 .fn_map = &isl_map_wrap,
2232 return un_op(umap, &control);
2235 struct isl_union_map_is_subset_data {
2236 isl_union_map *umap2;
2237 isl_bool is_subset;
2240 static isl_stat is_subset_entry(void **entry, void *user)
2242 struct isl_union_map_is_subset_data *data = user;
2243 uint32_t hash;
2244 struct isl_hash_table_entry *entry2;
2245 isl_map *map = *entry;
2247 hash = isl_space_get_hash(map->dim);
2248 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2249 hash, &has_space, map->dim, 0);
2250 if (!entry2) {
2251 int empty = isl_map_is_empty(map);
2252 if (empty < 0)
2253 return isl_stat_error;
2254 if (empty)
2255 return isl_stat_ok;
2256 data->is_subset = 0;
2257 return isl_stat_error;
2260 data->is_subset = isl_map_is_subset(map, entry2->data);
2261 if (data->is_subset < 0 || !data->is_subset)
2262 return isl_stat_error;
2264 return isl_stat_ok;
2267 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2268 __isl_keep isl_union_map *umap2)
2270 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2272 umap1 = isl_union_map_copy(umap1);
2273 umap2 = isl_union_map_copy(umap2);
2274 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2275 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2277 if (!umap1 || !umap2)
2278 goto error;
2280 data.umap2 = umap2;
2281 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2282 &is_subset_entry, &data) < 0 &&
2283 data.is_subset)
2284 goto error;
2286 isl_union_map_free(umap1);
2287 isl_union_map_free(umap2);
2289 return data.is_subset;
2290 error:
2291 isl_union_map_free(umap1);
2292 isl_union_map_free(umap2);
2293 return isl_bool_error;
2296 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2297 __isl_keep isl_union_set *uset2)
2299 return isl_union_map_is_subset(uset1, uset2);
2302 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2303 __isl_keep isl_union_map *umap2)
2305 isl_bool is_subset;
2307 if (!umap1 || !umap2)
2308 return isl_bool_error;
2309 is_subset = isl_union_map_is_subset(umap1, umap2);
2310 if (is_subset != isl_bool_true)
2311 return is_subset;
2312 is_subset = isl_union_map_is_subset(umap2, umap1);
2313 return is_subset;
2316 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2317 __isl_keep isl_union_set *uset2)
2319 return isl_union_map_is_equal(uset1, uset2);
2322 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2323 __isl_keep isl_union_map *umap2)
2325 isl_bool is_subset;
2327 if (!umap1 || !umap2)
2328 return isl_bool_error;
2329 is_subset = isl_union_map_is_subset(umap1, umap2);
2330 if (is_subset != isl_bool_true)
2331 return is_subset;
2332 is_subset = isl_union_map_is_subset(umap2, umap1);
2333 if (is_subset == isl_bool_error)
2334 return is_subset;
2335 return !is_subset;
2338 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2339 __isl_keep isl_union_set *uset2)
2341 return isl_union_map_is_strict_subset(uset1, uset2);
2344 /* Internal data structure for isl_union_map_is_disjoint.
2345 * umap2 is the union map with which we are comparing.
2346 * is_disjoint is initialized to 1 and is set to 0 as soon
2347 * as the union maps turn out not to be disjoint.
2349 struct isl_union_map_is_disjoint_data {
2350 isl_union_map *umap2;
2351 isl_bool is_disjoint;
2354 /* Check if "map" is disjoint from data->umap2 and abort
2355 * the search if it is not.
2357 static isl_stat is_disjoint_entry(void **entry, void *user)
2359 struct isl_union_map_is_disjoint_data *data = user;
2360 uint32_t hash;
2361 struct isl_hash_table_entry *entry2;
2362 isl_map *map = *entry;
2364 hash = isl_space_get_hash(map->dim);
2365 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2366 hash, &has_space, map->dim, 0);
2367 if (!entry2)
2368 return isl_stat_ok;
2370 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2371 if (data->is_disjoint < 0 || !data->is_disjoint)
2372 return isl_stat_error;
2374 return isl_stat_ok;
2377 /* Are "umap1" and "umap2" disjoint?
2379 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2380 __isl_keep isl_union_map *umap2)
2382 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2384 umap1 = isl_union_map_copy(umap1);
2385 umap2 = isl_union_map_copy(umap2);
2386 umap1 = isl_union_map_align_params(umap1,
2387 isl_union_map_get_space(umap2));
2388 umap2 = isl_union_map_align_params(umap2,
2389 isl_union_map_get_space(umap1));
2391 if (!umap1 || !umap2)
2392 goto error;
2394 data.umap2 = umap2;
2395 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2396 &is_disjoint_entry, &data) < 0 &&
2397 data.is_disjoint)
2398 goto error;
2400 isl_union_map_free(umap1);
2401 isl_union_map_free(umap2);
2403 return data.is_disjoint;
2404 error:
2405 isl_union_map_free(umap1);
2406 isl_union_map_free(umap2);
2407 return isl_bool_error;
2410 /* Are "uset1" and "uset2" disjoint?
2412 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2413 __isl_keep isl_union_set *uset2)
2415 return isl_union_map_is_disjoint(uset1, uset2);
2418 static isl_stat sample_entry(void **entry, void *user)
2420 isl_basic_map **sample = (isl_basic_map **)user;
2421 isl_map *map = *entry;
2423 *sample = isl_map_sample(isl_map_copy(map));
2424 if (!*sample)
2425 return isl_stat_error;
2426 if (!isl_basic_map_plain_is_empty(*sample))
2427 return isl_stat_error;
2428 return isl_stat_ok;
2431 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2433 isl_basic_map *sample = NULL;
2435 if (!umap)
2436 return NULL;
2438 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2439 &sample_entry, &sample) < 0 &&
2440 !sample)
2441 goto error;
2443 if (!sample)
2444 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2446 isl_union_map_free(umap);
2448 return sample;
2449 error:
2450 isl_union_map_free(umap);
2451 return NULL;
2454 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2456 return bset_from_bmap(isl_union_map_sample(uset));
2459 /* Return an element in "uset" in the form of an isl_point.
2460 * Return a void isl_point if "uset" is empty.
2462 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2464 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2467 struct isl_forall_data {
2468 isl_bool res;
2469 isl_bool (*fn)(__isl_keep isl_map *map);
2472 static isl_stat forall_entry(void **entry, void *user)
2474 struct isl_forall_data *data = user;
2475 isl_map *map = *entry;
2477 data->res = data->fn(map);
2478 if (data->res < 0)
2479 return isl_stat_error;
2481 if (!data->res)
2482 return isl_stat_error;
2484 return isl_stat_ok;
2487 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2488 isl_bool (*fn)(__isl_keep isl_map *map))
2490 struct isl_forall_data data = { isl_bool_true, fn };
2492 if (!umap)
2493 return isl_bool_error;
2495 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2496 &forall_entry, &data) < 0 && data.res)
2497 return isl_bool_error;
2499 return data.res;
2502 struct isl_forall_user_data {
2503 isl_bool res;
2504 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2505 void *user;
2508 static isl_stat forall_user_entry(void **entry, void *user)
2510 struct isl_forall_user_data *data = user;
2511 isl_map *map = *entry;
2513 data->res = data->fn(map, data->user);
2514 if (data->res < 0)
2515 return isl_stat_error;
2517 if (!data->res)
2518 return isl_stat_error;
2520 return isl_stat_ok;
2523 /* Check if fn(map, user) returns true for all maps "map" in umap.
2525 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2526 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2528 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2530 if (!umap)
2531 return isl_bool_error;
2533 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2534 &forall_user_entry, &data) < 0 && data.res)
2535 return isl_bool_error;
2537 return data.res;
2540 /* Is "umap" obviously empty?
2542 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2544 if (!umap)
2545 return isl_bool_error;
2546 return isl_union_map_n_map(umap) == 0;
2549 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2551 return union_map_forall(umap, &isl_map_is_empty);
2554 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2556 return isl_union_map_is_empty(uset);
2559 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2561 isl_bool is_subset;
2562 isl_space *dim;
2563 isl_map *id;
2565 if (!map)
2566 return isl_bool_error;
2568 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2569 map->dim, isl_dim_out))
2570 return isl_bool_false;
2572 dim = isl_map_get_space(map);
2573 id = isl_map_identity(dim);
2575 is_subset = isl_map_is_subset(map, id);
2577 isl_map_free(id);
2579 return is_subset;
2582 /* Given an isl_union_map that consists of a single map, check
2583 * if it is single-valued.
2585 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2587 isl_map *map;
2588 isl_bool sv;
2590 umap = isl_union_map_copy(umap);
2591 map = isl_map_from_union_map(umap);
2592 sv = isl_map_is_single_valued(map);
2593 isl_map_free(map);
2595 return sv;
2598 /* Internal data structure for single_valued_on_domain.
2600 * "umap" is the union map to be tested.
2601 * "sv" is set to 1 as long as "umap" may still be single-valued.
2603 struct isl_union_map_is_sv_data {
2604 isl_union_map *umap;
2605 isl_bool sv;
2608 /* Check if the data->umap is single-valued on "set".
2610 * If data->umap consists of a single map on "set", then test it
2611 * as an isl_map.
2613 * Otherwise, compute
2615 * M \circ M^-1
2617 * check if the result is a subset of the identity mapping and
2618 * store the result in data->sv.
2620 * Terminate as soon as data->umap has been determined not to
2621 * be single-valued.
2623 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2625 struct isl_union_map_is_sv_data *data = user;
2626 isl_union_map *umap, *test;
2628 umap = isl_union_map_copy(data->umap);
2629 umap = isl_union_map_intersect_domain(umap,
2630 isl_union_set_from_set(set));
2632 if (isl_union_map_n_map(umap) == 1) {
2633 data->sv = single_map_is_single_valued(umap);
2634 isl_union_map_free(umap);
2635 } else {
2636 test = isl_union_map_reverse(isl_union_map_copy(umap));
2637 test = isl_union_map_apply_range(test, umap);
2639 data->sv = union_map_forall(test, &is_subset_of_identity);
2641 isl_union_map_free(test);
2644 if (data->sv < 0 || !data->sv)
2645 return isl_stat_error;
2646 return isl_stat_ok;
2649 /* Check if the given map is single-valued.
2651 * If the union map consists of a single map, then test it as an isl_map.
2652 * Otherwise, check if the union map is single-valued on each of its
2653 * domain spaces.
2655 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2657 isl_union_map *universe;
2658 isl_union_set *domain;
2659 struct isl_union_map_is_sv_data data;
2661 if (isl_union_map_n_map(umap) == 1)
2662 return single_map_is_single_valued(umap);
2664 universe = isl_union_map_universe(isl_union_map_copy(umap));
2665 domain = isl_union_map_domain(universe);
2667 data.sv = isl_bool_true;
2668 data.umap = umap;
2669 if (isl_union_set_foreach_set(domain,
2670 &single_valued_on_domain, &data) < 0 && data.sv)
2671 data.sv = isl_bool_error;
2672 isl_union_set_free(domain);
2674 return data.sv;
2677 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2679 isl_bool in;
2681 umap = isl_union_map_copy(umap);
2682 umap = isl_union_map_reverse(umap);
2683 in = isl_union_map_is_single_valued(umap);
2684 isl_union_map_free(umap);
2686 return in;
2689 /* Is "map" obviously not an identity relation because
2690 * it maps elements from one space to another space?
2691 * Update *non_identity accordingly.
2693 * In particular, if the domain and range spaces are the same,
2694 * then the map is not considered to obviously not be an identity relation.
2695 * Otherwise, the map is considered to obviously not be an identity relation
2696 * if it is is non-empty.
2698 * If "map" is determined to obviously not be an identity relation,
2699 * then the search is aborted.
2701 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2703 isl_bool *non_identity = user;
2704 isl_bool equal;
2705 isl_space *space;
2707 space = isl_map_get_space(map);
2708 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2709 if (equal >= 0 && !equal)
2710 *non_identity = isl_bool_not(isl_map_is_empty(map));
2711 else
2712 *non_identity = isl_bool_not(equal);
2713 isl_space_free(space);
2714 isl_map_free(map);
2716 if (*non_identity < 0 || *non_identity)
2717 return isl_stat_error;
2719 return isl_stat_ok;
2722 /* Is "umap" obviously not an identity relation because
2723 * it maps elements from one space to another space?
2725 * As soon as a map has been found that maps elements to a different space,
2726 * non_identity is changed and the search is aborted.
2728 static isl_bool isl_union_map_plain_is_not_identity(
2729 __isl_keep isl_union_map *umap)
2731 isl_bool non_identity;
2733 non_identity = isl_bool_false;
2734 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2735 &non_identity) < 0 &&
2736 non_identity == isl_bool_false)
2737 return isl_bool_error;
2739 return non_identity;
2742 /* Does "map" only map elements to themselves?
2743 * Update *identity accordingly.
2745 * If "map" is determined not to be an identity relation,
2746 * then the search is aborted.
2748 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2750 isl_bool *identity = user;
2752 *identity = isl_map_is_identity(map);
2753 isl_map_free(map);
2755 if (*identity < 0 || !*identity)
2756 return isl_stat_error;
2758 return isl_stat_ok;
2761 /* Does "umap" only map elements to themselves?
2763 * First check if there are any maps that map elements to different spaces.
2764 * If not, then check that all the maps (between identical spaces)
2765 * are identity relations.
2767 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2769 isl_bool non_identity;
2770 isl_bool identity;
2772 non_identity = isl_union_map_plain_is_not_identity(umap);
2773 if (non_identity < 0 || non_identity)
2774 return isl_bool_not(non_identity);
2776 identity = isl_bool_true;
2777 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2778 identity == isl_bool_true)
2779 return isl_bool_error;
2781 return identity;
2784 /* Represents a map that has a fixed value (v) for one of its
2785 * range dimensions.
2786 * The map in this structure is not reference counted, so it
2787 * is only valid while the isl_union_map from which it was
2788 * obtained is still alive.
2790 struct isl_fixed_map {
2791 isl_int v;
2792 isl_map *map;
2795 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2796 int n)
2798 int i;
2799 struct isl_fixed_map *v;
2801 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2802 if (!v)
2803 return NULL;
2804 for (i = 0; i < n; ++i)
2805 isl_int_init(v[i].v);
2806 return v;
2809 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2811 int i;
2813 if (!v)
2814 return;
2815 for (i = 0; i < n; ++i)
2816 isl_int_clear(v[i].v);
2817 free(v);
2820 /* Compare the "v" field of two isl_fixed_map structs.
2822 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2824 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2825 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2827 return isl_int_cmp(e1->v, e2->v);
2830 /* Internal data structure used while checking whether all maps
2831 * in a union_map have a fixed value for a given output dimension.
2832 * v is the list of maps, with the fixed value for the dimension
2833 * n is the number of maps considered so far
2834 * pos is the output dimension under investigation
2836 struct isl_fixed_dim_data {
2837 struct isl_fixed_map *v;
2838 int n;
2839 int pos;
2842 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2844 struct isl_fixed_dim_data *data = user;
2846 data->v[data->n].map = map;
2847 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2848 &data->v[data->n++].v);
2851 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2852 int first, int n_range);
2854 /* Given a list of the maps, with their fixed values at output dimension "pos",
2855 * check whether the ranges of the maps form an obvious partition.
2857 * We first sort the maps according to their fixed values.
2858 * If all maps have a different value, then we know the ranges form
2859 * a partition.
2860 * Otherwise, we collect the maps with the same fixed value and
2861 * check whether each such collection is obviously injective
2862 * based on later dimensions.
2864 static int separates(struct isl_fixed_map *v, int n,
2865 __isl_take isl_space *dim, int pos, int n_range)
2867 int i;
2869 if (!v)
2870 goto error;
2872 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2874 for (i = 0; i + 1 < n; ++i) {
2875 int j, k;
2876 isl_union_map *part;
2877 int injective;
2879 for (j = i + 1; j < n; ++j)
2880 if (isl_int_ne(v[i].v, v[j].v))
2881 break;
2883 if (j == i + 1)
2884 continue;
2886 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2887 for (k = i; k < j; ++k)
2888 part = isl_union_map_add_map(part,
2889 isl_map_copy(v[k].map));
2891 injective = plain_injective_on_range(part, pos + 1, n_range);
2892 if (injective < 0)
2893 goto error;
2894 if (!injective)
2895 break;
2897 i = j - 1;
2900 isl_space_free(dim);
2901 free_isl_fixed_map_array(v, n);
2902 return i + 1 >= n;
2903 error:
2904 isl_space_free(dim);
2905 free_isl_fixed_map_array(v, n);
2906 return -1;
2909 /* Check whether the maps in umap have obviously distinct ranges.
2910 * In particular, check for an output dimension in the range
2911 * [first,n_range) for which all maps have a fixed value
2912 * and then check if these values, possibly along with fixed values
2913 * at later dimensions, entail distinct ranges.
2915 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2916 int first, int n_range)
2918 isl_ctx *ctx;
2919 int n;
2920 struct isl_fixed_dim_data data = { NULL };
2922 ctx = isl_union_map_get_ctx(umap);
2924 n = isl_union_map_n_map(umap);
2925 if (!umap)
2926 goto error;
2928 if (n <= 1) {
2929 isl_union_map_free(umap);
2930 return isl_bool_true;
2933 if (first >= n_range) {
2934 isl_union_map_free(umap);
2935 return isl_bool_false;
2938 data.v = alloc_isl_fixed_map_array(ctx, n);
2939 if (!data.v)
2940 goto error;
2942 for (data.pos = first; data.pos < n_range; ++data.pos) {
2943 isl_bool fixed;
2944 int injective;
2945 isl_space *dim;
2947 data.n = 0;
2948 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2949 if (fixed < 0)
2950 goto error;
2951 if (!fixed)
2952 continue;
2953 dim = isl_union_map_get_space(umap);
2954 injective = separates(data.v, n, dim, data.pos, n_range);
2955 isl_union_map_free(umap);
2956 return injective;
2959 free_isl_fixed_map_array(data.v, n);
2960 isl_union_map_free(umap);
2962 return isl_bool_false;
2963 error:
2964 free_isl_fixed_map_array(data.v, n);
2965 isl_union_map_free(umap);
2966 return isl_bool_error;
2969 /* Check whether the maps in umap that map to subsets of "ran"
2970 * have obviously distinct ranges.
2972 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2973 void *user)
2975 isl_union_map *umap = user;
2977 umap = isl_union_map_copy(umap);
2978 umap = isl_union_map_intersect_range(umap,
2979 isl_union_set_from_set(isl_set_copy(ran)));
2980 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2983 /* Check if the given union_map is obviously injective.
2985 * In particular, we first check if all individual maps are obviously
2986 * injective and then check if all the ranges of these maps are
2987 * obviously disjoint.
2989 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2991 isl_bool in;
2992 isl_union_map *univ;
2993 isl_union_set *ran;
2995 in = union_map_forall(umap, &isl_map_plain_is_injective);
2996 if (in < 0)
2997 return isl_bool_error;
2998 if (!in)
2999 return isl_bool_false;
3001 univ = isl_union_map_universe(isl_union_map_copy(umap));
3002 ran = isl_union_map_range(univ);
3004 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3006 isl_union_set_free(ran);
3008 return in;
3011 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3013 isl_bool sv;
3015 sv = isl_union_map_is_single_valued(umap);
3016 if (sv < 0 || !sv)
3017 return sv;
3019 return isl_union_map_is_injective(umap);
3022 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3024 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3025 struct isl_un_op_control control = {
3026 .filter = &un_op_filter_drop_user,
3027 .filter_user = &data,
3028 .fn_map = &isl_map_zip,
3030 return un_op(umap, &control);
3033 /* Given a union map, take the maps of the form A -> (B -> C) and
3034 * return the union of the corresponding maps (A -> B) -> C.
3036 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3038 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3039 struct isl_un_op_control control = {
3040 .filter = &un_op_filter_drop_user,
3041 .filter_user = &data,
3042 .fn_map = &isl_map_uncurry,
3044 return un_op(umap, &control);
3047 /* Given a union map, take the maps of the form (A -> B) -> C and
3048 * return the union of the corresponding maps A -> (B -> C).
3050 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3052 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3053 struct isl_un_op_control control = {
3054 .filter = &un_op_filter_drop_user,
3055 .filter_user = &data,
3056 .fn_map = &isl_map_curry,
3058 return un_op(umap, &control);
3061 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3062 * return the union of the corresponding maps A -> (B -> (C -> D)).
3064 __isl_give isl_union_map *isl_union_map_range_curry(
3065 __isl_take isl_union_map *umap)
3067 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3068 struct isl_un_op_control control = {
3069 .filter = &un_op_filter_drop_user,
3070 .filter_user = &data,
3071 .fn_map = &isl_map_range_curry,
3073 return un_op(umap, &control);
3076 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3078 struct isl_un_op_control control = {
3079 .fn_map = &isl_set_lift,
3081 return un_op(uset, &control);
3084 static isl_stat coefficients_entry(void **entry, void *user)
3086 isl_set *set = *entry;
3087 isl_union_set **res = user;
3089 set = isl_set_copy(set);
3090 set = isl_set_from_basic_set(isl_set_coefficients(set));
3091 *res = isl_union_set_add_set(*res, set);
3093 return isl_stat_ok;
3096 __isl_give isl_union_set *isl_union_set_coefficients(
3097 __isl_take isl_union_set *uset)
3099 isl_ctx *ctx;
3100 isl_space *dim;
3101 isl_union_set *res;
3103 if (!uset)
3104 return NULL;
3106 ctx = isl_union_set_get_ctx(uset);
3107 dim = isl_space_set_alloc(ctx, 0, 0);
3108 res = isl_union_map_alloc(dim, uset->table.n);
3109 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3110 &coefficients_entry, &res) < 0)
3111 goto error;
3113 isl_union_set_free(uset);
3114 return res;
3115 error:
3116 isl_union_set_free(uset);
3117 isl_union_set_free(res);
3118 return NULL;
3121 static isl_stat solutions_entry(void **entry, void *user)
3123 isl_set *set = *entry;
3124 isl_union_set **res = user;
3126 set = isl_set_copy(set);
3127 set = isl_set_from_basic_set(isl_set_solutions(set));
3128 if (!*res)
3129 *res = isl_union_set_from_set(set);
3130 else
3131 *res = isl_union_set_add_set(*res, set);
3133 if (!*res)
3134 return isl_stat_error;
3136 return isl_stat_ok;
3139 __isl_give isl_union_set *isl_union_set_solutions(
3140 __isl_take isl_union_set *uset)
3142 isl_union_set *res = NULL;
3144 if (!uset)
3145 return NULL;
3147 if (uset->table.n == 0) {
3148 res = isl_union_set_empty(isl_union_set_get_space(uset));
3149 isl_union_set_free(uset);
3150 return res;
3153 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3154 &solutions_entry, &res) < 0)
3155 goto error;
3157 isl_union_set_free(uset);
3158 return res;
3159 error:
3160 isl_union_set_free(uset);
3161 isl_union_set_free(res);
3162 return NULL;
3165 /* Is the domain space of "map" equal to "space"?
3167 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3169 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3170 space, isl_dim_out);
3173 /* Is the range space of "map" equal to "space"?
3175 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3177 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3178 space, isl_dim_out);
3181 /* Is the set space of "map" equal to "space"?
3183 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3185 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3186 space, isl_dim_out);
3189 /* Internal data structure for preimage_pw_multi_aff.
3191 * "pma" is the function under which the preimage should be taken.
3192 * "space" is the space of "pma".
3193 * "res" collects the results.
3194 * "fn" computes the preimage for a given map.
3195 * "match" returns true if "fn" can be called.
3197 struct isl_union_map_preimage_data {
3198 isl_space *space;
3199 isl_pw_multi_aff *pma;
3200 isl_union_map *res;
3201 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3202 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3203 __isl_take isl_pw_multi_aff *pma);
3206 /* Call data->fn to compute the preimage of the domain or range of *entry
3207 * under the function represented by data->pma, provided the domain/range
3208 * space of *entry matches the target space of data->pma
3209 * (as given by data->match), and add the result to data->res.
3211 static isl_stat preimage_entry(void **entry, void *user)
3213 int m;
3214 isl_map *map = *entry;
3215 struct isl_union_map_preimage_data *data = user;
3216 isl_bool empty;
3218 m = data->match(map, data->space);
3219 if (m < 0)
3220 return isl_stat_error;
3221 if (!m)
3222 return isl_stat_ok;
3224 map = isl_map_copy(map);
3225 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3227 empty = isl_map_is_empty(map);
3228 if (empty < 0 || empty) {
3229 isl_map_free(map);
3230 return empty < 0 ? isl_stat_error : isl_stat_ok;
3233 data->res = isl_union_map_add_map(data->res, map);
3235 return isl_stat_ok;
3238 /* Compute the preimage of the domain or range of "umap" under the function
3239 * represented by "pma".
3240 * In other words, plug in "pma" in the domain or range of "umap".
3241 * The function "fn" performs the actual preimage computation on a map,
3242 * while "match" determines to which maps the function should be applied.
3244 static __isl_give isl_union_map *preimage_pw_multi_aff(
3245 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3246 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3247 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3248 __isl_take isl_pw_multi_aff *pma))
3250 isl_ctx *ctx;
3251 isl_space *space;
3252 struct isl_union_map_preimage_data data;
3254 umap = isl_union_map_align_params(umap,
3255 isl_pw_multi_aff_get_space(pma));
3256 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3258 if (!umap || !pma)
3259 goto error;
3261 ctx = isl_union_map_get_ctx(umap);
3262 space = isl_union_map_get_space(umap);
3263 data.space = isl_pw_multi_aff_get_space(pma);
3264 data.pma = pma;
3265 data.res = isl_union_map_alloc(space, umap->table.n);
3266 data.match = match;
3267 data.fn = fn;
3268 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3269 &data) < 0)
3270 data.res = isl_union_map_free(data.res);
3272 isl_space_free(data.space);
3273 isl_union_map_free(umap);
3274 isl_pw_multi_aff_free(pma);
3275 return data.res;
3276 error:
3277 isl_union_map_free(umap);
3278 isl_pw_multi_aff_free(pma);
3279 return NULL;
3282 /* Compute the preimage of the domain of "umap" under the function
3283 * represented by "pma".
3284 * In other words, plug in "pma" in the domain of "umap".
3285 * The result contains maps that live in the same spaces as the maps of "umap"
3286 * with domain space equal to the target space of "pma",
3287 * except that the domain has been replaced by the domain space of "pma".
3289 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3290 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3292 return preimage_pw_multi_aff(umap, pma, &domain_match,
3293 &isl_map_preimage_domain_pw_multi_aff);
3296 /* Compute the preimage of the range of "umap" under the function
3297 * represented by "pma".
3298 * In other words, plug in "pma" in the range of "umap".
3299 * The result contains maps that live in the same spaces as the maps of "umap"
3300 * with range space equal to the target space of "pma",
3301 * except that the range has been replaced by the domain space of "pma".
3303 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3304 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3306 return preimage_pw_multi_aff(umap, pma, &range_match,
3307 &isl_map_preimage_range_pw_multi_aff);
3310 /* Compute the preimage of "uset" under the function represented by "pma".
3311 * In other words, plug in "pma" in "uset".
3312 * The result contains sets that live in the same spaces as the sets of "uset"
3313 * with space equal to the target space of "pma",
3314 * except that the space has been replaced by the domain space of "pma".
3316 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3317 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3319 return preimage_pw_multi_aff(uset, pma, &set_match,
3320 &isl_set_preimage_pw_multi_aff);
3323 /* Compute the preimage of the domain of "umap" under the function
3324 * represented by "ma".
3325 * In other words, plug in "ma" in the domain of "umap".
3326 * The result contains maps that live in the same spaces as the maps of "umap"
3327 * with domain space equal to the target space of "ma",
3328 * except that the domain has been replaced by the domain space of "ma".
3330 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3331 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3333 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3334 isl_pw_multi_aff_from_multi_aff(ma));
3337 /* Compute the preimage of the range of "umap" under the function
3338 * represented by "ma".
3339 * In other words, plug in "ma" in the range of "umap".
3340 * The result contains maps that live in the same spaces as the maps of "umap"
3341 * with range space equal to the target space of "ma",
3342 * except that the range has been replaced by the domain space of "ma".
3344 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3345 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3347 return isl_union_map_preimage_range_pw_multi_aff(umap,
3348 isl_pw_multi_aff_from_multi_aff(ma));
3351 /* Compute the preimage of "uset" under the function represented by "ma".
3352 * In other words, plug in "ma" in "uset".
3353 * The result contains sets that live in the same spaces as the sets of "uset"
3354 * with space equal to the target space of "ma",
3355 * except that the space has been replaced by the domain space of "ma".
3357 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3358 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3360 return isl_union_set_preimage_pw_multi_aff(uset,
3361 isl_pw_multi_aff_from_multi_aff(ma));
3364 /* Internal data structure for preimage_multi_pw_aff.
3366 * "mpa" is the function under which the preimage should be taken.
3367 * "space" is the space of "mpa".
3368 * "res" collects the results.
3369 * "fn" computes the preimage for a given map.
3370 * "match" returns true if "fn" can be called.
3372 struct isl_union_map_preimage_mpa_data {
3373 isl_space *space;
3374 isl_multi_pw_aff *mpa;
3375 isl_union_map *res;
3376 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3377 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3378 __isl_take isl_multi_pw_aff *mpa);
3381 /* Call data->fn to compute the preimage of the domain or range of *entry
3382 * under the function represented by data->mpa, provided the domain/range
3383 * space of *entry matches the target space of data->mpa
3384 * (as given by data->match), and add the result to data->res.
3386 static isl_stat preimage_mpa_entry(void **entry, void *user)
3388 int m;
3389 isl_map *map = *entry;
3390 struct isl_union_map_preimage_mpa_data *data = user;
3391 isl_bool empty;
3393 m = data->match(map, data->space);
3394 if (m < 0)
3395 return isl_stat_error;
3396 if (!m)
3397 return isl_stat_ok;
3399 map = isl_map_copy(map);
3400 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3402 empty = isl_map_is_empty(map);
3403 if (empty < 0 || empty) {
3404 isl_map_free(map);
3405 return empty < 0 ? isl_stat_error : isl_stat_ok;
3408 data->res = isl_union_map_add_map(data->res, map);
3410 return isl_stat_ok;
3413 /* Compute the preimage of the domain or range of "umap" under the function
3414 * represented by "mpa".
3415 * In other words, plug in "mpa" in the domain or range of "umap".
3416 * The function "fn" performs the actual preimage computation on a map,
3417 * while "match" determines to which maps the function should be applied.
3419 static __isl_give isl_union_map *preimage_multi_pw_aff(
3420 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3421 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3422 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3423 __isl_take isl_multi_pw_aff *mpa))
3425 isl_ctx *ctx;
3426 isl_space *space;
3427 struct isl_union_map_preimage_mpa_data data;
3429 umap = isl_union_map_align_params(umap,
3430 isl_multi_pw_aff_get_space(mpa));
3431 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3433 if (!umap || !mpa)
3434 goto error;
3436 ctx = isl_union_map_get_ctx(umap);
3437 space = isl_union_map_get_space(umap);
3438 data.space = isl_multi_pw_aff_get_space(mpa);
3439 data.mpa = mpa;
3440 data.res = isl_union_map_alloc(space, umap->table.n);
3441 data.match = match;
3442 data.fn = fn;
3443 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3444 &data) < 0)
3445 data.res = isl_union_map_free(data.res);
3447 isl_space_free(data.space);
3448 isl_union_map_free(umap);
3449 isl_multi_pw_aff_free(mpa);
3450 return data.res;
3451 error:
3452 isl_union_map_free(umap);
3453 isl_multi_pw_aff_free(mpa);
3454 return NULL;
3457 /* Compute the preimage of the domain of "umap" under the function
3458 * represented by "mpa".
3459 * In other words, plug in "mpa" in the domain of "umap".
3460 * The result contains maps that live in the same spaces as the maps of "umap"
3461 * with domain space equal to the target space of "mpa",
3462 * except that the domain has been replaced by the domain space of "mpa".
3464 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3465 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3467 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3468 &isl_map_preimage_domain_multi_pw_aff);
3471 /* Internal data structure for preimage_upma.
3473 * "umap" is the map of which the preimage should be computed.
3474 * "res" collects the results.
3475 * "fn" computes the preimage for a given piecewise multi-affine function.
3477 struct isl_union_map_preimage_upma_data {
3478 isl_union_map *umap;
3479 isl_union_map *res;
3480 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3481 __isl_take isl_pw_multi_aff *pma);
3484 /* Call data->fn to compute the preimage of the domain or range of data->umap
3485 * under the function represented by pma and add the result to data->res.
3487 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3489 struct isl_union_map_preimage_upma_data *data = user;
3490 isl_union_map *umap;
3492 umap = isl_union_map_copy(data->umap);
3493 umap = data->fn(umap, pma);
3494 data->res = isl_union_map_union(data->res, umap);
3496 return data->res ? isl_stat_ok : isl_stat_error;
3499 /* Compute the preimage of the domain or range of "umap" under the function
3500 * represented by "upma".
3501 * In other words, plug in "upma" in the domain or range of "umap".
3502 * The function "fn" performs the actual preimage computation
3503 * on a piecewise multi-affine function.
3505 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3506 __isl_take isl_union_map *umap,
3507 __isl_take isl_union_pw_multi_aff *upma,
3508 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3509 __isl_take isl_pw_multi_aff *pma))
3511 struct isl_union_map_preimage_upma_data data;
3513 data.umap = umap;
3514 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3515 data.fn = fn;
3516 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3517 &preimage_upma, &data) < 0)
3518 data.res = isl_union_map_free(data.res);
3520 isl_union_map_free(umap);
3521 isl_union_pw_multi_aff_free(upma);
3523 return data.res;
3526 /* Compute the preimage of the domain of "umap" under the function
3527 * represented by "upma".
3528 * In other words, plug in "upma" in the domain of "umap".
3529 * The result contains maps that live in the same spaces as the maps of "umap"
3530 * with domain space equal to one of the target spaces of "upma",
3531 * except that the domain has been replaced by one of the domain spaces that
3532 * correspond to that target space of "upma".
3534 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3535 __isl_take isl_union_map *umap,
3536 __isl_take isl_union_pw_multi_aff *upma)
3538 return preimage_union_pw_multi_aff(umap, upma,
3539 &isl_union_map_preimage_domain_pw_multi_aff);
3542 /* Compute the preimage of the range of "umap" under the function
3543 * represented by "upma".
3544 * In other words, plug in "upma" in the range of "umap".
3545 * The result contains maps that live in the same spaces as the maps of "umap"
3546 * with range space equal to one of the target spaces of "upma",
3547 * except that the range has been replaced by one of the domain spaces that
3548 * correspond to that target space of "upma".
3550 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3551 __isl_take isl_union_map *umap,
3552 __isl_take isl_union_pw_multi_aff *upma)
3554 return preimage_union_pw_multi_aff(umap, upma,
3555 &isl_union_map_preimage_range_pw_multi_aff);
3558 /* Compute the preimage of "uset" under the function represented by "upma".
3559 * In other words, plug in "upma" in the range of "uset".
3560 * The result contains sets that live in the same spaces as the sets of "uset"
3561 * with space equal to one of the target spaces of "upma",
3562 * except that the space has been replaced by one of the domain spaces that
3563 * correspond to that target space of "upma".
3565 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3566 __isl_take isl_union_set *uset,
3567 __isl_take isl_union_pw_multi_aff *upma)
3569 return preimage_union_pw_multi_aff(uset, upma,
3570 &isl_union_set_preimage_pw_multi_aff);
3573 /* Reset the user pointer on all identifiers of parameters and tuples
3574 * of the spaces of "umap".
3576 __isl_give isl_union_map *isl_union_map_reset_user(
3577 __isl_take isl_union_map *umap)
3579 umap = isl_union_map_cow(umap);
3580 if (!umap)
3581 return NULL;
3582 umap->dim = isl_space_reset_user(umap->dim);
3583 if (!umap->dim)
3584 return isl_union_map_free(umap);
3585 return total(umap, &isl_map_reset_user);
3588 /* Reset the user pointer on all identifiers of parameters and tuples
3589 * of the spaces of "uset".
3591 __isl_give isl_union_set *isl_union_set_reset_user(
3592 __isl_take isl_union_set *uset)
3594 return isl_union_map_reset_user(uset);
3597 /* Remove all existentially quantified variables and integer divisions
3598 * from "umap" using Fourier-Motzkin elimination.
3600 __isl_give isl_union_map *isl_union_map_remove_divs(
3601 __isl_take isl_union_map *umap)
3603 return total(umap, &isl_map_remove_divs);
3606 /* Remove all existentially quantified variables and integer divisions
3607 * from "uset" using Fourier-Motzkin elimination.
3609 __isl_give isl_union_set *isl_union_set_remove_divs(
3610 __isl_take isl_union_set *uset)
3612 return isl_union_map_remove_divs(uset);
3615 /* Internal data structure for isl_union_map_project_out.
3616 * "type", "first" and "n" are the arguments for the isl_map_project_out
3617 * call.
3618 * "res" collects the results.
3620 struct isl_union_map_project_out_data {
3621 enum isl_dim_type type;
3622 unsigned first;
3623 unsigned n;
3625 isl_union_map *res;
3628 /* Turn the data->n dimensions of type data->type, starting at data->first
3629 * into existentially quantified variables and add the result to data->res.
3631 static isl_stat project_out(__isl_take isl_map *map, void *user)
3633 struct isl_union_map_project_out_data *data = user;
3635 map = isl_map_project_out(map, data->type, data->first, data->n);
3636 data->res = isl_union_map_add_map(data->res, map);
3638 return isl_stat_ok;
3641 /* Turn the "n" dimensions of type "type", starting at "first"
3642 * into existentially quantified variables.
3643 * Since the space of an isl_union_map only contains parameters,
3644 * type is required to be equal to isl_dim_param.
3646 __isl_give isl_union_map *isl_union_map_project_out(
3647 __isl_take isl_union_map *umap,
3648 enum isl_dim_type type, unsigned first, unsigned n)
3650 isl_space *space;
3651 struct isl_union_map_project_out_data data = { type, first, n };
3653 if (!umap)
3654 return NULL;
3656 if (type != isl_dim_param)
3657 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3658 "can only project out parameters",
3659 return isl_union_map_free(umap));
3661 space = isl_union_map_get_space(umap);
3662 space = isl_space_drop_dims(space, type, first, n);
3663 data.res = isl_union_map_empty(space);
3664 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3665 data.res = isl_union_map_free(data.res);
3667 isl_union_map_free(umap);
3669 return data.res;
3672 /* Project out all parameters from "umap" by existentially quantifying
3673 * over them.
3675 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3676 __isl_take isl_union_map *umap)
3678 unsigned n;
3680 if (!umap)
3681 return NULL;
3682 n = isl_union_map_dim(umap, isl_dim_param);
3683 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3686 /* Turn the "n" dimensions of type "type", starting at "first"
3687 * into existentially quantified variables.
3688 * Since the space of an isl_union_set only contains parameters,
3689 * "type" is required to be equal to isl_dim_param.
3691 __isl_give isl_union_set *isl_union_set_project_out(
3692 __isl_take isl_union_set *uset,
3693 enum isl_dim_type type, unsigned first, unsigned n)
3695 return isl_union_map_project_out(uset, type, first, n);
3698 /* Internal data structure for isl_union_map_involves_dims.
3699 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3701 struct isl_union_map_involves_dims_data {
3702 unsigned first;
3703 unsigned n;
3706 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3708 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3710 struct isl_union_map_involves_dims_data *data = user;
3711 isl_bool involves;
3713 involves = isl_map_involves_dims(map,
3714 isl_dim_param, data->first, data->n);
3715 if (involves < 0)
3716 return isl_bool_error;
3717 return !involves;
3720 /* Does "umap" involve any of the n parameters starting at first?
3721 * "type" is required to be set to isl_dim_param.
3723 * "umap" involves any of those parameters if any of its maps
3724 * involve the parameters. In other words, "umap" does not
3725 * involve any of the parameters if all its maps to not
3726 * involve the parameters.
3728 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3729 enum isl_dim_type type, unsigned first, unsigned n)
3731 struct isl_union_map_involves_dims_data data = { first, n };
3732 isl_bool excludes;
3734 if (type != isl_dim_param)
3735 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3736 "can only reference parameters", return isl_bool_error);
3738 excludes = union_map_forall_user(umap, &map_excludes, &data);
3740 if (excludes < 0)
3741 return isl_bool_error;
3743 return !excludes;
3746 /* Internal data structure for isl_union_map_reset_range_space.
3747 * "range" is the space from which to set the range space.
3748 * "res" collects the results.
3750 struct isl_union_map_reset_range_space_data {
3751 isl_space *range;
3752 isl_union_map *res;
3755 /* Replace the range space of "map" by the range space of data->range and
3756 * add the result to data->res.
3758 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3760 struct isl_union_map_reset_range_space_data *data = user;
3761 isl_space *space;
3763 space = isl_map_get_space(map);
3764 space = isl_space_domain(space);
3765 space = isl_space_extend_domain_with_range(space,
3766 isl_space_copy(data->range));
3767 map = isl_map_reset_space(map, space);
3768 data->res = isl_union_map_add_map(data->res, map);
3770 return data->res ? isl_stat_ok : isl_stat_error;
3773 /* Replace the range space of all the maps in "umap" by
3774 * the range space of "space".
3776 * This assumes that all maps have the same output dimension.
3777 * This function should therefore not be made publicly available.
3779 * Since the spaces of the maps change, so do their hash value.
3780 * We therefore need to create a new isl_union_map.
3782 __isl_give isl_union_map *isl_union_map_reset_range_space(
3783 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3785 struct isl_union_map_reset_range_space_data data = { space };
3787 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3788 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3789 data.res = isl_union_map_free(data.res);
3791 isl_space_free(space);
3792 isl_union_map_free(umap);
3793 return data.res;
3796 /* Check that "umap" and "space" have the same number of parameters.
3798 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3799 __isl_keep isl_space *space)
3801 unsigned dim1, dim2;
3803 if (!umap || !space)
3804 return isl_stat_error;
3805 dim1 = isl_union_map_dim(umap, isl_dim_param);
3806 dim2 = isl_space_dim(space, isl_dim_param);
3807 if (dim1 == dim2)
3808 return isl_stat_ok;
3809 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3810 "number of parameters does not match", return isl_stat_error);
3813 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3814 * "space" is the target space.
3815 * "res" collects the results.
3817 struct isl_union_map_reset_params_data {
3818 isl_space *space;
3819 isl_union_map *res;
3822 /* Replace the parameters of "map" by those of data->space and
3823 * add the result to data->res.
3825 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3827 struct isl_union_map_reset_params_data *data = user;
3828 isl_space *space;
3830 space = isl_map_get_space(map);
3831 space = isl_space_replace_params(space, data->space);
3832 map = isl_map_reset_equal_dim_space(map, space);
3833 data->res = isl_union_map_add_map(data->res, map);
3835 return data->res ? isl_stat_ok : isl_stat_error;
3838 /* Replace the space of "umap" by "space", without modifying
3839 * the dimension of "umap", i.e., the number of parameters of "umap".
3841 * Since the hash values of the maps in the union map depend
3842 * on the parameters, a new union map needs to be constructed.
3844 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3845 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3847 struct isl_union_map_reset_params_data data = { space };
3848 isl_bool equal;
3849 isl_space *umap_space;
3851 umap_space = isl_union_map_peek_space(umap);
3852 equal = isl_space_is_equal(umap_space, space);
3853 if (equal < 0)
3854 goto error;
3855 if (equal) {
3856 isl_space_free(space);
3857 return umap;
3859 if (check_union_map_space_equal_dim(umap, space) < 0)
3860 goto error;
3862 data.res = isl_union_map_empty(isl_space_copy(space));
3863 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3864 data.res = isl_union_map_free(data.res);
3866 isl_space_free(space);
3867 isl_union_map_free(umap);
3868 return data.res;
3869 error:
3870 isl_union_map_free(umap);
3871 isl_space_free(space);
3872 return NULL;
3875 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3876 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3877 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3878 * to the domain and the range of each map.
3879 * "res" collects the results.
3881 struct isl_union_order_at_data {
3882 isl_multi_union_pw_aff *mupa;
3883 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3884 __isl_take isl_multi_pw_aff *mpa2);
3885 isl_union_map *res;
3888 /* Intersect "map" with the result of applying data->order to
3889 * the functions in data->mupa that apply to the domain and the range
3890 * of "map" and add the result to data->res.
3892 static isl_stat order_at(__isl_take isl_map *map, void *user)
3894 struct isl_union_order_at_data *data = user;
3895 isl_space *space;
3896 isl_multi_pw_aff *mpa1, *mpa2;
3897 isl_map *order;
3899 space = isl_space_domain(isl_map_get_space(map));
3900 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3901 space = isl_space_range(isl_map_get_space(map));
3902 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3903 order = data->order(mpa1, mpa2);
3904 map = isl_map_intersect(map, order);
3905 data->res = isl_union_map_add_map(data->res, map);
3907 return data->res ? isl_stat_ok : isl_stat_error;
3910 /* If "mupa" has a non-trivial explicit domain, then intersect
3911 * domain and range of "umap" with this explicit domain.
3912 * If the explicit domain only describes constraints on the parameters,
3913 * then the intersection only needs to be performed once.
3915 static __isl_give isl_union_map *intersect_explicit_domain(
3916 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
3918 isl_bool non_trivial, is_params;
3919 isl_union_set *dom;
3921 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3922 if (non_trivial < 0)
3923 return isl_union_map_free(umap);
3924 if (!non_trivial)
3925 return umap;
3926 mupa = isl_multi_union_pw_aff_copy(mupa);
3927 dom = isl_multi_union_pw_aff_domain(mupa);
3928 is_params = isl_union_set_is_params(dom);
3929 if (is_params < 0) {
3930 isl_union_set_free(dom);
3931 return isl_union_map_free(umap);
3933 if (is_params) {
3934 isl_set *set;
3936 set = isl_union_set_params(dom);
3937 umap = isl_union_map_intersect_params(umap, set);
3938 return umap;
3940 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
3941 umap = isl_union_map_intersect_range(umap, dom);
3942 return umap;
3945 /* Intersect each map in "umap" with the result of calling "order"
3946 * on the functions is "mupa" that apply to the domain and the range
3947 * of the map.
3949 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3950 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3951 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3952 __isl_take isl_multi_pw_aff *mpa2))
3954 struct isl_union_order_at_data data;
3956 umap = isl_union_map_align_params(umap,
3957 isl_multi_union_pw_aff_get_space(mupa));
3958 mupa = isl_multi_union_pw_aff_align_params(mupa,
3959 isl_union_map_get_space(umap));
3960 umap = intersect_explicit_domain(umap, mupa);
3961 data.mupa = mupa;
3962 data.order = order;
3963 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3964 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3965 data.res = isl_union_map_free(data.res);
3967 isl_multi_union_pw_aff_free(mupa);
3968 isl_union_map_free(umap);
3969 return data.res;
3972 /* Return the subset of "umap" where the domain and the range
3973 * have equal "mupa" values.
3975 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3976 __isl_take isl_union_map *umap,
3977 __isl_take isl_multi_union_pw_aff *mupa)
3979 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3980 &isl_multi_pw_aff_eq_map);
3983 /* Return the subset of "umap" where the domain has a lexicographically
3984 * smaller "mupa" value than the range.
3986 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3987 __isl_take isl_union_map *umap,
3988 __isl_take isl_multi_union_pw_aff *mupa)
3990 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3991 &isl_multi_pw_aff_lex_lt_map);
3994 /* Return the subset of "umap" where the domain has a lexicographically
3995 * greater "mupa" value than the range.
3997 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3998 __isl_take isl_union_map *umap,
3999 __isl_take isl_multi_union_pw_aff *mupa)
4001 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4002 &isl_multi_pw_aff_lex_gt_map);
4005 /* Return the union of the elements in the list "list".
4007 __isl_give isl_union_set *isl_union_set_list_union(
4008 __isl_take isl_union_set_list *list)
4010 int i, n;
4011 isl_ctx *ctx;
4012 isl_space *space;
4013 isl_union_set *res;
4015 if (!list)
4016 return NULL;
4018 ctx = isl_union_set_list_get_ctx(list);
4019 space = isl_space_params_alloc(ctx, 0);
4020 res = isl_union_set_empty(space);
4022 n = isl_union_set_list_n_union_set(list);
4023 for (i = 0; i < n; ++i) {
4024 isl_union_set *uset_i;
4026 uset_i = isl_union_set_list_get_union_set(list, i);
4027 res = isl_union_set_union(res, uset_i);
4030 isl_union_set_list_free(list);
4031 return res;
4034 /* Update *hash with the hash value of "map".
4036 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4038 uint32_t *hash = user;
4039 uint32_t map_hash;
4041 map_hash = isl_map_get_hash(map);
4042 isl_hash_hash(*hash, map_hash);
4044 isl_map_free(map);
4045 return isl_stat_ok;
4048 /* Return a hash value that digests "umap".
4050 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4052 uint32_t hash;
4054 if (!umap)
4055 return 0;
4057 hash = isl_hash_init();
4058 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4059 return 0;
4061 return hash;
4064 /* Return a hash value that digests "uset".
4066 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4068 return isl_union_map_get_hash(uset);
4071 /* Add the number of basic sets in "set" to "n".
4073 static isl_stat add_n(__isl_take isl_set *set, void *user)
4075 int *n = user;
4077 *n += isl_set_n_basic_set(set);
4078 isl_set_free(set);
4080 return isl_stat_ok;
4083 /* Return the total number of basic sets in "uset".
4085 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4087 int n = 0;
4089 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4090 return -1;
4092 return n;
4095 /* Add the basic sets in "set" to "list".
4097 static isl_stat add_list(__isl_take isl_set *set, void *user)
4099 isl_basic_set_list **list = user;
4100 isl_basic_set_list *list_i;
4102 list_i = isl_set_get_basic_set_list(set);
4103 *list = isl_basic_set_list_concat(*list, list_i);
4104 isl_set_free(set);
4106 if (!*list)
4107 return isl_stat_error;
4108 return isl_stat_ok;
4111 /* Return a list containing all the basic sets in "uset".
4113 * First construct a list of the appropriate size and
4114 * then add all the elements.
4116 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4117 __isl_keep isl_union_set *uset)
4119 int n;
4120 isl_ctx *ctx;
4121 isl_basic_set_list *list;
4123 if (!uset)
4124 return NULL;
4125 ctx = isl_union_set_get_ctx(uset);
4126 n = isl_union_set_n_basic_set(uset);
4127 if (n < 0)
4128 return NULL;
4129 list = isl_basic_set_list_alloc(ctx, n);
4130 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4131 list = isl_basic_set_list_free(list);
4133 return list;
4136 /* Internal data structure for isl_union_map_remove_map_if.
4137 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4139 struct isl_union_map_remove_map_if_data {
4140 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4141 void *user;
4144 /* isl_un_op_control filter that negates the result of data->fn
4145 * called on "map".
4147 static isl_bool not(__isl_keep isl_map *map, void *user)
4149 struct isl_union_map_remove_map_if_data *data = user;
4151 return isl_bool_not(data->fn(map, data->user));
4154 /* Dummy isl_un_op_control transformation callback that
4155 * simply returns the input.
4157 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4159 return map;
4162 /* Call "fn" on every map in "umap" and remove those maps
4163 * for which the callback returns true.
4165 * Use un_op to keep only those maps that are not filtered out,
4166 * applying an identity transformation on them.
4168 __isl_give isl_union_map *isl_union_map_remove_map_if(
4169 __isl_take isl_union_map *umap,
4170 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4172 struct isl_union_map_remove_map_if_data data = { fn, user };
4173 struct isl_un_op_control control = {
4174 .filter = &not,
4175 .filter_user = &data,
4176 .fn_map = &map_id,
4178 return un_op(umap, &control);