isl_tab_pip.c: enter_level: extract out finished_all_cases
[isl.git] / isl_union_map.c
blob2f55f1f2bae6be0b42c6cb82360ab48e8bb22292
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #define ISL_DIM_H
17 #include <isl_map_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl/ctx.h>
20 #include <isl/hash.h>
21 #include <isl/aff.h>
22 #include <isl/map.h>
23 #include <isl/set.h>
24 #include <isl_space_private.h>
25 #include <isl/union_set.h>
26 #include <isl_maybe_map.h>
27 #include <isl/deprecated/union_map_int.h>
29 #include <bset_from_bmap.c>
30 #include <set_to_map.c>
31 #include <set_from_map.c>
33 /* Return the number of parameters of "umap", where "type"
34 * is required to be set to isl_dim_param.
36 unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
37 enum isl_dim_type type)
39 if (!umap)
40 return 0;
42 if (type != isl_dim_param)
43 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
44 "can only reference parameters", return 0);
46 return isl_space_dim(umap->dim, type);
49 /* Return the number of parameters of "uset", where "type"
50 * is required to be set to isl_dim_param.
52 unsigned isl_union_set_dim(__isl_keep isl_union_set *uset,
53 enum isl_dim_type type)
55 return isl_union_map_dim(uset, type);
58 /* Return the id of the specified dimension.
60 __isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
61 enum isl_dim_type type, unsigned pos)
63 if (!umap)
64 return NULL;
66 if (type != isl_dim_param)
67 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
68 "can only reference parameters", return NULL);
70 return isl_space_get_dim_id(umap->dim, type, pos);
73 /* Is this union set a parameter domain?
75 isl_bool isl_union_set_is_params(__isl_keep isl_union_set *uset)
77 isl_set *set;
78 isl_bool params;
80 if (!uset)
81 return isl_bool_error;
82 if (uset->table.n != 1)
83 return isl_bool_false;
85 set = isl_set_from_union_set(isl_union_set_copy(uset));
86 params = isl_set_is_params(set);
87 isl_set_free(set);
88 return params;
91 static __isl_give isl_union_map *isl_union_map_alloc(
92 __isl_take isl_space *space, int size)
94 isl_union_map *umap;
96 space = isl_space_params(space);
97 if (!space)
98 return NULL;
100 umap = isl_calloc_type(space->ctx, isl_union_map);
101 if (!umap) {
102 isl_space_free(space);
103 return NULL;
106 umap->ref = 1;
107 umap->dim = space;
108 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
109 return isl_union_map_free(umap);
111 return umap;
114 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
116 return isl_union_map_alloc(space, 16);
119 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
121 return isl_union_map_empty(space);
124 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
126 return umap ? umap->dim->ctx : NULL;
129 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
131 return uset ? uset->dim->ctx : NULL;
134 /* Return the space of "umap".
136 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
138 return umap ? umap->dim : NULL;
141 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
143 return isl_space_copy(isl_union_map_peek_space(umap));
146 /* Return the position of the parameter with the given name
147 * in "umap".
148 * Return -1 if no such dimension can be found.
150 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
151 enum isl_dim_type type, const char *name)
153 if (!umap)
154 return -1;
155 return isl_space_find_dim_by_name(umap->dim, type, name);
158 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
160 return isl_union_map_get_space(uset);
163 static isl_stat free_umap_entry(void **entry, void *user)
165 isl_map *map = *entry;
166 isl_map_free(map);
167 return isl_stat_ok;
170 static isl_stat add_map(__isl_take isl_map *map, void *user)
172 isl_union_map **umap = (isl_union_map **)user;
174 *umap = isl_union_map_add_map(*umap, map);
176 return isl_stat_ok;
179 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
181 isl_union_map *dup;
183 if (!umap)
184 return NULL;
186 dup = isl_union_map_empty(isl_space_copy(umap->dim));
187 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
188 goto error;
189 return dup;
190 error:
191 isl_union_map_free(dup);
192 return NULL;
195 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
197 if (!umap)
198 return NULL;
200 if (umap->ref == 1)
201 return umap;
202 umap->ref--;
203 return isl_union_map_dup(umap);
206 struct isl_union_align {
207 isl_reordering *exp;
208 isl_union_map *res;
211 static isl_stat align_entry(void **entry, void *user)
213 isl_map *map = *entry;
214 isl_reordering *exp;
215 struct isl_union_align *data = user;
217 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
218 isl_map_get_space(map));
220 data->res = isl_union_map_add_map(data->res,
221 isl_map_realign(isl_map_copy(map), exp));
223 return isl_stat_ok;
226 /* Align the parameters of umap along those of model.
227 * The result has the parameters of model first, in the same order
228 * as they appear in model, followed by any remaining parameters of
229 * umap that do not appear in model.
231 __isl_give isl_union_map *isl_union_map_align_params(
232 __isl_take isl_union_map *umap, __isl_take isl_space *model)
234 struct isl_union_align data = { NULL, NULL };
235 isl_bool equal_params;
237 if (!umap || !model)
238 goto error;
240 equal_params = isl_space_has_equal_params(umap->dim, model);
241 if (equal_params < 0)
242 goto error;
243 if (equal_params) {
244 isl_space_free(model);
245 return umap;
248 model = isl_space_params(model);
249 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
250 if (!data.exp)
251 goto error;
253 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
254 umap->table.n);
255 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
256 &align_entry, &data) < 0)
257 goto error;
259 isl_reordering_free(data.exp);
260 isl_union_map_free(umap);
261 isl_space_free(model);
262 return data.res;
263 error:
264 isl_reordering_free(data.exp);
265 isl_union_map_free(umap);
266 isl_union_map_free(data.res);
267 isl_space_free(model);
268 return NULL;
271 __isl_give isl_union_set *isl_union_set_align_params(
272 __isl_take isl_union_set *uset, __isl_take isl_space *model)
274 return isl_union_map_align_params(uset, model);
277 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
278 __isl_take isl_union_map *umap2)
280 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
281 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
283 umap1 = isl_union_map_cow(umap1);
285 if (!umap1 || !umap2)
286 goto error;
288 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
289 goto error;
291 isl_union_map_free(umap2);
293 return umap1;
294 error:
295 isl_union_map_free(umap1);
296 isl_union_map_free(umap2);
297 return NULL;
300 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
301 __isl_take isl_union_set *uset2)
303 return isl_union_map_union(uset1, uset2);
306 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
308 if (!umap)
309 return NULL;
311 umap->ref++;
312 return umap;
315 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
317 return isl_union_map_copy(uset);
320 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
322 if (!umap)
323 return NULL;
325 if (--umap->ref > 0)
326 return NULL;
328 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
329 &free_umap_entry, NULL);
330 isl_hash_table_clear(&umap->table);
331 isl_space_free(umap->dim);
332 free(umap);
333 return NULL;
336 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
338 return isl_union_map_free(uset);
341 /* Do "umap" and "space" have the same parameters?
343 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
344 __isl_keep isl_space *space)
346 isl_space *umap_space;
348 umap_space = isl_union_map_peek_space(umap);
349 return isl_space_has_equal_params(umap_space, space);
352 static int has_space(const void *entry, const void *val)
354 isl_map *map = (isl_map *)entry;
355 isl_space *space = (isl_space *) val;
357 return isl_space_is_equal(map->dim, space);
360 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
361 __isl_take isl_map *map)
363 uint32_t hash;
364 struct isl_hash_table_entry *entry;
365 isl_bool aligned;
367 if (!map || !umap)
368 goto error;
370 if (isl_map_plain_is_empty(map)) {
371 isl_map_free(map);
372 return umap;
375 aligned = isl_map_space_has_equal_params(map, umap->dim);
376 if (aligned < 0)
377 goto error;
378 if (!aligned) {
379 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
380 map = isl_map_align_params(map, isl_union_map_get_space(umap));
383 umap = isl_union_map_cow(umap);
385 if (!map || !umap)
386 goto error;
388 hash = isl_space_get_hash(map->dim);
389 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
390 &has_space, map->dim, 1);
391 if (!entry)
392 goto error;
394 if (!entry->data)
395 entry->data = map;
396 else {
397 entry->data = isl_map_union(entry->data, isl_map_copy(map));
398 if (!entry->data)
399 goto error;
400 isl_map_free(map);
403 return umap;
404 error:
405 isl_map_free(map);
406 isl_union_map_free(umap);
407 return NULL;
410 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
411 __isl_take isl_set *set)
413 return isl_union_map_add_map(uset, set_to_map(set));
416 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
418 isl_space *dim;
419 isl_union_map *umap;
421 if (!map)
422 return NULL;
424 dim = isl_map_get_space(map);
425 dim = isl_space_params(dim);
426 umap = isl_union_map_empty(dim);
427 umap = isl_union_map_add_map(umap, map);
429 return umap;
432 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
434 return isl_union_map_from_map(set_to_map(set));
437 __isl_give isl_union_map *isl_union_map_from_basic_map(
438 __isl_take isl_basic_map *bmap)
440 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
443 __isl_give isl_union_set *isl_union_set_from_basic_set(
444 __isl_take isl_basic_set *bset)
446 return isl_union_map_from_basic_map(bset);
449 struct isl_union_map_foreach_data
451 isl_stat (*fn)(__isl_take isl_map *map, void *user);
452 void *user;
455 static isl_stat call_on_copy(void **entry, void *user)
457 isl_map *map = *entry;
458 struct isl_union_map_foreach_data *data;
459 data = (struct isl_union_map_foreach_data *)user;
461 return data->fn(isl_map_copy(map), data->user);
464 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
466 return umap ? umap->table.n : 0;
469 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
471 return uset ? uset->table.n : 0;
474 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
475 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
477 struct isl_union_map_foreach_data data = { fn, user };
479 if (!umap)
480 return isl_stat_error;
482 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
483 &call_on_copy, &data);
486 /* Internal data structure for isl_union_map_every_map.
488 * "test" is the user-specified callback function.
489 * "user" is the user-specified callback function argument.
491 * "failed" is initialized to 0 and set to 1 if "test" fails
492 * on any map.
494 struct isl_union_map_every_data {
495 isl_bool (*test)(__isl_keep isl_map *map, void *user);
496 void *user;
497 int failed;
500 /* Call data->test on "map".
501 * If this fails, then set data->failed and abort.
503 static isl_stat call_every(void **entry, void *user)
505 isl_map *map = *entry;
506 struct isl_union_map_every_data *data = user;
507 isl_bool r;
509 r = data->test(map, data->user);
510 if (r < 0)
511 return isl_stat_error;
512 if (r)
513 return isl_stat_ok;
514 data->failed = 1;
515 return isl_stat_error;
518 /* Does "test" succeed on every map in "umap"?
520 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
521 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
523 struct isl_union_map_every_data data = { test, user, 0 };
524 isl_stat r;
526 if (!umap)
527 return isl_bool_error;
529 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
530 &call_every, &data);
531 if (r >= 0)
532 return isl_bool_true;
533 if (data.failed)
534 return isl_bool_false;
535 return isl_bool_error;
538 static isl_stat copy_map(void **entry, void *user)
540 isl_map *map = *entry;
541 isl_map **map_p = user;
543 *map_p = isl_map_copy(map);
545 return isl_stat_error;
548 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
550 isl_ctx *ctx;
551 isl_map *map = NULL;
553 if (!umap)
554 return NULL;
555 ctx = isl_union_map_get_ctx(umap);
556 if (umap->table.n != 1)
557 isl_die(ctx, isl_error_invalid,
558 "union map needs to contain elements in exactly "
559 "one space", goto error);
561 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
563 isl_union_map_free(umap);
565 return map;
566 error:
567 isl_union_map_free(umap);
568 return NULL;
571 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
573 return isl_map_from_union_map(uset);
576 /* Extract the map in "umap" that lives in the given space (ignoring
577 * parameters).
579 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
580 __isl_take isl_space *space)
582 uint32_t hash;
583 struct isl_hash_table_entry *entry;
585 space = isl_space_drop_dims(space, isl_dim_param,
586 0, isl_space_dim(space, isl_dim_param));
587 space = isl_space_align_params(space, isl_union_map_get_space(umap));
588 if (!umap || !space)
589 goto error;
591 hash = isl_space_get_hash(space);
592 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
593 &has_space, space, 0);
594 if (!entry)
595 return isl_map_empty(space);
596 isl_space_free(space);
597 return isl_map_copy(entry->data);
598 error:
599 isl_space_free(space);
600 return NULL;
603 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
604 __isl_take isl_space *dim)
606 return set_from_map(isl_union_map_extract_map(uset, dim));
609 /* Check if umap contains a map in the given space.
611 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
612 __isl_keep isl_space *space)
614 uint32_t hash;
615 struct isl_hash_table_entry *entry;
617 if (!umap || !space)
618 return isl_bool_error;
620 hash = isl_space_get_hash(space);
621 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
622 &has_space, space, 0);
623 return !!entry;
626 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
627 __isl_keep isl_space *space)
629 return isl_union_map_contains(uset, space);
632 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
633 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
635 return isl_union_map_foreach_map(uset,
636 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
639 struct isl_union_set_foreach_point_data {
640 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
641 void *user;
644 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
646 struct isl_union_set_foreach_point_data *data = user;
647 isl_stat r;
649 r = isl_set_foreach_point(set, data->fn, data->user);
650 isl_set_free(set);
652 return r;
655 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
656 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
658 struct isl_union_set_foreach_point_data data = { fn, user };
659 return isl_union_set_foreach_set(uset, &foreach_point, &data);
662 /* Data structure that specifies how gen_bin_op should
663 * construct results from the inputs.
665 * If "subtract" is set, then a map in the first input is copied to the result
666 * if there is no corresponding map in the second input.
667 * Otherwise, a map in the first input with no corresponding map
668 * in the second input is ignored.
669 * If "filter" is not NULL, then it specifies which maps in the first
670 * input may have a matching map in the second input.
671 * In particular, it makes sure that "match_space" can be called
672 * on the space of the map.
673 * "match_space" specifies how to transform the space of a map
674 * in the first input to the space of the corresponding map
675 * in the second input.
676 * "fn_map" specifies how the matching maps, one from each input,
677 * should be combined to form a map in the result.
679 struct isl_bin_op_control {
680 int subtract;
681 isl_bool (*filter)(__isl_keep isl_map *map);
682 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
683 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
684 __isl_take isl_map *map2);
687 /* Internal data structure for gen_bin_op.
688 * "control" specifies how the maps in the result should be constructed.
689 * "umap2" is a pointer to the second argument.
690 * "res" collects the results.
692 struct isl_union_map_gen_bin_data {
693 struct isl_bin_op_control *control;
694 isl_union_map *umap2;
695 isl_union_map *res;
698 /* Add a copy of "map" to "res" and return the result.
700 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
701 __isl_keep isl_map *map)
703 return isl_union_map_add_map(res, isl_map_copy(map));
706 /* Combine "map1" and "map2", add the result to "res" and return the result.
707 * Check whether the result is empty before adding it to "res".
709 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
710 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
711 struct isl_union_map_gen_bin_data *data)
713 isl_bool empty;
714 isl_map *map;
716 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
717 empty = isl_map_is_empty(map);
718 if (empty < 0 || empty) {
719 isl_map_free(map);
720 if (empty < 0)
721 return isl_union_map_free(res);
722 return res;
724 return isl_union_map_add_map(res, map);
727 /* Dummy match_space function that simply returns the input space.
729 static __isl_give isl_space *identity(__isl_take isl_space *space)
731 return space;
734 /* Look for the map in data->umap2 that corresponds to "map", if any.
735 * Return (isl_bool_true, matching map) if there is one,
736 * (isl_bool_false, NULL) if there is no matching map and
737 * (isl_bool_error, NULL) on error.
739 * If not NULL, then data->control->filter specifies whether "map"
740 * can have any matching map. If so,
741 * data->control->match_space specifies which map in data->umap2
742 * corresponds to "map".
744 static __isl_keep isl_maybe_isl_map bin_try_get_match(
745 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
747 uint32_t hash;
748 struct isl_hash_table_entry *entry2;
749 isl_space *space;
750 isl_maybe_isl_map res = { isl_bool_error, NULL };
752 if (data->control->filter) {
753 res.valid = data->control->filter(map);
754 if (res.valid < 0 || !res.valid)
755 return res;
756 res.valid = isl_bool_error;
759 space = isl_map_get_space(map);
760 if (data->control->match_space != &identity)
761 space = data->control->match_space(space);
762 if (!space)
763 return res;
764 hash = isl_space_get_hash(space);
765 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
766 &data->umap2->table, hash,
767 &has_space, space, 0);
768 isl_space_free(space);
769 res.valid = entry2 != NULL;
770 if (entry2)
771 res.value = entry2->data;
773 return res;
776 /* isl_hash_table_foreach callback for gen_bin_op.
777 * Look for the map in data->umap2 that corresponds
778 * to the map that "entry" points to, apply the binary operation and
779 * add the result to data->res.
781 * If no corresponding map can be found, then the effect depends
782 * on data->control->subtract. If it is set, then the current map
783 * is added directly to the result. Otherwise, it is ignored.
785 static isl_stat gen_bin_entry(void **entry, void *user)
787 struct isl_union_map_gen_bin_data *data = user;
788 isl_map *map = *entry;
789 isl_maybe_isl_map m;
791 m = bin_try_get_match(data, map);
792 if (m.valid < 0)
793 return isl_stat_error;
794 if (!m.valid && !data->control->subtract)
795 return isl_stat_ok;
797 if (!m.valid)
798 data->res = bin_add_map(data->res, map);
799 else
800 data->res = bin_add_pair(data->res, map, m.value, data);
801 if (!data->res)
802 return isl_stat_error;
804 return isl_stat_ok;
807 /* Apply a binary operation to "umap1" and "umap2" based on "control".
808 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
809 * in gen_bin_entry.
811 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
812 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
814 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
816 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
817 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
819 if (!umap1 || !umap2)
820 goto error;
822 data.umap2 = umap2;
823 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
824 umap1->table.n);
825 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
826 &gen_bin_entry, &data) < 0)
827 goto error;
829 isl_union_map_free(umap1);
830 isl_union_map_free(umap2);
831 return data.res;
832 error:
833 isl_union_map_free(umap1);
834 isl_union_map_free(umap2);
835 isl_union_map_free(data.res);
836 return NULL;
839 __isl_give isl_union_map *isl_union_map_subtract(
840 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
842 struct isl_bin_op_control control = {
843 .subtract = 1,
844 .match_space = &identity,
845 .fn_map = &isl_map_subtract,
848 return gen_bin_op(umap1, umap2, &control);
851 __isl_give isl_union_set *isl_union_set_subtract(
852 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
854 return isl_union_map_subtract(uset1, uset2);
857 struct isl_union_map_gen_bin_set_data {
858 isl_set *set;
859 isl_union_map *res;
862 static isl_stat intersect_params_entry(void **entry, void *user)
864 struct isl_union_map_gen_bin_set_data *data = user;
865 isl_map *map = *entry;
866 int empty;
868 map = isl_map_copy(map);
869 map = isl_map_intersect_params(map, isl_set_copy(data->set));
871 empty = isl_map_is_empty(map);
872 if (empty < 0) {
873 isl_map_free(map);
874 return isl_stat_error;
877 data->res = isl_union_map_add_map(data->res, map);
879 return isl_stat_ok;
882 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
883 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
885 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
887 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
888 set = isl_set_align_params(set, isl_union_map_get_space(umap));
890 if (!umap || !set)
891 goto error;
893 data.set = set;
894 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
895 umap->table.n);
896 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
897 fn, &data) < 0)
898 goto error;
900 isl_union_map_free(umap);
901 isl_set_free(set);
902 return data.res;
903 error:
904 isl_union_map_free(umap);
905 isl_set_free(set);
906 isl_union_map_free(data.res);
907 return NULL;
910 /* Intersect "umap" with the parameter domain "set".
912 * If "set" does not have any constraints, then we can return immediately.
914 __isl_give isl_union_map *isl_union_map_intersect_params(
915 __isl_take isl_union_map *umap, __isl_take isl_set *set)
917 int is_universe;
919 is_universe = isl_set_plain_is_universe(set);
920 if (is_universe < 0)
921 goto error;
922 if (is_universe) {
923 isl_set_free(set);
924 return umap;
927 return gen_bin_set_op(umap, set, &intersect_params_entry);
928 error:
929 isl_union_map_free(umap);
930 isl_set_free(set);
931 return NULL;
934 __isl_give isl_union_set *isl_union_set_intersect_params(
935 __isl_take isl_union_set *uset, __isl_take isl_set *set)
937 return isl_union_map_intersect_params(uset, set);
940 static __isl_give isl_union_map *union_map_intersect_params(
941 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
943 return isl_union_map_intersect_params(umap,
944 isl_set_from_union_set(uset));
947 static __isl_give isl_union_map *union_map_gist_params(
948 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
950 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
953 struct isl_union_map_match_bin_data {
954 isl_union_map *umap2;
955 isl_union_map *res;
956 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
959 static isl_stat match_bin_entry(void **entry, void *user)
961 struct isl_union_map_match_bin_data *data = user;
962 uint32_t hash;
963 struct isl_hash_table_entry *entry2;
964 isl_map *map = *entry;
965 int empty;
967 hash = isl_space_get_hash(map->dim);
968 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
969 hash, &has_space, map->dim, 0);
970 if (!entry2)
971 return isl_stat_ok;
973 map = isl_map_copy(map);
974 map = data->fn(map, isl_map_copy(entry2->data));
976 empty = isl_map_is_empty(map);
977 if (empty < 0) {
978 isl_map_free(map);
979 return isl_stat_error;
981 if (empty) {
982 isl_map_free(map);
983 return isl_stat_ok;
986 data->res = isl_union_map_add_map(data->res, map);
988 return isl_stat_ok;
991 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
992 __isl_take isl_union_map *umap2,
993 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
995 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
997 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
998 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1000 if (!umap1 || !umap2)
1001 goto error;
1003 data.umap2 = umap2;
1004 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1005 umap1->table.n);
1006 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1007 &match_bin_entry, &data) < 0)
1008 goto error;
1010 isl_union_map_free(umap1);
1011 isl_union_map_free(umap2);
1012 return data.res;
1013 error:
1014 isl_union_map_free(umap1);
1015 isl_union_map_free(umap2);
1016 isl_union_map_free(data.res);
1017 return NULL;
1020 __isl_give isl_union_map *isl_union_map_intersect(
1021 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1023 return match_bin_op(umap1, umap2, &isl_map_intersect);
1026 /* Compute the intersection of the two union_sets.
1027 * As a special case, if exactly one of the two union_sets
1028 * is a parameter domain, then intersect the parameter domain
1029 * of the other one with this set.
1031 __isl_give isl_union_set *isl_union_set_intersect(
1032 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1034 int p1, p2;
1036 p1 = isl_union_set_is_params(uset1);
1037 p2 = isl_union_set_is_params(uset2);
1038 if (p1 < 0 || p2 < 0)
1039 goto error;
1040 if (!p1 && p2)
1041 return union_map_intersect_params(uset1, uset2);
1042 if (p1 && !p2)
1043 return union_map_intersect_params(uset2, uset1);
1044 return isl_union_map_intersect(uset1, uset2);
1045 error:
1046 isl_union_set_free(uset1);
1047 isl_union_set_free(uset2);
1048 return NULL;
1051 static isl_stat gist_params_entry(void **entry, void *user)
1053 struct isl_union_map_gen_bin_set_data *data = user;
1054 isl_map *map = *entry;
1055 int empty;
1057 map = isl_map_copy(map);
1058 map = isl_map_gist_params(map, isl_set_copy(data->set));
1060 empty = isl_map_is_empty(map);
1061 if (empty < 0) {
1062 isl_map_free(map);
1063 return isl_stat_error;
1066 data->res = isl_union_map_add_map(data->res, map);
1068 return isl_stat_ok;
1071 __isl_give isl_union_map *isl_union_map_gist_params(
1072 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1074 return gen_bin_set_op(umap, set, &gist_params_entry);
1077 __isl_give isl_union_set *isl_union_set_gist_params(
1078 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1080 return isl_union_map_gist_params(uset, set);
1083 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1084 __isl_take isl_union_map *context)
1086 return match_bin_op(umap, context, &isl_map_gist);
1089 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1090 __isl_take isl_union_set *context)
1092 if (isl_union_set_is_params(context))
1093 return union_map_gist_params(uset, context);
1094 return isl_union_map_gist(uset, context);
1097 /* For each map in "umap", remove the constraints in the corresponding map
1098 * of "context".
1099 * Each map in "context" is assumed to consist of a single disjunct and
1100 * to have explicit representations for all local variables.
1102 __isl_give isl_union_map *isl_union_map_plain_gist(
1103 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1105 return match_bin_op(umap, context, &isl_map_plain_gist);
1108 /* For each set in "uset", remove the constraints in the corresponding set
1109 * of "context".
1110 * Each set in "context" is assumed to consist of a single disjunct and
1111 * to have explicit representations for all local variables.
1113 __isl_give isl_union_set *isl_union_set_plain_gist(
1114 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1116 return isl_union_map_plain_gist(uset, context);
1119 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1120 __isl_take isl_map *set2)
1122 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1125 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1126 __isl_take isl_map *set2)
1128 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1131 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1132 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1134 return match_bin_op(uset1, uset2, &lex_lt_set);
1137 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1138 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1140 return match_bin_op(uset1, uset2, &lex_le_set);
1143 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1144 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1146 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1149 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1150 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1152 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1155 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1156 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1158 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1161 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1162 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1164 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1167 /* Intersect the domain of "umap" with "uset".
1169 static __isl_give isl_union_map *union_map_intersect_domain(
1170 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1172 struct isl_bin_op_control control = {
1173 .match_space = &isl_space_domain,
1174 .fn_map = &isl_map_intersect_domain,
1177 return gen_bin_op(umap, uset, &control);
1180 /* Intersect the domain of "umap" with "uset".
1181 * If "uset" is a parameters domain, then intersect the parameter
1182 * domain of "umap" with this set.
1184 __isl_give isl_union_map *isl_union_map_intersect_domain(
1185 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1187 if (isl_union_set_is_params(uset))
1188 return union_map_intersect_params(umap, uset);
1189 else
1190 return union_map_intersect_domain(umap, uset);
1193 /* Remove the elements of "uset" from the domain of "umap".
1195 __isl_give isl_union_map *isl_union_map_subtract_domain(
1196 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1198 struct isl_bin_op_control control = {
1199 .subtract = 1,
1200 .match_space = &isl_space_domain,
1201 .fn_map = &isl_map_subtract_domain,
1204 return gen_bin_op(umap, dom, &control);
1207 /* Remove the elements of "uset" from the range of "umap".
1209 __isl_give isl_union_map *isl_union_map_subtract_range(
1210 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1212 struct isl_bin_op_control control = {
1213 .subtract = 1,
1214 .match_space = &isl_space_range,
1215 .fn_map = &isl_map_subtract_range,
1218 return gen_bin_op(umap, dom, &control);
1221 /* Compute the gist of "umap" with respect to the domain "uset".
1223 static __isl_give isl_union_map *union_map_gist_domain(
1224 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1226 struct isl_bin_op_control control = {
1227 .match_space = &isl_space_domain,
1228 .fn_map = &isl_map_gist_domain,
1231 return gen_bin_op(umap, uset, &control);
1234 /* Compute the gist of "umap" with respect to the domain "uset".
1235 * If "uset" is a parameters domain, then compute the gist
1236 * with respect to this parameter domain.
1238 __isl_give isl_union_map *isl_union_map_gist_domain(
1239 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1241 if (isl_union_set_is_params(uset))
1242 return union_map_gist_params(umap, uset);
1243 else
1244 return union_map_gist_domain(umap, uset);
1247 /* Compute the gist of "umap" with respect to the range "uset".
1249 __isl_give isl_union_map *isl_union_map_gist_range(
1250 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1252 struct isl_bin_op_control control = {
1253 .match_space = &isl_space_range,
1254 .fn_map = &isl_map_gist_range,
1257 return gen_bin_op(umap, uset, &control);
1260 __isl_give isl_union_map *isl_union_map_intersect_range(
1261 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1263 struct isl_bin_op_control control = {
1264 .match_space = &isl_space_range,
1265 .fn_map = &isl_map_intersect_range,
1268 return gen_bin_op(umap, uset, &control);
1271 /* Intersect each map in "umap" in a space A -> [B -> C]
1272 * with the corresponding map in "factor" in the space A -> C and
1273 * collect the results.
1275 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1276 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1278 struct isl_bin_op_control control = {
1279 .filter = &isl_map_range_is_wrapping,
1280 .match_space = &isl_space_range_factor_range,
1281 .fn_map = &isl_map_intersect_range_factor_range,
1284 return gen_bin_op(umap, factor, &control);
1287 struct isl_union_map_bin_data {
1288 isl_union_map *umap2;
1289 isl_union_map *res;
1290 isl_map *map;
1291 isl_stat (*fn)(void **entry, void *user);
1294 static isl_stat apply_range_entry(void **entry, void *user)
1296 struct isl_union_map_bin_data *data = user;
1297 isl_map *map2 = *entry;
1298 isl_bool empty;
1300 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1301 map2->dim, isl_dim_in))
1302 return isl_stat_ok;
1304 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1306 empty = isl_map_is_empty(map2);
1307 if (empty < 0) {
1308 isl_map_free(map2);
1309 return isl_stat_error;
1311 if (empty) {
1312 isl_map_free(map2);
1313 return isl_stat_ok;
1316 data->res = isl_union_map_add_map(data->res, map2);
1318 return isl_stat_ok;
1321 static isl_stat bin_entry(void **entry, void *user)
1323 struct isl_union_map_bin_data *data = user;
1324 isl_map *map = *entry;
1326 data->map = map;
1327 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1328 data->fn, data) < 0)
1329 return isl_stat_error;
1331 return isl_stat_ok;
1334 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1335 __isl_take isl_union_map *umap2,
1336 isl_stat (*fn)(void **entry, void *user))
1338 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1340 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1341 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1343 if (!umap1 || !umap2)
1344 goto error;
1346 data.umap2 = umap2;
1347 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1348 umap1->table.n);
1349 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1350 &bin_entry, &data) < 0)
1351 goto error;
1353 isl_union_map_free(umap1);
1354 isl_union_map_free(umap2);
1355 return data.res;
1356 error:
1357 isl_union_map_free(umap1);
1358 isl_union_map_free(umap2);
1359 isl_union_map_free(data.res);
1360 return NULL;
1363 __isl_give isl_union_map *isl_union_map_apply_range(
1364 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1366 return bin_op(umap1, umap2, &apply_range_entry);
1369 __isl_give isl_union_map *isl_union_map_apply_domain(
1370 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1372 umap1 = isl_union_map_reverse(umap1);
1373 umap1 = isl_union_map_apply_range(umap1, umap2);
1374 return isl_union_map_reverse(umap1);
1377 __isl_give isl_union_set *isl_union_set_apply(
1378 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1380 return isl_union_map_apply_range(uset, umap);
1383 static isl_stat map_lex_lt_entry(void **entry, void *user)
1385 struct isl_union_map_bin_data *data = user;
1386 isl_map *map2 = *entry;
1388 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1389 map2->dim, isl_dim_out))
1390 return isl_stat_ok;
1392 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1394 data->res = isl_union_map_add_map(data->res, map2);
1396 return isl_stat_ok;
1399 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1400 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1402 return bin_op(umap1, umap2, &map_lex_lt_entry);
1405 static isl_stat map_lex_le_entry(void **entry, void *user)
1407 struct isl_union_map_bin_data *data = user;
1408 isl_map *map2 = *entry;
1410 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1411 map2->dim, isl_dim_out))
1412 return isl_stat_ok;
1414 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1416 data->res = isl_union_map_add_map(data->res, map2);
1418 return isl_stat_ok;
1421 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1422 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1424 return bin_op(umap1, umap2, &map_lex_le_entry);
1427 static isl_stat product_entry(void **entry, void *user)
1429 struct isl_union_map_bin_data *data = user;
1430 isl_map *map2 = *entry;
1432 map2 = isl_map_product(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_product(__isl_take isl_union_map *umap1,
1440 __isl_take isl_union_map *umap2)
1442 return bin_op(umap1, umap2, &product_entry);
1445 static isl_stat set_product_entry(void **entry, void *user)
1447 struct isl_union_map_bin_data *data = user;
1448 isl_set *set2 = *entry;
1450 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1452 data->res = isl_union_set_add_set(data->res, set2);
1454 return isl_stat_ok;
1457 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1458 __isl_take isl_union_set *uset2)
1460 return bin_op(uset1, uset2, &set_product_entry);
1463 static isl_stat domain_product_entry(void **entry, void *user)
1465 struct isl_union_map_bin_data *data = user;
1466 isl_map *map2 = *entry;
1468 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1469 map2->dim, isl_dim_out))
1470 return isl_stat_ok;
1472 map2 = isl_map_domain_product(isl_map_copy(data->map),
1473 isl_map_copy(map2));
1475 data->res = isl_union_map_add_map(data->res, map2);
1477 return isl_stat_ok;
1480 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1482 __isl_give isl_union_map *isl_union_map_domain_product(
1483 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1485 return bin_op(umap1, umap2, &domain_product_entry);
1488 static isl_stat range_product_entry(void **entry, void *user)
1490 struct isl_union_map_bin_data *data = user;
1491 isl_map *map2 = *entry;
1493 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1494 map2->dim, isl_dim_in))
1495 return isl_stat_ok;
1497 map2 = isl_map_range_product(isl_map_copy(data->map),
1498 isl_map_copy(map2));
1500 data->res = isl_union_map_add_map(data->res, map2);
1502 return isl_stat_ok;
1505 __isl_give isl_union_map *isl_union_map_range_product(
1506 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1508 return bin_op(umap1, umap2, &range_product_entry);
1511 /* If data->map A -> B and "map2" C -> D have the same range space,
1512 * then add (A, C) -> (B * D) to data->res.
1514 static isl_stat flat_domain_product_entry(void **entry, void *user)
1516 struct isl_union_map_bin_data *data = user;
1517 isl_map *map2 = *entry;
1519 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1520 map2->dim, isl_dim_out))
1521 return isl_stat_ok;
1523 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1524 isl_map_copy(map2));
1526 data->res = isl_union_map_add_map(data->res, map2);
1528 return isl_stat_ok;
1531 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1533 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1534 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1536 return bin_op(umap1, umap2, &flat_domain_product_entry);
1539 static isl_stat flat_range_product_entry(void **entry, void *user)
1541 struct isl_union_map_bin_data *data = user;
1542 isl_map *map2 = *entry;
1544 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1545 map2->dim, isl_dim_in))
1546 return isl_stat_ok;
1548 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1549 isl_map_copy(map2));
1551 data->res = isl_union_map_add_map(data->res, map2);
1553 return isl_stat_ok;
1556 __isl_give isl_union_map *isl_union_map_flat_range_product(
1557 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1559 return bin_op(umap1, umap2, &flat_range_product_entry);
1562 /* Data structure that specifies how un_op should modify
1563 * the maps in the union map.
1565 * If "inplace" is set, then the maps in the input union map
1566 * are modified in place. This means that "fn_map" should not
1567 * change the meaning of the map or that the union map only
1568 * has a single reference.
1569 * If "total" is set, then all maps need to be modified and
1570 * the results need to live in the same space.
1571 * Otherwise, a new union map is constructed to store the results.
1572 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1573 * are taken into account. "filter_user" is passed as the second argument
1574 * to "filter". No filter can be set if "inplace" or
1575 * "total" is set.
1576 * "fn_map" specifies how the maps (selected by "filter")
1577 * should be transformed.
1579 struct isl_un_op_control {
1580 int inplace;
1581 int total;
1582 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1583 void *filter_user;
1584 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1587 /* Data structure for wrapping the data for un_op_filter_drop_user.
1588 * "filter" is the function that is being wrapped.
1590 struct isl_un_op_drop_user_data {
1591 isl_bool (*filter)(__isl_keep isl_map *map);
1594 /* Wrapper for isl_un_op_control filters that do not require
1595 * a second argument.
1596 * Simply call data->filter without the second argument.
1598 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1600 struct isl_un_op_drop_user_data *data = user;
1601 return data->filter(map);
1604 /* Internal data structure for "un_op".
1605 * "control" specifies how the maps in the union map should be modified.
1606 * "res" collects the results.
1608 struct isl_union_map_un_data {
1609 struct isl_un_op_control *control;
1610 isl_union_map *res;
1613 /* isl_hash_table_foreach callback for un_op.
1614 * Handle the map that "entry" points to.
1616 * If control->filter is set, then check if this map satisfies the filter.
1617 * If so (or if control->filter is not set), modify the map
1618 * by calling control->fn_map and either add the result to data->res or
1619 * replace the original entry by the result (if control->inplace is set).
1621 static isl_stat un_entry(void **entry, void *user)
1623 struct isl_union_map_un_data *data = user;
1624 isl_map *map = *entry;
1626 if (data->control->filter) {
1627 isl_bool ok;
1629 ok = data->control->filter(map, data->control->filter_user);
1630 if (ok < 0)
1631 return isl_stat_error;
1632 if (!ok)
1633 return isl_stat_ok;
1636 map = data->control->fn_map(isl_map_copy(map));
1637 if (!map)
1638 return isl_stat_error;
1639 if (data->control->inplace) {
1640 isl_map_free(*entry);
1641 *entry = map;
1642 } else {
1643 data->res = isl_union_map_add_map(data->res, map);
1644 if (!data->res)
1645 return isl_stat_error;
1648 return isl_stat_ok;
1651 /* Modify the maps in "umap" based on "control".
1652 * If control->inplace is set, then modify the maps in "umap" in-place.
1653 * Otherwise, create a new union map to hold the results.
1654 * If control->total is set, then perform an inplace computation
1655 * if "umap" is only referenced once. Otherwise, create a new union map
1656 * to store the results.
1658 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1659 struct isl_un_op_control *control)
1661 struct isl_union_map_un_data data = { control };
1663 if (!umap)
1664 return NULL;
1665 if ((control->inplace || control->total) && control->filter)
1666 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1667 "inplace/total modification cannot be filtered",
1668 return isl_union_map_free(umap));
1670 if (control->total && umap->ref == 1)
1671 control->inplace = 1;
1672 if (control->inplace) {
1673 data.res = umap;
1674 } else {
1675 isl_space *space;
1677 space = isl_union_map_get_space(umap);
1678 data.res = isl_union_map_alloc(space, umap->table.n);
1680 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1681 &umap->table, &un_entry, &data) < 0)
1682 data.res = isl_union_map_free(data.res);
1684 if (control->inplace)
1685 return data.res;
1686 isl_union_map_free(umap);
1687 return data.res;
1690 __isl_give isl_union_map *isl_union_map_from_range(
1691 __isl_take isl_union_set *uset)
1693 struct isl_un_op_control control = {
1694 .fn_map = &isl_map_from_range,
1696 return un_op(uset, &control);
1699 __isl_give isl_union_map *isl_union_map_from_domain(
1700 __isl_take isl_union_set *uset)
1702 return isl_union_map_reverse(isl_union_map_from_range(uset));
1705 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1706 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1708 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1709 isl_union_map_from_range(range));
1712 /* Modify the maps in "umap" by applying "fn" on them.
1713 * "fn" should apply to all maps in "umap" and should not modify the space.
1715 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1716 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1718 struct isl_un_op_control control = {
1719 .total = 1,
1720 .fn_map = fn,
1723 return un_op(umap, &control);
1726 /* Compute the affine hull of "map" and return the result as an isl_map.
1728 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1730 return isl_map_from_basic_map(isl_map_affine_hull(map));
1733 __isl_give isl_union_map *isl_union_map_affine_hull(
1734 __isl_take isl_union_map *umap)
1736 return total(umap, &isl_map_affine_hull_map);
1739 __isl_give isl_union_set *isl_union_set_affine_hull(
1740 __isl_take isl_union_set *uset)
1742 return isl_union_map_affine_hull(uset);
1745 /* Wrapper around isl_set_combined_lineality_space
1746 * that returns the combined lineality space in the form of an isl_set
1747 * instead of an isl_basic_set.
1749 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1751 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1754 /* For each set in "uset", compute the (linear) hull
1755 * of the lineality spaces of its basic sets and
1756 * collect and return the results.
1758 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1759 __isl_take isl_union_set *uset)
1761 struct isl_un_op_control control = {
1762 .fn_map = &combined_lineality_space,
1764 return un_op(uset, &control);
1767 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1769 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1771 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1774 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1775 __isl_take isl_union_map *umap)
1777 return total(umap, &isl_map_polyhedral_hull_map);
1780 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1781 __isl_take isl_union_set *uset)
1783 return isl_union_map_polyhedral_hull(uset);
1786 /* Compute a superset of the convex hull of "map" that is described
1787 * by only translates of the constraints in the constituents of "map" and
1788 * return the result as an isl_map.
1790 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1792 return isl_map_from_basic_map(isl_map_simple_hull(map));
1795 __isl_give isl_union_map *isl_union_map_simple_hull(
1796 __isl_take isl_union_map *umap)
1798 return total(umap, &isl_map_simple_hull_map);
1801 __isl_give isl_union_set *isl_union_set_simple_hull(
1802 __isl_take isl_union_set *uset)
1804 return isl_union_map_simple_hull(uset);
1807 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1808 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1810 struct isl_un_op_control control = {
1811 .inplace = 1,
1812 .fn_map = fn,
1815 return un_op(umap, &control);
1818 /* Remove redundant constraints in each of the basic maps of "umap".
1819 * Since removing redundant constraints does not change the meaning
1820 * or the space, the operation can be performed in-place.
1822 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1823 __isl_take isl_union_map *umap)
1825 return inplace(umap, &isl_map_remove_redundancies);
1828 /* Remove redundant constraints in each of the basic sets of "uset".
1830 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1831 __isl_take isl_union_set *uset)
1833 return isl_union_map_remove_redundancies(uset);
1836 __isl_give isl_union_map *isl_union_map_coalesce(
1837 __isl_take isl_union_map *umap)
1839 return inplace(umap, &isl_map_coalesce);
1842 __isl_give isl_union_set *isl_union_set_coalesce(
1843 __isl_take isl_union_set *uset)
1845 return isl_union_map_coalesce(uset);
1848 __isl_give isl_union_map *isl_union_map_detect_equalities(
1849 __isl_take isl_union_map *umap)
1851 return inplace(umap, &isl_map_detect_equalities);
1854 __isl_give isl_union_set *isl_union_set_detect_equalities(
1855 __isl_take isl_union_set *uset)
1857 return isl_union_map_detect_equalities(uset);
1860 __isl_give isl_union_map *isl_union_map_compute_divs(
1861 __isl_take isl_union_map *umap)
1863 return inplace(umap, &isl_map_compute_divs);
1866 __isl_give isl_union_set *isl_union_set_compute_divs(
1867 __isl_take isl_union_set *uset)
1869 return isl_union_map_compute_divs(uset);
1872 __isl_give isl_union_map *isl_union_map_lexmin(
1873 __isl_take isl_union_map *umap)
1875 return total(umap, &isl_map_lexmin);
1878 __isl_give isl_union_set *isl_union_set_lexmin(
1879 __isl_take isl_union_set *uset)
1881 return isl_union_map_lexmin(uset);
1884 __isl_give isl_union_map *isl_union_map_lexmax(
1885 __isl_take isl_union_map *umap)
1887 return total(umap, &isl_map_lexmax);
1890 __isl_give isl_union_set *isl_union_set_lexmax(
1891 __isl_take isl_union_set *uset)
1893 return isl_union_map_lexmax(uset);
1896 /* Return the universe in the space of "map".
1898 static __isl_give isl_map *universe(__isl_take isl_map *map)
1900 isl_space *space;
1902 space = isl_map_get_space(map);
1903 isl_map_free(map);
1904 return isl_map_universe(space);
1907 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1909 struct isl_un_op_control control = {
1910 .fn_map = &universe,
1912 return un_op(umap, &control);
1915 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1917 return isl_union_map_universe(uset);
1920 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1922 struct isl_un_op_control control = {
1923 .fn_map = &isl_map_reverse,
1925 return un_op(umap, &control);
1928 /* Compute the parameter domain of the given union map.
1930 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1932 struct isl_un_op_control control = {
1933 .fn_map = &isl_map_params,
1935 int empty;
1937 empty = isl_union_map_is_empty(umap);
1938 if (empty < 0)
1939 goto error;
1940 if (empty) {
1941 isl_space *space;
1942 space = isl_union_map_get_space(umap);
1943 isl_union_map_free(umap);
1944 return isl_set_empty(space);
1946 return isl_set_from_union_set(un_op(umap, &control));
1947 error:
1948 isl_union_map_free(umap);
1949 return NULL;
1952 /* Compute the parameter domain of the given union set.
1954 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1956 return isl_union_map_params(uset);
1959 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1961 struct isl_un_op_control control = {
1962 .fn_map = &isl_map_domain,
1964 return un_op(umap, &control);
1967 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1969 struct isl_un_op_control control = {
1970 .fn_map = &isl_map_range,
1972 return un_op(umap, &control);
1975 __isl_give isl_union_map *isl_union_map_domain_map(
1976 __isl_take isl_union_map *umap)
1978 struct isl_un_op_control control = {
1979 .fn_map = &isl_map_domain_map,
1981 return un_op(umap, &control);
1984 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
1985 * add the result to "res".
1987 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1989 isl_union_pw_multi_aff **res = user;
1990 isl_multi_aff *ma;
1991 isl_pw_multi_aff *pma;
1993 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1994 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1995 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1997 return *res ? isl_stat_ok : isl_stat_error;
2001 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2002 * to its domain.
2004 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2005 __isl_take isl_union_map *umap)
2007 isl_union_pw_multi_aff *res;
2009 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2010 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2011 res = isl_union_pw_multi_aff_free(res);
2013 isl_union_map_free(umap);
2014 return res;
2017 __isl_give isl_union_map *isl_union_map_range_map(
2018 __isl_take isl_union_map *umap)
2020 struct isl_un_op_control control = {
2021 .fn_map = &isl_map_range_map,
2023 return un_op(umap, &control);
2026 /* Given a collection of wrapped maps of the form A[B -> C],
2027 * return the collection of maps A[B -> C] -> B.
2029 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2030 __isl_take isl_union_set *uset)
2032 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2033 struct isl_un_op_control control = {
2034 .filter = &un_op_filter_drop_user,
2035 .filter_user = &data,
2036 .fn_map = &isl_set_wrapped_domain_map,
2038 return un_op(uset, &control);
2041 /* Does "map" relate elements from the same space?
2043 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2045 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2046 map->dim, isl_dim_out);
2049 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2051 struct isl_un_op_control control = {
2052 .filter = &equal_tuples,
2053 .fn_map = &isl_map_deltas,
2055 return un_op(umap, &control);
2058 __isl_give isl_union_map *isl_union_map_deltas_map(
2059 __isl_take isl_union_map *umap)
2061 struct isl_un_op_control control = {
2062 .filter = &equal_tuples,
2063 .fn_map = &isl_map_deltas_map,
2065 return un_op(umap, &control);
2068 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2070 struct isl_un_op_control control = {
2071 .fn_map = &isl_set_identity,
2073 return un_op(uset, &control);
2076 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2078 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2080 isl_union_pw_multi_aff **res = user;
2081 isl_space *space;
2082 isl_pw_multi_aff *pma;
2084 space = isl_space_map_from_set(isl_set_get_space(set));
2085 pma = isl_pw_multi_aff_identity(space);
2086 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2087 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2089 return *res ? isl_stat_ok : isl_stat_error;
2092 /* Return an identity function on "uset" in the form
2093 * of an isl_union_pw_multi_aff.
2095 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2096 __isl_take isl_union_set *uset)
2098 isl_union_pw_multi_aff *res;
2100 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2101 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2102 res = isl_union_pw_multi_aff_free(res);
2104 isl_union_set_free(uset);
2105 return res;
2108 /* For each map in "umap" of the form [A -> B] -> C,
2109 * construct the map A -> C and collect the results.
2111 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2112 __isl_take isl_union_map *umap)
2114 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2115 struct isl_un_op_control control = {
2116 .filter = &un_op_filter_drop_user,
2117 .filter_user = &data,
2118 .fn_map = &isl_map_domain_factor_domain,
2120 return un_op(umap, &control);
2123 /* For each map in "umap" of the form [A -> B] -> C,
2124 * construct the map B -> C and collect the results.
2126 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2127 __isl_take isl_union_map *umap)
2129 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2130 struct isl_un_op_control control = {
2131 .filter = &un_op_filter_drop_user,
2132 .filter_user = &data,
2133 .fn_map = &isl_map_domain_factor_range,
2135 return un_op(umap, &control);
2138 /* For each map in "umap" of the form A -> [B -> C],
2139 * construct the map A -> B and collect the results.
2141 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2142 __isl_take isl_union_map *umap)
2144 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2145 struct isl_un_op_control control = {
2146 .filter = &un_op_filter_drop_user,
2147 .filter_user = &data,
2148 .fn_map = &isl_map_range_factor_domain,
2150 return un_op(umap, &control);
2153 /* For each map in "umap" of the form A -> [B -> C],
2154 * construct the map A -> C and collect the results.
2156 __isl_give isl_union_map *isl_union_map_range_factor_range(
2157 __isl_take isl_union_map *umap)
2159 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2160 struct isl_un_op_control control = {
2161 .filter = &un_op_filter_drop_user,
2162 .filter_user = &data,
2163 .fn_map = &isl_map_range_factor_range,
2165 return un_op(umap, &control);
2168 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2169 * construct the map A -> C and collect the results.
2171 __isl_give isl_union_map *isl_union_map_factor_domain(
2172 __isl_take isl_union_map *umap)
2174 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2175 struct isl_un_op_control control = {
2176 .filter = &un_op_filter_drop_user,
2177 .filter_user = &data,
2178 .fn_map = &isl_map_factor_domain,
2180 return un_op(umap, &control);
2183 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2184 * construct the map B -> D and collect the results.
2186 __isl_give isl_union_map *isl_union_map_factor_range(
2187 __isl_take isl_union_map *umap)
2189 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2190 struct isl_un_op_control control = {
2191 .filter = &un_op_filter_drop_user,
2192 .filter_user = &data,
2193 .fn_map = &isl_map_factor_range,
2195 return un_op(umap, &control);
2198 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2200 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2201 struct isl_un_op_control control = {
2202 .filter = &un_op_filter_drop_user,
2203 .filter_user = &data,
2204 .fn_map = &isl_set_unwrap,
2206 return un_op(uset, &control);
2209 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2211 struct isl_un_op_control control = {
2212 .fn_map = &isl_map_wrap,
2214 return un_op(umap, &control);
2217 struct isl_union_map_is_subset_data {
2218 isl_union_map *umap2;
2219 isl_bool is_subset;
2222 static isl_stat is_subset_entry(void **entry, void *user)
2224 struct isl_union_map_is_subset_data *data = user;
2225 uint32_t hash;
2226 struct isl_hash_table_entry *entry2;
2227 isl_map *map = *entry;
2229 hash = isl_space_get_hash(map->dim);
2230 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2231 hash, &has_space, map->dim, 0);
2232 if (!entry2) {
2233 int empty = isl_map_is_empty(map);
2234 if (empty < 0)
2235 return isl_stat_error;
2236 if (empty)
2237 return isl_stat_ok;
2238 data->is_subset = 0;
2239 return isl_stat_error;
2242 data->is_subset = isl_map_is_subset(map, entry2->data);
2243 if (data->is_subset < 0 || !data->is_subset)
2244 return isl_stat_error;
2246 return isl_stat_ok;
2249 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2250 __isl_keep isl_union_map *umap2)
2252 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2254 umap1 = isl_union_map_copy(umap1);
2255 umap2 = isl_union_map_copy(umap2);
2256 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2257 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2259 if (!umap1 || !umap2)
2260 goto error;
2262 data.umap2 = umap2;
2263 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2264 &is_subset_entry, &data) < 0 &&
2265 data.is_subset)
2266 goto error;
2268 isl_union_map_free(umap1);
2269 isl_union_map_free(umap2);
2271 return data.is_subset;
2272 error:
2273 isl_union_map_free(umap1);
2274 isl_union_map_free(umap2);
2275 return isl_bool_error;
2278 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2279 __isl_keep isl_union_set *uset2)
2281 return isl_union_map_is_subset(uset1, uset2);
2284 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2285 __isl_keep isl_union_map *umap2)
2287 isl_bool is_subset;
2289 if (!umap1 || !umap2)
2290 return isl_bool_error;
2291 is_subset = isl_union_map_is_subset(umap1, umap2);
2292 if (is_subset != isl_bool_true)
2293 return is_subset;
2294 is_subset = isl_union_map_is_subset(umap2, umap1);
2295 return is_subset;
2298 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2299 __isl_keep isl_union_set *uset2)
2301 return isl_union_map_is_equal(uset1, uset2);
2304 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2305 __isl_keep isl_union_map *umap2)
2307 isl_bool is_subset;
2309 if (!umap1 || !umap2)
2310 return isl_bool_error;
2311 is_subset = isl_union_map_is_subset(umap1, umap2);
2312 if (is_subset != isl_bool_true)
2313 return is_subset;
2314 is_subset = isl_union_map_is_subset(umap2, umap1);
2315 if (is_subset == isl_bool_error)
2316 return is_subset;
2317 return !is_subset;
2320 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2321 __isl_keep isl_union_set *uset2)
2323 return isl_union_map_is_strict_subset(uset1, uset2);
2326 /* Internal data structure for isl_union_map_is_disjoint.
2327 * umap2 is the union map with which we are comparing.
2328 * is_disjoint is initialized to 1 and is set to 0 as soon
2329 * as the union maps turn out not to be disjoint.
2331 struct isl_union_map_is_disjoint_data {
2332 isl_union_map *umap2;
2333 isl_bool is_disjoint;
2336 /* Check if "map" is disjoint from data->umap2 and abort
2337 * the search if it is not.
2339 static isl_stat is_disjoint_entry(void **entry, void *user)
2341 struct isl_union_map_is_disjoint_data *data = user;
2342 uint32_t hash;
2343 struct isl_hash_table_entry *entry2;
2344 isl_map *map = *entry;
2346 hash = isl_space_get_hash(map->dim);
2347 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2348 hash, &has_space, map->dim, 0);
2349 if (!entry2)
2350 return isl_stat_ok;
2352 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2353 if (data->is_disjoint < 0 || !data->is_disjoint)
2354 return isl_stat_error;
2356 return isl_stat_ok;
2359 /* Are "umap1" and "umap2" disjoint?
2361 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2362 __isl_keep isl_union_map *umap2)
2364 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2366 umap1 = isl_union_map_copy(umap1);
2367 umap2 = isl_union_map_copy(umap2);
2368 umap1 = isl_union_map_align_params(umap1,
2369 isl_union_map_get_space(umap2));
2370 umap2 = isl_union_map_align_params(umap2,
2371 isl_union_map_get_space(umap1));
2373 if (!umap1 || !umap2)
2374 goto error;
2376 data.umap2 = umap2;
2377 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2378 &is_disjoint_entry, &data) < 0 &&
2379 data.is_disjoint)
2380 goto error;
2382 isl_union_map_free(umap1);
2383 isl_union_map_free(umap2);
2385 return data.is_disjoint;
2386 error:
2387 isl_union_map_free(umap1);
2388 isl_union_map_free(umap2);
2389 return isl_bool_error;
2392 /* Are "uset1" and "uset2" disjoint?
2394 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2395 __isl_keep isl_union_set *uset2)
2397 return isl_union_map_is_disjoint(uset1, uset2);
2400 static isl_stat sample_entry(void **entry, void *user)
2402 isl_basic_map **sample = (isl_basic_map **)user;
2403 isl_map *map = *entry;
2405 *sample = isl_map_sample(isl_map_copy(map));
2406 if (!*sample)
2407 return isl_stat_error;
2408 if (!isl_basic_map_plain_is_empty(*sample))
2409 return isl_stat_error;
2410 return isl_stat_ok;
2413 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2415 isl_basic_map *sample = NULL;
2417 if (!umap)
2418 return NULL;
2420 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2421 &sample_entry, &sample) < 0 &&
2422 !sample)
2423 goto error;
2425 if (!sample)
2426 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2428 isl_union_map_free(umap);
2430 return sample;
2431 error:
2432 isl_union_map_free(umap);
2433 return NULL;
2436 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2438 return bset_from_bmap(isl_union_map_sample(uset));
2441 /* Return an element in "uset" in the form of an isl_point.
2442 * Return a void isl_point if "uset" is empty.
2444 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2446 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2449 struct isl_forall_data {
2450 isl_bool res;
2451 isl_bool (*fn)(__isl_keep isl_map *map);
2454 static isl_stat forall_entry(void **entry, void *user)
2456 struct isl_forall_data *data = user;
2457 isl_map *map = *entry;
2459 data->res = data->fn(map);
2460 if (data->res < 0)
2461 return isl_stat_error;
2463 if (!data->res)
2464 return isl_stat_error;
2466 return isl_stat_ok;
2469 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2470 isl_bool (*fn)(__isl_keep isl_map *map))
2472 struct isl_forall_data data = { isl_bool_true, fn };
2474 if (!umap)
2475 return isl_bool_error;
2477 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2478 &forall_entry, &data) < 0 && data.res)
2479 return isl_bool_error;
2481 return data.res;
2484 struct isl_forall_user_data {
2485 isl_bool res;
2486 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2487 void *user;
2490 static isl_stat forall_user_entry(void **entry, void *user)
2492 struct isl_forall_user_data *data = user;
2493 isl_map *map = *entry;
2495 data->res = data->fn(map, data->user);
2496 if (data->res < 0)
2497 return isl_stat_error;
2499 if (!data->res)
2500 return isl_stat_error;
2502 return isl_stat_ok;
2505 /* Check if fn(map, user) returns true for all maps "map" in umap.
2507 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2508 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2510 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2512 if (!umap)
2513 return isl_bool_error;
2515 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2516 &forall_user_entry, &data) < 0 && data.res)
2517 return isl_bool_error;
2519 return data.res;
2522 /* Is "umap" obviously empty?
2524 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2526 if (!umap)
2527 return isl_bool_error;
2528 return isl_union_map_n_map(umap) == 0;
2531 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2533 return union_map_forall(umap, &isl_map_is_empty);
2536 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2538 return isl_union_map_is_empty(uset);
2541 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2543 isl_bool is_subset;
2544 isl_space *dim;
2545 isl_map *id;
2547 if (!map)
2548 return isl_bool_error;
2550 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2551 map->dim, isl_dim_out))
2552 return isl_bool_false;
2554 dim = isl_map_get_space(map);
2555 id = isl_map_identity(dim);
2557 is_subset = isl_map_is_subset(map, id);
2559 isl_map_free(id);
2561 return is_subset;
2564 /* Given an isl_union_map that consists of a single map, check
2565 * if it is single-valued.
2567 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2569 isl_map *map;
2570 isl_bool sv;
2572 umap = isl_union_map_copy(umap);
2573 map = isl_map_from_union_map(umap);
2574 sv = isl_map_is_single_valued(map);
2575 isl_map_free(map);
2577 return sv;
2580 /* Internal data structure for single_valued_on_domain.
2582 * "umap" is the union map to be tested.
2583 * "sv" is set to 1 as long as "umap" may still be single-valued.
2585 struct isl_union_map_is_sv_data {
2586 isl_union_map *umap;
2587 isl_bool sv;
2590 /* Check if the data->umap is single-valued on "set".
2592 * If data->umap consists of a single map on "set", then test it
2593 * as an isl_map.
2595 * Otherwise, compute
2597 * M \circ M^-1
2599 * check if the result is a subset of the identity mapping and
2600 * store the result in data->sv.
2602 * Terminate as soon as data->umap has been determined not to
2603 * be single-valued.
2605 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2607 struct isl_union_map_is_sv_data *data = user;
2608 isl_union_map *umap, *test;
2610 umap = isl_union_map_copy(data->umap);
2611 umap = isl_union_map_intersect_domain(umap,
2612 isl_union_set_from_set(set));
2614 if (isl_union_map_n_map(umap) == 1) {
2615 data->sv = single_map_is_single_valued(umap);
2616 isl_union_map_free(umap);
2617 } else {
2618 test = isl_union_map_reverse(isl_union_map_copy(umap));
2619 test = isl_union_map_apply_range(test, umap);
2621 data->sv = union_map_forall(test, &is_subset_of_identity);
2623 isl_union_map_free(test);
2626 if (data->sv < 0 || !data->sv)
2627 return isl_stat_error;
2628 return isl_stat_ok;
2631 /* Check if the given map is single-valued.
2633 * If the union map consists of a single map, then test it as an isl_map.
2634 * Otherwise, check if the union map is single-valued on each of its
2635 * domain spaces.
2637 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2639 isl_union_map *universe;
2640 isl_union_set *domain;
2641 struct isl_union_map_is_sv_data data;
2643 if (isl_union_map_n_map(umap) == 1)
2644 return single_map_is_single_valued(umap);
2646 universe = isl_union_map_universe(isl_union_map_copy(umap));
2647 domain = isl_union_map_domain(universe);
2649 data.sv = isl_bool_true;
2650 data.umap = umap;
2651 if (isl_union_set_foreach_set(domain,
2652 &single_valued_on_domain, &data) < 0 && data.sv)
2653 data.sv = isl_bool_error;
2654 isl_union_set_free(domain);
2656 return data.sv;
2659 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2661 isl_bool in;
2663 umap = isl_union_map_copy(umap);
2664 umap = isl_union_map_reverse(umap);
2665 in = isl_union_map_is_single_valued(umap);
2666 isl_union_map_free(umap);
2668 return in;
2671 /* Is "map" obviously not an identity relation because
2672 * it maps elements from one space to another space?
2673 * Update *non_identity accordingly.
2675 * In particular, if the domain and range spaces are the same,
2676 * then the map is not considered to obviously not be an identity relation.
2677 * Otherwise, the map is considered to obviously not be an identity relation
2678 * if it is is non-empty.
2680 * If "map" is determined to obviously not be an identity relation,
2681 * then the search is aborted.
2683 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2685 isl_bool *non_identity = user;
2686 isl_bool equal;
2687 isl_space *space;
2689 space = isl_map_get_space(map);
2690 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2691 if (equal >= 0 && !equal)
2692 *non_identity = isl_bool_not(isl_map_is_empty(map));
2693 else
2694 *non_identity = isl_bool_not(equal);
2695 isl_space_free(space);
2696 isl_map_free(map);
2698 if (*non_identity < 0 || *non_identity)
2699 return isl_stat_error;
2701 return isl_stat_ok;
2704 /* Is "umap" obviously not an identity relation because
2705 * it maps elements from one space to another space?
2707 * As soon as a map has been found that maps elements to a different space,
2708 * non_identity is changed and the search is aborted.
2710 static isl_bool isl_union_map_plain_is_not_identity(
2711 __isl_keep isl_union_map *umap)
2713 isl_bool non_identity;
2715 non_identity = isl_bool_false;
2716 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2717 &non_identity) < 0 &&
2718 non_identity == isl_bool_false)
2719 return isl_bool_error;
2721 return non_identity;
2724 /* Does "map" only map elements to themselves?
2725 * Update *identity accordingly.
2727 * If "map" is determined not to be an identity relation,
2728 * then the search is aborted.
2730 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2732 isl_bool *identity = user;
2734 *identity = isl_map_is_identity(map);
2735 isl_map_free(map);
2737 if (*identity < 0 || !*identity)
2738 return isl_stat_error;
2740 return isl_stat_ok;
2743 /* Does "umap" only map elements to themselves?
2745 * First check if there are any maps that map elements to different spaces.
2746 * If not, then check that all the maps (between identical spaces)
2747 * are identity relations.
2749 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2751 isl_bool non_identity;
2752 isl_bool identity;
2754 non_identity = isl_union_map_plain_is_not_identity(umap);
2755 if (non_identity < 0 || non_identity)
2756 return isl_bool_not(non_identity);
2758 identity = isl_bool_true;
2759 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2760 identity == isl_bool_true)
2761 return isl_bool_error;
2763 return identity;
2766 /* Represents a map that has a fixed value (v) for one of its
2767 * range dimensions.
2768 * The map in this structure is not reference counted, so it
2769 * is only valid while the isl_union_map from which it was
2770 * obtained is still alive.
2772 struct isl_fixed_map {
2773 isl_int v;
2774 isl_map *map;
2777 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2778 int n)
2780 int i;
2781 struct isl_fixed_map *v;
2783 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2784 if (!v)
2785 return NULL;
2786 for (i = 0; i < n; ++i)
2787 isl_int_init(v[i].v);
2788 return v;
2791 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2793 int i;
2795 if (!v)
2796 return;
2797 for (i = 0; i < n; ++i)
2798 isl_int_clear(v[i].v);
2799 free(v);
2802 /* Compare the "v" field of two isl_fixed_map structs.
2804 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2806 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2807 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2809 return isl_int_cmp(e1->v, e2->v);
2812 /* Internal data structure used while checking whether all maps
2813 * in a union_map have a fixed value for a given output dimension.
2814 * v is the list of maps, with the fixed value for the dimension
2815 * n is the number of maps considered so far
2816 * pos is the output dimension under investigation
2818 struct isl_fixed_dim_data {
2819 struct isl_fixed_map *v;
2820 int n;
2821 int pos;
2824 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2826 struct isl_fixed_dim_data *data = user;
2828 data->v[data->n].map = map;
2829 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2830 &data->v[data->n++].v);
2833 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2834 int first, int n_range);
2836 /* Given a list of the maps, with their fixed values at output dimension "pos",
2837 * check whether the ranges of the maps form an obvious partition.
2839 * We first sort the maps according to their fixed values.
2840 * If all maps have a different value, then we know the ranges form
2841 * a partition.
2842 * Otherwise, we collect the maps with the same fixed value and
2843 * check whether each such collection is obviously injective
2844 * based on later dimensions.
2846 static int separates(struct isl_fixed_map *v, int n,
2847 __isl_take isl_space *dim, int pos, int n_range)
2849 int i;
2851 if (!v)
2852 goto error;
2854 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2856 for (i = 0; i + 1 < n; ++i) {
2857 int j, k;
2858 isl_union_map *part;
2859 int injective;
2861 for (j = i + 1; j < n; ++j)
2862 if (isl_int_ne(v[i].v, v[j].v))
2863 break;
2865 if (j == i + 1)
2866 continue;
2868 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2869 for (k = i; k < j; ++k)
2870 part = isl_union_map_add_map(part,
2871 isl_map_copy(v[k].map));
2873 injective = plain_injective_on_range(part, pos + 1, n_range);
2874 if (injective < 0)
2875 goto error;
2876 if (!injective)
2877 break;
2879 i = j - 1;
2882 isl_space_free(dim);
2883 free_isl_fixed_map_array(v, n);
2884 return i + 1 >= n;
2885 error:
2886 isl_space_free(dim);
2887 free_isl_fixed_map_array(v, n);
2888 return -1;
2891 /* Check whether the maps in umap have obviously distinct ranges.
2892 * In particular, check for an output dimension in the range
2893 * [first,n_range) for which all maps have a fixed value
2894 * and then check if these values, possibly along with fixed values
2895 * at later dimensions, entail distinct ranges.
2897 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2898 int first, int n_range)
2900 isl_ctx *ctx;
2901 int n;
2902 struct isl_fixed_dim_data data = { NULL };
2904 ctx = isl_union_map_get_ctx(umap);
2906 n = isl_union_map_n_map(umap);
2907 if (!umap)
2908 goto error;
2910 if (n <= 1) {
2911 isl_union_map_free(umap);
2912 return isl_bool_true;
2915 if (first >= n_range) {
2916 isl_union_map_free(umap);
2917 return isl_bool_false;
2920 data.v = alloc_isl_fixed_map_array(ctx, n);
2921 if (!data.v)
2922 goto error;
2924 for (data.pos = first; data.pos < n_range; ++data.pos) {
2925 isl_bool fixed;
2926 int injective;
2927 isl_space *dim;
2929 data.n = 0;
2930 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2931 if (fixed < 0)
2932 goto error;
2933 if (!fixed)
2934 continue;
2935 dim = isl_union_map_get_space(umap);
2936 injective = separates(data.v, n, dim, data.pos, n_range);
2937 isl_union_map_free(umap);
2938 return injective;
2941 free_isl_fixed_map_array(data.v, n);
2942 isl_union_map_free(umap);
2944 return isl_bool_false;
2945 error:
2946 free_isl_fixed_map_array(data.v, n);
2947 isl_union_map_free(umap);
2948 return isl_bool_error;
2951 /* Check whether the maps in umap that map to subsets of "ran"
2952 * have obviously distinct ranges.
2954 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2955 void *user)
2957 isl_union_map *umap = user;
2959 umap = isl_union_map_copy(umap);
2960 umap = isl_union_map_intersect_range(umap,
2961 isl_union_set_from_set(isl_set_copy(ran)));
2962 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2965 /* Check if the given union_map is obviously injective.
2967 * In particular, we first check if all individual maps are obviously
2968 * injective and then check if all the ranges of these maps are
2969 * obviously disjoint.
2971 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2973 isl_bool in;
2974 isl_union_map *univ;
2975 isl_union_set *ran;
2977 in = union_map_forall(umap, &isl_map_plain_is_injective);
2978 if (in < 0)
2979 return isl_bool_error;
2980 if (!in)
2981 return isl_bool_false;
2983 univ = isl_union_map_universe(isl_union_map_copy(umap));
2984 ran = isl_union_map_range(univ);
2986 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2988 isl_union_set_free(ran);
2990 return in;
2993 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2995 isl_bool sv;
2997 sv = isl_union_map_is_single_valued(umap);
2998 if (sv < 0 || !sv)
2999 return sv;
3001 return isl_union_map_is_injective(umap);
3004 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3006 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3007 struct isl_un_op_control control = {
3008 .filter = &un_op_filter_drop_user,
3009 .filter_user = &data,
3010 .fn_map = &isl_map_zip,
3012 return un_op(umap, &control);
3015 /* Given a union map, take the maps of the form A -> (B -> C) and
3016 * return the union of the corresponding maps (A -> B) -> C.
3018 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3020 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3021 struct isl_un_op_control control = {
3022 .filter = &un_op_filter_drop_user,
3023 .filter_user = &data,
3024 .fn_map = &isl_map_uncurry,
3026 return un_op(umap, &control);
3029 /* Given a union map, take the maps of the form (A -> B) -> C and
3030 * return the union of the corresponding maps A -> (B -> C).
3032 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3034 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3035 struct isl_un_op_control control = {
3036 .filter = &un_op_filter_drop_user,
3037 .filter_user = &data,
3038 .fn_map = &isl_map_curry,
3040 return un_op(umap, &control);
3043 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3044 * return the union of the corresponding maps A -> (B -> (C -> D)).
3046 __isl_give isl_union_map *isl_union_map_range_curry(
3047 __isl_take isl_union_map *umap)
3049 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3050 struct isl_un_op_control control = {
3051 .filter = &un_op_filter_drop_user,
3052 .filter_user = &data,
3053 .fn_map = &isl_map_range_curry,
3055 return un_op(umap, &control);
3058 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3060 struct isl_un_op_control control = {
3061 .fn_map = &isl_set_lift,
3063 return un_op(uset, &control);
3066 static isl_stat coefficients_entry(void **entry, void *user)
3068 isl_set *set = *entry;
3069 isl_union_set **res = user;
3071 set = isl_set_copy(set);
3072 set = isl_set_from_basic_set(isl_set_coefficients(set));
3073 *res = isl_union_set_add_set(*res, set);
3075 return isl_stat_ok;
3078 __isl_give isl_union_set *isl_union_set_coefficients(
3079 __isl_take isl_union_set *uset)
3081 isl_ctx *ctx;
3082 isl_space *dim;
3083 isl_union_set *res;
3085 if (!uset)
3086 return NULL;
3088 ctx = isl_union_set_get_ctx(uset);
3089 dim = isl_space_set_alloc(ctx, 0, 0);
3090 res = isl_union_map_alloc(dim, uset->table.n);
3091 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3092 &coefficients_entry, &res) < 0)
3093 goto error;
3095 isl_union_set_free(uset);
3096 return res;
3097 error:
3098 isl_union_set_free(uset);
3099 isl_union_set_free(res);
3100 return NULL;
3103 static isl_stat solutions_entry(void **entry, void *user)
3105 isl_set *set = *entry;
3106 isl_union_set **res = user;
3108 set = isl_set_copy(set);
3109 set = isl_set_from_basic_set(isl_set_solutions(set));
3110 if (!*res)
3111 *res = isl_union_set_from_set(set);
3112 else
3113 *res = isl_union_set_add_set(*res, set);
3115 if (!*res)
3116 return isl_stat_error;
3118 return isl_stat_ok;
3121 __isl_give isl_union_set *isl_union_set_solutions(
3122 __isl_take isl_union_set *uset)
3124 isl_union_set *res = NULL;
3126 if (!uset)
3127 return NULL;
3129 if (uset->table.n == 0) {
3130 res = isl_union_set_empty(isl_union_set_get_space(uset));
3131 isl_union_set_free(uset);
3132 return res;
3135 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3136 &solutions_entry, &res) < 0)
3137 goto error;
3139 isl_union_set_free(uset);
3140 return res;
3141 error:
3142 isl_union_set_free(uset);
3143 isl_union_set_free(res);
3144 return NULL;
3147 /* Is the domain space of "map" equal to "space"?
3149 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3151 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3152 space, isl_dim_out);
3155 /* Is the range space of "map" equal to "space"?
3157 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3159 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3160 space, isl_dim_out);
3163 /* Is the set space of "map" equal to "space"?
3165 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3167 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3168 space, isl_dim_out);
3171 /* Internal data structure for preimage_pw_multi_aff.
3173 * "pma" is the function under which the preimage should be taken.
3174 * "space" is the space of "pma".
3175 * "res" collects the results.
3176 * "fn" computes the preimage for a given map.
3177 * "match" returns true if "fn" can be called.
3179 struct isl_union_map_preimage_data {
3180 isl_space *space;
3181 isl_pw_multi_aff *pma;
3182 isl_union_map *res;
3183 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3184 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3185 __isl_take isl_pw_multi_aff *pma);
3188 /* Call data->fn to compute the preimage of the domain or range of *entry
3189 * under the function represented by data->pma, provided the domain/range
3190 * space of *entry matches the target space of data->pma
3191 * (as given by data->match), and add the result to data->res.
3193 static isl_stat preimage_entry(void **entry, void *user)
3195 int m;
3196 isl_map *map = *entry;
3197 struct isl_union_map_preimage_data *data = user;
3198 isl_bool empty;
3200 m = data->match(map, data->space);
3201 if (m < 0)
3202 return isl_stat_error;
3203 if (!m)
3204 return isl_stat_ok;
3206 map = isl_map_copy(map);
3207 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3209 empty = isl_map_is_empty(map);
3210 if (empty < 0 || empty) {
3211 isl_map_free(map);
3212 return empty < 0 ? isl_stat_error : isl_stat_ok;
3215 data->res = isl_union_map_add_map(data->res, map);
3217 return isl_stat_ok;
3220 /* Compute the preimage of the domain or range of "umap" under the function
3221 * represented by "pma".
3222 * In other words, plug in "pma" in the domain or range of "umap".
3223 * The function "fn" performs the actual preimage computation on a map,
3224 * while "match" determines to which maps the function should be applied.
3226 static __isl_give isl_union_map *preimage_pw_multi_aff(
3227 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3228 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3229 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3230 __isl_take isl_pw_multi_aff *pma))
3232 isl_ctx *ctx;
3233 isl_space *space;
3234 struct isl_union_map_preimage_data data;
3236 umap = isl_union_map_align_params(umap,
3237 isl_pw_multi_aff_get_space(pma));
3238 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3240 if (!umap || !pma)
3241 goto error;
3243 ctx = isl_union_map_get_ctx(umap);
3244 space = isl_union_map_get_space(umap);
3245 data.space = isl_pw_multi_aff_get_space(pma);
3246 data.pma = pma;
3247 data.res = isl_union_map_alloc(space, umap->table.n);
3248 data.match = match;
3249 data.fn = fn;
3250 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3251 &data) < 0)
3252 data.res = isl_union_map_free(data.res);
3254 isl_space_free(data.space);
3255 isl_union_map_free(umap);
3256 isl_pw_multi_aff_free(pma);
3257 return data.res;
3258 error:
3259 isl_union_map_free(umap);
3260 isl_pw_multi_aff_free(pma);
3261 return NULL;
3264 /* Compute the preimage of the domain of "umap" under the function
3265 * represented by "pma".
3266 * In other words, plug in "pma" in the domain of "umap".
3267 * The result contains maps that live in the same spaces as the maps of "umap"
3268 * with domain space equal to the target space of "pma",
3269 * except that the domain has been replaced by the domain space of "pma".
3271 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3272 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3274 return preimage_pw_multi_aff(umap, pma, &domain_match,
3275 &isl_map_preimage_domain_pw_multi_aff);
3278 /* Compute the preimage of the range of "umap" under the function
3279 * represented by "pma".
3280 * In other words, plug in "pma" in the range of "umap".
3281 * The result contains maps that live in the same spaces as the maps of "umap"
3282 * with range space equal to the target space of "pma",
3283 * except that the range has been replaced by the domain space of "pma".
3285 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3286 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3288 return preimage_pw_multi_aff(umap, pma, &range_match,
3289 &isl_map_preimage_range_pw_multi_aff);
3292 /* Compute the preimage of "uset" under the function represented by "pma".
3293 * In other words, plug in "pma" in "uset".
3294 * The result contains sets that live in the same spaces as the sets of "uset"
3295 * with space equal to the target space of "pma",
3296 * except that the space has been replaced by the domain space of "pma".
3298 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3299 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3301 return preimage_pw_multi_aff(uset, pma, &set_match,
3302 &isl_set_preimage_pw_multi_aff);
3305 /* Compute the preimage of the domain of "umap" under the function
3306 * represented by "ma".
3307 * In other words, plug in "ma" in the domain of "umap".
3308 * The result contains maps that live in the same spaces as the maps of "umap"
3309 * with domain space equal to the target space of "ma",
3310 * except that the domain has been replaced by the domain space of "ma".
3312 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3313 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3315 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3316 isl_pw_multi_aff_from_multi_aff(ma));
3319 /* Compute the preimage of the range of "umap" under the function
3320 * represented by "ma".
3321 * In other words, plug in "ma" in the range of "umap".
3322 * The result contains maps that live in the same spaces as the maps of "umap"
3323 * with range space equal to the target space of "ma",
3324 * except that the range has been replaced by the domain space of "ma".
3326 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3327 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3329 return isl_union_map_preimage_range_pw_multi_aff(umap,
3330 isl_pw_multi_aff_from_multi_aff(ma));
3333 /* Compute the preimage of "uset" under the function represented by "ma".
3334 * In other words, plug in "ma" in "uset".
3335 * The result contains sets that live in the same spaces as the sets of "uset"
3336 * with space equal to the target space of "ma",
3337 * except that the space has been replaced by the domain space of "ma".
3339 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3340 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3342 return isl_union_set_preimage_pw_multi_aff(uset,
3343 isl_pw_multi_aff_from_multi_aff(ma));
3346 /* Internal data structure for preimage_multi_pw_aff.
3348 * "mpa" is the function under which the preimage should be taken.
3349 * "space" is the space of "mpa".
3350 * "res" collects the results.
3351 * "fn" computes the preimage for a given map.
3352 * "match" returns true if "fn" can be called.
3354 struct isl_union_map_preimage_mpa_data {
3355 isl_space *space;
3356 isl_multi_pw_aff *mpa;
3357 isl_union_map *res;
3358 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3359 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3360 __isl_take isl_multi_pw_aff *mpa);
3363 /* Call data->fn to compute the preimage of the domain or range of *entry
3364 * under the function represented by data->mpa, provided the domain/range
3365 * space of *entry matches the target space of data->mpa
3366 * (as given by data->match), and add the result to data->res.
3368 static isl_stat preimage_mpa_entry(void **entry, void *user)
3370 int m;
3371 isl_map *map = *entry;
3372 struct isl_union_map_preimage_mpa_data *data = user;
3373 isl_bool empty;
3375 m = data->match(map, data->space);
3376 if (m < 0)
3377 return isl_stat_error;
3378 if (!m)
3379 return isl_stat_ok;
3381 map = isl_map_copy(map);
3382 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3384 empty = isl_map_is_empty(map);
3385 if (empty < 0 || empty) {
3386 isl_map_free(map);
3387 return empty < 0 ? isl_stat_error : isl_stat_ok;
3390 data->res = isl_union_map_add_map(data->res, map);
3392 return isl_stat_ok;
3395 /* Compute the preimage of the domain or range of "umap" under the function
3396 * represented by "mpa".
3397 * In other words, plug in "mpa" in the domain or range of "umap".
3398 * The function "fn" performs the actual preimage computation on a map,
3399 * while "match" determines to which maps the function should be applied.
3401 static __isl_give isl_union_map *preimage_multi_pw_aff(
3402 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3403 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3404 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3405 __isl_take isl_multi_pw_aff *mpa))
3407 isl_ctx *ctx;
3408 isl_space *space;
3409 struct isl_union_map_preimage_mpa_data data;
3411 umap = isl_union_map_align_params(umap,
3412 isl_multi_pw_aff_get_space(mpa));
3413 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3415 if (!umap || !mpa)
3416 goto error;
3418 ctx = isl_union_map_get_ctx(umap);
3419 space = isl_union_map_get_space(umap);
3420 data.space = isl_multi_pw_aff_get_space(mpa);
3421 data.mpa = mpa;
3422 data.res = isl_union_map_alloc(space, umap->table.n);
3423 data.match = match;
3424 data.fn = fn;
3425 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3426 &data) < 0)
3427 data.res = isl_union_map_free(data.res);
3429 isl_space_free(data.space);
3430 isl_union_map_free(umap);
3431 isl_multi_pw_aff_free(mpa);
3432 return data.res;
3433 error:
3434 isl_union_map_free(umap);
3435 isl_multi_pw_aff_free(mpa);
3436 return NULL;
3439 /* Compute the preimage of the domain of "umap" under the function
3440 * represented by "mpa".
3441 * In other words, plug in "mpa" in the domain of "umap".
3442 * The result contains maps that live in the same spaces as the maps of "umap"
3443 * with domain space equal to the target space of "mpa",
3444 * except that the domain has been replaced by the domain space of "mpa".
3446 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3447 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3449 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3450 &isl_map_preimage_domain_multi_pw_aff);
3453 /* Internal data structure for preimage_upma.
3455 * "umap" is the map of which the preimage should be computed.
3456 * "res" collects the results.
3457 * "fn" computes the preimage for a given piecewise multi-affine function.
3459 struct isl_union_map_preimage_upma_data {
3460 isl_union_map *umap;
3461 isl_union_map *res;
3462 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3463 __isl_take isl_pw_multi_aff *pma);
3466 /* Call data->fn to compute the preimage of the domain or range of data->umap
3467 * under the function represented by pma and add the result to data->res.
3469 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3471 struct isl_union_map_preimage_upma_data *data = user;
3472 isl_union_map *umap;
3474 umap = isl_union_map_copy(data->umap);
3475 umap = data->fn(umap, pma);
3476 data->res = isl_union_map_union(data->res, umap);
3478 return data->res ? isl_stat_ok : isl_stat_error;
3481 /* Compute the preimage of the domain or range of "umap" under the function
3482 * represented by "upma".
3483 * In other words, plug in "upma" in the domain or range of "umap".
3484 * The function "fn" performs the actual preimage computation
3485 * on a piecewise multi-affine function.
3487 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3488 __isl_take isl_union_map *umap,
3489 __isl_take isl_union_pw_multi_aff *upma,
3490 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3491 __isl_take isl_pw_multi_aff *pma))
3493 struct isl_union_map_preimage_upma_data data;
3495 data.umap = umap;
3496 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3497 data.fn = fn;
3498 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3499 &preimage_upma, &data) < 0)
3500 data.res = isl_union_map_free(data.res);
3502 isl_union_map_free(umap);
3503 isl_union_pw_multi_aff_free(upma);
3505 return data.res;
3508 /* Compute the preimage of the domain of "umap" under the function
3509 * represented by "upma".
3510 * In other words, plug in "upma" in the domain of "umap".
3511 * The result contains maps that live in the same spaces as the maps of "umap"
3512 * with domain space equal to one of the target spaces of "upma",
3513 * except that the domain has been replaced by one of the the domain spaces that
3514 * corresponds to that target space of "upma".
3516 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3517 __isl_take isl_union_map *umap,
3518 __isl_take isl_union_pw_multi_aff *upma)
3520 return preimage_union_pw_multi_aff(umap, upma,
3521 &isl_union_map_preimage_domain_pw_multi_aff);
3524 /* Compute the preimage of the range of "umap" under the function
3525 * represented by "upma".
3526 * In other words, plug in "upma" in the range of "umap".
3527 * The result contains maps that live in the same spaces as the maps of "umap"
3528 * with range space equal to one of the target spaces of "upma",
3529 * except that the range has been replaced by one of the the domain spaces that
3530 * corresponds to that target space of "upma".
3532 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3533 __isl_take isl_union_map *umap,
3534 __isl_take isl_union_pw_multi_aff *upma)
3536 return preimage_union_pw_multi_aff(umap, upma,
3537 &isl_union_map_preimage_range_pw_multi_aff);
3540 /* Compute the preimage of "uset" under the function represented by "upma".
3541 * In other words, plug in "upma" in the range of "uset".
3542 * The result contains sets that live in the same spaces as the sets of "uset"
3543 * with space equal to one of the target spaces of "upma",
3544 * except that the space has been replaced by one of the the domain spaces that
3545 * corresponds to that target space of "upma".
3547 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3548 __isl_take isl_union_set *uset,
3549 __isl_take isl_union_pw_multi_aff *upma)
3551 return preimage_union_pw_multi_aff(uset, upma,
3552 &isl_union_set_preimage_pw_multi_aff);
3555 /* Reset the user pointer on all identifiers of parameters and tuples
3556 * of the spaces of "umap".
3558 __isl_give isl_union_map *isl_union_map_reset_user(
3559 __isl_take isl_union_map *umap)
3561 umap = isl_union_map_cow(umap);
3562 if (!umap)
3563 return NULL;
3564 umap->dim = isl_space_reset_user(umap->dim);
3565 if (!umap->dim)
3566 return isl_union_map_free(umap);
3567 return total(umap, &isl_map_reset_user);
3570 /* Reset the user pointer on all identifiers of parameters and tuples
3571 * of the spaces of "uset".
3573 __isl_give isl_union_set *isl_union_set_reset_user(
3574 __isl_take isl_union_set *uset)
3576 return isl_union_map_reset_user(uset);
3579 /* Remove all existentially quantified variables and integer divisions
3580 * from "umap" using Fourier-Motzkin elimination.
3582 __isl_give isl_union_map *isl_union_map_remove_divs(
3583 __isl_take isl_union_map *umap)
3585 return total(umap, &isl_map_remove_divs);
3588 /* Remove all existentially quantified variables and integer divisions
3589 * from "uset" using Fourier-Motzkin elimination.
3591 __isl_give isl_union_set *isl_union_set_remove_divs(
3592 __isl_take isl_union_set *uset)
3594 return isl_union_map_remove_divs(uset);
3597 /* Internal data structure for isl_union_map_project_out.
3598 * "type", "first" and "n" are the arguments for the isl_map_project_out
3599 * call.
3600 * "res" collects the results.
3602 struct isl_union_map_project_out_data {
3603 enum isl_dim_type type;
3604 unsigned first;
3605 unsigned n;
3607 isl_union_map *res;
3610 /* Turn the data->n dimensions of type data->type, starting at data->first
3611 * into existentially quantified variables and add the result to data->res.
3613 static isl_stat project_out(__isl_take isl_map *map, void *user)
3615 struct isl_union_map_project_out_data *data = user;
3617 map = isl_map_project_out(map, data->type, data->first, data->n);
3618 data->res = isl_union_map_add_map(data->res, map);
3620 return isl_stat_ok;
3623 /* Turn the "n" dimensions of type "type", starting at "first"
3624 * into existentially quantified variables.
3625 * Since the space of an isl_union_map only contains parameters,
3626 * type is required to be equal to isl_dim_param.
3628 __isl_give isl_union_map *isl_union_map_project_out(
3629 __isl_take isl_union_map *umap,
3630 enum isl_dim_type type, unsigned first, unsigned n)
3632 isl_space *space;
3633 struct isl_union_map_project_out_data data = { type, first, n };
3635 if (!umap)
3636 return NULL;
3638 if (type != isl_dim_param)
3639 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3640 "can only project out parameters",
3641 return isl_union_map_free(umap));
3643 space = isl_union_map_get_space(umap);
3644 space = isl_space_drop_dims(space, type, first, n);
3645 data.res = isl_union_map_empty(space);
3646 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3647 data.res = isl_union_map_free(data.res);
3649 isl_union_map_free(umap);
3651 return data.res;
3654 /* Turn the "n" dimensions of type "type", starting at "first"
3655 * into existentially quantified variables.
3656 * Since the space of an isl_union_set only contains parameters,
3657 * "type" is required to be equal to isl_dim_param.
3659 __isl_give isl_union_set *isl_union_set_project_out(
3660 __isl_take isl_union_set *uset,
3661 enum isl_dim_type type, unsigned first, unsigned n)
3663 return isl_union_map_project_out(uset, type, first, n);
3666 /* Internal data structure for isl_union_map_involves_dims.
3667 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3669 struct isl_union_map_involves_dims_data {
3670 unsigned first;
3671 unsigned n;
3674 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3676 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3678 struct isl_union_map_involves_dims_data *data = user;
3679 isl_bool involves;
3681 involves = isl_map_involves_dims(map,
3682 isl_dim_param, data->first, data->n);
3683 if (involves < 0)
3684 return isl_bool_error;
3685 return !involves;
3688 /* Does "umap" involve any of the n parameters starting at first?
3689 * "type" is required to be set to isl_dim_param.
3691 * "umap" involves any of those parameters if any of its maps
3692 * involve the parameters. In other words, "umap" does not
3693 * involve any of the parameters if all its maps to not
3694 * involve the parameters.
3696 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3697 enum isl_dim_type type, unsigned first, unsigned n)
3699 struct isl_union_map_involves_dims_data data = { first, n };
3700 isl_bool excludes;
3702 if (type != isl_dim_param)
3703 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3704 "can only reference parameters", return isl_bool_error);
3706 excludes = union_map_forall_user(umap, &map_excludes, &data);
3708 if (excludes < 0)
3709 return isl_bool_error;
3711 return !excludes;
3714 /* Internal data structure for isl_union_map_reset_range_space.
3715 * "range" is the space from which to set the range space.
3716 * "res" collects the results.
3718 struct isl_union_map_reset_range_space_data {
3719 isl_space *range;
3720 isl_union_map *res;
3723 /* Replace the range space of "map" by the range space of data->range and
3724 * add the result to data->res.
3726 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3728 struct isl_union_map_reset_range_space_data *data = user;
3729 isl_space *space;
3731 space = isl_map_get_space(map);
3732 space = isl_space_domain(space);
3733 space = isl_space_extend_domain_with_range(space,
3734 isl_space_copy(data->range));
3735 map = isl_map_reset_space(map, space);
3736 data->res = isl_union_map_add_map(data->res, map);
3738 return data->res ? isl_stat_ok : isl_stat_error;
3741 /* Replace the range space of all the maps in "umap" by
3742 * the range space of "space".
3744 * This assumes that all maps have the same output dimension.
3745 * This function should therefore not be made publicly available.
3747 * Since the spaces of the maps change, so do their hash value.
3748 * We therefore need to create a new isl_union_map.
3750 __isl_give isl_union_map *isl_union_map_reset_range_space(
3751 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3753 struct isl_union_map_reset_range_space_data data = { space };
3755 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3756 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3757 data.res = isl_union_map_free(data.res);
3759 isl_space_free(space);
3760 isl_union_map_free(umap);
3761 return data.res;
3764 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3765 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3766 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3767 * to the domain and the range of each map.
3768 * "res" collects the results.
3770 struct isl_union_order_at_data {
3771 isl_multi_union_pw_aff *mupa;
3772 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3773 __isl_take isl_multi_pw_aff *mpa2);
3774 isl_union_map *res;
3777 /* Intersect "map" with the result of applying data->order to
3778 * the functions in data->mupa that apply to the domain and the range
3779 * of "map" and add the result to data->res.
3781 static isl_stat order_at(__isl_take isl_map *map, void *user)
3783 struct isl_union_order_at_data *data = user;
3784 isl_space *space;
3785 isl_multi_pw_aff *mpa1, *mpa2;
3786 isl_map *order;
3788 space = isl_space_domain(isl_map_get_space(map));
3789 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3790 space = isl_space_range(isl_map_get_space(map));
3791 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3792 order = data->order(mpa1, mpa2);
3793 map = isl_map_intersect(map, order);
3794 data->res = isl_union_map_add_map(data->res, map);
3796 return data->res ? isl_stat_ok : isl_stat_error;
3799 /* Intersect each map in "umap" with the result of calling "order"
3800 * on the functions is "mupa" that apply to the domain and the range
3801 * of the map.
3803 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3804 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3805 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3806 __isl_take isl_multi_pw_aff *mpa2))
3808 struct isl_union_order_at_data data;
3810 umap = isl_union_map_align_params(umap,
3811 isl_multi_union_pw_aff_get_space(mupa));
3812 mupa = isl_multi_union_pw_aff_align_params(mupa,
3813 isl_union_map_get_space(umap));
3814 data.mupa = mupa;
3815 data.order = order;
3816 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3817 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3818 data.res = isl_union_map_free(data.res);
3820 isl_multi_union_pw_aff_free(mupa);
3821 isl_union_map_free(umap);
3822 return data.res;
3825 /* Return the subset of "umap" where the domain and the range
3826 * have equal "mupa" values.
3828 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3829 __isl_take isl_union_map *umap,
3830 __isl_take isl_multi_union_pw_aff *mupa)
3832 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3833 &isl_multi_pw_aff_eq_map);
3836 /* Return the subset of "umap" where the domain has a lexicographically
3837 * smaller "mupa" value than the range.
3839 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3840 __isl_take isl_union_map *umap,
3841 __isl_take isl_multi_union_pw_aff *mupa)
3843 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3844 &isl_multi_pw_aff_lex_lt_map);
3847 /* Return the subset of "umap" where the domain has a lexicographically
3848 * greater "mupa" value than the range.
3850 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3851 __isl_take isl_union_map *umap,
3852 __isl_take isl_multi_union_pw_aff *mupa)
3854 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3855 &isl_multi_pw_aff_lex_gt_map);
3858 /* Return the union of the elements in the list "list".
3860 __isl_give isl_union_set *isl_union_set_list_union(
3861 __isl_take isl_union_set_list *list)
3863 int i, n;
3864 isl_ctx *ctx;
3865 isl_space *space;
3866 isl_union_set *res;
3868 if (!list)
3869 return NULL;
3871 ctx = isl_union_set_list_get_ctx(list);
3872 space = isl_space_params_alloc(ctx, 0);
3873 res = isl_union_set_empty(space);
3875 n = isl_union_set_list_n_union_set(list);
3876 for (i = 0; i < n; ++i) {
3877 isl_union_set *uset_i;
3879 uset_i = isl_union_set_list_get_union_set(list, i);
3880 res = isl_union_set_union(res, uset_i);
3883 isl_union_set_list_free(list);
3884 return res;
3887 /* Update *hash with the hash value of "map".
3889 static isl_stat add_hash(__isl_take isl_map *map, void *user)
3891 uint32_t *hash = user;
3892 uint32_t map_hash;
3894 map_hash = isl_map_get_hash(map);
3895 isl_hash_hash(*hash, map_hash);
3897 isl_map_free(map);
3898 return isl_stat_ok;
3901 /* Return a hash value that digests "umap".
3903 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3905 uint32_t hash;
3907 if (!umap)
3908 return 0;
3910 hash = isl_hash_init();
3911 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3912 return 0;
3914 return hash;
3917 /* Return a hash value that digests "uset".
3919 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
3921 return isl_union_map_get_hash(uset);
3924 /* Add the number of basic sets in "set" to "n".
3926 static isl_stat add_n(__isl_take isl_set *set, void *user)
3928 int *n = user;
3930 *n += isl_set_n_basic_set(set);
3931 isl_set_free(set);
3933 return isl_stat_ok;
3936 /* Return the total number of basic sets in "uset".
3938 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
3940 int n = 0;
3942 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
3943 return -1;
3945 return n;
3948 /* Add the basic sets in "set" to "list".
3950 static isl_stat add_list(__isl_take isl_set *set, void *user)
3952 isl_basic_set_list **list = user;
3953 isl_basic_set_list *list_i;
3955 list_i = isl_set_get_basic_set_list(set);
3956 *list = isl_basic_set_list_concat(*list, list_i);
3957 isl_set_free(set);
3959 if (!*list)
3960 return isl_stat_error;
3961 return isl_stat_ok;
3964 /* Return a list containing all the basic sets in "uset".
3966 * First construct a list of the appropriate size and
3967 * then add all the elements.
3969 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
3970 __isl_keep isl_union_set *uset)
3972 int n;
3973 isl_ctx *ctx;
3974 isl_basic_set_list *list;
3976 if (!uset)
3977 return NULL;
3978 ctx = isl_union_set_get_ctx(uset);
3979 n = isl_union_set_n_basic_set(uset);
3980 if (n < 0)
3981 return NULL;
3982 list = isl_basic_set_list_alloc(ctx, n);
3983 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
3984 list = isl_basic_set_list_free(list);
3986 return list;
3989 /* Internal data structure for isl_union_map_remove_map_if.
3990 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
3992 struct isl_union_map_remove_map_if_data {
3993 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
3994 void *user;
3997 /* isl_un_op_control filter that negates the result of data->fn
3998 * called on "map".
4000 static isl_bool not(__isl_keep isl_map *map, void *user)
4002 struct isl_union_map_remove_map_if_data *data = user;
4004 return isl_bool_not(data->fn(map, data->user));
4007 /* Dummy isl_un_op_control transformation callback that
4008 * simply returns the input.
4010 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4012 return map;
4015 /* Call "fn" on every map in "umap" and remove those maps
4016 * for which the callback returns true.
4018 * Use un_op to keep only those maps that are not filtered out,
4019 * applying an identity transformation on them.
4021 __isl_give isl_union_map *isl_union_map_remove_map_if(
4022 __isl_take isl_union_map *umap,
4023 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4025 struct isl_union_map_remove_map_if_data data = { fn, user };
4026 struct isl_un_op_control control = {
4027 .filter = &not,
4028 .filter_user = &data,
4029 .fn_map = &map_id,
4031 return un_op(umap, &control);